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
21 #define HAVE_AV_CONFIG_H
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_POLL_H
33 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
52 /* maximum number of simultaneous HTTP connections */
53 #define HTTP_MAX_CONNECTIONS 2000
56 HTTPSTATE_WAIT_REQUEST,
57 HTTPSTATE_SEND_HEADER,
58 HTTPSTATE_SEND_DATA_HEADER,
59 HTTPSTATE_SEND_DATA, /* sending TCP or UDP data */
60 HTTPSTATE_SEND_DATA_TRAILER,
61 HTTPSTATE_RECEIVE_DATA,
62 HTTPSTATE_WAIT_FEED, /* wait for data from the feed */
65 RTSPSTATE_WAIT_REQUEST,
67 RTSPSTATE_SEND_PACKET,
70 const char *http_state[] = {
86 #define IOBUFFER_INIT_SIZE 8192
88 /* timeouts are in ms */
89 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
90 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
92 #define SYNC_TIMEOUT (10 * 1000)
95 int64_t count1, count2;
99 /* context associated with one connection */
100 typedef struct HTTPContext {
101 enum HTTPState state;
102 int fd; /* socket file descriptor */
103 struct sockaddr_in from_addr; /* origin */
104 struct pollfd *poll_entry; /* used when polling */
106 uint8_t *buffer_ptr, *buffer_end;
109 struct HTTPContext *next;
110 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
114 /* input format handling */
115 AVFormatContext *fmt_in;
116 int64_t start_time; /* In milliseconds - this wraps fairly often */
117 int64_t first_pts; /* initial pts value */
118 int64_t cur_pts; /* current pts value from the stream in us */
119 int64_t cur_frame_duration; /* duration of the current frame in us */
120 int cur_frame_bytes; /* output frame size, needed to compute
121 the time at which we send each
123 int pts_stream_index; /* stream we choose as clock reference */
124 int64_t cur_clock; /* current clock reference value in us */
125 /* output format handling */
126 struct FFStream *stream;
127 /* -1 is invalid stream */
128 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
129 int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */
131 AVFormatContext fmt_ctx; /* instance of FFStream for one user */
132 int last_packet_sent; /* true if last data packet was sent */
134 DataRateData datarate;
141 int is_packetized; /* if true, the stream is packetized */
142 int packet_stream_index; /* current stream for output in state machine */
144 /* RTSP state specific */
145 uint8_t *pb_buffer; /* XXX: use that in all the code */
147 int seq; /* RTSP sequence number */
149 /* RTP state specific */
150 enum RTSPProtocol rtp_protocol;
151 char session_id[32]; /* session id */
152 AVFormatContext *rtp_ctx[MAX_STREAMS];
154 /* RTP/UDP specific */
155 URLContext *rtp_handles[MAX_STREAMS];
157 /* RTP/TCP specific */
158 struct HTTPContext *rtsp_c;
159 uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
162 static AVFrame dummy_frame;
164 /* each generated stream is described here */
168 STREAM_TYPE_REDIRECT,
171 enum IPAddressAction {
176 typedef struct IPAddressACL {
177 struct IPAddressACL *next;
178 enum IPAddressAction action;
179 /* These are in host order */
180 struct in_addr first;
184 /* description of each stream of the ffserver.conf file */
185 typedef struct FFStream {
186 enum StreamType stream_type;
187 char filename[1024]; /* stream filename */
188 struct FFStream *feed; /* feed we are using (can be null if
190 AVFormatParameters *ap_in; /* input parameters */
191 AVInputFormat *ifmt; /* if non NULL, force input format */
195 int prebuffer; /* Number of millseconds early to start */
196 int64_t max_time; /* Number of milliseconds to run */
198 AVStream *streams[MAX_STREAMS];
199 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
200 char feed_filename[1024]; /* file name of the feed storage, or
201 input file name for a stream */
206 pid_t pid; /* Of ffmpeg process */
207 time_t pid_start; /* Of ffmpeg process */
209 struct FFStream *next;
210 int bandwidth; /* bandwidth, in kbits/s */
213 /* multicast specific */
215 struct in_addr multicast_ip;
216 int multicast_port; /* first port used for multicast */
218 int loop; /* if true, send the stream in loops (only meaningful if file) */
221 int feed_opened; /* true if someone is writing to the feed */
222 int is_feed; /* true if it is a feed */
223 int readonly; /* True if writing is prohibited to the file */
225 int64_t bytes_served;
226 int64_t feed_max_size; /* maximum storage size, zero means unlimited */
227 int64_t feed_write_index; /* current write position in feed (it wraps round) */
228 int64_t feed_size; /* current size of feed */
229 struct FFStream *next_feed;
232 typedef struct FeedData {
233 long long data_count;
234 float avg_frame_size; /* frame size averraged over last frames with exponential mean */
237 static struct sockaddr_in my_http_addr;
238 static struct sockaddr_in my_rtsp_addr;
240 static char logfilename[1024];
241 static HTTPContext *first_http_ctx;
242 static FFStream *first_feed; /* contains only feeds */
243 static FFStream *first_stream; /* contains all streams, including feeds */
245 static void new_connection(int server_fd, int is_rtsp);
246 static void close_connection(HTTPContext *c);
249 static int handle_connection(HTTPContext *c);
250 static int http_parse_request(HTTPContext *c);
251 static int http_send_data(HTTPContext *c);
252 static void compute_stats(HTTPContext *c);
253 static int open_input_stream(HTTPContext *c, const char *info);
254 static int http_start_receive_data(HTTPContext *c);
255 static int http_receive_data(HTTPContext *c);
258 static int rtsp_parse_request(HTTPContext *c);
259 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
260 static void rtsp_cmd_options(HTTPContext *c, const char *url);
261 static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPHeader *h);
262 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h);
263 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h);
264 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h);
267 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
268 struct in_addr my_ip);
271 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
272 FFStream *stream, const char *session_id,
273 enum RTSPProtocol rtp_protocol);
274 static int rtp_new_av_stream(HTTPContext *c,
275 int stream_index, struct sockaddr_in *dest_addr,
276 HTTPContext *rtsp_c);
278 static const char *my_program_name;
279 static const char *my_program_dir;
281 static int ffserver_debug;
282 static int ffserver_daemon;
283 static int no_launch;
284 static int need_to_start_children;
286 static int nb_max_connections;
287 static int nb_connections;
289 static int max_bandwidth;
290 static int current_bandwidth;
292 static int64_t cur_time; // Making this global saves on passing it around everywhere
294 static AVRandomState random_state;
296 static FILE *logfile = NULL;
298 static void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
304 vfprintf(logfile, fmt, ap);
310 static char *ctime1(char *buf2)
318 p = buf2 + strlen(p) - 1;
324 static void log_connection(HTTPContext *c)
331 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64"\n",
332 inet_ntoa(c->from_addr.sin_addr),
333 ctime1(buf2), c->method, c->url,
334 c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
337 static void update_datarate(DataRateData *drd, int64_t count)
339 if (!drd->time1 && !drd->count1) {
340 drd->time1 = drd->time2 = cur_time;
341 drd->count1 = drd->count2 = count;
343 if (cur_time - drd->time2 > 5000) {
344 drd->time1 = drd->time2;
345 drd->count1 = drd->count2;
346 drd->time2 = cur_time;
352 /* In bytes per second */
353 static int compute_datarate(DataRateData *drd, int64_t count)
355 if (cur_time == drd->time1)
358 return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
362 static void start_children(FFStream *feed)
367 for (; feed; feed = feed->next) {
368 if (feed->child_argv && !feed->pid) {
369 feed->pid_start = time(0);
374 fprintf(stderr, "Unable to create children\n");
383 for (i = 3; i < 256; i++) {
387 if (!ffserver_debug) {
388 i = open("/dev/null", O_RDWR);
397 pstrcpy(pathname, sizeof(pathname), my_program_name);
399 slash = strrchr(pathname, '/');
405 strcpy(slash, "ffmpeg");
407 /* This is needed to make relative pathnames work */
408 chdir(my_program_dir);
410 signal(SIGPIPE, SIG_DFL);
412 execvp(pathname, feed->child_argv);
420 /* open a listening socket */
421 static int socket_open_listen(struct sockaddr_in *my_addr)
425 server_fd = socket(AF_INET,SOCK_STREAM,0);
432 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
434 if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
436 snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
438 closesocket(server_fd);
442 if (listen (server_fd, 5) < 0) {
444 closesocket(server_fd);
447 fcntl(server_fd, F_SETFL, O_NONBLOCK);
452 /* start all multicast streams */
453 static void start_multicast(void)
458 struct sockaddr_in dest_addr;
459 int default_port, stream_index;
462 for(stream = first_stream; stream != NULL; stream = stream->next) {
463 if (stream->is_multicast) {
464 /* open the RTP connection */
465 snprintf(session_id, sizeof(session_id), "%08x%08x",
466 av_random(&random_state), av_random(&random_state));
468 /* choose a port if none given */
469 if (stream->multicast_port == 0) {
470 stream->multicast_port = default_port;
474 dest_addr.sin_family = AF_INET;
475 dest_addr.sin_addr = stream->multicast_ip;
476 dest_addr.sin_port = htons(stream->multicast_port);
478 rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
479 RTSP_PROTOCOL_RTP_UDP_MULTICAST);
483 if (open_input_stream(rtp_c, "") < 0) {
484 fprintf(stderr, "Could not open input stream for stream '%s'\n",
489 /* open each RTP stream */
490 for(stream_index = 0; stream_index < stream->nb_streams;
492 dest_addr.sin_port = htons(stream->multicast_port +
494 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) < 0) {
495 fprintf(stderr, "Could not open output stream '%s/streamid=%d'\n",
496 stream->filename, stream_index);
501 /* change state to send data */
502 rtp_c->state = HTTPSTATE_SEND_DATA;
507 /* main loop of the http server */
508 static int http_server(void)
510 int server_fd, ret, rtsp_server_fd, delay, delay1;
511 struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 2], *poll_entry;
512 HTTPContext *c, *c_next;
514 server_fd = socket_open_listen(&my_http_addr);
518 rtsp_server_fd = socket_open_listen(&my_rtsp_addr);
519 if (rtsp_server_fd < 0)
522 http_log("ffserver started.\n");
524 start_children(first_feed);
526 first_http_ctx = NULL;
532 poll_entry = poll_table;
533 poll_entry->fd = server_fd;
534 poll_entry->events = POLLIN;
537 poll_entry->fd = rtsp_server_fd;
538 poll_entry->events = POLLIN;
541 /* wait for events on each HTTP handle */
548 case HTTPSTATE_SEND_HEADER:
549 case RTSPSTATE_SEND_REPLY:
550 case RTSPSTATE_SEND_PACKET:
551 c->poll_entry = poll_entry;
553 poll_entry->events = POLLOUT;
556 case HTTPSTATE_SEND_DATA_HEADER:
557 case HTTPSTATE_SEND_DATA:
558 case HTTPSTATE_SEND_DATA_TRAILER:
559 if (!c->is_packetized) {
560 /* for TCP, we output as much as we can (may need to put a limit) */
561 c->poll_entry = poll_entry;
563 poll_entry->events = POLLOUT;
566 /* when ffserver is doing the timing, we work by
567 looking at which packet need to be sent every
569 delay1 = 10; /* one tick wait XXX: 10 ms assumed */
574 case HTTPSTATE_WAIT_REQUEST:
575 case HTTPSTATE_RECEIVE_DATA:
576 case HTTPSTATE_WAIT_FEED:
577 case RTSPSTATE_WAIT_REQUEST:
578 /* need to catch errors */
579 c->poll_entry = poll_entry;
581 poll_entry->events = POLLIN;/* Maybe this will work */
585 c->poll_entry = NULL;
591 /* wait for an event on one connection. We poll at least every
592 second to handle timeouts */
594 ret = poll(poll_table, poll_entry - poll_table, delay);
595 if (ret < 0 && errno != EAGAIN && errno != EINTR)
599 cur_time = av_gettime() / 1000;
601 if (need_to_start_children) {
602 need_to_start_children = 0;
603 start_children(first_feed);
606 /* now handle the events */
607 for(c = first_http_ctx; c != NULL; c = c_next) {
609 if (handle_connection(c) < 0) {
610 /* close and free the connection */
616 poll_entry = poll_table;
617 /* new HTTP connection request ? */
618 if (poll_entry->revents & POLLIN) {
619 new_connection(server_fd, 0);
622 /* new RTSP connection request ? */
623 if (poll_entry->revents & POLLIN) {
624 new_connection(rtsp_server_fd, 1);
629 /* start waiting for a new HTTP/RTSP request */
630 static void start_wait_request(HTTPContext *c, int is_rtsp)
632 c->buffer_ptr = c->buffer;
633 c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
636 c->timeout = cur_time + RTSP_REQUEST_TIMEOUT;
637 c->state = RTSPSTATE_WAIT_REQUEST;
639 c->timeout = cur_time + HTTP_REQUEST_TIMEOUT;
640 c->state = HTTPSTATE_WAIT_REQUEST;
644 static void new_connection(int server_fd, int is_rtsp)
646 struct sockaddr_in from_addr;
648 HTTPContext *c = NULL;
650 len = sizeof(from_addr);
651 fd = accept(server_fd, (struct sockaddr *)&from_addr,
655 fcntl(fd, F_SETFL, O_NONBLOCK);
657 /* XXX: should output a warning page when coming
658 close to the connection limit */
659 if (nb_connections >= nb_max_connections)
662 /* add a new connection */
663 c = av_mallocz(sizeof(HTTPContext));
668 c->poll_entry = NULL;
669 c->from_addr = from_addr;
670 c->buffer_size = IOBUFFER_INIT_SIZE;
671 c->buffer = av_malloc(c->buffer_size);
675 c->next = first_http_ctx;
679 start_wait_request(c, is_rtsp);
691 static void close_connection(HTTPContext *c)
693 HTTPContext **cp, *c1;
695 AVFormatContext *ctx;
699 /* remove connection from list */
700 cp = &first_http_ctx;
701 while ((*cp) != NULL) {
710 /* remove references, if any (XXX: do it faster) */
711 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
716 /* remove connection associated resources */
720 /* close each frame parser */
721 for(i=0;i<c->fmt_in->nb_streams;i++) {
722 st = c->fmt_in->streams[i];
723 if (st->codec->codec) {
724 avcodec_close(st->codec);
727 av_close_input_file(c->fmt_in);
730 /* free RTP output streams if any */
733 nb_streams = c->stream->nb_streams;
735 for(i=0;i<nb_streams;i++) {
738 av_write_trailer(ctx);
741 h = c->rtp_handles[i];
749 if (!c->last_packet_sent) {
752 if (url_open_dyn_buf(&ctx->pb) >= 0) {
753 av_write_trailer(ctx);
754 url_close_dyn_buf(&ctx->pb, &c->pb_buffer);
759 for(i=0; i<ctx->nb_streams; i++)
760 av_free(ctx->streams[i]) ;
762 if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
763 current_bandwidth -= c->stream->bandwidth;
765 /* signal that there is no feed if we are the feeder socket */
766 if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
767 c->stream->feed_opened = 0;
771 av_freep(&c->pb_buffer);
772 av_freep(&c->packet_buffer);
778 static int handle_connection(HTTPContext *c)
783 case HTTPSTATE_WAIT_REQUEST:
784 case RTSPSTATE_WAIT_REQUEST:
786 if ((c->timeout - cur_time) < 0)
788 if (c->poll_entry->revents & (POLLERR | POLLHUP))
791 /* no need to read if no events */
792 if (!(c->poll_entry->revents & POLLIN))
796 len = recv(c->fd, c->buffer_ptr, 1, 0);
798 if (errno != EAGAIN && errno != EINTR)
800 } else if (len == 0) {
803 /* search for end of request. */
805 c->buffer_ptr += len;
807 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
808 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
809 /* request found : parse it and reply */
810 if (c->state == HTTPSTATE_WAIT_REQUEST) {
811 ret = http_parse_request(c);
813 ret = rtsp_parse_request(c);
817 } else if (ptr >= c->buffer_end) {
818 /* request too long: cannot do anything */
820 } else goto read_loop;
824 case HTTPSTATE_SEND_HEADER:
825 if (c->poll_entry->revents & (POLLERR | POLLHUP))
828 /* no need to write if no events */
829 if (!(c->poll_entry->revents & POLLOUT))
831 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
833 if (errno != EAGAIN && errno != EINTR) {
834 /* error : close connection */
835 av_freep(&c->pb_buffer);
839 c->buffer_ptr += len;
841 c->stream->bytes_served += len;
842 c->data_count += len;
843 if (c->buffer_ptr >= c->buffer_end) {
844 av_freep(&c->pb_buffer);
849 /* all the buffer was sent : synchronize to the incoming stream */
850 c->state = HTTPSTATE_SEND_DATA_HEADER;
851 c->buffer_ptr = c->buffer_end = c->buffer;
856 case HTTPSTATE_SEND_DATA:
857 case HTTPSTATE_SEND_DATA_HEADER:
858 case HTTPSTATE_SEND_DATA_TRAILER:
859 /* for packetized output, we consider we can always write (the
860 input streams sets the speed). It may be better to verify
861 that we do not rely too much on the kernel queues */
862 if (!c->is_packetized) {
863 if (c->poll_entry->revents & (POLLERR | POLLHUP))
866 /* no need to read if no events */
867 if (!(c->poll_entry->revents & POLLOUT))
870 if (http_send_data(c) < 0)
872 /* close connection if trailer sent */
873 if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
876 case HTTPSTATE_RECEIVE_DATA:
877 /* no need to read if no events */
878 if (c->poll_entry->revents & (POLLERR | POLLHUP))
880 if (!(c->poll_entry->revents & POLLIN))
882 if (http_receive_data(c) < 0)
885 case HTTPSTATE_WAIT_FEED:
886 /* no need to read if no events */
887 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
890 /* nothing to do, we'll be waken up by incoming feed packets */
893 case RTSPSTATE_SEND_REPLY:
894 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
895 av_freep(&c->pb_buffer);
898 /* no need to write if no events */
899 if (!(c->poll_entry->revents & POLLOUT))
901 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
903 if (errno != EAGAIN && errno != EINTR) {
904 /* error : close connection */
905 av_freep(&c->pb_buffer);
909 c->buffer_ptr += len;
910 c->data_count += len;
911 if (c->buffer_ptr >= c->buffer_end) {
912 /* all the buffer was sent : wait for a new request */
913 av_freep(&c->pb_buffer);
914 start_wait_request(c, 1);
918 case RTSPSTATE_SEND_PACKET:
919 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
920 av_freep(&c->packet_buffer);
923 /* no need to write if no events */
924 if (!(c->poll_entry->revents & POLLOUT))
926 len = send(c->fd, c->packet_buffer_ptr,
927 c->packet_buffer_end - c->packet_buffer_ptr, 0);
929 if (errno != EAGAIN && errno != EINTR) {
930 /* error : close connection */
931 av_freep(&c->packet_buffer);
935 c->packet_buffer_ptr += len;
936 if (c->packet_buffer_ptr >= c->packet_buffer_end) {
937 /* all the buffer was sent : wait for a new request */
938 av_freep(&c->packet_buffer);
939 c->state = RTSPSTATE_WAIT_REQUEST;
943 case HTTPSTATE_READY:
952 static int extract_rates(char *rates, int ratelen, const char *request)
956 for (p = request; *p && *p != '\r' && *p != '\n'; ) {
957 if (strncasecmp(p, "Pragma:", 7) == 0) {
958 const char *q = p + 7;
960 while (*q && *q != '\n' && isspace(*q))
963 if (strncasecmp(q, "stream-switch-entry=", 20) == 0) {
969 memset(rates, 0xff, ratelen);
972 while (*q && *q != '\n' && *q != ':')
975 if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2) {
979 if (stream_no < ratelen && stream_no >= 0) {
980 rates[stream_no] = rate_no;
983 while (*q && *q != '\n' && !isspace(*q))
1000 static int find_stream_in_feed(FFStream *feed, AVCodecContext *codec, int bit_rate)
1003 int best_bitrate = 100000000;
1006 for (i = 0; i < feed->nb_streams; i++) {
1007 AVCodecContext *feed_codec = feed->streams[i]->codec;
1009 if (feed_codec->codec_id != codec->codec_id ||
1010 feed_codec->sample_rate != codec->sample_rate ||
1011 feed_codec->width != codec->width ||
1012 feed_codec->height != codec->height) {
1016 /* Potential stream */
1018 /* We want the fastest stream less than bit_rate, or the slowest
1019 * faster than bit_rate
1022 if (feed_codec->bit_rate <= bit_rate) {
1023 if (best_bitrate > bit_rate || feed_codec->bit_rate > best_bitrate) {
1024 best_bitrate = feed_codec->bit_rate;
1028 if (feed_codec->bit_rate < best_bitrate) {
1029 best_bitrate = feed_codec->bit_rate;
1038 static int modify_current_stream(HTTPContext *c, char *rates)
1041 FFStream *req = c->stream;
1042 int action_required = 0;
1044 /* Not much we can do for a feed */
1048 for (i = 0; i < req->nb_streams; i++) {
1049 AVCodecContext *codec = req->streams[i]->codec;
1053 c->switch_feed_streams[i] = req->feed_streams[i];
1056 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1059 /* Wants off or slow */
1060 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1062 /* This doesn't work well when it turns off the only stream! */
1063 c->switch_feed_streams[i] = -2;
1064 c->feed_streams[i] = -2;
1069 if (c->switch_feed_streams[i] >= 0 && c->switch_feed_streams[i] != c->feed_streams[i])
1070 action_required = 1;
1073 return action_required;
1077 static void do_switch_stream(HTTPContext *c, int i)
1079 if (c->switch_feed_streams[i] >= 0) {
1081 c->feed_streams[i] = c->switch_feed_streams[i];
1084 /* Now update the stream */
1086 c->switch_feed_streams[i] = -1;
1089 /* XXX: factorize in utils.c ? */
1090 /* XXX: take care with different space meaning */
1091 static void skip_spaces(const char **pp)
1095 while (*p == ' ' || *p == '\t')
1100 static void get_word(char *buf, int buf_size, const char **pp)
1108 while (!isspace(*p) && *p != '\0') {
1109 if ((q - buf) < buf_size - 1)
1118 static int validate_acl(FFStream *stream, HTTPContext *c)
1120 enum IPAddressAction last_action = IP_DENY;
1122 struct in_addr *src = &c->from_addr.sin_addr;
1123 unsigned long src_addr = src->s_addr;
1125 for (acl = stream->acl; acl; acl = acl->next) {
1126 if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr) {
1127 return (acl->action == IP_ALLOW) ? 1 : 0;
1129 last_action = acl->action;
1132 /* Nothing matched, so return not the last action */
1133 return (last_action == IP_DENY) ? 1 : 0;
1136 /* compute the real filename of a file by matching it without its
1137 extensions to all the stream filenames */
1138 static void compute_real_filename(char *filename, int max_size)
1145 /* compute filename by matching without the file extensions */
1146 pstrcpy(file1, sizeof(file1), filename);
1147 p = strrchr(file1, '.');
1150 for(stream = first_stream; stream != NULL; stream = stream->next) {
1151 pstrcpy(file2, sizeof(file2), stream->filename);
1152 p = strrchr(file2, '.');
1155 if (!strcmp(file1, file2)) {
1156 pstrcpy(filename, max_size, stream->filename);
1171 /* parse http request and prepare header */
1172 static int http_parse_request(HTTPContext *c)
1175 enum RedirType redir_type;
1177 char info[1024], filename[1024];
1181 const char *mime_type;
1185 char *useragent = 0;
1188 get_word(cmd, sizeof(cmd), (const char **)&p);
1189 pstrcpy(c->method, sizeof(c->method), cmd);
1191 if (!strcmp(cmd, "GET"))
1193 else if (!strcmp(cmd, "POST"))
1198 get_word(url, sizeof(url), (const char **)&p);
1199 pstrcpy(c->url, sizeof(c->url), url);
1201 get_word(protocol, sizeof(protocol), (const char **)&p);
1202 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1205 pstrcpy(c->protocol, sizeof(c->protocol), protocol);
1208 http_log("New connection: %s %s\n", cmd, url);
1210 /* find the filename and the optional info string in the request */
1211 p = strchr(url, '?');
1213 pstrcpy(info, sizeof(info), p);
1219 pstrcpy(filename, sizeof(filename)-1, url + ((*url == '/') ? 1 : 0));
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 pstrcpy(filename, sizeof(filename) - 1, "index.html");
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, dont let start an 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;
1311 if (c->post == 0 && max_bandwidth < current_bandwidth) {
1312 c->http_error = 200;
1314 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 Server too busy\r\n");
1315 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1316 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1317 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Too busy</title></head><body>\r\n");
1318 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");
1319 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",
1320 current_bandwidth, max_bandwidth);
1321 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1323 /* prepare output buffer */
1324 c->buffer_ptr = c->buffer;
1326 c->state = HTTPSTATE_SEND_HEADER;
1330 if (redir_type != REDIR_NONE) {
1333 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1334 if (strncasecmp(p, "Host:", 5) == 0) {
1338 p = strchr(p, '\n');
1349 while (isspace(*hostinfo))
1352 eoh = strchr(hostinfo, '\n');
1354 if (eoh[-1] == '\r')
1357 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1358 memcpy(hostbuf, hostinfo, eoh - hostinfo);
1359 hostbuf[eoh - hostinfo] = 0;
1361 c->http_error = 200;
1363 switch(redir_type) {
1365 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASX Follows\r\n");
1366 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1367 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1368 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ASX Version=\"3\">\r\n");
1369 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1370 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1371 hostbuf, filename, info);
1372 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</ASX>\r\n");
1375 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RAM Follows\r\n");
1376 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: audio/x-pn-realaudio\r\n");
1377 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1378 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "# Autogenerated by ffserver\r\n");
1379 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "http://%s/%s%s\r\n",
1380 hostbuf, filename, info);
1383 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASF Redirect follows\r\n");
1384 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1385 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1386 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "[Reference]\r\n");
1387 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Ref1=http://%s/%s%s\r\n",
1388 hostbuf, filename, info);
1392 char hostname[256], *p;
1393 /* extract only hostname */
1394 pstrcpy(hostname, sizeof(hostname), hostbuf);
1395 p = strrchr(hostname, ':');
1398 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1399 /* XXX: incorrect mime type ? */
1400 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/x-rtsp\r\n");
1401 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1402 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "rtsp://%s:%d/%s\r\n",
1403 hostname, ntohs(my_rtsp_addr.sin_port),
1410 int sdp_data_size, len;
1411 struct sockaddr_in my_addr;
1413 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1414 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/sdp\r\n");
1415 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1417 len = sizeof(my_addr);
1418 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
1420 /* XXX: should use a dynamic buffer */
1421 sdp_data_size = prepare_sdp_description(stream,
1424 if (sdp_data_size > 0) {
1425 memcpy(q, sdp_data, sdp_data_size);
1437 /* prepare output buffer */
1438 c->buffer_ptr = c->buffer;
1440 c->state = HTTPSTATE_SEND_HEADER;
1446 snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
1450 stream->conns_served++;
1452 /* XXX: add there authenticate and IP match */
1455 /* if post, it means a feed is being sent */
1456 if (!stream->is_feed) {
1457 /* However it might be a status report from WMP! Lets log the data
1458 * as it might come in handy one day
1463 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1464 if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1468 if (strncasecmp(p, "Pragma: client-id=", 18) == 0) {
1469 client_id = strtol(p + 18, 0, 10);
1471 p = strchr(p, '\n');
1479 char *eol = strchr(logline, '\n');
1484 if (eol[-1] == '\r')
1486 http_log("%.*s\n", (int) (eol - logline), logline);
1487 c->suppress_log = 1;
1492 http_log("\nGot request:\n%s\n", c->buffer);
1495 if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1498 /* Now we have to find the client_id */
1499 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1500 if (wmpc->wmp_client_id == client_id)
1505 if (modify_current_stream(wmpc, ratebuf)) {
1506 wmpc->switch_pending = 1;
1511 snprintf(msg, sizeof(msg), "POST command not handled");
1515 if (http_start_receive_data(c) < 0) {
1516 snprintf(msg, sizeof(msg), "could not open feed");
1520 c->state = HTTPSTATE_RECEIVE_DATA;
1525 if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0) {
1526 http_log("\nGot request:\n%s\n", c->buffer);
1530 if (c->stream->stream_type == STREAM_TYPE_STATUS)
1533 /* open input stream */
1534 if (open_input_stream(c, info) < 0) {
1535 snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1539 /* prepare http header */
1541 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1542 mime_type = c->stream->fmt->mime_type;
1544 mime_type = "application/x-octet-stream";
1545 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
1547 /* for asf, we need extra headers */
1548 if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1549 /* Need to allocate a client id */
1551 c->wmp_client_id = av_random(&random_state) & 0x7fffffff;
1553 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);
1555 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-Type: %s\r\n", mime_type);
1556 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1558 /* prepare output buffer */
1560 c->buffer_ptr = c->buffer;
1562 c->state = HTTPSTATE_SEND_HEADER;
1565 c->http_error = 404;
1567 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 404 Not Found\r\n");
1568 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: %s\r\n", "text/html");
1569 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1570 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HTML>\n");
1571 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1572 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<BODY>%s</BODY>\n", msg);
1573 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</HTML>\n");
1575 /* prepare output buffer */
1576 c->buffer_ptr = c->buffer;
1578 c->state = HTTPSTATE_SEND_HEADER;
1582 c->http_error = 200; /* horrible : we use this value to avoid
1583 going to the send data state */
1584 c->state = HTTPSTATE_SEND_HEADER;
1588 static void fmt_bytecount(ByteIOContext *pb, int64_t count)
1590 static const char *suffix = " kMGTP";
1593 for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++) {
1596 url_fprintf(pb, "%"PRId64"%c", count, *s);
1599 static void compute_stats(HTTPContext *c)
1606 ByteIOContext pb1, *pb = &pb1;
1608 if (url_open_dyn_buf(pb) < 0) {
1609 /* XXX: return an error ? */
1610 c->buffer_ptr = c->buffer;
1611 c->buffer_end = c->buffer;
1615 url_fprintf(pb, "HTTP/1.0 200 OK\r\n");
1616 url_fprintf(pb, "Content-type: %s\r\n", "text/html");
1617 url_fprintf(pb, "Pragma: no-cache\r\n");
1618 url_fprintf(pb, "\r\n");
1620 url_fprintf(pb, "<HEAD><TITLE>FFServer Status</TITLE>\n");
1621 if (c->stream->feed_filename) {
1622 url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
1624 url_fprintf(pb, "</HEAD>\n<BODY>");
1625 url_fprintf(pb, "<H1>FFServer Status</H1>\n");
1627 url_fprintf(pb, "<H2>Available Streams</H2>\n");
1628 url_fprintf(pb, "<TABLE cellspacing=0 cellpadding=4>\n");
1629 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");
1630 stream = first_stream;
1631 while (stream != NULL) {
1632 char sfilename[1024];
1635 if (stream->feed != stream) {
1636 pstrcpy(sfilename, sizeof(sfilename) - 10, stream->filename);
1637 eosf = sfilename + strlen(sfilename);
1638 if (eosf - sfilename >= 4) {
1639 if (strcmp(eosf - 4, ".asf") == 0) {
1640 strcpy(eosf - 4, ".asx");
1641 } else if (strcmp(eosf - 3, ".rm") == 0) {
1642 strcpy(eosf - 3, ".ram");
1643 } else if (stream->fmt == &rtp_muxer) {
1644 /* generate a sample RTSP director if
1645 unicast. Generate an SDP redirector if
1647 eosf = strrchr(sfilename, '.');
1649 eosf = sfilename + strlen(sfilename);
1650 if (stream->is_multicast)
1651 strcpy(eosf, ".sdp");
1653 strcpy(eosf, ".rtsp");
1657 url_fprintf(pb, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1658 sfilename, stream->filename);
1659 url_fprintf(pb, "<td align=right> %d <td align=right> ",
1660 stream->conns_served);
1661 fmt_bytecount(pb, stream->bytes_served);
1662 switch(stream->stream_type) {
1663 case STREAM_TYPE_LIVE:
1665 int audio_bit_rate = 0;
1666 int video_bit_rate = 0;
1667 const char *audio_codec_name = "";
1668 const char *video_codec_name = "";
1669 const char *audio_codec_name_extra = "";
1670 const char *video_codec_name_extra = "";
1672 for(i=0;i<stream->nb_streams;i++) {
1673 AVStream *st = stream->streams[i];
1674 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1675 switch(st->codec->codec_type) {
1676 case CODEC_TYPE_AUDIO:
1677 audio_bit_rate += st->codec->bit_rate;
1679 if (*audio_codec_name)
1680 audio_codec_name_extra = "...";
1681 audio_codec_name = codec->name;
1684 case CODEC_TYPE_VIDEO:
1685 video_bit_rate += st->codec->bit_rate;
1687 if (*video_codec_name)
1688 video_codec_name_extra = "...";
1689 video_codec_name = codec->name;
1692 case CODEC_TYPE_DATA:
1693 video_bit_rate += st->codec->bit_rate;
1699 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",
1702 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
1703 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
1705 url_fprintf(pb, "<TD>%s", stream->feed->filename);
1707 url_fprintf(pb, "<TD>%s", stream->feed_filename);
1709 url_fprintf(pb, "\n");
1713 url_fprintf(pb, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1717 stream = stream->next;
1719 url_fprintf(pb, "</TABLE>\n");
1721 stream = first_stream;
1722 while (stream != NULL) {
1723 if (stream->feed == stream) {
1724 url_fprintf(pb, "<h2>Feed %s</h2>", stream->filename);
1726 url_fprintf(pb, "Running as pid %d.\n", stream->pid);
1728 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1733 /* This is somewhat linux specific I guess */
1734 snprintf(ps_cmd, sizeof(ps_cmd),
1735 "ps -o \"%%cpu,cputime\" --no-headers %d",
1738 pid_stat = popen(ps_cmd, "r");
1743 if (fscanf(pid_stat, "%10s %64s", cpuperc,
1745 url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",
1753 url_fprintf(pb, "<p>");
1755 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");
1757 for (i = 0; i < stream->nb_streams; i++) {
1758 AVStream *st = stream->streams[i];
1759 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1760 const char *type = "unknown";
1761 char parameters[64];
1765 switch(st->codec->codec_type) {
1766 case CODEC_TYPE_AUDIO:
1768 snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
1770 case CODEC_TYPE_VIDEO:
1772 snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,
1773 st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);
1778 url_fprintf(pb, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1779 i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);
1781 url_fprintf(pb, "</table>\n");
1784 stream = stream->next;
1790 AVCodecContext *enc;
1794 stream = first_feed;
1795 while (stream != NULL) {
1796 url_fprintf(pb, "<H1>Feed '%s'</H1>\n", stream->filename);
1797 url_fprintf(pb, "<TABLE>\n");
1798 url_fprintf(pb, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1799 for(i=0;i<stream->nb_streams;i++) {
1800 AVStream *st = stream->streams[i];
1801 FeedData *fdata = st->priv_data;
1804 avcodec_string(buf, sizeof(buf), enc);
1805 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
1806 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
1807 avg /= enc->frame_size;
1808 url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
1809 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
1811 url_fprintf(pb, "</TABLE>\n");
1812 stream = stream->next_feed;
1817 /* connection status */
1818 url_fprintf(pb, "<H2>Connection Status</H2>\n");
1820 url_fprintf(pb, "Number of connections: %d / %d<BR>\n",
1821 nb_connections, nb_max_connections);
1823 url_fprintf(pb, "Bandwidth in use: %dk / %dk<BR>\n",
1824 current_bandwidth, max_bandwidth);
1826 url_fprintf(pb, "<TABLE>\n");
1827 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");
1828 c1 = first_http_ctx;
1830 while (c1 != NULL) {
1836 for (j = 0; j < c1->stream->nb_streams; j++) {
1837 if (!c1->stream->feed) {
1838 bitrate += c1->stream->streams[j]->codec->bit_rate;
1840 if (c1->feed_streams[j] >= 0) {
1841 bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
1848 p = inet_ntoa(c1->from_addr.sin_addr);
1849 url_fprintf(pb, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1851 c1->stream ? c1->stream->filename : "",
1852 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
1855 http_state[c1->state]);
1856 fmt_bytecount(pb, bitrate);
1857 url_fprintf(pb, "<td align=right>");
1858 fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
1859 url_fprintf(pb, "<td align=right>");
1860 fmt_bytecount(pb, c1->data_count);
1861 url_fprintf(pb, "\n");
1864 url_fprintf(pb, "</TABLE>\n");
1869 url_fprintf(pb, "<HR size=1 noshade>Generated at %s", p);
1870 url_fprintf(pb, "</BODY>\n</HTML>\n");
1872 len = url_close_dyn_buf(pb, &c->pb_buffer);
1873 c->buffer_ptr = c->pb_buffer;
1874 c->buffer_end = c->pb_buffer + len;
1877 /* check if the parser needs to be opened for stream i */
1878 static void open_parser(AVFormatContext *s, int i)
1880 AVStream *st = s->streams[i];
1883 if (!st->codec->codec) {
1884 codec = avcodec_find_decoder(st->codec->codec_id);
1885 if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
1886 st->codec->parse_only = 1;
1887 if (avcodec_open(st->codec, codec) < 0) {
1888 st->codec->parse_only = 0;
1894 static int open_input_stream(HTTPContext *c, const char *info)
1897 char input_filename[1024];
1902 /* find file name */
1903 if (c->stream->feed) {
1904 strcpy(input_filename, c->stream->feed->feed_filename);
1905 buf_size = FFM_PACKET_SIZE;
1906 /* compute position (absolute time) */
1907 if (find_info_tag(buf, sizeof(buf), "date", info)) {
1908 stream_pos = parse_date(buf, 0);
1909 } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
1910 int prebuffer = strtol(buf, 0, 10);
1911 stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
1913 stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
1916 strcpy(input_filename, c->stream->feed_filename);
1918 /* compute position (relative time) */
1919 if (find_info_tag(buf, sizeof(buf), "date", info)) {
1920 stream_pos = parse_date(buf, 1);
1925 if (input_filename[0] == '\0')
1929 { time_t when = stream_pos / 1000000;
1930 http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
1935 if (av_open_input_file(&s, input_filename, c->stream->ifmt,
1936 buf_size, c->stream->ap_in) < 0) {
1937 http_log("%s not found", input_filename);
1942 /* open each parser */
1943 for(i=0;i<s->nb_streams;i++)
1946 /* choose stream as clock source (we favorize video stream if
1947 present) for packet sending */
1948 c->pts_stream_index = 0;
1949 for(i=0;i<c->stream->nb_streams;i++) {
1950 if (c->pts_stream_index == 0 &&
1951 c->stream->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
1952 c->pts_stream_index = i;
1957 if (c->fmt_in->iformat->read_seek) {
1958 c->fmt_in->iformat->read_seek(c->fmt_in, 0, stream_pos, 0);
1961 /* set the start time (needed for maxtime and RTP packet timing) */
1962 c->start_time = cur_time;
1963 c->first_pts = AV_NOPTS_VALUE;
1967 /* return the server clock (in us) */
1968 static int64_t get_server_clock(HTTPContext *c)
1970 /* compute current pts value from system time */
1971 return (cur_time - c->start_time) * 1000;
1974 /* return the estimated time at which the current packet must be sent
1976 static int64_t get_packet_send_clock(HTTPContext *c)
1978 int bytes_left, bytes_sent, frame_bytes;
1980 frame_bytes = c->cur_frame_bytes;
1981 if (frame_bytes <= 0) {
1984 bytes_left = c->buffer_end - c->buffer_ptr;
1985 bytes_sent = frame_bytes - bytes_left;
1986 return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
1991 static int http_prepare_data(HTTPContext *c)
1994 AVFormatContext *ctx;
1996 av_freep(&c->pb_buffer);
1998 case HTTPSTATE_SEND_DATA_HEADER:
1999 memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
2000 pstrcpy(c->fmt_ctx.author, sizeof(c->fmt_ctx.author),
2002 pstrcpy(c->fmt_ctx.comment, sizeof(c->fmt_ctx.comment),
2003 c->stream->comment);
2004 pstrcpy(c->fmt_ctx.copyright, sizeof(c->fmt_ctx.copyright),
2005 c->stream->copyright);
2006 pstrcpy(c->fmt_ctx.title, sizeof(c->fmt_ctx.title),
2009 /* open output stream by using specified codecs */
2010 c->fmt_ctx.oformat = c->stream->fmt;
2011 c->fmt_ctx.nb_streams = c->stream->nb_streams;
2012 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
2015 st = av_mallocz(sizeof(AVStream));
2016 st->codec= avcodec_alloc_context();
2017 c->fmt_ctx.streams[i] = st;
2018 /* if file or feed, then just take streams from FFStream struct */
2019 if (!c->stream->feed ||
2020 c->stream->feed == c->stream)
2021 src = c->stream->streams[i];
2023 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2027 st->codec->frame_number = 0; /* XXX: should be done in
2028 AVStream, not in codec */
2029 /* I'm pretty sure that this is not correct...
2030 * However, without it, we crash
2032 st->codec->coded_frame = &dummy_frame;
2034 c->got_key_frame = 0;
2036 /* prepare header and save header data in a stream */
2037 if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2038 /* XXX: potential leak */
2041 c->fmt_ctx.pb.is_streamed = 1;
2043 av_set_parameters(&c->fmt_ctx, NULL);
2044 if (av_write_header(&c->fmt_ctx) < 0)
2047 len = url_close_dyn_buf(&c->fmt_ctx.pb, &c->pb_buffer);
2048 c->buffer_ptr = c->pb_buffer;
2049 c->buffer_end = c->pb_buffer + len;
2051 c->state = HTTPSTATE_SEND_DATA;
2052 c->last_packet_sent = 0;
2054 case HTTPSTATE_SEND_DATA:
2055 /* find a new packet */
2059 /* read a packet from the input stream */
2060 if (c->stream->feed) {
2061 ffm_set_write_index(c->fmt_in,
2062 c->stream->feed->feed_write_index,
2063 c->stream->feed->feed_size);
2066 if (c->stream->max_time &&
2067 c->stream->max_time + c->start_time - cur_time < 0) {
2068 /* We have timed out */
2069 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2072 if (av_read_frame(c->fmt_in, &pkt) < 0) {
2073 if (c->stream->feed && c->stream->feed->feed_opened) {
2074 /* if coming from feed, it means we reached the end of the
2075 ffm file, so must wait for more data */
2076 c->state = HTTPSTATE_WAIT_FEED;
2077 return 1; /* state changed */
2079 if (c->stream->loop) {
2080 av_close_input_file(c->fmt_in);
2082 if (open_input_stream(c, "") < 0)
2087 /* must send trailer now because eof or error */
2088 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2092 /* update first pts if needed */
2093 if (c->first_pts == AV_NOPTS_VALUE) {
2094 c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2095 c->start_time = cur_time;
2097 /* send it to the appropriate stream */
2098 if (c->stream->feed) {
2099 /* if coming from a feed, select the right stream */
2100 if (c->switch_pending) {
2101 c->switch_pending = 0;
2102 for(i=0;i<c->stream->nb_streams;i++) {
2103 if (c->switch_feed_streams[i] == pkt.stream_index) {
2104 if (pkt.flags & PKT_FLAG_KEY) {
2105 do_switch_stream(c, i);
2108 if (c->switch_feed_streams[i] >= 0) {
2109 c->switch_pending = 1;
2113 for(i=0;i<c->stream->nb_streams;i++) {
2114 if (c->feed_streams[i] == pkt.stream_index) {
2115 pkt.stream_index = i;
2116 if (pkt.flags & PKT_FLAG_KEY) {
2117 c->got_key_frame |= 1 << i;
2119 /* See if we have all the key frames, then
2120 * we start to send. This logic is not quite
2121 * right, but it works for the case of a
2122 * single video stream with one or more
2123 * audio streams (for which every frame is
2124 * typically a key frame).
2126 if (!c->stream->send_on_key ||
2127 ((c->got_key_frame + 1) >> c->stream->nb_streams)) {
2133 AVCodecContext *codec;
2136 /* specific handling for RTP: we use several
2137 output stream (one for each RTP
2138 connection). XXX: need more abstract handling */
2139 if (c->is_packetized) {
2141 /* compute send time and duration */
2142 st = c->fmt_in->streams[pkt.stream_index];
2143 c->cur_pts = av_rescale_q(pkt.dts, st->time_base, AV_TIME_BASE_Q);
2144 if (st->start_time != AV_NOPTS_VALUE)
2145 c->cur_pts -= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
2146 c->cur_frame_duration = av_rescale_q(pkt.duration, st->time_base, AV_TIME_BASE_Q);
2148 printf("index=%d pts=%0.3f duration=%0.6f\n",
2150 (double)c->cur_pts /
2152 (double)c->cur_frame_duration /
2155 /* find RTP context */
2156 c->packet_stream_index = pkt.stream_index;
2157 ctx = c->rtp_ctx[c->packet_stream_index];
2159 av_free_packet(&pkt);
2162 codec = ctx->streams[0]->codec;
2163 /* only one stream per RTP connection */
2164 pkt.stream_index = 0;
2168 codec = ctx->streams[pkt.stream_index]->codec;
2171 codec->coded_frame->key_frame = ((pkt.flags & PKT_FLAG_KEY) != 0);
2172 if (c->is_packetized) {
2173 int max_packet_size;
2174 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP)
2175 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2177 max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);
2178 ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);
2180 ret = url_open_dyn_buf(&ctx->pb);
2183 /* XXX: potential leak */
2186 if (pkt.dts != AV_NOPTS_VALUE)
2187 pkt.dts = av_rescale_q(pkt.dts,
2188 c->fmt_in->streams[pkt.stream_index]->time_base,
2189 ctx->streams[pkt.stream_index]->time_base);
2190 if (pkt.pts != AV_NOPTS_VALUE)
2191 pkt.pts = av_rescale_q(pkt.pts,
2192 c->fmt_in->streams[pkt.stream_index]->time_base,
2193 ctx->streams[pkt.stream_index]->time_base);
2194 if (av_write_frame(ctx, &pkt)) {
2195 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2198 len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer);
2199 c->cur_frame_bytes = len;
2200 c->buffer_ptr = c->pb_buffer;
2201 c->buffer_end = c->pb_buffer + len;
2203 codec->frame_number++;
2207 av_free_packet(&pkt);
2213 case HTTPSTATE_SEND_DATA_TRAILER:
2214 /* last packet test ? */
2215 if (c->last_packet_sent || c->is_packetized)
2218 /* prepare header */
2219 if (url_open_dyn_buf(&ctx->pb) < 0) {
2220 /* XXX: potential leak */
2223 av_write_trailer(ctx);
2224 len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer);
2225 c->buffer_ptr = c->pb_buffer;
2226 c->buffer_end = c->pb_buffer + len;
2228 c->last_packet_sent = 1;
2234 /* should convert the format at the same time */
2235 /* send data starting at c->buffer_ptr to the output connection
2236 (either UDP or TCP connection) */
2237 static int http_send_data(HTTPContext *c)
2242 if (c->buffer_ptr >= c->buffer_end) {
2243 ret = http_prepare_data(c);
2246 else if (ret != 0) {
2247 /* state change requested */
2251 if (c->is_packetized) {
2252 /* RTP data output */
2253 len = c->buffer_end - c->buffer_ptr;
2255 /* fail safe - should never happen */
2257 c->buffer_ptr = c->buffer_end;
2260 len = (c->buffer_ptr[0] << 24) |
2261 (c->buffer_ptr[1] << 16) |
2262 (c->buffer_ptr[2] << 8) |
2264 if (len > (c->buffer_end - c->buffer_ptr))
2266 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2267 /* nothing to send yet: we can wait */
2271 c->data_count += len;
2272 update_datarate(&c->datarate, c->data_count);
2274 c->stream->bytes_served += len;
2276 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) {
2277 /* RTP packets are sent inside the RTSP TCP connection */
2278 ByteIOContext pb1, *pb = &pb1;
2279 int interleaved_index, size;
2281 HTTPContext *rtsp_c;
2284 /* if no RTSP connection left, error */
2287 /* if already sending something, then wait. */
2288 if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST) {
2291 if (url_open_dyn_buf(pb) < 0)
2293 interleaved_index = c->packet_stream_index * 2;
2294 /* RTCP packets are sent at odd indexes */
2295 if (c->buffer_ptr[1] == 200)
2296 interleaved_index++;
2297 /* write RTSP TCP header */
2299 header[1] = interleaved_index;
2300 header[2] = len >> 8;
2302 put_buffer(pb, header, 4);
2303 /* write RTP packet data */
2305 put_buffer(pb, c->buffer_ptr, len);
2306 size = url_close_dyn_buf(pb, &c->packet_buffer);
2307 /* prepare asynchronous TCP sending */
2308 rtsp_c->packet_buffer_ptr = c->packet_buffer;
2309 rtsp_c->packet_buffer_end = c->packet_buffer + size;
2310 c->buffer_ptr += len;
2312 /* send everything we can NOW */
2313 len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2314 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2316 rtsp_c->packet_buffer_ptr += len;
2318 if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2319 /* if we could not send all the data, we will
2320 send it later, so a new state is needed to
2321 "lock" the RTSP TCP connection */
2322 rtsp_c->state = RTSPSTATE_SEND_PACKET;
2325 /* all data has been sent */
2326 av_freep(&c->packet_buffer);
2329 /* send RTP packet directly in UDP */
2331 url_write(c->rtp_handles[c->packet_stream_index],
2332 c->buffer_ptr, len);
2333 c->buffer_ptr += len;
2334 /* here we continue as we can send several packets per 10 ms slot */
2337 /* TCP data output */
2338 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2340 if (errno != EAGAIN && errno != EINTR) {
2341 /* error : close connection */
2347 c->buffer_ptr += len;
2349 c->data_count += len;
2350 update_datarate(&c->datarate, c->data_count);
2352 c->stream->bytes_served += len;
2360 static int http_start_receive_data(HTTPContext *c)
2364 if (c->stream->feed_opened)
2367 /* Don't permit writing to this one */
2368 if (c->stream->readonly)
2372 fd = open(c->stream->feed_filename, O_RDWR);
2377 c->stream->feed_write_index = ffm_read_write_index(fd);
2378 c->stream->feed_size = lseek(fd, 0, SEEK_END);
2379 lseek(fd, 0, SEEK_SET);
2381 /* init buffer input */
2382 c->buffer_ptr = c->buffer;
2383 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2384 c->stream->feed_opened = 1;
2388 static int http_receive_data(HTTPContext *c)
2392 if (c->buffer_end > c->buffer_ptr) {
2395 len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2397 if (errno != EAGAIN && errno != EINTR) {
2398 /* error : close connection */
2401 } else if (len == 0) {
2402 /* end of connection : close it */
2405 c->buffer_ptr += len;
2406 c->data_count += len;
2407 update_datarate(&c->datarate, c->data_count);
2411 if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2412 if (c->buffer[0] != 'f' ||
2413 c->buffer[1] != 'm') {
2414 http_log("Feed stream has become desynchronized -- disconnecting\n");
2419 if (c->buffer_ptr >= c->buffer_end) {
2420 FFStream *feed = c->stream;
2421 /* a packet has been received : write it in the store, except
2423 if (c->data_count > FFM_PACKET_SIZE) {
2425 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2426 /* XXX: use llseek or url_seek */
2427 lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
2428 write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
2430 feed->feed_write_index += FFM_PACKET_SIZE;
2431 /* update file size */
2432 if (feed->feed_write_index > c->stream->feed_size)
2433 feed->feed_size = feed->feed_write_index;
2435 /* handle wrap around if max file size reached */
2436 if (c->stream->feed_max_size && feed->feed_write_index >= c->stream->feed_max_size)
2437 feed->feed_write_index = FFM_PACKET_SIZE;
2440 ffm_write_write_index(c->feed_fd, feed->feed_write_index);
2442 /* wake up any waiting connections */
2443 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2444 if (c1->state == HTTPSTATE_WAIT_FEED &&
2445 c1->stream->feed == c->stream->feed) {
2446 c1->state = HTTPSTATE_SEND_DATA;
2450 /* We have a header in our hands that contains useful data */
2452 AVInputFormat *fmt_in;
2453 ByteIOContext *pb = &s.pb;
2456 memset(&s, 0, sizeof(s));
2458 url_open_buf(pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
2459 pb->buf_end = c->buffer_end; /* ?? */
2460 pb->is_streamed = 1;
2462 /* use feed output format name to find corresponding input format */
2463 fmt_in = av_find_input_format(feed->fmt->name);
2467 if (fmt_in->priv_data_size > 0) {
2468 s.priv_data = av_mallocz(fmt_in->priv_data_size);
2474 if (fmt_in->read_header(&s, 0) < 0) {
2475 av_freep(&s.priv_data);
2479 /* Now we have the actual streams */
2480 if (s.nb_streams != feed->nb_streams) {
2481 av_freep(&s.priv_data);
2484 for (i = 0; i < s.nb_streams; i++) {
2485 memcpy(feed->streams[i]->codec,
2486 s.streams[i]->codec, sizeof(AVCodecContext));
2488 av_freep(&s.priv_data);
2490 c->buffer_ptr = c->buffer;
2495 c->stream->feed_opened = 0;
2500 /********************************************************************/
2503 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2510 switch(error_number) {
2511 case RTSP_STATUS_OK:
2514 case RTSP_STATUS_METHOD:
2515 str = "Method Not Allowed";
2517 case RTSP_STATUS_BANDWIDTH:
2518 str = "Not Enough Bandwidth";
2520 case RTSP_STATUS_SESSION:
2521 str = "Session Not Found";
2523 case RTSP_STATUS_STATE:
2524 str = "Method Not Valid in This State";
2526 case RTSP_STATUS_AGGREGATE:
2527 str = "Aggregate operation not allowed";
2529 case RTSP_STATUS_ONLY_AGGREGATE:
2530 str = "Only aggregate operation allowed";
2532 case RTSP_STATUS_TRANSPORT:
2533 str = "Unsupported transport";
2535 case RTSP_STATUS_INTERNAL:
2536 str = "Internal Server Error";
2538 case RTSP_STATUS_SERVICE:
2539 str = "Service Unavailable";
2541 case RTSP_STATUS_VERSION:
2542 str = "RTSP Version not supported";
2545 str = "Unknown Error";
2549 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2550 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2552 /* output GMT time */
2556 p = buf2 + strlen(p) - 1;
2559 url_fprintf(c->pb, "Date: %s GMT\r\n", buf2);
2562 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2564 rtsp_reply_header(c, error_number);
2565 url_fprintf(c->pb, "\r\n");
2568 static int rtsp_parse_request(HTTPContext *c)
2570 const char *p, *p1, *p2;
2577 RTSPHeader header1, *header = &header1;
2579 c->buffer_ptr[0] = '\0';
2582 get_word(cmd, sizeof(cmd), &p);
2583 get_word(url, sizeof(url), &p);
2584 get_word(protocol, sizeof(protocol), &p);
2586 pstrcpy(c->method, sizeof(c->method), cmd);
2587 pstrcpy(c->url, sizeof(c->url), url);
2588 pstrcpy(c->protocol, sizeof(c->protocol), protocol);
2591 if (url_open_dyn_buf(c->pb) < 0) {
2592 /* XXX: cannot do more */
2593 c->pb = NULL; /* safety */
2597 /* check version name */
2598 if (strcmp(protocol, "RTSP/1.0") != 0) {
2599 rtsp_reply_error(c, RTSP_STATUS_VERSION);
2603 /* parse each header line */
2604 memset(header, 0, sizeof(RTSPHeader));
2605 /* skip to next line */
2606 while (*p != '\n' && *p != '\0')
2610 while (*p != '\0') {
2611 p1 = strchr(p, '\n');
2615 if (p2 > p && p2[-1] == '\r')
2617 /* skip empty line */
2621 if (len > sizeof(line) - 1)
2622 len = sizeof(line) - 1;
2623 memcpy(line, p, len);
2625 rtsp_parse_line(header, line);
2629 /* handle sequence number */
2630 c->seq = header->seq;
2632 if (!strcmp(cmd, "DESCRIBE")) {
2633 rtsp_cmd_describe(c, url);
2634 } else if (!strcmp(cmd, "OPTIONS")) {
2635 rtsp_cmd_options(c, url);
2636 } else if (!strcmp(cmd, "SETUP")) {
2637 rtsp_cmd_setup(c, url, header);
2638 } else if (!strcmp(cmd, "PLAY")) {
2639 rtsp_cmd_play(c, url, header);
2640 } else if (!strcmp(cmd, "PAUSE")) {
2641 rtsp_cmd_pause(c, url, header);
2642 } else if (!strcmp(cmd, "TEARDOWN")) {
2643 rtsp_cmd_teardown(c, url, header);
2645 rtsp_reply_error(c, RTSP_STATUS_METHOD);
2648 len = url_close_dyn_buf(c->pb, &c->pb_buffer);
2649 c->pb = NULL; /* safety */
2651 /* XXX: cannot do more */
2654 c->buffer_ptr = c->pb_buffer;
2655 c->buffer_end = c->pb_buffer + len;
2656 c->state = RTSPSTATE_SEND_REPLY;
2660 /* XXX: move that to rtsp.c, but would need to replace FFStream by
2662 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
2663 struct in_addr my_ip)
2665 ByteIOContext pb1, *pb = &pb1;
2666 int i, payload_type, port, private_payload_type, j;
2667 const char *ipstr, *title, *mediatype;
2670 if (url_open_dyn_buf(pb) < 0)
2673 /* general media info */
2675 url_fprintf(pb, "v=0\n");
2676 ipstr = inet_ntoa(my_ip);
2677 url_fprintf(pb, "o=- 0 0 IN IP4 %s\n", ipstr);
2678 title = stream->title;
2679 if (title[0] == '\0')
2681 url_fprintf(pb, "s=%s\n", title);
2682 if (stream->comment[0] != '\0')
2683 url_fprintf(pb, "i=%s\n", stream->comment);
2684 if (stream->is_multicast) {
2685 url_fprintf(pb, "c=IN IP4 %s\n", inet_ntoa(stream->multicast_ip));
2687 /* for each stream, we output the necessary info */
2688 private_payload_type = RTP_PT_PRIVATE;
2689 for(i = 0; i < stream->nb_streams; i++) {
2690 st = stream->streams[i];
2691 if (st->codec->codec_id == CODEC_ID_MPEG2TS) {
2692 mediatype = "video";
2694 switch(st->codec->codec_type) {
2695 case CODEC_TYPE_AUDIO:
2696 mediatype = "audio";
2698 case CODEC_TYPE_VIDEO:
2699 mediatype = "video";
2702 mediatype = "application";
2706 /* NOTE: the port indication is not correct in case of
2707 unicast. It is not an issue because RTSP gives it */
2708 payload_type = rtp_get_payload_type(st->codec);
2709 if (payload_type < 0)
2710 payload_type = private_payload_type++;
2711 if (stream->is_multicast) {
2712 port = stream->multicast_port + 2 * i;
2716 url_fprintf(pb, "m=%s %d RTP/AVP %d\n",
2717 mediatype, port, payload_type);
2718 if (payload_type >= RTP_PT_PRIVATE) {
2719 /* for private payload type, we need to give more info */
2720 switch(st->codec->codec_id) {
2721 case CODEC_ID_MPEG4:
2724 url_fprintf(pb, "a=rtpmap:%d MP4V-ES/%d\n",
2725 payload_type, 90000);
2726 /* we must also add the mpeg4 header */
2727 data = st->codec->extradata;
2729 url_fprintf(pb, "a=fmtp:%d config=", payload_type);
2730 for(j=0;j<st->codec->extradata_size;j++) {
2731 url_fprintf(pb, "%02x", data[j]);
2733 url_fprintf(pb, "\n");
2738 /* XXX: add other codecs ? */
2742 url_fprintf(pb, "a=control:streamid=%d\n", i);
2744 return url_close_dyn_buf(pb, pbuffer);
2746 url_close_dyn_buf(pb, pbuffer);
2751 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2753 // rtsp_reply_header(c, RTSP_STATUS_OK);
2754 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2755 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2756 url_fprintf(c->pb, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2757 url_fprintf(c->pb, "\r\n");
2760 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2766 int content_length, len;
2767 struct sockaddr_in my_addr;
2769 /* find which url is asked */
2770 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2775 for(stream = first_stream; stream != NULL; stream = stream->next) {
2776 if (!stream->is_feed && stream->fmt == &rtp_muxer &&
2777 !strcmp(path, stream->filename)) {
2781 /* no stream found */
2782 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2786 /* prepare the media description in sdp format */
2788 /* get the host IP */
2789 len = sizeof(my_addr);
2790 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2791 content_length = prepare_sdp_description(stream, &content, my_addr.sin_addr);
2792 if (content_length < 0) {
2793 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2796 rtsp_reply_header(c, RTSP_STATUS_OK);
2797 url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
2798 url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
2799 url_fprintf(c->pb, "\r\n");
2800 put_buffer(c->pb, content, content_length);
2803 static HTTPContext *find_rtp_session(const char *session_id)
2807 if (session_id[0] == '\0')
2810 for(c = first_http_ctx; c != NULL; c = c->next) {
2811 if (!strcmp(c->session_id, session_id))
2817 static RTSPTransportField *find_transport(RTSPHeader *h, enum RTSPProtocol protocol)
2819 RTSPTransportField *th;
2822 for(i=0;i<h->nb_transports;i++) {
2823 th = &h->transports[i];
2824 if (th->protocol == protocol)
2830 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
2834 int stream_index, port;
2839 RTSPTransportField *th;
2840 struct sockaddr_in dest_addr;
2841 RTSPActionServerSetup setup;
2843 /* find which url is asked */
2844 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2849 /* now check each stream */
2850 for(stream = first_stream; stream != NULL; stream = stream->next) {
2851 if (!stream->is_feed && stream->fmt == &rtp_muxer) {
2852 /* accept aggregate filenames only if single stream */
2853 if (!strcmp(path, stream->filename)) {
2854 if (stream->nb_streams != 1) {
2855 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
2862 for(stream_index = 0; stream_index < stream->nb_streams;
2864 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2865 stream->filename, stream_index);
2866 if (!strcmp(path, buf))
2871 /* no stream found */
2872 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2876 /* generate session id if needed */
2877 if (h->session_id[0] == '\0') {
2878 snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
2879 av_random(&random_state), av_random(&random_state));
2882 /* find rtp session, and create it if none found */
2883 rtp_c = find_rtp_session(h->session_id);
2885 /* always prefer UDP */
2886 th = find_transport(h, RTSP_PROTOCOL_RTP_UDP);
2888 th = find_transport(h, RTSP_PROTOCOL_RTP_TCP);
2890 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2895 rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
2898 rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
2902 /* open input stream */
2903 if (open_input_stream(rtp_c, "") < 0) {
2904 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2909 /* test if stream is OK (test needed because several SETUP needs
2910 to be done for a given file) */
2911 if (rtp_c->stream != stream) {
2912 rtsp_reply_error(c, RTSP_STATUS_SERVICE);
2916 /* test if stream is already set up */
2917 if (rtp_c->rtp_ctx[stream_index]) {
2918 rtsp_reply_error(c, RTSP_STATUS_STATE);
2922 /* check transport */
2923 th = find_transport(h, rtp_c->rtp_protocol);
2924 if (!th || (th->protocol == RTSP_PROTOCOL_RTP_UDP &&
2925 th->client_port_min <= 0)) {
2926 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2930 /* setup default options */
2931 setup.transport_option[0] = '\0';
2932 dest_addr = rtp_c->from_addr;
2933 dest_addr.sin_port = htons(th->client_port_min);
2936 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2937 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2941 /* now everything is OK, so we can send the connection parameters */
2942 rtsp_reply_header(c, RTSP_STATUS_OK);
2944 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2946 switch(rtp_c->rtp_protocol) {
2947 case RTSP_PROTOCOL_RTP_UDP:
2948 port = rtp_get_local_port(rtp_c->rtp_handles[stream_index]);
2949 url_fprintf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2950 "client_port=%d-%d;server_port=%d-%d",
2951 th->client_port_min, th->client_port_min + 1,
2954 case RTSP_PROTOCOL_RTP_TCP:
2955 url_fprintf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2956 stream_index * 2, stream_index * 2 + 1);
2961 if (setup.transport_option[0] != '\0') {
2962 url_fprintf(c->pb, ";%s", setup.transport_option);
2964 url_fprintf(c->pb, "\r\n");
2967 url_fprintf(c->pb, "\r\n");
2971 /* find an rtp connection by using the session ID. Check consistency
2973 static HTTPContext *find_rtp_session_with_url(const char *url,
2974 const char *session_id)
2982 rtp_c = find_rtp_session(session_id);
2986 /* find which url is asked */
2987 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2991 if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
2992 for(s=0; s<rtp_c->stream->nb_streams; ++s) {
2993 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2994 rtp_c->stream->filename, s);
2995 if(!strncmp(path, buf, sizeof(buf))) {
2996 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
3003 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h)
3007 rtp_c = find_rtp_session_with_url(url, h->session_id);
3009 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3013 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3014 rtp_c->state != HTTPSTATE_WAIT_FEED &&
3015 rtp_c->state != HTTPSTATE_READY) {
3016 rtsp_reply_error(c, RTSP_STATUS_STATE);
3021 /* XXX: seek in stream */
3022 if (h->range_start != AV_NOPTS_VALUE) {
3023 printf("range_start=%0.3f\n", (double)h->range_start / AV_TIME_BASE);
3024 av_seek_frame(rtp_c->fmt_in, -1, h->range_start);
3028 rtp_c->state = HTTPSTATE_SEND_DATA;
3030 /* now everything is OK, so we can send the connection parameters */
3031 rtsp_reply_header(c, RTSP_STATUS_OK);
3033 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3034 url_fprintf(c->pb, "\r\n");
3037 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h)
3041 rtp_c = find_rtp_session_with_url(url, h->session_id);
3043 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3047 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3048 rtp_c->state != HTTPSTATE_WAIT_FEED) {
3049 rtsp_reply_error(c, RTSP_STATUS_STATE);
3053 rtp_c->state = HTTPSTATE_READY;
3054 rtp_c->first_pts = AV_NOPTS_VALUE;
3055 /* now everything is OK, so we can send the connection parameters */
3056 rtsp_reply_header(c, RTSP_STATUS_OK);
3058 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3059 url_fprintf(c->pb, "\r\n");
3062 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
3065 char session_id[32];
3067 rtp_c = find_rtp_session_with_url(url, h->session_id);
3069 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3073 pstrcpy(session_id, sizeof(session_id), rtp_c->session_id);
3075 /* abort the session */
3076 close_connection(rtp_c);
3078 /* now everything is OK, so we can send the connection parameters */
3079 rtsp_reply_header(c, RTSP_STATUS_OK);
3081 url_fprintf(c->pb, "Session: %s\r\n", session_id);
3082 url_fprintf(c->pb, "\r\n");
3086 /********************************************************************/
3089 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3090 FFStream *stream, const char *session_id,
3091 enum RTSPProtocol rtp_protocol)
3093 HTTPContext *c = NULL;
3094 const char *proto_str;
3096 /* XXX: should output a warning page when coming
3097 close to the connection limit */
3098 if (nb_connections >= nb_max_connections)
3101 /* add a new connection */
3102 c = av_mallocz(sizeof(HTTPContext));
3107 c->poll_entry = NULL;
3108 c->from_addr = *from_addr;
3109 c->buffer_size = IOBUFFER_INIT_SIZE;
3110 c->buffer = av_malloc(c->buffer_size);
3115 pstrcpy(c->session_id, sizeof(c->session_id), session_id);
3116 c->state = HTTPSTATE_READY;
3117 c->is_packetized = 1;
3118 c->rtp_protocol = rtp_protocol;
3120 /* protocol is shown in statistics */
3121 switch(c->rtp_protocol) {
3122 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3123 proto_str = "MCAST";
3125 case RTSP_PROTOCOL_RTP_UDP:
3128 case RTSP_PROTOCOL_RTP_TCP:
3135 pstrcpy(c->protocol, sizeof(c->protocol), "RTP/");
3136 pstrcat(c->protocol, sizeof(c->protocol), proto_str);
3138 current_bandwidth += stream->bandwidth;
3140 c->next = first_http_ctx;
3152 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3153 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3155 static int rtp_new_av_stream(HTTPContext *c,
3156 int stream_index, struct sockaddr_in *dest_addr,
3157 HTTPContext *rtsp_c)
3159 AVFormatContext *ctx;
3165 int max_packet_size;
3167 /* now we can open the relevant output stream */
3168 ctx = av_alloc_format_context();
3171 ctx->oformat = &rtp_muxer;
3173 st = av_mallocz(sizeof(AVStream));
3176 st->codec= avcodec_alloc_context();
3177 ctx->nb_streams = 1;
3178 ctx->streams[0] = st;
3180 if (!c->stream->feed ||
3181 c->stream->feed == c->stream) {
3182 memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3185 c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3188 st->priv_data = NULL;
3190 /* build destination RTP address */
3191 ipaddr = inet_ntoa(dest_addr->sin_addr);
3193 switch(c->rtp_protocol) {
3194 case RTSP_PROTOCOL_RTP_UDP:
3195 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3198 /* XXX: also pass as parameter to function ? */
3199 if (c->stream->is_multicast) {
3201 ttl = c->stream->multicast_ttl;
3204 snprintf(ctx->filename, sizeof(ctx->filename),
3205 "rtp://%s:%d?multicast=1&ttl=%d",
3206 ipaddr, ntohs(dest_addr->sin_port), ttl);
3208 snprintf(ctx->filename, sizeof(ctx->filename),
3209 "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3212 if (url_open(&h, ctx->filename, URL_WRONLY) < 0)
3214 c->rtp_handles[stream_index] = h;
3215 max_packet_size = url_get_max_packet_size(h);
3217 case RTSP_PROTOCOL_RTP_TCP:
3220 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3226 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3227 ipaddr, ntohs(dest_addr->sin_port),
3229 c->stream->filename, stream_index, c->protocol);
3231 /* normally, no packets should be output here, but the packet size may be checked */
3232 if (url_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0) {
3233 /* XXX: close stream */
3236 av_set_parameters(ctx, NULL);
3237 if (av_write_header(ctx) < 0) {
3244 url_close_dyn_buf(&ctx->pb, &dummy_buf);
3247 c->rtp_ctx[stream_index] = ctx;
3251 /********************************************************************/
3252 /* ffserver initialization */
3254 static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
3258 fst = av_mallocz(sizeof(AVStream));
3261 fst->codec= avcodec_alloc_context();
3262 fst->priv_data = av_mallocz(sizeof(FeedData));
3263 memcpy(fst->codec, codec, sizeof(AVCodecContext));
3264 fst->codec->coded_frame = &dummy_frame;
3265 fst->index = stream->nb_streams;
3266 av_set_pts_info(fst, 33, 1, 90000);
3267 stream->streams[stream->nb_streams++] = fst;
3271 /* return the stream number in the feed */
3272 static int add_av_stream(FFStream *feed, AVStream *st)
3275 AVCodecContext *av, *av1;
3279 for(i=0;i<feed->nb_streams;i++) {
3280 st = feed->streams[i];
3282 if (av1->codec_id == av->codec_id &&
3283 av1->codec_type == av->codec_type &&
3284 av1->bit_rate == av->bit_rate) {
3286 switch(av->codec_type) {
3287 case CODEC_TYPE_AUDIO:
3288 if (av1->channels == av->channels &&
3289 av1->sample_rate == av->sample_rate)
3292 case CODEC_TYPE_VIDEO:
3293 if (av1->width == av->width &&
3294 av1->height == av->height &&
3295 av1->time_base.den == av->time_base.den &&
3296 av1->time_base.num == av->time_base.num &&
3297 av1->gop_size == av->gop_size)
3306 fst = add_av_stream1(feed, av);
3309 return feed->nb_streams - 1;
3314 static void remove_stream(FFStream *stream)
3318 while (*ps != NULL) {
3319 if (*ps == stream) {
3327 /* specific mpeg4 handling : we extract the raw parameters */
3328 static void extract_mpeg4_header(AVFormatContext *infile)
3330 int mpeg4_count, i, size;
3336 for(i=0;i<infile->nb_streams;i++) {
3337 st = infile->streams[i];
3338 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3339 st->codec->extradata_size == 0) {
3346 printf("MPEG4 without extra data: trying to find header in %s\n", infile->filename);
3347 while (mpeg4_count > 0) {
3348 if (av_read_packet(infile, &pkt) < 0)
3350 st = infile->streams[pkt.stream_index];
3351 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3352 st->codec->extradata_size == 0) {
3353 av_freep(&st->codec->extradata);
3354 /* fill extradata with the header */
3355 /* XXX: we make hard suppositions here ! */
3357 while (p < pkt.data + pkt.size - 4) {
3358 /* stop when vop header is found */
3359 if (p[0] == 0x00 && p[1] == 0x00 &&
3360 p[2] == 0x01 && p[3] == 0xb6) {
3361 size = p - pkt.data;
3362 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3363 st->codec->extradata = av_malloc(size);
3364 st->codec->extradata_size = size;
3365 memcpy(st->codec->extradata, pkt.data, size);
3372 av_free_packet(&pkt);
3376 /* compute the needed AVStream for each file */
3377 static void build_file_streams(void)
3379 FFStream *stream, *stream_next;
3380 AVFormatContext *infile;
3383 /* gather all streams */
3384 for(stream = first_stream; stream != NULL; stream = stream_next) {
3385 stream_next = stream->next;
3386 if (stream->stream_type == STREAM_TYPE_LIVE &&
3388 /* the stream comes from a file */
3389 /* try to open the file */
3391 stream->ap_in = av_mallocz(sizeof(AVFormatParameters));
3392 if (stream->fmt == &rtp_muxer) {
3393 /* specific case : if transport stream output to RTP,
3394 we use a raw transport stream reader */
3395 stream->ap_in->mpeg2ts_raw = 1;
3396 stream->ap_in->mpeg2ts_compute_pcr = 1;
3399 if (av_open_input_file(&infile, stream->feed_filename,
3400 stream->ifmt, 0, stream->ap_in) < 0) {
3401 http_log("%s not found", stream->feed_filename);
3402 /* remove stream (no need to spend more time on it) */
3404 remove_stream(stream);
3406 /* find all the AVStreams inside and reference them in
3408 if (av_find_stream_info(infile) < 0) {
3409 http_log("Could not find codec parameters from '%s'",
3410 stream->feed_filename);
3411 av_close_input_file(infile);
3414 extract_mpeg4_header(infile);
3416 for(i=0;i<infile->nb_streams;i++) {
3417 add_av_stream1(stream, infile->streams[i]->codec);
3419 av_close_input_file(infile);
3425 /* compute the needed AVStream for each feed */
3426 static void build_feed_streams(void)
3428 FFStream *stream, *feed;
3431 /* gather all streams */
3432 for(stream = first_stream; stream != NULL; stream = stream->next) {
3433 feed = stream->feed;
3435 if (!stream->is_feed) {
3436 /* we handle a stream coming from a feed */
3437 for(i=0;i<stream->nb_streams;i++) {
3438 stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
3444 /* gather all streams */
3445 for(stream = first_stream; stream != NULL; stream = stream->next) {
3446 feed = stream->feed;
3448 if (stream->is_feed) {
3449 for(i=0;i<stream->nb_streams;i++) {
3450 stream->feed_streams[i] = i;
3456 /* create feed files if needed */
3457 for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
3460 if (url_exist(feed->feed_filename)) {
3461 /* See if it matches */
3465 if (av_open_input_file(&s, feed->feed_filename, NULL, FFM_PACKET_SIZE, NULL) >= 0) {
3466 /* Now see if it matches */
3467 if (s->nb_streams == feed->nb_streams) {
3469 for(i=0;i<s->nb_streams;i++) {
3471 sf = feed->streams[i];
3474 if (sf->index != ss->index ||
3476 printf("Index & Id do not match for stream %d (%s)\n",
3477 i, feed->feed_filename);
3480 AVCodecContext *ccf, *ccs;
3484 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3486 if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
3487 printf("Codecs do not match for stream %d\n", i);
3489 } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3490 printf("Codec bitrates do not match for stream %d\n", i);
3492 } else if (ccf->codec_type == CODEC_TYPE_VIDEO) {
3493 if (CHECK_CODEC(time_base.den) ||
3494 CHECK_CODEC(time_base.num) ||
3495 CHECK_CODEC(width) ||
3496 CHECK_CODEC(height)) {
3497 printf("Codec width, height and framerate do not match for stream %d\n", i);
3500 } else if (ccf->codec_type == CODEC_TYPE_AUDIO) {
3501 if (CHECK_CODEC(sample_rate) ||
3502 CHECK_CODEC(channels) ||
3503 CHECK_CODEC(frame_size)) {
3504 printf("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3508 printf("Unknown codec type\n");
3517 printf("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3518 feed->feed_filename, s->nb_streams, feed->nb_streams);
3521 av_close_input_file(s);
3523 printf("Deleting feed file '%s' as it appears to be corrupt\n",
3524 feed->feed_filename);
3527 if (feed->readonly) {
3528 printf("Unable to delete feed file '%s' as it is marked readonly\n",
3529 feed->feed_filename);
3532 unlink(feed->feed_filename);
3535 if (!url_exist(feed->feed_filename)) {
3536 AVFormatContext s1, *s = &s1;
3538 if (feed->readonly) {
3539 printf("Unable to create feed file '%s' as it is marked readonly\n",
3540 feed->feed_filename);
3544 /* only write the header of the ffm file */
3545 if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
3546 fprintf(stderr, "Could not open output feed file '%s'\n",
3547 feed->feed_filename);
3550 s->oformat = feed->fmt;
3551 s->nb_streams = feed->nb_streams;
3552 for(i=0;i<s->nb_streams;i++) {
3554 st = feed->streams[i];
3557 av_set_parameters(s, NULL);
3558 if (av_write_header(s) < 0) {
3559 fprintf(stderr, "Container doesn't supports the required parameters\n");
3562 /* XXX: need better api */
3563 av_freep(&s->priv_data);
3566 /* get feed size and write index */
3567 fd = open(feed->feed_filename, O_RDONLY);
3569 fprintf(stderr, "Could not open output feed file '%s'\n",
3570 feed->feed_filename);
3574 feed->feed_write_index = ffm_read_write_index(fd);
3575 feed->feed_size = lseek(fd, 0, SEEK_END);
3576 /* ensure that we do not wrap before the end of file */
3577 if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3578 feed->feed_max_size = feed->feed_size;
3584 /* compute the bandwidth used by each stream */
3585 static void compute_bandwidth(void)
3590 for(stream = first_stream; stream != NULL; stream = stream->next) {
3592 for(i=0;i<stream->nb_streams;i++) {
3593 AVStream *st = stream->streams[i];
3594 switch(st->codec->codec_type) {
3595 case CODEC_TYPE_AUDIO:
3596 case CODEC_TYPE_VIDEO:
3597 bandwidth += st->codec->bit_rate;
3603 stream->bandwidth = (bandwidth + 999) / 1000;
3607 static void get_arg(char *buf, int buf_size, const char **pp)
3614 while (isspace(*p)) p++;
3617 if (*p == '\"' || *p == '\'')
3629 if ((q - buf) < buf_size - 1)
3634 if (quote && *p == quote)
3639 /* add a codec and set the default parameters */
3640 static void add_codec(FFStream *stream, AVCodecContext *av)
3644 /* compute default parameters */
3645 switch(av->codec_type) {
3646 case CODEC_TYPE_AUDIO:
3647 if (av->bit_rate == 0)
3648 av->bit_rate = 64000;
3649 if (av->sample_rate == 0)
3650 av->sample_rate = 22050;
3651 if (av->channels == 0)
3654 case CODEC_TYPE_VIDEO:
3655 if (av->bit_rate == 0)
3656 av->bit_rate = 64000;
3657 if (av->time_base.num == 0){
3658 av->time_base.den = 5;
3659 av->time_base.num = 1;
3661 if (av->width == 0 || av->height == 0) {
3665 /* Bitrate tolerance is less for streaming */
3666 if (av->bit_rate_tolerance == 0)
3667 av->bit_rate_tolerance = av->bit_rate / 4;
3672 if (av->max_qdiff == 0)
3674 av->qcompress = 0.5;
3677 if (!av->nsse_weight)
3678 av->nsse_weight = 8;
3680 av->frame_skip_cmp = FF_CMP_DCTMAX;
3681 av->me_method = ME_EPZS;
3682 av->rc_buffer_aggressivity = 1.0;
3685 av->rc_eq = "tex^qComp";
3686 if (!av->i_quant_factor)
3687 av->i_quant_factor = -0.8;
3688 if (!av->b_quant_factor)
3689 av->b_quant_factor = 1.25;
3690 if (!av->b_quant_offset)
3691 av->b_quant_offset = 1.25;
3692 if (!av->rc_max_rate)
3693 av->rc_max_rate = av->bit_rate * 2;
3695 if (av->rc_max_rate && !av->rc_buffer_size) {
3696 av->rc_buffer_size = av->rc_max_rate;
3705 st = av_mallocz(sizeof(AVStream));
3708 st->codec = avcodec_alloc_context();
3709 stream->streams[stream->nb_streams++] = st;
3710 memcpy(st->codec, av, sizeof(AVCodecContext));
3713 static int opt_audio_codec(const char *arg)
3719 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
3724 return CODEC_ID_NONE;
3730 static int opt_video_codec(const char *arg)
3736 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
3741 return CODEC_ID_NONE;
3747 /* simplistic plugin support */
3750 static void load_module(const char *filename)
3753 void (*init_func)(void);
3754 dll = dlopen(filename, RTLD_NOW);
3756 fprintf(stderr, "Could not load module '%s' - %s\n",
3757 filename, dlerror());
3761 init_func = dlsym(dll, "ffserver_module_init");
3764 "%s: init function 'ffserver_module_init()' not found\n",
3773 static int parse_ffconfig(const char *filename)
3780 int val, errors, line_num;
3781 FFStream **last_stream, *stream, *redirect;
3782 FFStream **last_feed, *feed;
3783 AVCodecContext audio_enc, video_enc;
3784 int audio_id, video_id;
3786 f = fopen(filename, "r");
3794 first_stream = NULL;
3795 last_stream = &first_stream;
3797 last_feed = &first_feed;
3801 audio_id = CODEC_ID_NONE;
3802 video_id = CODEC_ID_NONE;
3804 if (fgets(line, sizeof(line), f) == NULL)
3810 if (*p == '\0' || *p == '#')
3813 get_arg(cmd, sizeof(cmd), &p);
3815 if (!strcasecmp(cmd, "Port")) {
3816 get_arg(arg, sizeof(arg), &p);
3818 if (val < 1 || val > 65536) {
3819 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3820 filename, line_num, arg);
3823 my_http_addr.sin_port = htons(val);
3824 } else if (!strcasecmp(cmd, "BindAddress")) {
3825 get_arg(arg, sizeof(arg), &p);
3826 if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
3827 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3828 filename, line_num, arg);
3831 } else if (!strcasecmp(cmd, "NoDaemon")) {
3832 ffserver_daemon = 0;
3833 } else if (!strcasecmp(cmd, "RTSPPort")) {
3834 get_arg(arg, sizeof(arg), &p);
3836 if (val < 1 || val > 65536) {
3837 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3838 filename, line_num, arg);
3841 my_rtsp_addr.sin_port = htons(atoi(arg));
3842 } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
3843 get_arg(arg, sizeof(arg), &p);
3844 if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
3845 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3846 filename, line_num, arg);
3849 } else if (!strcasecmp(cmd, "MaxClients")) {
3850 get_arg(arg, sizeof(arg), &p);
3852 if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
3853 fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
3854 filename, line_num, arg);
3857 nb_max_connections = val;
3859 } else if (!strcasecmp(cmd, "MaxBandwidth")) {
3860 get_arg(arg, sizeof(arg), &p);
3862 if (val < 10 || val > 100000) {
3863 fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
3864 filename, line_num, arg);
3867 max_bandwidth = val;
3869 } else if (!strcasecmp(cmd, "CustomLog")) {
3870 get_arg(logfilename, sizeof(logfilename), &p);
3871 } else if (!strcasecmp(cmd, "<Feed")) {
3872 /*********************************************/
3873 /* Feed related options */
3875 if (stream || feed) {
3876 fprintf(stderr, "%s:%d: Already in a tag\n",
3877 filename, line_num);
3879 feed = av_mallocz(sizeof(FFStream));
3880 /* add in stream list */
3881 *last_stream = feed;
3882 last_stream = &feed->next;
3883 /* add in feed list */
3885 last_feed = &feed->next_feed;
3887 get_arg(feed->filename, sizeof(feed->filename), &p);
3888 q = strrchr(feed->filename, '>');
3891 feed->fmt = guess_format("ffm", NULL, NULL);
3892 /* defaut feed file */
3893 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
3894 "/tmp/%s.ffm", feed->filename);
3895 feed->feed_max_size = 5 * 1024 * 1024;
3897 feed->feed = feed; /* self feeding :-) */
3899 } else if (!strcasecmp(cmd, "Launch")) {
3903 feed->child_argv = (char **) av_mallocz(64 * sizeof(char *));
3905 for (i = 0; i < 62; i++) {
3906 get_arg(arg, sizeof(arg), &p);
3910 feed->child_argv[i] = av_strdup(arg);
3913 feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
3915 snprintf(feed->child_argv[i], 30+strlen(feed->filename),
3917 (my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
3918 inet_ntoa(my_http_addr.sin_addr),
3919 ntohs(my_http_addr.sin_port), feed->filename);
3924 fprintf(stdout, "Launch commandline: ");
3925 for (j = 0; j <= i; j++)
3926 fprintf(stdout, "%s ", feed->child_argv[j]);
3927 fprintf(stdout, "\n");
3930 } else if (!strcasecmp(cmd, "ReadOnlyFile")) {
3932 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3934 } else if (stream) {
3935 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3937 } else if (!strcasecmp(cmd, "File")) {
3939 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3940 } else if (stream) {
3941 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3943 } else if (!strcasecmp(cmd, "FileMaxSize")) {
3948 get_arg(arg, sizeof(arg), &p);
3950 fsize = strtod(p1, (char **)&p1);
3951 switch(toupper(*p1)) {
3956 fsize *= 1024 * 1024;
3959 fsize *= 1024 * 1024 * 1024;
3962 feed->feed_max_size = (int64_t)fsize;
3964 } else if (!strcasecmp(cmd, "</Feed>")) {
3966 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
3967 filename, line_num);
3971 } else if (!strcasecmp(cmd, "<Stream")) {
3972 /*********************************************/
3973 /* Stream related options */
3975 if (stream || feed) {
3976 fprintf(stderr, "%s:%d: Already in a tag\n",
3977 filename, line_num);
3979 stream = av_mallocz(sizeof(FFStream));
3980 *last_stream = stream;
3981 last_stream = &stream->next;
3983 get_arg(stream->filename, sizeof(stream->filename), &p);
3984 q = strrchr(stream->filename, '>');
3987 stream->fmt = guess_stream_format(NULL, stream->filename, NULL);
3988 memset(&audio_enc, 0, sizeof(AVCodecContext));
3989 memset(&video_enc, 0, sizeof(AVCodecContext));
3990 audio_id = CODEC_ID_NONE;
3991 video_id = CODEC_ID_NONE;
3993 audio_id = stream->fmt->audio_codec;
3994 video_id = stream->fmt->video_codec;
3997 } else if (!strcasecmp(cmd, "Feed")) {
3998 get_arg(arg, sizeof(arg), &p);
4003 while (sfeed != NULL) {
4004 if (!strcmp(sfeed->filename, arg))
4006 sfeed = sfeed->next_feed;
4009 fprintf(stderr, "%s:%d: feed '%s' not defined\n",
4010 filename, line_num, arg);
4012 stream->feed = sfeed;
4015 } else if (!strcasecmp(cmd, "Format")) {
4016 get_arg(arg, sizeof(arg), &p);
4017 if (!strcmp(arg, "status")) {
4018 stream->stream_type = STREAM_TYPE_STATUS;
4021 stream->stream_type = STREAM_TYPE_LIVE;
4022 /* jpeg cannot be used here, so use single frame jpeg */
4023 if (!strcmp(arg, "jpeg"))
4024 strcpy(arg, "mjpeg");
4025 stream->fmt = guess_stream_format(arg, NULL, NULL);
4027 fprintf(stderr, "%s:%d: Unknown Format: %s\n",
4028 filename, line_num, arg);
4033 audio_id = stream->fmt->audio_codec;
4034 video_id = stream->fmt->video_codec;
4036 } else if (!strcasecmp(cmd, "InputFormat")) {
4037 get_arg(arg, sizeof(arg), &p);
4038 stream->ifmt = av_find_input_format(arg);
4039 if (!stream->ifmt) {
4040 fprintf(stderr, "%s:%d: Unknown input format: %s\n",
4041 filename, line_num, arg);
4043 } else if (!strcasecmp(cmd, "FaviconURL")) {
4044 if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
4045 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
4047 fprintf(stderr, "%s:%d: FaviconURL only permitted for status streams\n",
4048 filename, line_num);
4051 } else if (!strcasecmp(cmd, "Author")) {
4053 get_arg(stream->author, sizeof(stream->author), &p);
4055 } else if (!strcasecmp(cmd, "Comment")) {
4057 get_arg(stream->comment, sizeof(stream->comment), &p);
4059 } else if (!strcasecmp(cmd, "Copyright")) {
4061 get_arg(stream->copyright, sizeof(stream->copyright), &p);
4063 } else if (!strcasecmp(cmd, "Title")) {
4065 get_arg(stream->title, sizeof(stream->title), &p);
4067 } else if (!strcasecmp(cmd, "Preroll")) {
4068 get_arg(arg, sizeof(arg), &p);
4070 stream->prebuffer = atof(arg) * 1000;
4072 } else if (!strcasecmp(cmd, "StartSendOnKey")) {
4074 stream->send_on_key = 1;
4076 } else if (!strcasecmp(cmd, "AudioCodec")) {
4077 get_arg(arg, sizeof(arg), &p);
4078 audio_id = opt_audio_codec(arg);
4079 if (audio_id == CODEC_ID_NONE) {
4080 fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
4081 filename, line_num, arg);
4084 } else if (!strcasecmp(cmd, "VideoCodec")) {
4085 get_arg(arg, sizeof(arg), &p);
4086 video_id = opt_video_codec(arg);
4087 if (video_id == CODEC_ID_NONE) {
4088 fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
4089 filename, line_num, arg);
4092 } else if (!strcasecmp(cmd, "MaxTime")) {
4093 get_arg(arg, sizeof(arg), &p);
4095 stream->max_time = atof(arg) * 1000;
4097 } else if (!strcasecmp(cmd, "AudioBitRate")) {
4098 get_arg(arg, sizeof(arg), &p);
4100 audio_enc.bit_rate = atoi(arg) * 1000;
4102 } else if (!strcasecmp(cmd, "AudioChannels")) {
4103 get_arg(arg, sizeof(arg), &p);
4105 audio_enc.channels = atoi(arg);
4107 } else if (!strcasecmp(cmd, "AudioSampleRate")) {
4108 get_arg(arg, sizeof(arg), &p);
4110 audio_enc.sample_rate = atoi(arg);
4112 } else if (!strcasecmp(cmd, "AudioQuality")) {
4113 get_arg(arg, sizeof(arg), &p);
4115 // audio_enc.quality = atof(arg) * 1000;
4117 } else if (!strcasecmp(cmd, "VideoBitRateRange")) {
4119 int minrate, maxrate;
4121 get_arg(arg, sizeof(arg), &p);
4123 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
4124 video_enc.rc_min_rate = minrate * 1000;
4125 video_enc.rc_max_rate = maxrate * 1000;
4127 fprintf(stderr, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4128 filename, line_num, arg);
4132 } else if (!strcasecmp(cmd, "Debug")) {
4134 get_arg(arg, sizeof(arg), &p);
4135 video_enc.debug = strtol(arg,0,0);
4137 } else if (!strcasecmp(cmd, "Strict")) {
4139 get_arg(arg, sizeof(arg), &p);
4140 video_enc.strict_std_compliance = atoi(arg);
4142 } else if (!strcasecmp(cmd, "VideoBufferSize")) {
4144 get_arg(arg, sizeof(arg), &p);
4145 video_enc.rc_buffer_size = atoi(arg) * 8*1024;
4147 } else if (!strcasecmp(cmd, "VideoBitRateTolerance")) {
4149 get_arg(arg, sizeof(arg), &p);
4150 video_enc.bit_rate_tolerance = atoi(arg) * 1000;
4152 } else if (!strcasecmp(cmd, "VideoBitRate")) {
4153 get_arg(arg, sizeof(arg), &p);
4155 video_enc.bit_rate = atoi(arg) * 1000;
4157 } else if (!strcasecmp(cmd, "VideoSize")) {
4158 get_arg(arg, sizeof(arg), &p);
4160 parse_image_size(&video_enc.width, &video_enc.height, arg);
4161 if ((video_enc.width % 16) != 0 ||
4162 (video_enc.height % 16) != 0) {
4163 fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
4164 filename, line_num);
4168 } else if (!strcasecmp(cmd, "VideoFrameRate")) {
4169 get_arg(arg, sizeof(arg), &p);
4171 video_enc.time_base.num= DEFAULT_FRAME_RATE_BASE;
4172 video_enc.time_base.den = (int)(strtod(arg, NULL) * video_enc.time_base.num);
4174 } else if (!strcasecmp(cmd, "VideoGopSize")) {
4175 get_arg(arg, sizeof(arg), &p);
4177 video_enc.gop_size = atoi(arg);
4179 } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
4181 video_enc.gop_size = 1;
4183 } else if (!strcasecmp(cmd, "VideoHighQuality")) {
4185 video_enc.mb_decision = FF_MB_DECISION_BITS;
4187 } else if (!strcasecmp(cmd, "Video4MotionVector")) {
4189 video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
4190 video_enc.flags |= CODEC_FLAG_4MV;
4192 } else if (!strcasecmp(cmd, "VideoTag")) {
4193 get_arg(arg, sizeof(arg), &p);
4194 if ((strlen(arg) == 4) && stream) {
4195 video_enc.codec_tag = ff_get_fourcc(arg);
4197 } else if (!strcasecmp(cmd, "BitExact")) {
4199 video_enc.flags |= CODEC_FLAG_BITEXACT;
4201 } else if (!strcasecmp(cmd, "DctFastint")) {
4203 video_enc.dct_algo = FF_DCT_FASTINT;
4205 } else if (!strcasecmp(cmd, "IdctSimple")) {
4207 video_enc.idct_algo = FF_IDCT_SIMPLE;
4209 } else if (!strcasecmp(cmd, "Qscale")) {
4210 get_arg(arg, sizeof(arg), &p);
4212 video_enc.flags |= CODEC_FLAG_QSCALE;
4213 video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
4215 } else if (!strcasecmp(cmd, "VideoQDiff")) {
4216 get_arg(arg, sizeof(arg), &p);
4218 video_enc.max_qdiff = atoi(arg);
4219 if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
4220 fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
4221 filename, line_num);
4225 } else if (!strcasecmp(cmd, "VideoQMax")) {
4226 get_arg(arg, sizeof(arg), &p);
4228 video_enc.qmax = atoi(arg);
4229 if (video_enc.qmax < 1 || video_enc.qmax > 31) {
4230 fprintf(stderr, "%s:%d: VideoQMax out of range\n",
4231 filename, line_num);
4235 } else if (!strcasecmp(cmd, "VideoQMin")) {
4236 get_arg(arg, sizeof(arg), &p);
4238 video_enc.qmin = atoi(arg);
4239 if (video_enc.qmin < 1 || video_enc.qmin > 31) {
4240 fprintf(stderr, "%s:%d: VideoQMin out of range\n",
4241 filename, line_num);
4245 } else if (!strcasecmp(cmd, "LumaElim")) {
4246 get_arg(arg, sizeof(arg), &p);
4248 video_enc.luma_elim_threshold = atoi(arg);
4250 } else if (!strcasecmp(cmd, "ChromaElim")) {
4251 get_arg(arg, sizeof(arg), &p);
4253 video_enc.chroma_elim_threshold = atoi(arg);
4255 } else if (!strcasecmp(cmd, "LumiMask")) {
4256 get_arg(arg, sizeof(arg), &p);
4258 video_enc.lumi_masking = atof(arg);
4260 } else if (!strcasecmp(cmd, "DarkMask")) {
4261 get_arg(arg, sizeof(arg), &p);
4263 video_enc.dark_masking = atof(arg);
4265 } else if (!strcasecmp(cmd, "NoVideo")) {
4266 video_id = CODEC_ID_NONE;
4267 } else if (!strcasecmp(cmd, "NoAudio")) {
4268 audio_id = CODEC_ID_NONE;
4269 } else if (!strcasecmp(cmd, "ACL")) {
4272 get_arg(arg, sizeof(arg), &p);
4273 if (strcasecmp(arg, "allow") == 0) {
4274 acl.action = IP_ALLOW;
4275 } else if (strcasecmp(arg, "deny") == 0) {
4276 acl.action = IP_DENY;
4278 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4279 filename, line_num, arg);
4283 get_arg(arg, sizeof(arg), &p);
4285 if (resolve_host(&acl.first, arg) != 0) {
4286 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4287 filename, line_num, arg);
4290 acl.last = acl.first;
4293 get_arg(arg, sizeof(arg), &p);
4296 if (resolve_host(&acl.last, arg) != 0) {
4297 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4298 filename, line_num, arg);
4304 IPAddressACL *nacl = (IPAddressACL *) av_mallocz(sizeof(*nacl));
4305 IPAddressACL **naclp = 0;
4311 naclp = &stream->acl;
4315 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
4316 filename, line_num);
4322 naclp = &(*naclp)->next;
4327 } else if (!strcasecmp(cmd, "RTSPOption")) {
4328 get_arg(arg, sizeof(arg), &p);
4330 av_freep(&stream->rtsp_option);
4331 stream->rtsp_option = av_strdup(arg);
4333 } else if (!strcasecmp(cmd, "MulticastAddress")) {
4334 get_arg(arg, sizeof(arg), &p);
4336 if (resolve_host(&stream->multicast_ip, arg) != 0) {
4337 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
4338 filename, line_num, arg);
4341 stream->is_multicast = 1;
4342 stream->loop = 1; /* default is looping */
4344 } else if (!strcasecmp(cmd, "MulticastPort")) {
4345 get_arg(arg, sizeof(arg), &p);
4347 stream->multicast_port = atoi(arg);
4349 } else if (!strcasecmp(cmd, "MulticastTTL")) {
4350 get_arg(arg, sizeof(arg), &p);
4352 stream->multicast_ttl = atoi(arg);
4354 } else if (!strcasecmp(cmd, "NoLoop")) {
4358 } else if (!strcasecmp(cmd, "</Stream>")) {
4360 fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
4361 filename, line_num);
4364 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
4365 if (audio_id != CODEC_ID_NONE) {
4366 audio_enc.codec_type = CODEC_TYPE_AUDIO;
4367 audio_enc.codec_id = audio_id;
4368 add_codec(stream, &audio_enc);
4370 if (video_id != CODEC_ID_NONE) {
4371 video_enc.codec_type = CODEC_TYPE_VIDEO;
4372 video_enc.codec_id = video_id;
4373 add_codec(stream, &video_enc);
4377 } else if (!strcasecmp(cmd, "<Redirect")) {
4378 /*********************************************/
4380 if (stream || feed || redirect) {
4381 fprintf(stderr, "%s:%d: Already in a tag\n",
4382 filename, line_num);
4385 redirect = av_mallocz(sizeof(FFStream));
4386 *last_stream = redirect;
4387 last_stream = &redirect->next;
4389 get_arg(redirect->filename, sizeof(redirect->filename), &p);
4390 q = strrchr(redirect->filename, '>');
4393 redirect->stream_type = STREAM_TYPE_REDIRECT;
4395 } else if (!strcasecmp(cmd, "URL")) {
4397 get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
4399 } else if (!strcasecmp(cmd, "</Redirect>")) {
4401 fprintf(stderr, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4402 filename, line_num);
4405 if (!redirect->feed_filename[0]) {
4406 fprintf(stderr, "%s:%d: No URL found for <Redirect>\n",
4407 filename, line_num);
4411 } else if (!strcasecmp(cmd, "LoadModule")) {
4412 get_arg(arg, sizeof(arg), &p);
4416 fprintf(stderr, "%s:%d: Module support not compiled into this version: '%s'\n",
4417 filename, line_num, arg);
4421 fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
4422 filename, line_num, cmd);
4434 static void show_banner(void)
4436 printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000-2006 Fabrice Bellard, et al.\n");
4439 static void show_help(void)
4442 printf("usage: ffserver [-L] [-h] [-f configfile]\n"
4443 "Hyper fast multi format Audio/Video streaming server\n"
4445 "-L : print the LICENSE\n"
4447 "-f configfile : use configfile instead of /etc/ffserver.conf\n"
4451 static void show_license(void)
4455 "FFmpeg is free software; you can redistribute it and/or\n"
4456 "modify it under the terms of the GNU Lesser General Public\n"
4457 "License as published by the Free Software Foundation; either\n"
4458 "version 2.1 of the License, or (at your option) any later version.\n"
4460 "FFmpeg is distributed in the hope that it will be useful,\n"
4461 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
4462 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
4463 "Lesser General Public License for more details.\n"
4465 "You should have received a copy of the GNU Lesser General Public\n"
4466 "License along with FFmpeg; if not, write to the Free Software\n"
4467 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
4471 static void handle_child_exit(int sig)
4476 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
4479 for (feed = first_feed; feed; feed = feed->next) {
4480 if (feed->pid == pid) {
4481 int uptime = time(0) - feed->pid_start;
4484 fprintf(stderr, "%s: Pid %d exited with status %d after %d seconds\n", feed->filename, pid, status, uptime);
4487 /* Turn off any more restarts */
4488 feed->child_argv = 0;
4494 need_to_start_children = 1;
4497 int main(int argc, char **argv)
4499 const char *config_filename;
4501 struct sigaction sigact;
4505 config_filename = "/etc/ffserver.conf";
4507 my_program_name = argv[0];
4508 my_program_dir = getcwd(0, 0);
4509 ffserver_daemon = 1;
4512 c = getopt(argc, argv, "ndLh?f:");
4528 ffserver_daemon = 0;
4531 config_filename = optarg;
4538 putenv("http_proxy"); /* Kill the http_proxy */
4540 av_init_random(av_gettime() + (getpid() << 16), &random_state);
4542 /* address on which the server will handle HTTP connections */
4543 my_http_addr.sin_family = AF_INET;
4544 my_http_addr.sin_port = htons (8080);
4545 my_http_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4547 /* address on which the server will handle RTSP connections */
4548 my_rtsp_addr.sin_family = AF_INET;
4549 my_rtsp_addr.sin_port = htons (5454);
4550 my_rtsp_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4552 nb_max_connections = 5;
4553 max_bandwidth = 1000;
4554 first_stream = NULL;
4555 logfilename[0] = '\0';
4557 memset(&sigact, 0, sizeof(sigact));
4558 sigact.sa_handler = handle_child_exit;
4559 sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
4560 sigaction(SIGCHLD, &sigact, 0);
4562 if (parse_ffconfig(config_filename) < 0) {
4563 fprintf(stderr, "Incorrect config file - exiting.\n");
4567 build_file_streams();
4569 build_feed_streams();
4571 compute_bandwidth();
4573 /* put the process in background and detach it from its TTY */
4574 if (ffserver_daemon) {
4581 } else if (pid > 0) {
4589 open("/dev/null", O_RDWR);
4590 if (strcmp(logfilename, "-") != 0) {
4600 signal(SIGPIPE, SIG_IGN);
4602 /* open log file if needed */
4603 if (logfilename[0] != '\0') {
4604 if (!strcmp(logfilename, "-"))
4607 logfile = fopen(logfilename, "w");
4610 if (http_server() < 0) {
4611 fprintf(stderr, "Could not start server\n");