2 * Multiple format streaming server
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef HAVE_CLOSESOCKET
24 #define closesocket close
28 #include "libavutil/random.h"
29 #include "libavutil/avstring.h"
30 #include "libavformat/avformat.h"
31 #include "libavformat/network.h"
32 #include "libavformat/os_support.h"
33 #include "libavformat/rtp.h"
34 #include "libavformat/rtsp.h"
35 #include "libavcodec/opt.h"
39 #include <sys/ioctl.h>
45 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
57 const char program_name[] = "FFserver";
58 const int program_birth_year = 2000;
60 static const OptionDef options[];
62 /* maximum number of simultaneous HTTP connections */
63 #define HTTP_MAX_CONNECTIONS 2000
66 HTTPSTATE_WAIT_REQUEST,
67 HTTPSTATE_SEND_HEADER,
68 HTTPSTATE_SEND_DATA_HEADER,
69 HTTPSTATE_SEND_DATA, /* sending TCP or UDP data */
70 HTTPSTATE_SEND_DATA_TRAILER,
71 HTTPSTATE_RECEIVE_DATA,
72 HTTPSTATE_WAIT_FEED, /* wait for data from the feed */
75 RTSPSTATE_WAIT_REQUEST,
77 RTSPSTATE_SEND_PACKET,
80 static const char *http_state[] = {
96 #define IOBUFFER_INIT_SIZE 8192
98 /* timeouts are in ms */
99 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
100 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
102 #define SYNC_TIMEOUT (10 * 1000)
105 int64_t count1, count2;
106 int64_t time1, time2;
109 /* context associated with one connection */
110 typedef struct HTTPContext {
111 enum HTTPState state;
112 int fd; /* socket file descriptor */
113 struct sockaddr_in from_addr; /* origin */
114 struct pollfd *poll_entry; /* used when polling */
116 uint8_t *buffer_ptr, *buffer_end;
119 struct HTTPContext *next;
120 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
124 /* input format handling */
125 AVFormatContext *fmt_in;
126 int64_t start_time; /* In milliseconds - this wraps fairly often */
127 int64_t first_pts; /* initial pts value */
128 int64_t cur_pts; /* current pts value from the stream in us */
129 int64_t cur_frame_duration; /* duration of the current frame in us */
130 int cur_frame_bytes; /* output frame size, needed to compute
131 the time at which we send each
133 int pts_stream_index; /* stream we choose as clock reference */
134 int64_t cur_clock; /* current clock reference value in us */
135 /* output format handling */
136 struct FFStream *stream;
137 /* -1 is invalid stream */
138 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
139 int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */
141 AVFormatContext fmt_ctx; /* instance of FFStream for one user */
142 int last_packet_sent; /* true if last data packet was sent */
144 DataRateData datarate;
151 int is_packetized; /* if true, the stream is packetized */
152 int packet_stream_index; /* current stream for output in state machine */
154 /* RTSP state specific */
155 uint8_t *pb_buffer; /* XXX: use that in all the code */
157 int seq; /* RTSP sequence number */
159 /* RTP state specific */
160 enum RTSPProtocol rtp_protocol;
161 char session_id[32]; /* session id */
162 AVFormatContext *rtp_ctx[MAX_STREAMS];
164 /* RTP/UDP specific */
165 URLContext *rtp_handles[MAX_STREAMS];
167 /* RTP/TCP specific */
168 struct HTTPContext *rtsp_c;
169 uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
172 /* each generated stream is described here */
176 STREAM_TYPE_REDIRECT,
179 enum IPAddressAction {
184 typedef struct IPAddressACL {
185 struct IPAddressACL *next;
186 enum IPAddressAction action;
187 /* These are in host order */
188 struct in_addr first;
192 /* description of each stream of the ffserver.conf file */
193 typedef struct FFStream {
194 enum StreamType stream_type;
195 char filename[1024]; /* stream filename */
196 struct FFStream *feed; /* feed we are using (can be null if
198 AVFormatParameters *ap_in; /* input parameters */
199 AVInputFormat *ifmt; /* if non NULL, force input format */
203 int prebuffer; /* Number of millseconds early to start */
204 int64_t max_time; /* Number of milliseconds to run */
206 AVStream *streams[MAX_STREAMS];
207 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
208 char feed_filename[1024]; /* file name of the feed storage, or
209 input file name for a stream */
214 pid_t pid; /* Of ffmpeg process */
215 time_t pid_start; /* Of ffmpeg process */
217 struct FFStream *next;
218 unsigned bandwidth; /* bandwidth, in kbits/s */
221 /* multicast specific */
223 struct in_addr multicast_ip;
224 int multicast_port; /* first port used for multicast */
226 int loop; /* if true, send the stream in loops (only meaningful if file) */
229 int feed_opened; /* true if someone is writing to the feed */
230 int is_feed; /* true if it is a feed */
231 int readonly; /* True if writing is prohibited to the file */
233 int64_t bytes_served;
234 int64_t feed_max_size; /* maximum storage size, zero means unlimited */
235 int64_t feed_write_index; /* current write position in feed (it wraps around) */
236 int64_t feed_size; /* current size of feed */
237 struct FFStream *next_feed;
240 typedef struct FeedData {
241 long long data_count;
242 float avg_frame_size; /* frame size averaged over last frames with exponential mean */
245 static struct sockaddr_in my_http_addr;
246 static struct sockaddr_in my_rtsp_addr;
248 static char logfilename[1024];
249 static HTTPContext *first_http_ctx;
250 static FFStream *first_feed; /* contains only feeds */
251 static FFStream *first_stream; /* contains all streams, including feeds */
253 static void new_connection(int server_fd, int is_rtsp);
254 static void close_connection(HTTPContext *c);
257 static int handle_connection(HTTPContext *c);
258 static int http_parse_request(HTTPContext *c);
259 static int http_send_data(HTTPContext *c);
260 static void compute_status(HTTPContext *c);
261 static int open_input_stream(HTTPContext *c, const char *info);
262 static int http_start_receive_data(HTTPContext *c);
263 static int http_receive_data(HTTPContext *c);
266 static int rtsp_parse_request(HTTPContext *c);
267 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
268 static void rtsp_cmd_options(HTTPContext *c, const char *url);
269 static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPHeader *h);
270 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h);
271 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h);
272 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h);
275 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
276 struct in_addr my_ip);
279 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
280 FFStream *stream, const char *session_id,
281 enum RTSPProtocol rtp_protocol);
282 static int rtp_new_av_stream(HTTPContext *c,
283 int stream_index, struct sockaddr_in *dest_addr,
284 HTTPContext *rtsp_c);
286 static const char *my_program_name;
287 static const char *my_program_dir;
289 static const char *config_filename;
290 static int ffserver_debug;
291 static int ffserver_daemon;
292 static int no_launch;
293 static int need_to_start_children;
295 static int nb_max_connections;
296 static int nb_connections;
298 static uint64_t max_bandwidth;
299 static uint64_t current_bandwidth;
301 static int64_t cur_time; // Making this global saves on passing it around everywhere
303 static AVRandomState random_state;
305 static FILE *logfile = NULL;
307 static void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
313 vfprintf(logfile, fmt, ap);
319 static char *ctime1(char *buf2)
327 p = buf2 + strlen(p) - 1;
333 static void log_connection(HTTPContext *c)
340 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64"\n",
341 inet_ntoa(c->from_addr.sin_addr),
342 ctime1(buf2), c->method, c->url,
343 c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
346 static void update_datarate(DataRateData *drd, int64_t count)
348 if (!drd->time1 && !drd->count1) {
349 drd->time1 = drd->time2 = cur_time;
350 drd->count1 = drd->count2 = count;
351 } else if (cur_time - drd->time2 > 5000) {
352 drd->time1 = drd->time2;
353 drd->count1 = drd->count2;
354 drd->time2 = cur_time;
359 /* In bytes per second */
360 static int compute_datarate(DataRateData *drd, int64_t count)
362 if (cur_time == drd->time1)
365 return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
369 static void start_children(FFStream *feed)
374 for (; feed; feed = feed->next) {
375 if (feed->child_argv && !feed->pid) {
376 feed->pid_start = time(0);
381 http_log("Unable to create children\n");
390 for (i = 3; i < 256; i++)
393 if (!ffserver_debug) {
394 i = open("/dev/null", O_RDWR);
403 av_strlcpy(pathname, my_program_name, sizeof(pathname));
405 slash = strrchr(pathname, '/');
410 strcpy(slash, "ffmpeg");
412 /* This is needed to make relative pathnames work */
413 chdir(my_program_dir);
415 signal(SIGPIPE, SIG_DFL);
417 execvp(pathname, feed->child_argv);
425 /* open a listening socket */
426 static int socket_open_listen(struct sockaddr_in *my_addr)
430 server_fd = socket(AF_INET,SOCK_STREAM,0);
437 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
439 if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
441 snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
443 closesocket(server_fd);
447 if (listen (server_fd, 5) < 0) {
449 closesocket(server_fd);
452 ff_socket_nonblock(server_fd, 1);
457 /* start all multicast streams */
458 static void start_multicast(void)
463 struct sockaddr_in dest_addr;
464 int default_port, stream_index;
467 for(stream = first_stream; stream != NULL; stream = stream->next) {
468 if (stream->is_multicast) {
469 /* open the RTP connection */
470 snprintf(session_id, sizeof(session_id), "%08x%08x",
471 av_random(&random_state), av_random(&random_state));
473 /* choose a port if none given */
474 if (stream->multicast_port == 0) {
475 stream->multicast_port = default_port;
479 dest_addr.sin_family = AF_INET;
480 dest_addr.sin_addr = stream->multicast_ip;
481 dest_addr.sin_port = htons(stream->multicast_port);
483 rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
484 RTSP_PROTOCOL_RTP_UDP_MULTICAST);
488 if (open_input_stream(rtp_c, "") < 0) {
489 http_log("Could not open input stream for stream '%s'\n",
494 /* open each RTP stream */
495 for(stream_index = 0; stream_index < stream->nb_streams;
497 dest_addr.sin_port = htons(stream->multicast_port +
499 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) < 0) {
500 http_log("Could not open output stream '%s/streamid=%d'\n",
501 stream->filename, stream_index);
506 /* change state to send data */
507 rtp_c->state = HTTPSTATE_SEND_DATA;
512 /* main loop of the http server */
513 static int http_server(void)
515 int server_fd, ret, rtsp_server_fd, delay, delay1;
516 struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 2], *poll_entry;
517 HTTPContext *c, *c_next;
519 server_fd = socket_open_listen(&my_http_addr);
523 rtsp_server_fd = socket_open_listen(&my_rtsp_addr);
524 if (rtsp_server_fd < 0)
527 http_log("ffserver started.\n");
529 start_children(first_feed);
531 first_http_ctx = NULL;
537 poll_entry = poll_table;
538 poll_entry->fd = server_fd;
539 poll_entry->events = POLLIN;
542 poll_entry->fd = rtsp_server_fd;
543 poll_entry->events = POLLIN;
546 /* wait for events on each HTTP handle */
553 case HTTPSTATE_SEND_HEADER:
554 case RTSPSTATE_SEND_REPLY:
555 case RTSPSTATE_SEND_PACKET:
556 c->poll_entry = poll_entry;
558 poll_entry->events = POLLOUT;
561 case HTTPSTATE_SEND_DATA_HEADER:
562 case HTTPSTATE_SEND_DATA:
563 case HTTPSTATE_SEND_DATA_TRAILER:
564 if (!c->is_packetized) {
565 /* for TCP, we output as much as we can (may need to put a limit) */
566 c->poll_entry = poll_entry;
568 poll_entry->events = POLLOUT;
571 /* when ffserver is doing the timing, we work by
572 looking at which packet need to be sent every
574 delay1 = 10; /* one tick wait XXX: 10 ms assumed */
579 case HTTPSTATE_WAIT_REQUEST:
580 case HTTPSTATE_RECEIVE_DATA:
581 case HTTPSTATE_WAIT_FEED:
582 case RTSPSTATE_WAIT_REQUEST:
583 /* need to catch errors */
584 c->poll_entry = poll_entry;
586 poll_entry->events = POLLIN;/* Maybe this will work */
590 c->poll_entry = NULL;
596 /* wait for an event on one connection. We poll at least every
597 second to handle timeouts */
599 ret = poll(poll_table, poll_entry - poll_table, delay);
600 if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
601 ff_neterrno() != FF_NETERROR(EINTR))
605 cur_time = av_gettime() / 1000;
607 if (need_to_start_children) {
608 need_to_start_children = 0;
609 start_children(first_feed);
612 /* now handle the events */
613 for(c = first_http_ctx; c != NULL; c = c_next) {
615 if (handle_connection(c) < 0) {
616 /* close and free the connection */
622 poll_entry = poll_table;
623 /* new HTTP connection request ? */
624 if (poll_entry->revents & POLLIN)
625 new_connection(server_fd, 0);
627 /* new RTSP connection request ? */
628 if (poll_entry->revents & POLLIN)
629 new_connection(rtsp_server_fd, 1);
633 /* start waiting for a new HTTP/RTSP request */
634 static void start_wait_request(HTTPContext *c, int is_rtsp)
636 c->buffer_ptr = c->buffer;
637 c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
640 c->timeout = cur_time + RTSP_REQUEST_TIMEOUT;
641 c->state = RTSPSTATE_WAIT_REQUEST;
643 c->timeout = cur_time + HTTP_REQUEST_TIMEOUT;
644 c->state = HTTPSTATE_WAIT_REQUEST;
648 static void new_connection(int server_fd, int is_rtsp)
650 struct sockaddr_in from_addr;
652 HTTPContext *c = NULL;
654 len = sizeof(from_addr);
655 fd = accept(server_fd, (struct sockaddr *)&from_addr,
658 http_log("error during accept %s\n", strerror(errno));
661 ff_socket_nonblock(fd, 1);
663 /* XXX: should output a warning page when coming
664 close to the connection limit */
665 if (nb_connections >= nb_max_connections)
668 /* add a new connection */
669 c = av_mallocz(sizeof(HTTPContext));
674 c->poll_entry = NULL;
675 c->from_addr = from_addr;
676 c->buffer_size = IOBUFFER_INIT_SIZE;
677 c->buffer = av_malloc(c->buffer_size);
681 c->next = first_http_ctx;
685 start_wait_request(c, is_rtsp);
697 static void close_connection(HTTPContext *c)
699 HTTPContext **cp, *c1;
701 AVFormatContext *ctx;
705 /* remove connection from list */
706 cp = &first_http_ctx;
707 while ((*cp) != NULL) {
715 /* remove references, if any (XXX: do it faster) */
716 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
721 /* remove connection associated resources */
725 /* close each frame parser */
726 for(i=0;i<c->fmt_in->nb_streams;i++) {
727 st = c->fmt_in->streams[i];
728 if (st->codec->codec)
729 avcodec_close(st->codec);
731 av_close_input_file(c->fmt_in);
734 /* free RTP output streams if any */
737 nb_streams = c->stream->nb_streams;
739 for(i=0;i<nb_streams;i++) {
742 av_write_trailer(ctx);
745 h = c->rtp_handles[i];
752 if (!c->last_packet_sent) {
755 if (url_open_dyn_buf(&ctx->pb) >= 0) {
756 av_write_trailer(ctx);
757 url_close_dyn_buf(ctx->pb, &c->pb_buffer);
762 for(i=0; i<ctx->nb_streams; i++)
763 av_free(ctx->streams[i]);
765 if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
766 current_bandwidth -= c->stream->bandwidth;
768 /* signal that there is no feed if we are the feeder socket */
769 if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
770 c->stream->feed_opened = 0;
774 av_freep(&c->pb_buffer);
775 av_freep(&c->packet_buffer);
781 static int handle_connection(HTTPContext *c)
786 case HTTPSTATE_WAIT_REQUEST:
787 case RTSPSTATE_WAIT_REQUEST:
789 if ((c->timeout - cur_time) < 0)
791 if (c->poll_entry->revents & (POLLERR | POLLHUP))
794 /* no need to read if no events */
795 if (!(c->poll_entry->revents & POLLIN))
799 len = recv(c->fd, c->buffer_ptr, 1, 0);
801 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
802 ff_neterrno() != FF_NETERROR(EINTR))
804 } else if (len == 0) {
807 /* search for end of request. */
809 c->buffer_ptr += len;
811 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
812 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
813 /* request found : parse it and reply */
814 if (c->state == HTTPSTATE_WAIT_REQUEST) {
815 ret = http_parse_request(c);
817 ret = rtsp_parse_request(c);
821 } else if (ptr >= c->buffer_end) {
822 /* request too long: cannot do anything */
824 } else goto read_loop;
828 case HTTPSTATE_SEND_HEADER:
829 if (c->poll_entry->revents & (POLLERR | POLLHUP))
832 /* no need to write if no events */
833 if (!(c->poll_entry->revents & POLLOUT))
835 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
837 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
838 ff_neterrno() != FF_NETERROR(EINTR)) {
839 /* error : close connection */
840 av_freep(&c->pb_buffer);
844 c->buffer_ptr += len;
846 c->stream->bytes_served += len;
847 c->data_count += len;
848 if (c->buffer_ptr >= c->buffer_end) {
849 av_freep(&c->pb_buffer);
853 /* all the buffer was sent : synchronize to the incoming stream */
854 c->state = HTTPSTATE_SEND_DATA_HEADER;
855 c->buffer_ptr = c->buffer_end = c->buffer;
860 case HTTPSTATE_SEND_DATA:
861 case HTTPSTATE_SEND_DATA_HEADER:
862 case HTTPSTATE_SEND_DATA_TRAILER:
863 /* for packetized output, we consider we can always write (the
864 input streams sets the speed). It may be better to verify
865 that we do not rely too much on the kernel queues */
866 if (!c->is_packetized) {
867 if (c->poll_entry->revents & (POLLERR | POLLHUP))
870 /* no need to read if no events */
871 if (!(c->poll_entry->revents & POLLOUT))
874 if (http_send_data(c) < 0)
876 /* close connection if trailer sent */
877 if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
880 case HTTPSTATE_RECEIVE_DATA:
881 /* no need to read if no events */
882 if (c->poll_entry->revents & (POLLERR | POLLHUP))
884 if (!(c->poll_entry->revents & POLLIN))
886 if (http_receive_data(c) < 0)
889 case HTTPSTATE_WAIT_FEED:
890 /* no need to read if no events */
891 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
894 /* nothing to do, we'll be waken up by incoming feed packets */
897 case RTSPSTATE_SEND_REPLY:
898 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
899 av_freep(&c->pb_buffer);
902 /* no need to write if no events */
903 if (!(c->poll_entry->revents & POLLOUT))
905 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
907 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
908 ff_neterrno() != FF_NETERROR(EINTR)) {
909 /* error : close connection */
910 av_freep(&c->pb_buffer);
914 c->buffer_ptr += len;
915 c->data_count += len;
916 if (c->buffer_ptr >= c->buffer_end) {
917 /* all the buffer was sent : wait for a new request */
918 av_freep(&c->pb_buffer);
919 start_wait_request(c, 1);
923 case RTSPSTATE_SEND_PACKET:
924 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
925 av_freep(&c->packet_buffer);
928 /* no need to write if no events */
929 if (!(c->poll_entry->revents & POLLOUT))
931 len = send(c->fd, c->packet_buffer_ptr,
932 c->packet_buffer_end - c->packet_buffer_ptr, 0);
934 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
935 ff_neterrno() != FF_NETERROR(EINTR)) {
936 /* error : close connection */
937 av_freep(&c->packet_buffer);
941 c->packet_buffer_ptr += len;
942 if (c->packet_buffer_ptr >= c->packet_buffer_end) {
943 /* all the buffer was sent : wait for a new request */
944 av_freep(&c->packet_buffer);
945 c->state = RTSPSTATE_WAIT_REQUEST;
949 case HTTPSTATE_READY:
958 static int extract_rates(char *rates, int ratelen, const char *request)
962 for (p = request; *p && *p != '\r' && *p != '\n'; ) {
963 if (strncasecmp(p, "Pragma:", 7) == 0) {
964 const char *q = p + 7;
966 while (*q && *q != '\n' && isspace(*q))
969 if (strncasecmp(q, "stream-switch-entry=", 20) == 0) {
975 memset(rates, 0xff, ratelen);
978 while (*q && *q != '\n' && *q != ':')
981 if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
985 if (stream_no < ratelen && stream_no >= 0)
986 rates[stream_no] = rate_no;
988 while (*q && *q != '\n' && !isspace(*q))
1005 static int find_stream_in_feed(FFStream *feed, AVCodecContext *codec, int bit_rate)
1008 int best_bitrate = 100000000;
1011 for (i = 0; i < feed->nb_streams; i++) {
1012 AVCodecContext *feed_codec = feed->streams[i]->codec;
1014 if (feed_codec->codec_id != codec->codec_id ||
1015 feed_codec->sample_rate != codec->sample_rate ||
1016 feed_codec->width != codec->width ||
1017 feed_codec->height != codec->height)
1020 /* Potential stream */
1022 /* We want the fastest stream less than bit_rate, or the slowest
1023 * faster than bit_rate
1026 if (feed_codec->bit_rate <= bit_rate) {
1027 if (best_bitrate > bit_rate || feed_codec->bit_rate > best_bitrate) {
1028 best_bitrate = feed_codec->bit_rate;
1032 if (feed_codec->bit_rate < best_bitrate) {
1033 best_bitrate = feed_codec->bit_rate;
1042 static int modify_current_stream(HTTPContext *c, char *rates)
1045 FFStream *req = c->stream;
1046 int action_required = 0;
1048 /* Not much we can do for a feed */
1052 for (i = 0; i < req->nb_streams; i++) {
1053 AVCodecContext *codec = req->streams[i]->codec;
1057 c->switch_feed_streams[i] = req->feed_streams[i];
1060 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1063 /* Wants off or slow */
1064 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1066 /* This doesn't work well when it turns off the only stream! */
1067 c->switch_feed_streams[i] = -2;
1068 c->feed_streams[i] = -2;
1073 if (c->switch_feed_streams[i] >= 0 && c->switch_feed_streams[i] != c->feed_streams[i])
1074 action_required = 1;
1077 return action_required;
1081 static void do_switch_stream(HTTPContext *c, int i)
1083 if (c->switch_feed_streams[i] >= 0) {
1085 c->feed_streams[i] = c->switch_feed_streams[i];
1088 /* Now update the stream */
1090 c->switch_feed_streams[i] = -1;
1093 /* XXX: factorize in utils.c ? */
1094 /* XXX: take care with different space meaning */
1095 static void skip_spaces(const char **pp)
1099 while (*p == ' ' || *p == '\t')
1104 static void get_word(char *buf, int buf_size, const char **pp)
1112 while (!isspace(*p) && *p != '\0') {
1113 if ((q - buf) < buf_size - 1)
1122 static int validate_acl(FFStream *stream, HTTPContext *c)
1124 enum IPAddressAction last_action = IP_DENY;
1126 struct in_addr *src = &c->from_addr.sin_addr;
1127 unsigned long src_addr = src->s_addr;
1129 for (acl = stream->acl; acl; acl = acl->next) {
1130 if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
1131 return (acl->action == IP_ALLOW) ? 1 : 0;
1132 last_action = acl->action;
1135 /* Nothing matched, so return not the last action */
1136 return (last_action == IP_DENY) ? 1 : 0;
1139 /* compute the real filename of a file by matching it without its
1140 extensions to all the stream filenames */
1141 static void compute_real_filename(char *filename, int max_size)
1148 /* compute filename by matching without the file extensions */
1149 av_strlcpy(file1, filename, sizeof(file1));
1150 p = strrchr(file1, '.');
1153 for(stream = first_stream; stream != NULL; stream = stream->next) {
1154 av_strlcpy(file2, stream->filename, sizeof(file2));
1155 p = strrchr(file2, '.');
1158 if (!strcmp(file1, file2)) {
1159 av_strlcpy(filename, stream->filename, max_size);
1174 /* parse http request and prepare header */
1175 static int http_parse_request(HTTPContext *c)
1178 enum RedirType redir_type;
1180 char info[1024], filename[1024];
1184 const char *mime_type;
1188 char *useragent = 0;
1191 get_word(cmd, sizeof(cmd), (const char **)&p);
1192 av_strlcpy(c->method, cmd, sizeof(c->method));
1194 if (!strcmp(cmd, "GET"))
1196 else if (!strcmp(cmd, "POST"))
1201 get_word(url, sizeof(url), (const char **)&p);
1202 av_strlcpy(c->url, url, sizeof(c->url));
1204 get_word(protocol, sizeof(protocol), (const char **)&p);
1205 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1208 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
1211 http_log("New connection: %s %s\n", cmd, url);
1213 /* find the filename and the optional info string in the request */
1214 p = strchr(url, '?');
1216 av_strlcpy(info, p, sizeof(info));
1221 av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
1223 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1224 if (strncasecmp(p, "User-Agent:", 11) == 0) {
1226 if (*useragent && *useragent != '\n' && isspace(*useragent))
1230 p = strchr(p, '\n');
1237 redir_type = REDIR_NONE;
1238 if (match_ext(filename, "asx")) {
1239 redir_type = REDIR_ASX;
1240 filename[strlen(filename)-1] = 'f';
1241 } else if (match_ext(filename, "asf") &&
1242 (!useragent || strncasecmp(useragent, "NSPlayer", 8) != 0)) {
1243 /* if this isn't WMP or lookalike, return the redirector file */
1244 redir_type = REDIR_ASF;
1245 } else if (match_ext(filename, "rpm,ram")) {
1246 redir_type = REDIR_RAM;
1247 strcpy(filename + strlen(filename)-2, "m");
1248 } else if (match_ext(filename, "rtsp")) {
1249 redir_type = REDIR_RTSP;
1250 compute_real_filename(filename, sizeof(filename) - 1);
1251 } else if (match_ext(filename, "sdp")) {
1252 redir_type = REDIR_SDP;
1253 compute_real_filename(filename, sizeof(filename) - 1);
1256 // "redirect" / request to index.html
1257 if (!strlen(filename))
1258 av_strlcpy(filename, "index.html", sizeof(filename) - 1);
1260 stream = first_stream;
1261 while (stream != NULL) {
1262 if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
1264 stream = stream->next;
1266 if (stream == NULL) {
1267 snprintf(msg, sizeof(msg), "File '%s' not found", url);
1272 memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1273 memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1275 if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1276 c->http_error = 301;
1278 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 301 Moved\r\n");
1279 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Location: %s\r\n", stream->feed_filename);
1280 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1281 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1282 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Moved</title></head><body>\r\n");
1283 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "You should be <a href=\"%s\">redirected</a>.\r\n", stream->feed_filename);
1284 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
1286 /* prepare output buffer */
1287 c->buffer_ptr = c->buffer;
1289 c->state = HTTPSTATE_SEND_HEADER;
1293 /* If this is WMP, get the rate information */
1294 if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1295 if (modify_current_stream(c, ratebuf)) {
1296 for (i = 0; i < sizeof(c->feed_streams) / sizeof(c->feed_streams[0]); i++) {
1297 if (c->switch_feed_streams[i] >= 0)
1298 do_switch_stream(c, i);
1303 /* If already streaming this feed, do not let start another feeder. */
1304 if (stream->feed_opened) {
1305 snprintf(msg, sizeof(msg), "This feed is already being received.");
1309 if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
1310 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 %lldkbit/sec, and this exceeds the limit of %lldkbit/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 av_strlcpy(hostname, hostbuf, sizeof(hostname));
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);
1471 p = strchr(p, '\n');
1479 char *eol = strchr(logline, '\n');
1484 if (eol[-1] == '\r')
1486 http_log("%.*s\n", (int) (eol - logline), logline);
1487 c->suppress_log = 1;
1492 http_log("\nGot request:\n%s\n", c->buffer);
1495 if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1498 /* Now we have to find the client_id */
1499 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1500 if (wmpc->wmp_client_id == client_id)
1504 if (wmpc && modify_current_stream(wmpc, ratebuf))
1505 wmpc->switch_pending = 1;
1508 snprintf(msg, sizeof(msg), "POST command not handled");
1512 if (http_start_receive_data(c) < 0) {
1513 snprintf(msg, sizeof(msg), "could not open feed");
1517 c->state = HTTPSTATE_RECEIVE_DATA;
1522 if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
1523 http_log("\nGot request:\n%s\n", c->buffer);
1526 if (c->stream->stream_type == STREAM_TYPE_STATUS)
1529 /* open input stream */
1530 if (open_input_stream(c, info) < 0) {
1531 snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1535 /* prepare http header */
1537 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1538 mime_type = c->stream->fmt->mime_type;
1540 mime_type = "application/x-octet-stream";
1541 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
1543 /* for asf, we need extra headers */
1544 if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1545 /* Need to allocate a client id */
1547 c->wmp_client_id = av_random(&random_state) & 0x7fffffff;
1549 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);
1551 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-Type: %s\r\n", mime_type);
1552 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1554 /* prepare output buffer */
1556 c->buffer_ptr = c->buffer;
1558 c->state = HTTPSTATE_SEND_HEADER;
1561 c->http_error = 404;
1563 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 404 Not Found\r\n");
1564 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: %s\r\n", "text/html");
1565 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1566 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HTML>\n");
1567 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1568 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<BODY>%s</BODY>\n", msg);
1569 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</HTML>\n");
1571 /* prepare output buffer */
1572 c->buffer_ptr = c->buffer;
1574 c->state = HTTPSTATE_SEND_HEADER;
1578 c->http_error = 200; /* horrible : we use this value to avoid
1579 going to the send data state */
1580 c->state = HTTPSTATE_SEND_HEADER;
1584 static void fmt_bytecount(ByteIOContext *pb, int64_t count)
1586 static const char *suffix = " kMGTP";
1589 for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
1591 url_fprintf(pb, "%"PRId64"%c", count, *s);
1594 static void compute_status(HTTPContext *c)
1603 if (url_open_dyn_buf(&pb) < 0) {
1604 /* XXX: return an error ? */
1605 c->buffer_ptr = c->buffer;
1606 c->buffer_end = c->buffer;
1610 url_fprintf(pb, "HTTP/1.0 200 OK\r\n");
1611 url_fprintf(pb, "Content-type: %s\r\n", "text/html");
1612 url_fprintf(pb, "Pragma: no-cache\r\n");
1613 url_fprintf(pb, "\r\n");
1615 url_fprintf(pb, "<HEAD><TITLE>%s Status</TITLE>\n", program_name);
1616 if (c->stream->feed_filename)
1617 url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
1618 url_fprintf(pb, "</HEAD>\n<BODY>");
1619 url_fprintf(pb, "<H1>%s Status</H1>\n", program_name);
1621 url_fprintf(pb, "<H2>Available Streams</H2>\n");
1622 url_fprintf(pb, "<TABLE cellspacing=0 cellpadding=4>\n");
1623 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");
1624 stream = first_stream;
1625 while (stream != NULL) {
1626 char sfilename[1024];
1629 if (stream->feed != stream) {
1630 av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
1631 eosf = sfilename + strlen(sfilename);
1632 if (eosf - sfilename >= 4) {
1633 if (strcmp(eosf - 4, ".asf") == 0)
1634 strcpy(eosf - 4, ".asx");
1635 else if (strcmp(eosf - 3, ".rm") == 0)
1636 strcpy(eosf - 3, ".ram");
1637 else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
1638 /* generate a sample RTSP director if
1639 unicast. Generate an SDP redirector if
1641 eosf = strrchr(sfilename, '.');
1643 eosf = sfilename + strlen(sfilename);
1644 if (stream->is_multicast)
1645 strcpy(eosf, ".sdp");
1647 strcpy(eosf, ".rtsp");
1651 url_fprintf(pb, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1652 sfilename, stream->filename);
1653 url_fprintf(pb, "<td align=right> %d <td align=right> ",
1654 stream->conns_served);
1655 fmt_bytecount(pb, stream->bytes_served);
1656 switch(stream->stream_type) {
1657 case STREAM_TYPE_LIVE:
1659 int audio_bit_rate = 0;
1660 int video_bit_rate = 0;
1661 const char *audio_codec_name = "";
1662 const char *video_codec_name = "";
1663 const char *audio_codec_name_extra = "";
1664 const char *video_codec_name_extra = "";
1666 for(i=0;i<stream->nb_streams;i++) {
1667 AVStream *st = stream->streams[i];
1668 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1669 switch(st->codec->codec_type) {
1670 case CODEC_TYPE_AUDIO:
1671 audio_bit_rate += st->codec->bit_rate;
1673 if (*audio_codec_name)
1674 audio_codec_name_extra = "...";
1675 audio_codec_name = codec->name;
1678 case CODEC_TYPE_VIDEO:
1679 video_bit_rate += st->codec->bit_rate;
1681 if (*video_codec_name)
1682 video_codec_name_extra = "...";
1683 video_codec_name = codec->name;
1686 case CODEC_TYPE_DATA:
1687 video_bit_rate += st->codec->bit_rate;
1693 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",
1696 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
1697 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
1699 url_fprintf(pb, "<TD>%s", stream->feed->filename);
1701 url_fprintf(pb, "<TD>%s", stream->feed_filename);
1702 url_fprintf(pb, "\n");
1706 url_fprintf(pb, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1710 stream = stream->next;
1712 url_fprintf(pb, "</TABLE>\n");
1714 stream = first_stream;
1715 while (stream != NULL) {
1716 if (stream->feed == stream) {
1717 url_fprintf(pb, "<h2>Feed %s</h2>", stream->filename);
1719 url_fprintf(pb, "Running as pid %d.\n", stream->pid);
1721 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1726 /* This is somewhat linux specific I guess */
1727 snprintf(ps_cmd, sizeof(ps_cmd),
1728 "ps -o \"%%cpu,cputime\" --no-headers %d",
1731 pid_stat = popen(ps_cmd, "r");
1736 if (fscanf(pid_stat, "%10s %64s", cpuperc,
1738 url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",
1746 url_fprintf(pb, "<p>");
1748 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");
1750 for (i = 0; i < stream->nb_streams; i++) {
1751 AVStream *st = stream->streams[i];
1752 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1753 const char *type = "unknown";
1754 char parameters[64];
1758 switch(st->codec->codec_type) {
1759 case CODEC_TYPE_AUDIO:
1761 snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
1763 case CODEC_TYPE_VIDEO:
1765 snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,
1766 st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);
1771 url_fprintf(pb, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1772 i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);
1774 url_fprintf(pb, "</table>\n");
1777 stream = stream->next;
1783 AVCodecContext *enc;
1787 stream = first_feed;
1788 while (stream != NULL) {
1789 url_fprintf(pb, "<H1>Feed '%s'</H1>\n", stream->filename);
1790 url_fprintf(pb, "<TABLE>\n");
1791 url_fprintf(pb, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1792 for(i=0;i<stream->nb_streams;i++) {
1793 AVStream *st = stream->streams[i];
1794 FeedData *fdata = st->priv_data;
1797 avcodec_string(buf, sizeof(buf), enc);
1798 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
1799 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
1800 avg /= enc->frame_size;
1801 url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
1802 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
1804 url_fprintf(pb, "</TABLE>\n");
1805 stream = stream->next_feed;
1810 /* connection status */
1811 url_fprintf(pb, "<H2>Connection Status</H2>\n");
1813 url_fprintf(pb, "Number of connections: %d / %d<BR>\n",
1814 nb_connections, nb_max_connections);
1816 url_fprintf(pb, "Bandwidth in use: %lldk / %lldk<BR>\n",
1817 current_bandwidth, max_bandwidth);
1819 url_fprintf(pb, "<TABLE>\n");
1820 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");
1821 c1 = first_http_ctx;
1823 while (c1 != NULL) {
1829 for (j = 0; j < c1->stream->nb_streams; j++) {
1830 if (!c1->stream->feed)
1831 bitrate += c1->stream->streams[j]->codec->bit_rate;
1832 else if (c1->feed_streams[j] >= 0)
1833 bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
1838 p = inet_ntoa(c1->from_addr.sin_addr);
1839 url_fprintf(pb, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1841 c1->stream ? c1->stream->filename : "",
1842 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
1845 http_state[c1->state]);
1846 fmt_bytecount(pb, bitrate);
1847 url_fprintf(pb, "<td align=right>");
1848 fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
1849 url_fprintf(pb, "<td align=right>");
1850 fmt_bytecount(pb, c1->data_count);
1851 url_fprintf(pb, "\n");
1854 url_fprintf(pb, "</TABLE>\n");
1859 url_fprintf(pb, "<HR size=1 noshade>Generated at %s", p);
1860 url_fprintf(pb, "</BODY>\n</HTML>\n");
1862 len = url_close_dyn_buf(pb, &c->pb_buffer);
1863 c->buffer_ptr = c->pb_buffer;
1864 c->buffer_end = c->pb_buffer + len;
1867 /* check if the parser needs to be opened for stream i */
1868 static void open_parser(AVFormatContext *s, int i)
1870 AVStream *st = s->streams[i];
1873 if (!st->codec->codec) {
1874 codec = avcodec_find_decoder(st->codec->codec_id);
1875 if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
1876 st->codec->parse_only = 1;
1877 if (avcodec_open(st->codec, codec) < 0)
1878 st->codec->parse_only = 0;
1883 static int open_input_stream(HTTPContext *c, const char *info)
1886 char input_filename[1024];
1888 int buf_size, i, ret;
1891 /* find file name */
1892 if (c->stream->feed) {
1893 strcpy(input_filename, c->stream->feed->feed_filename);
1894 buf_size = FFM_PACKET_SIZE;
1895 /* compute position (absolute time) */
1896 if (find_info_tag(buf, sizeof(buf), "date", info))
1898 stream_pos = parse_date(buf, 0);
1899 if (stream_pos == INT64_MIN)
1902 else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
1903 int prebuffer = strtol(buf, 0, 10);
1904 stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
1906 stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
1908 strcpy(input_filename, c->stream->feed_filename);
1910 /* compute position (relative time) */
1911 if (find_info_tag(buf, sizeof(buf), "date", info))
1913 stream_pos = parse_date(buf, 1);
1914 if (stream_pos == INT64_MIN)
1920 if (input_filename[0] == '\0')
1924 { time_t when = stream_pos / 1000000;
1925 http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
1930 if ((ret = av_open_input_file(&s, input_filename, c->stream->ifmt,
1931 buf_size, c->stream->ap_in)) < 0) {
1932 http_log("could not open %s: %d\n", input_filename, ret);
1935 s->flags |= AVFMT_FLAG_GENPTS;
1937 av_find_stream_info(c->fmt_in);
1939 /* open each parser */
1940 for(i=0;i<s->nb_streams;i++)
1943 /* choose stream as clock source (we favorize video stream if
1944 present) for packet sending */
1945 c->pts_stream_index = 0;
1946 for(i=0;i<c->stream->nb_streams;i++) {
1947 if (c->pts_stream_index == 0 &&
1948 c->stream->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
1949 c->pts_stream_index = i;
1954 if (c->fmt_in->iformat->read_seek)
1955 c->fmt_in->iformat->read_seek(c->fmt_in, 0, stream_pos, 0);
1957 /* set the start time (needed for maxtime and RTP packet timing) */
1958 c->start_time = cur_time;
1959 c->first_pts = AV_NOPTS_VALUE;
1963 /* return the server clock (in us) */
1964 static int64_t get_server_clock(HTTPContext *c)
1966 /* compute current pts value from system time */
1967 return (cur_time - c->start_time) * 1000;
1970 /* return the estimated time at which the current packet must be sent
1972 static int64_t get_packet_send_clock(HTTPContext *c)
1974 int bytes_left, bytes_sent, frame_bytes;
1976 frame_bytes = c->cur_frame_bytes;
1977 if (frame_bytes <= 0)
1980 bytes_left = c->buffer_end - c->buffer_ptr;
1981 bytes_sent = frame_bytes - bytes_left;
1982 return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
1987 static int http_prepare_data(HTTPContext *c)
1990 AVFormatContext *ctx;
1992 av_freep(&c->pb_buffer);
1994 case HTTPSTATE_SEND_DATA_HEADER:
1995 memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
1996 av_strlcpy(c->fmt_ctx.author, c->stream->author,
1997 sizeof(c->fmt_ctx.author));
1998 av_strlcpy(c->fmt_ctx.comment, c->stream->comment,
1999 sizeof(c->fmt_ctx.comment));
2000 av_strlcpy(c->fmt_ctx.copyright, c->stream->copyright,
2001 sizeof(c->fmt_ctx.copyright));
2002 av_strlcpy(c->fmt_ctx.title, c->stream->title,
2003 sizeof(c->fmt_ctx.title));
2005 /* open output stream by using specified codecs */
2006 c->fmt_ctx.oformat = c->stream->fmt;
2007 c->fmt_ctx.nb_streams = c->stream->nb_streams;
2008 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
2011 st = av_mallocz(sizeof(AVStream));
2012 st->codec= avcodec_alloc_context();
2013 c->fmt_ctx.streams[i] = st;
2014 /* if file or feed, then just take streams from FFStream struct */
2015 if (!c->stream->feed ||
2016 c->stream->feed == c->stream)
2017 src = c->stream->streams[i];
2019 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2023 st->codec->frame_number = 0; /* XXX: should be done in
2024 AVStream, not in codec */
2026 c->got_key_frame = 0;
2028 /* prepare header and save header data in a stream */
2029 if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2030 /* XXX: potential leak */
2033 c->fmt_ctx.pb->is_streamed = 1;
2035 av_set_parameters(&c->fmt_ctx, NULL);
2036 if (av_write_header(&c->fmt_ctx) < 0) {
2037 http_log("Error writing output header\n");
2041 len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2042 c->buffer_ptr = c->pb_buffer;
2043 c->buffer_end = c->pb_buffer + len;
2045 c->state = HTTPSTATE_SEND_DATA;
2046 c->last_packet_sent = 0;
2048 case HTTPSTATE_SEND_DATA:
2049 /* find a new packet */
2050 /* read a packet from the input stream */
2051 if (c->stream->feed)
2052 ffm_set_write_index(c->fmt_in,
2053 c->stream->feed->feed_write_index,
2054 c->stream->feed->feed_size);
2056 if (c->stream->max_time &&
2057 c->stream->max_time + c->start_time - cur_time < 0)
2058 /* We have timed out */
2059 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2063 if (av_read_frame(c->fmt_in, &pkt) < 0) {
2064 if (c->stream->feed && c->stream->feed->feed_opened) {
2065 /* if coming from feed, it means we reached the end of the
2066 ffm file, so must wait for more data */
2067 c->state = HTTPSTATE_WAIT_FEED;
2068 return 1; /* state changed */
2070 if (c->stream->loop) {
2071 av_close_input_file(c->fmt_in);
2073 if (open_input_stream(c, "") < 0)
2078 /* must send trailer now because eof or error */
2079 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2083 int source_index = pkt.stream_index;
2084 /* update first pts if needed */
2085 if (c->first_pts == AV_NOPTS_VALUE) {
2086 c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2087 c->start_time = cur_time;
2089 /* send it to the appropriate stream */
2090 if (c->stream->feed) {
2091 /* if coming from a feed, select the right stream */
2092 if (c->switch_pending) {
2093 c->switch_pending = 0;
2094 for(i=0;i<c->stream->nb_streams;i++) {
2095 if (c->switch_feed_streams[i] == pkt.stream_index)
2096 if (pkt.flags & PKT_FLAG_KEY)
2097 do_switch_stream(c, i);
2098 if (c->switch_feed_streams[i] >= 0)
2099 c->switch_pending = 1;
2102 for(i=0;i<c->stream->nb_streams;i++) {
2103 if (c->feed_streams[i] == pkt.stream_index) {
2104 AVStream *st = c->fmt_in->streams[source_index];
2105 pkt.stream_index = i;
2106 if (pkt.flags & PKT_FLAG_KEY &&
2107 (st->codec->codec_type == CODEC_TYPE_VIDEO ||
2108 c->stream->nb_streams == 1))
2109 c->got_key_frame = 1;
2110 if (!c->stream->send_on_key || c->got_key_frame)
2115 AVCodecContext *codec;
2118 /* specific handling for RTP: we use several
2119 output stream (one for each RTP
2120 connection). XXX: need more abstract handling */
2121 if (c->is_packetized) {
2123 /* compute send time and duration */
2124 st = c->fmt_in->streams[pkt.stream_index];
2125 c->cur_pts = av_rescale_q(pkt.dts, st->time_base, AV_TIME_BASE_Q);
2126 if (st->start_time != AV_NOPTS_VALUE)
2127 c->cur_pts -= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
2128 c->cur_frame_duration = av_rescale_q(pkt.duration, st->time_base, AV_TIME_BASE_Q);
2130 printf("index=%d pts=%0.3f duration=%0.6f\n",
2132 (double)c->cur_pts /
2134 (double)c->cur_frame_duration /
2137 /* find RTP context */
2138 c->packet_stream_index = pkt.stream_index;
2139 ctx = c->rtp_ctx[c->packet_stream_index];
2141 av_free_packet(&pkt);
2144 codec = ctx->streams[0]->codec;
2145 /* only one stream per RTP connection */
2146 pkt.stream_index = 0;
2150 codec = ctx->streams[pkt.stream_index]->codec;
2153 if (c->is_packetized) {
2154 int max_packet_size;
2155 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP)
2156 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2158 max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);
2159 ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);
2161 ret = url_open_dyn_buf(&ctx->pb);
2164 /* XXX: potential leak */
2167 c->fmt_ctx.pb->is_streamed = 1;
2168 if (pkt.dts != AV_NOPTS_VALUE)
2169 pkt.dts = av_rescale_q(pkt.dts,
2170 c->fmt_in->streams[source_index]->time_base,
2171 ctx->streams[pkt.stream_index]->time_base);
2172 if (pkt.pts != AV_NOPTS_VALUE)
2173 pkt.pts = av_rescale_q(pkt.pts,
2174 c->fmt_in->streams[source_index]->time_base,
2175 ctx->streams[pkt.stream_index]->time_base);
2176 pkt.duration = av_rescale_q(pkt.duration,
2177 c->fmt_in->streams[source_index]->time_base,
2178 ctx->streams[pkt.stream_index]->time_base);
2179 if (av_write_frame(ctx, &pkt) < 0) {
2180 http_log("Error writing frame to output\n");
2181 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2184 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2185 c->cur_frame_bytes = len;
2186 c->buffer_ptr = c->pb_buffer;
2187 c->buffer_end = c->pb_buffer + len;
2189 codec->frame_number++;
2191 av_free_packet(&pkt);
2195 av_free_packet(&pkt);
2200 case HTTPSTATE_SEND_DATA_TRAILER:
2201 /* last packet test ? */
2202 if (c->last_packet_sent || c->is_packetized)
2205 /* prepare header */
2206 if (url_open_dyn_buf(&ctx->pb) < 0) {
2207 /* XXX: potential leak */
2210 c->fmt_ctx.pb->is_streamed = 1;
2211 av_write_trailer(ctx);
2212 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2213 c->buffer_ptr = c->pb_buffer;
2214 c->buffer_end = c->pb_buffer + len;
2216 c->last_packet_sent = 1;
2222 /* should convert the format at the same time */
2223 /* send data starting at c->buffer_ptr to the output connection
2224 (either UDP or TCP connection) */
2225 static int http_send_data(HTTPContext *c)
2230 if (c->buffer_ptr >= c->buffer_end) {
2231 ret = http_prepare_data(c);
2235 /* state change requested */
2238 if (c->is_packetized) {
2239 /* RTP data output */
2240 len = c->buffer_end - c->buffer_ptr;
2242 /* fail safe - should never happen */
2244 c->buffer_ptr = c->buffer_end;
2247 len = (c->buffer_ptr[0] << 24) |
2248 (c->buffer_ptr[1] << 16) |
2249 (c->buffer_ptr[2] << 8) |
2251 if (len > (c->buffer_end - c->buffer_ptr))
2253 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2254 /* nothing to send yet: we can wait */
2258 c->data_count += len;
2259 update_datarate(&c->datarate, c->data_count);
2261 c->stream->bytes_served += len;
2263 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) {
2264 /* RTP packets are sent inside the RTSP TCP connection */
2266 int interleaved_index, size;
2268 HTTPContext *rtsp_c;
2271 /* if no RTSP connection left, error */
2274 /* if already sending something, then wait. */
2275 if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
2277 if (url_open_dyn_buf(&pb) < 0)
2279 interleaved_index = c->packet_stream_index * 2;
2280 /* RTCP packets are sent at odd indexes */
2281 if (c->buffer_ptr[1] == 200)
2282 interleaved_index++;
2283 /* write RTSP TCP header */
2285 header[1] = interleaved_index;
2286 header[2] = len >> 8;
2288 put_buffer(pb, header, 4);
2289 /* write RTP packet data */
2291 put_buffer(pb, c->buffer_ptr, len);
2292 size = url_close_dyn_buf(pb, &c->packet_buffer);
2293 /* prepare asynchronous TCP sending */
2294 rtsp_c->packet_buffer_ptr = c->packet_buffer;
2295 rtsp_c->packet_buffer_end = c->packet_buffer + size;
2296 c->buffer_ptr += len;
2298 /* send everything we can NOW */
2299 len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2300 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2302 rtsp_c->packet_buffer_ptr += len;
2303 if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2304 /* if we could not send all the data, we will
2305 send it later, so a new state is needed to
2306 "lock" the RTSP TCP connection */
2307 rtsp_c->state = RTSPSTATE_SEND_PACKET;
2310 /* all data has been sent */
2311 av_freep(&c->packet_buffer);
2313 /* send RTP packet directly in UDP */
2315 url_write(c->rtp_handles[c->packet_stream_index],
2316 c->buffer_ptr, len);
2317 c->buffer_ptr += len;
2318 /* here we continue as we can send several packets per 10 ms slot */
2321 /* TCP data output */
2322 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2324 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2325 ff_neterrno() != FF_NETERROR(EINTR))
2326 /* error : close connection */
2331 c->buffer_ptr += len;
2333 c->data_count += len;
2334 update_datarate(&c->datarate, c->data_count);
2336 c->stream->bytes_served += len;
2344 static int http_start_receive_data(HTTPContext *c)
2348 if (c->stream->feed_opened)
2351 /* Don't permit writing to this one */
2352 if (c->stream->readonly)
2356 fd = open(c->stream->feed_filename, O_RDWR);
2358 http_log("Error opening feeder file: %s\n", strerror(errno));
2363 c->stream->feed_write_index = ffm_read_write_index(fd);
2364 c->stream->feed_size = lseek(fd, 0, SEEK_END);
2365 lseek(fd, 0, SEEK_SET);
2367 /* init buffer input */
2368 c->buffer_ptr = c->buffer;
2369 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2370 c->stream->feed_opened = 1;
2374 static int http_receive_data(HTTPContext *c)
2378 if (c->buffer_end > c->buffer_ptr) {
2381 len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2383 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2384 ff_neterrno() != FF_NETERROR(EINTR))
2385 /* error : close connection */
2387 } else if (len == 0)
2388 /* end of connection : close it */
2391 c->buffer_ptr += len;
2392 c->data_count += len;
2393 update_datarate(&c->datarate, c->data_count);
2397 if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2398 if (c->buffer[0] != 'f' ||
2399 c->buffer[1] != 'm') {
2400 http_log("Feed stream has become desynchronized -- disconnecting\n");
2405 if (c->buffer_ptr >= c->buffer_end) {
2406 FFStream *feed = c->stream;
2407 /* a packet has been received : write it in the store, except
2409 if (c->data_count > FFM_PACKET_SIZE) {
2411 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2412 /* XXX: use llseek or url_seek */
2413 lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
2414 if (write(c->feed_fd, c->buffer, FFM_PACKET_SIZE) < 0) {
2415 http_log("Error writing to feed file: %s\n", strerror(errno));
2419 feed->feed_write_index += FFM_PACKET_SIZE;
2420 /* update file size */
2421 if (feed->feed_write_index > c->stream->feed_size)
2422 feed->feed_size = feed->feed_write_index;
2424 /* handle wrap around if max file size reached */
2425 if (c->stream->feed_max_size && feed->feed_write_index >= c->stream->feed_max_size)
2426 feed->feed_write_index = FFM_PACKET_SIZE;
2429 ffm_write_write_index(c->feed_fd, feed->feed_write_index);
2431 /* wake up any waiting connections */
2432 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2433 if (c1->state == HTTPSTATE_WAIT_FEED &&
2434 c1->stream->feed == c->stream->feed)
2435 c1->state = HTTPSTATE_SEND_DATA;
2438 /* We have a header in our hands that contains useful data */
2440 AVInputFormat *fmt_in;
2443 memset(&s, 0, sizeof(s));
2445 url_open_buf(&s.pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
2446 s.pb->is_streamed = 1;
2448 /* use feed output format name to find corresponding input format */
2449 fmt_in = av_find_input_format(feed->fmt->name);
2453 if (fmt_in->priv_data_size > 0) {
2454 s.priv_data = av_mallocz(fmt_in->priv_data_size);
2460 if (fmt_in->read_header(&s, 0) < 0) {
2461 av_freep(&s.priv_data);
2465 /* Now we have the actual streams */
2466 if (s.nb_streams != feed->nb_streams) {
2467 av_freep(&s.priv_data);
2470 for (i = 0; i < s.nb_streams; i++)
2471 memcpy(feed->streams[i]->codec,
2472 s.streams[i]->codec, sizeof(AVCodecContext));
2473 av_freep(&s.priv_data);
2475 c->buffer_ptr = c->buffer;
2480 c->stream->feed_opened = 0;
2482 /* wake up any waiting connections to stop waiting for feed */
2483 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2484 if (c1->state == HTTPSTATE_WAIT_FEED &&
2485 c1->stream->feed == c->stream->feed)
2486 c1->state = HTTPSTATE_SEND_DATA_TRAILER;
2491 /********************************************************************/
2494 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2501 switch(error_number) {
2502 case RTSP_STATUS_OK:
2505 case RTSP_STATUS_METHOD:
2506 str = "Method Not Allowed";
2508 case RTSP_STATUS_BANDWIDTH:
2509 str = "Not Enough Bandwidth";
2511 case RTSP_STATUS_SESSION:
2512 str = "Session Not Found";
2514 case RTSP_STATUS_STATE:
2515 str = "Method Not Valid in This State";
2517 case RTSP_STATUS_AGGREGATE:
2518 str = "Aggregate operation not allowed";
2520 case RTSP_STATUS_ONLY_AGGREGATE:
2521 str = "Only aggregate operation allowed";
2523 case RTSP_STATUS_TRANSPORT:
2524 str = "Unsupported transport";
2526 case RTSP_STATUS_INTERNAL:
2527 str = "Internal Server Error";
2529 case RTSP_STATUS_SERVICE:
2530 str = "Service Unavailable";
2532 case RTSP_STATUS_VERSION:
2533 str = "RTSP Version not supported";
2536 str = "Unknown Error";
2540 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2541 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2543 /* output GMT time */
2547 p = buf2 + strlen(p) - 1;
2550 url_fprintf(c->pb, "Date: %s GMT\r\n", buf2);
2553 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2555 rtsp_reply_header(c, error_number);
2556 url_fprintf(c->pb, "\r\n");
2559 static int rtsp_parse_request(HTTPContext *c)
2561 const char *p, *p1, *p2;
2567 RTSPHeader header1, *header = &header1;
2569 c->buffer_ptr[0] = '\0';
2572 get_word(cmd, sizeof(cmd), &p);
2573 get_word(url, sizeof(url), &p);
2574 get_word(protocol, sizeof(protocol), &p);
2576 av_strlcpy(c->method, cmd, sizeof(c->method));
2577 av_strlcpy(c->url, url, sizeof(c->url));
2578 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2580 if (url_open_dyn_buf(&c->pb) < 0) {
2581 /* XXX: cannot do more */
2582 c->pb = NULL; /* safety */
2586 /* check version name */
2587 if (strcmp(protocol, "RTSP/1.0") != 0) {
2588 rtsp_reply_error(c, RTSP_STATUS_VERSION);
2592 /* parse each header line */
2593 memset(header, 0, sizeof(RTSPHeader));
2594 /* skip to next line */
2595 while (*p != '\n' && *p != '\0')
2599 while (*p != '\0') {
2600 p1 = strchr(p, '\n');
2604 if (p2 > p && p2[-1] == '\r')
2606 /* skip empty line */
2610 if (len > sizeof(line) - 1)
2611 len = sizeof(line) - 1;
2612 memcpy(line, p, len);
2614 rtsp_parse_line(header, line);
2618 /* handle sequence number */
2619 c->seq = header->seq;
2621 if (!strcmp(cmd, "DESCRIBE"))
2622 rtsp_cmd_describe(c, url);
2623 else if (!strcmp(cmd, "OPTIONS"))
2624 rtsp_cmd_options(c, url);
2625 else if (!strcmp(cmd, "SETUP"))
2626 rtsp_cmd_setup(c, url, header);
2627 else if (!strcmp(cmd, "PLAY"))
2628 rtsp_cmd_play(c, url, header);
2629 else if (!strcmp(cmd, "PAUSE"))
2630 rtsp_cmd_pause(c, url, header);
2631 else if (!strcmp(cmd, "TEARDOWN"))
2632 rtsp_cmd_teardown(c, url, header);
2634 rtsp_reply_error(c, RTSP_STATUS_METHOD);
2637 len = url_close_dyn_buf(c->pb, &c->pb_buffer);
2638 c->pb = NULL; /* safety */
2640 /* XXX: cannot do more */
2643 c->buffer_ptr = c->pb_buffer;
2644 c->buffer_end = c->pb_buffer + len;
2645 c->state = RTSPSTATE_SEND_REPLY;
2649 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
2650 struct in_addr my_ip)
2652 AVFormatContext *avc;
2653 AVStream avs[MAX_STREAMS];
2656 avc = av_alloc_format_context();
2660 if (stream->title[0] != 0) {
2661 av_strlcpy(avc->title, stream->title, sizeof(avc->title));
2663 av_strlcpy(avc->title, "No Title", sizeof(avc->title));
2665 avc->nb_streams = stream->nb_streams;
2666 if (stream->is_multicast) {
2667 snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2668 inet_ntoa(stream->multicast_ip),
2669 stream->multicast_port, stream->multicast_ttl);
2672 for(i = 0; i < stream->nb_streams; i++) {
2673 avc->streams[i] = &avs[i];
2674 avc->streams[i]->codec = stream->streams[i]->codec;
2676 *pbuffer = av_mallocz(2048);
2677 avf_sdp_create(&avc, 1, *pbuffer, 2048);
2680 return strlen(*pbuffer);
2683 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2685 // rtsp_reply_header(c, RTSP_STATUS_OK);
2686 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2687 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2688 url_fprintf(c->pb, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2689 url_fprintf(c->pb, "\r\n");
2692 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2698 int content_length, len;
2699 struct sockaddr_in my_addr;
2701 /* find which url is asked */
2702 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2707 for(stream = first_stream; stream != NULL; stream = stream->next) {
2708 if (!stream->is_feed &&
2709 stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2710 !strcmp(path, stream->filename)) {
2714 /* no stream found */
2715 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2719 /* prepare the media description in sdp format */
2721 /* get the host IP */
2722 len = sizeof(my_addr);
2723 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2724 content_length = prepare_sdp_description(stream, &content, my_addr.sin_addr);
2725 if (content_length < 0) {
2726 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2729 rtsp_reply_header(c, RTSP_STATUS_OK);
2730 url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
2731 url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
2732 url_fprintf(c->pb, "\r\n");
2733 put_buffer(c->pb, content, content_length);
2736 static HTTPContext *find_rtp_session(const char *session_id)
2740 if (session_id[0] == '\0')
2743 for(c = first_http_ctx; c != NULL; c = c->next) {
2744 if (!strcmp(c->session_id, session_id))
2750 static RTSPTransportField *find_transport(RTSPHeader *h, enum RTSPProtocol protocol)
2752 RTSPTransportField *th;
2755 for(i=0;i<h->nb_transports;i++) {
2756 th = &h->transports[i];
2757 if (th->protocol == protocol)
2763 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
2767 int stream_index, port;
2772 RTSPTransportField *th;
2773 struct sockaddr_in dest_addr;
2774 RTSPActionServerSetup setup;
2776 /* find which url is asked */
2777 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2782 /* now check each stream */
2783 for(stream = first_stream; stream != NULL; stream = stream->next) {
2784 if (!stream->is_feed &&
2785 stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
2786 /* accept aggregate filenames only if single stream */
2787 if (!strcmp(path, stream->filename)) {
2788 if (stream->nb_streams != 1) {
2789 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
2796 for(stream_index = 0; stream_index < stream->nb_streams;
2798 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2799 stream->filename, stream_index);
2800 if (!strcmp(path, buf))
2805 /* no stream found */
2806 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2810 /* generate session id if needed */
2811 if (h->session_id[0] == '\0')
2812 snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
2813 av_random(&random_state), av_random(&random_state));
2815 /* find rtp session, and create it if none found */
2816 rtp_c = find_rtp_session(h->session_id);
2818 /* always prefer UDP */
2819 th = find_transport(h, RTSP_PROTOCOL_RTP_UDP);
2821 th = find_transport(h, RTSP_PROTOCOL_RTP_TCP);
2823 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2828 rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
2831 rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
2835 /* open input stream */
2836 if (open_input_stream(rtp_c, "") < 0) {
2837 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2842 /* test if stream is OK (test needed because several SETUP needs
2843 to be done for a given file) */
2844 if (rtp_c->stream != stream) {
2845 rtsp_reply_error(c, RTSP_STATUS_SERVICE);
2849 /* test if stream is already set up */
2850 if (rtp_c->rtp_ctx[stream_index]) {
2851 rtsp_reply_error(c, RTSP_STATUS_STATE);
2855 /* check transport */
2856 th = find_transport(h, rtp_c->rtp_protocol);
2857 if (!th || (th->protocol == RTSP_PROTOCOL_RTP_UDP &&
2858 th->client_port_min <= 0)) {
2859 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2863 /* setup default options */
2864 setup.transport_option[0] = '\0';
2865 dest_addr = rtp_c->from_addr;
2866 dest_addr.sin_port = htons(th->client_port_min);
2869 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2870 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2874 /* now everything is OK, so we can send the connection parameters */
2875 rtsp_reply_header(c, RTSP_STATUS_OK);
2877 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2879 switch(rtp_c->rtp_protocol) {
2880 case RTSP_PROTOCOL_RTP_UDP:
2881 port = rtp_get_local_port(rtp_c->rtp_handles[stream_index]);
2882 url_fprintf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2883 "client_port=%d-%d;server_port=%d-%d",
2884 th->client_port_min, th->client_port_min + 1,
2887 case RTSP_PROTOCOL_RTP_TCP:
2888 url_fprintf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2889 stream_index * 2, stream_index * 2 + 1);
2894 if (setup.transport_option[0] != '\0')
2895 url_fprintf(c->pb, ";%s", setup.transport_option);
2896 url_fprintf(c->pb, "\r\n");
2899 url_fprintf(c->pb, "\r\n");
2903 /* find an rtp connection by using the session ID. Check consistency
2905 static HTTPContext *find_rtp_session_with_url(const char *url,
2906 const char *session_id)
2914 rtp_c = find_rtp_session(session_id);
2918 /* find which url is asked */
2919 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2923 if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
2924 for(s=0; s<rtp_c->stream->nb_streams; ++s) {
2925 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2926 rtp_c->stream->filename, s);
2927 if(!strncmp(path, buf, sizeof(buf))) {
2928 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2935 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h)
2939 rtp_c = find_rtp_session_with_url(url, h->session_id);
2941 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2945 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
2946 rtp_c->state != HTTPSTATE_WAIT_FEED &&
2947 rtp_c->state != HTTPSTATE_READY) {
2948 rtsp_reply_error(c, RTSP_STATUS_STATE);
2953 /* XXX: seek in stream */
2954 if (h->range_start != AV_NOPTS_VALUE) {
2955 printf("range_start=%0.3f\n", (double)h->range_start / AV_TIME_BASE);
2956 av_seek_frame(rtp_c->fmt_in, -1, h->range_start);
2960 rtp_c->state = HTTPSTATE_SEND_DATA;
2962 /* now everything is OK, so we can send the connection parameters */
2963 rtsp_reply_header(c, RTSP_STATUS_OK);
2965 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2966 url_fprintf(c->pb, "\r\n");
2969 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h)
2973 rtp_c = find_rtp_session_with_url(url, h->session_id);
2975 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2979 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
2980 rtp_c->state != HTTPSTATE_WAIT_FEED) {
2981 rtsp_reply_error(c, RTSP_STATUS_STATE);
2985 rtp_c->state = HTTPSTATE_READY;
2986 rtp_c->first_pts = AV_NOPTS_VALUE;
2987 /* now everything is OK, so we can send the connection parameters */
2988 rtsp_reply_header(c, RTSP_STATUS_OK);
2990 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2991 url_fprintf(c->pb, "\r\n");
2994 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
2997 char session_id[32];
2999 rtp_c = find_rtp_session_with_url(url, h->session_id);
3001 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3005 av_strlcpy(session_id, rtp_c->session_id, sizeof(session_id));
3007 /* abort the session */
3008 close_connection(rtp_c);
3010 /* now everything is OK, so we can send the connection parameters */
3011 rtsp_reply_header(c, RTSP_STATUS_OK);
3013 url_fprintf(c->pb, "Session: %s\r\n", session_id);
3014 url_fprintf(c->pb, "\r\n");
3018 /********************************************************************/
3021 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3022 FFStream *stream, const char *session_id,
3023 enum RTSPProtocol rtp_protocol)
3025 HTTPContext *c = NULL;
3026 const char *proto_str;
3028 /* XXX: should output a warning page when coming
3029 close to the connection limit */
3030 if (nb_connections >= nb_max_connections)
3033 /* add a new connection */
3034 c = av_mallocz(sizeof(HTTPContext));
3039 c->poll_entry = NULL;
3040 c->from_addr = *from_addr;
3041 c->buffer_size = IOBUFFER_INIT_SIZE;
3042 c->buffer = av_malloc(c->buffer_size);
3047 av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
3048 c->state = HTTPSTATE_READY;
3049 c->is_packetized = 1;
3050 c->rtp_protocol = rtp_protocol;
3052 /* protocol is shown in statistics */
3053 switch(c->rtp_protocol) {
3054 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3055 proto_str = "MCAST";
3057 case RTSP_PROTOCOL_RTP_UDP:
3060 case RTSP_PROTOCOL_RTP_TCP:
3067 av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
3068 av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
3070 current_bandwidth += stream->bandwidth;
3072 c->next = first_http_ctx;
3084 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3085 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3087 static int rtp_new_av_stream(HTTPContext *c,
3088 int stream_index, struct sockaddr_in *dest_addr,
3089 HTTPContext *rtsp_c)
3091 AVFormatContext *ctx;
3094 URLContext *h = NULL;
3097 int max_packet_size;
3099 /* now we can open the relevant output stream */
3100 ctx = av_alloc_format_context();
3103 ctx->oformat = guess_format("rtp", NULL, NULL);
3105 st = av_mallocz(sizeof(AVStream));
3108 st->codec= avcodec_alloc_context();
3109 ctx->nb_streams = 1;
3110 ctx->streams[0] = st;
3112 if (!c->stream->feed ||
3113 c->stream->feed == c->stream)
3114 memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3117 c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3119 st->priv_data = NULL;
3121 /* build destination RTP address */
3122 ipaddr = inet_ntoa(dest_addr->sin_addr);
3124 switch(c->rtp_protocol) {
3125 case RTSP_PROTOCOL_RTP_UDP:
3126 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3129 /* XXX: also pass as parameter to function ? */
3130 if (c->stream->is_multicast) {
3132 ttl = c->stream->multicast_ttl;
3135 snprintf(ctx->filename, sizeof(ctx->filename),
3136 "rtp://%s:%d?multicast=1&ttl=%d",
3137 ipaddr, ntohs(dest_addr->sin_port), ttl);
3139 snprintf(ctx->filename, sizeof(ctx->filename),
3140 "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3143 if (url_open(&h, ctx->filename, URL_WRONLY) < 0)
3145 c->rtp_handles[stream_index] = h;
3146 max_packet_size = url_get_max_packet_size(h);
3148 case RTSP_PROTOCOL_RTP_TCP:
3151 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3157 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3158 ipaddr, ntohs(dest_addr->sin_port),
3160 c->stream->filename, stream_index, c->protocol);
3162 /* normally, no packets should be output here, but the packet size may be checked */
3163 if (url_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0) {
3164 /* XXX: close stream */
3167 av_set_parameters(ctx, NULL);
3168 if (av_write_header(ctx) < 0) {
3175 url_close_dyn_buf(ctx->pb, &dummy_buf);
3178 c->rtp_ctx[stream_index] = ctx;
3182 /********************************************************************/
3183 /* ffserver initialization */
3185 static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
3189 fst = av_mallocz(sizeof(AVStream));
3192 fst->codec= avcodec_alloc_context();
3193 fst->priv_data = av_mallocz(sizeof(FeedData));
3194 memcpy(fst->codec, codec, sizeof(AVCodecContext));
3195 fst->index = stream->nb_streams;
3196 av_set_pts_info(fst, 33, 1, 90000);
3197 stream->streams[stream->nb_streams++] = fst;
3201 /* return the stream number in the feed */
3202 static int add_av_stream(FFStream *feed, AVStream *st)
3205 AVCodecContext *av, *av1;
3209 for(i=0;i<feed->nb_streams;i++) {
3210 st = feed->streams[i];
3212 if (av1->codec_id == av->codec_id &&
3213 av1->codec_type == av->codec_type &&
3214 av1->bit_rate == av->bit_rate) {
3216 switch(av->codec_type) {
3217 case CODEC_TYPE_AUDIO:
3218 if (av1->channels == av->channels &&
3219 av1->sample_rate == av->sample_rate)
3222 case CODEC_TYPE_VIDEO:
3223 if (av1->width == av->width &&
3224 av1->height == av->height &&
3225 av1->time_base.den == av->time_base.den &&
3226 av1->time_base.num == av->time_base.num &&
3227 av1->gop_size == av->gop_size)
3236 fst = add_av_stream1(feed, av);
3239 return feed->nb_streams - 1;
3244 static void remove_stream(FFStream *stream)
3248 while (*ps != NULL) {
3256 /* specific mpeg4 handling : we extract the raw parameters */
3257 static void extract_mpeg4_header(AVFormatContext *infile)
3259 int mpeg4_count, i, size;
3265 for(i=0;i<infile->nb_streams;i++) {
3266 st = infile->streams[i];
3267 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3268 st->codec->extradata_size == 0) {
3275 printf("MPEG4 without extra data: trying to find header in %s\n", infile->filename);
3276 while (mpeg4_count > 0) {
3277 if (av_read_packet(infile, &pkt) < 0)
3279 st = infile->streams[pkt.stream_index];
3280 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3281 st->codec->extradata_size == 0) {
3282 av_freep(&st->codec->extradata);
3283 /* fill extradata with the header */
3284 /* XXX: we make hard suppositions here ! */
3286 while (p < pkt.data + pkt.size - 4) {
3287 /* stop when vop header is found */
3288 if (p[0] == 0x00 && p[1] == 0x00 &&
3289 p[2] == 0x01 && p[3] == 0xb6) {
3290 size = p - pkt.data;
3291 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3292 st->codec->extradata = av_malloc(size);
3293 st->codec->extradata_size = size;
3294 memcpy(st->codec->extradata, pkt.data, size);
3301 av_free_packet(&pkt);
3305 /* compute the needed AVStream for each file */
3306 static void build_file_streams(void)
3308 FFStream *stream, *stream_next;
3309 AVFormatContext *infile;
3312 /* gather all streams */
3313 for(stream = first_stream; stream != NULL; stream = stream_next) {
3314 stream_next = stream->next;
3315 if (stream->stream_type == STREAM_TYPE_LIVE &&
3317 /* the stream comes from a file */
3318 /* try to open the file */
3320 stream->ap_in = av_mallocz(sizeof(AVFormatParameters));
3321 if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
3322 /* specific case : if transport stream output to RTP,
3323 we use a raw transport stream reader */
3324 stream->ap_in->mpeg2ts_raw = 1;
3325 stream->ap_in->mpeg2ts_compute_pcr = 1;
3328 if ((ret = av_open_input_file(&infile, stream->feed_filename,
3329 stream->ifmt, 0, stream->ap_in)) < 0) {
3330 http_log("could not open %s: %d\n", stream->feed_filename, ret);
3331 /* remove stream (no need to spend more time on it) */
3333 remove_stream(stream);
3335 /* find all the AVStreams inside and reference them in
3337 if (av_find_stream_info(infile) < 0) {
3338 http_log("Could not find codec parameters from '%s'\n",
3339 stream->feed_filename);
3340 av_close_input_file(infile);
3343 extract_mpeg4_header(infile);
3345 for(i=0;i<infile->nb_streams;i++)
3346 add_av_stream1(stream, infile->streams[i]->codec);
3348 av_close_input_file(infile);
3354 /* compute the needed AVStream for each feed */
3355 static void build_feed_streams(void)
3357 FFStream *stream, *feed;
3360 /* gather all streams */
3361 for(stream = first_stream; stream != NULL; stream = stream->next) {
3362 feed = stream->feed;
3364 if (!stream->is_feed) {
3365 /* we handle a stream coming from a feed */
3366 for(i=0;i<stream->nb_streams;i++)
3367 stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
3372 /* gather all streams */
3373 for(stream = first_stream; stream != NULL; stream = stream->next) {
3374 feed = stream->feed;
3376 if (stream->is_feed) {
3377 for(i=0;i<stream->nb_streams;i++)
3378 stream->feed_streams[i] = i;
3383 /* create feed files if needed */
3384 for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
3387 if (url_exist(feed->feed_filename)) {
3388 /* See if it matches */
3392 if (av_open_input_file(&s, feed->feed_filename, NULL, FFM_PACKET_SIZE, NULL) >= 0) {
3393 /* Now see if it matches */
3394 if (s->nb_streams == feed->nb_streams) {
3396 for(i=0;i<s->nb_streams;i++) {
3398 sf = feed->streams[i];
3401 if (sf->index != ss->index ||
3403 printf("Index & Id do not match for stream %d (%s)\n",
3404 i, feed->feed_filename);
3407 AVCodecContext *ccf, *ccs;
3411 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3413 if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
3414 printf("Codecs do not match for stream %d\n", i);
3416 } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3417 printf("Codec bitrates do not match for stream %d\n", i);
3419 } else if (ccf->codec_type == CODEC_TYPE_VIDEO) {
3420 if (CHECK_CODEC(time_base.den) ||
3421 CHECK_CODEC(time_base.num) ||
3422 CHECK_CODEC(width) ||
3423 CHECK_CODEC(height)) {
3424 printf("Codec width, height and framerate do not match for stream %d\n", i);
3427 } else if (ccf->codec_type == CODEC_TYPE_AUDIO) {
3428 if (CHECK_CODEC(sample_rate) ||
3429 CHECK_CODEC(channels) ||
3430 CHECK_CODEC(frame_size)) {
3431 printf("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3435 printf("Unknown codec type\n");
3443 printf("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3444 feed->feed_filename, s->nb_streams, feed->nb_streams);
3446 av_close_input_file(s);
3448 printf("Deleting feed file '%s' as it appears to be corrupt\n",
3449 feed->feed_filename);
3452 if (feed->readonly) {
3453 printf("Unable to delete feed file '%s' as it is marked readonly\n",
3454 feed->feed_filename);
3457 unlink(feed->feed_filename);
3460 if (!url_exist(feed->feed_filename)) {
3461 AVFormatContext s1, *s = &s1;
3463 if (feed->readonly) {
3464 printf("Unable to create feed file '%s' as it is marked readonly\n",
3465 feed->feed_filename);
3469 /* only write the header of the ffm file */
3470 if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
3471 http_log("Could not open output feed file '%s'\n",
3472 feed->feed_filename);
3475 s->oformat = feed->fmt;
3476 s->nb_streams = feed->nb_streams;
3477 for(i=0;i<s->nb_streams;i++) {
3479 st = feed->streams[i];
3482 av_set_parameters(s, NULL);
3483 if (av_write_header(s) < 0) {
3484 http_log("Container doesn't supports the required parameters\n");
3487 /* XXX: need better api */
3488 av_freep(&s->priv_data);
3491 /* get feed size and write index */
3492 fd = open(feed->feed_filename, O_RDONLY);
3494 http_log("Could not open output feed file '%s'\n",
3495 feed->feed_filename);
3499 feed->feed_write_index = ffm_read_write_index(fd);
3500 feed->feed_size = lseek(fd, 0, SEEK_END);
3501 /* ensure that we do not wrap before the end of file */
3502 if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3503 feed->feed_max_size = feed->feed_size;
3509 /* compute the bandwidth used by each stream */
3510 static void compute_bandwidth(void)
3516 for(stream = first_stream; stream != NULL; stream = stream->next) {
3518 for(i=0;i<stream->nb_streams;i++) {
3519 AVStream *st = stream->streams[i];
3520 switch(st->codec->codec_type) {
3521 case CODEC_TYPE_AUDIO:
3522 case CODEC_TYPE_VIDEO:
3523 bandwidth += st->codec->bit_rate;
3529 stream->bandwidth = (bandwidth + 999) / 1000;
3533 static void get_arg(char *buf, int buf_size, const char **pp)
3540 while (isspace(*p)) p++;
3543 if (*p == '\"' || *p == '\'')
3555 if ((q - buf) < buf_size - 1)
3560 if (quote && *p == quote)
3565 /* add a codec and set the default parameters */
3566 static void add_codec(FFStream *stream, AVCodecContext *av)
3570 /* compute default parameters */
3571 switch(av->codec_type) {
3572 case CODEC_TYPE_AUDIO:
3573 if (av->bit_rate == 0)
3574 av->bit_rate = 64000;
3575 if (av->sample_rate == 0)
3576 av->sample_rate = 22050;
3577 if (av->channels == 0)
3580 case CODEC_TYPE_VIDEO:
3581 if (av->bit_rate == 0)
3582 av->bit_rate = 64000;
3583 if (av->time_base.num == 0){
3584 av->time_base.den = 5;
3585 av->time_base.num = 1;
3587 if (av->width == 0 || av->height == 0) {
3591 /* Bitrate tolerance is less for streaming */
3592 if (av->bit_rate_tolerance == 0)
3593 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
3594 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
3599 if (av->max_qdiff == 0)
3601 av->qcompress = 0.5;
3604 if (!av->nsse_weight)
3605 av->nsse_weight = 8;
3607 av->frame_skip_cmp = FF_CMP_DCTMAX;
3608 av->me_method = ME_EPZS;
3609 av->rc_buffer_aggressivity = 1.0;
3612 av->rc_eq = "tex^qComp";
3613 if (!av->i_quant_factor)
3614 av->i_quant_factor = -0.8;
3615 if (!av->b_quant_factor)
3616 av->b_quant_factor = 1.25;
3617 if (!av->b_quant_offset)
3618 av->b_quant_offset = 1.25;
3619 if (!av->rc_max_rate)
3620 av->rc_max_rate = av->bit_rate * 2;
3622 if (av->rc_max_rate && !av->rc_buffer_size) {
3623 av->rc_buffer_size = av->rc_max_rate;
3632 st = av_mallocz(sizeof(AVStream));
3635 st->codec = avcodec_alloc_context();
3636 stream->streams[stream->nb_streams++] = st;
3637 memcpy(st->codec, av, sizeof(AVCodecContext));
3640 static int opt_audio_codec(const char *arg)
3642 AVCodec *p= avcodec_find_encoder_by_name(arg);
3644 if (p == NULL || p->type != CODEC_TYPE_AUDIO)
3645 return CODEC_ID_NONE;
3650 static int opt_video_codec(const char *arg)
3652 AVCodec *p= avcodec_find_encoder_by_name(arg);
3654 if (p == NULL || p->type != CODEC_TYPE_VIDEO)
3655 return CODEC_ID_NONE;
3660 /* simplistic plugin support */
3663 static void load_module(const char *filename)
3666 void (*init_func)(void);
3667 dll = dlopen(filename, RTLD_NOW);
3669 fprintf(stderr, "Could not load module '%s' - %s\n",
3670 filename, dlerror());
3674 init_func = dlsym(dll, "ffserver_module_init");
3677 "%s: init function 'ffserver_module_init()' not found\n",
3686 static int opt_default(const char *opt, const char *arg,
3687 AVCodecContext *avctx, int type)
3689 const AVOption *o = NULL;
3690 const AVOption *o2 = av_find_opt(avctx, opt, NULL, type, type);
3692 o = av_set_string(avctx, opt, arg);
3698 static int parse_ffconfig(const char *filename)
3705 int val, errors, line_num;
3706 FFStream **last_stream, *stream, *redirect;
3707 FFStream **last_feed, *feed;
3708 AVCodecContext audio_enc, video_enc;
3709 int audio_id, video_id;
3711 f = fopen(filename, "r");
3719 first_stream = NULL;
3720 last_stream = &first_stream;
3722 last_feed = &first_feed;
3726 audio_id = CODEC_ID_NONE;
3727 video_id = CODEC_ID_NONE;
3729 if (fgets(line, sizeof(line), f) == NULL)
3735 if (*p == '\0' || *p == '#')
3738 get_arg(cmd, sizeof(cmd), &p);
3740 if (!strcasecmp(cmd, "Port")) {
3741 get_arg(arg, sizeof(arg), &p);
3743 if (val < 1 || val > 65536) {
3744 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3745 filename, line_num, arg);
3748 my_http_addr.sin_port = htons(val);
3749 } else if (!strcasecmp(cmd, "BindAddress")) {
3750 get_arg(arg, sizeof(arg), &p);
3751 if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
3752 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3753 filename, line_num, arg);
3756 } else if (!strcasecmp(cmd, "NoDaemon")) {
3757 ffserver_daemon = 0;
3758 } else if (!strcasecmp(cmd, "RTSPPort")) {
3759 get_arg(arg, sizeof(arg), &p);
3761 if (val < 1 || val > 65536) {
3762 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3763 filename, line_num, arg);
3766 my_rtsp_addr.sin_port = htons(atoi(arg));
3767 } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
3768 get_arg(arg, sizeof(arg), &p);
3769 if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
3770 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3771 filename, line_num, arg);
3774 } else if (!strcasecmp(cmd, "MaxClients")) {
3775 get_arg(arg, sizeof(arg), &p);
3777 if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
3778 fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
3779 filename, line_num, arg);
3782 nb_max_connections = val;
3784 } else if (!strcasecmp(cmd, "MaxBandwidth")) {
3786 get_arg(arg, sizeof(arg), &p);
3788 if (llval < 10 || llval > 10000000) {
3789 fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
3790 filename, line_num, arg);
3793 max_bandwidth = llval;
3794 } else if (!strcasecmp(cmd, "CustomLog")) {
3795 if (!ffserver_debug)
3796 get_arg(logfilename, sizeof(logfilename), &p);
3797 } else if (!strcasecmp(cmd, "<Feed")) {
3798 /*********************************************/
3799 /* Feed related options */
3801 if (stream || feed) {
3802 fprintf(stderr, "%s:%d: Already in a tag\n",
3803 filename, line_num);
3805 feed = av_mallocz(sizeof(FFStream));
3806 /* add in stream list */
3807 *last_stream = feed;
3808 last_stream = &feed->next;
3809 /* add in feed list */
3811 last_feed = &feed->next_feed;
3813 get_arg(feed->filename, sizeof(feed->filename), &p);
3814 q = strrchr(feed->filename, '>');
3817 feed->fmt = guess_format("ffm", NULL, NULL);
3818 /* defaut feed file */
3819 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
3820 "/tmp/%s.ffm", feed->filename);
3821 feed->feed_max_size = 5 * 1024 * 1024;
3823 feed->feed = feed; /* self feeding :-) */
3825 } else if (!strcasecmp(cmd, "Launch")) {
3829 feed->child_argv = av_mallocz(64 * sizeof(char *));
3831 for (i = 0; i < 62; i++) {
3832 get_arg(arg, sizeof(arg), &p);
3836 feed->child_argv[i] = av_strdup(arg);
3839 feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
3841 snprintf(feed->child_argv[i], 30+strlen(feed->filename),
3843 (my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
3844 inet_ntoa(my_http_addr.sin_addr),
3845 ntohs(my_http_addr.sin_port), feed->filename);
3850 fprintf(stdout, "Launch commandline: ");
3851 for (j = 0; j <= i; j++)
3852 fprintf(stdout, "%s ", feed->child_argv[j]);
3853 fprintf(stdout, "\n");
3856 } else if (!strcasecmp(cmd, "ReadOnlyFile")) {
3858 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3860 } else if (stream) {
3861 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3863 } else if (!strcasecmp(cmd, "File")) {
3865 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3867 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3868 } else if (!strcasecmp(cmd, "FileMaxSize")) {
3873 get_arg(arg, sizeof(arg), &p);
3875 fsize = strtod(p1, &p1);
3876 switch(toupper(*p1)) {
3881 fsize *= 1024 * 1024;
3884 fsize *= 1024 * 1024 * 1024;
3887 feed->feed_max_size = (int64_t)fsize;
3889 } else if (!strcasecmp(cmd, "</Feed>")) {
3891 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
3892 filename, line_num);
3896 } else if (!strcasecmp(cmd, "<Stream")) {
3897 /*********************************************/
3898 /* Stream related options */
3900 if (stream || feed) {
3901 fprintf(stderr, "%s:%d: Already in a tag\n",
3902 filename, line_num);
3904 const AVClass *class;
3905 stream = av_mallocz(sizeof(FFStream));
3906 *last_stream = stream;
3907 last_stream = &stream->next;
3909 get_arg(stream->filename, sizeof(stream->filename), &p);
3910 q = strrchr(stream->filename, '>');
3913 stream->fmt = guess_stream_format(NULL, stream->filename, NULL);
3914 /* fetch avclass so AVOption works
3915 * FIXME try to use avcodec_get_context_defaults2
3916 * without changing defaults too much */
3917 avcodec_get_context_defaults(&video_enc);
3918 class = video_enc.av_class;
3919 memset(&audio_enc, 0, sizeof(AVCodecContext));
3920 memset(&video_enc, 0, sizeof(AVCodecContext));
3921 audio_enc.av_class = class;
3922 video_enc.av_class = class;
3923 audio_id = CODEC_ID_NONE;
3924 video_id = CODEC_ID_NONE;
3926 audio_id = stream->fmt->audio_codec;
3927 video_id = stream->fmt->video_codec;
3930 } else if (!strcasecmp(cmd, "Feed")) {
3931 get_arg(arg, sizeof(arg), &p);
3936 while (sfeed != NULL) {
3937 if (!strcmp(sfeed->filename, arg))
3939 sfeed = sfeed->next_feed;
3942 fprintf(stderr, "%s:%d: feed '%s' not defined\n",
3943 filename, line_num, arg);
3945 stream->feed = sfeed;
3947 } else if (!strcasecmp(cmd, "Format")) {
3948 get_arg(arg, sizeof(arg), &p);
3950 if (!strcmp(arg, "status")) {
3951 stream->stream_type = STREAM_TYPE_STATUS;
3954 stream->stream_type = STREAM_TYPE_LIVE;
3955 /* jpeg cannot be used here, so use single frame jpeg */
3956 if (!strcmp(arg, "jpeg"))
3957 strcpy(arg, "mjpeg");
3958 stream->fmt = guess_stream_format(arg, NULL, NULL);
3960 fprintf(stderr, "%s:%d: Unknown Format: %s\n",
3961 filename, line_num, arg);
3966 audio_id = stream->fmt->audio_codec;
3967 video_id = stream->fmt->video_codec;
3970 } else if (!strcasecmp(cmd, "InputFormat")) {
3971 get_arg(arg, sizeof(arg), &p);
3972 stream->ifmt = av_find_input_format(arg);
3973 if (!stream->ifmt) {
3974 fprintf(stderr, "%s:%d: Unknown input format: %s\n",
3975 filename, line_num, arg);
3977 } else if (!strcasecmp(cmd, "FaviconURL")) {
3978 if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
3979 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3981 fprintf(stderr, "%s:%d: FaviconURL only permitted for status streams\n",
3982 filename, line_num);
3985 } else if (!strcasecmp(cmd, "Author")) {
3987 get_arg(stream->author, sizeof(stream->author), &p);
3988 } else if (!strcasecmp(cmd, "Comment")) {
3990 get_arg(stream->comment, sizeof(stream->comment), &p);
3991 } else if (!strcasecmp(cmd, "Copyright")) {
3993 get_arg(stream->copyright, sizeof(stream->copyright), &p);
3994 } else if (!strcasecmp(cmd, "Title")) {
3996 get_arg(stream->title, sizeof(stream->title), &p);
3997 } else if (!strcasecmp(cmd, "Preroll")) {
3998 get_arg(arg, sizeof(arg), &p);
4000 stream->prebuffer = atof(arg) * 1000;
4001 } else if (!strcasecmp(cmd, "StartSendOnKey")) {
4003 stream->send_on_key = 1;
4004 } else if (!strcasecmp(cmd, "AudioCodec")) {
4005 get_arg(arg, sizeof(arg), &p);
4006 audio_id = opt_audio_codec(arg);
4007 if (audio_id == CODEC_ID_NONE) {
4008 fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
4009 filename, line_num, arg);
4012 } else if (!strcasecmp(cmd, "VideoCodec")) {
4013 get_arg(arg, sizeof(arg), &p);
4014 video_id = opt_video_codec(arg);
4015 if (video_id == CODEC_ID_NONE) {
4016 fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
4017 filename, line_num, arg);
4020 } else if (!strcasecmp(cmd, "MaxTime")) {
4021 get_arg(arg, sizeof(arg), &p);
4023 stream->max_time = atof(arg) * 1000;
4024 } else if (!strcasecmp(cmd, "AudioBitRate")) {
4025 get_arg(arg, sizeof(arg), &p);
4027 audio_enc.bit_rate = atoi(arg) * 1000;
4028 } else if (!strcasecmp(cmd, "AudioChannels")) {
4029 get_arg(arg, sizeof(arg), &p);
4031 audio_enc.channels = atoi(arg);
4032 } else if (!strcasecmp(cmd, "AudioSampleRate")) {
4033 get_arg(arg, sizeof(arg), &p);
4035 audio_enc.sample_rate = atoi(arg);
4036 } else if (!strcasecmp(cmd, "AudioQuality")) {
4037 get_arg(arg, sizeof(arg), &p);
4039 // audio_enc.quality = atof(arg) * 1000;
4041 } else if (!strcasecmp(cmd, "VideoBitRateRange")) {
4043 int minrate, maxrate;
4045 get_arg(arg, sizeof(arg), &p);
4047 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
4048 video_enc.rc_min_rate = minrate * 1000;
4049 video_enc.rc_max_rate = maxrate * 1000;
4051 fprintf(stderr, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4052 filename, line_num, arg);
4056 } else if (!strcasecmp(cmd, "Debug")) {
4058 get_arg(arg, sizeof(arg), &p);
4059 video_enc.debug = strtol(arg,0,0);
4061 } else if (!strcasecmp(cmd, "Strict")) {
4063 get_arg(arg, sizeof(arg), &p);
4064 video_enc.strict_std_compliance = atoi(arg);
4066 } else if (!strcasecmp(cmd, "VideoBufferSize")) {
4068 get_arg(arg, sizeof(arg), &p);
4069 video_enc.rc_buffer_size = atoi(arg) * 8*1024;
4071 } else if (!strcasecmp(cmd, "VideoBitRateTolerance")) {
4073 get_arg(arg, sizeof(arg), &p);
4074 video_enc.bit_rate_tolerance = atoi(arg) * 1000;
4076 } else if (!strcasecmp(cmd, "VideoBitRate")) {
4077 get_arg(arg, sizeof(arg), &p);
4079 video_enc.bit_rate = atoi(arg) * 1000;
4081 } else if (!strcasecmp(cmd, "VideoSize")) {
4082 get_arg(arg, sizeof(arg), &p);
4084 av_parse_video_frame_size(&video_enc.width, &video_enc.height, arg);
4085 if ((video_enc.width % 16) != 0 ||
4086 (video_enc.height % 16) != 0) {
4087 fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
4088 filename, line_num);
4092 } else if (!strcasecmp(cmd, "VideoFrameRate")) {
4093 get_arg(arg, sizeof(arg), &p);
4095 AVRational frame_rate;
4096 if (av_parse_video_frame_rate(&frame_rate, arg) < 0) {
4097 fprintf(stderr, "Incorrect frame rate\n");
4100 video_enc.time_base.num = frame_rate.den;
4101 video_enc.time_base.den = frame_rate.num;
4104 } else if (!strcasecmp(cmd, "VideoGopSize")) {
4105 get_arg(arg, sizeof(arg), &p);
4107 video_enc.gop_size = atoi(arg);
4108 } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
4110 video_enc.gop_size = 1;
4111 } else if (!strcasecmp(cmd, "VideoHighQuality")) {
4113 video_enc.mb_decision = FF_MB_DECISION_BITS;
4114 } else if (!strcasecmp(cmd, "Video4MotionVector")) {
4116 video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
4117 video_enc.flags |= CODEC_FLAG_4MV;
4119 } else if (!strcasecmp(cmd, "AVOptionVideo") ||
4120 !strcasecmp(cmd, "AVOptionAudio")) {
4122 AVCodecContext *avctx;
4124 get_arg(arg, sizeof(arg), &p);
4125 get_arg(arg2, sizeof(arg2), &p);
4126 if (!strcasecmp(cmd, "AVOptionVideo")) {
4128 type = AV_OPT_FLAG_VIDEO_PARAM;
4131 type = AV_OPT_FLAG_AUDIO_PARAM;
4133 if (opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {
4134 fprintf(stderr, "AVOption error: %s %s\n", arg, arg2);
4137 } else if (!strcasecmp(cmd, "VideoTag")) {
4138 get_arg(arg, sizeof(arg), &p);
4139 if ((strlen(arg) == 4) && stream)
4140 video_enc.codec_tag = ff_get_fourcc(arg);
4141 } else if (!strcasecmp(cmd, "BitExact")) {
4143 video_enc.flags |= CODEC_FLAG_BITEXACT;
4144 } else if (!strcasecmp(cmd, "DctFastint")) {
4146 video_enc.dct_algo = FF_DCT_FASTINT;
4147 } else if (!strcasecmp(cmd, "IdctSimple")) {
4149 video_enc.idct_algo = FF_IDCT_SIMPLE;
4150 } else if (!strcasecmp(cmd, "Qscale")) {
4151 get_arg(arg, sizeof(arg), &p);
4153 video_enc.flags |= CODEC_FLAG_QSCALE;
4154 video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
4156 } else if (!strcasecmp(cmd, "VideoQDiff")) {
4157 get_arg(arg, sizeof(arg), &p);
4159 video_enc.max_qdiff = atoi(arg);
4160 if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
4161 fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
4162 filename, line_num);
4166 } else if (!strcasecmp(cmd, "VideoQMax")) {
4167 get_arg(arg, sizeof(arg), &p);
4169 video_enc.qmax = atoi(arg);
4170 if (video_enc.qmax < 1 || video_enc.qmax > 31) {
4171 fprintf(stderr, "%s:%d: VideoQMax out of range\n",
4172 filename, line_num);
4176 } else if (!strcasecmp(cmd, "VideoQMin")) {
4177 get_arg(arg, sizeof(arg), &p);
4179 video_enc.qmin = atoi(arg);
4180 if (video_enc.qmin < 1 || video_enc.qmin > 31) {
4181 fprintf(stderr, "%s:%d: VideoQMin out of range\n",
4182 filename, line_num);
4186 } else if (!strcasecmp(cmd, "LumaElim")) {
4187 get_arg(arg, sizeof(arg), &p);
4189 video_enc.luma_elim_threshold = atoi(arg);
4190 } else if (!strcasecmp(cmd, "ChromaElim")) {
4191 get_arg(arg, sizeof(arg), &p);
4193 video_enc.chroma_elim_threshold = atoi(arg);
4194 } else if (!strcasecmp(cmd, "LumiMask")) {
4195 get_arg(arg, sizeof(arg), &p);
4197 video_enc.lumi_masking = atof(arg);
4198 } else if (!strcasecmp(cmd, "DarkMask")) {
4199 get_arg(arg, sizeof(arg), &p);
4201 video_enc.dark_masking = atof(arg);
4202 } else if (!strcasecmp(cmd, "NoVideo")) {
4203 video_id = CODEC_ID_NONE;
4204 } else if (!strcasecmp(cmd, "NoAudio")) {
4205 audio_id = CODEC_ID_NONE;
4206 } else if (!strcasecmp(cmd, "ACL")) {
4209 get_arg(arg, sizeof(arg), &p);
4210 if (strcasecmp(arg, "allow") == 0)
4211 acl.action = IP_ALLOW;
4212 else if (strcasecmp(arg, "deny") == 0)
4213 acl.action = IP_DENY;
4215 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4216 filename, line_num, arg);
4220 get_arg(arg, sizeof(arg), &p);
4222 if (resolve_host(&acl.first, arg) != 0) {
4223 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4224 filename, line_num, arg);
4227 acl.last = acl.first;
4229 get_arg(arg, sizeof(arg), &p);
4232 if (resolve_host(&acl.last, arg) != 0) {
4233 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4234 filename, line_num, arg);
4240 IPAddressACL *nacl = av_mallocz(sizeof(*nacl));
4241 IPAddressACL **naclp = 0;
4247 naclp = &stream->acl;
4251 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
4252 filename, line_num);
4258 naclp = &(*naclp)->next;
4263 } else if (!strcasecmp(cmd, "RTSPOption")) {
4264 get_arg(arg, sizeof(arg), &p);
4266 av_freep(&stream->rtsp_option);
4267 stream->rtsp_option = av_strdup(arg);
4269 } else if (!strcasecmp(cmd, "MulticastAddress")) {
4270 get_arg(arg, sizeof(arg), &p);
4272 if (resolve_host(&stream->multicast_ip, arg) != 0) {
4273 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
4274 filename, line_num, arg);
4277 stream->is_multicast = 1;
4278 stream->loop = 1; /* default is looping */
4280 } else if (!strcasecmp(cmd, "MulticastPort")) {
4281 get_arg(arg, sizeof(arg), &p);
4283 stream->multicast_port = atoi(arg);
4284 } else if (!strcasecmp(cmd, "MulticastTTL")) {
4285 get_arg(arg, sizeof(arg), &p);
4287 stream->multicast_ttl = atoi(arg);
4288 } else if (!strcasecmp(cmd, "NoLoop")) {
4291 } else if (!strcasecmp(cmd, "</Stream>")) {
4293 fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
4294 filename, line_num);
4297 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
4298 if (audio_id != CODEC_ID_NONE) {
4299 audio_enc.codec_type = CODEC_TYPE_AUDIO;
4300 audio_enc.codec_id = audio_id;
4301 add_codec(stream, &audio_enc);
4303 if (video_id != CODEC_ID_NONE) {
4304 video_enc.codec_type = CODEC_TYPE_VIDEO;
4305 video_enc.codec_id = video_id;
4306 add_codec(stream, &video_enc);
4311 } else if (!strcasecmp(cmd, "<Redirect")) {
4312 /*********************************************/
4314 if (stream || feed || redirect) {
4315 fprintf(stderr, "%s:%d: Already in a tag\n",
4316 filename, line_num);
4319 redirect = av_mallocz(sizeof(FFStream));
4320 *last_stream = redirect;
4321 last_stream = &redirect->next;
4323 get_arg(redirect->filename, sizeof(redirect->filename), &p);
4324 q = strrchr(redirect->filename, '>');
4327 redirect->stream_type = STREAM_TYPE_REDIRECT;
4329 } else if (!strcasecmp(cmd, "URL")) {
4331 get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
4332 } else if (!strcasecmp(cmd, "</Redirect>")) {
4334 fprintf(stderr, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4335 filename, line_num);
4338 if (!redirect->feed_filename[0]) {
4339 fprintf(stderr, "%s:%d: No URL found for <Redirect>\n",
4340 filename, line_num);
4345 } else if (!strcasecmp(cmd, "LoadModule")) {
4346 get_arg(arg, sizeof(arg), &p);
4350 fprintf(stderr, "%s:%d: Module support not compiled into this version: '%s'\n",
4351 filename, line_num, arg);
4355 fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
4356 filename, line_num, cmd);
4368 static void handle_child_exit(int sig)
4373 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
4376 for (feed = first_feed; feed; feed = feed->next) {
4377 if (feed->pid == pid) {
4378 int uptime = time(0) - feed->pid_start;
4381 fprintf(stderr, "%s: Pid %d exited with status %d after %d seconds\n", feed->filename, pid, status, uptime);
4384 /* Turn off any more restarts */
4385 feed->child_argv = 0;
4390 need_to_start_children = 1;
4393 static void opt_debug()
4396 ffserver_daemon = 0;
4397 logfilename[0] = '-';
4400 static void opt_show_help(void)
4402 printf("usage: ffserver [options]\n"
4403 "Hyper fast multi format Audio/Video streaming server\n");
4405 show_help_options(options, "Main options:\n", 0, 0);
4408 static const OptionDef options[] = {
4409 { "h", OPT_EXIT, {(void*)opt_show_help}, "show help" },
4410 { "version", OPT_EXIT, {(void*)show_version}, "show version" },
4411 { "L", OPT_EXIT, {(void*)show_license}, "show license" },
4412 { "formats", OPT_EXIT, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
4413 { "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
4414 { "d", 0, {(void*)opt_debug}, "enable debug mode" },
4415 { "f", HAS_ARG | OPT_STRING, {(void*)&config_filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
4419 int main(int argc, char **argv)
4421 struct sigaction sigact;
4427 config_filename = "/etc/ffserver.conf";
4429 my_program_name = argv[0];
4430 my_program_dir = getcwd(0, 0);
4431 ffserver_daemon = 1;
4433 parse_options(argc, argv, options, NULL);
4435 unsetenv("http_proxy"); /* Kill the http_proxy */
4437 av_init_random(av_gettime() + (getpid() << 16), &random_state);
4439 /* address on which the server will handle HTTP connections */
4440 my_http_addr.sin_family = AF_INET;
4441 my_http_addr.sin_port = htons (8080);
4442 my_http_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4444 /* address on which the server will handle RTSP connections */
4445 my_rtsp_addr.sin_family = AF_INET;
4446 my_rtsp_addr.sin_port = htons (5454);
4447 my_rtsp_addr.sin_addr.s_addr = htonl (INADDR_ANY);
4449 nb_max_connections = 5;
4450 max_bandwidth = 1000;
4451 first_stream = NULL;
4453 memset(&sigact, 0, sizeof(sigact));
4454 sigact.sa_handler = handle_child_exit;
4455 sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
4456 sigaction(SIGCHLD, &sigact, 0);
4458 if (parse_ffconfig(config_filename) < 0) {
4459 fprintf(stderr, "Incorrect config file - exiting.\n");
4463 build_file_streams();
4465 build_feed_streams();
4467 compute_bandwidth();
4469 /* put the process in background and detach it from its TTY */
4470 if (ffserver_daemon) {
4477 } else if (pid > 0) {
4485 open("/dev/null", O_RDWR);
4486 if (strcmp(logfilename, "-") != 0) {
4496 signal(SIGPIPE, SIG_IGN);
4498 /* open log file if needed */
4499 if (logfilename[0] != '\0') {
4500 if (!strcmp(logfilename, "-"))
4503 logfile = fopen(logfilename, "a");
4506 if (http_server() < 0) {
4507 http_log("Could not start server\n");