]> git.sesse.net Git - ffmpeg/blobdiff - ffserver.c
typo
[ffmpeg] / ffserver.c
index 144e100ee9c0bc3f7b8b7521886c2fa4733ed186..8f9bba6b49c9cc610ab4519eed21d2948d4a9591 100644 (file)
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
+#ifdef HAVE_SYS_POLL_H
 #include <sys/poll.h>
+#endif
 #include <errno.h>
 #include <sys/time.h>
 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
 #include <time.h>
-#include <sys/types.h>
-#include <sys/socket.h>
 #include <sys/wait.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
 #include <signal.h>
-#ifdef CONFIG_HAVE_DLFCN
+#ifdef HAVE_DLFCN_H
 #include <dlfcn.h>
 #endif
 
+#include "network.h"
 #include "version.h"
 #include "ffserver.h"
+#include "random.h"
+
+#undef exit
 
 /* maximum number of simultaneous HTTP connections */
 #define HTTP_MAX_CONNECTIONS 2000
@@ -80,9 +81,6 @@ const char *http_state[] = {
 
 #define IOBUFFER_INIT_SIZE 8192
 
-/* coef for exponential mean for bitrate estimation in statistics */
-#define AVG_COEF 0.9
-
 /* timeouts are in ms */
 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
@@ -91,7 +89,7 @@ const char *http_state[] = {
 
 typedef struct {
     int64_t count1, count2;
-    long time1, time2;
+    int64_t time1, time2;
 } DataRateData;
 
 /* context associated with one connection */
@@ -100,7 +98,7 @@ typedef struct HTTPContext {
     int fd; /* socket file descriptor */
     struct sockaddr_in from_addr; /* origin */
     struct pollfd *poll_entry; /* used when polling */
-    long timeout;
+    int64_t timeout;
     uint8_t *buffer_ptr, *buffer_end;
     int http_error;
     int post;
@@ -111,7 +109,7 @@ typedef struct HTTPContext {
     int feed_fd;
     /* input format handling */
     AVFormatContext *fmt_in;
-    long start_time;            /* In milliseconds - this wraps fairly often */
+    int64_t start_time;            /* In milliseconds - this wraps fairly often */
     int64_t first_pts;            /* initial pts value */
     int64_t cur_pts;             /* current pts value from the stream in us */
     int64_t cur_frame_duration;  /* duration of the current frame in us */
@@ -191,7 +189,7 @@ typedef struct FFStream {
     IPAddressACL *acl;
     int nb_streams;
     int prebuffer;      /* Number of millseconds early to start */
-    long max_time;      /* Number of milliseconds to run */
+    int64_t max_time;      /* Number of milliseconds to run */
     int send_on_key;
     AVStream *streams[MAX_STREAMS];
     int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
@@ -232,8 +230,8 @@ typedef struct FeedData {
     float avg_frame_size;   /* frame size averraged over last frames with exponential mean */
 } FeedData;
 
-struct sockaddr_in my_http_addr;
-struct sockaddr_in my_rtsp_addr;
+static struct sockaddr_in my_http_addr;
+static struct sockaddr_in my_rtsp_addr;
 
 static char logfilename[1024];
 static HTTPContext *first_http_ctx;
@@ -287,15 +285,9 @@ static int nb_connections;
 static int max_bandwidth;
 static int current_bandwidth;
 
-static long cur_time;           // Making this global saves on passing it around everywhere
+static int64_t cur_time;           // Making this global saves on passing it around everywhere
 
-static long gettime_ms(void)
-{
-    struct timeval tv;
-
-    gettimeofday(&tv,NULL);
-    return (long long)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
-}
+static AVRandomState random_state;
 
 static FILE *logfile = NULL;
 
@@ -439,16 +431,16 @@ static int socket_open_listen(struct sockaddr_in *my_addr)
         char bindmsg[32];
         snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
         perror (bindmsg);
-        close(server_fd);
+        closesocket(server_fd);
         return -1;
     }
 
     if (listen (server_fd, 5) < 0) {
         perror ("listen");
-        close(server_fd);
+        closesocket(server_fd);
         return -1;
     }
-    fcntl(server_fd, F_SETFL, O_NONBLOCK);
+    ff_socket_nonblock(server_fd, 1);
 
     return server_fd;
 }
@@ -466,8 +458,8 @@ static void start_multicast(void)
     for(stream = first_stream; stream != NULL; stream = stream->next) {
         if (stream->is_multicast) {
             /* open the RTP connection */
-            snprintf(session_id, sizeof(session_id),
-                     "%08x%08x", (int)random(), (int)random());
+            snprintf(session_id, sizeof(session_id), "%08x%08x",
+                     av_random(&random_state), av_random(&random_state));
 
             /* choose a port if none given */
             if (stream->multicast_port == 0) {
@@ -596,11 +588,12 @@ static int http_server(void)
            second to handle timeouts */
         do {
             ret = poll(poll_table, poll_entry - poll_table, delay);
-            if (ret < 0 && errno != EAGAIN && errno != EINTR)
+            if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
+                ff_neterrno() != FF_NETERROR(EINTR))
                 return -1;
-        } while (ret <= 0);
+        } while (ret < 0);
 
-        cur_time = gettime_ms();
+        cur_time = av_gettime() / 1000;
 
         if (need_to_start_children) {
             need_to_start_children = 0;
@@ -656,7 +649,7 @@ static void new_connection(int server_fd, int is_rtsp)
                 &len);
     if (fd < 0)
         return;
-    fcntl(fd, F_SETFL, O_NONBLOCK);
+    ff_socket_nonblock(fd, 1);
 
     /* XXX: should output a warning page when coming
        close to the connection limit */
@@ -689,7 +682,7 @@ static void new_connection(int server_fd, int is_rtsp)
         av_free(c->buffer);
         av_free(c);
     }
-    close(fd);
+    closesocket(fd);
 }
 
 static void close_connection(HTTPContext *c)
@@ -719,7 +712,7 @@ static void close_connection(HTTPContext *c)
 
     /* remove connection associated resources */
     if (c->fd >= 0)
-        close(c->fd);
+        closesocket(c->fd);
     if (c->fmt_in) {
         /* close each frame parser */
         for(i=0;i<c->fmt_in->nb_streams;i++) {
@@ -765,6 +758,13 @@ static void close_connection(HTTPContext *c)
 
     if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
         current_bandwidth -= c->stream->bandwidth;
+
+    /* 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);
+    }
+
     av_freep(&c->pb_buffer);
     av_freep(&c->packet_buffer);
     av_free(c->buffer);
@@ -790,9 +790,10 @@ static int handle_connection(HTTPContext *c)
             return 0;
         /* read the data */
     read_loop:
-        len = read(c->fd, c->buffer_ptr, 1);
+        len = recv(c->fd, c->buffer_ptr, 1, 0);
         if (len < 0) {
-            if (errno != EAGAIN && errno != EINTR)
+            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
+                ff_neterrno() != FF_NETERROR(EINTR))
                 return -1;
         } else if (len == 0) {
             return -1;
@@ -825,9 +826,10 @@ static int handle_connection(HTTPContext *c)
         /* no need to write if no events */
         if (!(c->poll_entry->revents & POLLOUT))
             return 0;
-        len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
+        len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
         if (len < 0) {
-            if (errno != EAGAIN && errno != EINTR) {
+            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
+                ff_neterrno() != FF_NETERROR(EINTR)) {
                 /* error : close connection */
                 av_freep(&c->pb_buffer);
                 return -1;
@@ -866,6 +868,9 @@ static int handle_connection(HTTPContext *c)
         }
         if (http_send_data(c) < 0)
             return -1;
+        /* close connection if trailer sent */
+        if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
+            return -1;
         break;
     case HTTPSTATE_RECEIVE_DATA:
         /* no need to read if no events */
@@ -892,9 +897,10 @@ static int handle_connection(HTTPContext *c)
         /* no need to write if no events */
         if (!(c->poll_entry->revents & POLLOUT))
             return 0;
-        len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
+        len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
         if (len < 0) {
-            if (errno != EAGAIN && errno != EINTR) {
+            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
+                ff_neterrno() != FF_NETERROR(EINTR)) {
                 /* error : close connection */
                 av_freep(&c->pb_buffer);
                 return -1;
@@ -917,10 +923,11 @@ static int handle_connection(HTTPContext *c)
         /* no need to write if no events */
         if (!(c->poll_entry->revents & POLLOUT))
             return 0;
-        len = write(c->fd, c->packet_buffer_ptr,
-                    c->packet_buffer_end - c->packet_buffer_ptr);
+        len = send(c->fd, c->packet_buffer_ptr,
+                    c->packet_buffer_end - c->packet_buffer_ptr, 0);
         if (len < 0) {
-            if (errno != EAGAIN && errno != EINTR) {
+            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
+                ff_neterrno() != FF_NETERROR(EINTR)) {
                 /* error : close connection */
                 av_freep(&c->packet_buffer);
                 return -1;
@@ -1114,7 +1121,7 @@ static int validate_acl(FFStream *stream, HTTPContext *c)
     enum IPAddressAction last_action = IP_DENY;
     IPAddressACL *acl;
     struct in_addr *src = &c->from_addr.sin_addr;
-    unsigned long src_addr = ntohl(src->s_addr);
+    unsigned long src_addr = src->s_addr;
 
     for (acl = stream->acl; acl; acl = acl->next) {
         if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr) {
@@ -1168,7 +1175,7 @@ static int http_parse_request(HTTPContext *c)
     char *p;
     enum RedirType redir_type;
     char cmd[32];
-    char info[1024], *filename;
+    char info[1024], filename[1024];
     char url[1024], *q;
     char protocol[32];
     char msg[1024];
@@ -1202,11 +1209,7 @@ static int http_parse_request(HTTPContext *c)
         http_log("New connection: %s %s\n", cmd, url);
 
     /* find the filename and the optional info string in the request */
-    p = url;
-    if (*p == '/')
-        p++;
-    filename = p;
-    p = strchr(p, '?');
+    p = strchr(url, '?');
     if (p) {
         pstrcpy(info, sizeof(info), p);
         *p = '\0';
@@ -1214,6 +1217,8 @@ static int http_parse_request(HTTPContext *c)
         info[0] = '\0';
     }
 
+    pstrcpy(filename, sizeof(filename)-1, url + ((*url == '/') ? 1 : 0));
+
     for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
         if (strncasecmp(p, "User-Agent:", 11) == 0) {
             useragent = p + 11;
@@ -1241,12 +1246,16 @@ static int http_parse_request(HTTPContext *c)
         strcpy(filename + strlen(filename)-2, "m");
     } else if (match_ext(filename, "rtsp")) {
         redir_type = REDIR_RTSP;
-        compute_real_filename(filename, sizeof(url) - 1);
+        compute_real_filename(filename, sizeof(filename) - 1);
     } else if (match_ext(filename, "sdp")) {
         redir_type = REDIR_SDP;
-        compute_real_filename(filename, sizeof(url) - 1);
+        compute_real_filename(filename, sizeof(filename) - 1);
     }
 
+    // "redirect" / request to index.html
+    if (!strlen(filename))
+        pstrcpy(filename, sizeof(filename) - 1, "index.html");
+
     stream = first_stream;
     while (stream != NULL) {
         if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
@@ -1290,6 +1299,12 @@ static int http_parse_request(HTTPContext *c)
         }
     }
 
+    /* If already streaming this feed, dont let start an another feeder */
+    if (stream->feed_opened) {
+        snprintf(msg, sizeof(msg), "This feed is already being received.");
+        goto send_error;
+    }
+
     if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE) {
         current_bandwidth += stream->bandwidth;
     }
@@ -1527,14 +1542,14 @@ static int http_parse_request(HTTPContext *c)
     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
     mime_type = c->stream->fmt->mime_type;
     if (!mime_type)
-        mime_type = "application/x-octet_stream";
+        mime_type = "application/x-octet-stream";
     q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
 
     /* for asf, we need extra headers */
     if (!strcmp(c->stream->fmt->name,"asf_stream")) {
         /* Need to allocate a client id */
 
-        c->wmp_client_id = random() & 0x7fffffff;
+        c->wmp_client_id = av_random(&random_state) & 0x7fffffff;
 
         q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=%d\r\nPragma: features=\"broadcast\"\r\n", c->wmp_client_id);
     }
@@ -1751,6 +1766,7 @@ static void compute_stats(HTTPContext *c)
                 switch(st->codec->codec_type) {
                 case CODEC_TYPE_AUDIO:
                     type = "audio";
+                    snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
                     break;
                 case CODEC_TYPE_VIDEO:
                     type = "video";
@@ -1790,7 +1806,7 @@ static void compute_stats(HTTPContext *c)
                 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
                 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
                     avg /= enc->frame_size;
-                url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n",
+                url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
                              buf, enc->frame_number, fdata->data_count, avg / 1000.0);
             }
             url_fprintf(pb, "</TABLE>\n");
@@ -1912,7 +1928,7 @@ static int open_input_stream(HTTPContext *c, const char *info)
 
 #if 0
     { time_t when = stream_pos / 1000000;
-    http_log("Stream pos = %lld, time=%s", stream_pos, ctime(&when));
+    http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
     }
 #endif
 
@@ -1953,7 +1969,7 @@ static int open_input_stream(HTTPContext *c, const char *info)
 static int64_t get_server_clock(HTTPContext *c)
 {
     /* compute current pts value from system time */
-    return (int64_t)(cur_time - c->start_time) * 1000LL;
+    return (cur_time - c->start_time) * 1000;
 }
 
 /* return the estimated time at which the current packet must be sent
@@ -2026,7 +2042,8 @@ static int http_prepare_data(HTTPContext *c)
         c->fmt_ctx.pb.is_streamed = 1;
 
         av_set_parameters(&c->fmt_ctx, NULL);
-        av_write_header(&c->fmt_ctx);
+        if (av_write_header(&c->fmt_ctx) < 0)
+            return -1;
 
         len = url_close_dyn_buf(&c->fmt_ctx.pb, &c->pb_buffer);
         c->buffer_ptr = c->pb_buffer;
@@ -2167,6 +2184,14 @@ static int http_prepare_data(HTTPContext *c)
                             /* XXX: potential leak */
                             return -1;
                         }
+                        if (pkt.dts != AV_NOPTS_VALUE)
+                            pkt.dts = av_rescale_q(pkt.dts,
+                                c->fmt_in->streams[pkt.stream_index]->time_base,
+                                ctx->streams[pkt.stream_index]->time_base);
+                        if (pkt.pts != AV_NOPTS_VALUE)
+                            pkt.pts = av_rescale_q(pkt.pts,
+                                c->fmt_in->streams[pkt.stream_index]->time_base,
+                                ctx->streams[pkt.stream_index]->time_base);
                         if (av_write_frame(ctx, &pkt)) {
                             c->state = HTTPSTATE_SEND_DATA_TRAILER;
                         }
@@ -2207,9 +2232,6 @@ static int http_prepare_data(HTTPContext *c)
     return 0;
 }
 
-/* in bit/s */
-#define SHORT_TERM_BANDWIDTH 8000000
-
 /* should convert the format at the same time */
 /* send data starting at c->buffer_ptr to the output connection
    (either UDP or TCP connection) */
@@ -2289,8 +2311,8 @@ static int http_send_data(HTTPContext *c)
                     c->buffer_ptr += len;
 
                     /* send everything we can NOW */
-                    len = write(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
-                                rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr);
+                    len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
+                                rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
                     if (len > 0) {
                         rtsp_c->packet_buffer_ptr += len;
                     }
@@ -2314,9 +2336,10 @@ static int http_send_data(HTTPContext *c)
                 }
             } else {
                 /* TCP data output */
-                len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
+                len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
                 if (len < 0) {
-                    if (errno != EAGAIN && errno != EINTR) {
+                    if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
+                        ff_neterrno() != FF_NETERROR(EINTR)) {
                         /* error : close connection */
                         return -1;
                     } else {
@@ -2371,9 +2394,10 @@ static int http_receive_data(HTTPContext *c)
     if (c->buffer_end > c->buffer_ptr) {
         int len;
 
-        len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
+        len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
         if (len < 0) {
-            if (errno != EAGAIN && errno != EINTR) {
+            if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
+                ff_neterrno() != FF_NETERROR(EINTR)) {
                 /* error : close connection */
                 goto fail;
             }
@@ -2401,7 +2425,7 @@ static int http_receive_data(HTTPContext *c)
            if header */
         if (c->data_count > FFM_PACKET_SIZE) {
 
-            //            printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
+            //            printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
             /* XXX: use llseek or url_seek */
             lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
             write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
@@ -2487,9 +2511,39 @@ static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
     char buf2[32];
 
     switch(error_number) {
-#define DEF(n, c, s) case c: str = s; break;
-#include "rtspcodes.h"
-#undef DEF
+    case RTSP_STATUS_OK:
+        str = "OK";
+        break;
+    case RTSP_STATUS_METHOD:
+        str = "Method Not Allowed";
+        break;
+    case RTSP_STATUS_BANDWIDTH:
+        str = "Not Enough Bandwidth";
+        break;
+    case RTSP_STATUS_SESSION:
+        str = "Session Not Found";
+        break;
+    case RTSP_STATUS_STATE:
+        str = "Method Not Valid in This State";
+        break;
+    case RTSP_STATUS_AGGREGATE:
+        str = "Aggregate operation not allowed";
+        break;
+    case RTSP_STATUS_ONLY_AGGREGATE:
+        str = "Only aggregate operation allowed";
+        break;
+    case RTSP_STATUS_TRANSPORT:
+        str = "Unsupported transport";
+        break;
+    case RTSP_STATUS_INTERNAL:
+        str = "Internal Server Error";
+        break;
+    case RTSP_STATUS_SERVICE:
+        str = "Service Unavailable";
+        break;
+    case RTSP_STATUS_VERSION:
+        str = "RTSP Version not supported";
+        break;
     default:
         str = "Unknown Error";
         break;
@@ -2824,8 +2878,8 @@ static void rtsp_cmd_setup(HTTPContext *c, const char *url,
 
     /* generate session id if needed */
     if (h->session_id[0] == '\0') {
-        snprintf(h->session_id, sizeof(h->session_id),
-                 "%08x%08x", (int)random(), (int)random());
+        snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
+                 av_random(&random_state), av_random(&random_state));
     }
 
     /* find rtp session, and create it if none found */
@@ -2881,18 +2935,6 @@ static void rtsp_cmd_setup(HTTPContext *c, const char *url,
     dest_addr = rtp_c->from_addr;
     dest_addr.sin_port = htons(th->client_port_min);
 
-    /* add transport option if needed */
-    if (ff_rtsp_callback) {
-        setup.ipaddr = ntohl(dest_addr.sin_addr.s_addr);
-        if (ff_rtsp_callback(RTSP_ACTION_SERVER_SETUP, rtp_c->session_id,
-                             (char *)&setup, sizeof(setup),
-                             stream->rtsp_option) < 0) {
-            rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
-            return;
-        }
-        dest_addr.sin_addr.s_addr = htonl(setup.ipaddr);
-    }
-
     /* setup stream */
     if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
         rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
@@ -3023,6 +3065,7 @@ static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h)
 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
 {
     HTTPContext *rtp_c;
+    char session_id[32];
 
     rtp_c = find_rtp_session_with_url(url, h->session_id);
     if (!rtp_c) {
@@ -3030,19 +3073,15 @@ static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
         return;
     }
 
+    pstrcpy(session_id, sizeof(session_id), rtp_c->session_id);
+
     /* abort the session */
     close_connection(rtp_c);
 
-    if (ff_rtsp_callback) {
-        ff_rtsp_callback(RTSP_ACTION_SERVER_TEARDOWN, rtp_c->session_id,
-                         NULL, 0,
-                         rtp_c->stream->rtsp_option);
-    }
-
     /* now everything is OK, so we can send the connection parameters */
     rtsp_reply_header(c, RTSP_STATUS_OK);
     /* session ID */
-    url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
+    url_fprintf(c->pb, "Session: %s\r\n", session_id);
     url_fprintf(c->pb, "\r\n");
 }
 
@@ -3149,6 +3188,7 @@ static int rtp_new_av_stream(HTTPContext *c,
                c->stream->feed->streams[c->stream->feed_streams[stream_index]],
                sizeof(AVStream));
     }
+    st->priv_data = NULL;
 
     /* build destination RTP address */
     ipaddr = inet_ntoa(dest_addr->sin_addr);
@@ -3322,7 +3362,7 @@ static void extract_mpeg4_header(AVFormatContext *infile)
                 if (p[0] == 0x00 && p[1] == 0x00 &&
                     p[2] == 0x01 && p[3] == 0xb6) {
                     size = p - pkt.data;
-                    //                    av_hex_dump(pkt.data, size);
+                    //                    av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
                     st->codec->extradata = av_malloc(size);
                     st->codec->extradata_size = size;
                     memcpy(st->codec->extradata, pkt.data, size);
@@ -3518,7 +3558,10 @@ static void build_feed_streams(void)
                 s->streams[i] = st;
             }
             av_set_parameters(s, NULL);
-            av_write_header(s);
+            if (av_write_header(s) < 0) {
+                fprintf(stderr, "Container doesn't supports the required parameters\n");
+                exit(1);
+            }
             /* XXX: need better api */
             av_freep(&s->priv_data);
             url_fclose(&s->pb);
@@ -3706,7 +3749,7 @@ static int opt_video_codec(const char *arg)
 
 /* simplistic plugin support */
 
-#ifdef CONFIG_HAVE_DLOPEN
+#ifdef HAVE_DLOPEN
 static void load_module(const char *filename)
 {
     void *dll;
@@ -3774,11 +3817,17 @@ static int parse_ffconfig(const char *filename)
 
         if (!strcasecmp(cmd, "Port")) {
             get_arg(arg, sizeof(arg), &p);
-            my_http_addr.sin_port = htons (atoi(arg));
+            val = atoi(arg);
+            if (val < 1 || val > 65536) {
+                fprintf(stderr, "%s:%d: Invalid port: %s\n",
+                        filename, line_num, arg);
+                errors++;
+            }
+            my_http_addr.sin_port = htons(val);
         } else if (!strcasecmp(cmd, "BindAddress")) {
             get_arg(arg, sizeof(arg), &p);
-            if (!inet_aton(arg, &my_http_addr.sin_addr)) {
-                fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
+            if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
+                fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
                         filename, line_num, arg);
                 errors++;
             }
@@ -3786,11 +3835,17 @@ static int parse_ffconfig(const char *filename)
             ffserver_daemon = 0;
         } else if (!strcasecmp(cmd, "RTSPPort")) {
             get_arg(arg, sizeof(arg), &p);
-            my_rtsp_addr.sin_port = htons (atoi(arg));
+            val = atoi(arg);
+            if (val < 1 || val > 65536) {
+                fprintf(stderr, "%s:%d: Invalid port: %s\n",
+                        filename, line_num, arg);
+                errors++;
+            }
+            my_rtsp_addr.sin_port = htons(atoi(arg));
         } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
             get_arg(arg, sizeof(arg), &p);
-            if (!inet_aton(arg, &my_rtsp_addr.sin_addr)) {
-                fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
+            if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
+                fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
                         filename, line_num, arg);
                 errors++;
             }
@@ -3851,14 +3906,11 @@ static int parse_ffconfig(const char *filename)
                 feed->child_argv = (char **) av_mallocz(64 * sizeof(char *));
 
                 for (i = 0; i < 62; i++) {
-                    char argbuf[256];
-
-                    get_arg(argbuf, sizeof(argbuf), &p);
-                    if (!argbuf[0])
+                    get_arg(arg, sizeof(arg), &p);
+                    if (!arg[0])
                         break;
 
-                    feed->child_argv[i] = av_malloc(strlen(argbuf) + 1);
-                    strcpy(feed->child_argv[i], argbuf);
+                    feed->child_argv[i] = av_strdup(arg);
                 }
 
                 feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
@@ -3917,16 +3969,6 @@ static int parse_ffconfig(const char *filename)
                 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
                         filename, line_num);
                 errors++;
-#if 0
-            } else {
-                /* Make sure that we start out clean */
-                if (unlink(feed->feed_filename) < 0
-                    && errno != ENOENT) {
-                    fprintf(stderr, "%s:%d: Unable to clean old feed file '%s': %s\n",
-                        filename, line_num, feed->feed_filename, strerror(errno));
-                    errors++;
-                }
-#endif
             }
             feed = NULL;
         } else if (!strcasecmp(cmd, "<Stream")) {
@@ -3995,6 +4037,7 @@ static int parse_ffconfig(const char *filename)
                 video_id = stream->fmt->video_codec;
             }
         } else if (!strcasecmp(cmd, "InputFormat")) {
+            get_arg(arg, sizeof(arg), &p);
             stream->ifmt = av_find_input_format(arg);
             if (!stream->ifmt) {
                 fprintf(stderr, "%s:%d: Unknown input format: %s\n",
@@ -4149,6 +4192,11 @@ static int parse_ffconfig(const char *filename)
                 video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
                 video_enc.flags |= CODEC_FLAG_4MV;
             }
+        } else if (!strcasecmp(cmd, "VideoTag")) {
+            get_arg(arg, sizeof(arg), &p);
+            if ((strlen(arg) == 4) && stream) {
+                video_enc.codec_tag = ff_get_fourcc(arg);
+            }
         } else if (!strcasecmp(cmd, "BitExact")) {
             if (stream) {
                 video_enc.flags |= CODEC_FLAG_BITEXACT;
@@ -4223,7 +4271,6 @@ static int parse_ffconfig(const char *filename)
             audio_id = CODEC_ID_NONE;
         } else if (!strcasecmp(cmd, "ACL")) {
             IPAddressACL acl;
-            struct hostent *he;
 
             get_arg(arg, sizeof(arg), &p);
             if (strcasecmp(arg, "allow") == 0) {
@@ -4238,28 +4285,21 @@ static int parse_ffconfig(const char *filename)
 
             get_arg(arg, sizeof(arg), &p);
 
-            he = gethostbyname(arg);
-            if (!he) {
+            if (resolve_host(&acl.first, arg) != 0) {
                 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
                         filename, line_num, arg);
                 errors++;
             } else {
-                /* Only take the first */
-                acl.first.s_addr = ntohl(((struct in_addr *) he->h_addr_list[0])->s_addr);
                 acl.last = acl.first;
             }
 
             get_arg(arg, sizeof(arg), &p);
 
             if (arg[0]) {
-                he = gethostbyname(arg);
-                if (!he) {
+                if (resolve_host(&acl.last, arg) != 0) {
                     fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
                             filename, line_num, arg);
                     errors++;
-                } else {
-                    /* Only take the first */
-                    acl.last.s_addr = ntohl(((struct in_addr *) he->h_addr_list[0])->s_addr);
                 }
             }
 
@@ -4267,8 +4307,8 @@ static int parse_ffconfig(const char *filename)
                 IPAddressACL *nacl = (IPAddressACL *) av_mallocz(sizeof(*nacl));
                 IPAddressACL **naclp = 0;
 
+                acl.next = 0;
                 *nacl = acl;
-                nacl->next = 0;
 
                 if (stream) {
                     naclp = &stream->acl;
@@ -4291,17 +4331,13 @@ static int parse_ffconfig(const char *filename)
             get_arg(arg, sizeof(arg), &p);
             if (stream) {
                 av_freep(&stream->rtsp_option);
-                /* XXX: av_strdup ? */
-                stream->rtsp_option = av_malloc(strlen(arg) + 1);
-                if (stream->rtsp_option) {
-                    strcpy(stream->rtsp_option, arg);
-                }
+                stream->rtsp_option = av_strdup(arg);
             }
         } else if (!strcasecmp(cmd, "MulticastAddress")) {
             get_arg(arg, sizeof(arg), &p);
             if (stream) {
-                if (!inet_aton(arg, &stream->multicast_ip)) {
-                    fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
+                if (resolve_host(&stream->multicast_ip, arg) != 0) {
+                    fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
                             filename, line_num, arg);
                     errors++;
                 }
@@ -4377,7 +4413,7 @@ static int parse_ffconfig(const char *filename)
             redirect = NULL;
         } else if (!strcasecmp(cmd, "LoadModule")) {
             get_arg(arg, sizeof(arg), &p);
-#ifdef CONFIG_HAVE_DLOPEN
+#ifdef HAVE_DLOPEN
             load_module(arg);
 #else
             fprintf(stderr, "%s:%d: Module support not compiled into this version: '%s'\n",
@@ -4398,25 +4434,6 @@ static int parse_ffconfig(const char *filename)
         return 0;
 }
 
-
-#if 0
-static void write_packet(FFCodec *ffenc,
-                         uint8_t *buf, int size)
-{
-    PacketHeader hdr;
-    AVCodecContext *enc = &ffenc->enc;
-    uint8_t *wptr;
-    mk_header(&hdr, enc, size);
-    wptr = http_fifo.wptr;
-    fifo_write(&http_fifo, (uint8_t *)&hdr, sizeof(hdr), &wptr);
-    fifo_write(&http_fifo, buf, size, &wptr);
-    /* atomic modification of wptr */
-    http_fifo.wptr = wptr;
-    ffenc->data_count += size;
-    ffenc->avg_frame_size = ffenc->avg_frame_size * AVG_COEF + size * (1.0 - AVG_COEF);
-}
-#endif
-
 static void show_banner(void)
 {
     printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000-2006 Fabrice Bellard, et al.\n");
@@ -4523,7 +4540,7 @@ int main(int argc, char **argv)
 
     putenv("http_proxy");               /* Kill the http_proxy */
 
-    srandom(gettime_ms() + (getpid() << 16));
+    av_init_random(av_gettime() + (getpid() << 16), &random_state);
 
     /* address on which the server will handle HTTP connections */
     my_http_addr.sin_family = AF_INET;