ffserver.c
85f07f22
 /*
773a21b8
  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
85f07f22
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
773a21b8
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
85f07f22
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
85f07f22
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
773a21b8
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
85f07f22
  *
773a21b8
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
85f07f22
  */
364a9607
 
93613338
 /**
  * @file
  * multiple format streaming server based on the FFmpeg libraries
  */
 
0f4e8165
 #include "config.h"
b250f9c6
 #if !HAVE_CLOSESOCKET
0f4e8165
 #define closesocket close
 #endif
 #include <string.h>
 #include <stdlib.h>
a4cd2ad8
 #include <stdio.h>
245976da
 #include "libavformat/avformat.h"
469c335c
 /* FIXME: those are internal headers, ffserver _really_ shouldn't use them */
3ee53dab
 #include "libavformat/ffm.h"
245976da
 #include "libavformat/network.h"
 #include "libavformat/os_support.h"
302879cb
 #include "libavformat/rtpdec.h"
b7e6da98
 #include "libavformat/rtpproto.h"
245976da
 #include "libavformat/rtsp.h"
02497a5d
 #include "libavformat/rtspcodes.h"
403ee835
 #include "libavformat/avio_internal.h"
b263bf66
 #include "libavformat/internal.h"
 #include "libavformat/url.h"
 
9c6af3a3
 #include "libavutil/avassert.h"
959da985
 #include "libavutil/avstring.h"
042819c5
 #include "libavutil/lfg.h"
d2d67e42
 #include "libavutil/dict.h"
3b20eb25
 #include "libavutil/intreadwrite.h"
0ebcdf5c
 #include "libavutil/mathematics.h"
042819c5
 #include "libavutil/random_seed.h"
737eb597
 #include "libavutil/parseutils.h"
41d0eb1c
 #include "libavutil/opt.h"
676ea8fa
 #include "libavutil/time.h"
 
85f07f22
 #include <stdarg.h>
307239bd
 #if HAVE_UNISTD_H
85f07f22
 #include <unistd.h>
307239bd
 #endif
85f07f22
 #include <fcntl.h>
 #include <sys/ioctl.h>
b250f9c6
 #if HAVE_POLL_H
f8cda19e
 #include <poll.h>
b0c858d8
 #endif
85f07f22
 #include <errno.h>
 #include <time.h>
5eb765ef
 #include <sys/wait.h>
85f07f22
 #include <signal.h>
2effd274
 
4ce5df08
 #include "cmdutils.h"
f524d2e4
 #include "ffserver_config.h"
85f07f22
 
fb349359
 #define PATH_LENGTH 1024
 
89b503b5
 const char program_name[] = "ffserver";
ea9c581f
 const int program_birth_year = 2000;
86074ed1
 
5a635bc7
 static const OptionDef options[];
 
85f07f22
 enum HTTPState {
     HTTPSTATE_WAIT_REQUEST,
     HTTPSTATE_SEND_HEADER,
     HTTPSTATE_SEND_DATA_HEADER,
2effd274
     HTTPSTATE_SEND_DATA,          /* sending TCP or UDP data */
85f07f22
     HTTPSTATE_SEND_DATA_TRAILER,
115329f1
     HTTPSTATE_RECEIVE_DATA,
2effd274
     HTTPSTATE_WAIT_FEED,          /* wait for data from the feed */
     HTTPSTATE_READY,
 
     RTSPSTATE_WAIT_REQUEST,
     RTSPSTATE_SEND_REPLY,
bc351386
     RTSPSTATE_SEND_PACKET,
85f07f22
 };
 
d9e2aceb
 static const char * const http_state[] = {
2effd274
     "HTTP_WAIT_REQUEST",
     "HTTP_SEND_HEADER",
 
85f07f22
     "SEND_DATA_HEADER",
     "SEND_DATA",
     "SEND_DATA_TRAILER",
     "RECEIVE_DATA",
     "WAIT_FEED",
2effd274
     "READY",
 
     "RTSP_WAIT_REQUEST",
     "RTSP_SEND_REPLY",
bc351386
     "RTSP_SEND_PACKET",
85f07f22
 };
 
cde25790
 #define IOBUFFER_INIT_SIZE 8192
85f07f22
 
 /* timeouts are in ms */
2effd274
 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
 
85f07f22
 #define SYNC_TIMEOUT (10 * 1000)
 
b516ecdd
 typedef struct RTSPActionServerSetup {
     uint32_t ipaddr;
     char transport_option[512];
 } RTSPActionServerSetup;
 
5eb765ef
 typedef struct {
0c1a9eda
     int64_t count1, count2;
c3f58185
     int64_t time1, time2;
5eb765ef
 } DataRateData;
 
85f07f22
 /* context associated with one connection */
 typedef struct HTTPContext {
     enum HTTPState state;
     int fd; /* socket file descriptor */
     struct sockaddr_in from_addr; /* origin */
     struct pollfd *poll_entry; /* used when polling */
c3f58185
     int64_t timeout;
0c1a9eda
     uint8_t *buffer_ptr, *buffer_end;
85f07f22
     int http_error;
edfdd798
     int post;
19c8c4ec
     int chunked_encoding;
     int chunk_size;               /* 0 if it needs to be read */
85f07f22
     struct HTTPContext *next;
42a63c6a
     int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
0c1a9eda
     int64_t data_count;
85f07f22
     /* feed input */
     int feed_fd;
     /* input format handling */
     AVFormatContext *fmt_in;
c3f58185
     int64_t start_time;            /* In milliseconds - this wraps fairly often */
0c1a9eda
     int64_t first_pts;            /* initial pts value */
e240a0bb
     int64_t cur_pts;             /* current pts value from the stream in us */
     int64_t cur_frame_duration;  /* duration of the current frame in us */
     int cur_frame_bytes;       /* output frame size, needed to compute
                                   the time at which we send each
                                   packet */
     int pts_stream_index;        /* stream we choose as clock reference */
     int64_t cur_clock;           /* current clock reference value in us */
85f07f22
     /* output format handling */
f524d2e4
     struct FFServerStream *stream;
cde25790
     /* -1 is invalid stream */
f524d2e4
     int feed_streams[FFSERVER_MAX_STREAMS]; /* index of streams in the feed */
     int switch_feed_streams[FFSERVER_MAX_STREAMS]; /* index of streams in the feed */
cde25790
     int switch_pending;
f524d2e4
     AVFormatContext fmt_ctx; /* instance of FFServerStream for one user */
85f07f22
     int last_packet_sent; /* true if last data packet was sent */
7434ba6d
     int suppress_log;
5eb765ef
     DataRateData datarate;
3120d2a2
     int wmp_client_id;
7434ba6d
     char protocol[16];
     char method[16];
     char url[128];
cde25790
     int buffer_size;
0c1a9eda
     uint8_t *buffer;
2effd274
     int is_packetized; /* if true, the stream is packetized */
     int packet_stream_index; /* current stream for output in state machine */
115329f1
 
2effd274
     /* RTSP state specific */
0c1a9eda
     uint8_t *pb_buffer; /* XXX: use that in all the code */
ae628ec1
     AVIOContext *pb;
2effd274
     int seq; /* RTSP sequence number */
115329f1
 
2effd274
     /* RTP state specific */
90abbdba
     enum RTSPLowerTransport rtp_protocol;
2effd274
     char session_id[32]; /* session id */
f524d2e4
     AVFormatContext *rtp_ctx[FFSERVER_MAX_STREAMS];
e240a0bb
 
bc351386
     /* RTP/UDP specific */
f524d2e4
     URLContext *rtp_handles[FFSERVER_MAX_STREAMS];
bc351386
 
     /* RTP/TCP specific */
     struct HTTPContext *rtsp_c;
     uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
85f07f22
 } HTTPContext;
 
 typedef struct FeedData {
     long long data_count;
8bfb108b
     float avg_frame_size;   /* frame size averaged over last frames with exponential mean */
85f07f22
 } FeedData;
 
33f5e2ec
 static HTTPContext *first_http_ctx;
f524d2e4
 
 static FFServerConfig config = {
     .nb_max_http_connections = 2000,
     .nb_max_connections = 5,
     .max_bandwidth = 1000,
aaf6cc92
     .use_defaults = 1,
f524d2e4
 };
85f07f22
 
2effd274
 static void new_connection(int server_fd, int is_rtsp);
 static void close_connection(HTTPContext *c);
 
 /* HTTP handling */
 static int handle_connection(HTTPContext *c);
1714fe29
 static inline void print_stream_params(AVIOContext *pb, FFServerStream *stream);
dca21085
 static void compute_status(HTTPContext *c);
85f07f22
 static int open_input_stream(HTTPContext *c, const char *info);
8129ccec
 static int http_parse_request(HTTPContext *c);
 static int http_send_data(HTTPContext *c);
85f07f22
 static int http_start_receive_data(HTTPContext *c);
 static int http_receive_data(HTTPContext *c);
2effd274
 
 /* RTSP handling */
 static int rtsp_parse_request(HTTPContext *c);
 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
0df65975
 static void rtsp_cmd_options(HTTPContext *c, const char *url);
1abdfb10
 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
                            RTSPMessageHeader *h);
 static void rtsp_cmd_play(HTTPContext *c, const char *url,
                           RTSPMessageHeader *h);
 static void rtsp_cmd_interrupt(HTTPContext *c, const char *url,
                                RTSPMessageHeader *h, int pause_only);
2effd274
 
829ac53d
 /* SDP handling */
f524d2e4
 static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
829ac53d
                                    struct in_addr my_ip);
 
2effd274
 /* RTP handling */
115329f1
 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
1abdfb10
                                        FFServerStream *stream,
                                        const char *session_id,
90abbdba
                                        enum RTSPLowerTransport rtp_protocol);
115329f1
 static int rtp_new_av_stream(HTTPContext *c,
bc351386
                              int stream_index, struct sockaddr_in *dest_addr,
                              HTTPContext *rtsp_c);
ae2ed20b
 /* utils */
daaa5358
 static size_t htmlencode (const char *src, char **dest);
 static inline void cp_html_entity (char *buffer, const char *entity);
ae2ed20b
 static inline int check_codec_match(AVCodecContext *ccf, AVCodecContext *ccs,
                                     int stream);
85f07f22
 
cde25790
 static const char *my_program_name;
 
2ac887ba
 static int no_launch;
5eb765ef
 static int need_to_start_children;
2ac887ba
 
1c9ff179
 /* maximum number of simultaneous HTTP connections */
4af92de6
 static unsigned int nb_connections;
85f07f22
 
1ad8289e
 static uint64_t current_bandwidth;
42a63c6a
 
469c335c
 /* Making this global saves on passing it around everywhere */
 static int64_t cur_time;
5eb765ef
 
042819c5
 static AVLFG random_state;
1df93ae9
 
85f07f22
 static FILE *logfile = NULL;
 
daaa5358
 static inline void cp_html_entity (char *buffer, const char *entity) {
     if (!buffer || !entity)
         return;
     while (*entity)
         *buffer++ = *entity++;
 }
 
 /**
  * Substitutes known conflicting chars on a text string with
  * their corresponding HTML entities.
  *
  * Returns the number of bytes in the 'encoded' representation
  * not including the terminating NUL.
  */
 static size_t htmlencode (const char *src, char **dest) {
     const char *amp = "&amp;";
     const char *lt  = "&lt;";
     const char *gt  = "&gt;";
     const char *start;
     char *tmp;
     size_t final_size = 0;
 
     if (!src)
         return 0;
 
     start = src;
 
     /* Compute needed dest size */
     while (*src != '\0') {
         switch(*src) {
             case 38: /* & */
                 final_size += 5;
                 break;
             case 60: /* < */
             case 62: /* > */
                 final_size += 4;
                 break;
             default:
                 final_size++;
         }
         src++;
     }
 
     src = start;
     *dest = av_mallocz(final_size + 1);
     if (!*dest)
         return 0;
 
     /* Build dest */
     tmp = *dest;
     while (*src != '\0') {
         switch(*src) {
             case 38: /* & */
                 cp_html_entity (tmp, amp);
                 tmp += 5;
                 break;
             case 60: /* < */
                 cp_html_entity (tmp, lt);
                 tmp += 4;
                 break;
             case 62: /* > */
                 cp_html_entity (tmp, gt);
                 tmp += 4;
                 break;
             default:
                 *tmp = *src;
                 tmp += 1;
         }
         src++;
885739f3
     }
daaa5358
     *tmp = '\0';
 
     return final_size;
885739f3
 }
 
3b20eb25
 static int64_t ffm_read_write_index(int fd)
 {
     uint8_t buf[8];
 
71bc8c95
     if (lseek(fd, 8, SEEK_SET) < 0)
         return AVERROR(EIO);
3b20eb25
     if (read(fd, buf, 8) != 8)
         return AVERROR(EIO);
     return AV_RB64(buf);
 }
 
 static int ffm_write_write_index(int fd, int64_t pos)
 {
     uint8_t buf[8];
     int i;
 
     for(i=0;i<8;i++)
         buf[i] = (pos >> (56 - i * 8)) & 0xff;
378a5b9c
     if (lseek(fd, 8, SEEK_SET) < 0)
3388bcce
         goto bail_eio;
3b20eb25
     if (write(fd, buf, 8) != 8)
3388bcce
         goto bail_eio;
 
3b20eb25
     return 8;
3388bcce
 
 bail_eio:
     return AVERROR(EIO);
3b20eb25
 }
 
 static void ffm_set_write_index(AVFormatContext *s, int64_t pos,
                                 int64_t file_size)
 {
e33d3720
     av_opt_set_int(s, "server_attached", 1, AV_OPT_SEARCH_CHILDREN);
a2f8beef
     av_opt_set_int(s, "ffm_write_index", pos, AV_OPT_SEARCH_CHILDREN);
     av_opt_set_int(s, "ffm_file_size", file_size, AV_OPT_SEARCH_CHILDREN);
3b20eb25
 }
 
6c2dbff7
 static char *ctime1(char *buf2, size_t buf_size)
9fd3442f
 {
     time_t ti;
     char *p;
 
     ti = time(NULL);
     p = ctime(&ti);
6c2dbff7
     if (!p || !*p) {
         *buf2 = '\0';
         return buf2;
     }
4a595cff
     av_strlcpy(buf2, p, buf_size);
6c2dbff7
     p = buf2 + strlen(buf2) - 1;
9fd3442f
     if (*p == '\n')
         *p = '\0';
     return buf2;
 }
 
bcd3ce59
 static void http_vlog(const char *fmt, va_list vargs)
85f07f22
 {
124ed1c0
     static int print_prefix = 1;
2ea642ff
     char buf[32];
e9754564
 
     if (!logfile)
         return;
 
7d3857b7
     if (print_prefix) {
         ctime1(buf, sizeof(buf));
         fprintf(logfile, "%s ", buf);
     }
     print_prefix = strstr(fmt, "\n") != NULL;
     vfprintf(logfile, fmt, vargs);
     fflush(logfile);
bcd3ce59
 }
 
efa6ce99
 #ifdef __GNUC__
 __attribute__ ((format (printf, 1, 2)))
 #endif
 static void http_log(const char *fmt, ...)
bcd3ce59
 {
     va_list vargs;
     va_start(vargs, fmt);
     http_vlog(fmt, vargs);
     va_end(vargs);
 }
 
 static void http_av_log(void *ptr, int level, const char *fmt, va_list vargs)
 {
     static int print_prefix = 1;
     AVClass *avc = ptr ? *(AVClass**)ptr : NULL;
49ceb58b
     if (level > av_log_get_level())
bcd3ce59
         return;
     if (print_prefix && avc)
59e7894c
         http_log("[%s @ %p]", avc->item_name(ptr), ptr);
bcd3ce59
     print_prefix = strstr(fmt, "\n") != NULL;
     http_vlog(fmt, vargs);
85f07f22
 }
 
6edd6884
 static void log_connection(HTTPContext *c)
 {
115329f1
     if (c->suppress_log)
6edd6884
         return;
 
82e0be62
     http_log("%s - - [%s] \"%s %s\" %d %"PRId64"\n",
              inet_ntoa(c->from_addr.sin_addr), c->method, c->url,
6edd6884
              c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
cde25790
 }
 
0c1a9eda
 static void update_datarate(DataRateData *drd, int64_t count)
5eb765ef
 {
     if (!drd->time1 && !drd->count1) {
         drd->time1 = drd->time2 = cur_time;
         drd->count1 = drd->count2 = count;
eeffbdea
     } else if (cur_time - drd->time2 > 5000) {
33a4ecbe
         drd->time1 = drd->time2;
         drd->count1 = drd->count2;
         drd->time2 = cur_time;
         drd->count2 = count;
5eb765ef
     }
 }
 
 /* In bytes per second */
0c1a9eda
 static int compute_datarate(DataRateData *drd, int64_t count)
5eb765ef
 {
     if (cur_time == drd->time1)
         return 0;
115329f1
 
5eb765ef
     return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
 }
 
a782f209
 
f524d2e4
 static void start_children(FFServerStream *feed)
cde25790
 {
fb349359
     char *pathname;
ec422517
     char *slash;
     int i;
fb349359
     size_t cmd_length;
ec422517
 
2ac887ba
     if (no_launch)
         return;
 
fb349359
     cmd_length = strlen(my_program_name);
 
    /**
     * FIXME: WIP Safeguard. Remove after clearing all harcoded
     * '1024' path lengths
     */
     if (cmd_length > PATH_LENGTH - 1) {
         http_log("Could not start children. Command line: '%s' exceeds "
                     "path length limit (%d)\n", my_program_name, PATH_LENGTH);
         return;
     }
 
     pathname = av_strdup (my_program_name);
     if (!pathname) {
         http_log("Could not allocate memory for children cmd line\n");
         return;
     }
ec422517
    /* replace "ffserver" with "ffmpeg" in the path of current
     * program. Ignore user provided path */
 
     slash = strrchr(pathname, '/');
     if (!slash)
         slash = pathname;
     else
         slash++;
     strcpy(slash, "ffmpeg");
 
cde25790
     for (; feed; feed = feed->next) {
ec422517
 
         if (!feed->child_argv || feed->pid)
             continue;
 
18011888
         feed->pid_start = time(0);
5eb765ef
 
18011888
         feed->pid = fork();
         if (feed->pid < 0) {
eb68c356
             http_log("Unable to create children: %s\n", strerror(errno));
dadb9514
             av_free (pathname);
1fa81430
             exit(EXIT_FAILURE);
18011888
         }
ec422517
 
18011888
         if (feed->pid)
             continue;
40444a59
 
18011888
         /* In child */
40444a59
 
18011888
         http_log("Launch command line: ");
         http_log("%s ", pathname);
cde25790
 
18011888
         for (i = 1; feed->child_argv[i] && feed->child_argv[i][0]; i++)
             http_log("%s ", feed->child_argv[i]);
         http_log("\n");
d6562d2c
 
18011888
         for (i = 3; i < 256; i++)
             close(i);
a4d70941
 
18011888
         if (!config.debug) {
             if (!freopen("/dev/null", "r", stdin))
                 http_log("failed to redirect STDIN to /dev/null\n;");
             if (!freopen("/dev/null", "w", stdout))
                 http_log("failed to redirect STDOUT to /dev/null\n;");
             if (!freopen("/dev/null", "w", stderr))
                 http_log("failed to redirect STDERR to /dev/null\n;");
         }
cde25790
 
18011888
         signal(SIGPIPE, SIG_DFL);
         execvp(pathname, feed->child_argv);
fb349359
         av_free (pathname);
18011888
         _exit(1);
cde25790
     }
fe95b0eb
     av_free (pathname);
7434ba6d
 }
 
2effd274
 /* open a listening socket */
 static int socket_open_listen(struct sockaddr_in *my_addr)
85f07f22
 {
2effd274
     int server_fd, tmp;
85f07f22
 
     server_fd = socket(AF_INET,SOCK_STREAM,0);
     if (server_fd < 0) {
         perror ("socket");
         return -1;
     }
115329f1
 
85f07f22
     tmp = 1;
8baa5b32
     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)))
         av_log(NULL, AV_LOG_WARNING, "setsockopt SO_REUSEADDR failed\n");
85f07f22
 
f5e717f3
     my_addr->sin_family = AF_INET;
2effd274
     if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
b17d099d
         char bindmsg[32];
50c1cac4
         snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)",
                  ntohs(my_addr->sin_port));
b17d099d
         perror (bindmsg);
9a168e93
         goto fail;
85f07f22
     }
115329f1
 
85f07f22
     if (listen (server_fd, 5) < 0) {
         perror ("listen");
9a168e93
         goto fail;
85f07f22
     }
6d953ae2
 
     if (ff_socket_nonblock(server_fd, 1) < 0)
         av_log(NULL, AV_LOG_WARNING, "ff_socket_nonblock failed\n");
2effd274
 
     return server_fd;
9a168e93
 
 fail:
     closesocket(server_fd);
     return -1;
2effd274
 }
 
6edd6884
 /* start all multicast streams */
 static void start_multicast(void)
 {
f524d2e4
     FFServerStream *stream;
6edd6884
     char session_id[32];
     HTTPContext *rtp_c;
a04698c4
     struct sockaddr_in dest_addr = {0};
6edd6884
     int default_port, stream_index;
719cc025
     unsigned int random0, random1;
6edd6884
 
     default_port = 6000;
f524d2e4
     for(stream = config.first_stream; stream; stream = stream->next) {
978bc432
 
719cc025
         if (!stream->is_multicast)
             continue;
611c5741
 
978bc432
         random0 = av_lfg_get(&random_state);
         random1 = av_lfg_get(&random_state);
 
         /* open the RTP connection */
d8a04d91
         snprintf(session_id, sizeof(session_id), "%08x%08x", random0, random1);
978bc432
 
         /* choose a port if none given */
         if (stream->multicast_port == 0) {
             stream->multicast_port = default_port;
             default_port += 100;
         }
 
         dest_addr.sin_family = AF_INET;
         dest_addr.sin_addr = stream->multicast_ip;
         dest_addr.sin_port = htons(stream->multicast_port);
 
         rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
                                    RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
         if (!rtp_c)
             continue;
 
         if (open_input_stream(rtp_c, "") < 0) {
             http_log("Could not open input stream for stream '%s'\n",
                      stream->filename);
             continue;
         }
 
         /* open each RTP stream */
         for(stream_index = 0; stream_index < stream->nb_streams;
             stream_index++) {
             dest_addr.sin_port = htons(stream->multicast_port +
                                        2 * stream_index);
             if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) >= 0)
6edd6884
                 continue;
978bc432
 
             http_log("Could not open output stream '%s/streamid=%d'\n",
                      stream->filename, stream_index);
             exit(1);
         }
 
         rtp_c->state = HTTPSTATE_SEND_DATA;
6edd6884
     }
 }
2effd274
 
47ba4728
 /* main loop of the HTTP server */
2effd274
 static int http_server(void)
 {
d2a1ea1d
     int server_fd = 0, rtsp_server_fd = 0;
958d98cc
     int ret, delay;
1c9ff179
     struct pollfd *poll_table, *poll_entry;
2effd274
     HTTPContext *c, *c_next;
85f07f22
 
11462236
     poll_table = av_mallocz_array(config.nb_max_http_connections + 2,
                                   sizeof(*poll_table));
     if(!poll_table) {
         http_log("Impossible to allocate a poll table handling %d "
                  "connections.\n", config.nb_max_http_connections);
1c9ff179
         return -1;
     }
 
f524d2e4
     if (config.http_addr.sin_port) {
         server_fd = socket_open_listen(&config.http_addr);
2580395e
         if (server_fd < 0)
             goto quit;
d2a1ea1d
     }
2effd274
 
f524d2e4
     if (config.rtsp_addr.sin_port) {
         rtsp_server_fd = socket_open_listen(&config.rtsp_addr);
7228bdee
         if (rtsp_server_fd < 0) {
             closesocket(server_fd);
2580395e
             goto quit;
7228bdee
         }
d2a1ea1d
     }
 
     if (!rtsp_server_fd && !server_fd) {
         http_log("HTTP and RTSP disabled.\n");
2580395e
         goto quit;
d2a1ea1d
     }
115329f1
 
a3341b9d
     http_log("FFserver started.\n");
85f07f22
 
f524d2e4
     start_children(config.first_feed);
cde25790
 
6edd6884
     start_multicast();
 
85f07f22
     for(;;) {
         poll_entry = poll_table;
d2a1ea1d
         if (server_fd) {
2b9cd1e7
             poll_entry->fd = server_fd;
             poll_entry->events = POLLIN;
             poll_entry++;
d2a1ea1d
         }
         if (rtsp_server_fd) {
2b9cd1e7
             poll_entry->fd = rtsp_server_fd;
             poll_entry->events = POLLIN;
             poll_entry++;
d2a1ea1d
         }
2effd274
 
85f07f22
         /* wait for events on each HTTP handle */
         c = first_http_ctx;
2effd274
         delay = 1000;
81a663f4
         while (c) {
85f07f22
             int fd;
             fd = c->fd;
             switch(c->state) {
2effd274
             case HTTPSTATE_SEND_HEADER:
             case RTSPSTATE_SEND_REPLY:
bc351386
             case RTSPSTATE_SEND_PACKET:
85f07f22
                 c->poll_entry = poll_entry;
                 poll_entry->fd = fd;
2effd274
                 poll_entry->events = POLLOUT;
85f07f22
                 poll_entry++;
                 break;
             case HTTPSTATE_SEND_DATA_HEADER:
             case HTTPSTATE_SEND_DATA:
             case HTTPSTATE_SEND_DATA_TRAILER:
2effd274
                 if (!c->is_packetized) {
47ba4728
                     /* for TCP, we output as much as we can
                      * (may need to put a limit) */
2effd274
                     c->poll_entry = poll_entry;
                     poll_entry->fd = fd;
                     poll_entry->events = POLLOUT;
                     poll_entry++;
                 } else {
e240a0bb
                     /* when ffserver is doing the timing, we work by
469c335c
                      * looking at which packet needs to be sent every
                      * 10 ms (one tick wait XXX: 10 ms assumed) */
958d98cc
                     if (delay > 10)
                         delay = 10;
2effd274
                 }
85f07f22
                 break;
2effd274
             case HTTPSTATE_WAIT_REQUEST:
85f07f22
             case HTTPSTATE_RECEIVE_DATA:
             case HTTPSTATE_WAIT_FEED:
2effd274
             case RTSPSTATE_WAIT_REQUEST:
85f07f22
                 /* need to catch errors */
                 c->poll_entry = poll_entry;
                 poll_entry->fd = fd;
a6e14edd
                 poll_entry->events = POLLIN;/* Maybe this will work */
85f07f22
                 poll_entry++;
                 break;
             default:
                 c->poll_entry = NULL;
                 break;
             }
             c = c->next;
         }
 
         /* wait for an event on one connection. We poll at least every
469c335c
          * second to handle timeouts */
85f07f22
         do {
2effd274
             ret = poll(poll_table, poll_entry - poll_table, delay);
28c4741a
             if (ret < 0 && ff_neterrno() != AVERROR(EAGAIN) &&
e0877aa5
                 ff_neterrno() != AVERROR(EINTR)) {
2580395e
                 goto quit;
e0877aa5
             }
e8d658df
         } while (ret < 0);
115329f1
 
c3f58185
         cur_time = av_gettime() / 1000;
85f07f22
 
5eb765ef
         if (need_to_start_children) {
             need_to_start_children = 0;
f524d2e4
             start_children(config.first_feed);
5eb765ef
         }
 
85f07f22
         /* now handle the events */
81a663f4
         for(c = first_http_ctx; c; c = c_next) {
2effd274
             c_next = c->next;
             if (handle_connection(c) < 0) {
7434ba6d
                 log_connection(c);
db93c2d0
                 /* close and free the connection */
2effd274
                 close_connection(c);
85f07f22
             }
         }
 
         poll_entry = poll_table;
d2a1ea1d
         if (server_fd) {
2b9cd1e7
             /* new HTTP connection request ? */
             if (poll_entry->revents & POLLIN)
                 new_connection(server_fd, 0);
             poll_entry++;
d2a1ea1d
         }
         if (rtsp_server_fd) {
2b9cd1e7
             /* new RTSP connection request ? */
             if (poll_entry->revents & POLLIN)
                 new_connection(rtsp_server_fd, 1);
d2a1ea1d
         }
85f07f22
     }
2580395e
 
 quit:
     av_free(poll_table);
     return -1;
85f07f22
 }
 
2effd274
 /* start waiting for a new HTTP/RTSP request */
 static void start_wait_request(HTTPContext *c, int is_rtsp)
85f07f22
 {
2effd274
     c->buffer_ptr = c->buffer;
     c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
 
c75bc268
     c->state = is_rtsp ? RTSPSTATE_WAIT_REQUEST : HTTPSTATE_WAIT_REQUEST;
     c->timeout = cur_time +
c719b7a4
                  (is_rtsp ? RTSP_REQUEST_TIMEOUT : HTTP_REQUEST_TIMEOUT);
2effd274
 }
 
0bdd8b85
 static void http_send_too_busy_reply(int fd)
 {
9c6af3a3
     char buffer[400];
0bdd8b85
     int len = snprintf(buffer, sizeof(buffer),
2a22187f
                        "HTTP/1.0 503 Server too busy\r\n"
0bdd8b85
                        "Content-type: text/html\r\n"
                        "\r\n"
b25ac3c9
                        "<!DOCTYPE html>\n"
0bdd8b85
                        "<html><head><title>Too busy</title></head><body>\r\n"
8a908994
                        "<p>The server is too busy to serve your request at "
                        "this time.</p>\r\n"
                        "<p>The number of current connections is %u, and this "
                        "exceeds the limit of %u.</p>\r\n"
0bdd8b85
                        "</body></html>\r\n",
f524d2e4
                        nb_connections, config.nb_max_connections);
9c6af3a3
     av_assert0(len < sizeof(buffer));
e79bc6a8
     if (send(fd, buffer, len, 0) < len)
50c1cac4
         av_log(NULL, AV_LOG_WARNING,
                "Could not send too-busy reply, send() failed\n");
0bdd8b85
 }
 
 
2effd274
 static void new_connection(int server_fd, int is_rtsp)
 {
     struct sockaddr_in from_addr;
cc64ec57
     socklen_t len;
     int fd;
2effd274
     HTTPContext *c = NULL;
 
     len = sizeof(from_addr);
115329f1
     fd = accept(server_fd, (struct sockaddr *)&from_addr,
2effd274
                 &len);
050056d0
     if (fd < 0) {
         http_log("error during accept %s\n", strerror(errno));
2effd274
         return;
050056d0
     }
6d953ae2
     if (ff_socket_nonblock(fd, 1) < 0)
         av_log(NULL, AV_LOG_WARNING, "ff_socket_nonblock failed\n");
2effd274
 
f524d2e4
     if (nb_connections >= config.nb_max_connections) {
0bdd8b85
         http_send_too_busy_reply(fd);
2effd274
         goto fail;
0bdd8b85
     }
115329f1
 
2effd274
     /* add a new connection */
     c = av_mallocz(sizeof(HTTPContext));
     if (!c)
         goto fail;
115329f1
 
2effd274
     c->fd = fd;
     c->poll_entry = NULL;
     c->from_addr = from_addr;
     c->buffer_size = IOBUFFER_INIT_SIZE;
     c->buffer = av_malloc(c->buffer_size);
     if (!c->buffer)
         goto fail;
8bc80f8b
 
     c->next = first_http_ctx;
     first_http_ctx = c;
2effd274
     nb_connections++;
115329f1
 
2effd274
     start_wait_request(c, is_rtsp);
 
     return;
 
  fail:
     if (c) {
e97f38ce
         av_freep(&c->buffer);
2effd274
         av_free(c);
     }
d96633bb
     closesocket(fd);
2effd274
 }
 
 static void close_connection(HTTPContext *c)
 {
     HTTPContext **cp, *c1;
     int i, nb_streams;
     AVFormatContext *ctx;
     AVStream *st;
 
     /* remove connection from list */
     cp = &first_http_ctx;
81a663f4
     while (*cp) {
2effd274
         c1 = *cp;
611c5741
         if (c1 == c)
2effd274
             *cp = c->next;
611c5741
         else
2effd274
             cp = &c1->next;
     }
 
bc351386
     /* remove references, if any (XXX: do it faster) */
81a663f4
     for(c1 = first_http_ctx; c1; c1 = c1->next) {
bc351386
         if (c1->rtsp_c == c)
             c1->rtsp_c = NULL;
     }
 
2effd274
     /* remove connection associated resources */
     if (c->fd >= 0)
d96633bb
         closesocket(c->fd);
2effd274
     if (c->fmt_in) {
         /* close each frame parser */
         for(i=0;i<c->fmt_in->nb_streams;i++) {
             st = c->fmt_in->streams[i];
611c5741
             if (st->codec->codec)
01f4895c
                 avcodec_close(st->codec);
2effd274
         }
cd3716b9
         avformat_close_input(&c->fmt_in);
2effd274
     }
 
     /* free RTP output streams if any */
     nb_streams = 0;
115329f1
     if (c->stream)
2effd274
         nb_streams = c->stream->nb_streams;
115329f1
 
2effd274
     for(i=0;i<nb_streams;i++) {
         ctx = c->rtp_ctx[i];
         if (ctx) {
             av_write_trailer(ctx);
d2d67e42
             av_dict_free(&ctx->metadata);
e97f38ce
             av_freep(&ctx->streams[0]);
             av_freep(&ctx);
2effd274
         }
89234dea
         ffurl_close(c->rtp_handles[i]);
2effd274
     }
115329f1
 
b88ba823
     ctx = &c->fmt_ctx;
 
637b638e
     if (!c->last_packet_sent && c->state == HTTPSTATE_SEND_DATA_TRAILER) {
0121ddab
         /* prepare header */
         if (ctx->oformat && avio_open_dyn_buf(&ctx->pb) >= 0) {
             av_write_trailer(ctx);
             av_freep(&c->pb_buffer);
             avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
87638494
         }
     }
 
115329f1
     for(i=0; i<ctx->nb_streams; i++)
e97f38ce
         av_freep(&ctx->streams[i]);
0e482a8e
     av_freep(&ctx->streams);
     av_freep(&ctx->priv_data);
f0ef6240
 
edfdd798
     if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
6edd6884
         current_bandwidth -= c->stream->bandwidth;
5400e092
 
     /* signal that there is no feed if we are the feeder socket */
     if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
         c->stream->feed_opened = 0;
         close(c->feed_fd);
     }
 
2effd274
     av_freep(&c->pb_buffer);
bc351386
     av_freep(&c->packet_buffer);
e97f38ce
     av_freep(&c->buffer);
2effd274
     av_free(c);
     nb_connections--;
 }
 
 static int handle_connection(HTTPContext *c)
 {
     int len, ret;
eadd66a4
     uint8_t *ptr;
115329f1
 
85f07f22
     switch(c->state) {
     case HTTPSTATE_WAIT_REQUEST:
2effd274
     case RTSPSTATE_WAIT_REQUEST:
85f07f22
         /* timeout ? */
         if ((c->timeout - cur_time) < 0)
             return -1;
         if (c->poll_entry->revents & (POLLERR | POLLHUP))
             return -1;
 
         /* no need to read if no events */
         if (!(c->poll_entry->revents & POLLIN))
             return 0;
         /* read the data */
1bc1cfdd
     read_loop:
eadd66a4
         if (!(len = recv(c->fd, c->buffer_ptr, 1, 0)))
             return -1;
 
85f07f22
         if (len < 0) {
28c4741a
             if (ff_neterrno() != AVERROR(EAGAIN) &&
                 ff_neterrno() != AVERROR(EINTR))
85f07f22
                 return -1;
eadd66a4
             break;
         }
197acee7
         /* search for end of request. */
         c->buffer_ptr += len;
         ptr = c->buffer_ptr;
         if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
             (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
             /* request found : parse it and reply */
469c335c
             if (c->state == HTTPSTATE_WAIT_REQUEST)
197acee7
                 ret = http_parse_request(c);
469c335c
             else
197acee7
                 ret = rtsp_parse_request(c);
469c335c
 
197acee7
             if (ret < 0)
85f07f22
                 return -1;
197acee7
         } else if (ptr >= c->buffer_end) {
             /* request too long: cannot do anything */
             return -1;
         } else goto read_loop;
eadd66a4
 
85f07f22
         break;
 
     case HTTPSTATE_SEND_HEADER:
         if (c->poll_entry->revents & (POLLERR | POLLHUP))
             return -1;
 
2effd274
         /* no need to write if no events */
85f07f22
         if (!(c->poll_entry->revents & POLLOUT))
             return 0;
c60202df
         len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
85f07f22
         if (len < 0) {
28c4741a
             if (ff_neterrno() != AVERROR(EAGAIN) &&
                 ff_neterrno() != AVERROR(EINTR)) {
ba6186d6
                 goto close_connection;
85f07f22
             }
eadd66a4
             break;
         }
197acee7
         c->buffer_ptr += len;
         if (c->stream)
             c->stream->bytes_served += len;
         c->data_count += len;
         if (c->buffer_ptr >= c->buffer_end) {
             av_freep(&c->pb_buffer);
             /* if error, exit */
             if (c->http_error)
                 return -1;
             /* all the buffer was sent : synchronize to the incoming
              * stream */
             c->state = HTTPSTATE_SEND_DATA_HEADER;
             c->buffer_ptr = c->buffer_end = c->buffer;
         }
85f07f22
         break;
 
     case HTTPSTATE_SEND_DATA:
     case HTTPSTATE_SEND_DATA_HEADER:
     case HTTPSTATE_SEND_DATA_TRAILER:
2effd274
         /* for packetized output, we consider we can always write (the
469c335c
          * input streams set the speed). It may be better to verify
          * that we do not rely too much on the kernel queues */
2effd274
         if (!c->is_packetized) {
             if (c->poll_entry->revents & (POLLERR | POLLHUP))
                 return -1;
115329f1
 
2effd274
             /* no need to read if no events */
             if (!(c->poll_entry->revents & POLLOUT))
                 return 0;
         }
5eb765ef
         if (http_send_data(c) < 0)
85f07f22
             return -1;
638831aa
         /* close connection if trailer sent */
         if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
             return -1;
3b89a673
         /* Check if it is a single jpeg frame 123 */
         if (c->stream->single_frame && c->data_count > c->cur_frame_bytes && c->cur_frame_bytes > 0) {
             close_connection(c);
         }
85f07f22
         break;
     case HTTPSTATE_RECEIVE_DATA:
         /* no need to read if no events */
         if (c->poll_entry->revents & (POLLERR | POLLHUP))
             return -1;
         if (!(c->poll_entry->revents & POLLIN))
             return 0;
         if (http_receive_data(c) < 0)
             return -1;
         break;
     case HTTPSTATE_WAIT_FEED:
         /* no need to read if no events */
a6e14edd
         if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
85f07f22
             return -1;
 
         /* nothing to do, we'll be waken up by incoming feed packets */
         break;
2effd274
 
     case RTSPSTATE_SEND_REPLY:
ba6186d6
         if (c->poll_entry->revents & (POLLERR | POLLHUP))
             goto close_connection;
2effd274
         /* no need to write if no events */
         if (!(c->poll_entry->revents & POLLOUT))
             return 0;
c60202df
         len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2effd274
         if (len < 0) {
28c4741a
             if (ff_neterrno() != AVERROR(EAGAIN) &&
                 ff_neterrno() != AVERROR(EINTR)) {
ba6186d6
                 goto close_connection;
2effd274
             }
eadd66a4
             break;
         }
197acee7
         c->buffer_ptr += len;
         c->data_count += len;
         if (c->buffer_ptr >= c->buffer_end) {
             /* all the buffer was sent : wait for a new request */
             av_freep(&c->pb_buffer);
             start_wait_request(c, 1);
         }
2effd274
         break;
bc351386
     case RTSPSTATE_SEND_PACKET:
         if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
             av_freep(&c->packet_buffer);
             return -1;
         }
         /* no need to write if no events */
         if (!(c->poll_entry->revents & POLLOUT))
             return 0;
c60202df
         len = send(c->fd, c->packet_buffer_ptr,
                     c->packet_buffer_end - c->packet_buffer_ptr, 0);
bc351386
         if (len < 0) {
28c4741a
             if (ff_neterrno() != AVERROR(EAGAIN) &&
                 ff_neterrno() != AVERROR(EINTR)) {
bc351386
                 /* error : close connection */
                 av_freep(&c->packet_buffer);
                 return -1;
             }
eadd66a4
             break;
         }
197acee7
         c->packet_buffer_ptr += len;
         if (c->packet_buffer_ptr >= c->packet_buffer_end) {
             /* all the buffer was sent : wait for a new request */
             av_freep(&c->packet_buffer);
             c->state = RTSPSTATE_WAIT_REQUEST;
         }
bc351386
         break;
2effd274
     case HTTPSTATE_READY:
         /* nothing to do */
         break;
85f07f22
     default:
         return -1;
     }
     return 0;
ba6186d6
 
 close_connection:
     av_freep(&c->pb_buffer);
     return -1;
85f07f22
 }
 
3120d2a2
 static int extract_rates(char *rates, int ratelen, const char *request)
 {
     const char *p;
 
     for (p = request; *p && *p != '\r' && *p != '\n'; ) {
bb3244de
         if (av_strncasecmp(p, "Pragma:", 7) == 0) {
3120d2a2
             const char *q = p + 7;
 
88d55b82
             while (*q && *q != '\n' && av_isspace(*q))
3120d2a2
                 q++;
 
bb3244de
             if (av_strncasecmp(q, "stream-switch-entry=", 20) == 0) {
3120d2a2
                 int stream_no;
                 int rate_no;
 
                 q += 20;
 
cde25790
                 memset(rates, 0xff, ratelen);
3120d2a2
 
                 while (1) {
                     while (*q && *q != '\n' && *q != ':')
                         q++;
 
611c5741
                     if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
3120d2a2
                         break;
611c5741
 
3120d2a2
                     stream_no--;
611c5741
                     if (stream_no < ratelen && stream_no >= 0)
3120d2a2
                         rates[stream_no] = rate_no;
 
88d55b82
                     while (*q && *q != '\n' && !av_isspace(*q))
3120d2a2
                         q++;
                 }
 
                 return 1;
             }
         }
         p = strchr(p, '\n');
         if (!p)
             break;
 
         p++;
     }
 
     return 0;
 }
 
50c1cac4
 static int find_stream_in_feed(FFServerStream *feed, AVCodecContext *codec,
                                int bit_rate)
3120d2a2
 {
     int i;
cde25790
     int best_bitrate = 100000000;
     int best = -1;
 
     for (i = 0; i < feed->nb_streams; i++) {
01f4895c
         AVCodecContext *feed_codec = feed->streams[i]->codec;
cde25790
 
         if (feed_codec->codec_id != codec->codec_id ||
             feed_codec->sample_rate != codec->sample_rate ||
             feed_codec->width != codec->width ||
611c5741
             feed_codec->height != codec->height)
cde25790
             continue;
 
         /* Potential stream */
 
115329f1
         /* We want the fastest stream less than bit_rate, or the slowest
cde25790
          * faster than bit_rate
          */
 
         if (feed_codec->bit_rate <= bit_rate) {
50c1cac4
             if (best_bitrate > bit_rate ||
                 feed_codec->bit_rate > best_bitrate) {
cde25790
                 best_bitrate = feed_codec->bit_rate;
                 best = i;
             }
2699a378
             continue;
         }
         if (feed_codec->bit_rate < best_bitrate) {
             best_bitrate = feed_codec->bit_rate;
             best = i;
cde25790
         }
     }
     return best;
 }
 
 static int modify_current_stream(HTTPContext *c, char *rates)
 {
     int i;
f524d2e4
     FFServerStream *req = c->stream;
cde25790
     int action_required = 0;
3120d2a2
 
001bcd29
     /* Not much we can do for a feed */
     if (!req->feed)
         return 0;
 
3120d2a2
     for (i = 0; i < req->nb_streams; i++) {
01f4895c
         AVCodecContext *codec = req->streams[i]->codec;
3120d2a2
 
         switch(rates[i]) {
             case 0:
cde25790
                 c->switch_feed_streams[i] = req->feed_streams[i];
3120d2a2
                 break;
             case 1:
cde25790
                 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
3120d2a2
                 break;
             case 2:
cde25790
                 /* Wants off or slow */
                 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
 #ifdef WANTS_OFF
                 /* This doesn't work well when it turns off the only stream! */
                 c->switch_feed_streams[i] = -2;
                 c->feed_streams[i] = -2;
 #endif
3120d2a2
                 break;
         }
 
d8a04d91
         if (c->switch_feed_streams[i] >= 0 &&
             c->switch_feed_streams[i] != c->feed_streams[i]) {
cde25790
             action_required = 1;
d8a04d91
         }
cde25790
     }
3120d2a2
 
cde25790
     return action_required;
 }
3120d2a2
 
2effd274
 static void get_word(char *buf, int buf_size, const char **pp)
 {
     const char *p;
     char *q;
 
     p = *pp;
2638af2f
     p += strspn(p, SPACE_CHARS);
2effd274
     q = buf;
88d55b82
     while (!av_isspace(*p) && *p != '\0') {
2effd274
         if ((q - buf) < buf_size - 1)
             *q++ = *p;
         p++;
     }
     if (buf_size > 0)
         *q = '\0';
     *pp = p;
 }
 
50c1cac4
 static FFServerIPAddressACL* parse_dynamic_acl(FFServerStream *stream,
                                                HTTPContext *c)
58f48adb
 {
     FILE* f;
     char line[1024];
     char  cmd[1024];
f524d2e4
     FFServerIPAddressACL *acl = NULL;
58f48adb
     int line_num = 0;
     const char *p;
 
     f = fopen(stream->dynamic_acl, "r");
     if (!f) {
         perror(stream->dynamic_acl);
         return NULL;
     }
 
f524d2e4
     acl = av_mallocz(sizeof(FFServerIPAddressACL));
d6039063
     if (!acl) {
         fclose(f);
         return NULL;
     }
58f48adb
 
     /* Build ACL */
a18456a2
     while (fgets(line, sizeof(line), f)) {
58f48adb
         line_num++;
         p = line;
88d55b82
         while (av_isspace(*p))
58f48adb
             p++;
         if (*p == '\0' || *p == '#')
             continue;
f524d2e4
         ffserver_get_arg(cmd, sizeof(cmd), &p);
58f48adb
 
bb3244de
         if (!av_strcasecmp(cmd, "ACL"))
50c1cac4
             ffserver_parse_acl_row(NULL, NULL, acl, p, stream->dynamic_acl,
                                    line_num);
58f48adb
     }
     fclose(f);
     return acl;
 }
 
 
f524d2e4
 static void free_acl_list(FFServerIPAddressACL *in_acl)
58f48adb
 {
f524d2e4
     FFServerIPAddressACL *pacl, *pacl2;
58f48adb
 
     pacl = in_acl;
     while(pacl) {
         pacl2 = pacl;
         pacl = pacl->next;
         av_freep(pacl2);
     }
 }
 
f524d2e4
 static int validate_acl_list(FFServerIPAddressACL *in_acl, HTTPContext *c)
8256c0a3
 {
f524d2e4
     enum FFServerIPAddressAction last_action = IP_DENY;
     FFServerIPAddressACL *acl;
8256c0a3
     struct in_addr *src = &c->from_addr.sin_addr;
2bd8416e
     unsigned long src_addr = src->s_addr;
8256c0a3
 
58f48adb
     for (acl = in_acl; acl; acl = acl->next) {
611c5741
         if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
8256c0a3
             return (acl->action == IP_ALLOW) ? 1 : 0;
         last_action = acl->action;
     }
 
     /* Nothing matched, so return not the last action */
     return (last_action == IP_DENY) ? 1 : 0;
 }
 
f524d2e4
 static int validate_acl(FFServerStream *stream, HTTPContext *c)
58f48adb
 {
     int ret = 0;
f524d2e4
     FFServerIPAddressACL *acl;
58f48adb
 
     /* if stream->acl is null validate_acl_list will return 1 */
     ret = validate_acl_list(stream->acl, c);
 
     if (stream->dynamic_acl[0]) {
         acl = parse_dynamic_acl(stream, c);
         ret = validate_acl_list(acl, c);
         free_acl_list(acl);
     }
 
     return ret;
 }
 
469c335c
 /**
  * compute the real filename of a file by matching it without its
  * extensions to all the stream's filenames
  */
829ac53d
 static void compute_real_filename(char *filename, int max_size)
 {
     char file1[1024];
     char file2[1024];
     char *p;
f524d2e4
     FFServerStream *stream;
829ac53d
 
f7d78f36
     av_strlcpy(file1, filename, sizeof(file1));
829ac53d
     p = strrchr(file1, '.');
     if (p)
         *p = '\0';
f524d2e4
     for(stream = config.first_stream; stream; stream = stream->next) {
f7d78f36
         av_strlcpy(file2, stream->filename, sizeof(file2));
829ac53d
         p = strrchr(file2, '.');
         if (p)
             *p = '\0';
         if (!strcmp(file1, file2)) {
f7d78f36
             av_strlcpy(filename, stream->filename, max_size);
829ac53d
             break;
         }
     }
 }
 
 enum RedirType {
     REDIR_NONE,
     REDIR_ASX,
     REDIR_RAM,
     REDIR_ASF,
     REDIR_RTSP,
     REDIR_SDP,
 };
 
47ba4728
 /* parse HTTP request and prepare header */
85f07f22
 static int http_parse_request(HTTPContext *c)
 {
39c4afd9
     const char *p;
     char *p1;
829ac53d
     enum RedirType redir_type;
85f07f22
     char cmd[32];
bae79c04
     char info[1024], filename[1024];
85f07f22
     char url[1024], *q;
     char protocol[32];
     char msg[1024];
daaa5358
     char *encoded_msg = NULL;
85f07f22
     const char *mime_type;
f524d2e4
     FFServerStream *stream;
42a63c6a
     int i;
3120d2a2
     char ratebuf[32];
39c4afd9
     const char *useragent = 0;
85f07f22
 
     p = c->buffer;
39c4afd9
     get_word(cmd, sizeof(cmd), &p);
f7d78f36
     av_strlcpy(c->method, cmd, sizeof(c->method));
7434ba6d
 
85f07f22
     if (!strcmp(cmd, "GET"))
edfdd798
         c->post = 0;
85f07f22
     else if (!strcmp(cmd, "POST"))
edfdd798
         c->post = 1;
85f07f22
     else
         return -1;
 
39c4afd9
     get_word(url, sizeof(url), &p);
f7d78f36
     av_strlcpy(c->url, url, sizeof(c->url));
7434ba6d
 
2effd274
     get_word(protocol, sizeof(protocol), (const char **)&p);
85f07f22
     if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
         return -1;
7434ba6d
 
f7d78f36
     av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
90f9c440
 
f524d2e4
     if (config.debug)
50c1cac4
         http_log("%s - - New connection: %s %s\n",
                  inet_ntoa(c->from_addr.sin_addr), cmd, url);
115329f1
 
85f07f22
     /* find the filename and the optional info string in the request */
39c4afd9
     p1 = strchr(url, '?');
     if (p1) {
         av_strlcpy(info, p1, sizeof(info));
         *p1 = '\0';
611c5741
     } else
85f07f22
         info[0] = '\0';
 
f7d78f36
     av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
bae79c04
 
cde25790
     for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
bb3244de
         if (av_strncasecmp(p, "User-Agent:", 11) == 0) {
cde25790
             useragent = p + 11;
88d55b82
             if (*useragent && *useragent != '\n' && av_isspace(*useragent))
cde25790
                 useragent++;
             break;
         }
         p = strchr(p, '\n');
         if (!p)
             break;
 
         p++;
     }
 
829ac53d
     redir_type = REDIR_NONE;
aa13b573
     if (av_match_ext(filename, "asx")) {
829ac53d
         redir_type = REDIR_ASX;
7434ba6d
         filename[strlen(filename)-1] = 'f';
aa13b573
     } else if (av_match_ext(filename, "asf") &&
33d6f90e
         (!useragent || av_strncasecmp(useragent, "NSPlayer", 8))) {
cde25790
         /* if this isn't WMP or lookalike, return the redirector file */
829ac53d
         redir_type = REDIR_ASF;
aa13b573
     } else if (av_match_ext(filename, "rpm,ram")) {
829ac53d
         redir_type = REDIR_RAM;
42a63c6a
         strcpy(filename + strlen(filename)-2, "m");
aa13b573
     } else if (av_match_ext(filename, "rtsp")) {
829ac53d
         redir_type = REDIR_RTSP;
bae79c04
         compute_real_filename(filename, sizeof(filename) - 1);
aa13b573
     } else if (av_match_ext(filename, "sdp")) {
829ac53d
         redir_type = REDIR_SDP;
bae79c04
         compute_real_filename(filename, sizeof(filename) - 1);
42a63c6a
     }
115329f1
 
469c335c
     /* "redirect" request to index.html */
bae79c04
     if (!strlen(filename))
f7d78f36
         av_strlcpy(filename, "index.html", sizeof(filename) - 1);
bae79c04
 
f524d2e4
     stream = config.first_stream;
81a663f4
     while (stream) {
8256c0a3
         if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
85f07f22
             break;
         stream = stream->next;
     }
fb33bff9
     if (!stream) {
d445a7e9
         snprintf(msg, sizeof(msg), "File '%s' not found", url);
77553ae3
         http_log("File '%s' not found\n", url);
85f07f22
         goto send_error;
     }
42a63c6a
 
cde25790
     c->stream = stream;
     memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
     memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
 
     if (stream->stream_type == STREAM_TYPE_REDIRECT) {
         c->http_error = 301;
         q = c->buffer;
1fc3e8f4
         snprintf(q, c->buffer_size,
a3aa4fed
                       "HTTP/1.0 301 Moved\r\n"
                       "Location: %s\r\n"
                       "Content-type: text/html\r\n"
                       "\r\n"
b25ac3c9
                       "<!DOCTYPE html>\n"
a3aa4fed
                       "<html><head><title>Moved</title></head><body>\r\n"
                       "You should be <a href=\"%s\">redirected</a>.\r\n"
50c1cac4
                       "</body></html>\r\n",
                  stream->feed_filename, stream->feed_filename);
1fc3e8f4
         q += strlen(q);
cde25790
         /* prepare output buffer */
         c->buffer_ptr = c->buffer;
         c->buffer_end = q;
         c->state = HTTPSTATE_SEND_HEADER;
         return 0;
     }
 
3120d2a2
     /* If this is WMP, get the rate information */
     if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
cde25790
         if (modify_current_stream(c, ratebuf)) {
37d3e066
             for (i = 0; i < FF_ARRAY_ELEMS(c->feed_streams); i++) {
cde25790
                 if (c->switch_feed_streams[i] >= 0)
305ca590
                     c->switch_feed_streams[i] = -1;
cde25790
             }
         }
3120d2a2
     }
 
d8f28a77
     if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
         current_bandwidth += stream->bandwidth;
 
314bc20d
     /* If already streaming this feed, do not let another feeder start */
d0a5513b
     if (stream->feed_opened) {
         snprintf(msg, sizeof(msg), "This feed is already being received.");
77553ae3
         http_log("Feed '%s' already being received\n", stream->feed_filename);
d0a5513b
         goto send_error;
     }
 
f524d2e4
     if (c->post == 0 && config.max_bandwidth < current_bandwidth) {
0aee2a57
         c->http_error = 503;
42a63c6a
         q = c->buffer;
1fc3e8f4
         snprintf(q, c->buffer_size,
0aee2a57
                       "HTTP/1.0 503 Server too busy\r\n"
a3aa4fed
                       "Content-type: text/html\r\n"
                       "\r\n"
b25ac3c9
                       "<!DOCTYPE html>\n"
a3aa4fed
                       "<html><head><title>Too busy</title></head><body>\r\n"
d8c714cb
                       "<p>The server is too busy to serve your request at "
                       "this time.</p>\r\n"
                       "<p>The bandwidth being served (including your stream) "
84b4998d
                       "is %"PRIu64"kbit/s, and this exceeds the limit of "
                       "%"PRIu64"kbit/s.</p>\r\n"
50c1cac4
                       "</body></html>\r\n",
                  current_bandwidth, config.max_bandwidth);
1fc3e8f4
         q += strlen(q);
42a63c6a
         /* prepare output buffer */
         c->buffer_ptr = c->buffer;
         c->buffer_end = q;
         c->state = HTTPSTATE_SEND_HEADER;
         return 0;
     }
115329f1
 
829ac53d
     if (redir_type != REDIR_NONE) {
39c4afd9
         const char *hostinfo = 0;
115329f1
 
7434ba6d
         for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
bb3244de
             if (av_strncasecmp(p, "Host:", 5) == 0) {
7434ba6d
                 hostinfo = p + 5;
                 break;
             }
             p = strchr(p, '\n');
             if (!p)
                 break;
 
             p++;
         }
 
         if (hostinfo) {
             char *eoh;
             char hostbuf[260];
 
88d55b82
             while (av_isspace(*hostinfo))
7434ba6d
                 hostinfo++;
 
             eoh = strchr(hostinfo, '\n');
             if (eoh) {
                 if (eoh[-1] == '\r')
                     eoh--;
 
                 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
                     memcpy(hostbuf, hostinfo, eoh - hostinfo);
                     hostbuf[eoh - hostinfo] = 0;
 
                     c->http_error = 200;
                     q = c->buffer;
829ac53d
                     switch(redir_type) {
                     case REDIR_ASX:
1fc3e8f4
                         snprintf(q, c->buffer_size,
a3aa4fed
                                       "HTTP/1.0 200 ASX Follows\r\n"
                                       "Content-type: video/x-ms-asf\r\n"
                                       "\r\n"
                                       "<ASX Version=\"3\">\r\n"
                                       //"<!-- Autogenerated by ffserver -->\r\n"
                                       "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n"
                                       "</ASX>\r\n", hostbuf, filename, info);
1fc3e8f4
                         q += strlen(q);
829ac53d
                         break;
                     case REDIR_RAM:
1fc3e8f4
                         snprintf(q, c->buffer_size,
a3aa4fed
                                       "HTTP/1.0 200 RAM Follows\r\n"
                                       "Content-type: audio/x-pn-realaudio\r\n"
                                       "\r\n"
                                       "# Autogenerated by ffserver\r\n"
                                       "http://%s/%s%s\r\n", hostbuf, filename, info);
1fc3e8f4
                         q += strlen(q);
829ac53d
                         break;
                     case REDIR_ASF:
1fc3e8f4
                         snprintf(q, c->buffer_size,
a3aa4fed
                                       "HTTP/1.0 200 ASF Redirect follows\r\n"
                                       "Content-type: video/x-ms-asf\r\n"
                                       "\r\n"
                                       "[Reference]\r\n"
                                       "Ref1=http://%s/%s%s\r\n", hostbuf, filename, info);
1fc3e8f4
                         q += strlen(q);
829ac53d
                         break;
                     case REDIR_RTSP:
                         {
                             char hostname[256], *p;
                             /* extract only hostname */
f7d78f36
                             av_strlcpy(hostname, hostbuf, sizeof(hostname));
829ac53d
                             p = strrchr(hostname, ':');
                             if (p)
                                 *p = '\0';
1fc3e8f4
                             snprintf(q, c->buffer_size,
a3aa4fed
                                           "HTTP/1.0 200 RTSP Redirect follows\r\n"
0c5a6ef8
                                           /* XXX: incorrect MIME type ? */
a3aa4fed
                                           "Content-type: application/x-rtsp\r\n"
                                           "\r\n"
f524d2e4
                                           "rtsp://%s:%d/%s\r\n", hostname, ntohs(config.rtsp_addr.sin_port), filename);
1fc3e8f4
                             q += strlen(q);
829ac53d
                         }
                         break;
                     case REDIR_SDP:
                         {
0c1a9eda
                             uint8_t *sdp_data;
cc64ec57
                             int sdp_data_size;
                             socklen_t len;
829ac53d
                             struct sockaddr_in my_addr;
 
1fc3e8f4
                             snprintf(q, c->buffer_size,
a3aa4fed
                                           "HTTP/1.0 200 OK\r\n"
                                           "Content-type: application/sdp\r\n"
                                           "\r\n");
1fc3e8f4
                             q += strlen(q);
829ac53d
 
                             len = sizeof(my_addr);
898192e0
 
                             /* XXX: Should probably fail? */
                             if (getsockname(c->fd, (struct sockaddr *)&my_addr, &len))
                                 http_log("getsockname() failed\n");
115329f1
 
829ac53d
                             /* XXX: should use a dynamic buffer */
115329f1
                             sdp_data_size = prepare_sdp_description(stream,
                                                                     &sdp_data,
829ac53d
                                                                     my_addr.sin_addr);
                             if (sdp_data_size > 0) {
                                 memcpy(q, sdp_data, sdp_data_size);
                                 q += sdp_data_size;
                                 *q = '\0';
                                 av_free(sdp_data);
                             }
                         }
                         break;
                     default:
0f4e8165
                         abort();
829ac53d
                         break;
2effd274
                     }
7434ba6d
 
                     /* prepare output buffer */
                     c->buffer_ptr = c->buffer;
                     c->buffer_end = q;
                     c->state = HTTPSTATE_SEND_HEADER;
                     return 0;
                 }
             }
         }
 
d445a7e9
         snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
7434ba6d
         goto send_error;
85f07f22
     }
 
a6e14edd
     stream->conns_served++;
7434ba6d
 
85f07f22
     /* XXX: add there authenticate and IP match */
 
edfdd798
     if (c->post) {
85f07f22
         /* if post, it means a feed is being sent */
         if (!stream->is_feed) {
e16190fa
             /* However it might be a status report from WMP! Let us log the
0c5a6ef8
              * data as it might come handy one day. */
39c4afd9
             const char *logline = 0;
3120d2a2
             int client_id = 0;
115329f1
 
7434ba6d
             for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
bb3244de
                 if (av_strncasecmp(p, "Pragma: log-line=", 17) == 0) {
7434ba6d
                     logline = p;
                     break;
                 }
bb3244de
                 if (av_strncasecmp(p, "Pragma: client-id=", 18) == 0)
3120d2a2
                     client_id = strtol(p + 18, 0, 10);
7434ba6d
                 p = strchr(p, '\n');
                 if (!p)
                     break;
 
                 p++;
             }
 
             if (logline) {
                 char *eol = strchr(logline, '\n');
 
                 logline += 17;
 
                 if (eol) {
                     if (eol[-1] == '\r')
                         eol--;
7906085f
                     http_log("%.*s\n", (int) (eol - logline), logline);
7434ba6d
                     c->suppress_log = 1;
                 }
             }
3120d2a2
 
f190f676
 #ifdef DEBUG
cde25790
             http_log("\nGot request:\n%s\n", c->buffer);
3120d2a2
 #endif
 
             if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
                 HTTPContext *wmpc;
 
                 /* Now we have to find the client_id */
                 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
                     if (wmpc->wmp_client_id == client_id)
                         break;
                 }
 
2d563d2f
                 if (wmpc && modify_current_stream(wmpc, ratebuf))
                     wmpc->switch_pending = 1;
3120d2a2
             }
115329f1
 
d445a7e9
             snprintf(msg, sizeof(msg), "POST command not handled");
cb275dd9
             c->stream = 0;
85f07f22
             goto send_error;
         }
         if (http_start_receive_data(c) < 0) {
d445a7e9
             snprintf(msg, sizeof(msg), "could not open feed");
85f07f22
             goto send_error;
         }
         c->http_error = 0;
         c->state = HTTPSTATE_RECEIVE_DATA;
         return 0;
     }
 
f190f676
 #ifdef DEBUG
611c5741
     if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
cde25790
         http_log("\nGot request:\n%s\n", c->buffer);
3120d2a2
 #endif
 
85f07f22
     if (c->stream->stream_type == STREAM_TYPE_STATUS)
dca21085
         goto send_status;
85f07f22
 
     /* open input stream */
     if (open_input_stream(c, info) < 0) {
d445a7e9
         snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
85f07f22
         goto send_error;
     }
 
47ba4728
     /* prepare HTTP header */
1fc3e8f4
     c->buffer[0] = 0;
     av_strlcatf(c->buffer, c->buffer_size, "HTTP/1.0 200 OK\r\n");
85f07f22
     mime_type = c->stream->fmt->mime_type;
     if (!mime_type)
087fa475
         mime_type = "application/x-octet-stream";
1fc3e8f4
     av_strlcatf(c->buffer, c->buffer_size, "Pragma: no-cache\r\n");
85f07f22
 
     /* for asf, we need extra headers */
8256c0a3
     if (!strcmp(c->stream->fmt->name,"asf_stream")) {
3120d2a2
         /* Need to allocate a client id */
 
042819c5
         c->wmp_client_id = av_lfg_get(&random_state);
3120d2a2
 
1fc3e8f4
         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);
85f07f22
     }
1fc3e8f4
     av_strlcatf(c->buffer, c->buffer_size, "Content-Type: %s\r\n", mime_type);
     av_strlcatf(c->buffer, c->buffer_size, "\r\n");
     q = c->buffer + strlen(c->buffer);
115329f1
 
85f07f22
     /* prepare output buffer */
     c->http_error = 0;
     c->buffer_ptr = c->buffer;
     c->buffer_end = q;
     c->state = HTTPSTATE_SEND_HEADER;
     return 0;
  send_error:
     c->http_error = 404;
     q = c->buffer;
daaa5358
     if (!htmlencode(msg, &encoded_msg)) {
         http_log("Could not encode filename '%s' as HTML\n", msg);
     }
1fc3e8f4
     snprintf(q, c->buffer_size,
a3aa4fed
                   "HTTP/1.0 404 Not Found\r\n"
                   "Content-type: text/html\r\n"
                   "\r\n"
0e5c1dc9
                   "<!DOCTYPE html>\n"
5e567aae
                   "<html>\n"
0e5c1dc9
                   "<head>\n"
daaa5358
                   "<meta charset=\"UTF-8\">\n"
0e5c1dc9
                   "<title>404 Not Found</title>\n"
                   "</head>\n"
5e567aae
                   "<body>%s</body>\n"
daaa5358
                   "</html>\n", encoded_msg? encoded_msg : "File not found");
1fc3e8f4
     q += strlen(q);
85f07f22
     /* prepare output buffer */
     c->buffer_ptr = c->buffer;
     c->buffer_end = q;
     c->state = HTTPSTATE_SEND_HEADER;
daaa5358
     av_freep(&encoded_msg);
85f07f22
     return 0;
dca21085
  send_status:
     compute_status(c);
469c335c
     /* horrible: we use this value to avoid
      * going to the send data state */
     c->http_error = 200;
85f07f22
     c->state = HTTPSTATE_SEND_HEADER;
     return 0;
 }
 
ae628ec1
 static void fmt_bytecount(AVIOContext *pb, int64_t count)
2ac887ba
 {
b0f29db5
     static const char suffix[] = " kMGTP";
2ac887ba
     const char *s;
 
611c5741
     for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
2ac887ba
 
d9d86e00
     avio_printf(pb, "%"PRId64"%c", count, *s);
2ac887ba
 }
 
1714fe29
 static inline void print_stream_params(AVIOContext *pb, FFServerStream *stream)
 {
     int i, stream_no;
     const char *type = "unknown";
     char parameters[64];
     AVStream *st;
     AVCodec *codec;
 
     stream_no = stream->nb_streams;
 
     avio_printf(pb, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>"
84b4998d
                     "type<th>kbit/s<th align=left>codec<th align=left>"
1714fe29
                     "Parameters\n");
 
     for (i = 0; i < stream_no; i++) {
         st = stream->streams[i];
         codec = avcodec_find_encoder(st->codec->codec_id);
 
         parameters[0] = 0;
 
         switch(st->codec->codec_type) {
         case AVMEDIA_TYPE_AUDIO:
             type = "audio";
             snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz",
                      st->codec->channels, st->codec->sample_rate);
             break;
         case AVMEDIA_TYPE_VIDEO:
             type = "video";
             snprintf(parameters, sizeof(parameters),
                      "%dx%d, q=%d-%d, fps=%d", st->codec->width,
                      st->codec->height, st->codec->qmin, st->codec->qmax,
                      st->codec->time_base.den / st->codec->time_base.num);
             break;
         default:
             abort();
         }
 
7404f3bd
         avio_printf(pb, "<tr><td align=right>%d<td>%s<td align=right>%"PRId64
1714fe29
                         "<td>%s<td>%s\n",
7404f3bd
                     i, type, (int64_t)st->codec->bit_rate/1000,
1714fe29
                     codec ? codec->name : "", parameters);
      }
 
      avio_printf(pb, "</table>\n");
 }
 
dca21085
 static void compute_status(HTTPContext *c)
85f07f22
 {
     HTTPContext *c1;
f524d2e4
     FFServerStream *stream;
2effd274
     char *p;
85f07f22
     time_t ti;
2effd274
     int i, len;
ae628ec1
     AVIOContext *pb;
cde25790
 
b92c5452
     if (avio_open_dyn_buf(&pb) < 0) {
2effd274
         /* XXX: return an error ? */
cde25790
         c->buffer_ptr = c->buffer;
2effd274
         c->buffer_end = c->buffer;
         return;
cde25790
     }
85f07f22
 
d9d86e00
     avio_printf(pb, "HTTP/1.0 200 OK\r\n");
5df2a502
     avio_printf(pb, "Content-type: text/html\r\n");
d9d86e00
     avio_printf(pb, "Pragma: no-cache\r\n");
     avio_printf(pb, "\r\n");
115329f1
 
b25ac3c9
     avio_printf(pb, "<!DOCTYPE html>\n");
d9d86e00
     avio_printf(pb, "<html><head><title>%s Status</title>\n", program_name);
0679719d
     if (c->stream->feed_filename[0])
50c1cac4
         avio_printf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n",
                     c->stream->feed_filename);
d9d86e00
     avio_printf(pb, "</head>\n<body>");
     avio_printf(pb, "<h1>%s Status</h1>\n", program_name);
85f07f22
     /* format status */
d9d86e00
     avio_printf(pb, "<h2>Available Streams</h2>\n");
     avio_printf(pb, "<table cellspacing=0 cellpadding=4>\n");
84b4998d
     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");
f524d2e4
     stream = config.first_stream;
81a663f4
     while (stream) {
42a63c6a
         char sfilename[1024];
         char *eosf;
 
907101eb
         if (stream->feed == stream) {
             stream = stream->next;
             continue;
         }
9e55130a
 
         av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
         eosf = sfilename + strlen(sfilename);
         if (eosf - sfilename >= 4) {
             if (strcmp(eosf - 4, ".asf") == 0)
                 strcpy(eosf - 4, ".asx");
             else if (strcmp(eosf - 3, ".rm") == 0)
                 strcpy(eosf - 3, ".ram");
             else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
                 /* generate a sample RTSP director if
469c335c
                  * unicast. Generate an SDP redirector if
                  * multicast */
9e55130a
                 eosf = strrchr(sfilename, '.');
                 if (!eosf)
                     eosf = sfilename + strlen(sfilename);
                 if (stream->is_multicast)
                     strcpy(eosf, ".sdp");
                 else
                     strcpy(eosf, ".rtsp");
42a63c6a
             }
9e55130a
         }
115329f1
 
9e55130a
         avio_printf(pb, "<tr><td><a href=\"/%s\">%s</a> ",
                     sfilename, stream->filename);
         avio_printf(pb, "<td align=right> %d <td align=right> ",
                     stream->conns_served);
         fmt_bytecount(pb, stream->bytes_served);
 
         switch(stream->stream_type) {
         case STREAM_TYPE_LIVE: {
             int audio_bit_rate = 0;
             int video_bit_rate = 0;
             const char *audio_codec_name = "";
             const char *video_codec_name = "";
             const char *audio_codec_name_extra = "";
             const char *video_codec_name_extra = "";
 
             for(i=0;i<stream->nb_streams;i++) {
                 AVStream *st = stream->streams[i];
                 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
 
                 switch(st->codec->codec_type) {
                 case AVMEDIA_TYPE_AUDIO:
                     audio_bit_rate += st->codec->bit_rate;
                     if (codec) {
                         if (*audio_codec_name)
                             audio_codec_name_extra = "...";
                         audio_codec_name = codec->name;
85f07f22
                     }
9e55130a
                     break;
                 case AVMEDIA_TYPE_VIDEO:
                     video_bit_rate += st->codec->bit_rate;
                     if (codec) {
                         if (*video_codec_name)
                             video_codec_name_extra = "...";
                         video_codec_name = codec->name;
                     }
                     break;
                 case AVMEDIA_TYPE_DATA:
                     video_bit_rate += st->codec->bit_rate;
                     break;
                 default:
                     abort();
85f07f22
                 }
             }
9e55130a
 
             avio_printf(pb, "<td align=center> %s <td align=right> %d "
                             "<td align=right> %d <td> %s %s <td align=right> "
                             "%d <td> %s %s",
                         stream->fmt->name, stream->bandwidth,
                         video_bit_rate / 1000, video_codec_name,
                         video_codec_name_extra, audio_bit_rate / 1000,
                         audio_codec_name, audio_codec_name_extra);
 
             if (stream->feed)
                 avio_printf(pb, "<td>%s", stream->feed->filename);
             else
                 avio_printf(pb, "<td>%s", stream->feed_filename);
             avio_printf(pb, "\n");
         }
             break;
         default:
             avio_printf(pb, "<td align=center> - <td align=right> - "
                             "<td align=right> - <td><td align=right> - <td>\n");
             break;
         }
85f07f22
         stream = stream->next;
     }
d9d86e00
     avio_printf(pb, "</table>\n");
a6e14edd
 
f524d2e4
     stream = config.first_stream;
81a663f4
     while (stream) {
9e55130a
 
907101eb
         if (stream->feed != stream) {
             stream = stream->next;
             continue;
         }
9e55130a
 
         avio_printf(pb, "<h2>Feed %s</h2>", stream->filename);
         if (stream->pid) {
92b1a0fa
             avio_printf(pb, "Running as pid %"PRId64".\n", (int64_t) stream->pid);
cde25790
 
c2c17268
 #if defined(linux)
9e55130a
             {
                 FILE *pid_stat;
                 char ps_cmd[64];
 
                 /* This is somewhat linux specific I guess */
                 snprintf(ps_cmd, sizeof(ps_cmd),
92b1a0fa
                          "ps -o \"%%cpu,cputime\" --no-headers %"PRId64"",
                          (int64_t) stream->pid);
9e55130a
 
                  pid_stat = popen(ps_cmd, "r");
                  if (pid_stat) {
                      char cpuperc[10];
                      char cpuused[64];
 
                      if (fscanf(pid_stat, "%9s %63s", cpuperc, cpuused) == 2) {
                          avio_printf(pb, "Currently using %s%% of the cpu. "
                                          "Total time used %s.\n",
                                      cpuperc, cpuused);
                      }
                      fclose(pid_stat);
                  }
             }
cde25790
 #endif
 
9e55130a
             avio_printf(pb, "<p>");
         }
a6e14edd
 
1714fe29
         print_stream_params(pb, stream);
a6e14edd
         stream = stream->next;
     }
115329f1
 
85f07f22
     /* connection status */
d9d86e00
     avio_printf(pb, "<h2>Connection Status</h2>\n");
85f07f22
 
d9d86e00
     avio_printf(pb, "Number of connections: %d / %d<br>\n",
9e55130a
                 nb_connections, config.nb_max_connections);
85f07f22
 
d9d86e00
     avio_printf(pb, "Bandwidth in use: %"PRIu64"k / %"PRIu64"k<br>\n",
9e55130a
                 current_bandwidth, config.max_bandwidth);
42a63c6a
 
d9d86e00
     avio_printf(pb, "<table>\n");
9e55130a
     avio_printf(pb, "<tr><th>#<th>File<th>IP<th>Proto<th>State<th>Target "
84b4998d
                     "bit/s<th>Actual bit/s<th>Bytes transferred\n");
85f07f22
     c1 = first_http_ctx;
     i = 0;
81a663f4
     while (c1) {
cde25790
         int bitrate;
         int j;
 
         bitrate = 0;
2effd274
         if (c1->stream) {
             for (j = 0; j < c1->stream->nb_streams; j++) {
2d563d2f
                 if (!c1->stream->feed)
01f4895c
                     bitrate += c1->stream->streams[j]->codec->bit_rate;
2d563d2f
                 else if (c1->feed_streams[j] >= 0)
                     bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
cde25790
             }
         }
 
85f07f22
         i++;
         p = inet_ntoa(c1->from_addr.sin_addr);
9e55130a
         avio_printf(pb, "<tr><td><b>%d</b><td>%s%s<td>%s<td>%s<td>%s"
                         "<td align=right>",
                     i, c1->stream ? c1->stream->filename : "",
                     c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "", p,
                     c1->protocol, http_state[c1->state]);
2effd274
         fmt_bytecount(pb, bitrate);
d9d86e00
         avio_printf(pb, "<td align=right>");
2effd274
         fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
d9d86e00
         avio_printf(pb, "<td align=right>");
2effd274
         fmt_bytecount(pb, c1->data_count);
d9d86e00
         avio_printf(pb, "\n");
85f07f22
         c1 = c1->next;
     }
d9d86e00
     avio_printf(pb, "</table>\n");
115329f1
 
85f07f22
     /* date */
     ti = time(NULL);
     p = ctime(&ti);
d9d86e00
     avio_printf(pb, "<hr size=1 noshade>Generated at %s", p);
     avio_printf(pb, "</body>\n</html>\n");
85f07f22
 
6dc7d80d
     len = avio_close_dyn_buf(pb, &c->pb_buffer);
2effd274
     c->buffer_ptr = c->pb_buffer;
     c->buffer_end = c->pb_buffer + len;
85f07f22
 }
 
 static int open_input_stream(HTTPContext *c, const char *info)
 {
     char buf[128];
     char input_filename[1024];
50f2dfad
     AVFormatContext *s = NULL;
9fcc62ed
     int buf_size, i, ret;
0c1a9eda
     int64_t stream_pos;
85f07f22
 
     /* find file name */
     if (c->stream->feed) {
         strcpy(input_filename, c->stream->feed->feed_filename);
9fcc62ed
         buf_size = FFM_PACKET_SIZE;
85f07f22
         /* compute position (absolute time) */
ab0287fc
         if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
0e1e5d00
             if ((ret = av_parse_time(&stream_pos, buf, 0)) < 0) {
                 http_log("Invalid date specification '%s' for stream\n", buf);
9fcae973
                 return ret;
0e1e5d00
             }
ab0287fc
         } else if (av_find_info_tag(buf, sizeof(buf), "buffer", info)) {
f747e6d3
             int prebuffer = strtol(buf, 0, 10);
0c1a9eda
             stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
611c5741
         } else
0c1a9eda
             stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
85f07f22
     } else {
         strcpy(input_filename, c->stream->feed_filename);
9fcc62ed
         buf_size = 0;
85f07f22
         /* compute position (relative time) */
ab0287fc
         if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
0e1e5d00
             if ((ret = av_parse_time(&stream_pos, buf, 1)) < 0) {
                 http_log("Invalid date specification '%s' for stream\n", buf);
9fcae973
                 return ret;
0e1e5d00
             }
ace21da3
         } else
85f07f22
             stream_pos = 0;
     }
0e1e5d00
     if (!input_filename[0]) {
         http_log("No filename was specified for stream\n");
         return AVERROR(EINVAL);
     }
85f07f22
 
     /* open stream */
bd8e16f2
     ret = avformat_open_input(&s, input_filename, c->stream->ifmt,
                               &c->stream->in_opts);
     if (ret < 0) {
923a2445
         http_log("Could not open input '%s': %s\n",
                  input_filename, av_err2str(ret));
0e1e5d00
         return ret;
2effd274
     }
00969376
 
     /* set buffer size */
ddda9cee
     if (buf_size > 0) {
         ret = ffio_set_buf_size(s->pb, buf_size);
         if (ret < 0) {
             http_log("Failed to set buffer size\n");
             return ret;
         }
     }
00969376
 
9dc0bc3d
     s->flags |= AVFMT_FLAG_GENPTS;
85f07f22
     c->fmt_in = s;
0e1e5d00
     if (strcmp(s->iformat->name, "ffm") &&
         (ret = avformat_find_stream_info(c->fmt_in, NULL)) < 0) {
         http_log("Could not find stream info for input '%s'\n", input_filename);
cd3716b9
         avformat_close_input(&s);
0e1e5d00
         return ret;
20f93c3c
     }
115329f1
 
35e525b7
     /* choose stream as clock source (we favor the video stream if
      * present) for packet sending */
2effd274
     c->pts_stream_index = 0;
     for(i=0;i<c->stream->nb_streams;i++) {
115329f1
         if (c->pts_stream_index == 0 &&
72415b2a
             c->stream->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2effd274
             c->pts_stream_index = i;
         }
     }
85f07f22
 
611c5741
     if (c->fmt_in->iformat->read_seek)
60a04f7f
         av_seek_frame(c->fmt_in, -1, stream_pos, 0);
2effd274
     /* set the start time (needed for maxtime and RTP packet timing) */
     c->start_time = cur_time;
     c->first_pts = AV_NOPTS_VALUE;
     return 0;
 }
 
e240a0bb
 /* return the server clock (in us) */
 static int64_t get_server_clock(HTTPContext *c)
2effd274
 {
e240a0bb
     /* compute current pts value from system time */
c3f58185
     return (cur_time - c->start_time) * 1000;
2effd274
 }
 
469c335c
 /* return the estimated time (in us) at which the current packet must be sent */
e240a0bb
 static int64_t get_packet_send_clock(HTTPContext *c)
2effd274
 {
e240a0bb
     int bytes_left, bytes_sent, frame_bytes;
115329f1
 
e240a0bb
     frame_bytes = c->cur_frame_bytes;
611c5741
     if (frame_bytes <= 0)
e240a0bb
         return c->cur_pts;
683f5735
 
     bytes_left = c->buffer_end - c->buffer_ptr;
     bytes_sent = frame_bytes - bytes_left;
     return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
2effd274
 }
 
 
5eb765ef
 static int http_prepare_data(HTTPContext *c)
85f07f22
 {
2effd274
     int i, len, ret;
     AVFormatContext *ctx;
85f07f22
 
bc351386
     av_freep(&c->pb_buffer);
85f07f22
     switch(c->state) {
     case HTTPSTATE_SEND_DATA_HEADER:
c18cfd10
         ctx = avformat_alloc_context();
d6039063
         if (!ctx)
             return AVERROR(ENOMEM);
c18cfd10
         c->fmt_ctx = *ctx;
         av_freep(&ctx);
9985710a
         av_dict_copy(&(c->fmt_ctx.metadata), c->stream->metadata, 0);
923a2445
         c->fmt_ctx.streams = av_mallocz_array(c->stream->nb_streams,
                                               sizeof(AVStream *));
d6039063
         if (!c->fmt_ctx.streams)
             return AVERROR(ENOMEM);
db3262b7
 
3d9cc27d
         for(i=0;i<c->stream->nb_streams;i++) {
bb270c08
             AVStream *src;
db3262b7
             c->fmt_ctx.streams[i] = av_mallocz(sizeof(AVStream));
d6039063
 
469c335c
             /* if file or feed, then just take streams from FFServerStream
              * struct */
115329f1
             if (!c->stream->feed ||
2effd274
                 c->stream->feed == c->stream)
7c054ea7
                 src = c->stream->streams[i];
2effd274
             else
7c054ea7
                 src = c->stream->feed->streams[c->stream->feed_streams[i]];
 
db3262b7
             *(c->fmt_ctx.streams[i]) = *src;
             c->fmt_ctx.streams[i]->priv_data = 0;
35e525b7
             /* XXX: should be done in AVStream, not in codec */
             c->fmt_ctx.streams[i]->codec->frame_number = 0;
2effd274
         }
3d9cc27d
         /* set output format parameters */
         c->fmt_ctx.oformat = c->stream->fmt;
         c->fmt_ctx.nb_streams = c->stream->nb_streams;
 
2effd274
         c->got_key_frame = 0;
f747e6d3
 
2effd274
         /* prepare header and save header data in a stream */
b92c5452
         if (avio_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2effd274
             /* XXX: potential leak */
             return -1;
85f07f22
         }
8978feda
         c->fmt_ctx.pb->seekable = 0;
2effd274
 
8aae202e
         /*
0c5a6ef8
          * HACK to avoid MPEG-PS muxer to spit many underflow errors
8aae202e
          * Default value from FFmpeg
35e525b7
          * Try to set it using configuration option
8aae202e
          */
         c->fmt_ctx.max_delay = (int)(0.7*AV_TIME_BASE);
 
73b87304
         if ((ret = avformat_write_header(&c->fmt_ctx, NULL)) < 0) {
             http_log("Error writing output header for stream '%s': %s\n",
                      c->stream->filename, av_err2str(ret));
             return ret;
929a9b75
         }
d2d67e42
         av_dict_free(&c->fmt_ctx.metadata);
2effd274
 
6dc7d80d
         len = avio_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2effd274
         c->buffer_ptr = c->pb_buffer;
         c->buffer_end = c->pb_buffer + len;
 
85f07f22
         c->state = HTTPSTATE_SEND_DATA;
         c->last_packet_sent = 0;
         break;
     case HTTPSTATE_SEND_DATA:
         /* find a new packet */
3b371676
         /* read a packet from the input stream */
         if (c->stream->feed)
             ffm_set_write_index(c->fmt_in,
                                 c->stream->feed->feed_write_index,
                                 c->stream->feed->feed_size);
 
         if (c->stream->max_time &&
             c->stream->max_time + c->start_time - cur_time < 0)
             /* We have timed out */
             c->state = HTTPSTATE_SEND_DATA_TRAILER;
         else {
             AVPacket pkt;
         redo:
d9aac267
             ret = av_read_frame(c->fmt_in, &pkt);
             if (ret < 0) {
                 if (c->stream->feed) {
3b371676
                     /* if coming from feed, it means we reached the end of the
469c335c
                      * ffm file, so must wait for more data */
3b371676
                     c->state = HTTPSTATE_WAIT_FEED;
                     return 1; /* state changed */
6504047f
                 }
                 if (ret == AVERROR(EAGAIN)) {
d9aac267
                     /* input not ready, come back later */
                     return 0;
758c7a5c
                 }
                 if (c->stream->loop) {
                     avformat_close_input(&c->fmt_in);
                     if (open_input_stream(c, "") < 0)
                         goto no_loop;
                     goto redo;
2effd274
                 } else {
3b371676
                     no_loop:
35e525b7
                         /* must send trailer now because EOF or error */
3b371676
                         c->state = HTTPSTATE_SEND_DATA_TRAILER;
                 }
             } else {
084a8912
                 int source_index = pkt.stream_index;
3b371676
                 /* update first pts if needed */
64e220be
                 if (c->first_pts == AV_NOPTS_VALUE && pkt.dts != AV_NOPTS_VALUE) {
3b371676
                     c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
                     c->start_time = cur_time;
                 }
                 /* send it to the appropriate stream */
                 if (c->stream->feed) {
                     /* if coming from a feed, select the right stream */
                     if (c->switch_pending) {
                         c->switch_pending = 0;
cde25790
                         for(i=0;i<c->stream->nb_streams;i++) {
3b371676
                             if (c->switch_feed_streams[i] == pkt.stream_index)
cc947f04
                                 if (pkt.flags & AV_PKT_FLAG_KEY)
305ca590
                                     c->switch_feed_streams[i] = -1;
3b371676
                             if (c->switch_feed_streams[i] >= 0)
                                 c->switch_pending = 1;
cde25790
                         }
3b371676
                     }
                     for(i=0;i<c->stream->nb_streams;i++) {
a5ba4ced
                         if (c->stream->feed_streams[i] == pkt.stream_index) {
78728064
                             AVStream *st = c->fmt_in->streams[source_index];
3b371676
                             pkt.stream_index = i;
cc947f04
                             if (pkt.flags & AV_PKT_FLAG_KEY &&
72415b2a
                                 (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
180b7026
                                  c->stream->nb_streams == 1))
0332f549
                                 c->got_key_frame = 1;
                             if (!c->stream->send_on_key || c->got_key_frame)
3b371676
                                 goto send_it;
                         }
                     }
                 } else {
                     AVCodecContext *codec;
dc3a6a36
                     AVStream *ist, *ost;
                 send_it:
                     ist = c->fmt_in->streams[source_index];
3b371676
                     /* specific handling for RTP: we use several
35e525b7
                      * output streams (one for each RTP connection).
                      * XXX: need more abstract handling */
3b371676
                     if (c->is_packetized) {
                         /* compute send time and duration */
64e220be
                         if (pkt.dts != AV_NOPTS_VALUE) {
                             c->cur_pts = av_rescale_q(pkt.dts, ist->time_base, AV_TIME_BASE_Q);
                             c->cur_pts -= c->first_pts;
                         }
8f56ccca
                         c->cur_frame_duration = av_rescale_q(pkt.duration, ist->time_base, AV_TIME_BASE_Q);
3b371676
                         /* find RTP context */
                         c->packet_stream_index = pkt.stream_index;
                         ctx = c->rtp_ctx[c->packet_stream_index];
                         if(!ctx) {
c2f861ca
                             av_packet_unref(&pkt);
3b371676
                             break;
8a0b55ff
                         }
3b371676
                         codec = ctx->streams[0]->codec;
                         /* only one stream per RTP connection */
                         pkt.stream_index = 0;
                     } else {
                         ctx = &c->fmt_ctx;
                         /* Fudge here */
3ab29d8e
                         codec = ctx->streams[pkt.stream_index]->codec;
3b371676
                     }
 
                     if (c->is_packetized) {
                         int max_packet_size;
90abbdba
                         if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP)
3b371676
                             max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
                         else
b263bf66
                             max_packet_size = c->rtp_handles[c->packet_stream_index]->max_packet_size;
923a2445
                         ret = ffio_open_dyn_packet_buf(&ctx->pb,
                                                        max_packet_size);
469c335c
                     } else
b92c5452
                         ret = avio_open_dyn_buf(&ctx->pb);
469c335c
 
3b371676
                     if (ret < 0) {
                         /* XXX: potential leak */
                         return -1;
                     }
3ab29d8e
                     ost = ctx->streams[pkt.stream_index];
 
8978feda
                     ctx->pb->seekable = 0;
3b371676
                     if (pkt.dts != AV_NOPTS_VALUE)
923a2445
                         pkt.dts = av_rescale_q(pkt.dts, ist->time_base,
                                                ost->time_base);
3b371676
                     if (pkt.pts != AV_NOPTS_VALUE)
923a2445
                         pkt.pts = av_rescale_q(pkt.pts, ist->time_base,
                                                ost->time_base);
                     pkt.duration = av_rescale_q(pkt.duration, ist->time_base,
                                                 ost->time_base);
93f08827
                     if ((ret = av_write_frame(ctx, &pkt)) < 0) {
                         http_log("Error writing frame to output for stream '%s': %s\n",
                                  c->stream->filename, av_err2str(ret));
3b371676
                         c->state = HTTPSTATE_SEND_DATA_TRAILER;
3766ed72
                     }
3b371676
 
3d086791
                     av_freep(&c->pb_buffer);
6dc7d80d
                     len = avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
40063a90
                     ctx->pb = NULL;
3b371676
                     c->cur_frame_bytes = len;
                     c->buffer_ptr = c->pb_buffer;
                     c->buffer_end = c->pb_buffer + len;
 
                     codec->frame_number++;
                     if (len == 0) {
c2f861ca
                         av_packet_unref(&pkt);
3b371676
                         goto redo;
f747e6d3
                     }
85f07f22
                 }
c2f861ca
                 av_packet_unref(&pkt);
85f07f22
             }
3b371676
         }
85f07f22
         break;
     default:
     case HTTPSTATE_SEND_DATA_TRAILER:
         /* last packet test ? */
2effd274
         if (c->last_packet_sent || c->is_packetized)
85f07f22
             return -1;
2effd274
         ctx = &c->fmt_ctx;
85f07f22
         /* prepare header */
b92c5452
         if (avio_open_dyn_buf(&ctx->pb) < 0) {
2effd274
             /* XXX: potential leak */
             return -1;
         }
8978feda
         c->fmt_ctx.pb->seekable = 0;
2effd274
         av_write_trailer(ctx);
6dc7d80d
         len = avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
2effd274
         c->buffer_ptr = c->pb_buffer;
         c->buffer_end = c->pb_buffer + len;
 
85f07f22
         c->last_packet_sent = 1;
         break;
     }
     return 0;
 }
 
 /* should convert the format at the same time */
bc351386
 /* send data starting at c->buffer_ptr to the output connection
469c335c
  * (either UDP or TCP)
  */
5eb765ef
 static int http_send_data(HTTPContext *c)
85f07f22
 {
e240a0bb
     int len, ret;
85f07f22
 
bc351386
     for(;;) {
         if (c->buffer_ptr >= c->buffer_end) {
             ret = http_prepare_data(c);
             if (ret < 0)
                 return -1;
33d6f90e
             else if (ret)
bc351386
                 /* state change requested */
                 break;
2effd274
         } else {
bc351386
             if (c->is_packetized) {
                 /* RTP data output */
                 len = c->buffer_end - c->buffer_ptr;
                 if (len < 4) {
                     /* fail safe - should never happen */
                 fail1:
                     c->buffer_ptr = c->buffer_end;
2effd274
                     return 0;
                 }
bc351386
                 len = (c->buffer_ptr[0] << 24) |
                     (c->buffer_ptr[1] << 16) |
                     (c->buffer_ptr[2] << 8) |
                     (c->buffer_ptr[3]);
                 if (len > (c->buffer_end - c->buffer_ptr))
                     goto fail1;
e240a0bb
                 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
                     /* nothing to send yet: we can wait */
                     return 0;
                 }
 
                 c->data_count += len;
                 update_datarate(&c->datarate, c->data_count);
                 if (c->stream)
                     c->stream->bytes_served += len;
 
90abbdba
                 if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP) {
bc351386
                     /* RTP packets are sent inside the RTSP TCP connection */
ae628ec1
                     AVIOContext *pb;
bc351386
                     int interleaved_index, size;
                     uint8_t header[4];
                     HTTPContext *rtsp_c;
115329f1
 
bc351386
                     rtsp_c = c->rtsp_c;
                     /* if no RTSP connection left, error */
                     if (!rtsp_c)
                         return -1;
                     /* if already sending something, then wait. */
611c5741
                     if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
bc351386
                         break;
b92c5452
                     if (avio_open_dyn_buf(&pb) < 0)
bc351386
                         goto fail1;
                     interleaved_index = c->packet_stream_index * 2;
                     /* RTCP packets are sent at odd indexes */
                     if (c->buffer_ptr[1] == 200)
                         interleaved_index++;
                     /* write RTSP TCP header */
                     header[0] = '$';
                     header[1] = interleaved_index;
                     header[2] = len >> 8;
                     header[3] = len;
77eb5504
                     avio_write(pb, header, 4);
bc351386
                     /* write RTP packet data */
                     c->buffer_ptr += 4;
77eb5504
                     avio_write(pb, c->buffer_ptr, len);
6dc7d80d
                     size = avio_close_dyn_buf(pb, &c->packet_buffer);
bc351386
                     /* prepare asynchronous TCP sending */
                     rtsp_c->packet_buffer_ptr = c->packet_buffer;
                     rtsp_c->packet_buffer_end = c->packet_buffer + size;
e240a0bb
                     c->buffer_ptr += len;
115329f1
 
e240a0bb
                     /* send everything we can NOW */
c60202df
                     len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
df4346c5
                                rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
611c5741
                     if (len > 0)
e240a0bb
                         rtsp_c->packet_buffer_ptr += len;
                     if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
                         /* if we could not send all the data, we will
469c335c
                          * send it later, so a new state is needed to
                          * "lock" the RTSP TCP connection */
e240a0bb
                         rtsp_c->state = RTSPSTATE_SEND_PACKET;
                         break;
611c5741
                     } else
e240a0bb
                         /* all data has been sent */
                         av_freep(&c->packet_buffer);
                 } else {
                     /* send RTP packet directly in UDP */
bc351386
                     c->buffer_ptr += 4;
b263bf66
                     ffurl_write(c->rtp_handles[c->packet_stream_index],
                                 c->buffer_ptr, len);
e240a0bb
                     c->buffer_ptr += len;
923a2445
                     /* here we continue as we can send several packets
                      * per 10 ms slot */
bc351386
                 }
             } else {
                 /* TCP data output */
50c1cac4
                 len = send(c->fd, c->buffer_ptr,
                            c->buffer_end - c->buffer_ptr, 0);
bc351386
                 if (len < 0) {
28c4741a
                     if (ff_neterrno() != AVERROR(EAGAIN) &&
                         ff_neterrno() != AVERROR(EINTR))
bc351386
                         /* error : close connection */
                         return -1;
611c5741
                     else
bc351386
                         return 0;
196bc03a
                 }
                 c->buffer_ptr += len;
611c5741
 
e240a0bb
                 c->data_count += len;
                 update_datarate(&c->datarate, c->data_count);
                 if (c->stream)
                     c->stream->bytes_served += len;
                 break;
2effd274
             }
85f07f22
         }
bc351386
     } /* for(;;) */
85f07f22
     return 0;
 }
 
 static int http_start_receive_data(HTTPContext *c)
 {
     int fd;
0124fca0
     int ret;
baec6d8a
     int64_t ret64;
85f07f22
 
0124fca0
     if (c->stream->feed_opened) {
50c1cac4
         http_log("Stream feed '%s' was not opened\n",
                  c->stream->feed_filename);
0124fca0
         return AVERROR(EINVAL);
     }
85f07f22
 
e322ea48
     /* Don't permit writing to this one */
0124fca0
     if (c->stream->readonly) {
50c1cac4
         http_log("Cannot write to read-only file '%s'\n",
                  c->stream->feed_filename);
0124fca0
         return AVERROR(EINVAL);
     }
e322ea48
 
85f07f22
     /* open feed */
     fd = open(c->stream->feed_filename, O_RDWR);
929a9b75
     if (fd < 0) {
0124fca0
         ret = AVERROR(errno);
33f10fa6
         http_log("Could not open feed file '%s': %s\n",
0124fca0
                  c->stream->feed_filename, strerror(errno));
         return ret;
929a9b75
     }
85f07f22
     c->feed_fd = fd;
115329f1
 
861ec13a
     if (c->stream->truncate) {
         /* truncate feed file */
         ffm_write_write_index(c->feed_fd, FFM_PACKET_SIZE);
         http_log("Truncating feed file '%s'\n", c->stream->feed_filename);
0de1319e
         if (ftruncate(c->feed_fd, FFM_PACKET_SIZE) < 0) {
0124fca0
             ret = AVERROR(errno);
             http_log("Error truncating feed file '%s': %s\n",
                      c->stream->feed_filename, strerror(errno));
             return ret;
0de1319e
         }
861ec13a
     } else {
baec6d8a
         ret64 = ffm_read_write_index(fd);
         if (ret64 < 0) {
0124fca0
             http_log("Error reading write index from feed file '%s': %s\n",
                      c->stream->feed_filename, strerror(errno));
baec6d8a
             return ret64;
1f611549
         }
baec6d8a
         c->stream->feed_write_index = ret64;
861ec13a
     }
 
50c1cac4
     c->stream->feed_write_index = FFMAX(ffm_read_write_index(fd),
                                         FFM_PACKET_SIZE);
85f07f22
     c->stream->feed_size = lseek(fd, 0, SEEK_END);
     lseek(fd, 0, SEEK_SET);
 
     /* init buffer input */
     c->buffer_ptr = c->buffer;
     c->buffer_end = c->buffer + FFM_PACKET_SIZE;
     c->stream->feed_opened = 1;
fd7bec5e
     c->chunked_encoding = !!av_stristr(c->buffer, "Transfer-Encoding: chunked");
85f07f22
     return 0;
 }
115329f1
 
85f07f22
 static int http_receive_data(HTTPContext *c)
 {
     HTTPContext *c1;
19c8c4ec
     int len, loop_run = 0;
85f07f22
 
19c8c4ec
     while (c->chunked_encoding && !c->chunk_size &&
            c->buffer_end > c->buffer_ptr) {
         /* read chunk header, if present */
         len = recv(c->fd, c->buffer_ptr, 1, 0);
 
         if (len < 0) {
28c4741a
             if (ff_neterrno() != AVERROR(EAGAIN) &&
                 ff_neterrno() != AVERROR(EINTR))
19c8c4ec
                 /* error : close connection */
                 goto fail;
686d6f40
             return 0;
19c8c4ec
         } else if (len == 0) {
             /* end of connection : close it */
             goto fail;
         } else if (c->buffer_ptr - c->buffer >= 2 &&
                    !memcmp(c->buffer_ptr - 1, "\r\n", 2)) {
             c->chunk_size = strtol(c->buffer, 0, 16);
c12ee64e
             if (c->chunk_size <= 0) { // end of stream or invalid chunk size
                 c->chunk_size = 0;
19c8c4ec
                 goto fail;
c12ee64e
             }
19c8c4ec
             c->buffer_ptr = c->buffer;
             break;
469c335c
         } else if (++loop_run > 10)
19c8c4ec
             /* no chunk header, abort */
             goto fail;
469c335c
         else
19c8c4ec
             c->buffer_ptr++;
     }
a6e14edd
 
19c8c4ec
     if (c->buffer_end > c->buffer_ptr) {
         len = recv(c->fd, c->buffer_ptr,
                    FFMIN(c->chunk_size, c->buffer_end - c->buffer_ptr), 0);
a6e14edd
         if (len < 0) {
28c4741a
             if (ff_neterrno() != AVERROR(EAGAIN) &&
                 ff_neterrno() != AVERROR(EINTR))
a6e14edd
                 /* error : close connection */
                 goto fail;
611c5741
         } else if (len == 0)
a6e14edd
             /* end of connection : close it */
             goto fail;
611c5741
         else {
c12ee64e
             av_assert0(len <= c->chunk_size);
19c8c4ec
             c->chunk_size -= len;
a6e14edd
             c->buffer_ptr += len;
             c->data_count += len;
5eb765ef
             update_datarate(&c->datarate, c->data_count);
a6e14edd
         }
     }
 
d445a7e9
     if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
         if (c->buffer[0] != 'f' ||
             c->buffer[1] != 'm') {
             http_log("Feed stream has become desynchronized -- disconnecting\n");
             goto fail;
         }
     }
 
85f07f22
     if (c->buffer_ptr >= c->buffer_end) {
f524d2e4
         FFServerStream *feed = c->stream;
85f07f22
         /* a packet has been received : write it in the store, except
469c335c
          * if header */
85f07f22
         if (c->data_count > FFM_PACKET_SIZE) {
5b881499
             /* XXX: use llseek or url_seek
              * XXX: Should probably fail? */
             if (lseek(c->feed_fd, feed->feed_write_index, SEEK_SET) == -1)
                 http_log("Seek to %"PRId64" failed\n", feed->feed_write_index);
 
929a9b75
             if (write(c->feed_fd, c->buffer, FFM_PACKET_SIZE) < 0) {
                 http_log("Error writing to feed file: %s\n", strerror(errno));
                 goto fail;
             }
115329f1
 
85f07f22
             feed->feed_write_index += FFM_PACKET_SIZE;
             /* update file size */
             if (feed->feed_write_index > c->stream->feed_size)
                 feed->feed_size = feed->feed_write_index;
 
             /* handle wrap around if max file size reached */
50c1cac4
             if (c->stream->feed_max_size &&
                 feed->feed_write_index >= c->stream->feed_max_size)
85f07f22
                 feed->feed_write_index = FFM_PACKET_SIZE;
 
             /* write index */
2779cdad
             if (ffm_write_write_index(c->feed_fd, feed->feed_write_index) < 0) {
50c1cac4
                 http_log("Error writing index to feed file: %s\n",
                          strerror(errno));
2779cdad
                 goto fail;
             }
85f07f22
 
             /* wake up any waiting connections */
81a663f4
             for(c1 = first_http_ctx; c1; c1 = c1->next) {
115329f1
                 if (c1->state == HTTPSTATE_WAIT_FEED &&
611c5741
                     c1->stream->feed == c->stream->feed)
85f07f22
                     c1->state = HTTPSTATE_SEND_DATA;
             }
f747e6d3
         } else {
             /* We have a header in our hands that contains useful data */
50f2dfad
             AVFormatContext *s = avformat_alloc_context();
ae628ec1
             AVIOContext *pb;
bd7cf6ad
             AVInputFormat *fmt_in;
f747e6d3
             int i;
 
50f2dfad
             if (!s)
                 goto fail;
 
bd7cf6ad
             /* use feed output format name to find corresponding input format */
             fmt_in = av_find_input_format(feed->fmt->name);
             if (!fmt_in)
                 goto fail;
 
83fddaeb
             pb = avio_alloc_context(c->buffer, c->buffer_end - c->buffer,
                                     0, NULL, NULL, NULL, NULL);
cea3c9b2
             if (!pb)
                 goto fail;
 
8978feda
             pb->seekable = 0;
697efa36
 
50f2dfad
             s->pb = pb;
             if (avformat_open_input(&s, c->stream->feed_filename, fmt_in, NULL) < 0) {
e97f38ce
                 av_freep(&pb);
e6f0deab
                 goto fail;
             }
f747e6d3
 
             /* Now we have the actual streams */
f2972c8c
             if (s->nb_streams != feed->nb_streams) {
cd3716b9
                 avformat_close_input(&s);
e97f38ce
                 av_freep(&pb);
77553ae3
                 http_log("Feed '%s' stream number does not match registered feed\n",
                          c->stream->feed_filename);
f747e6d3
                 goto fail;
             }
f2972c8c
 
cb51aef1
             for (i = 0; i < s->nb_streams; i++) {
                 AVStream *fst = feed->streams[i];
                 AVStream *st = s->streams[i];
5634f30c
                 avcodec_copy_context(fst->codec, st->codec);
cb51aef1
             }
f2972c8c
 
cd3716b9
             avformat_close_input(&s);
e97f38ce
             av_freep(&pb);
85f07f22
         }
         c->buffer_ptr = c->buffer;
     }
 
     return 0;
  fail:
     c->stream->feed_opened = 0;
     close(c->feed_fd);
c1593d0e
     /* wake up any waiting connections to stop waiting for feed */
81a663f4
     for(c1 = first_http_ctx; c1; c1 = c1->next) {
c1593d0e
         if (c1->state == HTTPSTATE_WAIT_FEED &&
             c1->stream->feed == c->stream->feed)
             c1->state = HTTPSTATE_SEND_DATA_TRAILER;
     }
85f07f22
     return -1;
 }
 
2effd274
 /********************************************************************/
 /* RTSP handling */
 
 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
 {
     const char *str;
     time_t ti;
0156fcf2
     struct tm *tm;
2effd274
     char buf2[32];
 
02497a5d
     str = RTSP_STATUS_CODE2STRING(error_number);
     if (!str)
2effd274
         str = "Unknown Error";
115329f1
 
d9d86e00
     avio_printf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
     avio_printf(c->pb, "CSeq: %d\r\n", c->seq);
2effd274
 
     /* output GMT time */
     ti = time(NULL);
0156fcf2
     tm = gmtime(&ti);
     strftime(buf2, sizeof(buf2), "%a, %d %b %Y %H:%M:%S", tm);
d9d86e00
     avio_printf(c->pb, "Date: %s GMT\r\n", buf2);
2effd274
 }
 
 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
 {
     rtsp_reply_header(c, error_number);
d9d86e00
     avio_printf(c->pb, "\r\n");
2effd274
 }
 
 static int rtsp_parse_request(HTTPContext *c)
 {
     const char *p, *p1, *p2;
     char cmd[32];
     char url[1024];
     char protocol[32];
     char line[1024];
     int len;
a92be9b8
     RTSPMessageHeader header1 = { 0 }, *header = &header1;
115329f1
 
2effd274
     c->buffer_ptr[0] = '\0';
     p = c->buffer;
115329f1
 
2effd274
     get_word(cmd, sizeof(cmd), &p);
     get_word(url, sizeof(url), &p);
     get_word(protocol, sizeof(protocol), &p);
 
f7d78f36
     av_strlcpy(c->method, cmd, sizeof(c->method));
     av_strlcpy(c->url, url, sizeof(c->url));
     av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2effd274
 
b92c5452
     if (avio_open_dyn_buf(&c->pb) < 0) {
2effd274
         /* XXX: cannot do more */
         c->pb = NULL; /* safety */
         return -1;
     }
 
     /* check version name */
33d6f90e
     if (strcmp(protocol, "RTSP/1.0")) {
2effd274
         rtsp_reply_error(c, RTSP_STATUS_VERSION);
         goto the_end;
     }
 
     /* parse each header line */
     /* skip to next line */
     while (*p != '\n' && *p != '\0')
         p++;
     if (*p == '\n')
         p++;
     while (*p != '\0') {
c966c912
         p1 = memchr(p, '\n', (char *)c->buffer_ptr - p);
2effd274
         if (!p1)
             break;
         p2 = p1;
         if (p2 > p && p2[-1] == '\r')
             p2--;
         /* skip empty line */
         if (p2 == p)
             break;
         len = p2 - p;
         if (len > sizeof(line) - 1)
             len = sizeof(line) - 1;
         memcpy(line, p, len);
         line[len] = '\0';
15206ffd
         ff_rtsp_parse_line(NULL, header, line, NULL, NULL);
2effd274
         p = p1 + 1;
     }
 
     /* handle sequence number */
     c->seq = header->seq;
 
611c5741
     if (!strcmp(cmd, "DESCRIBE"))
2effd274
         rtsp_cmd_describe(c, url);
611c5741
     else if (!strcmp(cmd, "OPTIONS"))
0df65975
         rtsp_cmd_options(c, url);
611c5741
     else if (!strcmp(cmd, "SETUP"))
2effd274
         rtsp_cmd_setup(c, url, header);
611c5741
     else if (!strcmp(cmd, "PLAY"))
2effd274
         rtsp_cmd_play(c, url, header);
611c5741
     else if (!strcmp(cmd, "PAUSE"))
87079bd0
         rtsp_cmd_interrupt(c, url, header, 1);
611c5741
     else if (!strcmp(cmd, "TEARDOWN"))
87079bd0
         rtsp_cmd_interrupt(c, url, header, 0);
611c5741
     else
2effd274
         rtsp_reply_error(c, RTSP_STATUS_METHOD);
611c5741
 
2effd274
  the_end:
6dc7d80d
     len = avio_close_dyn_buf(c->pb, &c->pb_buffer);
2effd274
     c->pb = NULL; /* safety */
469c335c
     if (len < 0)
2effd274
         /* XXX: cannot do more */
         return -1;
469c335c
 
2effd274
     c->buffer_ptr = c->pb_buffer;
     c->buffer_end = c->pb_buffer + len;
     c->state = RTSPSTATE_SEND_REPLY;
     return 0;
 }
 
f524d2e4
 static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
829ac53d
                                    struct in_addr my_ip)
2effd274
 {
dd723472
     AVFormatContext *avc;
9389b925
     AVStream *avs = NULL;
cbe43e62
     AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
9985710a
     AVDictionaryEntry *entry = av_dict_get(stream->metadata, "title", NULL, 0);
dd723472
     int i;
115329f1
 
1d8d21b9
     *pbuffer = NULL;
 
8e2fd8e1
     avc =  avformat_alloc_context();
469c335c
     if (!avc || !rtp_format)
2effd274
         return -1;
469c335c
 
cbe43e62
     avc->oformat = rtp_format;
d2d67e42
     av_dict_set(&avc->metadata, "title",
9985710a
                 entry ? entry->value : "No Title", 0);
dd723472
     avc->nb_streams = stream->nb_streams;
     if (stream->is_multicast) {
         snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
                  inet_ntoa(stream->multicast_ip),
                  stream->multicast_port, stream->multicast_ttl);
469c335c
     } else
43d09faf
         snprintf(avc->filename, 1024, "rtp://0.0.0.0");
115329f1
 
0002a22e
     avc->streams = av_malloc_array(avc->nb_streams, sizeof(*avc->streams));
     if (!avc->streams)
9389b925
         goto sdp_done;
0002a22e
 
     avs = av_malloc_array(avc->nb_streams, sizeof(*avs));
     if (!avs)
9389b925
         goto sdp_done;
 
2effd274
     for(i = 0; i < stream->nb_streams; i++) {
dd723472
         avc->streams[i] = &avs[i];
         avc->streams[i]->codec = stream->streams[i]->codec;
6f69f7a8
         avcodec_parameters_from_context(stream->streams[i]->codecpar, stream->streams[i]->codec);
         avc->streams[i]->codecpar = stream->streams[i]->codecpar;
2effd274
     }
dd723472
     *pbuffer = av_mallocz(2048);
d6039063
     if (!*pbuffer)
         goto sdp_done;
c3675dfe
     av_sdp_create(&avc, 1, *pbuffer, 2048);
9389b925
 
  sdp_done:
e97f38ce
     av_freep(&avc->streams);
d2d67e42
     av_dict_free(&avc->metadata);
dd723472
     av_free(avc);
9389b925
     av_free(avs);
dd723472
 
1d8d21b9
     return *pbuffer ? strlen(*pbuffer) : AVERROR(ENOMEM);
2effd274
 }
 
0df65975
 static void rtsp_cmd_options(HTTPContext *c, const char *url)
 {
469c335c
     /* rtsp_reply_header(c, RTSP_STATUS_OK); */
d9d86e00
     avio_printf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
     avio_printf(c->pb, "CSeq: %d\r\n", c->seq);
50c1cac4
     avio_printf(c->pb, "Public: %s\r\n",
                 "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
d9d86e00
     avio_printf(c->pb, "\r\n");
0df65975
 }
 
2effd274
 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
 {
f524d2e4
     FFServerStream *stream;
2effd274
     char path1[1024];
     const char *path;
0c1a9eda
     uint8_t *content;
cc64ec57
     int content_length;
     socklen_t len;
829ac53d
     struct sockaddr_in my_addr;
115329f1
 
0c5a6ef8
     /* find which URL is asked */
f3bfe388
     av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2effd274
     path = path1;
     if (*path == '/')
         path++;
 
f524d2e4
     for(stream = config.first_stream; stream; stream = stream->next) {
25e3e53d
         if (!stream->is_feed &&
             stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2effd274
             !strcmp(path, stream->filename)) {
             goto found;
         }
     }
     /* no stream found */
4c8be331
     rtsp_reply_error(c, RTSP_STATUS_NOT_FOUND);
2effd274
     return;
 
  found:
0c5a6ef8
     /* prepare the media description in SDP format */
829ac53d
 
     /* get the host IP */
     len = sizeof(my_addr);
     getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
923a2445
     content_length = prepare_sdp_description(stream, &content,
                                              my_addr.sin_addr);
2effd274
     if (content_length < 0) {
         rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
         return;
     }
     rtsp_reply_header(c, RTSP_STATUS_OK);
d9d86e00
     avio_printf(c->pb, "Content-Base: %s/\r\n", url);
     avio_printf(c->pb, "Content-Type: application/sdp\r\n");
     avio_printf(c->pb, "Content-Length: %d\r\n", content_length);
     avio_printf(c->pb, "\r\n");
77eb5504
     avio_write(c->pb, content, content_length);
ea4f8aab
     av_free(content);
2effd274
 }
 
 static HTTPContext *find_rtp_session(const char *session_id)
 {
     HTTPContext *c;
 
     if (session_id[0] == '\0')
         return NULL;
 
81a663f4
     for(c = first_http_ctx; c; c = c->next) {
2effd274
         if (!strcmp(c->session_id, session_id))
             return c;
     }
     return NULL;
 }
 
a9e534d5
 static RTSPTransportField *find_transport(RTSPMessageHeader *h, enum RTSPLowerTransport lower_transport)
2effd274
 {
     RTSPTransportField *th;
     int i;
 
     for(i=0;i<h->nb_transports;i++) {
         th = &h->transports[i];
90abbdba
         if (th->lower_transport == lower_transport)
2effd274
             return th;
     }
     return NULL;
 }
 
115329f1
 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
a9e534d5
                            RTSPMessageHeader *h)
2effd274
 {
f524d2e4
     FFServerStream *stream;
bacde646
     int stream_index, rtp_port, rtcp_port;
2effd274
     char buf[1024];
     char path1[1024];
     const char *path;
     HTTPContext *rtp_c;
     RTSPTransportField *th;
     struct sockaddr_in dest_addr;
     RTSPActionServerSetup setup;
115329f1
 
0c5a6ef8
     /* find which URL is asked */
f3bfe388
     av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2effd274
     path = path1;
     if (*path == '/')
         path++;
 
     /* now check each stream */
f524d2e4
     for(stream = config.first_stream; stream; stream = stream->next) {
ac1940b2
         if (stream->is_feed || !stream->fmt ||
             strcmp(stream->fmt->name, "rtp")) {
             continue;
         }
99750884
         /* accept aggregate filenames only if single stream */
         if (!strcmp(path, stream->filename)) {
             if (stream->nb_streams != 1) {
                 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
                 return;
2effd274
             }
99750884
             stream_index = 0;
             goto found;
         }
115329f1
 
99750884
         for(stream_index = 0; stream_index < stream->nb_streams;
             stream_index++) {
             snprintf(buf, sizeof(buf), "%s/streamid=%d",
                      stream->filename, stream_index);
             if (!strcmp(path, buf))
                 goto found;
         }
2effd274
     }
     /* no stream found */
     rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
     return;
  found:
 
     /* generate session id if needed */
d40c0e4a
     if (h->session_id[0] == '\0') {
         unsigned random0 = av_lfg_get(&random_state);
         unsigned random1 = av_lfg_get(&random_state);
1df93ae9
         snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
d40c0e4a
                  random0, random1);
     }
2effd274
 
0c5a6ef8
     /* find RTP session, and create it if none found */
2effd274
     rtp_c = find_rtp_session(h->session_id);
     if (!rtp_c) {
bc351386
         /* always prefer UDP */
90abbdba
         th = find_transport(h, RTSP_LOWER_TRANSPORT_UDP);
bc351386
         if (!th) {
90abbdba
             th = find_transport(h, RTSP_LOWER_TRANSPORT_TCP);
bc351386
             if (!th) {
                 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
                 return;
             }
         }
 
         rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
90abbdba
                                    th->lower_transport);
2effd274
         if (!rtp_c) {
             rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
             return;
         }
 
         /* open input stream */
         if (open_input_stream(rtp_c, "") < 0) {
             rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
             return;
         }
     }
115329f1
 
2effd274
     /* test if stream is OK (test needed because several SETUP needs
469c335c
      * to be done for a given file) */
2effd274
     if (rtp_c->stream != stream) {
         rtsp_reply_error(c, RTSP_STATUS_SERVICE);
         return;
     }
115329f1
 
2effd274
     /* test if stream is already set up */
     if (rtp_c->rtp_ctx[stream_index]) {
         rtsp_reply_error(c, RTSP_STATUS_STATE);
         return;
     }
 
     /* check transport */
     th = find_transport(h, rtp_c->rtp_protocol);
90abbdba
     if (!th || (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
2effd274
                 th->client_port_min <= 0)) {
         rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
         return;
     }
 
     /* setup default options */
     setup.transport_option[0] = '\0';
     dest_addr = rtp_c->from_addr;
     dest_addr.sin_port = htons(th->client_port_min);
115329f1
 
2effd274
     /* setup stream */
bc351386
     if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2effd274
         rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
         return;
     }
 
     /* now everything is OK, so we can send the connection parameters */
     rtsp_reply_header(c, RTSP_STATUS_OK);
     /* session ID */
d9d86e00
     avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2effd274
 
     switch(rtp_c->rtp_protocol) {
90abbdba
     case RTSP_LOWER_TRANSPORT_UDP:
bfc6db44
         rtp_port = ff_rtp_get_local_rtp_port(rtp_c->rtp_handles[stream_index]);
         rtcp_port = ff_rtp_get_local_rtcp_port(rtp_c->rtp_handles[stream_index]);
d9d86e00
         avio_printf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2effd274
                     "client_port=%d-%d;server_port=%d-%d",
bacde646
                     th->client_port_min, th->client_port_max,
                     rtp_port, rtcp_port);
2effd274
         break;
90abbdba
     case RTSP_LOWER_TRANSPORT_TCP:
d9d86e00
         avio_printf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2effd274
                     stream_index * 2, stream_index * 2 + 1);
         break;
     default:
         break;
     }
611c5741
     if (setup.transport_option[0] != '\0')
d9d86e00
         avio_printf(c->pb, ";%s", setup.transport_option);
     avio_printf(c->pb, "\r\n");
115329f1
 
2effd274
 
d9d86e00
     avio_printf(c->pb, "\r\n");
2effd274
 }
 
 
469c335c
 /**
  * find an RTP connection by using the session ID. Check consistency
  * with filename
  */
115329f1
 static HTTPContext *find_rtp_session_with_url(const char *url,
2effd274
                                               const char *session_id)
 {
     HTTPContext *rtp_c;
     char path1[1024];
     const char *path;
94d9ad5f
     char buf[1024];
c2ca851b
     int s, len;
2effd274
 
     rtp_c = find_rtp_session(session_id);
     if (!rtp_c)
         return NULL;
 
0c5a6ef8
     /* find which URL is asked */
f3bfe388
     av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2effd274
     path = path1;
     if (*path == '/')
         path++;
94d9ad5f
     if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
     for(s=0; s<rtp_c->stream->nb_streams; ++s) {
       snprintf(buf, sizeof(buf), "%s/streamid=%d",
         rtp_c->stream->filename, s);
469c335c
       if(!strncmp(path, buf, sizeof(buf)))
         /* XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE
          * if nb_streams>1? */
94d9ad5f
         return rtp_c;
     }
c2ca851b
     len = strlen(path);
     if (len > 0 && path[len - 1] == '/' &&
         !strncmp(path, rtp_c->stream->filename, len - 1))
         return rtp_c;
94d9ad5f
     return NULL;
2effd274
 }
 
a9e534d5
 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPMessageHeader *h)
2effd274
 {
     HTTPContext *rtp_c;
 
     rtp_c = find_rtp_session_with_url(url, h->session_id);
     if (!rtp_c) {
         rtsp_reply_error(c, RTSP_STATUS_SESSION);
         return;
     }
115329f1
 
2effd274
     if (rtp_c->state != HTTPSTATE_SEND_DATA &&
         rtp_c->state != HTTPSTATE_WAIT_FEED &&
         rtp_c->state != HTTPSTATE_READY) {
         rtsp_reply_error(c, RTSP_STATUS_STATE);
         return;
     }
 
     rtp_c->state = HTTPSTATE_SEND_DATA;
115329f1
 
2effd274
     /* now everything is OK, so we can send the connection parameters */
     rtsp_reply_header(c, RTSP_STATUS_OK);
     /* session ID */
d9d86e00
     avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
     avio_printf(c->pb, "\r\n");
2effd274
 }
 
50c1cac4
 static void rtsp_cmd_interrupt(HTTPContext *c, const char *url,
                                RTSPMessageHeader *h, int pause_only)
2effd274
 {
     HTTPContext *rtp_c;
 
     rtp_c = find_rtp_session_with_url(url, h->session_id);
     if (!rtp_c) {
         rtsp_reply_error(c, RTSP_STATUS_SESSION);
         return;
     }
115329f1
 
87079bd0
     if (pause_only) {
         if (rtp_c->state != HTTPSTATE_SEND_DATA &&
             rtp_c->state != HTTPSTATE_WAIT_FEED) {
             rtsp_reply_error(c, RTSP_STATUS_STATE);
             return;
         }
         rtp_c->state = HTTPSTATE_READY;
         rtp_c->first_pts = AV_NOPTS_VALUE;
2effd274
     }
115329f1
 
2effd274
     /* now everything is OK, so we can send the connection parameters */
     rtsp_reply_header(c, RTSP_STATUS_OK);
     /* session ID */
e2d7dc87
     avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
d9d86e00
     avio_printf(c->pb, "\r\n");
e2d7dc87
 
87079bd0
     if (!pause_only)
         close_connection(rtp_c);
2effd274
 }
 
 /********************************************************************/
 /* RTP handling */
 
115329f1
 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
50c1cac4
                                        FFServerStream *stream,
                                        const char *session_id,
90abbdba
                                        enum RTSPLowerTransport rtp_protocol)
2effd274
 {
     HTTPContext *c = NULL;
bc351386
     const char *proto_str;
115329f1
 
2effd274
     /* XXX: should output a warning page when coming
469c335c
      * close to the connection limit */
f524d2e4
     if (nb_connections >= config.nb_max_connections)
2effd274
         goto fail;
115329f1
 
2effd274
     /* add a new connection */
     c = av_mallocz(sizeof(HTTPContext));
     if (!c)
         goto fail;
115329f1
 
2effd274
     c->fd = -1;
     c->poll_entry = NULL;
6edd6884
     c->from_addr = *from_addr;
2effd274
     c->buffer_size = IOBUFFER_INIT_SIZE;
     c->buffer = av_malloc(c->buffer_size);
     if (!c->buffer)
         goto fail;
     nb_connections++;
     c->stream = stream;
f7d78f36
     av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
2effd274
     c->state = HTTPSTATE_READY;
     c->is_packetized = 1;
bc351386
     c->rtp_protocol = rtp_protocol;
 
2effd274
     /* protocol is shown in statistics */
bc351386
     switch(c->rtp_protocol) {
90abbdba
     case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
bc351386
         proto_str = "MCAST";
         break;
90abbdba
     case RTSP_LOWER_TRANSPORT_UDP:
bc351386
         proto_str = "UDP";
         break;
90abbdba
     case RTSP_LOWER_TRANSPORT_TCP:
bc351386
         proto_str = "TCP";
         break;
     default:
         proto_str = "???";
         break;
     }
f7d78f36
     av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
     av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
2effd274
 
6edd6884
     current_bandwidth += stream->bandwidth;
 
2effd274
     c->next = first_http_ctx;
     first_http_ctx = c;
     return c;
115329f1
 
2effd274
  fail:
     if (c) {
e97f38ce
         av_freep(&c->buffer);
2effd274
         av_free(c);
     }
     return NULL;
 }
 
469c335c
 /**
  * add a new RTP stream in an RTP connection (used in RTSP SETUP
  * command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
  * used.
  */
115329f1
 static int rtp_new_av_stream(HTTPContext *c,
bc351386
                              int stream_index, struct sockaddr_in *dest_addr,
                              HTTPContext *rtsp_c)
2effd274
 {
     AVFormatContext *ctx;
     AVStream *st;
     char *ipaddr;
75480e86
     URLContext *h = NULL;
0c1a9eda
     uint8_t *dummy_buf;
bc351386
     int max_packet_size;
2ec18db7
     void *st_internal;
115329f1
 
2effd274
     /* now we can open the relevant output stream */
8e2fd8e1
     ctx = avformat_alloc_context();
2effd274
     if (!ctx)
         return -1;
0f52ef1a
     ctx->oformat = av_guess_format("rtp", NULL, NULL);
2effd274
 
2ec18db7
     st = avformat_new_stream(ctx, NULL);
2effd274
     if (!st)
         goto fail;
2ec18db7
 
     av_freep(&st->codec);
     av_freep(&st->info);
     st_internal = st->internal;
2effd274
 
115329f1
     if (!c->stream->feed ||
611c5741
         c->stream->feed == c->stream)
2effd274
         memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
611c5741
     else
115329f1
         memcpy(st,
2effd274
                c->stream->feed->streams[c->stream->feed_streams[stream_index]],
                sizeof(AVStream));
57dbe08b
     st->priv_data = NULL;
2ec18db7
     st->internal = st_internal;
115329f1
 
bc351386
     /* build destination RTP address */
     ipaddr = inet_ntoa(dest_addr->sin_addr);
 
     switch(c->rtp_protocol) {
90abbdba
     case RTSP_LOWER_TRANSPORT_UDP:
     case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
bc351386
         /* RTP/UDP case */
115329f1
 
6edd6884
         /* XXX: also pass as parameter to function ? */
         if (c->stream->is_multicast) {
             int ttl;
             ttl = c->stream->multicast_ttl;
             if (!ttl)
                 ttl = 16;
             snprintf(ctx->filename, sizeof(ctx->filename),
115329f1
                      "rtp://%s:%d?multicast=1&ttl=%d",
6edd6884
                      ipaddr, ntohs(dest_addr->sin_port), ttl);
         } else {
             snprintf(ctx->filename, sizeof(ctx->filename),
                      "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
         }
2effd274
 
b263bf66
         if (ffurl_open(&h, ctx->filename, AVIO_FLAG_WRITE, NULL, NULL) < 0)
2effd274
             goto fail;
         c->rtp_handles[stream_index] = h;
b263bf66
         max_packet_size = h->max_packet_size;
bc351386
         break;
90abbdba
     case RTSP_LOWER_TRANSPORT_TCP:
bc351386
         /* RTP/TCP case */
         c->rtsp_c = rtsp_c;
         max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
         break;
     default:
2effd274
         goto fail;
     }
 
e21ac209
     http_log("%s:%d - - \"PLAY %s/streamid=%d %s\"\n",
115329f1
              ipaddr, ntohs(dest_addr->sin_port),
bc351386
              c->stream->filename, stream_index, c->protocol);
6edd6884
 
47ba4728
     /* normally, no packets should be output here, but the packet size may
      * be checked */
469c335c
     if (ffio_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0)
2effd274
         /* XXX: close stream */
         goto fail;
469c335c
 
50f2dfad
     if (avformat_write_header(ctx, NULL) < 0) {
2effd274
     fail:
         if (h)
b263bf66
             ffurl_close(h);
1404e2a3
         av_free(st);
2effd274
         av_free(ctx);
         return -1;
     }
6dc7d80d
     avio_close_dyn_buf(ctx->pb, &dummy_buf);
a00cc2e4
     ctx->pb = NULL;
2effd274
     av_free(dummy_buf);
115329f1
 
2effd274
     c->rtp_ctx[stream_index] = ctx;
     return 0;
 }
 
 /********************************************************************/
 /* ffserver initialization */
 
5a31f231
 /* FIXME: This code should use avformat_new_stream() */
50c1cac4
 static AVStream *add_av_stream1(FFServerStream *stream,
                                 AVCodecContext *codec, int copy)
2effd274
 {
     AVStream *fst;
 
0f46825d
     if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
         return NULL;
 
2effd274
     fst = av_mallocz(sizeof(AVStream));
     if (!fst)
         return NULL;
e175b55e
     if (copy) {
8b0226f2
         fst->codec = avcodec_alloc_context3(codec->codec);
d6039063
         if (!fst->codec) {
             av_free(fst);
             return NULL;
         }
8b0226f2
         avcodec_copy_context(fst->codec, codec);
469c335c
     } else
e175b55e
         /* live streams must use the actual feed's codec since it may be
35e525b7
          * updated later to carry extradata needed by them.
e175b55e
          */
         fst->codec = codec;
469c335c
 
2effd274
     fst->priv_data = av_mallocz(sizeof(FeedData));
5a31f231
     fst->internal = av_mallocz(sizeof(*fst->internal));
6f69f7a8
     fst->internal->avctx = avcodec_alloc_context3(NULL);
     fst->codecpar = avcodec_parameters_alloc();
d445a7e9
     fst->index = stream->nb_streams;
b263bf66
     avpriv_set_pts_info(fst, 33, 1, 90000);
6741f7c9
     fst->sample_aspect_ratio = codec->sample_aspect_ratio;
2effd274
     stream->streams[stream->nb_streams++] = fst;
     return fst;
 }
 
85f07f22
 /* return the stream number in the feed */
f524d2e4
 static int add_av_stream(FFServerStream *feed, AVStream *st)
85f07f22
 {
     AVStream *fst;
     AVCodecContext *av, *av1;
     int i;
 
01f4895c
     av = st->codec;
85f07f22
     for(i=0;i<feed->nb_streams;i++) {
ec6e035b
         av1 = feed->streams[i]->codec;
f747e6d3
         if (av1->codec_id == av->codec_id &&
             av1->codec_type == av->codec_type &&
85f07f22
             av1->bit_rate == av->bit_rate) {
 
             switch(av->codec_type) {
72415b2a
             case AVMEDIA_TYPE_AUDIO:
85f07f22
                 if (av1->channels == av->channels &&
                     av1->sample_rate == av->sample_rate)
71a1d111
                     return i;
85f07f22
                 break;
72415b2a
             case AVMEDIA_TYPE_VIDEO:
85f07f22
                 if (av1->width == av->width &&
                     av1->height == av->height &&
c0df9d75
                     av1->time_base.den == av->time_base.den &&
                     av1->time_base.num == av->time_base.num &&
85f07f22
                     av1->gop_size == av->gop_size)
71a1d111
                     return i;
85f07f22
                 break;
f747e6d3
             default:
0f4e8165
                 abort();
85f07f22
             }
         }
     }
115329f1
 
e175b55e
     fst = add_av_stream1(feed, av, 0);
85f07f22
     if (!fst)
         return -1;
ec6e035b
     if (av_stream_get_recommended_encoder_configuration(st))
         av_stream_set_recommended_encoder_configuration(fst,
             av_strdup(av_stream_get_recommended_encoder_configuration(st)));
85f07f22
     return feed->nb_streams - 1;
 }
 
f524d2e4
 static void remove_stream(FFServerStream *stream)
2effd274
 {
f524d2e4
     FFServerStream **ps;
     ps = &config.first_stream;
81a663f4
     while (*ps) {
611c5741
         if (*ps == stream)
2effd274
             *ps = (*ps)->next;
611c5741
         else
2effd274
             ps = &(*ps)->next;
     }
 }
 
0c5a6ef8
 /* specific MPEG4 handling : we extract the raw parameters */
b29f97d1
 static void extract_mpeg4_header(AVFormatContext *infile)
0fa45e19
 {
     int mpeg4_count, i, size;
     AVPacket pkt;
     AVStream *st;
0c1a9eda
     const uint8_t *p;
0fa45e19
 
566de8cd
     infile->flags |= AVFMT_FLAG_NOFILLIN | AVFMT_FLAG_NOPARSE;
 
0fa45e19
     mpeg4_count = 0;
     for(i=0;i<infile->nb_streams;i++) {
         st = infile->streams[i];
36ef5369
         if (st->codec->codec_id == AV_CODEC_ID_MPEG4 &&
01f4895c
             st->codec->extradata_size == 0) {
0fa45e19
             mpeg4_count++;
         }
     }
     if (!mpeg4_count)
         return;
 
50c1cac4
     printf("MPEG4 without extra data: trying to find header in %s\n",
            infile->filename);
0fa45e19
     while (mpeg4_count > 0) {
566de8cd
         if (av_read_frame(infile, &pkt) < 0)
0fa45e19
             break;
         st = infile->streams[pkt.stream_index];
36ef5369
         if (st->codec->codec_id == AV_CODEC_ID_MPEG4 &&
01f4895c
             st->codec->extradata_size == 0) {
             av_freep(&st->codec->extradata);
0fa45e19
             /* fill extradata with the header */
             /* XXX: we make hard suppositions here ! */
             p = pkt.data;
             while (p < pkt.data + pkt.size - 4) {
                 /* stop when vop header is found */
115329f1
                 if (p[0] == 0x00 && p[1] == 0x00 &&
0fa45e19
                     p[2] == 0x01 && p[3] == 0xb6) {
                     size = p - pkt.data;
29d147c9
                     st->codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
01f4895c
                     st->codec->extradata_size = size;
                     memcpy(st->codec->extradata, pkt.data, size);
0fa45e19
                     break;
                 }
                 p++;
             }
             mpeg4_count--;
         }
c2f861ca
         av_packet_unref(&pkt);
0fa45e19
     }
 }
 
2effd274
 /* compute the needed AVStream for each file */
b29f97d1
 static void build_file_streams(void)
2effd274
 {
4ba148a6
     FFServerStream *stream;
     AVFormatContext *infile;
f61d45c9
     int i, ret;
2effd274
 
     /* gather all streams */
4ba148a6
     for(stream = config.first_stream; stream; stream = stream->next) {
         infile = NULL;
115329f1
 
4ba148a6
         if (stream->stream_type != STREAM_TYPE_LIVE || stream->feed)
             continue;
 
         /* the stream comes from a file */
         /* try to open the file */
         /* open stream */
b133ec62
 
0fa45e19
 
4ba148a6
         /* specific case: if transport stream output to RTP,
          * we use a raw transport stream reader */
         if (stream->fmt && !strcmp(stream->fmt->name, "rtp"))
             av_dict_set(&stream->in_opts, "mpeg2ts_compute_pcr", "1", 0);
611c5741
 
4ba148a6
         if (!stream->feed_filename[0]) {
             http_log("Unspecified feed file for stream '%s'\n",
                      stream->filename);
             goto fail;
         }
 
         http_log("Opening feed file '%s' for stream '%s'\n",
                  stream->feed_filename, stream->filename);
 
         ret = avformat_open_input(&infile, stream->feed_filename,
                                   stream->ifmt, &stream->in_opts);
         if (ret < 0) {
             http_log("Could not open '%s': %s\n", stream->feed_filename,
                      av_err2str(ret));
             /* remove stream (no need to spend more time on it) */
         fail:
             remove_stream(stream);
         } else {
             /* find all the AVStreams inside and reference them in
              * 'stream' */
             if (avformat_find_stream_info(infile, NULL) < 0) {
                 http_log("Could not find codec parameters from '%s'\n",
                          stream->feed_filename);
cd3716b9
                 avformat_close_input(&infile);
4ba148a6
                 goto fail;
2effd274
             }
4ba148a6
             extract_mpeg4_header(infile);
 
             for(i=0;i<infile->nb_streams;i++)
                 add_av_stream1(stream, infile->streams[i]->codec, 1);
 
             avformat_close_input(&infile);
2effd274
         }
     }
 }
 
ae2ed20b
 static inline
 int check_codec_match(AVCodecContext *ccf, AVCodecContext *ccs, int stream)
 {
     int matches = 1;
 
 #define CHECK_CODEC(x)  (ccf->x != ccs->x)
     if (CHECK_CODEC(codec_id) || CHECK_CODEC(codec_type)) {
         http_log("Codecs do not match for stream %d\n", stream);
         matches = 0;
     } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
         http_log("Codec bitrates do not match for stream %d\n", stream);
         matches = 0;
     } else if (ccf->codec_type == AVMEDIA_TYPE_VIDEO) {
         if (CHECK_CODEC(time_base.den) ||
             CHECK_CODEC(time_base.num) ||
             CHECK_CODEC(width) ||
             CHECK_CODEC(height)) {
             http_log("Codec width, height or framerate do not match for stream %d\n", stream);
             matches = 0;
         }
     } else if (ccf->codec_type == AVMEDIA_TYPE_AUDIO) {
         if (CHECK_CODEC(sample_rate) ||
             CHECK_CODEC(channels) ||
             CHECK_CODEC(frame_size)) {
             http_log("Codec sample_rate, channels, frame_size do not match for stream %d\n", stream);
             matches = 0;
         }
     } else {
         http_log("Unknown codec type for stream %d\n", stream);
         matches = 0;
     }
 
     return matches;
 }
 
85f07f22
 /* compute the needed AVStream for each feed */
532a2833
 static int build_feed_streams(void)
85f07f22
 {
f524d2e4
     FFServerStream *stream, *feed;
ae2ed20b
     int i, fd;
85f07f22
 
     /* gather all streams */
f524d2e4
     for(stream = config.first_stream; stream; stream = stream->next) {
85f07f22
         feed = stream->feed;
36a617c1
         if (!feed)
             continue;
 
         if (stream->is_feed) {
             for(i=0;i<stream->nb_streams;i++)
                 stream->feed_streams[i] = i;
ae2ed20b
             continue;
85f07f22
         }
ae2ed20b
         /* we handle a stream coming from a feed */
         for(i=0;i<stream->nb_streams;i++)
             stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
85f07f22
     }
 
     /* create feed files if needed */
f524d2e4
     for(feed = config.first_feed; feed; feed = feed->next_feed) {
85f07f22
 
55815edc
         if (avio_check(feed->feed_filename, AVIO_FLAG_READ) > 0) {
50f2dfad
             AVFormatContext *s = NULL;
59eb2ed1
             int matches = 0;
 
ae2ed20b
             /* See if it matches */
ddda9cee
 
ae2ed20b
             if (avformat_open_input(&s, feed->feed_filename, NULL, NULL) < 0) {
                 http_log("Deleting feed file '%s' as it appears "
                             "to be corrupt\n",
                          feed->feed_filename);
                 goto drop;
             }
59eb2ed1
 
ae2ed20b
             /* set buffer size */
             if (ffio_set_buf_size(s->pb, FFM_PACKET_SIZE) < 0) {
                 http_log("Failed to set buffer size\n");
                 avformat_close_input(&s);
                 goto bail;
             }
 
             /* Now see if it matches */
             if (s->nb_streams != feed->nb_streams) {
                 http_log("Deleting feed file '%s' as stream counts "
                             "differ (%d != %d)\n",
                          feed->feed_filename, s->nb_streams, feed->nb_streams);
                 goto drop;
             }
 
             matches = 1;
             for(i=0;i<s->nb_streams;i++) {
                 AVStream *sf, *ss;
 
                 sf = feed->streams[i];
                 ss = s->streams[i];
59eb2ed1
 
ae2ed20b
                 if (sf->index != ss->index || sf->id != ss->id) {
                     http_log("Index & Id do not match for stream %d (%s)\n",
                              i, feed->feed_filename);
                     matches = 0;
                     break;
                 }
 
                 matches = check_codec_match (sf->codec, ss->codec, i);
                 if (!matches)
                     break;
             }
 
 drop:
             if (s)
cd3716b9
                 avformat_close_input(&s);
611c5741
 
e322ea48
             if (!matches) {
                 if (feed->readonly) {
ae2ed20b
                     http_log("Unable to delete read-only feed file '%s'\n",
                              feed->feed_filename);
532a2833
                     goto bail;
e322ea48
                 }
59eb2ed1
                 unlink(feed->feed_filename);
e322ea48
             }
59eb2ed1
         }
ae2ed20b
 
55815edc
         if (avio_check(feed->feed_filename, AVIO_FLAG_WRITE) <= 0) {
c18cfd10
             AVFormatContext *s = avformat_alloc_context();
85f07f22
 
d6039063
             if (!s) {
                 http_log("Failed to allocate context\n");
532a2833
                 goto bail;
d6039063
             }
 
e322ea48
             if (feed->readonly) {
ae2ed20b
                 http_log("Unable to create feed file '%s' as it is "
                             "marked readonly\n",
                          feed->feed_filename);
                 avformat_free_context(s);
532a2833
                 goto bail;
e322ea48
             }
 
85f07f22
             /* only write the header of the ffm file */
59d96941
             if (avio_open(&s->pb, feed->feed_filename, AVIO_FLAG_WRITE) < 0) {
b4befb99
                 http_log("Could not open output feed file '%s'\n",
                          feed->feed_filename);
ae2ed20b
                 avformat_free_context(s);
532a2833
                 goto bail;
85f07f22
             }
bd7cf6ad
             s->oformat = feed->fmt;
85f07f22
             s->nb_streams = feed->nb_streams;
840238b8
             s->streams = feed->streams;
50f2dfad
             if (avformat_write_header(s, NULL) < 0) {
f1b6c142
                 http_log("Container doesn't support the required parameters\n");
ae2ed20b
                 avio_closep(&s->pb);
d970f7ba
                 s->streams = NULL;
                 s->nb_streams = 0;
ae2ed20b
                 avformat_free_context(s);
532a2833
                 goto bail;
f75cdda7
             }
0c5a6ef8
             /* XXX: need better API */
bd7cf6ad
             av_freep(&s->priv_data);
eb3e661b
             avio_closep(&s->pb);
c18cfd10
             s->streams = NULL;
             s->nb_streams = 0;
             avformat_free_context(s);
85f07f22
         }
ae2ed20b
 
85f07f22
         /* get feed size and write index */
         fd = open(feed->feed_filename, O_RDONLY);
         if (fd < 0) {
b4befb99
             http_log("Could not open output feed file '%s'\n",
85f07f22
                     feed->feed_filename);
532a2833
             goto bail;
85f07f22
         }
 
ae2ed20b
         feed->feed_write_index = FFMAX(ffm_read_write_index(fd),
                                        FFM_PACKET_SIZE);
85f07f22
         feed->feed_size = lseek(fd, 0, SEEK_END);
         /* ensure that we do not wrap before the end of file */
6b0bdc75
         if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
85f07f22
             feed->feed_max_size = feed->feed_size;
 
         close(fd);
     }
532a2833
     return 0;
 
 bail:
     return -1;
85f07f22
 }
 
6edd6884
 /* compute the bandwidth used by each stream */
 static void compute_bandwidth(void)
 {
177d2564
     unsigned bandwidth;
     int i;
f524d2e4
     FFServerStream *stream;
115329f1
 
f524d2e4
     for(stream = config.first_stream; stream; stream = stream->next) {
6edd6884
         bandwidth = 0;
         for(i=0;i<stream->nb_streams;i++) {
             AVStream *st = stream->streams[i];
01f4895c
             switch(st->codec->codec_type) {
72415b2a
             case AVMEDIA_TYPE_AUDIO:
             case AVMEDIA_TYPE_VIDEO:
01f4895c
                 bandwidth += st->codec->bit_rate;
6edd6884
                 break;
             default:
                 break;
             }
         }
         stream->bandwidth = (bandwidth + 999) / 1000;
     }
 }
 
5eb765ef
 static void handle_child_exit(int sig)
 {
     pid_t pid;
0bdf84b6
     int status;
     time_t uptime;
5eb765ef
 
     while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
f524d2e4
         FFServerStream *feed;
5eb765ef
 
f524d2e4
         for (feed = config.first_feed; feed; feed = feed->next) {
08724da9
             if (feed->pid != pid)
                 continue;
5eb765ef
 
08724da9
             uptime = time(0) - feed->pid_start;
             feed->pid = 0;
             fprintf(stderr,
0bdf84b6
                     "%s: Pid %"PRId64" exited with status %d after %"PRId64" "
                         "seconds\n",
e5c16e38
                     feed->filename, (int64_t) pid, status, (int64_t)uptime);
5eb765ef
 
08724da9
             if (uptime < 30)
                 /* Turn off any more restarts */
                 ffserver_free_child_args(&feed->child_argv);
5eb765ef
         }
     }
 
     need_to_start_children = 1;
 }
 
cf2c671f
 static void opt_debug(void)
5a635bc7
 {
f524d2e4
     config.debug = 1;
     snprintf(config.logfilename, sizeof(config.logfilename), "-");
5a635bc7
 }
 
a3ad68d3
 void show_help_default(const char *opt, const char *arg)
5a635bc7
 {
10ba7404
     printf("usage: ffserver [options]\n"
5a635bc7
            "Hyper fast multi format Audio/Video streaming server\n");
     printf("\n");
f9fada27
     show_help_options(options, "Main options:", 0, 0, 0);
5a635bc7
 }
 
 static const OptionDef options[] = {
992f8eae
 #include "cmdutils_common_opts.h"
5a635bc7
     { "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
     { "d", 0, {(void*)opt_debug}, "enable debug mode" },
f524d2e4
     { "f", HAS_ARG | OPT_STRING, {(void*)&config.filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
5a635bc7
     { NULL },
 };
 
85f07f22
 int main(int argc, char **argv)
 {
a92be9b8
     struct sigaction sigact = { { 0 } };
532a2833
     int cfg_parsed;
     int ret = EXIT_FAILURE;
 
3bf142c7
     init_dynload();
85f07f22
 
f524d2e4
     config.filename = av_strdup("/etc/ffserver.conf");
612a5049
 
182cbe43
     parse_loglevel(argc, argv, options);
2c4ae653
     av_register_all();
776f2bb9
     avformat_network_init();
85f07f22
 
452406bd
     show_banner(argc, argv, options);
3578e9a0
 
cde25790
     my_program_name = argv[0];
115329f1
 
7cc8d638
     parse_options(NULL, argc, argv, options, NULL);
85f07f22
 
59c2959d
     unsetenv("http_proxy");             /* Kill the http_proxy */
cde25790
 
576fb48e
     av_lfg_init(&random_state, av_get_random_seed());
8256c0a3
 
5eb765ef
     sigact.sa_handler = handle_child_exit;
     sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
     sigaction(SIGCHLD, &sigact, 0);
 
532a2833
     if ((cfg_parsed = ffserver_parse_ffconfig(config.filename, &config)) < 0) {
04702a0d
         fprintf(stderr, "Error reading configuration file '%s': %s\n",
532a2833
                 config.filename, av_err2str(cfg_parsed));
         goto bail;
85f07f22
     }
 
f10d55ed
     /* open log file if needed */
f524d2e4
     if (config.logfilename[0] != '\0') {
         if (!strcmp(config.logfilename, "-"))
a9d9aa36
             logfile = stdout;
f10d55ed
         else
f524d2e4
             logfile = fopen(config.logfilename, "a");
f10d55ed
         av_log_set_callback(http_av_log);
     }
 
2effd274
     build_file_streams();
 
532a2833
     if (build_feed_streams() < 0) {
         http_log("Could not setup feed streams\n");
         goto bail;
     }
85f07f22
 
6edd6884
     compute_bandwidth();
 
85f07f22
     /* signal init */
     signal(SIGPIPE, SIG_IGN);
 
2effd274
     if (http_server() < 0) {
acae1492
         http_log("Could not start server\n");
532a2833
         goto bail;
85f07f22
     }
 
532a2833
     ret=EXIT_SUCCESS;
 
 bail:
     av_freep (&config.filename);
     avformat_network_deinit();
     return ret;
85f07f22
 }