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
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_POLL_H
33 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
48 /* maximum number of simultaneous HTTP connections */
49 #define HTTP_MAX_CONNECTIONS 2000
52 HTTPSTATE_WAIT_REQUEST,
53 HTTPSTATE_SEND_HEADER,
54 HTTPSTATE_SEND_DATA_HEADER,
55 HTTPSTATE_SEND_DATA, /* sending TCP or UDP data */
56 HTTPSTATE_SEND_DATA_TRAILER,
57 HTTPSTATE_RECEIVE_DATA,
58 HTTPSTATE_WAIT_FEED, /* wait for data from the feed */
61 RTSPSTATE_WAIT_REQUEST,
63 RTSPSTATE_SEND_PACKET,
66 const char *http_state[] = {
82 #define IOBUFFER_INIT_SIZE 8192
84 /* timeouts are in ms */
85 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
86 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
88 #define SYNC_TIMEOUT (10 * 1000)
91 int64_t count1, count2;
95 /* context associated with one connection */
96 typedef struct HTTPContext {
98 int fd; /* socket file descriptor */
99 struct sockaddr_in from_addr; /* origin */
100 struct pollfd *poll_entry; /* used when polling */
102 uint8_t *buffer_ptr, *buffer_end;
105 struct HTTPContext *next;
106 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
110 /* input format handling */
111 AVFormatContext *fmt_in;
112 int64_t start_time; /* In milliseconds - this wraps fairly often */
113 int64_t first_pts; /* initial pts value */
114 int64_t cur_pts; /* current pts value from the stream in us */
115 int64_t cur_frame_duration; /* duration of the current frame in us */
116 int cur_frame_bytes; /* output frame size, needed to compute
117 the time at which we send each
119 int pts_stream_index; /* stream we choose as clock reference */
120 int64_t cur_clock; /* current clock reference value in us */
121 /* output format handling */
122 struct FFStream *stream;
123 /* -1 is invalid stream */
124 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
125 int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */
127 AVFormatContext fmt_ctx; /* instance of FFStream for one user */
128 int last_packet_sent; /* true if last data packet was sent */
130 DataRateData datarate;
137 int is_packetized; /* if true, the stream is packetized */
138 int packet_stream_index; /* current stream for output in state machine */
140 /* RTSP state specific */
141 uint8_t *pb_buffer; /* XXX: use that in all the code */
143 int seq; /* RTSP sequence number */
145 /* RTP state specific */
146 enum RTSPProtocol rtp_protocol;
147 char session_id[32]; /* session id */
148 AVFormatContext *rtp_ctx[MAX_STREAMS];
150 /* RTP/UDP specific */
151 URLContext *rtp_handles[MAX_STREAMS];
153 /* RTP/TCP specific */
154 struct HTTPContext *rtsp_c;
155 uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
158 static AVFrame dummy_frame;
160 /* each generated stream is described here */
164 STREAM_TYPE_REDIRECT,
167 enum IPAddressAction {
172 typedef struct IPAddressACL {
173 struct IPAddressACL *next;
174 enum IPAddressAction action;
175 /* These are in host order */
176 struct in_addr first;
180 /* description of each stream of the ffserver.conf file */
181 typedef struct FFStream {
182 enum StreamType stream_type;
183 char filename[1024]; /* stream filename */
184 struct FFStream *feed; /* feed we are using (can be null if
186 AVFormatParameters *ap_in; /* input parameters */
187 AVInputFormat *ifmt; /* if non NULL, force input format */
191 int prebuffer; /* Number of millseconds early to start */
192 int64_t max_time; /* Number of milliseconds to run */
194 AVStream *streams[MAX_STREAMS];
195 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
196 char feed_filename[1024]; /* file name of the feed storage, or
197 input file name for a stream */
202 pid_t pid; /* Of ffmpeg process */
203 time_t pid_start; /* Of ffmpeg process */
205 struct FFStream *next;
206 int bandwidth; /* bandwidth, in kbits/s */
209 /* multicast specific */
211 struct in_addr multicast_ip;
212 int multicast_port; /* first port used for multicast */
214 int loop; /* if true, send the stream in loops (only meaningful if file) */
217 int feed_opened; /* true if someone is writing to the feed */
218 int is_feed; /* true if it is a feed */
219 int readonly; /* True if writing is prohibited to the file */
221 int64_t bytes_served;
222 int64_t feed_max_size; /* maximum storage size, zero means unlimited */
223 int64_t feed_write_index; /* current write position in feed (it wraps round) */
224 int64_t feed_size; /* current size of feed */
225 struct FFStream *next_feed;
228 typedef struct FeedData {
229 long long data_count;
230 float avg_frame_size; /* frame size averraged over last frames with exponential mean */
233 static struct sockaddr_in my_http_addr;
234 static struct sockaddr_in my_rtsp_addr;
236 static char logfilename[1024];
237 static HTTPContext *first_http_ctx;
238 static FFStream *first_feed; /* contains only feeds */
239 static FFStream *first_stream; /* contains all streams, including feeds */
241 static void new_connection(int server_fd, int is_rtsp);
242 static void close_connection(HTTPContext *c);
245 static int handle_connection(HTTPContext *c);
246 static int http_parse_request(HTTPContext *c);
247 static int http_send_data(HTTPContext *c);
248 static void compute_stats(HTTPContext *c);
249 static int open_input_stream(HTTPContext *c, const char *info);
250 static int http_start_receive_data(HTTPContext *c);
251 static int http_receive_data(HTTPContext *c);
254 static int rtsp_parse_request(HTTPContext *c);
255 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
256 static void rtsp_cmd_options(HTTPContext *c, const char *url);
257 static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPHeader *h);
258 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h);
259 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h);
260 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h);
263 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
264 struct in_addr my_ip);
267 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
268 FFStream *stream, const char *session_id,
269 enum RTSPProtocol rtp_protocol);
270 static int rtp_new_av_stream(HTTPContext *c,
271 int stream_index, struct sockaddr_in *dest_addr,
272 HTTPContext *rtsp_c);
274 static const char *my_program_name;
275 static const char *my_program_dir;
277 static int ffserver_debug;
278 static int ffserver_daemon;
279 static int no_launch;
280 static int need_to_start_children;
282 static int nb_max_connections;
283 static int nb_connections;
285 static int max_bandwidth;
286 static int current_bandwidth;
288 static int64_t cur_time; // Making this global saves on passing it around everywhere
290 static AVRandomState random_state;
292 static FILE *logfile = NULL;
294 static void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
300 vfprintf(logfile, fmt, ap);
306 static char *ctime1(char *buf2)
314 p = buf2 + strlen(p) - 1;
320 static void log_connection(HTTPContext *c)
327 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64"\n",
328 inet_ntoa(c->from_addr.sin_addr),
329 ctime1(buf2), c->method, c->url,
330 c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
333 static void update_datarate(DataRateData *drd, int64_t count)
335 if (!drd->time1 && !drd->count1) {
336 drd->time1 = drd->time2 = cur_time;
337 drd->count1 = drd->count2 = count;
339 if (cur_time - drd->time2 > 5000) {
340 drd->time1 = drd->time2;
341 drd->count1 = drd->count2;
342 drd->time2 = cur_time;
348 /* In bytes per second */
349 static int compute_datarate(DataRateData *drd, int64_t count)
351 if (cur_time == drd->time1)
354 return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
358 static void start_children(FFStream *feed)
363 for (; feed; feed = feed->next) {
364 if (feed->child_argv && !feed->pid) {
365 feed->pid_start = time(0);
370 fprintf(stderr, "Unable to create children\n");
379 for (i = 3; i < 256; i++) {
383 if (!ffserver_debug) {
384 i = open("/dev/null", O_RDWR);
393 pstrcpy(pathname, sizeof(pathname), my_program_name);
395 slash = strrchr(pathname, '/');
401 strcpy(slash, "ffmpeg");
403 /* This is needed to make relative pathnames work */
404 chdir(my_program_dir);
406 signal(SIGPIPE, SIG_DFL);
408 execvp(pathname, feed->child_argv);
416 /* open a listening socket */
417 static int socket_open_listen(struct sockaddr_in *my_addr)
421 server_fd = socket(AF_INET,SOCK_STREAM,0);
428 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
430 if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
432 snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
434 closesocket(server_fd);
438 if (listen (server_fd, 5) < 0) {
440 closesocket(server_fd);
443 ff_socket_nonblock(server_fd, 1);
448 /* start all multicast streams */
449 static void start_multicast(void)
454 struct sockaddr_in dest_addr;
455 int default_port, stream_index;
458 for(stream = first_stream; stream != NULL; stream = stream->next) {
459 if (stream->is_multicast) {
460 /* open the RTP connection */
461 snprintf(session_id, sizeof(session_id), "%08x%08x",
462 av_random(&random_state), av_random(&random_state));
464 /* choose a port if none given */
465 if (stream->multicast_port == 0) {
466 stream->multicast_port = default_port;
470 dest_addr.sin_family = AF_INET;
471 dest_addr.sin_addr = stream->multicast_ip;
472 dest_addr.sin_port = htons(stream->multicast_port);
474 rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
475 RTSP_PROTOCOL_RTP_UDP_MULTICAST);
479 if (open_input_stream(rtp_c, "") < 0) {
480 fprintf(stderr, "Could not open input stream for stream '%s'\n",
485 /* open each RTP stream */
486 for(stream_index = 0; stream_index < stream->nb_streams;
488 dest_addr.sin_port = htons(stream->multicast_port +
490 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) < 0) {
491 fprintf(stderr, "Could not open output stream '%s/streamid=%d'\n",
492 stream->filename, stream_index);
497 /* change state to send data */
498 rtp_c->state = HTTPSTATE_SEND_DATA;
503 /* main loop of the http server */
504 static int http_server(void)
506 int server_fd, ret, rtsp_server_fd, delay, delay1;
507 struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 2], *poll_entry;
508 HTTPContext *c, *c_next;
510 server_fd = socket_open_listen(&my_http_addr);
514 rtsp_server_fd = socket_open_listen(&my_rtsp_addr);
515 if (rtsp_server_fd < 0)
518 http_log("ffserver started.\n");
520 start_children(first_feed);
522 first_http_ctx = NULL;
528 poll_entry = poll_table;
529 poll_entry->fd = server_fd;
530 poll_entry->events = POLLIN;
533 poll_entry->fd = rtsp_server_fd;
534 poll_entry->events = POLLIN;
537 /* wait for events on each HTTP handle */
544 case HTTPSTATE_SEND_HEADER:
545 case RTSPSTATE_SEND_REPLY:
546 case RTSPSTATE_SEND_PACKET:
547 c->poll_entry = poll_entry;
549 poll_entry->events = POLLOUT;
552 case HTTPSTATE_SEND_DATA_HEADER:
553 case HTTPSTATE_SEND_DATA:
554 case HTTPSTATE_SEND_DATA_TRAILER:
555 if (!c->is_packetized) {
556 /* for TCP, we output as much as we can (may need to put a limit) */
557 c->poll_entry = poll_entry;
559 poll_entry->events = POLLOUT;
562 /* when ffserver is doing the timing, we work by
563 looking at which packet need to be sent every
565 delay1 = 10; /* one tick wait XXX: 10 ms assumed */
570 case HTTPSTATE_WAIT_REQUEST:
571 case HTTPSTATE_RECEIVE_DATA:
572 case HTTPSTATE_WAIT_FEED:
573 case RTSPSTATE_WAIT_REQUEST:
574 /* need to catch errors */
575 c->poll_entry = poll_entry;
577 poll_entry->events = POLLIN;/* Maybe this will work */
581 c->poll_entry = NULL;
587 /* wait for an event on one connection. We poll at least every
588 second to handle timeouts */
590 ret = poll(poll_table, poll_entry - poll_table, delay);
591 if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
592 ff_neterrno() != FF_NETERROR(EINTR))
596 cur_time = av_gettime() / 1000;
598 if (need_to_start_children) {
599 need_to_start_children = 0;
600 start_children(first_feed);
603 /* now handle the events */
604 for(c = first_http_ctx; c != NULL; c = c_next) {
606 if (handle_connection(c) < 0) {
607 /* close and free the connection */
613 poll_entry = poll_table;
614 /* new HTTP connection request ? */
615 if (poll_entry->revents & POLLIN) {
616 new_connection(server_fd, 0);
619 /* new RTSP connection request ? */
620 if (poll_entry->revents & POLLIN) {
621 new_connection(rtsp_server_fd, 1);
626 /* start waiting for a new HTTP/RTSP request */
627 static void start_wait_request(HTTPContext *c, int is_rtsp)
629 c->buffer_ptr = c->buffer;
630 c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
633 c->timeout = cur_time + RTSP_REQUEST_TIMEOUT;
634 c->state = RTSPSTATE_WAIT_REQUEST;
636 c->timeout = cur_time + HTTP_REQUEST_TIMEOUT;
637 c->state = HTTPSTATE_WAIT_REQUEST;
641 static void new_connection(int server_fd, int is_rtsp)
643 struct sockaddr_in from_addr;
645 HTTPContext *c = NULL;
647 len = sizeof(from_addr);
648 fd = accept(server_fd, (struct sockaddr *)&from_addr,
652 ff_socket_nonblock(fd, 1);
654 /* XXX: should output a warning page when coming
655 close to the connection limit */
656 if (nb_connections >= nb_max_connections)
659 /* add a new connection */
660 c = av_mallocz(sizeof(HTTPContext));
665 c->poll_entry = NULL;
666 c->from_addr = from_addr;
667 c->buffer_size = IOBUFFER_INIT_SIZE;
668 c->buffer = av_malloc(c->buffer_size);
672 c->next = first_http_ctx;
676 start_wait_request(c, is_rtsp);
688 static void close_connection(HTTPContext *c)
690 HTTPContext **cp, *c1;
692 AVFormatContext *ctx;
696 /* remove connection from list */
697 cp = &first_http_ctx;
698 while ((*cp) != NULL) {
707 /* remove references, if any (XXX: do it faster) */
708 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
713 /* remove connection associated resources */
717 /* close each frame parser */
718 for(i=0;i<c->fmt_in->nb_streams;i++) {
719 st = c->fmt_in->streams[i];
720 if (st->codec->codec) {
721 avcodec_close(st->codec);
724 av_close_input_file(c->fmt_in);
727 /* free RTP output streams if any */
730 nb_streams = c->stream->nb_streams;
732 for(i=0;i<nb_streams;i++) {
735 av_write_trailer(ctx);
738 h = c->rtp_handles[i];
746 if (!c->last_packet_sent) {
749 if (url_open_dyn_buf(&ctx->pb) >= 0) {
750 av_write_trailer(ctx);
751 url_close_dyn_buf(&ctx->pb, &c->pb_buffer);
756 for(i=0; i<ctx->nb_streams; i++)
757 av_free(ctx->streams[i]) ;
759 if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
760 current_bandwidth -= c->stream->bandwidth;
762 /* signal that there is no feed if we are the feeder socket */
763 if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
764 c->stream->feed_opened = 0;
768 av_freep(&c->pb_buffer);
769 av_freep(&c->packet_buffer);
775 static int handle_connection(HTTPContext *c)
780 case HTTPSTATE_WAIT_REQUEST:
781 case RTSPSTATE_WAIT_REQUEST:
783 if ((c->timeout - cur_time) < 0)
785 if (c->poll_entry->revents & (POLLERR | POLLHUP))
788 /* no need to read if no events */
789 if (!(c->poll_entry->revents & POLLIN))
793 len = recv(c->fd, c->buffer_ptr, 1, 0);
795 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
796 ff_neterrno() != FF_NETERROR(EINTR))
798 } else if (len == 0) {
801 /* search for end of request. */
803 c->buffer_ptr += len;
805 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
806 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
807 /* request found : parse it and reply */
808 if (c->state == HTTPSTATE_WAIT_REQUEST) {
809 ret = http_parse_request(c);
811 ret = rtsp_parse_request(c);
815 } else if (ptr >= c->buffer_end) {
816 /* request too long: cannot do anything */
818 } else goto read_loop;
822 case HTTPSTATE_SEND_HEADER:
823 if (c->poll_entry->revents & (POLLERR | POLLHUP))
826 /* no need to write if no events */
827 if (!(c->poll_entry->revents & POLLOUT))
829 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
831 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
832 ff_neterrno() != FF_NETERROR(EINTR)) {
833 /* error : close connection */
834 av_freep(&c->pb_buffer);
838 c->buffer_ptr += len;
840 c->stream->bytes_served += len;
841 c->data_count += len;
842 if (c->buffer_ptr >= c->buffer_end) {
843 av_freep(&c->pb_buffer);
848 /* all the buffer was sent : synchronize to the incoming stream */
849 c->state = HTTPSTATE_SEND_DATA_HEADER;
850 c->buffer_ptr = c->buffer_end = c->buffer;
855 case HTTPSTATE_SEND_DATA:
856 case HTTPSTATE_SEND_DATA_HEADER:
857 case HTTPSTATE_SEND_DATA_TRAILER:
858 /* for packetized output, we consider we can always write (the
859 input streams sets the speed). It may be better to verify
860 that we do not rely too much on the kernel queues */
861 if (!c->is_packetized) {
862 if (c->poll_entry->revents & (POLLERR | POLLHUP))
865 /* no need to read if no events */
866 if (!(c->poll_entry->revents & POLLOUT))
869 if (http_send_data(c) < 0)
871 /* close connection if trailer sent */
872 if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
875 case HTTPSTATE_RECEIVE_DATA:
876 /* no need to read if no events */
877 if (c->poll_entry->revents & (POLLERR | POLLHUP))
879 if (!(c->poll_entry->revents & POLLIN))
881 if (http_receive_data(c) < 0)
884 case HTTPSTATE_WAIT_FEED:
885 /* no need to read if no events */
886 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
889 /* nothing to do, we'll be waken up by incoming feed packets */
892 case RTSPSTATE_SEND_REPLY:
893 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
894 av_freep(&c->pb_buffer);
897 /* no need to write if no events */
898 if (!(c->poll_entry->revents & POLLOUT))
900 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
902 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
903 ff_neterrno() != FF_NETERROR(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 (ff_neterrno() != FF_NETERROR(EAGAIN) &&
930 ff_neterrno() != FF_NETERROR(EINTR)) {
931 /* error : close connection */
932 av_freep(&c->packet_buffer);
936 c->packet_buffer_ptr += len;
937 if (c->packet_buffer_ptr >= c->packet_buffer_end) {
938 /* all the buffer was sent : wait for a new request */
939 av_freep(&c->packet_buffer);
940 c->state = RTSPSTATE_WAIT_REQUEST;
944 case HTTPSTATE_READY:
953 static int extract_rates(char *rates, int ratelen, const char *request)
957 for (p = request; *p && *p != '\r' && *p != '\n'; ) {
958 if (strncasecmp(p, "Pragma:", 7) == 0) {
959 const char *q = p + 7;
961 while (*q && *q != '\n' && isspace(*q))
964 if (strncasecmp(q, "stream-switch-entry=", 20) == 0) {
970 memset(rates, 0xff, ratelen);
973 while (*q && *q != '\n' && *q != ':')
976 if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2) {
980 if (stream_no < ratelen && stream_no >= 0) {
981 rates[stream_no] = rate_no;
984 while (*q && *q != '\n' && !isspace(*q))
1001 static int find_stream_in_feed(FFStream *feed, AVCodecContext *codec, int bit_rate)
1004 int best_bitrate = 100000000;
1007 for (i = 0; i < feed->nb_streams; i++) {
1008 AVCodecContext *feed_codec = feed->streams[i]->codec;
1010 if (feed_codec->codec_id != codec->codec_id ||
1011 feed_codec->sample_rate != codec->sample_rate ||
1012 feed_codec->width != codec->width ||
1013 feed_codec->height != codec->height) {
1017 /* Potential stream */
1019 /* We want the fastest stream less than bit_rate, or the slowest
1020 * faster than bit_rate
1023 if (feed_codec->bit_rate <= bit_rate) {
1024 if (best_bitrate > bit_rate || feed_codec->bit_rate > best_bitrate) {
1025 best_bitrate = feed_codec->bit_rate;
1029 if (feed_codec->bit_rate < best_bitrate) {
1030 best_bitrate = feed_codec->bit_rate;
1039 static int modify_current_stream(HTTPContext *c, char *rates)
1042 FFStream *req = c->stream;
1043 int action_required = 0;
1045 /* Not much we can do for a feed */
1049 for (i = 0; i < req->nb_streams; i++) {
1050 AVCodecContext *codec = req->streams[i]->codec;
1054 c->switch_feed_streams[i] = req->feed_streams[i];
1057 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1060 /* Wants off or slow */
1061 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1063 /* This doesn't work well when it turns off the only stream! */
1064 c->switch_feed_streams[i] = -2;
1065 c->feed_streams[i] = -2;
1070 if (c->switch_feed_streams[i] >= 0 && c->switch_feed_streams[i] != c->feed_streams[i])
1071 action_required = 1;
1074 return action_required;
1078 static void do_switch_stream(HTTPContext *c, int i)
1080 if (c->switch_feed_streams[i] >= 0) {
1082 c->feed_streams[i] = c->switch_feed_streams[i];
1085 /* Now update the stream */
1087 c->switch_feed_streams[i] = -1;
1090 /* XXX: factorize in utils.c ? */
1091 /* XXX: take care with different space meaning */
1092 static void skip_spaces(const char **pp)
1096 while (*p == ' ' || *p == '\t')
1101 static void get_word(char *buf, int buf_size, const char **pp)
1109 while (!isspace(*p) && *p != '\0') {
1110 if ((q - buf) < buf_size - 1)
1119 static int validate_acl(FFStream *stream, HTTPContext *c)
1121 enum IPAddressAction last_action = IP_DENY;
1123 struct in_addr *src = &c->from_addr.sin_addr;
1124 unsigned long src_addr = src->s_addr;
1126 for (acl = stream->acl; acl; acl = acl->next) {
1127 if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr) {
1128 return (acl->action == IP_ALLOW) ? 1 : 0;
1130 last_action = acl->action;
1133 /* Nothing matched, so return not the last action */
1134 return (last_action == IP_DENY) ? 1 : 0;
1137 /* compute the real filename of a file by matching it without its
1138 extensions to all the stream filenames */
1139 static void compute_real_filename(char *filename, int max_size)
1146 /* compute filename by matching without the file extensions */
1147 pstrcpy(file1, sizeof(file1), filename);
1148 p = strrchr(file1, '.');
1151 for(stream = first_stream; stream != NULL; stream = stream->next) {
1152 pstrcpy(file2, sizeof(file2), stream->filename);
1153 p = strrchr(file2, '.');
1156 if (!strcmp(file1, file2)) {
1157 pstrcpy(filename, max_size, stream->filename);
1172 /* parse http request and prepare header */
1173 static int http_parse_request(HTTPContext *c)
1176 enum RedirType redir_type;
1178 char info[1024], filename[1024];
1182 const char *mime_type;
1186 char *useragent = 0;
1189 get_word(cmd, sizeof(cmd), (const char **)&p);
1190 pstrcpy(c->method, sizeof(c->method), cmd);
1192 if (!strcmp(cmd, "GET"))
1194 else if (!strcmp(cmd, "POST"))
1199 get_word(url, sizeof(url), (const char **)&p);
1200 pstrcpy(c->url, sizeof(c->url), url);
1202 get_word(protocol, sizeof(protocol), (const char **)&p);
1203 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1206 pstrcpy(c->protocol, sizeof(c->protocol), protocol);
1209 http_log("New connection: %s %s\n", cmd, url);
1211 /* find the filename and the optional info string in the request */
1212 p = strchr(url, '?');
1214 pstrcpy(info, sizeof(info), p);
1220 pstrcpy(filename, sizeof(filename)-1, url + ((*url == '/') ? 1 : 0));
1222 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1223 if (strncasecmp(p, "User-Agent:", 11) == 0) {
1225 if (*useragent && *useragent != '\n' && isspace(*useragent))
1229 p = strchr(p, '\n');
1236 redir_type = REDIR_NONE;
1237 if (match_ext(filename, "asx")) {
1238 redir_type = REDIR_ASX;
1239 filename[strlen(filename)-1] = 'f';
1240 } else if (match_ext(filename, "asf") &&
1241 (!useragent || strncasecmp(useragent, "NSPlayer", 8) != 0)) {
1242 /* if this isn't WMP or lookalike, return the redirector file */
1243 redir_type = REDIR_ASF;
1244 } else if (match_ext(filename, "rpm,ram")) {
1245 redir_type = REDIR_RAM;
1246 strcpy(filename + strlen(filename)-2, "m");
1247 } else if (match_ext(filename, "rtsp")) {
1248 redir_type = REDIR_RTSP;
1249 compute_real_filename(filename, sizeof(filename) - 1);
1250 } else if (match_ext(filename, "sdp")) {
1251 redir_type = REDIR_SDP;
1252 compute_real_filename(filename, sizeof(filename) - 1);
1255 // "redirect" / request to index.html
1256 if (!strlen(filename))
1257 pstrcpy(filename, sizeof(filename) - 1, "index.html");
1259 stream = first_stream;
1260 while (stream != NULL) {
1261 if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
1263 stream = stream->next;
1265 if (stream == NULL) {
1266 snprintf(msg, sizeof(msg), "File '%s' not found", url);
1271 memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1272 memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1274 if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1275 c->http_error = 301;
1277 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 301 Moved\r\n");
1278 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Location: %s\r\n", stream->feed_filename);
1279 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1280 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1281 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Moved</title></head><body>\r\n");
1282 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "You should be <a href=\"%s\">redirected</a>.\r\n", stream->feed_filename);
1283 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1285 /* prepare output buffer */
1286 c->buffer_ptr = c->buffer;
1288 c->state = HTTPSTATE_SEND_HEADER;
1292 /* If this is WMP, get the rate information */
1293 if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1294 if (modify_current_stream(c, ratebuf)) {
1295 for (i = 0; i < sizeof(c->feed_streams) / sizeof(c->feed_streams[0]); i++) {
1296 if (c->switch_feed_streams[i] >= 0)
1297 do_switch_stream(c, i);
1302 /* If already streaming this feed, dont let start an another feeder */
1303 if (stream->feed_opened) {
1304 snprintf(msg, sizeof(msg), "This feed is already being received.");
1308 if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE) {
1309 current_bandwidth += stream->bandwidth;
1312 if (c->post == 0 && max_bandwidth < current_bandwidth) {
1313 c->http_error = 200;
1315 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 Server too busy\r\n");
1316 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1317 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1318 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Too busy</title></head><body>\r\n");
1319 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");
1320 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",
1321 current_bandwidth, max_bandwidth);
1322 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1324 /* prepare output buffer */
1325 c->buffer_ptr = c->buffer;
1327 c->state = HTTPSTATE_SEND_HEADER;
1331 if (redir_type != REDIR_NONE) {
1334 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1335 if (strncasecmp(p, "Host:", 5) == 0) {
1339 p = strchr(p, '\n');
1350 while (isspace(*hostinfo))
1353 eoh = strchr(hostinfo, '\n');
1355 if (eoh[-1] == '\r')
1358 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1359 memcpy(hostbuf, hostinfo, eoh - hostinfo);
1360 hostbuf[eoh - hostinfo] = 0;
1362 c->http_error = 200;
1364 switch(redir_type) {
1366 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASX Follows\r\n");
1367 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1368 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1369 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ASX Version=\"3\">\r\n");
1370 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1371 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1372 hostbuf, filename, info);
1373 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</ASX>\r\n");
1376 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RAM Follows\r\n");
1377 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: audio/x-pn-realaudio\r\n");
1378 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1379 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "# Autogenerated by ffserver\r\n");
1380 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "http://%s/%s%s\r\n",
1381 hostbuf, filename, info);
1384 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASF Redirect follows\r\n");
1385 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1386 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1387 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "[Reference]\r\n");
1388 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Ref1=http://%s/%s%s\r\n",
1389 hostbuf, filename, info);
1393 char hostname[256], *p;
1394 /* extract only hostname */
1395 pstrcpy(hostname, sizeof(hostname), hostbuf);
1396 p = strrchr(hostname, ':');
1399 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1400 /* XXX: incorrect mime type ? */
1401 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/x-rtsp\r\n");
1402 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1403 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "rtsp://%s:%d/%s\r\n",
1404 hostname, ntohs(my_rtsp_addr.sin_port),
1411 int sdp_data_size, len;
1412 struct sockaddr_in my_addr;
1414 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1415 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/sdp\r\n");
1416 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1418 len = sizeof(my_addr);
1419 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
1421 /* XXX: should use a dynamic buffer */
1422 sdp_data_size = prepare_sdp_description(stream,
1425 if (sdp_data_size > 0) {
1426 memcpy(q, sdp_data, sdp_data_size);
1438 /* prepare output buffer */
1439 c->buffer_ptr = c->buffer;
1441 c->state = HTTPSTATE_SEND_HEADER;
1447 snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
1451 stream->conns_served++;
1453 /* XXX: add there authenticate and IP match */
1456 /* if post, it means a feed is being sent */
1457 if (!stream->is_feed) {
1458 /* However it might be a status report from WMP! Lets log the data
1459 * as it might come in handy one day
1464 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1465 if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1469 if (strncasecmp(p, "Pragma: client-id=", 18) == 0) {
1470 client_id = strtol(p + 18, 0, 10);
1472 p = strchr(p, '\n');
1480 char *eol = strchr(logline, '\n');
1485 if (eol[-1] == '\r')
1487 http_log("%.*s\n", (int) (eol - logline), logline);
1488 c->suppress_log = 1;
1493 http_log("\nGot request:\n%s\n", c->buffer);
1496 if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1499 /* Now we have to find the client_id */
1500 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1501 if (wmpc->wmp_client_id == client_id)
1506 if (modify_current_stream(wmpc, ratebuf)) {
1507 wmpc->switch_pending = 1;
1512 snprintf(msg, sizeof(msg), "POST command not handled");
1516 if (http_start_receive_data(c) < 0) {
1517 snprintf(msg, sizeof(msg), "could not open feed");
1521 c->state = HTTPSTATE_RECEIVE_DATA;
1526 if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0) {
1527 http_log("\nGot request:\n%s\n", c->buffer);
1531 if (c->stream->stream_type == STREAM_TYPE_STATUS)
1534 /* open input stream */
1535 if (open_input_stream(c, info) < 0) {
1536 snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1540 /* prepare http header */
1542 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1543 mime_type = c->stream->fmt->mime_type;
1545 mime_type = "application/x-octet-stream";
1546 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
1548 /* for asf, we need extra headers */
1549 if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1550 /* Need to allocate a client id */
1552 c->wmp_client_id = av_random(&random_state) & 0x7fffffff;
1554 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);
1556 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-Type: %s\r\n", mime_type);
1557 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1559 /* prepare output buffer */
1561 c->buffer_ptr = c->buffer;
1563 c->state = HTTPSTATE_SEND_HEADER;
1566 c->http_error = 404;
1568 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 404 Not Found\r\n");
1569 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: %s\r\n", "text/html");
1570 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1571 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HTML>\n");
1572 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1573 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<BODY>%s</BODY>\n", msg);
1574 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</HTML>\n");
1576 /* prepare output buffer */
1577 c->buffer_ptr = c->buffer;
1579 c->state = HTTPSTATE_SEND_HEADER;
1583 c->http_error = 200; /* horrible : we use this value to avoid
1584 going to the send data state */
1585 c->state = HTTPSTATE_SEND_HEADER;
1589 static void fmt_bytecount(ByteIOContext *pb, int64_t count)
1591 static const char *suffix = " kMGTP";
1594 for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++) {
1597 url_fprintf(pb, "%"PRId64"%c", count, *s);
1600 static void compute_stats(HTTPContext *c)
1607 ByteIOContext pb1, *pb = &pb1;
1609 if (url_open_dyn_buf(pb) < 0) {
1610 /* XXX: return an error ? */
1611 c->buffer_ptr = c->buffer;
1612 c->buffer_end = c->buffer;
1616 url_fprintf(pb, "HTTP/1.0 200 OK\r\n");
1617 url_fprintf(pb, "Content-type: %s\r\n", "text/html");
1618 url_fprintf(pb, "Pragma: no-cache\r\n");
1619 url_fprintf(pb, "\r\n");
1621 url_fprintf(pb, "<HEAD><TITLE>FFServer Status</TITLE>\n");
1622 if (c->stream->feed_filename) {
1623 url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
1625 url_fprintf(pb, "</HEAD>\n<BODY>");
1626 url_fprintf(pb, "<H1>FFServer Status</H1>\n");
1628 url_fprintf(pb, "<H2>Available Streams</H2>\n");
1629 url_fprintf(pb, "<TABLE cellspacing=0 cellpadding=4>\n");
1630 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");
1631 stream = first_stream;
1632 while (stream != NULL) {
1633 char sfilename[1024];
1636 if (stream->feed != stream) {
1637 pstrcpy(sfilename, sizeof(sfilename) - 10, stream->filename);
1638 eosf = sfilename + strlen(sfilename);
1639 if (eosf - sfilename >= 4) {
1640 if (strcmp(eosf - 4, ".asf") == 0) {
1641 strcpy(eosf - 4, ".asx");
1642 } else if (strcmp(eosf - 3, ".rm") == 0) {
1643 strcpy(eosf - 3, ".ram");
1644 } else if (stream->fmt == &rtp_muxer) {
1645 /* generate a sample RTSP director if
1646 unicast. Generate an SDP redirector if
1648 eosf = strrchr(sfilename, '.');
1650 eosf = sfilename + strlen(sfilename);
1651 if (stream->is_multicast)
1652 strcpy(eosf, ".sdp");
1654 strcpy(eosf, ".rtsp");
1658 url_fprintf(pb, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1659 sfilename, stream->filename);
1660 url_fprintf(pb, "<td align=right> %d <td align=right> ",
1661 stream->conns_served);
1662 fmt_bytecount(pb, stream->bytes_served);
1663 switch(stream->stream_type) {
1664 case STREAM_TYPE_LIVE:
1666 int audio_bit_rate = 0;
1667 int video_bit_rate = 0;
1668 const char *audio_codec_name = "";
1669 const char *video_codec_name = "";
1670 const char *audio_codec_name_extra = "";
1671 const char *video_codec_name_extra = "";
1673 for(i=0;i<stream->nb_streams;i++) {
1674 AVStream *st = stream->streams[i];
1675 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1676 switch(st->codec->codec_type) {
1677 case CODEC_TYPE_AUDIO:
1678 audio_bit_rate += st->codec->bit_rate;
1680 if (*audio_codec_name)
1681 audio_codec_name_extra = "...";
1682 audio_codec_name = codec->name;
1685 case CODEC_TYPE_VIDEO:
1686 video_bit_rate += st->codec->bit_rate;
1688 if (*video_codec_name)
1689 video_codec_name_extra = "...";
1690 video_codec_name = codec->name;
1693 case CODEC_TYPE_DATA:
1694 video_bit_rate += st->codec->bit_rate;
1700 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",
1703 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
1704 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
1706 url_fprintf(pb, "<TD>%s", stream->feed->filename);
1708 url_fprintf(pb, "<TD>%s", stream->feed_filename);
1710 url_fprintf(pb, "\n");
1714 url_fprintf(pb, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1718 stream = stream->next;
1720 url_fprintf(pb, "</TABLE>\n");
1722 stream = first_stream;
1723 while (stream != NULL) {
1724 if (stream->feed == stream) {
1725 url_fprintf(pb, "<h2>Feed %s</h2>", stream->filename);
1727 url_fprintf(pb, "Running as pid %d.\n", stream->pid);
1729 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1734 /* This is somewhat linux specific I guess */
1735 snprintf(ps_cmd, sizeof(ps_cmd),
1736 "ps -o \"%%cpu,cputime\" --no-headers %d",
1739 pid_stat = popen(ps_cmd, "r");
1744 if (fscanf(pid_stat, "%10s %64s", cpuperc,
1746 url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",
1754 url_fprintf(pb, "<p>");
1756 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");
1758 for (i = 0; i < stream->nb_streams; i++) {
1759 AVStream *st = stream->streams[i];
1760 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1761 const char *type = "unknown";
1762 char parameters[64];
1766 switch(st->codec->codec_type) {
1767 case CODEC_TYPE_AUDIO:
1769 snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
1771 case CODEC_TYPE_VIDEO:
1773 snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,
1774 st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);
1779 url_fprintf(pb, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1780 i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);
1782 url_fprintf(pb, "</table>\n");
1785 stream = stream->next;
1791 AVCodecContext *enc;
1795 stream = first_feed;
1796 while (stream != NULL) {
1797 url_fprintf(pb, "<H1>Feed '%s'</H1>\n", stream->filename);
1798 url_fprintf(pb, "<TABLE>\n");
1799 url_fprintf(pb, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1800 for(i=0;i<stream->nb_streams;i++) {
1801 AVStream *st = stream->streams[i];
1802 FeedData *fdata = st->priv_data;
1805 avcodec_string(buf, sizeof(buf), enc);
1806 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
1807 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
1808 avg /= enc->frame_size;
1809 url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
1810 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
1812 url_fprintf(pb, "</TABLE>\n");
1813 stream = stream->next_feed;
1818 /* connection status */
1819 url_fprintf(pb, "<H2>Connection Status</H2>\n");
1821 url_fprintf(pb, "Number of connections: %d / %d<BR>\n",
1822 nb_connections, nb_max_connections);
1824 url_fprintf(pb, "Bandwidth in use: %dk / %dk<BR>\n",
1825 current_bandwidth, max_bandwidth);
1827 url_fprintf(pb, "<TABLE>\n");
1828 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");
1829 c1 = first_http_ctx;
1831 while (c1 != NULL) {
1837 for (j = 0; j < c1->stream->nb_streams; j++) {
1838 if (!c1->stream->feed) {
1839 bitrate += c1->stream->streams[j]->codec->bit_rate;
1841 if (c1->feed_streams[j] >= 0) {
1842 bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
1849 p = inet_ntoa(c1->from_addr.sin_addr);
1850 url_fprintf(pb, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1852 c1->stream ? c1->stream->filename : "",
1853 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
1856 http_state[c1->state]);
1857 fmt_bytecount(pb, bitrate);
1858 url_fprintf(pb, "<td align=right>");
1859 fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
1860 url_fprintf(pb, "<td align=right>");
1861 fmt_bytecount(pb, c1->data_count);
1862 url_fprintf(pb, "\n");
1865 url_fprintf(pb, "</TABLE>\n");
1870 url_fprintf(pb, "<HR size=1 noshade>Generated at %s", p);
1871 url_fprintf(pb, "</BODY>\n</HTML>\n");
1873 len = url_close_dyn_buf(pb, &c->pb_buffer);
1874 c->buffer_ptr = c->pb_buffer;
1875 c->buffer_end = c->pb_buffer + len;
1878 /* check if the parser needs to be opened for stream i */
1879 static void open_parser(AVFormatContext *s, int i)
1881 AVStream *st = s->streams[i];
1884 if (!st->codec->codec) {
1885 codec = avcodec_find_decoder(st->codec->codec_id);
1886 if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
1887 st->codec->parse_only = 1;
1888 if (avcodec_open(st->codec, codec) < 0) {
1889 st->codec->parse_only = 0;
1895 static int open_input_stream(HTTPContext *c, const char *info)
1898 char input_filename[1024];
1903 /* find file name */
1904 if (c->stream->feed) {
1905 strcpy(input_filename, c->stream->feed->feed_filename);
1906 buf_size = FFM_PACKET_SIZE;
1907 /* compute position (absolute time) */
1908 if (find_info_tag(buf, sizeof(buf), "date", info)) {
1909 stream_pos = parse_date(buf, 0);
1910 } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
1911 int prebuffer = strtol(buf, 0, 10);
1912 stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
1914 stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
1917 strcpy(input_filename, c->stream->feed_filename);
1919 /* compute position (relative time) */
1920 if (find_info_tag(buf, sizeof(buf), "date", info)) {
1921 stream_pos = parse_date(buf, 1);
1926 if (input_filename[0] == '\0')
1930 { time_t when = stream_pos / 1000000;
1931 http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
1936 if (av_open_input_file(&s, input_filename, c->stream->ifmt,
1937 buf_size, c->stream->ap_in) < 0) {
1938 http_log("%s not found", input_filename);
1943 /* open each parser */
1944 for(i=0;i<s->nb_streams;i++)
1947 /* choose stream as clock source (we favorize video stream if
1948 present) for packet sending */
1949 c->pts_stream_index = 0;
1950 for(i=0;i<c->stream->nb_streams;i++) {
1951 if (c->pts_stream_index == 0 &&
1952 c->stream->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
1953 c->pts_stream_index = i;
1958 if (c->fmt_in->iformat->read_seek) {
1959 c->fmt_in->iformat->read_seek(c->fmt_in, 0, stream_pos, 0);
1962 /* set the start time (needed for maxtime and RTP packet timing) */
1963 c->start_time = cur_time;
1964 c->first_pts = AV_NOPTS_VALUE;
1968 /* return the server clock (in us) */
1969 static int64_t get_server_clock(HTTPContext *c)
1971 /* compute current pts value from system time */
1972 return (cur_time - c->start_time) * 1000;
1975 /* return the estimated time at which the current packet must be sent
1977 static int64_t get_packet_send_clock(HTTPContext *c)
1979 int bytes_left, bytes_sent, frame_bytes;
1981 frame_bytes = c->cur_frame_bytes;
1982 if (frame_bytes <= 0) {
1985 bytes_left = c->buffer_end - c->buffer_ptr;
1986 bytes_sent = frame_bytes - bytes_left;
1987 return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
1992 static int http_prepare_data(HTTPContext *c)
1995 AVFormatContext *ctx;
1997 av_freep(&c->pb_buffer);
1999 case HTTPSTATE_SEND_DATA_HEADER:
2000 memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
2001 pstrcpy(c->fmt_ctx.author, sizeof(c->fmt_ctx.author),
2003 pstrcpy(c->fmt_ctx.comment, sizeof(c->fmt_ctx.comment),
2004 c->stream->comment);
2005 pstrcpy(c->fmt_ctx.copyright, sizeof(c->fmt_ctx.copyright),
2006 c->stream->copyright);
2007 pstrcpy(c->fmt_ctx.title, sizeof(c->fmt_ctx.title),
2010 /* open output stream by using specified codecs */
2011 c->fmt_ctx.oformat = c->stream->fmt;
2012 c->fmt_ctx.nb_streams = c->stream->nb_streams;
2013 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
2016 st = av_mallocz(sizeof(AVStream));
2017 st->codec= avcodec_alloc_context();
2018 c->fmt_ctx.streams[i] = st;
2019 /* if file or feed, then just take streams from FFStream struct */
2020 if (!c->stream->feed ||
2021 c->stream->feed == c->stream)
2022 src = c->stream->streams[i];
2024 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2028 st->codec->frame_number = 0; /* XXX: should be done in
2029 AVStream, not in codec */
2030 /* I'm pretty sure that this is not correct...
2031 * However, without it, we crash
2033 st->codec->coded_frame = &dummy_frame;
2035 c->got_key_frame = 0;
2037 /* prepare header and save header data in a stream */
2038 if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2039 /* XXX: potential leak */
2042 c->fmt_ctx.pb.is_streamed = 1;
2044 av_set_parameters(&c->fmt_ctx, NULL);
2045 if (av_write_header(&c->fmt_ctx) < 0)
2048 len = url_close_dyn_buf(&c->fmt_ctx.pb, &c->pb_buffer);
2049 c->buffer_ptr = c->pb_buffer;
2050 c->buffer_end = c->pb_buffer + len;
2052 c->state = HTTPSTATE_SEND_DATA;
2053 c->last_packet_sent = 0;
2055 case HTTPSTATE_SEND_DATA:
2056 /* find a new packet */
2060 /* read a packet from the input stream */
2061 if (c->stream->feed) {
2062 ffm_set_write_index(c->fmt_in,
2063 c->stream->feed->feed_write_index,
2064 c->stream->feed->feed_size);
2067 if (c->stream->max_time &&
2068 c->stream->max_time + c->start_time - cur_time < 0) {
2069 /* We have timed out */
2070 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2073 if (av_read_frame(c->fmt_in, &pkt) < 0) {
2074 if (c->stream->feed && c->stream->feed->feed_opened) {
2075 /* if coming from feed, it means we reached the end of the
2076 ffm file, so must wait for more data */
2077 c->state = HTTPSTATE_WAIT_FEED;
2078 return 1; /* state changed */
2080 if (c->stream->loop) {
2081 av_close_input_file(c->fmt_in);
2083 if (open_input_stream(c, "") < 0)
2088 /* must send trailer now because eof or error */
2089 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2093 /* update first pts if needed */
2094 if (c->first_pts == AV_NOPTS_VALUE) {
2095 c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2096 c->start_time = cur_time;
2098 /* send it to the appropriate stream */
2099 if (c->stream->feed) {
2100 /* if coming from a feed, select the right stream */
2101 if (c->switch_pending) {
2102 c->switch_pending = 0;
2103 for(i=0;i<c->stream->nb_streams;i++) {
2104 if (c->switch_feed_streams[i] == pkt.stream_index) {
2105 if (pkt.flags & PKT_FLAG_KEY) {
2106 do_switch_stream(c, i);
2109 if (c->switch_feed_streams[i] >= 0) {
2110 c->switch_pending = 1;
2114 for(i=0;i<c->stream->nb_streams;i++) {
2115 if (c->feed_streams[i] == pkt.stream_index) {
2116 pkt.stream_index = i;
2117 if (pkt.flags & PKT_FLAG_KEY) {
2118 c->got_key_frame |= 1 << i;
2120 /* See if we have all the key frames, then
2121 * we start to send. This logic is not quite
2122 * right, but it works for the case of a
2123 * single video stream with one or more
2124 * audio streams (for which every frame is
2125 * typically a key frame).
2127 if (!c->stream->send_on_key ||
2128 ((c->got_key_frame + 1) >> c->stream->nb_streams)) {
2134 AVCodecContext *codec;
2137 /* specific handling for RTP: we use several
2138 output stream (one for each RTP
2139 connection). XXX: need more abstract handling */
2140 if (c->is_packetized) {
2142 /* compute send time and duration */
2143 st = c->fmt_in->streams[pkt.stream_index];
2144 c->cur_pts = av_rescale_q(pkt.dts, st->time_base, AV_TIME_BASE_Q);
2145 if (st->start_time != AV_NOPTS_VALUE)
2146 c->cur_pts -= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
2147 c->cur_frame_duration = av_rescale_q(pkt.duration, st->time_base, AV_TIME_BASE_Q);
2149 printf("index=%d pts=%0.3f duration=%0.6f\n",
2151 (double)c->cur_pts /
2153 (double)c->cur_frame_duration /
2156 /* find RTP context */
2157 c->packet_stream_index = pkt.stream_index;
2158 ctx = c->rtp_ctx[c->packet_stream_index];
2160 av_free_packet(&pkt);
2163 codec = ctx->streams[0]->codec;
2164 /* only one stream per RTP connection */
2165 pkt.stream_index = 0;
2169 codec = ctx->streams[pkt.stream_index]->codec;
2172 codec->coded_frame->key_frame = ((pkt.flags & PKT_FLAG_KEY) != 0);
2173 if (c->is_packetized) {
2174 int max_packet_size;
2175 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP)
2176 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2178 max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);
2179 ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);
2181 ret = url_open_dyn_buf(&ctx->pb);
2184 /* XXX: potential leak */
2187 if (pkt.dts != AV_NOPTS_VALUE)
2188 pkt.dts = av_rescale_q(pkt.dts,
2189 c->fmt_in->streams[pkt.stream_index]->time_base,
2190 ctx->streams[pkt.stream_index]->time_base);
2191 if (pkt.pts != AV_NOPTS_VALUE)
2192 pkt.pts = av_rescale_q(pkt.pts,
2193 c->fmt_in->streams[pkt.stream_index]->time_base,
2194 ctx->streams[pkt.stream_index]->time_base);
2195 if (av_write_frame(ctx, &pkt)) {
2196 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2199 len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer);
2200 c->cur_frame_bytes = len;
2201 c->buffer_ptr = c->pb_buffer;
2202 c->buffer_end = c->pb_buffer + len;
2204 codec->frame_number++;
2208 av_free_packet(&pkt);
2214 case HTTPSTATE_SEND_DATA_TRAILER:
2215 /* last packet test ? */
2216 if (c->last_packet_sent || c->is_packetized)
2219 /* prepare header */
2220 if (url_open_dyn_buf(&ctx->pb) < 0) {
2221 /* XXX: potential leak */
2224 av_write_trailer(ctx);
2225 len = url_close_dyn_buf(&ctx->pb, &c->pb_buffer);
2226 c->buffer_ptr = c->pb_buffer;
2227 c->buffer_end = c->pb_buffer + len;
2229 c->last_packet_sent = 1;
2235 /* should convert the format at the same time */
2236 /* send data starting at c->buffer_ptr to the output connection
2237 (either UDP or TCP connection) */
2238 static int http_send_data(HTTPContext *c)
2243 if (c->buffer_ptr >= c->buffer_end) {
2244 ret = http_prepare_data(c);
2247 else if (ret != 0) {
2248 /* state change requested */
2252 if (c->is_packetized) {
2253 /* RTP data output */
2254 len = c->buffer_end - c->buffer_ptr;
2256 /* fail safe - should never happen */
2258 c->buffer_ptr = c->buffer_end;
2261 len = (c->buffer_ptr[0] << 24) |
2262 (c->buffer_ptr[1] << 16) |
2263 (c->buffer_ptr[2] << 8) |
2265 if (len > (c->buffer_end - c->buffer_ptr))
2267 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2268 /* nothing to send yet: we can wait */
2272 c->data_count += len;
2273 update_datarate(&c->datarate, c->data_count);
2275 c->stream->bytes_served += len;
2277 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) {
2278 /* RTP packets are sent inside the RTSP TCP connection */
2279 ByteIOContext pb1, *pb = &pb1;
2280 int interleaved_index, size;
2282 HTTPContext *rtsp_c;
2285 /* if no RTSP connection left, error */
2288 /* if already sending something, then wait. */
2289 if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST) {
2292 if (url_open_dyn_buf(pb) < 0)
2294 interleaved_index = c->packet_stream_index * 2;
2295 /* RTCP packets are sent at odd indexes */
2296 if (c->buffer_ptr[1] == 200)
2297 interleaved_index++;
2298 /* write RTSP TCP header */
2300 header[1] = interleaved_index;
2301 header[2] = len >> 8;
2303 put_buffer(pb, header, 4);
2304 /* write RTP packet data */
2306 put_buffer(pb, c->buffer_ptr, len);
2307 size = url_close_dyn_buf(pb, &c->packet_buffer);
2308 /* prepare asynchronous TCP sending */
2309 rtsp_c->packet_buffer_ptr = c->packet_buffer;
2310 rtsp_c->packet_buffer_end = c->packet_buffer + size;
2311 c->buffer_ptr += len;
2313 /* send everything we can NOW */
2314 len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2315 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2317 rtsp_c->packet_buffer_ptr += len;
2319 if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2320 /* if we could not send all the data, we will
2321 send it later, so a new state is needed to
2322 "lock" the RTSP TCP connection */
2323 rtsp_c->state = RTSPSTATE_SEND_PACKET;
2326 /* all data has been sent */
2327 av_freep(&c->packet_buffer);
2330 /* send RTP packet directly in UDP */
2332 url_write(c->rtp_handles[c->packet_stream_index],
2333 c->buffer_ptr, len);
2334 c->buffer_ptr += len;
2335 /* here we continue as we can send several packets per 10 ms slot */
2338 /* TCP data output */
2339 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2341 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2342 ff_neterrno() != FF_NETERROR(EINTR)) {
2343 /* error : close connection */
2349 c->buffer_ptr += len;
2351 c->data_count += len;
2352 update_datarate(&c->datarate, c->data_count);
2354 c->stream->bytes_served += len;
2362 static int http_start_receive_data(HTTPContext *c)
2366 if (c->stream->feed_opened)
2369 /* Don't permit writing to this one */
2370 if (c->stream->readonly)
2374 fd = open(c->stream->feed_filename, O_RDWR);
2379 c->stream->feed_write_index = ffm_read_write_index(fd);
2380 c->stream->feed_size = lseek(fd, 0, SEEK_END);
2381 lseek(fd, 0, SEEK_SET);
2383 /* init buffer input */
2384 c->buffer_ptr = c->buffer;
2385 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2386 c->stream->feed_opened = 1;
2390 static int http_receive_data(HTTPContext *c)
2394 if (c->buffer_end > c->buffer_ptr) {
2397 len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2399 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2400 ff_neterrno() != FF_NETERROR(EINTR)) {
2401 /* error : close connection */
2404 } else if (len == 0) {
2405 /* end of connection : close it */
2408 c->buffer_ptr += len;
2409 c->data_count += len;
2410 update_datarate(&c->datarate, c->data_count);
2414 if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2415 if (c->buffer[0] != 'f' ||
2416 c->buffer[1] != 'm') {
2417 http_log("Feed stream has become desynchronized -- disconnecting\n");
2422 if (c->buffer_ptr >= c->buffer_end) {
2423 FFStream *feed = c->stream;
2424 /* a packet has been received : write it in the store, except
2426 if (c->data_count > FFM_PACKET_SIZE) {
2428 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2429 /* XXX: use llseek or url_seek */
2430 lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
2431 write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
2433 feed->feed_write_index += FFM_PACKET_SIZE;
2434 /* update file size */
2435 if (feed->feed_write_index > c->stream->feed_size)
2436 feed->feed_size = feed->feed_write_index;
2438 /* handle wrap around if max file size reached */
2439 if (c->stream->feed_max_size && feed->feed_write_index >= c->stream->feed_max_size)
2440 feed->feed_write_index = FFM_PACKET_SIZE;
2443 ffm_write_write_index(c->feed_fd, feed->feed_write_index);
2445 /* wake up any waiting connections */
2446 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2447 if (c1->state == HTTPSTATE_WAIT_FEED &&
2448 c1->stream->feed == c->stream->feed) {
2449 c1->state = HTTPSTATE_SEND_DATA;
2453 /* We have a header in our hands that contains useful data */
2455 AVInputFormat *fmt_in;
2456 ByteIOContext *pb = &s.pb;
2459 memset(&s, 0, sizeof(s));
2461 url_open_buf(pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
2462 pb->buf_end = c->buffer_end; /* ?? */
2463 pb->is_streamed = 1;
2465 /* use feed output format name to find corresponding input format */
2466 fmt_in = av_find_input_format(feed->fmt->name);
2470 if (fmt_in->priv_data_size > 0) {
2471 s.priv_data = av_mallocz(fmt_in->priv_data_size);
2477 if (fmt_in->read_header(&s, 0) < 0) {
2478 av_freep(&s.priv_data);
2482 /* Now we have the actual streams */
2483 if (s.nb_streams != feed->nb_streams) {
2484 av_freep(&s.priv_data);
2487 for (i = 0; i < s.nb_streams; i++) {
2488 memcpy(feed->streams[i]->codec,
2489 s.streams[i]->codec, sizeof(AVCodecContext));
2491 av_freep(&s.priv_data);
2493 c->buffer_ptr = c->buffer;
2498 c->stream->feed_opened = 0;
2503 /********************************************************************/
2506 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2513 switch(error_number) {
2514 case RTSP_STATUS_OK:
2517 case RTSP_STATUS_METHOD:
2518 str = "Method Not Allowed";
2520 case RTSP_STATUS_BANDWIDTH:
2521 str = "Not Enough Bandwidth";
2523 case RTSP_STATUS_SESSION:
2524 str = "Session Not Found";
2526 case RTSP_STATUS_STATE:
2527 str = "Method Not Valid in This State";
2529 case RTSP_STATUS_AGGREGATE:
2530 str = "Aggregate operation not allowed";
2532 case RTSP_STATUS_ONLY_AGGREGATE:
2533 str = "Only aggregate operation allowed";
2535 case RTSP_STATUS_TRANSPORT:
2536 str = "Unsupported transport";
2538 case RTSP_STATUS_INTERNAL:
2539 str = "Internal Server Error";
2541 case RTSP_STATUS_SERVICE:
2542 str = "Service Unavailable";
2544 case RTSP_STATUS_VERSION:
2545 str = "RTSP Version not supported";
2548 str = "Unknown Error";
2552 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2553 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2555 /* output GMT time */
2559 p = buf2 + strlen(p) - 1;
2562 url_fprintf(c->pb, "Date: %s GMT\r\n", buf2);
2565 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2567 rtsp_reply_header(c, error_number);
2568 url_fprintf(c->pb, "\r\n");
2571 static int rtsp_parse_request(HTTPContext *c)
2573 const char *p, *p1, *p2;
2580 RTSPHeader header1, *header = &header1;
2582 c->buffer_ptr[0] = '\0';
2585 get_word(cmd, sizeof(cmd), &p);
2586 get_word(url, sizeof(url), &p);
2587 get_word(protocol, sizeof(protocol), &p);
2589 pstrcpy(c->method, sizeof(c->method), cmd);
2590 pstrcpy(c->url, sizeof(c->url), url);
2591 pstrcpy(c->protocol, sizeof(c->protocol), protocol);
2594 if (url_open_dyn_buf(c->pb) < 0) {
2595 /* XXX: cannot do more */
2596 c->pb = NULL; /* safety */
2600 /* check version name */
2601 if (strcmp(protocol, "RTSP/1.0") != 0) {
2602 rtsp_reply_error(c, RTSP_STATUS_VERSION);
2606 /* parse each header line */
2607 memset(header, 0, sizeof(RTSPHeader));
2608 /* skip to next line */
2609 while (*p != '\n' && *p != '\0')
2613 while (*p != '\0') {
2614 p1 = strchr(p, '\n');
2618 if (p2 > p && p2[-1] == '\r')
2620 /* skip empty line */
2624 if (len > sizeof(line) - 1)
2625 len = sizeof(line) - 1;
2626 memcpy(line, p, len);
2628 rtsp_parse_line(header, line);
2632 /* handle sequence number */
2633 c->seq = header->seq;
2635 if (!strcmp(cmd, "DESCRIBE")) {
2636 rtsp_cmd_describe(c, url);
2637 } else if (!strcmp(cmd, "OPTIONS")) {
2638 rtsp_cmd_options(c, url);
2639 } else if (!strcmp(cmd, "SETUP")) {
2640 rtsp_cmd_setup(c, url, header);
2641 } else if (!strcmp(cmd, "PLAY")) {
2642 rtsp_cmd_play(c, url, header);
2643 } else if (!strcmp(cmd, "PAUSE")) {
2644 rtsp_cmd_pause(c, url, header);
2645 } else if (!strcmp(cmd, "TEARDOWN")) {
2646 rtsp_cmd_teardown(c, url, header);
2648 rtsp_reply_error(c, RTSP_STATUS_METHOD);
2651 len = url_close_dyn_buf(c->pb, &c->pb_buffer);
2652 c->pb = NULL; /* safety */
2654 /* XXX: cannot do more */
2657 c->buffer_ptr = c->pb_buffer;
2658 c->buffer_end = c->pb_buffer + len;
2659 c->state = RTSPSTATE_SEND_REPLY;
2663 /* XXX: move that to rtsp.c, but would need to replace FFStream by
2665 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
2666 struct in_addr my_ip)
2668 ByteIOContext pb1, *pb = &pb1;
2669 int i, payload_type, port, private_payload_type, j;
2670 const char *ipstr, *title, *mediatype;
2673 if (url_open_dyn_buf(pb) < 0)
2676 /* general media info */
2678 url_fprintf(pb, "v=0\n");
2679 ipstr = inet_ntoa(my_ip);
2680 url_fprintf(pb, "o=- 0 0 IN IP4 %s\n", ipstr);
2681 title = stream->title;
2682 if (title[0] == '\0')
2684 url_fprintf(pb, "s=%s\n", title);
2685 if (stream->comment[0] != '\0')
2686 url_fprintf(pb, "i=%s\n", stream->comment);
2687 if (stream->is_multicast) {
2688 url_fprintf(pb, "c=IN IP4 %s\n", inet_ntoa(stream->multicast_ip));
2690 /* for each stream, we output the necessary info */
2691 private_payload_type = RTP_PT_PRIVATE;
2692 for(i = 0; i < stream->nb_streams; i++) {
2693 st = stream->streams[i];
2694 if (st->codec->codec_id == CODEC_ID_MPEG2TS) {
2695 mediatype = "video";
2697 switch(st->codec->codec_type) {
2698 case CODEC_TYPE_AUDIO:
2699 mediatype = "audio";
2701 case CODEC_TYPE_VIDEO:
2702 mediatype = "video";
2705 mediatype = "application";
2709 /* NOTE: the port indication is not correct in case of
2710 unicast. It is not an issue because RTSP gives it */
2711 payload_type = rtp_get_payload_type(st->codec);
2712 if (payload_type < 0)
2713 payload_type = private_payload_type++;
2714 if (stream->is_multicast) {
2715 port = stream->multicast_port + 2 * i;
2719 url_fprintf(pb, "m=%s %d RTP/AVP %d\n",
2720 mediatype, port, payload_type);
2721 if (payload_type >= RTP_PT_PRIVATE) {
2722 /* for private payload type, we need to give more info */
2723 switch(st->codec->codec_id) {
2724 case CODEC_ID_MPEG4:
2727 url_fprintf(pb, "a=rtpmap:%d MP4V-ES/%d\n",
2728 payload_type, 90000);
2729 /* we must also add the mpeg4 header */
2730 data = st->codec->extradata;
2732 url_fprintf(pb, "a=fmtp:%d config=", payload_type);
2733 for(j=0;j<st->codec->extradata_size;j++) {
2734 url_fprintf(pb, "%02x", data[j]);
2736 url_fprintf(pb, "\n");
2741 /* XXX: add other codecs ? */
2745 url_fprintf(pb, "a=control:streamid=%d\n", i);
2747 return url_close_dyn_buf(pb, pbuffer);
2749 url_close_dyn_buf(pb, pbuffer);
2754 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2756 // rtsp_reply_header(c, RTSP_STATUS_OK);
2757 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2758 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2759 url_fprintf(c->pb, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2760 url_fprintf(c->pb, "\r\n");
2763 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2769 int content_length, len;
2770 struct sockaddr_in my_addr;
2772 /* find which url is asked */
2773 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2778 for(stream = first_stream; stream != NULL; stream = stream->next) {
2779 if (!stream->is_feed && stream->fmt == &rtp_muxer &&
2780 !strcmp(path, stream->filename)) {
2784 /* no stream found */
2785 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2789 /* prepare the media description in sdp format */
2791 /* get the host IP */
2792 len = sizeof(my_addr);
2793 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2794 content_length = prepare_sdp_description(stream, &content, my_addr.sin_addr);
2795 if (content_length < 0) {
2796 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2799 rtsp_reply_header(c, RTSP_STATUS_OK);
2800 url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
2801 url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
2802 url_fprintf(c->pb, "\r\n");
2803 put_buffer(c->pb, content, content_length);
2806 static HTTPContext *find_rtp_session(const char *session_id)
2810 if (session_id[0] == '\0')
2813 for(c = first_http_ctx; c != NULL; c = c->next) {
2814 if (!strcmp(c->session_id, session_id))
2820 static RTSPTransportField *find_transport(RTSPHeader *h, enum RTSPProtocol protocol)
2822 RTSPTransportField *th;
2825 for(i=0;i<h->nb_transports;i++) {
2826 th = &h->transports[i];
2827 if (th->protocol == protocol)
2833 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
2837 int stream_index, port;
2842 RTSPTransportField *th;
2843 struct sockaddr_in dest_addr;
2844 RTSPActionServerSetup setup;
2846 /* find which url is asked */
2847 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2852 /* now check each stream */
2853 for(stream = first_stream; stream != NULL; stream = stream->next) {
2854 if (!stream->is_feed && stream->fmt == &rtp_muxer) {
2855 /* accept aggregate filenames only if single stream */
2856 if (!strcmp(path, stream->filename)) {
2857 if (stream->nb_streams != 1) {
2858 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
2865 for(stream_index = 0; stream_index < stream->nb_streams;
2867 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2868 stream->filename, stream_index);
2869 if (!strcmp(path, buf))
2874 /* no stream found */
2875 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2879 /* generate session id if needed */
2880 if (h->session_id[0] == '\0') {
2881 snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
2882 av_random(&random_state), av_random(&random_state));
2885 /* find rtp session, and create it if none found */
2886 rtp_c = find_rtp_session(h->session_id);
2888 /* always prefer UDP */
2889 th = find_transport(h, RTSP_PROTOCOL_RTP_UDP);
2891 th = find_transport(h, RTSP_PROTOCOL_RTP_TCP);
2893 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2898 rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
2901 rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
2905 /* open input stream */
2906 if (open_input_stream(rtp_c, "") < 0) {
2907 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2912 /* test if stream is OK (test needed because several SETUP needs
2913 to be done for a given file) */
2914 if (rtp_c->stream != stream) {
2915 rtsp_reply_error(c, RTSP_STATUS_SERVICE);
2919 /* test if stream is already set up */
2920 if (rtp_c->rtp_ctx[stream_index]) {
2921 rtsp_reply_error(c, RTSP_STATUS_STATE);
2925 /* check transport */
2926 th = find_transport(h, rtp_c->rtp_protocol);
2927 if (!th || (th->protocol == RTSP_PROTOCOL_RTP_UDP &&
2928 th->client_port_min <= 0)) {
2929 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2933 /* setup default options */
2934 setup.transport_option[0] = '\0';
2935 dest_addr = rtp_c->from_addr;
2936 dest_addr.sin_port = htons(th->client_port_min);
2939 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2940 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2944 /* now everything is OK, so we can send the connection parameters */
2945 rtsp_reply_header(c, RTSP_STATUS_OK);
2947 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2949 switch(rtp_c->rtp_protocol) {
2950 case RTSP_PROTOCOL_RTP_UDP:
2951 port = rtp_get_local_port(rtp_c->rtp_handles[stream_index]);
2952 url_fprintf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2953 "client_port=%d-%d;server_port=%d-%d",
2954 th->client_port_min, th->client_port_min + 1,
2957 case RTSP_PROTOCOL_RTP_TCP:
2958 url_fprintf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2959 stream_index * 2, stream_index * 2 + 1);
2964 if (setup.transport_option[0] != '\0') {
2965 url_fprintf(c->pb, ";%s", setup.transport_option);
2967 url_fprintf(c->pb, "\r\n");
2970 url_fprintf(c->pb, "\r\n");
2974 /* find an rtp connection by using the session ID. Check consistency
2976 static HTTPContext *find_rtp_session_with_url(const char *url,
2977 const char *session_id)
2985 rtp_c = find_rtp_session(session_id);
2989 /* find which url is asked */
2990 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2994 if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
2995 for(s=0; s<rtp_c->stream->nb_streams; ++s) {
2996 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2997 rtp_c->stream->filename, s);
2998 if(!strncmp(path, buf, sizeof(buf))) {
2999 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
3006 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h)
3010 rtp_c = find_rtp_session_with_url(url, h->session_id);
3012 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3016 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3017 rtp_c->state != HTTPSTATE_WAIT_FEED &&
3018 rtp_c->state != HTTPSTATE_READY) {
3019 rtsp_reply_error(c, RTSP_STATUS_STATE);
3024 /* XXX: seek in stream */
3025 if (h->range_start != AV_NOPTS_VALUE) {
3026 printf("range_start=%0.3f\n", (double)h->range_start / AV_TIME_BASE);
3027 av_seek_frame(rtp_c->fmt_in, -1, h->range_start);
3031 rtp_c->state = HTTPSTATE_SEND_DATA;
3033 /* now everything is OK, so we can send the connection parameters */
3034 rtsp_reply_header(c, RTSP_STATUS_OK);
3036 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3037 url_fprintf(c->pb, "\r\n");
3040 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h)
3044 rtp_c = find_rtp_session_with_url(url, h->session_id);
3046 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3050 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3051 rtp_c->state != HTTPSTATE_WAIT_FEED) {
3052 rtsp_reply_error(c, RTSP_STATUS_STATE);
3056 rtp_c->state = HTTPSTATE_READY;
3057 rtp_c->first_pts = AV_NOPTS_VALUE;
3058 /* now everything is OK, so we can send the connection parameters */
3059 rtsp_reply_header(c, RTSP_STATUS_OK);
3061 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3062 url_fprintf(c->pb, "\r\n");
3065 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
3068 char session_id[32];
3070 rtp_c = find_rtp_session_with_url(url, h->session_id);
3072 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3076 pstrcpy(session_id, sizeof(session_id), rtp_c->session_id);
3078 /* abort the session */
3079 close_connection(rtp_c);
3081 /* now everything is OK, so we can send the connection parameters */
3082 rtsp_reply_header(c, RTSP_STATUS_OK);
3084 url_fprintf(c->pb, "Session: %s\r\n", session_id);
3085 url_fprintf(c->pb, "\r\n");
3089 /********************************************************************/
3092 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3093 FFStream *stream, const char *session_id,
3094 enum RTSPProtocol rtp_protocol)
3096 HTTPContext *c = NULL;
3097 const char *proto_str;
3099 /* XXX: should output a warning page when coming
3100 close to the connection limit */
3101 if (nb_connections >= nb_max_connections)
3104 /* add a new connection */
3105 c = av_mallocz(sizeof(HTTPContext));
3110 c->poll_entry = NULL;
3111 c->from_addr = *from_addr;
3112 c->buffer_size = IOBUFFER_INIT_SIZE;
3113 c->buffer = av_malloc(c->buffer_size);
3118 pstrcpy(c->session_id, sizeof(c->session_id), session_id);
3119 c->state = HTTPSTATE_READY;
3120 c->is_packetized = 1;
3121 c->rtp_protocol = rtp_protocol;
3123 /* protocol is shown in statistics */
3124 switch(c->rtp_protocol) {
3125 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3126 proto_str = "MCAST";
3128 case RTSP_PROTOCOL_RTP_UDP:
3131 case RTSP_PROTOCOL_RTP_TCP:
3138 pstrcpy(c->protocol, sizeof(c->protocol), "RTP/");
3139 pstrcat(c->protocol, sizeof(c->protocol), proto_str);
3141 current_bandwidth += stream->bandwidth;
3143 c->next = first_http_ctx;
3155 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3156 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3158 static int rtp_new_av_stream(HTTPContext *c,
3159 int stream_index, struct sockaddr_in *dest_addr,
3160 HTTPContext *rtsp_c)
3162 AVFormatContext *ctx;
3168 int max_packet_size;
3170 /* now we can open the relevant output stream */
3171 ctx = av_alloc_format_context();
3174 ctx->oformat = &rtp_muxer;
3176 st = av_mallocz(sizeof(AVStream));
3179 st->codec= avcodec_alloc_context();
3180 ctx->nb_streams = 1;
3181 ctx->streams[0] = st;
3183 if (!c->stream->feed ||
3184 c->stream->feed == c->stream) {
3185 memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3188 c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3191 st->priv_data = NULL;
3193 /* build destination RTP address */
3194 ipaddr = inet_ntoa(dest_addr->sin_addr);
3196 switch(c->rtp_protocol) {
3197 case RTSP_PROTOCOL_RTP_UDP:
3198 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3201 /* XXX: also pass as parameter to function ? */
3202 if (c->stream->is_multicast) {
3204 ttl = c->stream->multicast_ttl;
3207 snprintf(ctx->filename, sizeof(ctx->filename),
3208 "rtp://%s:%d?multicast=1&ttl=%d",
3209 ipaddr, ntohs(dest_addr->sin_port), ttl);
3211 snprintf(ctx->filename, sizeof(ctx->filename),
3212 "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3215 if (url_open(&h, ctx->filename, URL_WRONLY) < 0)
3217 c->rtp_handles[stream_index] = h;
3218 max_packet_size = url_get_max_packet_size(h);
3220 case RTSP_PROTOCOL_RTP_TCP:
3223 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3229 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3230 ipaddr, ntohs(dest_addr->sin_port),
3232 c->stream->filename, stream_index, c->protocol);
3234 /* normally, no packets should be output here, but the packet size may be checked */
3235 if (url_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0) {
3236 /* XXX: close stream */
3239 av_set_parameters(ctx, NULL);
3240 if (av_write_header(ctx) < 0) {
3247 url_close_dyn_buf(&ctx->pb, &dummy_buf);
3250 c->rtp_ctx[stream_index] = ctx;
3254 /********************************************************************/
3255 /* ffserver initialization */
3257 static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
3261 fst = av_mallocz(sizeof(AVStream));
3264 fst->codec= avcodec_alloc_context();
3265 fst->priv_data = av_mallocz(sizeof(FeedData));
3266 memcpy(fst->codec, codec, sizeof(AVCodecContext));
3267 fst->codec->coded_frame = &dummy_frame;
3268 fst->index = stream->nb_streams;
3269 av_set_pts_info(fst, 33, 1, 90000);
3270 stream->streams[stream->nb_streams++] = fst;
3274 /* return the stream number in the feed */
3275 static int add_av_stream(FFStream *feed, AVStream *st)
3278 AVCodecContext *av, *av1;
3282 for(i=0;i<feed->nb_streams;i++) {
3283 st = feed->streams[i];
3285 if (av1->codec_id == av->codec_id &&
3286 av1->codec_type == av->codec_type &&
3287 av1->bit_rate == av->bit_rate) {
3289 switch(av->codec_type) {
3290 case CODEC_TYPE_AUDIO:
3291 if (av1->channels == av->channels &&
3292 av1->sample_rate == av->sample_rate)
3295 case CODEC_TYPE_VIDEO:
3296 if (av1->width == av->width &&
3297 av1->height == av->height &&
3298 av1->time_base.den == av->time_base.den &&
3299 av1->time_base.num == av->time_base.num &&
3300 av1->gop_size == av->gop_size)
3309 fst = add_av_stream1(feed, av);
3312 return feed->nb_streams - 1;
3317 static void remove_stream(FFStream *stream)
3321 while (*ps != NULL) {
3322 if (*ps == stream) {
3330 /* specific mpeg4 handling : we extract the raw parameters */
3331 static void extract_mpeg4_header(AVFormatContext *infile)
3333 int mpeg4_count, i, size;
3339 for(i=0;i<infile->nb_streams;i++) {
3340 st = infile->streams[i];
3341 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3342 st->codec->extradata_size == 0) {
3349 printf("MPEG4 without extra data: trying to find header in %s\n", infile->filename);
3350 while (mpeg4_count > 0) {
3351 if (av_read_packet(infile, &pkt) < 0)
3353 st = infile->streams[pkt.stream_index];
3354 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3355 st->codec->extradata_size == 0) {
3356 av_freep(&st->codec->extradata);
3357 /* fill extradata with the header */
3358 /* XXX: we make hard suppositions here ! */
3360 while (p < pkt.data + pkt.size - 4) {
3361 /* stop when vop header is found */
3362 if (p[0] == 0x00 && p[1] == 0x00 &&
3363 p[2] == 0x01 && p[3] == 0xb6) {
3364 size = p - pkt.data;
3365 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3366 st->codec->extradata = av_malloc(size);
3367 st->codec->extradata_size = size;
3368 memcpy(st->codec->extradata, pkt.data, size);
3375 av_free_packet(&pkt);
3379 /* compute the needed AVStream for each file */
3380 static void build_file_streams(void)
3382 FFStream *stream, *stream_next;
3383 AVFormatContext *infile;
3386 /* gather all streams */
3387 for(stream = first_stream; stream != NULL; stream = stream_next) {
3388 stream_next = stream->next;
3389 if (stream->stream_type == STREAM_TYPE_LIVE &&
3391 /* the stream comes from a file */
3392 /* try to open the file */
3394 stream->ap_in = av_mallocz(sizeof(AVFormatParameters));
3395 if (stream->fmt == &rtp_muxer) {
3396 /* specific case : if transport stream output to RTP,
3397 we use a raw transport stream reader */
3398 stream->ap_in->mpeg2ts_raw = 1;
3399 stream->ap_in->mpeg2ts_compute_pcr = 1;
3402 if (av_open_input_file(&infile, stream->feed_filename,
3403 stream->ifmt, 0, stream->ap_in) < 0) {
3404 http_log("%s not found", stream->feed_filename);
3405 /* remove stream (no need to spend more time on it) */
3407 remove_stream(stream);
3409 /* find all the AVStreams inside and reference them in
3411 if (av_find_stream_info(infile) < 0) {
3412 http_log("Could not find codec parameters from '%s'",
3413 stream->feed_filename);
3414 av_close_input_file(infile);
3417 extract_mpeg4_header(infile);
3419 for(i=0;i<infile->nb_streams;i++) {
3420 add_av_stream1(stream, infile->streams[i]->codec);
3422 av_close_input_file(infile);
3428 /* compute the needed AVStream for each feed */
3429 static void build_feed_streams(void)
3431 FFStream *stream, *feed;
3434 /* gather all streams */
3435 for(stream = first_stream; stream != NULL; stream = stream->next) {
3436 feed = stream->feed;
3438 if (!stream->is_feed) {
3439 /* we handle a stream coming from a feed */
3440 for(i=0;i<stream->nb_streams;i++) {
3441 stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
3447 /* gather all streams */
3448 for(stream = first_stream; stream != NULL; stream = stream->next) {
3449 feed = stream->feed;
3451 if (stream->is_feed) {
3452 for(i=0;i<stream->nb_streams;i++) {
3453 stream->feed_streams[i] = i;
3459 /* create feed files if needed */
3460 for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
3463 if (url_exist(feed->feed_filename)) {
3464 /* See if it matches */
3468 if (av_open_input_file(&s, feed->feed_filename, NULL, FFM_PACKET_SIZE, NULL) >= 0) {
3469 /* Now see if it matches */
3470 if (s->nb_streams == feed->nb_streams) {
3472 for(i=0;i<s->nb_streams;i++) {
3474 sf = feed->streams[i];
3477 if (sf->index != ss->index ||
3479 printf("Index & Id do not match for stream %d (%s)\n",
3480 i, feed->feed_filename);
3483 AVCodecContext *ccf, *ccs;
3487 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3489 if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
3490 printf("Codecs do not match for stream %d\n", i);
3492 } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3493 printf("Codec bitrates do not match for stream %d\n", i);
3495 } else if (ccf->codec_type == CODEC_TYPE_VIDEO) {
3496 if (CHECK_CODEC(time_base.den) ||
3497 CHECK_CODEC(time_base.num) ||
3498 CHECK_CODEC(width) ||
3499 CHECK_CODEC(height)) {
3500 printf("Codec width, height and framerate do not match for stream %d\n", i);
3503 } else if (ccf->codec_type == CODEC_TYPE_AUDIO) {
3504 if (CHECK_CODEC(sample_rate) ||
3505 CHECK_CODEC(channels) ||
3506 CHECK_CODEC(frame_size)) {
3507 printf("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3511 printf("Unknown codec type\n");
3520 printf("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3521 feed->feed_filename, s->nb_streams, feed->nb_streams);
3524 av_close_input_file(s);
3526 printf("Deleting feed file '%s' as it appears to be corrupt\n",
3527 feed->feed_filename);
3530 if (feed->readonly) {
3531 printf("Unable to delete feed file '%s' as it is marked readonly\n",
3532 feed->feed_filename);
3535 unlink(feed->feed_filename);
3538 if (!url_exist(feed->feed_filename)) {
3539 AVFormatContext s1, *s = &s1;
3541 if (feed->readonly) {
3542 printf("Unable to create feed file '%s' as it is marked readonly\n",
3543 feed->feed_filename);
3547 /* only write the header of the ffm file */
3548 if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
3549 fprintf(stderr, "Could not open output feed file '%s'\n",
3550 feed->feed_filename);
3553 s->oformat = feed->fmt;
3554 s->nb_streams = feed->nb_streams;
3555 for(i=0;i<s->nb_streams;i++) {
3557 st = feed->streams[i];
3560 av_set_parameters(s, NULL);
3561 if (av_write_header(s) < 0) {
3562 fprintf(stderr, "Container doesn't supports the required parameters\n");
3565 /* XXX: need better api */
3566 av_freep(&s->priv_data);
3569 /* get feed size and write index */
3570 fd = open(feed->feed_filename, O_RDONLY);
3572 fprintf(stderr, "Could not open output feed file '%s'\n",
3573 feed->feed_filename);
3577 feed->feed_write_index = ffm_read_write_index(fd);
3578 feed->feed_size = lseek(fd, 0, SEEK_END);
3579 /* ensure that we do not wrap before the end of file */
3580 if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3581 feed->feed_max_size = feed->feed_size;
3587 /* compute the bandwidth used by each stream */
3588 static void compute_bandwidth(void)
3593 for(stream = first_stream; stream != NULL; stream = stream->next) {
3595 for(i=0;i<stream->nb_streams;i++) {
3596 AVStream *st = stream->streams[i];
3597 switch(st->codec->codec_type) {
3598 case CODEC_TYPE_AUDIO:
3599 case CODEC_TYPE_VIDEO:
3600 bandwidth += st->codec->bit_rate;
3606 stream->bandwidth = (bandwidth + 999) / 1000;
3610 static void get_arg(char *buf, int buf_size, const char **pp)
3617 while (isspace(*p)) p++;
3620 if (*p == '\"' || *p == '\'')
3632 if ((q - buf) < buf_size - 1)
3637 if (quote && *p == quote)
3642 /* add a codec and set the default parameters */
3643 static void add_codec(FFStream *stream, AVCodecContext *av)
3647 /* compute default parameters */
3648 switch(av->codec_type) {
3649 case CODEC_TYPE_AUDIO:
3650 if (av->bit_rate == 0)
3651 av->bit_rate = 64000;
3652 if (av->sample_rate == 0)
3653 av->sample_rate = 22050;
3654 if (av->channels == 0)
3657 case CODEC_TYPE_VIDEO:
3658 if (av->bit_rate == 0)
3659 av->bit_rate = 64000;
3660 if (av->time_base.num == 0){
3661 av->time_base.den = 5;
3662 av->time_base.num = 1;
3664 if (av->width == 0 || av->height == 0) {
3668 /* Bitrate tolerance is less for streaming */
3669 if (av->bit_rate_tolerance == 0)
3670 av->bit_rate_tolerance = av->bit_rate / 4;
3675 if (av->max_qdiff == 0)
3677 av->qcompress = 0.5;
3680 if (!av->nsse_weight)
3681 av->nsse_weight = 8;
3683 av->frame_skip_cmp = FF_CMP_DCTMAX;
3684 av->me_method = ME_EPZS;
3685 av->rc_buffer_aggressivity = 1.0;
3688 av->rc_eq = "tex^qComp";
3689 if (!av->i_quant_factor)
3690 av->i_quant_factor = -0.8;
3691 if (!av->b_quant_factor)
3692 av->b_quant_factor = 1.25;
3693 if (!av->b_quant_offset)
3694 av->b_quant_offset = 1.25;
3695 if (!av->rc_max_rate)
3696 av->rc_max_rate = av->bit_rate * 2;
3698 if (av->rc_max_rate && !av->rc_buffer_size) {
3699 av->rc_buffer_size = av->rc_max_rate;
3708 st = av_mallocz(sizeof(AVStream));
3711 st->codec = avcodec_alloc_context();
3712 stream->streams[stream->nb_streams++] = st;
3713 memcpy(st->codec, av, sizeof(AVCodecContext));
3716 static int opt_audio_codec(const char *arg)
3722 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
3727 return CODEC_ID_NONE;
3733 static int opt_video_codec(const char *arg)
3739 if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
3744 return CODEC_ID_NONE;
3750 /* simplistic plugin support */
3753 static void load_module(const char *filename)
3756 void (*init_func)(void);
3757 dll = dlopen(filename, RTLD_NOW);
3759 fprintf(stderr, "Could not load module '%s' - %s\n",
3760 filename, dlerror());
3764 init_func = dlsym(dll, "ffserver_module_init");
3767 "%s: init function 'ffserver_module_init()' not found\n",
3776 static int parse_ffconfig(const char *filename)
3783 int val, errors, line_num;
3784 FFStream **last_stream, *stream, *redirect;
3785 FFStream **last_feed, *feed;
3786 AVCodecContext audio_enc, video_enc;
3787 int audio_id, video_id;
3789 f = fopen(filename, "r");
3797 first_stream = NULL;
3798 last_stream = &first_stream;
3800 last_feed = &first_feed;
3804 audio_id = CODEC_ID_NONE;
3805 video_id = CODEC_ID_NONE;
3807 if (fgets(line, sizeof(line), f) == NULL)
3813 if (*p == '\0' || *p == '#')
3816 get_arg(cmd, sizeof(cmd), &p);
3818 if (!strcasecmp(cmd, "Port")) {
3819 get_arg(arg, sizeof(arg), &p);
3821 if (val < 1 || val > 65536) {
3822 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3823 filename, line_num, arg);
3826 my_http_addr.sin_port = htons(val);
3827 } else if (!strcasecmp(cmd, "BindAddress")) {
3828 get_arg(arg, sizeof(arg), &p);
3829 if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
3830 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3831 filename, line_num, arg);
3834 } else if (!strcasecmp(cmd, "NoDaemon")) {
3835 ffserver_daemon = 0;
3836 } else if (!strcasecmp(cmd, "RTSPPort")) {
3837 get_arg(arg, sizeof(arg), &p);
3839 if (val < 1 || val > 65536) {
3840 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3841 filename, line_num, arg);
3844 my_rtsp_addr.sin_port = htons(atoi(arg));
3845 } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
3846 get_arg(arg, sizeof(arg), &p);
3847 if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
3848 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3849 filename, line_num, arg);
3852 } else if (!strcasecmp(cmd, "MaxClients")) {
3853 get_arg(arg, sizeof(arg), &p);
3855 if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
3856 fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
3857 filename, line_num, arg);
3860 nb_max_connections = val;
3862 } else if (!strcasecmp(cmd, "MaxBandwidth")) {
3863 get_arg(arg, sizeof(arg), &p);
3865 if (val < 10 || val > 100000) {
3866 fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
3867 filename, line_num, arg);
3870 max_bandwidth = val;
3872 } else if (!strcasecmp(cmd, "CustomLog")) {
3873 get_arg(logfilename, sizeof(logfilename), &p);
3874 } else if (!strcasecmp(cmd, "<Feed")) {
3875 /*********************************************/
3876 /* Feed related options */
3878 if (stream || feed) {
3879 fprintf(stderr, "%s:%d: Already in a tag\n",
3880 filename, line_num);
3882 feed = av_mallocz(sizeof(FFStream));
3883 /* add in stream list */
3884 *last_stream = feed;
3885 last_stream = &feed->next;
3886 /* add in feed list */
3888 last_feed = &feed->next_feed;
3890 get_arg(feed->filename, sizeof(feed->filename), &p);
3891 q = strrchr(feed->filename, '>');
3894 feed->fmt = guess_format("ffm", NULL, NULL);
3895 /* defaut feed file */
3896 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
3897 "/tmp/%s.ffm", feed->filename);
3898 feed->feed_max_size = 5 * 1024 * 1024;
3900 feed->feed = feed; /* self feeding :-) */
3902 } else if (!strcasecmp(cmd, "Launch")) {
3906 feed->child_argv = (char **) av_mallocz(64 * sizeof(char *));
3908 for (i = 0; i < 62; i++) {
3909 get_arg(arg, sizeof(arg), &p);
3913 feed->child_argv[i] = av_strdup(arg);
3916 feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
3918 snprintf(feed->child_argv[i], 30+strlen(feed->filename),
3920 (my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
3921 inet_ntoa(my_http_addr.sin_addr),
3922 ntohs(my_http_addr.sin_port), feed->filename);
3927 fprintf(stdout, "Launch commandline: ");
3928 for (j = 0; j <= i; j++)
3929 fprintf(stdout, "%s ", feed->child_argv[j]);
3930 fprintf(stdout, "\n");
3933 } else if (!strcasecmp(cmd, "ReadOnlyFile")) {
3935 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3937 } else if (stream) {
3938 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3940 } else if (!strcasecmp(cmd, "File")) {
3942 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3943 } else if (stream) {
3944 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3946 } else if (!strcasecmp(cmd, "FileMaxSize")) {
3951 get_arg(arg, sizeof(arg), &p);
3953 fsize = strtod(p1, (char **)&p1);
3954 switch(toupper(*p1)) {
3959 fsize *= 1024 * 1024;
3962 fsize *= 1024 * 1024 * 1024;
3965 feed->feed_max_size = (int64_t)fsize;
3967 } else if (!strcasecmp(cmd, "</Feed>")) {
3969 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
3970 filename, line_num);
3974 } else if (!strcasecmp(cmd, "<Stream")) {
3975 /*********************************************/
3976 /* Stream related options */
3978 if (stream || feed) {
3979 fprintf(stderr, "%s:%d: Already in a tag\n",
3980 filename, line_num);
3982 stream = av_mallocz(sizeof(FFStream));
3983 *last_stream = stream;
3984 last_stream = &stream->next;
3986 get_arg(stream->filename, sizeof(stream->filename), &p);
3987 q = strrchr(stream->filename, '>');
3990 stream->fmt = guess_stream_format(NULL, stream->filename, NULL);
3991 memset(&audio_enc, 0, sizeof(AVCodecContext));
3992 memset(&video_enc, 0, sizeof(AVCodecContext));
3993 audio_id = CODEC_ID_NONE;
3994 video_id = CODEC_ID_NONE;
3996 audio_id = stream->fmt->audio_codec;
3997 video_id = stream->fmt->video_codec;
4000 } else if (!strcasecmp(cmd, "Feed")) {
4001 get_arg(arg, sizeof(arg), &p);
4006 while (sfeed != NULL) {
4007 if (!strcmp(sfeed->filename, arg))
4009 sfeed = sfeed->next_feed;
4012 fprintf(stderr, "%s:%d: feed '%s' not defined\n",
4013 filename, line_num, arg);
4015 stream->feed = sfeed;
4018 } else if (!strcasecmp(cmd, "Format")) {
4019 get_arg(arg, sizeof(arg), &p);
4020 if (!strcmp(arg, "status")) {
4021 stream->stream_type = STREAM_TYPE_STATUS;
4024 stream->stream_type = STREAM_TYPE_LIVE;
4025 /* jpeg cannot be used here, so use single frame jpeg */
4026 if (!strcmp(arg, "jpeg"))
4027 strcpy(arg, "mjpeg");
4028 stream->fmt = guess_stream_format(arg, NULL, NULL);
4030 fprintf(stderr, "%s:%d: Unknown Format: %s\n",
4031 filename, line_num, arg);
4036 audio_id = stream->fmt->audio_codec;
4037 video_id = stream->fmt->video_codec;
4039 } else if (!strcasecmp(cmd, "InputFormat")) {
4040 get_arg(arg, sizeof(arg), &p);
4041 stream->ifmt = av_find_input_format(arg);
4042 if (!stream->ifmt) {
4043 fprintf(stderr, "%s:%d: Unknown input format: %s\n",
4044 filename, line_num, arg);
4046 } else if (!strcasecmp(cmd, "FaviconURL")) {
4047 if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
4048 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
4050 fprintf(stderr, "%s:%d: FaviconURL only permitted for status streams\n",
4051 filename, line_num);
4054 } else if (!strcasecmp(cmd, "Author")) {
4056 get_arg(stream->author, sizeof(stream->author), &p);
4058 } else if (!strcasecmp(cmd, "Comment")) {
4060 get_arg(stream->comment, sizeof(stream->comment), &p);
4062 } else if (!strcasecmp(cmd, "Copyright")) {
4064 get_arg(stream->copyright, sizeof(stream->copyright), &p);
4066 } else if (!strcasecmp(cmd, "Title")) {
4068 get_arg(stream->title, sizeof(stream->title), &p);
4070 } else if (!strcasecmp(cmd, "Preroll")) {
4071 get_arg(arg, sizeof(arg), &p);
4073 stream->prebuffer = atof(arg) * 1000;
4075 } else if (!strcasecmp(cmd, "StartSendOnKey")) {
4077 stream->send_on_key = 1;
4079 } else if (!strcasecmp(cmd, "AudioCodec")) {
4080 get_arg(arg, sizeof(arg), &p);
4081 audio_id = opt_audio_codec(arg);
4082 if (audio_id == CODEC_ID_NONE) {
4083 fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
4084 filename, line_num, arg);
4087 } else if (!strcasecmp(cmd, "VideoCodec")) {
4088 get_arg(arg, sizeof(arg), &p);
4089 video_id = opt_video_codec(arg);
4090 if (video_id == CODEC_ID_NONE) {
4091 fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
4092 filename, line_num, arg);
4095 } else if (!strcasecmp(cmd, "MaxTime")) {
4096 get_arg(arg, sizeof(arg), &p);
4098 stream->max_time = atof(arg) * 1000;
4100 } else if (!strcasecmp(cmd, "AudioBitRate")) {
4101 get_arg(arg, sizeof(arg), &p);
4103 audio_enc.bit_rate = atoi(arg) * 1000;
4105 } else if (!strcasecmp(cmd, "AudioChannels")) {
4106 get_arg(arg, sizeof(arg), &p);
4108 audio_enc.channels = atoi(arg);
4110 } else if (!strcasecmp(cmd, "AudioSampleRate")) {
4111 get_arg(arg, sizeof(arg), &p);
4113 audio_enc.sample_rate = atoi(arg);
4115 } else if (!strcasecmp(cmd, "AudioQuality")) {
4116 get_arg(arg, sizeof(arg), &p);
4118 // audio_enc.quality = atof(arg) * 1000;
4120 } else if (!strcasecmp(cmd, "VideoBitRateRange")) {
4122 int minrate, maxrate;
4124 get_arg(arg, sizeof(arg), &p);
4126 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
4127 video_enc.rc_min_rate = minrate * 1000;
4128 video_enc.rc_max_rate = maxrate * 1000;
4130 fprintf(stderr, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4131 filename, line_num, arg);
4135 } else if (!strcasecmp(cmd, "Debug")) {
4137 get_arg(arg, sizeof(arg), &p);
4138 video_enc.debug = strtol(arg,0,0);
4140 } else if (!strcasecmp(cmd, "Strict")) {
4142 get_arg(arg, sizeof(arg), &p);
4143 video_enc.strict_std_compliance = atoi(arg);
4145 } else if (!strcasecmp(cmd, "VideoBufferSize")) {
4147 get_arg(arg, sizeof(arg), &p);
4148 video_enc.rc_buffer_size = atoi(arg) * 8*1024;
4150 } else if (!strcasecmp(cmd, "VideoBitRateTolerance")) {
4152 get_arg(arg, sizeof(arg), &p);
4153 video_enc.bit_rate_tolerance = atoi(arg) * 1000;
4155 } else if (!strcasecmp(cmd, "VideoBitRate")) {
4156 get_arg(arg, sizeof(arg), &p);
4158 video_enc.bit_rate = atoi(arg) * 1000;
4160 } else if (!strcasecmp(cmd, "VideoSize")) {
4161 get_arg(arg, sizeof(arg), &p);
4163 parse_image_size(&video_enc.width, &video_enc.height, arg);
4164 if ((video_enc.width % 16) != 0 ||
4165 (video_enc.height % 16) != 0) {
4166 fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
4167 filename, line_num);
4171 } else if (!strcasecmp(cmd, "VideoFrameRate")) {
4172 get_arg(arg, sizeof(arg), &p);
4174 video_enc.time_base.num= DEFAULT_FRAME_RATE_BASE;
4175 video_enc.time_base.den = (int)(strtod(arg, NULL) * video_enc.time_base.num);
4177 } else if (!strcasecmp(cmd, "VideoGopSize")) {
4178 get_arg(arg, sizeof(arg), &p);
4180 video_enc.gop_size = atoi(arg);
4182 } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
4184 video_enc.gop_size = 1;
4186 } else if (!strcasecmp(cmd, "VideoHighQuality")) {
4188 video_enc.mb_decision = FF_MB_DECISION_BITS;
4190 } else if (!strcasecmp(cmd, "Video4MotionVector")) {
4192 video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
4193 video_enc.flags |= CODEC_FLAG_4MV;
4195 } else if (!strcasecmp(cmd, "VideoTag")) {
4196 get_arg(arg, sizeof(arg), &p);
4197 if ((strlen(arg) == 4) && stream) {
4198 video_enc.codec_tag = ff_get_fourcc(arg);
4200 } else if (!strcasecmp(cmd, "BitExact")) {
4202 video_enc.flags |= CODEC_FLAG_BITEXACT;
4204 } else if (!strcasecmp(cmd, "DctFastint")) {
4206 video_enc.dct_algo = FF_DCT_FASTINT;
4208 } else if (!strcasecmp(cmd, "IdctSimple")) {
4210 video_enc.idct_algo = FF_IDCT_SIMPLE;
4212 } else if (!strcasecmp(cmd, "Qscale")) {
4213 get_arg(arg, sizeof(arg), &p);
4215 video_enc.flags |= CODEC_FLAG_QSCALE;
4216 video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
4218 } else if (!strcasecmp(cmd, "VideoQDiff")) {
4219 get_arg(arg, sizeof(arg), &p);
4221 video_enc.max_qdiff = atoi(arg);
4222 if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
4223 fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
4224 filename, line_num);
4228 } else if (!strcasecmp(cmd, "VideoQMax")) {
4229 get_arg(arg, sizeof(arg), &p);
4231 video_enc.qmax = atoi(arg);
4232 if (video_enc.qmax < 1 || video_enc.qmax > 31) {
4233 fprintf(stderr, "%s:%d: VideoQMax out of range\n",
4234 filename, line_num);
4238 } else if (!strcasecmp(cmd, "VideoQMin")) {
4239 get_arg(arg, sizeof(arg), &p);
4241 video_enc.qmin = atoi(arg);
4242 if (video_enc.qmin < 1 || video_enc.qmin > 31) {
4243 fprintf(stderr, "%s:%d: VideoQMin out of range\n",
4244 filename, line_num);
4248 } else if (!strcasecmp(cmd, "LumaElim")) {
4249 get_arg(arg, sizeof(arg), &p);
4251 video_enc.luma_elim_threshold = atoi(arg);
4253 } else if (!strcasecmp(cmd, "ChromaElim")) {
4254 get_arg(arg, sizeof(arg), &p);
4256 video_enc.chroma_elim_threshold = atoi(arg);
4258 } else if (!strcasecmp(cmd, "LumiMask")) {
4259 get_arg(arg, sizeof(arg), &p);
4261 video_enc.lumi_masking = atof(arg);
4263 } else if (!strcasecmp(cmd, "DarkMask")) {
4264 get_arg(arg, sizeof(arg), &p);
4266 video_enc.dark_masking = atof(arg);
4268 } else if (!strcasecmp(cmd, "NoVideo")) {
4269 video_id = CODEC_ID_NONE;
4270 } else if (!strcasecmp(cmd, "NoAudio")) {
4271 audio_id = CODEC_ID_NONE;
4272 } else if (!strcasecmp(cmd, "ACL")) {
4275 get_arg(arg, sizeof(arg), &p);
4276 if (strcasecmp(arg, "allow") == 0) {
4277 acl.action = IP_ALLOW;
4278 } else if (strcasecmp(arg, "deny") == 0) {
4279 acl.action = IP_DENY;
4281 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4282 filename, line_num, arg);
4286 get_arg(arg, sizeof(arg), &p);
4288 if (resolve_host(&acl.first, arg) != 0) {
4289 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4290 filename, line_num, arg);
4293 acl.last = acl.first;
4296 get_arg(arg, sizeof(arg), &p);
4299 if (resolve_host(&acl.last, arg) != 0) {
4300 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4301 filename, line_num, arg);
4307 IPAddressACL *nacl = (IPAddressACL *) av_mallocz(sizeof(*nacl));
4308 IPAddressACL **naclp = 0;
4314 naclp = &stream->acl;
4318 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
4319 filename, line_num);
4325 naclp = &(*naclp)->next;
4330 } else if (!strcasecmp(cmd, "RTSPOption")) {
4331 get_arg(arg, sizeof(arg), &p);
4333 av_freep(&stream->rtsp_option);
4334 stream->rtsp_option = av_strdup(arg);
4336 } else if (!strcasecmp(cmd, "MulticastAddress")) {
4337 get_arg(arg, sizeof(arg), &p);
4339 if (resolve_host(&stream->multicast_ip, arg) != 0) {
4340 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
4341 filename, line_num, arg);
4344 stream->is_multicast = 1;
4345 stream->loop = 1; /* default is looping */
4347 } else if (!strcasecmp(cmd, "MulticastPort")) {
4348 get_arg(arg, sizeof(arg), &p);
4350 stream->multicast_port = atoi(arg);
4352 } else if (!strcasecmp(cmd, "MulticastTTL")) {
4353 get_arg(arg, sizeof(arg), &p);
4355 stream->multicast_ttl = atoi(arg);
4357 } else if (!strcasecmp(cmd, "NoLoop")) {
4361 } else if (!strcasecmp(cmd, "</Stream>")) {
4363 fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
4364 filename, line_num);
4367 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
4368 if (audio_id != CODEC_ID_NONE) {
4369 audio_enc.codec_type = CODEC_TYPE_AUDIO;
4370 audio_enc.codec_id = audio_id;
4371 add_codec(stream, &audio_enc);
4373 if (video_id != CODEC_ID_NONE) {
4374 video_enc.codec_type = CODEC_TYPE_VIDEO;
4375 video_enc.codec_id = video_id;
4376 add_codec(stream, &video_enc);
4380 } else if (!strcasecmp(cmd, "<Redirect")) {
4381 /*********************************************/
4383 if (stream || feed || redirect) {
4384 fprintf(stderr, "%s:%d: Already in a tag\n",
4385 filename, line_num);
4388 redirect = av_mallocz(sizeof(FFStream));
4389 *last_stream = redirect;
4390 last_stream = &redirect->next;
4392 get_arg(redirect->filename, sizeof(redirect->filename), &p);
4393 q = strrchr(redirect->filename, '>');
4396 redirect->stream_type = STREAM_TYPE_REDIRECT;
4398 } else if (!strcasecmp(cmd, "URL")) {
4400 get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
4402 } else if (!strcasecmp(cmd, "</Redirect>")) {
4404 fprintf(stderr, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4405 filename, line_num);
4408 if (!redirect->feed_filename[0]) {
4409 fprintf(stderr, "%s:%d: No URL found for <Redirect>\n",
4410 filename, line_num);
4414 } else if (!strcasecmp(cmd, "LoadModule")) {
4415 get_arg(arg, sizeof(arg), &p);
4419 fprintf(stderr, "%s:%d: Module support not compiled into this version: '%s'\n",
4420 filename, line_num, arg);
4424 fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
4425 filename, line_num, cmd);
4437 static void show_banner(void)
4439 printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000-2006 Fabrice Bellard, et al.\n");
4442 static void show_help(void)
4445 printf("usage: ffserver [-L] [-h] [-f configfile]\n"
4446 "Hyper fast multi format Audio/Video streaming server\n"
4448 "-L : print the LICENSE\n"
4450 "-f configfile : use configfile instead of /etc/ffserver.conf\n"
4454 static void show_license(void)
4458 "FFmpeg is free software; you can redistribute it and/or\n"
4459 "modify it under the terms of the GNU Lesser General Public\n"
4460 "License as published by the Free Software Foundation; either\n"
4461 "version 2.1 of the License, or (at your option) any later version.\n"
4463 "FFmpeg is distributed in the hope that it will be useful,\n"
4464 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
4465 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
4466 "Lesser General Public License for more details.\n"
4468 "You should have received a copy of the GNU Lesser General Public\n"
4469 "License along with FFmpeg; if not, write to the Free Software\n"
4470 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
4474 static void handle_child_exit(int sig)
4479 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
4482 for (feed = first_feed; feed; feed = feed->next) {
4483 if (feed->pid == pid) {
4484 int uptime = time(0) - feed->pid_start;
4487 fprintf(stderr, "%s: Pid %d exited with status %d after %d seconds\n", feed->filename, pid, status, uptime);
4490 /* Turn off any more restarts */
4491 feed->child_argv = 0;
4497 need_to_start_children = 1;
4500 int main(int argc, char **argv)
4502 const char *config_filename;
4504 struct sigaction sigact;
4508 config_filename = "/etc/ffserver.conf";
4510 my_program_name = argv[0];
4511 my_program_dir = getcwd(0, 0);
4512 ffserver_daemon = 1;
4515 c = getopt(argc, argv, "ndLh?f:");
4531 ffserver_daemon = 0;
4534 config_filename = optarg;
4541 putenv("http_proxy"); /* Kill the http_proxy */
4543 av_init_random(av_gettime() + (getpid() << 16), &random_state);
4545 /* address on which the server will handle HTTP connections */
4546 my_http_addr.sin_family = AF_INET;
4547 my_http_addr.sin_port = htons (8080);
4548 my_http_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4550 /* address on which the server will handle RTSP connections */
4551 my_rtsp_addr.sin_family = AF_INET;
4552 my_rtsp_addr.sin_port = htons (5454);
4553 my_rtsp_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4555 nb_max_connections = 5;
4556 max_bandwidth = 1000;
4557 first_stream = NULL;
4558 logfilename[0] = '\0';
4560 memset(&sigact, 0, sizeof(sigact));
4561 sigact.sa_handler = handle_child_exit;
4562 sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
4563 sigaction(SIGCHLD, &sigact, 0);
4565 if (parse_ffconfig(config_filename) < 0) {
4566 fprintf(stderr, "Incorrect config file - exiting.\n");
4570 build_file_streams();
4572 build_feed_streams();
4574 compute_bandwidth();
4576 /* put the process in background and detach it from its TTY */
4577 if (ffserver_daemon) {
4584 } else if (pid > 0) {
4592 open("/dev/null", O_RDWR);
4593 if (strcmp(logfilename, "-") != 0) {
4603 signal(SIGPIPE, SIG_IGN);
4605 /* open log file if needed */
4606 if (logfilename[0] != '\0') {
4607 if (!strcmp(logfilename, "-"))
4610 logfile = fopen(logfilename, "w");
4613 if (http_server() < 0) {
4614 fprintf(stderr, "Could not start server\n");