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
22 #define _XOPEN_SOURCE 600
26 #define closesocket close
31 /* avformat.h defines LIBAVFORMAT_BUILD, include it before all the other libav* headers which use it */
32 #include "libavformat/avformat.h"
33 #include "libavformat/network.h"
34 #include "libavformat/os_support.h"
35 #include "libavformat/rtpdec.h"
36 #include "libavformat/rtsp.h"
37 #include "libavutil/avstring.h"
38 #include "libavutil/lfg.h"
39 #include "libavutil/random_seed.h"
40 #include "libavutil/intreadwrite.h"
41 #include "libavcodec/opt.h"
45 #include <sys/ioctl.h>
51 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
63 const char program_name[] = "FFserver";
64 const int program_birth_year = 2000;
66 static const OptionDef options[];
69 HTTPSTATE_WAIT_REQUEST,
70 HTTPSTATE_SEND_HEADER,
71 HTTPSTATE_SEND_DATA_HEADER,
72 HTTPSTATE_SEND_DATA, /* sending TCP or UDP data */
73 HTTPSTATE_SEND_DATA_TRAILER,
74 HTTPSTATE_RECEIVE_DATA,
75 HTTPSTATE_WAIT_FEED, /* wait for data from the feed */
78 RTSPSTATE_WAIT_REQUEST,
80 RTSPSTATE_SEND_PACKET,
83 static const char *http_state[] = {
99 #define IOBUFFER_INIT_SIZE 8192
101 /* timeouts are in ms */
102 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
103 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
105 #define SYNC_TIMEOUT (10 * 1000)
107 typedef struct RTSPActionServerSetup {
109 char transport_option[512];
110 } RTSPActionServerSetup;
113 int64_t count1, count2;
114 int64_t time1, time2;
117 /* context associated with one connection */
118 typedef struct HTTPContext {
119 enum HTTPState state;
120 int fd; /* socket file descriptor */
121 struct sockaddr_in from_addr; /* origin */
122 struct pollfd *poll_entry; /* used when polling */
124 uint8_t *buffer_ptr, *buffer_end;
127 struct HTTPContext *next;
128 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
132 /* input format handling */
133 AVFormatContext *fmt_in;
134 int64_t start_time; /* In milliseconds - this wraps fairly often */
135 int64_t first_pts; /* initial pts value */
136 int64_t cur_pts; /* current pts value from the stream in us */
137 int64_t cur_frame_duration; /* duration of the current frame in us */
138 int cur_frame_bytes; /* output frame size, needed to compute
139 the time at which we send each
141 int pts_stream_index; /* stream we choose as clock reference */
142 int64_t cur_clock; /* current clock reference value in us */
143 /* output format handling */
144 struct FFStream *stream;
145 /* -1 is invalid stream */
146 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
147 int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */
149 AVFormatContext fmt_ctx; /* instance of FFStream for one user */
150 int last_packet_sent; /* true if last data packet was sent */
152 DataRateData datarate;
159 int is_packetized; /* if true, the stream is packetized */
160 int packet_stream_index; /* current stream for output in state machine */
162 /* RTSP state specific */
163 uint8_t *pb_buffer; /* XXX: use that in all the code */
165 int seq; /* RTSP sequence number */
167 /* RTP state specific */
168 enum RTSPLowerTransport rtp_protocol;
169 char session_id[32]; /* session id */
170 AVFormatContext *rtp_ctx[MAX_STREAMS];
172 /* RTP/UDP specific */
173 URLContext *rtp_handles[MAX_STREAMS];
175 /* RTP/TCP specific */
176 struct HTTPContext *rtsp_c;
177 uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
180 /* each generated stream is described here */
184 STREAM_TYPE_REDIRECT,
187 enum IPAddressAction {
192 typedef struct IPAddressACL {
193 struct IPAddressACL *next;
194 enum IPAddressAction action;
195 /* These are in host order */
196 struct in_addr first;
200 /* description of each stream of the ffserver.conf file */
201 typedef struct FFStream {
202 enum StreamType stream_type;
203 char filename[1024]; /* stream filename */
204 struct FFStream *feed; /* feed we are using (can be null if
206 AVFormatParameters *ap_in; /* input parameters */
207 AVInputFormat *ifmt; /* if non NULL, force input format */
211 int prebuffer; /* Number of millseconds early to start */
212 int64_t max_time; /* Number of milliseconds to run */
214 AVStream *streams[MAX_STREAMS];
215 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
216 char feed_filename[1024]; /* file name of the feed storage, or
217 input file name for a stream */
222 pid_t pid; /* Of ffmpeg process */
223 time_t pid_start; /* Of ffmpeg process */
225 struct FFStream *next;
226 unsigned bandwidth; /* bandwidth, in kbits/s */
229 /* multicast specific */
231 struct in_addr multicast_ip;
232 int multicast_port; /* first port used for multicast */
234 int loop; /* if true, send the stream in loops (only meaningful if file) */
237 int feed_opened; /* true if someone is writing to the feed */
238 int is_feed; /* true if it is a feed */
239 int readonly; /* True if writing is prohibited to the file */
241 int64_t bytes_served;
242 int64_t feed_max_size; /* maximum storage size, zero means unlimited */
243 int64_t feed_write_index; /* current write position in feed (it wraps around) */
244 int64_t feed_size; /* current size of feed */
245 struct FFStream *next_feed;
248 typedef struct FeedData {
249 long long data_count;
250 float avg_frame_size; /* frame size averaged over last frames with exponential mean */
253 static struct sockaddr_in my_http_addr;
254 static struct sockaddr_in my_rtsp_addr;
256 static char logfilename[1024];
257 static HTTPContext *first_http_ctx;
258 static FFStream *first_feed; /* contains only feeds */
259 static FFStream *first_stream; /* contains all streams, including feeds */
261 static void new_connection(int server_fd, int is_rtsp);
262 static void close_connection(HTTPContext *c);
265 static int handle_connection(HTTPContext *c);
266 static int http_parse_request(HTTPContext *c);
267 static int http_send_data(HTTPContext *c);
268 static void compute_status(HTTPContext *c);
269 static int open_input_stream(HTTPContext *c, const char *info);
270 static int http_start_receive_data(HTTPContext *c);
271 static int http_receive_data(HTTPContext *c);
274 static int rtsp_parse_request(HTTPContext *c);
275 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
276 static void rtsp_cmd_options(HTTPContext *c, const char *url);
277 static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPMessageHeader *h);
278 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPMessageHeader *h);
279 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPMessageHeader *h);
280 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPMessageHeader *h);
283 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
284 struct in_addr my_ip);
287 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
288 FFStream *stream, const char *session_id,
289 enum RTSPLowerTransport rtp_protocol);
290 static int rtp_new_av_stream(HTTPContext *c,
291 int stream_index, struct sockaddr_in *dest_addr,
292 HTTPContext *rtsp_c);
294 static const char *my_program_name;
295 static const char *my_program_dir;
297 static const char *config_filename;
298 static int ffserver_debug;
299 static int ffserver_daemon;
300 static int no_launch;
301 static int need_to_start_children;
303 /* maximum number of simultaneous HTTP connections */
304 static unsigned int nb_max_http_connections = 2000;
305 static unsigned int nb_max_connections = 5;
306 static unsigned int nb_connections;
308 static uint64_t max_bandwidth = 1000;
309 static uint64_t current_bandwidth;
311 static int64_t cur_time; // Making this global saves on passing it around everywhere
313 static AVLFG random_state;
315 static FILE *logfile = NULL;
317 static char *ctime1(char *buf2)
325 p = buf2 + strlen(p) - 1;
331 static void http_vlog(const char *fmt, va_list vargs)
333 static int print_prefix = 1;
338 fprintf(logfile, "%s ", buf);
340 print_prefix = strstr(fmt, "\n") != NULL;
341 vfprintf(logfile, fmt, vargs);
346 void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
349 va_start(vargs, fmt);
350 http_vlog(fmt, vargs);
354 static void http_av_log(void *ptr, int level, const char *fmt, va_list vargs)
356 static int print_prefix = 1;
357 AVClass *avc = ptr ? *(AVClass**)ptr : NULL;
358 if (level > av_log_get_level())
360 if (print_prefix && avc)
361 http_log("[%s @ %p]", avc->item_name(ptr), ptr);
362 print_prefix = strstr(fmt, "\n") != NULL;
363 http_vlog(fmt, vargs);
366 static void log_connection(HTTPContext *c)
371 http_log("%s - - [%s] \"%s %s\" %d %"PRId64"\n",
372 inet_ntoa(c->from_addr.sin_addr), c->method, c->url,
373 c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
376 static void update_datarate(DataRateData *drd, int64_t count)
378 if (!drd->time1 && !drd->count1) {
379 drd->time1 = drd->time2 = cur_time;
380 drd->count1 = drd->count2 = count;
381 } else if (cur_time - drd->time2 > 5000) {
382 drd->time1 = drd->time2;
383 drd->count1 = drd->count2;
384 drd->time2 = cur_time;
389 /* In bytes per second */
390 static int compute_datarate(DataRateData *drd, int64_t count)
392 if (cur_time == drd->time1)
395 return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
399 static void start_children(FFStream *feed)
404 for (; feed; feed = feed->next) {
405 if (feed->child_argv && !feed->pid) {
406 feed->pid_start = time(0);
411 http_log("Unable to create children\n");
420 av_strlcpy(pathname, my_program_name, sizeof(pathname));
422 slash = strrchr(pathname, '/');
427 strcpy(slash, "ffmpeg");
429 http_log("Launch commandline: ");
430 http_log("%s ", pathname);
431 for (i = 1; feed->child_argv[i] && feed->child_argv[i][0]; i++)
432 http_log("%s ", feed->child_argv[i]);
435 for (i = 3; i < 256; i++)
438 if (!ffserver_debug) {
439 i = open("/dev/null", O_RDWR);
448 /* This is needed to make relative pathnames work */
449 chdir(my_program_dir);
451 signal(SIGPIPE, SIG_DFL);
453 execvp(pathname, feed->child_argv);
461 /* open a listening socket */
462 static int socket_open_listen(struct sockaddr_in *my_addr)
466 server_fd = socket(AF_INET,SOCK_STREAM,0);
473 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
475 if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
477 snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
479 closesocket(server_fd);
483 if (listen (server_fd, 5) < 0) {
485 closesocket(server_fd);
488 ff_socket_nonblock(server_fd, 1);
493 /* start all multicast streams */
494 static void start_multicast(void)
499 struct sockaddr_in dest_addr;
500 int default_port, stream_index;
503 for(stream = first_stream; stream != NULL; stream = stream->next) {
504 if (stream->is_multicast) {
505 /* open the RTP connection */
506 snprintf(session_id, sizeof(session_id), "%08x%08x",
507 av_lfg_get(&random_state), av_lfg_get(&random_state));
509 /* choose a port if none given */
510 if (stream->multicast_port == 0) {
511 stream->multicast_port = default_port;
515 dest_addr.sin_family = AF_INET;
516 dest_addr.sin_addr = stream->multicast_ip;
517 dest_addr.sin_port = htons(stream->multicast_port);
519 rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
520 RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
524 if (open_input_stream(rtp_c, "") < 0) {
525 http_log("Could not open input stream for stream '%s'\n",
530 /* open each RTP stream */
531 for(stream_index = 0; stream_index < stream->nb_streams;
533 dest_addr.sin_port = htons(stream->multicast_port +
535 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) < 0) {
536 http_log("Could not open output stream '%s/streamid=%d'\n",
537 stream->filename, stream_index);
542 /* change state to send data */
543 rtp_c->state = HTTPSTATE_SEND_DATA;
548 /* main loop of the http server */
549 static int http_server(void)
551 int server_fd = 0, rtsp_server_fd = 0;
552 int ret, delay, delay1;
553 struct pollfd *poll_table, *poll_entry;
554 HTTPContext *c, *c_next;
556 if(!(poll_table = av_mallocz((nb_max_http_connections + 2)*sizeof(*poll_table)))) {
557 http_log("Impossible to allocate a poll table handling %d connections.\n", nb_max_http_connections);
561 if (my_http_addr.sin_port) {
562 server_fd = socket_open_listen(&my_http_addr);
567 if (my_rtsp_addr.sin_port) {
568 rtsp_server_fd = socket_open_listen(&my_rtsp_addr);
569 if (rtsp_server_fd < 0)
573 if (!rtsp_server_fd && !server_fd) {
574 http_log("HTTP and RTSP disabled.\n");
578 http_log("FFserver started.\n");
580 start_children(first_feed);
585 poll_entry = poll_table;
587 poll_entry->fd = server_fd;
588 poll_entry->events = POLLIN;
591 if (rtsp_server_fd) {
592 poll_entry->fd = rtsp_server_fd;
593 poll_entry->events = POLLIN;
597 /* wait for events on each HTTP handle */
604 case HTTPSTATE_SEND_HEADER:
605 case RTSPSTATE_SEND_REPLY:
606 case RTSPSTATE_SEND_PACKET:
607 c->poll_entry = poll_entry;
609 poll_entry->events = POLLOUT;
612 case HTTPSTATE_SEND_DATA_HEADER:
613 case HTTPSTATE_SEND_DATA:
614 case HTTPSTATE_SEND_DATA_TRAILER:
615 if (!c->is_packetized) {
616 /* for TCP, we output as much as we can (may need to put a limit) */
617 c->poll_entry = poll_entry;
619 poll_entry->events = POLLOUT;
622 /* when ffserver is doing the timing, we work by
623 looking at which packet need to be sent every
625 delay1 = 10; /* one tick wait XXX: 10 ms assumed */
630 case HTTPSTATE_WAIT_REQUEST:
631 case HTTPSTATE_RECEIVE_DATA:
632 case HTTPSTATE_WAIT_FEED:
633 case RTSPSTATE_WAIT_REQUEST:
634 /* need to catch errors */
635 c->poll_entry = poll_entry;
637 poll_entry->events = POLLIN;/* Maybe this will work */
641 c->poll_entry = NULL;
647 /* wait for an event on one connection. We poll at least every
648 second to handle timeouts */
650 ret = poll(poll_table, poll_entry - poll_table, delay);
651 if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
652 ff_neterrno() != FF_NETERROR(EINTR))
656 cur_time = av_gettime() / 1000;
658 if (need_to_start_children) {
659 need_to_start_children = 0;
660 start_children(first_feed);
663 /* now handle the events */
664 for(c = first_http_ctx; c != NULL; c = c_next) {
666 if (handle_connection(c) < 0) {
667 /* close and free the connection */
673 poll_entry = poll_table;
675 /* new HTTP connection request ? */
676 if (poll_entry->revents & POLLIN)
677 new_connection(server_fd, 0);
680 if (rtsp_server_fd) {
681 /* new RTSP connection request ? */
682 if (poll_entry->revents & POLLIN)
683 new_connection(rtsp_server_fd, 1);
688 /* start waiting for a new HTTP/RTSP request */
689 static void start_wait_request(HTTPContext *c, int is_rtsp)
691 c->buffer_ptr = c->buffer;
692 c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
695 c->timeout = cur_time + RTSP_REQUEST_TIMEOUT;
696 c->state = RTSPSTATE_WAIT_REQUEST;
698 c->timeout = cur_time + HTTP_REQUEST_TIMEOUT;
699 c->state = HTTPSTATE_WAIT_REQUEST;
703 static void new_connection(int server_fd, int is_rtsp)
705 struct sockaddr_in from_addr;
707 HTTPContext *c = NULL;
709 len = sizeof(from_addr);
710 fd = accept(server_fd, (struct sockaddr *)&from_addr,
713 http_log("error during accept %s\n", strerror(errno));
716 ff_socket_nonblock(fd, 1);
718 /* XXX: should output a warning page when coming
719 close to the connection limit */
720 if (nb_connections >= nb_max_connections)
723 /* add a new connection */
724 c = av_mallocz(sizeof(HTTPContext));
729 c->poll_entry = NULL;
730 c->from_addr = from_addr;
731 c->buffer_size = IOBUFFER_INIT_SIZE;
732 c->buffer = av_malloc(c->buffer_size);
736 c->next = first_http_ctx;
740 start_wait_request(c, is_rtsp);
752 static void close_connection(HTTPContext *c)
754 HTTPContext **cp, *c1;
756 AVFormatContext *ctx;
760 /* remove connection from list */
761 cp = &first_http_ctx;
762 while ((*cp) != NULL) {
770 /* remove references, if any (XXX: do it faster) */
771 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
776 /* remove connection associated resources */
780 /* close each frame parser */
781 for(i=0;i<c->fmt_in->nb_streams;i++) {
782 st = c->fmt_in->streams[i];
783 if (st->codec->codec)
784 avcodec_close(st->codec);
786 av_close_input_file(c->fmt_in);
789 /* free RTP output streams if any */
792 nb_streams = c->stream->nb_streams;
794 for(i=0;i<nb_streams;i++) {
797 av_write_trailer(ctx);
800 h = c->rtp_handles[i];
807 if (!c->last_packet_sent && c->state == HTTPSTATE_SEND_DATA_TRAILER) {
810 if (url_open_dyn_buf(&ctx->pb) >= 0) {
811 av_write_trailer(ctx);
812 av_freep(&c->pb_buffer);
813 url_close_dyn_buf(ctx->pb, &c->pb_buffer);
818 for(i=0; i<ctx->nb_streams; i++)
819 av_free(ctx->streams[i]);
821 if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
822 current_bandwidth -= c->stream->bandwidth;
824 /* signal that there is no feed if we are the feeder socket */
825 if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
826 c->stream->feed_opened = 0;
830 av_freep(&c->pb_buffer);
831 av_freep(&c->packet_buffer);
837 static int handle_connection(HTTPContext *c)
842 case HTTPSTATE_WAIT_REQUEST:
843 case RTSPSTATE_WAIT_REQUEST:
845 if ((c->timeout - cur_time) < 0)
847 if (c->poll_entry->revents & (POLLERR | POLLHUP))
850 /* no need to read if no events */
851 if (!(c->poll_entry->revents & POLLIN))
855 len = recv(c->fd, c->buffer_ptr, 1, 0);
857 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
858 ff_neterrno() != FF_NETERROR(EINTR))
860 } else if (len == 0) {
863 /* search for end of request. */
865 c->buffer_ptr += len;
867 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
868 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
869 /* request found : parse it and reply */
870 if (c->state == HTTPSTATE_WAIT_REQUEST) {
871 ret = http_parse_request(c);
873 ret = rtsp_parse_request(c);
877 } else if (ptr >= c->buffer_end) {
878 /* request too long: cannot do anything */
880 } else goto read_loop;
884 case HTTPSTATE_SEND_HEADER:
885 if (c->poll_entry->revents & (POLLERR | POLLHUP))
888 /* no need to write if no events */
889 if (!(c->poll_entry->revents & POLLOUT))
891 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
893 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
894 ff_neterrno() != FF_NETERROR(EINTR)) {
895 /* error : close connection */
896 av_freep(&c->pb_buffer);
900 c->buffer_ptr += len;
902 c->stream->bytes_served += len;
903 c->data_count += len;
904 if (c->buffer_ptr >= c->buffer_end) {
905 av_freep(&c->pb_buffer);
909 /* all the buffer was sent : synchronize to the incoming stream */
910 c->state = HTTPSTATE_SEND_DATA_HEADER;
911 c->buffer_ptr = c->buffer_end = c->buffer;
916 case HTTPSTATE_SEND_DATA:
917 case HTTPSTATE_SEND_DATA_HEADER:
918 case HTTPSTATE_SEND_DATA_TRAILER:
919 /* for packetized output, we consider we can always write (the
920 input streams sets the speed). It may be better to verify
921 that we do not rely too much on the kernel queues */
922 if (!c->is_packetized) {
923 if (c->poll_entry->revents & (POLLERR | POLLHUP))
926 /* no need to read if no events */
927 if (!(c->poll_entry->revents & POLLOUT))
930 if (http_send_data(c) < 0)
932 /* close connection if trailer sent */
933 if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
936 case HTTPSTATE_RECEIVE_DATA:
937 /* no need to read if no events */
938 if (c->poll_entry->revents & (POLLERR | POLLHUP))
940 if (!(c->poll_entry->revents & POLLIN))
942 if (http_receive_data(c) < 0)
945 case HTTPSTATE_WAIT_FEED:
946 /* no need to read if no events */
947 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
950 /* nothing to do, we'll be waken up by incoming feed packets */
953 case RTSPSTATE_SEND_REPLY:
954 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
955 av_freep(&c->pb_buffer);
958 /* no need to write if no events */
959 if (!(c->poll_entry->revents & POLLOUT))
961 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
963 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
964 ff_neterrno() != FF_NETERROR(EINTR)) {
965 /* error : close connection */
966 av_freep(&c->pb_buffer);
970 c->buffer_ptr += len;
971 c->data_count += len;
972 if (c->buffer_ptr >= c->buffer_end) {
973 /* all the buffer was sent : wait for a new request */
974 av_freep(&c->pb_buffer);
975 start_wait_request(c, 1);
979 case RTSPSTATE_SEND_PACKET:
980 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
981 av_freep(&c->packet_buffer);
984 /* no need to write if no events */
985 if (!(c->poll_entry->revents & POLLOUT))
987 len = send(c->fd, c->packet_buffer_ptr,
988 c->packet_buffer_end - c->packet_buffer_ptr, 0);
990 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
991 ff_neterrno() != FF_NETERROR(EINTR)) {
992 /* error : close connection */
993 av_freep(&c->packet_buffer);
997 c->packet_buffer_ptr += len;
998 if (c->packet_buffer_ptr >= c->packet_buffer_end) {
999 /* all the buffer was sent : wait for a new request */
1000 av_freep(&c->packet_buffer);
1001 c->state = RTSPSTATE_WAIT_REQUEST;
1005 case HTTPSTATE_READY:
1014 static int extract_rates(char *rates, int ratelen, const char *request)
1018 for (p = request; *p && *p != '\r' && *p != '\n'; ) {
1019 if (strncasecmp(p, "Pragma:", 7) == 0) {
1020 const char *q = p + 7;
1022 while (*q && *q != '\n' && isspace(*q))
1025 if (strncasecmp(q, "stream-switch-entry=", 20) == 0) {
1031 memset(rates, 0xff, ratelen);
1034 while (*q && *q != '\n' && *q != ':')
1037 if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
1041 if (stream_no < ratelen && stream_no >= 0)
1042 rates[stream_no] = rate_no;
1044 while (*q && *q != '\n' && !isspace(*q))
1051 p = strchr(p, '\n');
1061 static int find_stream_in_feed(FFStream *feed, AVCodecContext *codec, int bit_rate)
1064 int best_bitrate = 100000000;
1067 for (i = 0; i < feed->nb_streams; i++) {
1068 AVCodecContext *feed_codec = feed->streams[i]->codec;
1070 if (feed_codec->codec_id != codec->codec_id ||
1071 feed_codec->sample_rate != codec->sample_rate ||
1072 feed_codec->width != codec->width ||
1073 feed_codec->height != codec->height)
1076 /* Potential stream */
1078 /* We want the fastest stream less than bit_rate, or the slowest
1079 * faster than bit_rate
1082 if (feed_codec->bit_rate <= bit_rate) {
1083 if (best_bitrate > bit_rate || feed_codec->bit_rate > best_bitrate) {
1084 best_bitrate = feed_codec->bit_rate;
1088 if (feed_codec->bit_rate < best_bitrate) {
1089 best_bitrate = feed_codec->bit_rate;
1098 static int modify_current_stream(HTTPContext *c, char *rates)
1101 FFStream *req = c->stream;
1102 int action_required = 0;
1104 /* Not much we can do for a feed */
1108 for (i = 0; i < req->nb_streams; i++) {
1109 AVCodecContext *codec = req->streams[i]->codec;
1113 c->switch_feed_streams[i] = req->feed_streams[i];
1116 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1119 /* Wants off or slow */
1120 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1122 /* This doesn't work well when it turns off the only stream! */
1123 c->switch_feed_streams[i] = -2;
1124 c->feed_streams[i] = -2;
1129 if (c->switch_feed_streams[i] >= 0 && c->switch_feed_streams[i] != c->feed_streams[i])
1130 action_required = 1;
1133 return action_required;
1137 static void do_switch_stream(HTTPContext *c, int i)
1139 if (c->switch_feed_streams[i] >= 0) {
1141 c->feed_streams[i] = c->switch_feed_streams[i];
1144 /* Now update the stream */
1146 c->switch_feed_streams[i] = -1;
1149 /* XXX: factorize in utils.c ? */
1150 /* XXX: take care with different space meaning */
1151 static void skip_spaces(const char **pp)
1155 while (*p == ' ' || *p == '\t')
1160 static void get_word(char *buf, int buf_size, const char **pp)
1168 while (!isspace(*p) && *p != '\0') {
1169 if ((q - buf) < buf_size - 1)
1178 static int validate_acl(FFStream *stream, HTTPContext *c)
1180 enum IPAddressAction last_action = IP_DENY;
1182 struct in_addr *src = &c->from_addr.sin_addr;
1183 unsigned long src_addr = src->s_addr;
1185 for (acl = stream->acl; acl; acl = acl->next) {
1186 if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
1187 return (acl->action == IP_ALLOW) ? 1 : 0;
1188 last_action = acl->action;
1191 /* Nothing matched, so return not the last action */
1192 return (last_action == IP_DENY) ? 1 : 0;
1195 /* compute the real filename of a file by matching it without its
1196 extensions to all the stream filenames */
1197 static void compute_real_filename(char *filename, int max_size)
1204 /* compute filename by matching without the file extensions */
1205 av_strlcpy(file1, filename, sizeof(file1));
1206 p = strrchr(file1, '.');
1209 for(stream = first_stream; stream != NULL; stream = stream->next) {
1210 av_strlcpy(file2, stream->filename, sizeof(file2));
1211 p = strrchr(file2, '.');
1214 if (!strcmp(file1, file2)) {
1215 av_strlcpy(filename, stream->filename, max_size);
1230 /* parse http request and prepare header */
1231 static int http_parse_request(HTTPContext *c)
1234 enum RedirType redir_type;
1236 char info[1024], filename[1024];
1240 const char *mime_type;
1244 char *useragent = 0;
1247 get_word(cmd, sizeof(cmd), (const char **)&p);
1248 av_strlcpy(c->method, cmd, sizeof(c->method));
1250 if (!strcmp(cmd, "GET"))
1252 else if (!strcmp(cmd, "POST"))
1257 get_word(url, sizeof(url), (const char **)&p);
1258 av_strlcpy(c->url, url, sizeof(c->url));
1260 get_word(protocol, sizeof(protocol), (const char **)&p);
1261 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1264 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
1267 http_log("%s - - New connection: %s %s\n", inet_ntoa(c->from_addr.sin_addr), cmd, url);
1269 /* find the filename and the optional info string in the request */
1270 p = strchr(url, '?');
1272 av_strlcpy(info, p, sizeof(info));
1277 av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
1279 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1280 if (strncasecmp(p, "User-Agent:", 11) == 0) {
1282 if (*useragent && *useragent != '\n' && isspace(*useragent))
1286 p = strchr(p, '\n');
1293 redir_type = REDIR_NONE;
1294 if (match_ext(filename, "asx")) {
1295 redir_type = REDIR_ASX;
1296 filename[strlen(filename)-1] = 'f';
1297 } else if (match_ext(filename, "asf") &&
1298 (!useragent || strncasecmp(useragent, "NSPlayer", 8) != 0)) {
1299 /* if this isn't WMP or lookalike, return the redirector file */
1300 redir_type = REDIR_ASF;
1301 } else if (match_ext(filename, "rpm,ram")) {
1302 redir_type = REDIR_RAM;
1303 strcpy(filename + strlen(filename)-2, "m");
1304 } else if (match_ext(filename, "rtsp")) {
1305 redir_type = REDIR_RTSP;
1306 compute_real_filename(filename, sizeof(filename) - 1);
1307 } else if (match_ext(filename, "sdp")) {
1308 redir_type = REDIR_SDP;
1309 compute_real_filename(filename, sizeof(filename) - 1);
1312 // "redirect" / request to index.html
1313 if (!strlen(filename))
1314 av_strlcpy(filename, "index.html", sizeof(filename) - 1);
1316 stream = first_stream;
1317 while (stream != NULL) {
1318 if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
1320 stream = stream->next;
1322 if (stream == NULL) {
1323 snprintf(msg, sizeof(msg), "File '%s' not found", url);
1324 http_log("File '%s' not found\n", url);
1329 memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1330 memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1332 if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1333 c->http_error = 301;
1335 q += snprintf(q, c->buffer_size,
1336 "HTTP/1.0 301 Moved\r\n"
1338 "Content-type: text/html\r\n"
1340 "<html><head><title>Moved</title></head><body>\r\n"
1341 "You should be <a href=\"%s\">redirected</a>.\r\n"
1342 "</body></html>\r\n", stream->feed_filename, stream->feed_filename);
1343 /* prepare output buffer */
1344 c->buffer_ptr = c->buffer;
1346 c->state = HTTPSTATE_SEND_HEADER;
1350 /* If this is WMP, get the rate information */
1351 if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1352 if (modify_current_stream(c, ratebuf)) {
1353 for (i = 0; i < FF_ARRAY_ELEMS(c->feed_streams); i++) {
1354 if (c->switch_feed_streams[i] >= 0)
1355 do_switch_stream(c, i);
1360 if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
1361 current_bandwidth += stream->bandwidth;
1363 /* If already streaming this feed, do not let start another feeder. */
1364 if (stream->feed_opened) {
1365 snprintf(msg, sizeof(msg), "This feed is already being received.");
1366 http_log("Feed '%s' already being received\n", stream->feed_filename);
1370 if (c->post == 0 && max_bandwidth < current_bandwidth) {
1371 c->http_error = 200;
1373 q += snprintf(q, c->buffer_size,
1374 "HTTP/1.0 200 Server too busy\r\n"
1375 "Content-type: text/html\r\n"
1377 "<html><head><title>Too busy</title></head><body>\r\n"
1378 "<p>The server is too busy to serve your request at this time.</p>\r\n"
1379 "<p>The bandwidth being served (including your stream) is %"PRIu64"kbit/sec, "
1380 "and this exceeds the limit of %"PRIu64"kbit/sec.</p>\r\n"
1381 "</body></html>\r\n", current_bandwidth, max_bandwidth);
1382 /* prepare output buffer */
1383 c->buffer_ptr = c->buffer;
1385 c->state = HTTPSTATE_SEND_HEADER;
1389 if (redir_type != REDIR_NONE) {
1392 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1393 if (strncasecmp(p, "Host:", 5) == 0) {
1397 p = strchr(p, '\n');
1408 while (isspace(*hostinfo))
1411 eoh = strchr(hostinfo, '\n');
1413 if (eoh[-1] == '\r')
1416 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1417 memcpy(hostbuf, hostinfo, eoh - hostinfo);
1418 hostbuf[eoh - hostinfo] = 0;
1420 c->http_error = 200;
1422 switch(redir_type) {
1424 q += snprintf(q, c->buffer_size,
1425 "HTTP/1.0 200 ASX Follows\r\n"
1426 "Content-type: video/x-ms-asf\r\n"
1428 "<ASX Version=\"3\">\r\n"
1429 //"<!-- Autogenerated by ffserver -->\r\n"
1430 "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n"
1431 "</ASX>\r\n", hostbuf, filename, info);
1434 q += snprintf(q, c->buffer_size,
1435 "HTTP/1.0 200 RAM Follows\r\n"
1436 "Content-type: audio/x-pn-realaudio\r\n"
1438 "# Autogenerated by ffserver\r\n"
1439 "http://%s/%s%s\r\n", hostbuf, filename, info);
1442 q += snprintf(q, c->buffer_size,
1443 "HTTP/1.0 200 ASF Redirect follows\r\n"
1444 "Content-type: video/x-ms-asf\r\n"
1447 "Ref1=http://%s/%s%s\r\n", hostbuf, filename, info);
1451 char hostname[256], *p;
1452 /* extract only hostname */
1453 av_strlcpy(hostname, hostbuf, sizeof(hostname));
1454 p = strrchr(hostname, ':');
1457 q += snprintf(q, c->buffer_size,
1458 "HTTP/1.0 200 RTSP Redirect follows\r\n"
1459 /* XXX: incorrect mime type ? */
1460 "Content-type: application/x-rtsp\r\n"
1462 "rtsp://%s:%d/%s\r\n", hostname, ntohs(my_rtsp_addr.sin_port), filename);
1468 int sdp_data_size, len;
1469 struct sockaddr_in my_addr;
1471 q += snprintf(q, c->buffer_size,
1472 "HTTP/1.0 200 OK\r\n"
1473 "Content-type: application/sdp\r\n"
1476 len = sizeof(my_addr);
1477 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
1479 /* XXX: should use a dynamic buffer */
1480 sdp_data_size = prepare_sdp_description(stream,
1483 if (sdp_data_size > 0) {
1484 memcpy(q, sdp_data, sdp_data_size);
1496 /* prepare output buffer */
1497 c->buffer_ptr = c->buffer;
1499 c->state = HTTPSTATE_SEND_HEADER;
1505 snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
1509 stream->conns_served++;
1511 /* XXX: add there authenticate and IP match */
1514 /* if post, it means a feed is being sent */
1515 if (!stream->is_feed) {
1516 /* However it might be a status report from WMP! Let us log the
1517 * data as it might come in handy one day. */
1521 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1522 if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1526 if (strncasecmp(p, "Pragma: client-id=", 18) == 0)
1527 client_id = strtol(p + 18, 0, 10);
1528 p = strchr(p, '\n');
1536 char *eol = strchr(logline, '\n');
1541 if (eol[-1] == '\r')
1543 http_log("%.*s\n", (int) (eol - logline), logline);
1544 c->suppress_log = 1;
1549 http_log("\nGot request:\n%s\n", c->buffer);
1552 if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1555 /* Now we have to find the client_id */
1556 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1557 if (wmpc->wmp_client_id == client_id)
1561 if (wmpc && modify_current_stream(wmpc, ratebuf))
1562 wmpc->switch_pending = 1;
1565 snprintf(msg, sizeof(msg), "POST command not handled");
1569 if (http_start_receive_data(c) < 0) {
1570 snprintf(msg, sizeof(msg), "could not open feed");
1574 c->state = HTTPSTATE_RECEIVE_DATA;
1579 if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
1580 http_log("\nGot request:\n%s\n", c->buffer);
1583 if (c->stream->stream_type == STREAM_TYPE_STATUS)
1586 /* open input stream */
1587 if (open_input_stream(c, info) < 0) {
1588 snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1592 /* prepare http header */
1594 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1595 mime_type = c->stream->fmt->mime_type;
1597 mime_type = "application/x-octet-stream";
1598 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
1600 /* for asf, we need extra headers */
1601 if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1602 /* Need to allocate a client id */
1604 c->wmp_client_id = av_lfg_get(&random_state);
1606 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);
1608 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-Type: %s\r\n", mime_type);
1609 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1611 /* prepare output buffer */
1613 c->buffer_ptr = c->buffer;
1615 c->state = HTTPSTATE_SEND_HEADER;
1618 c->http_error = 404;
1620 q += snprintf(q, c->buffer_size,
1621 "HTTP/1.0 404 Not Found\r\n"
1622 "Content-type: text/html\r\n"
1625 "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n"
1628 /* prepare output buffer */
1629 c->buffer_ptr = c->buffer;
1631 c->state = HTTPSTATE_SEND_HEADER;
1635 c->http_error = 200; /* horrible : we use this value to avoid
1636 going to the send data state */
1637 c->state = HTTPSTATE_SEND_HEADER;
1641 static void fmt_bytecount(ByteIOContext *pb, int64_t count)
1643 static const char *suffix = " kMGTP";
1646 for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
1648 url_fprintf(pb, "%"PRId64"%c", count, *s);
1651 static void compute_status(HTTPContext *c)
1660 if (url_open_dyn_buf(&pb) < 0) {
1661 /* XXX: return an error ? */
1662 c->buffer_ptr = c->buffer;
1663 c->buffer_end = c->buffer;
1667 url_fprintf(pb, "HTTP/1.0 200 OK\r\n");
1668 url_fprintf(pb, "Content-type: %s\r\n", "text/html");
1669 url_fprintf(pb, "Pragma: no-cache\r\n");
1670 url_fprintf(pb, "\r\n");
1672 url_fprintf(pb, "<HTML><HEAD><TITLE>%s Status</TITLE>\n", program_name);
1673 if (c->stream->feed_filename[0])
1674 url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
1675 url_fprintf(pb, "</HEAD>\n<BODY>");
1676 url_fprintf(pb, "<H1>%s Status</H1>\n", program_name);
1678 url_fprintf(pb, "<H2>Available Streams</H2>\n");
1679 url_fprintf(pb, "<TABLE cellspacing=0 cellpadding=4>\n");
1680 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");
1681 stream = first_stream;
1682 while (stream != NULL) {
1683 char sfilename[1024];
1686 if (stream->feed != stream) {
1687 av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
1688 eosf = sfilename + strlen(sfilename);
1689 if (eosf - sfilename >= 4) {
1690 if (strcmp(eosf - 4, ".asf") == 0)
1691 strcpy(eosf - 4, ".asx");
1692 else if (strcmp(eosf - 3, ".rm") == 0)
1693 strcpy(eosf - 3, ".ram");
1694 else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
1695 /* generate a sample RTSP director if
1696 unicast. Generate an SDP redirector if
1698 eosf = strrchr(sfilename, '.');
1700 eosf = sfilename + strlen(sfilename);
1701 if (stream->is_multicast)
1702 strcpy(eosf, ".sdp");
1704 strcpy(eosf, ".rtsp");
1708 url_fprintf(pb, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1709 sfilename, stream->filename);
1710 url_fprintf(pb, "<td align=right> %d <td align=right> ",
1711 stream->conns_served);
1712 fmt_bytecount(pb, stream->bytes_served);
1713 switch(stream->stream_type) {
1714 case STREAM_TYPE_LIVE: {
1715 int audio_bit_rate = 0;
1716 int video_bit_rate = 0;
1717 const char *audio_codec_name = "";
1718 const char *video_codec_name = "";
1719 const char *audio_codec_name_extra = "";
1720 const char *video_codec_name_extra = "";
1722 for(i=0;i<stream->nb_streams;i++) {
1723 AVStream *st = stream->streams[i];
1724 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1725 switch(st->codec->codec_type) {
1726 case CODEC_TYPE_AUDIO:
1727 audio_bit_rate += st->codec->bit_rate;
1729 if (*audio_codec_name)
1730 audio_codec_name_extra = "...";
1731 audio_codec_name = codec->name;
1734 case CODEC_TYPE_VIDEO:
1735 video_bit_rate += st->codec->bit_rate;
1737 if (*video_codec_name)
1738 video_codec_name_extra = "...";
1739 video_codec_name = codec->name;
1742 case CODEC_TYPE_DATA:
1743 video_bit_rate += st->codec->bit_rate;
1749 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",
1752 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
1753 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
1755 url_fprintf(pb, "<TD>%s", stream->feed->filename);
1757 url_fprintf(pb, "<TD>%s", stream->feed_filename);
1758 url_fprintf(pb, "\n");
1762 url_fprintf(pb, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1766 stream = stream->next;
1768 url_fprintf(pb, "</TABLE>\n");
1770 stream = first_stream;
1771 while (stream != NULL) {
1772 if (stream->feed == stream) {
1773 url_fprintf(pb, "<h2>Feed %s</h2>", stream->filename);
1775 url_fprintf(pb, "Running as pid %d.\n", stream->pid);
1777 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1782 /* This is somewhat linux specific I guess */
1783 snprintf(ps_cmd, sizeof(ps_cmd),
1784 "ps -o \"%%cpu,cputime\" --no-headers %d",
1787 pid_stat = popen(ps_cmd, "r");
1792 if (fscanf(pid_stat, "%10s %64s", cpuperc,
1794 url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",
1802 url_fprintf(pb, "<p>");
1804 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");
1806 for (i = 0; i < stream->nb_streams; i++) {
1807 AVStream *st = stream->streams[i];
1808 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1809 const char *type = "unknown";
1810 char parameters[64];
1814 switch(st->codec->codec_type) {
1815 case CODEC_TYPE_AUDIO:
1817 snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
1819 case CODEC_TYPE_VIDEO:
1821 snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,
1822 st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);
1827 url_fprintf(pb, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1828 i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);
1830 url_fprintf(pb, "</table>\n");
1833 stream = stream->next;
1839 AVCodecContext *enc;
1843 stream = first_feed;
1844 while (stream != NULL) {
1845 url_fprintf(pb, "<H1>Feed '%s'</H1>\n", stream->filename);
1846 url_fprintf(pb, "<TABLE>\n");
1847 url_fprintf(pb, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1848 for(i=0;i<stream->nb_streams;i++) {
1849 AVStream *st = stream->streams[i];
1850 FeedData *fdata = st->priv_data;
1853 avcodec_string(buf, sizeof(buf), enc);
1854 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
1855 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
1856 avg /= enc->frame_size;
1857 url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
1858 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
1860 url_fprintf(pb, "</TABLE>\n");
1861 stream = stream->next_feed;
1866 /* connection status */
1867 url_fprintf(pb, "<H2>Connection Status</H2>\n");
1869 url_fprintf(pb, "Number of connections: %d / %d<BR>\n",
1870 nb_connections, nb_max_connections);
1872 url_fprintf(pb, "Bandwidth in use: %"PRIu64"k / %"PRIu64"k<BR>\n",
1873 current_bandwidth, max_bandwidth);
1875 url_fprintf(pb, "<TABLE>\n");
1876 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");
1877 c1 = first_http_ctx;
1879 while (c1 != NULL) {
1885 for (j = 0; j < c1->stream->nb_streams; j++) {
1886 if (!c1->stream->feed)
1887 bitrate += c1->stream->streams[j]->codec->bit_rate;
1888 else if (c1->feed_streams[j] >= 0)
1889 bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
1894 p = inet_ntoa(c1->from_addr.sin_addr);
1895 url_fprintf(pb, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1897 c1->stream ? c1->stream->filename : "",
1898 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
1901 http_state[c1->state]);
1902 fmt_bytecount(pb, bitrate);
1903 url_fprintf(pb, "<td align=right>");
1904 fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
1905 url_fprintf(pb, "<td align=right>");
1906 fmt_bytecount(pb, c1->data_count);
1907 url_fprintf(pb, "\n");
1910 url_fprintf(pb, "</TABLE>\n");
1915 url_fprintf(pb, "<HR size=1 noshade>Generated at %s", p);
1916 url_fprintf(pb, "</BODY>\n</HTML>\n");
1918 len = url_close_dyn_buf(pb, &c->pb_buffer);
1919 c->buffer_ptr = c->pb_buffer;
1920 c->buffer_end = c->pb_buffer + len;
1923 /* check if the parser needs to be opened for stream i */
1924 static void open_parser(AVFormatContext *s, int i)
1926 AVStream *st = s->streams[i];
1929 if (!st->codec->codec) {
1930 codec = avcodec_find_decoder(st->codec->codec_id);
1931 if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
1932 st->codec->parse_only = 1;
1933 if (avcodec_open(st->codec, codec) < 0)
1934 st->codec->parse_only = 0;
1939 static int open_input_stream(HTTPContext *c, const char *info)
1942 char input_filename[1024];
1944 int buf_size, i, ret;
1947 /* find file name */
1948 if (c->stream->feed) {
1949 strcpy(input_filename, c->stream->feed->feed_filename);
1950 buf_size = FFM_PACKET_SIZE;
1951 /* compute position (absolute time) */
1952 if (find_info_tag(buf, sizeof(buf), "date", info)) {
1953 stream_pos = parse_date(buf, 0);
1954 if (stream_pos == INT64_MIN)
1956 } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
1957 int prebuffer = strtol(buf, 0, 10);
1958 stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
1960 stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
1962 strcpy(input_filename, c->stream->feed_filename);
1964 /* compute position (relative time) */
1965 if (find_info_tag(buf, sizeof(buf), "date", info)) {
1966 stream_pos = parse_date(buf, 1);
1967 if (stream_pos == INT64_MIN)
1972 if (input_filename[0] == '\0')
1976 { time_t when = stream_pos / 1000000;
1977 http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
1982 if ((ret = av_open_input_file(&s, input_filename, c->stream->ifmt,
1983 buf_size, c->stream->ap_in)) < 0) {
1984 http_log("could not open %s: %d\n", input_filename, ret);
1987 s->flags |= AVFMT_FLAG_GENPTS;
1989 if (av_find_stream_info(c->fmt_in) < 0) {
1990 http_log("Could not find stream info '%s'\n", input_filename);
1991 av_close_input_file(s);
1995 /* open each parser */
1996 for(i=0;i<s->nb_streams;i++)
1999 /* choose stream as clock source (we favorize video stream if
2000 present) for packet sending */
2001 c->pts_stream_index = 0;
2002 for(i=0;i<c->stream->nb_streams;i++) {
2003 if (c->pts_stream_index == 0 &&
2004 c->stream->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
2005 c->pts_stream_index = i;
2010 if (c->fmt_in->iformat->read_seek)
2011 av_seek_frame(c->fmt_in, -1, stream_pos, 0);
2013 /* set the start time (needed for maxtime and RTP packet timing) */
2014 c->start_time = cur_time;
2015 c->first_pts = AV_NOPTS_VALUE;
2019 /* return the server clock (in us) */
2020 static int64_t get_server_clock(HTTPContext *c)
2022 /* compute current pts value from system time */
2023 return (cur_time - c->start_time) * 1000;
2026 /* return the estimated time at which the current packet must be sent
2028 static int64_t get_packet_send_clock(HTTPContext *c)
2030 int bytes_left, bytes_sent, frame_bytes;
2032 frame_bytes = c->cur_frame_bytes;
2033 if (frame_bytes <= 0)
2036 bytes_left = c->buffer_end - c->buffer_ptr;
2037 bytes_sent = frame_bytes - bytes_left;
2038 return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
2043 static int http_prepare_data(HTTPContext *c)
2046 AVFormatContext *ctx;
2048 av_freep(&c->pb_buffer);
2050 case HTTPSTATE_SEND_DATA_HEADER:
2051 memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
2052 av_metadata_set(&c->fmt_ctx.metadata, "author" ,c->stream->author);
2053 av_metadata_set(&c->fmt_ctx.metadata, "comment" ,c->stream->comment);
2054 av_metadata_set(&c->fmt_ctx.metadata, "copyright",c->stream->copyright);
2055 av_metadata_set(&c->fmt_ctx.metadata, "title" ,c->stream->title);
2057 for(i=0;i<c->stream->nb_streams;i++) {
2060 st = av_mallocz(sizeof(AVStream));
2061 c->fmt_ctx.streams[i] = st;
2062 /* if file or feed, then just take streams from FFStream struct */
2063 if (!c->stream->feed ||
2064 c->stream->feed == c->stream)
2065 src = c->stream->streams[i];
2067 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2071 st->codec->frame_number = 0; /* XXX: should be done in
2072 AVStream, not in codec */
2074 /* set output format parameters */
2075 c->fmt_ctx.oformat = c->stream->fmt;
2076 c->fmt_ctx.nb_streams = c->stream->nb_streams;
2078 c->got_key_frame = 0;
2080 /* prepare header and save header data in a stream */
2081 if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2082 /* XXX: potential leak */
2085 c->fmt_ctx.pb->is_streamed = 1;
2088 * HACK to avoid mpeg ps muxer to spit many underflow errors
2089 * Default value from FFmpeg
2090 * Try to set it use configuration option
2092 c->fmt_ctx.preload = (int)(0.5*AV_TIME_BASE);
2093 c->fmt_ctx.max_delay = (int)(0.7*AV_TIME_BASE);
2095 av_set_parameters(&c->fmt_ctx, NULL);
2096 if (av_write_header(&c->fmt_ctx) < 0) {
2097 http_log("Error writing output header\n");
2101 len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2102 c->buffer_ptr = c->pb_buffer;
2103 c->buffer_end = c->pb_buffer + len;
2105 c->state = HTTPSTATE_SEND_DATA;
2106 c->last_packet_sent = 0;
2108 case HTTPSTATE_SEND_DATA:
2109 /* find a new packet */
2110 /* read a packet from the input stream */
2111 if (c->stream->feed)
2112 ffm_set_write_index(c->fmt_in,
2113 c->stream->feed->feed_write_index,
2114 c->stream->feed->feed_size);
2116 if (c->stream->max_time &&
2117 c->stream->max_time + c->start_time - cur_time < 0)
2118 /* We have timed out */
2119 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2123 if (av_read_frame(c->fmt_in, &pkt) < 0) {
2124 if (c->stream->feed && c->stream->feed->feed_opened) {
2125 /* if coming from feed, it means we reached the end of the
2126 ffm file, so must wait for more data */
2127 c->state = HTTPSTATE_WAIT_FEED;
2128 return 1; /* state changed */
2130 if (c->stream->loop) {
2131 av_close_input_file(c->fmt_in);
2133 if (open_input_stream(c, "") < 0)
2138 /* must send trailer now because eof or error */
2139 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2143 int source_index = pkt.stream_index;
2144 /* update first pts if needed */
2145 if (c->first_pts == AV_NOPTS_VALUE) {
2146 c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2147 c->start_time = cur_time;
2149 /* send it to the appropriate stream */
2150 if (c->stream->feed) {
2151 /* if coming from a feed, select the right stream */
2152 if (c->switch_pending) {
2153 c->switch_pending = 0;
2154 for(i=0;i<c->stream->nb_streams;i++) {
2155 if (c->switch_feed_streams[i] == pkt.stream_index)
2156 if (pkt.flags & PKT_FLAG_KEY)
2157 do_switch_stream(c, i);
2158 if (c->switch_feed_streams[i] >= 0)
2159 c->switch_pending = 1;
2162 for(i=0;i<c->stream->nb_streams;i++) {
2163 if (c->feed_streams[i] == pkt.stream_index) {
2164 AVStream *st = c->fmt_in->streams[source_index];
2165 pkt.stream_index = i;
2166 if (pkt.flags & PKT_FLAG_KEY &&
2167 (st->codec->codec_type == CODEC_TYPE_VIDEO ||
2168 c->stream->nb_streams == 1))
2169 c->got_key_frame = 1;
2170 if (!c->stream->send_on_key || c->got_key_frame)
2175 AVCodecContext *codec;
2176 AVStream *ist, *ost;
2178 ist = c->fmt_in->streams[source_index];
2179 /* specific handling for RTP: we use several
2180 output stream (one for each RTP
2181 connection). XXX: need more abstract handling */
2182 if (c->is_packetized) {
2183 /* compute send time and duration */
2184 c->cur_pts = av_rescale_q(pkt.dts, ist->time_base, AV_TIME_BASE_Q);
2185 if (ist->start_time != AV_NOPTS_VALUE)
2186 c->cur_pts -= av_rescale_q(ist->start_time, ist->time_base, AV_TIME_BASE_Q);
2187 c->cur_frame_duration = av_rescale_q(pkt.duration, ist->time_base, AV_TIME_BASE_Q);
2189 printf("index=%d pts=%0.3f duration=%0.6f\n",
2191 (double)c->cur_pts /
2193 (double)c->cur_frame_duration /
2196 /* find RTP context */
2197 c->packet_stream_index = pkt.stream_index;
2198 ctx = c->rtp_ctx[c->packet_stream_index];
2200 av_free_packet(&pkt);
2203 codec = ctx->streams[0]->codec;
2204 /* only one stream per RTP connection */
2205 pkt.stream_index = 0;
2209 codec = ctx->streams[pkt.stream_index]->codec;
2212 if (c->is_packetized) {
2213 int max_packet_size;
2214 if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP)
2215 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2217 max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);
2218 ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);
2220 ret = url_open_dyn_buf(&ctx->pb);
2223 /* XXX: potential leak */
2226 ost = ctx->streams[pkt.stream_index];
2228 ctx->pb->is_streamed = 1;
2229 if (pkt.dts != AV_NOPTS_VALUE)
2230 pkt.dts = av_rescale_q(pkt.dts, ist->time_base, ost->time_base);
2231 if (pkt.pts != AV_NOPTS_VALUE)
2232 pkt.pts = av_rescale_q(pkt.pts, ist->time_base, ost->time_base);
2233 pkt.duration = av_rescale_q(pkt.duration, ist->time_base, ost->time_base);
2234 if (av_write_frame(ctx, &pkt) < 0) {
2235 http_log("Error writing frame to output\n");
2236 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2239 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2240 c->cur_frame_bytes = len;
2241 c->buffer_ptr = c->pb_buffer;
2242 c->buffer_end = c->pb_buffer + len;
2244 codec->frame_number++;
2246 av_free_packet(&pkt);
2250 av_free_packet(&pkt);
2255 case HTTPSTATE_SEND_DATA_TRAILER:
2256 /* last packet test ? */
2257 if (c->last_packet_sent || c->is_packetized)
2260 /* prepare header */
2261 if (url_open_dyn_buf(&ctx->pb) < 0) {
2262 /* XXX: potential leak */
2265 c->fmt_ctx.pb->is_streamed = 1;
2266 av_write_trailer(ctx);
2267 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2268 c->buffer_ptr = c->pb_buffer;
2269 c->buffer_end = c->pb_buffer + len;
2271 c->last_packet_sent = 1;
2277 /* should convert the format at the same time */
2278 /* send data starting at c->buffer_ptr to the output connection
2279 (either UDP or TCP connection) */
2280 static int http_send_data(HTTPContext *c)
2285 if (c->buffer_ptr >= c->buffer_end) {
2286 ret = http_prepare_data(c);
2290 /* state change requested */
2293 if (c->is_packetized) {
2294 /* RTP data output */
2295 len = c->buffer_end - c->buffer_ptr;
2297 /* fail safe - should never happen */
2299 c->buffer_ptr = c->buffer_end;
2302 len = (c->buffer_ptr[0] << 24) |
2303 (c->buffer_ptr[1] << 16) |
2304 (c->buffer_ptr[2] << 8) |
2306 if (len > (c->buffer_end - c->buffer_ptr))
2308 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2309 /* nothing to send yet: we can wait */
2313 c->data_count += len;
2314 update_datarate(&c->datarate, c->data_count);
2316 c->stream->bytes_served += len;
2318 if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP) {
2319 /* RTP packets are sent inside the RTSP TCP connection */
2321 int interleaved_index, size;
2323 HTTPContext *rtsp_c;
2326 /* if no RTSP connection left, error */
2329 /* if already sending something, then wait. */
2330 if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
2332 if (url_open_dyn_buf(&pb) < 0)
2334 interleaved_index = c->packet_stream_index * 2;
2335 /* RTCP packets are sent at odd indexes */
2336 if (c->buffer_ptr[1] == 200)
2337 interleaved_index++;
2338 /* write RTSP TCP header */
2340 header[1] = interleaved_index;
2341 header[2] = len >> 8;
2343 put_buffer(pb, header, 4);
2344 /* write RTP packet data */
2346 put_buffer(pb, c->buffer_ptr, len);
2347 size = url_close_dyn_buf(pb, &c->packet_buffer);
2348 /* prepare asynchronous TCP sending */
2349 rtsp_c->packet_buffer_ptr = c->packet_buffer;
2350 rtsp_c->packet_buffer_end = c->packet_buffer + size;
2351 c->buffer_ptr += len;
2353 /* send everything we can NOW */
2354 len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2355 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2357 rtsp_c->packet_buffer_ptr += len;
2358 if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2359 /* if we could not send all the data, we will
2360 send it later, so a new state is needed to
2361 "lock" the RTSP TCP connection */
2362 rtsp_c->state = RTSPSTATE_SEND_PACKET;
2365 /* all data has been sent */
2366 av_freep(&c->packet_buffer);
2368 /* send RTP packet directly in UDP */
2370 url_write(c->rtp_handles[c->packet_stream_index],
2371 c->buffer_ptr, len);
2372 c->buffer_ptr += len;
2373 /* here we continue as we can send several packets per 10 ms slot */
2376 /* TCP data output */
2377 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2379 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2380 ff_neterrno() != FF_NETERROR(EINTR))
2381 /* error : close connection */
2386 c->buffer_ptr += len;
2388 c->data_count += len;
2389 update_datarate(&c->datarate, c->data_count);
2391 c->stream->bytes_served += len;
2399 static int http_start_receive_data(HTTPContext *c)
2403 if (c->stream->feed_opened)
2406 /* Don't permit writing to this one */
2407 if (c->stream->readonly)
2411 fd = open(c->stream->feed_filename, O_RDWR);
2413 http_log("Error opening feeder file: %s\n", strerror(errno));
2418 if ((c->stream->feed_write_index = ffm_read_write_index(fd)) < 0) {
2419 http_log("Error reading write index from feed file: %s\n", strerror(errno));
2422 c->stream->feed_write_index = FFMAX(ffm_read_write_index(fd), FFM_PACKET_SIZE);
2424 c->stream->feed_size = lseek(fd, 0, SEEK_END);
2425 lseek(fd, 0, SEEK_SET);
2427 /* init buffer input */
2428 c->buffer_ptr = c->buffer;
2429 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2430 c->stream->feed_opened = 1;
2434 static int http_receive_data(HTTPContext *c)
2438 if (c->buffer_end > c->buffer_ptr) {
2441 len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2443 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
2444 ff_neterrno() != FF_NETERROR(EINTR))
2445 /* error : close connection */
2447 } else if (len == 0)
2448 /* end of connection : close it */
2451 c->buffer_ptr += len;
2452 c->data_count += len;
2453 update_datarate(&c->datarate, c->data_count);
2457 if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2458 if (c->buffer[0] != 'f' ||
2459 c->buffer[1] != 'm') {
2460 http_log("Feed stream has become desynchronized -- disconnecting\n");
2465 if (c->buffer_ptr >= c->buffer_end) {
2466 FFStream *feed = c->stream;
2467 /* a packet has been received : write it in the store, except
2469 if (c->data_count > FFM_PACKET_SIZE) {
2471 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2472 /* XXX: use llseek or url_seek */
2473 lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
2474 if (write(c->feed_fd, c->buffer, FFM_PACKET_SIZE) < 0) {
2475 http_log("Error writing to feed file: %s\n", strerror(errno));
2479 feed->feed_write_index += FFM_PACKET_SIZE;
2480 /* update file size */
2481 if (feed->feed_write_index > c->stream->feed_size)
2482 feed->feed_size = feed->feed_write_index;
2484 /* handle wrap around if max file size reached */
2485 if (c->stream->feed_max_size && feed->feed_write_index >= c->stream->feed_max_size)
2486 feed->feed_write_index = FFM_PACKET_SIZE;
2489 if (ffm_write_write_index(c->feed_fd, feed->feed_write_index) < 0) {
2490 http_log("Error writing index to feed file: %s\n", strerror(errno));
2494 /* wake up any waiting connections */
2495 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2496 if (c1->state == HTTPSTATE_WAIT_FEED &&
2497 c1->stream->feed == c->stream->feed)
2498 c1->state = HTTPSTATE_SEND_DATA;
2501 /* We have a header in our hands that contains useful data */
2502 AVFormatContext *s = NULL;
2504 AVInputFormat *fmt_in;
2507 /* use feed output format name to find corresponding input format */
2508 fmt_in = av_find_input_format(feed->fmt->name);
2512 url_open_buf(&pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
2513 pb->is_streamed = 1;
2515 if (av_open_input_stream(&s, pb, c->stream->feed_filename, fmt_in, NULL) < 0) {
2520 /* Now we have the actual streams */
2521 if (s->nb_streams != feed->nb_streams) {
2522 av_close_input_stream(s);
2524 http_log("Feed '%s' stream number does not match registered feed\n",
2525 c->stream->feed_filename);
2529 for (i = 0; i < s->nb_streams; i++) {
2530 AVStream *fst = feed->streams[i];
2531 AVStream *st = s->streams[i];
2532 memcpy(fst->codec, st->codec, sizeof(AVCodecContext));
2533 if (fst->codec->extradata_size) {
2534 fst->codec->extradata = av_malloc(fst->codec->extradata_size);
2535 if (!fst->codec->extradata)
2537 memcpy(fst->codec->extradata, st->codec->extradata,
2538 fst->codec->extradata_size);
2542 av_close_input_stream(s);
2545 c->buffer_ptr = c->buffer;
2550 c->stream->feed_opened = 0;
2552 /* wake up any waiting connections to stop waiting for feed */
2553 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
2554 if (c1->state == HTTPSTATE_WAIT_FEED &&
2555 c1->stream->feed == c->stream->feed)
2556 c1->state = HTTPSTATE_SEND_DATA_TRAILER;
2561 /********************************************************************/
2564 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2571 switch(error_number) {
2572 case RTSP_STATUS_OK:
2575 case RTSP_STATUS_METHOD:
2576 str = "Method Not Allowed";
2578 case RTSP_STATUS_BANDWIDTH:
2579 str = "Not Enough Bandwidth";
2581 case RTSP_STATUS_SESSION:
2582 str = "Session Not Found";
2584 case RTSP_STATUS_STATE:
2585 str = "Method Not Valid in This State";
2587 case RTSP_STATUS_AGGREGATE:
2588 str = "Aggregate operation not allowed";
2590 case RTSP_STATUS_ONLY_AGGREGATE:
2591 str = "Only aggregate operation allowed";
2593 case RTSP_STATUS_TRANSPORT:
2594 str = "Unsupported transport";
2596 case RTSP_STATUS_INTERNAL:
2597 str = "Internal Server Error";
2599 case RTSP_STATUS_SERVICE:
2600 str = "Service Unavailable";
2602 case RTSP_STATUS_VERSION:
2603 str = "RTSP Version not supported";
2606 str = "Unknown Error";
2610 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2611 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2613 /* output GMT time */
2617 p = buf2 + strlen(p) - 1;
2620 url_fprintf(c->pb, "Date: %s GMT\r\n", buf2);
2623 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2625 rtsp_reply_header(c, error_number);
2626 url_fprintf(c->pb, "\r\n");
2629 static int rtsp_parse_request(HTTPContext *c)
2631 const char *p, *p1, *p2;
2637 RTSPMessageHeader header1, *header = &header1;
2639 c->buffer_ptr[0] = '\0';
2642 get_word(cmd, sizeof(cmd), &p);
2643 get_word(url, sizeof(url), &p);
2644 get_word(protocol, sizeof(protocol), &p);
2646 av_strlcpy(c->method, cmd, sizeof(c->method));
2647 av_strlcpy(c->url, url, sizeof(c->url));
2648 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2650 if (url_open_dyn_buf(&c->pb) < 0) {
2651 /* XXX: cannot do more */
2652 c->pb = NULL; /* safety */
2656 /* check version name */
2657 if (strcmp(protocol, "RTSP/1.0") != 0) {
2658 rtsp_reply_error(c, RTSP_STATUS_VERSION);
2662 /* parse each header line */
2663 memset(header, 0, sizeof(*header));
2664 /* skip to next line */
2665 while (*p != '\n' && *p != '\0')
2669 while (*p != '\0') {
2670 p1 = strchr(p, '\n');
2674 if (p2 > p && p2[-1] == '\r')
2676 /* skip empty line */
2680 if (len > sizeof(line) - 1)
2681 len = sizeof(line) - 1;
2682 memcpy(line, p, len);
2684 rtsp_parse_line(header, line);
2688 /* handle sequence number */
2689 c->seq = header->seq;
2691 if (!strcmp(cmd, "DESCRIBE"))
2692 rtsp_cmd_describe(c, url);
2693 else if (!strcmp(cmd, "OPTIONS"))
2694 rtsp_cmd_options(c, url);
2695 else if (!strcmp(cmd, "SETUP"))
2696 rtsp_cmd_setup(c, url, header);
2697 else if (!strcmp(cmd, "PLAY"))
2698 rtsp_cmd_play(c, url, header);
2699 else if (!strcmp(cmd, "PAUSE"))
2700 rtsp_cmd_pause(c, url, header);
2701 else if (!strcmp(cmd, "TEARDOWN"))
2702 rtsp_cmd_teardown(c, url, header);
2704 rtsp_reply_error(c, RTSP_STATUS_METHOD);
2707 len = url_close_dyn_buf(c->pb, &c->pb_buffer);
2708 c->pb = NULL; /* safety */
2710 /* XXX: cannot do more */
2713 c->buffer_ptr = c->pb_buffer;
2714 c->buffer_end = c->pb_buffer + len;
2715 c->state = RTSPSTATE_SEND_REPLY;
2719 static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
2720 struct in_addr my_ip)
2722 AVFormatContext *avc;
2723 AVStream avs[MAX_STREAMS];
2726 avc = avformat_alloc_context();
2730 av_metadata_set(&avc->metadata, "title",
2731 stream->title[0] ? stream->title : "No Title");
2732 avc->nb_streams = stream->nb_streams;
2733 if (stream->is_multicast) {
2734 snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2735 inet_ntoa(stream->multicast_ip),
2736 stream->multicast_port, stream->multicast_ttl);
2739 for(i = 0; i < stream->nb_streams; i++) {
2740 avc->streams[i] = &avs[i];
2741 avc->streams[i]->codec = stream->streams[i]->codec;
2743 *pbuffer = av_mallocz(2048);
2744 avf_sdp_create(&avc, 1, *pbuffer, 2048);
2747 return strlen(*pbuffer);
2750 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2752 // rtsp_reply_header(c, RTSP_STATUS_OK);
2753 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2754 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2755 url_fprintf(c->pb, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2756 url_fprintf(c->pb, "\r\n");
2759 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2765 int content_length, len;
2766 struct sockaddr_in my_addr;
2768 /* find which url is asked */
2769 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2774 for(stream = first_stream; stream != NULL; stream = stream->next) {
2775 if (!stream->is_feed &&
2776 stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2777 !strcmp(path, stream->filename)) {
2781 /* no stream found */
2782 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2786 /* prepare the media description in sdp format */
2788 /* get the host IP */
2789 len = sizeof(my_addr);
2790 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2791 content_length = prepare_sdp_description(stream, &content, my_addr.sin_addr);
2792 if (content_length < 0) {
2793 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2796 rtsp_reply_header(c, RTSP_STATUS_OK);
2797 url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
2798 url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
2799 url_fprintf(c->pb, "\r\n");
2800 put_buffer(c->pb, content, content_length);
2803 static HTTPContext *find_rtp_session(const char *session_id)
2807 if (session_id[0] == '\0')
2810 for(c = first_http_ctx; c != NULL; c = c->next) {
2811 if (!strcmp(c->session_id, session_id))
2817 static RTSPTransportField *find_transport(RTSPMessageHeader *h, enum RTSPLowerTransport lower_transport)
2819 RTSPTransportField *th;
2822 for(i=0;i<h->nb_transports;i++) {
2823 th = &h->transports[i];
2824 if (th->lower_transport == lower_transport)
2830 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
2831 RTSPMessageHeader *h)
2834 int stream_index, port;
2839 RTSPTransportField *th;
2840 struct sockaddr_in dest_addr;
2841 RTSPActionServerSetup setup;
2843 /* find which url is asked */
2844 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2849 /* now check each stream */
2850 for(stream = first_stream; stream != NULL; stream = stream->next) {
2851 if (!stream->is_feed &&
2852 stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
2853 /* accept aggregate filenames only if single stream */
2854 if (!strcmp(path, stream->filename)) {
2855 if (stream->nb_streams != 1) {
2856 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
2863 for(stream_index = 0; stream_index < stream->nb_streams;
2865 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2866 stream->filename, stream_index);
2867 if (!strcmp(path, buf))
2872 /* no stream found */
2873 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2877 /* generate session id if needed */
2878 if (h->session_id[0] == '\0')
2879 snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
2880 av_lfg_get(&random_state), av_lfg_get(&random_state));
2882 /* find rtp session, and create it if none found */
2883 rtp_c = find_rtp_session(h->session_id);
2885 /* always prefer UDP */
2886 th = find_transport(h, RTSP_LOWER_TRANSPORT_UDP);
2888 th = find_transport(h, RTSP_LOWER_TRANSPORT_TCP);
2890 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2895 rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
2896 th->lower_transport);
2898 rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
2902 /* open input stream */
2903 if (open_input_stream(rtp_c, "") < 0) {
2904 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2909 /* test if stream is OK (test needed because several SETUP needs
2910 to be done for a given file) */
2911 if (rtp_c->stream != stream) {
2912 rtsp_reply_error(c, RTSP_STATUS_SERVICE);
2916 /* test if stream is already set up */
2917 if (rtp_c->rtp_ctx[stream_index]) {
2918 rtsp_reply_error(c, RTSP_STATUS_STATE);
2922 /* check transport */
2923 th = find_transport(h, rtp_c->rtp_protocol);
2924 if (!th || (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
2925 th->client_port_min <= 0)) {
2926 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2930 /* setup default options */
2931 setup.transport_option[0] = '\0';
2932 dest_addr = rtp_c->from_addr;
2933 dest_addr.sin_port = htons(th->client_port_min);
2936 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2937 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2941 /* now everything is OK, so we can send the connection parameters */
2942 rtsp_reply_header(c, RTSP_STATUS_OK);
2944 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2946 switch(rtp_c->rtp_protocol) {
2947 case RTSP_LOWER_TRANSPORT_UDP:
2948 port = rtp_get_local_port(rtp_c->rtp_handles[stream_index]);
2949 url_fprintf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2950 "client_port=%d-%d;server_port=%d-%d",
2951 th->client_port_min, th->client_port_min + 1,
2954 case RTSP_LOWER_TRANSPORT_TCP:
2955 url_fprintf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2956 stream_index * 2, stream_index * 2 + 1);
2961 if (setup.transport_option[0] != '\0')
2962 url_fprintf(c->pb, ";%s", setup.transport_option);
2963 url_fprintf(c->pb, "\r\n");
2966 url_fprintf(c->pb, "\r\n");
2970 /* find an rtp connection by using the session ID. Check consistency
2972 static HTTPContext *find_rtp_session_with_url(const char *url,
2973 const char *session_id)
2981 rtp_c = find_rtp_session(session_id);
2985 /* find which url is asked */
2986 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2990 if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
2991 for(s=0; s<rtp_c->stream->nb_streams; ++s) {
2992 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2993 rtp_c->stream->filename, s);
2994 if(!strncmp(path, buf, sizeof(buf))) {
2995 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
3002 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPMessageHeader *h)
3006 rtp_c = find_rtp_session_with_url(url, h->session_id);
3008 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3012 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3013 rtp_c->state != HTTPSTATE_WAIT_FEED &&
3014 rtp_c->state != HTTPSTATE_READY) {
3015 rtsp_reply_error(c, RTSP_STATUS_STATE);
3020 /* XXX: seek in stream */
3021 if (h->range_start != AV_NOPTS_VALUE) {
3022 printf("range_start=%0.3f\n", (double)h->range_start / AV_TIME_BASE);
3023 av_seek_frame(rtp_c->fmt_in, -1, h->range_start);
3027 rtp_c->state = HTTPSTATE_SEND_DATA;
3029 /* now everything is OK, so we can send the connection parameters */
3030 rtsp_reply_header(c, RTSP_STATUS_OK);
3032 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3033 url_fprintf(c->pb, "\r\n");
3036 static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPMessageHeader *h)
3040 rtp_c = find_rtp_session_with_url(url, h->session_id);
3042 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3046 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3047 rtp_c->state != HTTPSTATE_WAIT_FEED) {
3048 rtsp_reply_error(c, RTSP_STATUS_STATE);
3052 rtp_c->state = HTTPSTATE_READY;
3053 rtp_c->first_pts = AV_NOPTS_VALUE;
3054 /* now everything is OK, so we can send the connection parameters */
3055 rtsp_reply_header(c, RTSP_STATUS_OK);
3057 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3058 url_fprintf(c->pb, "\r\n");
3061 static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPMessageHeader *h)
3064 char session_id[32];
3066 rtp_c = find_rtp_session_with_url(url, h->session_id);
3068 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3072 av_strlcpy(session_id, rtp_c->session_id, sizeof(session_id));
3074 /* abort the session */
3075 close_connection(rtp_c);
3077 /* now everything is OK, so we can send the connection parameters */
3078 rtsp_reply_header(c, RTSP_STATUS_OK);
3080 url_fprintf(c->pb, "Session: %s\r\n", session_id);
3081 url_fprintf(c->pb, "\r\n");
3085 /********************************************************************/
3088 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3089 FFStream *stream, const char *session_id,
3090 enum RTSPLowerTransport rtp_protocol)
3092 HTTPContext *c = NULL;
3093 const char *proto_str;
3095 /* XXX: should output a warning page when coming
3096 close to the connection limit */
3097 if (nb_connections >= nb_max_connections)
3100 /* add a new connection */
3101 c = av_mallocz(sizeof(HTTPContext));
3106 c->poll_entry = NULL;
3107 c->from_addr = *from_addr;
3108 c->buffer_size = IOBUFFER_INIT_SIZE;
3109 c->buffer = av_malloc(c->buffer_size);
3114 av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
3115 c->state = HTTPSTATE_READY;
3116 c->is_packetized = 1;
3117 c->rtp_protocol = rtp_protocol;
3119 /* protocol is shown in statistics */
3120 switch(c->rtp_protocol) {
3121 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
3122 proto_str = "MCAST";
3124 case RTSP_LOWER_TRANSPORT_UDP:
3127 case RTSP_LOWER_TRANSPORT_TCP:
3134 av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
3135 av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
3137 current_bandwidth += stream->bandwidth;
3139 c->next = first_http_ctx;
3151 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3152 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3154 static int rtp_new_av_stream(HTTPContext *c,
3155 int stream_index, struct sockaddr_in *dest_addr,
3156 HTTPContext *rtsp_c)
3158 AVFormatContext *ctx;
3161 URLContext *h = NULL;
3163 int max_packet_size;
3165 /* now we can open the relevant output stream */
3166 ctx = avformat_alloc_context();
3169 ctx->oformat = guess_format("rtp", NULL, NULL);
3171 st = av_mallocz(sizeof(AVStream));
3174 st->codec= avcodec_alloc_context();
3175 ctx->nb_streams = 1;
3176 ctx->streams[0] = st;
3178 if (!c->stream->feed ||
3179 c->stream->feed == c->stream)
3180 memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3183 c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3185 st->priv_data = NULL;
3187 /* build destination RTP address */
3188 ipaddr = inet_ntoa(dest_addr->sin_addr);
3190 switch(c->rtp_protocol) {
3191 case RTSP_LOWER_TRANSPORT_UDP:
3192 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
3195 /* XXX: also pass as parameter to function ? */
3196 if (c->stream->is_multicast) {
3198 ttl = c->stream->multicast_ttl;
3201 snprintf(ctx->filename, sizeof(ctx->filename),
3202 "rtp://%s:%d?multicast=1&ttl=%d",
3203 ipaddr, ntohs(dest_addr->sin_port), ttl);
3205 snprintf(ctx->filename, sizeof(ctx->filename),
3206 "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3209 if (url_open(&h, ctx->filename, URL_WRONLY) < 0)
3211 c->rtp_handles[stream_index] = h;
3212 max_packet_size = url_get_max_packet_size(h);
3214 case RTSP_LOWER_TRANSPORT_TCP:
3217 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3223 http_log("%s:%d - - \"PLAY %s/streamid=%d %s\"\n",
3224 ipaddr, ntohs(dest_addr->sin_port),
3225 c->stream->filename, stream_index, c->protocol);
3227 /* normally, no packets should be output here, but the packet size may be checked */
3228 if (url_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0) {
3229 /* XXX: close stream */
3232 av_set_parameters(ctx, NULL);
3233 if (av_write_header(ctx) < 0) {
3240 url_close_dyn_buf(ctx->pb, &dummy_buf);
3243 c->rtp_ctx[stream_index] = ctx;
3247 /********************************************************************/
3248 /* ffserver initialization */
3250 static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
3254 fst = av_mallocz(sizeof(AVStream));
3257 fst->codec= avcodec_alloc_context();
3258 fst->priv_data = av_mallocz(sizeof(FeedData));
3259 memcpy(fst->codec, codec, sizeof(AVCodecContext));
3260 fst->index = stream->nb_streams;
3261 av_set_pts_info(fst, 33, 1, 90000);
3262 stream->streams[stream->nb_streams++] = fst;
3266 /* return the stream number in the feed */
3267 static int add_av_stream(FFStream *feed, AVStream *st)
3270 AVCodecContext *av, *av1;
3274 for(i=0;i<feed->nb_streams;i++) {
3275 st = feed->streams[i];
3277 if (av1->codec_id == av->codec_id &&
3278 av1->codec_type == av->codec_type &&
3279 av1->bit_rate == av->bit_rate) {
3281 switch(av->codec_type) {
3282 case CODEC_TYPE_AUDIO:
3283 if (av1->channels == av->channels &&
3284 av1->sample_rate == av->sample_rate)
3287 case CODEC_TYPE_VIDEO:
3288 if (av1->width == av->width &&
3289 av1->height == av->height &&
3290 av1->time_base.den == av->time_base.den &&
3291 av1->time_base.num == av->time_base.num &&
3292 av1->gop_size == av->gop_size)
3301 fst = add_av_stream1(feed, av);
3304 return feed->nb_streams - 1;
3309 static void remove_stream(FFStream *stream)
3313 while (*ps != NULL) {
3321 /* specific mpeg4 handling : we extract the raw parameters */
3322 static void extract_mpeg4_header(AVFormatContext *infile)
3324 int mpeg4_count, i, size;
3330 for(i=0;i<infile->nb_streams;i++) {
3331 st = infile->streams[i];
3332 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3333 st->codec->extradata_size == 0) {
3340 printf("MPEG4 without extra data: trying to find header in %s\n", infile->filename);
3341 while (mpeg4_count > 0) {
3342 if (av_read_packet(infile, &pkt) < 0)
3344 st = infile->streams[pkt.stream_index];
3345 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3346 st->codec->extradata_size == 0) {
3347 av_freep(&st->codec->extradata);
3348 /* fill extradata with the header */
3349 /* XXX: we make hard suppositions here ! */
3351 while (p < pkt.data + pkt.size - 4) {
3352 /* stop when vop header is found */
3353 if (p[0] == 0x00 && p[1] == 0x00 &&
3354 p[2] == 0x01 && p[3] == 0xb6) {
3355 size = p - pkt.data;
3356 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3357 st->codec->extradata = av_malloc(size);
3358 st->codec->extradata_size = size;
3359 memcpy(st->codec->extradata, pkt.data, size);
3366 av_free_packet(&pkt);
3370 /* compute the needed AVStream for each file */
3371 static void build_file_streams(void)
3373 FFStream *stream, *stream_next;
3374 AVFormatContext *infile;
3377 /* gather all streams */
3378 for(stream = first_stream; stream != NULL; stream = stream_next) {
3379 stream_next = stream->next;
3380 if (stream->stream_type == STREAM_TYPE_LIVE &&
3382 /* the stream comes from a file */
3383 /* try to open the file */
3385 stream->ap_in = av_mallocz(sizeof(AVFormatParameters));
3386 if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
3387 /* specific case : if transport stream output to RTP,
3388 we use a raw transport stream reader */
3389 stream->ap_in->mpeg2ts_raw = 1;
3390 stream->ap_in->mpeg2ts_compute_pcr = 1;
3393 http_log("Opening file '%s'\n", stream->feed_filename);
3394 if ((ret = av_open_input_file(&infile, stream->feed_filename,
3395 stream->ifmt, 0, stream->ap_in)) < 0) {
3396 http_log("Could not open '%s': %d\n", stream->feed_filename, ret);
3397 /* remove stream (no need to spend more time on it) */
3399 remove_stream(stream);
3401 /* find all the AVStreams inside and reference them in
3403 if (av_find_stream_info(infile) < 0) {
3404 http_log("Could not find codec parameters from '%s'\n",
3405 stream->feed_filename);
3406 av_close_input_file(infile);
3409 extract_mpeg4_header(infile);
3411 for(i=0;i<infile->nb_streams;i++)
3412 add_av_stream1(stream, infile->streams[i]->codec);
3414 av_close_input_file(infile);
3420 /* compute the needed AVStream for each feed */
3421 static void build_feed_streams(void)
3423 FFStream *stream, *feed;
3426 /* gather all streams */
3427 for(stream = first_stream; stream != NULL; stream = stream->next) {
3428 feed = stream->feed;
3430 if (!stream->is_feed) {
3431 /* we handle a stream coming from a feed */
3432 for(i=0;i<stream->nb_streams;i++)
3433 stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
3438 /* gather all streams */
3439 for(stream = first_stream; stream != NULL; stream = stream->next) {
3440 feed = stream->feed;
3442 if (stream->is_feed) {
3443 for(i=0;i<stream->nb_streams;i++)
3444 stream->feed_streams[i] = i;
3449 /* create feed files if needed */
3450 for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
3453 if (url_exist(feed->feed_filename)) {
3454 /* See if it matches */
3458 if (av_open_input_file(&s, feed->feed_filename, NULL, FFM_PACKET_SIZE, NULL) >= 0) {
3459 /* Now see if it matches */
3460 if (s->nb_streams == feed->nb_streams) {
3462 for(i=0;i<s->nb_streams;i++) {
3464 sf = feed->streams[i];
3467 if (sf->index != ss->index ||
3469 http_log("Index & Id do not match for stream %d (%s)\n",
3470 i, feed->feed_filename);
3473 AVCodecContext *ccf, *ccs;
3477 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3479 if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
3480 http_log("Codecs do not match for stream %d\n", i);
3482 } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3483 http_log("Codec bitrates do not match for stream %d\n", i);
3485 } else if (ccf->codec_type == CODEC_TYPE_VIDEO) {
3486 if (CHECK_CODEC(time_base.den) ||
3487 CHECK_CODEC(time_base.num) ||
3488 CHECK_CODEC(width) ||
3489 CHECK_CODEC(height)) {
3490 http_log("Codec width, height and framerate do not match for stream %d\n", i);
3493 } else if (ccf->codec_type == CODEC_TYPE_AUDIO) {
3494 if (CHECK_CODEC(sample_rate) ||
3495 CHECK_CODEC(channels) ||
3496 CHECK_CODEC(frame_size)) {
3497 http_log("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3501 http_log("Unknown codec type\n");
3509 http_log("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3510 feed->feed_filename, s->nb_streams, feed->nb_streams);
3512 av_close_input_file(s);
3514 http_log("Deleting feed file '%s' as it appears to be corrupt\n",
3515 feed->feed_filename);
3518 if (feed->readonly) {
3519 http_log("Unable to delete feed file '%s' as it is marked readonly\n",
3520 feed->feed_filename);
3523 unlink(feed->feed_filename);
3526 if (!url_exist(feed->feed_filename)) {
3527 AVFormatContext s1 = {0}, *s = &s1;
3529 if (feed->readonly) {
3530 http_log("Unable to create feed file '%s' as it is marked readonly\n",
3531 feed->feed_filename);
3535 /* only write the header of the ffm file */
3536 if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
3537 http_log("Could not open output feed file '%s'\n",
3538 feed->feed_filename);
3541 s->oformat = feed->fmt;
3542 s->nb_streams = feed->nb_streams;
3543 for(i=0;i<s->nb_streams;i++) {
3545 st = feed->streams[i];
3548 av_set_parameters(s, NULL);
3549 if (av_write_header(s) < 0) {
3550 http_log("Container doesn't supports the required parameters\n");
3553 /* XXX: need better api */
3554 av_freep(&s->priv_data);
3557 /* get feed size and write index */
3558 fd = open(feed->feed_filename, O_RDONLY);
3560 http_log("Could not open output feed file '%s'\n",
3561 feed->feed_filename);
3565 feed->feed_write_index = FFMAX(ffm_read_write_index(fd), FFM_PACKET_SIZE);
3566 feed->feed_size = lseek(fd, 0, SEEK_END);
3567 /* ensure that we do not wrap before the end of file */
3568 if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3569 feed->feed_max_size = feed->feed_size;
3575 /* compute the bandwidth used by each stream */
3576 static void compute_bandwidth(void)
3582 for(stream = first_stream; stream != NULL; stream = stream->next) {
3584 for(i=0;i<stream->nb_streams;i++) {
3585 AVStream *st = stream->streams[i];
3586 switch(st->codec->codec_type) {
3587 case CODEC_TYPE_AUDIO:
3588 case CODEC_TYPE_VIDEO:
3589 bandwidth += st->codec->bit_rate;
3595 stream->bandwidth = (bandwidth + 999) / 1000;
3599 static void get_arg(char *buf, int buf_size, const char **pp)
3606 while (isspace(*p)) p++;
3609 if (*p == '\"' || *p == '\'')
3621 if ((q - buf) < buf_size - 1)
3626 if (quote && *p == quote)
3631 /* add a codec and set the default parameters */
3632 static void add_codec(FFStream *stream, AVCodecContext *av)
3636 /* compute default parameters */
3637 switch(av->codec_type) {
3638 case CODEC_TYPE_AUDIO:
3639 if (av->bit_rate == 0)
3640 av->bit_rate = 64000;
3641 if (av->sample_rate == 0)
3642 av->sample_rate = 22050;
3643 if (av->channels == 0)
3646 case CODEC_TYPE_VIDEO:
3647 if (av->bit_rate == 0)
3648 av->bit_rate = 64000;
3649 if (av->time_base.num == 0){
3650 av->time_base.den = 5;
3651 av->time_base.num = 1;
3653 if (av->width == 0 || av->height == 0) {
3657 /* Bitrate tolerance is less for streaming */
3658 if (av->bit_rate_tolerance == 0)
3659 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
3660 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
3665 if (av->max_qdiff == 0)
3667 av->qcompress = 0.5;
3670 if (!av->nsse_weight)
3671 av->nsse_weight = 8;
3673 av->frame_skip_cmp = FF_CMP_DCTMAX;
3674 av->me_method = ME_EPZS;
3675 av->rc_buffer_aggressivity = 1.0;
3678 av->rc_eq = "tex^qComp";
3679 if (!av->i_quant_factor)
3680 av->i_quant_factor = -0.8;
3681 if (!av->b_quant_factor)
3682 av->b_quant_factor = 1.25;
3683 if (!av->b_quant_offset)
3684 av->b_quant_offset = 1.25;
3685 if (!av->rc_max_rate)
3686 av->rc_max_rate = av->bit_rate * 2;
3688 if (av->rc_max_rate && !av->rc_buffer_size) {
3689 av->rc_buffer_size = av->rc_max_rate;
3698 st = av_mallocz(sizeof(AVStream));
3701 st->codec = avcodec_alloc_context();
3702 stream->streams[stream->nb_streams++] = st;
3703 memcpy(st->codec, av, sizeof(AVCodecContext));
3706 static enum CodecID opt_audio_codec(const char *arg)
3708 AVCodec *p= avcodec_find_encoder_by_name(arg);
3710 if (p == NULL || p->type != CODEC_TYPE_AUDIO)
3711 return CODEC_ID_NONE;
3716 static enum CodecID opt_video_codec(const char *arg)
3718 AVCodec *p= avcodec_find_encoder_by_name(arg);
3720 if (p == NULL || p->type != CODEC_TYPE_VIDEO)
3721 return CODEC_ID_NONE;
3726 /* simplistic plugin support */
3729 static void load_module(const char *filename)
3732 void (*init_func)(void);
3733 dll = dlopen(filename, RTLD_NOW);
3735 fprintf(stderr, "Could not load module '%s' - %s\n",
3736 filename, dlerror());
3740 init_func = dlsym(dll, "ffserver_module_init");
3743 "%s: init function 'ffserver_module_init()' not found\n",
3752 static int ffserver_opt_default(const char *opt, const char *arg,
3753 AVCodecContext *avctx, int type)
3756 const AVOption *o = av_find_opt(avctx, opt, NULL, type, type);
3758 ret = av_set_string3(avctx, opt, arg, 1, NULL);
3762 static int parse_ffconfig(const char *filename)
3769 int val, errors, line_num;
3770 FFStream **last_stream, *stream, *redirect;
3771 FFStream **last_feed, *feed, *s;
3772 AVCodecContext audio_enc, video_enc;
3773 enum CodecID audio_id, video_id;
3775 f = fopen(filename, "r");
3783 first_stream = NULL;
3784 last_stream = &first_stream;
3786 last_feed = &first_feed;
3790 audio_id = CODEC_ID_NONE;
3791 video_id = CODEC_ID_NONE;
3793 if (fgets(line, sizeof(line), f) == NULL)
3799 if (*p == '\0' || *p == '#')
3802 get_arg(cmd, sizeof(cmd), &p);
3804 if (!strcasecmp(cmd, "Port")) {
3805 get_arg(arg, sizeof(arg), &p);
3807 if (val < 1 || val > 65536) {
3808 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3809 filename, line_num, arg);
3812 my_http_addr.sin_port = htons(val);
3813 } else if (!strcasecmp(cmd, "BindAddress")) {
3814 get_arg(arg, sizeof(arg), &p);
3815 if (resolve_host(&my_http_addr.sin_addr, arg) != 0) {
3816 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3817 filename, line_num, arg);
3820 } else if (!strcasecmp(cmd, "NoDaemon")) {
3821 ffserver_daemon = 0;
3822 } else if (!strcasecmp(cmd, "RTSPPort")) {
3823 get_arg(arg, sizeof(arg), &p);
3825 if (val < 1 || val > 65536) {
3826 fprintf(stderr, "%s:%d: Invalid port: %s\n",
3827 filename, line_num, arg);
3830 my_rtsp_addr.sin_port = htons(atoi(arg));
3831 } else if (!strcasecmp(cmd, "RTSPBindAddress")) {
3832 get_arg(arg, sizeof(arg), &p);
3833 if (resolve_host(&my_rtsp_addr.sin_addr, arg) != 0) {
3834 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
3835 filename, line_num, arg);
3838 } else if (!strcasecmp(cmd, "MaxHTTPConnections")) {
3839 get_arg(arg, sizeof(arg), &p);
3841 if (val < 1 || val > 65536) {
3842 fprintf(stderr, "%s:%d: Invalid MaxHTTPConnections: %s\n",
3843 filename, line_num, arg);
3846 nb_max_http_connections = val;
3847 } else if (!strcasecmp(cmd, "MaxClients")) {
3848 get_arg(arg, sizeof(arg), &p);
3850 if (val < 1 || val > nb_max_http_connections) {
3851 fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
3852 filename, line_num, arg);
3855 nb_max_connections = val;
3857 } else if (!strcasecmp(cmd, "MaxBandwidth")) {
3859 get_arg(arg, sizeof(arg), &p);
3861 if (llval < 10 || llval > 10000000) {
3862 fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
3863 filename, line_num, arg);
3866 max_bandwidth = llval;
3867 } else if (!strcasecmp(cmd, "CustomLog")) {
3868 if (!ffserver_debug)
3869 get_arg(logfilename, sizeof(logfilename), &p);
3870 } else if (!strcasecmp(cmd, "<Feed")) {
3871 /*********************************************/
3872 /* Feed related options */
3874 if (stream || feed) {
3875 fprintf(stderr, "%s:%d: Already in a tag\n",
3876 filename, line_num);
3878 feed = av_mallocz(sizeof(FFStream));
3879 get_arg(feed->filename, sizeof(feed->filename), &p);
3880 q = strrchr(feed->filename, '>');
3884 for (s = first_feed; s; s = s->next) {
3885 if (!strcmp(feed->filename, s->filename)) {
3886 fprintf(stderr, "%s:%d: Feed '%s' already registered\n",
3887 filename, line_num, s->filename);
3892 feed->fmt = guess_format("ffm", NULL, NULL);
3893 /* defaut feed file */
3894 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
3895 "/tmp/%s.ffm", feed->filename);
3896 feed->feed_max_size = 5 * 1024 * 1024;
3898 feed->feed = feed; /* self feeding :-) */
3900 /* add in stream list */
3901 *last_stream = feed;
3902 last_stream = &feed->next;
3903 /* add in feed list */
3905 last_feed = &feed->next_feed;
3907 } else if (!strcasecmp(cmd, "Launch")) {
3911 feed->child_argv = av_mallocz(64 * sizeof(char *));
3913 for (i = 0; i < 62; i++) {
3914 get_arg(arg, sizeof(arg), &p);
3918 feed->child_argv[i] = av_strdup(arg);
3921 feed->child_argv[i] = av_malloc(30 + strlen(feed->filename));
3923 snprintf(feed->child_argv[i], 30+strlen(feed->filename),
3925 (my_http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
3926 inet_ntoa(my_http_addr.sin_addr),
3927 ntohs(my_http_addr.sin_port), feed->filename);
3929 } else if (!strcasecmp(cmd, "ReadOnlyFile")) {
3931 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3933 } else if (stream) {
3934 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3936 } else if (!strcasecmp(cmd, "File")) {
3938 get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
3940 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
3941 } else if (!strcasecmp(cmd, "FileMaxSize")) {
3946 get_arg(arg, sizeof(arg), &p);
3948 fsize = strtod(p1, &p1);
3949 switch(toupper(*p1)) {
3954 fsize *= 1024 * 1024;
3957 fsize *= 1024 * 1024 * 1024;
3960 feed->feed_max_size = (int64_t)fsize;
3962 } else if (!strcasecmp(cmd, "</Feed>")) {
3964 fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
3965 filename, line_num);
3969 } else if (!strcasecmp(cmd, "<Stream")) {
3970 /*********************************************/
3971 /* Stream related options */
3973 if (stream || feed) {
3974 fprintf(stderr, "%s:%d: Already in a tag\n",
3975 filename, line_num);
3978 const AVClass *class;
3979 stream = av_mallocz(sizeof(FFStream));
3980 get_arg(stream->filename, sizeof(stream->filename), &p);
3981 q = strrchr(stream->filename, '>');
3985 for (s = first_stream; s; s = s->next) {
3986 if (!strcmp(stream->filename, s->filename)) {
3987 fprintf(stderr, "%s:%d: Stream '%s' already registered\n",
3988 filename, line_num, s->filename);
3993 stream->fmt = guess_stream_format(NULL, stream->filename, NULL);
3994 /* fetch avclass so AVOption works
3995 * FIXME try to use avcodec_get_context_defaults2
3996 * without changing defaults too much */
3997 avcodec_get_context_defaults(&video_enc);
3998 class = video_enc.av_class;
3999 memset(&audio_enc, 0, sizeof(AVCodecContext));
4000 memset(&video_enc, 0, sizeof(AVCodecContext));
4001 audio_enc.av_class = class;
4002 video_enc.av_class = class;
4003 audio_id = CODEC_ID_NONE;
4004 video_id = CODEC_ID_NONE;
4006 audio_id = stream->fmt->audio_codec;
4007 video_id = stream->fmt->video_codec;
4010 *last_stream = stream;
4011 last_stream = &stream->next;
4013 } else if (!strcasecmp(cmd, "Feed")) {
4014 get_arg(arg, sizeof(arg), &p);
4019 while (sfeed != NULL) {
4020 if (!strcmp(sfeed->filename, arg))
4022 sfeed = sfeed->next_feed;
4025 fprintf(stderr, "%s:%d: feed '%s' not defined\n",
4026 filename, line_num, arg);
4028 stream->feed = sfeed;
4030 } else if (!strcasecmp(cmd, "Format")) {
4031 get_arg(arg, sizeof(arg), &p);
4033 if (!strcmp(arg, "status")) {
4034 stream->stream_type = STREAM_TYPE_STATUS;
4037 stream->stream_type = STREAM_TYPE_LIVE;
4038 /* jpeg cannot be used here, so use single frame jpeg */
4039 if (!strcmp(arg, "jpeg"))
4040 strcpy(arg, "mjpeg");
4041 stream->fmt = guess_stream_format(arg, NULL, NULL);
4043 fprintf(stderr, "%s:%d: Unknown Format: %s\n",
4044 filename, line_num, arg);
4049 audio_id = stream->fmt->audio_codec;
4050 video_id = stream->fmt->video_codec;
4053 } else if (!strcasecmp(cmd, "InputFormat")) {
4054 get_arg(arg, sizeof(arg), &p);
4056 stream->ifmt = av_find_input_format(arg);
4057 if (!stream->ifmt) {
4058 fprintf(stderr, "%s:%d: Unknown input format: %s\n",
4059 filename, line_num, arg);
4062 } else if (!strcasecmp(cmd, "FaviconURL")) {
4063 if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
4064 get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
4066 fprintf(stderr, "%s:%d: FaviconURL only permitted for status streams\n",
4067 filename, line_num);
4070 } else if (!strcasecmp(cmd, "Author")) {
4072 get_arg(stream->author, sizeof(stream->author), &p);
4073 } else if (!strcasecmp(cmd, "Comment")) {
4075 get_arg(stream->comment, sizeof(stream->comment), &p);
4076 } else if (!strcasecmp(cmd, "Copyright")) {
4078 get_arg(stream->copyright, sizeof(stream->copyright), &p);
4079 } else if (!strcasecmp(cmd, "Title")) {
4081 get_arg(stream->title, sizeof(stream->title), &p);
4082 } else if (!strcasecmp(cmd, "Preroll")) {
4083 get_arg(arg, sizeof(arg), &p);
4085 stream->prebuffer = atof(arg) * 1000;
4086 } else if (!strcasecmp(cmd, "StartSendOnKey")) {
4088 stream->send_on_key = 1;
4089 } else if (!strcasecmp(cmd, "AudioCodec")) {
4090 get_arg(arg, sizeof(arg), &p);
4091 audio_id = opt_audio_codec(arg);
4092 if (audio_id == CODEC_ID_NONE) {
4093 fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
4094 filename, line_num, arg);
4097 } else if (!strcasecmp(cmd, "VideoCodec")) {
4098 get_arg(arg, sizeof(arg), &p);
4099 video_id = opt_video_codec(arg);
4100 if (video_id == CODEC_ID_NONE) {
4101 fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
4102 filename, line_num, arg);
4105 } else if (!strcasecmp(cmd, "MaxTime")) {
4106 get_arg(arg, sizeof(arg), &p);
4108 stream->max_time = atof(arg) * 1000;
4109 } else if (!strcasecmp(cmd, "AudioBitRate")) {
4110 get_arg(arg, sizeof(arg), &p);
4112 audio_enc.bit_rate = atoi(arg) * 1000;
4113 } else if (!strcasecmp(cmd, "AudioChannels")) {
4114 get_arg(arg, sizeof(arg), &p);
4116 audio_enc.channels = atoi(arg);
4117 } else if (!strcasecmp(cmd, "AudioSampleRate")) {
4118 get_arg(arg, sizeof(arg), &p);
4120 audio_enc.sample_rate = atoi(arg);
4121 } else if (!strcasecmp(cmd, "AudioQuality")) {
4122 get_arg(arg, sizeof(arg), &p);
4124 // audio_enc.quality = atof(arg) * 1000;
4126 } else if (!strcasecmp(cmd, "VideoBitRateRange")) {
4128 int minrate, maxrate;
4130 get_arg(arg, sizeof(arg), &p);
4132 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
4133 video_enc.rc_min_rate = minrate * 1000;
4134 video_enc.rc_max_rate = maxrate * 1000;
4136 fprintf(stderr, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4137 filename, line_num, arg);
4141 } else if (!strcasecmp(cmd, "Debug")) {
4143 get_arg(arg, sizeof(arg), &p);
4144 video_enc.debug = strtol(arg,0,0);
4146 } else if (!strcasecmp(cmd, "Strict")) {
4148 get_arg(arg, sizeof(arg), &p);
4149 video_enc.strict_std_compliance = atoi(arg);
4151 } else if (!strcasecmp(cmd, "VideoBufferSize")) {
4153 get_arg(arg, sizeof(arg), &p);
4154 video_enc.rc_buffer_size = atoi(arg) * 8*1024;
4156 } else if (!strcasecmp(cmd, "VideoBitRateTolerance")) {
4158 get_arg(arg, sizeof(arg), &p);
4159 video_enc.bit_rate_tolerance = atoi(arg) * 1000;
4161 } else if (!strcasecmp(cmd, "VideoBitRate")) {
4162 get_arg(arg, sizeof(arg), &p);
4164 video_enc.bit_rate = atoi(arg) * 1000;
4166 } else if (!strcasecmp(cmd, "VideoSize")) {
4167 get_arg(arg, sizeof(arg), &p);
4169 av_parse_video_frame_size(&video_enc.width, &video_enc.height, arg);
4170 if ((video_enc.width % 16) != 0 ||
4171 (video_enc.height % 16) != 0) {
4172 fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
4173 filename, line_num);
4177 } else if (!strcasecmp(cmd, "VideoFrameRate")) {
4178 get_arg(arg, sizeof(arg), &p);
4180 AVRational frame_rate;
4181 if (av_parse_video_frame_rate(&frame_rate, arg) < 0) {
4182 fprintf(stderr, "Incorrect frame rate\n");
4185 video_enc.time_base.num = frame_rate.den;
4186 video_enc.time_base.den = frame_rate.num;
4189 } else if (!strcasecmp(cmd, "VideoGopSize")) {
4190 get_arg(arg, sizeof(arg), &p);
4192 video_enc.gop_size = atoi(arg);
4193 } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
4195 video_enc.gop_size = 1;
4196 } else if (!strcasecmp(cmd, "VideoHighQuality")) {
4198 video_enc.mb_decision = FF_MB_DECISION_BITS;
4199 } else if (!strcasecmp(cmd, "Video4MotionVector")) {
4201 video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
4202 video_enc.flags |= CODEC_FLAG_4MV;
4204 } else if (!strcasecmp(cmd, "AVOptionVideo") ||
4205 !strcasecmp(cmd, "AVOptionAudio")) {
4207 AVCodecContext *avctx;
4209 get_arg(arg, sizeof(arg), &p);
4210 get_arg(arg2, sizeof(arg2), &p);
4211 if (!strcasecmp(cmd, "AVOptionVideo")) {
4213 type = AV_OPT_FLAG_VIDEO_PARAM;
4216 type = AV_OPT_FLAG_AUDIO_PARAM;
4218 if (ffserver_opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {
4219 fprintf(stderr, "AVOption error: %s %s\n", arg, arg2);
4222 } else if (!strcasecmp(cmd, "VideoTag")) {
4223 get_arg(arg, sizeof(arg), &p);
4224 if ((strlen(arg) == 4) && stream)
4225 video_enc.codec_tag = AV_RL32(arg);
4226 } else if (!strcasecmp(cmd, "BitExact")) {
4228 video_enc.flags |= CODEC_FLAG_BITEXACT;
4229 } else if (!strcasecmp(cmd, "DctFastint")) {
4231 video_enc.dct_algo = FF_DCT_FASTINT;
4232 } else if (!strcasecmp(cmd, "IdctSimple")) {
4234 video_enc.idct_algo = FF_IDCT_SIMPLE;
4235 } else if (!strcasecmp(cmd, "Qscale")) {
4236 get_arg(arg, sizeof(arg), &p);
4238 video_enc.flags |= CODEC_FLAG_QSCALE;
4239 video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
4241 } else if (!strcasecmp(cmd, "VideoQDiff")) {
4242 get_arg(arg, sizeof(arg), &p);
4244 video_enc.max_qdiff = atoi(arg);
4245 if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
4246 fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
4247 filename, line_num);
4251 } else if (!strcasecmp(cmd, "VideoQMax")) {
4252 get_arg(arg, sizeof(arg), &p);
4254 video_enc.qmax = atoi(arg);
4255 if (video_enc.qmax < 1 || video_enc.qmax > 31) {
4256 fprintf(stderr, "%s:%d: VideoQMax out of range\n",
4257 filename, line_num);
4261 } else if (!strcasecmp(cmd, "VideoQMin")) {
4262 get_arg(arg, sizeof(arg), &p);
4264 video_enc.qmin = atoi(arg);
4265 if (video_enc.qmin < 1 || video_enc.qmin > 31) {
4266 fprintf(stderr, "%s:%d: VideoQMin out of range\n",
4267 filename, line_num);
4271 } else if (!strcasecmp(cmd, "LumaElim")) {
4272 get_arg(arg, sizeof(arg), &p);
4274 video_enc.luma_elim_threshold = atoi(arg);
4275 } else if (!strcasecmp(cmd, "ChromaElim")) {
4276 get_arg(arg, sizeof(arg), &p);
4278 video_enc.chroma_elim_threshold = atoi(arg);
4279 } else if (!strcasecmp(cmd, "LumiMask")) {
4280 get_arg(arg, sizeof(arg), &p);
4282 video_enc.lumi_masking = atof(arg);
4283 } else if (!strcasecmp(cmd, "DarkMask")) {
4284 get_arg(arg, sizeof(arg), &p);
4286 video_enc.dark_masking = atof(arg);
4287 } else if (!strcasecmp(cmd, "NoVideo")) {
4288 video_id = CODEC_ID_NONE;
4289 } else if (!strcasecmp(cmd, "NoAudio")) {
4290 audio_id = CODEC_ID_NONE;
4291 } else if (!strcasecmp(cmd, "ACL")) {
4294 get_arg(arg, sizeof(arg), &p);
4295 if (strcasecmp(arg, "allow") == 0)
4296 acl.action = IP_ALLOW;
4297 else if (strcasecmp(arg, "deny") == 0)
4298 acl.action = IP_DENY;
4300 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4301 filename, line_num, arg);
4305 get_arg(arg, sizeof(arg), &p);
4307 if (resolve_host(&acl.first, arg) != 0) {
4308 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4309 filename, line_num, arg);
4312 acl.last = acl.first;
4314 get_arg(arg, sizeof(arg), &p);
4317 if (resolve_host(&acl.last, arg) != 0) {
4318 fprintf(stderr, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4319 filename, line_num, arg);
4325 IPAddressACL *nacl = av_mallocz(sizeof(*nacl));
4326 IPAddressACL **naclp = 0;
4332 naclp = &stream->acl;
4336 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
4337 filename, line_num);
4343 naclp = &(*naclp)->next;
4348 } else if (!strcasecmp(cmd, "RTSPOption")) {
4349 get_arg(arg, sizeof(arg), &p);
4351 av_freep(&stream->rtsp_option);
4352 stream->rtsp_option = av_strdup(arg);
4354 } else if (!strcasecmp(cmd, "MulticastAddress")) {
4355 get_arg(arg, sizeof(arg), &p);
4357 if (resolve_host(&stream->multicast_ip, arg) != 0) {
4358 fprintf(stderr, "%s:%d: Invalid host/IP address: %s\n",
4359 filename, line_num, arg);
4362 stream->is_multicast = 1;
4363 stream->loop = 1; /* default is looping */
4365 } else if (!strcasecmp(cmd, "MulticastPort")) {
4366 get_arg(arg, sizeof(arg), &p);
4368 stream->multicast_port = atoi(arg);
4369 } else if (!strcasecmp(cmd, "MulticastTTL")) {
4370 get_arg(arg, sizeof(arg), &p);
4372 stream->multicast_ttl = atoi(arg);
4373 } else if (!strcasecmp(cmd, "NoLoop")) {
4376 } else if (!strcasecmp(cmd, "</Stream>")) {
4378 fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
4379 filename, line_num);
4382 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
4383 if (audio_id != CODEC_ID_NONE) {
4384 audio_enc.codec_type = CODEC_TYPE_AUDIO;
4385 audio_enc.codec_id = audio_id;
4386 add_codec(stream, &audio_enc);
4388 if (video_id != CODEC_ID_NONE) {
4389 video_enc.codec_type = CODEC_TYPE_VIDEO;
4390 video_enc.codec_id = video_id;
4391 add_codec(stream, &video_enc);
4396 } else if (!strcasecmp(cmd, "<Redirect")) {
4397 /*********************************************/
4399 if (stream || feed || redirect) {
4400 fprintf(stderr, "%s:%d: Already in a tag\n",
4401 filename, line_num);
4404 redirect = av_mallocz(sizeof(FFStream));
4405 *last_stream = redirect;
4406 last_stream = &redirect->next;
4408 get_arg(redirect->filename, sizeof(redirect->filename), &p);
4409 q = strrchr(redirect->filename, '>');
4412 redirect->stream_type = STREAM_TYPE_REDIRECT;
4414 } else if (!strcasecmp(cmd, "URL")) {
4416 get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
4417 } else if (!strcasecmp(cmd, "</Redirect>")) {
4419 fprintf(stderr, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4420 filename, line_num);
4423 if (!redirect->feed_filename[0]) {
4424 fprintf(stderr, "%s:%d: No URL found for <Redirect>\n",
4425 filename, line_num);
4430 } else if (!strcasecmp(cmd, "LoadModule")) {
4431 get_arg(arg, sizeof(arg), &p);
4435 fprintf(stderr, "%s:%d: Module support not compiled into this version: '%s'\n",
4436 filename, line_num, arg);
4440 fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
4441 filename, line_num, cmd);
4452 static void handle_child_exit(int sig)
4457 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
4460 for (feed = first_feed; feed; feed = feed->next) {
4461 if (feed->pid == pid) {
4462 int uptime = time(0) - feed->pid_start;
4465 fprintf(stderr, "%s: Pid %d exited with status %d after %d seconds\n", feed->filename, pid, status, uptime);
4468 /* Turn off any more restarts */
4469 feed->child_argv = 0;
4474 need_to_start_children = 1;
4477 static void opt_debug(void)
4480 ffserver_daemon = 0;
4481 logfilename[0] = '-';
4484 static void opt_show_help(void)
4486 printf("usage: ffserver [options]\n"
4487 "Hyper fast multi format Audio/Video streaming server\n");
4489 show_help_options(options, "Main options:\n", 0, 0);
4492 static const OptionDef options[] = {
4493 { "h", OPT_EXIT, {(void*)opt_show_help}, "show help" },
4494 { "version", OPT_EXIT, {(void*)show_version}, "show version" },
4495 { "L", OPT_EXIT, {(void*)show_license}, "show license" },
4496 { "formats", OPT_EXIT, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
4497 { "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
4498 { "d", 0, {(void*)opt_debug}, "enable debug mode" },
4499 { "f", HAS_ARG | OPT_STRING, {(void*)&config_filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
4503 int main(int argc, char **argv)
4505 struct sigaction sigact;
4511 config_filename = "/etc/ffserver.conf";
4513 my_program_name = argv[0];
4514 my_program_dir = getcwd(0, 0);
4515 ffserver_daemon = 1;
4517 parse_options(argc, argv, options, NULL);
4519 unsetenv("http_proxy"); /* Kill the http_proxy */
4521 av_lfg_init(&random_state, ff_random_get_seed());
4523 memset(&sigact, 0, sizeof(sigact));
4524 sigact.sa_handler = handle_child_exit;
4525 sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
4526 sigaction(SIGCHLD, &sigact, 0);
4528 if (parse_ffconfig(config_filename) < 0) {
4529 fprintf(stderr, "Incorrect config file - exiting.\n");
4533 /* open log file if needed */
4534 if (logfilename[0] != '\0') {
4535 if (!strcmp(logfilename, "-"))
4538 logfile = fopen(logfilename, "a");
4539 av_log_set_callback(http_av_log);
4542 build_file_streams();
4544 build_feed_streams();
4546 compute_bandwidth();
4548 /* put the process in background and detach it from its TTY */
4549 if (ffserver_daemon) {
4556 } else if (pid > 0) {
4563 open("/dev/null", O_RDWR);
4564 if (strcmp(logfilename, "-") != 0) {
4574 signal(SIGPIPE, SIG_IGN);
4576 if (ffserver_daemon)
4579 if (http_server() < 0) {
4580 http_log("Could not start server\n");