2 * Multiple format streaming server
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef HAVE_CLOSESOCKET
24 #define closesocket close
28 #include "libavutil/random.h"
29 #include "libavutil/avstring.h"
30 #include "libavformat/avformat.h"
31 #include "libavformat/network.h"
32 #include "libavformat/os_support.h"
33 #include "libavformat/rtp.h"
34 #include "libavformat/rtsp.h"
39 #include <sys/ioctl.h>
45 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
57 const char program_name[] = "FFserver";
58 const int program_birth_year = 2000;
60 static const OptionDef options[];
62 /* maximum number of simultaneous HTTP connections */
63 #define HTTP_MAX_CONNECTIONS 2000
66 HTTPSTATE_WAIT_REQUEST,
67 HTTPSTATE_SEND_HEADER,
68 HTTPSTATE_SEND_DATA_HEADER,
69 HTTPSTATE_SEND_DATA, /* sending TCP or UDP data */
70 HTTPSTATE_SEND_DATA_TRAILER,
71 HTTPSTATE_RECEIVE_DATA,
72 HTTPSTATE_WAIT_FEED, /* wait for data from the feed */
75 RTSPSTATE_WAIT_REQUEST,
77 RTSPSTATE_SEND_PACKET,
80 const char *http_state[] = {
96 #define IOBUFFER_INIT_SIZE 8192
98 /* timeouts are in ms */
99 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
100 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
102 #define SYNC_TIMEOUT (10 * 1000)
105 int64_t count1, count2;
106 int64_t time1, time2;
109 /* context associated with one connection */
110 typedef struct HTTPContext {
111 enum HTTPState state;
112 int fd; /* socket file descriptor */
113 struct sockaddr_in from_addr; /* origin */
114 struct pollfd *poll_entry; /* used when polling */
116 uint8_t *buffer_ptr, *buffer_end;
119 struct HTTPContext *next;
120 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
124 /* input format handling */
125 AVFormatContext *fmt_in;
126 int64_t start_time; /* In milliseconds - this wraps fairly often */
127 int64_t first_pts; /* initial pts value */
128 int64_t cur_pts; /* current pts value from the stream in us */
129 int64_t cur_frame_duration; /* duration of the current frame in us */
130 int cur_frame_bytes; /* output frame size, needed to compute
131 the time at which we send each
133 int pts_stream_index; /* stream we choose as clock reference */
134 int64_t cur_clock; /* current clock reference value in us */
135 /* output format handling */
136 struct FFStream *stream;
137 /* -1 is invalid stream */
138 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
139 int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */
141 AVFormatContext fmt_ctx; /* instance of FFStream for one user */
142 int last_packet_sent; /* true if last data packet was sent */
144 DataRateData datarate;
151 int is_packetized; /* if true, the stream is packetized */
152 int packet_stream_index; /* current stream for output in state machine */
154 /* RTSP state specific */
155 uint8_t *pb_buffer; /* XXX: use that in all the code */
157 int seq; /* RTSP sequence number */
159 /* RTP state specific */
160 enum RTSPProtocol rtp_protocol;
161 char session_id[32]; /* session id */
162 AVFormatContext *rtp_ctx[MAX_STREAMS];
164 /* RTP/UDP specific */
165 URLContext *rtp_handles[MAX_STREAMS];
167 /* RTP/TCP specific */
168 struct HTTPContext *rtsp_c;
169 uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
172 /* each generated stream is described here */
176 STREAM_TYPE_REDIRECT,
179 enum IPAddressAction {
184 typedef struct IPAddressACL {
185 struct IPAddressACL *next;
186 enum IPAddressAction action;
187 /* These are in host order */
188 struct in_addr first;
192 /* description of each stream of the ffserver.conf file */
193 typedef struct FFStream {
194 enum StreamType stream_type;
195 char filename[1024]; /* stream filename */
196 struct FFStream *feed; /* feed we are using (can be null if
198 AVFormatParameters *ap_in; /* input parameters */
199 AVInputFormat *ifmt; /* if non NULL, force input format */
203 int prebuffer; /* Number of millseconds early to start */
204 int64_t max_time; /* Number of milliseconds to run */
206 AVStream *streams[MAX_STREAMS];
207 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
208 char feed_filename[1024]; /* file name of the feed storage, or
209 input file name for a stream */
214 pid_t pid; /* Of ffmpeg process */
215 time_t pid_start; /* Of ffmpeg process */
217 struct FFStream *next;
218 int bandwidth; /* bandwidth, in kbits/s */
221 /* multicast specific */
223 struct in_addr multicast_ip;
224 int multicast_port; /* first port used for multicast */
226 int loop; /* if true, send the stream in loops (only meaningful if file) */
229 int feed_opened; /* true if someone is writing to the feed */
230 int is_feed; /* true if it is a feed */
231 int readonly; /* True if writing is prohibited to the file */
233 int64_t bytes_served;
234 int64_t feed_max_size; /* maximum storage size, zero means unlimited */
235 int64_t feed_write_index; /* current write position in feed (it wraps around) */
236 int64_t feed_size; /* current size of feed */
237 struct FFStream *next_feed;
240 typedef struct FeedData {
241 long long data_count;
242 float avg_frame_size; /* frame size averaged over last frames with exponential mean */
245 static struct sockaddr_in my_http_addr;
246 static struct sockaddr_in my_rtsp_addr;
248 static char logfilename[1024];
249 static HTTPContext *first_http_ctx;
250 static FFStream *first_feed; /* contains only feeds */
251 static FFStream *first_stream; /* contains all streams, including feeds */
253 static void new_connection(int server_fd, int is_rtsp);
254 static void close_connection(HTTPContext *c);
257 static int handle_connection(HTTPContext *c);
258 static int http_parse_request(HTTPContext *c);
259 static int http_send_data(HTTPContext *c);
260 static void compute_stats(HTTPContext *c);
261 static int open_input_stream(HTTPContext *c, const char *info);
262 static int http_start_receive_data(HTTPContext *c);
263 static int http_receive_data(HTTPContext *c);
266 static int rtsp_parse_request(HTTPContext *c);
267 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
268 static void rtsp_cmd_options(HTTPContext *c, const char *url);
269 static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPHeader *h);
270 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h);
271 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h);
272 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h);
275 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
276 struct in_addr my_ip);
279 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
280 FFStream *stream, const char *session_id,
281 enum RTSPProtocol rtp_protocol);
282 static int rtp_new_av_stream(HTTPContext *c,
283 int stream_index, struct sockaddr_in *dest_addr,
284 HTTPContext *rtsp_c);
286 static const char *my_program_name;
287 static const char *my_program_dir;
289 static const char *config_filename;
290 static int ffserver_debug;
291 static int ffserver_daemon;
292 static int no_launch;
293 static int need_to_start_children;
295 static int nb_max_connections;
296 static int nb_connections;
298 static int max_bandwidth;
299 static int current_bandwidth;
301 static int64_t cur_time; // Making this global saves on passing it around everywhere
303 static AVRandomState random_state;
305 static FILE *logfile = NULL;
307 static void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
313 vfprintf(logfile, fmt, ap);
319 static char *ctime1(char *buf2)
327 p = buf2 + strlen(p) - 1;
333 static void log_connection(HTTPContext *c)
340 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64"\n",
341 inet_ntoa(c->from_addr.sin_addr),
342 ctime1(buf2), c->method, c->url,
343 c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
346 static void update_datarate(DataRateData *drd, int64_t count)
348 if (!drd->time1 && !drd->count1) {
349 drd->time1 = drd->time2 = cur_time;
350 drd->count1 = drd->count2 = count;
351 } else if (cur_time - drd->time2 > 5000) {
352 drd->time1 = drd->time2;
353 drd->count1 = drd->count2;
354 drd->time2 = cur_time;
359 /* In bytes per second */
360 static int compute_datarate(DataRateData *drd, int64_t count)
362 if (cur_time == drd->time1)
365 return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
369 static void start_children(FFStream *feed)
374 for (; feed; feed = feed->next) {
375 if (feed->child_argv && !feed->pid) {
376 feed->pid_start = time(0);
381 fprintf(stderr, "Unable to create children\n");
390 for (i = 3; i < 256; i++)
393 if (!ffserver_debug) {
394 i = open("/dev/null", O_RDWR);
403 av_strlcpy(pathname, my_program_name, sizeof(pathname));
405 slash = strrchr(pathname, '/');
410 strcpy(slash, "ffmpeg");
412 /* This is needed to make relative pathnames work */
413 chdir(my_program_dir);
415 signal(SIGPIPE, SIG_DFL);
417 execvp(pathname, feed->child_argv);
425 /* open a listening socket */
426 static int socket_open_listen(struct sockaddr_in *my_addr)
430 server_fd = socket(AF_INET,SOCK_STREAM,0);
437 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
439 if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
441 snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
443 closesocket(server_fd);
447 if (listen (server_fd, 5) < 0) {
449 closesocket(server_fd);
452 ff_socket_nonblock(server_fd, 1);
457 /* start all multicast streams */
458 static void start_multicast(void)
463 struct sockaddr_in dest_addr;
464 int default_port, stream_index;
467 for(stream = first_stream; stream != NULL; stream = stream->next) {
468 if (stream->is_multicast) {
469 /* open the RTP connection */
470 snprintf(session_id, sizeof(session_id), "%08x%08x",
471 av_random(&random_state), av_random(&random_state));
473 /* choose a port if none given */
474 if (stream->multicast_port == 0) {
475 stream->multicast_port = default_port;
479 dest_addr.sin_family = AF_INET;
480 dest_addr.sin_addr = stream->multicast_ip;
481 dest_addr.sin_port = htons(stream->multicast_port);
483 rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
484 RTSP_PROTOCOL_RTP_UDP_MULTICAST);
488 if (open_input_stream(rtp_c, "") < 0) {
489 fprintf(stderr, "Could not open input stream for stream '%s'\n",
494 /* open each RTP stream */
495 for(stream_index = 0; stream_index < stream->nb_streams;
497 dest_addr.sin_port = htons(stream->multicast_port +
499 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) < 0) {
500 fprintf(stderr, "Could not open output stream '%s/streamid=%d'\n",
501 stream->filename, stream_index);
506 /* change state to send data */
507 rtp_c->state = HTTPSTATE_SEND_DATA;
512 /* main loop of the http server */
513 static int http_server(void)
515 int server_fd, ret, rtsp_server_fd, delay, delay1;
516 struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 2], *poll_entry;
517 HTTPContext *c, *c_next;
519 server_fd = socket_open_listen(&my_http_addr);
523 rtsp_server_fd = socket_open_listen(&my_rtsp_addr);
524 if (rtsp_server_fd < 0)
527 http_log("ffserver started.\n");
529 start_children(first_feed);
531 first_http_ctx = NULL;
537 poll_entry = poll_table;
538 poll_entry->fd = server_fd;
539 poll_entry->events = POLLIN;
542 poll_entry->fd = rtsp_server_fd;
543 poll_entry->events = POLLIN;
546 /* wait for events on each HTTP handle */
553 case HTTPSTATE_SEND_HEADER:
554 case RTSPSTATE_SEND_REPLY:
555 case RTSPSTATE_SEND_PACKET:
556 c->poll_entry = poll_entry;
558 poll_entry->events = POLLOUT;
561 case HTTPSTATE_SEND_DATA_HEADER:
562 case HTTPSTATE_SEND_DATA:
563 case HTTPSTATE_SEND_DATA_TRAILER:
564 if (!c->is_packetized) {
565 /* for TCP, we output as much as we can (may need to put a limit) */
566 c->poll_entry = poll_entry;
568 poll_entry->events = POLLOUT;
571 /* when ffserver is doing the timing, we work by
572 looking at which packet need to be sent every
574 delay1 = 10; /* one tick wait XXX: 10 ms assumed */
579 case HTTPSTATE_WAIT_REQUEST:
580 case HTTPSTATE_RECEIVE_DATA:
581 case HTTPSTATE_WAIT_FEED:
582 case RTSPSTATE_WAIT_REQUEST:
583 /* need to catch errors */
584 c->poll_entry = poll_entry;
586 poll_entry->events = POLLIN;/* Maybe this will work */
590 c->poll_entry = NULL;
596 /* wait for an event on one connection. We poll at least every
597 second to handle timeouts */
599 ret = poll(poll_table, poll_entry - poll_table, delay);
600 if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
601 ff_neterrno() != FF_NETERROR(EINTR))
605 cur_time = av_gettime() / 1000;
607 if (need_to_start_children) {
608 need_to_start_children = 0;
609 start_children(first_feed);
612 /* now handle the events */
613 for(c = first_http_ctx; c != NULL; c = c_next) {
615 if (handle_connection(c) < 0) {
616 /* close and free the connection */
622 poll_entry = poll_table;
623 /* new HTTP connection request ? */
624 if (poll_entry->revents & POLLIN)
625 new_connection(server_fd, 0);
627 /* new RTSP connection request ? */
628 if (poll_entry->revents & POLLIN)
629 new_connection(rtsp_server_fd, 1);
633 /* start waiting for a new HTTP/RTSP request */
634 static void start_wait_request(HTTPContext *c, int is_rtsp)
636 c->buffer_ptr = c->buffer;
637 c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
640 c->timeout = cur_time + RTSP_REQUEST_TIMEOUT;
641 c->state = RTSPSTATE_WAIT_REQUEST;
643 c->timeout = cur_time + HTTP_REQUEST_TIMEOUT;
644 c->state = HTTPSTATE_WAIT_REQUEST;
648 static void new_connection(int server_fd, int is_rtsp)
650 struct sockaddr_in from_addr;
652 HTTPContext *c = NULL;
654 len = sizeof(from_addr);
655 fd = accept(server_fd, (struct sockaddr *)&from_addr,
659 ff_socket_nonblock(fd, 1);
661 /* XXX: should output a warning page when coming
662 close to the connection limit */
663 if (nb_connections >= nb_max_connections)
666 /* add a new connection */
667 c = av_mallocz(sizeof(HTTPContext));
672 c->poll_entry = NULL;
673 c->from_addr = from_addr;
674 c->buffer_size = IOBUFFER_INIT_SIZE;
675 c->buffer = av_malloc(c->buffer_size);
679 c->next = first_http_ctx;
683 start_wait_request(c, is_rtsp);
695 static void close_connection(HTTPContext *c)
697 HTTPContext **cp, *c1;
699 AVFormatContext *ctx;
703 /* remove connection from list */
704 cp = &first_http_ctx;
705 while ((*cp) != NULL) {
713 /* remove references, if any (XXX: do it faster) */
714 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
719 /* remove connection associated resources */
723 /* close each frame parser */
724 for(i=0;i<c->fmt_in->nb_streams;i++) {
725 st = c->fmt_in->streams[i];
726 if (st->codec->codec)
727 avcodec_close(st->codec);
729 av_close_input_file(c->fmt_in);
732 /* free RTP output streams if any */
735 nb_streams = c->stream->nb_streams;
737 for(i=0;i<nb_streams;i++) {
740 av_write_trailer(ctx);
743 h = c->rtp_handles[i];
750 if (!c->last_packet_sent) {
753 if (url_open_dyn_buf(&ctx->pb) >= 0) {
754 av_write_trailer(ctx);
755 url_close_dyn_buf(ctx->pb, &c->pb_buffer);
760 for(i=0; i<ctx->nb_streams; i++)
761 av_free(ctx->streams[i]);
763 if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
764 current_bandwidth -= c->stream->bandwidth;
766 /* signal that there is no feed if we are the feeder socket */
767 if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
768 c->stream->feed_opened = 0;
772 av_freep(&c->pb_buffer);
773 av_freep(&c->packet_buffer);
779 static int handle_connection(HTTPContext *c)
784 case HTTPSTATE_WAIT_REQUEST:
785 case RTSPSTATE_WAIT_REQUEST:
787 if ((c->timeout - cur_time) < 0)
789 if (c->poll_entry->revents & (POLLERR | POLLHUP))
792 /* no need to read if no events */
793 if (!(c->poll_entry->revents & POLLIN))
797 len = recv(c->fd, c->buffer_ptr, 1, 0);
799 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
800 ff_neterrno() != FF_NETERROR(EINTR))
802 } else if (len == 0) {
805 /* search for end of request. */
807 c->buffer_ptr += len;
809 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
810 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
811 /* request found : parse it and reply */
812 if (c->state == HTTPSTATE_WAIT_REQUEST) {
813 ret = http_parse_request(c);
815 ret = rtsp_parse_request(c);
819 } else if (ptr >= c->buffer_end) {
820 /* request too long: cannot do anything */
822 } else goto read_loop;
826 case HTTPSTATE_SEND_HEADER:
827 if (c->poll_entry->revents & (POLLERR | POLLHUP))
830 /* no need to write if no events */
831 if (!(c->poll_entry->revents & POLLOUT))
833 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
835 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
836 ff_neterrno() != FF_NETERROR(EINTR)) {
837 /* error : close connection */
838 av_freep(&c->pb_buffer);
842 c->buffer_ptr += len;
844 c->stream->bytes_served += len;
845 c->data_count += len;
846 if (c->buffer_ptr >= c->buffer_end) {
847 av_freep(&c->pb_buffer);
851 /* all the buffer was sent : synchronize to the incoming stream */
852 c->state = HTTPSTATE_SEND_DATA_HEADER;
853 c->buffer_ptr = c->buffer_end = c->buffer;
858 case HTTPSTATE_SEND_DATA:
859 case HTTPSTATE_SEND_DATA_HEADER:
860 case HTTPSTATE_SEND_DATA_TRAILER:
861 /* for packetized output, we consider we can always write (the
862 input streams sets the speed). It may be better to verify
863 that we do not rely too much on the kernel queues */
864 if (!c->is_packetized) {
865 if (c->poll_entry->revents & (POLLERR | POLLHUP))
868 /* no need to read if no events */
869 if (!(c->poll_entry->revents & POLLOUT))
872 if (http_send_data(c) < 0)
874 /* close connection if trailer sent */
875 if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
878 case HTTPSTATE_RECEIVE_DATA:
879 /* no need to read if no events */
880 if (c->poll_entry->revents & (POLLERR | POLLHUP))
882 if (!(c->poll_entry->revents & POLLIN))
884 if (http_receive_data(c) < 0)
887 case HTTPSTATE_WAIT_FEED:
888 /* no need to read if no events */
889 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
892 /* nothing to do, we'll be waken up by incoming feed packets */
895 case RTSPSTATE_SEND_REPLY:
896 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
897 av_freep(&c->pb_buffer);
900 /* no need to write if no events */
901 if (!(c->poll_entry->revents & POLLOUT))
903 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
905 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
906 ff_neterrno() != FF_NETERROR(EINTR)) {
907 /* error : close connection */
908 av_freep(&c->pb_buffer);
912 c->buffer_ptr += len;
913 c->data_count += len;
914 if (c->buffer_ptr >= c->buffer_end) {
915 /* all the buffer was sent : wait for a new request */
916 av_freep(&c->pb_buffer);
917 start_wait_request(c, 1);
921 case RTSPSTATE_SEND_PACKET:
922 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
923 av_freep(&c->packet_buffer);
926 /* no need to write if no events */
927 if (!(c->poll_entry->revents & POLLOUT))
929 len = send(c->fd, c->packet_buffer_ptr,
930 c->packet_buffer_end - c->packet_buffer_ptr, 0);
932 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
933 ff_neterrno() != FF_NETERROR(EINTR)) {
934 /* error : close connection */
935 av_freep(&c->packet_buffer);
939 c->packet_buffer_ptr += len;
940 if (c->packet_buffer_ptr >= c->packet_buffer_end) {
941 /* all the buffer was sent : wait for a new request */
942 av_freep(&c->packet_buffer);
943 c->state = RTSPSTATE_WAIT_REQUEST;
947 case HTTPSTATE_READY:
956 static int extract_rates(char *rates, int ratelen, const char *request)
960 for (p = request; *p && *p != '\r' && *p != '\n'; ) {
961 if (strncasecmp(p, "Pragma:", 7) == 0) {
962 const char *q = p + 7;
964 while (*q && *q != '\n' && isspace(*q))
967 if (strncasecmp(q, "stream-switch-entry=", 20) == 0) {
973 memset(rates, 0xff, ratelen);
976 while (*q && *q != '\n' && *q != ':')
979 if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
983 if (stream_no < ratelen && stream_no >= 0)
984 rates[stream_no] = rate_no;
986 while (*q && *q != '\n' && !isspace(*q))
1003 static int find_stream_in_feed(FFStream *feed, AVCodecContext *codec, int bit_rate)
1006 int best_bitrate = 100000000;
1009 for (i = 0; i < feed->nb_streams; i++) {
1010 AVCodecContext *feed_codec = feed->streams[i]->codec;
1012 if (feed_codec->codec_id != codec->codec_id ||
1013 feed_codec->sample_rate != codec->sample_rate ||
1014 feed_codec->width != codec->width ||
1015 feed_codec->height != codec->height)
1018 /* Potential stream */
1020 /* We want the fastest stream less than bit_rate, or the slowest
1021 * faster than bit_rate
1024 if (feed_codec->bit_rate <= bit_rate) {
1025 if (best_bitrate > bit_rate || feed_codec->bit_rate > best_bitrate) {
1026 best_bitrate = feed_codec->bit_rate;
1030 if (feed_codec->bit_rate < best_bitrate) {
1031 best_bitrate = feed_codec->bit_rate;
1040 static int modify_current_stream(HTTPContext *c, char *rates)
1043 FFStream *req = c->stream;
1044 int action_required = 0;
1046 /* Not much we can do for a feed */
1050 for (i = 0; i < req->nb_streams; i++) {
1051 AVCodecContext *codec = req->streams[i]->codec;
1055 c->switch_feed_streams[i] = req->feed_streams[i];
1058 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1061 /* Wants off or slow */
1062 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1064 /* This doesn't work well when it turns off the only stream! */
1065 c->switch_feed_streams[i] = -2;
1066 c->feed_streams[i] = -2;
1071 if (c->switch_feed_streams[i] >= 0 && c->switch_feed_streams[i] != c->feed_streams[i])
1072 action_required = 1;
1075 return action_required;
1079 static void do_switch_stream(HTTPContext *c, int i)
1081 if (c->switch_feed_streams[i] >= 0) {
1083 c->feed_streams[i] = c->switch_feed_streams[i];
1086 /* Now update the stream */
1088 c->switch_feed_streams[i] = -1;
1091 /* XXX: factorize in utils.c ? */
1092 /* XXX: take care with different space meaning */
1093 static void skip_spaces(const char **pp)
1097 while (*p == ' ' || *p == '\t')
1102 static void get_word(char *buf, int buf_size, const char **pp)
1110 while (!isspace(*p) && *p != '\0') {
1111 if ((q - buf) < buf_size - 1)
1120 static int validate_acl(FFStream *stream, HTTPContext *c)
1122 enum IPAddressAction last_action = IP_DENY;
1124 struct in_addr *src = &c->from_addr.sin_addr;
1125 unsigned long src_addr = src->s_addr;
1127 for (acl = stream->acl; acl; acl = acl->next) {
1128 if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
1129 return (acl->action == IP_ALLOW) ? 1 : 0;
1130 last_action = acl->action;
1133 /* Nothing matched, so return not the last action */
1134 return (last_action == IP_DENY) ? 1 : 0;
1137 /* compute the real filename of a file by matching it without its
1138 extensions to all the stream filenames */
1139 static void compute_real_filename(char *filename, int max_size)
1146 /* compute filename by matching without the file extensions */
1147 av_strlcpy(file1, filename, sizeof(file1));
1148 p = strrchr(file1, '.');
1151 for(stream = first_stream; stream != NULL; stream = stream->next) {
1152 av_strlcpy(file2, stream->filename, sizeof(file2));
1153 p = strrchr(file2, '.');
1156 if (!strcmp(file1, file2)) {
1157 av_strlcpy(filename, stream->filename, max_size);
1172 /* parse http request and prepare header */
1173 static int http_parse_request(HTTPContext *c)
1176 enum RedirType redir_type;
1178 char info[1024], filename[1024];
1182 const char *mime_type;
1186 char *useragent = 0;
1189 get_word(cmd, sizeof(cmd), (const char **)&p);
1190 av_strlcpy(c->method, cmd, sizeof(c->method));
1192 if (!strcmp(cmd, "GET"))
1194 else if (!strcmp(cmd, "POST"))
1199 get_word(url, sizeof(url), (const char **)&p);
1200 av_strlcpy(c->url, url, sizeof(c->url));
1202 get_word(protocol, sizeof(protocol), (const char **)&p);
1203 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1206 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
1209 http_log("New connection: %s %s\n", cmd, url);
1211 /* find the filename and the optional info string in the request */
1212 p = strchr(url, '?');
1214 av_strlcpy(info, p, sizeof(info));
1219 av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
1221 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1222 if (strncasecmp(p, "User-Agent:", 11) == 0) {
1224 if (*useragent && *useragent != '\n' && isspace(*useragent))
1228 p = strchr(p, '\n');
1235 redir_type = REDIR_NONE;
1236 if (match_ext(filename, "asx")) {
1237 redir_type = REDIR_ASX;
1238 filename[strlen(filename)-1] = 'f';
1239 } else if (match_ext(filename, "asf") &&
1240 (!useragent || strncasecmp(useragent, "NSPlayer", 8) != 0)) {
1241 /* if this isn't WMP or lookalike, return the redirector file */
1242 redir_type = REDIR_ASF;
1243 } else if (match_ext(filename, "rpm,ram")) {
1244 redir_type = REDIR_RAM;
1245 strcpy(filename + strlen(filename)-2, "m");
1246 } else if (match_ext(filename, "rtsp")) {
1247 redir_type = REDIR_RTSP;
1248 compute_real_filename(filename, sizeof(filename) - 1);
1249 } else if (match_ext(filename, "sdp")) {
1250 redir_type = REDIR_SDP;
1251 compute_real_filename(filename, sizeof(filename) - 1);
1254 // "redirect" / request to index.html
1255 if (!strlen(filename))
1256 av_strlcpy(filename, "index.html", sizeof(filename) - 1);
1258 stream = first_stream;
1259 while (stream != NULL) {
1260 if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
1262 stream = stream->next;
1264 if (stream == NULL) {
1265 snprintf(msg, sizeof(msg), "File '%s' not found", url);
1270 memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1271 memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1273 if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1274 c->http_error = 301;
1276 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 301 Moved\r\n");
1277 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Location: %s\r\n", stream->feed_filename);
1278 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1279 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1280 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Moved</title></head><body>\r\n");
1281 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "You should be <a href=\"%s\">redirected</a>.\r\n", stream->feed_filename);
1282 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1284 /* prepare output buffer */
1285 c->buffer_ptr = c->buffer;
1287 c->state = HTTPSTATE_SEND_HEADER;
1291 /* If this is WMP, get the rate information */
1292 if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1293 if (modify_current_stream(c, ratebuf)) {
1294 for (i = 0; i < sizeof(c->feed_streams) / sizeof(c->feed_streams[0]); i++) {
1295 if (c->switch_feed_streams[i] >= 0)
1296 do_switch_stream(c, i);
1301 /* If already streaming this feed, do not let start another feeder. */
1302 if (stream->feed_opened) {
1303 snprintf(msg, sizeof(msg), "This feed is already being received.");
1307 if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
1308 current_bandwidth += stream->bandwidth;
1310 if (c->post == 0 && max_bandwidth < current_bandwidth) {
1311 c->http_error = 200;
1313 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 Server too busy\r\n");
1314 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1315 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1316 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Too busy</title></head><body>\r\n");
1317 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<p>The server is too busy to serve your request at this time.</p>\r\n");
1318 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<p>The bandwidth being served (including your stream) is %dkbit/sec, and this exceeds the limit of %dkbit/sec.</p>\r\n",
1319 current_bandwidth, max_bandwidth);
1320 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1322 /* prepare output buffer */
1323 c->buffer_ptr = c->buffer;
1325 c->state = HTTPSTATE_SEND_HEADER;
1329 if (redir_type != REDIR_NONE) {
1332 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1333 if (strncasecmp(p, "Host:", 5) == 0) {
1337 p = strchr(p, '\n');
1348 while (isspace(*hostinfo))
1351 eoh = strchr(hostinfo, '\n');
1353 if (eoh[-1] == '\r')
1356 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1357 memcpy(hostbuf, hostinfo, eoh - hostinfo);
1358 hostbuf[eoh - hostinfo] = 0;
1360 c->http_error = 200;
1362 switch(redir_type) {
1364 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASX Follows\r\n");
1365 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1366 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1367 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ASX Version=\"3\">\r\n");
1368 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1369 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1370 hostbuf, filename, info);
1371 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</ASX>\r\n");
1374 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RAM Follows\r\n");
1375 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: audio/x-pn-realaudio\r\n");
1376 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1377 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "# Autogenerated by ffserver\r\n");
1378 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "http://%s/%s%s\r\n",
1379 hostbuf, filename, info);
1382 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASF Redirect follows\r\n");
1383 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1384 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1385 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "[Reference]\r\n");
1386 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Ref1=http://%s/%s%s\r\n",
1387 hostbuf, filename, info);
1391 char hostname[256], *p;
1392 /* extract only hostname */
1393 av_strlcpy(hostname, hostbuf, sizeof(hostname));
1394 p = strrchr(hostname, ':');
1397 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1398 /* XXX: incorrect mime type ? */
1399 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/x-rtsp\r\n");
1400 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1401 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "rtsp://%s:%d/%s\r\n",
1402 hostname, ntohs(my_rtsp_addr.sin_port),
1409 int sdp_data_size, len;
1410 struct sockaddr_in my_addr;
1412 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1413 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/sdp\r\n");
1414 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1416 len = sizeof(my_addr);
1417 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
1419 /* XXX: should use a dynamic buffer */
1420 sdp_data_size = prepare_sdp_description(stream,
1423 if (sdp_data_size > 0) {
1424 memcpy(q, sdp_data, sdp_data_size);
1436 /* prepare output buffer */
1437 c->buffer_ptr = c->buffer;
1439 c->state = HTTPSTATE_SEND_HEADER;
1445 snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
1449 stream->conns_served++;
1451 /* XXX: add there authenticate and IP match */
1454 /* if post, it means a feed is being sent */
1455 if (!stream->is_feed) {
1456 /* However it might be a status report from WMP! Lets log the data
1457 * as it might come in handy one day
1462 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1463 if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1467 if (strncasecmp(p, "Pragma: client-id=", 18) == 0)
1468 client_id = strtol(p + 18, 0, 10);
1469 p = strchr(p, '\n');
1477 char *eol = strchr(logline, '\n');
1482 if (eol[-1] == '\r')
1484 http_log("%.*s\n", (int) (eol - logline), logline);
1485 c->suppress_log = 1;
1490 http_log("\nGot request:\n%s\n", c->buffer);
1493 if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1496 /* Now we have to find the client_id */
1497 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1498 if (wmpc->wmp_client_id == client_id)
1502 if (wmpc && modify_current_stream(wmpc, ratebuf))
1503 wmpc->switch_pending = 1;
1506 snprintf(msg, sizeof(msg), "POST command not handled");
1510 if (http_start_receive_data(c) < 0) {
1511 snprintf(msg, sizeof(msg), "could not open feed");
1515 c->state = HTTPSTATE_RECEIVE_DATA;
1520 if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
1521 http_log("\nGot request:\n%s\n", c->buffer);
1524 if (c->stream->stream_type == STREAM_TYPE_STATUS)
1527 /* open input stream */
1528 if (open_input_stream(c, info) < 0) {
1529 snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1533 /* prepare http header */
1535 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1536 mime_type = c->stream->fmt->mime_type;
1538 mime_type = "application/x-octet-stream";
1539 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
1541 /* for asf, we need extra headers */
1542 if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1543 /* Need to allocate a client id */
1545 c->wmp_client_id = av_random(&random_state) & 0x7fffffff;
1547 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);
1549 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-Type: %s\r\n", mime_type);
1550 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1552 /* prepare output buffer */
1554 c->buffer_ptr = c->buffer;
1556 c->state = HTTPSTATE_SEND_HEADER;
1559 c->http_error = 404;
1561 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 404 Not Found\r\n");
1562 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: %s\r\n", "text/html");
1563 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1564 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HTML>\n");
1565 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1566 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<BODY>%s</BODY>\n", msg);
1567 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</HTML>\n");
1569 /* prepare output buffer */
1570 c->buffer_ptr = c->buffer;
1572 c->state = HTTPSTATE_SEND_HEADER;
1576 c->http_error = 200; /* horrible : we use this value to avoid
1577 going to the send data state */
1578 c->state = HTTPSTATE_SEND_HEADER;
1582 static void fmt_bytecount(ByteIOContext *pb, int64_t count)
1584 static const char *suffix = " kMGTP";
1587 for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
1589 url_fprintf(pb, "%"PRId64"%c", count, *s);
1592 static void compute_stats(HTTPContext *c)
1601 if (url_open_dyn_buf(&pb) < 0) {
1602 /* XXX: return an error ? */
1603 c->buffer_ptr = c->buffer;
1604 c->buffer_end = c->buffer;
1608 url_fprintf(pb, "HTTP/1.0 200 OK\r\n");
1609 url_fprintf(pb, "Content-type: %s\r\n", "text/html");
1610 url_fprintf(pb, "Pragma: no-cache\r\n");
1611 url_fprintf(pb, "\r\n");
1613 url_fprintf(pb, "<HEAD><TITLE>FFServer Status</TITLE>\n");
1614 if (c->stream->feed_filename)
1615 url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
1616 url_fprintf(pb, "</HEAD>\n<BODY>");
1617 url_fprintf(pb, "<H1>FFServer Status</H1>\n");
1619 url_fprintf(pb, "<H2>Available Streams</H2>\n");
1620 url_fprintf(pb, "<TABLE cellspacing=0 cellpadding=4>\n");
1621 url_fprintf(pb, "<TR><Th valign=top>Path<th align=left>Served<br>Conns<Th><br>bytes<Th valign=top>Format<Th>Bit rate<br>kbits/s<Th align=left>Video<br>kbits/s<th><br>Codec<Th align=left>Audio<br>kbits/s<th><br>Codec<Th align=left valign=top>Feed\n");
1622 stream = first_stream;
1623 while (stream != NULL) {
1624 char sfilename[1024];
1627 if (stream->feed != stream) {
1628 av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
1629 eosf = sfilename + strlen(sfilename);
1630 if (eosf - sfilename >= 4) {
1631 if (strcmp(eosf - 4, ".asf") == 0)
1632 strcpy(eosf - 4, ".asx");
1633 else if (strcmp(eosf - 3, ".rm") == 0)
1634 strcpy(eosf - 3, ".ram");
1635 else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
1636 /* generate a sample RTSP director if
1637 unicast. Generate an SDP redirector if
1639 eosf = strrchr(sfilename, '.');
1641 eosf = sfilename + strlen(sfilename);
1642 if (stream->is_multicast)
1643 strcpy(eosf, ".sdp");
1645 strcpy(eosf, ".rtsp");
1649 url_fprintf(pb, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1650 sfilename, stream->filename);
1651 url_fprintf(pb, "<td align=right> %d <td align=right> ",
1652 stream->conns_served);
1653 fmt_bytecount(pb, stream->bytes_served);
1654 switch(stream->stream_type) {
1655 case STREAM_TYPE_LIVE:
1657 int audio_bit_rate = 0;
1658 int video_bit_rate = 0;
1659 const char *audio_codec_name = "";
1660 const char *video_codec_name = "";
1661 const char *audio_codec_name_extra = "";
1662 const char *video_codec_name_extra = "";
1664 for(i=0;i<stream->nb_streams;i++) {
1665 AVStream *st = stream->streams[i];
1666 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1667 switch(st->codec->codec_type) {
1668 case CODEC_TYPE_AUDIO:
1669 audio_bit_rate += st->codec->bit_rate;
1671 if (*audio_codec_name)
1672 audio_codec_name_extra = "...";
1673 audio_codec_name = codec->name;
1676 case CODEC_TYPE_VIDEO:
1677 video_bit_rate += st->codec->bit_rate;
1679 if (*video_codec_name)
1680 video_codec_name_extra = "...";
1681 video_codec_name = codec->name;
1684 case CODEC_TYPE_DATA:
1685 video_bit_rate += st->codec->bit_rate;
1691 url_fprintf(pb, "<TD align=center> %s <TD align=right> %d <TD align=right> %d <TD> %s %s <TD align=right> %d <TD> %s %s",
1694 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
1695 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
1697 url_fprintf(pb, "<TD>%s", stream->feed->filename);
1699 url_fprintf(pb, "<TD>%s", stream->feed_filename);
1700 url_fprintf(pb, "\n");
1704 url_fprintf(pb, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1708 stream = stream->next;
1710 url_fprintf(pb, "</TABLE>\n");
1712 stream = first_stream;
1713 while (stream != NULL) {
1714 if (stream->feed == stream) {
1715 url_fprintf(pb, "<h2>Feed %s</h2>", stream->filename);
1717 url_fprintf(pb, "Running as pid %d.\n", stream->pid);
1719 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1724 /* This is somewhat linux specific I guess */
1725 snprintf(ps_cmd, sizeof(ps_cmd),
1726 "ps -o \"%%cpu,cputime\" --no-headers %d",
1729 pid_stat = popen(ps_cmd, "r");
1734 if (fscanf(pid_stat, "%10s %64s", cpuperc,
1736 url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",
1744 url_fprintf(pb, "<p>");
1746 url_fprintf(pb, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>type<th>kbits/s<th align=left>codec<th align=left>Parameters\n");
1748 for (i = 0; i < stream->nb_streams; i++) {
1749 AVStream *st = stream->streams[i];
1750 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1751 const char *type = "unknown";
1752 char parameters[64];
1756 switch(st->codec->codec_type) {
1757 case CODEC_TYPE_AUDIO:
1759 snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
1761 case CODEC_TYPE_VIDEO:
1763 snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,
1764 st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);
1769 url_fprintf(pb, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1770 i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);
1772 url_fprintf(pb, "</table>\n");
1775 stream = stream->next;
1781 AVCodecContext *enc;
1785 stream = first_feed;
1786 while (stream != NULL) {
1787 url_fprintf(pb, "<H1>Feed '%s'</H1>\n", stream->filename);
1788 url_fprintf(pb, "<TABLE>\n");
1789 url_fprintf(pb, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1790 for(i=0;i<stream->nb_streams;i++) {
1791 AVStream *st = stream->streams[i];
1792 FeedData *fdata = st->priv_data;
1795 avcodec_string(buf, sizeof(buf), enc);
1796 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
1797 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
1798 avg /= enc->frame_size;
1799 url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
1800 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
1802 url_fprintf(pb, "</TABLE>\n");
1803 stream = stream->next_feed;
1808 /* connection status */
1809 url_fprintf(pb, "<H2>Connection Status</H2>\n");
1811 url_fprintf(pb, "Number of connections: %d / %d<BR>\n",
1812 nb_connections, nb_max_connections);
1814 url_fprintf(pb, "Bandwidth in use: %dk / %dk<BR>\n",
1815 current_bandwidth, max_bandwidth);
1817 url_fprintf(pb, "<TABLE>\n");
1818 url_fprintf(pb, "<TR><th>#<th>File<th>IP<th>Proto<th>State<th>Target bits/sec<th>Actual bits/sec<th>Bytes transferred\n");
1819 c1 = first_http_ctx;
1821 while (c1 != NULL) {
1827 for (j = 0; j < c1->stream->nb_streams; j++) {
1828 if (!c1->stream->feed)
1829 bitrate += c1->stream->streams[j]->codec->bit_rate;
1830 else if (c1->feed_streams[j] >= 0)
1831 bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
1836 p = inet_ntoa(c1->from_addr.sin_addr);
1837 url_fprintf(pb, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1839 c1->stream ? c1->stream->filename : "",
1840 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
1843 http_state[c1->state]);
1844 fmt_bytecount(pb, bitrate);
1845 url_fprintf(pb, "<td align=right>");
1846 fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
1847 url_fprintf(pb, "<td align=right>");
1848 fmt_bytecount(pb, c1->data_count);
1849 url_fprintf(pb, "\n");
1852 url_fprintf(pb, "</TABLE>\n");
1857 url_fprintf(pb, "<HR size=1 noshade>Generated at %s", p);
1858 url_fprintf(pb, "</BODY>\n</HTML>\n");
1860 len = url_close_dyn_buf(pb, &c->pb_buffer);
1861 c->buffer_ptr = c->pb_buffer;
1862 c->buffer_end = c->pb_buffer + len;
1865 /* check if the parser needs to be opened for stream i */
1866 static void open_parser(AVFormatContext *s, int i)
1868 AVStream *st = s->streams[i];
1871 if (!st->codec->codec) {
1872 codec = avcodec_find_decoder(st->codec->codec_id);
1873 if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
1874 st->codec->parse_only = 1;
1875 if (avcodec_open(st->codec, codec) < 0)
1876 st->codec->parse_only = 0;
1881 static int open_input_stream(HTTPContext *c, const char *info)
1884 char input_filename[1024];
1886 int buf_size, i, ret;
1889 /* find file name */
1890 if (c->stream->feed) {
1891 strcpy(input_filename, c->stream->feed->feed_filename);
1892 buf_size = FFM_PACKET_SIZE;
1893 /* compute position (absolute time) */
1894 if (find_info_tag(buf, sizeof(buf), "date", info))
1896 stream_pos = parse_date(buf, 0);
1897 if (stream_pos == INT64_MIN)
1900 else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
1901 int prebuffer = strtol(buf, 0, 10);
1902 stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
1904 stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
1906 strcpy(input_filename, c->stream->feed_filename);
1908 /* compute position (relative time) */
1909 if (find_info_tag(buf, sizeof(buf), "date", info))
1911 stream_pos = parse_date(buf, 1);
1912 if (stream_pos == INT64_MIN)
1918 if (input_filename[0] == '\0')
1922 { time_t when = stream_pos / 1000000;
1923 http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
1928 if ((ret = av_open_input_file(&s, input_filename, c->stream->ifmt,
1929 buf_size, c->stream->ap_in)) < 0) {
1930 http_log("could not open %s: %d\n", input_filename, ret);
1933 s->flags |= AVFMT_FLAG_GENPTS;
1935 av_find_stream_info(c->fmt_in);
1937 /* open each parser */
1938 for(i=0;i<s->nb_streams;i++)
1941 /* choose stream as clock source (we favorize video stream if
1942 present) for packet sending */
1943 c->pts_stream_index = 0;
1944 for(i=0;i<c->stream->nb_streams;i++) {
1945 if (c->pts_stream_index == 0 &&
1946 c->stream->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
1947 c->pts_stream_index = i;
1952 if (c->fmt_in->iformat->read_seek)
1953 c->fmt_in->iformat->read_seek(c->fmt_in, 0, stream_pos, 0);
1955 /* set the start time (needed for maxtime and RTP packet timing) */
1956 c->start_time = cur_time;
1957 c->first_pts = AV_NOPTS_VALUE;
1961 /* return the server clock (in us) */
1962 static int64_t get_server_clock(HTTPContext *c)
1964 /* compute current pts value from system time */
1965 return (cur_time - c->start_time) * 1000;
1968 /* return the estimated time at which the current packet must be sent
1970 static int64_t get_packet_send_clock(HTTPContext *c)
1972 int bytes_left, bytes_sent, frame_bytes;
1974 frame_bytes = c->cur_frame_bytes;
1975 if (frame_bytes <= 0)
1978 bytes_left = c->buffer_end - c->buffer_ptr;
1979 bytes_sent = frame_bytes - bytes_left;
1980 return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
1985 static int http_prepare_data(HTTPContext *c)
1988 AVFormatContext *ctx;
1990 av_freep(&c->pb_buffer);
1992 case HTTPSTATE_SEND_DATA_HEADER:
1993 memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
1994 av_strlcpy(c->fmt_ctx.author, c->stream->author,
1995 sizeof(c->fmt_ctx.author));
1996 av_strlcpy(c->fmt_ctx.comment, c->stream->comment,
1997 sizeof(c->fmt_ctx.comment));
1998 av_strlcpy(c->fmt_ctx.copyright, c->stream->copyright,
1999 sizeof(c->fmt_ctx.copyright));
2000 av_strlcpy(c->fmt_ctx.title, c->stream->title,
2001 sizeof(c->fmt_ctx.title));
2003 /* open output stream by using specified codecs */
2004 c->fmt_ctx.oformat = c->stream->fmt;
2005 c->fmt_ctx.nb_streams = c->stream->nb_streams;
2006 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
2009 st = av_mallocz(sizeof(AVStream));
2010 st->codec= avcodec_alloc_context();
2011 c->fmt_ctx.streams[i] = st;
2012 /* if file or feed, then just take streams from FFStream struct */
2013 if (!c->stream->feed ||
2014 c->stream->feed == c->stream)
2015 src = c->stream->streams[i];
2017 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2021 st->codec->frame_number = 0; /* XXX: should be done in
2022 AVStream, not in codec */
2024 c->got_key_frame = 0;
2026 /* prepare header and save header data in a stream */
2027 if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2028 /* XXX: potential leak */
2031 c->fmt_ctx.pb->is_streamed = 1;
2033 av_set_parameters(&c->fmt_ctx, NULL);
2034 if (av_write_header(&c->fmt_ctx) < 0)
2037 len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2038 c->buffer_ptr = c->pb_buffer;
2039 c->buffer_end = c->pb_buffer + len;
2041 c->state = HTTPSTATE_SEND_DATA;
2042 c->last_packet_sent = 0;
2044 case HTTPSTATE_SEND_DATA:
2045 /* find a new packet */
2046 /* read a packet from the input stream */
2047 if (c->stream->feed)
2048 ffm_set_write_index(c->fmt_in,
2049 c->stream->feed->feed_write_index,
2050 c->stream->feed->feed_size);
2052 if (c->stream->max_time &&
2053 c->stream->max_time + c->start_time - cur_time < 0)
2054 /* We have timed out */
2055 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2059 if (av_read_frame(c->fmt_in, &pkt) < 0) {
2060 if (c->stream->feed && c->stream->feed->feed_opened) {
2061 /* if coming from feed, it means we reached the end of the
2062 ffm file, so must wait for more data */
2063 c->state = HTTPSTATE_WAIT_FEED;
2064 return 1; /* state changed */
2066 if (c->stream->loop) {
2067 av_close_input_file(c->fmt_in);
2069 if (open_input_stream(c, "") < 0)
2074 /* must send trailer now because eof or error */
2075 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2079 /* update first pts if needed */
2080 if (c->first_pts == AV_NOPTS_VALUE) {
2081 c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2082 c->start_time = cur_time;
2084 /* send it to the appropriate stream */
2085 if (c->stream->feed) {
2086 /* if coming from a feed, select the right stream */
2087 if (c->switch_pending) {
2088 c->switch_pending = 0;
2089 for(i=0;i<c->stream->nb_streams;i++) {
2090 if (c->switch_feed_streams[i] == pkt.stream_index)
2091 if (pkt.flags & PKT_FLAG_KEY)
2092 do_switch_stream(c, i);
2093 if (c->switch_feed_streams[i] >= 0)
2094 c->switch_pending = 1;
2097 for(i=0;i<c->stream->nb_streams;i++) {
2098 if (c->feed_streams[i] == pkt.stream_index) {
2099 pkt.stream_index = i;
2100 if (pkt.flags & PKT_FLAG_KEY)
2101 c->got_key_frame |= 1 << i;
2102 /* See if we have all the key frames, then
2103 * we start to send. This logic is not quite
2104 * right, but it works for the case of a
2105 * single video stream with one or more
2106 * audio streams (for which every frame is
2107 * typically a key frame).
2109 if (!c->stream->send_on_key ||
2110 ((c->got_key_frame + 1) >> c->stream->nb_streams))
2115 AVCodecContext *codec;
2118 /* specific handling for RTP: we use several
2119 output stream (one for each RTP
2120 connection). XXX: need more abstract handling */
2121 if (c->is_packetized) {
2123 /* compute send time and duration */
2124 st = c->fmt_in->streams[pkt.stream_index];
2125 c->cur_pts = av_rescale_q(pkt.dts, st->time_base, AV_TIME_BASE_Q);
2126 if (st->start_time != AV_NOPTS_VALUE)
2127 c->cur_pts -= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
2128 c->cur_frame_duration = av_rescale_q(pkt.duration, st->time_base, AV_TIME_BASE_Q);
2130 printf("index=%d pts=%0.3f duration=%0.6f\n",
2132 (double)c->cur_pts /
2134 (double)c->cur_frame_duration /
2137 /* find RTP context */
2138 c->packet_stream_index = pkt.stream_index;
2139 ctx = c->rtp_ctx[c->packet_stream_index];
2141 av_free_packet(&pkt);
2144 codec = ctx->streams[0]->codec;
2145 /* only one stream per RTP connection */
2146 pkt.stream_index = 0;
2150 codec = ctx->streams[pkt.stream_index]->codec;
2153 if (c->is_packetized) {
2154 int max_packet_size;
2155 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP)
2156 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2158 max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);
2159 ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);
2161 ret = url_open_dyn_buf(&ctx->pb);
2164 /* XXX: potential leak */
2167 c->fmt_ctx.pb->is_streamed = 1;
2168 if (pkt.dts != AV_NOPTS_VALUE)
2169 pkt.dts = av_rescale_q(pkt.dts,
2170 c->fmt_in->streams[pkt.stream_index]->time_base,
2171 ctx->streams[pkt.stream_index]->time_base);
2172 if (pkt.pts != AV_NOPTS_VALUE)
2173 pkt.pts = av_rescale_q(pkt.pts,
2174 c->fmt_in->streams[pkt.stream_index]->time_base,
2175 ctx->streams[pkt.stream_index]->time_base);
2176 if (av_write_frame(ctx, &pkt))
2177 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2179 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2180 c->cur_frame_bytes = len;
2181 c->buffer_ptr = c->pb_buffer;
2182 c->buffer_end = c->pb_buffer + len;
2184 codec->frame_number++;
2186 av_free_packet(&pkt);
2190 av_free_packet(&pkt);
2195 case HTTPSTATE_SEND_DATA_TRAILER:
2196 /* last packet test ? */
2197 if (c->last_packet_sent || c->is_packetized)
2200 /* prepare header */
2201 if (url_open_dyn_buf(&ctx->pb) < 0) {
2202 /* XXX: potential leak */
2205 c->fmt_ctx.pb->is_streamed = 1;
2206 av_write_trailer(ctx);
2207 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2208 c->buffer_ptr = c->pb_buffer;
2209 c->buffer_end = c->pb_buffer + len;
2211 c->last_packet_sent = 1;
2217 /* should convert the format at the same time */
2218 /* send data starting at c->buffer_ptr to the output connection
2219 (either UDP or TCP connection) */
2220 static int http_send_data(HTTPContext *c)
2225 if (c->buffer_ptr >= c->buffer_end) {
2226 ret = http_prepare_data(c);
2230 /* state change requested */
2233 if (c->is_packetized) {
2234 /* RTP data output */
2235 len = c->buffer_end - c->buffer_ptr;
2237 /* fail safe - should never happen */
2239 c->buffer_ptr = c->buffer_end;
2242 len = (c->buffer_ptr[0] << 24) |
2243 (c->buffer_ptr[1] << 16) |
2244 (c->buffer_ptr[2] << 8) |
2246 if (len > (c->buffer_end - c->buffer_ptr))
2248 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2249 /* nothing to send yet: we can wait */
2253 c->data_count += len;
2254 update_datarate(&c->datarate, c->data_count);
2256 c->stream->bytes_served += len;
2258 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) {
2259 /* RTP packets are sent inside the RTSP TCP connection */
2261 int interleaved_index, size;
2263 HTTPContext *rtsp_c;
2266 /* if no RTSP connection left, error */
2269 /* if already sending something, then wait. */
2270 if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
2272 if (url_open_dyn_buf(&pb) < 0)
2274 interleaved_index = c->packet_stream_index * 2;
2275 /* RTCP packets are sent at odd indexes */
2276 if (c->buffer_ptr[1] == 200)
2277 interleaved_index++;
2278 /* write RTSP TCP header */
2280 header[1] = interleaved_index;
2281 header[2] = len >> 8;
2283 put_buffer(pb, header, 4);
2284 /* write RTP packet data */
2286 put_buffer(pb, c->buffer_ptr, len);
2287 size = url_close_dyn_buf(pb, &c->packet_buffer);
2288 /* prepare asynchronous TCP sending */
2289 rtsp_c->packet_buffer_ptr = c->packet_buffer;
2290 rtsp_c->packet_buffer_end = c->packet_buffer + size;
2291 c->buffer_ptr += len;
2293 /* send everything we can NOW */
2294 len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2295 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2297 rtsp_c->packet_buffer_ptr += len;
2298 if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2299 /* if we could not send all the data, we will
2300 send it later, so a new state is needed to
2301 "lock" the RTSP TCP connection */
2302 rtsp_c->state = RTSPSTATE_SEND_PACKET;
2305 /* all data has been sent */
2306 av_freep(&c->packet_buffer);
2308 /* send RTP packet directly in UDP */
2310 url_write(c->rtp_handles[c->packet_stream_index],
2311 c->buffer_ptr, len);
2312 c->buffer_ptr += len;
2313 /* here we continue as we can send several packets per 10 ms slot */
2316 /* TCP data output */
2317 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2319 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2320 ff_neterrno() != FF_NETERROR(EINTR))
2321 /* error : close connection */
2326 c->buffer_ptr += len;
2328 c->data_count += len;
2329 update_datarate(&c->datarate, c->data_count);
2331 c->stream->bytes_served += len;
2339 static int http_start_receive_data(HTTPContext *c)
2343 if (c->stream->feed_opened)
2346 /* Don't permit writing to this one */
2347 if (c->stream->readonly)
2351 fd = open(c->stream->feed_filename, O_RDWR);
2356 c->stream->feed_write_index = ffm_read_write_index(fd);
2357 c->stream->feed_size = lseek(fd, 0, SEEK_END);
2358 lseek(fd, 0, SEEK_SET);
2360 /* init buffer input */
2361 c->buffer_ptr = c->buffer;
2362 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2363 c->stream->feed_opened = 1;
2367 static int http_receive_data(HTTPContext *c)
2371 if (c->buffer_end > c->buffer_ptr) {
2374 len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2376 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2377 ff_neterrno() != FF_NETERROR(EINTR))
2378 /* error : close connection */
2380 } else if (len == 0)
2381 /* end of connection : close it */
2384 c->buffer_ptr += len;
2385 c->data_count += len;
2386 update_datarate(&c->datarate, c->data_count);
2390 if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2391 if (c->buffer[0] != 'f' ||
2392 c->buffer[1] != 'm') {
2393 http_log("Feed stream has become desynchronized -- disconnecting\n");
2398 if (c->buffer_ptr >= c->buffer_end) {
2399 FFStream *feed = c->stream;
2400 /* a packet has been received : write it in the store, except
2402 if (c->data_count > FFM_PACKET_SIZE) {
2404 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2405 /* XXX: use llseek or url_seek */
2406 lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
2407 write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
2409 feed->feed_write_index += FFM_PACKET_SIZE;
2410 /* update file size */
2411 if (feed->feed_write_index > c->stream->feed_size)
2412 feed->feed_size = feed->feed_write_index;
2414 /* handle wrap around if max file size reached */
2415 if (c->stream->feed_max_size && feed->feed_write_index >= c->stream->feed_max_size)
2416 feed->feed_write_index = FFM_PACKET_SIZE;
2419 ffm_write_write_index(c->feed_fd, feed->feed_write_index);
2421 /* wake up any waiting connections */
2422 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2423 if (c1->state == HTTPSTATE_WAIT_FEED &&
2424 c1->stream->feed == c->stream->feed)
2425 c1->state = HTTPSTATE_SEND_DATA;
2428 /* We have a header in our hands that contains useful data */
2430 AVInputFormat *fmt_in;
2433 memset(&s, 0, sizeof(s));
2435 url_open_buf(&s.pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
2436 s.pb->is_streamed = 1;
2438 /* use feed output format name to find corresponding input format */
2439 fmt_in = av_find_input_format(feed->fmt->name);
2443 if (fmt_in->priv_data_size > 0) {
2444 s.priv_data = av_mallocz(fmt_in->priv_data_size);
2450 if (fmt_in->read_header(&s, 0) < 0) {
2451 av_freep(&s.priv_data);
2455 /* Now we have the actual streams */
2456 if (s.nb_streams != feed->nb_streams) {
2457 av_freep(&s.priv_data);
2460 for (i = 0; i < s.nb_streams; i++)
2461 memcpy(feed->streams[i]->codec,
2462 s.streams[i]->codec, sizeof(AVCodecContext));
2463 av_freep(&s.priv_data);
2465 c->buffer_ptr = c->buffer;
2470 c->stream->feed_opened = 0;
2475 /********************************************************************/
2478 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2485 switch(error_number) {
2486 case RTSP_STATUS_OK:
2489 case RTSP_STATUS_METHOD:
2490 str = "Method Not Allowed";
2492 case RTSP_STATUS_BANDWIDTH:
2493 str = "Not Enough Bandwidth";
2495 case RTSP_STATUS_SESSION:
2496 str = "Session Not Found";
2498 case RTSP_STATUS_STATE:
2499 str = "Method Not Valid in This State";
2501 case RTSP_STATUS_AGGREGATE:
2502 str = "Aggregate operation not allowed";
2504 case RTSP_STATUS_ONLY_AGGREGATE:
2505 str = "Only aggregate operation allowed";
2507 case RTSP_STATUS_TRANSPORT:
2508 str = "Unsupported transport";
2510 case RTSP_STATUS_INTERNAL:
2511 str = "Internal Server Error";
2513 case RTSP_STATUS_SERVICE:
2514 str = "Service Unavailable";
2516 case RTSP_STATUS_VERSION:
2517 str = "RTSP Version not supported";
2520 str = "Unknown Error";
2524 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2525 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2527 /* output GMT time */
2531 p = buf2 + strlen(p) - 1;
2534 url_fprintf(c->pb, "Date: %s GMT\r\n", buf2);
2537 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2539 rtsp_reply_header(c, error_number);
2540 url_fprintf(c->pb, "\r\n");
2543 static int rtsp_parse_request(HTTPContext *c)
2545 const char *p, *p1, *p2;
2551 RTSPHeader header1, *header = &header1;
2553 c->buffer_ptr[0] = '\0';
2556 get_word(cmd, sizeof(cmd), &p);
2557 get_word(url, sizeof(url), &p);
2558 get_word(protocol, sizeof(protocol), &p);
2560 av_strlcpy(c->method, cmd, sizeof(c->method));
2561 av_strlcpy(c->url, url, sizeof(c->url));
2562 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2564 if (url_open_dyn_buf(&c->pb) < 0) {
2565 /* XXX: cannot do more */
2566 c->pb = NULL; /* safety */
2570 /* check version name */
2571 if (strcmp(protocol, "RTSP/1.0") != 0) {
2572 rtsp_reply_error(c, RTSP_STATUS_VERSION);
2576 /* parse each header line */
2577 memset(header, 0, sizeof(RTSPHeader));
2578 /* skip to next line */
2579 while (*p != '\n' && *p != '\0')
2583 while (*p != '\0') {
2584 p1 = strchr(p, '\n');
2588 if (p2 > p && p2[-1] == '\r')
2590 /* skip empty line */
2594 if (len > sizeof(line) - 1)
2595 len = sizeof(line) - 1;
2596 memcpy(line, p, len);
2598 rtsp_parse_line(header, line);
2602 /* handle sequence number */
2603 c->seq = header->seq;
2605 if (!strcmp(cmd, "DESCRIBE"))
2606 rtsp_cmd_describe(c, url);
2607 else if (!strcmp(cmd, "OPTIONS"))
2608 rtsp_cmd_options(c, url);
2609 else if (!strcmp(cmd, "SETUP"))
2610 rtsp_cmd_setup(c, url, header);
2611 else if (!strcmp(cmd, "PLAY"))
2612 rtsp_cmd_play(c, url, header);
2613 else if (!strcmp(cmd, "PAUSE"))
2614 rtsp_cmd_pause(c, url, header);
2615 else if (!strcmp(cmd, "TEARDOWN"))
2616 rtsp_cmd_teardown(c, url, header);
2618 rtsp_reply_error(c, RTSP_STATUS_METHOD);
2621 len = url_close_dyn_buf(c->pb, &c->pb_buffer);
2622 c->pb = NULL; /* safety */
2624 /* XXX: cannot do more */
2627 c->buffer_ptr = c->pb_buffer;
2628 c->buffer_end = c->pb_buffer + len;
2629 c->state = RTSPSTATE_SEND_REPLY;
2633 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
2634 struct in_addr my_ip)
2636 AVFormatContext *avc;
2637 AVStream avs[MAX_STREAMS];
2640 avc = av_alloc_format_context();
2644 if (stream->title[0] != 0) {
2645 av_strlcpy(avc->title, stream->title, sizeof(avc->title));
2647 av_strlcpy(avc->title, "No Title", sizeof(avc->title));
2649 avc->nb_streams = stream->nb_streams;
2650 if (stream->is_multicast) {
2651 snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2652 inet_ntoa(stream->multicast_ip),
2653 stream->multicast_port, stream->multicast_ttl);
2656 for(i = 0; i < stream->nb_streams; i++) {
2657 avc->streams[i] = &avs[i];
2658 avc->streams[i]->codec = stream->streams[i]->codec;
2660 *pbuffer = av_mallocz(2048);
2661 avf_sdp_create(&avc, 1, *pbuffer, 2048);
2664 return strlen(*pbuffer);
2667 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2669 // rtsp_reply_header(c, RTSP_STATUS_OK);
2670 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2671 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2672 url_fprintf(c->pb, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2673 url_fprintf(c->pb, "\r\n");
2676 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2682 int content_length, len;
2683 struct sockaddr_in my_addr;
2685 /* find which url is asked */
2686 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2691 for(stream = first_stream; stream != NULL; stream = stream->next) {
2692 if (!stream->is_feed &&
2693 stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2694 !strcmp(path, stream->filename)) {
2698 /* no stream found */
2699 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2703 /* prepare the media description in sdp format */
2705 /* get the host IP */
2706 len = sizeof(my_addr);
2707 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2708 content_length = prepare_sdp_description(stream, &content, my_addr.sin_addr);
2709 if (content_length < 0) {
2710 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2713 rtsp_reply_header(c, RTSP_STATUS_OK);
2714 url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
2715 url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
2716 url_fprintf(c->pb, "\r\n");
2717 put_buffer(c->pb, content, content_length);
2720 static HTTPContext *find_rtp_session(const char *session_id)
2724 if (session_id[0] == '\0')
2727 for(c = first_http_ctx; c != NULL; c = c->next) {
2728 if (!strcmp(c->session_id, session_id))
2734 static RTSPTransportField *find_transport(RTSPHeader *h, enum RTSPProtocol protocol)
2736 RTSPTransportField *th;
2739 for(i=0;i<h->nb_transports;i++) {
2740 th = &h->transports[i];
2741 if (th->protocol == protocol)
2747 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
2751 int stream_index, port;
2756 RTSPTransportField *th;
2757 struct sockaddr_in dest_addr;
2758 RTSPActionServerSetup setup;
2760 /* find which url is asked */
2761 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2766 /* now check each stream */
2767 for(stream = first_stream; stream != NULL; stream = stream->next) {
2768 if (!stream->is_feed &&
2769 stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
2770 /* accept aggregate filenames only if single stream */
2771 if (!strcmp(path, stream->filename)) {
2772 if (stream->nb_streams != 1) {
2773 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
2780 for(stream_index = 0; stream_index < stream->nb_streams;
2782 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2783 stream->filename, stream_index);
2784 if (!strcmp(path, buf))
2789 /* no stream found */
2790 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2794 /* generate session id if needed */
2795 if (h->session_id[0] == '\0')
2796 snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
2797 av_random(&random_state), av_random(&random_state));
2799 /* find rtp session, and create it if none found */
2800 rtp_c = find_rtp_session(h->session_id);
2802 /* always prefer UDP */
2803 th = find_transport(h, RTSP_PROTOCOL_RTP_UDP);
2805 th = find_transport(h, RTSP_PROTOCOL_RTP_TCP);
2807 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2812 rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
2815 rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
2819 /* open input stream */
2820 if (open_input_stream(rtp_c, "") < 0) {
2821 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2826 /* test if stream is OK (test needed because several SETUP needs
2827 to be done for a given file) */
2828 if (rtp_c->stream != stream) {
2829 rtsp_reply_error(c, RTSP_STATUS_SERVICE);
2833 /* test if stream is already set up */
2834 if (rtp_c->rtp_ctx[stream_index]) {
2835 rtsp_reply_error(c, RTSP_STATUS_STATE);
2839 /* check transport */
2840 th = find_transport(h, rtp_c->rtp_protocol);
2841 if (!th || (th->protocol == RTSP_PROTOCOL_RTP_UDP &&
2842 th->client_port_min <= 0)) {
2843 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2847 /* setup default options */
2848 setup.transport_option[0] = '\0';
2849 dest_addr = rtp_c->from_addr;
2850 dest_addr.sin_port = htons(th->client_port_min);
2853 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2854 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2858 /* now everything is OK, so we can send the connection parameters */
2859 rtsp_reply_header(c, RTSP_STATUS_OK);
2861 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2863 switch(rtp_c->rtp_protocol) {
2864 case RTSP_PROTOCOL_RTP_UDP:
2865 port = rtp_get_local_port(rtp_c->rtp_handles[stream_index]);
2866 url_fprintf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2867 "client_port=%d-%d;server_port=%d-%d",
2868 th->client_port_min, th->client_port_min + 1,
2871 case RTSP_PROTOCOL_RTP_TCP:
2872 url_fprintf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2873 stream_index * 2, stream_index * 2 + 1);
2878 if (setup.transport_option[0] != '\0')
2879 url_fprintf(c->pb, ";%s", setup.transport_option);
2880 url_fprintf(c->pb, "\r\n");
2883 url_fprintf(c->pb, "\r\n");
2887 /* find an rtp connection by using the session ID. Check consistency
2889 static HTTPContext *find_rtp_session_with_url(const char *url,
2890 const char *session_id)
2898 rtp_c = find_rtp_session(session_id);
2902 /* find which url is asked */
2903 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2907 if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
2908 for(s=0; s<rtp_c->stream->nb_streams; ++s) {
2909 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2910 rtp_c->stream->filename, s);
2911 if(!strncmp(path, buf, sizeof(buf))) {
2912 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2919 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h)
2923 rtp_c = find_rtp_session_with_url(url, h->session_id);
2925 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2929 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
2930 rtp_c->state != HTTPSTATE_WAIT_FEED &&
2931 rtp_c->state != HTTPSTATE_READY) {
2932 rtsp_reply_error(c, RTSP_STATUS_STATE);
2937 /* XXX: seek in stream */
2938 if (h->range_start != AV_NOPTS_VALUE) {
2939 printf("range_start=%0.3f\n", (double)h->range_start / AV_TIME_BASE);
2940 av_seek_frame(rtp_c->fmt_in, -1, h->range_start);
2944 rtp_c->state = HTTPSTATE_SEND_DATA;
2946 /* now everything is OK, so we can send the connection parameters */
2947 rtsp_reply_header(c, RTSP_STATUS_OK);
2949 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2950 url_fprintf(c->pb, "\r\n");
2953 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h)
2957 rtp_c = find_rtp_session_with_url(url, h->session_id);
2959 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2963 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
2964 rtp_c->state != HTTPSTATE_WAIT_FEED) {
2965 rtsp_reply_error(c, RTSP_STATUS_STATE);
2969 rtp_c->state = HTTPSTATE_READY;
2970 rtp_c->first_pts = AV_NOPTS_VALUE;
2971 /* now everything is OK, so we can send the connection parameters */
2972 rtsp_reply_header(c, RTSP_STATUS_OK);
2974 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2975 url_fprintf(c->pb, "\r\n");
2978 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
2981 char session_id[32];
2983 rtp_c = find_rtp_session_with_url(url, h->session_id);
2985 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2989 av_strlcpy(session_id, rtp_c->session_id, sizeof(session_id));
2991 /* abort the session */
2992 close_connection(rtp_c);
2994 /* now everything is OK, so we can send the connection parameters */
2995 rtsp_reply_header(c, RTSP_STATUS_OK);
2997 url_fprintf(c->pb, "Session: %s\r\n", session_id);
2998 url_fprintf(c->pb, "\r\n");
3002 /********************************************************************/
3005 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3006 FFStream *stream, const char *session_id,
3007 enum RTSPProtocol rtp_protocol)
3009 HTTPContext *c = NULL;
3010 const char *proto_str;
3012 /* XXX: should output a warning page when coming
3013 close to the connection limit */
3014 if (nb_connections >= nb_max_connections)
3017 /* add a new connection */
3018 c = av_mallocz(sizeof(HTTPContext));
3023 c->poll_entry = NULL;
3024 c->from_addr = *from_addr;
3025 c->buffer_size = IOBUFFER_INIT_SIZE;
3026 c->buffer = av_malloc(c->buffer_size);
3031 av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
3032 c->state = HTTPSTATE_READY;
3033 c->is_packetized = 1;
3034 c->rtp_protocol = rtp_protocol;
3036 /* protocol is shown in statistics */
3037 switch(c->rtp_protocol) {
3038 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3039 proto_str = "MCAST";
3041 case RTSP_PROTOCOL_RTP_UDP:
3044 case RTSP_PROTOCOL_RTP_TCP:
3051 av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
3052 av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
3054 current_bandwidth += stream->bandwidth;
3056 c->next = first_http_ctx;
3068 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3069 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3071 static int rtp_new_av_stream(HTTPContext *c,
3072 int stream_index, struct sockaddr_in *dest_addr,
3073 HTTPContext *rtsp_c)
3075 AVFormatContext *ctx;
3078 URLContext *h = NULL;
3081 int max_packet_size;
3083 /* now we can open the relevant output stream */
3084 ctx = av_alloc_format_context();
3087 ctx->oformat = guess_format("rtp", NULL, NULL);
3089 st = av_mallocz(sizeof(AVStream));
3092 st->codec= avcodec_alloc_context();
3093 ctx->nb_streams = 1;
3094 ctx->streams[0] = st;
3096 if (!c->stream->feed ||
3097 c->stream->feed == c->stream)
3098 memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3101 c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3103 st->priv_data = NULL;
3105 /* build destination RTP address */
3106 ipaddr = inet_ntoa(dest_addr->sin_addr);
3108 switch(c->rtp_protocol) {
3109 case RTSP_PROTOCOL_RTP_UDP:
3110 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3113 /* XXX: also pass as parameter to function ? */
3114 if (c->stream->is_multicast) {
3116 ttl = c->stream->multicast_ttl;
3119 snprintf(ctx->filename, sizeof(ctx->filename),
3120 "rtp://%s:%d?multicast=1&ttl=%d",
3121 ipaddr, ntohs(dest_addr->sin_port), ttl);
3123 snprintf(ctx->filename, sizeof(ctx->filename),
3124 "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3127 if (url_open(&h, ctx->filename, URL_WRONLY) < 0)
3129 c->rtp_handles[stream_index] = h;
3130 max_packet_size = url_get_max_packet_size(h);
3132 case RTSP_PROTOCOL_RTP_TCP:
3135 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3141 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3142 ipaddr, ntohs(dest_addr->sin_port),
3144 c->stream->filename, stream_index, c->protocol);
3146 /* normally, no packets should be output here, but the packet size may be checked */
3147 if (url_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0) {
3148 /* XXX: close stream */
3151 av_set_parameters(ctx, NULL);
3152 if (av_write_header(ctx) < 0) {
3159 url_close_dyn_buf(ctx->pb, &dummy_buf);
3162 c->rtp_ctx[stream_index] = ctx;
3166 /********************************************************************/
3167 /* ffserver initialization */
3169 static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
3173 fst = av_mallocz(sizeof(AVStream));
3176 fst->codec= avcodec_alloc_context();
3177 fst->priv_data = av_mallocz(sizeof(FeedData));
3178 memcpy(fst->codec, codec, sizeof(AVCodecContext));
3179 fst->index = stream->nb_streams;
3180 av_set_pts_info(fst, 33, 1, 90000);
3181 stream->streams[stream->nb_streams++] = fst;
3185 /* return the stream number in the feed */
3186 static int add_av_stream(FFStream *feed, AVStream *st)
3189 AVCodecContext *av, *av1;
3193 for(i=0;i<feed->nb_streams;i++) {
3194 st = feed->streams[i];
3196 if (av1->codec_id == av->codec_id &&
3197 av1->codec_type == av->codec_type &&
3198 av1->bit_rate == av->bit_rate) {
3200 switch(av->codec_type) {
3201 case CODEC_TYPE_AUDIO:
3202 if (av1->channels == av->channels &&
3203 av1->sample_rate == av->sample_rate)
3206 case CODEC_TYPE_VIDEO:
3207 if (av1->width == av->width &&
3208 av1->height == av->height &&
3209 av1->time_base.den == av->time_base.den &&
3210 av1->time_base.num == av->time_base.num &&
3211 av1->gop_size == av->gop_size)
3220 fst = add_av_stream1(feed, av);
3223 return feed->nb_streams - 1;
3228 static void remove_stream(FFStream *stream)
3232 while (*ps != NULL) {
3240 /* specific mpeg4 handling : we extract the raw parameters */
3241 static void extract_mpeg4_header(AVFormatContext *infile)
3243 int mpeg4_count, i, size;
3249 for(i=0;i<infile->nb_streams;i++) {
3250 st = infile->streams[i];
3251 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3252 st->codec->extradata_size == 0) {
3259 printf("MPEG4 without extra data: trying to find header in %s\n", infile->filename);
3260 while (mpeg4_count > 0) {
3261 if (av_read_packet(infile, &pkt) < 0)
3263 st = infile->streams[pkt.stream_index];
3264 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3265 st->codec->extradata_size == 0) {
3266 av_freep(&st->codec->extradata);
3267 /* fill extradata with the header */
3268 /* XXX: we make hard suppositions here ! */
3270 while (p < pkt.data + pkt.size - 4) {
3271 /* stop when vop header is found */
3272 if (p[0] == 0x00 && p[1] == 0x00 &&
3273 p[2] == 0x01 && p[3] == 0xb6) {
3274 size = p - pkt.data;
3275 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3276 st->codec->extradata = av_malloc(size);
3277 st->codec->extradata_size = size;
3278 memcpy(st->codec->extradata, pkt.data, size);
3285 av_free_packet(&pkt);
3289 /* compute the needed AVStream for each file */
3290 static void build_file_streams(void)
3292 FFStream *stream, *stream_next;
3293 AVFormatContext *infile;
3296 /* gather all streams */
3297 for(stream = first_stream; stream != NULL; stream = stream_next) {
3298 stream_next = stream->next;
3299 if (stream->stream_type == STREAM_TYPE_LIVE &&
3301 /* the stream comes from a file */
3302 /* try to open the file */
3304 stream->ap_in = av_mallocz(sizeof(AVFormatParameters));
3305 if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
3306 /* specific case : if transport stream output to RTP,
3307 we use a raw transport stream reader */
3308 stream->ap_in->mpeg2ts_raw = 1;
3309 stream->ap_in->mpeg2ts_compute_pcr = 1;
3312 if ((ret = av_open_input_file(&infile, stream->feed_filename,
3313 stream->ifmt, 0, stream->ap_in)) < 0) {
3314 http_log("could not open %s: %d\n", stream->feed_filename, ret);
3315 /* remove stream (no need to spend more time on it) */
3317 remove_stream(stream);
3319 /* find all the AVStreams inside and reference them in
3321 if (av_find_stream_info(infile) < 0) {
3322 http_log("Could not find codec parameters from '%s'",
3323 stream->feed_filename);
3324 av_close_input_file(infile);
3327 extract_mpeg4_header(infile);
3329 for(i=0;i<infile->nb_streams;i++)
3330 add_av_stream1(stream, infile->streams[i]->codec);
3332 av_close_input_file(infile);
3338 /* compute the needed AVStream for each feed */
3339 static void build_feed_streams(void)
3341 FFStream *stream, *feed;
3344 /* gather all streams */
3345 for(stream = first_stream; stream != NULL; stream = stream->next) {
3346 feed = stream->feed;
3348 if (!stream->is_feed) {
3349 /* we handle a stream coming from a feed */
3350 for(i=0;i<stream->nb_streams;i++)
3351 stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
3356 /* gather all streams */
3357 for(stream = first_stream; stream != NULL; stream = stream->next) {
3358 feed = stream->feed;
3360 if (stream->is_feed) {
3361 for(i=0;i<stream->nb_streams;i++)
3362 stream->feed_streams[i] = i;
3367 /* create feed files if needed */
3368 for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
3371 if (url_exist(feed->feed_filename)) {
3372 /* See if it matches */
3376 if (av_open_input_file(&s, feed->feed_filename, NULL, FFM_PACKET_SIZE, NULL) >= 0) {
3377 /* Now see if it matches */
3378 if (s->nb_streams == feed->nb_streams) {
3380 for(i=0;i<s->nb_streams;i++) {
3382 sf = feed->streams[i];
3385 if (sf->index != ss->index ||
3387 printf("Index & Id do not match for stream %d (%s)\n",
3388 i, feed->feed_filename);
3391 AVCodecContext *ccf, *ccs;
3395 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3397 if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
3398 printf("Codecs do not match for stream %d\n", i);
3400 } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3401 printf("Codec bitrates do not match for stream %d\n", i);
3403 } else if (ccf->codec_type == CODEC_TYPE_VIDEO) {
3404 if (CHECK_CODEC(time_base.den) ||
3405 CHECK_CODEC(time_base.num) ||
3406 CHECK_CODEC(width) ||
3407 CHECK_CODEC(height)) {
3408 printf("Codec width, height and framerate do not match for stream %d\n", i);
3411 } else if (ccf->codec_type == CODEC_TYPE_AUDIO) {
3412 if (CHECK_CODEC(sample_rate) ||
3413 CHECK_CODEC(channels) ||
3414 CHECK_CODEC(frame_size)) {
3415 printf("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3419 printf("Unknown codec type\n");
3427 printf("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3428 feed->feed_filename, s->nb_streams, feed->nb_streams);
3430 av_close_input_file(s);
3432 printf("Deleting feed file '%s' as it appears to be corrupt\n",
3433 feed->feed_filename);
3436 if (feed->readonly) {
3437 printf("Unable to delete feed file '%s' as it is marked readonly\n",
3438 feed->feed_filename);
3441 unlink(feed->feed_filename);
3444 if (!url_exist(feed->feed_filename)) {
3445 AVFormatContext s1, *s = &s1;
3447 if (feed->readonly) {
3448 printf("Unable to create feed file '%s' as it is marked readonly\n",
3449 feed->feed_filename);
3453 /* only write the header of the ffm file */
3454 if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
3455 fprintf(stderr, "Could not open output feed file '%s'\n",
3456 feed->feed_filename);
3459 s->oformat = feed->fmt;
3460 s->nb_streams = feed->nb_streams;
3461 for(i=0;i<s->nb_streams;i++) {
3463 st = feed->streams[i];
3466 av_set_parameters(s, NULL);
3467 if (av_write_header(s) < 0) {
3468 fprintf(stderr, "Container doesn't supports the required parameters\n");
3471 /* XXX: need better api */
3472 av_freep(&s->priv_data);
3475 /* get feed size and write index */
3476 fd = open(feed->feed_filename, O_RDONLY);
3478 fprintf(stderr, "Could not open output feed file '%s'\n",
3479 feed->feed_filename);
3483 feed->feed_write_index = ffm_read_write_index(fd);
3484 feed->feed_size = lseek(fd, 0, SEEK_END);
3485 /* ensure that we do not wrap before the end of file */
3486 if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3487 feed->feed_max_size = feed->feed_size;
3493 /* compute the bandwidth used by each stream */
3494 static void compute_bandwidth(void)
3499 for(stream = first_stream; stream != NULL; stream = stream->next) {
3501 for(i=0;i<stream->nb_streams;i++) {
3502 AVStream *st = stream->streams[i];
3503 switch(st->codec->codec_type) {
3504 case CODEC_TYPE_AUDIO:
3505 case CODEC_TYPE_VIDEO:
3506 bandwidth += st->codec->bit_rate;
3512 stream->bandwidth = (bandwidth + 999) / 1000;
3516 static void get_arg(char *buf, int buf_size, const char **pp)
3523 while (isspace(*p)) p++;
3526 if (*p == '\"' || *p == '\'')
3538 if ((q - buf) < buf_size - 1)
3543 if (quote && *p == quote)
3548 /* add a codec and set the default parameters */
3549 static void add_codec(FFStream *stream, AVCodecContext *av)
3553 /* compute default parameters */
3554 switch(av->codec_type) {
3555 case CODEC_TYPE_AUDIO:
3556 if (av->bit_rate == 0)
3557 av->bit_rate = 64000;
3558 if (av->sample_rate == 0)
3559 av->sample_rate = 22050;
3560 if (av->channels == 0)
3563 case CODEC_TYPE_VIDEO:
3564 if (av->bit_rate == 0)
3565 av->bit_rate = 64000;
3566 if (av->time_base.num == 0){
3567 av->time_base.den = 5;
3568 av->time_base.num = 1;
3570 if (av->width == 0 || av->height == 0) {
3574 /* Bitrate tolerance is less for streaming */
3575 if (av->bit_rate_tolerance == 0)
3576 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
3577 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
3582 if (av->max_qdiff == 0)
3584 av->qcompress = 0.5;
3587 if (!av->nsse_weight)
3588 av->nsse_weight = 8;
3590 av->frame_skip_cmp = FF_CMP_DCTMAX;
3591 av->me_method = ME_EPZS;
3592 av->rc_buffer_aggressivity = 1.0;
3595 av->rc_eq = "tex^qComp";
3596 if (!av->i_quant_factor)
3597 av->i_quant_factor = -0.8;
3598 if (!av->b_quant_factor)
3599 av->b_quant_factor = 1.25;
3600 if (!av->b_quant_offset)
3601 av->b_quant_offset = 1.25;
3602 if (!av->rc_max_rate)
3603 av->rc_max_rate = av->bit_rate * 2;
3605 if (av->rc_max_rate && !av->rc_buffer_size) {
3606 av->rc_buffer_size = av->rc_max_rate;
3615 st = av_mallocz(sizeof(AVStream));
3618 st->codec = avcodec_alloc_context();
3619 stream->streams[stream->nb_streams++] = st;
3620 memcpy(st->codec, av, sizeof(AVCodecContext));
3623 static int opt_audio_codec(const char *arg)
3625 AVCodec *p= avcodec_find_encoder_by_name(arg);
3627 if (p == NULL || p->type != CODEC_TYPE_AUDIO)
3628 return CODEC_ID_NONE;
3633 static int opt_video_codec(const char *arg)
3635 AVCodec *p= avcodec_find_encoder_by_name(arg);
3637 if (p == NULL || p->type != CODEC_TYPE_VIDEO)
3638 return CODEC_ID_NONE;
3643 /* simplistic plugin support */
3646 static void load_module(const char *filename)
3649 void (*init_func)(void);
3650 dll = dlopen(filename, RTLD_NOW);
3652 fprintf(stderr, "Could not load module '%s' - %s\n",
3653 filename, dlerror());
3657 init_func = dlsym(dll, "ffserver_module_init");
3660 "%s: init function 'ffserver_module_init()' not found\n",
3669 static int parse_ffconfig(const char *filename)
3676 int val, errors, line_num;
3677 FFStream **last_stream, *stream, *redirect;
3678 FFStream **last_feed, *feed;
3679 AVCodecContext audio_enc, video_enc;
3680 int audio_id, video_id;
3682 f = fopen(filename, "r");
3690 first_stream = NULL;
3691 last_stream = &first_stream;
3693 last_feed = &first_feed;
3697 audio_id = CODEC_ID_NONE;
3698 video_id = CODEC_ID_NONE;
3700 if (fgets(line, sizeof(line), f) == NULL)
3706 if (*p == '\0' || *p == '#')
3709 get_arg(cmd, sizeof(cmd), &p);
3711 if (!strcasecmp(cmd, "Port")) {
3712 get_arg(arg, sizeof(arg), &p);
3714 if (val < 1 || val > 65536) {
3715 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3716 filename, line_num, arg);
3719 my_http_addr.sin_port = htons(val);
3720 } else if (!strcasecmp(cmd, "BindAddress")) {
3721 get_arg(arg, sizeof(arg), &p);
3722 if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
3723 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3724 filename, line_num, arg);
3727 } else if (!strcasecmp(cmd, "NoDaemon")) {
3728 ffserver_daemon = 0;
3729 } else if (!strcasecmp(cmd, "RTSPPort")) {
3730 get_arg(arg, sizeof(arg), &p);
3732 if (val < 1 || val > 65536) {
3733 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3734 filename, line_num, arg);
3737 my_rtsp_addr.sin_port = htons(atoi(arg));
3738 } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
3739 get_arg(arg, sizeof(arg), &p);
3740 if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
3741 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3742 filename, line_num, arg);
3745 } else if (!strcasecmp(cmd, "MaxClients")) {
3746 get_arg(arg, sizeof(arg), &p);
3748 if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
3749 fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
3750 filename, line_num, arg);
3753 nb_max_connections = val;
3755 } else if (!strcasecmp(cmd, "MaxBandwidth")) {
3756 get_arg(arg, sizeof(arg), &p);
3758 if (val < 10 || val > 100000) {
3759 fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
3760 filename, line_num, arg);
3763 max_bandwidth = val;
3764 } else if (!strcasecmp(cmd, "CustomLog")) {
3765 get_arg(logfilename, sizeof(logfilename), &p);
3766 } else if (!strcasecmp(cmd, "<Feed")) {
3767 /*********************************************/
3768 /* Feed related options */
3770 if (stream || feed) {
3771 fprintf(stderr, "%s:%d: Already in a tag\n",
3772 filename, line_num);
3774 feed = av_mallocz(sizeof(FFStream));
3775 /* add in stream list */
3776 *last_stream = feed;
3777 last_stream = &feed->next;
3778 /* add in feed list */
3780 last_feed = &feed->next_feed;
3782 get_arg(feed->filename, sizeof(feed->filename), &p);
3783 q = strrchr(feed->filename, '>');
3786 feed->fmt = guess_format("ffm", NULL, NULL);
3787 /* defaut feed file */
3788 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
3789 "/tmp/%s.ffm", feed->filename);
3790 feed->feed_max_size = 5 * 1024 * 1024;
3792 feed->feed = feed; /* self feeding :-) */
3794 } else if (!strcasecmp(cmd, "Launch")) {
3798 feed->child_argv = av_mallocz(64 * sizeof(char *));
3800 for (i = 0; i < 62; i++) {
3801 get_arg(arg, sizeof(arg), &p);
3805 feed->child_argv[i] = av_strdup(arg);
3808 feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
3810 snprintf(feed->child_argv[i], 30+strlen(feed->filename),
3812 (my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
3813 inet_ntoa(my_http_addr.sin_addr),
3814 ntohs(my_http_addr.sin_port), feed->filename);
3819 fprintf(stdout, "Launch commandline: ");
3820 for (j = 0; j <= i; j++)
3821 fprintf(stdout, "%s ", feed->child_argv[j]);
3822 fprintf(stdout, "\n");
3825 } else if (!strcasecmp(cmd, "ReadOnlyFile")) {
3827 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3829 } else if (stream) {
3830 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3832 } else if (!strcasecmp(cmd, "File")) {
3834 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3836 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3837 } else if (!strcasecmp(cmd, "FileMaxSize")) {
3842 get_arg(arg, sizeof(arg), &p);
3844 fsize = strtod(p1, &p1);
3845 switch(toupper(*p1)) {
3850 fsize *= 1024 * 1024;
3853 fsize *= 1024 * 1024 * 1024;
3856 feed->feed_max_size = (int64_t)fsize;
3858 } else if (!strcasecmp(cmd, "</Feed>")) {
3860 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
3861 filename, line_num);
3865 } else if (!strcasecmp(cmd, "<Stream")) {
3866 /*********************************************/
3867 /* Stream related options */
3869 if (stream || feed) {
3870 fprintf(stderr, "%s:%d: Already in a tag\n",
3871 filename, line_num);
3873 stream = av_mallocz(sizeof(FFStream));
3874 *last_stream = stream;
3875 last_stream = &stream->next;
3877 get_arg(stream->filename, sizeof(stream->filename), &p);
3878 q = strrchr(stream->filename, '>');
3881 stream->fmt = guess_stream_format(NULL, stream->filename, NULL);
3882 memset(&audio_enc, 0, sizeof(AVCodecContext));
3883 memset(&video_enc, 0, sizeof(AVCodecContext));
3884 audio_id = CODEC_ID_NONE;
3885 video_id = CODEC_ID_NONE;
3887 audio_id = stream->fmt->audio_codec;
3888 video_id = stream->fmt->video_codec;
3891 } else if (!strcasecmp(cmd, "Feed")) {
3892 get_arg(arg, sizeof(arg), &p);
3897 while (sfeed != NULL) {
3898 if (!strcmp(sfeed->filename, arg))
3900 sfeed = sfeed->next_feed;
3903 fprintf(stderr, "%s:%d: feed '%s' not defined\n",
3904 filename, line_num, arg);
3906 stream->feed = sfeed;
3908 } else if (!strcasecmp(cmd, "Format")) {
3909 get_arg(arg, sizeof(arg), &p);
3911 if (!strcmp(arg, "status")) {
3912 stream->stream_type = STREAM_TYPE_STATUS;
3915 stream->stream_type = STREAM_TYPE_LIVE;
3916 /* jpeg cannot be used here, so use single frame jpeg */
3917 if (!strcmp(arg, "jpeg"))
3918 strcpy(arg, "mjpeg");
3919 stream->fmt = guess_stream_format(arg, NULL, NULL);
3921 fprintf(stderr, "%s:%d: Unknown Format: %s\n",
3922 filename, line_num, arg);
3927 audio_id = stream->fmt->audio_codec;
3928 video_id = stream->fmt->video_codec;
3931 } else if (!strcasecmp(cmd, "InputFormat")) {
3932 get_arg(arg, sizeof(arg), &p);
3933 stream->ifmt = av_find_input_format(arg);
3934 if (!stream->ifmt) {
3935 fprintf(stderr, "%s:%d: Unknown input format: %s\n",
3936 filename, line_num, arg);
3938 } else if (!strcasecmp(cmd, "FaviconURL")) {
3939 if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
3940 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3942 fprintf(stderr, "%s:%d: FaviconURL only permitted for status streams\n",
3943 filename, line_num);
3946 } else if (!strcasecmp(cmd, "Author")) {
3948 get_arg(stream->author, sizeof(stream->author), &p);
3949 } else if (!strcasecmp(cmd, "Comment")) {
3951 get_arg(stream->comment, sizeof(stream->comment), &p);
3952 } else if (!strcasecmp(cmd, "Copyright")) {
3954 get_arg(stream->copyright, sizeof(stream->copyright), &p);
3955 } else if (!strcasecmp(cmd, "Title")) {
3957 get_arg(stream->title, sizeof(stream->title), &p);
3958 } else if (!strcasecmp(cmd, "Preroll")) {
3959 get_arg(arg, sizeof(arg), &p);
3961 stream->prebuffer = atof(arg) * 1000;
3962 } else if (!strcasecmp(cmd, "StartSendOnKey")) {
3964 stream->send_on_key = 1;
3965 } else if (!strcasecmp(cmd, "AudioCodec")) {
3966 get_arg(arg, sizeof(arg), &p);
3967 audio_id = opt_audio_codec(arg);
3968 if (audio_id == CODEC_ID_NONE) {
3969 fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
3970 filename, line_num, arg);
3973 } else if (!strcasecmp(cmd, "VideoCodec")) {
3974 get_arg(arg, sizeof(arg), &p);
3975 video_id = opt_video_codec(arg);
3976 if (video_id == CODEC_ID_NONE) {
3977 fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
3978 filename, line_num, arg);
3981 } else if (!strcasecmp(cmd, "MaxTime")) {
3982 get_arg(arg, sizeof(arg), &p);
3984 stream->max_time = atof(arg) * 1000;
3985 } else if (!strcasecmp(cmd, "AudioBitRate")) {
3986 get_arg(arg, sizeof(arg), &p);
3988 audio_enc.bit_rate = atoi(arg) * 1000;
3989 } else if (!strcasecmp(cmd, "AudioChannels")) {
3990 get_arg(arg, sizeof(arg), &p);
3992 audio_enc.channels = atoi(arg);
3993 } else if (!strcasecmp(cmd, "AudioSampleRate")) {
3994 get_arg(arg, sizeof(arg), &p);
3996 audio_enc.sample_rate = atoi(arg);
3997 } else if (!strcasecmp(cmd, "AudioQuality")) {
3998 get_arg(arg, sizeof(arg), &p);
4000 // audio_enc.quality = atof(arg) * 1000;
4002 } else if (!strcasecmp(cmd, "VideoBitRateRange")) {
4004 int minrate, maxrate;
4006 get_arg(arg, sizeof(arg), &p);
4008 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
4009 video_enc.rc_min_rate = minrate * 1000;
4010 video_enc.rc_max_rate = maxrate * 1000;
4012 fprintf(stderr, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4013 filename, line_num, arg);
4017 } else if (!strcasecmp(cmd, "Debug")) {
4019 get_arg(arg, sizeof(arg), &p);
4020 video_enc.debug = strtol(arg,0,0);
4022 } else if (!strcasecmp(cmd, "Strict")) {
4024 get_arg(arg, sizeof(arg), &p);
4025 video_enc.strict_std_compliance = atoi(arg);
4027 } else if (!strcasecmp(cmd, "VideoBufferSize")) {
4029 get_arg(arg, sizeof(arg), &p);
4030 video_enc.rc_buffer_size = atoi(arg) * 8*1024;
4032 } else if (!strcasecmp(cmd, "VideoBitRateTolerance")) {
4034 get_arg(arg, sizeof(arg), &p);
4035 video_enc.bit_rate_tolerance = atoi(arg) * 1000;
4037 } else if (!strcasecmp(cmd, "VideoBitRate")) {
4038 get_arg(arg, sizeof(arg), &p);
4040 video_enc.bit_rate = atoi(arg) * 1000;
4042 } else if (!strcasecmp(cmd, "VideoSize")) {
4043 get_arg(arg, sizeof(arg), &p);
4045 av_parse_video_frame_size(&video_enc.width, &video_enc.height, arg);
4046 if ((video_enc.width % 16) != 0 ||
4047 (video_enc.height % 16) != 0) {
4048 fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
4049 filename, line_num);
4053 } else if (!strcasecmp(cmd, "VideoFrameRate")) {
4054 get_arg(arg, sizeof(arg), &p);
4056 AVRational frame_rate;
4057 if (av_parse_video_frame_rate(&frame_rate, arg) < 0) {
4058 fprintf(stderr, "Incorrect frame rate\n");
4061 video_enc.time_base.num = frame_rate.den;
4062 video_enc.time_base.den = frame_rate.num;
4065 } else if (!strcasecmp(cmd, "VideoGopSize")) {
4066 get_arg(arg, sizeof(arg), &p);
4068 video_enc.gop_size = atoi(arg);
4069 } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
4071 video_enc.gop_size = 1;
4072 } else if (!strcasecmp(cmd, "VideoHighQuality")) {
4074 video_enc.mb_decision = FF_MB_DECISION_BITS;
4075 } else if (!strcasecmp(cmd, "Video4MotionVector")) {
4077 video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
4078 video_enc.flags |= CODEC_FLAG_4MV;
4080 } else if (!strcasecmp(cmd, "VideoTag")) {
4081 get_arg(arg, sizeof(arg), &p);
4082 if ((strlen(arg) == 4) && stream)
4083 video_enc.codec_tag = ff_get_fourcc(arg);
4084 } else if (!strcasecmp(cmd, "BitExact")) {
4086 video_enc.flags |= CODEC_FLAG_BITEXACT;
4087 } else if (!strcasecmp(cmd, "DctFastint")) {
4089 video_enc.dct_algo = FF_DCT_FASTINT;
4090 } else if (!strcasecmp(cmd, "IdctSimple")) {
4092 video_enc.idct_algo = FF_IDCT_SIMPLE;
4093 } else if (!strcasecmp(cmd, "Qscale")) {
4094 get_arg(arg, sizeof(arg), &p);
4096 video_enc.flags |= CODEC_FLAG_QSCALE;
4097 video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
4099 } else if (!strcasecmp(cmd, "VideoQDiff")) {
4100 get_arg(arg, sizeof(arg), &p);
4102 video_enc.max_qdiff = atoi(arg);
4103 if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
4104 fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
4105 filename, line_num);
4109 } else if (!strcasecmp(cmd, "VideoQMax")) {
4110 get_arg(arg, sizeof(arg), &p);
4112 video_enc.qmax = atoi(arg);
4113 if (video_enc.qmax < 1 || video_enc.qmax > 31) {
4114 fprintf(stderr, "%s:%d: VideoQMax out of range\n",
4115 filename, line_num);
4119 } else if (!strcasecmp(cmd, "VideoQMin")) {
4120 get_arg(arg, sizeof(arg), &p);
4122 video_enc.qmin = atoi(arg);
4123 if (video_enc.qmin < 1 || video_enc.qmin > 31) {
4124 fprintf(stderr, "%s:%d: VideoQMin out of range\n",
4125 filename, line_num);
4129 } else if (!strcasecmp(cmd, "LumaElim")) {
4130 get_arg(arg, sizeof(arg), &p);
4132 video_enc.luma_elim_threshold = atoi(arg);
4133 } else if (!strcasecmp(cmd, "ChromaElim")) {
4134 get_arg(arg, sizeof(arg), &p);
4136 video_enc.chroma_elim_threshold = atoi(arg);
4137 } else if (!strcasecmp(cmd, "LumiMask")) {
4138 get_arg(arg, sizeof(arg), &p);
4140 video_enc.lumi_masking = atof(arg);
4141 } else if (!strcasecmp(cmd, "DarkMask")) {
4142 get_arg(arg, sizeof(arg), &p);
4144 video_enc.dark_masking = atof(arg);
4145 } else if (!strcasecmp(cmd, "NoVideo")) {
4146 video_id = CODEC_ID_NONE;
4147 } else if (!strcasecmp(cmd, "NoAudio")) {
4148 audio_id = CODEC_ID_NONE;
4149 } else if (!strcasecmp(cmd, "ACL")) {
4152 get_arg(arg, sizeof(arg), &p);
4153 if (strcasecmp(arg, "allow") == 0)
4154 acl.action = IP_ALLOW;
4155 else if (strcasecmp(arg, "deny") == 0)
4156 acl.action = IP_DENY;
4158 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4159 filename, line_num, arg);
4163 get_arg(arg, sizeof(arg), &p);
4165 if (resolve_host(&acl.first, arg) != 0) {
4166 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4167 filename, line_num, arg);
4170 acl.last = acl.first;
4172 get_arg(arg, sizeof(arg), &p);
4175 if (resolve_host(&acl.last, arg) != 0) {
4176 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4177 filename, line_num, arg);
4183 IPAddressACL *nacl = av_mallocz(sizeof(*nacl));
4184 IPAddressACL **naclp = 0;
4190 naclp = &stream->acl;
4194 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
4195 filename, line_num);
4201 naclp = &(*naclp)->next;
4206 } else if (!strcasecmp(cmd, "RTSPOption")) {
4207 get_arg(arg, sizeof(arg), &p);
4209 av_freep(&stream->rtsp_option);
4210 stream->rtsp_option = av_strdup(arg);
4212 } else if (!strcasecmp(cmd, "MulticastAddress")) {
4213 get_arg(arg, sizeof(arg), &p);
4215 if (resolve_host(&stream->multicast_ip, arg) != 0) {
4216 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
4217 filename, line_num, arg);
4220 stream->is_multicast = 1;
4221 stream->loop = 1; /* default is looping */
4223 } else if (!strcasecmp(cmd, "MulticastPort")) {
4224 get_arg(arg, sizeof(arg), &p);
4226 stream->multicast_port = atoi(arg);
4227 } else if (!strcasecmp(cmd, "MulticastTTL")) {
4228 get_arg(arg, sizeof(arg), &p);
4230 stream->multicast_ttl = atoi(arg);
4231 } else if (!strcasecmp(cmd, "NoLoop")) {
4234 } else if (!strcasecmp(cmd, "</Stream>")) {
4236 fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
4237 filename, line_num);
4240 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
4241 if (audio_id != CODEC_ID_NONE) {
4242 audio_enc.codec_type = CODEC_TYPE_AUDIO;
4243 audio_enc.codec_id = audio_id;
4244 add_codec(stream, &audio_enc);
4246 if (video_id != CODEC_ID_NONE) {
4247 video_enc.codec_type = CODEC_TYPE_VIDEO;
4248 video_enc.codec_id = video_id;
4249 add_codec(stream, &video_enc);
4254 } else if (!strcasecmp(cmd, "<Redirect")) {
4255 /*********************************************/
4257 if (stream || feed || redirect) {
4258 fprintf(stderr, "%s:%d: Already in a tag\n",
4259 filename, line_num);
4262 redirect = av_mallocz(sizeof(FFStream));
4263 *last_stream = redirect;
4264 last_stream = &redirect->next;
4266 get_arg(redirect->filename, sizeof(redirect->filename), &p);
4267 q = strrchr(redirect->filename, '>');
4270 redirect->stream_type = STREAM_TYPE_REDIRECT;
4272 } else if (!strcasecmp(cmd, "URL")) {
4274 get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
4275 } else if (!strcasecmp(cmd, "</Redirect>")) {
4277 fprintf(stderr, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4278 filename, line_num);
4281 if (!redirect->feed_filename[0]) {
4282 fprintf(stderr, "%s:%d: No URL found for <Redirect>\n",
4283 filename, line_num);
4288 } else if (!strcasecmp(cmd, "LoadModule")) {
4289 get_arg(arg, sizeof(arg), &p);
4293 fprintf(stderr, "%s:%d: Module support not compiled into this version: '%s'\n",
4294 filename, line_num, arg);
4298 fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
4299 filename, line_num, cmd);
4311 static void handle_child_exit(int sig)
4316 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
4319 for (feed = first_feed; feed; feed = feed->next) {
4320 if (feed->pid == pid) {
4321 int uptime = time(0) - feed->pid_start;
4324 fprintf(stderr, "%s: Pid %d exited with status %d after %d seconds\n", feed->filename, pid, status, uptime);
4327 /* Turn off any more restarts */
4328 feed->child_argv = 0;
4333 need_to_start_children = 1;
4336 static void opt_debug()
4339 ffserver_daemon = 0;
4342 static void opt_show_help(void)
4344 printf("usage: ffserver [options]\n"
4345 "Hyper fast multi format Audio/Video streaming server\n");
4347 show_help_options(options, "Main options:\n", 0, 0);
4350 static const OptionDef options[] = {
4351 { "h", OPT_EXIT, {(void*)opt_show_help}, "show help" },
4352 { "version", OPT_EXIT, {(void*)show_version}, "show version" },
4353 { "L", OPT_EXIT, {(void*)show_license}, "show license" },
4354 { "formats", OPT_EXIT, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
4355 { "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
4356 { "d", 0, {(void*)opt_debug}, "enable debug mode" },
4357 { "f", HAS_ARG | OPT_STRING, {(void*)&config_filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
4361 int main(int argc, char **argv)
4363 struct sigaction sigact;
4369 config_filename = "/etc/ffserver.conf";
4371 my_program_name = argv[0];
4372 my_program_dir = getcwd(0, 0);
4373 ffserver_daemon = 1;
4375 parse_options(argc, argv, options, NULL);
4377 putenv("http_proxy"); /* Kill the http_proxy */
4379 av_init_random(av_gettime() + (getpid() << 16), &random_state);
4381 /* address on which the server will handle HTTP connections */
4382 my_http_addr.sin_family = AF_INET;
4383 my_http_addr.sin_port = htons (8080);
4384 my_http_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4386 /* address on which the server will handle RTSP connections */
4387 my_rtsp_addr.sin_family = AF_INET;
4388 my_rtsp_addr.sin_port = htons (5454);
4389 my_rtsp_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4391 nb_max_connections = 5;
4392 max_bandwidth = 1000;
4393 first_stream = NULL;
4394 logfilename[0] = '\0';
4396 memset(&sigact, 0, sizeof(sigact));
4397 sigact.sa_handler = handle_child_exit;
4398 sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
4399 sigaction(SIGCHLD, &sigact, 0);
4401 if (parse_ffconfig(config_filename) < 0) {
4402 fprintf(stderr, "Incorrect config file - exiting.\n");
4406 build_file_streams();
4408 build_feed_streams();
4410 compute_bandwidth();
4412 /* put the process in background and detach it from its TTY */
4413 if (ffserver_daemon) {
4420 } else if (pid > 0) {
4428 open("/dev/null", O_RDWR);
4429 if (strcmp(logfilename, "-") != 0) {
4439 signal(SIGPIPE, SIG_IGN);
4441 /* open log file if needed */
4442 if (logfilename[0] != '\0') {
4443 if (!strcmp(logfilename, "-"))
4446 logfile = fopen(logfilename, "a");
4449 if (http_server() < 0) {
4450 fprintf(stderr, "Could not start server\n");