2 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * multiple format streaming server based on the FFmpeg libraries
28 #define closesocket close
33 #include "libavformat/avformat.h"
34 /* FIXME: those are internal headers, ffserver _really_ shouldn't use them */
35 #include "libavformat/ffm.h"
36 #include "libavformat/network.h"
37 #include "libavformat/os_support.h"
38 #include "libavformat/rtpdec.h"
39 #include "libavformat/rtpproto.h"
40 #include "libavformat/rtsp.h"
41 #include "libavformat/rtspcodes.h"
42 #include "libavformat/avio_internal.h"
43 #include "libavformat/internal.h"
44 #include "libavformat/url.h"
46 #include "libavutil/avassert.h"
47 #include "libavutil/avstring.h"
48 #include "libavutil/lfg.h"
49 #include "libavutil/dict.h"
50 #include "libavutil/intreadwrite.h"
51 #include "libavutil/mathematics.h"
52 #include "libavutil/random_seed.h"
53 #include "libavutil/parseutils.h"
54 #include "libavutil/opt.h"
55 #include "libavutil/time.h"
62 #include <sys/ioctl.h>
72 #include "ffserver_config.h"
74 #define PATH_LENGTH 1024
76 const char program_name[] = "ffserver";
77 const int program_birth_year = 2000;
79 static const OptionDef options[];
82 HTTPSTATE_WAIT_REQUEST,
83 HTTPSTATE_SEND_HEADER,
84 HTTPSTATE_SEND_DATA_HEADER,
85 HTTPSTATE_SEND_DATA, /* sending TCP or UDP data */
86 HTTPSTATE_SEND_DATA_TRAILER,
87 HTTPSTATE_RECEIVE_DATA,
88 HTTPSTATE_WAIT_FEED, /* wait for data from the feed */
91 RTSPSTATE_WAIT_REQUEST,
93 RTSPSTATE_SEND_PACKET,
96 static const char * const http_state[] = {
112 #define IOBUFFER_INIT_SIZE 8192
114 /* timeouts are in ms */
115 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
116 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
118 #define SYNC_TIMEOUT (10 * 1000)
120 typedef struct RTSPActionServerSetup {
122 char transport_option[512];
123 } RTSPActionServerSetup;
126 int64_t count1, count2;
127 int64_t time1, time2;
130 /* context associated with one connection */
131 typedef struct HTTPContext {
132 enum HTTPState state;
133 int fd; /* socket file descriptor */
134 struct sockaddr_in from_addr; /* origin */
135 struct pollfd *poll_entry; /* used when polling */
137 uint8_t *buffer_ptr, *buffer_end;
140 int chunked_encoding;
141 int chunk_size; /* 0 if it needs to be read */
142 struct HTTPContext *next;
143 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
147 /* input format handling */
148 AVFormatContext *fmt_in;
149 int64_t start_time; /* In milliseconds - this wraps fairly often */
150 int64_t first_pts; /* initial pts value */
151 int64_t cur_pts; /* current pts value from the stream in us */
152 int64_t cur_frame_duration; /* duration of the current frame in us */
153 int cur_frame_bytes; /* output frame size, needed to compute
154 the time at which we send each
156 int pts_stream_index; /* stream we choose as clock reference */
157 int64_t cur_clock; /* current clock reference value in us */
158 /* output format handling */
159 struct FFServerStream *stream;
160 /* -1 is invalid stream */
161 int feed_streams[FFSERVER_MAX_STREAMS]; /* index of streams in the feed */
162 int switch_feed_streams[FFSERVER_MAX_STREAMS]; /* index of streams in the feed */
164 AVFormatContext fmt_ctx; /* instance of FFServerStream for one user */
165 int last_packet_sent; /* true if last data packet was sent */
167 DataRateData datarate;
174 int is_packetized; /* if true, the stream is packetized */
175 int packet_stream_index; /* current stream for output in state machine */
177 /* RTSP state specific */
178 uint8_t *pb_buffer; /* XXX: use that in all the code */
180 int seq; /* RTSP sequence number */
182 /* RTP state specific */
183 enum RTSPLowerTransport rtp_protocol;
184 char session_id[32]; /* session id */
185 AVFormatContext *rtp_ctx[FFSERVER_MAX_STREAMS];
187 /* RTP/UDP specific */
188 URLContext *rtp_handles[FFSERVER_MAX_STREAMS];
190 /* RTP/TCP specific */
191 struct HTTPContext *rtsp_c;
192 uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
195 typedef struct FeedData {
196 long long data_count;
197 float avg_frame_size; /* frame size averaged over last frames with exponential mean */
200 static HTTPContext *first_http_ctx;
202 static FFServerConfig config = {
203 .nb_max_http_connections = 2000,
204 .nb_max_connections = 5,
205 .max_bandwidth = 1000,
209 static void new_connection(int server_fd, int is_rtsp);
210 static void close_connection(HTTPContext *c);
213 static int handle_connection(HTTPContext *c);
214 static inline void print_stream_params(AVIOContext *pb, FFServerStream *stream);
215 static void compute_status(HTTPContext *c);
216 static int open_input_stream(HTTPContext *c, const char *info);
217 static int http_parse_request(HTTPContext *c);
218 static int http_send_data(HTTPContext *c);
219 static int http_start_receive_data(HTTPContext *c);
220 static int http_receive_data(HTTPContext *c);
223 static int rtsp_parse_request(HTTPContext *c);
224 static void rtsp_cmd_describe(HTTPContext *c, const char *url);
225 static void rtsp_cmd_options(HTTPContext *c, const char *url);
226 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
227 RTSPMessageHeader *h);
228 static void rtsp_cmd_play(HTTPContext *c, const char *url,
229 RTSPMessageHeader *h);
230 static void rtsp_cmd_interrupt(HTTPContext *c, const char *url,
231 RTSPMessageHeader *h, int pause_only);
234 static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
235 struct in_addr my_ip);
238 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
239 FFServerStream *stream,
240 const char *session_id,
241 enum RTSPLowerTransport rtp_protocol);
242 static int rtp_new_av_stream(HTTPContext *c,
243 int stream_index, struct sockaddr_in *dest_addr,
244 HTTPContext *rtsp_c);
246 static const char *my_program_name;
248 static int no_launch;
249 static int need_to_start_children;
251 /* maximum number of simultaneous HTTP connections */
252 static unsigned int nb_connections;
254 static uint64_t current_bandwidth;
256 /* Making this global saves on passing it around everywhere */
257 static int64_t cur_time;
259 static AVLFG random_state;
261 static FILE *logfile = NULL;
263 static void htmlstrip(char *s) {
265 s += strspn(s, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,. ");
271 static int64_t ffm_read_write_index(int fd)
275 if (lseek(fd, 8, SEEK_SET) < 0)
277 if (read(fd, buf, 8) != 8)
282 static int ffm_write_write_index(int fd, int64_t pos)
288 buf[i] = (pos >> (56 - i * 8)) & 0xff;
289 if (lseek(fd, 8, SEEK_SET) < 0)
291 if (write(fd, buf, 8) != 8)
300 static void ffm_set_write_index(AVFormatContext *s, int64_t pos,
303 FFMContext *ffm = s->priv_data;
304 ffm->write_index = pos;
305 ffm->file_size = file_size;
308 static char *ctime1(char *buf2, int buf_size)
315 av_strlcpy(buf2, p, buf_size);
316 p = buf2 + strlen(p) - 1;
322 static void http_vlog(const char *fmt, va_list vargs)
324 static int print_prefix = 1;
331 ctime1(buf, sizeof(buf));
332 fprintf(logfile, "%s ", buf);
334 print_prefix = strstr(fmt, "\n") != NULL;
335 vfprintf(logfile, fmt, vargs);
340 __attribute__ ((format (printf, 1, 2)))
342 static void http_log(const char *fmt, ...)
345 va_start(vargs, fmt);
346 http_vlog(fmt, vargs);
350 static void http_av_log(void *ptr, int level, const char *fmt, va_list vargs)
352 static int print_prefix = 1;
353 AVClass *avc = ptr ? *(AVClass**)ptr : NULL;
354 if (level > av_log_get_level())
356 if (print_prefix && avc)
357 http_log("[%s @ %p]", avc->item_name(ptr), ptr);
358 print_prefix = strstr(fmt, "\n") != NULL;
359 http_vlog(fmt, vargs);
362 static void log_connection(HTTPContext *c)
367 http_log("%s - - [%s] \"%s %s\" %d %"PRId64"\n",
368 inet_ntoa(c->from_addr.sin_addr), c->method, c->url,
369 c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
372 static void update_datarate(DataRateData *drd, int64_t count)
374 if (!drd->time1 && !drd->count1) {
375 drd->time1 = drd->time2 = cur_time;
376 drd->count1 = drd->count2 = count;
377 } else if (cur_time - drd->time2 > 5000) {
378 drd->time1 = drd->time2;
379 drd->count1 = drd->count2;
380 drd->time2 = cur_time;
385 /* In bytes per second */
386 static int compute_datarate(DataRateData *drd, int64_t count)
388 if (cur_time == drd->time1)
391 return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
395 static void start_children(FFServerStream *feed)
405 cmd_length = strlen(my_program_name);
408 * FIXME: WIP Safeguard. Remove after clearing all harcoded
409 * '1024' path lengths
411 if (cmd_length > PATH_LENGTH - 1) {
412 http_log("Could not start children. Command line: '%s' exceeds "
413 "path length limit (%d)\n", my_program_name, PATH_LENGTH);
417 pathname = av_strdup (my_program_name);
419 http_log("Could not allocate memory for children cmd line\n");
422 /* replace "ffserver" with "ffmpeg" in the path of current
423 * program. Ignore user provided path */
425 slash = strrchr(pathname, '/');
430 strcpy(slash, "ffmpeg");
432 for (; feed; feed = feed->next) {
434 if (!feed->child_argv || feed->pid)
437 feed->pid_start = time(0);
441 http_log("Unable to create children\n");
450 http_log("Launch command line: ");
451 http_log("%s ", pathname);
453 for (i = 1; feed->child_argv[i] && feed->child_argv[i][0]; i++)
454 http_log("%s ", feed->child_argv[i]);
457 for (i = 3; i < 256; i++)
461 if (!freopen("/dev/null", "r", stdin))
462 http_log("failed to redirect STDIN to /dev/null\n;");
463 if (!freopen("/dev/null", "w", stdout))
464 http_log("failed to redirect STDOUT to /dev/null\n;");
465 if (!freopen("/dev/null", "w", stderr))
466 http_log("failed to redirect STDERR to /dev/null\n;");
469 signal(SIGPIPE, SIG_DFL);
470 execvp(pathname, feed->child_argv);
477 /* open a listening socket */
478 static int socket_open_listen(struct sockaddr_in *my_addr)
482 server_fd = socket(AF_INET,SOCK_STREAM,0);
489 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)))
490 av_log(NULL, AV_LOG_WARNING, "setsockopt SO_REUSEADDR failed\n");
492 my_addr->sin_family = AF_INET;
493 if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
495 snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)",
496 ntohs(my_addr->sin_port));
501 if (listen (server_fd, 5) < 0) {
506 if (ff_socket_nonblock(server_fd, 1) < 0)
507 av_log(NULL, AV_LOG_WARNING, "ff_socket_nonblock failed\n");
512 closesocket(server_fd);
516 /* start all multicast streams */
517 static void start_multicast(void)
519 FFServerStream *stream;
522 struct sockaddr_in dest_addr = {0};
523 int default_port, stream_index;
524 unsigned int random0, random1;
527 for(stream = config.first_stream; stream; stream = stream->next) {
529 if (!stream->is_multicast)
532 random0 = av_lfg_get(&random_state);
533 random1 = av_lfg_get(&random_state);
535 /* open the RTP connection */
536 snprintf(session_id, sizeof(session_id), "%08x%08x", random0, random1);
538 /* choose a port if none given */
539 if (stream->multicast_port == 0) {
540 stream->multicast_port = default_port;
544 dest_addr.sin_family = AF_INET;
545 dest_addr.sin_addr = stream->multicast_ip;
546 dest_addr.sin_port = htons(stream->multicast_port);
548 rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
549 RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
553 if (open_input_stream(rtp_c, "") < 0) {
554 http_log("Could not open input stream for stream '%s'\n",
559 /* open each RTP stream */
560 for(stream_index = 0; stream_index < stream->nb_streams;
562 dest_addr.sin_port = htons(stream->multicast_port +
564 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) >= 0)
567 http_log("Could not open output stream '%s/streamid=%d'\n",
568 stream->filename, stream_index);
572 rtp_c->state = HTTPSTATE_SEND_DATA;
576 /* main loop of the HTTP server */
577 static int http_server(void)
579 int server_fd = 0, rtsp_server_fd = 0;
581 struct pollfd *poll_table, *poll_entry;
582 HTTPContext *c, *c_next;
584 poll_table = av_mallocz_array(config.nb_max_http_connections + 2,
585 sizeof(*poll_table));
587 http_log("Impossible to allocate a poll table handling %d "
588 "connections.\n", config.nb_max_http_connections);
592 if (config.http_addr.sin_port) {
593 server_fd = socket_open_listen(&config.http_addr);
598 if (config.rtsp_addr.sin_port) {
599 rtsp_server_fd = socket_open_listen(&config.rtsp_addr);
600 if (rtsp_server_fd < 0) {
601 closesocket(server_fd);
606 if (!rtsp_server_fd && !server_fd) {
607 http_log("HTTP and RTSP disabled.\n");
611 http_log("FFserver started.\n");
613 start_children(config.first_feed);
618 poll_entry = poll_table;
620 poll_entry->fd = server_fd;
621 poll_entry->events = POLLIN;
624 if (rtsp_server_fd) {
625 poll_entry->fd = rtsp_server_fd;
626 poll_entry->events = POLLIN;
630 /* wait for events on each HTTP handle */
637 case HTTPSTATE_SEND_HEADER:
638 case RTSPSTATE_SEND_REPLY:
639 case RTSPSTATE_SEND_PACKET:
640 c->poll_entry = poll_entry;
642 poll_entry->events = POLLOUT;
645 case HTTPSTATE_SEND_DATA_HEADER:
646 case HTTPSTATE_SEND_DATA:
647 case HTTPSTATE_SEND_DATA_TRAILER:
648 if (!c->is_packetized) {
649 /* for TCP, we output as much as we can
650 * (may need to put a limit) */
651 c->poll_entry = poll_entry;
653 poll_entry->events = POLLOUT;
656 /* when ffserver is doing the timing, we work by
657 * looking at which packet needs to be sent every
658 * 10 ms (one tick wait XXX: 10 ms assumed) */
663 case HTTPSTATE_WAIT_REQUEST:
664 case HTTPSTATE_RECEIVE_DATA:
665 case HTTPSTATE_WAIT_FEED:
666 case RTSPSTATE_WAIT_REQUEST:
667 /* need to catch errors */
668 c->poll_entry = poll_entry;
670 poll_entry->events = POLLIN;/* Maybe this will work */
674 c->poll_entry = NULL;
680 /* wait for an event on one connection. We poll at least every
681 * second to handle timeouts */
683 ret = poll(poll_table, poll_entry - poll_table, delay);
684 if (ret < 0 && ff_neterrno() != AVERROR(EAGAIN) &&
685 ff_neterrno() != AVERROR(EINTR)) {
690 cur_time = av_gettime() / 1000;
692 if (need_to_start_children) {
693 need_to_start_children = 0;
694 start_children(config.first_feed);
697 /* now handle the events */
698 for(c = first_http_ctx; c; c = c_next) {
700 if (handle_connection(c) < 0) {
702 /* close and free the connection */
707 poll_entry = poll_table;
709 /* new HTTP connection request ? */
710 if (poll_entry->revents & POLLIN)
711 new_connection(server_fd, 0);
714 if (rtsp_server_fd) {
715 /* new RTSP connection request ? */
716 if (poll_entry->revents & POLLIN)
717 new_connection(rtsp_server_fd, 1);
726 /* start waiting for a new HTTP/RTSP request */
727 static void start_wait_request(HTTPContext *c, int is_rtsp)
729 c->buffer_ptr = c->buffer;
730 c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
732 c->state = is_rtsp ? RTSPSTATE_WAIT_REQUEST : HTTPSTATE_WAIT_REQUEST;
733 c->timeout = cur_time +
734 (is_rtsp ? RTSP_REQUEST_TIMEOUT : HTTP_REQUEST_TIMEOUT);
737 static void http_send_too_busy_reply(int fd)
740 int len = snprintf(buffer, sizeof(buffer),
741 "HTTP/1.0 503 Server too busy\r\n"
742 "Content-type: text/html\r\n"
744 "<html><head><title>Too busy</title></head><body>\r\n"
745 "<p>The server is too busy to serve your request at "
747 "<p>The number of current connections is %u, and this "
748 "exceeds the limit of %u.</p>\r\n"
749 "</body></html>\r\n",
750 nb_connections, config.nb_max_connections);
751 av_assert0(len < sizeof(buffer));
752 if (send(fd, buffer, len, 0) < len)
753 av_log(NULL, AV_LOG_WARNING,
754 "Could not send too-busy reply, send() failed\n");
758 static void new_connection(int server_fd, int is_rtsp)
760 struct sockaddr_in from_addr;
763 HTTPContext *c = NULL;
765 len = sizeof(from_addr);
766 fd = accept(server_fd, (struct sockaddr *)&from_addr,
769 http_log("error during accept %s\n", strerror(errno));
772 if (ff_socket_nonblock(fd, 1) < 0)
773 av_log(NULL, AV_LOG_WARNING, "ff_socket_nonblock failed\n");
775 if (nb_connections >= config.nb_max_connections) {
776 http_send_too_busy_reply(fd);
780 /* add a new connection */
781 c = av_mallocz(sizeof(HTTPContext));
786 c->poll_entry = NULL;
787 c->from_addr = from_addr;
788 c->buffer_size = IOBUFFER_INIT_SIZE;
789 c->buffer = av_malloc(c->buffer_size);
793 c->next = first_http_ctx;
797 start_wait_request(c, is_rtsp);
803 av_freep(&c->buffer);
809 static void close_connection(HTTPContext *c)
811 HTTPContext **cp, *c1;
813 AVFormatContext *ctx;
816 /* remove connection from list */
817 cp = &first_http_ctx;
826 /* remove references, if any (XXX: do it faster) */
827 for(c1 = first_http_ctx; c1; c1 = c1->next) {
832 /* remove connection associated resources */
836 /* close each frame parser */
837 for(i=0;i<c->fmt_in->nb_streams;i++) {
838 st = c->fmt_in->streams[i];
839 if (st->codec->codec)
840 avcodec_close(st->codec);
842 avformat_close_input(&c->fmt_in);
845 /* free RTP output streams if any */
848 nb_streams = c->stream->nb_streams;
850 for(i=0;i<nb_streams;i++) {
853 av_write_trailer(ctx);
854 av_dict_free(&ctx->metadata);
855 av_freep(&ctx->streams[0]);
858 ffurl_close(c->rtp_handles[i]);
863 if (!c->last_packet_sent && c->state == HTTPSTATE_SEND_DATA_TRAILER) {
865 if (ctx->oformat && avio_open_dyn_buf(&ctx->pb) >= 0) {
866 av_write_trailer(ctx);
867 av_freep(&c->pb_buffer);
868 avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
872 for(i=0; i<ctx->nb_streams; i++)
873 av_freep(&ctx->streams[i]);
874 av_freep(&ctx->streams);
875 av_freep(&ctx->priv_data);
877 if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
878 current_bandwidth -= c->stream->bandwidth;
880 /* signal that there is no feed if we are the feeder socket */
881 if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
882 c->stream->feed_opened = 0;
886 av_freep(&c->pb_buffer);
887 av_freep(&c->packet_buffer);
888 av_freep(&c->buffer);
893 static int handle_connection(HTTPContext *c)
899 case HTTPSTATE_WAIT_REQUEST:
900 case RTSPSTATE_WAIT_REQUEST:
902 if ((c->timeout - cur_time) < 0)
904 if (c->poll_entry->revents & (POLLERR | POLLHUP))
907 /* no need to read if no events */
908 if (!(c->poll_entry->revents & POLLIN))
912 if (!(len = recv(c->fd, c->buffer_ptr, 1, 0)))
916 if (ff_neterrno() != AVERROR(EAGAIN) &&
917 ff_neterrno() != AVERROR(EINTR))
921 /* search for end of request. */
922 c->buffer_ptr += len;
924 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
925 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
926 /* request found : parse it and reply */
927 if (c->state == HTTPSTATE_WAIT_REQUEST)
928 ret = http_parse_request(c);
930 ret = rtsp_parse_request(c);
934 } else if (ptr >= c->buffer_end) {
935 /* request too long: cannot do anything */
937 } else goto read_loop;
941 case HTTPSTATE_SEND_HEADER:
942 if (c->poll_entry->revents & (POLLERR | POLLHUP))
945 /* no need to write if no events */
946 if (!(c->poll_entry->revents & POLLOUT))
948 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
950 if (ff_neterrno() != AVERROR(EAGAIN) &&
951 ff_neterrno() != AVERROR(EINTR)) {
952 goto close_connection;
956 c->buffer_ptr += len;
958 c->stream->bytes_served += len;
959 c->data_count += len;
960 if (c->buffer_ptr >= c->buffer_end) {
961 av_freep(&c->pb_buffer);
965 /* all the buffer was sent : synchronize to the incoming
967 c->state = HTTPSTATE_SEND_DATA_HEADER;
968 c->buffer_ptr = c->buffer_end = c->buffer;
972 case HTTPSTATE_SEND_DATA:
973 case HTTPSTATE_SEND_DATA_HEADER:
974 case HTTPSTATE_SEND_DATA_TRAILER:
975 /* for packetized output, we consider we can always write (the
976 * input streams set the speed). It may be better to verify
977 * that we do not rely too much on the kernel queues */
978 if (!c->is_packetized) {
979 if (c->poll_entry->revents & (POLLERR | POLLHUP))
982 /* no need to read if no events */
983 if (!(c->poll_entry->revents & POLLOUT))
986 if (http_send_data(c) < 0)
988 /* close connection if trailer sent */
989 if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
991 /* Check if it is a single jpeg frame 123 */
992 if (c->stream->single_frame && c->data_count > c->cur_frame_bytes && c->cur_frame_bytes > 0) {
996 case HTTPSTATE_RECEIVE_DATA:
997 /* no need to read if no events */
998 if (c->poll_entry->revents & (POLLERR | POLLHUP))
1000 if (!(c->poll_entry->revents & POLLIN))
1002 if (http_receive_data(c) < 0)
1005 case HTTPSTATE_WAIT_FEED:
1006 /* no need to read if no events */
1007 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
1010 /* nothing to do, we'll be waken up by incoming feed packets */
1013 case RTSPSTATE_SEND_REPLY:
1014 if (c->poll_entry->revents & (POLLERR | POLLHUP))
1015 goto close_connection;
1016 /* no need to write if no events */
1017 if (!(c->poll_entry->revents & POLLOUT))
1019 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
1021 if (ff_neterrno() != AVERROR(EAGAIN) &&
1022 ff_neterrno() != AVERROR(EINTR)) {
1023 goto close_connection;
1027 c->buffer_ptr += len;
1028 c->data_count += len;
1029 if (c->buffer_ptr >= c->buffer_end) {
1030 /* all the buffer was sent : wait for a new request */
1031 av_freep(&c->pb_buffer);
1032 start_wait_request(c, 1);
1035 case RTSPSTATE_SEND_PACKET:
1036 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
1037 av_freep(&c->packet_buffer);
1040 /* no need to write if no events */
1041 if (!(c->poll_entry->revents & POLLOUT))
1043 len = send(c->fd, c->packet_buffer_ptr,
1044 c->packet_buffer_end - c->packet_buffer_ptr, 0);
1046 if (ff_neterrno() != AVERROR(EAGAIN) &&
1047 ff_neterrno() != AVERROR(EINTR)) {
1048 /* error : close connection */
1049 av_freep(&c->packet_buffer);
1054 c->packet_buffer_ptr += len;
1055 if (c->packet_buffer_ptr >= c->packet_buffer_end) {
1056 /* all the buffer was sent : wait for a new request */
1057 av_freep(&c->packet_buffer);
1058 c->state = RTSPSTATE_WAIT_REQUEST;
1061 case HTTPSTATE_READY:
1070 av_freep(&c->pb_buffer);
1074 static int extract_rates(char *rates, int ratelen, const char *request)
1078 for (p = request; *p && *p != '\r' && *p != '\n'; ) {
1079 if (av_strncasecmp(p, "Pragma:", 7) == 0) {
1080 const char *q = p + 7;
1082 while (*q && *q != '\n' && av_isspace(*q))
1085 if (av_strncasecmp(q, "stream-switch-entry=", 20) == 0) {
1091 memset(rates, 0xff, ratelen);
1094 while (*q && *q != '\n' && *q != ':')
1097 if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
1101 if (stream_no < ratelen && stream_no >= 0)
1102 rates[stream_no] = rate_no;
1104 while (*q && *q != '\n' && !av_isspace(*q))
1111 p = strchr(p, '\n');
1121 static int find_stream_in_feed(FFServerStream *feed, AVCodecContext *codec,
1125 int best_bitrate = 100000000;
1128 for (i = 0; i < feed->nb_streams; i++) {
1129 AVCodecContext *feed_codec = feed->streams[i]->codec;
1131 if (feed_codec->codec_id != codec->codec_id ||
1132 feed_codec->sample_rate != codec->sample_rate ||
1133 feed_codec->width != codec->width ||
1134 feed_codec->height != codec->height)
1137 /* Potential stream */
1139 /* We want the fastest stream less than bit_rate, or the slowest
1140 * faster than bit_rate
1143 if (feed_codec->bit_rate <= bit_rate) {
1144 if (best_bitrate > bit_rate ||
1145 feed_codec->bit_rate > best_bitrate) {
1146 best_bitrate = feed_codec->bit_rate;
1151 if (feed_codec->bit_rate < best_bitrate) {
1152 best_bitrate = feed_codec->bit_rate;
1159 static int modify_current_stream(HTTPContext *c, char *rates)
1162 FFServerStream *req = c->stream;
1163 int action_required = 0;
1165 /* Not much we can do for a feed */
1169 for (i = 0; i < req->nb_streams; i++) {
1170 AVCodecContext *codec = req->streams[i]->codec;
1174 c->switch_feed_streams[i] = req->feed_streams[i];
1177 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
1180 /* Wants off or slow */
1181 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1183 /* This doesn't work well when it turns off the only stream! */
1184 c->switch_feed_streams[i] = -2;
1185 c->feed_streams[i] = -2;
1190 if (c->switch_feed_streams[i] >= 0 &&
1191 c->switch_feed_streams[i] != c->feed_streams[i]) {
1192 action_required = 1;
1196 return action_required;
1199 static void get_word(char *buf, int buf_size, const char **pp)
1205 p += strspn(p, SPACE_CHARS);
1207 while (!av_isspace(*p) && *p != '\0') {
1208 if ((q - buf) < buf_size - 1)
1217 static FFServerIPAddressACL* parse_dynamic_acl(FFServerStream *stream,
1223 FFServerIPAddressACL *acl = NULL;
1227 f = fopen(stream->dynamic_acl, "r");
1229 perror(stream->dynamic_acl);
1233 acl = av_mallocz(sizeof(FFServerIPAddressACL));
1240 while (fgets(line, sizeof(line), f)) {
1243 while (av_isspace(*p))
1245 if (*p == '\0' || *p == '#')
1247 ffserver_get_arg(cmd, sizeof(cmd), &p);
1249 if (!av_strcasecmp(cmd, "ACL"))
1250 ffserver_parse_acl_row(NULL, NULL, acl, p, stream->dynamic_acl,
1258 static void free_acl_list(FFServerIPAddressACL *in_acl)
1260 FFServerIPAddressACL *pacl, *pacl2;
1270 static int validate_acl_list(FFServerIPAddressACL *in_acl, HTTPContext *c)
1272 enum FFServerIPAddressAction last_action = IP_DENY;
1273 FFServerIPAddressACL *acl;
1274 struct in_addr *src = &c->from_addr.sin_addr;
1275 unsigned long src_addr = src->s_addr;
1277 for (acl = in_acl; acl; acl = acl->next) {
1278 if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
1279 return (acl->action == IP_ALLOW) ? 1 : 0;
1280 last_action = acl->action;
1283 /* Nothing matched, so return not the last action */
1284 return (last_action == IP_DENY) ? 1 : 0;
1287 static int validate_acl(FFServerStream *stream, HTTPContext *c)
1290 FFServerIPAddressACL *acl;
1292 /* if stream->acl is null validate_acl_list will return 1 */
1293 ret = validate_acl_list(stream->acl, c);
1295 if (stream->dynamic_acl[0]) {
1296 acl = parse_dynamic_acl(stream, c);
1297 ret = validate_acl_list(acl, c);
1305 * compute the real filename of a file by matching it without its
1306 * extensions to all the stream's filenames
1308 static void compute_real_filename(char *filename, int max_size)
1313 FFServerStream *stream;
1315 av_strlcpy(file1, filename, sizeof(file1));
1316 p = strrchr(file1, '.');
1319 for(stream = config.first_stream; stream; stream = stream->next) {
1320 av_strlcpy(file2, stream->filename, sizeof(file2));
1321 p = strrchr(file2, '.');
1324 if (!strcmp(file1, file2)) {
1325 av_strlcpy(filename, stream->filename, max_size);
1340 /* parse HTTP request and prepare header */
1341 static int http_parse_request(HTTPContext *c)
1345 enum RedirType redir_type;
1347 char info[1024], filename[1024];
1351 const char *mime_type;
1352 FFServerStream *stream;
1355 const char *useragent = 0;
1358 get_word(cmd, sizeof(cmd), &p);
1359 av_strlcpy(c->method, cmd, sizeof(c->method));
1361 if (!strcmp(cmd, "GET"))
1363 else if (!strcmp(cmd, "POST"))
1368 get_word(url, sizeof(url), &p);
1369 av_strlcpy(c->url, url, sizeof(c->url));
1371 get_word(protocol, sizeof(protocol), (const char **)&p);
1372 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1375 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
1378 http_log("%s - - New connection: %s %s\n",
1379 inet_ntoa(c->from_addr.sin_addr), cmd, url);
1381 /* find the filename and the optional info string in the request */
1382 p1 = strchr(url, '?');
1384 av_strlcpy(info, p1, sizeof(info));
1389 av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
1391 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1392 if (av_strncasecmp(p, "User-Agent:", 11) == 0) {
1394 if (*useragent && *useragent != '\n' && av_isspace(*useragent))
1398 p = strchr(p, '\n');
1405 redir_type = REDIR_NONE;
1406 if (av_match_ext(filename, "asx")) {
1407 redir_type = REDIR_ASX;
1408 filename[strlen(filename)-1] = 'f';
1409 } else if (av_match_ext(filename, "asf") &&
1410 (!useragent || av_strncasecmp(useragent, "NSPlayer", 8))) {
1411 /* if this isn't WMP or lookalike, return the redirector file */
1412 redir_type = REDIR_ASF;
1413 } else if (av_match_ext(filename, "rpm,ram")) {
1414 redir_type = REDIR_RAM;
1415 strcpy(filename + strlen(filename)-2, "m");
1416 } else if (av_match_ext(filename, "rtsp")) {
1417 redir_type = REDIR_RTSP;
1418 compute_real_filename(filename, sizeof(filename) - 1);
1419 } else if (av_match_ext(filename, "sdp")) {
1420 redir_type = REDIR_SDP;
1421 compute_real_filename(filename, sizeof(filename) - 1);
1424 /* "redirect" request to index.html */
1425 if (!strlen(filename))
1426 av_strlcpy(filename, "index.html", sizeof(filename) - 1);
1428 stream = config.first_stream;
1430 if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
1432 stream = stream->next;
1435 snprintf(msg, sizeof(msg), "File '%s' not found", url);
1436 http_log("File '%s' not found\n", url);
1441 memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1442 memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1444 if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1445 c->http_error = 301;
1447 snprintf(q, c->buffer_size,
1448 "HTTP/1.0 301 Moved\r\n"
1450 "Content-type: text/html\r\n"
1452 "<html><head><title>Moved</title></head><body>\r\n"
1453 "You should be <a href=\"%s\">redirected</a>.\r\n"
1454 "</body></html>\r\n",
1455 stream->feed_filename, stream->feed_filename);
1457 /* prepare output buffer */
1458 c->buffer_ptr = c->buffer;
1460 c->state = HTTPSTATE_SEND_HEADER;
1464 /* If this is WMP, get the rate information */
1465 if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1466 if (modify_current_stream(c, ratebuf)) {
1467 for (i = 0; i < FF_ARRAY_ELEMS(c->feed_streams); i++) {
1468 if (c->switch_feed_streams[i] >= 0)
1469 c->switch_feed_streams[i] = -1;
1474 if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
1475 current_bandwidth += stream->bandwidth;
1477 /* If already streaming this feed, do not let another feeder start */
1478 if (stream->feed_opened) {
1479 snprintf(msg, sizeof(msg), "This feed is already being received.");
1480 http_log("Feed '%s' already being received\n", stream->feed_filename);
1484 if (c->post == 0 && config.max_bandwidth < current_bandwidth) {
1485 c->http_error = 503;
1487 snprintf(q, c->buffer_size,
1488 "HTTP/1.0 503 Server too busy\r\n"
1489 "Content-type: text/html\r\n"
1491 "<html><head><title>Too busy</title></head><body>\r\n"
1492 "<p>The server is too busy to serve your request at "
1493 "this time.</p>\r\n"
1494 "<p>The bandwidth being served (including your stream) "
1495 "is %"PRIu64"kbit/s, and this exceeds the limit of "
1496 "%"PRIu64"kbit/s.</p>\r\n"
1497 "</body></html>\r\n",
1498 current_bandwidth, config.max_bandwidth);
1500 /* prepare output buffer */
1501 c->buffer_ptr = c->buffer;
1503 c->state = HTTPSTATE_SEND_HEADER;
1507 if (redir_type != REDIR_NONE) {
1508 const char *hostinfo = 0;
1510 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1511 if (av_strncasecmp(p, "Host:", 5) == 0) {
1515 p = strchr(p, '\n');
1526 while (av_isspace(*hostinfo))
1529 eoh = strchr(hostinfo, '\n');
1531 if (eoh[-1] == '\r')
1534 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1535 memcpy(hostbuf, hostinfo, eoh - hostinfo);
1536 hostbuf[eoh - hostinfo] = 0;
1538 c->http_error = 200;
1540 switch(redir_type) {
1542 snprintf(q, c->buffer_size,
1543 "HTTP/1.0 200 ASX Follows\r\n"
1544 "Content-type: video/x-ms-asf\r\n"
1546 "<ASX Version=\"3\">\r\n"
1547 //"<!-- Autogenerated by ffserver -->\r\n"
1548 "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n"
1549 "</ASX>\r\n", hostbuf, filename, info);
1553 snprintf(q, c->buffer_size,
1554 "HTTP/1.0 200 RAM Follows\r\n"
1555 "Content-type: audio/x-pn-realaudio\r\n"
1557 "# Autogenerated by ffserver\r\n"
1558 "http://%s/%s%s\r\n", hostbuf, filename, info);
1562 snprintf(q, c->buffer_size,
1563 "HTTP/1.0 200 ASF Redirect follows\r\n"
1564 "Content-type: video/x-ms-asf\r\n"
1567 "Ref1=http://%s/%s%s\r\n", hostbuf, filename, info);
1572 char hostname[256], *p;
1573 /* extract only hostname */
1574 av_strlcpy(hostname, hostbuf, sizeof(hostname));
1575 p = strrchr(hostname, ':');
1578 snprintf(q, c->buffer_size,
1579 "HTTP/1.0 200 RTSP Redirect follows\r\n"
1580 /* XXX: incorrect MIME type ? */
1581 "Content-type: application/x-rtsp\r\n"
1583 "rtsp://%s:%d/%s\r\n", hostname, ntohs(config.rtsp_addr.sin_port), filename);
1592 struct sockaddr_in my_addr;
1594 snprintf(q, c->buffer_size,
1595 "HTTP/1.0 200 OK\r\n"
1596 "Content-type: application/sdp\r\n"
1600 len = sizeof(my_addr);
1602 /* XXX: Should probably fail? */
1603 if (getsockname(c->fd, (struct sockaddr *)&my_addr, &len))
1604 http_log("getsockname() failed\n");
1606 /* XXX: should use a dynamic buffer */
1607 sdp_data_size = prepare_sdp_description(stream,
1610 if (sdp_data_size > 0) {
1611 memcpy(q, sdp_data, sdp_data_size);
1623 /* prepare output buffer */
1624 c->buffer_ptr = c->buffer;
1626 c->state = HTTPSTATE_SEND_HEADER;
1632 snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
1636 stream->conns_served++;
1638 /* XXX: add there authenticate and IP match */
1641 /* if post, it means a feed is being sent */
1642 if (!stream->is_feed) {
1643 /* However it might be a status report from WMP! Let us log the
1644 * data as it might come handy one day. */
1645 const char *logline = 0;
1648 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1649 if (av_strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1653 if (av_strncasecmp(p, "Pragma: client-id=", 18) == 0)
1654 client_id = strtol(p + 18, 0, 10);
1655 p = strchr(p, '\n');
1663 char *eol = strchr(logline, '\n');
1668 if (eol[-1] == '\r')
1670 http_log("%.*s\n", (int) (eol - logline), logline);
1671 c->suppress_log = 1;
1676 http_log("\nGot request:\n%s\n", c->buffer);
1679 if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1682 /* Now we have to find the client_id */
1683 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1684 if (wmpc->wmp_client_id == client_id)
1688 if (wmpc && modify_current_stream(wmpc, ratebuf))
1689 wmpc->switch_pending = 1;
1692 snprintf(msg, sizeof(msg), "POST command not handled");
1696 if (http_start_receive_data(c) < 0) {
1697 snprintf(msg, sizeof(msg), "could not open feed");
1701 c->state = HTTPSTATE_RECEIVE_DATA;
1706 if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
1707 http_log("\nGot request:\n%s\n", c->buffer);
1710 if (c->stream->stream_type == STREAM_TYPE_STATUS)
1713 /* open input stream */
1714 if (open_input_stream(c, info) < 0) {
1715 snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
1719 /* prepare HTTP header */
1721 av_strlcatf(c->buffer, c->buffer_size, "HTTP/1.0 200 OK\r\n");
1722 mime_type = c->stream->fmt->mime_type;
1724 mime_type = "application/x-octet-stream";
1725 av_strlcatf(c->buffer, c->buffer_size, "Pragma: no-cache\r\n");
1727 /* for asf, we need extra headers */
1728 if (!strcmp(c->stream->fmt->name,"asf_stream")) {
1729 /* Need to allocate a client id */
1731 c->wmp_client_id = av_lfg_get(&random_state);
1733 av_strlcatf(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);
1735 av_strlcatf(c->buffer, c->buffer_size, "Content-Type: %s\r\n", mime_type);
1736 av_strlcatf(c->buffer, c->buffer_size, "\r\n");
1737 q = c->buffer + strlen(c->buffer);
1739 /* prepare output buffer */
1741 c->buffer_ptr = c->buffer;
1743 c->state = HTTPSTATE_SEND_HEADER;
1746 c->http_error = 404;
1749 snprintf(q, c->buffer_size,
1750 "HTTP/1.0 404 Not Found\r\n"
1751 "Content-type: text/html\r\n"
1754 "<head><title>404 Not Found</title></head>\n"
1758 /* prepare output buffer */
1759 c->buffer_ptr = c->buffer;
1761 c->state = HTTPSTATE_SEND_HEADER;
1765 /* horrible: we use this value to avoid
1766 * going to the send data state */
1767 c->http_error = 200;
1768 c->state = HTTPSTATE_SEND_HEADER;
1772 static void fmt_bytecount(AVIOContext *pb, int64_t count)
1774 static const char suffix[] = " kMGTP";
1777 for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
1779 avio_printf(pb, "%"PRId64"%c", count, *s);
1782 static inline void print_stream_params(AVIOContext *pb, FFServerStream *stream)
1785 const char *type = "unknown";
1786 char parameters[64];
1790 stream_no = stream->nb_streams;
1792 avio_printf(pb, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>"
1793 "type<th>kbit/s<th align=left>codec<th align=left>"
1796 for (i = 0; i < stream_no; i++) {
1797 st = stream->streams[i];
1798 codec = avcodec_find_encoder(st->codec->codec_id);
1802 switch(st->codec->codec_type) {
1803 case AVMEDIA_TYPE_AUDIO:
1805 snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz",
1806 st->codec->channels, st->codec->sample_rate);
1808 case AVMEDIA_TYPE_VIDEO:
1810 snprintf(parameters, sizeof(parameters),
1811 "%dx%d, q=%d-%d, fps=%d", st->codec->width,
1812 st->codec->height, st->codec->qmin, st->codec->qmax,
1813 st->codec->time_base.den / st->codec->time_base.num);
1819 avio_printf(pb, "<tr><td align=right>%d<td>%s<td align=right>%"PRId64
1821 i, type, (int64_t)st->codec->bit_rate/1000,
1822 codec ? codec->name : "", parameters);
1825 avio_printf(pb, "</table>\n");
1828 static void compute_status(HTTPContext *c)
1831 FFServerStream *stream;
1837 if (avio_open_dyn_buf(&pb) < 0) {
1838 /* XXX: return an error ? */
1839 c->buffer_ptr = c->buffer;
1840 c->buffer_end = c->buffer;
1844 avio_printf(pb, "HTTP/1.0 200 OK\r\n");
1845 avio_printf(pb, "Content-type: text/html\r\n");
1846 avio_printf(pb, "Pragma: no-cache\r\n");
1847 avio_printf(pb, "\r\n");
1849 avio_printf(pb, "<html><head><title>%s Status</title>\n", program_name);
1850 if (c->stream->feed_filename[0])
1851 avio_printf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n",
1852 c->stream->feed_filename);
1853 avio_printf(pb, "</head>\n<body>");
1854 avio_printf(pb, "<h1>%s Status</h1>\n", program_name);
1856 avio_printf(pb, "<h2>Available Streams</h2>\n");
1857 avio_printf(pb, "<table cellspacing=0 cellpadding=4>\n");
1858 avio_printf(pb, "<tr><th valign=top>Path<th align=left>Served<br>Conns<th><br>bytes<th valign=top>Format<th>Bit rate<br>kbit/s<th align=left>Video<br>kbit/s<th><br>Codec<th align=left>Audio<br>kbit/s<th><br>Codec<th align=left valign=top>Feed\n");
1859 stream = config.first_stream;
1861 char sfilename[1024];
1864 if (stream->feed == stream) {
1865 stream = stream->next;
1869 av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
1870 eosf = sfilename + strlen(sfilename);
1871 if (eosf - sfilename >= 4) {
1872 if (strcmp(eosf - 4, ".asf") == 0)
1873 strcpy(eosf - 4, ".asx");
1874 else if (strcmp(eosf - 3, ".rm") == 0)
1875 strcpy(eosf - 3, ".ram");
1876 else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
1877 /* generate a sample RTSP director if
1878 * unicast. Generate an SDP redirector if
1880 eosf = strrchr(sfilename, '.');
1882 eosf = sfilename + strlen(sfilename);
1883 if (stream->is_multicast)
1884 strcpy(eosf, ".sdp");
1886 strcpy(eosf, ".rtsp");
1890 avio_printf(pb, "<tr><td><a href=\"/%s\">%s</a> ",
1891 sfilename, stream->filename);
1892 avio_printf(pb, "<td align=right> %d <td align=right> ",
1893 stream->conns_served);
1894 fmt_bytecount(pb, stream->bytes_served);
1896 switch(stream->stream_type) {
1897 case STREAM_TYPE_LIVE: {
1898 int audio_bit_rate = 0;
1899 int video_bit_rate = 0;
1900 const char *audio_codec_name = "";
1901 const char *video_codec_name = "";
1902 const char *audio_codec_name_extra = "";
1903 const char *video_codec_name_extra = "";
1905 for(i=0;i<stream->nb_streams;i++) {
1906 AVStream *st = stream->streams[i];
1907 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1909 switch(st->codec->codec_type) {
1910 case AVMEDIA_TYPE_AUDIO:
1911 audio_bit_rate += st->codec->bit_rate;
1913 if (*audio_codec_name)
1914 audio_codec_name_extra = "...";
1915 audio_codec_name = codec->name;
1918 case AVMEDIA_TYPE_VIDEO:
1919 video_bit_rate += st->codec->bit_rate;
1921 if (*video_codec_name)
1922 video_codec_name_extra = "...";
1923 video_codec_name = codec->name;
1926 case AVMEDIA_TYPE_DATA:
1927 video_bit_rate += st->codec->bit_rate;
1934 avio_printf(pb, "<td align=center> %s <td align=right> %d "
1935 "<td align=right> %d <td> %s %s <td align=right> "
1937 stream->fmt->name, stream->bandwidth,
1938 video_bit_rate / 1000, video_codec_name,
1939 video_codec_name_extra, audio_bit_rate / 1000,
1940 audio_codec_name, audio_codec_name_extra);
1943 avio_printf(pb, "<td>%s", stream->feed->filename);
1945 avio_printf(pb, "<td>%s", stream->feed_filename);
1946 avio_printf(pb, "\n");
1950 avio_printf(pb, "<td align=center> - <td align=right> - "
1951 "<td align=right> - <td><td align=right> - <td>\n");
1954 stream = stream->next;
1956 avio_printf(pb, "</table>\n");
1958 stream = config.first_stream;
1961 if (stream->feed != stream) {
1962 stream = stream->next;
1966 avio_printf(pb, "<h2>Feed %s</h2>", stream->filename);
1968 avio_printf(pb, "Running as pid %"PRId64".\n", (int64_t) stream->pid);
1975 /* This is somewhat linux specific I guess */
1976 snprintf(ps_cmd, sizeof(ps_cmd),
1977 "ps -o \"%%cpu,cputime\" --no-headers %"PRId64"",
1978 (int64_t) stream->pid);
1980 pid_stat = popen(ps_cmd, "r");
1985 if (fscanf(pid_stat, "%9s %63s", cpuperc, cpuused) == 2) {
1986 avio_printf(pb, "Currently using %s%% of the cpu. "
1987 "Total time used %s.\n",
1995 avio_printf(pb, "<p>");
1998 print_stream_params(pb, stream);
1999 stream = stream->next;
2002 /* connection status */
2003 avio_printf(pb, "<h2>Connection Status</h2>\n");
2005 avio_printf(pb, "Number of connections: %d / %d<br>\n",
2006 nb_connections, config.nb_max_connections);
2008 avio_printf(pb, "Bandwidth in use: %"PRIu64"k / %"PRIu64"k<br>\n",
2009 current_bandwidth, config.max_bandwidth);
2011 avio_printf(pb, "<table>\n");
2012 avio_printf(pb, "<tr><th>#<th>File<th>IP<th>Proto<th>State<th>Target "
2013 "bit/s<th>Actual bit/s<th>Bytes transferred\n");
2014 c1 = first_http_ctx;
2022 for (j = 0; j < c1->stream->nb_streams; j++) {
2023 if (!c1->stream->feed)
2024 bitrate += c1->stream->streams[j]->codec->bit_rate;
2025 else if (c1->feed_streams[j] >= 0)
2026 bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
2031 p = inet_ntoa(c1->from_addr.sin_addr);
2032 avio_printf(pb, "<tr><td><b>%d</b><td>%s%s<td>%s<td>%s<td>%s"
2034 i, c1->stream ? c1->stream->filename : "",
2035 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "", p,
2036 c1->protocol, http_state[c1->state]);
2037 fmt_bytecount(pb, bitrate);
2038 avio_printf(pb, "<td align=right>");
2039 fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
2040 avio_printf(pb, "<td align=right>");
2041 fmt_bytecount(pb, c1->data_count);
2042 avio_printf(pb, "\n");
2045 avio_printf(pb, "</table>\n");
2050 avio_printf(pb, "<hr size=1 noshade>Generated at %s", p);
2051 avio_printf(pb, "</body>\n</html>\n");
2053 len = avio_close_dyn_buf(pb, &c->pb_buffer);
2054 c->buffer_ptr = c->pb_buffer;
2055 c->buffer_end = c->pb_buffer + len;
2058 static int open_input_stream(HTTPContext *c, const char *info)
2061 char input_filename[1024];
2062 AVFormatContext *s = NULL;
2063 int buf_size, i, ret;
2066 /* find file name */
2067 if (c->stream->feed) {
2068 strcpy(input_filename, c->stream->feed->feed_filename);
2069 buf_size = FFM_PACKET_SIZE;
2070 /* compute position (absolute time) */
2071 if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
2072 if ((ret = av_parse_time(&stream_pos, buf, 0)) < 0) {
2073 http_log("Invalid date specification '%s' for stream\n", buf);
2076 } else if (av_find_info_tag(buf, sizeof(buf), "buffer", info)) {
2077 int prebuffer = strtol(buf, 0, 10);
2078 stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
2080 stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
2082 strcpy(input_filename, c->stream->feed_filename);
2084 /* compute position (relative time) */
2085 if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
2086 if ((ret = av_parse_time(&stream_pos, buf, 1)) < 0) {
2087 http_log("Invalid date specification '%s' for stream\n", buf);
2093 if (!input_filename[0]) {
2094 http_log("No filename was specified for stream\n");
2095 return AVERROR(EINVAL);
2099 ret = avformat_open_input(&s, input_filename, c->stream->ifmt,
2100 &c->stream->in_opts);
2102 http_log("Could not open input '%s': %s\n",
2103 input_filename, av_err2str(ret));
2107 /* set buffer size */
2109 ret = ffio_set_buf_size(s->pb, buf_size);
2111 http_log("Failed to set buffer size\n");
2116 s->flags |= AVFMT_FLAG_GENPTS;
2118 if (strcmp(s->iformat->name, "ffm") &&
2119 (ret = avformat_find_stream_info(c->fmt_in, NULL)) < 0) {
2120 http_log("Could not find stream info for input '%s'\n", input_filename);
2121 avformat_close_input(&s);
2125 /* choose stream as clock source (we favor the video stream if
2126 * present) for packet sending */
2127 c->pts_stream_index = 0;
2128 for(i=0;i<c->stream->nb_streams;i++) {
2129 if (c->pts_stream_index == 0 &&
2130 c->stream->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2131 c->pts_stream_index = i;
2135 if (c->fmt_in->iformat->read_seek)
2136 av_seek_frame(c->fmt_in, -1, stream_pos, 0);
2137 /* set the start time (needed for maxtime and RTP packet timing) */
2138 c->start_time = cur_time;
2139 c->first_pts = AV_NOPTS_VALUE;
2143 /* return the server clock (in us) */
2144 static int64_t get_server_clock(HTTPContext *c)
2146 /* compute current pts value from system time */
2147 return (cur_time - c->start_time) * 1000;
2150 /* return the estimated time (in us) at which the current packet must be sent */
2151 static int64_t get_packet_send_clock(HTTPContext *c)
2153 int bytes_left, bytes_sent, frame_bytes;
2155 frame_bytes = c->cur_frame_bytes;
2156 if (frame_bytes <= 0)
2159 bytes_left = c->buffer_end - c->buffer_ptr;
2160 bytes_sent = frame_bytes - bytes_left;
2161 return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
2165 static int http_prepare_data(HTTPContext *c)
2168 AVFormatContext *ctx;
2170 av_freep(&c->pb_buffer);
2172 case HTTPSTATE_SEND_DATA_HEADER:
2173 ctx = avformat_alloc_context();
2175 return AVERROR(ENOMEM);
2178 av_dict_copy(&(c->fmt_ctx.metadata), c->stream->metadata, 0);
2179 c->fmt_ctx.streams = av_mallocz_array(c->stream->nb_streams,
2180 sizeof(AVStream *));
2181 if (!c->fmt_ctx.streams)
2182 return AVERROR(ENOMEM);
2184 for(i=0;i<c->stream->nb_streams;i++) {
2186 c->fmt_ctx.streams[i] = av_mallocz(sizeof(AVStream));
2188 /* if file or feed, then just take streams from FFServerStream
2190 if (!c->stream->feed ||
2191 c->stream->feed == c->stream)
2192 src = c->stream->streams[i];
2194 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2196 *(c->fmt_ctx.streams[i]) = *src;
2197 c->fmt_ctx.streams[i]->priv_data = 0;
2198 /* XXX: should be done in AVStream, not in codec */
2199 c->fmt_ctx.streams[i]->codec->frame_number = 0;
2201 /* set output format parameters */
2202 c->fmt_ctx.oformat = c->stream->fmt;
2203 c->fmt_ctx.nb_streams = c->stream->nb_streams;
2205 c->got_key_frame = 0;
2207 /* prepare header and save header data in a stream */
2208 if (avio_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2209 /* XXX: potential leak */
2212 c->fmt_ctx.pb->seekable = 0;
2215 * HACK to avoid MPEG-PS muxer to spit many underflow errors
2216 * Default value from FFmpeg
2217 * Try to set it using configuration option
2219 c->fmt_ctx.max_delay = (int)(0.7*AV_TIME_BASE);
2221 if ((ret = avformat_write_header(&c->fmt_ctx, NULL)) < 0) {
2222 http_log("Error writing output header for stream '%s': %s\n",
2223 c->stream->filename, av_err2str(ret));
2226 av_dict_free(&c->fmt_ctx.metadata);
2228 len = avio_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2229 c->buffer_ptr = c->pb_buffer;
2230 c->buffer_end = c->pb_buffer + len;
2232 c->state = HTTPSTATE_SEND_DATA;
2233 c->last_packet_sent = 0;
2235 case HTTPSTATE_SEND_DATA:
2236 /* find a new packet */
2237 /* read a packet from the input stream */
2238 if (c->stream->feed)
2239 ffm_set_write_index(c->fmt_in,
2240 c->stream->feed->feed_write_index,
2241 c->stream->feed->feed_size);
2243 if (c->stream->max_time &&
2244 c->stream->max_time + c->start_time - cur_time < 0)
2245 /* We have timed out */
2246 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2250 ret = av_read_frame(c->fmt_in, &pkt);
2252 if (c->stream->feed) {
2253 /* if coming from feed, it means we reached the end of the
2254 * ffm file, so must wait for more data */
2255 c->state = HTTPSTATE_WAIT_FEED;
2256 return 1; /* state changed */
2258 if (ret == AVERROR(EAGAIN)) {
2259 /* input not ready, come back later */
2262 if (c->stream->loop) {
2263 avformat_close_input(&c->fmt_in);
2264 if (open_input_stream(c, "") < 0)
2269 /* must send trailer now because EOF or error */
2270 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2273 int source_index = pkt.stream_index;
2274 /* update first pts if needed */
2275 if (c->first_pts == AV_NOPTS_VALUE) {
2276 c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2277 c->start_time = cur_time;
2279 /* send it to the appropriate stream */
2280 if (c->stream->feed) {
2281 /* if coming from a feed, select the right stream */
2282 if (c->switch_pending) {
2283 c->switch_pending = 0;
2284 for(i=0;i<c->stream->nb_streams;i++) {
2285 if (c->switch_feed_streams[i] == pkt.stream_index)
2286 if (pkt.flags & AV_PKT_FLAG_KEY)
2287 c->switch_feed_streams[i] = -1;
2288 if (c->switch_feed_streams[i] >= 0)
2289 c->switch_pending = 1;
2292 for(i=0;i<c->stream->nb_streams;i++) {
2293 if (c->stream->feed_streams[i] == pkt.stream_index) {
2294 AVStream *st = c->fmt_in->streams[source_index];
2295 pkt.stream_index = i;
2296 if (pkt.flags & AV_PKT_FLAG_KEY &&
2297 (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2298 c->stream->nb_streams == 1))
2299 c->got_key_frame = 1;
2300 if (!c->stream->send_on_key || c->got_key_frame)
2305 AVCodecContext *codec;
2306 AVStream *ist, *ost;
2308 ist = c->fmt_in->streams[source_index];
2309 /* specific handling for RTP: we use several
2310 * output streams (one for each RTP connection).
2311 * XXX: need more abstract handling */
2312 if (c->is_packetized) {
2313 /* compute send time and duration */
2314 c->cur_pts = av_rescale_q(pkt.dts, ist->time_base, AV_TIME_BASE_Q);
2315 c->cur_pts -= c->first_pts;
2316 c->cur_frame_duration = av_rescale_q(pkt.duration, ist->time_base, AV_TIME_BASE_Q);
2317 /* find RTP context */
2318 c->packet_stream_index = pkt.stream_index;
2319 ctx = c->rtp_ctx[c->packet_stream_index];
2321 av_packet_unref(&pkt);
2324 codec = ctx->streams[0]->codec;
2325 /* only one stream per RTP connection */
2326 pkt.stream_index = 0;
2330 codec = ctx->streams[pkt.stream_index]->codec;
2333 if (c->is_packetized) {
2334 int max_packet_size;
2335 if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP)
2336 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2338 max_packet_size = c->rtp_handles[c->packet_stream_index]->max_packet_size;
2339 ret = ffio_open_dyn_packet_buf(&ctx->pb,
2342 ret = avio_open_dyn_buf(&ctx->pb);
2345 /* XXX: potential leak */
2348 ost = ctx->streams[pkt.stream_index];
2350 ctx->pb->seekable = 0;
2351 if (pkt.dts != AV_NOPTS_VALUE)
2352 pkt.dts = av_rescale_q(pkt.dts, ist->time_base,
2354 if (pkt.pts != AV_NOPTS_VALUE)
2355 pkt.pts = av_rescale_q(pkt.pts, ist->time_base,
2357 pkt.duration = av_rescale_q(pkt.duration, ist->time_base,
2359 if ((ret = av_write_frame(ctx, &pkt)) < 0) {
2360 http_log("Error writing frame to output for stream '%s': %s\n",
2361 c->stream->filename, av_err2str(ret));
2362 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2365 av_freep(&c->pb_buffer);
2366 len = avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
2367 c->cur_frame_bytes = len;
2368 c->buffer_ptr = c->pb_buffer;
2369 c->buffer_end = c->pb_buffer + len;
2371 codec->frame_number++;
2373 av_packet_unref(&pkt);
2377 av_packet_unref(&pkt);
2382 case HTTPSTATE_SEND_DATA_TRAILER:
2383 /* last packet test ? */
2384 if (c->last_packet_sent || c->is_packetized)
2387 /* prepare header */
2388 if (avio_open_dyn_buf(&ctx->pb) < 0) {
2389 /* XXX: potential leak */
2392 c->fmt_ctx.pb->seekable = 0;
2393 av_write_trailer(ctx);
2394 len = avio_close_dyn_buf(ctx->pb, &c->pb_buffer);
2395 c->buffer_ptr = c->pb_buffer;
2396 c->buffer_end = c->pb_buffer + len;
2398 c->last_packet_sent = 1;
2404 /* should convert the format at the same time */
2405 /* send data starting at c->buffer_ptr to the output connection
2406 * (either UDP or TCP)
2408 static int http_send_data(HTTPContext *c)
2413 if (c->buffer_ptr >= c->buffer_end) {
2414 ret = http_prepare_data(c);
2418 /* state change requested */
2421 if (c->is_packetized) {
2422 /* RTP data output */
2423 len = c->buffer_end - c->buffer_ptr;
2425 /* fail safe - should never happen */
2427 c->buffer_ptr = c->buffer_end;
2430 len = (c->buffer_ptr[0] << 24) |
2431 (c->buffer_ptr[1] << 16) |
2432 (c->buffer_ptr[2] << 8) |
2434 if (len > (c->buffer_end - c->buffer_ptr))
2436 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2437 /* nothing to send yet: we can wait */
2441 c->data_count += len;
2442 update_datarate(&c->datarate, c->data_count);
2444 c->stream->bytes_served += len;
2446 if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP) {
2447 /* RTP packets are sent inside the RTSP TCP connection */
2449 int interleaved_index, size;
2451 HTTPContext *rtsp_c;
2454 /* if no RTSP connection left, error */
2457 /* if already sending something, then wait. */
2458 if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
2460 if (avio_open_dyn_buf(&pb) < 0)
2462 interleaved_index = c->packet_stream_index * 2;
2463 /* RTCP packets are sent at odd indexes */
2464 if (c->buffer_ptr[1] == 200)
2465 interleaved_index++;
2466 /* write RTSP TCP header */
2468 header[1] = interleaved_index;
2469 header[2] = len >> 8;
2471 avio_write(pb, header, 4);
2472 /* write RTP packet data */
2474 avio_write(pb, c->buffer_ptr, len);
2475 size = avio_close_dyn_buf(pb, &c->packet_buffer);
2476 /* prepare asynchronous TCP sending */
2477 rtsp_c->packet_buffer_ptr = c->packet_buffer;
2478 rtsp_c->packet_buffer_end = c->packet_buffer + size;
2479 c->buffer_ptr += len;
2481 /* send everything we can NOW */
2482 len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2483 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
2485 rtsp_c->packet_buffer_ptr += len;
2486 if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2487 /* if we could not send all the data, we will
2488 * send it later, so a new state is needed to
2489 * "lock" the RTSP TCP connection */
2490 rtsp_c->state = RTSPSTATE_SEND_PACKET;
2493 /* all data has been sent */
2494 av_freep(&c->packet_buffer);
2496 /* send RTP packet directly in UDP */
2498 ffurl_write(c->rtp_handles[c->packet_stream_index],
2499 c->buffer_ptr, len);
2500 c->buffer_ptr += len;
2501 /* here we continue as we can send several packets
2505 /* TCP data output */
2506 len = send(c->fd, c->buffer_ptr,
2507 c->buffer_end - c->buffer_ptr, 0);
2509 if (ff_neterrno() != AVERROR(EAGAIN) &&
2510 ff_neterrno() != AVERROR(EINTR))
2511 /* error : close connection */
2516 c->buffer_ptr += len;
2518 c->data_count += len;
2519 update_datarate(&c->datarate, c->data_count);
2521 c->stream->bytes_served += len;
2529 static int http_start_receive_data(HTTPContext *c)
2534 if (c->stream->feed_opened) {
2535 http_log("Stream feed '%s' was not opened\n",
2536 c->stream->feed_filename);
2537 return AVERROR(EINVAL);
2540 /* Don't permit writing to this one */
2541 if (c->stream->readonly) {
2542 http_log("Cannot write to read-only file '%s'\n",
2543 c->stream->feed_filename);
2544 return AVERROR(EINVAL);
2548 fd = open(c->stream->feed_filename, O_RDWR);
2550 ret = AVERROR(errno);
2551 http_log("Could not open feed file '%s': %s\n",
2552 c->stream->feed_filename, strerror(errno));
2557 if (c->stream->truncate) {
2558 /* truncate feed file */
2559 ffm_write_write_index(c->feed_fd, FFM_PACKET_SIZE);
2560 http_log("Truncating feed file '%s'\n", c->stream->feed_filename);
2561 if (ftruncate(c->feed_fd, FFM_PACKET_SIZE) < 0) {
2562 ret = AVERROR(errno);
2563 http_log("Error truncating feed file '%s': %s\n",
2564 c->stream->feed_filename, strerror(errno));
2568 ret = ffm_read_write_index(fd);
2570 http_log("Error reading write index from feed file '%s': %s\n",
2571 c->stream->feed_filename, strerror(errno));
2574 c->stream->feed_write_index = ret;
2577 c->stream->feed_write_index = FFMAX(ffm_read_write_index(fd),
2579 c->stream->feed_size = lseek(fd, 0, SEEK_END);
2580 lseek(fd, 0, SEEK_SET);
2582 /* init buffer input */
2583 c->buffer_ptr = c->buffer;
2584 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2585 c->stream->feed_opened = 1;
2586 c->chunked_encoding = !!av_stristr(c->buffer, "Transfer-Encoding: chunked");
2590 static int http_receive_data(HTTPContext *c)
2593 int len, loop_run = 0;
2595 while (c->chunked_encoding && !c->chunk_size &&
2596 c->buffer_end > c->buffer_ptr) {
2597 /* read chunk header, if present */
2598 len = recv(c->fd, c->buffer_ptr, 1, 0);
2601 if (ff_neterrno() != AVERROR(EAGAIN) &&
2602 ff_neterrno() != AVERROR(EINTR))
2603 /* error : close connection */
2606 } else if (len == 0) {
2607 /* end of connection : close it */
2609 } else if (c->buffer_ptr - c->buffer >= 2 &&
2610 !memcmp(c->buffer_ptr - 1, "\r\n", 2)) {
2611 c->chunk_size = strtol(c->buffer, 0, 16);
2612 if (c->chunk_size == 0) // end of stream
2614 c->buffer_ptr = c->buffer;
2616 } else if (++loop_run > 10)
2617 /* no chunk header, abort */
2623 if (c->buffer_end > c->buffer_ptr) {
2624 len = recv(c->fd, c->buffer_ptr,
2625 FFMIN(c->chunk_size, c->buffer_end - c->buffer_ptr), 0);
2627 if (ff_neterrno() != AVERROR(EAGAIN) &&
2628 ff_neterrno() != AVERROR(EINTR))
2629 /* error : close connection */
2631 } else if (len == 0)
2632 /* end of connection : close it */
2635 c->chunk_size -= len;
2636 c->buffer_ptr += len;
2637 c->data_count += len;
2638 update_datarate(&c->datarate, c->data_count);
2642 if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2643 if (c->buffer[0] != 'f' ||
2644 c->buffer[1] != 'm') {
2645 http_log("Feed stream has become desynchronized -- disconnecting\n");
2650 if (c->buffer_ptr >= c->buffer_end) {
2651 FFServerStream *feed = c->stream;
2652 /* a packet has been received : write it in the store, except
2654 if (c->data_count > FFM_PACKET_SIZE) {
2655 /* XXX: use llseek or url_seek
2656 * XXX: Should probably fail? */
2657 if (lseek(c->feed_fd, feed->feed_write_index, SEEK_SET) == -1)
2658 http_log("Seek to %"PRId64" failed\n", feed->feed_write_index);
2660 if (write(c->feed_fd, c->buffer, FFM_PACKET_SIZE) < 0) {
2661 http_log("Error writing to feed file: %s\n", strerror(errno));
2665 feed->feed_write_index += FFM_PACKET_SIZE;
2666 /* update file size */
2667 if (feed->feed_write_index > c->stream->feed_size)
2668 feed->feed_size = feed->feed_write_index;
2670 /* handle wrap around if max file size reached */
2671 if (c->stream->feed_max_size &&
2672 feed->feed_write_index >= c->stream->feed_max_size)
2673 feed->feed_write_index = FFM_PACKET_SIZE;
2676 if (ffm_write_write_index(c->feed_fd, feed->feed_write_index) < 0) {
2677 http_log("Error writing index to feed file: %s\n",
2682 /* wake up any waiting connections */
2683 for(c1 = first_http_ctx; c1; c1 = c1->next) {
2684 if (c1->state == HTTPSTATE_WAIT_FEED &&
2685 c1->stream->feed == c->stream->feed)
2686 c1->state = HTTPSTATE_SEND_DATA;
2689 /* We have a header in our hands that contains useful data */
2690 AVFormatContext *s = avformat_alloc_context();
2692 AVInputFormat *fmt_in;
2698 /* use feed output format name to find corresponding input format */
2699 fmt_in = av_find_input_format(feed->fmt->name);
2703 pb = avio_alloc_context(c->buffer, c->buffer_end - c->buffer,
2704 0, NULL, NULL, NULL, NULL);
2711 if (avformat_open_input(&s, c->stream->feed_filename, fmt_in, NULL) < 0) {
2716 /* Now we have the actual streams */
2717 if (s->nb_streams != feed->nb_streams) {
2718 avformat_close_input(&s);
2720 http_log("Feed '%s' stream number does not match registered feed\n",
2721 c->stream->feed_filename);
2725 for (i = 0; i < s->nb_streams; i++) {
2726 AVStream *fst = feed->streams[i];
2727 AVStream *st = s->streams[i];
2728 avcodec_copy_context(fst->codec, st->codec);
2731 avformat_close_input(&s);
2734 c->buffer_ptr = c->buffer;
2739 c->stream->feed_opened = 0;
2741 /* wake up any waiting connections to stop waiting for feed */
2742 for(c1 = first_http_ctx; c1; c1 = c1->next) {
2743 if (c1->state == HTTPSTATE_WAIT_FEED &&
2744 c1->stream->feed == c->stream->feed)
2745 c1->state = HTTPSTATE_SEND_DATA_TRAILER;
2750 /********************************************************************/
2753 static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2760 str = RTSP_STATUS_CODE2STRING(error_number);
2762 str = "Unknown Error";
2764 avio_printf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2765 avio_printf(c->pb, "CSeq: %d\r\n", c->seq);
2767 /* output GMT time */
2770 strftime(buf2, sizeof(buf2), "%a, %d %b %Y %H:%M:%S", tm);
2771 avio_printf(c->pb, "Date: %s GMT\r\n", buf2);
2774 static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2776 rtsp_reply_header(c, error_number);
2777 avio_printf(c->pb, "\r\n");
2780 static int rtsp_parse_request(HTTPContext *c)
2782 const char *p, *p1, *p2;
2788 RTSPMessageHeader header1 = { 0 }, *header = &header1;
2790 c->buffer_ptr[0] = '\0';
2793 get_word(cmd, sizeof(cmd), &p);
2794 get_word(url, sizeof(url), &p);
2795 get_word(protocol, sizeof(protocol), &p);
2797 av_strlcpy(c->method, cmd, sizeof(c->method));
2798 av_strlcpy(c->url, url, sizeof(c->url));
2799 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2801 if (avio_open_dyn_buf(&c->pb) < 0) {
2802 /* XXX: cannot do more */
2803 c->pb = NULL; /* safety */
2807 /* check version name */
2808 if (strcmp(protocol, "RTSP/1.0")) {
2809 rtsp_reply_error(c, RTSP_STATUS_VERSION);
2813 /* parse each header line */
2814 /* skip to next line */
2815 while (*p != '\n' && *p != '\0')
2819 while (*p != '\0') {
2820 p1 = memchr(p, '\n', (char *)c->buffer_ptr - p);
2824 if (p2 > p && p2[-1] == '\r')
2826 /* skip empty line */
2830 if (len > sizeof(line) - 1)
2831 len = sizeof(line) - 1;
2832 memcpy(line, p, len);
2834 ff_rtsp_parse_line(header, line, NULL, NULL);
2838 /* handle sequence number */
2839 c->seq = header->seq;
2841 if (!strcmp(cmd, "DESCRIBE"))
2842 rtsp_cmd_describe(c, url);
2843 else if (!strcmp(cmd, "OPTIONS"))
2844 rtsp_cmd_options(c, url);
2845 else if (!strcmp(cmd, "SETUP"))
2846 rtsp_cmd_setup(c, url, header);
2847 else if (!strcmp(cmd, "PLAY"))
2848 rtsp_cmd_play(c, url, header);
2849 else if (!strcmp(cmd, "PAUSE"))
2850 rtsp_cmd_interrupt(c, url, header, 1);
2851 else if (!strcmp(cmd, "TEARDOWN"))
2852 rtsp_cmd_interrupt(c, url, header, 0);
2854 rtsp_reply_error(c, RTSP_STATUS_METHOD);
2857 len = avio_close_dyn_buf(c->pb, &c->pb_buffer);
2858 c->pb = NULL; /* safety */
2860 /* XXX: cannot do more */
2863 c->buffer_ptr = c->pb_buffer;
2864 c->buffer_end = c->pb_buffer + len;
2865 c->state = RTSPSTATE_SEND_REPLY;
2869 static int prepare_sdp_description(FFServerStream *stream, uint8_t **pbuffer,
2870 struct in_addr my_ip)
2872 AVFormatContext *avc;
2873 AVStream *avs = NULL;
2874 AVOutputFormat *rtp_format = av_guess_format("rtp", NULL, NULL);
2875 AVDictionaryEntry *entry = av_dict_get(stream->metadata, "title", NULL, 0);
2880 avc = avformat_alloc_context();
2881 if (!avc || !rtp_format)
2884 avc->oformat = rtp_format;
2885 av_dict_set(&avc->metadata, "title",
2886 entry ? entry->value : "No Title", 0);
2887 avc->nb_streams = stream->nb_streams;
2888 if (stream->is_multicast) {
2889 snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2890 inet_ntoa(stream->multicast_ip),
2891 stream->multicast_port, stream->multicast_ttl);
2893 snprintf(avc->filename, 1024, "rtp://0.0.0.0");
2895 avc->streams = av_malloc_array(avc->nb_streams, sizeof(*avc->streams));
2899 avs = av_malloc_array(avc->nb_streams, sizeof(*avs));
2903 for(i = 0; i < stream->nb_streams; i++) {
2904 avc->streams[i] = &avs[i];
2905 avc->streams[i]->codec = stream->streams[i]->codec;
2907 *pbuffer = av_mallocz(2048);
2910 av_sdp_create(&avc, 1, *pbuffer, 2048);
2913 av_freep(&avc->streams);
2914 av_dict_free(&avc->metadata);
2918 return *pbuffer ? strlen(*pbuffer) : AVERROR(ENOMEM);
2921 static void rtsp_cmd_options(HTTPContext *c, const char *url)
2923 /* rtsp_reply_header(c, RTSP_STATUS_OK); */
2924 avio_printf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2925 avio_printf(c->pb, "CSeq: %d\r\n", c->seq);
2926 avio_printf(c->pb, "Public: %s\r\n",
2927 "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2928 avio_printf(c->pb, "\r\n");
2931 static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2933 FFServerStream *stream;
2939 struct sockaddr_in my_addr;
2941 /* find which URL is asked */
2942 av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2947 for(stream = config.first_stream; stream; stream = stream->next) {
2948 if (!stream->is_feed &&
2949 stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2950 !strcmp(path, stream->filename)) {
2954 /* no stream found */
2955 rtsp_reply_error(c, RTSP_STATUS_NOT_FOUND);
2959 /* prepare the media description in SDP format */
2961 /* get the host IP */
2962 len = sizeof(my_addr);
2963 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
2964 content_length = prepare_sdp_description(stream, &content,
2966 if (content_length < 0) {
2967 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2970 rtsp_reply_header(c, RTSP_STATUS_OK);
2971 avio_printf(c->pb, "Content-Base: %s/\r\n", url);
2972 avio_printf(c->pb, "Content-Type: application/sdp\r\n");
2973 avio_printf(c->pb, "Content-Length: %d\r\n", content_length);
2974 avio_printf(c->pb, "\r\n");
2975 avio_write(c->pb, content, content_length);
2979 static HTTPContext *find_rtp_session(const char *session_id)
2983 if (session_id[0] == '\0')
2986 for(c = first_http_ctx; c; c = c->next) {
2987 if (!strcmp(c->session_id, session_id))
2993 static RTSPTransportField *find_transport(RTSPMessageHeader *h, enum RTSPLowerTransport lower_transport)
2995 RTSPTransportField *th;
2998 for(i=0;i<h->nb_transports;i++) {
2999 th = &h->transports[i];
3000 if (th->lower_transport == lower_transport)
3006 static void rtsp_cmd_setup(HTTPContext *c, const char *url,
3007 RTSPMessageHeader *h)
3009 FFServerStream *stream;
3010 int stream_index, rtp_port, rtcp_port;
3015 RTSPTransportField *th;
3016 struct sockaddr_in dest_addr;
3017 RTSPActionServerSetup setup;
3019 /* find which URL is asked */
3020 av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
3025 /* now check each stream */
3026 for(stream = config.first_stream; stream; stream = stream->next) {
3027 if (stream->is_feed || !stream->fmt ||
3028 strcmp(stream->fmt->name, "rtp")) {
3031 /* accept aggregate filenames only if single stream */
3032 if (!strcmp(path, stream->filename)) {
3033 if (stream->nb_streams != 1) {
3034 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
3041 for(stream_index = 0; stream_index < stream->nb_streams;
3043 snprintf(buf, sizeof(buf), "%s/streamid=%d",
3044 stream->filename, stream_index);
3045 if (!strcmp(path, buf))
3049 /* no stream found */
3050 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
3054 /* generate session id if needed */
3055 if (h->session_id[0] == '\0') {
3056 unsigned random0 = av_lfg_get(&random_state);
3057 unsigned random1 = av_lfg_get(&random_state);
3058 snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
3062 /* find RTP session, and create it if none found */
3063 rtp_c = find_rtp_session(h->session_id);
3065 /* always prefer UDP */
3066 th = find_transport(h, RTSP_LOWER_TRANSPORT_UDP);
3068 th = find_transport(h, RTSP_LOWER_TRANSPORT_TCP);
3070 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
3075 rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
3076 th->lower_transport);
3078 rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
3082 /* open input stream */
3083 if (open_input_stream(rtp_c, "") < 0) {
3084 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
3089 /* test if stream is OK (test needed because several SETUP needs
3090 * to be done for a given file) */
3091 if (rtp_c->stream != stream) {
3092 rtsp_reply_error(c, RTSP_STATUS_SERVICE);
3096 /* test if stream is already set up */
3097 if (rtp_c->rtp_ctx[stream_index]) {
3098 rtsp_reply_error(c, RTSP_STATUS_STATE);
3102 /* check transport */
3103 th = find_transport(h, rtp_c->rtp_protocol);
3104 if (!th || (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
3105 th->client_port_min <= 0)) {
3106 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
3110 /* setup default options */
3111 setup.transport_option[0] = '\0';
3112 dest_addr = rtp_c->from_addr;
3113 dest_addr.sin_port = htons(th->client_port_min);
3116 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
3117 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
3121 /* now everything is OK, so we can send the connection parameters */
3122 rtsp_reply_header(c, RTSP_STATUS_OK);
3124 avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3126 switch(rtp_c->rtp_protocol) {
3127 case RTSP_LOWER_TRANSPORT_UDP:
3128 rtp_port = ff_rtp_get_local_rtp_port(rtp_c->rtp_handles[stream_index]);
3129 rtcp_port = ff_rtp_get_local_rtcp_port(rtp_c->rtp_handles[stream_index]);
3130 avio_printf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
3131 "client_port=%d-%d;server_port=%d-%d",
3132 th->client_port_min, th->client_port_max,
3133 rtp_port, rtcp_port);
3135 case RTSP_LOWER_TRANSPORT_TCP:
3136 avio_printf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
3137 stream_index * 2, stream_index * 2 + 1);
3142 if (setup.transport_option[0] != '\0')
3143 avio_printf(c->pb, ";%s", setup.transport_option);
3144 avio_printf(c->pb, "\r\n");
3147 avio_printf(c->pb, "\r\n");
3152 * find an RTP connection by using the session ID. Check consistency
3155 static HTTPContext *find_rtp_session_with_url(const char *url,
3156 const char *session_id)
3164 rtp_c = find_rtp_session(session_id);
3168 /* find which URL is asked */
3169 av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
3173 if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
3174 for(s=0; s<rtp_c->stream->nb_streams; ++s) {
3175 snprintf(buf, sizeof(buf), "%s/streamid=%d",
3176 rtp_c->stream->filename, s);
3177 if(!strncmp(path, buf, sizeof(buf)))
3178 /* XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE
3179 * if nb_streams>1? */
3183 if (len > 0 && path[len - 1] == '/' &&
3184 !strncmp(path, rtp_c->stream->filename, len - 1))
3189 static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPMessageHeader *h)
3193 rtp_c = find_rtp_session_with_url(url, h->session_id);
3195 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3199 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3200 rtp_c->state != HTTPSTATE_WAIT_FEED &&
3201 rtp_c->state != HTTPSTATE_READY) {
3202 rtsp_reply_error(c, RTSP_STATUS_STATE);
3206 rtp_c->state = HTTPSTATE_SEND_DATA;
3208 /* now everything is OK, so we can send the connection parameters */
3209 rtsp_reply_header(c, RTSP_STATUS_OK);
3211 avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3212 avio_printf(c->pb, "\r\n");
3215 static void rtsp_cmd_interrupt(HTTPContext *c, const char *url,
3216 RTSPMessageHeader *h, int pause_only)
3220 rtp_c = find_rtp_session_with_url(url, h->session_id);
3222 rtsp_reply_error(c, RTSP_STATUS_SESSION);
3227 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
3228 rtp_c->state != HTTPSTATE_WAIT_FEED) {
3229 rtsp_reply_error(c, RTSP_STATUS_STATE);
3232 rtp_c->state = HTTPSTATE_READY;
3233 rtp_c->first_pts = AV_NOPTS_VALUE;
3236 /* now everything is OK, so we can send the connection parameters */
3237 rtsp_reply_header(c, RTSP_STATUS_OK);
3239 avio_printf(c->pb, "Session: %s\r\n", rtp_c->session_id);
3240 avio_printf(c->pb, "\r\n");
3243 close_connection(rtp_c);
3246 /********************************************************************/
3249 static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
3250 FFServerStream *stream,
3251 const char *session_id,
3252 enum RTSPLowerTransport rtp_protocol)
3254 HTTPContext *c = NULL;
3255 const char *proto_str;
3257 /* XXX: should output a warning page when coming
3258 * close to the connection limit */
3259 if (nb_connections >= config.nb_max_connections)
3262 /* add a new connection */
3263 c = av_mallocz(sizeof(HTTPContext));
3268 c->poll_entry = NULL;
3269 c->from_addr = *from_addr;
3270 c->buffer_size = IOBUFFER_INIT_SIZE;
3271 c->buffer = av_malloc(c->buffer_size);
3276 av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
3277 c->state = HTTPSTATE_READY;
3278 c->is_packetized = 1;
3279 c->rtp_protocol = rtp_protocol;
3281 /* protocol is shown in statistics */
3282 switch(c->rtp_protocol) {
3283 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
3284 proto_str = "MCAST";
3286 case RTSP_LOWER_TRANSPORT_UDP:
3289 case RTSP_LOWER_TRANSPORT_TCP:
3296 av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
3297 av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
3299 current_bandwidth += stream->bandwidth;
3301 c->next = first_http_ctx;
3307 av_freep(&c->buffer);
3314 * add a new RTP stream in an RTP connection (used in RTSP SETUP
3315 * command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3318 static int rtp_new_av_stream(HTTPContext *c,
3319 int stream_index, struct sockaddr_in *dest_addr,
3320 HTTPContext *rtsp_c)
3322 AVFormatContext *ctx;
3325 URLContext *h = NULL;
3327 int max_packet_size;
3329 /* now we can open the relevant output stream */
3330 ctx = avformat_alloc_context();
3333 ctx->oformat = av_guess_format("rtp", NULL, NULL);
3335 st = av_mallocz(sizeof(AVStream));
3338 ctx->nb_streams = 1;
3339 ctx->streams = av_mallocz_array(ctx->nb_streams, sizeof(AVStream *));
3342 ctx->streams[0] = st;
3344 if (!c->stream->feed ||
3345 c->stream->feed == c->stream)
3346 memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
3349 c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3351 st->priv_data = NULL;
3353 /* build destination RTP address */
3354 ipaddr = inet_ntoa(dest_addr->sin_addr);
3356 switch(c->rtp_protocol) {
3357 case RTSP_LOWER_TRANSPORT_UDP:
3358 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
3361 /* XXX: also pass as parameter to function ? */
3362 if (c->stream->is_multicast) {
3364 ttl = c->stream->multicast_ttl;
3367 snprintf(ctx->filename, sizeof(ctx->filename),
3368 "rtp://%s:%d?multicast=1&ttl=%d",
3369 ipaddr, ntohs(dest_addr->sin_port), ttl);
3371 snprintf(ctx->filename, sizeof(ctx->filename),
3372 "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3375 if (ffurl_open(&h, ctx->filename, AVIO_FLAG_WRITE, NULL, NULL) < 0)
3377 c->rtp_handles[stream_index] = h;
3378 max_packet_size = h->max_packet_size;
3380 case RTSP_LOWER_TRANSPORT_TCP:
3383 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3389 http_log("%s:%d - - \"PLAY %s/streamid=%d %s\"\n",
3390 ipaddr, ntohs(dest_addr->sin_port),
3391 c->stream->filename, stream_index, c->protocol);
3393 /* normally, no packets should be output here, but the packet size may
3395 if (ffio_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0)
3396 /* XXX: close stream */
3399 if (avformat_write_header(ctx, NULL) < 0) {
3407 avio_close_dyn_buf(ctx->pb, &dummy_buf);
3410 c->rtp_ctx[stream_index] = ctx;
3414 /********************************************************************/
3415 /* ffserver initialization */
3417 static AVStream *add_av_stream1(FFServerStream *stream,
3418 AVCodecContext *codec, int copy)
3422 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
3425 fst = av_mallocz(sizeof(AVStream));
3429 fst->codec = avcodec_alloc_context3(codec->codec);
3434 avcodec_copy_context(fst->codec, codec);
3436 /* live streams must use the actual feed's codec since it may be
3437 * updated later to carry extradata needed by them.
3441 fst->priv_data = av_mallocz(sizeof(FeedData));
3442 fst->index = stream->nb_streams;
3443 avpriv_set_pts_info(fst, 33, 1, 90000);
3444 fst->sample_aspect_ratio = codec->sample_aspect_ratio;
3445 stream->streams[stream->nb_streams++] = fst;
3449 /* return the stream number in the feed */
3450 static int add_av_stream(FFServerStream *feed, AVStream *st)
3453 AVCodecContext *av, *av1;
3457 for(i=0;i<feed->nb_streams;i++) {
3458 av1 = feed->streams[i]->codec;
3459 if (av1->codec_id == av->codec_id &&
3460 av1->codec_type == av->codec_type &&
3461 av1->bit_rate == av->bit_rate) {
3463 switch(av->codec_type) {
3464 case AVMEDIA_TYPE_AUDIO:
3465 if (av1->channels == av->channels &&
3466 av1->sample_rate == av->sample_rate)
3469 case AVMEDIA_TYPE_VIDEO:
3470 if (av1->width == av->width &&
3471 av1->height == av->height &&
3472 av1->time_base.den == av->time_base.den &&
3473 av1->time_base.num == av->time_base.num &&
3474 av1->gop_size == av->gop_size)
3483 fst = add_av_stream1(feed, av, 0);
3486 if (av_stream_get_recommended_encoder_configuration(st))
3487 av_stream_set_recommended_encoder_configuration(fst,
3488 av_strdup(av_stream_get_recommended_encoder_configuration(st)));
3489 return feed->nb_streams - 1;
3492 static void remove_stream(FFServerStream *stream)
3494 FFServerStream **ps;
3495 ps = &config.first_stream;
3504 /* specific MPEG4 handling : we extract the raw parameters */
3505 static void extract_mpeg4_header(AVFormatContext *infile)
3507 int mpeg4_count, i, size;
3512 infile->flags |= AVFMT_FLAG_NOFILLIN | AVFMT_FLAG_NOPARSE;
3515 for(i=0;i<infile->nb_streams;i++) {
3516 st = infile->streams[i];
3517 if (st->codec->codec_id == AV_CODEC_ID_MPEG4 &&
3518 st->codec->extradata_size == 0) {
3525 printf("MPEG4 without extra data: trying to find header in %s\n",
3527 while (mpeg4_count > 0) {
3528 if (av_read_frame(infile, &pkt) < 0)
3530 st = infile->streams[pkt.stream_index];
3531 if (st->codec->codec_id == AV_CODEC_ID_MPEG4 &&
3532 st->codec->extradata_size == 0) {
3533 av_freep(&st->codec->extradata);
3534 /* fill extradata with the header */
3535 /* XXX: we make hard suppositions here ! */
3537 while (p < pkt.data + pkt.size - 4) {
3538 /* stop when vop header is found */
3539 if (p[0] == 0x00 && p[1] == 0x00 &&
3540 p[2] == 0x01 && p[3] == 0xb6) {
3541 size = p - pkt.data;
3542 st->codec->extradata = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
3543 st->codec->extradata_size = size;
3544 memcpy(st->codec->extradata, pkt.data, size);
3551 av_packet_unref(&pkt);
3555 /* compute the needed AVStream for each file */
3556 static void build_file_streams(void)
3558 FFServerStream *stream, *stream_next;
3561 /* gather all streams */
3562 for(stream = config.first_stream; stream; stream = stream_next) {
3563 AVFormatContext *infile = NULL;
3564 stream_next = stream->next;
3565 if (stream->stream_type == STREAM_TYPE_LIVE &&
3567 /* the stream comes from a file */
3568 /* try to open the file */
3570 if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
3571 /* specific case : if transport stream output to RTP,
3572 * we use a raw transport stream reader */
3573 av_dict_set(&stream->in_opts, "mpeg2ts_compute_pcr", "1", 0);
3576 if (!stream->feed_filename[0]) {
3577 http_log("Unspecified feed file for stream '%s'\n",
3582 http_log("Opening feed file '%s' for stream '%s'\n",
3583 stream->feed_filename, stream->filename);
3584 ret = avformat_open_input(&infile, stream->feed_filename,
3585 stream->ifmt, &stream->in_opts);
3587 http_log("Could not open '%s': %s\n", stream->feed_filename,
3589 /* remove stream (no need to spend more time on it) */
3591 remove_stream(stream);
3593 /* find all the AVStreams inside and reference them in
3595 if (avformat_find_stream_info(infile, NULL) < 0) {
3596 http_log("Could not find codec parameters from '%s'\n",
3597 stream->feed_filename);
3598 avformat_close_input(&infile);
3601 extract_mpeg4_header(infile);
3603 for(i=0;i<infile->nb_streams;i++)
3604 add_av_stream1(stream, infile->streams[i]->codec, 1);
3606 avformat_close_input(&infile);
3612 /* compute the needed AVStream for each feed */
3613 static void build_feed_streams(void)
3615 FFServerStream *stream, *feed;
3618 /* gather all streams */
3619 for(stream = config.first_stream; stream; stream = stream->next) {
3620 feed = stream->feed;
3624 if (stream->is_feed) {
3625 for(i=0;i<stream->nb_streams;i++)
3626 stream->feed_streams[i] = i;
3628 /* we handle a stream coming from a feed */
3629 for(i=0;i<stream->nb_streams;i++)
3630 stream->feed_streams[i] = add_av_stream(feed,
3631 stream->streams[i]);
3635 /* create feed files if needed */
3636 for(feed = config.first_feed; feed; feed = feed->next_feed) {
3639 if (avio_check(feed->feed_filename, AVIO_FLAG_READ) > 0) {
3640 /* See if it matches */
3641 AVFormatContext *s = NULL;
3644 if (avformat_open_input(&s, feed->feed_filename, NULL, NULL) >= 0) {
3645 /* set buffer size */
3646 int ret = ffio_set_buf_size(s->pb, FFM_PACKET_SIZE);
3648 http_log("Failed to set buffer size\n");
3652 /* Now see if it matches */
3653 if (s->nb_streams == feed->nb_streams) {
3655 for(i=0;i<s->nb_streams;i++) {
3657 sf = feed->streams[i];
3660 if (sf->index != ss->index ||
3662 http_log("Index & Id do not match for stream %d (%s)\n",
3663 i, feed->feed_filename);
3666 AVCodecContext *ccf, *ccs;
3670 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3672 if (CHECK_CODEC(codec_id) || CHECK_CODEC(codec_type)) {
3673 http_log("Codecs do not match for stream %d\n", i);
3675 } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3676 http_log("Codec bitrates do not match for stream %d\n", i);
3678 } else if (ccf->codec_type == AVMEDIA_TYPE_VIDEO) {
3679 if (CHECK_CODEC(time_base.den) ||
3680 CHECK_CODEC(time_base.num) ||
3681 CHECK_CODEC(width) ||
3682 CHECK_CODEC(height)) {
3683 http_log("Codec width, height and framerate do not match for stream %d\n", i);
3686 } else if (ccf->codec_type == AVMEDIA_TYPE_AUDIO) {
3687 if (CHECK_CODEC(sample_rate) ||
3688 CHECK_CODEC(channels) ||
3689 CHECK_CODEC(frame_size)) {
3690 http_log("Codec sample_rate, channels, frame_size do not match for stream %d\n", i);
3694 http_log("Unknown codec type\n");
3702 http_log("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3703 feed->feed_filename, s->nb_streams, feed->nb_streams);
3705 avformat_close_input(&s);
3707 http_log("Deleting feed file '%s' as it appears to be corrupt\n",
3708 feed->feed_filename);
3711 if (feed->readonly) {
3712 http_log("Unable to delete feed file '%s' as it is marked readonly\n",
3713 feed->feed_filename);
3716 unlink(feed->feed_filename);
3719 if (avio_check(feed->feed_filename, AVIO_FLAG_WRITE) <= 0) {
3720 AVFormatContext *s = avformat_alloc_context();
3723 http_log("Failed to allocate context\n");
3727 if (feed->readonly) {
3728 http_log("Unable to create feed file '%s' as it is marked readonly\n",
3729 feed->feed_filename);
3733 /* only write the header of the ffm file */
3734 if (avio_open(&s->pb, feed->feed_filename, AVIO_FLAG_WRITE) < 0) {
3735 http_log("Could not open output feed file '%s'\n",
3736 feed->feed_filename);
3739 s->oformat = feed->fmt;
3740 s->nb_streams = feed->nb_streams;
3741 s->streams = feed->streams;
3742 if (avformat_write_header(s, NULL) < 0) {
3743 http_log("Container doesn't support the required parameters\n");
3746 /* XXX: need better API */
3747 av_freep(&s->priv_data);
3748 avio_closep(&s->pb);
3751 avformat_free_context(s);
3753 /* get feed size and write index */
3754 fd = open(feed->feed_filename, O_RDONLY);
3756 http_log("Could not open output feed file '%s'\n",
3757 feed->feed_filename);
3761 feed->feed_write_index = FFMAX(ffm_read_write_index(fd), FFM_PACKET_SIZE);
3762 feed->feed_size = lseek(fd, 0, SEEK_END);
3763 /* ensure that we do not wrap before the end of file */
3764 if (feed->feed_max_size && feed->feed_max_size < feed->feed_size)
3765 feed->feed_max_size = feed->feed_size;
3771 /* compute the bandwidth used by each stream */
3772 static void compute_bandwidth(void)
3776 FFServerStream *stream;
3778 for(stream = config.first_stream; stream; stream = stream->next) {
3780 for(i=0;i<stream->nb_streams;i++) {
3781 AVStream *st = stream->streams[i];
3782 switch(st->codec->codec_type) {
3783 case AVMEDIA_TYPE_AUDIO:
3784 case AVMEDIA_TYPE_VIDEO:
3785 bandwidth += st->codec->bit_rate;
3791 stream->bandwidth = (bandwidth + 999) / 1000;
3795 static void handle_child_exit(int sig)
3800 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
3801 FFServerStream *feed;
3803 for (feed = config.first_feed; feed; feed = feed->next) {
3804 if (feed->pid != pid)
3807 uptime = time(0) - feed->pid_start;
3810 "%s: Pid %"PRId64" exited with status %d after %d seconds\n",
3811 feed->filename, (int64_t) pid, status, uptime);
3814 /* Turn off any more restarts */
3815 ffserver_free_child_args(&feed->child_argv);
3819 need_to_start_children = 1;
3822 static void opt_debug(void)
3825 snprintf(config.logfilename, sizeof(config.logfilename), "-");
3828 void show_help_default(const char *opt, const char *arg)
3830 printf("usage: ffserver [options]\n"
3831 "Hyper fast multi format Audio/Video streaming server\n");
3833 show_help_options(options, "Main options:", 0, 0, 0);
3836 static const OptionDef options[] = {
3837 #include "cmdutils_common_opts.h"
3838 { "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
3839 { "d", 0, {(void*)opt_debug}, "enable debug mode" },
3840 { "f", HAS_ARG | OPT_STRING, {(void*)&config.filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
3844 int main(int argc, char **argv)
3846 struct sigaction sigact = { { 0 } };
3849 config.filename = av_strdup("/etc/ffserver.conf");
3851 parse_loglevel(argc, argv, options);
3853 avformat_network_init();
3855 show_banner(argc, argv, options);
3857 my_program_name = argv[0];
3859 parse_options(NULL, argc, argv, options, NULL);
3861 unsetenv("http_proxy"); /* Kill the http_proxy */
3863 av_lfg_init(&random_state, av_get_random_seed());
3865 sigact.sa_handler = handle_child_exit;
3866 sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
3867 sigaction(SIGCHLD, &sigact, 0);
3869 if ((ret = ffserver_parse_ffconfig(config.filename, &config)) < 0) {
3870 fprintf(stderr, "Error reading configuration file '%s': %s\n",
3871 config.filename, av_err2str(ret));
3872 av_freep(&config.filename);
3875 av_freep(&config.filename);
3877 /* open log file if needed */
3878 if (config.logfilename[0] != '\0') {
3879 if (!strcmp(config.logfilename, "-"))
3882 logfile = fopen(config.logfilename, "a");
3883 av_log_set_callback(http_av_log);
3886 build_file_streams();
3888 build_feed_streams();
3890 compute_bandwidth();
3893 signal(SIGPIPE, SIG_IGN);
3895 if (http_server() < 0) {
3896 http_log("Could not start server\n");