]> git.sesse.net Git - ffmpeg/blob - ffserver.c
Merge commit 'cd0e08813a0484002b5defbf557c859f123953ae'
[ffmpeg] / ffserver.c
1 /*
2  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * multiple format streaming server based on the FFmpeg libraries
24  */
25
26 #include "config.h"
27 #if !HAVE_CLOSESOCKET
28 #define closesocket close
29 #endif
30 #include <string.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include "libavformat/avformat.h"
34 /* FIXME: those are internal headers, ffserver _really_ shouldn't use them */
35 #include "libavformat/ffm.h"
36 #include "libavformat/network.h"
37 #include "libavformat/os_support.h"
38 #include "libavformat/rtpdec.h"
39 #include "libavformat/rtpproto.h"
40 #include "libavformat/rtsp.h"
41 #include "libavformat/rtspcodes.h"
42 #include "libavformat/avio_internal.h"
43 #include "libavformat/internal.h"
44 #include "libavformat/url.h"
45
46 #include "libavutil/avassert.h"
47 #include "libavutil/avstring.h"
48 #include "libavutil/lfg.h"
49 #include "libavutil/dict.h"
50 #include "libavutil/intreadwrite.h"
51 #include "libavutil/mathematics.h"
52 #include "libavutil/random_seed.h"
53 #include "libavutil/parseutils.h"
54 #include "libavutil/opt.h"
55 #include "libavutil/time.h"
56
57 #include <stdarg.h>
58 #if HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif
61 #include <fcntl.h>
62 #include <sys/ioctl.h>
63 #if HAVE_POLL_H
64 #include <poll.h>
65 #endif
66 #include <errno.h>
67 #include <time.h>
68 #include <sys/wait.h>
69 #include <signal.h>
70
71 #include "cmdutils.h"
72 #include "ffserver_config.h"
73
74 #define PATH_LENGTH 1024
75
76 const char program_name[] = "ffserver";
77 const int program_birth_year = 2000;
78
79 static const OptionDef options[];
80
81 enum HTTPState {
82     HTTPSTATE_WAIT_REQUEST,
83     HTTPSTATE_SEND_HEADER,
84     HTTPSTATE_SEND_DATA_HEADER,
85     HTTPSTATE_SEND_DATA,          /* sending TCP or UDP data */
86     HTTPSTATE_SEND_DATA_TRAILER,
87     HTTPSTATE_RECEIVE_DATA,
88     HTTPSTATE_WAIT_FEED,          /* wait for data from the feed */
89     HTTPSTATE_READY,
90
91     RTSPSTATE_WAIT_REQUEST,
92     RTSPSTATE_SEND_REPLY,
93     RTSPSTATE_SEND_PACKET,
94 };
95
96 static const char * const http_state[] = {
97     "HTTP_WAIT_REQUEST",
98     "HTTP_SEND_HEADER",
99
100     "SEND_DATA_HEADER",
101     "SEND_DATA",
102     "SEND_DATA_TRAILER",
103     "RECEIVE_DATA",
104     "WAIT_FEED",
105     "READY",
106
107     "RTSP_WAIT_REQUEST",
108     "RTSP_SEND_REPLY",
109     "RTSP_SEND_PACKET",
110 };
111
112 #define IOBUFFER_INIT_SIZE 8192
113
114 /* timeouts are in ms */
115 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
116 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
117
118 #define SYNC_TIMEOUT (10 * 1000)
119
120 typedef struct RTSPActionServerSetup {
121     uint32_t ipaddr;
122     char transport_option[512];
123 } RTSPActionServerSetup;
124
125 typedef struct {
126     int64_t count1, count2;
127     int64_t time1, time2;
128 } DataRateData;
129
130 /* context associated with one connection */
131 typedef struct HTTPContext {
132     enum HTTPState state;
133     int fd; /* socket file descriptor */
134     struct sockaddr_in from_addr; /* origin */
135     struct pollfd *poll_entry; /* used when polling */
136     int64_t timeout;
137     uint8_t *buffer_ptr, *buffer_end;
138     int http_error;
139     int post;
140     int chunked_encoding;
141     int chunk_size;               /* 0 if it needs to be read */
142     struct HTTPContext *next;
143     int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
144     int64_t data_count;
145     /* feed input */
146     int feed_fd;
147     /* input format handling */
148     AVFormatContext *fmt_in;
149     int64_t start_time;            /* In milliseconds - this wraps fairly often */
150     int64_t first_pts;            /* initial pts value */
151     int64_t cur_pts;             /* current pts value from the stream in us */
152     int64_t cur_frame_duration;  /* duration of the current frame in us */
153     int cur_frame_bytes;       /* output frame size, needed to compute
154                                   the time at which we send each
155                                   packet */
156     int pts_stream_index;        /* stream we choose as clock reference */
157     int64_t cur_clock;           /* current clock reference value in us */
158     /* output format handling */
159     struct FFServerStream *stream;
160     /* -1 is invalid stream */
161     int feed_streams[FFSERVER_MAX_STREAMS]; /* index of streams in the feed */
162     int switch_feed_streams[FFSERVER_MAX_STREAMS]; /* index of streams in the feed */
163     int switch_pending;
164     AVFormatContext fmt_ctx; /* instance of FFServerStream for one user */
165     int last_packet_sent; /* true if last data packet was sent */
166     int suppress_log;
167     DataRateData datarate;
168     int wmp_client_id;
169     char protocol[16];
170     char method[16];
171     char url[128];
172     int buffer_size;
173     uint8_t *buffer;
174     int is_packetized; /* if true, the stream is packetized */
175     int packet_stream_index; /* current stream for output in state machine */
176
177     /* RTSP state specific */
178     uint8_t *pb_buffer; /* XXX: use that in all the code */
179     AVIOContext *pb;
180     int seq; /* RTSP sequence number */
181
182     /* RTP state specific */
183     enum RTSPLowerTransport rtp_protocol;
184     char session_id[32]; /* session id */
185     AVFormatContext *rtp_ctx[FFSERVER_MAX_STREAMS];
186
187     /* RTP/UDP specific */
188     URLContext *rtp_handles[FFSERVER_MAX_STREAMS];
189
190     /* RTP/TCP specific */
191     struct HTTPContext *rtsp_c;
192     uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
193 } HTTPContext;
194
195 typedef struct FeedData {
196     long long data_count;
197     float avg_frame_size;   /* frame size averaged over last frames with exponential mean */
198 } FeedData;
199
200 static HTTPContext *first_http_ctx;
201
202 static FFServerConfig config = {
203     .nb_max_http_connections = 2000,
204     .nb_max_connections = 5,
205     .max_bandwidth = 1000,
206     .use_defaults = 1,
207 };
208
209 static void new_connection(int server_fd, int is_rtsp);
210 static void close_connection(HTTPContext *c);
211
212 /* HTTP handling */
213 static int handle_connection(HTTPContext *c);
214 static inline void print_stream_params(AVIOContext *pb, FFServerStream *stream);
215 static void compute_status(HTTPContext *c);
216 static int open_input_stream(HTTPContext *c, const char *info);
217 static int http_parse_request(HTTPContext *c);
218 static int http_send_data(HTTPContext *c);
219 static int http_start_receive_data(HTTPContext *c);
220 static int http_receive_data(HTTPContext *c);
221
222 /* RTSP handling */
223 static int rtsp_parse_request(HTTPContext *c);
224 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
225 static void rtsp_cmd_options(HTTPContext *c, const char *url);
226 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
227                            RTSPMessageHeader *h);
228 static void rtsp_cmd_play(HTTPContext *c, const char *url,
229                           RTSPMessageHeader *h);
230 static void rtsp_cmd_interrupt(HTTPContext *c, const char *url,
231                                RTSPMessageHeader *h, int pause_only);
232
233 /* SDP handling */
234 static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
235                                    struct in_addr my_ip);
236
237 /* RTP handling */
238 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
239                                        FFServerStream *stream,
240                                        const char *session_id,
241                                        enum RTSPLowerTransport rtp_protocol);
242 static int rtp_new_av_stream(HTTPContext *c,
243                              int stream_index, struct sockaddr_in *dest_addr,
244                              HTTPContext *rtsp_c);
245
246 static const char *my_program_name;
247
248 static int no_launch;
249 static int need_to_start_children;
250
251 /* maximum number of simultaneous HTTP connections */
252 static unsigned int nb_connections;
253
254 static uint64_t current_bandwidth;
255
256 /* Making this global saves on passing it around everywhere */
257 static int64_t cur_time;
258
259 static AVLFG random_state;
260
261 static FILE *logfile = NULL;
262
263 static void htmlstrip(char *s) {
264     while (s && *s) {
265         s += strspn(s, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,. ");
266         if (*s)
267             *s++ = '?';
268     }
269 }
270
271 static int64_t ffm_read_write_index(int fd)
272 {
273     uint8_t buf[8];
274
275     if (lseek(fd, 8, SEEK_SET) < 0)
276         return AVERROR(EIO);
277     if (read(fd, buf, 8) != 8)
278         return AVERROR(EIO);
279     return AV_RB64(buf);
280 }
281
282 static int ffm_write_write_index(int fd, int64_t pos)
283 {
284     uint8_t buf[8];
285     int i;
286
287     for(i=0;i<8;i++)
288         buf[i] = (pos >> (56 - i * 8)) & 0xff;
289     if (lseek(fd, 8, SEEK_SET) < 0)
290         goto bail_eio;
291     if (write(fd, buf, 8) != 8)
292         goto bail_eio;
293
294     return 8;
295
296 bail_eio:
297     return AVERROR(EIO);
298 }
299
300 static void ffm_set_write_index(AVFormatContext *s, int64_t pos,
301                                 int64_t file_size)
302 {
303     FFMContext *ffm = s->priv_data;
304     ffm->write_index = pos;
305     ffm->file_size = file_size;
306 }
307
308 static char *ctime1(char *buf2, int buf_size)
309 {
310     time_t ti;
311     char *p;
312
313     ti = time(NULL);
314     p = ctime(&ti);
315     av_strlcpy(buf2, p, buf_size);
316     p = buf2 + strlen(p) - 1;
317     if (*p == '\n')
318         *p = '\0';
319     return buf2;
320 }
321
322 static void http_vlog(const char *fmt, va_list vargs)
323 {
324     static int print_prefix = 1;
325     char buf[32];
326
327     if (!logfile)
328         return;
329
330     if (print_prefix) {
331         ctime1(buf, sizeof(buf));
332         fprintf(logfile, "%s ", buf);
333     }
334     print_prefix = strstr(fmt, "\n") != NULL;
335     vfprintf(logfile, fmt, vargs);
336     fflush(logfile);
337 }
338
339 #ifdef __GNUC__
340 __attribute__ ((format (printf, 1, 2)))
341 #endif
342 static void http_log(const char *fmt, ...)
343 {
344     va_list vargs;
345     va_start(vargs, fmt);
346     http_vlog(fmt, vargs);
347     va_end(vargs);
348 }
349
350 static void http_av_log(void *ptr, int level, const char *fmt, va_list vargs)
351 {
352     static int print_prefix = 1;
353     AVClass *avc = ptr ? *(AVClass**)ptr : NULL;
354     if (level > av_log_get_level())
355         return;
356     if (print_prefix && avc)
357         http_log("[%s @ %p]", avc->item_name(ptr), ptr);
358     print_prefix = strstr(fmt, "\n") != NULL;
359     http_vlog(fmt, vargs);
360 }
361
362 static void log_connection(HTTPContext *c)
363 {
364     if (c->suppress_log)
365         return;
366
367     http_log("%s - - [%s] \"%s %s\" %d %"PRId64"\n",
368              inet_ntoa(c->from_addr.sin_addr), c->method, c->url,
369              c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
370 }
371
372 static void update_datarate(DataRateData *drd, int64_t count)
373 {
374     if (!drd->time1 && !drd->count1) {
375         drd->time1 = drd->time2 = cur_time;
376         drd->count1 = drd->count2 = count;
377     } else if (cur_time - drd->time2 > 5000) {
378         drd->time1 = drd->time2;
379         drd->count1 = drd->count2;
380         drd->time2 = cur_time;
381         drd->count2 = count;
382     }
383 }
384
385 /* In bytes per second */
386 static int compute_datarate(DataRateData *drd, int64_t count)
387 {
388     if (cur_time == drd->time1)
389         return 0;
390
391     return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
392 }
393
394
395 static void start_children(FFServerStream *feed)
396 {
397     char *pathname;
398     char *slash;
399     int i;
400     size_t cmd_length;
401
402     if (no_launch)
403         return;
404
405     cmd_length = strlen(my_program_name);
406
407    /**
408     * FIXME: WIP Safeguard. Remove after clearing all harcoded
409     * '1024' path lengths
410     */
411     if (cmd_length > PATH_LENGTH - 1) {
412         http_log("Could not start children. Command line: '%s' exceeds "
413                     "path length limit (%d)\n", my_program_name, PATH_LENGTH);
414         return;
415     }
416
417     pathname = av_strdup (my_program_name);
418     if (!pathname) {
419         http_log("Could not allocate memory for children cmd line\n");
420         return;
421     }
422    /* replace "ffserver" with "ffmpeg" in the path of current
423     * program. Ignore user provided path */
424
425     slash = strrchr(pathname, '/');
426     if (!slash)
427         slash = pathname;
428     else
429         slash++;
430     strcpy(slash, "ffmpeg");
431
432     for (; feed; feed = feed->next) {
433
434         if (!feed->child_argv || feed->pid)
435             continue;
436
437         feed->pid_start = time(0);
438
439         feed->pid = fork();
440         if (feed->pid < 0) {
441             http_log("Unable to create children\n");
442             exit(1);
443         }
444
445         if (feed->pid)
446             continue;
447
448         /* In child */
449
450         http_log("Launch command line: ");
451         http_log("%s ", pathname);
452
453         for (i = 1; feed->child_argv[i] && feed->child_argv[i][0]; i++)
454             http_log("%s ", feed->child_argv[i]);
455         http_log("\n");
456
457         for (i = 3; i < 256; i++)
458             close(i);
459
460         if (!config.debug) {
461             if (!freopen("/dev/null", "r", stdin))
462                 http_log("failed to redirect STDIN to /dev/null\n;");
463             if (!freopen("/dev/null", "w", stdout))
464                 http_log("failed to redirect STDOUT to /dev/null\n;");
465             if (!freopen("/dev/null", "w", stderr))
466                 http_log("failed to redirect STDERR to /dev/null\n;");
467         }
468
469         signal(SIGPIPE, SIG_DFL);
470         execvp(pathname, feed->child_argv);
471         av_free (pathname);
472         _exit(1);
473     }
474     av_free (pathname);
475 }
476
477 /* open a listening socket */
478 static int socket_open_listen(struct sockaddr_in *my_addr)
479 {
480     int server_fd, tmp;
481
482     server_fd = socket(AF_INET,SOCK_STREAM,0);
483     if (server_fd < 0) {
484         perror ("socket");
485         return -1;
486     }
487
488     tmp = 1;
489     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)))
490         av_log(NULL, AV_LOG_WARNING, "setsockopt SO_REUSEADDR failed\n");
491
492     my_addr->sin_family = AF_INET;
493     if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
494         char bindmsg[32];
495         snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)",
496                  ntohs(my_addr->sin_port));
497         perror (bindmsg);
498         goto fail;
499     }
500
501     if (listen (server_fd, 5) < 0) {
502         perror ("listen");
503         goto fail;
504     }
505
506     if (ff_socket_nonblock(server_fd, 1) < 0)
507         av_log(NULL, AV_LOG_WARNING, "ff_socket_nonblock failed\n");
508
509     return server_fd;
510
511 fail:
512     closesocket(server_fd);
513     return -1;
514 }
515
516 /* start all multicast streams */
517 static void start_multicast(void)
518 {
519     FFServerStream *stream;
520     char session_id[32];
521     HTTPContext *rtp_c;
522     struct sockaddr_in dest_addr = {0};
523     int default_port, stream_index;
524     unsigned int random0, random1;
525
526     default_port = 6000;
527     for(stream = config.first_stream; stream; stream = stream->next) {
528
529         if (!stream->is_multicast)
530             continue;
531
532         random0 = av_lfg_get(&random_state);
533         random1 = av_lfg_get(&random_state);
534
535         /* open the RTP connection */
536         snprintf(session_id, sizeof(session_id), "%08x%08x", random0, random1);
537
538         /* choose a port if none given */
539         if (stream->multicast_port == 0) {
540             stream->multicast_port = default_port;
541             default_port += 100;
542         }
543
544         dest_addr.sin_family = AF_INET;
545         dest_addr.sin_addr = stream->multicast_ip;
546         dest_addr.sin_port = htons(stream->multicast_port);
547
548         rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
549                                    RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
550         if (!rtp_c)
551             continue;
552
553         if (open_input_stream(rtp_c, "") < 0) {
554             http_log("Could not open input stream for stream '%s'\n",
555                      stream->filename);
556             continue;
557         }
558
559         /* open each RTP stream */
560         for(stream_index = 0; stream_index < stream->nb_streams;
561             stream_index++) {
562             dest_addr.sin_port = htons(stream->multicast_port +
563                                        2 * stream_index);
564             if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) >= 0)
565                 continue;
566
567             http_log("Could not open output stream '%s/streamid=%d'\n",
568                      stream->filename, stream_index);
569             exit(1);
570         }
571
572         rtp_c->state = HTTPSTATE_SEND_DATA;
573     }
574 }
575
576 /* main loop of the HTTP server */
577 static int http_server(void)
578 {
579     int server_fd = 0, rtsp_server_fd = 0;
580     int ret, delay;
581     struct pollfd *poll_table, *poll_entry;
582     HTTPContext *c, *c_next;
583
584     poll_table = av_mallocz_array(config.nb_max_http_connections + 2,
585                                   sizeof(*poll_table));
586     if(!poll_table) {
587         http_log("Impossible to allocate a poll table handling %d "
588                  "connections.\n", config.nb_max_http_connections);
589         return -1;
590     }
591
592     if (config.http_addr.sin_port) {
593         server_fd = socket_open_listen(&config.http_addr);
594         if (server_fd < 0)
595             goto quit;
596     }
597
598     if (config.rtsp_addr.sin_port) {
599         rtsp_server_fd = socket_open_listen(&config.rtsp_addr);
600         if (rtsp_server_fd < 0) {
601             closesocket(server_fd);
602             goto quit;
603         }
604     }
605
606     if (!rtsp_server_fd && !server_fd) {
607         http_log("HTTP and RTSP disabled.\n");
608         goto quit;
609     }
610
611     http_log("FFserver started.\n");
612
613     start_children(config.first_feed);
614
615     start_multicast();
616
617     for(;;) {
618         poll_entry = poll_table;
619         if (server_fd) {
620             poll_entry->fd = server_fd;
621             poll_entry->events = POLLIN;
622             poll_entry++;
623         }
624         if (rtsp_server_fd) {
625             poll_entry->fd = rtsp_server_fd;
626             poll_entry->events = POLLIN;
627             poll_entry++;
628         }
629
630         /* wait for events on each HTTP handle */
631         c = first_http_ctx;
632         delay = 1000;
633         while (c) {
634             int fd;
635             fd = c->fd;
636             switch(c->state) {
637             case HTTPSTATE_SEND_HEADER:
638             case RTSPSTATE_SEND_REPLY:
639             case RTSPSTATE_SEND_PACKET:
640                 c->poll_entry = poll_entry;
641                 poll_entry->fd = fd;
642                 poll_entry->events = POLLOUT;
643                 poll_entry++;
644                 break;
645             case HTTPSTATE_SEND_DATA_HEADER:
646             case HTTPSTATE_SEND_DATA:
647             case HTTPSTATE_SEND_DATA_TRAILER:
648                 if (!c->is_packetized) {
649                     /* for TCP, we output as much as we can
650                      * (may need to put a limit) */
651                     c->poll_entry = poll_entry;
652                     poll_entry->fd = fd;
653                     poll_entry->events = POLLOUT;
654                     poll_entry++;
655                 } else {
656                     /* when ffserver is doing the timing, we work by
657                      * looking at which packet needs to be sent every
658                      * 10 ms (one tick wait XXX: 10 ms assumed) */
659                     if (delay > 10)
660                         delay = 10;
661                 }
662                 break;
663             case HTTPSTATE_WAIT_REQUEST:
664             case HTTPSTATE_RECEIVE_DATA:
665             case HTTPSTATE_WAIT_FEED:
666             case RTSPSTATE_WAIT_REQUEST:
667                 /* need to catch errors */
668                 c->poll_entry = poll_entry;
669                 poll_entry->fd = fd;
670                 poll_entry->events = POLLIN;/* Maybe this will work */
671                 poll_entry++;
672                 break;
673             default:
674                 c->poll_entry = NULL;
675                 break;
676             }
677             c = c->next;
678         }
679
680         /* wait for an event on one connection. We poll at least every
681          * second to handle timeouts */
682         do {
683             ret = poll(poll_table, poll_entry - poll_table, delay);
684             if (ret < 0 && ff_neterrno() != AVERROR(EAGAIN) &&
685                 ff_neterrno() != AVERROR(EINTR)) {
686                 goto quit;
687             }
688         } while (ret < 0);
689
690         cur_time = av_gettime() / 1000;
691
692         if (need_to_start_children) {
693             need_to_start_children = 0;
694             start_children(config.first_feed);
695         }
696
697         /* now handle the events */
698         for(c = first_http_ctx; c; c = c_next) {
699             c_next = c->next;
700             if (handle_connection(c) < 0) {
701                 log_connection(c);
702                 /* close and free the connection */
703                 close_connection(c);
704             }
705         }
706
707         poll_entry = poll_table;
708         if (server_fd) {
709             /* new HTTP connection request ? */
710             if (poll_entry->revents & POLLIN)
711                 new_connection(server_fd, 0);
712             poll_entry++;
713         }
714         if (rtsp_server_fd) {
715             /* new RTSP connection request ? */
716             if (poll_entry->revents & POLLIN)
717                 new_connection(rtsp_server_fd, 1);
718         }
719     }
720
721 quit:
722     av_free(poll_table);
723     return -1;
724 }
725
726 /* start waiting for a new HTTP/RTSP request */
727 static void start_wait_request(HTTPContext *c, int is_rtsp)
728 {
729     c->buffer_ptr = c->buffer;
730     c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
731
732     c->state = is_rtsp ? RTSPSTATE_WAIT_REQUEST : HTTPSTATE_WAIT_REQUEST;
733     c->timeout = cur_time +
734                  (is_rtsp ? RTSP_REQUEST_TIMEOUT : HTTP_REQUEST_TIMEOUT);
735 }
736
737 static void http_send_too_busy_reply(int fd)
738 {
739     char buffer[400];
740     int len = snprintf(buffer, sizeof(buffer),
741                        "HTTP/1.0 503 Server too busy\r\n"
742                        "Content-type: text/html\r\n"
743                        "\r\n"
744                        "<html><head><title>Too busy</title></head><body>\r\n"
745                        "<p>The server is too busy to serve your request at "
746                        "this time.</p>\r\n"
747                        "<p>The number of current connections is %u, and this "
748                        "exceeds the limit of %u.</p>\r\n"
749                        "</body></html>\r\n",
750                        nb_connections, config.nb_max_connections);
751     av_assert0(len < sizeof(buffer));
752     if (send(fd, buffer, len, 0) < len)
753         av_log(NULL, AV_LOG_WARNING,
754                "Could not send too-busy reply, send() failed\n");
755 }
756
757
758 static void new_connection(int server_fd, int is_rtsp)
759 {
760     struct sockaddr_in from_addr;
761     socklen_t len;
762     int fd;
763     HTTPContext *c = NULL;
764
765     len = sizeof(from_addr);
766     fd = accept(server_fd, (struct sockaddr *)&from_addr,
767                 &len);
768     if (fd < 0) {
769         http_log("error during accept %s\n", strerror(errno));
770         return;
771     }
772     if (ff_socket_nonblock(fd, 1) < 0)
773         av_log(NULL, AV_LOG_WARNING, "ff_socket_nonblock failed\n");
774
775     if (nb_connections >= config.nb_max_connections) {
776         http_send_too_busy_reply(fd);
777         goto fail;
778     }
779
780     /* add a new connection */
781     c = av_mallocz(sizeof(HTTPContext));
782     if (!c)
783         goto fail;
784
785     c->fd = fd;
786     c->poll_entry = NULL;
787     c->from_addr = from_addr;
788     c->buffer_size = IOBUFFER_INIT_SIZE;
789     c->buffer = av_malloc(c->buffer_size);
790     if (!c->buffer)
791         goto fail;
792
793     c->next = first_http_ctx;
794     first_http_ctx = c;
795     nb_connections++;
796
797     start_wait_request(c, is_rtsp);
798
799     return;
800
801  fail:
802     if (c) {
803         av_freep(&c->buffer);
804         av_free(c);
805     }
806     closesocket(fd);
807 }
808
809 static void close_connection(HTTPContext *c)
810 {
811     HTTPContext **cp, *c1;
812     int i, nb_streams;
813     AVFormatContext *ctx;
814     AVStream *st;
815
816     /* remove connection from list */
817     cp = &first_http_ctx;
818     while (*cp) {
819         c1 = *cp;
820         if (c1 == c)
821             *cp = c->next;
822         else
823             cp = &c1->next;
824     }
825
826     /* remove references, if any (XXX: do it faster) */
827     for(c1 = first_http_ctx; c1; c1 = c1->next) {
828         if (c1->rtsp_c == c)
829             c1->rtsp_c = NULL;
830     }
831
832     /* remove connection associated resources */
833     if (c->fd >= 0)
834         closesocket(c->fd);
835     if (c->fmt_in) {
836         /* close each frame parser */
837         for(i=0;i<c->fmt_in->nb_streams;i++) {
838             st = c->fmt_in->streams[i];
839             if (st->codec->codec)
840                 avcodec_close(st->codec);
841         }
842         avformat_close_input(&c->fmt_in);
843     }
844
845     /* free RTP output streams if any */
846     nb_streams = 0;
847     if (c->stream)
848         nb_streams = c->stream->nb_streams;
849
850     for(i=0;i<nb_streams;i++) {
851         ctx = c->rtp_ctx[i];
852         if (ctx) {
853             av_write_trailer(ctx);
854             av_dict_free(&ctx->metadata);
855             av_freep(&ctx->streams[0]);
856             av_freep(&ctx);
857         }
858         ffurl_close(c->rtp_handles[i]);
859     }
860
861     ctx = &c->fmt_ctx;
862
863     if (!c->last_packet_sent && c->state == HTTPSTATE_SEND_DATA_TRAILER) {
864         /* prepare header */
865         if (ctx->oformat && avio_open_dyn_buf(&ctx->pb) >= 0) {
866             av_write_trailer(ctx);
867             av_freep(&c->pb_buffer);
868             avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
869         }
870     }
871
872     for(i=0; i<ctx->nb_streams; i++)
873         av_freep(&ctx->streams[i]);
874     av_freep(&ctx->streams);
875     av_freep(&ctx->priv_data);
876
877     if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
878         current_bandwidth -= c->stream->bandwidth;
879
880     /* signal that there is no feed if we are the feeder socket */
881     if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
882         c->stream->feed_opened = 0;
883         close(c->feed_fd);
884     }
885
886     av_freep(&c->pb_buffer);
887     av_freep(&c->packet_buffer);
888     av_freep(&c->buffer);
889     av_free(c);
890     nb_connections--;
891 }
892
893 static int handle_connection(HTTPContext *c)
894 {
895     int len, ret;
896     uint8_t *ptr;
897
898     switch(c->state) {
899     case HTTPSTATE_WAIT_REQUEST:
900     case RTSPSTATE_WAIT_REQUEST:
901         /* timeout ? */
902         if ((c->timeout - cur_time) < 0)
903             return -1;
904         if (c->poll_entry->revents & (POLLERR | POLLHUP))
905             return -1;
906
907         /* no need to read if no events */
908         if (!(c->poll_entry->revents & POLLIN))
909             return 0;
910         /* read the data */
911     read_loop:
912         if (!(len = recv(c->fd, c->buffer_ptr, 1, 0)))
913             return -1;
914
915         if (len < 0) {
916             if (ff_neterrno() != AVERROR(EAGAIN) &&
917                 ff_neterrno() != AVERROR(EINTR))
918                 return -1;
919             break;
920         }
921         /* search for end of request. */
922         c->buffer_ptr += len;
923         ptr = c->buffer_ptr;
924         if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
925             (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
926             /* request found : parse it and reply */
927             if (c->state == HTTPSTATE_WAIT_REQUEST)
928                 ret = http_parse_request(c);
929             else
930                 ret = rtsp_parse_request(c);
931
932             if (ret < 0)
933                 return -1;
934         } else if (ptr >= c->buffer_end) {
935             /* request too long: cannot do anything */
936             return -1;
937         } else goto read_loop;
938
939         break;
940
941     case HTTPSTATE_SEND_HEADER:
942         if (c->poll_entry->revents & (POLLERR | POLLHUP))
943             return -1;
944
945         /* no need to write if no events */
946         if (!(c->poll_entry->revents & POLLOUT))
947             return 0;
948         len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
949         if (len < 0) {
950             if (ff_neterrno() != AVERROR(EAGAIN) &&
951                 ff_neterrno() != AVERROR(EINTR)) {
952                 goto close_connection;
953             }
954             break;
955         }
956         c->buffer_ptr += len;
957         if (c->stream)
958             c->stream->bytes_served += len;
959         c->data_count += len;
960         if (c->buffer_ptr >= c->buffer_end) {
961             av_freep(&c->pb_buffer);
962             /* if error, exit */
963             if (c->http_error)
964                 return -1;
965             /* all the buffer was sent : synchronize to the incoming
966              * stream */
967             c->state = HTTPSTATE_SEND_DATA_HEADER;
968             c->buffer_ptr = c->buffer_end = c->buffer;
969         }
970         break;
971
972     case HTTPSTATE_SEND_DATA:
973     case HTTPSTATE_SEND_DATA_HEADER:
974     case HTTPSTATE_SEND_DATA_TRAILER:
975         /* for packetized output, we consider we can always write (the
976          * input streams set the speed). It may be better to verify
977          * that we do not rely too much on the kernel queues */
978         if (!c->is_packetized) {
979             if (c->poll_entry->revents & (POLLERR | POLLHUP))
980                 return -1;
981
982             /* no need to read if no events */
983             if (!(c->poll_entry->revents & POLLOUT))
984                 return 0;
985         }
986         if (http_send_data(c) < 0)
987             return -1;
988         /* close connection if trailer sent */
989         if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
990             return -1;
991         /* Check if it is a single jpeg frame 123 */
992         if (c->stream->single_frame && c->data_count > c->cur_frame_bytes && c->cur_frame_bytes > 0) {
993             close_connection(c);
994         }
995         break;
996     case HTTPSTATE_RECEIVE_DATA:
997         /* no need to read if no events */
998         if (c->poll_entry->revents & (POLLERR | POLLHUP))
999             return -1;
1000         if (!(c->poll_entry->revents & POLLIN))
1001             return 0;
1002         if (http_receive_data(c) < 0)
1003             return -1;
1004         break;
1005     case HTTPSTATE_WAIT_FEED:
1006         /* no need to read if no events */
1007         if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
1008             return -1;
1009
1010         /* nothing to do, we'll be waken up by incoming feed packets */
1011         break;
1012
1013     case RTSPSTATE_SEND_REPLY:
1014         if (c->poll_entry->revents & (POLLERR | POLLHUP))
1015             goto close_connection;
1016         /* no need to write if no events */
1017         if (!(c->poll_entry->revents & POLLOUT))
1018             return 0;
1019         len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
1020         if (len < 0) {
1021             if (ff_neterrno() != AVERROR(EAGAIN) &&
1022                 ff_neterrno() != AVERROR(EINTR)) {
1023                 goto close_connection;
1024             }
1025             break;
1026         }
1027         c->buffer_ptr += len;
1028         c->data_count += len;
1029         if (c->buffer_ptr >= c->buffer_end) {
1030             /* all the buffer was sent : wait for a new request */
1031             av_freep(&c->pb_buffer);
1032             start_wait_request(c, 1);
1033         }
1034         break;
1035     case RTSPSTATE_SEND_PACKET:
1036         if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
1037             av_freep(&c->packet_buffer);
1038             return -1;
1039         }
1040         /* no need to write if no events */
1041         if (!(c->poll_entry->revents & POLLOUT))
1042             return 0;
1043         len = send(c->fd, c->packet_buffer_ptr,
1044                     c->packet_buffer_end - c->packet_buffer_ptr, 0);
1045         if (len < 0) {
1046             if (ff_neterrno() != AVERROR(EAGAIN) &&
1047                 ff_neterrno() != AVERROR(EINTR)) {
1048                 /* error : close connection */
1049                 av_freep(&c->packet_buffer);
1050                 return -1;
1051             }
1052             break;
1053         }
1054         c->packet_buffer_ptr += len;
1055         if (c->packet_buffer_ptr >= c->packet_buffer_end) {
1056             /* all the buffer was sent : wait for a new request */
1057             av_freep(&c->packet_buffer);
1058             c->state = RTSPSTATE_WAIT_REQUEST;
1059         }
1060         break;
1061     case HTTPSTATE_READY:
1062         /* nothing to do */
1063         break;
1064     default:
1065         return -1;
1066     }
1067     return 0;
1068
1069 close_connection:
1070     av_freep(&c->pb_buffer);
1071     return -1;
1072 }
1073
1074 static int extract_rates(char *rates, int ratelen, const char *request)
1075 {
1076     const char *p;
1077
1078     for (p = request; *p && *p != '\r' && *p != '\n'; ) {
1079         if (av_strncasecmp(p, "Pragma:", 7) == 0) {
1080             const char *q = p + 7;
1081
1082             while (*q && *q != '\n' && av_isspace(*q))
1083                 q++;
1084
1085             if (av_strncasecmp(q, "stream-switch-entry=", 20) == 0) {
1086                 int stream_no;
1087                 int rate_no;
1088
1089                 q += 20;
1090
1091                 memset(rates, 0xff, ratelen);
1092
1093                 while (1) {
1094                     while (*q && *q != '\n' && *q != ':')
1095                         q++;
1096
1097                     if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
1098                         break;
1099
1100                     stream_no--;
1101                     if (stream_no < ratelen && stream_no >= 0)
1102                         rates[stream_no] = rate_no;
1103
1104                     while (*q && *q != '\n' && !av_isspace(*q))
1105                         q++;
1106                 }
1107
1108                 return 1;
1109             }
1110         }
1111         p = strchr(p, '\n');
1112         if (!p)
1113             break;
1114
1115         p++;
1116     }
1117
1118     return 0;
1119 }
1120
1121 static int find_stream_in_feed(FFServerStream *feed, AVCodecContext *codec,
1122                                int bit_rate)
1123 {
1124     int i;
1125     int best_bitrate = 100000000;
1126     int best = -1;
1127
1128     for (i = 0; i < feed->nb_streams; i++) {
1129         AVCodecContext *feed_codec = feed->streams[i]->codec;
1130
1131         if (feed_codec->codec_id != codec->codec_id ||
1132             feed_codec->sample_rate != codec->sample_rate ||
1133             feed_codec->width != codec->width ||
1134             feed_codec->height != codec->height)
1135             continue;
1136
1137         /* Potential stream */
1138
1139         /* We want the fastest stream less than bit_rate, or the slowest
1140          * faster than bit_rate
1141          */
1142
1143         if (feed_codec->bit_rate <= bit_rate) {
1144             if (best_bitrate > bit_rate ||
1145                 feed_codec->bit_rate > best_bitrate) {
1146                 best_bitrate = feed_codec->bit_rate;
1147                 best = i;
1148             }
1149             continue;
1150         }
1151         if (feed_codec->bit_rate < best_bitrate) {
1152             best_bitrate = feed_codec->bit_rate;
1153             best = i;
1154         }
1155     }
1156     return best;
1157 }
1158
1159 static int modify_current_stream(HTTPContext *c, char *rates)
1160 {
1161     int i;
1162     FFServerStream *req = c->stream;
1163     int action_required = 0;
1164
1165     /* Not much we can do for a feed */
1166     if (!req->feed)
1167         return 0;
1168
1169     for (i = 0; i < req->nb_streams; i++) {
1170         AVCodecContext *codec = req->streams[i]->codec;
1171
1172         switch(rates[i]) {
1173             case 0:
1174                 c->switch_feed_streams[i] = req->feed_streams[i];
1175                 break;
1176             case 1:
1177                 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1178                 break;
1179             case 2:
1180                 /* Wants off or slow */
1181                 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1182 #ifdef WANTS_OFF
1183                 /* This doesn't work well when it turns off the only stream! */
1184                 c->switch_feed_streams[i] = -2;
1185                 c->feed_streams[i] = -2;
1186 #endif
1187                 break;
1188         }
1189
1190         if (c->switch_feed_streams[i] >= 0 &&
1191             c->switch_feed_streams[i] != c->feed_streams[i]) {
1192             action_required = 1;
1193         }
1194     }
1195
1196     return action_required;
1197 }
1198
1199 static void get_word(char *buf, int buf_size, const char **pp)
1200 {
1201     const char *p;
1202     char *q;
1203
1204     p = *pp;
1205     p += strspn(p, SPACE_CHARS);
1206     q = buf;
1207     while (!av_isspace(*p) && *p != '\0') {
1208         if ((q - buf) < buf_size - 1)
1209             *q++ = *p;
1210         p++;
1211     }
1212     if (buf_size > 0)
1213         *q = '\0';
1214     *pp = p;
1215 }
1216
1217 static FFServerIPAddressACL* parse_dynamic_acl(FFServerStream *stream,
1218                                                HTTPContext *c)
1219 {
1220     FILE* f;
1221     char line[1024];
1222     char  cmd[1024];
1223     FFServerIPAddressACL *acl = NULL;
1224     int line_num = 0;
1225     const char *p;
1226
1227     f = fopen(stream->dynamic_acl, "r");
1228     if (!f) {
1229         perror(stream->dynamic_acl);
1230         return NULL;
1231     }
1232
1233     acl = av_mallocz(sizeof(FFServerIPAddressACL));
1234     if (!acl) {
1235         fclose(f);
1236         return NULL;
1237     }
1238
1239     /* Build ACL */
1240     while (fgets(line, sizeof(line), f)) {
1241         line_num++;
1242         p = line;
1243         while (av_isspace(*p))
1244             p++;
1245         if (*p == '\0' || *p == '#')
1246             continue;
1247         ffserver_get_arg(cmd, sizeof(cmd), &p);
1248
1249         if (!av_strcasecmp(cmd, "ACL"))
1250             ffserver_parse_acl_row(NULL, NULL, acl, p, stream->dynamic_acl,
1251                                    line_num);
1252     }
1253     fclose(f);
1254     return acl;
1255 }
1256
1257
1258 static void free_acl_list(FFServerIPAddressACL *in_acl)
1259 {
1260     FFServerIPAddressACL *pacl, *pacl2;
1261
1262     pacl = in_acl;
1263     while(pacl) {
1264         pacl2 = pacl;
1265         pacl = pacl->next;
1266         av_freep(pacl2);
1267     }
1268 }
1269
1270 static int validate_acl_list(FFServerIPAddressACL *in_acl, HTTPContext *c)
1271 {
1272     enum FFServerIPAddressAction last_action = IP_DENY;
1273     FFServerIPAddressACL *acl;
1274     struct in_addr *src = &c->from_addr.sin_addr;
1275     unsigned long src_addr = src->s_addr;
1276
1277     for (acl = in_acl; acl; acl = acl->next) {
1278         if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
1279             return (acl->action == IP_ALLOW) ? 1 : 0;
1280         last_action = acl->action;
1281     }
1282
1283     /* Nothing matched, so return not the last action */
1284     return (last_action == IP_DENY) ? 1 : 0;
1285 }
1286
1287 static int validate_acl(FFServerStream *stream, HTTPContext *c)
1288 {
1289     int ret = 0;
1290     FFServerIPAddressACL *acl;
1291
1292     /* if stream->acl is null validate_acl_list will return 1 */
1293     ret = validate_acl_list(stream->acl, c);
1294
1295     if (stream->dynamic_acl[0]) {
1296         acl = parse_dynamic_acl(stream, c);
1297         ret = validate_acl_list(acl, c);
1298         free_acl_list(acl);
1299     }
1300
1301     return ret;
1302 }
1303
1304 /**
1305  * compute the real filename of a file by matching it without its
1306  * extensions to all the stream's filenames
1307  */
1308 static void compute_real_filename(char *filename, int max_size)
1309 {
1310     char file1[1024];
1311     char file2[1024];
1312     char *p;
1313     FFServerStream *stream;
1314
1315     av_strlcpy(file1, filename, sizeof(file1));
1316     p = strrchr(file1, '.');
1317     if (p)
1318         *p = '\0';
1319     for(stream = config.first_stream; stream; stream = stream->next) {
1320         av_strlcpy(file2, stream->filename, sizeof(file2));
1321         p = strrchr(file2, '.');
1322         if (p)
1323             *p = '\0';
1324         if (!strcmp(file1, file2)) {
1325             av_strlcpy(filename, stream->filename, max_size);
1326             break;
1327         }
1328     }
1329 }
1330
1331 enum RedirType {
1332     REDIR_NONE,
1333     REDIR_ASX,
1334     REDIR_RAM,
1335     REDIR_ASF,
1336     REDIR_RTSP,
1337     REDIR_SDP,
1338 };
1339
1340 /* parse HTTP request and prepare header */
1341 static int http_parse_request(HTTPContext *c)
1342 {
1343     const char *p;
1344     char *p1;
1345     enum RedirType redir_type;
1346     char cmd[32];
1347     char info[1024], filename[1024];
1348     char url[1024], *q;
1349     char protocol[32];
1350     char msg[1024];
1351     const char *mime_type;
1352     FFServerStream *stream;
1353     int i;
1354     char ratebuf[32];
1355     const char *useragent = 0;
1356
1357     p = c->buffer;
1358     get_word(cmd, sizeof(cmd), &p);
1359     av_strlcpy(c->method, cmd, sizeof(c->method));
1360
1361     if (!strcmp(cmd, "GET"))
1362         c->post = 0;
1363     else if (!strcmp(cmd, "POST"))
1364         c->post = 1;
1365     else
1366         return -1;
1367
1368     get_word(url, sizeof(url), &p);
1369     av_strlcpy(c->url, url, sizeof(c->url));
1370
1371     get_word(protocol, sizeof(protocol), (const char **)&p);
1372     if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1373         return -1;
1374
1375     av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
1376
1377     if (config.debug)
1378         http_log("%s - - New connection: %s %s\n",
1379                  inet_ntoa(c->from_addr.sin_addr), cmd, url);
1380
1381     /* find the filename and the optional info string in the request */
1382     p1 = strchr(url, '?');
1383     if (p1) {
1384         av_strlcpy(info, p1, sizeof(info));
1385         *p1 = '\0';
1386     } else
1387         info[0] = '\0';
1388
1389     av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
1390
1391     for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1392         if (av_strncasecmp(p, "User-Agent:", 11) == 0) {
1393             useragent = p + 11;
1394             if (*useragent && *useragent != '\n' && av_isspace(*useragent))
1395                 useragent++;
1396             break;
1397         }
1398         p = strchr(p, '\n');
1399         if (!p)
1400             break;
1401
1402         p++;
1403     }
1404
1405     redir_type = REDIR_NONE;
1406     if (av_match_ext(filename, "asx")) {
1407         redir_type = REDIR_ASX;
1408         filename[strlen(filename)-1] = 'f';
1409     } else if (av_match_ext(filename, "asf") &&
1410         (!useragent || av_strncasecmp(useragent, "NSPlayer", 8))) {
1411         /* if this isn't WMP or lookalike, return the redirector file */
1412         redir_type = REDIR_ASF;
1413     } else if (av_match_ext(filename, "rpm,ram")) {
1414         redir_type = REDIR_RAM;
1415         strcpy(filename + strlen(filename)-2, "m");
1416     } else if (av_match_ext(filename, "rtsp")) {
1417         redir_type = REDIR_RTSP;
1418         compute_real_filename(filename, sizeof(filename) - 1);
1419     } else if (av_match_ext(filename, "sdp")) {
1420         redir_type = REDIR_SDP;
1421         compute_real_filename(filename, sizeof(filename) - 1);
1422     }
1423
1424     /* "redirect" request to index.html */
1425     if (!strlen(filename))
1426         av_strlcpy(filename, "index.html", sizeof(filename) - 1);
1427
1428     stream = config.first_stream;
1429     while (stream) {
1430         if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
1431             break;
1432         stream = stream->next;
1433     }
1434     if (!stream) {
1435         snprintf(msg, sizeof(msg), "File '%s' not found", url);
1436         http_log("File '%s' not found\n", url);
1437         goto send_error;
1438     }
1439
1440     c->stream = stream;
1441     memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1442     memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1443
1444     if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1445         c->http_error = 301;
1446         q = c->buffer;
1447         snprintf(q, c->buffer_size,
1448                       "HTTP/1.0 301 Moved\r\n"
1449                       "Location: %s\r\n"
1450                       "Content-type: text/html\r\n"
1451                       "\r\n"
1452                       "<html><head><title>Moved</title></head><body>\r\n"
1453                       "You should be <a href=\"%s\">redirected</a>.\r\n"
1454                       "</body></html>\r\n",
1455                  stream->feed_filename, stream->feed_filename);
1456         q += strlen(q);
1457         /* prepare output buffer */
1458         c->buffer_ptr = c->buffer;
1459         c->buffer_end = q;
1460         c->state = HTTPSTATE_SEND_HEADER;
1461         return 0;
1462     }
1463
1464     /* If this is WMP, get the rate information */
1465     if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1466         if (modify_current_stream(c, ratebuf)) {
1467             for (i = 0; i < FF_ARRAY_ELEMS(c->feed_streams); i++) {
1468                 if (c->switch_feed_streams[i] >= 0)
1469                     c->switch_feed_streams[i] = -1;
1470             }
1471         }
1472     }
1473
1474     if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
1475         current_bandwidth += stream->bandwidth;
1476
1477     /* If already streaming this feed, do not let another feeder start */
1478     if (stream->feed_opened) {
1479         snprintf(msg, sizeof(msg), "This feed is already being received.");
1480         http_log("Feed '%s' already being received\n", stream->feed_filename);
1481         goto send_error;
1482     }
1483
1484     if (c->post == 0 && config.max_bandwidth < current_bandwidth) {
1485         c->http_error = 503;
1486         q = c->buffer;
1487         snprintf(q, c->buffer_size,
1488                       "HTTP/1.0 503 Server too busy\r\n"
1489                       "Content-type: text/html\r\n"
1490                       "\r\n"
1491                       "<html><head><title>Too busy</title></head><body>\r\n"
1492                       "<p>The server is too busy to serve your request at "
1493                       "this time.</p>\r\n"
1494                       "<p>The bandwidth being served (including your stream) "
1495                       "is %"PRIu64"kbit/s, and this exceeds the limit of "
1496                       "%"PRIu64"kbit/s.</p>\r\n"
1497                       "</body></html>\r\n",
1498                  current_bandwidth, config.max_bandwidth);
1499         q += strlen(q);
1500         /* prepare output buffer */
1501         c->buffer_ptr = c->buffer;
1502         c->buffer_end = q;
1503         c->state = HTTPSTATE_SEND_HEADER;
1504         return 0;
1505     }
1506
1507     if (redir_type != REDIR_NONE) {
1508         const char *hostinfo = 0;
1509
1510         for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1511             if (av_strncasecmp(p, "Host:", 5) == 0) {
1512                 hostinfo = p + 5;
1513                 break;
1514             }
1515             p = strchr(p, '\n');
1516             if (!p)
1517                 break;
1518
1519             p++;
1520         }
1521
1522         if (hostinfo) {
1523             char *eoh;
1524             char hostbuf[260];
1525
1526             while (av_isspace(*hostinfo))
1527                 hostinfo++;
1528
1529             eoh = strchr(hostinfo, '\n');
1530             if (eoh) {
1531                 if (eoh[-1] == '\r')
1532                     eoh--;
1533
1534                 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1535                     memcpy(hostbuf, hostinfo, eoh - hostinfo);
1536                     hostbuf[eoh - hostinfo] = 0;
1537
1538                     c->http_error = 200;
1539                     q = c->buffer;
1540                     switch(redir_type) {
1541                     case REDIR_ASX:
1542                         snprintf(q, c->buffer_size,
1543                                       "HTTP/1.0 200 ASX Follows\r\n"
1544                                       "Content-type: video/x-ms-asf\r\n"
1545                                       "\r\n"
1546                                       "<ASX Version=\"3\">\r\n"
1547                                       //"<!-- Autogenerated by ffserver -->\r\n"
1548                                       "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n"
1549                                       "</ASX>\r\n", hostbuf, filename, info);
1550                         q += strlen(q);
1551                         break;
1552                     case REDIR_RAM:
1553                         snprintf(q, c->buffer_size,
1554                                       "HTTP/1.0 200 RAM Follows\r\n"
1555                                       "Content-type: audio/x-pn-realaudio\r\n"
1556                                       "\r\n"
1557                                       "# Autogenerated by ffserver\r\n"
1558                                       "http://%s/%s%s\r\n", hostbuf, filename, info);
1559                         q += strlen(q);
1560                         break;
1561                     case REDIR_ASF:
1562                         snprintf(q, c->buffer_size,
1563                                       "HTTP/1.0 200 ASF Redirect follows\r\n"
1564                                       "Content-type: video/x-ms-asf\r\n"
1565                                       "\r\n"
1566                                       "[Reference]\r\n"
1567                                       "Ref1=http://%s/%s%s\r\n", hostbuf, filename, info);
1568                         q += strlen(q);
1569                         break;
1570                     case REDIR_RTSP:
1571                         {
1572                             char hostname[256], *p;
1573                             /* extract only hostname */
1574                             av_strlcpy(hostname, hostbuf, sizeof(hostname));
1575                             p = strrchr(hostname, ':');
1576                             if (p)
1577                                 *p = '\0';
1578                             snprintf(q, c->buffer_size,
1579                                           "HTTP/1.0 200 RTSP Redirect follows\r\n"
1580                                           /* XXX: incorrect MIME type ? */
1581                                           "Content-type: application/x-rtsp\r\n"
1582                                           "\r\n"
1583                                           "rtsp://%s:%d/%s\r\n", hostname, ntohs(config.rtsp_addr.sin_port), filename);
1584                             q += strlen(q);
1585                         }
1586                         break;
1587                     case REDIR_SDP:
1588                         {
1589                             uint8_t *sdp_data;
1590                             int sdp_data_size;
1591                             socklen_t len;
1592                             struct sockaddr_in my_addr;
1593
1594                             snprintf(q, c->buffer_size,
1595                                           "HTTP/1.0 200 OK\r\n"
1596                                           "Content-type: application/sdp\r\n"
1597                                           "\r\n");
1598                             q += strlen(q);
1599
1600                             len = sizeof(my_addr);
1601
1602                             /* XXX: Should probably fail? */
1603                             if (getsockname(c->fd, (struct sockaddr *)&my_addr, &len))
1604                                 http_log("getsockname() failed\n");
1605
1606                             /* XXX: should use a dynamic buffer */
1607                             sdp_data_size = prepare_sdp_description(stream,
1608                                                                     &sdp_data,
1609                                                                     my_addr.sin_addr);
1610                             if (sdp_data_size > 0) {
1611                                 memcpy(q, sdp_data, sdp_data_size);
1612                                 q += sdp_data_size;
1613                                 *q = '\0';
1614                                 av_free(sdp_data);
1615                             }
1616                         }
1617                         break;
1618                     default:
1619                         abort();
1620                         break;
1621                     }
1622
1623                     /* prepare output buffer */
1624                     c->buffer_ptr = c->buffer;
1625                     c->buffer_end = q;
1626                     c->state = HTTPSTATE_SEND_HEADER;
1627                     return 0;
1628                 }
1629             }
1630         }
1631
1632         snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
1633         goto send_error;
1634     }
1635
1636     stream->conns_served++;
1637
1638     /* XXX: add there authenticate and IP match */
1639
1640     if (c->post) {
1641         /* if post, it means a feed is being sent */
1642         if (!stream->is_feed) {
1643             /* However it might be a status report from WMP! Let us log the
1644              * data as it might come handy one day. */
1645             const char *logline = 0;
1646             int client_id = 0;
1647
1648             for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1649                 if (av_strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1650                     logline = p;
1651                     break;
1652                 }
1653                 if (av_strncasecmp(p, "Pragma: client-id=", 18) == 0)
1654                     client_id = strtol(p + 18, 0, 10);
1655                 p = strchr(p, '\n');
1656                 if (!p)
1657                     break;
1658
1659                 p++;
1660             }
1661
1662             if (logline) {
1663                 char *eol = strchr(logline, '\n');
1664
1665                 logline += 17;
1666
1667                 if (eol) {
1668                     if (eol[-1] == '\r')
1669                         eol--;
1670                     http_log("%.*s\n", (int) (eol - logline), logline);
1671                     c->suppress_log = 1;
1672                 }
1673             }
1674
1675 #ifdef DEBUG
1676             http_log("\nGot request:\n%s\n", c->buffer);
1677 #endif
1678
1679             if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1680                 HTTPContext *wmpc;
1681
1682                 /* Now we have to find the client_id */
1683                 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1684                     if (wmpc->wmp_client_id == client_id)
1685                         break;
1686                 }
1687
1688                 if (wmpc && modify_current_stream(wmpc, ratebuf))
1689                     wmpc->switch_pending = 1;
1690             }
1691
1692             snprintf(msg, sizeof(msg), "POST command not handled");
1693             c->stream = 0;
1694             goto send_error;
1695         }
1696         if (http_start_receive_data(c) < 0) {
1697             snprintf(msg, sizeof(msg), "could not open feed");
1698             goto send_error;
1699         }
1700         c->http_error = 0;
1701         c->state = HTTPSTATE_RECEIVE_DATA;
1702         return 0;
1703     }
1704
1705 #ifdef DEBUG
1706     if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
1707         http_log("\nGot request:\n%s\n", c->buffer);
1708 #endif
1709
1710     if (c->stream->stream_type == STREAM_TYPE_STATUS)
1711         goto send_status;
1712
1713     /* open input stream */
1714     if (open_input_stream(c, info) < 0) {
1715         snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1716         goto send_error;
1717     }
1718
1719     /* prepare HTTP header */
1720     c->buffer[0] = 0;
1721     av_strlcatf(c->buffer, c->buffer_size, "HTTP/1.0 200 OK\r\n");
1722     mime_type = c->stream->fmt->mime_type;
1723     if (!mime_type)
1724         mime_type = "application/x-octet-stream";
1725     av_strlcatf(c->buffer, c->buffer_size, "Pragma: no-cache\r\n");
1726
1727     /* for asf, we need extra headers */
1728     if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1729         /* Need to allocate a client id */
1730
1731         c->wmp_client_id = av_lfg_get(&random_state);
1732
1733         av_strlcatf(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);
1734     }
1735     av_strlcatf(c->buffer, c->buffer_size, "Content-Type: %s\r\n", mime_type);
1736     av_strlcatf(c->buffer, c->buffer_size, "\r\n");
1737     q = c->buffer + strlen(c->buffer);
1738
1739     /* prepare output buffer */
1740     c->http_error = 0;
1741     c->buffer_ptr = c->buffer;
1742     c->buffer_end = q;
1743     c->state = HTTPSTATE_SEND_HEADER;
1744     return 0;
1745  send_error:
1746     c->http_error = 404;
1747     q = c->buffer;
1748     htmlstrip(msg);
1749     snprintf(q, c->buffer_size,
1750                   "HTTP/1.0 404 Not Found\r\n"
1751                   "Content-type: text/html\r\n"
1752                   "\r\n"
1753                   "<html>\n"
1754                   "<head><title>404 Not Found</title></head>\n"
1755                   "<body>%s</body>\n"
1756                   "</html>\n", msg);
1757     q += strlen(q);
1758     /* prepare output buffer */
1759     c->buffer_ptr = c->buffer;
1760     c->buffer_end = q;
1761     c->state = HTTPSTATE_SEND_HEADER;
1762     return 0;
1763  send_status:
1764     compute_status(c);
1765     /* horrible: we use this value to avoid
1766      * going to the send data state */
1767     c->http_error = 200;
1768     c->state = HTTPSTATE_SEND_HEADER;
1769     return 0;
1770 }
1771
1772 static void fmt_bytecount(AVIOContext *pb, int64_t count)
1773 {
1774     static const char suffix[] = " kMGTP";
1775     const char *s;
1776
1777     for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
1778
1779     avio_printf(pb, "%"PRId64"%c", count, *s);
1780 }
1781
1782 static inline void print_stream_params(AVIOContext *pb, FFServerStream *stream)
1783 {
1784     int i, stream_no;
1785     const char *type = "unknown";
1786     char parameters[64];
1787     AVStream *st;
1788     AVCodec *codec;
1789
1790     stream_no = stream->nb_streams;
1791
1792     avio_printf(pb, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>"
1793                     "type<th>kbit/s<th align=left>codec<th align=left>"
1794                     "Parameters\n");
1795
1796     for (i = 0; i < stream_no; i++) {
1797         st = stream->streams[i];
1798         codec = avcodec_find_encoder(st->codec->codec_id);
1799
1800         parameters[0] = 0;
1801
1802         switch(st->codec->codec_type) {
1803         case AVMEDIA_TYPE_AUDIO:
1804             type = "audio";
1805             snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz",
1806                      st->codec->channels, st->codec->sample_rate);
1807             break;
1808         case AVMEDIA_TYPE_VIDEO:
1809             type = "video";
1810             snprintf(parameters, sizeof(parameters),
1811                      "%dx%d, q=%d-%d, fps=%d", st->codec->width,
1812                      st->codec->height, st->codec->qmin, st->codec->qmax,
1813                      st->codec->time_base.den / st->codec->time_base.num);
1814             break;
1815         default:
1816             abort();
1817         }
1818
1819         avio_printf(pb, "<tr><td align=right>%d<td>%s<td align=right>%"PRId64
1820                         "<td>%s<td>%s\n",
1821                     i, type, (int64_t)st->codec->bit_rate/1000,
1822                     codec ? codec->name : "", parameters);
1823      }
1824
1825      avio_printf(pb, "</table>\n");
1826 }
1827
1828 static void compute_status(HTTPContext *c)
1829 {
1830     HTTPContext *c1;
1831     FFServerStream *stream;
1832     char *p;
1833     time_t ti;
1834     int i, len;
1835     AVIOContext *pb;
1836
1837     if (avio_open_dyn_buf(&pb) < 0) {
1838         /* XXX: return an error ? */
1839         c->buffer_ptr = c->buffer;
1840         c->buffer_end = c->buffer;
1841         return;
1842     }
1843
1844     avio_printf(pb, "HTTP/1.0 200 OK\r\n");
1845     avio_printf(pb, "Content-type: text/html\r\n");
1846     avio_printf(pb, "Pragma: no-cache\r\n");
1847     avio_printf(pb, "\r\n");
1848
1849     avio_printf(pb, "<html><head><title>%s Status</title>\n", program_name);
1850     if (c->stream->feed_filename[0])
1851         avio_printf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n",
1852                     c->stream->feed_filename);
1853     avio_printf(pb, "</head>\n<body>");
1854     avio_printf(pb, "<h1>%s Status</h1>\n", program_name);
1855     /* format status */
1856     avio_printf(pb, "<h2>Available Streams</h2>\n");
1857     avio_printf(pb, "<table cellspacing=0 cellpadding=4>\n");
1858     avio_printf(pb, "<tr><th valign=top>Path<th align=left>Served<br>Conns<th><br>bytes<th valign=top>Format<th>Bit rate<br>kbit/s<th align=left>Video<br>kbit/s<th><br>Codec<th align=left>Audio<br>kbit/s<th><br>Codec<th align=left valign=top>Feed\n");
1859     stream = config.first_stream;
1860     while (stream) {
1861         char sfilename[1024];
1862         char *eosf;
1863
1864         if (stream->feed == stream) {
1865             stream = stream->next;
1866             continue;
1867         }
1868
1869         av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
1870         eosf = sfilename + strlen(sfilename);
1871         if (eosf - sfilename >= 4) {
1872             if (strcmp(eosf - 4, ".asf") == 0)
1873                 strcpy(eosf - 4, ".asx");
1874             else if (strcmp(eosf - 3, ".rm") == 0)
1875                 strcpy(eosf - 3, ".ram");
1876             else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
1877                 /* generate a sample RTSP director if
1878                  * unicast. Generate an SDP redirector if
1879                  * multicast */
1880                 eosf = strrchr(sfilename, '.');
1881                 if (!eosf)
1882                     eosf = sfilename + strlen(sfilename);
1883                 if (stream->is_multicast)
1884                     strcpy(eosf, ".sdp");
1885                 else
1886                     strcpy(eosf, ".rtsp");
1887             }
1888         }
1889
1890         avio_printf(pb, "<tr><td><a href=\"/%s\">%s</a> ",
1891                     sfilename, stream->filename);
1892         avio_printf(pb, "<td align=right> %d <td align=right> ",
1893                     stream->conns_served);
1894         fmt_bytecount(pb, stream->bytes_served);
1895
1896         switch(stream->stream_type) {
1897         case STREAM_TYPE_LIVE: {
1898             int audio_bit_rate = 0;
1899             int video_bit_rate = 0;
1900             const char *audio_codec_name = "";
1901             const char *video_codec_name = "";
1902             const char *audio_codec_name_extra = "";
1903             const char *video_codec_name_extra = "";
1904
1905             for(i=0;i<stream->nb_streams;i++) {
1906                 AVStream *st = stream->streams[i];
1907                 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1908
1909                 switch(st->codec->codec_type) {
1910                 case AVMEDIA_TYPE_AUDIO:
1911                     audio_bit_rate += st->codec->bit_rate;
1912                     if (codec) {
1913                         if (*audio_codec_name)
1914                             audio_codec_name_extra = "...";
1915                         audio_codec_name = codec->name;
1916                     }
1917                     break;
1918                 case AVMEDIA_TYPE_VIDEO:
1919                     video_bit_rate += st->codec->bit_rate;
1920                     if (codec) {
1921                         if (*video_codec_name)
1922                             video_codec_name_extra = "...";
1923                         video_codec_name = codec->name;
1924                     }
1925                     break;
1926                 case AVMEDIA_TYPE_DATA:
1927                     video_bit_rate += st->codec->bit_rate;
1928                     break;
1929                 default:
1930                     abort();
1931                 }
1932             }
1933
1934             avio_printf(pb, "<td align=center> %s <td align=right> %d "
1935                             "<td align=right> %d <td> %s %s <td align=right> "
1936                             "%d <td> %s %s",
1937                         stream->fmt->name, stream->bandwidth,
1938                         video_bit_rate / 1000, video_codec_name,
1939                         video_codec_name_extra, audio_bit_rate / 1000,
1940                         audio_codec_name, audio_codec_name_extra);
1941
1942             if (stream->feed)
1943                 avio_printf(pb, "<td>%s", stream->feed->filename);
1944             else
1945                 avio_printf(pb, "<td>%s", stream->feed_filename);
1946             avio_printf(pb, "\n");
1947         }
1948             break;
1949         default:
1950             avio_printf(pb, "<td align=center> - <td align=right> - "
1951                             "<td align=right> - <td><td align=right> - <td>\n");
1952             break;
1953         }
1954         stream = stream->next;
1955     }
1956     avio_printf(pb, "</table>\n");
1957
1958     stream = config.first_stream;
1959     while (stream) {
1960
1961         if (stream->feed != stream) {
1962             stream = stream->next;
1963             continue;
1964         }
1965
1966         avio_printf(pb, "<h2>Feed %s</h2>", stream->filename);
1967         if (stream->pid) {
1968             avio_printf(pb, "Running as pid %"PRId64".\n", (int64_t) stream->pid);
1969
1970 #if defined(linux)
1971             {
1972                 FILE *pid_stat;
1973                 char ps_cmd[64];
1974
1975                 /* This is somewhat linux specific I guess */
1976                 snprintf(ps_cmd, sizeof(ps_cmd),
1977                          "ps -o \"%%cpu,cputime\" --no-headers %"PRId64"",
1978                          (int64_t) stream->pid);
1979
1980                  pid_stat = popen(ps_cmd, "r");
1981                  if (pid_stat) {
1982                      char cpuperc[10];
1983                      char cpuused[64];
1984
1985                      if (fscanf(pid_stat, "%9s %63s", cpuperc, cpuused) == 2) {
1986                          avio_printf(pb, "Currently using %s%% of the cpu. "
1987                                          "Total time used %s.\n",
1988                                      cpuperc, cpuused);
1989                      }
1990                      fclose(pid_stat);
1991                  }
1992             }
1993 #endif
1994
1995             avio_printf(pb, "<p>");
1996         }
1997
1998         print_stream_params(pb, stream);
1999         stream = stream->next;
2000     }
2001
2002     /* connection status */
2003     avio_printf(pb, "<h2>Connection Status</h2>\n");
2004
2005     avio_printf(pb, "Number of connections: %d / %d<br>\n",
2006                 nb_connections, config.nb_max_connections);
2007
2008     avio_printf(pb, "Bandwidth in use: %"PRIu64"k / %"PRIu64"k<br>\n",
2009                 current_bandwidth, config.max_bandwidth);
2010
2011     avio_printf(pb, "<table>\n");
2012     avio_printf(pb, "<tr><th>#<th>File<th>IP<th>Proto<th>State<th>Target "
2013                     "bit/s<th>Actual bit/s<th>Bytes transferred\n");
2014     c1 = first_http_ctx;
2015     i = 0;
2016     while (c1) {
2017         int bitrate;
2018         int j;
2019
2020         bitrate = 0;
2021         if (c1->stream) {
2022             for (j = 0; j < c1->stream->nb_streams; j++) {
2023                 if (!c1->stream->feed)
2024                     bitrate += c1->stream->streams[j]->codec->bit_rate;
2025                 else if (c1->feed_streams[j] >= 0)
2026                     bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
2027             }
2028         }
2029
2030         i++;
2031         p = inet_ntoa(c1->from_addr.sin_addr);
2032         avio_printf(pb, "<tr><td><b>%d</b><td>%s%s<td>%s<td>%s<td>%s"
2033                         "<td align=right>",
2034                     i, c1->stream ? c1->stream->filename : "",
2035                     c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "", p,
2036                     c1->protocol, http_state[c1->state]);
2037         fmt_bytecount(pb, bitrate);
2038         avio_printf(pb, "<td align=right>");
2039         fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
2040         avio_printf(pb, "<td align=right>");
2041         fmt_bytecount(pb, c1->data_count);
2042         avio_printf(pb, "\n");
2043         c1 = c1->next;
2044     }
2045     avio_printf(pb, "</table>\n");
2046
2047     /* date */
2048     ti = time(NULL);
2049     p = ctime(&ti);
2050     avio_printf(pb, "<hr size=1 noshade>Generated at %s", p);
2051     avio_printf(pb, "</body>\n</html>\n");
2052
2053     len = avio_close_dyn_buf(pb, &c->pb_buffer);
2054     c->buffer_ptr = c->pb_buffer;
2055     c->buffer_end = c->pb_buffer + len;
2056 }
2057
2058 static int open_input_stream(HTTPContext *c, const char *info)
2059 {
2060     char buf[128];
2061     char input_filename[1024];
2062     AVFormatContext *s = NULL;
2063     int buf_size, i, ret;
2064     int64_t stream_pos;
2065
2066     /* find file name */
2067     if (c->stream->feed) {
2068         strcpy(input_filename, c->stream->feed->feed_filename);
2069         buf_size = FFM_PACKET_SIZE;
2070         /* compute position (absolute time) */
2071         if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
2072             if ((ret = av_parse_time(&stream_pos, buf, 0)) < 0) {
2073                 http_log("Invalid date specification '%s' for stream\n", buf);
2074                 return ret;
2075             }
2076         } else if (av_find_info_tag(buf, sizeof(buf), "buffer", info)) {
2077             int prebuffer = strtol(buf, 0, 10);
2078             stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
2079         } else
2080             stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
2081     } else {
2082         strcpy(input_filename, c->stream->feed_filename);
2083         buf_size = 0;
2084         /* compute position (relative time) */
2085         if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
2086             if ((ret = av_parse_time(&stream_pos, buf, 1)) < 0) {
2087                 http_log("Invalid date specification '%s' for stream\n", buf);
2088                 return ret;
2089             }
2090         } else
2091             stream_pos = 0;
2092     }
2093     if (!input_filename[0]) {
2094         http_log("No filename was specified for stream\n");
2095         return AVERROR(EINVAL);
2096     }
2097
2098     /* open stream */
2099     ret = avformat_open_input(&s, input_filename, c->stream->ifmt,
2100                               &c->stream->in_opts);
2101     if (ret < 0) {
2102         http_log("Could not open input '%s': %s\n",
2103                  input_filename, av_err2str(ret));
2104         return ret;
2105     }
2106
2107     /* set buffer size */
2108     if (buf_size > 0) {
2109         ret = ffio_set_buf_size(s->pb, buf_size);
2110         if (ret < 0) {
2111             http_log("Failed to set buffer size\n");
2112             return ret;
2113         }
2114     }
2115
2116     s->flags |= AVFMT_FLAG_GENPTS;
2117     c->fmt_in = s;
2118     if (strcmp(s->iformat->name, "ffm") &&
2119         (ret = avformat_find_stream_info(c->fmt_in, NULL)) < 0) {
2120         http_log("Could not find stream info for input '%s'\n", input_filename);
2121         avformat_close_input(&s);
2122         return ret;
2123     }
2124
2125     /* choose stream as clock source (we favor the video stream if
2126      * present) for packet sending */
2127     c->pts_stream_index = 0;
2128     for(i=0;i<c->stream->nb_streams;i++) {
2129         if (c->pts_stream_index == 0 &&
2130             c->stream->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2131             c->pts_stream_index = i;
2132         }
2133     }
2134
2135     if (c->fmt_in->iformat->read_seek)
2136         av_seek_frame(c->fmt_in, -1, stream_pos, 0);
2137     /* set the start time (needed for maxtime and RTP packet timing) */
2138     c->start_time = cur_time;
2139     c->first_pts = AV_NOPTS_VALUE;
2140     return 0;
2141 }
2142
2143 /* return the server clock (in us) */
2144 static int64_t get_server_clock(HTTPContext *c)
2145 {
2146     /* compute current pts value from system time */
2147     return (cur_time - c->start_time) * 1000;
2148 }
2149
2150 /* return the estimated time (in us) at which the current packet must be sent */
2151 static int64_t get_packet_send_clock(HTTPContext *c)
2152 {
2153     int bytes_left, bytes_sent, frame_bytes;
2154
2155     frame_bytes = c->cur_frame_bytes;
2156     if (frame_bytes <= 0)
2157         return c->cur_pts;
2158
2159     bytes_left = c->buffer_end - c->buffer_ptr;
2160     bytes_sent = frame_bytes - bytes_left;
2161     return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
2162 }
2163
2164
2165 static int http_prepare_data(HTTPContext *c)
2166 {
2167     int i, len, ret;
2168     AVFormatContext *ctx;
2169
2170     av_freep(&c->pb_buffer);
2171     switch(c->state) {
2172     case HTTPSTATE_SEND_DATA_HEADER:
2173         ctx = avformat_alloc_context();
2174         if (!ctx)
2175             return AVERROR(ENOMEM);
2176         c->fmt_ctx = *ctx;
2177         av_freep(&ctx);
2178         av_dict_copy(&(c->fmt_ctx.metadata), c->stream->metadata, 0);
2179         c->fmt_ctx.streams = av_mallocz_array(c->stream->nb_streams,
2180                                               sizeof(AVStream *));
2181         if (!c->fmt_ctx.streams)
2182             return AVERROR(ENOMEM);
2183
2184         for(i=0;i<c->stream->nb_streams;i++) {
2185             AVStream *src;
2186             c->fmt_ctx.streams[i] = av_mallocz(sizeof(AVStream));
2187
2188             /* if file or feed, then just take streams from FFServerStream
2189              * struct */
2190             if (!c->stream->feed ||
2191                 c->stream->feed == c->stream)
2192                 src = c->stream->streams[i];
2193             else
2194                 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2195
2196             *(c->fmt_ctx.streams[i]) = *src;
2197             c->fmt_ctx.streams[i]->priv_data = 0;
2198             /* XXX: should be done in AVStream, not in codec */
2199             c->fmt_ctx.streams[i]->codec->frame_number = 0;
2200         }
2201         /* set output format parameters */
2202         c->fmt_ctx.oformat = c->stream->fmt;
2203         c->fmt_ctx.nb_streams = c->stream->nb_streams;
2204
2205         c->got_key_frame = 0;
2206
2207         /* prepare header and save header data in a stream */
2208         if (avio_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2209             /* XXX: potential leak */
2210             return -1;
2211         }
2212         c->fmt_ctx.pb->seekable = 0;
2213
2214         /*
2215          * HACK to avoid MPEG-PS muxer to spit many underflow errors
2216          * Default value from FFmpeg
2217          * Try to set it using configuration option
2218          */
2219         c->fmt_ctx.max_delay = (int)(0.7*AV_TIME_BASE);
2220
2221         if ((ret = avformat_write_header(&c->fmt_ctx, NULL)) < 0) {
2222             http_log("Error writing output header for stream '%s': %s\n",
2223                      c->stream->filename, av_err2str(ret));
2224             return ret;
2225         }
2226         av_dict_free(&c->fmt_ctx.metadata);
2227
2228         len = avio_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2229         c->buffer_ptr = c->pb_buffer;
2230         c->buffer_end = c->pb_buffer + len;
2231
2232         c->state = HTTPSTATE_SEND_DATA;
2233         c->last_packet_sent = 0;
2234         break;
2235     case HTTPSTATE_SEND_DATA:
2236         /* find a new packet */
2237         /* read a packet from the input stream */
2238         if (c->stream->feed)
2239             ffm_set_write_index(c->fmt_in,
2240                                 c->stream->feed->feed_write_index,
2241                                 c->stream->feed->feed_size);
2242
2243         if (c->stream->max_time &&
2244             c->stream->max_time + c->start_time - cur_time < 0)
2245             /* We have timed out */
2246             c->state = HTTPSTATE_SEND_DATA_TRAILER;
2247         else {
2248             AVPacket pkt;
2249         redo:
2250             ret = av_read_frame(c->fmt_in, &pkt);
2251             if (ret < 0) {
2252                 if (c->stream->feed) {
2253                     /* if coming from feed, it means we reached the end of the
2254                      * ffm file, so must wait for more data */
2255                     c->state = HTTPSTATE_WAIT_FEED;
2256                     return 1; /* state changed */
2257                 }
2258                 if (ret == AVERROR(EAGAIN)) {
2259                     /* input not ready, come back later */
2260                     return 0;
2261                 }
2262                 if (c->stream->loop) {
2263                     avformat_close_input(&c->fmt_in);
2264                     if (open_input_stream(c, "") < 0)
2265                         goto no_loop;
2266                     goto redo;
2267                 } else {
2268                     no_loop:
2269                         /* must send trailer now because EOF or error */
2270                         c->state = HTTPSTATE_SEND_DATA_TRAILER;
2271                 }
2272             } else {
2273                 int source_index = pkt.stream_index;
2274                 /* update first pts if needed */
2275                 if (c->first_pts == AV_NOPTS_VALUE) {
2276                     c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2277                     c->start_time = cur_time;
2278                 }
2279                 /* send it to the appropriate stream */
2280                 if (c->stream->feed) {
2281                     /* if coming from a feed, select the right stream */
2282                     if (c->switch_pending) {
2283                         c->switch_pending = 0;
2284                         for(i=0;i<c->stream->nb_streams;i++) {
2285                             if (c->switch_feed_streams[i] == pkt.stream_index)
2286                                 if (pkt.flags & AV_PKT_FLAG_KEY)
2287                                     c->switch_feed_streams[i] = -1;
2288                             if (c->switch_feed_streams[i] >= 0)
2289                                 c->switch_pending = 1;
2290                         }
2291                     }
2292                     for(i=0;i<c->stream->nb_streams;i++) {
2293                         if (c->stream->feed_streams[i] == pkt.stream_index) {
2294                             AVStream *st = c->fmt_in->streams[source_index];
2295                             pkt.stream_index = i;
2296                             if (pkt.flags & AV_PKT_FLAG_KEY &&
2297                                 (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2298                                  c->stream->nb_streams == 1))
2299                                 c->got_key_frame = 1;
2300                             if (!c->stream->send_on_key || c->got_key_frame)
2301                                 goto send_it;
2302                         }
2303                     }
2304                 } else {
2305                     AVCodecContext *codec;
2306                     AVStream *ist, *ost;
2307                 send_it:
2308                     ist = c->fmt_in->streams[source_index];
2309                     /* specific handling for RTP: we use several
2310                      * output streams (one for each RTP connection).
2311                      * XXX: need more abstract handling */
2312                     if (c->is_packetized) {
2313                         /* compute send time and duration */
2314                         c->cur_pts = av_rescale_q(pkt.dts, ist->time_base, AV_TIME_BASE_Q);
2315                         c->cur_pts -= c->first_pts;
2316                         c->cur_frame_duration = av_rescale_q(pkt.duration, ist->time_base, AV_TIME_BASE_Q);
2317                         /* find RTP context */
2318                         c->packet_stream_index = pkt.stream_index;
2319                         ctx = c->rtp_ctx[c->packet_stream_index];
2320                         if(!ctx) {
2321                             av_packet_unref(&pkt);
2322                             break;
2323                         }
2324                         codec = ctx->streams[0]->codec;
2325                         /* only one stream per RTP connection */
2326                         pkt.stream_index = 0;
2327                     } else {
2328                         ctx = &c->fmt_ctx;
2329                         /* Fudge here */
2330                         codec = ctx->streams[pkt.stream_index]->codec;
2331                     }
2332
2333                     if (c->is_packetized) {
2334                         int max_packet_size;
2335                         if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP)
2336                             max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2337                         else
2338                             max_packet_size = c->rtp_handles[c->packet_stream_index]->max_packet_size;
2339                         ret = ffio_open_dyn_packet_buf(&ctx->pb,
2340                                                        max_packet_size);
2341                     } else
2342                         ret = avio_open_dyn_buf(&ctx->pb);
2343
2344                     if (ret < 0) {
2345                         /* XXX: potential leak */
2346                         return -1;
2347                     }
2348                     ost = ctx->streams[pkt.stream_index];
2349
2350                     ctx->pb->seekable = 0;
2351                     if (pkt.dts != AV_NOPTS_VALUE)
2352                         pkt.dts = av_rescale_q(pkt.dts, ist->time_base,
2353                                                ost->time_base);
2354                     if (pkt.pts != AV_NOPTS_VALUE)
2355                         pkt.pts = av_rescale_q(pkt.pts, ist->time_base,
2356                                                ost->time_base);
2357                     pkt.duration = av_rescale_q(pkt.duration, ist->time_base,
2358                                                 ost->time_base);
2359                     if ((ret = av_write_frame(ctx, &pkt)) < 0) {
2360                         http_log("Error writing frame to output for stream '%s': %s\n",
2361                                  c->stream->filename, av_err2str(ret));
2362                         c->state = HTTPSTATE_SEND_DATA_TRAILER;
2363                     }
2364
2365                     av_freep(&c->pb_buffer);
2366                     len = avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
2367                     c->cur_frame_bytes = len;
2368                     c->buffer_ptr = c->pb_buffer;
2369                     c->buffer_end = c->pb_buffer + len;
2370
2371                     codec->frame_number++;
2372                     if (len == 0) {
2373                         av_packet_unref(&pkt);
2374                         goto redo;
2375                     }
2376                 }
2377                 av_packet_unref(&pkt);
2378             }
2379         }
2380         break;
2381     default:
2382     case HTTPSTATE_SEND_DATA_TRAILER:
2383         /* last packet test ? */
2384         if (c->last_packet_sent || c->is_packetized)
2385             return -1;
2386         ctx = &c->fmt_ctx;
2387         /* prepare header */
2388         if (avio_open_dyn_buf(&ctx->pb) < 0) {
2389             /* XXX: potential leak */
2390             return -1;
2391         }
2392         c->fmt_ctx.pb->seekable = 0;
2393         av_write_trailer(ctx);
2394         len = avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
2395         c->buffer_ptr = c->pb_buffer;
2396         c->buffer_end = c->pb_buffer + len;
2397
2398         c->last_packet_sent = 1;
2399         break;
2400     }
2401     return 0;
2402 }
2403
2404 /* should convert the format at the same time */
2405 /* send data starting at c->buffer_ptr to the output connection
2406  * (either UDP or TCP)
2407  */
2408 static int http_send_data(HTTPContext *c)
2409 {
2410     int len, ret;
2411
2412     for(;;) {
2413         if (c->buffer_ptr >= c->buffer_end) {
2414             ret = http_prepare_data(c);
2415             if (ret < 0)
2416                 return -1;
2417             else if (ret)
2418                 /* state change requested */
2419                 break;
2420         } else {
2421             if (c->is_packetized) {
2422                 /* RTP data output */
2423                 len = c->buffer_end - c->buffer_ptr;
2424                 if (len < 4) {
2425                     /* fail safe - should never happen */
2426                 fail1:
2427                     c->buffer_ptr = c->buffer_end;
2428                     return 0;
2429                 }
2430                 len = (c->buffer_ptr[0] << 24) |
2431                     (c->buffer_ptr[1] << 16) |
2432                     (c->buffer_ptr[2] << 8) |
2433                     (c->buffer_ptr[3]);
2434                 if (len > (c->buffer_end - c->buffer_ptr))
2435                     goto fail1;
2436                 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2437                     /* nothing to send yet: we can wait */
2438                     return 0;
2439                 }
2440
2441                 c->data_count += len;
2442                 update_datarate(&c->datarate, c->data_count);
2443                 if (c->stream)
2444                     c->stream->bytes_served += len;
2445
2446                 if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP) {
2447                     /* RTP packets are sent inside the RTSP TCP connection */
2448                     AVIOContext *pb;
2449                     int interleaved_index, size;
2450                     uint8_t header[4];
2451                     HTTPContext *rtsp_c;
2452
2453                     rtsp_c = c->rtsp_c;
2454                     /* if no RTSP connection left, error */
2455                     if (!rtsp_c)
2456                         return -1;
2457                     /* if already sending something, then wait. */
2458                     if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
2459                         break;
2460                     if (avio_open_dyn_buf(&pb) < 0)
2461                         goto fail1;
2462                     interleaved_index = c->packet_stream_index * 2;
2463                     /* RTCP packets are sent at odd indexes */
2464                     if (c->buffer_ptr[1] == 200)
2465                         interleaved_index++;
2466                     /* write RTSP TCP header */
2467                     header[0] = '$';
2468                     header[1] = interleaved_index;
2469                     header[2] = len >> 8;
2470                     header[3] = len;
2471                     avio_write(pb, header, 4);
2472                     /* write RTP packet data */
2473                     c->buffer_ptr += 4;
2474                     avio_write(pb, c->buffer_ptr, len);
2475                     size = avio_close_dyn_buf(pb, &c->packet_buffer);
2476                     /* prepare asynchronous TCP sending */
2477                     rtsp_c->packet_buffer_ptr = c->packet_buffer;
2478                     rtsp_c->packet_buffer_end = c->packet_buffer + size;
2479                     c->buffer_ptr += len;
2480
2481                     /* send everything we can NOW */
2482                     len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2483                                rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2484                     if (len > 0)
2485                         rtsp_c->packet_buffer_ptr += len;
2486                     if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2487                         /* if we could not send all the data, we will
2488                          * send it later, so a new state is needed to
2489                          * "lock" the RTSP TCP connection */
2490                         rtsp_c->state = RTSPSTATE_SEND_PACKET;
2491                         break;
2492                     } else
2493                         /* all data has been sent */
2494                         av_freep(&c->packet_buffer);
2495                 } else {
2496                     /* send RTP packet directly in UDP */
2497                     c->buffer_ptr += 4;
2498                     ffurl_write(c->rtp_handles[c->packet_stream_index],
2499                                 c->buffer_ptr, len);
2500                     c->buffer_ptr += len;
2501                     /* here we continue as we can send several packets
2502                      * per 10 ms slot */
2503                 }
2504             } else {
2505                 /* TCP data output */
2506                 len = send(c->fd, c->buffer_ptr,
2507                            c->buffer_end - c->buffer_ptr, 0);
2508                 if (len < 0) {
2509                     if (ff_neterrno() != AVERROR(EAGAIN) &&
2510                         ff_neterrno() != AVERROR(EINTR))
2511                         /* error : close connection */
2512                         return -1;
2513                     else
2514                         return 0;
2515                 }
2516                 c->buffer_ptr += len;
2517
2518                 c->data_count += len;
2519                 update_datarate(&c->datarate, c->data_count);
2520                 if (c->stream)
2521                     c->stream->bytes_served += len;
2522                 break;
2523             }
2524         }
2525     } /* for(;;) */
2526     return 0;
2527 }
2528
2529 static int http_start_receive_data(HTTPContext *c)
2530 {
2531     int fd;
2532     int ret;
2533
2534     if (c->stream->feed_opened) {
2535         http_log("Stream feed '%s' was not opened\n",
2536                  c->stream->feed_filename);
2537         return AVERROR(EINVAL);
2538     }
2539
2540     /* Don't permit writing to this one */
2541     if (c->stream->readonly) {
2542         http_log("Cannot write to read-only file '%s'\n",
2543                  c->stream->feed_filename);
2544         return AVERROR(EINVAL);
2545     }
2546
2547     /* open feed */
2548     fd = open(c->stream->feed_filename, O_RDWR);
2549     if (fd < 0) {
2550         ret = AVERROR(errno);
2551         http_log("Could not open feed file '%s': %s\n",
2552                  c->stream->feed_filename, strerror(errno));
2553         return ret;
2554     }
2555     c->feed_fd = fd;
2556
2557     if (c->stream->truncate) {
2558         /* truncate feed file */
2559         ffm_write_write_index(c->feed_fd, FFM_PACKET_SIZE);
2560         http_log("Truncating feed file '%s'\n", c->stream->feed_filename);
2561         if (ftruncate(c->feed_fd, FFM_PACKET_SIZE) < 0) {
2562             ret = AVERROR(errno);
2563             http_log("Error truncating feed file '%s': %s\n",
2564                      c->stream->feed_filename, strerror(errno));
2565             return ret;
2566         }
2567     } else {
2568         ret = ffm_read_write_index(fd);
2569         if (ret < 0) {
2570             http_log("Error reading write index from feed file '%s': %s\n",
2571                      c->stream->feed_filename, strerror(errno));
2572             return ret;
2573         }
2574         c->stream->feed_write_index = ret;
2575     }
2576
2577     c->stream->feed_write_index = FFMAX(ffm_read_write_index(fd),
2578                                         FFM_PACKET_SIZE);
2579     c->stream->feed_size = lseek(fd, 0, SEEK_END);
2580     lseek(fd, 0, SEEK_SET);
2581
2582     /* init buffer input */
2583     c->buffer_ptr = c->buffer;
2584     c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2585     c->stream->feed_opened = 1;
2586     c->chunked_encoding = !!av_stristr(c->buffer, "Transfer-Encoding: chunked");
2587     return 0;
2588 }
2589
2590 static int http_receive_data(HTTPContext *c)
2591 {
2592     HTTPContext *c1;
2593     int len, loop_run = 0;
2594
2595     while (c->chunked_encoding && !c->chunk_size &&
2596            c->buffer_end > c->buffer_ptr) {
2597         /* read chunk header, if present */
2598         len = recv(c->fd, c->buffer_ptr, 1, 0);
2599
2600         if (len < 0) {
2601             if (ff_neterrno() != AVERROR(EAGAIN) &&
2602                 ff_neterrno() != AVERROR(EINTR))
2603                 /* error : close connection */
2604                 goto fail;
2605             return 0;
2606         } else if (len == 0) {
2607             /* end of connection : close it */
2608             goto fail;
2609         } else if (c->buffer_ptr - c->buffer >= 2 &&
2610                    !memcmp(c->buffer_ptr - 1, "\r\n", 2)) {
2611             c->chunk_size = strtol(c->buffer, 0, 16);
2612             if (c->chunk_size == 0) // end of stream
2613                 goto fail;
2614             c->buffer_ptr = c->buffer;
2615             break;
2616         } else if (++loop_run > 10)
2617             /* no chunk header, abort */
2618             goto fail;
2619         else
2620             c->buffer_ptr++;
2621     }
2622
2623     if (c->buffer_end > c->buffer_ptr) {
2624         len = recv(c->fd, c->buffer_ptr,
2625                    FFMIN(c->chunk_size, c->buffer_end - c->buffer_ptr), 0);
2626         if (len < 0) {
2627             if (ff_neterrno() != AVERROR(EAGAIN) &&
2628                 ff_neterrno() != AVERROR(EINTR))
2629                 /* error : close connection */
2630                 goto fail;
2631         } else if (len == 0)
2632             /* end of connection : close it */
2633             goto fail;
2634         else {
2635             c->chunk_size -= len;
2636             c->buffer_ptr += len;
2637             c->data_count += len;
2638             update_datarate(&c->datarate, c->data_count);
2639         }
2640     }
2641
2642     if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2643         if (c->buffer[0] != 'f' ||
2644             c->buffer[1] != 'm') {
2645             http_log("Feed stream has become desynchronized -- disconnecting\n");
2646             goto fail;
2647         }
2648     }
2649
2650     if (c->buffer_ptr >= c->buffer_end) {
2651         FFServerStream *feed = c->stream;
2652         /* a packet has been received : write it in the store, except
2653          * if header */
2654         if (c->data_count > FFM_PACKET_SIZE) {
2655             /* XXX: use llseek or url_seek
2656              * XXX: Should probably fail? */
2657             if (lseek(c->feed_fd, feed->feed_write_index, SEEK_SET) == -1)
2658                 http_log("Seek to %"PRId64" failed\n", feed->feed_write_index);
2659
2660             if (write(c->feed_fd, c->buffer, FFM_PACKET_SIZE) < 0) {
2661                 http_log("Error writing to feed file: %s\n", strerror(errno));
2662                 goto fail;
2663             }
2664
2665             feed->feed_write_index += FFM_PACKET_SIZE;
2666             /* update file size */
2667             if (feed->feed_write_index > c->stream->feed_size)
2668                 feed->feed_size = feed->feed_write_index;
2669
2670             /* handle wrap around if max file size reached */
2671             if (c->stream->feed_max_size &&
2672                 feed->feed_write_index >= c->stream->feed_max_size)
2673                 feed->feed_write_index = FFM_PACKET_SIZE;
2674
2675             /* write index */
2676             if (ffm_write_write_index(c->feed_fd, feed->feed_write_index) < 0) {
2677                 http_log("Error writing index to feed file: %s\n",
2678                          strerror(errno));
2679                 goto fail;
2680             }
2681
2682             /* wake up any waiting connections */
2683             for(c1 = first_http_ctx; c1; c1 = c1->next) {
2684                 if (c1->state == HTTPSTATE_WAIT_FEED &&
2685                     c1->stream->feed == c->stream->feed)
2686                     c1->state = HTTPSTATE_SEND_DATA;
2687             }
2688         } else {
2689             /* We have a header in our hands that contains useful data */
2690             AVFormatContext *s = avformat_alloc_context();
2691             AVIOContext *pb;
2692             AVInputFormat *fmt_in;
2693             int i;
2694
2695             if (!s)
2696                 goto fail;
2697
2698             /* use feed output format name to find corresponding input format */
2699             fmt_in = av_find_input_format(feed->fmt->name);
2700             if (!fmt_in)
2701                 goto fail;
2702
2703             pb = avio_alloc_context(c->buffer, c->buffer_end - c->buffer,
2704                                     0, NULL, NULL, NULL, NULL);
2705             if (!pb)
2706                 goto fail;
2707
2708             pb->seekable = 0;
2709
2710             s->pb = pb;
2711             if (avformat_open_input(&s, c->stream->feed_filename, fmt_in, NULL) < 0) {
2712                 av_freep(&pb);
2713                 goto fail;
2714             }
2715
2716             /* Now we have the actual streams */
2717             if (s->nb_streams != feed->nb_streams) {
2718                 avformat_close_input(&s);
2719                 av_freep(&pb);
2720                 http_log("Feed '%s' stream number does not match registered feed\n",
2721                          c->stream->feed_filename);
2722                 goto fail;
2723             }
2724
2725             for (i = 0; i < s->nb_streams; i++) {
2726                 AVStream *fst = feed->streams[i];
2727                 AVStream *st = s->streams[i];
2728                 avcodec_copy_context(fst->codec, st->codec);
2729             }
2730
2731             avformat_close_input(&s);
2732             av_freep(&pb);
2733         }
2734         c->buffer_ptr = c->buffer;
2735     }
2736
2737     return 0;
2738  fail:
2739     c->stream->feed_opened = 0;
2740     close(c->feed_fd);
2741     /* wake up any waiting connections to stop waiting for feed */
2742     for(c1 = first_http_ctx; c1; c1 = c1->next) {
2743         if (c1->state == HTTPSTATE_WAIT_FEED &&
2744             c1->stream->feed == c->stream->feed)
2745             c1->state = HTTPSTATE_SEND_DATA_TRAILER;
2746     }
2747     return -1;
2748 }
2749
2750 /********************************************************************/
2751 /* RTSP handling */
2752
2753 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2754 {
2755     const char *str;
2756     time_t ti;
2757     struct tm *tm;
2758     char buf2[32];
2759
2760     str = RTSP_STATUS_CODE2STRING(error_number);
2761     if (!str)
2762         str = "Unknown Error";
2763
2764     avio_printf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2765     avio_printf(c->pb, "CSeq: %d\r\n", c->seq);
2766
2767     /* output GMT time */
2768     ti = time(NULL);
2769     tm = gmtime(&ti);
2770     strftime(buf2, sizeof(buf2), "%a, %d %b %Y %H:%M:%S", tm);
2771     avio_printf(c->pb, "Date: %s GMT\r\n", buf2);
2772 }
2773
2774 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2775 {
2776     rtsp_reply_header(c, error_number);
2777     avio_printf(c->pb, "\r\n");
2778 }
2779
2780 static int rtsp_parse_request(HTTPContext *c)
2781 {
2782     const char *p, *p1, *p2;
2783     char cmd[32];
2784     char url[1024];
2785     char protocol[32];
2786     char line[1024];
2787     int len;
2788     RTSPMessageHeader header1 = { 0 }, *header = &header1;
2789
2790     c->buffer_ptr[0] = '\0';
2791     p = c->buffer;
2792
2793     get_word(cmd, sizeof(cmd), &p);
2794     get_word(url, sizeof(url), &p);
2795     get_word(protocol, sizeof(protocol), &p);
2796
2797     av_strlcpy(c->method, cmd, sizeof(c->method));
2798     av_strlcpy(c->url, url, sizeof(c->url));
2799     av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2800
2801     if (avio_open_dyn_buf(&c->pb) < 0) {
2802         /* XXX: cannot do more */
2803         c->pb = NULL; /* safety */
2804         return -1;
2805     }
2806
2807     /* check version name */
2808     if (strcmp(protocol, "RTSP/1.0")) {
2809         rtsp_reply_error(c, RTSP_STATUS_VERSION);
2810         goto the_end;
2811     }
2812
2813     /* parse each header line */
2814     /* skip to next line */
2815     while (*p != '\n' && *p != '\0')
2816         p++;
2817     if (*p == '\n')
2818         p++;
2819     while (*p != '\0') {
2820         p1 = memchr(p, '\n', (char *)c->buffer_ptr - p);
2821         if (!p1)
2822             break;
2823         p2 = p1;
2824         if (p2 > p && p2[-1] == '\r')
2825             p2--;
2826         /* skip empty line */
2827         if (p2 == p)
2828             break;
2829         len = p2 - p;
2830         if (len > sizeof(line) - 1)
2831             len = sizeof(line) - 1;
2832         memcpy(line, p, len);
2833         line[len] = '\0';
2834         ff_rtsp_parse_line(header, line, NULL, NULL);
2835         p = p1 + 1;
2836     }
2837
2838     /* handle sequence number */
2839     c->seq = header->seq;
2840
2841     if (!strcmp(cmd, "DESCRIBE"))
2842         rtsp_cmd_describe(c, url);
2843     else if (!strcmp(cmd, "OPTIONS"))
2844         rtsp_cmd_options(c, url);
2845     else if (!strcmp(cmd, "SETUP"))
2846         rtsp_cmd_setup(c, url, header);
2847     else if (!strcmp(cmd, "PLAY"))
2848         rtsp_cmd_play(c, url, header);
2849     else if (!strcmp(cmd, "PAUSE"))
2850         rtsp_cmd_interrupt(c, url, header, 1);
2851     else if (!strcmp(cmd, "TEARDOWN"))
2852         rtsp_cmd_interrupt(c, url, header, 0);
2853     else
2854         rtsp_reply_error(c, RTSP_STATUS_METHOD);
2855
2856  the_end:
2857     len = avio_close_dyn_buf(c->pb, &c->pb_buffer);
2858     c->pb = NULL; /* safety */
2859     if (len < 0)
2860         /* XXX: cannot do more */
2861         return -1;
2862
2863     c->buffer_ptr = c->pb_buffer;
2864     c->buffer_end = c->pb_buffer + len;
2865     c->state = RTSPSTATE_SEND_REPLY;
2866     return 0;
2867 }
2868
2869 static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
2870                                    struct in_addr my_ip)
2871 {
2872     AVFormatContext *avc;
2873     AVStream *avs = NULL;
2874     AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
2875     AVDictionaryEntry *entry = av_dict_get(stream->metadata, "title", NULL, 0);
2876     int i;
2877
2878     *pbuffer = NULL;
2879
2880     avc =  avformat_alloc_context();
2881     if (!avc || !rtp_format)
2882         return -1;
2883
2884     avc->oformat = rtp_format;
2885     av_dict_set(&avc->metadata, "title",
2886                 entry ? entry->value : "No Title", 0);
2887     avc->nb_streams = stream->nb_streams;
2888     if (stream->is_multicast) {
2889         snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2890                  inet_ntoa(stream->multicast_ip),
2891                  stream->multicast_port, stream->multicast_ttl);
2892     } else
2893         snprintf(avc->filename, 1024, "rtp://0.0.0.0");
2894
2895     avc->streams = av_malloc_array(avc->nb_streams, sizeof(*avc->streams));
2896     if (!avc->streams)
2897         goto sdp_done;
2898
2899     avs = av_malloc_array(avc->nb_streams, sizeof(*avs));
2900     if (!avs)
2901         goto sdp_done;
2902
2903     for(i = 0; i < stream->nb_streams; i++) {
2904         avc->streams[i] = &avs[i];
2905         avc->streams[i]->codec = stream->streams[i]->codec;
2906     }
2907     *pbuffer = av_mallocz(2048);
2908     if (!*pbuffer)
2909         goto sdp_done;
2910     av_sdp_create(&avc, 1, *pbuffer, 2048);
2911
2912  sdp_done:
2913     av_freep(&avc->streams);
2914     av_dict_free(&avc->metadata);
2915     av_free(avc);
2916     av_free(avs);
2917
2918     return *pbuffer ? strlen(*pbuffer) : AVERROR(ENOMEM);
2919 }
2920
2921 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2922 {
2923     /* rtsp_reply_header(c, RTSP_STATUS_OK); */
2924     avio_printf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2925     avio_printf(c->pb, "CSeq: %d\r\n", c->seq);
2926     avio_printf(c->pb, "Public: %s\r\n",
2927                 "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2928     avio_printf(c->pb, "\r\n");
2929 }
2930
2931 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2932 {
2933     FFServerStream *stream;
2934     char path1[1024];
2935     const char *path;
2936     uint8_t *content;
2937     int content_length;
2938     socklen_t len;
2939     struct sockaddr_in my_addr;
2940
2941     /* find which URL is asked */
2942     av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2943     path = path1;
2944     if (*path == '/')
2945         path++;
2946
2947     for(stream = config.first_stream; stream; stream = stream->next) {
2948         if (!stream->is_feed &&
2949             stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2950             !strcmp(path, stream->filename)) {
2951             goto found;
2952         }
2953     }
2954     /* no stream found */
2955     rtsp_reply_error(c, RTSP_STATUS_NOT_FOUND);
2956     return;
2957
2958  found:
2959     /* prepare the media description in SDP format */
2960
2961     /* get the host IP */
2962     len = sizeof(my_addr);
2963     getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2964     content_length = prepare_sdp_description(stream, &content,
2965                                              my_addr.sin_addr);
2966     if (content_length < 0) {
2967         rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2968         return;
2969     }
2970     rtsp_reply_header(c, RTSP_STATUS_OK);
2971     avio_printf(c->pb, "Content-Base: %s/\r\n", url);
2972     avio_printf(c->pb, "Content-Type: application/sdp\r\n");
2973     avio_printf(c->pb, "Content-Length: %d\r\n", content_length);
2974     avio_printf(c->pb, "\r\n");
2975     avio_write(c->pb, content, content_length);
2976     av_free(content);
2977 }
2978
2979 static HTTPContext *find_rtp_session(const char *session_id)
2980 {
2981     HTTPContext *c;
2982
2983     if (session_id[0] == '\0')
2984         return NULL;
2985
2986     for(c = first_http_ctx; c; c = c->next) {
2987         if (!strcmp(c->session_id, session_id))
2988             return c;
2989     }
2990     return NULL;
2991 }
2992
2993 static RTSPTransportField *find_transport(RTSPMessageHeader *h, enum RTSPLowerTransport lower_transport)
2994 {
2995     RTSPTransportField *th;
2996     int i;
2997
2998     for(i=0;i<h->nb_transports;i++) {
2999         th = &h->transports[i];
3000         if (th->lower_transport == lower_transport)
3001             return th;
3002     }
3003     return NULL;
3004 }
3005
3006 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
3007                            RTSPMessageHeader *h)
3008 {
3009     FFServerStream *stream;
3010     int stream_index, rtp_port, rtcp_port;
3011     char buf[1024];
3012     char path1[1024];
3013     const char *path;
3014     HTTPContext *rtp_c;
3015     RTSPTransportField *th;
3016     struct sockaddr_in dest_addr;
3017     RTSPActionServerSetup setup;
3018
3019     /* find which URL is asked */
3020     av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
3021     path = path1;
3022     if (*path == '/')
3023         path++;
3024
3025     /* now check each stream */
3026     for(stream = config.first_stream; stream; stream = stream->next) {
3027         if (stream->is_feed || !stream->fmt ||
3028             strcmp(stream->fmt->name, "rtp")) {
3029             continue;
3030         }
3031         /* accept aggregate filenames only if single stream */
3032         if (!strcmp(path, stream->filename)) {
3033             if (stream->nb_streams != 1) {
3034                 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
3035                 return;
3036             }
3037             stream_index = 0;
3038             goto found;
3039         }
3040
3041         for(stream_index = 0; stream_index < stream->nb_streams;
3042             stream_index++) {
3043             snprintf(buf, sizeof(buf), "%s/streamid=%d",
3044                      stream->filename, stream_index);
3045             if (!strcmp(path, buf))
3046                 goto found;
3047         }
3048     }
3049     /* no stream found */
3050     rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
3051     return;
3052  found:
3053
3054     /* generate session id if needed */
3055     if (h->session_id[0] == '\0') {
3056         unsigned random0 = av_lfg_get(&random_state);
3057         unsigned random1 = av_lfg_get(&random_state);
3058         snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
3059                  random0, random1);
3060     }
3061
3062     /* find RTP session, and create it if none found */
3063     rtp_c = find_rtp_session(h->session_id);
3064     if (!rtp_c) {
3065         /* always prefer UDP */
3066         th = find_transport(h, RTSP_LOWER_TRANSPORT_UDP);
3067         if (!th) {
3068             th = find_transport(h, RTSP_LOWER_TRANSPORT_TCP);
3069             if (!th) {
3070                 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
3071                 return;
3072             }
3073         }
3074
3075         rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
3076                                    th->lower_transport);
3077         if (!rtp_c) {
3078             rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
3079             return;
3080         }
3081
3082         /* open input stream */
3083         if (open_input_stream(rtp_c, "") < 0) {
3084             rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
3085             return;
3086         }
3087     }
3088
3089     /* test if stream is OK (test needed because several SETUP needs
3090      * to be done for a given file) */
3091     if (rtp_c->stream != stream) {
3092         rtsp_reply_error(c, RTSP_STATUS_SERVICE);
3093         return;
3094     }
3095
3096     /* test if stream is already set up */
3097     if (rtp_c->rtp_ctx[stream_index]) {
3098         rtsp_reply_error(c, RTSP_STATUS_STATE);
3099         return;
3100     }
3101
3102     /* check transport */
3103     th = find_transport(h, rtp_c->rtp_protocol);
3104     if (!th || (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
3105                 th->client_port_min <= 0)) {
3106         rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
3107         return;
3108     }
3109
3110     /* setup default options */
3111     setup.transport_option[0] = '\0';
3112     dest_addr = rtp_c->from_addr;
3113     dest_addr.sin_port = htons(th->client_port_min);
3114
3115     /* setup stream */
3116     if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
3117         rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
3118         return;
3119     }
3120
3121     /* now everything is OK, so we can send the connection parameters */
3122     rtsp_reply_header(c, RTSP_STATUS_OK);
3123     /* session ID */
3124     avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3125
3126     switch(rtp_c->rtp_protocol) {
3127     case RTSP_LOWER_TRANSPORT_UDP:
3128         rtp_port = ff_rtp_get_local_rtp_port(rtp_c->rtp_handles[stream_index]);
3129         rtcp_port = ff_rtp_get_local_rtcp_port(rtp_c->rtp_handles[stream_index]);
3130         avio_printf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
3131                     "client_port=%d-%d;server_port=%d-%d",
3132                     th->client_port_min, th->client_port_max,
3133                     rtp_port, rtcp_port);
3134         break;
3135     case RTSP_LOWER_TRANSPORT_TCP:
3136         avio_printf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
3137                     stream_index * 2, stream_index * 2 + 1);
3138         break;
3139     default:
3140         break;
3141     }
3142     if (setup.transport_option[0] != '\0')
3143         avio_printf(c->pb, ";%s", setup.transport_option);
3144     avio_printf(c->pb, "\r\n");
3145
3146
3147     avio_printf(c->pb, "\r\n");
3148 }
3149
3150
3151 /**
3152  * find an RTP connection by using the session ID. Check consistency
3153  * with filename
3154  */
3155 static HTTPContext *find_rtp_session_with_url(const char *url,
3156                                               const char *session_id)
3157 {
3158     HTTPContext *rtp_c;
3159     char path1[1024];
3160     const char *path;
3161     char buf[1024];
3162     int s, len;
3163
3164     rtp_c = find_rtp_session(session_id);
3165     if (!rtp_c)
3166         return NULL;
3167
3168     /* find which URL is asked */
3169     av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
3170     path = path1;
3171     if (*path == '/')
3172         path++;
3173     if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
3174     for(s=0; s<rtp_c->stream->nb_streams; ++s) {
3175       snprintf(buf, sizeof(buf), "%s/streamid=%d",
3176         rtp_c->stream->filename, s);
3177       if(!strncmp(path, buf, sizeof(buf)))
3178         /* XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE
3179          * if nb_streams>1? */
3180         return rtp_c;
3181     }
3182     len = strlen(path);
3183     if (len > 0 && path[len - 1] == '/' &&
3184         !strncmp(path, rtp_c->stream->filename, len - 1))
3185         return rtp_c;
3186     return NULL;
3187 }
3188
3189 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPMessageHeader *h)
3190 {
3191     HTTPContext *rtp_c;
3192
3193     rtp_c = find_rtp_session_with_url(url, h->session_id);
3194     if (!rtp_c) {
3195         rtsp_reply_error(c, RTSP_STATUS_SESSION);
3196         return;
3197     }
3198
3199     if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3200         rtp_c->state != HTTPSTATE_WAIT_FEED &&
3201         rtp_c->state != HTTPSTATE_READY) {
3202         rtsp_reply_error(c, RTSP_STATUS_STATE);
3203         return;
3204     }
3205
3206     rtp_c->state = HTTPSTATE_SEND_DATA;
3207
3208     /* now everything is OK, so we can send the connection parameters */
3209     rtsp_reply_header(c, RTSP_STATUS_OK);
3210     /* session ID */
3211     avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3212     avio_printf(c->pb, "\r\n");
3213 }
3214
3215 static void rtsp_cmd_interrupt(HTTPContext *c, const char *url,
3216                                RTSPMessageHeader *h, int pause_only)
3217 {
3218     HTTPContext *rtp_c;
3219
3220     rtp_c = find_rtp_session_with_url(url, h->session_id);
3221     if (!rtp_c) {
3222         rtsp_reply_error(c, RTSP_STATUS_SESSION);
3223         return;
3224     }
3225
3226     if (pause_only) {
3227         if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3228             rtp_c->state != HTTPSTATE_WAIT_FEED) {
3229             rtsp_reply_error(c, RTSP_STATUS_STATE);
3230             return;
3231         }
3232         rtp_c->state = HTTPSTATE_READY;
3233         rtp_c->first_pts = AV_NOPTS_VALUE;
3234     }
3235
3236     /* now everything is OK, so we can send the connection parameters */
3237     rtsp_reply_header(c, RTSP_STATUS_OK);
3238     /* session ID */
3239     avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3240     avio_printf(c->pb, "\r\n");
3241
3242     if (!pause_only)
3243         close_connection(rtp_c);
3244 }
3245
3246 /********************************************************************/
3247 /* RTP handling */
3248
3249 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3250                                        FFServerStream *stream,
3251                                        const char *session_id,
3252                                        enum RTSPLowerTransport rtp_protocol)
3253 {
3254     HTTPContext *c = NULL;
3255     const char *proto_str;
3256
3257     /* XXX: should output a warning page when coming
3258      * close to the connection limit */
3259     if (nb_connections >= config.nb_max_connections)
3260         goto fail;
3261
3262     /* add a new connection */
3263     c = av_mallocz(sizeof(HTTPContext));
3264     if (!c)
3265         goto fail;
3266
3267     c->fd = -1;
3268     c->poll_entry = NULL;
3269     c->from_addr = *from_addr;
3270     c->buffer_size = IOBUFFER_INIT_SIZE;
3271     c->buffer = av_malloc(c->buffer_size);
3272     if (!c->buffer)
3273         goto fail;
3274     nb_connections++;
3275     c->stream = stream;
3276     av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
3277     c->state = HTTPSTATE_READY;
3278     c->is_packetized = 1;
3279     c->rtp_protocol = rtp_protocol;
3280
3281     /* protocol is shown in statistics */
3282     switch(c->rtp_protocol) {
3283     case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
3284         proto_str = "MCAST";
3285         break;
3286     case RTSP_LOWER_TRANSPORT_UDP:
3287         proto_str = "UDP";
3288         break;
3289     case RTSP_LOWER_TRANSPORT_TCP:
3290         proto_str = "TCP";
3291         break;
3292     default:
3293         proto_str = "???";
3294         break;
3295     }
3296     av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
3297     av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
3298
3299     current_bandwidth += stream->bandwidth;
3300
3301     c->next = first_http_ctx;
3302     first_http_ctx = c;
3303     return c;
3304
3305  fail:
3306     if (c) {
3307         av_freep(&c->buffer);
3308         av_free(c);
3309     }
3310     return NULL;
3311 }
3312
3313 /**
3314  * add a new RTP stream in an RTP connection (used in RTSP SETUP
3315  * command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3316  * used.
3317  */
3318 static int rtp_new_av_stream(HTTPContext *c,
3319                              int stream_index, struct sockaddr_in *dest_addr,
3320                              HTTPContext *rtsp_c)
3321 {
3322     AVFormatContext *ctx;
3323     AVStream *st;
3324     char *ipaddr;
3325     URLContext *h = NULL;
3326     uint8_t *dummy_buf;
3327     int max_packet_size;
3328
3329     /* now we can open the relevant output stream */
3330     ctx = avformat_alloc_context();
3331     if (!ctx)
3332         return -1;
3333     ctx->oformat = av_guess_format("rtp", NULL, NULL);
3334
3335     st = av_mallocz(sizeof(AVStream));
3336     if (!st)
3337         goto fail;
3338     ctx->nb_streams = 1;
3339     ctx->streams = av_mallocz_array(ctx->nb_streams, sizeof(AVStream *));
3340     if (!ctx->streams)
3341       goto fail;
3342     ctx->streams[0] = st;
3343
3344     if (!c->stream->feed ||
3345         c->stream->feed == c->stream)
3346         memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3347     else
3348         memcpy(st,
3349                c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3350                sizeof(AVStream));
3351     st->priv_data = NULL;
3352
3353     /* build destination RTP address */
3354     ipaddr = inet_ntoa(dest_addr->sin_addr);
3355
3356     switch(c->rtp_protocol) {
3357     case RTSP_LOWER_TRANSPORT_UDP:
3358     case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
3359         /* RTP/UDP case */
3360
3361         /* XXX: also pass as parameter to function ? */
3362         if (c->stream->is_multicast) {
3363             int ttl;
3364             ttl = c->stream->multicast_ttl;
3365             if (!ttl)
3366                 ttl = 16;
3367             snprintf(ctx->filename, sizeof(ctx->filename),
3368                      "rtp://%s:%d?multicast=1&ttl=%d",
3369                      ipaddr, ntohs(dest_addr->sin_port), ttl);
3370         } else {
3371             snprintf(ctx->filename, sizeof(ctx->filename),
3372                      "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3373         }
3374
3375         if (ffurl_open(&h, ctx->filename, AVIO_FLAG_WRITE, NULL, NULL) < 0)
3376             goto fail;
3377         c->rtp_handles[stream_index] = h;
3378         max_packet_size = h->max_packet_size;
3379         break;
3380     case RTSP_LOWER_TRANSPORT_TCP:
3381         /* RTP/TCP case */
3382         c->rtsp_c = rtsp_c;
3383         max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3384         break;
3385     default:
3386         goto fail;
3387     }
3388
3389     http_log("%s:%d - - \"PLAY %s/streamid=%d %s\"\n",
3390              ipaddr, ntohs(dest_addr->sin_port),
3391              c->stream->filename, stream_index, c->protocol);
3392
3393     /* normally, no packets should be output here, but the packet size may
3394      * be checked */
3395     if (ffio_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0)
3396         /* XXX: close stream */
3397         goto fail;
3398
3399     if (avformat_write_header(ctx, NULL) < 0) {
3400     fail:
3401         if (h)
3402             ffurl_close(h);
3403         av_free(st);
3404         av_free(ctx);
3405         return -1;
3406     }
3407     avio_close_dyn_buf(ctx->pb, &dummy_buf);
3408     av_free(dummy_buf);
3409
3410     c->rtp_ctx[stream_index] = ctx;
3411     return 0;
3412 }
3413
3414 /********************************************************************/
3415 /* ffserver initialization */
3416
3417 static AVStream *add_av_stream1(FFServerStream *stream,
3418                                 AVCodecContext *codec, int copy)
3419 {
3420     AVStream *fst;
3421
3422     if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
3423         return NULL;
3424
3425     fst = av_mallocz(sizeof(AVStream));
3426     if (!fst)
3427         return NULL;
3428     if (copy) {
3429         fst->codec = avcodec_alloc_context3(codec->codec);
3430         if (!fst->codec) {
3431             av_free(fst);
3432             return NULL;
3433         }
3434         avcodec_copy_context(fst->codec, codec);
3435     } else
3436         /* live streams must use the actual feed's codec since it may be
3437          * updated later to carry extradata needed by them.
3438          */
3439         fst->codec = codec;
3440
3441     fst->priv_data = av_mallocz(sizeof(FeedData));
3442     fst->index = stream->nb_streams;
3443     avpriv_set_pts_info(fst, 33, 1, 90000);
3444     fst->sample_aspect_ratio = codec->sample_aspect_ratio;
3445     stream->streams[stream->nb_streams++] = fst;
3446     return fst;
3447 }
3448
3449 /* return the stream number in the feed */
3450 static int add_av_stream(FFServerStream *feed, AVStream *st)
3451 {
3452     AVStream *fst;
3453     AVCodecContext *av, *av1;
3454     int i;
3455
3456     av = st->codec;
3457     for(i=0;i<feed->nb_streams;i++) {
3458         av1 = feed->streams[i]->codec;
3459         if (av1->codec_id == av->codec_id &&
3460             av1->codec_type == av->codec_type &&
3461             av1->bit_rate == av->bit_rate) {
3462
3463             switch(av->codec_type) {
3464             case AVMEDIA_TYPE_AUDIO:
3465                 if (av1->channels == av->channels &&
3466                     av1->sample_rate == av->sample_rate)
3467                     return i;
3468                 break;
3469             case AVMEDIA_TYPE_VIDEO:
3470                 if (av1->width == av->width &&
3471                     av1->height == av->height &&
3472                     av1->time_base.den == av->time_base.den &&
3473                     av1->time_base.num == av->time_base.num &&
3474                     av1->gop_size == av->gop_size)
3475                     return i;
3476                 break;
3477             default:
3478                 abort();
3479             }
3480         }
3481     }
3482
3483     fst = add_av_stream1(feed, av, 0);
3484     if (!fst)
3485         return -1;
3486     if (av_stream_get_recommended_encoder_configuration(st))
3487         av_stream_set_recommended_encoder_configuration(fst,
3488             av_strdup(av_stream_get_recommended_encoder_configuration(st)));
3489     return feed->nb_streams - 1;
3490 }
3491
3492 static void remove_stream(FFServerStream *stream)
3493 {
3494     FFServerStream **ps;
3495     ps = &config.first_stream;
3496     while (*ps) {
3497         if (*ps == stream)
3498             *ps = (*ps)->next;
3499         else
3500             ps = &(*ps)->next;
3501     }
3502 }
3503
3504 /* specific MPEG4 handling : we extract the raw parameters */
3505 static void extract_mpeg4_header(AVFormatContext *infile)
3506 {
3507     int mpeg4_count, i, size;
3508     AVPacket pkt;
3509     AVStream *st;
3510     const uint8_t *p;
3511
3512     infile->flags |= AVFMT_FLAG_NOFILLIN | AVFMT_FLAG_NOPARSE;
3513
3514     mpeg4_count = 0;
3515     for(i=0;i<infile->nb_streams;i++) {
3516         st = infile->streams[i];
3517         if (st->codec->codec_id == AV_CODEC_ID_MPEG4 &&
3518             st->codec->extradata_size == 0) {
3519             mpeg4_count++;
3520         }
3521     }
3522     if (!mpeg4_count)
3523         return;
3524
3525     printf("MPEG4 without extra data: trying to find header in %s\n",
3526            infile->filename);
3527     while (mpeg4_count > 0) {
3528         if (av_read_frame(infile, &pkt) < 0)
3529             break;
3530         st = infile->streams[pkt.stream_index];
3531         if (st->codec->codec_id == AV_CODEC_ID_MPEG4 &&
3532             st->codec->extradata_size == 0) {
3533             av_freep(&st->codec->extradata);
3534             /* fill extradata with the header */
3535             /* XXX: we make hard suppositions here ! */
3536             p = pkt.data;
3537             while (p < pkt.data + pkt.size - 4) {
3538                 /* stop when vop header is found */
3539                 if (p[0] == 0x00 && p[1] == 0x00 &&
3540                     p[2] == 0x01 && p[3] == 0xb6) {
3541                     size = p - pkt.data;
3542                     st->codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
3543                     st->codec->extradata_size = size;
3544                     memcpy(st->codec->extradata, pkt.data, size);
3545                     break;
3546                 }
3547                 p++;
3548             }
3549             mpeg4_count--;
3550         }
3551         av_packet_unref(&pkt);
3552     }
3553 }
3554
3555 /* compute the needed AVStream for each file */
3556 static void build_file_streams(void)
3557 {
3558     FFServerStream *stream, *stream_next;
3559     int i, ret;
3560
3561     /* gather all streams */
3562     for(stream = config.first_stream; stream; stream = stream_next) {
3563         AVFormatContext *infile = NULL;
3564         stream_next = stream->next;
3565         if (stream->stream_type == STREAM_TYPE_LIVE &&
3566             !stream->feed) {
3567             /* the stream comes from a file */
3568             /* try to open the file */
3569             /* open stream */
3570             if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
3571                 /* specific case : if transport stream output to RTP,
3572                  * we use a raw transport stream reader */
3573                 av_dict_set(&stream->in_opts, "mpeg2ts_compute_pcr", "1", 0);
3574             }
3575
3576             if (!stream->feed_filename[0]) {
3577                 http_log("Unspecified feed file for stream '%s'\n",
3578                          stream->filename);
3579                 goto fail;
3580             }
3581
3582             http_log("Opening feed file '%s' for stream '%s'\n",
3583                      stream->feed_filename, stream->filename);
3584             ret = avformat_open_input(&infile, stream->feed_filename,
3585                                       stream->ifmt, &stream->in_opts);
3586             if (ret < 0) {
3587                 http_log("Could not open '%s': %s\n", stream->feed_filename,
3588                          av_err2str(ret));
3589                 /* remove stream (no need to spend more time on it) */
3590             fail:
3591                 remove_stream(stream);
3592             } else {
3593                 /* find all the AVStreams inside and reference them in
3594                  * 'stream' */
3595                 if (avformat_find_stream_info(infile, NULL) < 0) {
3596                     http_log("Could not find codec parameters from '%s'\n",
3597                              stream->feed_filename);
3598                     avformat_close_input(&infile);
3599                     goto fail;
3600                 }
3601                 extract_mpeg4_header(infile);
3602
3603                 for(i=0;i<infile->nb_streams;i++)
3604                     add_av_stream1(stream, infile->streams[i]->codec, 1);
3605
3606                 avformat_close_input(&infile);
3607             }
3608         }
3609     }
3610 }
3611
3612 /* compute the needed AVStream for each feed */
3613 static void build_feed_streams(void)
3614 {
3615     FFServerStream *stream, *feed;
3616     int i;
3617
3618     /* gather all streams */
3619     for(stream = config.first_stream; stream; stream = stream->next) {
3620         feed = stream->feed;
3621         if (!feed)
3622             continue;
3623
3624         if (stream->is_feed) {
3625             for(i=0;i<stream->nb_streams;i++)
3626                 stream->feed_streams[i] = i;
3627         } else {
3628             /* we handle a stream coming from a feed */
3629             for(i=0;i<stream->nb_streams;i++)
3630                 stream->feed_streams[i] = add_av_stream(feed,
3631                                                         stream->streams[i]);
3632         }
3633     }
3634
3635     /* create feed files if needed */
3636     for(feed = config.first_feed; feed; feed = feed->next_feed) {
3637         int fd;
3638
3639         if (avio_check(feed->feed_filename, AVIO_FLAG_READ) > 0) {
3640             /* See if it matches */
3641             AVFormatContext *s = NULL;
3642             int matches = 0;
3643
3644             if (avformat_open_input(&s, feed->feed_filename, NULL, NULL) >= 0) {
3645                 /* set buffer size */
3646                 int ret = ffio_set_buf_size(s->pb, FFM_PACKET_SIZE);
3647                 if (ret < 0) {
3648                     http_log("Failed to set buffer size\n");
3649                     exit(1);
3650                 }
3651
3652                 /* Now see if it matches */
3653                 if (s->nb_streams == feed->nb_streams) {
3654                     matches = 1;
3655                     for(i=0;i<s->nb_streams;i++) {
3656                         AVStream *sf, *ss;
3657                         sf = feed->streams[i];
3658                         ss = s->streams[i];
3659
3660                         if (sf->index != ss->index ||
3661                             sf->id != ss->id) {
3662                             http_log("Index & Id do not match for stream %d (%s)\n",
3663                                    i, feed->feed_filename);
3664                             matches = 0;
3665                         } else {
3666                             AVCodecContext *ccf, *ccs;
3667
3668                             ccf = sf->codec;
3669                             ccs = ss->codec;
3670 #define CHECK_CODEC(x)  (ccf->x != ccs->x)
3671
3672                             if (CHECK_CODEC(codec_id) || CHECK_CODEC(codec_type)) {
3673                                 http_log("Codecs do not match for stream %d\n", i);
3674                                 matches = 0;
3675                             } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3676                                 http_log("Codec bitrates do not match for stream %d\n", i);
3677                                 matches = 0;
3678                             } else if (ccf->codec_type == AVMEDIA_TYPE_VIDEO) {
3679                                 if (CHECK_CODEC(time_base.den) ||
3680                                     CHECK_CODEC(time_base.num) ||
3681                                     CHECK_CODEC(width) ||
3682                                     CHECK_CODEC(height)) {
3683                                     http_log("Codec width, height and framerate do not match for stream %d\n", i);
3684                                     matches = 0;
3685                                 }
3686                             } else if (ccf->codec_type == AVMEDIA_TYPE_AUDIO) {
3687                                 if (CHECK_CODEC(sample_rate) ||
3688                                     CHECK_CODEC(channels) ||
3689                                     CHECK_CODEC(frame_size)) {
3690                                     http_log("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3691                                     matches = 0;
3692                                 }
3693                             } else {
3694                                 http_log("Unknown codec type\n");
3695                                 matches = 0;
3696                             }
3697                         }
3698                         if (!matches)
3699                             break;
3700                     }
3701                 } else
3702                     http_log("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3703                         feed->feed_filename, s->nb_streams, feed->nb_streams);
3704
3705                 avformat_close_input(&s);
3706             } else
3707                 http_log("Deleting feed file '%s' as it appears to be corrupt\n",
3708                         feed->feed_filename);
3709
3710             if (!matches) {
3711                 if (feed->readonly) {
3712                     http_log("Unable to delete feed file '%s' as it is marked readonly\n",
3713                         feed->feed_filename);
3714                     exit(1);
3715                 }
3716                 unlink(feed->feed_filename);
3717             }
3718         }
3719         if (avio_check(feed->feed_filename, AVIO_FLAG_WRITE) <= 0) {
3720             AVFormatContext *s = avformat_alloc_context();
3721
3722             if (!s) {
3723                 http_log("Failed to allocate context\n");
3724                 exit(1);
3725             }
3726
3727             if (feed->readonly) {
3728                 http_log("Unable to create feed file '%s' as it is marked readonly\n",
3729                     feed->feed_filename);
3730                 exit(1);
3731             }
3732
3733             /* only write the header of the ffm file */
3734             if (avio_open(&s->pb, feed->feed_filename, AVIO_FLAG_WRITE) < 0) {
3735                 http_log("Could not open output feed file '%s'\n",
3736                          feed->feed_filename);
3737                 exit(1);
3738             }
3739             s->oformat = feed->fmt;
3740             s->nb_streams = feed->nb_streams;
3741             s->streams = feed->streams;
3742             if (avformat_write_header(s, NULL) < 0) {
3743                 http_log("Container doesn't support the required parameters\n");
3744                 exit(1);
3745             }
3746             /* XXX: need better API */
3747             av_freep(&s->priv_data);
3748             avio_closep(&s->pb);
3749             s->streams = NULL;
3750             s->nb_streams = 0;
3751             avformat_free_context(s);
3752         }
3753         /* get feed size and write index */
3754         fd = open(feed->feed_filename, O_RDONLY);
3755         if (fd < 0) {
3756             http_log("Could not open output feed file '%s'\n",
3757                     feed->feed_filename);
3758             exit(1);
3759         }
3760
3761         feed->feed_write_index = FFMAX(ffm_read_write_index(fd), FFM_PACKET_SIZE);
3762         feed->feed_size = lseek(fd, 0, SEEK_END);
3763         /* ensure that we do not wrap before the end of file */
3764         if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3765             feed->feed_max_size = feed->feed_size;
3766
3767         close(fd);
3768     }
3769 }
3770
3771 /* compute the bandwidth used by each stream */
3772 static void compute_bandwidth(void)
3773 {
3774     unsigned bandwidth;
3775     int i;
3776     FFServerStream *stream;
3777
3778     for(stream = config.first_stream; stream; stream = stream->next) {
3779         bandwidth = 0;
3780         for(i=0;i<stream->nb_streams;i++) {
3781             AVStream *st = stream->streams[i];
3782             switch(st->codec->codec_type) {
3783             case AVMEDIA_TYPE_AUDIO:
3784             case AVMEDIA_TYPE_VIDEO:
3785                 bandwidth += st->codec->bit_rate;
3786                 break;
3787             default:
3788                 break;
3789             }
3790         }
3791         stream->bandwidth = (bandwidth + 999) / 1000;
3792     }
3793 }
3794
3795 static void handle_child_exit(int sig)
3796 {
3797     pid_t pid;
3798     int status, uptime;
3799
3800     while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
3801         FFServerStream *feed;
3802
3803         for (feed = config.first_feed; feed; feed = feed->next) {
3804             if (feed->pid != pid)
3805                 continue;
3806
3807             uptime = time(0) - feed->pid_start;
3808             feed->pid = 0;
3809             fprintf(stderr,
3810                     "%s: Pid %"PRId64" exited with status %d after %d seconds\n",
3811                     feed->filename, (int64_t) pid, status, uptime);
3812
3813             if (uptime < 30)
3814                 /* Turn off any more restarts */
3815                 ffserver_free_child_args(&feed->child_argv);
3816         }
3817     }
3818
3819     need_to_start_children = 1;
3820 }
3821
3822 static void opt_debug(void)
3823 {
3824     config.debug = 1;
3825     snprintf(config.logfilename, sizeof(config.logfilename), "-");
3826 }
3827
3828 void show_help_default(const char *opt, const char *arg)
3829 {
3830     printf("usage: ffserver [options]\n"
3831            "Hyper fast multi format Audio/Video streaming server\n");
3832     printf("\n");
3833     show_help_options(options, "Main options:", 0, 0, 0);
3834 }
3835
3836 static const OptionDef options[] = {
3837 #include "cmdutils_common_opts.h"
3838     { "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
3839     { "d", 0, {(void*)opt_debug}, "enable debug mode" },
3840     { "f", HAS_ARG | OPT_STRING, {(void*)&config.filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
3841     { NULL },
3842 };
3843
3844 int main(int argc, char **argv)
3845 {
3846     struct sigaction sigact = { { 0 } };
3847     int ret = 0;
3848
3849     config.filename = av_strdup("/etc/ffserver.conf");
3850
3851     parse_loglevel(argc, argv, options);
3852     av_register_all();
3853     avformat_network_init();
3854
3855     show_banner(argc, argv, options);
3856
3857     my_program_name = argv[0];
3858
3859     parse_options(NULL, argc, argv, options, NULL);
3860
3861     unsetenv("http_proxy");             /* Kill the http_proxy */
3862
3863     av_lfg_init(&random_state, av_get_random_seed());
3864
3865     sigact.sa_handler = handle_child_exit;
3866     sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
3867     sigaction(SIGCHLD, &sigact, 0);
3868
3869     if ((ret = ffserver_parse_ffconfig(config.filename, &config)) < 0) {
3870         fprintf(stderr, "Error reading configuration file '%s': %s\n",
3871                 config.filename, av_err2str(ret));
3872         av_freep(&config.filename);
3873         exit(1);
3874     }
3875     av_freep(&config.filename);
3876
3877     /* open log file if needed */
3878     if (config.logfilename[0] != '\0') {
3879         if (!strcmp(config.logfilename, "-"))
3880             logfile = stdout;
3881         else
3882             logfile = fopen(config.logfilename, "a");
3883         av_log_set_callback(http_av_log);
3884     }
3885
3886     build_file_streams();
3887
3888     build_feed_streams();
3889
3890     compute_bandwidth();
3891
3892     /* signal init */
3893     signal(SIGPIPE, SIG_IGN);
3894
3895     if (http_server() < 0) {
3896         http_log("Could not start server\n");
3897         exit(1);
3898     }
3899
3900     return 0;
3901 }