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 */
240 int truncate
; /* True if feeder connection truncate the feed file */
242 int64_t bytes_served
;
243 int64_t feed_max_size
; /* maximum storage size, zero means unlimited */
244 int64_t feed_write_index
; /* current write position in feed (it wraps around) */
245 int64_t feed_size
; /* current size of feed */
246 struct FFStream
*next_feed
;
249 typedef struct FeedData
{
250 long long data_count
;
251 float avg_frame_size
; /* frame size averaged over last frames with exponential mean */
254 static struct sockaddr_in my_http_addr
;
255 static struct sockaddr_in my_rtsp_addr
;
257 static char logfilename
[1024];
258 static HTTPContext
*first_http_ctx
;
259 static FFStream
*first_feed
; /* contains only feeds */
260 static FFStream
*first_stream
; /* contains all streams, including feeds */
262 static void new_connection(int server_fd
, int is_rtsp
);
263 static void close_connection(HTTPContext
*c
);
266 static int handle_connection(HTTPContext
*c
);
267 static int http_parse_request(HTTPContext
*c
);
268 static int http_send_data(HTTPContext
*c
);
269 static void compute_status(HTTPContext
*c
);
270 static int open_input_stream(HTTPContext
*c
, const char *info
);
271 static int http_start_receive_data(HTTPContext
*c
);
272 static int http_receive_data(HTTPContext
*c
);
275 static int rtsp_parse_request(HTTPContext
*c
);
276 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
);
277 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
);
278 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
, RTSPMessageHeader
*h
);
279 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPMessageHeader
*h
);
280 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPMessageHeader
*h
);
281 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPMessageHeader
*h
);
284 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
285 struct in_addr my_ip
);
288 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
289 FFStream
*stream
, const char *session_id
,
290 enum RTSPLowerTransport rtp_protocol
);
291 static int rtp_new_av_stream(HTTPContext
*c
,
292 int stream_index
, struct sockaddr_in
*dest_addr
,
293 HTTPContext
*rtsp_c
);
295 static const char *my_program_name
;
296 static const char *my_program_dir
;
298 static const char *config_filename
;
299 static int ffserver_debug
;
300 static int ffserver_daemon
;
301 static int no_launch
;
302 static int need_to_start_children
;
304 /* maximum number of simultaneous HTTP connections */
305 static unsigned int nb_max_http_connections
= 2000;
306 static unsigned int nb_max_connections
= 5;
307 static unsigned int nb_connections
;
309 static uint64_t max_bandwidth
= 1000;
310 static uint64_t current_bandwidth
;
312 static int64_t cur_time
; // Making this global saves on passing it around everywhere
314 static AVLFG random_state
;
316 static FILE *logfile
= NULL
;
318 static char *ctime1(char *buf2
)
326 p
= buf2
+ strlen(p
) - 1;
332 static void http_vlog(const char *fmt
, va_list vargs
)
334 static int print_prefix
= 1;
339 fprintf(logfile
, "%s ", buf
);
341 print_prefix
= strstr(fmt
, "\n") != NULL
;
342 vfprintf(logfile
, fmt
, vargs
);
347 void __attribute__ ((format (printf
, 1, 2))) http_log(const char *fmt
, ...)
350 va_start(vargs
, fmt
);
351 http_vlog(fmt
, vargs
);
355 static void http_av_log(void *ptr
, int level
, const char *fmt
, va_list vargs
)
357 static int print_prefix
= 1;
358 AVClass
*avc
= ptr
? *(AVClass
**)ptr
: NULL
;
359 if (level
> av_log_get_level())
361 if (print_prefix
&& avc
)
362 http_log("[%s @ %p]", avc
->item_name(ptr
), ptr
);
363 print_prefix
= strstr(fmt
, "\n") != NULL
;
364 http_vlog(fmt
, vargs
);
367 static void log_connection(HTTPContext
*c
)
372 http_log("%s - - [%s] \"%s %s\" %d %"PRId64
"\n",
373 inet_ntoa(c
->from_addr
.sin_addr
), c
->method
, c
->url
,
374 c
->protocol
, (c
->http_error
? c
->http_error
: 200), c
->data_count
);
377 static void update_datarate(DataRateData
*drd
, int64_t count
)
379 if (!drd
->time1
&& !drd
->count1
) {
380 drd
->time1
= drd
->time2
= cur_time
;
381 drd
->count1
= drd
->count2
= count
;
382 } else if (cur_time
- drd
->time2
> 5000) {
383 drd
->time1
= drd
->time2
;
384 drd
->count1
= drd
->count2
;
385 drd
->time2
= cur_time
;
390 /* In bytes per second */
391 static int compute_datarate(DataRateData
*drd
, int64_t count
)
393 if (cur_time
== drd
->time1
)
396 return ((count
- drd
->count1
) * 1000) / (cur_time
- drd
->time1
);
400 static void start_children(FFStream
*feed
)
405 for (; feed
; feed
= feed
->next
) {
406 if (feed
->child_argv
&& !feed
->pid
) {
407 feed
->pid_start
= time(0);
412 http_log("Unable to create children\n");
421 av_strlcpy(pathname
, my_program_name
, sizeof(pathname
));
423 slash
= strrchr(pathname
, '/');
428 strcpy(slash
, "ffmpeg");
430 http_log("Launch commandline: ");
431 http_log("%s ", pathname
);
432 for (i
= 1; feed
->child_argv
[i
] && feed
->child_argv
[i
][0]; i
++)
433 http_log("%s ", feed
->child_argv
[i
]);
436 for (i
= 3; i
< 256; i
++)
439 if (!ffserver_debug
) {
440 i
= open("/dev/null", O_RDWR
);
449 /* This is needed to make relative pathnames work */
450 chdir(my_program_dir
);
452 signal(SIGPIPE
, SIG_DFL
);
454 execvp(pathname
, feed
->child_argv
);
462 /* open a listening socket */
463 static int socket_open_listen(struct sockaddr_in
*my_addr
)
467 server_fd
= socket(AF_INET
,SOCK_STREAM
,0);
474 setsockopt(server_fd
, SOL_SOCKET
, SO_REUSEADDR
, &tmp
, sizeof(tmp
));
476 if (bind (server_fd
, (struct sockaddr
*) my_addr
, sizeof (*my_addr
)) < 0) {
478 snprintf(bindmsg
, sizeof(bindmsg
), "bind(port %d)", ntohs(my_addr
->sin_port
));
480 closesocket(server_fd
);
484 if (listen (server_fd
, 5) < 0) {
486 closesocket(server_fd
);
489 ff_socket_nonblock(server_fd
, 1);
494 /* start all multicast streams */
495 static void start_multicast(void)
500 struct sockaddr_in dest_addr
;
501 int default_port
, stream_index
;
504 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
505 if (stream
->is_multicast
) {
506 /* open the RTP connection */
507 snprintf(session_id
, sizeof(session_id
), "%08x%08x",
508 av_lfg_get(&random_state
), av_lfg_get(&random_state
));
510 /* choose a port if none given */
511 if (stream
->multicast_port
== 0) {
512 stream
->multicast_port
= default_port
;
516 dest_addr
.sin_family
= AF_INET
;
517 dest_addr
.sin_addr
= stream
->multicast_ip
;
518 dest_addr
.sin_port
= htons(stream
->multicast_port
);
520 rtp_c
= rtp_new_connection(&dest_addr
, stream
, session_id
,
521 RTSP_LOWER_TRANSPORT_UDP_MULTICAST
);
525 if (open_input_stream(rtp_c
, "") < 0) {
526 http_log("Could not open input stream for stream '%s'\n",
531 /* open each RTP stream */
532 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
534 dest_addr
.sin_port
= htons(stream
->multicast_port
+
536 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, NULL
) < 0) {
537 http_log("Could not open output stream '%s/streamid=%d'\n",
538 stream
->filename
, stream_index
);
543 /* change state to send data */
544 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
549 /* main loop of the http server */
550 static int http_server(void)
552 int server_fd
= 0, rtsp_server_fd
= 0;
553 int ret
, delay
, delay1
;
554 struct pollfd
*poll_table
, *poll_entry
;
555 HTTPContext
*c
, *c_next
;
557 if(!(poll_table
= av_mallocz((nb_max_http_connections
+ 2)*sizeof(*poll_table
)))) {
558 http_log("Impossible to allocate a poll table handling %d connections.\n", nb_max_http_connections
);
562 if (my_http_addr
.sin_port
) {
563 server_fd
= socket_open_listen(&my_http_addr
);
568 if (my_rtsp_addr
.sin_port
) {
569 rtsp_server_fd
= socket_open_listen(&my_rtsp_addr
);
570 if (rtsp_server_fd
< 0)
574 if (!rtsp_server_fd
&& !server_fd
) {
575 http_log("HTTP and RTSP disabled.\n");
579 http_log("FFserver started.\n");
581 start_children(first_feed
);
586 poll_entry
= poll_table
;
588 poll_entry
->fd
= server_fd
;
589 poll_entry
->events
= POLLIN
;
592 if (rtsp_server_fd
) {
593 poll_entry
->fd
= rtsp_server_fd
;
594 poll_entry
->events
= POLLIN
;
598 /* wait for events on each HTTP handle */
605 case HTTPSTATE_SEND_HEADER
:
606 case RTSPSTATE_SEND_REPLY
:
607 case RTSPSTATE_SEND_PACKET
:
608 c
->poll_entry
= poll_entry
;
610 poll_entry
->events
= POLLOUT
;
613 case HTTPSTATE_SEND_DATA_HEADER
:
614 case HTTPSTATE_SEND_DATA
:
615 case HTTPSTATE_SEND_DATA_TRAILER
:
616 if (!c
->is_packetized
) {
617 /* for TCP, we output as much as we can (may need to put a limit) */
618 c
->poll_entry
= poll_entry
;
620 poll_entry
->events
= POLLOUT
;
623 /* when ffserver is doing the timing, we work by
624 looking at which packet need to be sent every
626 delay1
= 10; /* one tick wait XXX: 10 ms assumed */
631 case HTTPSTATE_WAIT_REQUEST
:
632 case HTTPSTATE_RECEIVE_DATA
:
633 case HTTPSTATE_WAIT_FEED
:
634 case RTSPSTATE_WAIT_REQUEST
:
635 /* need to catch errors */
636 c
->poll_entry
= poll_entry
;
638 poll_entry
->events
= POLLIN
;/* Maybe this will work */
642 c
->poll_entry
= NULL
;
648 /* wait for an event on one connection. We poll at least every
649 second to handle timeouts */
651 ret
= poll(poll_table
, poll_entry
- poll_table
, delay
);
652 if (ret
< 0 && ff_neterrno() != FF_NETERROR(EAGAIN
) &&
653 ff_neterrno() != FF_NETERROR(EINTR
))
657 cur_time
= av_gettime() / 1000;
659 if (need_to_start_children
) {
660 need_to_start_children
= 0;
661 start_children(first_feed
);
664 /* now handle the events */
665 for(c
= first_http_ctx
; c
!= NULL
; c
= c_next
) {
667 if (handle_connection(c
) < 0) {
668 /* close and free the connection */
674 poll_entry
= poll_table
;
676 /* new HTTP connection request ? */
677 if (poll_entry
->revents
& POLLIN
)
678 new_connection(server_fd
, 0);
681 if (rtsp_server_fd
) {
682 /* new RTSP connection request ? */
683 if (poll_entry
->revents
& POLLIN
)
684 new_connection(rtsp_server_fd
, 1);
689 /* start waiting for a new HTTP/RTSP request */
690 static void start_wait_request(HTTPContext
*c
, int is_rtsp
)
692 c
->buffer_ptr
= c
->buffer
;
693 c
->buffer_end
= c
->buffer
+ c
->buffer_size
- 1; /* leave room for '\0' */
696 c
->timeout
= cur_time
+ RTSP_REQUEST_TIMEOUT
;
697 c
->state
= RTSPSTATE_WAIT_REQUEST
;
699 c
->timeout
= cur_time
+ HTTP_REQUEST_TIMEOUT
;
700 c
->state
= HTTPSTATE_WAIT_REQUEST
;
704 static void http_send_too_busy_reply(int fd
)
707 int len
= snprintf(buffer
, sizeof(buffer
),
708 "HTTP/1.0 200 Server too busy\r\n"
709 "Content-type: text/html\r\n"
711 "<html><head><title>Too busy</title></head><body>\r\n"
712 "<p>The server is too busy to serve your request at this time.</p>\r\n"
713 "<p>The number of current connections is %d, and this exceeds the limit of %d.</p>\r\n"
714 "</body></html>\r\n",
715 nb_connections
, nb_max_connections
);
716 send(fd
, buffer
, len
, 0);
720 static void new_connection(int server_fd
, int is_rtsp
)
722 struct sockaddr_in from_addr
;
724 HTTPContext
*c
= NULL
;
726 len
= sizeof(from_addr
);
727 fd
= accept(server_fd
, (struct sockaddr
*)&from_addr
,
730 http_log("error during accept %s\n", strerror(errno
));
733 ff_socket_nonblock(fd
, 1);
735 if (nb_connections
>= nb_max_connections
) {
736 http_send_too_busy_reply(fd
);
740 /* add a new connection */
741 c
= av_mallocz(sizeof(HTTPContext
));
746 c
->poll_entry
= NULL
;
747 c
->from_addr
= from_addr
;
748 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
749 c
->buffer
= av_malloc(c
->buffer_size
);
753 c
->next
= first_http_ctx
;
757 start_wait_request(c
, is_rtsp
);
769 static void close_connection(HTTPContext
*c
)
771 HTTPContext
**cp
, *c1
;
773 AVFormatContext
*ctx
;
777 /* remove connection from list */
778 cp
= &first_http_ctx
;
779 while ((*cp
) != NULL
) {
787 /* remove references, if any (XXX: do it faster) */
788 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
793 /* remove connection associated resources */
797 /* close each frame parser */
798 for(i
=0;i
<c
->fmt_in
->nb_streams
;i
++) {
799 st
= c
->fmt_in
->streams
[i
];
800 if (st
->codec
->codec
)
801 avcodec_close(st
->codec
);
803 av_close_input_file(c
->fmt_in
);
806 /* free RTP output streams if any */
809 nb_streams
= c
->stream
->nb_streams
;
811 for(i
=0;i
<nb_streams
;i
++) {
814 av_write_trailer(ctx
);
817 h
= c
->rtp_handles
[i
];
824 if (!c
->last_packet_sent
&& c
->state
== HTTPSTATE_SEND_DATA_TRAILER
) {
827 if (url_open_dyn_buf(&ctx
->pb
) >= 0) {
828 av_write_trailer(ctx
);
829 av_freep(&c
->pb_buffer
);
830 url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
835 for(i
=0; i
<ctx
->nb_streams
; i
++)
836 av_free(ctx
->streams
[i
]);
838 if (c
->stream
&& !c
->post
&& c
->stream
->stream_type
== STREAM_TYPE_LIVE
)
839 current_bandwidth
-= c
->stream
->bandwidth
;
841 /* signal that there is no feed if we are the feeder socket */
842 if (c
->state
== HTTPSTATE_RECEIVE_DATA
&& c
->stream
) {
843 c
->stream
->feed_opened
= 0;
847 av_freep(&c
->pb_buffer
);
848 av_freep(&c
->packet_buffer
);
854 static int handle_connection(HTTPContext
*c
)
859 case HTTPSTATE_WAIT_REQUEST
:
860 case RTSPSTATE_WAIT_REQUEST
:
862 if ((c
->timeout
- cur_time
) < 0)
864 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
867 /* no need to read if no events */
868 if (!(c
->poll_entry
->revents
& POLLIN
))
872 len
= recv(c
->fd
, c
->buffer_ptr
, 1, 0);
874 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
875 ff_neterrno() != FF_NETERROR(EINTR
))
877 } else if (len
== 0) {
880 /* search for end of request. */
882 c
->buffer_ptr
+= len
;
884 if ((ptr
>= c
->buffer
+ 2 && !memcmp(ptr
-2, "\n\n", 2)) ||
885 (ptr
>= c
->buffer
+ 4 && !memcmp(ptr
-4, "\r\n\r\n", 4))) {
886 /* request found : parse it and reply */
887 if (c
->state
== HTTPSTATE_WAIT_REQUEST
) {
888 ret
= http_parse_request(c
);
890 ret
= rtsp_parse_request(c
);
894 } else if (ptr
>= c
->buffer_end
) {
895 /* request too long: cannot do anything */
897 } else goto read_loop
;
901 case HTTPSTATE_SEND_HEADER
:
902 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
905 /* no need to write if no events */
906 if (!(c
->poll_entry
->revents
& POLLOUT
))
908 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
910 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
911 ff_neterrno() != FF_NETERROR(EINTR
)) {
912 /* error : close connection */
913 av_freep(&c
->pb_buffer
);
917 c
->buffer_ptr
+= len
;
919 c
->stream
->bytes_served
+= len
;
920 c
->data_count
+= len
;
921 if (c
->buffer_ptr
>= c
->buffer_end
) {
922 av_freep(&c
->pb_buffer
);
926 /* all the buffer was sent : synchronize to the incoming stream */
927 c
->state
= HTTPSTATE_SEND_DATA_HEADER
;
928 c
->buffer_ptr
= c
->buffer_end
= c
->buffer
;
933 case HTTPSTATE_SEND_DATA
:
934 case HTTPSTATE_SEND_DATA_HEADER
:
935 case HTTPSTATE_SEND_DATA_TRAILER
:
936 /* for packetized output, we consider we can always write (the
937 input streams sets the speed). It may be better to verify
938 that we do not rely too much on the kernel queues */
939 if (!c
->is_packetized
) {
940 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
943 /* no need to read if no events */
944 if (!(c
->poll_entry
->revents
& POLLOUT
))
947 if (http_send_data(c
) < 0)
949 /* close connection if trailer sent */
950 if (c
->state
== HTTPSTATE_SEND_DATA_TRAILER
)
953 case HTTPSTATE_RECEIVE_DATA
:
954 /* no need to read if no events */
955 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
957 if (!(c
->poll_entry
->revents
& POLLIN
))
959 if (http_receive_data(c
) < 0)
962 case HTTPSTATE_WAIT_FEED
:
963 /* no need to read if no events */
964 if (c
->poll_entry
->revents
& (POLLIN
| POLLERR
| POLLHUP
))
967 /* nothing to do, we'll be waken up by incoming feed packets */
970 case RTSPSTATE_SEND_REPLY
:
971 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
972 av_freep(&c
->pb_buffer
);
975 /* no need to write if no events */
976 if (!(c
->poll_entry
->revents
& POLLOUT
))
978 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
980 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
981 ff_neterrno() != FF_NETERROR(EINTR
)) {
982 /* error : close connection */
983 av_freep(&c
->pb_buffer
);
987 c
->buffer_ptr
+= len
;
988 c
->data_count
+= len
;
989 if (c
->buffer_ptr
>= c
->buffer_end
) {
990 /* all the buffer was sent : wait for a new request */
991 av_freep(&c
->pb_buffer
);
992 start_wait_request(c
, 1);
996 case RTSPSTATE_SEND_PACKET
:
997 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
998 av_freep(&c
->packet_buffer
);
1001 /* no need to write if no events */
1002 if (!(c
->poll_entry
->revents
& POLLOUT
))
1004 len
= send(c
->fd
, c
->packet_buffer_ptr
,
1005 c
->packet_buffer_end
- c
->packet_buffer_ptr
, 0);
1007 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
1008 ff_neterrno() != FF_NETERROR(EINTR
)) {
1009 /* error : close connection */
1010 av_freep(&c
->packet_buffer
);
1014 c
->packet_buffer_ptr
+= len
;
1015 if (c
->packet_buffer_ptr
>= c
->packet_buffer_end
) {
1016 /* all the buffer was sent : wait for a new request */
1017 av_freep(&c
->packet_buffer
);
1018 c
->state
= RTSPSTATE_WAIT_REQUEST
;
1022 case HTTPSTATE_READY
:
1031 static int extract_rates(char *rates
, int ratelen
, const char *request
)
1035 for (p
= request
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1036 if (strncasecmp(p
, "Pragma:", 7) == 0) {
1037 const char *q
= p
+ 7;
1039 while (*q
&& *q
!= '\n' && isspace(*q
))
1042 if (strncasecmp(q
, "stream-switch-entry=", 20) == 0) {
1048 memset(rates
, 0xff, ratelen
);
1051 while (*q
&& *q
!= '\n' && *q
!= ':')
1054 if (sscanf(q
, ":%d:%d", &stream_no
, &rate_no
) != 2)
1058 if (stream_no
< ratelen
&& stream_no
>= 0)
1059 rates
[stream_no
] = rate_no
;
1061 while (*q
&& *q
!= '\n' && !isspace(*q
))
1068 p
= strchr(p
, '\n');
1078 static int find_stream_in_feed(FFStream
*feed
, AVCodecContext
*codec
, int bit_rate
)
1081 int best_bitrate
= 100000000;
1084 for (i
= 0; i
< feed
->nb_streams
; i
++) {
1085 AVCodecContext
*feed_codec
= feed
->streams
[i
]->codec
;
1087 if (feed_codec
->codec_id
!= codec
->codec_id
||
1088 feed_codec
->sample_rate
!= codec
->sample_rate
||
1089 feed_codec
->width
!= codec
->width
||
1090 feed_codec
->height
!= codec
->height
)
1093 /* Potential stream */
1095 /* We want the fastest stream less than bit_rate, or the slowest
1096 * faster than bit_rate
1099 if (feed_codec
->bit_rate
<= bit_rate
) {
1100 if (best_bitrate
> bit_rate
|| feed_codec
->bit_rate
> best_bitrate
) {
1101 best_bitrate
= feed_codec
->bit_rate
;
1105 if (feed_codec
->bit_rate
< best_bitrate
) {
1106 best_bitrate
= feed_codec
->bit_rate
;
1115 static int modify_current_stream(HTTPContext
*c
, char *rates
)
1118 FFStream
*req
= c
->stream
;
1119 int action_required
= 0;
1121 /* Not much we can do for a feed */
1125 for (i
= 0; i
< req
->nb_streams
; i
++) {
1126 AVCodecContext
*codec
= req
->streams
[i
]->codec
;
1130 c
->switch_feed_streams
[i
] = req
->feed_streams
[i
];
1133 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 2);
1136 /* Wants off or slow */
1137 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 4);
1139 /* This doesn't work well when it turns off the only stream! */
1140 c
->switch_feed_streams
[i
] = -2;
1141 c
->feed_streams
[i
] = -2;
1146 if (c
->switch_feed_streams
[i
] >= 0 && c
->switch_feed_streams
[i
] != c
->feed_streams
[i
])
1147 action_required
= 1;
1150 return action_required
;
1154 static void do_switch_stream(HTTPContext
*c
, int i
)
1156 if (c
->switch_feed_streams
[i
] >= 0) {
1158 c
->feed_streams
[i
] = c
->switch_feed_streams
[i
];
1161 /* Now update the stream */
1163 c
->switch_feed_streams
[i
] = -1;
1166 /* XXX: factorize in utils.c ? */
1167 /* XXX: take care with different space meaning */
1168 static void skip_spaces(const char **pp
)
1172 while (*p
== ' ' || *p
== '\t')
1177 static void get_word(char *buf
, int buf_size
, const char **pp
)
1185 while (!isspace(*p
) && *p
!= '\0') {
1186 if ((q
- buf
) < buf_size
- 1)
1195 static int validate_acl(FFStream
*stream
, HTTPContext
*c
)
1197 enum IPAddressAction last_action
= IP_DENY
;
1199 struct in_addr
*src
= &c
->from_addr
.sin_addr
;
1200 unsigned long src_addr
= src
->s_addr
;
1202 for (acl
= stream
->acl
; acl
; acl
= acl
->next
) {
1203 if (src_addr
>= acl
->first
.s_addr
&& src_addr
<= acl
->last
.s_addr
)
1204 return (acl
->action
== IP_ALLOW
) ? 1 : 0;
1205 last_action
= acl
->action
;
1208 /* Nothing matched, so return not the last action */
1209 return (last_action
== IP_DENY
) ? 1 : 0;
1212 /* compute the real filename of a file by matching it without its
1213 extensions to all the stream filenames */
1214 static void compute_real_filename(char *filename
, int max_size
)
1221 /* compute filename by matching without the file extensions */
1222 av_strlcpy(file1
, filename
, sizeof(file1
));
1223 p
= strrchr(file1
, '.');
1226 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
1227 av_strlcpy(file2
, stream
->filename
, sizeof(file2
));
1228 p
= strrchr(file2
, '.');
1231 if (!strcmp(file1
, file2
)) {
1232 av_strlcpy(filename
, stream
->filename
, max_size
);
1247 /* parse http request and prepare header */
1248 static int http_parse_request(HTTPContext
*c
)
1251 enum RedirType redir_type
;
1253 char info
[1024], filename
[1024];
1257 const char *mime_type
;
1261 char *useragent
= 0;
1264 get_word(cmd
, sizeof(cmd
), (const char **)&p
);
1265 av_strlcpy(c
->method
, cmd
, sizeof(c
->method
));
1267 if (!strcmp(cmd
, "GET"))
1269 else if (!strcmp(cmd
, "POST"))
1274 get_word(url
, sizeof(url
), (const char **)&p
);
1275 av_strlcpy(c
->url
, url
, sizeof(c
->url
));
1277 get_word(protocol
, sizeof(protocol
), (const char **)&p
);
1278 if (strcmp(protocol
, "HTTP/1.0") && strcmp(protocol
, "HTTP/1.1"))
1281 av_strlcpy(c
->protocol
, protocol
, sizeof(c
->protocol
));
1284 http_log("%s - - New connection: %s %s\n", inet_ntoa(c
->from_addr
.sin_addr
), cmd
, url
);
1286 /* find the filename and the optional info string in the request */
1287 p
= strchr(url
, '?');
1289 av_strlcpy(info
, p
, sizeof(info
));
1294 av_strlcpy(filename
, url
+ ((*url
== '/') ? 1 : 0), sizeof(filename
)-1);
1296 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1297 if (strncasecmp(p
, "User-Agent:", 11) == 0) {
1299 if (*useragent
&& *useragent
!= '\n' && isspace(*useragent
))
1303 p
= strchr(p
, '\n');
1310 redir_type
= REDIR_NONE
;
1311 if (match_ext(filename
, "asx")) {
1312 redir_type
= REDIR_ASX
;
1313 filename
[strlen(filename
)-1] = 'f';
1314 } else if (match_ext(filename
, "asf") &&
1315 (!useragent
|| strncasecmp(useragent
, "NSPlayer", 8) != 0)) {
1316 /* if this isn't WMP or lookalike, return the redirector file */
1317 redir_type
= REDIR_ASF
;
1318 } else if (match_ext(filename
, "rpm,ram")) {
1319 redir_type
= REDIR_RAM
;
1320 strcpy(filename
+ strlen(filename
)-2, "m");
1321 } else if (match_ext(filename
, "rtsp")) {
1322 redir_type
= REDIR_RTSP
;
1323 compute_real_filename(filename
, sizeof(filename
) - 1);
1324 } else if (match_ext(filename
, "sdp")) {
1325 redir_type
= REDIR_SDP
;
1326 compute_real_filename(filename
, sizeof(filename
) - 1);
1329 // "redirect" / request to index.html
1330 if (!strlen(filename
))
1331 av_strlcpy(filename
, "index.html", sizeof(filename
) - 1);
1333 stream
= first_stream
;
1334 while (stream
!= NULL
) {
1335 if (!strcmp(stream
->filename
, filename
) && validate_acl(stream
, c
))
1337 stream
= stream
->next
;
1339 if (stream
== NULL
) {
1340 snprintf(msg
, sizeof(msg
), "File '%s' not found", url
);
1341 http_log("File '%s' not found\n", url
);
1346 memcpy(c
->feed_streams
, stream
->feed_streams
, sizeof(c
->feed_streams
));
1347 memset(c
->switch_feed_streams
, -1, sizeof(c
->switch_feed_streams
));
1349 if (stream
->stream_type
== STREAM_TYPE_REDIRECT
) {
1350 c
->http_error
= 301;
1352 q
+= snprintf(q
, c
->buffer_size
,
1353 "HTTP/1.0 301 Moved\r\n"
1355 "Content-type: text/html\r\n"
1357 "<html><head><title>Moved</title></head><body>\r\n"
1358 "You should be <a href=\"%s\">redirected</a>.\r\n"
1359 "</body></html>\r\n", stream
->feed_filename
, stream
->feed_filename
);
1360 /* prepare output buffer */
1361 c
->buffer_ptr
= c
->buffer
;
1363 c
->state
= HTTPSTATE_SEND_HEADER
;
1367 /* If this is WMP, get the rate information */
1368 if (extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1369 if (modify_current_stream(c
, ratebuf
)) {
1370 for (i
= 0; i
< FF_ARRAY_ELEMS(c
->feed_streams
); i
++) {
1371 if (c
->switch_feed_streams
[i
] >= 0)
1372 do_switch_stream(c
, i
);
1377 if (c
->post
== 0 && stream
->stream_type
== STREAM_TYPE_LIVE
)
1378 current_bandwidth
+= stream
->bandwidth
;
1380 /* If already streaming this feed, do not let start another feeder. */
1381 if (stream
->feed_opened
) {
1382 snprintf(msg
, sizeof(msg
), "This feed is already being received.");
1383 http_log("Feed '%s' already being received\n", stream
->feed_filename
);
1387 if (c
->post
== 0 && max_bandwidth
< current_bandwidth
) {
1388 c
->http_error
= 200;
1390 q
+= snprintf(q
, c
->buffer_size
,
1391 "HTTP/1.0 200 Server too busy\r\n"
1392 "Content-type: text/html\r\n"
1394 "<html><head><title>Too busy</title></head><body>\r\n"
1395 "<p>The server is too busy to serve your request at this time.</p>\r\n"
1396 "<p>The bandwidth being served (including your stream) is %"PRIu64
"kbit/sec, "
1397 "and this exceeds the limit of %"PRIu64
"kbit/sec.</p>\r\n"
1398 "</body></html>\r\n", current_bandwidth
, max_bandwidth
);
1399 /* prepare output buffer */
1400 c
->buffer_ptr
= c
->buffer
;
1402 c
->state
= HTTPSTATE_SEND_HEADER
;
1406 if (redir_type
!= REDIR_NONE
) {
1409 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1410 if (strncasecmp(p
, "Host:", 5) == 0) {
1414 p
= strchr(p
, '\n');
1425 while (isspace(*hostinfo
))
1428 eoh
= strchr(hostinfo
, '\n');
1430 if (eoh
[-1] == '\r')
1433 if (eoh
- hostinfo
< sizeof(hostbuf
) - 1) {
1434 memcpy(hostbuf
, hostinfo
, eoh
- hostinfo
);
1435 hostbuf
[eoh
- hostinfo
] = 0;
1437 c
->http_error
= 200;
1439 switch(redir_type
) {
1441 q
+= snprintf(q
, c
->buffer_size
,
1442 "HTTP/1.0 200 ASX Follows\r\n"
1443 "Content-type: video/x-ms-asf\r\n"
1445 "<ASX Version=\"3\">\r\n"
1446 //"<!-- Autogenerated by ffserver -->\r\n"
1447 "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n"
1448 "</ASX>\r\n", hostbuf
, filename
, info
);
1451 q
+= snprintf(q
, c
->buffer_size
,
1452 "HTTP/1.0 200 RAM Follows\r\n"
1453 "Content-type: audio/x-pn-realaudio\r\n"
1455 "# Autogenerated by ffserver\r\n"
1456 "http://%s/%s%s\r\n", hostbuf
, filename
, info
);
1459 q
+= snprintf(q
, c
->buffer_size
,
1460 "HTTP/1.0 200 ASF Redirect follows\r\n"
1461 "Content-type: video/x-ms-asf\r\n"
1464 "Ref1=http://%s/%s%s\r\n", hostbuf
, filename
, info
);
1468 char hostname
[256], *p
;
1469 /* extract only hostname */
1470 av_strlcpy(hostname
, hostbuf
, sizeof(hostname
));
1471 p
= strrchr(hostname
, ':');
1474 q
+= snprintf(q
, c
->buffer_size
,
1475 "HTTP/1.0 200 RTSP Redirect follows\r\n"
1476 /* XXX: incorrect mime type ? */
1477 "Content-type: application/x-rtsp\r\n"
1479 "rtsp://%s:%d/%s\r\n", hostname
, ntohs(my_rtsp_addr
.sin_port
), filename
);
1485 int sdp_data_size
, len
;
1486 struct sockaddr_in my_addr
;
1488 q
+= snprintf(q
, c
->buffer_size
,
1489 "HTTP/1.0 200 OK\r\n"
1490 "Content-type: application/sdp\r\n"
1493 len
= sizeof(my_addr
);
1494 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
1496 /* XXX: should use a dynamic buffer */
1497 sdp_data_size
= prepare_sdp_description(stream
,
1500 if (sdp_data_size
> 0) {
1501 memcpy(q
, sdp_data
, sdp_data_size
);
1513 /* prepare output buffer */
1514 c
->buffer_ptr
= c
->buffer
;
1516 c
->state
= HTTPSTATE_SEND_HEADER
;
1522 snprintf(msg
, sizeof(msg
), "ASX/RAM file not handled");
1526 stream
->conns_served
++;
1528 /* XXX: add there authenticate and IP match */
1531 /* if post, it means a feed is being sent */
1532 if (!stream
->is_feed
) {
1533 /* However it might be a status report from WMP! Let us log the
1534 * data as it might come in handy one day. */
1538 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1539 if (strncasecmp(p
, "Pragma: log-line=", 17) == 0) {
1543 if (strncasecmp(p
, "Pragma: client-id=", 18) == 0)
1544 client_id
= strtol(p
+ 18, 0, 10);
1545 p
= strchr(p
, '\n');
1553 char *eol
= strchr(logline
, '\n');
1558 if (eol
[-1] == '\r')
1560 http_log("%.*s\n", (int) (eol
- logline
), logline
);
1561 c
->suppress_log
= 1;
1566 http_log("\nGot request:\n%s\n", c
->buffer
);
1569 if (client_id
&& extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1572 /* Now we have to find the client_id */
1573 for (wmpc
= first_http_ctx
; wmpc
; wmpc
= wmpc
->next
) {
1574 if (wmpc
->wmp_client_id
== client_id
)
1578 if (wmpc
&& modify_current_stream(wmpc
, ratebuf
))
1579 wmpc
->switch_pending
= 1;
1582 snprintf(msg
, sizeof(msg
), "POST command not handled");
1586 if (http_start_receive_data(c
) < 0) {
1587 snprintf(msg
, sizeof(msg
), "could not open feed");
1591 c
->state
= HTTPSTATE_RECEIVE_DATA
;
1596 if (strcmp(stream
->filename
+ strlen(stream
->filename
) - 4, ".asf") == 0)
1597 http_log("\nGot request:\n%s\n", c
->buffer
);
1600 if (c
->stream
->stream_type
== STREAM_TYPE_STATUS
)
1603 /* open input stream */
1604 if (open_input_stream(c
, info
) < 0) {
1605 snprintf(msg
, sizeof(msg
), "Input stream corresponding to '%s' not found", url
);
1609 /* prepare http header */
1611 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1612 mime_type
= c
->stream
->fmt
->mime_type
;
1614 mime_type
= "application/x-octet-stream";
1615 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Pragma: no-cache\r\n");
1617 /* for asf, we need extra headers */
1618 if (!strcmp(c
->stream
->fmt
->name
,"asf_stream")) {
1619 /* Need to allocate a client id */
1621 c
->wmp_client_id
= av_lfg_get(&random_state
);
1623 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
);
1625 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-Type: %s\r\n", mime_type
);
1626 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1628 /* prepare output buffer */
1630 c
->buffer_ptr
= c
->buffer
;
1632 c
->state
= HTTPSTATE_SEND_HEADER
;
1635 c
->http_error
= 404;
1637 q
+= snprintf(q
, c
->buffer_size
,
1638 "HTTP/1.0 404 Not Found\r\n"
1639 "Content-type: text/html\r\n"
1642 "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n"
1645 /* prepare output buffer */
1646 c
->buffer_ptr
= c
->buffer
;
1648 c
->state
= HTTPSTATE_SEND_HEADER
;
1652 c
->http_error
= 200; /* horrible : we use this value to avoid
1653 going to the send data state */
1654 c
->state
= HTTPSTATE_SEND_HEADER
;
1658 static void fmt_bytecount(ByteIOContext
*pb
, int64_t count
)
1660 static const char *suffix
= " kMGTP";
1663 for (s
= suffix
; count
>= 100000 && s
[1]; count
/= 1000, s
++);
1665 url_fprintf(pb
, "%"PRId64
"%c", count
, *s
);
1668 static void compute_status(HTTPContext
*c
)
1677 if (url_open_dyn_buf(&pb
) < 0) {
1678 /* XXX: return an error ? */
1679 c
->buffer_ptr
= c
->buffer
;
1680 c
->buffer_end
= c
->buffer
;
1684 url_fprintf(pb
, "HTTP/1.0 200 OK\r\n");
1685 url_fprintf(pb
, "Content-type: %s\r\n", "text/html");
1686 url_fprintf(pb
, "Pragma: no-cache\r\n");
1687 url_fprintf(pb
, "\r\n");
1689 url_fprintf(pb
, "<HTML><HEAD><TITLE>%s Status</TITLE>\n", program_name
);
1690 if (c
->stream
->feed_filename
[0])
1691 url_fprintf(pb
, "<link rel=\"shortcut icon\" href=\"%s\">\n", c
->stream
->feed_filename
);
1692 url_fprintf(pb
, "</HEAD>\n<BODY>");
1693 url_fprintf(pb
, "<H1>%s Status</H1>\n", program_name
);
1695 url_fprintf(pb
, "<H2>Available Streams</H2>\n");
1696 url_fprintf(pb
, "<TABLE cellspacing=0 cellpadding=4>\n");
1697 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");
1698 stream
= first_stream
;
1699 while (stream
!= NULL
) {
1700 char sfilename
[1024];
1703 if (stream
->feed
!= stream
) {
1704 av_strlcpy(sfilename
, stream
->filename
, sizeof(sfilename
) - 10);
1705 eosf
= sfilename
+ strlen(sfilename
);
1706 if (eosf
- sfilename
>= 4) {
1707 if (strcmp(eosf
- 4, ".asf") == 0)
1708 strcpy(eosf
- 4, ".asx");
1709 else if (strcmp(eosf
- 3, ".rm") == 0)
1710 strcpy(eosf
- 3, ".ram");
1711 else if (stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
1712 /* generate a sample RTSP director if
1713 unicast. Generate an SDP redirector if
1715 eosf
= strrchr(sfilename
, '.');
1717 eosf
= sfilename
+ strlen(sfilename
);
1718 if (stream
->is_multicast
)
1719 strcpy(eosf
, ".sdp");
1721 strcpy(eosf
, ".rtsp");
1725 url_fprintf(pb
, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1726 sfilename
, stream
->filename
);
1727 url_fprintf(pb
, "<td align=right> %d <td align=right> ",
1728 stream
->conns_served
);
1729 fmt_bytecount(pb
, stream
->bytes_served
);
1730 switch(stream
->stream_type
) {
1731 case STREAM_TYPE_LIVE
: {
1732 int audio_bit_rate
= 0;
1733 int video_bit_rate
= 0;
1734 const char *audio_codec_name
= "";
1735 const char *video_codec_name
= "";
1736 const char *audio_codec_name_extra
= "";
1737 const char *video_codec_name_extra
= "";
1739 for(i
=0;i
<stream
->nb_streams
;i
++) {
1740 AVStream
*st
= stream
->streams
[i
];
1741 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1742 switch(st
->codec
->codec_type
) {
1743 case CODEC_TYPE_AUDIO
:
1744 audio_bit_rate
+= st
->codec
->bit_rate
;
1746 if (*audio_codec_name
)
1747 audio_codec_name_extra
= "...";
1748 audio_codec_name
= codec
->name
;
1751 case CODEC_TYPE_VIDEO
:
1752 video_bit_rate
+= st
->codec
->bit_rate
;
1754 if (*video_codec_name
)
1755 video_codec_name_extra
= "...";
1756 video_codec_name
= codec
->name
;
1759 case CODEC_TYPE_DATA
:
1760 video_bit_rate
+= st
->codec
->bit_rate
;
1766 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",
1769 video_bit_rate
/ 1000, video_codec_name
, video_codec_name_extra
,
1770 audio_bit_rate
/ 1000, audio_codec_name
, audio_codec_name_extra
);
1772 url_fprintf(pb
, "<TD>%s", stream
->feed
->filename
);
1774 url_fprintf(pb
, "<TD>%s", stream
->feed_filename
);
1775 url_fprintf(pb
, "\n");
1779 url_fprintf(pb
, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1783 stream
= stream
->next
;
1785 url_fprintf(pb
, "</TABLE>\n");
1787 stream
= first_stream
;
1788 while (stream
!= NULL
) {
1789 if (stream
->feed
== stream
) {
1790 url_fprintf(pb
, "<h2>Feed %s</h2>", stream
->filename
);
1792 url_fprintf(pb
, "Running as pid %d.\n", stream
->pid
);
1794 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1799 /* This is somewhat linux specific I guess */
1800 snprintf(ps_cmd
, sizeof(ps_cmd
),
1801 "ps -o \"%%cpu,cputime\" --no-headers %d",
1804 pid_stat
= popen(ps_cmd
, "r");
1809 if (fscanf(pid_stat
, "%10s %64s", cpuperc
,
1811 url_fprintf(pb
, "Currently using %s%% of the cpu. Total time used %s.\n",
1819 url_fprintf(pb
, "<p>");
1821 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");
1823 for (i
= 0; i
< stream
->nb_streams
; i
++) {
1824 AVStream
*st
= stream
->streams
[i
];
1825 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1826 const char *type
= "unknown";
1827 char parameters
[64];
1831 switch(st
->codec
->codec_type
) {
1832 case CODEC_TYPE_AUDIO
:
1834 snprintf(parameters
, sizeof(parameters
), "%d channel(s), %d Hz", st
->codec
->channels
, st
->codec
->sample_rate
);
1836 case CODEC_TYPE_VIDEO
:
1838 snprintf(parameters
, sizeof(parameters
), "%dx%d, q=%d-%d, fps=%d", st
->codec
->width
, st
->codec
->height
,
1839 st
->codec
->qmin
, st
->codec
->qmax
, st
->codec
->time_base
.den
/ st
->codec
->time_base
.num
);
1844 url_fprintf(pb
, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1845 i
, type
, st
->codec
->bit_rate
/1000, codec
? codec
->name
: "", parameters
);
1847 url_fprintf(pb
, "</table>\n");
1850 stream
= stream
->next
;
1856 AVCodecContext
*enc
;
1860 stream
= first_feed
;
1861 while (stream
!= NULL
) {
1862 url_fprintf(pb
, "<H1>Feed '%s'</H1>\n", stream
->filename
);
1863 url_fprintf(pb
, "<TABLE>\n");
1864 url_fprintf(pb
, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1865 for(i
=0;i
<stream
->nb_streams
;i
++) {
1866 AVStream
*st
= stream
->streams
[i
];
1867 FeedData
*fdata
= st
->priv_data
;
1870 avcodec_string(buf
, sizeof(buf
), enc
);
1871 avg
= fdata
->avg_frame_size
* (float)enc
->rate
* 8.0;
1872 if (enc
->codec
->type
== CODEC_TYPE_AUDIO
&& enc
->frame_size
> 0)
1873 avg
/= enc
->frame_size
;
1874 url_fprintf(pb
, "<TR><TD>%s <TD> %d <TD> %"PRId64
" <TD> %0.1f\n",
1875 buf
, enc
->frame_number
, fdata
->data_count
, avg
/ 1000.0);
1877 url_fprintf(pb
, "</TABLE>\n");
1878 stream
= stream
->next_feed
;
1883 /* connection status */
1884 url_fprintf(pb
, "<H2>Connection Status</H2>\n");
1886 url_fprintf(pb
, "Number of connections: %d / %d<BR>\n",
1887 nb_connections
, nb_max_connections
);
1889 url_fprintf(pb
, "Bandwidth in use: %"PRIu64
"k / %"PRIu64
"k<BR>\n",
1890 current_bandwidth
, max_bandwidth
);
1892 url_fprintf(pb
, "<TABLE>\n");
1893 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");
1894 c1
= first_http_ctx
;
1896 while (c1
!= NULL
) {
1902 for (j
= 0; j
< c1
->stream
->nb_streams
; j
++) {
1903 if (!c1
->stream
->feed
)
1904 bitrate
+= c1
->stream
->streams
[j
]->codec
->bit_rate
;
1905 else if (c1
->feed_streams
[j
] >= 0)
1906 bitrate
+= c1
->stream
->feed
->streams
[c1
->feed_streams
[j
]]->codec
->bit_rate
;
1911 p
= inet_ntoa(c1
->from_addr
.sin_addr
);
1912 url_fprintf(pb
, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1914 c1
->stream
? c1
->stream
->filename
: "",
1915 c1
->state
== HTTPSTATE_RECEIVE_DATA
? "(input)" : "",
1918 http_state
[c1
->state
]);
1919 fmt_bytecount(pb
, bitrate
);
1920 url_fprintf(pb
, "<td align=right>");
1921 fmt_bytecount(pb
, compute_datarate(&c1
->datarate
, c1
->data_count
) * 8);
1922 url_fprintf(pb
, "<td align=right>");
1923 fmt_bytecount(pb
, c1
->data_count
);
1924 url_fprintf(pb
, "\n");
1927 url_fprintf(pb
, "</TABLE>\n");
1932 url_fprintf(pb
, "<HR size=1 noshade>Generated at %s", p
);
1933 url_fprintf(pb
, "</BODY>\n</HTML>\n");
1935 len
= url_close_dyn_buf(pb
, &c
->pb_buffer
);
1936 c
->buffer_ptr
= c
->pb_buffer
;
1937 c
->buffer_end
= c
->pb_buffer
+ len
;
1940 /* check if the parser needs to be opened for stream i */
1941 static void open_parser(AVFormatContext
*s
, int i
)
1943 AVStream
*st
= s
->streams
[i
];
1946 if (!st
->codec
->codec
) {
1947 codec
= avcodec_find_decoder(st
->codec
->codec_id
);
1948 if (codec
&& (codec
->capabilities
& CODEC_CAP_PARSE_ONLY
)) {
1949 st
->codec
->parse_only
= 1;
1950 if (avcodec_open(st
->codec
, codec
) < 0)
1951 st
->codec
->parse_only
= 0;
1956 static int open_input_stream(HTTPContext
*c
, const char *info
)
1959 char input_filename
[1024];
1961 int buf_size
, i
, ret
;
1964 /* find file name */
1965 if (c
->stream
->feed
) {
1966 strcpy(input_filename
, c
->stream
->feed
->feed_filename
);
1967 buf_size
= FFM_PACKET_SIZE
;
1968 /* compute position (absolute time) */
1969 if (find_info_tag(buf
, sizeof(buf
), "date", info
)) {
1970 stream_pos
= parse_date(buf
, 0);
1971 if (stream_pos
== INT64_MIN
)
1973 } else if (find_info_tag(buf
, sizeof(buf
), "buffer", info
)) {
1974 int prebuffer
= strtol(buf
, 0, 10);
1975 stream_pos
= av_gettime() - prebuffer
* (int64_t)1000000;
1977 stream_pos
= av_gettime() - c
->stream
->prebuffer
* (int64_t)1000;
1979 strcpy(input_filename
, c
->stream
->feed_filename
);
1981 /* compute position (relative time) */
1982 if (find_info_tag(buf
, sizeof(buf
), "date", info
)) {
1983 stream_pos
= parse_date(buf
, 1);
1984 if (stream_pos
== INT64_MIN
)
1989 if (input_filename
[0] == '\0')
1993 { time_t when
= stream_pos
/ 1000000;
1994 http_log("Stream pos = %"PRId64
", time=%s", stream_pos
, ctime(&when
));
1999 if ((ret
= av_open_input_file(&s
, input_filename
, c
->stream
->ifmt
,
2000 buf_size
, c
->stream
->ap_in
)) < 0) {
2001 http_log("could not open %s: %d\n", input_filename
, ret
);
2004 s
->flags
|= AVFMT_FLAG_GENPTS
;
2006 if (strcmp(s
->iformat
->name
, "ffm") && av_find_stream_info(c
->fmt_in
) < 0) {
2007 http_log("Could not find stream info '%s'\n", input_filename
);
2008 av_close_input_file(s
);
2012 /* open each parser */
2013 for(i
=0;i
<s
->nb_streams
;i
++)
2016 /* choose stream as clock source (we favorize video stream if
2017 present) for packet sending */
2018 c
->pts_stream_index
= 0;
2019 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2020 if (c
->pts_stream_index
== 0 &&
2021 c
->stream
->streams
[i
]->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
2022 c
->pts_stream_index
= i
;
2027 if (c
->fmt_in
->iformat
->read_seek
)
2028 av_seek_frame(c
->fmt_in
, -1, stream_pos
, 0);
2030 /* set the start time (needed for maxtime and RTP packet timing) */
2031 c
->start_time
= cur_time
;
2032 c
->first_pts
= AV_NOPTS_VALUE
;
2036 /* return the server clock (in us) */
2037 static int64_t get_server_clock(HTTPContext
*c
)
2039 /* compute current pts value from system time */
2040 return (cur_time
- c
->start_time
) * 1000;
2043 /* return the estimated time at which the current packet must be sent
2045 static int64_t get_packet_send_clock(HTTPContext
*c
)
2047 int bytes_left
, bytes_sent
, frame_bytes
;
2049 frame_bytes
= c
->cur_frame_bytes
;
2050 if (frame_bytes
<= 0)
2053 bytes_left
= c
->buffer_end
- c
->buffer_ptr
;
2054 bytes_sent
= frame_bytes
- bytes_left
;
2055 return c
->cur_pts
+ (c
->cur_frame_duration
* bytes_sent
) / frame_bytes
;
2060 static int http_prepare_data(HTTPContext
*c
)
2063 AVFormatContext
*ctx
;
2065 av_freep(&c
->pb_buffer
);
2067 case HTTPSTATE_SEND_DATA_HEADER
:
2068 memset(&c
->fmt_ctx
, 0, sizeof(c
->fmt_ctx
));
2069 av_metadata_set(&c
->fmt_ctx
.metadata
, "author" ,c
->stream
->author
);
2070 av_metadata_set(&c
->fmt_ctx
.metadata
, "comment" ,c
->stream
->comment
);
2071 av_metadata_set(&c
->fmt_ctx
.metadata
, "copyright",c
->stream
->copyright
);
2072 av_metadata_set(&c
->fmt_ctx
.metadata
, "title" ,c
->stream
->title
);
2074 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2077 st
= av_mallocz(sizeof(AVStream
));
2078 c
->fmt_ctx
.streams
[i
] = st
;
2079 /* if file or feed, then just take streams from FFStream struct */
2080 if (!c
->stream
->feed
||
2081 c
->stream
->feed
== c
->stream
)
2082 src
= c
->stream
->streams
[i
];
2084 src
= c
->stream
->feed
->streams
[c
->stream
->feed_streams
[i
]];
2088 st
->codec
->frame_number
= 0; /* XXX: should be done in
2089 AVStream, not in codec */
2091 /* set output format parameters */
2092 c
->fmt_ctx
.oformat
= c
->stream
->fmt
;
2093 c
->fmt_ctx
.nb_streams
= c
->stream
->nb_streams
;
2095 c
->got_key_frame
= 0;
2097 /* prepare header and save header data in a stream */
2098 if (url_open_dyn_buf(&c
->fmt_ctx
.pb
) < 0) {
2099 /* XXX: potential leak */
2102 c
->fmt_ctx
.pb
->is_streamed
= 1;
2105 * HACK to avoid mpeg ps muxer to spit many underflow errors
2106 * Default value from FFmpeg
2107 * Try to set it use configuration option
2109 c
->fmt_ctx
.preload
= (int)(0.5*AV_TIME_BASE
);
2110 c
->fmt_ctx
.max_delay
= (int)(0.7*AV_TIME_BASE
);
2112 av_set_parameters(&c
->fmt_ctx
, NULL
);
2113 if (av_write_header(&c
->fmt_ctx
) < 0) {
2114 http_log("Error writing output header\n");
2118 len
= url_close_dyn_buf(c
->fmt_ctx
.pb
, &c
->pb_buffer
);
2119 c
->buffer_ptr
= c
->pb_buffer
;
2120 c
->buffer_end
= c
->pb_buffer
+ len
;
2122 c
->state
= HTTPSTATE_SEND_DATA
;
2123 c
->last_packet_sent
= 0;
2125 case HTTPSTATE_SEND_DATA
:
2126 /* find a new packet */
2127 /* read a packet from the input stream */
2128 if (c
->stream
->feed
)
2129 ffm_set_write_index(c
->fmt_in
,
2130 c
->stream
->feed
->feed_write_index
,
2131 c
->stream
->feed
->feed_size
);
2133 if (c
->stream
->max_time
&&
2134 c
->stream
->max_time
+ c
->start_time
- cur_time
< 0)
2135 /* We have timed out */
2136 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2140 if (av_read_frame(c
->fmt_in
, &pkt
) < 0) {
2141 if (c
->stream
->feed
&& c
->stream
->feed
->feed_opened
) {
2142 /* if coming from feed, it means we reached the end of the
2143 ffm file, so must wait for more data */
2144 c
->state
= HTTPSTATE_WAIT_FEED
;
2145 return 1; /* state changed */
2147 if (c
->stream
->loop
) {
2148 av_close_input_file(c
->fmt_in
);
2150 if (open_input_stream(c
, "") < 0)
2155 /* must send trailer now because eof or error */
2156 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2160 int source_index
= pkt
.stream_index
;
2161 /* update first pts if needed */
2162 if (c
->first_pts
== AV_NOPTS_VALUE
) {
2163 c
->first_pts
= av_rescale_q(pkt
.dts
, c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
, AV_TIME_BASE_Q
);
2164 c
->start_time
= cur_time
;
2166 /* send it to the appropriate stream */
2167 if (c
->stream
->feed
) {
2168 /* if coming from a feed, select the right stream */
2169 if (c
->switch_pending
) {
2170 c
->switch_pending
= 0;
2171 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2172 if (c
->switch_feed_streams
[i
] == pkt
.stream_index
)
2173 if (pkt
.flags
& PKT_FLAG_KEY
)
2174 do_switch_stream(c
, i
);
2175 if (c
->switch_feed_streams
[i
] >= 0)
2176 c
->switch_pending
= 1;
2179 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2180 if (c
->feed_streams
[i
] == pkt
.stream_index
) {
2181 AVStream
*st
= c
->fmt_in
->streams
[source_index
];
2182 pkt
.stream_index
= i
;
2183 if (pkt
.flags
& PKT_FLAG_KEY
&&
2184 (st
->codec
->codec_type
== CODEC_TYPE_VIDEO
||
2185 c
->stream
->nb_streams
== 1))
2186 c
->got_key_frame
= 1;
2187 if (!c
->stream
->send_on_key
|| c
->got_key_frame
)
2192 AVCodecContext
*codec
;
2193 AVStream
*ist
, *ost
;
2195 ist
= c
->fmt_in
->streams
[source_index
];
2196 /* specific handling for RTP: we use several
2197 output stream (one for each RTP
2198 connection). XXX: need more abstract handling */
2199 if (c
->is_packetized
) {
2200 /* compute send time and duration */
2201 c
->cur_pts
= av_rescale_q(pkt
.dts
, ist
->time_base
, AV_TIME_BASE_Q
);
2202 if (ist
->start_time
!= AV_NOPTS_VALUE
)
2203 c
->cur_pts
-= av_rescale_q(ist
->start_time
, ist
->time_base
, AV_TIME_BASE_Q
);
2204 c
->cur_frame_duration
= av_rescale_q(pkt
.duration
, ist
->time_base
, AV_TIME_BASE_Q
);
2206 printf("index=%d pts=%0.3f duration=%0.6f\n",
2208 (double)c
->cur_pts
/
2210 (double)c
->cur_frame_duration
/
2213 /* find RTP context */
2214 c
->packet_stream_index
= pkt
.stream_index
;
2215 ctx
= c
->rtp_ctx
[c
->packet_stream_index
];
2217 av_free_packet(&pkt
);
2220 codec
= ctx
->streams
[0]->codec
;
2221 /* only one stream per RTP connection */
2222 pkt
.stream_index
= 0;
2226 codec
= ctx
->streams
[pkt
.stream_index
]->codec
;
2229 if (c
->is_packetized
) {
2230 int max_packet_size
;
2231 if (c
->rtp_protocol
== RTSP_LOWER_TRANSPORT_TCP
)
2232 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
2234 max_packet_size
= url_get_max_packet_size(c
->rtp_handles
[c
->packet_stream_index
]);
2235 ret
= url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
);
2237 ret
= url_open_dyn_buf(&ctx
->pb
);
2240 /* XXX: potential leak */
2243 ost
= ctx
->streams
[pkt
.stream_index
];
2245 ctx
->pb
->is_streamed
= 1;
2246 if (pkt
.dts
!= AV_NOPTS_VALUE
)
2247 pkt
.dts
= av_rescale_q(pkt
.dts
, ist
->time_base
, ost
->time_base
);
2248 if (pkt
.pts
!= AV_NOPTS_VALUE
)
2249 pkt
.pts
= av_rescale_q(pkt
.pts
, ist
->time_base
, ost
->time_base
);
2250 pkt
.duration
= av_rescale_q(pkt
.duration
, ist
->time_base
, ost
->time_base
);
2251 if (av_write_frame(ctx
, &pkt
) < 0) {
2252 http_log("Error writing frame to output\n");
2253 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2256 len
= url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
2257 c
->cur_frame_bytes
= len
;
2258 c
->buffer_ptr
= c
->pb_buffer
;
2259 c
->buffer_end
= c
->pb_buffer
+ len
;
2261 codec
->frame_number
++;
2263 av_free_packet(&pkt
);
2267 av_free_packet(&pkt
);
2272 case HTTPSTATE_SEND_DATA_TRAILER
:
2273 /* last packet test ? */
2274 if (c
->last_packet_sent
|| c
->is_packetized
)
2277 /* prepare header */
2278 if (url_open_dyn_buf(&ctx
->pb
) < 0) {
2279 /* XXX: potential leak */
2282 c
->fmt_ctx
.pb
->is_streamed
= 1;
2283 av_write_trailer(ctx
);
2284 len
= url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
2285 c
->buffer_ptr
= c
->pb_buffer
;
2286 c
->buffer_end
= c
->pb_buffer
+ len
;
2288 c
->last_packet_sent
= 1;
2294 /* should convert the format at the same time */
2295 /* send data starting at c->buffer_ptr to the output connection
2296 (either UDP or TCP connection) */
2297 static int http_send_data(HTTPContext
*c
)
2302 if (c
->buffer_ptr
>= c
->buffer_end
) {
2303 ret
= http_prepare_data(c
);
2307 /* state change requested */
2310 if (c
->is_packetized
) {
2311 /* RTP data output */
2312 len
= c
->buffer_end
- c
->buffer_ptr
;
2314 /* fail safe - should never happen */
2316 c
->buffer_ptr
= c
->buffer_end
;
2319 len
= (c
->buffer_ptr
[0] << 24) |
2320 (c
->buffer_ptr
[1] << 16) |
2321 (c
->buffer_ptr
[2] << 8) |
2323 if (len
> (c
->buffer_end
- c
->buffer_ptr
))
2325 if ((get_packet_send_clock(c
) - get_server_clock(c
)) > 0) {
2326 /* nothing to send yet: we can wait */
2330 c
->data_count
+= len
;
2331 update_datarate(&c
->datarate
, c
->data_count
);
2333 c
->stream
->bytes_served
+= len
;
2335 if (c
->rtp_protocol
== RTSP_LOWER_TRANSPORT_TCP
) {
2336 /* RTP packets are sent inside the RTSP TCP connection */
2338 int interleaved_index
, size
;
2340 HTTPContext
*rtsp_c
;
2343 /* if no RTSP connection left, error */
2346 /* if already sending something, then wait. */
2347 if (rtsp_c
->state
!= RTSPSTATE_WAIT_REQUEST
)
2349 if (url_open_dyn_buf(&pb
) < 0)
2351 interleaved_index
= c
->packet_stream_index
* 2;
2352 /* RTCP packets are sent at odd indexes */
2353 if (c
->buffer_ptr
[1] == 200)
2354 interleaved_index
++;
2355 /* write RTSP TCP header */
2357 header
[1] = interleaved_index
;
2358 header
[2] = len
>> 8;
2360 put_buffer(pb
, header
, 4);
2361 /* write RTP packet data */
2363 put_buffer(pb
, c
->buffer_ptr
, len
);
2364 size
= url_close_dyn_buf(pb
, &c
->packet_buffer
);
2365 /* prepare asynchronous TCP sending */
2366 rtsp_c
->packet_buffer_ptr
= c
->packet_buffer
;
2367 rtsp_c
->packet_buffer_end
= c
->packet_buffer
+ size
;
2368 c
->buffer_ptr
+= len
;
2370 /* send everything we can NOW */
2371 len
= send(rtsp_c
->fd
, rtsp_c
->packet_buffer_ptr
,
2372 rtsp_c
->packet_buffer_end
- rtsp_c
->packet_buffer_ptr
, 0);
2374 rtsp_c
->packet_buffer_ptr
+= len
;
2375 if (rtsp_c
->packet_buffer_ptr
< rtsp_c
->packet_buffer_end
) {
2376 /* if we could not send all the data, we will
2377 send it later, so a new state is needed to
2378 "lock" the RTSP TCP connection */
2379 rtsp_c
->state
= RTSPSTATE_SEND_PACKET
;
2382 /* all data has been sent */
2383 av_freep(&c
->packet_buffer
);
2385 /* send RTP packet directly in UDP */
2387 url_write(c
->rtp_handles
[c
->packet_stream_index
],
2388 c
->buffer_ptr
, len
);
2389 c
->buffer_ptr
+= len
;
2390 /* here we continue as we can send several packets per 10 ms slot */
2393 /* TCP data output */
2394 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2396 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
2397 ff_neterrno() != FF_NETERROR(EINTR
))
2398 /* error : close connection */
2403 c
->buffer_ptr
+= len
;
2405 c
->data_count
+= len
;
2406 update_datarate(&c
->datarate
, c
->data_count
);
2408 c
->stream
->bytes_served
+= len
;
2416 static int http_start_receive_data(HTTPContext
*c
)
2420 if (c
->stream
->feed_opened
)
2423 /* Don't permit writing to this one */
2424 if (c
->stream
->readonly
)
2428 fd
= open(c
->stream
->feed_filename
, O_RDWR
);
2430 http_log("Error opening feeder file: %s\n", strerror(errno
));
2435 if (c
->stream
->truncate
) {
2436 /* truncate feed file */
2437 ffm_write_write_index(c
->feed_fd
, FFM_PACKET_SIZE
);
2438 ftruncate(c
->feed_fd
, FFM_PACKET_SIZE
);
2439 http_log("Truncating feed file '%s'\n", c
->stream
->feed_filename
);
2441 if ((c
->stream
->feed_write_index
= ffm_read_write_index(fd
)) < 0) {
2442 http_log("Error reading write index from feed file: %s\n", strerror(errno
));
2447 c
->stream
->feed_write_index
= FFMAX(ffm_read_write_index(fd
), FFM_PACKET_SIZE
);
2448 c
->stream
->feed_size
= lseek(fd
, 0, SEEK_END
);
2449 lseek(fd
, 0, SEEK_SET
);
2451 /* init buffer input */
2452 c
->buffer_ptr
= c
->buffer
;
2453 c
->buffer_end
= c
->buffer
+ FFM_PACKET_SIZE
;
2454 c
->stream
->feed_opened
= 1;
2458 static int http_receive_data(HTTPContext
*c
)
2462 if (c
->buffer_end
> c
->buffer_ptr
) {
2465 len
= recv(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2467 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
2468 ff_neterrno() != FF_NETERROR(EINTR
))
2469 /* error : close connection */
2471 } else if (len
== 0)
2472 /* end of connection : close it */
2475 c
->buffer_ptr
+= len
;
2476 c
->data_count
+= len
;
2477 update_datarate(&c
->datarate
, c
->data_count
);
2481 if (c
->buffer_ptr
- c
->buffer
>= 2 && c
->data_count
> FFM_PACKET_SIZE
) {
2482 if (c
->buffer
[0] != 'f' ||
2483 c
->buffer
[1] != 'm') {
2484 http_log("Feed stream has become desynchronized -- disconnecting\n");
2489 if (c
->buffer_ptr
>= c
->buffer_end
) {
2490 FFStream
*feed
= c
->stream
;
2491 /* a packet has been received : write it in the store, except
2493 if (c
->data_count
> FFM_PACKET_SIZE
) {
2495 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2496 /* XXX: use llseek or url_seek */
2497 lseek(c
->feed_fd
, feed
->feed_write_index
, SEEK_SET
);
2498 if (write(c
->feed_fd
, c
->buffer
, FFM_PACKET_SIZE
) < 0) {
2499 http_log("Error writing to feed file: %s\n", strerror(errno
));
2503 feed
->feed_write_index
+= FFM_PACKET_SIZE
;
2504 /* update file size */
2505 if (feed
->feed_write_index
> c
->stream
->feed_size
)
2506 feed
->feed_size
= feed
->feed_write_index
;
2508 /* handle wrap around if max file size reached */
2509 if (c
->stream
->feed_max_size
&& feed
->feed_write_index
>= c
->stream
->feed_max_size
)
2510 feed
->feed_write_index
= FFM_PACKET_SIZE
;
2513 if (ffm_write_write_index(c
->feed_fd
, feed
->feed_write_index
) < 0) {
2514 http_log("Error writing index to feed file: %s\n", strerror(errno
));
2518 /* wake up any waiting connections */
2519 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
2520 if (c1
->state
== HTTPSTATE_WAIT_FEED
&&
2521 c1
->stream
->feed
== c
->stream
->feed
)
2522 c1
->state
= HTTPSTATE_SEND_DATA
;
2525 /* We have a header in our hands that contains useful data */
2526 AVFormatContext
*s
= NULL
;
2528 AVInputFormat
*fmt_in
;
2531 /* use feed output format name to find corresponding input format */
2532 fmt_in
= av_find_input_format(feed
->fmt
->name
);
2536 url_open_buf(&pb
, c
->buffer
, c
->buffer_end
- c
->buffer
, URL_RDONLY
);
2537 pb
->is_streamed
= 1;
2539 if (av_open_input_stream(&s
, pb
, c
->stream
->feed_filename
, fmt_in
, NULL
) < 0) {
2544 /* Now we have the actual streams */
2545 if (s
->nb_streams
!= feed
->nb_streams
) {
2546 av_close_input_stream(s
);
2548 http_log("Feed '%s' stream number does not match registered feed\n",
2549 c
->stream
->feed_filename
);
2553 for (i
= 0; i
< s
->nb_streams
; i
++) {
2554 AVStream
*fst
= feed
->streams
[i
];
2555 AVStream
*st
= s
->streams
[i
];
2556 memcpy(fst
->codec
, st
->codec
, sizeof(AVCodecContext
));
2557 if (fst
->codec
->extradata_size
) {
2558 fst
->codec
->extradata
= av_malloc(fst
->codec
->extradata_size
);
2559 if (!fst
->codec
->extradata
)
2561 memcpy(fst
->codec
->extradata
, st
->codec
->extradata
,
2562 fst
->codec
->extradata_size
);
2566 av_close_input_stream(s
);
2569 c
->buffer_ptr
= c
->buffer
;
2574 c
->stream
->feed_opened
= 0;
2576 /* wake up any waiting connections to stop waiting for feed */
2577 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
2578 if (c1
->state
== HTTPSTATE_WAIT_FEED
&&
2579 c1
->stream
->feed
== c
->stream
->feed
)
2580 c1
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2585 /********************************************************************/
2588 static void rtsp_reply_header(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2595 switch(error_number
) {
2596 case RTSP_STATUS_OK
:
2599 case RTSP_STATUS_METHOD
:
2600 str
= "Method Not Allowed";
2602 case RTSP_STATUS_BANDWIDTH
:
2603 str
= "Not Enough Bandwidth";
2605 case RTSP_STATUS_SESSION
:
2606 str
= "Session Not Found";
2608 case RTSP_STATUS_STATE
:
2609 str
= "Method Not Valid in This State";
2611 case RTSP_STATUS_AGGREGATE
:
2612 str
= "Aggregate operation not allowed";
2614 case RTSP_STATUS_ONLY_AGGREGATE
:
2615 str
= "Only aggregate operation allowed";
2617 case RTSP_STATUS_TRANSPORT
:
2618 str
= "Unsupported transport";
2620 case RTSP_STATUS_INTERNAL
:
2621 str
= "Internal Server Error";
2623 case RTSP_STATUS_SERVICE
:
2624 str
= "Service Unavailable";
2626 case RTSP_STATUS_VERSION
:
2627 str
= "RTSP Version not supported";
2630 str
= "Unknown Error";
2634 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", error_number
, str
);
2635 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2637 /* output GMT time */
2641 p
= buf2
+ strlen(p
) - 1;
2644 url_fprintf(c
->pb
, "Date: %s GMT\r\n", buf2
);
2647 static void rtsp_reply_error(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2649 rtsp_reply_header(c
, error_number
);
2650 url_fprintf(c
->pb
, "\r\n");
2653 static int rtsp_parse_request(HTTPContext
*c
)
2655 const char *p
, *p1
, *p2
;
2661 RTSPMessageHeader header1
, *header
= &header1
;
2663 c
->buffer_ptr
[0] = '\0';
2666 get_word(cmd
, sizeof(cmd
), &p
);
2667 get_word(url
, sizeof(url
), &p
);
2668 get_word(protocol
, sizeof(protocol
), &p
);
2670 av_strlcpy(c
->method
, cmd
, sizeof(c
->method
));
2671 av_strlcpy(c
->url
, url
, sizeof(c
->url
));
2672 av_strlcpy(c
->protocol
, protocol
, sizeof(c
->protocol
));
2674 if (url_open_dyn_buf(&c
->pb
) < 0) {
2675 /* XXX: cannot do more */
2676 c
->pb
= NULL
; /* safety */
2680 /* check version name */
2681 if (strcmp(protocol
, "RTSP/1.0") != 0) {
2682 rtsp_reply_error(c
, RTSP_STATUS_VERSION
);
2686 /* parse each header line */
2687 memset(header
, 0, sizeof(*header
));
2688 /* skip to next line */
2689 while (*p
!= '\n' && *p
!= '\0')
2693 while (*p
!= '\0') {
2694 p1
= strchr(p
, '\n');
2698 if (p2
> p
&& p2
[-1] == '\r')
2700 /* skip empty line */
2704 if (len
> sizeof(line
) - 1)
2705 len
= sizeof(line
) - 1;
2706 memcpy(line
, p
, len
);
2708 rtsp_parse_line(header
, line
);
2712 /* handle sequence number */
2713 c
->seq
= header
->seq
;
2715 if (!strcmp(cmd
, "DESCRIBE"))
2716 rtsp_cmd_describe(c
, url
);
2717 else if (!strcmp(cmd
, "OPTIONS"))
2718 rtsp_cmd_options(c
, url
);
2719 else if (!strcmp(cmd
, "SETUP"))
2720 rtsp_cmd_setup(c
, url
, header
);
2721 else if (!strcmp(cmd
, "PLAY"))
2722 rtsp_cmd_play(c
, url
, header
);
2723 else if (!strcmp(cmd
, "PAUSE"))
2724 rtsp_cmd_pause(c
, url
, header
);
2725 else if (!strcmp(cmd
, "TEARDOWN"))
2726 rtsp_cmd_teardown(c
, url
, header
);
2728 rtsp_reply_error(c
, RTSP_STATUS_METHOD
);
2731 len
= url_close_dyn_buf(c
->pb
, &c
->pb_buffer
);
2732 c
->pb
= NULL
; /* safety */
2734 /* XXX: cannot do more */
2737 c
->buffer_ptr
= c
->pb_buffer
;
2738 c
->buffer_end
= c
->pb_buffer
+ len
;
2739 c
->state
= RTSPSTATE_SEND_REPLY
;
2743 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
2744 struct in_addr my_ip
)
2746 AVFormatContext
*avc
;
2747 AVStream avs
[MAX_STREAMS
];
2750 avc
= avformat_alloc_context();
2754 av_metadata_set(&avc
->metadata
, "title",
2755 stream
->title
[0] ? stream
->title
: "No Title");
2756 avc
->nb_streams
= stream
->nb_streams
;
2757 if (stream
->is_multicast
) {
2758 snprintf(avc
->filename
, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2759 inet_ntoa(stream
->multicast_ip
),
2760 stream
->multicast_port
, stream
->multicast_ttl
);
2763 for(i
= 0; i
< stream
->nb_streams
; i
++) {
2764 avc
->streams
[i
] = &avs
[i
];
2765 avc
->streams
[i
]->codec
= stream
->streams
[i
]->codec
;
2767 *pbuffer
= av_mallocz(2048);
2768 avf_sdp_create(&avc
, 1, *pbuffer
, 2048);
2771 return strlen(*pbuffer
);
2774 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
)
2776 // rtsp_reply_header(c, RTSP_STATUS_OK);
2777 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK
, "OK");
2778 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2779 url_fprintf(c
->pb
, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2780 url_fprintf(c
->pb
, "\r\n");
2783 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
)
2789 int content_length
, len
;
2790 struct sockaddr_in my_addr
;
2792 /* find which url is asked */
2793 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2798 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2799 if (!stream
->is_feed
&&
2800 stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp") &&
2801 !strcmp(path
, stream
->filename
)) {
2805 /* no stream found */
2806 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2810 /* prepare the media description in sdp format */
2812 /* get the host IP */
2813 len
= sizeof(my_addr
);
2814 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
2815 content_length
= prepare_sdp_description(stream
, &content
, my_addr
.sin_addr
);
2816 if (content_length
< 0) {
2817 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2820 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2821 url_fprintf(c
->pb
, "Content-Type: application/sdp\r\n");
2822 url_fprintf(c
->pb
, "Content-Length: %d\r\n", content_length
);
2823 url_fprintf(c
->pb
, "\r\n");
2824 put_buffer(c
->pb
, content
, content_length
);
2827 static HTTPContext
*find_rtp_session(const char *session_id
)
2831 if (session_id
[0] == '\0')
2834 for(c
= first_http_ctx
; c
!= NULL
; c
= c
->next
) {
2835 if (!strcmp(c
->session_id
, session_id
))
2841 static RTSPTransportField
*find_transport(RTSPMessageHeader
*h
, enum RTSPLowerTransport lower_transport
)
2843 RTSPTransportField
*th
;
2846 for(i
=0;i
<h
->nb_transports
;i
++) {
2847 th
= &h
->transports
[i
];
2848 if (th
->lower_transport
== lower_transport
)
2854 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
,
2855 RTSPMessageHeader
*h
)
2858 int stream_index
, port
;
2863 RTSPTransportField
*th
;
2864 struct sockaddr_in dest_addr
;
2865 RTSPActionServerSetup setup
;
2867 /* find which url is asked */
2868 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2873 /* now check each stream */
2874 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2875 if (!stream
->is_feed
&&
2876 stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
2877 /* accept aggregate filenames only if single stream */
2878 if (!strcmp(path
, stream
->filename
)) {
2879 if (stream
->nb_streams
!= 1) {
2880 rtsp_reply_error(c
, RTSP_STATUS_AGGREGATE
);
2887 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
2889 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2890 stream
->filename
, stream_index
);
2891 if (!strcmp(path
, buf
))
2896 /* no stream found */
2897 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2901 /* generate session id if needed */
2902 if (h
->session_id
[0] == '\0')
2903 snprintf(h
->session_id
, sizeof(h
->session_id
), "%08x%08x",
2904 av_lfg_get(&random_state
), av_lfg_get(&random_state
));
2906 /* find rtp session, and create it if none found */
2907 rtp_c
= find_rtp_session(h
->session_id
);
2909 /* always prefer UDP */
2910 th
= find_transport(h
, RTSP_LOWER_TRANSPORT_UDP
);
2912 th
= find_transport(h
, RTSP_LOWER_TRANSPORT_TCP
);
2914 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2919 rtp_c
= rtp_new_connection(&c
->from_addr
, stream
, h
->session_id
,
2920 th
->lower_transport
);
2922 rtsp_reply_error(c
, RTSP_STATUS_BANDWIDTH
);
2926 /* open input stream */
2927 if (open_input_stream(rtp_c
, "") < 0) {
2928 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2933 /* test if stream is OK (test needed because several SETUP needs
2934 to be done for a given file) */
2935 if (rtp_c
->stream
!= stream
) {
2936 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
);
2940 /* test if stream is already set up */
2941 if (rtp_c
->rtp_ctx
[stream_index
]) {
2942 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2946 /* check transport */
2947 th
= find_transport(h
, rtp_c
->rtp_protocol
);
2948 if (!th
|| (th
->lower_transport
== RTSP_LOWER_TRANSPORT_UDP
&&
2949 th
->client_port_min
<= 0)) {
2950 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2954 /* setup default options */
2955 setup
.transport_option
[0] = '\0';
2956 dest_addr
= rtp_c
->from_addr
;
2957 dest_addr
.sin_port
= htons(th
->client_port_min
);
2960 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, c
) < 0) {
2961 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2965 /* now everything is OK, so we can send the connection parameters */
2966 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2968 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2970 switch(rtp_c
->rtp_protocol
) {
2971 case RTSP_LOWER_TRANSPORT_UDP
:
2972 port
= rtp_get_local_port(rtp_c
->rtp_handles
[stream_index
]);
2973 url_fprintf(c
->pb
, "Transport: RTP/AVP/UDP;unicast;"
2974 "client_port=%d-%d;server_port=%d-%d",
2975 th
->client_port_min
, th
->client_port_min
+ 1,
2978 case RTSP_LOWER_TRANSPORT_TCP
:
2979 url_fprintf(c
->pb
, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2980 stream_index
* 2, stream_index
* 2 + 1);
2985 if (setup
.transport_option
[0] != '\0')
2986 url_fprintf(c
->pb
, ";%s", setup
.transport_option
);
2987 url_fprintf(c
->pb
, "\r\n");
2990 url_fprintf(c
->pb
, "\r\n");
2994 /* find an rtp connection by using the session ID. Check consistency
2996 static HTTPContext
*find_rtp_session_with_url(const char *url
,
2997 const char *session_id
)
3005 rtp_c
= find_rtp_session(session_id
);
3009 /* find which url is asked */
3010 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
3014 if(!strcmp(path
, rtp_c
->stream
->filename
)) return rtp_c
;
3015 for(s
=0; s
<rtp_c
->stream
->nb_streams
; ++s
) {
3016 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
3017 rtp_c
->stream
->filename
, s
);
3018 if(!strncmp(path
, buf
, sizeof(buf
))) {
3019 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
3026 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPMessageHeader
*h
)
3030 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
3032 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3036 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
3037 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
&&
3038 rtp_c
->state
!= HTTPSTATE_READY
) {
3039 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
3044 /* XXX: seek in stream */
3045 if (h
->range_start
!= AV_NOPTS_VALUE
) {
3046 printf("range_start=%0.3f\n", (double)h
->range_start
/ AV_TIME_BASE
);
3047 av_seek_frame(rtp_c
->fmt_in
, -1, h
->range_start
);
3051 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
3053 /* now everything is OK, so we can send the connection parameters */
3054 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3056 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
3057 url_fprintf(c
->pb
, "\r\n");
3060 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPMessageHeader
*h
)
3064 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
3066 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3070 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
3071 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
) {
3072 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
3076 rtp_c
->state
= HTTPSTATE_READY
;
3077 rtp_c
->first_pts
= AV_NOPTS_VALUE
;
3078 /* now everything is OK, so we can send the connection parameters */
3079 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3081 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
3082 url_fprintf(c
->pb
, "\r\n");
3085 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPMessageHeader
*h
)
3088 char session_id
[32];
3090 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
3092 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3096 av_strlcpy(session_id
, rtp_c
->session_id
, sizeof(session_id
));
3098 /* abort the session */
3099 close_connection(rtp_c
);
3101 /* now everything is OK, so we can send the connection parameters */
3102 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3104 url_fprintf(c
->pb
, "Session: %s\r\n", session_id
);
3105 url_fprintf(c
->pb
, "\r\n");
3109 /********************************************************************/
3112 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
3113 FFStream
*stream
, const char *session_id
,
3114 enum RTSPLowerTransport rtp_protocol
)
3116 HTTPContext
*c
= NULL
;
3117 const char *proto_str
;
3119 /* XXX: should output a warning page when coming
3120 close to the connection limit */
3121 if (nb_connections
>= nb_max_connections
)
3124 /* add a new connection */
3125 c
= av_mallocz(sizeof(HTTPContext
));
3130 c
->poll_entry
= NULL
;
3131 c
->from_addr
= *from_addr
;
3132 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
3133 c
->buffer
= av_malloc(c
->buffer_size
);
3138 av_strlcpy(c
->session_id
, session_id
, sizeof(c
->session_id
));
3139 c
->state
= HTTPSTATE_READY
;
3140 c
->is_packetized
= 1;
3141 c
->rtp_protocol
= rtp_protocol
;
3143 /* protocol is shown in statistics */
3144 switch(c
->rtp_protocol
) {
3145 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST
:
3146 proto_str
= "MCAST";
3148 case RTSP_LOWER_TRANSPORT_UDP
:
3151 case RTSP_LOWER_TRANSPORT_TCP
:
3158 av_strlcpy(c
->protocol
, "RTP/", sizeof(c
->protocol
));
3159 av_strlcat(c
->protocol
, proto_str
, sizeof(c
->protocol
));
3161 current_bandwidth
+= stream
->bandwidth
;
3163 c
->next
= first_http_ctx
;
3175 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3176 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3178 static int rtp_new_av_stream(HTTPContext
*c
,
3179 int stream_index
, struct sockaddr_in
*dest_addr
,
3180 HTTPContext
*rtsp_c
)
3182 AVFormatContext
*ctx
;
3185 URLContext
*h
= NULL
;
3187 int max_packet_size
;
3189 /* now we can open the relevant output stream */
3190 ctx
= avformat_alloc_context();
3193 ctx
->oformat
= guess_format("rtp", NULL
, NULL
);
3195 st
= av_mallocz(sizeof(AVStream
));
3198 st
->codec
= avcodec_alloc_context();
3199 ctx
->nb_streams
= 1;
3200 ctx
->streams
[0] = st
;
3202 if (!c
->stream
->feed
||
3203 c
->stream
->feed
== c
->stream
)
3204 memcpy(st
, c
->stream
->streams
[stream_index
], sizeof(AVStream
));
3207 c
->stream
->feed
->streams
[c
->stream
->feed_streams
[stream_index
]],
3209 st
->priv_data
= NULL
;
3211 /* build destination RTP address */
3212 ipaddr
= inet_ntoa(dest_addr
->sin_addr
);
3214 switch(c
->rtp_protocol
) {
3215 case RTSP_LOWER_TRANSPORT_UDP
:
3216 case RTSP_LOWER_TRANSPORT_UDP_MULTICAST
:
3219 /* XXX: also pass as parameter to function ? */
3220 if (c
->stream
->is_multicast
) {
3222 ttl
= c
->stream
->multicast_ttl
;
3225 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3226 "rtp://%s:%d?multicast=1&ttl=%d",
3227 ipaddr
, ntohs(dest_addr
->sin_port
), ttl
);
3229 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3230 "rtp://%s:%d", ipaddr
, ntohs(dest_addr
->sin_port
));
3233 if (url_open(&h
, ctx
->filename
, URL_WRONLY
) < 0)
3235 c
->rtp_handles
[stream_index
] = h
;
3236 max_packet_size
= url_get_max_packet_size(h
);
3238 case RTSP_LOWER_TRANSPORT_TCP
:
3241 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
3247 http_log("%s:%d - - \"PLAY %s/streamid=%d %s\"\n",
3248 ipaddr
, ntohs(dest_addr
->sin_port
),
3249 c
->stream
->filename
, stream_index
, c
->protocol
);
3251 /* normally, no packets should be output here, but the packet size may be checked */
3252 if (url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
) < 0) {
3253 /* XXX: close stream */
3256 av_set_parameters(ctx
, NULL
);
3257 if (av_write_header(ctx
) < 0) {
3264 url_close_dyn_buf(ctx
->pb
, &dummy_buf
);
3267 c
->rtp_ctx
[stream_index
] = ctx
;
3271 /********************************************************************/
3272 /* ffserver initialization */
3274 static AVStream
*add_av_stream1(FFStream
*stream
, AVCodecContext
*codec
)
3278 fst
= av_mallocz(sizeof(AVStream
));
3281 fst
->codec
= avcodec_alloc_context();
3282 fst
->priv_data
= av_mallocz(sizeof(FeedData
));
3283 memcpy(fst
->codec
, codec
, sizeof(AVCodecContext
));
3284 fst
->index
= stream
->nb_streams
;
3285 av_set_pts_info(fst
, 33, 1, 90000);
3286 stream
->streams
[stream
->nb_streams
++] = fst
;
3290 /* return the stream number in the feed */
3291 static int add_av_stream(FFStream
*feed
, AVStream
*st
)
3294 AVCodecContext
*av
, *av1
;
3298 for(i
=0;i
<feed
->nb_streams
;i
++) {
3299 st
= feed
->streams
[i
];
3301 if (av1
->codec_id
== av
->codec_id
&&
3302 av1
->codec_type
== av
->codec_type
&&
3303 av1
->bit_rate
== av
->bit_rate
) {
3305 switch(av
->codec_type
) {
3306 case CODEC_TYPE_AUDIO
:
3307 if (av1
->channels
== av
->channels
&&
3308 av1
->sample_rate
== av
->sample_rate
)
3311 case CODEC_TYPE_VIDEO
:
3312 if (av1
->width
== av
->width
&&
3313 av1
->height
== av
->height
&&
3314 av1
->time_base
.den
== av
->time_base
.den
&&
3315 av1
->time_base
.num
== av
->time_base
.num
&&
3316 av1
->gop_size
== av
->gop_size
)
3325 fst
= add_av_stream1(feed
, av
);
3328 return feed
->nb_streams
- 1;
3333 static void remove_stream(FFStream
*stream
)
3337 while (*ps
!= NULL
) {
3345 /* specific mpeg4 handling : we extract the raw parameters */
3346 static void extract_mpeg4_header(AVFormatContext
*infile
)
3348 int mpeg4_count
, i
, size
;
3354 for(i
=0;i
<infile
->nb_streams
;i
++) {
3355 st
= infile
->streams
[i
];
3356 if (st
->codec
->codec_id
== CODEC_ID_MPEG4
&&
3357 st
->codec
->extradata_size
== 0) {
3364 printf("MPEG4 without extra data: trying to find header in %s\n", infile
->filename
);
3365 while (mpeg4_count
> 0) {
3366 if (av_read_packet(infile
, &pkt
) < 0)
3368 st
= infile
->streams
[pkt
.stream_index
];
3369 if (st
->codec
->codec_id
== CODEC_ID_MPEG4
&&
3370 st
->codec
->extradata_size
== 0) {
3371 av_freep(&st
->codec
->extradata
);
3372 /* fill extradata with the header */
3373 /* XXX: we make hard suppositions here ! */
3375 while (p
< pkt
.data
+ pkt
.size
- 4) {
3376 /* stop when vop header is found */
3377 if (p
[0] == 0x00 && p
[1] == 0x00 &&
3378 p
[2] == 0x01 && p
[3] == 0xb6) {
3379 size
= p
- pkt
.data
;
3380 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3381 st
->codec
->extradata
= av_malloc(size
);
3382 st
->codec
->extradata_size
= size
;
3383 memcpy(st
->codec
->extradata
, pkt
.data
, size
);
3390 av_free_packet(&pkt
);
3394 /* compute the needed AVStream for each file */
3395 static void build_file_streams(void)
3397 FFStream
*stream
, *stream_next
;
3398 AVFormatContext
*infile
;
3401 /* gather all streams */
3402 for(stream
= first_stream
; stream
!= NULL
; stream
= stream_next
) {
3403 stream_next
= stream
->next
;
3404 if (stream
->stream_type
== STREAM_TYPE_LIVE
&&
3406 /* the stream comes from a file */
3407 /* try to open the file */
3409 stream
->ap_in
= av_mallocz(sizeof(AVFormatParameters
));
3410 if (stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
3411 /* specific case : if transport stream output to RTP,
3412 we use a raw transport stream reader */
3413 stream
->ap_in
->mpeg2ts_raw
= 1;
3414 stream
->ap_in
->mpeg2ts_compute_pcr
= 1;
3417 http_log("Opening file '%s'\n", stream
->feed_filename
);
3418 if ((ret
= av_open_input_file(&infile
, stream
->feed_filename
,
3419 stream
->ifmt
, 0, stream
->ap_in
)) < 0) {
3420 http_log("Could not open '%s': %d\n", stream
->feed_filename
, ret
);
3421 /* remove stream (no need to spend more time on it) */
3423 remove_stream(stream
);
3425 /* find all the AVStreams inside and reference them in
3427 if (av_find_stream_info(infile
) < 0) {
3428 http_log("Could not find codec parameters from '%s'\n",
3429 stream
->feed_filename
);
3430 av_close_input_file(infile
);
3433 extract_mpeg4_header(infile
);
3435 for(i
=0;i
<infile
->nb_streams
;i
++)
3436 add_av_stream1(stream
, infile
->streams
[i
]->codec
);
3438 av_close_input_file(infile
);
3444 /* compute the needed AVStream for each feed */
3445 static void build_feed_streams(void)
3447 FFStream
*stream
, *feed
;
3450 /* gather all streams */
3451 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3452 feed
= stream
->feed
;
3454 if (!stream
->is_feed
) {
3455 /* we handle a stream coming from a feed */
3456 for(i
=0;i
<stream
->nb_streams
;i
++)
3457 stream
->feed_streams
[i
] = add_av_stream(feed
, stream
->streams
[i
]);
3462 /* gather all streams */
3463 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3464 feed
= stream
->feed
;
3466 if (stream
->is_feed
) {
3467 for(i
=0;i
<stream
->nb_streams
;i
++)
3468 stream
->feed_streams
[i
] = i
;
3473 /* create feed files if needed */
3474 for(feed
= first_feed
; feed
!= NULL
; feed
= feed
->next_feed
) {
3477 if (url_exist(feed
->feed_filename
)) {
3478 /* See if it matches */
3482 if (av_open_input_file(&s
, feed
->feed_filename
, NULL
, FFM_PACKET_SIZE
, NULL
) >= 0) {
3483 /* Now see if it matches */
3484 if (s
->nb_streams
== feed
->nb_streams
) {
3486 for(i
=0;i
<s
->nb_streams
;i
++) {
3488 sf
= feed
->streams
[i
];
3491 if (sf
->index
!= ss
->index
||
3493 http_log("Index & Id do not match for stream %d (%s)\n",
3494 i
, feed
->feed_filename
);
3497 AVCodecContext
*ccf
, *ccs
;
3501 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3503 if (CHECK_CODEC(codec
) || CHECK_CODEC(codec_type
)) {
3504 http_log("Codecs do not match for stream %d\n", i
);
3506 } else if (CHECK_CODEC(bit_rate
) || CHECK_CODEC(flags
)) {
3507 http_log("Codec bitrates do not match for stream %d\n", i
);
3509 } else if (ccf
->codec_type
== CODEC_TYPE_VIDEO
) {
3510 if (CHECK_CODEC(time_base
.den
) ||
3511 CHECK_CODEC(time_base
.num
) ||
3512 CHECK_CODEC(width
) ||
3513 CHECK_CODEC(height
)) {
3514 http_log("Codec width, height and framerate do not match for stream %d\n", i
);
3517 } else if (ccf
->codec_type
== CODEC_TYPE_AUDIO
) {
3518 if (CHECK_CODEC(sample_rate
) ||
3519 CHECK_CODEC(channels
) ||
3520 CHECK_CODEC(frame_size
)) {
3521 http_log("Codec sample_rate, channels, frame_size do not match for stream %d\n", i
);
3525 http_log("Unknown codec type\n");
3533 http_log("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3534 feed
->feed_filename
, s
->nb_streams
, feed
->nb_streams
);
3536 av_close_input_file(s
);
3538 http_log("Deleting feed file '%s' as it appears to be corrupt\n",
3539 feed
->feed_filename
);
3542 if (feed
->readonly
) {
3543 http_log("Unable to delete feed file '%s' as it is marked readonly\n",
3544 feed
->feed_filename
);
3547 unlink(feed
->feed_filename
);
3550 if (!url_exist(feed
->feed_filename
)) {
3551 AVFormatContext s1
= {0}, *s
= &s1
;
3553 if (feed
->readonly
) {
3554 http_log("Unable to create feed file '%s' as it is marked readonly\n",
3555 feed
->feed_filename
);
3559 /* only write the header of the ffm file */
3560 if (url_fopen(&s
->pb
, feed
->feed_filename
, URL_WRONLY
) < 0) {
3561 http_log("Could not open output feed file '%s'\n",
3562 feed
->feed_filename
);
3565 s
->oformat
= feed
->fmt
;
3566 s
->nb_streams
= feed
->nb_streams
;
3567 for(i
=0;i
<s
->nb_streams
;i
++) {
3569 st
= feed
->streams
[i
];
3572 av_set_parameters(s
, NULL
);
3573 if (av_write_header(s
) < 0) {
3574 http_log("Container doesn't supports the required parameters\n");
3577 /* XXX: need better api */
3578 av_freep(&s
->priv_data
);
3581 /* get feed size and write index */
3582 fd
= open(feed
->feed_filename
, O_RDONLY
);
3584 http_log("Could not open output feed file '%s'\n",
3585 feed
->feed_filename
);
3589 feed
->feed_write_index
= FFMAX(ffm_read_write_index(fd
), FFM_PACKET_SIZE
);
3590 feed
->feed_size
= lseek(fd
, 0, SEEK_END
);
3591 /* ensure that we do not wrap before the end of file */
3592 if (feed
->feed_max_size
&& feed
->feed_max_size
< feed
->feed_size
)
3593 feed
->feed_max_size
= feed
->feed_size
;
3599 /* compute the bandwidth used by each stream */
3600 static void compute_bandwidth(void)
3606 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3608 for(i
=0;i
<stream
->nb_streams
;i
++) {
3609 AVStream
*st
= stream
->streams
[i
];
3610 switch(st
->codec
->codec_type
) {
3611 case CODEC_TYPE_AUDIO
:
3612 case CODEC_TYPE_VIDEO
:
3613 bandwidth
+= st
->codec
->bit_rate
;
3619 stream
->bandwidth
= (bandwidth
+ 999) / 1000;
3623 static void get_arg(char *buf
, int buf_size
, const char **pp
)
3630 while (isspace(*p
)) p
++;
3633 if (*p
== '\"' || *p
== '\'')
3645 if ((q
- buf
) < buf_size
- 1)
3650 if (quote
&& *p
== quote
)
3655 /* add a codec and set the default parameters */
3656 static void add_codec(FFStream
*stream
, AVCodecContext
*av
)
3660 /* compute default parameters */
3661 switch(av
->codec_type
) {
3662 case CODEC_TYPE_AUDIO
:
3663 if (av
->bit_rate
== 0)
3664 av
->bit_rate
= 64000;
3665 if (av
->sample_rate
== 0)
3666 av
->sample_rate
= 22050;
3667 if (av
->channels
== 0)
3670 case CODEC_TYPE_VIDEO
:
3671 if (av
->bit_rate
== 0)
3672 av
->bit_rate
= 64000;
3673 if (av
->time_base
.num
== 0){
3674 av
->time_base
.den
= 5;
3675 av
->time_base
.num
= 1;
3677 if (av
->width
== 0 || av
->height
== 0) {
3681 /* Bitrate tolerance is less for streaming */
3682 if (av
->bit_rate_tolerance
== 0)
3683 av
->bit_rate_tolerance
= FFMAX(av
->bit_rate
/ 4,
3684 (int64_t)av
->bit_rate
*av
->time_base
.num
/av
->time_base
.den
);
3689 if (av
->max_qdiff
== 0)
3691 av
->qcompress
= 0.5;
3694 if (!av
->nsse_weight
)
3695 av
->nsse_weight
= 8;
3697 av
->frame_skip_cmp
= FF_CMP_DCTMAX
;
3698 av
->me_method
= ME_EPZS
;
3699 av
->rc_buffer_aggressivity
= 1.0;
3702 av
->rc_eq
= "tex^qComp";
3703 if (!av
->i_quant_factor
)
3704 av
->i_quant_factor
= -0.8;
3705 if (!av
->b_quant_factor
)
3706 av
->b_quant_factor
= 1.25;
3707 if (!av
->b_quant_offset
)
3708 av
->b_quant_offset
= 1.25;
3709 if (!av
->rc_max_rate
)
3710 av
->rc_max_rate
= av
->bit_rate
* 2;
3712 if (av
->rc_max_rate
&& !av
->rc_buffer_size
) {
3713 av
->rc_buffer_size
= av
->rc_max_rate
;
3722 st
= av_mallocz(sizeof(AVStream
));
3725 st
->codec
= avcodec_alloc_context();
3726 stream
->streams
[stream
->nb_streams
++] = st
;
3727 memcpy(st
->codec
, av
, sizeof(AVCodecContext
));
3730 static enum CodecID
opt_audio_codec(const char *arg
)
3732 AVCodec
*p
= avcodec_find_encoder_by_name(arg
);
3734 if (p
== NULL
|| p
->type
!= CODEC_TYPE_AUDIO
)
3735 return CODEC_ID_NONE
;
3740 static enum CodecID
opt_video_codec(const char *arg
)
3742 AVCodec
*p
= avcodec_find_encoder_by_name(arg
);
3744 if (p
== NULL
|| p
->type
!= CODEC_TYPE_VIDEO
)
3745 return CODEC_ID_NONE
;
3750 /* simplistic plugin support */
3753 static void load_module(const char *filename
)
3756 void (*init_func
)(void);
3757 dll
= dlopen(filename
, RTLD_NOW
);
3759 fprintf(stderr
, "Could not load module '%s' - %s\n",
3760 filename
, dlerror());
3764 init_func
= dlsym(dll
, "ffserver_module_init");
3767 "%s: init function 'ffserver_module_init()' not found\n",
3776 static int ffserver_opt_default(const char *opt
, const char *arg
,
3777 AVCodecContext
*avctx
, int type
)
3780 const AVOption
*o
= av_find_opt(avctx
, opt
, NULL
, type
, type
);
3782 ret
= av_set_string3(avctx
, opt
, arg
, 1, NULL
);
3786 static int parse_ffconfig(const char *filename
)
3793 int val
, errors
, line_num
;
3794 FFStream
**last_stream
, *stream
, *redirect
;
3795 FFStream
**last_feed
, *feed
, *s
;
3796 AVCodecContext audio_enc
, video_enc
;
3797 enum CodecID audio_id
, video_id
;
3799 f
= fopen(filename
, "r");
3807 first_stream
= NULL
;
3808 last_stream
= &first_stream
;
3810 last_feed
= &first_feed
;
3814 audio_id
= CODEC_ID_NONE
;
3815 video_id
= CODEC_ID_NONE
;
3817 if (fgets(line
, sizeof(line
), f
) == NULL
)
3823 if (*p
== '\0' || *p
== '#')
3826 get_arg(cmd
, sizeof(cmd
), &p
);
3828 if (!strcasecmp(cmd
, "Port")) {
3829 get_arg(arg
, sizeof(arg
), &p
);
3831 if (val
< 1 || val
> 65536) {
3832 fprintf(stderr
, "%s:%d: Invalid port: %s\n",
3833 filename
, line_num
, arg
);
3836 my_http_addr
.sin_port
= htons(val
);
3837 } else if (!strcasecmp(cmd
, "BindAddress")) {
3838 get_arg(arg
, sizeof(arg
), &p
);
3839 if (resolve_host(&my_http_addr
.sin_addr
, arg
) != 0) {
3840 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
3841 filename
, line_num
, arg
);
3844 } else if (!strcasecmp(cmd
, "NoDaemon")) {
3845 ffserver_daemon
= 0;
3846 } else if (!strcasecmp(cmd
, "RTSPPort")) {
3847 get_arg(arg
, sizeof(arg
), &p
);
3849 if (val
< 1 || val
> 65536) {
3850 fprintf(stderr
, "%s:%d: Invalid port: %s\n",
3851 filename
, line_num
, arg
);
3854 my_rtsp_addr
.sin_port
= htons(atoi(arg
));
3855 } else if (!strcasecmp(cmd
, "RTSPBindAddress")) {
3856 get_arg(arg
, sizeof(arg
), &p
);
3857 if (resolve_host(&my_rtsp_addr
.sin_addr
, arg
) != 0) {
3858 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
3859 filename
, line_num
, arg
);
3862 } else if (!strcasecmp(cmd
, "MaxHTTPConnections")) {
3863 get_arg(arg
, sizeof(arg
), &p
);
3865 if (val
< 1 || val
> 65536) {
3866 fprintf(stderr
, "%s:%d: Invalid MaxHTTPConnections: %s\n",
3867 filename
, line_num
, arg
);
3870 nb_max_http_connections
= val
;
3871 } else if (!strcasecmp(cmd
, "MaxClients")) {
3872 get_arg(arg
, sizeof(arg
), &p
);
3874 if (val
< 1 || val
> nb_max_http_connections
) {
3875 fprintf(stderr
, "%s:%d: Invalid MaxClients: %s\n",
3876 filename
, line_num
, arg
);
3879 nb_max_connections
= val
;
3881 } else if (!strcasecmp(cmd
, "MaxBandwidth")) {
3883 get_arg(arg
, sizeof(arg
), &p
);
3885 if (llval
< 10 || llval
> 10000000) {
3886 fprintf(stderr
, "%s:%d: Invalid MaxBandwidth: %s\n",
3887 filename
, line_num
, arg
);
3890 max_bandwidth
= llval
;
3891 } else if (!strcasecmp(cmd
, "CustomLog")) {
3892 if (!ffserver_debug
)
3893 get_arg(logfilename
, sizeof(logfilename
), &p
);
3894 } else if (!strcasecmp(cmd
, "<Feed")) {
3895 /*********************************************/
3896 /* Feed related options */
3898 if (stream
|| feed
) {
3899 fprintf(stderr
, "%s:%d: Already in a tag\n",
3900 filename
, line_num
);
3902 feed
= av_mallocz(sizeof(FFStream
));
3903 get_arg(feed
->filename
, sizeof(feed
->filename
), &p
);
3904 q
= strrchr(feed
->filename
, '>');
3908 for (s
= first_feed
; s
; s
= s
->next
) {
3909 if (!strcmp(feed
->filename
, s
->filename
)) {
3910 fprintf(stderr
, "%s:%d: Feed '%s' already registered\n",
3911 filename
, line_num
, s
->filename
);
3916 feed
->fmt
= guess_format("ffm", NULL
, NULL
);
3917 /* defaut feed file */
3918 snprintf(feed
->feed_filename
, sizeof(feed
->feed_filename
),
3919 "/tmp/%s.ffm", feed
->filename
);
3920 feed
->feed_max_size
= 5 * 1024 * 1024;
3922 feed
->feed
= feed
; /* self feeding :-) */
3924 /* add in stream list */
3925 *last_stream
= feed
;
3926 last_stream
= &feed
->next
;
3927 /* add in feed list */
3929 last_feed
= &feed
->next_feed
;
3931 } else if (!strcasecmp(cmd
, "Launch")) {
3935 feed
->child_argv
= av_mallocz(64 * sizeof(char *));
3937 for (i
= 0; i
< 62; i
++) {
3938 get_arg(arg
, sizeof(arg
), &p
);
3942 feed
->child_argv
[i
] = av_strdup(arg
);
3945 feed
->child_argv
[i
] = av_malloc(30 + strlen(feed
->filename
));
3947 snprintf(feed
->child_argv
[i
], 30+strlen(feed
->filename
),
3949 (my_http_addr
.sin_addr
.s_addr
== INADDR_ANY
) ? "127.0.0.1" :
3950 inet_ntoa(my_http_addr
.sin_addr
),
3951 ntohs(my_http_addr
.sin_port
), feed
->filename
);
3953 } else if (!strcasecmp(cmd
, "ReadOnlyFile")) {
3955 get_arg(feed
->feed_filename
, sizeof(feed
->feed_filename
), &p
);
3957 } else if (stream
) {
3958 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3960 } else if (!strcasecmp(cmd
, "File")) {
3962 get_arg(feed
->feed_filename
, sizeof(feed
->feed_filename
), &p
);
3964 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3965 } else if (!strcasecmp(cmd
, "Truncate")) {
3967 get_arg(arg
, sizeof(arg
), &p
);
3968 feed
->truncate
= strtod(arg
, NULL
);
3970 } else if (!strcasecmp(cmd
, "FileMaxSize")) {
3975 get_arg(arg
, sizeof(arg
), &p
);
3977 fsize
= strtod(p1
, &p1
);
3978 switch(toupper(*p1
)) {
3983 fsize
*= 1024 * 1024;
3986 fsize
*= 1024 * 1024 * 1024;
3989 feed
->feed_max_size
= (int64_t)fsize
;
3990 if (feed
->feed_max_size
< FFM_PACKET_SIZE
*4) {
3991 fprintf(stderr
, "%s:%d: Feed max file size is too small, "
3992 "must be at least %d\n", filename
, line_num
, FFM_PACKET_SIZE
*4);
3996 } else if (!strcasecmp(cmd
, "</Feed>")) {
3998 fprintf(stderr
, "%s:%d: No corresponding <Feed> for </Feed>\n",
3999 filename
, line_num
);
4003 } else if (!strcasecmp(cmd
, "<Stream")) {
4004 /*********************************************/
4005 /* Stream related options */
4007 if (stream
|| feed
) {
4008 fprintf(stderr
, "%s:%d: Already in a tag\n",
4009 filename
, line_num
);
4012 const AVClass
*class;
4013 stream
= av_mallocz(sizeof(FFStream
));
4014 get_arg(stream
->filename
, sizeof(stream
->filename
), &p
);
4015 q
= strrchr(stream
->filename
, '>');
4019 for (s
= first_stream
; s
; s
= s
->next
) {
4020 if (!strcmp(stream
->filename
, s
->filename
)) {
4021 fprintf(stderr
, "%s:%d: Stream '%s' already registered\n",
4022 filename
, line_num
, s
->filename
);
4027 stream
->fmt
= guess_stream_format(NULL
, stream
->filename
, NULL
);
4028 /* fetch avclass so AVOption works
4029 * FIXME try to use avcodec_get_context_defaults2
4030 * without changing defaults too much */
4031 avcodec_get_context_defaults(&video_enc
);
4032 class = video_enc
.av_class
;
4033 memset(&audio_enc
, 0, sizeof(AVCodecContext
));
4034 memset(&video_enc
, 0, sizeof(AVCodecContext
));
4035 audio_enc
.av_class
= class;
4036 video_enc
.av_class
= class;
4037 audio_id
= CODEC_ID_NONE
;
4038 video_id
= CODEC_ID_NONE
;
4040 audio_id
= stream
->fmt
->audio_codec
;
4041 video_id
= stream
->fmt
->video_codec
;
4044 *last_stream
= stream
;
4045 last_stream
= &stream
->next
;
4047 } else if (!strcasecmp(cmd
, "Feed")) {
4048 get_arg(arg
, sizeof(arg
), &p
);
4053 while (sfeed
!= NULL
) {
4054 if (!strcmp(sfeed
->filename
, arg
))
4056 sfeed
= sfeed
->next_feed
;
4059 fprintf(stderr
, "%s:%d: feed '%s' not defined\n",
4060 filename
, line_num
, arg
);
4062 stream
->feed
= sfeed
;
4064 } else if (!strcasecmp(cmd
, "Format")) {
4065 get_arg(arg
, sizeof(arg
), &p
);
4067 if (!strcmp(arg
, "status")) {
4068 stream
->stream_type
= STREAM_TYPE_STATUS
;
4071 stream
->stream_type
= STREAM_TYPE_LIVE
;
4072 /* jpeg cannot be used here, so use single frame jpeg */
4073 if (!strcmp(arg
, "jpeg"))
4074 strcpy(arg
, "mjpeg");
4075 stream
->fmt
= guess_stream_format(arg
, NULL
, NULL
);
4077 fprintf(stderr
, "%s:%d: Unknown Format: %s\n",
4078 filename
, line_num
, arg
);
4083 audio_id
= stream
->fmt
->audio_codec
;
4084 video_id
= stream
->fmt
->video_codec
;
4087 } else if (!strcasecmp(cmd
, "InputFormat")) {
4088 get_arg(arg
, sizeof(arg
), &p
);
4090 stream
->ifmt
= av_find_input_format(arg
);
4091 if (!stream
->ifmt
) {
4092 fprintf(stderr
, "%s:%d: Unknown input format: %s\n",
4093 filename
, line_num
, arg
);
4096 } else if (!strcasecmp(cmd
, "FaviconURL")) {
4097 if (stream
&& stream
->stream_type
== STREAM_TYPE_STATUS
) {
4098 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
4100 fprintf(stderr
, "%s:%d: FaviconURL only permitted for status streams\n",
4101 filename
, line_num
);
4104 } else if (!strcasecmp(cmd
, "Author")) {
4106 get_arg(stream
->author
, sizeof(stream
->author
), &p
);
4107 } else if (!strcasecmp(cmd
, "Comment")) {
4109 get_arg(stream
->comment
, sizeof(stream
->comment
), &p
);
4110 } else if (!strcasecmp(cmd
, "Copyright")) {
4112 get_arg(stream
->copyright
, sizeof(stream
->copyright
), &p
);
4113 } else if (!strcasecmp(cmd
, "Title")) {
4115 get_arg(stream
->title
, sizeof(stream
->title
), &p
);
4116 } else if (!strcasecmp(cmd
, "Preroll")) {
4117 get_arg(arg
, sizeof(arg
), &p
);
4119 stream
->prebuffer
= atof(arg
) * 1000;
4120 } else if (!strcasecmp(cmd
, "StartSendOnKey")) {
4122 stream
->send_on_key
= 1;
4123 } else if (!strcasecmp(cmd
, "AudioCodec")) {
4124 get_arg(arg
, sizeof(arg
), &p
);
4125 audio_id
= opt_audio_codec(arg
);
4126 if (audio_id
== CODEC_ID_NONE
) {
4127 fprintf(stderr
, "%s:%d: Unknown AudioCodec: %s\n",
4128 filename
, line_num
, arg
);
4131 } else if (!strcasecmp(cmd
, "VideoCodec")) {
4132 get_arg(arg
, sizeof(arg
), &p
);
4133 video_id
= opt_video_codec(arg
);
4134 if (video_id
== CODEC_ID_NONE
) {
4135 fprintf(stderr
, "%s:%d: Unknown VideoCodec: %s\n",
4136 filename
, line_num
, arg
);
4139 } else if (!strcasecmp(cmd
, "MaxTime")) {
4140 get_arg(arg
, sizeof(arg
), &p
);
4142 stream
->max_time
= atof(arg
) * 1000;
4143 } else if (!strcasecmp(cmd
, "AudioBitRate")) {
4144 get_arg(arg
, sizeof(arg
), &p
);
4146 audio_enc
.bit_rate
= atoi(arg
) * 1000;
4147 } else if (!strcasecmp(cmd
, "AudioChannels")) {
4148 get_arg(arg
, sizeof(arg
), &p
);
4150 audio_enc
.channels
= atoi(arg
);
4151 } else if (!strcasecmp(cmd
, "AudioSampleRate")) {
4152 get_arg(arg
, sizeof(arg
), &p
);
4154 audio_enc
.sample_rate
= atoi(arg
);
4155 } else if (!strcasecmp(cmd
, "AudioQuality")) {
4156 get_arg(arg
, sizeof(arg
), &p
);
4158 // audio_enc.quality = atof(arg) * 1000;
4160 } else if (!strcasecmp(cmd
, "VideoBitRateRange")) {
4162 int minrate
, maxrate
;
4164 get_arg(arg
, sizeof(arg
), &p
);
4166 if (sscanf(arg
, "%d-%d", &minrate
, &maxrate
) == 2) {
4167 video_enc
.rc_min_rate
= minrate
* 1000;
4168 video_enc
.rc_max_rate
= maxrate
* 1000;
4170 fprintf(stderr
, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4171 filename
, line_num
, arg
);
4175 } else if (!strcasecmp(cmd
, "Debug")) {
4177 get_arg(arg
, sizeof(arg
), &p
);
4178 video_enc
.debug
= strtol(arg
,0,0);
4180 } else if (!strcasecmp(cmd
, "Strict")) {
4182 get_arg(arg
, sizeof(arg
), &p
);
4183 video_enc
.strict_std_compliance
= atoi(arg
);
4185 } else if (!strcasecmp(cmd
, "VideoBufferSize")) {
4187 get_arg(arg
, sizeof(arg
), &p
);
4188 video_enc
.rc_buffer_size
= atoi(arg
) * 8*1024;
4190 } else if (!strcasecmp(cmd
, "VideoBitRateTolerance")) {
4192 get_arg(arg
, sizeof(arg
), &p
);
4193 video_enc
.bit_rate_tolerance
= atoi(arg
) * 1000;
4195 } else if (!strcasecmp(cmd
, "VideoBitRate")) {
4196 get_arg(arg
, sizeof(arg
), &p
);
4198 video_enc
.bit_rate
= atoi(arg
) * 1000;
4200 } else if (!strcasecmp(cmd
, "VideoSize")) {
4201 get_arg(arg
, sizeof(arg
), &p
);
4203 av_parse_video_frame_size(&video_enc
.width
, &video_enc
.height
, arg
);
4204 if ((video_enc
.width
% 16) != 0 ||
4205 (video_enc
.height
% 16) != 0) {
4206 fprintf(stderr
, "%s:%d: Image size must be a multiple of 16\n",
4207 filename
, line_num
);
4211 } else if (!strcasecmp(cmd
, "VideoFrameRate")) {
4212 get_arg(arg
, sizeof(arg
), &p
);
4214 AVRational frame_rate
;
4215 if (av_parse_video_frame_rate(&frame_rate
, arg
) < 0) {
4216 fprintf(stderr
, "Incorrect frame rate\n");
4219 video_enc
.time_base
.num
= frame_rate
.den
;
4220 video_enc
.time_base
.den
= frame_rate
.num
;
4223 } else if (!strcasecmp(cmd
, "VideoGopSize")) {
4224 get_arg(arg
, sizeof(arg
), &p
);
4226 video_enc
.gop_size
= atoi(arg
);
4227 } else if (!strcasecmp(cmd
, "VideoIntraOnly")) {
4229 video_enc
.gop_size
= 1;
4230 } else if (!strcasecmp(cmd
, "VideoHighQuality")) {
4232 video_enc
.mb_decision
= FF_MB_DECISION_BITS
;
4233 } else if (!strcasecmp(cmd
, "Video4MotionVector")) {
4235 video_enc
.mb_decision
= FF_MB_DECISION_BITS
; //FIXME remove
4236 video_enc
.flags
|= CODEC_FLAG_4MV
;
4238 } else if (!strcasecmp(cmd
, "AVOptionVideo") ||
4239 !strcasecmp(cmd
, "AVOptionAudio")) {
4241 AVCodecContext
*avctx
;
4243 get_arg(arg
, sizeof(arg
), &p
);
4244 get_arg(arg2
, sizeof(arg2
), &p
);
4245 if (!strcasecmp(cmd
, "AVOptionVideo")) {
4247 type
= AV_OPT_FLAG_VIDEO_PARAM
;
4250 type
= AV_OPT_FLAG_AUDIO_PARAM
;
4252 if (ffserver_opt_default(arg
, arg2
, avctx
, type
|AV_OPT_FLAG_ENCODING_PARAM
)) {
4253 fprintf(stderr
, "AVOption error: %s %s\n", arg
, arg2
);
4256 } else if (!strcasecmp(cmd
, "VideoTag")) {
4257 get_arg(arg
, sizeof(arg
), &p
);
4258 if ((strlen(arg
) == 4) && stream
)
4259 video_enc
.codec_tag
= AV_RL32(arg
);
4260 } else if (!strcasecmp(cmd
, "BitExact")) {
4262 video_enc
.flags
|= CODEC_FLAG_BITEXACT
;
4263 } else if (!strcasecmp(cmd
, "DctFastint")) {
4265 video_enc
.dct_algo
= FF_DCT_FASTINT
;
4266 } else if (!strcasecmp(cmd
, "IdctSimple")) {
4268 video_enc
.idct_algo
= FF_IDCT_SIMPLE
;
4269 } else if (!strcasecmp(cmd
, "Qscale")) {
4270 get_arg(arg
, sizeof(arg
), &p
);
4272 video_enc
.flags
|= CODEC_FLAG_QSCALE
;
4273 video_enc
.global_quality
= FF_QP2LAMBDA
* atoi(arg
);
4275 } else if (!strcasecmp(cmd
, "VideoQDiff")) {
4276 get_arg(arg
, sizeof(arg
), &p
);
4278 video_enc
.max_qdiff
= atoi(arg
);
4279 if (video_enc
.max_qdiff
< 1 || video_enc
.max_qdiff
> 31) {
4280 fprintf(stderr
, "%s:%d: VideoQDiff out of range\n",
4281 filename
, line_num
);
4285 } else if (!strcasecmp(cmd
, "VideoQMax")) {
4286 get_arg(arg
, sizeof(arg
), &p
);
4288 video_enc
.qmax
= atoi(arg
);
4289 if (video_enc
.qmax
< 1 || video_enc
.qmax
> 31) {
4290 fprintf(stderr
, "%s:%d: VideoQMax out of range\n",
4291 filename
, line_num
);
4295 } else if (!strcasecmp(cmd
, "VideoQMin")) {
4296 get_arg(arg
, sizeof(arg
), &p
);
4298 video_enc
.qmin
= atoi(arg
);
4299 if (video_enc
.qmin
< 1 || video_enc
.qmin
> 31) {
4300 fprintf(stderr
, "%s:%d: VideoQMin out of range\n",
4301 filename
, line_num
);
4305 } else if (!strcasecmp(cmd
, "LumaElim")) {
4306 get_arg(arg
, sizeof(arg
), &p
);
4308 video_enc
.luma_elim_threshold
= atoi(arg
);
4309 } else if (!strcasecmp(cmd
, "ChromaElim")) {
4310 get_arg(arg
, sizeof(arg
), &p
);
4312 video_enc
.chroma_elim_threshold
= atoi(arg
);
4313 } else if (!strcasecmp(cmd
, "LumiMask")) {
4314 get_arg(arg
, sizeof(arg
), &p
);
4316 video_enc
.lumi_masking
= atof(arg
);
4317 } else if (!strcasecmp(cmd
, "DarkMask")) {
4318 get_arg(arg
, sizeof(arg
), &p
);
4320 video_enc
.dark_masking
= atof(arg
);
4321 } else if (!strcasecmp(cmd
, "NoVideo")) {
4322 video_id
= CODEC_ID_NONE
;
4323 } else if (!strcasecmp(cmd
, "NoAudio")) {
4324 audio_id
= CODEC_ID_NONE
;
4325 } else if (!strcasecmp(cmd
, "ACL")) {
4328 get_arg(arg
, sizeof(arg
), &p
);
4329 if (strcasecmp(arg
, "allow") == 0)
4330 acl
.action
= IP_ALLOW
;
4331 else if (strcasecmp(arg
, "deny") == 0)
4332 acl
.action
= IP_DENY
;
4334 fprintf(stderr
, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4335 filename
, line_num
, arg
);
4339 get_arg(arg
, sizeof(arg
), &p
);
4341 if (resolve_host(&acl
.first
, arg
) != 0) {
4342 fprintf(stderr
, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4343 filename
, line_num
, arg
);
4346 acl
.last
= acl
.first
;
4348 get_arg(arg
, sizeof(arg
), &p
);
4351 if (resolve_host(&acl
.last
, arg
) != 0) {
4352 fprintf(stderr
, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4353 filename
, line_num
, arg
);
4359 IPAddressACL
*nacl
= av_mallocz(sizeof(*nacl
));
4360 IPAddressACL
**naclp
= 0;
4366 naclp
= &stream
->acl
;
4370 fprintf(stderr
, "%s:%d: ACL found not in <stream> or <feed>\n",
4371 filename
, line_num
);
4377 naclp
= &(*naclp
)->next
;
4382 } else if (!strcasecmp(cmd
, "RTSPOption")) {
4383 get_arg(arg
, sizeof(arg
), &p
);
4385 av_freep(&stream
->rtsp_option
);
4386 stream
->rtsp_option
= av_strdup(arg
);
4388 } else if (!strcasecmp(cmd
, "MulticastAddress")) {
4389 get_arg(arg
, sizeof(arg
), &p
);
4391 if (resolve_host(&stream
->multicast_ip
, arg
) != 0) {
4392 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
4393 filename
, line_num
, arg
);
4396 stream
->is_multicast
= 1;
4397 stream
->loop
= 1; /* default is looping */
4399 } else if (!strcasecmp(cmd
, "MulticastPort")) {
4400 get_arg(arg
, sizeof(arg
), &p
);
4402 stream
->multicast_port
= atoi(arg
);
4403 } else if (!strcasecmp(cmd
, "MulticastTTL")) {
4404 get_arg(arg
, sizeof(arg
), &p
);
4406 stream
->multicast_ttl
= atoi(arg
);
4407 } else if (!strcasecmp(cmd
, "NoLoop")) {
4410 } else if (!strcasecmp(cmd
, "</Stream>")) {
4412 fprintf(stderr
, "%s:%d: No corresponding <Stream> for </Stream>\n",
4413 filename
, line_num
);
4416 if (stream
->feed
&& stream
->fmt
&& strcmp(stream
->fmt
->name
, "ffm") != 0) {
4417 if (audio_id
!= CODEC_ID_NONE
) {
4418 audio_enc
.codec_type
= CODEC_TYPE_AUDIO
;
4419 audio_enc
.codec_id
= audio_id
;
4420 add_codec(stream
, &audio_enc
);
4422 if (video_id
!= CODEC_ID_NONE
) {
4423 video_enc
.codec_type
= CODEC_TYPE_VIDEO
;
4424 video_enc
.codec_id
= video_id
;
4425 add_codec(stream
, &video_enc
);
4430 } else if (!strcasecmp(cmd
, "<Redirect")) {
4431 /*********************************************/
4433 if (stream
|| feed
|| redirect
) {
4434 fprintf(stderr
, "%s:%d: Already in a tag\n",
4435 filename
, line_num
);
4438 redirect
= av_mallocz(sizeof(FFStream
));
4439 *last_stream
= redirect
;
4440 last_stream
= &redirect
->next
;
4442 get_arg(redirect
->filename
, sizeof(redirect
->filename
), &p
);
4443 q
= strrchr(redirect
->filename
, '>');
4446 redirect
->stream_type
= STREAM_TYPE_REDIRECT
;
4448 } else if (!strcasecmp(cmd
, "URL")) {
4450 get_arg(redirect
->feed_filename
, sizeof(redirect
->feed_filename
), &p
);
4451 } else if (!strcasecmp(cmd
, "</Redirect>")) {
4453 fprintf(stderr
, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4454 filename
, line_num
);
4457 if (!redirect
->feed_filename
[0]) {
4458 fprintf(stderr
, "%s:%d: No URL found for <Redirect>\n",
4459 filename
, line_num
);
4464 } else if (!strcasecmp(cmd
, "LoadModule")) {
4465 get_arg(arg
, sizeof(arg
), &p
);
4469 fprintf(stderr
, "%s:%d: Module support not compiled into this version: '%s'\n",
4470 filename
, line_num
, arg
);
4474 fprintf(stderr
, "%s:%d: Incorrect keyword: '%s'\n",
4475 filename
, line_num
, cmd
);
4486 static void handle_child_exit(int sig
)
4491 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
4494 for (feed
= first_feed
; feed
; feed
= feed
->next
) {
4495 if (feed
->pid
== pid
) {
4496 int uptime
= time(0) - feed
->pid_start
;
4499 fprintf(stderr
, "%s: Pid %d exited with status %d after %d seconds\n", feed
->filename
, pid
, status
, uptime
);
4502 /* Turn off any more restarts */
4503 feed
->child_argv
= 0;
4508 need_to_start_children
= 1;
4511 static void opt_debug(void)
4514 ffserver_daemon
= 0;
4515 logfilename
[0] = '-';
4518 static void opt_show_help(void)
4520 printf("usage: ffserver [options]\n"
4521 "Hyper fast multi format Audio/Video streaming server\n");
4523 show_help_options(options
, "Main options:\n", 0, 0);
4526 static const OptionDef options
[] = {
4527 { "h", OPT_EXIT
, {(void*)opt_show_help
}, "show help" },
4528 { "version", OPT_EXIT
, {(void*)show_version
}, "show version" },
4529 { "L", OPT_EXIT
, {(void*)show_license
}, "show license" },
4530 { "formats", OPT_EXIT
, {(void*)show_formats
}, "show available formats, codecs, protocols, ..." },
4531 { "n", OPT_BOOL
, {(void *)&no_launch
}, "enable no-launch mode" },
4532 { "d", 0, {(void*)opt_debug
}, "enable debug mode" },
4533 { "f", HAS_ARG
| OPT_STRING
, {(void*)&config_filename
}, "use configfile instead of /etc/ffserver.conf", "configfile" },
4537 int main(int argc
, char **argv
)
4539 struct sigaction sigact
;
4545 config_filename
= "/etc/ffserver.conf";
4547 my_program_name
= argv
[0];
4548 my_program_dir
= getcwd(0, 0);
4549 ffserver_daemon
= 1;
4551 parse_options(argc
, argv
, options
, NULL
);
4553 unsetenv("http_proxy"); /* Kill the http_proxy */
4555 av_lfg_init(&random_state
, ff_random_get_seed());
4557 memset(&sigact
, 0, sizeof(sigact
));
4558 sigact
.sa_handler
= handle_child_exit
;
4559 sigact
.sa_flags
= SA_NOCLDSTOP
| SA_RESTART
;
4560 sigaction(SIGCHLD
, &sigact
, 0);
4562 if (parse_ffconfig(config_filename
) < 0) {
4563 fprintf(stderr
, "Incorrect config file - exiting.\n");
4567 /* open log file if needed */
4568 if (logfilename
[0] != '\0') {
4569 if (!strcmp(logfilename
, "-"))
4572 logfile
= fopen(logfilename
, "a");
4573 av_log_set_callback(http_av_log
);
4576 build_file_streams();
4578 build_feed_streams();
4580 compute_bandwidth();
4582 /* put the process in background and detach it from its TTY */
4583 if (ffserver_daemon
) {
4590 } else if (pid
> 0) {
4597 open("/dev/null", O_RDWR
);
4598 if (strcmp(logfilename
, "-") != 0) {
4608 signal(SIGPIPE
, SIG_IGN
);
4610 if (ffserver_daemon
)
4613 if (http_server() < 0) {
4614 http_log("Could not start server\n");