]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - ffserver.c
remove return 1, we don't change state, we are still sending data
[frescor/ffmpeg.git] / ffserver.c
1 /*
2  * Multiple format streaming server
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "config.h"
23 #ifndef HAVE_CLOSESOCKET
24 #define closesocket close
25 #endif
26 #include <string.h>
27 #include <stdlib.h>
28 #include "libavutil/random.h"
29 #include "libavutil/avstring.h"
30 #include "libavformat/avformat.h"
31 #include "libavformat/network.h"
32 #include "libavformat/os_support.h"
33 #include "libavformat/rtp.h"
34 #include "libavformat/rtsp.h"
35 #include "libavcodec/opt.h"
36 #include <stdarg.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <sys/ioctl.h>
40 #ifdef HAVE_POLL_H
41 #include <poll.h>
42 #endif
43 #include <errno.h>
44 #include <sys/time.h>
45 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
46 #include <time.h>
47 #include <sys/wait.h>
48 #include <signal.h>
49 #ifdef HAVE_DLFCN_H
50 #include <dlfcn.h>
51 #endif
52
53 #include "cmdutils.h"
54
55 #undef exit
56
57 const char program_name[] = "FFserver";
58 const int program_birth_year = 2000;
59
60 static const OptionDef options[];
61
62 /* maximum number of simultaneous HTTP connections */
63 #define HTTP_MAX_CONNECTIONS 2000
64
65 enum HTTPState {
66     HTTPSTATE_WAIT_REQUEST,
67     HTTPSTATE_SEND_HEADER,
68     HTTPSTATE_SEND_DATA_HEADER,
69     HTTPSTATE_SEND_DATA,          /* sending TCP or UDP data */
70     HTTPSTATE_SEND_DATA_TRAILER,
71     HTTPSTATE_RECEIVE_DATA,
72     HTTPSTATE_WAIT_FEED,          /* wait for data from the feed */
73     HTTPSTATE_READY,
74
75     RTSPSTATE_WAIT_REQUEST,
76     RTSPSTATE_SEND_REPLY,
77     RTSPSTATE_SEND_PACKET,
78 };
79
80 static const char *http_state[] = {
81     "HTTP_WAIT_REQUEST",
82     "HTTP_SEND_HEADER",
83
84     "SEND_DATA_HEADER",
85     "SEND_DATA",
86     "SEND_DATA_TRAILER",
87     "RECEIVE_DATA",
88     "WAIT_FEED",
89     "READY",
90
91     "RTSP_WAIT_REQUEST",
92     "RTSP_SEND_REPLY",
93     "RTSP_SEND_PACKET",
94 };
95
96 #define IOBUFFER_INIT_SIZE 8192
97
98 /* timeouts are in ms */
99 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
100 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
101
102 #define SYNC_TIMEOUT (10 * 1000)
103
104 typedef struct {
105     int64_t count1, count2;
106     int64_t time1, time2;
107 } DataRateData;
108
109 /* context associated with one connection */
110 typedef struct HTTPContext {
111     enum HTTPState state;
112     int fd; /* socket file descriptor */
113     struct sockaddr_in from_addr; /* origin */
114     struct pollfd *poll_entry; /* used when polling */
115     int64_t timeout;
116     uint8_t *buffer_ptr, *buffer_end;
117     int http_error;
118     int post;
119     struct HTTPContext *next;
120     int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
121     int64_t data_count;
122     /* feed input */
123     int feed_fd;
124     /* input format handling */
125     AVFormatContext *fmt_in;
126     int64_t start_time;            /* In milliseconds - this wraps fairly often */
127     int64_t first_pts;            /* initial pts value */
128     int64_t cur_pts;             /* current pts value from the stream in us */
129     int64_t cur_frame_duration;  /* duration of the current frame in us */
130     int cur_frame_bytes;       /* output frame size, needed to compute
131                                   the time at which we send each
132                                   packet */
133     int pts_stream_index;        /* stream we choose as clock reference */
134     int64_t cur_clock;           /* current clock reference value in us */
135     /* output format handling */
136     struct FFStream *stream;
137     /* -1 is invalid stream */
138     int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
139     int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */
140     int switch_pending;
141     AVFormatContext fmt_ctx; /* instance of FFStream for one user */
142     int last_packet_sent; /* true if last data packet was sent */
143     int suppress_log;
144     DataRateData datarate;
145     int wmp_client_id;
146     char protocol[16];
147     char method[16];
148     char url[128];
149     int buffer_size;
150     uint8_t *buffer;
151     int is_packetized; /* if true, the stream is packetized */
152     int packet_stream_index; /* current stream for output in state machine */
153
154     /* RTSP state specific */
155     uint8_t *pb_buffer; /* XXX: use that in all the code */
156     ByteIOContext *pb;
157     int seq; /* RTSP sequence number */
158
159     /* RTP state specific */
160     enum RTSPProtocol rtp_protocol;
161     char session_id[32]; /* session id */
162     AVFormatContext *rtp_ctx[MAX_STREAMS];
163
164     /* RTP/UDP specific */
165     URLContext *rtp_handles[MAX_STREAMS];
166
167     /* RTP/TCP specific */
168     struct HTTPContext *rtsp_c;
169     uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
170 } HTTPContext;
171
172 /* each generated stream is described here */
173 enum StreamType {
174     STREAM_TYPE_LIVE,
175     STREAM_TYPE_STATUS,
176     STREAM_TYPE_REDIRECT,
177 };
178
179 enum IPAddressAction {
180     IP_ALLOW = 1,
181     IP_DENY,
182 };
183
184 typedef struct IPAddressACL {
185     struct IPAddressACL *next;
186     enum IPAddressAction action;
187     /* These are in host order */
188     struct in_addr first;
189     struct in_addr last;
190 } IPAddressACL;
191
192 /* description of each stream of the ffserver.conf file */
193 typedef struct FFStream {
194     enum StreamType stream_type;
195     char filename[1024];     /* stream filename */
196     struct FFStream *feed;   /* feed we are using (can be null if
197                                 coming from file) */
198     AVFormatParameters *ap_in; /* input parameters */
199     AVInputFormat *ifmt;       /* if non NULL, force input format */
200     AVOutputFormat *fmt;
201     IPAddressACL *acl;
202     int nb_streams;
203     int prebuffer;      /* Number of millseconds early to start */
204     int64_t max_time;      /* Number of milliseconds to run */
205     int send_on_key;
206     AVStream *streams[MAX_STREAMS];
207     int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
208     char feed_filename[1024]; /* file name of the feed storage, or
209                                  input file name for a stream */
210     char author[512];
211     char title[512];
212     char copyright[512];
213     char comment[512];
214     pid_t pid;  /* Of ffmpeg process */
215     time_t pid_start;  /* Of ffmpeg process */
216     char **child_argv;
217     struct FFStream *next;
218     unsigned bandwidth; /* bandwidth, in kbits/s */
219     /* RTSP options */
220     char *rtsp_option;
221     /* multicast specific */
222     int is_multicast;
223     struct in_addr multicast_ip;
224     int multicast_port; /* first port used for multicast */
225     int multicast_ttl;
226     int loop; /* if true, send the stream in loops (only meaningful if file) */
227
228     /* feed specific */
229     int feed_opened;     /* true if someone is writing to the feed */
230     int is_feed;         /* true if it is a feed */
231     int readonly;        /* True if writing is prohibited to the file */
232     int conns_served;
233     int64_t bytes_served;
234     int64_t feed_max_size;      /* maximum storage size, zero means unlimited */
235     int64_t feed_write_index;   /* current write position in feed (it wraps around) */
236     int64_t feed_size;          /* current size of feed */
237     struct FFStream *next_feed;
238 } FFStream;
239
240 typedef struct FeedData {
241     long long data_count;
242     float avg_frame_size;   /* frame size averaged over last frames with exponential mean */
243 } FeedData;
244
245 static struct sockaddr_in my_http_addr;
246 static struct sockaddr_in my_rtsp_addr;
247
248 static char logfilename[1024];
249 static HTTPContext *first_http_ctx;
250 static FFStream *first_feed;   /* contains only feeds */
251 static FFStream *first_stream; /* contains all streams, including feeds */
252
253 static void new_connection(int server_fd, int is_rtsp);
254 static void close_connection(HTTPContext *c);
255
256 /* HTTP handling */
257 static int handle_connection(HTTPContext *c);
258 static int http_parse_request(HTTPContext *c);
259 static int http_send_data(HTTPContext *c);
260 static void compute_status(HTTPContext *c);
261 static int open_input_stream(HTTPContext *c, const char *info);
262 static int http_start_receive_data(HTTPContext *c);
263 static int http_receive_data(HTTPContext *c);
264
265 /* RTSP handling */
266 static int rtsp_parse_request(HTTPContext *c);
267 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
268 static void rtsp_cmd_options(HTTPContext *c, const char *url);
269 static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPHeader *h);
270 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h);
271 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h);
272 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h);
273
274 /* SDP handling */
275 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
276                                    struct in_addr my_ip);
277
278 /* RTP handling */
279 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
280                                        FFStream *stream, const char *session_id,
281                                        enum RTSPProtocol rtp_protocol);
282 static int rtp_new_av_stream(HTTPContext *c,
283                              int stream_index, struct sockaddr_in *dest_addr,
284                              HTTPContext *rtsp_c);
285
286 static const char *my_program_name;
287 static const char *my_program_dir;
288
289 static const char *config_filename;
290 static int ffserver_debug;
291 static int ffserver_daemon;
292 static int no_launch;
293 static int need_to_start_children;
294
295 static int nb_max_connections = 5;
296 static int nb_connections;
297
298 static uint64_t max_bandwidth = 1000;
299 static uint64_t current_bandwidth;
300
301 static int64_t cur_time;           // Making this global saves on passing it around everywhere
302
303 static AVRandomState random_state;
304
305 static FILE *logfile = NULL;
306
307 static char *ctime1(char *buf2)
308 {
309     time_t ti;
310     char *p;
311
312     ti = time(NULL);
313     p = ctime(&ti);
314     strcpy(buf2, p);
315     p = buf2 + strlen(p) - 1;
316     if (*p == '\n')
317         *p = '\0';
318     return buf2;
319 }
320
321 static void http_vlog(const char *fmt, va_list vargs)
322 {
323     static int print_prefix = 1;
324     if (logfile) {
325         if (print_prefix) {
326             char buf[32];
327             ctime1(buf);
328             fprintf(logfile, "%s ", buf);
329         }
330         print_prefix = strstr(fmt, "\n") != NULL;
331         vfprintf(logfile, fmt, vargs);
332         fflush(logfile);
333     }
334 }
335
336 void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
337 {
338     va_list vargs;
339     va_start(vargs, fmt);
340     http_vlog(fmt, vargs);
341     va_end(vargs);
342 }
343
344 static void http_av_log(void *ptr, int level, const char *fmt, va_list vargs)
345 {
346     static int print_prefix = 1;
347     AVClass *avc = ptr ? *(AVClass**)ptr : NULL;
348     if (level > av_log_level)
349         return;
350     if (print_prefix && avc)
351         http_log("[%s @ %p]", avc->item_name(ptr), avc);
352     print_prefix = strstr(fmt, "\n") != NULL;
353     http_vlog(fmt, vargs);
354 }
355
356 static void log_connection(HTTPContext *c)
357 {
358     if (c->suppress_log)
359         return;
360
361     http_log("%s - - [%s] \"%s %s\" %d %"PRId64"\n",
362              inet_ntoa(c->from_addr.sin_addr), c->method, c->url,
363              c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
364 }
365
366 static void update_datarate(DataRateData *drd, int64_t count)
367 {
368     if (!drd->time1 && !drd->count1) {
369         drd->time1 = drd->time2 = cur_time;
370         drd->count1 = drd->count2 = count;
371     } else if (cur_time - drd->time2 > 5000) {
372         drd->time1 = drd->time2;
373         drd->count1 = drd->count2;
374         drd->time2 = cur_time;
375         drd->count2 = count;
376     }
377 }
378
379 /* In bytes per second */
380 static int compute_datarate(DataRateData *drd, int64_t count)
381 {
382     if (cur_time == drd->time1)
383         return 0;
384
385     return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
386 }
387
388
389 static void start_children(FFStream *feed)
390 {
391     if (no_launch)
392         return;
393
394     for (; feed; feed = feed->next) {
395         if (feed->child_argv && !feed->pid) {
396             feed->pid_start = time(0);
397
398             feed->pid = fork();
399
400             if (feed->pid < 0) {
401                 http_log("Unable to create children\n");
402                 exit(1);
403             }
404             if (!feed->pid) {
405                 /* In child */
406                 char pathname[1024];
407                 char *slash;
408                 int i;
409
410                 for (i = 3; i < 256; i++)
411                     close(i);
412
413                 if (!ffserver_debug) {
414                     i = open("/dev/null", O_RDWR);
415                     if (i != -1) {
416                         dup2(i, 0);
417                         dup2(i, 1);
418                         dup2(i, 2);
419                         close(i);
420                     }
421                 }
422
423                 av_strlcpy(pathname, my_program_name, sizeof(pathname));
424
425                 slash = strrchr(pathname, '/');
426                 if (!slash)
427                     slash = pathname;
428                 else
429                     slash++;
430                 strcpy(slash, "ffmpeg");
431
432                 /* This is needed to make relative pathnames work */
433                 chdir(my_program_dir);
434
435                 signal(SIGPIPE, SIG_DFL);
436
437                 execvp(pathname, feed->child_argv);
438
439                 _exit(1);
440             }
441         }
442     }
443 }
444
445 /* open a listening socket */
446 static int socket_open_listen(struct sockaddr_in *my_addr)
447 {
448     int server_fd, tmp;
449
450     server_fd = socket(AF_INET,SOCK_STREAM,0);
451     if (server_fd < 0) {
452         perror ("socket");
453         return -1;
454     }
455
456     tmp = 1;
457     setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
458
459     if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
460         char bindmsg[32];
461         snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
462         perror (bindmsg);
463         closesocket(server_fd);
464         return -1;
465     }
466
467     if (listen (server_fd, 5) < 0) {
468         perror ("listen");
469         closesocket(server_fd);
470         return -1;
471     }
472     ff_socket_nonblock(server_fd, 1);
473
474     return server_fd;
475 }
476
477 /* start all multicast streams */
478 static void start_multicast(void)
479 {
480     FFStream *stream;
481     char session_id[32];
482     HTTPContext *rtp_c;
483     struct sockaddr_in dest_addr;
484     int default_port, stream_index;
485
486     default_port = 6000;
487     for(stream = first_stream; stream != NULL; stream = stream->next) {
488         if (stream->is_multicast) {
489             /* open the RTP connection */
490             snprintf(session_id, sizeof(session_id), "%08x%08x",
491                      av_random(&random_state), av_random(&random_state));
492
493             /* choose a port if none given */
494             if (stream->multicast_port == 0) {
495                 stream->multicast_port = default_port;
496                 default_port += 100;
497             }
498
499             dest_addr.sin_family = AF_INET;
500             dest_addr.sin_addr = stream->multicast_ip;
501             dest_addr.sin_port = htons(stream->multicast_port);
502
503             rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
504                                        RTSP_PROTOCOL_RTP_UDP_MULTICAST);
505             if (!rtp_c)
506                 continue;
507
508             if (open_input_stream(rtp_c, "") < 0) {
509                 http_log("Could not open input stream for stream '%s'\n",
510                          stream->filename);
511                 continue;
512             }
513
514             /* open each RTP stream */
515             for(stream_index = 0; stream_index < stream->nb_streams;
516                 stream_index++) {
517                 dest_addr.sin_port = htons(stream->multicast_port +
518                                            2 * stream_index);
519                 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) < 0) {
520                     http_log("Could not open output stream '%s/streamid=%d'\n",
521                              stream->filename, stream_index);
522                     exit(1);
523                 }
524             }
525
526             /* change state to send data */
527             rtp_c->state = HTTPSTATE_SEND_DATA;
528         }
529     }
530 }
531
532 /* main loop of the http server */
533 static int http_server(void)
534 {
535     int server_fd = 0, rtsp_server_fd = 0;
536     int ret, delay, delay1;
537     struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 2], *poll_entry;
538     HTTPContext *c, *c_next;
539
540     if (my_http_addr.sin_port) {
541         server_fd = socket_open_listen(&my_http_addr);
542         if (server_fd < 0)
543             return -1;
544     }
545
546     if (my_rtsp_addr.sin_port) {
547         rtsp_server_fd = socket_open_listen(&my_rtsp_addr);
548         if (rtsp_server_fd < 0)
549             return -1;
550     }
551
552     if (!rtsp_server_fd && !server_fd) {
553         http_log("HTTP and RTSP disabled.\n");
554         return -1;
555     }
556
557     http_log("ffserver started.\n");
558
559     start_children(first_feed);
560
561     first_http_ctx = NULL;
562     nb_connections = 0;
563
564     start_multicast();
565
566     for(;;) {
567         poll_entry = poll_table;
568         if (server_fd) {
569             poll_entry->fd = server_fd;
570             poll_entry->events = POLLIN;
571             poll_entry++;
572         }
573         if (rtsp_server_fd) {
574             poll_entry->fd = rtsp_server_fd;
575             poll_entry->events = POLLIN;
576             poll_entry++;
577         }
578
579         /* wait for events on each HTTP handle */
580         c = first_http_ctx;
581         delay = 1000;
582         while (c != NULL) {
583             int fd;
584             fd = c->fd;
585             switch(c->state) {
586             case HTTPSTATE_SEND_HEADER:
587             case RTSPSTATE_SEND_REPLY:
588             case RTSPSTATE_SEND_PACKET:
589                 c->poll_entry = poll_entry;
590                 poll_entry->fd = fd;
591                 poll_entry->events = POLLOUT;
592                 poll_entry++;
593                 break;
594             case HTTPSTATE_SEND_DATA_HEADER:
595             case HTTPSTATE_SEND_DATA:
596             case HTTPSTATE_SEND_DATA_TRAILER:
597                 if (!c->is_packetized) {
598                     /* for TCP, we output as much as we can (may need to put a limit) */
599                     c->poll_entry = poll_entry;
600                     poll_entry->fd = fd;
601                     poll_entry->events = POLLOUT;
602                     poll_entry++;
603                 } else {
604                     /* when ffserver is doing the timing, we work by
605                        looking at which packet need to be sent every
606                        10 ms */
607                     delay1 = 10; /* one tick wait XXX: 10 ms assumed */
608                     if (delay1 < delay)
609                         delay = delay1;
610                 }
611                 break;
612             case HTTPSTATE_WAIT_REQUEST:
613             case HTTPSTATE_RECEIVE_DATA:
614             case HTTPSTATE_WAIT_FEED:
615             case RTSPSTATE_WAIT_REQUEST:
616                 /* need to catch errors */
617                 c->poll_entry = poll_entry;
618                 poll_entry->fd = fd;
619                 poll_entry->events = POLLIN;/* Maybe this will work */
620                 poll_entry++;
621                 break;
622             default:
623                 c->poll_entry = NULL;
624                 break;
625             }
626             c = c->next;
627         }
628
629         /* wait for an event on one connection. We poll at least every
630            second to handle timeouts */
631         do {
632             ret = poll(poll_table, poll_entry - poll_table, delay);
633             if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
634                 ff_neterrno() != FF_NETERROR(EINTR))
635                 return -1;
636         } while (ret < 0);
637
638         cur_time = av_gettime() / 1000;
639
640         if (need_to_start_children) {
641             need_to_start_children = 0;
642             start_children(first_feed);
643         }
644
645         /* now handle the events */
646         for(c = first_http_ctx; c != NULL; c = c_next) {
647             c_next = c->next;
648             if (handle_connection(c) < 0) {
649                 /* close and free the connection */
650                 log_connection(c);
651                 close_connection(c);
652             }
653         }
654
655         poll_entry = poll_table;
656         if (server_fd) {
657             /* new HTTP connection request ? */
658             if (poll_entry->revents & POLLIN)
659                 new_connection(server_fd, 0);
660             poll_entry++;
661         }
662         if (rtsp_server_fd) {
663             /* new RTSP connection request ? */
664             if (poll_entry->revents & POLLIN)
665                 new_connection(rtsp_server_fd, 1);
666         }
667     }
668 }
669
670 /* start waiting for a new HTTP/RTSP request */
671 static void start_wait_request(HTTPContext *c, int is_rtsp)
672 {
673     c->buffer_ptr = c->buffer;
674     c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
675
676     if (is_rtsp) {
677         c->timeout = cur_time + RTSP_REQUEST_TIMEOUT;
678         c->state = RTSPSTATE_WAIT_REQUEST;
679     } else {
680         c->timeout = cur_time + HTTP_REQUEST_TIMEOUT;
681         c->state = HTTPSTATE_WAIT_REQUEST;
682     }
683 }
684
685 static void new_connection(int server_fd, int is_rtsp)
686 {
687     struct sockaddr_in from_addr;
688     int fd, len;
689     HTTPContext *c = NULL;
690
691     len = sizeof(from_addr);
692     fd = accept(server_fd, (struct sockaddr *)&from_addr,
693                 &len);
694     if (fd < 0) {
695         http_log("error during accept %s\n", strerror(errno));
696         return;
697     }
698     ff_socket_nonblock(fd, 1);
699
700     /* XXX: should output a warning page when coming
701        close to the connection limit */
702     if (nb_connections >= nb_max_connections)
703         goto fail;
704
705     /* add a new connection */
706     c = av_mallocz(sizeof(HTTPContext));
707     if (!c)
708         goto fail;
709
710     c->fd = fd;
711     c->poll_entry = NULL;
712     c->from_addr = from_addr;
713     c->buffer_size = IOBUFFER_INIT_SIZE;
714     c->buffer = av_malloc(c->buffer_size);
715     if (!c->buffer)
716         goto fail;
717
718     c->next = first_http_ctx;
719     first_http_ctx = c;
720     nb_connections++;
721
722     start_wait_request(c, is_rtsp);
723
724     return;
725
726  fail:
727     if (c) {
728         av_free(c->buffer);
729         av_free(c);
730     }
731     closesocket(fd);
732 }
733
734 static void close_connection(HTTPContext *c)
735 {
736     HTTPContext **cp, *c1;
737     int i, nb_streams;
738     AVFormatContext *ctx;
739     URLContext *h;
740     AVStream *st;
741
742     /* remove connection from list */
743     cp = &first_http_ctx;
744     while ((*cp) != NULL) {
745         c1 = *cp;
746         if (c1 == c)
747             *cp = c->next;
748         else
749             cp = &c1->next;
750     }
751
752     /* remove references, if any (XXX: do it faster) */
753     for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
754         if (c1->rtsp_c == c)
755             c1->rtsp_c = NULL;
756     }
757
758     /* remove connection associated resources */
759     if (c->fd >= 0)
760         closesocket(c->fd);
761     if (c->fmt_in) {
762         /* close each frame parser */
763         for(i=0;i<c->fmt_in->nb_streams;i++) {
764             st = c->fmt_in->streams[i];
765             if (st->codec->codec)
766                 avcodec_close(st->codec);
767         }
768         av_close_input_file(c->fmt_in);
769     }
770
771     /* free RTP output streams if any */
772     nb_streams = 0;
773     if (c->stream)
774         nb_streams = c->stream->nb_streams;
775
776     for(i=0;i<nb_streams;i++) {
777         ctx = c->rtp_ctx[i];
778         if (ctx) {
779             av_write_trailer(ctx);
780             av_free(ctx);
781         }
782         h = c->rtp_handles[i];
783         if (h)
784             url_close(h);
785     }
786
787     ctx = &c->fmt_ctx;
788
789     if (!c->last_packet_sent) {
790         if (ctx->oformat) {
791             /* prepare header */
792             if (url_open_dyn_buf(&ctx->pb) >= 0) {
793                 av_write_trailer(ctx);
794                 av_freep(&c->pb_buffer);
795                 url_close_dyn_buf(ctx->pb, &c->pb_buffer);
796             }
797         }
798     }
799
800     for(i=0; i<ctx->nb_streams; i++)
801         av_free(ctx->streams[i]);
802
803     if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
804         current_bandwidth -= c->stream->bandwidth;
805
806     /* signal that there is no feed if we are the feeder socket */
807     if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
808         c->stream->feed_opened = 0;
809         close(c->feed_fd);
810     }
811
812     av_freep(&c->pb_buffer);
813     av_freep(&c->packet_buffer);
814     av_free(c->buffer);
815     av_free(c);
816     nb_connections--;
817 }
818
819 static int handle_connection(HTTPContext *c)
820 {
821     int len, ret;
822
823     switch(c->state) {
824     case HTTPSTATE_WAIT_REQUEST:
825     case RTSPSTATE_WAIT_REQUEST:
826         /* timeout ? */
827         if ((c->timeout - cur_time) < 0)
828             return -1;
829         if (c->poll_entry->revents & (POLLERR | POLLHUP))
830             return -1;
831
832         /* no need to read if no events */
833         if (!(c->poll_entry->revents & POLLIN))
834             return 0;
835         /* read the data */
836     read_loop:
837         len = recv(c->fd, c->buffer_ptr, 1, 0);
838         if (len < 0) {
839             if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
840                 ff_neterrno() != FF_NETERROR(EINTR))
841                 return -1;
842         } else if (len == 0) {
843             return -1;
844         } else {
845             /* search for end of request. */
846             uint8_t *ptr;
847             c->buffer_ptr += len;
848             ptr = c->buffer_ptr;
849             if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
850                 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
851                 /* request found : parse it and reply */
852                 if (c->state == HTTPSTATE_WAIT_REQUEST) {
853                     ret = http_parse_request(c);
854                 } else {
855                     ret = rtsp_parse_request(c);
856                 }
857                 if (ret < 0)
858                     return -1;
859             } else if (ptr >= c->buffer_end) {
860                 /* request too long: cannot do anything */
861                 return -1;
862             } else goto read_loop;
863         }
864         break;
865
866     case HTTPSTATE_SEND_HEADER:
867         if (c->poll_entry->revents & (POLLERR | POLLHUP))
868             return -1;
869
870         /* no need to write if no events */
871         if (!(c->poll_entry->revents & POLLOUT))
872             return 0;
873         len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
874         if (len < 0) {
875             if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
876                 ff_neterrno() != FF_NETERROR(EINTR)) {
877                 /* error : close connection */
878                 av_freep(&c->pb_buffer);
879                 return -1;
880             }
881         } else {
882             c->buffer_ptr += len;
883             if (c->stream)
884                 c->stream->bytes_served += len;
885             c->data_count += len;
886             if (c->buffer_ptr >= c->buffer_end) {
887                 av_freep(&c->pb_buffer);
888                 /* if error, exit */
889                 if (c->http_error)
890                     return -1;
891                 /* all the buffer was sent : synchronize to the incoming stream */
892                 c->state = HTTPSTATE_SEND_DATA_HEADER;
893                 c->buffer_ptr = c->buffer_end = c->buffer;
894             }
895         }
896         break;
897
898     case HTTPSTATE_SEND_DATA:
899     case HTTPSTATE_SEND_DATA_HEADER:
900     case HTTPSTATE_SEND_DATA_TRAILER:
901         /* for packetized output, we consider we can always write (the
902            input streams sets the speed). It may be better to verify
903            that we do not rely too much on the kernel queues */
904         if (!c->is_packetized) {
905             if (c->poll_entry->revents & (POLLERR | POLLHUP))
906                 return -1;
907
908             /* no need to read if no events */
909             if (!(c->poll_entry->revents & POLLOUT))
910                 return 0;
911         }
912         if (http_send_data(c) < 0)
913             return -1;
914         /* close connection if trailer sent */
915         if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
916             return -1;
917         break;
918     case HTTPSTATE_RECEIVE_DATA:
919         /* no need to read if no events */
920         if (c->poll_entry->revents & (POLLERR | POLLHUP))
921             return -1;
922         if (!(c->poll_entry->revents & POLLIN))
923             return 0;
924         if (http_receive_data(c) < 0)
925             return -1;
926         break;
927     case HTTPSTATE_WAIT_FEED:
928         /* no need to read if no events */
929         if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
930             return -1;
931
932         /* nothing to do, we'll be waken up by incoming feed packets */
933         break;
934
935     case RTSPSTATE_SEND_REPLY:
936         if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
937             av_freep(&c->pb_buffer);
938             return -1;
939         }
940         /* no need to write if no events */
941         if (!(c->poll_entry->revents & POLLOUT))
942             return 0;
943         len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
944         if (len < 0) {
945             if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
946                 ff_neterrno() != FF_NETERROR(EINTR)) {
947                 /* error : close connection */
948                 av_freep(&c->pb_buffer);
949                 return -1;
950             }
951         } else {
952             c->buffer_ptr += len;
953             c->data_count += len;
954             if (c->buffer_ptr >= c->buffer_end) {
955                 /* all the buffer was sent : wait for a new request */
956                 av_freep(&c->pb_buffer);
957                 start_wait_request(c, 1);
958             }
959         }
960         break;
961     case RTSPSTATE_SEND_PACKET:
962         if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
963             av_freep(&c->packet_buffer);
964             return -1;
965         }
966         /* no need to write if no events */
967         if (!(c->poll_entry->revents & POLLOUT))
968             return 0;
969         len = send(c->fd, c->packet_buffer_ptr,
970                     c->packet_buffer_end - c->packet_buffer_ptr, 0);
971         if (len < 0) {
972             if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
973                 ff_neterrno() != FF_NETERROR(EINTR)) {
974                 /* error : close connection */
975                 av_freep(&c->packet_buffer);
976                 return -1;
977             }
978         } else {
979             c->packet_buffer_ptr += len;
980             if (c->packet_buffer_ptr >= c->packet_buffer_end) {
981                 /* all the buffer was sent : wait for a new request */
982                 av_freep(&c->packet_buffer);
983                 c->state = RTSPSTATE_WAIT_REQUEST;
984             }
985         }
986         break;
987     case HTTPSTATE_READY:
988         /* nothing to do */
989         break;
990     default:
991         return -1;
992     }
993     return 0;
994 }
995
996 static int extract_rates(char *rates, int ratelen, const char *request)
997 {
998     const char *p;
999
1000     for (p = request; *p && *p != '\r' && *p != '\n'; ) {
1001         if (strncasecmp(p, "Pragma:", 7) == 0) {
1002             const char *q = p + 7;
1003
1004             while (*q && *q != '\n' && isspace(*q))
1005                 q++;
1006
1007             if (strncasecmp(q, "stream-switch-entry=", 20) == 0) {
1008                 int stream_no;
1009                 int rate_no;
1010
1011                 q += 20;
1012
1013                 memset(rates, 0xff, ratelen);
1014
1015                 while (1) {
1016                     while (*q && *q != '\n' && *q != ':')
1017                         q++;
1018
1019                     if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
1020                         break;
1021
1022                     stream_no--;
1023                     if (stream_no < ratelen && stream_no >= 0)
1024                         rates[stream_no] = rate_no;
1025
1026                     while (*q && *q != '\n' && !isspace(*q))
1027                         q++;
1028                 }
1029
1030                 return 1;
1031             }
1032         }
1033         p = strchr(p, '\n');
1034         if (!p)
1035             break;
1036
1037         p++;
1038     }
1039
1040     return 0;
1041 }
1042
1043 static int find_stream_in_feed(FFStream *feed, AVCodecContext *codec, int bit_rate)
1044 {
1045     int i;
1046     int best_bitrate = 100000000;
1047     int best = -1;
1048
1049     for (i = 0; i < feed->nb_streams; i++) {
1050         AVCodecContext *feed_codec = feed->streams[i]->codec;
1051
1052         if (feed_codec->codec_id != codec->codec_id ||
1053             feed_codec->sample_rate != codec->sample_rate ||
1054             feed_codec->width != codec->width ||
1055             feed_codec->height != codec->height)
1056             continue;
1057
1058         /* Potential stream */
1059
1060         /* We want the fastest stream less than bit_rate, or the slowest
1061          * faster than bit_rate
1062          */
1063
1064         if (feed_codec->bit_rate <= bit_rate) {
1065             if (best_bitrate > bit_rate || feed_codec->bit_rate > best_bitrate) {
1066                 best_bitrate = feed_codec->bit_rate;
1067                 best = i;
1068             }
1069         } else {
1070             if (feed_codec->bit_rate < best_bitrate) {
1071                 best_bitrate = feed_codec->bit_rate;
1072                 best = i;
1073             }
1074         }
1075     }
1076
1077     return best;
1078 }
1079
1080 static int modify_current_stream(HTTPContext *c, char *rates)
1081 {
1082     int i;
1083     FFStream *req = c->stream;
1084     int action_required = 0;
1085
1086     /* Not much we can do for a feed */
1087     if (!req->feed)
1088         return 0;
1089
1090     for (i = 0; i < req->nb_streams; i++) {
1091         AVCodecContext *codec = req->streams[i]->codec;
1092
1093         switch(rates[i]) {
1094             case 0:
1095                 c->switch_feed_streams[i] = req->feed_streams[i];
1096                 break;
1097             case 1:
1098                 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1099                 break;
1100             case 2:
1101                 /* Wants off or slow */
1102                 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1103 #ifdef WANTS_OFF
1104                 /* This doesn't work well when it turns off the only stream! */
1105                 c->switch_feed_streams[i] = -2;
1106                 c->feed_streams[i] = -2;
1107 #endif
1108                 break;
1109         }
1110
1111         if (c->switch_feed_streams[i] >= 0 && c->switch_feed_streams[i] != c->feed_streams[i])
1112             action_required = 1;
1113     }
1114
1115     return action_required;
1116 }
1117
1118
1119 static void do_switch_stream(HTTPContext *c, int i)
1120 {
1121     if (c->switch_feed_streams[i] >= 0) {
1122 #ifdef PHILIP
1123         c->feed_streams[i] = c->switch_feed_streams[i];
1124 #endif
1125
1126         /* Now update the stream */
1127     }
1128     c->switch_feed_streams[i] = -1;
1129 }
1130
1131 /* XXX: factorize in utils.c ? */
1132 /* XXX: take care with different space meaning */
1133 static void skip_spaces(const char **pp)
1134 {
1135     const char *p;
1136     p = *pp;
1137     while (*p == ' ' || *p == '\t')
1138         p++;
1139     *pp = p;
1140 }
1141
1142 static void get_word(char *buf, int buf_size, const char **pp)
1143 {
1144     const char *p;
1145     char *q;
1146
1147     p = *pp;
1148     skip_spaces(&p);
1149     q = buf;
1150     while (!isspace(*p) && *p != '\0') {
1151         if ((q - buf) < buf_size - 1)
1152             *q++ = *p;
1153         p++;
1154     }
1155     if (buf_size > 0)
1156         *q = '\0';
1157     *pp = p;
1158 }
1159
1160 static int validate_acl(FFStream *stream, HTTPContext *c)
1161 {
1162     enum IPAddressAction last_action = IP_DENY;
1163     IPAddressACL *acl;
1164     struct in_addr *src = &c->from_addr.sin_addr;
1165     unsigned long src_addr = src->s_addr;
1166
1167     for (acl = stream->acl; acl; acl = acl->next) {
1168         if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
1169             return (acl->action == IP_ALLOW) ? 1 : 0;
1170         last_action = acl->action;
1171     }
1172
1173     /* Nothing matched, so return not the last action */
1174     return (last_action == IP_DENY) ? 1 : 0;
1175 }
1176
1177 /* compute the real filename of a file by matching it without its
1178    extensions to all the stream filenames */
1179 static void compute_real_filename(char *filename, int max_size)
1180 {
1181     char file1[1024];
1182     char file2[1024];
1183     char *p;
1184     FFStream *stream;
1185
1186     /* compute filename by matching without the file extensions */
1187     av_strlcpy(file1, filename, sizeof(file1));
1188     p = strrchr(file1, '.');
1189     if (p)
1190         *p = '\0';
1191     for(stream = first_stream; stream != NULL; stream = stream->next) {
1192         av_strlcpy(file2, stream->filename, sizeof(file2));
1193         p = strrchr(file2, '.');
1194         if (p)
1195             *p = '\0';
1196         if (!strcmp(file1, file2)) {
1197             av_strlcpy(filename, stream->filename, max_size);
1198             break;
1199         }
1200     }
1201 }
1202
1203 enum RedirType {
1204     REDIR_NONE,
1205     REDIR_ASX,
1206     REDIR_RAM,
1207     REDIR_ASF,
1208     REDIR_RTSP,
1209     REDIR_SDP,
1210 };
1211
1212 /* parse http request and prepare header */
1213 static int http_parse_request(HTTPContext *c)
1214 {
1215     char *p;
1216     enum RedirType redir_type;
1217     char cmd[32];
1218     char info[1024], filename[1024];
1219     char url[1024], *q;
1220     char protocol[32];
1221     char msg[1024];
1222     const char *mime_type;
1223     FFStream *stream;
1224     int i;
1225     char ratebuf[32];
1226     char *useragent = 0;
1227
1228     p = c->buffer;
1229     get_word(cmd, sizeof(cmd), (const char **)&p);
1230     av_strlcpy(c->method, cmd, sizeof(c->method));
1231
1232     if (!strcmp(cmd, "GET"))
1233         c->post = 0;
1234     else if (!strcmp(cmd, "POST"))
1235         c->post = 1;
1236     else
1237         return -1;
1238
1239     get_word(url, sizeof(url), (const char **)&p);
1240     av_strlcpy(c->url, url, sizeof(c->url));
1241
1242     get_word(protocol, sizeof(protocol), (const char **)&p);
1243     if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1244         return -1;
1245
1246     av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
1247
1248     if (ffserver_debug)
1249         http_log("New connection: %s %s\n", cmd, url);
1250
1251     /* find the filename and the optional info string in the request */
1252     p = strchr(url, '?');
1253     if (p) {
1254         av_strlcpy(info, p, sizeof(info));
1255         *p = '\0';
1256     } else
1257         info[0] = '\0';
1258
1259     av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
1260
1261     for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1262         if (strncasecmp(p, "User-Agent:", 11) == 0) {
1263             useragent = p + 11;
1264             if (*useragent && *useragent != '\n' && isspace(*useragent))
1265                 useragent++;
1266             break;
1267         }
1268         p = strchr(p, '\n');
1269         if (!p)
1270             break;
1271
1272         p++;
1273     }
1274
1275     redir_type = REDIR_NONE;
1276     if (match_ext(filename, "asx")) {
1277         redir_type = REDIR_ASX;
1278         filename[strlen(filename)-1] = 'f';
1279     } else if (match_ext(filename, "asf") &&
1280         (!useragent || strncasecmp(useragent, "NSPlayer", 8) != 0)) {
1281         /* if this isn't WMP or lookalike, return the redirector file */
1282         redir_type = REDIR_ASF;
1283     } else if (match_ext(filename, "rpm,ram")) {
1284         redir_type = REDIR_RAM;
1285         strcpy(filename + strlen(filename)-2, "m");
1286     } else if (match_ext(filename, "rtsp")) {
1287         redir_type = REDIR_RTSP;
1288         compute_real_filename(filename, sizeof(filename) - 1);
1289     } else if (match_ext(filename, "sdp")) {
1290         redir_type = REDIR_SDP;
1291         compute_real_filename(filename, sizeof(filename) - 1);
1292     }
1293
1294     // "redirect" / request to index.html
1295     if (!strlen(filename))
1296         av_strlcpy(filename, "index.html", sizeof(filename) - 1);
1297
1298     stream = first_stream;
1299     while (stream != NULL) {
1300         if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
1301             break;
1302         stream = stream->next;
1303     }
1304     if (stream == NULL) {
1305         snprintf(msg, sizeof(msg), "File '%s' not found", url);
1306         goto send_error;
1307     }
1308
1309     c->stream = stream;
1310     memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1311     memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1312
1313     if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1314         c->http_error = 301;
1315         q = c->buffer;
1316         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 301 Moved\r\n");
1317         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Location: %s\r\n", stream->feed_filename);
1318         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1319         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1320         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Moved</title></head><body>\r\n");
1321         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "You should be <a href=\"%s\">redirected</a>.\r\n", stream->feed_filename);
1322         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1323
1324         /* prepare output buffer */
1325         c->buffer_ptr = c->buffer;
1326         c->buffer_end = q;
1327         c->state = HTTPSTATE_SEND_HEADER;
1328         return 0;
1329     }
1330
1331     /* If this is WMP, get the rate information */
1332     if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1333         if (modify_current_stream(c, ratebuf)) {
1334             for (i = 0; i < sizeof(c->feed_streams) / sizeof(c->feed_streams[0]); i++) {
1335                 if (c->switch_feed_streams[i] >= 0)
1336                     do_switch_stream(c, i);
1337             }
1338         }
1339     }
1340
1341     /* If already streaming this feed, do not let start another feeder. */
1342     if (stream->feed_opened) {
1343         snprintf(msg, sizeof(msg), "This feed is already being received.");
1344         goto send_error;
1345     }
1346
1347     if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
1348         current_bandwidth += stream->bandwidth;
1349
1350     if (c->post == 0 && max_bandwidth < current_bandwidth) {
1351         c->http_error = 200;
1352         q = c->buffer;
1353         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 Server too busy\r\n");
1354         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1355         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1356         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Too busy</title></head><body>\r\n");
1357         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<p>The server is too busy to serve your request at this time.</p>\r\n");
1358         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<p>The bandwidth being served (including your stream) is %lldkbit/sec, and this exceeds the limit of %lldkbit/sec.</p>\r\n",
1359             current_bandwidth, max_bandwidth);
1360         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1361
1362         /* prepare output buffer */
1363         c->buffer_ptr = c->buffer;
1364         c->buffer_end = q;
1365         c->state = HTTPSTATE_SEND_HEADER;
1366         return 0;
1367     }
1368
1369     if (redir_type != REDIR_NONE) {
1370         char *hostinfo = 0;
1371
1372         for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1373             if (strncasecmp(p, "Host:", 5) == 0) {
1374                 hostinfo = p + 5;
1375                 break;
1376             }
1377             p = strchr(p, '\n');
1378             if (!p)
1379                 break;
1380
1381             p++;
1382         }
1383
1384         if (hostinfo) {
1385             char *eoh;
1386             char hostbuf[260];
1387
1388             while (isspace(*hostinfo))
1389                 hostinfo++;
1390
1391             eoh = strchr(hostinfo, '\n');
1392             if (eoh) {
1393                 if (eoh[-1] == '\r')
1394                     eoh--;
1395
1396                 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1397                     memcpy(hostbuf, hostinfo, eoh - hostinfo);
1398                     hostbuf[eoh - hostinfo] = 0;
1399
1400                     c->http_error = 200;
1401                     q = c->buffer;
1402                     switch(redir_type) {
1403                     case REDIR_ASX:
1404                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASX Follows\r\n");
1405                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1406                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1407                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ASX Version=\"3\">\r\n");
1408                         //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1409                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1410                                 hostbuf, filename, info);
1411                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</ASX>\r\n");
1412                         break;
1413                     case REDIR_RAM:
1414                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RAM Follows\r\n");
1415                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: audio/x-pn-realaudio\r\n");
1416                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1417                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "# Autogenerated by ffserver\r\n");
1418                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "http://%s/%s%s\r\n",
1419                                 hostbuf, filename, info);
1420                         break;
1421                     case REDIR_ASF:
1422                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASF Redirect follows\r\n");
1423                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1424                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1425                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "[Reference]\r\n");
1426                         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Ref1=http://%s/%s%s\r\n",
1427                                 hostbuf, filename, info);
1428                         break;
1429                     case REDIR_RTSP:
1430                         {
1431                             char hostname[256], *p;
1432                             /* extract only hostname */
1433                             av_strlcpy(hostname, hostbuf, sizeof(hostname));
1434                             p = strrchr(hostname, ':');
1435                             if (p)
1436                                 *p = '\0';
1437                             q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1438                             /* XXX: incorrect mime type ? */
1439                             q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/x-rtsp\r\n");
1440                             q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1441                             q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "rtsp://%s:%d/%s\r\n",
1442                                          hostname, ntohs(my_rtsp_addr.sin_port),
1443                                          filename);
1444                         }
1445                         break;
1446                     case REDIR_SDP:
1447                         {
1448                             uint8_t *sdp_data;
1449                             int sdp_data_size, len;
1450                             struct sockaddr_in my_addr;
1451
1452                             q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1453                             q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/sdp\r\n");
1454                             q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1455
1456                             len = sizeof(my_addr);
1457                             getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
1458
1459                             /* XXX: should use a dynamic buffer */
1460                             sdp_data_size = prepare_sdp_description(stream,
1461                                                                     &sdp_data,
1462                                                                     my_addr.sin_addr);
1463                             if (sdp_data_size > 0) {
1464                                 memcpy(q, sdp_data, sdp_data_size);
1465                                 q += sdp_data_size;
1466                                 *q = '\0';
1467                                 av_free(sdp_data);
1468                             }
1469                         }
1470                         break;
1471                     default:
1472                         abort();
1473                         break;
1474                     }
1475
1476                     /* prepare output buffer */
1477                     c->buffer_ptr = c->buffer;
1478                     c->buffer_end = q;
1479                     c->state = HTTPSTATE_SEND_HEADER;
1480                     return 0;
1481                 }
1482             }
1483         }
1484
1485         snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
1486         goto send_error;
1487     }
1488
1489     stream->conns_served++;
1490
1491     /* XXX: add there authenticate and IP match */
1492
1493     if (c->post) {
1494         /* if post, it means a feed is being sent */
1495         if (!stream->is_feed) {
1496             /* However it might be a status report from WMP! Lets log the data
1497              * as it might come in handy one day
1498              */
1499             char *logline = 0;
1500             int client_id = 0;
1501
1502             for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1503                 if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1504                     logline = p;
1505                     break;
1506                 }
1507                 if (strncasecmp(p, "Pragma: client-id=", 18) == 0)
1508                     client_id = strtol(p + 18, 0, 10);
1509                 p = strchr(p, '\n');
1510                 if (!p)
1511                     break;
1512
1513                 p++;
1514             }
1515
1516             if (logline) {
1517                 char *eol = strchr(logline, '\n');
1518
1519                 logline += 17;
1520
1521                 if (eol) {
1522                     if (eol[-1] == '\r')
1523                         eol--;
1524                     http_log("%.*s\n", (int) (eol - logline), logline);
1525                     c->suppress_log = 1;
1526                 }
1527             }
1528
1529 #ifdef DEBUG_WMP
1530             http_log("\nGot request:\n%s\n", c->buffer);
1531 #endif
1532
1533             if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1534                 HTTPContext *wmpc;
1535
1536                 /* Now we have to find the client_id */
1537                 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1538                     if (wmpc->wmp_client_id == client_id)
1539                         break;
1540                 }
1541
1542                 if (wmpc && modify_current_stream(wmpc, ratebuf))
1543                     wmpc->switch_pending = 1;
1544             }
1545
1546             snprintf(msg, sizeof(msg), "POST command not handled");
1547             c->stream = 0;
1548             goto send_error;
1549         }
1550         if (http_start_receive_data(c) < 0) {
1551             snprintf(msg, sizeof(msg), "could not open feed");
1552             goto send_error;
1553         }
1554         c->http_error = 0;
1555         c->state = HTTPSTATE_RECEIVE_DATA;
1556         return 0;
1557     }
1558
1559 #ifdef DEBUG_WMP
1560     if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
1561         http_log("\nGot request:\n%s\n", c->buffer);
1562 #endif
1563
1564     if (c->stream->stream_type == STREAM_TYPE_STATUS)
1565         goto send_status;
1566
1567     /* open input stream */
1568     if (open_input_stream(c, info) < 0) {
1569         snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1570         goto send_error;
1571     }
1572
1573     /* prepare http header */
1574     q = c->buffer;
1575     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1576     mime_type = c->stream->fmt->mime_type;
1577     if (!mime_type)
1578         mime_type = "application/x-octet-stream";
1579     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
1580
1581     /* for asf, we need extra headers */
1582     if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1583         /* Need to allocate a client id */
1584
1585         c->wmp_client_id = av_random(&random_state) & 0x7fffffff;
1586
1587         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=%d\r\nPragma: features=\"broadcast\"\r\n", c->wmp_client_id);
1588     }
1589     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-Type: %s\r\n", mime_type);
1590     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1591
1592     /* prepare output buffer */
1593     c->http_error = 0;
1594     c->buffer_ptr = c->buffer;
1595     c->buffer_end = q;
1596     c->state = HTTPSTATE_SEND_HEADER;
1597     return 0;
1598  send_error:
1599     c->http_error = 404;
1600     q = c->buffer;
1601     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 404 Not Found\r\n");
1602     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: %s\r\n", "text/html");
1603     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1604     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HTML>\n");
1605     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1606     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<BODY>%s</BODY>\n", msg);
1607     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</HTML>\n");
1608
1609     /* prepare output buffer */
1610     c->buffer_ptr = c->buffer;
1611     c->buffer_end = q;
1612     c->state = HTTPSTATE_SEND_HEADER;
1613     return 0;
1614  send_status:
1615     compute_status(c);
1616     c->http_error = 200; /* horrible : we use this value to avoid
1617                             going to the send data state */
1618     c->state = HTTPSTATE_SEND_HEADER;
1619     return 0;
1620 }
1621
1622 static void fmt_bytecount(ByteIOContext *pb, int64_t count)
1623 {
1624     static const char *suffix = " kMGTP";
1625     const char *s;
1626
1627     for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
1628
1629     url_fprintf(pb, "%"PRId64"%c", count, *s);
1630 }
1631
1632 static void compute_status(HTTPContext *c)
1633 {
1634     HTTPContext *c1;
1635     FFStream *stream;
1636     char *p;
1637     time_t ti;
1638     int i, len;
1639     ByteIOContext *pb;
1640
1641     if (url_open_dyn_buf(&pb) < 0) {
1642         /* XXX: return an error ? */
1643         c->buffer_ptr = c->buffer;
1644         c->buffer_end = c->buffer;
1645         return;
1646     }
1647
1648     url_fprintf(pb, "HTTP/1.0 200 OK\r\n");
1649     url_fprintf(pb, "Content-type: %s\r\n", "text/html");
1650     url_fprintf(pb, "Pragma: no-cache\r\n");
1651     url_fprintf(pb, "\r\n");
1652
1653     url_fprintf(pb, "<HEAD><TITLE>%s Status</TITLE>\n", program_name);
1654     if (c->stream->feed_filename[0])
1655         url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
1656     url_fprintf(pb, "</HEAD>\n<BODY>");
1657     url_fprintf(pb, "<H1>%s Status</H1>\n", program_name);
1658     /* format status */
1659     url_fprintf(pb, "<H2>Available Streams</H2>\n");
1660     url_fprintf(pb, "<TABLE cellspacing=0 cellpadding=4>\n");
1661     url_fprintf(pb, "<TR><Th valign=top>Path<th align=left>Served<br>Conns<Th><br>bytes<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");
1662     stream = first_stream;
1663     while (stream != NULL) {
1664         char sfilename[1024];
1665         char *eosf;
1666
1667         if (stream->feed != stream) {
1668             av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
1669             eosf = sfilename + strlen(sfilename);
1670             if (eosf - sfilename >= 4) {
1671                 if (strcmp(eosf - 4, ".asf") == 0)
1672                     strcpy(eosf - 4, ".asx");
1673                 else if (strcmp(eosf - 3, ".rm") == 0)
1674                     strcpy(eosf - 3, ".ram");
1675                 else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
1676                     /* generate a sample RTSP director if
1677                        unicast. Generate an SDP redirector if
1678                        multicast */
1679                     eosf = strrchr(sfilename, '.');
1680                     if (!eosf)
1681                         eosf = sfilename + strlen(sfilename);
1682                     if (stream->is_multicast)
1683                         strcpy(eosf, ".sdp");
1684                     else
1685                         strcpy(eosf, ".rtsp");
1686                 }
1687             }
1688
1689             url_fprintf(pb, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1690                          sfilename, stream->filename);
1691             url_fprintf(pb, "<td align=right> %d <td align=right> ",
1692                         stream->conns_served);
1693             fmt_bytecount(pb, stream->bytes_served);
1694             switch(stream->stream_type) {
1695             case STREAM_TYPE_LIVE:
1696                 {
1697                     int audio_bit_rate = 0;
1698                     int video_bit_rate = 0;
1699                     const char *audio_codec_name = "";
1700                     const char *video_codec_name = "";
1701                     const char *audio_codec_name_extra = "";
1702                     const char *video_codec_name_extra = "";
1703
1704                     for(i=0;i<stream->nb_streams;i++) {
1705                         AVStream *st = stream->streams[i];
1706                         AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1707                         switch(st->codec->codec_type) {
1708                         case CODEC_TYPE_AUDIO:
1709                             audio_bit_rate += st->codec->bit_rate;
1710                             if (codec) {
1711                                 if (*audio_codec_name)
1712                                     audio_codec_name_extra = "...";
1713                                 audio_codec_name = codec->name;
1714                             }
1715                             break;
1716                         case CODEC_TYPE_VIDEO:
1717                             video_bit_rate += st->codec->bit_rate;
1718                             if (codec) {
1719                                 if (*video_codec_name)
1720                                     video_codec_name_extra = "...";
1721                                 video_codec_name = codec->name;
1722                             }
1723                             break;
1724                         case CODEC_TYPE_DATA:
1725                             video_bit_rate += st->codec->bit_rate;
1726                             break;
1727                         default:
1728                             abort();
1729                         }
1730                     }
1731                     url_fprintf(pb, "<TD align=center> %s <TD align=right> %d <TD align=right> %d <TD> %s %s <TD align=right> %d <TD> %s %s",
1732                                  stream->fmt->name,
1733                                  stream->bandwidth,
1734                                  video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
1735                                  audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
1736                     if (stream->feed)
1737                         url_fprintf(pb, "<TD>%s", stream->feed->filename);
1738                     else
1739                         url_fprintf(pb, "<TD>%s", stream->feed_filename);
1740                     url_fprintf(pb, "\n");
1741                 }
1742                 break;
1743             default:
1744                 url_fprintf(pb, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1745                 break;
1746             }
1747         }
1748         stream = stream->next;
1749     }
1750     url_fprintf(pb, "</TABLE>\n");
1751
1752     stream = first_stream;
1753     while (stream != NULL) {
1754         if (stream->feed == stream) {
1755             url_fprintf(pb, "<h2>Feed %s</h2>", stream->filename);
1756             if (stream->pid) {
1757                 url_fprintf(pb, "Running as pid %d.\n", stream->pid);
1758
1759 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1760                 {
1761                     FILE *pid_stat;
1762                     char ps_cmd[64];
1763
1764                     /* This is somewhat linux specific I guess */
1765                     snprintf(ps_cmd, sizeof(ps_cmd),
1766                              "ps -o \"%%cpu,cputime\" --no-headers %d",
1767                              stream->pid);
1768
1769                     pid_stat = popen(ps_cmd, "r");
1770                     if (pid_stat) {
1771                         char cpuperc[10];
1772                         char cpuused[64];
1773
1774                         if (fscanf(pid_stat, "%10s %64s", cpuperc,
1775                                    cpuused) == 2) {
1776                             url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",
1777                                          cpuperc, cpuused);
1778                         }
1779                         fclose(pid_stat);
1780                     }
1781                 }
1782 #endif
1783
1784                 url_fprintf(pb, "<p>");
1785             }
1786             url_fprintf(pb, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>type<th>kbits/s<th align=left>codec<th align=left>Parameters\n");
1787
1788             for (i = 0; i < stream->nb_streams; i++) {
1789                 AVStream *st = stream->streams[i];
1790                 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1791                 const char *type = "unknown";
1792                 char parameters[64];
1793
1794                 parameters[0] = 0;
1795
1796                 switch(st->codec->codec_type) {
1797                 case CODEC_TYPE_AUDIO:
1798                     type = "audio";
1799                     snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
1800                     break;
1801                 case CODEC_TYPE_VIDEO:
1802                     type = "video";
1803                     snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,
1804                                 st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);
1805                     break;
1806                 default:
1807                     abort();
1808                 }
1809                 url_fprintf(pb, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1810                         i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);
1811             }
1812             url_fprintf(pb, "</table>\n");
1813
1814         }
1815         stream = stream->next;
1816     }
1817
1818 #if 0
1819     {
1820         float avg;
1821         AVCodecContext *enc;
1822         char buf[1024];
1823
1824         /* feed status */
1825         stream = first_feed;
1826         while (stream != NULL) {
1827             url_fprintf(pb, "<H1>Feed '%s'</H1>\n", stream->filename);
1828             url_fprintf(pb, "<TABLE>\n");
1829             url_fprintf(pb, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1830             for(i=0;i<stream->nb_streams;i++) {
1831                 AVStream *st = stream->streams[i];
1832                 FeedData *fdata = st->priv_data;
1833                 enc = st->codec;
1834
1835                 avcodec_string(buf, sizeof(buf), enc);
1836                 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
1837                 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
1838                     avg /= enc->frame_size;
1839                 url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
1840                              buf, enc->frame_number, fdata->data_count, avg / 1000.0);
1841             }
1842             url_fprintf(pb, "</TABLE>\n");
1843             stream = stream->next_feed;
1844         }
1845     }
1846 #endif
1847
1848     /* connection status */
1849     url_fprintf(pb, "<H2>Connection Status</H2>\n");
1850
1851     url_fprintf(pb, "Number of connections: %d / %d<BR>\n",
1852                  nb_connections, nb_max_connections);
1853
1854     url_fprintf(pb, "Bandwidth in use: %lldk / %lldk<BR>\n",
1855                  current_bandwidth, max_bandwidth);
1856
1857     url_fprintf(pb, "<TABLE>\n");
1858     url_fprintf(pb, "<TR><th>#<th>File<th>IP<th>Proto<th>State<th>Target bits/sec<th>Actual bits/sec<th>Bytes transferred\n");
1859     c1 = first_http_ctx;
1860     i = 0;
1861     while (c1 != NULL) {
1862         int bitrate;
1863         int j;
1864
1865         bitrate = 0;
1866         if (c1->stream) {
1867             for (j = 0; j < c1->stream->nb_streams; j++) {
1868                 if (!c1->stream->feed)
1869                     bitrate += c1->stream->streams[j]->codec->bit_rate;
1870                 else if (c1->feed_streams[j] >= 0)
1871                     bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
1872             }
1873         }
1874
1875         i++;
1876         p = inet_ntoa(c1->from_addr.sin_addr);
1877         url_fprintf(pb, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1878                     i,
1879                     c1->stream ? c1->stream->filename : "",
1880                     c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
1881                     p,
1882                     c1->protocol,
1883                     http_state[c1->state]);
1884         fmt_bytecount(pb, bitrate);
1885         url_fprintf(pb, "<td align=right>");
1886         fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
1887         url_fprintf(pb, "<td align=right>");
1888         fmt_bytecount(pb, c1->data_count);
1889         url_fprintf(pb, "\n");
1890         c1 = c1->next;
1891     }
1892     url_fprintf(pb, "</TABLE>\n");
1893
1894     /* date */
1895     ti = time(NULL);
1896     p = ctime(&ti);
1897     url_fprintf(pb, "<HR size=1 noshade>Generated at %s", p);
1898     url_fprintf(pb, "</BODY>\n</HTML>\n");
1899
1900     len = url_close_dyn_buf(pb, &c->pb_buffer);
1901     c->buffer_ptr = c->pb_buffer;
1902     c->buffer_end = c->pb_buffer + len;
1903 }
1904
1905 /* check if the parser needs to be opened for stream i */
1906 static void open_parser(AVFormatContext *s, int i)
1907 {
1908     AVStream *st = s->streams[i];
1909     AVCodec *codec;
1910
1911     if (!st->codec->codec) {
1912         codec = avcodec_find_decoder(st->codec->codec_id);
1913         if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
1914             st->codec->parse_only = 1;
1915             if (avcodec_open(st->codec, codec) < 0)
1916                 st->codec->parse_only = 0;
1917         }
1918     }
1919 }
1920
1921 static int open_input_stream(HTTPContext *c, const char *info)
1922 {
1923     char buf[128];
1924     char input_filename[1024];
1925     AVFormatContext *s;
1926     int buf_size, i, ret;
1927     int64_t stream_pos;
1928
1929     /* find file name */
1930     if (c->stream->feed) {
1931         strcpy(input_filename, c->stream->feed->feed_filename);
1932         buf_size = FFM_PACKET_SIZE;
1933         /* compute position (absolute time) */
1934         if (find_info_tag(buf, sizeof(buf), "date", info))
1935         {
1936             stream_pos = parse_date(buf, 0);
1937             if (stream_pos == INT64_MIN)
1938                 return -1;
1939         }
1940         else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
1941             int prebuffer = strtol(buf, 0, 10);
1942             stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
1943         } else
1944             stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
1945     } else {
1946         strcpy(input_filename, c->stream->feed_filename);
1947         buf_size = 0;
1948         /* compute position (relative time) */
1949         if (find_info_tag(buf, sizeof(buf), "date", info))
1950         {
1951             stream_pos = parse_date(buf, 1);
1952             if (stream_pos == INT64_MIN)
1953                 return -1;
1954         }
1955         else
1956             stream_pos = 0;
1957     }
1958     if (input_filename[0] == '\0')
1959         return -1;
1960
1961 #if 0
1962     { time_t when = stream_pos / 1000000;
1963     http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
1964     }
1965 #endif
1966
1967     /* open stream */
1968     if ((ret = av_open_input_file(&s, input_filename, c->stream->ifmt,
1969                                   buf_size, c->stream->ap_in)) < 0) {
1970         http_log("could not open %s: %d\n", input_filename, ret);
1971         return -1;
1972     }
1973     s->flags |= AVFMT_FLAG_GENPTS;
1974     c->fmt_in = s;
1975     av_find_stream_info(c->fmt_in);
1976
1977     /* open each parser */
1978     for(i=0;i<s->nb_streams;i++)
1979         open_parser(s, i);
1980
1981     /* choose stream as clock source (we favorize video stream if
1982        present) for packet sending */
1983     c->pts_stream_index = 0;
1984     for(i=0;i<c->stream->nb_streams;i++) {
1985         if (c->pts_stream_index == 0 &&
1986             c->stream->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
1987             c->pts_stream_index = i;
1988         }
1989     }
1990
1991 #if 1
1992     if (c->fmt_in->iformat->read_seek)
1993         av_seek_frame(c->fmt_in, -1, stream_pos, 0);
1994 #endif
1995     /* set the start time (needed for maxtime and RTP packet timing) */
1996     c->start_time = cur_time;
1997     c->first_pts = AV_NOPTS_VALUE;
1998     return 0;
1999 }
2000
2001 /* return the server clock (in us) */
2002 static int64_t get_server_clock(HTTPContext *c)
2003 {
2004     /* compute current pts value from system time */
2005     return (cur_time - c->start_time) * 1000;
2006 }
2007
2008 /* return the estimated time at which the current packet must be sent
2009    (in us) */
2010 static int64_t get_packet_send_clock(HTTPContext *c)
2011 {
2012     int bytes_left, bytes_sent, frame_bytes;
2013
2014     frame_bytes = c->cur_frame_bytes;
2015     if (frame_bytes <= 0)
2016         return c->cur_pts;
2017     else {
2018         bytes_left = c->buffer_end - c->buffer_ptr;
2019         bytes_sent = frame_bytes - bytes_left;
2020         return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
2021     }
2022 }
2023
2024
2025 static int http_prepare_data(HTTPContext *c)
2026 {
2027     int i, len, ret;
2028     AVFormatContext *ctx;
2029
2030     av_freep(&c->pb_buffer);
2031     switch(c->state) {
2032     case HTTPSTATE_SEND_DATA_HEADER:
2033         memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
2034         av_strlcpy(c->fmt_ctx.author, c->stream->author,
2035                    sizeof(c->fmt_ctx.author));
2036         av_strlcpy(c->fmt_ctx.comment, c->stream->comment,
2037                    sizeof(c->fmt_ctx.comment));
2038         av_strlcpy(c->fmt_ctx.copyright, c->stream->copyright,
2039                    sizeof(c->fmt_ctx.copyright));
2040         av_strlcpy(c->fmt_ctx.title, c->stream->title,
2041                    sizeof(c->fmt_ctx.title));
2042
2043         /* open output stream by using specified codecs */
2044         c->fmt_ctx.oformat = c->stream->fmt;
2045         c->fmt_ctx.nb_streams = c->stream->nb_streams;
2046         for(i=0;i<c->fmt_ctx.nb_streams;i++) {
2047             AVStream *st;
2048             AVStream *src;
2049             st = av_mallocz(sizeof(AVStream));
2050             c->fmt_ctx.streams[i] = st;
2051             /* if file or feed, then just take streams from FFStream struct */
2052             if (!c->stream->feed ||
2053                 c->stream->feed == c->stream)
2054                 src = c->stream->streams[i];
2055             else
2056                 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2057
2058             *st = *src;
2059             st->priv_data = 0;
2060             st->codec->frame_number = 0; /* XXX: should be done in
2061                                            AVStream, not in codec */
2062         }
2063         c->got_key_frame = 0;
2064
2065         /* prepare header and save header data in a stream */
2066         if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2067             /* XXX: potential leak */
2068             return -1;
2069         }
2070         c->fmt_ctx.pb->is_streamed = 1;
2071
2072         /*
2073          * HACK to avoid mpeg ps muxer to spit many underflow errors
2074          * Default value from FFmpeg
2075          * Try to set it use configuration option
2076          */
2077         c->fmt_ctx.preload   = (int)(0.5*AV_TIME_BASE);
2078         c->fmt_ctx.max_delay = (int)(0.7*AV_TIME_BASE);
2079
2080         av_set_parameters(&c->fmt_ctx, NULL);
2081         if (av_write_header(&c->fmt_ctx) < 0) {
2082             http_log("Error writing output header\n");
2083             return -1;
2084         }
2085
2086         len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2087         c->buffer_ptr = c->pb_buffer;
2088         c->buffer_end = c->pb_buffer + len;
2089
2090         c->state = HTTPSTATE_SEND_DATA;
2091         c->last_packet_sent = 0;
2092         break;
2093     case HTTPSTATE_SEND_DATA:
2094         /* find a new packet */
2095         /* read a packet from the input stream */
2096         if (c->stream->feed)
2097             ffm_set_write_index(c->fmt_in,
2098                                 c->stream->feed->feed_write_index,
2099                                 c->stream->feed->feed_size);
2100
2101         if (c->stream->max_time &&
2102             c->stream->max_time + c->start_time - cur_time < 0)
2103             /* We have timed out */
2104             c->state = HTTPSTATE_SEND_DATA_TRAILER;
2105         else {
2106             AVPacket pkt;
2107         redo:
2108             if (av_read_frame(c->fmt_in, &pkt) < 0) {
2109                 if (c->stream->feed && c->stream->feed->feed_opened) {
2110                     /* if coming from feed, it means we reached the end of the
2111                        ffm file, so must wait for more data */
2112                     c->state = HTTPSTATE_WAIT_FEED;
2113                     return 1; /* state changed */
2114                 } else {
2115                     if (c->stream->loop) {
2116                         av_close_input_file(c->fmt_in);
2117                         c->fmt_in = NULL;
2118                         if (open_input_stream(c, "") < 0)
2119                             goto no_loop;
2120                         goto redo;
2121                     } else {
2122                     no_loop:
2123                         /* must send trailer now because eof or error */
2124                         c->state = HTTPSTATE_SEND_DATA_TRAILER;
2125                     }
2126                 }
2127             } else {
2128                 int source_index = pkt.stream_index;
2129                 /* update first pts if needed */
2130                 if (c->first_pts == AV_NOPTS_VALUE) {
2131                     c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2132                     c->start_time = cur_time;
2133                 }
2134                 /* send it to the appropriate stream */
2135                 if (c->stream->feed) {
2136                     /* if coming from a feed, select the right stream */
2137                     if (c->switch_pending) {
2138                         c->switch_pending = 0;
2139                         for(i=0;i<c->stream->nb_streams;i++) {
2140                             if (c->switch_feed_streams[i] == pkt.stream_index)
2141                                 if (pkt.flags & PKT_FLAG_KEY)
2142                                     do_switch_stream(c, i);
2143                             if (c->switch_feed_streams[i] >= 0)
2144                                 c->switch_pending = 1;
2145                         }
2146                     }
2147                     for(i=0;i<c->stream->nb_streams;i++) {
2148                         if (c->feed_streams[i] == pkt.stream_index) {
2149                             AVStream *st = c->fmt_in->streams[source_index];
2150                             pkt.stream_index = i;
2151                             if (pkt.flags & PKT_FLAG_KEY &&
2152                                 (st->codec->codec_type == CODEC_TYPE_VIDEO ||
2153                                  c->stream->nb_streams == 1))
2154                                 c->got_key_frame = 1;
2155                             if (!c->stream->send_on_key || c->got_key_frame)
2156                                 goto send_it;
2157                         }
2158                     }
2159                 } else {
2160                     AVCodecContext *codec;
2161
2162                 send_it:
2163                     /* specific handling for RTP: we use several
2164                        output stream (one for each RTP
2165                        connection). XXX: need more abstract handling */
2166                     if (c->is_packetized) {
2167                         AVStream *st;
2168                         /* compute send time and duration */
2169                         st = c->fmt_in->streams[pkt.stream_index];
2170                         c->cur_pts = av_rescale_q(pkt.dts, st->time_base, AV_TIME_BASE_Q);
2171                         if (st->start_time != AV_NOPTS_VALUE)
2172                             c->cur_pts -= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
2173                         c->cur_frame_duration = av_rescale_q(pkt.duration, st->time_base, AV_TIME_BASE_Q);
2174 #if 0
2175                         printf("index=%d pts=%0.3f duration=%0.6f\n",
2176                                pkt.stream_index,
2177                                (double)c->cur_pts /
2178                                AV_TIME_BASE,
2179                                (double)c->cur_frame_duration /
2180                                AV_TIME_BASE);
2181 #endif
2182                         /* find RTP context */
2183                         c->packet_stream_index = pkt.stream_index;
2184                         ctx = c->rtp_ctx[c->packet_stream_index];
2185                         if(!ctx) {
2186                             av_free_packet(&pkt);
2187                             break;
2188                         }
2189                         codec = ctx->streams[0]->codec;
2190                         /* only one stream per RTP connection */
2191                         pkt.stream_index = 0;
2192                     } else {
2193                         ctx = &c->fmt_ctx;
2194                         /* Fudge here */
2195                         codec = ctx->streams[pkt.stream_index]->codec;
2196                     }
2197
2198                     if (c->is_packetized) {
2199                         int max_packet_size;
2200                         if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP)
2201                             max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2202                         else
2203                             max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);
2204                         ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);
2205                     } else {
2206                         ret = url_open_dyn_buf(&ctx->pb);
2207                     }
2208                     if (ret < 0) {
2209                         /* XXX: potential leak */
2210                         return -1;
2211                     }
2212                     c->fmt_ctx.pb->is_streamed = 1;
2213                     if (pkt.dts != AV_NOPTS_VALUE)
2214                         pkt.dts = av_rescale_q(pkt.dts,
2215                                                c->fmt_in->streams[source_index]->time_base,
2216                                                ctx->streams[pkt.stream_index]->time_base);
2217                     if (pkt.pts != AV_NOPTS_VALUE)
2218                         pkt.pts = av_rescale_q(pkt.pts,
2219                                                c->fmt_in->streams[source_index]->time_base,
2220                                                ctx->streams[pkt.stream_index]->time_base);
2221                     pkt.duration = av_rescale_q(pkt.duration,
2222                                                 c->fmt_in->streams[source_index]->time_base,
2223                                                 ctx->streams[pkt.stream_index]->time_base);
2224                     if (av_write_frame(ctx, &pkt) < 0) {
2225                         http_log("Error writing frame to output\n");
2226                         c->state = HTTPSTATE_SEND_DATA_TRAILER;
2227                     }
2228
2229                     len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2230                     c->cur_frame_bytes = len;
2231                     c->buffer_ptr = c->pb_buffer;
2232                     c->buffer_end = c->pb_buffer + len;
2233
2234                     codec->frame_number++;
2235                     if (len == 0) {
2236                         av_free_packet(&pkt);
2237                         goto redo;
2238                     }
2239                 }
2240                 av_free_packet(&pkt);
2241             }
2242         }
2243         break;
2244     default:
2245     case HTTPSTATE_SEND_DATA_TRAILER:
2246         /* last packet test ? */
2247         if (c->last_packet_sent || c->is_packetized)
2248             return -1;
2249         ctx = &c->fmt_ctx;
2250         /* prepare header */
2251         if (url_open_dyn_buf(&ctx->pb) < 0) {
2252             /* XXX: potential leak */
2253             return -1;
2254         }
2255         c->fmt_ctx.pb->is_streamed = 1;
2256         av_write_trailer(ctx);
2257         len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2258         c->buffer_ptr = c->pb_buffer;
2259         c->buffer_end = c->pb_buffer + len;
2260
2261         c->last_packet_sent = 1;
2262         break;
2263     }
2264     return 0;
2265 }
2266
2267 /* should convert the format at the same time */
2268 /* send data starting at c->buffer_ptr to the output connection
2269    (either UDP or TCP connection) */
2270 static int http_send_data(HTTPContext *c)
2271 {
2272     int len, ret;
2273
2274     for(;;) {
2275         if (c->buffer_ptr >= c->buffer_end) {
2276             ret = http_prepare_data(c);
2277             if (ret < 0)
2278                 return -1;
2279             else if (ret != 0)
2280                 /* state change requested */
2281                 break;
2282         } else {
2283             if (c->is_packetized) {
2284                 /* RTP data output */
2285                 len = c->buffer_end - c->buffer_ptr;
2286                 if (len < 4) {
2287                     /* fail safe - should never happen */
2288                 fail1:
2289                     c->buffer_ptr = c->buffer_end;
2290                     return 0;
2291                 }
2292                 len = (c->buffer_ptr[0] << 24) |
2293                     (c->buffer_ptr[1] << 16) |
2294                     (c->buffer_ptr[2] << 8) |
2295                     (c->buffer_ptr[3]);
2296                 if (len > (c->buffer_end - c->buffer_ptr))
2297                     goto fail1;
2298                 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2299                     /* nothing to send yet: we can wait */
2300                     return 0;
2301                 }
2302
2303                 c->data_count += len;
2304                 update_datarate(&c->datarate, c->data_count);
2305                 if (c->stream)
2306                     c->stream->bytes_served += len;
2307
2308                 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) {
2309                     /* RTP packets are sent inside the RTSP TCP connection */
2310                     ByteIOContext *pb;
2311                     int interleaved_index, size;
2312                     uint8_t header[4];
2313                     HTTPContext *rtsp_c;
2314
2315                     rtsp_c = c->rtsp_c;
2316                     /* if no RTSP connection left, error */
2317                     if (!rtsp_c)
2318                         return -1;
2319                     /* if already sending something, then wait. */
2320                     if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
2321                         break;
2322                     if (url_open_dyn_buf(&pb) < 0)
2323                         goto fail1;
2324                     interleaved_index = c->packet_stream_index * 2;
2325                     /* RTCP packets are sent at odd indexes */
2326                     if (c->buffer_ptr[1] == 200)
2327                         interleaved_index++;
2328                     /* write RTSP TCP header */
2329                     header[0] = '$';
2330                     header[1] = interleaved_index;
2331                     header[2] = len >> 8;
2332                     header[3] = len;
2333                     put_buffer(pb, header, 4);
2334                     /* write RTP packet data */
2335                     c->buffer_ptr += 4;
2336                     put_buffer(pb, c->buffer_ptr, len);
2337                     size = url_close_dyn_buf(pb, &c->packet_buffer);
2338                     /* prepare asynchronous TCP sending */
2339                     rtsp_c->packet_buffer_ptr = c->packet_buffer;
2340                     rtsp_c->packet_buffer_end = c->packet_buffer + size;
2341                     c->buffer_ptr += len;
2342
2343                     /* send everything we can NOW */
2344                     len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2345                                 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2346                     if (len > 0)
2347                         rtsp_c->packet_buffer_ptr += len;
2348                     if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2349                         /* if we could not send all the data, we will
2350                            send it later, so a new state is needed to
2351                            "lock" the RTSP TCP connection */
2352                         rtsp_c->state = RTSPSTATE_SEND_PACKET;
2353                         break;
2354                     } else
2355                         /* all data has been sent */
2356                         av_freep(&c->packet_buffer);
2357                 } else {
2358                     /* send RTP packet directly in UDP */
2359                     c->buffer_ptr += 4;
2360                     url_write(c->rtp_handles[c->packet_stream_index],
2361                               c->buffer_ptr, len);
2362                     c->buffer_ptr += len;
2363                     /* here we continue as we can send several packets per 10 ms slot */
2364                 }
2365             } else {
2366                 /* TCP data output */
2367                 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2368                 if (len < 0) {
2369                     if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2370                         ff_neterrno() != FF_NETERROR(EINTR))
2371                         /* error : close connection */
2372                         return -1;
2373                     else
2374                         return 0;
2375                 } else
2376                     c->buffer_ptr += len;
2377
2378                 c->data_count += len;
2379                 update_datarate(&c->datarate, c->data_count);
2380                 if (c->stream)
2381                     c->stream->bytes_served += len;
2382                 break;
2383             }
2384         }
2385     } /* for(;;) */
2386     return 0;
2387 }
2388
2389 static int http_start_receive_data(HTTPContext *c)
2390 {
2391     int fd;
2392
2393     if (c->stream->feed_opened)
2394         return -1;
2395
2396     /* Don't permit writing to this one */
2397     if (c->stream->readonly)
2398         return -1;
2399
2400     /* open feed */
2401     fd = open(c->stream->feed_filename, O_RDWR);
2402     if (fd < 0) {
2403         http_log("Error opening feeder file: %s\n", strerror(errno));
2404         return -1;
2405     }
2406     c->feed_fd = fd;
2407
2408     c->stream->feed_write_index = ffm_read_write_index(fd);
2409     c->stream->feed_size = lseek(fd, 0, SEEK_END);
2410     lseek(fd, 0, SEEK_SET);
2411
2412     /* init buffer input */
2413     c->buffer_ptr = c->buffer;
2414     c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2415     c->stream->feed_opened = 1;
2416     return 0;
2417 }
2418
2419 static int http_receive_data(HTTPContext *c)
2420 {
2421     HTTPContext *c1;
2422
2423     if (c->buffer_end > c->buffer_ptr) {
2424         int len;
2425
2426         len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2427         if (len < 0) {
2428             if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2429                 ff_neterrno() != FF_NETERROR(EINTR))
2430                 /* error : close connection */
2431                 goto fail;
2432         } else if (len == 0)
2433             /* end of connection : close it */
2434             goto fail;
2435         else {
2436             c->buffer_ptr += len;
2437             c->data_count += len;
2438             update_datarate(&c->datarate, c->data_count);
2439         }
2440     }
2441
2442     if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2443         if (c->buffer[0] != 'f' ||
2444             c->buffer[1] != 'm') {
2445             http_log("Feed stream has become desynchronized -- disconnecting\n");
2446             goto fail;
2447         }
2448     }
2449
2450     if (c->buffer_ptr >= c->buffer_end) {
2451         FFStream *feed = c->stream;
2452         /* a packet has been received : write it in the store, except
2453            if header */
2454         if (c->data_count > FFM_PACKET_SIZE) {
2455
2456             //            printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2457             /* XXX: use llseek or url_seek */
2458             lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
2459             if (write(c->feed_fd, c->buffer, FFM_PACKET_SIZE) < 0) {
2460                 http_log("Error writing to feed file: %s\n", strerror(errno));
2461                 goto fail;
2462             }
2463
2464             feed->feed_write_index += FFM_PACKET_SIZE;
2465             /* update file size */
2466             if (feed->feed_write_index > c->stream->feed_size)
2467                 feed->feed_size = feed->feed_write_index;
2468
2469             /* handle wrap around if max file size reached */
2470             if (c->stream->feed_max_size && feed->feed_write_index >= c->stream->feed_max_size)
2471                 feed->feed_write_index = FFM_PACKET_SIZE;
2472
2473             /* write index */
2474             ffm_write_write_index(c->feed_fd, feed->feed_write_index);
2475
2476             /* wake up any waiting connections */
2477             for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2478                 if (c1->state == HTTPSTATE_WAIT_FEED &&
2479                     c1->stream->feed == c->stream->feed)
2480                     c1->state = HTTPSTATE_SEND_DATA;
2481             }
2482         } else {
2483             /* We have a header in our hands that contains useful data */
2484             AVFormatContext *s = NULL;
2485             ByteIOContext *pb;
2486             AVInputFormat *fmt_in;
2487             int i;
2488
2489             url_open_buf(&pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
2490             pb->is_streamed = 1;
2491
2492             /* use feed output format name to find corresponding input format */
2493             fmt_in = av_find_input_format(feed->fmt->name);
2494             if (!fmt_in)
2495                 goto fail;
2496
2497             av_open_input_stream(&s, pb, c->stream->feed_filename, fmt_in, NULL);
2498
2499             /* Now we have the actual streams */
2500             if (s->nb_streams != feed->nb_streams) {
2501                 av_close_input_stream(s);
2502                 av_free(pb);
2503                 goto fail;
2504             }
2505
2506             for (i = 0; i < s->nb_streams; i++)
2507                 memcpy(feed->streams[i]->codec,
2508                        s->streams[i]->codec, sizeof(AVCodecContext));
2509
2510             av_close_input_stream(s);
2511             av_free(pb);
2512         }
2513         c->buffer_ptr = c->buffer;
2514     }
2515
2516     return 0;
2517  fail:
2518     c->stream->feed_opened = 0;
2519     close(c->feed_fd);
2520     /* wake up any waiting connections to stop waiting for feed */
2521     for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2522         if (c1->state == HTTPSTATE_WAIT_FEED &&
2523             c1->stream->feed == c->stream->feed)
2524             c1->state = HTTPSTATE_SEND_DATA_TRAILER;
2525     }
2526     return -1;
2527 }
2528
2529 /********************************************************************/
2530 /* RTSP handling */
2531
2532 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2533 {
2534     const char *str;
2535     time_t ti;
2536     char *p;
2537     char buf2[32];
2538
2539     switch(error_number) {
2540     case RTSP_STATUS_OK:
2541         str = "OK";
2542         break;
2543     case RTSP_STATUS_METHOD:
2544         str = "Method Not Allowed";
2545         break;
2546     case RTSP_STATUS_BANDWIDTH:
2547         str = "Not Enough Bandwidth";
2548         break;
2549     case RTSP_STATUS_SESSION:
2550         str = "Session Not Found";
2551         break;
2552     case RTSP_STATUS_STATE:
2553         str = "Method Not Valid in This State";
2554         break;
2555     case RTSP_STATUS_AGGREGATE:
2556         str = "Aggregate operation not allowed";
2557         break;
2558     case RTSP_STATUS_ONLY_AGGREGATE:
2559         str = "Only aggregate operation allowed";
2560         break;
2561     case RTSP_STATUS_TRANSPORT:
2562         str = "Unsupported transport";
2563         break;
2564     case RTSP_STATUS_INTERNAL:
2565         str = "Internal Server Error";
2566         break;
2567     case RTSP_STATUS_SERVICE:
2568         str = "Service Unavailable";
2569         break;
2570     case RTSP_STATUS_VERSION:
2571         str = "RTSP Version not supported";
2572         break;
2573     default:
2574         str = "Unknown Error";
2575         break;
2576     }
2577
2578     url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2579     url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2580
2581     /* output GMT time */
2582     ti = time(NULL);
2583     p = ctime(&ti);
2584     strcpy(buf2, p);
2585     p = buf2 + strlen(p) - 1;
2586     if (*p == '\n')
2587         *p = '\0';
2588     url_fprintf(c->pb, "Date: %s GMT\r\n", buf2);
2589 }
2590
2591 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2592 {
2593     rtsp_reply_header(c, error_number);
2594     url_fprintf(c->pb, "\r\n");
2595 }
2596
2597 static int rtsp_parse_request(HTTPContext *c)
2598 {
2599     const char *p, *p1, *p2;
2600     char cmd[32];
2601     char url[1024];
2602     char protocol[32];
2603     char line[1024];
2604     int len;
2605     RTSPHeader header1, *header = &header1;
2606
2607     c->buffer_ptr[0] = '\0';
2608     p = c->buffer;
2609
2610     get_word(cmd, sizeof(cmd), &p);
2611     get_word(url, sizeof(url), &p);
2612     get_word(protocol, sizeof(protocol), &p);
2613
2614     av_strlcpy(c->method, cmd, sizeof(c->method));
2615     av_strlcpy(c->url, url, sizeof(c->url));
2616     av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2617
2618     if (url_open_dyn_buf(&c->pb) < 0) {
2619         /* XXX: cannot do more */
2620         c->pb = NULL; /* safety */
2621         return -1;
2622     }
2623
2624     /* check version name */
2625     if (strcmp(protocol, "RTSP/1.0") != 0) {
2626         rtsp_reply_error(c, RTSP_STATUS_VERSION);
2627         goto the_end;
2628     }
2629
2630     /* parse each header line */
2631     memset(header, 0, sizeof(RTSPHeader));
2632     /* skip to next line */
2633     while (*p != '\n' && *p != '\0')
2634         p++;
2635     if (*p == '\n')
2636         p++;
2637     while (*p != '\0') {
2638         p1 = strchr(p, '\n');
2639         if (!p1)
2640             break;
2641         p2 = p1;
2642         if (p2 > p && p2[-1] == '\r')
2643             p2--;
2644         /* skip empty line */
2645         if (p2 == p)
2646             break;
2647         len = p2 - p;
2648         if (len > sizeof(line) - 1)
2649             len = sizeof(line) - 1;
2650         memcpy(line, p, len);
2651         line[len] = '\0';
2652         rtsp_parse_line(header, line);
2653         p = p1 + 1;
2654     }
2655
2656     /* handle sequence number */
2657     c->seq = header->seq;
2658
2659     if (!strcmp(cmd, "DESCRIBE"))
2660         rtsp_cmd_describe(c, url);
2661     else if (!strcmp(cmd, "OPTIONS"))
2662         rtsp_cmd_options(c, url);
2663     else if (!strcmp(cmd, "SETUP"))
2664         rtsp_cmd_setup(c, url, header);
2665     else if (!strcmp(cmd, "PLAY"))
2666         rtsp_cmd_play(c, url, header);
2667     else if (!strcmp(cmd, "PAUSE"))
2668         rtsp_cmd_pause(c, url, header);
2669     else if (!strcmp(cmd, "TEARDOWN"))
2670         rtsp_cmd_teardown(c, url, header);
2671     else
2672         rtsp_reply_error(c, RTSP_STATUS_METHOD);
2673
2674  the_end:
2675     len = url_close_dyn_buf(c->pb, &c->pb_buffer);
2676     c->pb = NULL; /* safety */
2677     if (len < 0) {
2678         /* XXX: cannot do more */
2679         return -1;
2680     }
2681     c->buffer_ptr = c->pb_buffer;
2682     c->buffer_end = c->pb_buffer + len;
2683     c->state = RTSPSTATE_SEND_REPLY;
2684     return 0;
2685 }
2686
2687 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
2688                                    struct in_addr my_ip)
2689 {
2690     AVFormatContext *avc;
2691     AVStream avs[MAX_STREAMS];
2692     int i;
2693
2694     avc =  av_alloc_format_context();
2695     if (avc == NULL) {
2696         return -1;
2697     }
2698     if (stream->title[0] != 0) {
2699         av_strlcpy(avc->title, stream->title, sizeof(avc->title));
2700     } else {
2701         av_strlcpy(avc->title, "No Title", sizeof(avc->title));
2702     }
2703     avc->nb_streams = stream->nb_streams;
2704     if (stream->is_multicast) {
2705         snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2706                  inet_ntoa(stream->multicast_ip),
2707                  stream->multicast_port, stream->multicast_ttl);
2708     }
2709
2710     for(i = 0; i < stream->nb_streams; i++) {
2711         avc->streams[i] = &avs[i];
2712         avc->streams[i]->codec = stream->streams[i]->codec;
2713     }
2714     *pbuffer = av_mallocz(2048);
2715     avf_sdp_create(&avc, 1, *pbuffer, 2048);
2716     av_free(avc);
2717
2718     return strlen(*pbuffer);
2719 }
2720
2721 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2722 {
2723 //    rtsp_reply_header(c, RTSP_STATUS_OK);
2724     url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2725     url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2726     url_fprintf(c->pb, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2727     url_fprintf(c->pb, "\r\n");
2728 }
2729
2730 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2731 {
2732     FFStream *stream;
2733     char path1[1024];
2734     const char *path;
2735     uint8_t *content;
2736     int content_length, len;
2737     struct sockaddr_in my_addr;
2738
2739     /* find which url is asked */
2740     url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2741     path = path1;
2742     if (*path == '/')
2743         path++;
2744
2745     for(stream = first_stream; stream != NULL; stream = stream->next) {
2746         if (!stream->is_feed &&
2747             stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2748             !strcmp(path, stream->filename)) {
2749             goto found;
2750         }
2751     }
2752     /* no stream found */
2753     rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2754     return;
2755
2756  found:
2757     /* prepare the media description in sdp format */
2758
2759     /* get the host IP */
2760     len = sizeof(my_addr);
2761     getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2762     content_length = prepare_sdp_description(stream, &content, my_addr.sin_addr);
2763     if (content_length < 0) {
2764         rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2765         return;
2766     }
2767     rtsp_reply_header(c, RTSP_STATUS_OK);
2768     url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
2769     url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
2770     url_fprintf(c->pb, "\r\n");
2771     put_buffer(c->pb, content, content_length);
2772 }
2773
2774 static HTTPContext *find_rtp_session(const char *session_id)
2775 {
2776     HTTPContext *c;
2777
2778     if (session_id[0] == '\0')
2779         return NULL;
2780
2781     for(c = first_http_ctx; c != NULL; c = c->next) {
2782         if (!strcmp(c->session_id, session_id))
2783             return c;
2784     }
2785     return NULL;
2786 }
2787
2788 static RTSPTransportField *find_transport(RTSPHeader *h, enum RTSPProtocol protocol)
2789 {
2790     RTSPTransportField *th;
2791     int i;
2792
2793     for(i=0;i<h->nb_transports;i++) {
2794         th = &h->transports[i];
2795         if (th->protocol == protocol)
2796             return th;
2797     }
2798     return NULL;
2799 }
2800
2801 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
2802                            RTSPHeader *h)
2803 {
2804     FFStream *stream;
2805     int stream_index, port;
2806     char buf[1024];
2807     char path1[1024];
2808     const char *path;
2809     HTTPContext *rtp_c;
2810     RTSPTransportField *th;
2811     struct sockaddr_in dest_addr;
2812     RTSPActionServerSetup setup;
2813
2814     /* find which url is asked */
2815     url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2816     path = path1;
2817     if (*path == '/')
2818         path++;
2819
2820     /* now check each stream */
2821     for(stream = first_stream; stream != NULL; stream = stream->next) {
2822         if (!stream->is_feed &&
2823             stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
2824             /* accept aggregate filenames only if single stream */
2825             if (!strcmp(path, stream->filename)) {
2826                 if (stream->nb_streams != 1) {
2827                     rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
2828                     return;
2829                 }
2830                 stream_index = 0;
2831                 goto found;
2832             }
2833
2834             for(stream_index = 0; stream_index < stream->nb_streams;
2835                 stream_index++) {
2836                 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2837                          stream->filename, stream_index);
2838                 if (!strcmp(path, buf))
2839                     goto found;
2840             }
2841         }
2842     }
2843     /* no stream found */
2844     rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2845     return;
2846  found:
2847
2848     /* generate session id if needed */
2849     if (h->session_id[0] == '\0')
2850         snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
2851                  av_random(&random_state), av_random(&random_state));
2852
2853     /* find rtp session, and create it if none found */
2854     rtp_c = find_rtp_session(h->session_id);
2855     if (!rtp_c) {
2856         /* always prefer UDP */
2857         th = find_transport(h, RTSP_PROTOCOL_RTP_UDP);
2858         if (!th) {
2859             th = find_transport(h, RTSP_PROTOCOL_RTP_TCP);
2860             if (!th) {
2861                 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2862                 return;
2863             }
2864         }
2865
2866         rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
2867                                    th->protocol);
2868         if (!rtp_c) {
2869             rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
2870             return;
2871         }
2872
2873         /* open input stream */
2874         if (open_input_stream(rtp_c, "") < 0) {
2875             rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2876             return;
2877         }
2878     }
2879
2880     /* test if stream is OK (test needed because several SETUP needs
2881        to be done for a given file) */
2882     if (rtp_c->stream != stream) {
2883         rtsp_reply_error(c, RTSP_STATUS_SERVICE);
2884         return;
2885     }
2886
2887     /* test if stream is already set up */
2888     if (rtp_c->rtp_ctx[stream_index]) {
2889         rtsp_reply_error(c, RTSP_STATUS_STATE);
2890         return;
2891     }
2892
2893     /* check transport */
2894     th = find_transport(h, rtp_c->rtp_protocol);
2895     if (!th || (th->protocol == RTSP_PROTOCOL_RTP_UDP &&
2896                 th->client_port_min <= 0)) {
2897         rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2898         return;
2899     }
2900
2901     /* setup default options */
2902     setup.transport_option[0] = '\0';
2903     dest_addr = rtp_c->from_addr;
2904     dest_addr.sin_port = htons(th->client_port_min);
2905
2906     /* setup stream */
2907     if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2908         rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2909         return;
2910     }
2911
2912     /* now everything is OK, so we can send the connection parameters */
2913     rtsp_reply_header(c, RTSP_STATUS_OK);
2914     /* session ID */
2915     url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2916
2917     switch(rtp_c->rtp_protocol) {
2918     case RTSP_PROTOCOL_RTP_UDP:
2919         port = rtp_get_local_port(rtp_c->rtp_handles[stream_index]);
2920         url_fprintf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2921                     "client_port=%d-%d;server_port=%d-%d",
2922                     th->client_port_min, th->client_port_min + 1,
2923                     port, port + 1);
2924         break;
2925     case RTSP_PROTOCOL_RTP_TCP:
2926         url_fprintf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2927                     stream_index * 2, stream_index * 2 + 1);
2928         break;
2929     default:
2930         break;
2931     }
2932     if (setup.transport_option[0] != '\0')
2933         url_fprintf(c->pb, ";%s", setup.transport_option);
2934     url_fprintf(c->pb, "\r\n");
2935
2936
2937     url_fprintf(c->pb, "\r\n");
2938 }
2939
2940
2941 /* find an rtp connection by using the session ID. Check consistency
2942    with filename */
2943 static HTTPContext *find_rtp_session_with_url(const char *url,
2944                                               const char *session_id)
2945 {
2946     HTTPContext *rtp_c;
2947     char path1[1024];
2948     const char *path;
2949     char buf[1024];
2950     int s;
2951
2952     rtp_c = find_rtp_session(session_id);
2953     if (!rtp_c)
2954         return NULL;
2955
2956     /* find which url is asked */
2957     url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2958     path = path1;
2959     if (*path == '/')
2960         path++;
2961     if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
2962     for(s=0; s<rtp_c->stream->nb_streams; ++s) {
2963       snprintf(buf, sizeof(buf), "%s/streamid=%d",
2964         rtp_c->stream->filename, s);
2965       if(!strncmp(path, buf, sizeof(buf))) {
2966     // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2967         return rtp_c;
2968       }
2969     }
2970     return NULL;
2971 }
2972
2973 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h)
2974 {
2975     HTTPContext *rtp_c;
2976
2977     rtp_c = find_rtp_session_with_url(url, h->session_id);
2978     if (!rtp_c) {
2979         rtsp_reply_error(c, RTSP_STATUS_SESSION);
2980         return;
2981     }
2982
2983     if (rtp_c->state != HTTPSTATE_SEND_DATA &&
2984         rtp_c->state != HTTPSTATE_WAIT_FEED &&
2985         rtp_c->state != HTTPSTATE_READY) {
2986         rtsp_reply_error(c, RTSP_STATUS_STATE);
2987         return;
2988     }
2989
2990 #if 0
2991     /* XXX: seek in stream */
2992     if (h->range_start != AV_NOPTS_VALUE) {
2993         printf("range_start=%0.3f\n", (double)h->range_start / AV_TIME_BASE);
2994         av_seek_frame(rtp_c->fmt_in, -1, h->range_start);
2995     }
2996 #endif
2997
2998     rtp_c->state = HTTPSTATE_SEND_DATA;
2999
3000     /* now everything is OK, so we can send the connection parameters */
3001     rtsp_reply_header(c, RTSP_STATUS_OK);
3002     /* session ID */
3003     url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3004     url_fprintf(c->pb, "\r\n");
3005 }
3006
3007 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h)
3008 {
3009     HTTPContext *rtp_c;
3010
3011     rtp_c = find_rtp_session_with_url(url, h->session_id);
3012     if (!rtp_c) {
3013         rtsp_reply_error(c, RTSP_STATUS_SESSION);
3014         return;
3015     }
3016
3017     if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3018         rtp_c->state != HTTPSTATE_WAIT_FEED) {
3019         rtsp_reply_error(c, RTSP_STATUS_STATE);
3020         return;
3021     }
3022
3023     rtp_c->state = HTTPSTATE_READY;
3024     rtp_c->first_pts = AV_NOPTS_VALUE;
3025     /* now everything is OK, so we can send the connection parameters */
3026     rtsp_reply_header(c, RTSP_STATUS_OK);
3027     /* session ID */
3028     url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3029     url_fprintf(c->pb, "\r\n");
3030 }
3031
3032 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
3033 {
3034     HTTPContext *rtp_c;
3035     char session_id[32];
3036
3037     rtp_c = find_rtp_session_with_url(url, h->session_id);
3038     if (!rtp_c) {
3039         rtsp_reply_error(c, RTSP_STATUS_SESSION);
3040         return;
3041     }
3042
3043     av_strlcpy(session_id, rtp_c->session_id, sizeof(session_id));
3044
3045     /* abort the session */
3046     close_connection(rtp_c);
3047
3048     /* now everything is OK, so we can send the connection parameters */
3049     rtsp_reply_header(c, RTSP_STATUS_OK);
3050     /* session ID */
3051     url_fprintf(c->pb, "Session: %s\r\n", session_id);
3052     url_fprintf(c->pb, "\r\n");
3053 }
3054
3055
3056 /********************************************************************/
3057 /* RTP handling */
3058
3059 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3060                                        FFStream *stream, const char *session_id,
3061                                        enum RTSPProtocol rtp_protocol)
3062 {
3063     HTTPContext *c = NULL;
3064     const char *proto_str;
3065
3066     /* XXX: should output a warning page when coming
3067        close to the connection limit */
3068     if (nb_connections >= nb_max_connections)
3069         goto fail;
3070
3071     /* add a new connection */
3072     c = av_mallocz(sizeof(HTTPContext));
3073     if (!c)
3074         goto fail;
3075
3076     c->fd = -1;
3077     c->poll_entry = NULL;
3078     c->from_addr = *from_addr;
3079     c->buffer_size = IOBUFFER_INIT_SIZE;
3080     c->buffer = av_malloc(c->buffer_size);
3081     if (!c->buffer)
3082         goto fail;
3083     nb_connections++;
3084     c->stream = stream;
3085     av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
3086     c->state = HTTPSTATE_READY;
3087     c->is_packetized = 1;
3088     c->rtp_protocol = rtp_protocol;
3089
3090     /* protocol is shown in statistics */
3091     switch(c->rtp_protocol) {
3092     case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3093         proto_str = "MCAST";
3094         break;
3095     case RTSP_PROTOCOL_RTP_UDP:
3096         proto_str = "UDP";
3097         break;
3098     case RTSP_PROTOCOL_RTP_TCP:
3099         proto_str = "TCP";
3100         break;
3101     default:
3102         proto_str = "???";
3103         break;
3104     }
3105     av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
3106     av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
3107
3108     current_bandwidth += stream->bandwidth;
3109
3110     c->next = first_http_ctx;
3111     first_http_ctx = c;
3112     return c;
3113
3114  fail:
3115     if (c) {
3116         av_free(c->buffer);
3117         av_free(c);
3118     }
3119     return NULL;
3120 }
3121
3122 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3123    command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3124    used. */
3125 static int rtp_new_av_stream(HTTPContext *c,
3126                              int stream_index, struct sockaddr_in *dest_addr,
3127                              HTTPContext *rtsp_c)
3128 {
3129     AVFormatContext *ctx;
3130     AVStream *st;
3131     char *ipaddr;
3132     URLContext *h = NULL;
3133     uint8_t *dummy_buf;
3134     char buf2[32];
3135     int max_packet_size;
3136
3137     /* now we can open the relevant output stream */
3138     ctx = av_alloc_format_context();
3139     if (!ctx)
3140         return -1;
3141     ctx->oformat = guess_format("rtp", NULL, NULL);
3142
3143     st = av_mallocz(sizeof(AVStream));
3144     if (!st)
3145         goto fail;
3146     st->codec= avcodec_alloc_context();
3147     ctx->nb_streams = 1;
3148     ctx->streams[0] = st;
3149
3150     if (!c->stream->feed ||
3151         c->stream->feed == c->stream)
3152         memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3153     else
3154         memcpy(st,
3155                c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3156                sizeof(AVStream));
3157     st->priv_data = NULL;
3158
3159     /* build destination RTP address */
3160     ipaddr = inet_ntoa(dest_addr->sin_addr);
3161
3162     switch(c->rtp_protocol) {
3163     case RTSP_PROTOCOL_RTP_UDP:
3164     case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3165         /* RTP/UDP case */
3166
3167         /* XXX: also pass as parameter to function ? */
3168         if (c->stream->is_multicast) {
3169             int ttl;
3170             ttl = c->stream->multicast_ttl;
3171             if (!ttl)
3172                 ttl = 16;
3173             snprintf(ctx->filename, sizeof(ctx->filename),
3174                      "rtp://%s:%d?multicast=1&ttl=%d",
3175                      ipaddr, ntohs(dest_addr->sin_port), ttl);
3176         } else {
3177             snprintf(ctx->filename, sizeof(ctx->filename),
3178                      "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3179         }
3180
3181         if (url_open(&h, ctx->filename, URL_WRONLY) < 0)
3182             goto fail;
3183         c->rtp_handles[stream_index] = h;
3184         max_packet_size = url_get_max_packet_size(h);
3185         break;
3186     case RTSP_PROTOCOL_RTP_TCP:
3187         /* RTP/TCP case */
3188         c->rtsp_c = rtsp_c;
3189         max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3190         break;
3191     default:
3192         goto fail;
3193     }
3194
3195     http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3196              ipaddr, ntohs(dest_addr->sin_port),
3197              ctime1(buf2),
3198              c->stream->filename, stream_index, c->protocol);
3199
3200     /* normally, no packets should be output here, but the packet size may be checked */
3201     if (url_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0) {
3202         /* XXX: close stream */
3203         goto fail;
3204     }
3205     av_set_parameters(ctx, NULL);
3206     if (av_write_header(ctx) < 0) {
3207     fail:
3208         if (h)
3209             url_close(h);
3210         av_free(ctx);
3211         return -1;
3212     }
3213     url_close_dyn_buf(ctx->pb, &dummy_buf);
3214     av_free(dummy_buf);
3215
3216     c->rtp_ctx[stream_index] = ctx;
3217     return 0;
3218 }
3219
3220 /********************************************************************/
3221 /* ffserver initialization */
3222
3223 static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
3224 {
3225     AVStream *fst;
3226
3227     fst = av_mallocz(sizeof(AVStream));
3228     if (!fst)
3229         return NULL;
3230     fst->codec= avcodec_alloc_context();
3231     fst->priv_data = av_mallocz(sizeof(FeedData));
3232     memcpy(fst->codec, codec, sizeof(AVCodecContext));
3233     fst->index = stream->nb_streams;
3234     av_set_pts_info(fst, 33, 1, 90000);
3235     stream->streams[stream->nb_streams++] = fst;
3236     return fst;
3237 }
3238
3239 /* return the stream number in the feed */
3240 static int add_av_stream(FFStream *feed, AVStream *st)
3241 {
3242     AVStream *fst;
3243     AVCodecContext *av, *av1;
3244     int i;
3245
3246     av = st->codec;
3247     for(i=0;i<feed->nb_streams;i++) {
3248         st = feed->streams[i];
3249         av1 = st->codec;
3250         if (av1->codec_id == av->codec_id &&
3251             av1->codec_type == av->codec_type &&
3252             av1->bit_rate == av->bit_rate) {
3253
3254             switch(av->codec_type) {
3255             case CODEC_TYPE_AUDIO:
3256                 if (av1->channels == av->channels &&
3257                     av1->sample_rate == av->sample_rate)
3258                     goto found;
3259                 break;
3260             case CODEC_TYPE_VIDEO:
3261                 if (av1->width == av->width &&
3262                     av1->height == av->height &&
3263                     av1->time_base.den == av->time_base.den &&
3264                     av1->time_base.num == av->time_base.num &&
3265                     av1->gop_size == av->gop_size)
3266                     goto found;
3267                 break;
3268             default:
3269                 abort();
3270             }
3271         }
3272     }
3273
3274     fst = add_av_stream1(feed, av);
3275     if (!fst)
3276         return -1;
3277     return feed->nb_streams - 1;
3278  found:
3279     return i;
3280 }
3281
3282 static void remove_stream(FFStream *stream)
3283 {
3284     FFStream **ps;
3285     ps = &first_stream;
3286     while (*ps != NULL) {
3287         if (*ps == stream)
3288             *ps = (*ps)->next;
3289         else
3290             ps = &(*ps)->next;
3291     }
3292 }
3293
3294 /* specific mpeg4 handling : we extract the raw parameters */
3295 static void extract_mpeg4_header(AVFormatContext *infile)
3296 {
3297     int mpeg4_count, i, size;
3298     AVPacket pkt;
3299     AVStream *st;
3300     const uint8_t *p;
3301
3302     mpeg4_count = 0;
3303     for(i=0;i<infile->nb_streams;i++) {
3304         st = infile->streams[i];
3305         if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3306             st->codec->extradata_size == 0) {
3307             mpeg4_count++;
3308         }
3309     }
3310     if (!mpeg4_count)
3311         return;
3312
3313     printf("MPEG4 without extra data: trying to find header in %s\n", infile->filename);
3314     while (mpeg4_count > 0) {
3315         if (av_read_packet(infile, &pkt) < 0)
3316             break;
3317         st = infile->streams[pkt.stream_index];
3318         if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3319             st->codec->extradata_size == 0) {
3320             av_freep(&st->codec->extradata);
3321             /* fill extradata with the header */
3322             /* XXX: we make hard suppositions here ! */
3323             p = pkt.data;
3324             while (p < pkt.data + pkt.size - 4) {
3325                 /* stop when vop header is found */
3326                 if (p[0] == 0x00 && p[1] == 0x00 &&
3327                     p[2] == 0x01 && p[3] == 0xb6) {
3328                     size = p - pkt.data;
3329                     //                    av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3330                     st->codec->extradata = av_malloc(size);
3331                     st->codec->extradata_size = size;
3332                     memcpy(st->codec->extradata, pkt.data, size);
3333                     break;
3334                 }
3335                 p++;
3336             }
3337             mpeg4_count--;
3338         }
3339         av_free_packet(&pkt);
3340     }
3341 }
3342
3343 /* compute the needed AVStream for each file */
3344 static void build_file_streams(void)
3345 {
3346     FFStream *stream, *stream_next;
3347     AVFormatContext *infile;
3348     int i, ret;
3349
3350     /* gather all streams */
3351     for(stream = first_stream; stream != NULL; stream = stream_next) {
3352         stream_next = stream->next;
3353         if (stream->stream_type == STREAM_TYPE_LIVE &&
3354             !stream->feed) {
3355             /* the stream comes from a file */
3356             /* try to open the file */
3357             /* open stream */
3358             stream->ap_in = av_mallocz(sizeof(AVFormatParameters));
3359             if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
3360                 /* specific case : if transport stream output to RTP,
3361                    we use a raw transport stream reader */
3362                 stream->ap_in->mpeg2ts_raw = 1;
3363                 stream->ap_in->mpeg2ts_compute_pcr = 1;
3364             }
3365
3366             if ((ret = av_open_input_file(&infile, stream->feed_filename,
3367                                           stream->ifmt, 0, stream->ap_in)) < 0) {
3368                 http_log("could not open %s: %d\n", stream->feed_filename, ret);
3369                 /* remove stream (no need to spend more time on it) */
3370             fail:
3371                 remove_stream(stream);
3372             } else {
3373                 /* find all the AVStreams inside and reference them in
3374                    'stream' */
3375                 if (av_find_stream_info(infile) < 0) {
3376                     http_log("Could not find codec parameters from '%s'\n",
3377                              stream->feed_filename);
3378                     av_close_input_file(infile);
3379                     goto fail;
3380                 }
3381                 extract_mpeg4_header(infile);
3382
3383                 for(i=0;i<infile->nb_streams;i++)
3384                     add_av_stream1(stream, infile->streams[i]->codec);
3385
3386                 av_close_input_file(infile);
3387             }
3388         }
3389     }
3390 }
3391
3392 /* compute the needed AVStream for each feed */
3393 static void build_feed_streams(void)
3394 {
3395     FFStream *stream, *feed;
3396     int i;
3397
3398     /* gather all streams */
3399     for(stream = first_stream; stream != NULL; stream = stream->next) {
3400         feed = stream->feed;
3401         if (feed) {
3402             if (!stream->is_feed) {
3403                 /* we handle a stream coming from a feed */
3404                 for(i=0;i<stream->nb_streams;i++)
3405                     stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
3406             }
3407         }
3408     }
3409
3410     /* gather all streams */
3411     for(stream = first_stream; stream != NULL; stream = stream->next) {
3412         feed = stream->feed;
3413         if (feed) {
3414             if (stream->is_feed) {
3415                 for(i=0;i<stream->nb_streams;i++)
3416                     stream->feed_streams[i] = i;
3417             }
3418         }
3419     }
3420
3421     /* create feed files if needed */
3422     for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
3423         int fd;
3424
3425         if (url_exist(feed->feed_filename)) {
3426             /* See if it matches */
3427             AVFormatContext *s;
3428             int matches = 0;
3429
3430             if (av_open_input_file(&s, feed->feed_filename, NULL, FFM_PACKET_SIZE, NULL) >= 0) {
3431                 /* Now see if it matches */
3432                 if (s->nb_streams == feed->nb_streams) {
3433                     matches = 1;
3434                     for(i=0;i<s->nb_streams;i++) {
3435                         AVStream *sf, *ss;
3436                         sf = feed->streams[i];
3437                         ss = s->streams[i];
3438
3439                         if (sf->index != ss->index ||
3440                             sf->id != ss->id) {
3441                             printf("Index & Id do not match for stream %d (%s)\n",
3442                                    i, feed->feed_filename);
3443                             matches = 0;
3444                         } else {
3445                             AVCodecContext *ccf, *ccs;
3446
3447                             ccf = sf->codec;
3448                             ccs = ss->codec;
3449 #define CHECK_CODEC(x)  (ccf->x != ccs->x)
3450
3451                             if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
3452                                 printf("Codecs do not match for stream %d\n", i);
3453                                 matches = 0;
3454                             } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3455                                 printf("Codec bitrates do not match for stream %d\n", i);
3456                                 matches = 0;
3457                             } else if (ccf->codec_type == CODEC_TYPE_VIDEO) {
3458                                 if (CHECK_CODEC(time_base.den) ||
3459                                     CHECK_CODEC(time_base.num) ||
3460                                     CHECK_CODEC(width) ||
3461                                     CHECK_CODEC(height)) {
3462                                     printf("Codec width, height and framerate do not match for stream %d\n", i);
3463                                     matches = 0;
3464                                 }
3465                             } else if (ccf->codec_type == CODEC_TYPE_AUDIO) {
3466                                 if (CHECK_CODEC(sample_rate) ||
3467                                     CHECK_CODEC(channels) ||
3468                                     CHECK_CODEC(frame_size)) {
3469                                     printf("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3470                                     matches = 0;
3471                                 }
3472                             } else {
3473                                 printf("Unknown codec type\n");
3474                                 matches = 0;
3475                             }
3476                         }
3477                         if (!matches)
3478                             break;
3479                     }
3480                 } else
3481                     printf("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3482                         feed->feed_filename, s->nb_streams, feed->nb_streams);
3483
3484                 av_close_input_file(s);
3485             } else
3486                 printf("Deleting feed file '%s' as it appears to be corrupt\n",
3487                         feed->feed_filename);
3488
3489             if (!matches) {
3490                 if (feed->readonly) {
3491                     printf("Unable to delete feed file '%s' as it is marked readonly\n",
3492                         feed->feed_filename);
3493                     exit(1);
3494                 }
3495                 unlink(feed->feed_filename);
3496             }
3497         }
3498         if (!url_exist(feed->feed_filename)) {
3499             AVFormatContext s1, *s = &s1;
3500
3501             if (feed->readonly) {
3502                 printf("Unable to create feed file '%s' as it is marked readonly\n",
3503                     feed->feed_filename);
3504                 exit(1);
3505             }
3506
3507             /* only write the header of the ffm file */
3508             if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
3509                 http_log("Could not open output feed file '%s'\n",
3510                          feed->feed_filename);
3511                 exit(1);
3512             }
3513             s->oformat = feed->fmt;
3514             s->nb_streams = feed->nb_streams;
3515             for(i=0;i<s->nb_streams;i++) {
3516                 AVStream *st;
3517                 st = feed->streams[i];
3518                 s->streams[i] = st;
3519             }
3520             av_set_parameters(s, NULL);
3521             if (av_write_header(s) < 0) {
3522                 http_log("Container doesn't supports the required parameters\n");
3523                 exit(1);
3524             }
3525             /* XXX: need better api */
3526             av_freep(&s->priv_data);
3527             url_fclose(s->pb);
3528         }
3529         /* get feed size and write index */
3530         fd = open(feed->feed_filename, O_RDONLY);
3531         if (fd < 0) {
3532             http_log("Could not open output feed file '%s'\n",
3533                     feed->feed_filename);
3534             exit(1);
3535         }
3536
3537         feed->feed_write_index = ffm_read_write_index(fd);
3538         feed->feed_size = lseek(fd, 0, SEEK_END);
3539         /* ensure that we do not wrap before the end of file */
3540         if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3541             feed->feed_max_size = feed->feed_size;
3542
3543         close(fd);
3544     }
3545 }
3546
3547 /* compute the bandwidth used by each stream */
3548 static void compute_bandwidth(void)
3549 {
3550     unsigned bandwidth;
3551     int i;
3552     FFStream *stream;
3553
3554     for(stream = first_stream; stream != NULL; stream = stream->next) {
3555         bandwidth = 0;
3556         for(i=0;i<stream->nb_streams;i++) {
3557             AVStream *st = stream->streams[i];
3558             switch(st->codec->codec_type) {
3559             case CODEC_TYPE_AUDIO:
3560             case CODEC_TYPE_VIDEO:
3561                 bandwidth += st->codec->bit_rate;
3562                 break;
3563             default:
3564                 break;
3565             }
3566         }
3567         stream->bandwidth = (bandwidth + 999) / 1000;
3568     }
3569 }
3570
3571 static void get_arg(char *buf, int buf_size, const char **pp)
3572 {
3573     const char *p;
3574     char *q;
3575     int quote;
3576
3577     p = *pp;
3578     while (isspace(*p)) p++;
3579     q = buf;
3580     quote = 0;
3581     if (*p == '\"' || *p == '\'')
3582         quote = *p++;
3583     for(;;) {
3584         if (quote) {
3585             if (*p == quote)
3586                 break;
3587         } else {
3588             if (isspace(*p))
3589                 break;
3590         }
3591         if (*p == '\0')
3592             break;
3593         if ((q - buf) < buf_size - 1)
3594             *q++ = *p;
3595         p++;
3596     }
3597     *q = '\0';
3598     if (quote && *p == quote)
3599         p++;
3600     *pp = p;
3601 }
3602
3603 /* add a codec and set the default parameters */
3604 static void add_codec(FFStream *stream, AVCodecContext *av)
3605 {
3606     AVStream *st;
3607
3608     /* compute default parameters */
3609     switch(av->codec_type) {
3610     case CODEC_TYPE_AUDIO:
3611         if (av->bit_rate == 0)
3612             av->bit_rate = 64000;
3613         if (av->sample_rate == 0)
3614             av->sample_rate = 22050;
3615         if (av->channels == 0)
3616             av->channels = 1;
3617         break;
3618     case CODEC_TYPE_VIDEO:
3619         if (av->bit_rate == 0)
3620             av->bit_rate = 64000;
3621         if (av->time_base.num == 0){
3622             av->time_base.den = 5;
3623             av->time_base.num = 1;
3624         }
3625         if (av->width == 0 || av->height == 0) {
3626             av->width = 160;
3627             av->height = 128;
3628         }
3629         /* Bitrate tolerance is less for streaming */
3630         if (av->bit_rate_tolerance == 0)
3631             av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
3632                       (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
3633         if (av->qmin == 0)
3634             av->qmin = 3;
3635         if (av->qmax == 0)
3636             av->qmax = 31;
3637         if (av->max_qdiff == 0)
3638             av->max_qdiff = 3;
3639         av->qcompress = 0.5;
3640         av->qblur = 0.5;
3641
3642         if (!av->nsse_weight)
3643             av->nsse_weight = 8;
3644
3645         av->frame_skip_cmp = FF_CMP_DCTMAX;
3646         av->me_method = ME_EPZS;
3647         av->rc_buffer_aggressivity = 1.0;
3648
3649         if (!av->rc_eq)
3650             av->rc_eq = "tex^qComp";
3651         if (!av->i_quant_factor)
3652             av->i_quant_factor = -0.8;
3653         if (!av->b_quant_factor)
3654             av->b_quant_factor = 1.25;
3655         if (!av->b_quant_offset)
3656             av->b_quant_offset = 1.25;
3657         if (!av->rc_max_rate)
3658             av->rc_max_rate = av->bit_rate * 2;
3659
3660         if (av->rc_max_rate && !av->rc_buffer_size) {
3661             av->rc_buffer_size = av->rc_max_rate;
3662         }
3663
3664
3665         break;
3666     default:
3667         abort();
3668     }
3669
3670     st = av_mallocz(sizeof(AVStream));
3671     if (!st)
3672         return;
3673     st->codec = avcodec_alloc_context();
3674     stream->streams[stream->nb_streams++] = st;
3675     memcpy(st->codec, av, sizeof(AVCodecContext));
3676 }
3677
3678 static int opt_audio_codec(const char *arg)
3679 {
3680     AVCodec *p= avcodec_find_encoder_by_name(arg);
3681
3682     if (p == NULL || p->type != CODEC_TYPE_AUDIO)
3683         return CODEC_ID_NONE;
3684
3685     return p->id;
3686 }
3687
3688 static int opt_video_codec(const char *arg)
3689 {
3690     AVCodec *p= avcodec_find_encoder_by_name(arg);
3691
3692     if (p == NULL || p->type != CODEC_TYPE_VIDEO)
3693         return CODEC_ID_NONE;
3694
3695     return p->id;
3696 }
3697
3698 /* simplistic plugin support */
3699
3700 #ifdef HAVE_DLOPEN
3701 static void load_module(const char *filename)
3702 {
3703     void *dll;
3704     void (*init_func)(void);
3705     dll = dlopen(filename, RTLD_NOW);
3706     if (!dll) {
3707         fprintf(stderr, "Could not load module '%s' - %s\n",
3708                 filename, dlerror());
3709         return;
3710     }
3711
3712     init_func = dlsym(dll, "ffserver_module_init");
3713     if (!init_func) {
3714         fprintf(stderr,
3715                 "%s: init function 'ffserver_module_init()' not found\n",
3716                 filename);
3717         dlclose(dll);
3718     }
3719
3720     init_func();
3721 }
3722 #endif
3723
3724 static int opt_default(const char *opt, const char *arg,
3725                        AVCodecContext *avctx, int type)
3726 {
3727     const AVOption *o  = NULL;
3728     const AVOption *o2 = av_find_opt(avctx, opt, NULL, type, type);
3729     if(o2)
3730         o = av_set_string(avctx, opt, arg);
3731     if(!o)
3732         return -1;
3733     return 0;
3734 }
3735
3736 static int parse_ffconfig(const char *filename)
3737 {
3738     FILE *f;
3739     char line[1024];
3740     char cmd[64];
3741     char arg[1024];
3742     const char *p;
3743     int val, errors, line_num;
3744     FFStream **last_stream, *stream, *redirect;
3745     FFStream **last_feed, *feed;
3746     AVCodecContext audio_enc, video_enc;
3747     int audio_id, video_id;
3748
3749     f = fopen(filename, "r");
3750     if (!f) {
3751         perror(filename);
3752         return -1;
3753     }
3754
3755     errors = 0;
3756     line_num = 0;
3757     first_stream = NULL;
3758     last_stream = &first_stream;
3759     first_feed = NULL;
3760     last_feed = &first_feed;
3761     stream = NULL;
3762     feed = NULL;
3763     redirect = NULL;
3764     audio_id = CODEC_ID_NONE;
3765     video_id = CODEC_ID_NONE;
3766     for(;;) {
3767         if (fgets(line, sizeof(line), f) == NULL)
3768             break;
3769         line_num++;
3770         p = line;
3771         while (isspace(*p))
3772             p++;
3773         if (*p == '\0' || *p == '#')
3774             continue;
3775
3776         get_arg(cmd, sizeof(cmd), &p);
3777
3778         if (!strcasecmp(cmd, "Port")) {
3779             get_arg(arg, sizeof(arg), &p);
3780             val = atoi(arg);
3781             if (val < 1 || val > 65536) {
3782                 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3783                         filename, line_num, arg);
3784                 errors++;
3785             }
3786             my_http_addr.sin_port = htons(val);
3787         } else if (!strcasecmp(cmd, "BindAddress")) {
3788             get_arg(arg, sizeof(arg), &p);
3789             if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
3790                 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3791                         filename, line_num, arg);
3792                 errors++;
3793             }
3794         } else if (!strcasecmp(cmd, "NoDaemon")) {
3795             ffserver_daemon = 0;
3796         } else if (!strcasecmp(cmd, "RTSPPort")) {
3797             get_arg(arg, sizeof(arg), &p);
3798             val = atoi(arg);
3799             if (val < 1 || val > 65536) {
3800                 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3801                         filename, line_num, arg);
3802                 errors++;
3803             }
3804             my_rtsp_addr.sin_port = htons(atoi(arg));
3805         } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
3806             get_arg(arg, sizeof(arg), &p);
3807             if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
3808                 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3809                         filename, line_num, arg);
3810                 errors++;
3811             }
3812         } else if (!strcasecmp(cmd, "MaxClients")) {
3813             get_arg(arg, sizeof(arg), &p);
3814             val = atoi(arg);
3815             if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
3816                 fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
3817                         filename, line_num, arg);
3818                 errors++;
3819             } else {
3820                 nb_max_connections = val;
3821             }
3822         } else if (!strcasecmp(cmd, "MaxBandwidth")) {
3823             int64_t llval;
3824             get_arg(arg, sizeof(arg), &p);
3825             llval = atoll(arg);
3826             if (llval < 10 || llval > 10000000) {
3827                 fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
3828                         filename, line_num, arg);
3829                 errors++;
3830             } else
3831                 max_bandwidth = llval;
3832         } else if (!strcasecmp(cmd, "CustomLog")) {
3833             if (!ffserver_debug)
3834                 get_arg(logfilename, sizeof(logfilename), &p);
3835         } else if (!strcasecmp(cmd, "<Feed")) {
3836             /*********************************************/
3837             /* Feed related options */
3838             char *q;
3839             if (stream || feed) {
3840                 fprintf(stderr, "%s:%d: Already in a tag\n",
3841                         filename, line_num);
3842             } else {
3843                 feed = av_mallocz(sizeof(FFStream));
3844                 /* add in stream list */
3845                 *last_stream = feed;
3846                 last_stream = &feed->next;
3847                 /* add in feed list */
3848                 *last_feed = feed;
3849                 last_feed = &feed->next_feed;
3850
3851                 get_arg(feed->filename, sizeof(feed->filename), &p);
3852                 q = strrchr(feed->filename, '>');
3853                 if (*q)
3854                     *q = '\0';
3855                 feed->fmt = guess_format("ffm", NULL, NULL);
3856                 /* defaut feed file */
3857                 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
3858                          "/tmp/%s.ffm", feed->filename);
3859                 feed->feed_max_size = 5 * 1024 * 1024;
3860                 feed->is_feed = 1;
3861                 feed->feed = feed; /* self feeding :-) */
3862             }
3863         } else if (!strcasecmp(cmd, "Launch")) {
3864             if (feed) {
3865                 int i;
3866
3867                 feed->child_argv = av_mallocz(64 * sizeof(char *));
3868
3869                 for (i = 0; i < 62; i++) {
3870                     get_arg(arg, sizeof(arg), &p);
3871                     if (!arg[0])
3872                         break;
3873
3874                     feed->child_argv[i] = av_strdup(arg);
3875                 }
3876
3877                 feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
3878
3879                 snprintf(feed->child_argv[i], 30+strlen(feed->filename),
3880                     "http://%s:%d/%s",
3881                         (my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
3882                     inet_ntoa(my_http_addr.sin_addr),
3883                     ntohs(my_http_addr.sin_port), feed->filename);
3884
3885                 if (ffserver_debug)
3886                 {
3887                     int j;
3888                     fprintf(stdout, "Launch commandline: ");
3889                     for (j = 0; j <= i; j++)
3890                         fprintf(stdout, "%s ", feed->child_argv[j]);
3891                     fprintf(stdout, "\n");
3892                 }
3893             }
3894         } else if (!strcasecmp(cmd, "ReadOnlyFile")) {
3895             if (feed) {
3896                 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3897                 feed->readonly = 1;
3898             } else if (stream) {
3899                 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3900             }
3901         } else if (!strcasecmp(cmd, "File")) {
3902             if (feed) {
3903                 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3904             } else if (stream)
3905                 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3906         } else if (!strcasecmp(cmd, "FileMaxSize")) {
3907             if (feed) {
3908                 char *p1;
3909                 double fsize;
3910
3911                 get_arg(arg, sizeof(arg), &p);
3912                 p1 = arg;
3913                 fsize = strtod(p1, &p1);
3914                 switch(toupper(*p1)) {
3915                 case 'K':
3916                     fsize *= 1024;
3917                     break;
3918                 case 'M':
3919                     fsize *= 1024 * 1024;
3920                     break;
3921                 case 'G':
3922                     fsize *= 1024 * 1024 * 1024;
3923                     break;
3924                 }
3925                 feed->feed_max_size = (int64_t)fsize;
3926             }
3927         } else if (!strcasecmp(cmd, "</Feed>")) {
3928             if (!feed) {
3929                 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
3930                         filename, line_num);
3931                 errors++;
3932             }
3933             feed = NULL;
3934         } else if (!strcasecmp(cmd, "<Stream")) {
3935             /*********************************************/
3936             /* Stream related options */
3937             char *q;
3938             if (stream || feed) {
3939                 fprintf(stderr, "%s:%d: Already in a tag\n",
3940                         filename, line_num);
3941             } else {
3942                 const AVClass *class;
3943                 stream = av_mallocz(sizeof(FFStream));
3944                 *last_stream = stream;
3945                 last_stream = &stream->next;
3946
3947                 get_arg(stream->filename, sizeof(stream->filename), &p);
3948                 q = strrchr(stream->filename, '>');
3949                 if (*q)
3950                     *q = '\0';
3951                 stream->fmt = guess_stream_format(NULL, stream->filename, NULL);
3952                 /* fetch avclass so AVOption works
3953                  * FIXME try to use avcodec_get_context_defaults2
3954                  * without changing defaults too much */
3955                 avcodec_get_context_defaults(&video_enc);
3956                 class = video_enc.av_class;
3957                 memset(&audio_enc, 0, sizeof(AVCodecContext));
3958                 memset(&video_enc, 0, sizeof(AVCodecContext));
3959                 audio_enc.av_class = class;
3960                 video_enc.av_class = class;
3961                 audio_id = CODEC_ID_NONE;
3962                 video_id = CODEC_ID_NONE;
3963                 if (stream->fmt) {
3964                     audio_id = stream->fmt->audio_codec;
3965                     video_id = stream->fmt->video_codec;
3966                 }
3967             }
3968         } else if (!strcasecmp(cmd, "Feed")) {
3969             get_arg(arg, sizeof(arg), &p);
3970             if (stream) {
3971                 FFStream *sfeed;
3972
3973                 sfeed = first_feed;
3974                 while (sfeed != NULL) {
3975                     if (!strcmp(sfeed->filename, arg))
3976                         break;
3977                     sfeed = sfeed->next_feed;
3978                 }
3979                 if (!sfeed)
3980                     fprintf(stderr, "%s:%d: feed '%s' not defined\n",
3981                             filename, line_num, arg);
3982                 else
3983                     stream->feed = sfeed;
3984             }
3985         } else if (!strcasecmp(cmd, "Format")) {
3986             get_arg(arg, sizeof(arg), &p);
3987             if (stream) {
3988             if (!strcmp(arg, "status")) {
3989                 stream->stream_type = STREAM_TYPE_STATUS;
3990                 stream->fmt = NULL;
3991             } else {
3992                 stream->stream_type = STREAM_TYPE_LIVE;
3993                 /* jpeg cannot be used here, so use single frame jpeg */
3994                 if (!strcmp(arg, "jpeg"))
3995                     strcpy(arg, "mjpeg");
3996                 stream->fmt = guess_stream_format(arg, NULL, NULL);
3997                 if (!stream->fmt) {
3998                     fprintf(stderr, "%s:%d: Unknown Format: %s\n",
3999                             filename, line_num, arg);
4000                     errors++;
4001                 }
4002             }
4003             if (stream->fmt) {
4004                 audio_id = stream->fmt->audio_codec;
4005                 video_id = stream->fmt->video_codec;
4006             }
4007             }
4008         } else if (!strcasecmp(cmd, "InputFormat")) {
4009             get_arg(arg, sizeof(arg), &p);
4010             stream->ifmt = av_find_input_format(arg);
4011             if (!stream->ifmt) {
4012                 fprintf(stderr, "%s:%d: Unknown input format: %s\n",
4013                         filename, line_num, arg);
4014             }
4015         } else if (!strcasecmp(cmd, "FaviconURL")) {
4016             if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
4017                 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
4018             } else {
4019                 fprintf(stderr, "%s:%d: FaviconURL only permitted for status streams\n",
4020                             filename, line_num);
4021                 errors++;
4022             }
4023         } else if (!strcasecmp(cmd, "Author")) {
4024             if (stream)
4025                 get_arg(stream->author, sizeof(stream->author), &p);
4026         } else if (!strcasecmp(cmd, "Comment")) {
4027             if (stream)
4028                 get_arg(stream->comment, sizeof(stream->comment), &p);
4029         } else if (!strcasecmp(cmd, "Copyright")) {
4030             if (stream)
4031                 get_arg(stream->copyright, sizeof(stream->copyright), &p);
4032         } else if (!strcasecmp(cmd, "Title")) {
4033             if (stream)
4034                 get_arg(stream->title, sizeof(stream->title), &p);
4035         } else if (!strcasecmp(cmd, "Preroll")) {
4036             get_arg(arg, sizeof(arg), &p);
4037             if (stream)
4038                 stream->prebuffer = atof(arg) * 1000;
4039         } else if (!strcasecmp(cmd, "StartSendOnKey")) {
4040             if (stream)
4041                 stream->send_on_key = 1;
4042         } else if (!strcasecmp(cmd, "AudioCodec")) {
4043             get_arg(arg, sizeof(arg), &p);
4044             audio_id = opt_audio_codec(arg);
4045             if (audio_id == CODEC_ID_NONE) {
4046                 fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
4047                         filename, line_num, arg);
4048                 errors++;
4049             }
4050         } else if (!strcasecmp(cmd, "VideoCodec")) {
4051             get_arg(arg, sizeof(arg), &p);
4052             video_id = opt_video_codec(arg);
4053             if (video_id == CODEC_ID_NONE) {
4054                 fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
4055                         filename, line_num, arg);
4056                 errors++;
4057             }
4058         } else if (!strcasecmp(cmd, "MaxTime")) {
4059             get_arg(arg, sizeof(arg), &p);
4060             if (stream)
4061                 stream->max_time = atof(arg) * 1000;
4062         } else if (!strcasecmp(cmd, "AudioBitRate")) {
4063             get_arg(arg, sizeof(arg), &p);
4064             if (stream)
4065                 audio_enc.bit_rate = atoi(arg) * 1000;
4066         } else if (!strcasecmp(cmd, "AudioChannels")) {
4067             get_arg(arg, sizeof(arg), &p);
4068             if (stream)
4069                 audio_enc.channels = atoi(arg);
4070         } else if (!strcasecmp(cmd, "AudioSampleRate")) {
4071             get_arg(arg, sizeof(arg), &p);
4072             if (stream)
4073                 audio_enc.sample_rate = atoi(arg);
4074         } else if (!strcasecmp(cmd, "AudioQuality")) {
4075             get_arg(arg, sizeof(arg), &p);
4076             if (stream) {
4077 //                audio_enc.quality = atof(arg) * 1000;
4078             }
4079         } else if (!strcasecmp(cmd, "VideoBitRateRange")) {
4080             if (stream) {
4081                 int minrate, maxrate;
4082
4083                 get_arg(arg, sizeof(arg), &p);
4084
4085                 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
4086                     video_enc.rc_min_rate = minrate * 1000;
4087                     video_enc.rc_max_rate = maxrate * 1000;
4088                 } else {
4089                     fprintf(stderr, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4090                             filename, line_num, arg);
4091                     errors++;
4092                 }
4093             }
4094         } else if (!strcasecmp(cmd, "Debug")) {
4095             if (stream) {
4096                 get_arg(arg, sizeof(arg), &p);
4097                 video_enc.debug = strtol(arg,0,0);
4098             }
4099         } else if (!strcasecmp(cmd, "Strict")) {
4100             if (stream) {
4101                 get_arg(arg, sizeof(arg), &p);
4102                 video_enc.strict_std_compliance = atoi(arg);
4103             }
4104         } else if (!strcasecmp(cmd, "VideoBufferSize")) {
4105             if (stream) {
4106                 get_arg(arg, sizeof(arg), &p);
4107                 video_enc.rc_buffer_size = atoi(arg) * 8*1024;
4108             }
4109         } else if (!strcasecmp(cmd, "VideoBitRateTolerance")) {
4110             if (stream) {
4111                 get_arg(arg, sizeof(arg), &p);
4112                 video_enc.bit_rate_tolerance = atoi(arg) * 1000;
4113             }
4114         } else if (!strcasecmp(cmd, "VideoBitRate")) {
4115             get_arg(arg, sizeof(arg), &p);
4116             if (stream) {
4117                 video_enc.bit_rate = atoi(arg) * 1000;
4118             }
4119         } else if (!strcasecmp(cmd, "VideoSize")) {
4120             get_arg(arg, sizeof(arg), &p);
4121             if (stream) {
4122                 av_parse_video_frame_size(&video_enc.width, &video_enc.height, arg);
4123                 if ((video_enc.width % 16) != 0 ||
4124                     (video_enc.height % 16) != 0) {
4125                     fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
4126                             filename, line_num);
4127                     errors++;
4128                 }
4129             }
4130         } else if (!strcasecmp(cmd, "VideoFrameRate")) {
4131             get_arg(arg, sizeof(arg), &p);
4132             if (stream) {
4133                 AVRational frame_rate;
4134                 if (av_parse_video_frame_rate(&frame_rate, arg) < 0) {
4135                     fprintf(stderr, "Incorrect frame rate\n");
4136                     errors++;
4137                 } else {
4138                     video_enc.time_base.num = frame_rate.den;
4139                     video_enc.time_base.den = frame_rate.num;
4140                 }
4141             }
4142         } else if (!strcasecmp(cmd, "VideoGopSize")) {
4143             get_arg(arg, sizeof(arg), &p);
4144             if (stream)
4145                 video_enc.gop_size = atoi(arg);
4146         } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
4147             if (stream)
4148                 video_enc.gop_size = 1;
4149         } else if (!strcasecmp(cmd, "VideoHighQuality")) {
4150             if (stream)
4151                 video_enc.mb_decision = FF_MB_DECISION_BITS;
4152         } else if (!strcasecmp(cmd, "Video4MotionVector")) {
4153             if (stream) {
4154                 video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
4155                 video_enc.flags |= CODEC_FLAG_4MV;
4156             }
4157         } else if (!strcasecmp(cmd, "AVOptionVideo") ||
4158                    !strcasecmp(cmd, "AVOptionAudio")) {
4159             char arg2[1024];
4160             AVCodecContext *avctx;
4161             int type;
4162             get_arg(arg, sizeof(arg), &p);
4163             get_arg(arg2, sizeof(arg2), &p);
4164             if (!strcasecmp(cmd, "AVOptionVideo")) {
4165                 avctx = &video_enc;
4166                 type = AV_OPT_FLAG_VIDEO_PARAM;
4167             } else {
4168                 avctx = &audio_enc;
4169                 type = AV_OPT_FLAG_AUDIO_PARAM;
4170             }
4171             if (opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {
4172                 fprintf(stderr, "AVOption error: %s %s\n", arg, arg2);
4173                 errors++;
4174             }
4175         } else if (!strcasecmp(cmd, "VideoTag")) {
4176             get_arg(arg, sizeof(arg), &p);
4177             if ((strlen(arg) == 4) && stream)
4178                 video_enc.codec_tag = ff_get_fourcc(arg);
4179         } else if (!strcasecmp(cmd, "BitExact")) {
4180             if (stream)
4181                 video_enc.flags |= CODEC_FLAG_BITEXACT;
4182         } else if (!strcasecmp(cmd, "DctFastint")) {
4183             if (stream)
4184                 video_enc.dct_algo  = FF_DCT_FASTINT;
4185         } else if (!strcasecmp(cmd, "IdctSimple")) {
4186             if (stream)
4187                 video_enc.idct_algo = FF_IDCT_SIMPLE;
4188         } else if (!strcasecmp(cmd, "Qscale")) {
4189             get_arg(arg, sizeof(arg), &p);
4190             if (stream) {
4191                 video_enc.flags |= CODEC_FLAG_QSCALE;
4192                 video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
4193             }
4194         } else if (!strcasecmp(cmd, "VideoQDiff")) {
4195             get_arg(arg, sizeof(arg), &p);
4196             if (stream) {
4197                 video_enc.max_qdiff = atoi(arg);
4198                 if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
4199                     fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
4200                             filename, line_num);
4201                     errors++;
4202                 }
4203             }
4204         } else if (!strcasecmp(cmd, "VideoQMax")) {
4205             get_arg(arg, sizeof(arg), &p);
4206             if (stream) {
4207                 video_enc.qmax = atoi(arg);
4208                 if (video_enc.qmax < 1 || video_enc.qmax > 31) {
4209                     fprintf(stderr, "%s:%d: VideoQMax out of range\n",
4210                             filename, line_num);
4211                     errors++;
4212                 }
4213             }
4214         } else if (!strcasecmp(cmd, "VideoQMin")) {
4215             get_arg(arg, sizeof(arg), &p);
4216             if (stream) {
4217                 video_enc.qmin = atoi(arg);
4218                 if (video_enc.qmin < 1 || video_enc.qmin > 31) {
4219                     fprintf(stderr, "%s:%d: VideoQMin out of range\n",
4220                             filename, line_num);
4221                     errors++;
4222                 }
4223             }
4224         } else if (!strcasecmp(cmd, "LumaElim")) {
4225             get_arg(arg, sizeof(arg), &p);
4226             if (stream)
4227                 video_enc.luma_elim_threshold = atoi(arg);
4228         } else if (!strcasecmp(cmd, "ChromaElim")) {
4229             get_arg(arg, sizeof(arg), &p);
4230             if (stream)
4231                 video_enc.chroma_elim_threshold = atoi(arg);
4232         } else if (!strcasecmp(cmd, "LumiMask")) {
4233             get_arg(arg, sizeof(arg), &p);
4234             if (stream)
4235                 video_enc.lumi_masking = atof(arg);
4236         } else if (!strcasecmp(cmd, "DarkMask")) {
4237             get_arg(arg, sizeof(arg), &p);
4238             if (stream)
4239                 video_enc.dark_masking = atof(arg);
4240         } else if (!strcasecmp(cmd, "NoVideo")) {
4241             video_id = CODEC_ID_NONE;
4242         } else if (!strcasecmp(cmd, "NoAudio")) {
4243             audio_id = CODEC_ID_NONE;
4244         } else if (!strcasecmp(cmd, "ACL")) {
4245             IPAddressACL acl;
4246
4247             get_arg(arg, sizeof(arg), &p);
4248             if (strcasecmp(arg, "allow") == 0)
4249                 acl.action = IP_ALLOW;
4250             else if (strcasecmp(arg, "deny") == 0)
4251                 acl.action = IP_DENY;
4252             else {
4253                 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4254                         filename, line_num, arg);
4255                 errors++;
4256             }
4257
4258             get_arg(arg, sizeof(arg), &p);
4259
4260             if (resolve_host(&acl.first, arg) != 0) {
4261                 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4262                         filename, line_num, arg);
4263                 errors++;
4264             } else
4265                 acl.last = acl.first;
4266
4267             get_arg(arg, sizeof(arg), &p);
4268
4269             if (arg[0]) {
4270                 if (resolve_host(&acl.last, arg) != 0) {
4271                     fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4272                             filename, line_num, arg);
4273                     errors++;
4274                 }
4275             }
4276
4277             if (!errors) {
4278                 IPAddressACL *nacl = av_mallocz(sizeof(*nacl));
4279                 IPAddressACL **naclp = 0;
4280
4281                 acl.next = 0;
4282                 *nacl = acl;
4283
4284                 if (stream)
4285                     naclp = &stream->acl;
4286                 else if (feed)
4287                     naclp = &feed->acl;
4288                 else {
4289                     fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
4290                             filename, line_num);
4291                     errors++;
4292                 }
4293
4294                 if (naclp) {
4295                     while (*naclp)
4296                         naclp = &(*naclp)->next;
4297
4298                     *naclp = nacl;
4299                 }
4300             }
4301         } else if (!strcasecmp(cmd, "RTSPOption")) {
4302             get_arg(arg, sizeof(arg), &p);
4303             if (stream) {
4304                 av_freep(&stream->rtsp_option);
4305                 stream->rtsp_option = av_strdup(arg);
4306             }
4307         } else if (!strcasecmp(cmd, "MulticastAddress")) {
4308             get_arg(arg, sizeof(arg), &p);
4309             if (stream) {
4310                 if (resolve_host(&stream->multicast_ip, arg) != 0) {
4311                     fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
4312                             filename, line_num, arg);
4313                     errors++;
4314                 }
4315                 stream->is_multicast = 1;
4316                 stream->loop = 1; /* default is looping */
4317             }
4318         } else if (!strcasecmp(cmd, "MulticastPort")) {
4319             get_arg(arg, sizeof(arg), &p);
4320             if (stream)
4321                 stream->multicast_port = atoi(arg);
4322         } else if (!strcasecmp(cmd, "MulticastTTL")) {
4323             get_arg(arg, sizeof(arg), &p);
4324             if (stream)
4325                 stream->multicast_ttl = atoi(arg);
4326         } else if (!strcasecmp(cmd, "NoLoop")) {
4327             if (stream)
4328                 stream->loop = 0;
4329         } else if (!strcasecmp(cmd, "</Stream>")) {
4330             if (!stream) {
4331                 fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
4332                         filename, line_num);
4333                 errors++;
4334             } else {
4335                 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
4336                     if (audio_id != CODEC_ID_NONE) {
4337                         audio_enc.codec_type = CODEC_TYPE_AUDIO;
4338                         audio_enc.codec_id = audio_id;
4339                         add_codec(stream, &audio_enc);
4340                     }
4341                     if (video_id != CODEC_ID_NONE) {
4342                         video_enc.codec_type = CODEC_TYPE_VIDEO;
4343                         video_enc.codec_id = video_id;
4344                         add_codec(stream, &video_enc);
4345                     }
4346                 }
4347                 stream = NULL;
4348             }
4349         } else if (!strcasecmp(cmd, "<Redirect")) {
4350             /*********************************************/
4351             char *q;
4352             if (stream || feed || redirect) {
4353                 fprintf(stderr, "%s:%d: Already in a tag\n",
4354                         filename, line_num);
4355                 errors++;
4356             } else {
4357                 redirect = av_mallocz(sizeof(FFStream));
4358                 *last_stream = redirect;
4359                 last_stream = &redirect->next;
4360
4361                 get_arg(redirect->filename, sizeof(redirect->filename), &p);
4362                 q = strrchr(redirect->filename, '>');
4363                 if (*q)
4364                     *q = '\0';
4365                 redirect->stream_type = STREAM_TYPE_REDIRECT;
4366             }
4367         } else if (!strcasecmp(cmd, "URL")) {
4368             if (redirect)
4369                 get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
4370         } else if (!strcasecmp(cmd, "</Redirect>")) {
4371             if (!redirect) {
4372                 fprintf(stderr, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4373                         filename, line_num);
4374                 errors++;
4375             } else {
4376                 if (!redirect->feed_filename[0]) {
4377                     fprintf(stderr, "%s:%d: No URL found for <Redirect>\n",
4378                             filename, line_num);
4379                     errors++;
4380                 }
4381                 redirect = NULL;
4382             }
4383         } else if (!strcasecmp(cmd, "LoadModule")) {
4384             get_arg(arg, sizeof(arg), &p);
4385 #ifdef HAVE_DLOPEN
4386             load_module(arg);
4387 #else
4388             fprintf(stderr, "%s:%d: Module support not compiled into this version: '%s'\n",
4389                     filename, line_num, arg);
4390             errors++;
4391 #endif
4392         } else {
4393             fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
4394                     filename, line_num, cmd);
4395             errors++;
4396         }
4397     }
4398
4399     fclose(f);
4400     if (errors)
4401         return -1;
4402     else
4403         return 0;
4404 }
4405
4406 static void handle_child_exit(int sig)
4407 {
4408     pid_t pid;
4409     int status;
4410
4411     while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
4412         FFStream *feed;
4413
4414         for (feed = first_feed; feed; feed = feed->next) {
4415             if (feed->pid == pid) {
4416                 int uptime = time(0) - feed->pid_start;
4417
4418                 feed->pid = 0;
4419                 fprintf(stderr, "%s: Pid %d exited with status %d after %d seconds\n", feed->filename, pid, status, uptime);
4420
4421                 if (uptime < 30)
4422                     /* Turn off any more restarts */
4423                     feed->child_argv = 0;
4424             }
4425         }
4426     }
4427
4428     need_to_start_children = 1;
4429 }
4430
4431 static void opt_debug()
4432 {
4433     ffserver_debug = 1;
4434     ffserver_daemon = 0;
4435     logfilename[0] = '-';
4436 }
4437
4438 static void opt_show_help(void)
4439 {
4440     printf("usage: ffserver [options]\n"
4441            "Hyper fast multi format Audio/Video streaming server\n");
4442     printf("\n");
4443     show_help_options(options, "Main options:\n", 0, 0);
4444 }
4445
4446 static const OptionDef options[] = {
4447     { "h", OPT_EXIT, {(void*)opt_show_help}, "show help" },
4448     { "version", OPT_EXIT, {(void*)show_version}, "show version" },
4449     { "L", OPT_EXIT, {(void*)show_license}, "show license" },
4450     { "formats", OPT_EXIT, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
4451     { "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
4452     { "d", 0, {(void*)opt_debug}, "enable debug mode" },
4453     { "f", HAS_ARG | OPT_STRING, {(void*)&config_filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
4454     { NULL },
4455 };
4456
4457 int main(int argc, char **argv)
4458 {
4459     struct sigaction sigact;
4460
4461     av_register_all();
4462
4463     show_banner();
4464
4465     config_filename = "/etc/ffserver.conf";
4466
4467     my_program_name = argv[0];
4468     my_program_dir = getcwd(0, 0);
4469     ffserver_daemon = 1;
4470
4471     parse_options(argc, argv, options, NULL);
4472
4473     unsetenv("http_proxy");             /* Kill the http_proxy */
4474
4475     av_init_random(av_gettime() + (getpid() << 16), &random_state);
4476
4477     memset(&sigact, 0, sizeof(sigact));
4478     sigact.sa_handler = handle_child_exit;
4479     sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
4480     sigaction(SIGCHLD, &sigact, 0);
4481
4482     if (parse_ffconfig(config_filename) < 0) {
4483         fprintf(stderr, "Incorrect config file - exiting.\n");
4484         exit(1);
4485     }
4486
4487     build_file_streams();
4488
4489     build_feed_streams();
4490
4491     compute_bandwidth();
4492
4493     /* put the process in background and detach it from its TTY */
4494     if (ffserver_daemon) {
4495         int pid;
4496
4497         pid = fork();
4498         if (pid < 0) {
4499             perror("fork");
4500             exit(1);
4501         } else if (pid > 0) {
4502             /* parent : exit */
4503             exit(0);
4504         } else {
4505             /* child */
4506             setsid();
4507             chdir("/");
4508             close(0);
4509             open("/dev/null", O_RDWR);
4510             if (strcmp(logfilename, "-") != 0) {
4511                 close(1);
4512                 dup(0);
4513             }
4514             close(2);
4515             dup(0);
4516         }
4517     }
4518
4519     /* signal init */
4520     signal(SIGPIPE, SIG_IGN);
4521
4522     /* open log file if needed */
4523     if (logfilename[0] != '\0') {
4524         if (!strcmp(logfilename, "-"))
4525             logfile = stderr;
4526         else
4527             logfile = fopen(logfilename, "a");
4528         av_log_set_callback(http_av_log);
4529     }
4530
4531     if (http_server() < 0) {
4532         http_log("Could not start server\n");
4533         exit(1);
4534     }
4535
4536     return 0;
4537 }