2 * Multiple format streaming server
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef HAVE_CLOSESOCKET
24 #define closesocket close
28 #include "libavutil/random.h"
29 #include "libavutil/avstring.h"
30 #include "libavformat/avformat.h"
31 #include "libavformat/network.h"
32 #include "libavformat/os_support.h"
33 #include "libavformat/rtp.h"
34 #include "libavformat/rtsp.h"
39 #include <sys/ioctl.h>
45 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
59 const char program_name
[] = "FFserver";
60 static const int program_birth_year
= 2000;
62 static const OptionDef options
[];
64 /* maximum number of simultaneous HTTP connections */
65 #define HTTP_MAX_CONNECTIONS 2000
68 HTTPSTATE_WAIT_REQUEST
,
69 HTTPSTATE_SEND_HEADER
,
70 HTTPSTATE_SEND_DATA_HEADER
,
71 HTTPSTATE_SEND_DATA
, /* sending TCP or UDP data */
72 HTTPSTATE_SEND_DATA_TRAILER
,
73 HTTPSTATE_RECEIVE_DATA
,
74 HTTPSTATE_WAIT_FEED
, /* wait for data from the feed */
77 RTSPSTATE_WAIT_REQUEST
,
79 RTSPSTATE_SEND_PACKET
,
82 const char *http_state
[] = {
98 #define IOBUFFER_INIT_SIZE 8192
100 /* timeouts are in ms */
101 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
102 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
104 #define SYNC_TIMEOUT (10 * 1000)
107 int64_t count1
, count2
;
108 int64_t time1
, time2
;
111 /* context associated with one connection */
112 typedef struct HTTPContext
{
113 enum HTTPState state
;
114 int fd
; /* socket file descriptor */
115 struct sockaddr_in from_addr
; /* origin */
116 struct pollfd
*poll_entry
; /* used when polling */
118 uint8_t *buffer_ptr
, *buffer_end
;
121 struct HTTPContext
*next
;
122 int got_key_frame
; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
126 /* input format handling */
127 AVFormatContext
*fmt_in
;
128 int64_t start_time
; /* In milliseconds - this wraps fairly often */
129 int64_t first_pts
; /* initial pts value */
130 int64_t cur_pts
; /* current pts value from the stream in us */
131 int64_t cur_frame_duration
; /* duration of the current frame in us */
132 int cur_frame_bytes
; /* output frame size, needed to compute
133 the time at which we send each
135 int pts_stream_index
; /* stream we choose as clock reference */
136 int64_t cur_clock
; /* current clock reference value in us */
137 /* output format handling */
138 struct FFStream
*stream
;
139 /* -1 is invalid stream */
140 int feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
141 int switch_feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
143 AVFormatContext fmt_ctx
; /* instance of FFStream for one user */
144 int last_packet_sent
; /* true if last data packet was sent */
146 DataRateData datarate
;
153 int is_packetized
; /* if true, the stream is packetized */
154 int packet_stream_index
; /* current stream for output in state machine */
156 /* RTSP state specific */
157 uint8_t *pb_buffer
; /* XXX: use that in all the code */
159 int seq
; /* RTSP sequence number */
161 /* RTP state specific */
162 enum RTSPProtocol rtp_protocol
;
163 char session_id
[32]; /* session id */
164 AVFormatContext
*rtp_ctx
[MAX_STREAMS
];
166 /* RTP/UDP specific */
167 URLContext
*rtp_handles
[MAX_STREAMS
];
169 /* RTP/TCP specific */
170 struct HTTPContext
*rtsp_c
;
171 uint8_t *packet_buffer
, *packet_buffer_ptr
, *packet_buffer_end
;
174 /* each generated stream is described here */
178 STREAM_TYPE_REDIRECT
,
181 enum IPAddressAction
{
186 typedef struct IPAddressACL
{
187 struct IPAddressACL
*next
;
188 enum IPAddressAction action
;
189 /* These are in host order */
190 struct in_addr first
;
194 /* description of each stream of the ffserver.conf file */
195 typedef struct FFStream
{
196 enum StreamType stream_type
;
197 char filename
[1024]; /* stream filename */
198 struct FFStream
*feed
; /* feed we are using (can be null if
200 AVFormatParameters
*ap_in
; /* input parameters */
201 AVInputFormat
*ifmt
; /* if non NULL, force input format */
205 int prebuffer
; /* Number of millseconds early to start */
206 int64_t max_time
; /* Number of milliseconds to run */
208 AVStream
*streams
[MAX_STREAMS
];
209 int feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
210 char feed_filename
[1024]; /* file name of the feed storage, or
211 input file name for a stream */
216 pid_t pid
; /* Of ffmpeg process */
217 time_t pid_start
; /* Of ffmpeg process */
219 struct FFStream
*next
;
220 int bandwidth
; /* bandwidth, in kbits/s */
223 /* multicast specific */
225 struct in_addr multicast_ip
;
226 int multicast_port
; /* first port used for multicast */
228 int loop
; /* if true, send the stream in loops (only meaningful if file) */
231 int feed_opened
; /* true if someone is writing to the feed */
232 int is_feed
; /* true if it is a feed */
233 int readonly
; /* True if writing is prohibited to the file */
235 int64_t bytes_served
;
236 int64_t feed_max_size
; /* maximum storage size, zero means unlimited */
237 int64_t feed_write_index
; /* current write position in feed (it wraps around) */
238 int64_t feed_size
; /* current size of feed */
239 struct FFStream
*next_feed
;
242 typedef struct FeedData
{
243 long long data_count
;
244 float avg_frame_size
; /* frame size averaged over last frames with exponential mean */
247 static struct sockaddr_in my_http_addr
;
248 static struct sockaddr_in my_rtsp_addr
;
250 static char logfilename
[1024];
251 static HTTPContext
*first_http_ctx
;
252 static FFStream
*first_feed
; /* contains only feeds */
253 static FFStream
*first_stream
; /* contains all streams, including feeds */
255 static void new_connection(int server_fd
, int is_rtsp
);
256 static void close_connection(HTTPContext
*c
);
259 static int handle_connection(HTTPContext
*c
);
260 static int http_parse_request(HTTPContext
*c
);
261 static int http_send_data(HTTPContext
*c
);
262 static void compute_stats(HTTPContext
*c
);
263 static int open_input_stream(HTTPContext
*c
, const char *info
);
264 static int http_start_receive_data(HTTPContext
*c
);
265 static int http_receive_data(HTTPContext
*c
);
268 static int rtsp_parse_request(HTTPContext
*c
);
269 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
);
270 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
);
271 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
272 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
273 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
274 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
277 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
278 struct in_addr my_ip
);
281 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
282 FFStream
*stream
, const char *session_id
,
283 enum RTSPProtocol rtp_protocol
);
284 static int rtp_new_av_stream(HTTPContext
*c
,
285 int stream_index
, struct sockaddr_in
*dest_addr
,
286 HTTPContext
*rtsp_c
);
288 static const char *my_program_name
;
289 static const char *my_program_dir
;
291 static const char *config_filename
;
292 static int ffserver_debug
;
293 static int ffserver_daemon
;
294 static int no_launch
;
295 static int need_to_start_children
;
297 static int nb_max_connections
;
298 static int nb_connections
;
300 static int max_bandwidth
;
301 static int current_bandwidth
;
303 static int64_t cur_time
; // Making this global saves on passing it around everywhere
305 static AVRandomState random_state
;
307 static FILE *logfile
= NULL
;
309 static void __attribute__ ((format (printf
, 1, 2))) http_log(const char *fmt
, ...)
315 vfprintf(logfile
, fmt
, ap
);
321 static char *ctime1(char *buf2
)
329 p
= buf2
+ strlen(p
) - 1;
335 static void log_connection(HTTPContext
*c
)
342 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64
"\n",
343 inet_ntoa(c
->from_addr
.sin_addr
),
344 ctime1(buf2
), c
->method
, c
->url
,
345 c
->protocol
, (c
->http_error
? c
->http_error
: 200), c
->data_count
);
348 static void update_datarate(DataRateData
*drd
, int64_t count
)
350 if (!drd
->time1
&& !drd
->count1
) {
351 drd
->time1
= drd
->time2
= cur_time
;
352 drd
->count1
= drd
->count2
= count
;
353 } else if (cur_time
- drd
->time2
> 5000) {
354 drd
->time1
= drd
->time2
;
355 drd
->count1
= drd
->count2
;
356 drd
->time2
= cur_time
;
361 /* In bytes per second */
362 static int compute_datarate(DataRateData
*drd
, int64_t count
)
364 if (cur_time
== drd
->time1
)
367 return ((count
- drd
->count1
) * 1000) / (cur_time
- drd
->time1
);
371 static void start_children(FFStream
*feed
)
376 for (; feed
; feed
= feed
->next
) {
377 if (feed
->child_argv
&& !feed
->pid
) {
378 feed
->pid_start
= time(0);
383 fprintf(stderr
, "Unable to create children\n");
392 for (i
= 3; i
< 256; i
++)
395 if (!ffserver_debug
) {
396 i
= open("/dev/null", O_RDWR
);
405 av_strlcpy(pathname
, my_program_name
, sizeof(pathname
));
407 slash
= strrchr(pathname
, '/');
412 strcpy(slash
, "ffmpeg");
414 /* This is needed to make relative pathnames work */
415 chdir(my_program_dir
);
417 signal(SIGPIPE
, SIG_DFL
);
419 execvp(pathname
, feed
->child_argv
);
427 /* open a listening socket */
428 static int socket_open_listen(struct sockaddr_in
*my_addr
)
432 server_fd
= socket(AF_INET
,SOCK_STREAM
,0);
439 setsockopt(server_fd
, SOL_SOCKET
, SO_REUSEADDR
, &tmp
, sizeof(tmp
));
441 if (bind (server_fd
, (struct sockaddr
*) my_addr
, sizeof (*my_addr
)) < 0) {
443 snprintf(bindmsg
, sizeof(bindmsg
), "bind(port %d)", ntohs(my_addr
->sin_port
));
445 closesocket(server_fd
);
449 if (listen (server_fd
, 5) < 0) {
451 closesocket(server_fd
);
454 ff_socket_nonblock(server_fd
, 1);
459 /* start all multicast streams */
460 static void start_multicast(void)
465 struct sockaddr_in dest_addr
;
466 int default_port
, stream_index
;
469 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
470 if (stream
->is_multicast
) {
471 /* open the RTP connection */
472 snprintf(session_id
, sizeof(session_id
), "%08x%08x",
473 av_random(&random_state
), av_random(&random_state
));
475 /* choose a port if none given */
476 if (stream
->multicast_port
== 0) {
477 stream
->multicast_port
= default_port
;
481 dest_addr
.sin_family
= AF_INET
;
482 dest_addr
.sin_addr
= stream
->multicast_ip
;
483 dest_addr
.sin_port
= htons(stream
->multicast_port
);
485 rtp_c
= rtp_new_connection(&dest_addr
, stream
, session_id
,
486 RTSP_PROTOCOL_RTP_UDP_MULTICAST
);
490 if (open_input_stream(rtp_c
, "") < 0) {
491 fprintf(stderr
, "Could not open input stream for stream '%s'\n",
496 /* open each RTP stream */
497 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
499 dest_addr
.sin_port
= htons(stream
->multicast_port
+
501 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, NULL
) < 0) {
502 fprintf(stderr
, "Could not open output stream '%s/streamid=%d'\n",
503 stream
->filename
, stream_index
);
508 /* change state to send data */
509 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
514 /* main loop of the http server */
515 static int http_server(void)
517 int server_fd
, ret
, rtsp_server_fd
, delay
, delay1
;
518 struct pollfd poll_table
[HTTP_MAX_CONNECTIONS
+ 2], *poll_entry
;
519 HTTPContext
*c
, *c_next
;
521 server_fd
= socket_open_listen(&my_http_addr
);
525 rtsp_server_fd
= socket_open_listen(&my_rtsp_addr
);
526 if (rtsp_server_fd
< 0)
529 http_log("ffserver started.\n");
531 start_children(first_feed
);
533 first_http_ctx
= NULL
;
539 poll_entry
= poll_table
;
540 poll_entry
->fd
= server_fd
;
541 poll_entry
->events
= POLLIN
;
544 poll_entry
->fd
= rtsp_server_fd
;
545 poll_entry
->events
= POLLIN
;
548 /* wait for events on each HTTP handle */
555 case HTTPSTATE_SEND_HEADER
:
556 case RTSPSTATE_SEND_REPLY
:
557 case RTSPSTATE_SEND_PACKET
:
558 c
->poll_entry
= poll_entry
;
560 poll_entry
->events
= POLLOUT
;
563 case HTTPSTATE_SEND_DATA_HEADER
:
564 case HTTPSTATE_SEND_DATA
:
565 case HTTPSTATE_SEND_DATA_TRAILER
:
566 if (!c
->is_packetized
) {
567 /* for TCP, we output as much as we can (may need to put a limit) */
568 c
->poll_entry
= poll_entry
;
570 poll_entry
->events
= POLLOUT
;
573 /* when ffserver is doing the timing, we work by
574 looking at which packet need to be sent every
576 delay1
= 10; /* one tick wait XXX: 10 ms assumed */
581 case HTTPSTATE_WAIT_REQUEST
:
582 case HTTPSTATE_RECEIVE_DATA
:
583 case HTTPSTATE_WAIT_FEED
:
584 case RTSPSTATE_WAIT_REQUEST
:
585 /* need to catch errors */
586 c
->poll_entry
= poll_entry
;
588 poll_entry
->events
= POLLIN
;/* Maybe this will work */
592 c
->poll_entry
= NULL
;
598 /* wait for an event on one connection. We poll at least every
599 second to handle timeouts */
601 ret
= poll(poll_table
, poll_entry
- poll_table
, delay
);
602 if (ret
< 0 && ff_neterrno() != FF_NETERROR(EAGAIN
) &&
603 ff_neterrno() != FF_NETERROR(EINTR
))
607 cur_time
= av_gettime() / 1000;
609 if (need_to_start_children
) {
610 need_to_start_children
= 0;
611 start_children(first_feed
);
614 /* now handle the events */
615 for(c
= first_http_ctx
; c
!= NULL
; c
= c_next
) {
617 if (handle_connection(c
) < 0) {
618 /* close and free the connection */
624 poll_entry
= poll_table
;
625 /* new HTTP connection request ? */
626 if (poll_entry
->revents
& POLLIN
)
627 new_connection(server_fd
, 0);
629 /* new RTSP connection request ? */
630 if (poll_entry
->revents
& POLLIN
)
631 new_connection(rtsp_server_fd
, 1);
635 /* start waiting for a new HTTP/RTSP request */
636 static void start_wait_request(HTTPContext
*c
, int is_rtsp
)
638 c
->buffer_ptr
= c
->buffer
;
639 c
->buffer_end
= c
->buffer
+ c
->buffer_size
- 1; /* leave room for '\0' */
642 c
->timeout
= cur_time
+ RTSP_REQUEST_TIMEOUT
;
643 c
->state
= RTSPSTATE_WAIT_REQUEST
;
645 c
->timeout
= cur_time
+ HTTP_REQUEST_TIMEOUT
;
646 c
->state
= HTTPSTATE_WAIT_REQUEST
;
650 static void new_connection(int server_fd
, int is_rtsp
)
652 struct sockaddr_in from_addr
;
654 HTTPContext
*c
= NULL
;
656 len
= sizeof(from_addr
);
657 fd
= accept(server_fd
, (struct sockaddr
*)&from_addr
,
661 ff_socket_nonblock(fd
, 1);
663 /* XXX: should output a warning page when coming
664 close to the connection limit */
665 if (nb_connections
>= nb_max_connections
)
668 /* add a new connection */
669 c
= av_mallocz(sizeof(HTTPContext
));
674 c
->poll_entry
= NULL
;
675 c
->from_addr
= from_addr
;
676 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
677 c
->buffer
= av_malloc(c
->buffer_size
);
681 c
->next
= first_http_ctx
;
685 start_wait_request(c
, is_rtsp
);
697 static void close_connection(HTTPContext
*c
)
699 HTTPContext
**cp
, *c1
;
701 AVFormatContext
*ctx
;
705 /* remove connection from list */
706 cp
= &first_http_ctx
;
707 while ((*cp
) != NULL
) {
715 /* remove references, if any (XXX: do it faster) */
716 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
721 /* remove connection associated resources */
725 /* close each frame parser */
726 for(i
=0;i
<c
->fmt_in
->nb_streams
;i
++) {
727 st
= c
->fmt_in
->streams
[i
];
728 if (st
->codec
->codec
)
729 avcodec_close(st
->codec
);
731 av_close_input_file(c
->fmt_in
);
734 /* free RTP output streams if any */
737 nb_streams
= c
->stream
->nb_streams
;
739 for(i
=0;i
<nb_streams
;i
++) {
742 av_write_trailer(ctx
);
745 h
= c
->rtp_handles
[i
];
752 if (!c
->last_packet_sent
) {
755 if (url_open_dyn_buf(&ctx
->pb
) >= 0) {
756 av_write_trailer(ctx
);
757 url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
762 for(i
=0; i
<ctx
->nb_streams
; i
++)
763 av_free(ctx
->streams
[i
]);
765 if (c
->stream
&& !c
->post
&& c
->stream
->stream_type
== STREAM_TYPE_LIVE
)
766 current_bandwidth
-= c
->stream
->bandwidth
;
768 /* signal that there is no feed if we are the feeder socket */
769 if (c
->state
== HTTPSTATE_RECEIVE_DATA
&& c
->stream
) {
770 c
->stream
->feed_opened
= 0;
774 av_freep(&c
->pb_buffer
);
775 av_freep(&c
->packet_buffer
);
781 static int handle_connection(HTTPContext
*c
)
786 case HTTPSTATE_WAIT_REQUEST
:
787 case RTSPSTATE_WAIT_REQUEST
:
789 if ((c
->timeout
- cur_time
) < 0)
791 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
794 /* no need to read if no events */
795 if (!(c
->poll_entry
->revents
& POLLIN
))
799 len
= recv(c
->fd
, c
->buffer_ptr
, 1, 0);
801 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
802 ff_neterrno() != FF_NETERROR(EINTR
))
804 } else if (len
== 0) {
807 /* search for end of request. */
809 c
->buffer_ptr
+= len
;
811 if ((ptr
>= c
->buffer
+ 2 && !memcmp(ptr
-2, "\n\n", 2)) ||
812 (ptr
>= c
->buffer
+ 4 && !memcmp(ptr
-4, "\r\n\r\n", 4))) {
813 /* request found : parse it and reply */
814 if (c
->state
== HTTPSTATE_WAIT_REQUEST
) {
815 ret
= http_parse_request(c
);
817 ret
= rtsp_parse_request(c
);
821 } else if (ptr
>= c
->buffer_end
) {
822 /* request too long: cannot do anything */
824 } else goto read_loop
;
828 case HTTPSTATE_SEND_HEADER
:
829 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
832 /* no need to write if no events */
833 if (!(c
->poll_entry
->revents
& POLLOUT
))
835 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
837 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
838 ff_neterrno() != FF_NETERROR(EINTR
)) {
839 /* error : close connection */
840 av_freep(&c
->pb_buffer
);
844 c
->buffer_ptr
+= len
;
846 c
->stream
->bytes_served
+= len
;
847 c
->data_count
+= len
;
848 if (c
->buffer_ptr
>= c
->buffer_end
) {
849 av_freep(&c
->pb_buffer
);
853 /* all the buffer was sent : synchronize to the incoming stream */
854 c
->state
= HTTPSTATE_SEND_DATA_HEADER
;
855 c
->buffer_ptr
= c
->buffer_end
= c
->buffer
;
860 case HTTPSTATE_SEND_DATA
:
861 case HTTPSTATE_SEND_DATA_HEADER
:
862 case HTTPSTATE_SEND_DATA_TRAILER
:
863 /* for packetized output, we consider we can always write (the
864 input streams sets the speed). It may be better to verify
865 that we do not rely too much on the kernel queues */
866 if (!c
->is_packetized
) {
867 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
870 /* no need to read if no events */
871 if (!(c
->poll_entry
->revents
& POLLOUT
))
874 if (http_send_data(c
) < 0)
876 /* close connection if trailer sent */
877 if (c
->state
== HTTPSTATE_SEND_DATA_TRAILER
)
880 case HTTPSTATE_RECEIVE_DATA
:
881 /* no need to read if no events */
882 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
884 if (!(c
->poll_entry
->revents
& POLLIN
))
886 if (http_receive_data(c
) < 0)
889 case HTTPSTATE_WAIT_FEED
:
890 /* no need to read if no events */
891 if (c
->poll_entry
->revents
& (POLLIN
| POLLERR
| POLLHUP
))
894 /* nothing to do, we'll be waken up by incoming feed packets */
897 case RTSPSTATE_SEND_REPLY
:
898 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
899 av_freep(&c
->pb_buffer
);
902 /* no need to write if no events */
903 if (!(c
->poll_entry
->revents
& POLLOUT
))
905 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
907 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
908 ff_neterrno() != FF_NETERROR(EINTR
)) {
909 /* error : close connection */
910 av_freep(&c
->pb_buffer
);
914 c
->buffer_ptr
+= len
;
915 c
->data_count
+= len
;
916 if (c
->buffer_ptr
>= c
->buffer_end
) {
917 /* all the buffer was sent : wait for a new request */
918 av_freep(&c
->pb_buffer
);
919 start_wait_request(c
, 1);
923 case RTSPSTATE_SEND_PACKET
:
924 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
925 av_freep(&c
->packet_buffer
);
928 /* no need to write if no events */
929 if (!(c
->poll_entry
->revents
& POLLOUT
))
931 len
= send(c
->fd
, c
->packet_buffer_ptr
,
932 c
->packet_buffer_end
- c
->packet_buffer_ptr
, 0);
934 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
935 ff_neterrno() != FF_NETERROR(EINTR
)) {
936 /* error : close connection */
937 av_freep(&c
->packet_buffer
);
941 c
->packet_buffer_ptr
+= len
;
942 if (c
->packet_buffer_ptr
>= c
->packet_buffer_end
) {
943 /* all the buffer was sent : wait for a new request */
944 av_freep(&c
->packet_buffer
);
945 c
->state
= RTSPSTATE_WAIT_REQUEST
;
949 case HTTPSTATE_READY
:
958 static int extract_rates(char *rates
, int ratelen
, const char *request
)
962 for (p
= request
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
963 if (strncasecmp(p
, "Pragma:", 7) == 0) {
964 const char *q
= p
+ 7;
966 while (*q
&& *q
!= '\n' && isspace(*q
))
969 if (strncasecmp(q
, "stream-switch-entry=", 20) == 0) {
975 memset(rates
, 0xff, ratelen
);
978 while (*q
&& *q
!= '\n' && *q
!= ':')
981 if (sscanf(q
, ":%d:%d", &stream_no
, &rate_no
) != 2)
985 if (stream_no
< ratelen
&& stream_no
>= 0)
986 rates
[stream_no
] = rate_no
;
988 while (*q
&& *q
!= '\n' && !isspace(*q
))
1005 static int find_stream_in_feed(FFStream
*feed
, AVCodecContext
*codec
, int bit_rate
)
1008 int best_bitrate
= 100000000;
1011 for (i
= 0; i
< feed
->nb_streams
; i
++) {
1012 AVCodecContext
*feed_codec
= feed
->streams
[i
]->codec
;
1014 if (feed_codec
->codec_id
!= codec
->codec_id
||
1015 feed_codec
->sample_rate
!= codec
->sample_rate
||
1016 feed_codec
->width
!= codec
->width
||
1017 feed_codec
->height
!= codec
->height
)
1020 /* Potential stream */
1022 /* We want the fastest stream less than bit_rate, or the slowest
1023 * faster than bit_rate
1026 if (feed_codec
->bit_rate
<= bit_rate
) {
1027 if (best_bitrate
> bit_rate
|| feed_codec
->bit_rate
> best_bitrate
) {
1028 best_bitrate
= feed_codec
->bit_rate
;
1032 if (feed_codec
->bit_rate
< best_bitrate
) {
1033 best_bitrate
= feed_codec
->bit_rate
;
1042 static int modify_current_stream(HTTPContext
*c
, char *rates
)
1045 FFStream
*req
= c
->stream
;
1046 int action_required
= 0;
1048 /* Not much we can do for a feed */
1052 for (i
= 0; i
< req
->nb_streams
; i
++) {
1053 AVCodecContext
*codec
= req
->streams
[i
]->codec
;
1057 c
->switch_feed_streams
[i
] = req
->feed_streams
[i
];
1060 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 2);
1063 /* Wants off or slow */
1064 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 4);
1066 /* This doesn't work well when it turns off the only stream! */
1067 c
->switch_feed_streams
[i
] = -2;
1068 c
->feed_streams
[i
] = -2;
1073 if (c
->switch_feed_streams
[i
] >= 0 && c
->switch_feed_streams
[i
] != c
->feed_streams
[i
])
1074 action_required
= 1;
1077 return action_required
;
1081 static void do_switch_stream(HTTPContext
*c
, int i
)
1083 if (c
->switch_feed_streams
[i
] >= 0) {
1085 c
->feed_streams
[i
] = c
->switch_feed_streams
[i
];
1088 /* Now update the stream */
1090 c
->switch_feed_streams
[i
] = -1;
1093 /* XXX: factorize in utils.c ? */
1094 /* XXX: take care with different space meaning */
1095 static void skip_spaces(const char **pp
)
1099 while (*p
== ' ' || *p
== '\t')
1104 static void get_word(char *buf
, int buf_size
, const char **pp
)
1112 while (!isspace(*p
) && *p
!= '\0') {
1113 if ((q
- buf
) < buf_size
- 1)
1122 static int validate_acl(FFStream
*stream
, HTTPContext
*c
)
1124 enum IPAddressAction last_action
= IP_DENY
;
1126 struct in_addr
*src
= &c
->from_addr
.sin_addr
;
1127 unsigned long src_addr
= src
->s_addr
;
1129 for (acl
= stream
->acl
; acl
; acl
= acl
->next
) {
1130 if (src_addr
>= acl
->first
.s_addr
&& src_addr
<= acl
->last
.s_addr
)
1131 return (acl
->action
== IP_ALLOW
) ? 1 : 0;
1132 last_action
= acl
->action
;
1135 /* Nothing matched, so return not the last action */
1136 return (last_action
== IP_DENY
) ? 1 : 0;
1139 /* compute the real filename of a file by matching it without its
1140 extensions to all the stream filenames */
1141 static void compute_real_filename(char *filename
, int max_size
)
1148 /* compute filename by matching without the file extensions */
1149 av_strlcpy(file1
, filename
, sizeof(file1
));
1150 p
= strrchr(file1
, '.');
1153 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
1154 av_strlcpy(file2
, stream
->filename
, sizeof(file2
));
1155 p
= strrchr(file2
, '.');
1158 if (!strcmp(file1
, file2
)) {
1159 av_strlcpy(filename
, stream
->filename
, max_size
);
1174 /* parse http request and prepare header */
1175 static int http_parse_request(HTTPContext
*c
)
1178 enum RedirType redir_type
;
1180 char info
[1024], filename
[1024];
1184 const char *mime_type
;
1188 char *useragent
= 0;
1191 get_word(cmd
, sizeof(cmd
), (const char **)&p
);
1192 av_strlcpy(c
->method
, cmd
, sizeof(c
->method
));
1194 if (!strcmp(cmd
, "GET"))
1196 else if (!strcmp(cmd
, "POST"))
1201 get_word(url
, sizeof(url
), (const char **)&p
);
1202 av_strlcpy(c
->url
, url
, sizeof(c
->url
));
1204 get_word(protocol
, sizeof(protocol
), (const char **)&p
);
1205 if (strcmp(protocol
, "HTTP/1.0") && strcmp(protocol
, "HTTP/1.1"))
1208 av_strlcpy(c
->protocol
, protocol
, sizeof(c
->protocol
));
1211 http_log("New connection: %s %s\n", cmd
, url
);
1213 /* find the filename and the optional info string in the request */
1214 p
= strchr(url
, '?');
1216 av_strlcpy(info
, p
, sizeof(info
));
1221 av_strlcpy(filename
, url
+ ((*url
== '/') ? 1 : 0), sizeof(filename
)-1);
1223 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1224 if (strncasecmp(p
, "User-Agent:", 11) == 0) {
1226 if (*useragent
&& *useragent
!= '\n' && isspace(*useragent
))
1230 p
= strchr(p
, '\n');
1237 redir_type
= REDIR_NONE
;
1238 if (match_ext(filename
, "asx")) {
1239 redir_type
= REDIR_ASX
;
1240 filename
[strlen(filename
)-1] = 'f';
1241 } else if (match_ext(filename
, "asf") &&
1242 (!useragent
|| strncasecmp(useragent
, "NSPlayer", 8) != 0)) {
1243 /* if this isn't WMP or lookalike, return the redirector file */
1244 redir_type
= REDIR_ASF
;
1245 } else if (match_ext(filename
, "rpm,ram")) {
1246 redir_type
= REDIR_RAM
;
1247 strcpy(filename
+ strlen(filename
)-2, "m");
1248 } else if (match_ext(filename
, "rtsp")) {
1249 redir_type
= REDIR_RTSP
;
1250 compute_real_filename(filename
, sizeof(filename
) - 1);
1251 } else if (match_ext(filename
, "sdp")) {
1252 redir_type
= REDIR_SDP
;
1253 compute_real_filename(filename
, sizeof(filename
) - 1);
1256 // "redirect" / request to index.html
1257 if (!strlen(filename
))
1258 av_strlcpy(filename
, "index.html", sizeof(filename
) - 1);
1260 stream
= first_stream
;
1261 while (stream
!= NULL
) {
1262 if (!strcmp(stream
->filename
, filename
) && validate_acl(stream
, c
))
1264 stream
= stream
->next
;
1266 if (stream
== NULL
) {
1267 snprintf(msg
, sizeof(msg
), "File '%s' not found", url
);
1272 memcpy(c
->feed_streams
, stream
->feed_streams
, sizeof(c
->feed_streams
));
1273 memset(c
->switch_feed_streams
, -1, sizeof(c
->switch_feed_streams
));
1275 if (stream
->stream_type
== STREAM_TYPE_REDIRECT
) {
1276 c
->http_error
= 301;
1278 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 301 Moved\r\n");
1279 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Location: %s\r\n", stream
->feed_filename
);
1280 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1281 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1282 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Moved</title></head><body>\r\n");
1283 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "You should be <a href=\"%s\">redirected</a>.\r\n", stream
->feed_filename
);
1284 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1286 /* prepare output buffer */
1287 c
->buffer_ptr
= c
->buffer
;
1289 c
->state
= HTTPSTATE_SEND_HEADER
;
1293 /* If this is WMP, get the rate information */
1294 if (extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1295 if (modify_current_stream(c
, ratebuf
)) {
1296 for (i
= 0; i
< sizeof(c
->feed_streams
) / sizeof(c
->feed_streams
[0]); i
++) {
1297 if (c
->switch_feed_streams
[i
] >= 0)
1298 do_switch_stream(c
, i
);
1303 /* If already streaming this feed, do not let start another feeder. */
1304 if (stream
->feed_opened
) {
1305 snprintf(msg
, sizeof(msg
), "This feed is already being received.");
1309 if (c
->post
== 0 && stream
->stream_type
== STREAM_TYPE_LIVE
)
1310 current_bandwidth
+= stream
->bandwidth
;
1312 if (c
->post
== 0 && max_bandwidth
< current_bandwidth
) {
1313 c
->http_error
= 200;
1315 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 Server too busy\r\n");
1316 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1317 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1318 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Too busy</title></head><body>\r\n");
1319 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<p>The server is too busy to serve your request at this time.</p>\r\n");
1320 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<p>The bandwidth being served (including your stream) is %dkbit/sec, and this exceeds the limit of %dkbit/sec.</p>\r\n",
1321 current_bandwidth
, max_bandwidth
);
1322 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1324 /* prepare output buffer */
1325 c
->buffer_ptr
= c
->buffer
;
1327 c
->state
= HTTPSTATE_SEND_HEADER
;
1331 if (redir_type
!= REDIR_NONE
) {
1334 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1335 if (strncasecmp(p
, "Host:", 5) == 0) {
1339 p
= strchr(p
, '\n');
1350 while (isspace(*hostinfo
))
1353 eoh
= strchr(hostinfo
, '\n');
1355 if (eoh
[-1] == '\r')
1358 if (eoh
- hostinfo
< sizeof(hostbuf
) - 1) {
1359 memcpy(hostbuf
, hostinfo
, eoh
- hostinfo
);
1360 hostbuf
[eoh
- hostinfo
] = 0;
1362 c
->http_error
= 200;
1364 switch(redir_type
) {
1366 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASX Follows\r\n");
1367 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1368 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1369 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ASX Version=\"3\">\r\n");
1370 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1371 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1372 hostbuf
, filename
, info
);
1373 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</ASX>\r\n");
1376 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RAM Follows\r\n");
1377 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: audio/x-pn-realaudio\r\n");
1378 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1379 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "# Autogenerated by ffserver\r\n");
1380 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "http://%s/%s%s\r\n",
1381 hostbuf
, filename
, info
);
1384 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASF Redirect follows\r\n");
1385 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1386 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1387 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "[Reference]\r\n");
1388 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Ref1=http://%s/%s%s\r\n",
1389 hostbuf
, filename
, info
);
1393 char hostname
[256], *p
;
1394 /* extract only hostname */
1395 av_strlcpy(hostname
, hostbuf
, sizeof(hostname
));
1396 p
= strrchr(hostname
, ':');
1399 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1400 /* XXX: incorrect mime type ? */
1401 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/x-rtsp\r\n");
1402 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1403 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "rtsp://%s:%d/%s\r\n",
1404 hostname
, ntohs(my_rtsp_addr
.sin_port
),
1411 int sdp_data_size
, len
;
1412 struct sockaddr_in my_addr
;
1414 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1415 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/sdp\r\n");
1416 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1418 len
= sizeof(my_addr
);
1419 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
1421 /* XXX: should use a dynamic buffer */
1422 sdp_data_size
= prepare_sdp_description(stream
,
1425 if (sdp_data_size
> 0) {
1426 memcpy(q
, sdp_data
, sdp_data_size
);
1438 /* prepare output buffer */
1439 c
->buffer_ptr
= c
->buffer
;
1441 c
->state
= HTTPSTATE_SEND_HEADER
;
1447 snprintf(msg
, sizeof(msg
), "ASX/RAM file not handled");
1451 stream
->conns_served
++;
1453 /* XXX: add there authenticate and IP match */
1456 /* if post, it means a feed is being sent */
1457 if (!stream
->is_feed
) {
1458 /* However it might be a status report from WMP! Lets log the data
1459 * as it might come in handy one day
1464 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1465 if (strncasecmp(p
, "Pragma: log-line=", 17) == 0) {
1469 if (strncasecmp(p
, "Pragma: client-id=", 18) == 0)
1470 client_id
= strtol(p
+ 18, 0, 10);
1471 p
= strchr(p
, '\n');
1479 char *eol
= strchr(logline
, '\n');
1484 if (eol
[-1] == '\r')
1486 http_log("%.*s\n", (int) (eol
- logline
), logline
);
1487 c
->suppress_log
= 1;
1492 http_log("\nGot request:\n%s\n", c
->buffer
);
1495 if (client_id
&& extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1498 /* Now we have to find the client_id */
1499 for (wmpc
= first_http_ctx
; wmpc
; wmpc
= wmpc
->next
) {
1500 if (wmpc
->wmp_client_id
== client_id
)
1504 if (wmpc
&& modify_current_stream(wmpc
, ratebuf
))
1505 wmpc
->switch_pending
= 1;
1508 snprintf(msg
, sizeof(msg
), "POST command not handled");
1512 if (http_start_receive_data(c
) < 0) {
1513 snprintf(msg
, sizeof(msg
), "could not open feed");
1517 c
->state
= HTTPSTATE_RECEIVE_DATA
;
1522 if (strcmp(stream
->filename
+ strlen(stream
->filename
) - 4, ".asf") == 0)
1523 http_log("\nGot request:\n%s\n", c
->buffer
);
1526 if (c
->stream
->stream_type
== STREAM_TYPE_STATUS
)
1529 /* open input stream */
1530 if (open_input_stream(c
, info
) < 0) {
1531 snprintf(msg
, sizeof(msg
), "Input stream corresponding to '%s' not found", url
);
1535 /* prepare http header */
1537 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1538 mime_type
= c
->stream
->fmt
->mime_type
;
1540 mime_type
= "application/x-octet-stream";
1541 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Pragma: no-cache\r\n");
1543 /* for asf, we need extra headers */
1544 if (!strcmp(c
->stream
->fmt
->name
,"asf_stream")) {
1545 /* Need to allocate a client id */
1547 c
->wmp_client_id
= av_random(&random_state
) & 0x7fffffff;
1549 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=%d\r\nPragma: features=\"broadcast\"\r\n", c
->wmp_client_id
);
1551 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-Type: %s\r\n", mime_type
);
1552 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1554 /* prepare output buffer */
1556 c
->buffer_ptr
= c
->buffer
;
1558 c
->state
= HTTPSTATE_SEND_HEADER
;
1561 c
->http_error
= 404;
1563 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 404 Not Found\r\n");
1564 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: %s\r\n", "text/html");
1565 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1566 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HTML>\n");
1567 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1568 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<BODY>%s</BODY>\n", msg
);
1569 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</HTML>\n");
1571 /* prepare output buffer */
1572 c
->buffer_ptr
= c
->buffer
;
1574 c
->state
= HTTPSTATE_SEND_HEADER
;
1578 c
->http_error
= 200; /* horrible : we use this value to avoid
1579 going to the send data state */
1580 c
->state
= HTTPSTATE_SEND_HEADER
;
1584 static void fmt_bytecount(ByteIOContext
*pb
, int64_t count
)
1586 static const char *suffix
= " kMGTP";
1589 for (s
= suffix
; count
>= 100000 && s
[1]; count
/= 1000, s
++);
1591 url_fprintf(pb
, "%"PRId64
"%c", count
, *s
);
1594 static void compute_stats(HTTPContext
*c
)
1603 if (url_open_dyn_buf(&pb
) < 0) {
1604 /* XXX: return an error ? */
1605 c
->buffer_ptr
= c
->buffer
;
1606 c
->buffer_end
= c
->buffer
;
1610 url_fprintf(pb
, "HTTP/1.0 200 OK\r\n");
1611 url_fprintf(pb
, "Content-type: %s\r\n", "text/html");
1612 url_fprintf(pb
, "Pragma: no-cache\r\n");
1613 url_fprintf(pb
, "\r\n");
1615 url_fprintf(pb
, "<HEAD><TITLE>FFServer Status</TITLE>\n");
1616 if (c
->stream
->feed_filename
)
1617 url_fprintf(pb
, "<link rel=\"shortcut icon\" href=\"%s\">\n", c
->stream
->feed_filename
);
1618 url_fprintf(pb
, "</HEAD>\n<BODY>");
1619 url_fprintf(pb
, "<H1>FFServer Status</H1>\n");
1621 url_fprintf(pb
, "<H2>Available Streams</H2>\n");
1622 url_fprintf(pb
, "<TABLE cellspacing=0 cellpadding=4>\n");
1623 url_fprintf(pb
, "<TR><Th valign=top>Path<th align=left>Served<br>Conns<Th><br>bytes<Th valign=top>Format<Th>Bit rate<br>kbits/s<Th align=left>Video<br>kbits/s<th><br>Codec<Th align=left>Audio<br>kbits/s<th><br>Codec<Th align=left valign=top>Feed\n");
1624 stream
= first_stream
;
1625 while (stream
!= NULL
) {
1626 char sfilename
[1024];
1629 if (stream
->feed
!= stream
) {
1630 av_strlcpy(sfilename
, stream
->filename
, sizeof(sfilename
) - 10);
1631 eosf
= sfilename
+ strlen(sfilename
);
1632 if (eosf
- sfilename
>= 4) {
1633 if (strcmp(eosf
- 4, ".asf") == 0)
1634 strcpy(eosf
- 4, ".asx");
1635 else if (strcmp(eosf
- 3, ".rm") == 0)
1636 strcpy(eosf
- 3, ".ram");
1637 else if (stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
1638 /* generate a sample RTSP director if
1639 unicast. Generate an SDP redirector if
1641 eosf
= strrchr(sfilename
, '.');
1643 eosf
= sfilename
+ strlen(sfilename
);
1644 if (stream
->is_multicast
)
1645 strcpy(eosf
, ".sdp");
1647 strcpy(eosf
, ".rtsp");
1651 url_fprintf(pb
, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1652 sfilename
, stream
->filename
);
1653 url_fprintf(pb
, "<td align=right> %d <td align=right> ",
1654 stream
->conns_served
);
1655 fmt_bytecount(pb
, stream
->bytes_served
);
1656 switch(stream
->stream_type
) {
1657 case STREAM_TYPE_LIVE
:
1659 int audio_bit_rate
= 0;
1660 int video_bit_rate
= 0;
1661 const char *audio_codec_name
= "";
1662 const char *video_codec_name
= "";
1663 const char *audio_codec_name_extra
= "";
1664 const char *video_codec_name_extra
= "";
1666 for(i
=0;i
<stream
->nb_streams
;i
++) {
1667 AVStream
*st
= stream
->streams
[i
];
1668 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1669 switch(st
->codec
->codec_type
) {
1670 case CODEC_TYPE_AUDIO
:
1671 audio_bit_rate
+= st
->codec
->bit_rate
;
1673 if (*audio_codec_name
)
1674 audio_codec_name_extra
= "...";
1675 audio_codec_name
= codec
->name
;
1678 case CODEC_TYPE_VIDEO
:
1679 video_bit_rate
+= st
->codec
->bit_rate
;
1681 if (*video_codec_name
)
1682 video_codec_name_extra
= "...";
1683 video_codec_name
= codec
->name
;
1686 case CODEC_TYPE_DATA
:
1687 video_bit_rate
+= st
->codec
->bit_rate
;
1693 url_fprintf(pb
, "<TD align=center> %s <TD align=right> %d <TD align=right> %d <TD> %s %s <TD align=right> %d <TD> %s %s",
1696 video_bit_rate
/ 1000, video_codec_name
, video_codec_name_extra
,
1697 audio_bit_rate
/ 1000, audio_codec_name
, audio_codec_name_extra
);
1699 url_fprintf(pb
, "<TD>%s", stream
->feed
->filename
);
1701 url_fprintf(pb
, "<TD>%s", stream
->feed_filename
);
1702 url_fprintf(pb
, "\n");
1706 url_fprintf(pb
, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1710 stream
= stream
->next
;
1712 url_fprintf(pb
, "</TABLE>\n");
1714 stream
= first_stream
;
1715 while (stream
!= NULL
) {
1716 if (stream
->feed
== stream
) {
1717 url_fprintf(pb
, "<h2>Feed %s</h2>", stream
->filename
);
1719 url_fprintf(pb
, "Running as pid %d.\n", stream
->pid
);
1721 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1726 /* This is somewhat linux specific I guess */
1727 snprintf(ps_cmd
, sizeof(ps_cmd
),
1728 "ps -o \"%%cpu,cputime\" --no-headers %d",
1731 pid_stat
= popen(ps_cmd
, "r");
1736 if (fscanf(pid_stat
, "%10s %64s", cpuperc
,
1738 url_fprintf(pb
, "Currently using %s%% of the cpu. Total time used %s.\n",
1746 url_fprintf(pb
, "<p>");
1748 url_fprintf(pb
, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>type<th>kbits/s<th align=left>codec<th align=left>Parameters\n");
1750 for (i
= 0; i
< stream
->nb_streams
; i
++) {
1751 AVStream
*st
= stream
->streams
[i
];
1752 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1753 const char *type
= "unknown";
1754 char parameters
[64];
1758 switch(st
->codec
->codec_type
) {
1759 case CODEC_TYPE_AUDIO
:
1761 snprintf(parameters
, sizeof(parameters
), "%d channel(s), %d Hz", st
->codec
->channels
, st
->codec
->sample_rate
);
1763 case CODEC_TYPE_VIDEO
:
1765 snprintf(parameters
, sizeof(parameters
), "%dx%d, q=%d-%d, fps=%d", st
->codec
->width
, st
->codec
->height
,
1766 st
->codec
->qmin
, st
->codec
->qmax
, st
->codec
->time_base
.den
/ st
->codec
->time_base
.num
);
1771 url_fprintf(pb
, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1772 i
, type
, st
->codec
->bit_rate
/1000, codec
? codec
->name
: "", parameters
);
1774 url_fprintf(pb
, "</table>\n");
1777 stream
= stream
->next
;
1783 AVCodecContext
*enc
;
1787 stream
= first_feed
;
1788 while (stream
!= NULL
) {
1789 url_fprintf(pb
, "<H1>Feed '%s'</H1>\n", stream
->filename
);
1790 url_fprintf(pb
, "<TABLE>\n");
1791 url_fprintf(pb
, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1792 for(i
=0;i
<stream
->nb_streams
;i
++) {
1793 AVStream
*st
= stream
->streams
[i
];
1794 FeedData
*fdata
= st
->priv_data
;
1797 avcodec_string(buf
, sizeof(buf
), enc
);
1798 avg
= fdata
->avg_frame_size
* (float)enc
->rate
* 8.0;
1799 if (enc
->codec
->type
== CODEC_TYPE_AUDIO
&& enc
->frame_size
> 0)
1800 avg
/= enc
->frame_size
;
1801 url_fprintf(pb
, "<TR><TD>%s <TD> %d <TD> %"PRId64
" <TD> %0.1f\n",
1802 buf
, enc
->frame_number
, fdata
->data_count
, avg
/ 1000.0);
1804 url_fprintf(pb
, "</TABLE>\n");
1805 stream
= stream
->next_feed
;
1810 /* connection status */
1811 url_fprintf(pb
, "<H2>Connection Status</H2>\n");
1813 url_fprintf(pb
, "Number of connections: %d / %d<BR>\n",
1814 nb_connections
, nb_max_connections
);
1816 url_fprintf(pb
, "Bandwidth in use: %dk / %dk<BR>\n",
1817 current_bandwidth
, max_bandwidth
);
1819 url_fprintf(pb
, "<TABLE>\n");
1820 url_fprintf(pb
, "<TR><th>#<th>File<th>IP<th>Proto<th>State<th>Target bits/sec<th>Actual bits/sec<th>Bytes transferred\n");
1821 c1
= first_http_ctx
;
1823 while (c1
!= NULL
) {
1829 for (j
= 0; j
< c1
->stream
->nb_streams
; j
++) {
1830 if (!c1
->stream
->feed
)
1831 bitrate
+= c1
->stream
->streams
[j
]->codec
->bit_rate
;
1832 else if (c1
->feed_streams
[j
] >= 0)
1833 bitrate
+= c1
->stream
->feed
->streams
[c1
->feed_streams
[j
]]->codec
->bit_rate
;
1838 p
= inet_ntoa(c1
->from_addr
.sin_addr
);
1839 url_fprintf(pb
, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1841 c1
->stream
? c1
->stream
->filename
: "",
1842 c1
->state
== HTTPSTATE_RECEIVE_DATA
? "(input)" : "",
1845 http_state
[c1
->state
]);
1846 fmt_bytecount(pb
, bitrate
);
1847 url_fprintf(pb
, "<td align=right>");
1848 fmt_bytecount(pb
, compute_datarate(&c1
->datarate
, c1
->data_count
) * 8);
1849 url_fprintf(pb
, "<td align=right>");
1850 fmt_bytecount(pb
, c1
->data_count
);
1851 url_fprintf(pb
, "\n");
1854 url_fprintf(pb
, "</TABLE>\n");
1859 url_fprintf(pb
, "<HR size=1 noshade>Generated at %s", p
);
1860 url_fprintf(pb
, "</BODY>\n</HTML>\n");
1862 len
= url_close_dyn_buf(pb
, &c
->pb_buffer
);
1863 c
->buffer_ptr
= c
->pb_buffer
;
1864 c
->buffer_end
= c
->pb_buffer
+ len
;
1867 /* check if the parser needs to be opened for stream i */
1868 static void open_parser(AVFormatContext
*s
, int i
)
1870 AVStream
*st
= s
->streams
[i
];
1873 if (!st
->codec
->codec
) {
1874 codec
= avcodec_find_decoder(st
->codec
->codec_id
);
1875 if (codec
&& (codec
->capabilities
& CODEC_CAP_PARSE_ONLY
)) {
1876 st
->codec
->parse_only
= 1;
1877 if (avcodec_open(st
->codec
, codec
) < 0)
1878 st
->codec
->parse_only
= 0;
1883 static int open_input_stream(HTTPContext
*c
, const char *info
)
1886 char input_filename
[1024];
1888 int buf_size
, i
, ret
;
1891 /* find file name */
1892 if (c
->stream
->feed
) {
1893 strcpy(input_filename
, c
->stream
->feed
->feed_filename
);
1894 buf_size
= FFM_PACKET_SIZE
;
1895 /* compute position (absolute time) */
1896 if (find_info_tag(buf
, sizeof(buf
), "date", info
))
1898 stream_pos
= parse_date(buf
, 0);
1899 if (stream_pos
== INT64_MIN
)
1902 else if (find_info_tag(buf
, sizeof(buf
), "buffer", info
)) {
1903 int prebuffer
= strtol(buf
, 0, 10);
1904 stream_pos
= av_gettime() - prebuffer
* (int64_t)1000000;
1906 stream_pos
= av_gettime() - c
->stream
->prebuffer
* (int64_t)1000;
1908 strcpy(input_filename
, c
->stream
->feed_filename
);
1910 /* compute position (relative time) */
1911 if (find_info_tag(buf
, sizeof(buf
), "date", info
))
1913 stream_pos
= parse_date(buf
, 1);
1914 if (stream_pos
== INT64_MIN
)
1920 if (input_filename
[0] == '\0')
1924 { time_t when
= stream_pos
/ 1000000;
1925 http_log("Stream pos = %"PRId64
", time=%s", stream_pos
, ctime(&when
));
1930 if ((ret
= av_open_input_file(&s
, input_filename
, c
->stream
->ifmt
,
1931 buf_size
, c
->stream
->ap_in
)) < 0) {
1932 http_log("could not open %s: %d\n", input_filename
, ret
);
1935 s
->flags
|= AVFMT_FLAG_GENPTS
;
1937 av_find_stream_info(c
->fmt_in
);
1939 /* open each parser */
1940 for(i
=0;i
<s
->nb_streams
;i
++)
1943 /* choose stream as clock source (we favorize video stream if
1944 present) for packet sending */
1945 c
->pts_stream_index
= 0;
1946 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
1947 if (c
->pts_stream_index
== 0 &&
1948 c
->stream
->streams
[i
]->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
1949 c
->pts_stream_index
= i
;
1954 if (c
->fmt_in
->iformat
->read_seek
)
1955 c
->fmt_in
->iformat
->read_seek(c
->fmt_in
, 0, stream_pos
, 0);
1957 /* set the start time (needed for maxtime and RTP packet timing) */
1958 c
->start_time
= cur_time
;
1959 c
->first_pts
= AV_NOPTS_VALUE
;
1963 /* return the server clock (in us) */
1964 static int64_t get_server_clock(HTTPContext
*c
)
1966 /* compute current pts value from system time */
1967 return (cur_time
- c
->start_time
) * 1000;
1970 /* return the estimated time at which the current packet must be sent
1972 static int64_t get_packet_send_clock(HTTPContext
*c
)
1974 int bytes_left
, bytes_sent
, frame_bytes
;
1976 frame_bytes
= c
->cur_frame_bytes
;
1977 if (frame_bytes
<= 0)
1980 bytes_left
= c
->buffer_end
- c
->buffer_ptr
;
1981 bytes_sent
= frame_bytes
- bytes_left
;
1982 return c
->cur_pts
+ (c
->cur_frame_duration
* bytes_sent
) / frame_bytes
;
1987 static int http_prepare_data(HTTPContext
*c
)
1990 AVFormatContext
*ctx
;
1992 av_freep(&c
->pb_buffer
);
1994 case HTTPSTATE_SEND_DATA_HEADER
:
1995 memset(&c
->fmt_ctx
, 0, sizeof(c
->fmt_ctx
));
1996 av_strlcpy(c
->fmt_ctx
.author
, c
->stream
->author
,
1997 sizeof(c
->fmt_ctx
.author
));
1998 av_strlcpy(c
->fmt_ctx
.comment
, c
->stream
->comment
,
1999 sizeof(c
->fmt_ctx
.comment
));
2000 av_strlcpy(c
->fmt_ctx
.copyright
, c
->stream
->copyright
,
2001 sizeof(c
->fmt_ctx
.copyright
));
2002 av_strlcpy(c
->fmt_ctx
.title
, c
->stream
->title
,
2003 sizeof(c
->fmt_ctx
.title
));
2005 /* open output stream by using specified codecs */
2006 c
->fmt_ctx
.oformat
= c
->stream
->fmt
;
2007 c
->fmt_ctx
.nb_streams
= c
->stream
->nb_streams
;
2008 for(i
=0;i
<c
->fmt_ctx
.nb_streams
;i
++) {
2011 st
= av_mallocz(sizeof(AVStream
));
2012 st
->codec
= avcodec_alloc_context();
2013 c
->fmt_ctx
.streams
[i
] = st
;
2014 /* if file or feed, then just take streams from FFStream struct */
2015 if (!c
->stream
->feed
||
2016 c
->stream
->feed
== c
->stream
)
2017 src
= c
->stream
->streams
[i
];
2019 src
= c
->stream
->feed
->streams
[c
->stream
->feed_streams
[i
]];
2023 st
->codec
->frame_number
= 0; /* XXX: should be done in
2024 AVStream, not in codec */
2026 c
->got_key_frame
= 0;
2028 /* prepare header and save header data in a stream */
2029 if (url_open_dyn_buf(&c
->fmt_ctx
.pb
) < 0) {
2030 /* XXX: potential leak */
2033 c
->fmt_ctx
.pb
->is_streamed
= 1;
2035 av_set_parameters(&c
->fmt_ctx
, NULL
);
2036 if (av_write_header(&c
->fmt_ctx
) < 0)
2039 len
= url_close_dyn_buf(c
->fmt_ctx
.pb
, &c
->pb_buffer
);
2040 c
->buffer_ptr
= c
->pb_buffer
;
2041 c
->buffer_end
= c
->pb_buffer
+ len
;
2043 c
->state
= HTTPSTATE_SEND_DATA
;
2044 c
->last_packet_sent
= 0;
2046 case HTTPSTATE_SEND_DATA
:
2047 /* find a new packet */
2051 /* read a packet from the input stream */
2052 if (c
->stream
->feed
)
2053 ffm_set_write_index(c
->fmt_in
,
2054 c
->stream
->feed
->feed_write_index
,
2055 c
->stream
->feed
->feed_size
);
2057 if (c
->stream
->max_time
&&
2058 c
->stream
->max_time
+ c
->start_time
- cur_time
< 0)
2059 /* We have timed out */
2060 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2063 if (av_read_frame(c
->fmt_in
, &pkt
) < 0) {
2064 if (c
->stream
->feed
&& c
->stream
->feed
->feed_opened
) {
2065 /* if coming from feed, it means we reached the end of the
2066 ffm file, so must wait for more data */
2067 c
->state
= HTTPSTATE_WAIT_FEED
;
2068 return 1; /* state changed */
2070 if (c
->stream
->loop
) {
2071 av_close_input_file(c
->fmt_in
);
2073 if (open_input_stream(c
, "") < 0)
2078 /* must send trailer now because eof or error */
2079 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2083 /* update first pts if needed */
2084 if (c
->first_pts
== AV_NOPTS_VALUE
) {
2085 c
->first_pts
= av_rescale_q(pkt
.dts
, c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
, AV_TIME_BASE_Q
);
2086 c
->start_time
= cur_time
;
2088 /* send it to the appropriate stream */
2089 if (c
->stream
->feed
) {
2090 /* if coming from a feed, select the right stream */
2091 if (c
->switch_pending
) {
2092 c
->switch_pending
= 0;
2093 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2094 if (c
->switch_feed_streams
[i
] == pkt
.stream_index
)
2095 if (pkt
.flags
& PKT_FLAG_KEY
)
2096 do_switch_stream(c
, i
);
2097 if (c
->switch_feed_streams
[i
] >= 0)
2098 c
->switch_pending
= 1;
2101 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2102 if (c
->feed_streams
[i
] == pkt
.stream_index
) {
2103 pkt
.stream_index
= i
;
2104 if (pkt
.flags
& PKT_FLAG_KEY
)
2105 c
->got_key_frame
|= 1 << i
;
2106 /* See if we have all the key frames, then
2107 * we start to send. This logic is not quite
2108 * right, but it works for the case of a
2109 * single video stream with one or more
2110 * audio streams (for which every frame is
2111 * typically a key frame).
2113 if (!c
->stream
->send_on_key
||
2114 ((c
->got_key_frame
+ 1) >> c
->stream
->nb_streams
))
2119 AVCodecContext
*codec
;
2122 /* specific handling for RTP: we use several
2123 output stream (one for each RTP
2124 connection). XXX: need more abstract handling */
2125 if (c
->is_packetized
) {
2127 /* compute send time and duration */
2128 st
= c
->fmt_in
->streams
[pkt
.stream_index
];
2129 c
->cur_pts
= av_rescale_q(pkt
.dts
, st
->time_base
, AV_TIME_BASE_Q
);
2130 if (st
->start_time
!= AV_NOPTS_VALUE
)
2131 c
->cur_pts
-= av_rescale_q(st
->start_time
, st
->time_base
, AV_TIME_BASE_Q
);
2132 c
->cur_frame_duration
= av_rescale_q(pkt
.duration
, st
->time_base
, AV_TIME_BASE_Q
);
2134 printf("index=%d pts=%0.3f duration=%0.6f\n",
2136 (double)c
->cur_pts
/
2138 (double)c
->cur_frame_duration
/
2141 /* find RTP context */
2142 c
->packet_stream_index
= pkt
.stream_index
;
2143 ctx
= c
->rtp_ctx
[c
->packet_stream_index
];
2145 av_free_packet(&pkt
);
2148 codec
= ctx
->streams
[0]->codec
;
2149 /* only one stream per RTP connection */
2150 pkt
.stream_index
= 0;
2154 codec
= ctx
->streams
[pkt
.stream_index
]->codec
;
2157 if (c
->is_packetized
) {
2158 int max_packet_size
;
2159 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
)
2160 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
2162 max_packet_size
= url_get_max_packet_size(c
->rtp_handles
[c
->packet_stream_index
]);
2163 ret
= url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
);
2165 ret
= url_open_dyn_buf(&ctx
->pb
);
2168 /* XXX: potential leak */
2171 if (pkt
.dts
!= AV_NOPTS_VALUE
)
2172 pkt
.dts
= av_rescale_q(pkt
.dts
,
2173 c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
,
2174 ctx
->streams
[pkt
.stream_index
]->time_base
);
2175 if (pkt
.pts
!= AV_NOPTS_VALUE
)
2176 pkt
.pts
= av_rescale_q(pkt
.pts
,
2177 c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
,
2178 ctx
->streams
[pkt
.stream_index
]->time_base
);
2179 if (av_write_frame(ctx
, &pkt
))
2180 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2182 len
= url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
2183 c
->cur_frame_bytes
= len
;
2184 c
->buffer_ptr
= c
->pb_buffer
;
2185 c
->buffer_end
= c
->pb_buffer
+ len
;
2187 codec
->frame_number
++;
2189 av_free_packet(&pkt
);
2193 av_free_packet(&pkt
);
2199 case HTTPSTATE_SEND_DATA_TRAILER
:
2200 /* last packet test ? */
2201 if (c
->last_packet_sent
|| c
->is_packetized
)
2204 /* prepare header */
2205 if (url_open_dyn_buf(&ctx
->pb
) < 0) {
2206 /* XXX: potential leak */
2209 av_write_trailer(ctx
);
2210 len
= url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
2211 c
->buffer_ptr
= c
->pb_buffer
;
2212 c
->buffer_end
= c
->pb_buffer
+ len
;
2214 c
->last_packet_sent
= 1;
2220 /* should convert the format at the same time */
2221 /* send data starting at c->buffer_ptr to the output connection
2222 (either UDP or TCP connection) */
2223 static int http_send_data(HTTPContext
*c
)
2228 if (c
->buffer_ptr
>= c
->buffer_end
) {
2229 ret
= http_prepare_data(c
);
2233 /* state change requested */
2236 if (c
->is_packetized
) {
2237 /* RTP data output */
2238 len
= c
->buffer_end
- c
->buffer_ptr
;
2240 /* fail safe - should never happen */
2242 c
->buffer_ptr
= c
->buffer_end
;
2245 len
= (c
->buffer_ptr
[0] << 24) |
2246 (c
->buffer_ptr
[1] << 16) |
2247 (c
->buffer_ptr
[2] << 8) |
2249 if (len
> (c
->buffer_end
- c
->buffer_ptr
))
2251 if ((get_packet_send_clock(c
) - get_server_clock(c
)) > 0) {
2252 /* nothing to send yet: we can wait */
2256 c
->data_count
+= len
;
2257 update_datarate(&c
->datarate
, c
->data_count
);
2259 c
->stream
->bytes_served
+= len
;
2261 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
) {
2262 /* RTP packets are sent inside the RTSP TCP connection */
2264 int interleaved_index
, size
;
2266 HTTPContext
*rtsp_c
;
2269 /* if no RTSP connection left, error */
2272 /* if already sending something, then wait. */
2273 if (rtsp_c
->state
!= RTSPSTATE_WAIT_REQUEST
)
2275 if (url_open_dyn_buf(&pb
) < 0)
2277 interleaved_index
= c
->packet_stream_index
* 2;
2278 /* RTCP packets are sent at odd indexes */
2279 if (c
->buffer_ptr
[1] == 200)
2280 interleaved_index
++;
2281 /* write RTSP TCP header */
2283 header
[1] = interleaved_index
;
2284 header
[2] = len
>> 8;
2286 put_buffer(pb
, header
, 4);
2287 /* write RTP packet data */
2289 put_buffer(pb
, c
->buffer_ptr
, len
);
2290 size
= url_close_dyn_buf(pb
, &c
->packet_buffer
);
2291 /* prepare asynchronous TCP sending */
2292 rtsp_c
->packet_buffer_ptr
= c
->packet_buffer
;
2293 rtsp_c
->packet_buffer_end
= c
->packet_buffer
+ size
;
2294 c
->buffer_ptr
+= len
;
2296 /* send everything we can NOW */
2297 len
= send(rtsp_c
->fd
, rtsp_c
->packet_buffer_ptr
,
2298 rtsp_c
->packet_buffer_end
- rtsp_c
->packet_buffer_ptr
, 0);
2300 rtsp_c
->packet_buffer_ptr
+= len
;
2301 if (rtsp_c
->packet_buffer_ptr
< rtsp_c
->packet_buffer_end
) {
2302 /* if we could not send all the data, we will
2303 send it later, so a new state is needed to
2304 "lock" the RTSP TCP connection */
2305 rtsp_c
->state
= RTSPSTATE_SEND_PACKET
;
2308 /* all data has been sent */
2309 av_freep(&c
->packet_buffer
);
2311 /* send RTP packet directly in UDP */
2313 url_write(c
->rtp_handles
[c
->packet_stream_index
],
2314 c
->buffer_ptr
, len
);
2315 c
->buffer_ptr
+= len
;
2316 /* here we continue as we can send several packets per 10 ms slot */
2319 /* TCP data output */
2320 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2322 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
2323 ff_neterrno() != FF_NETERROR(EINTR
))
2324 /* error : close connection */
2329 c
->buffer_ptr
+= len
;
2331 c
->data_count
+= len
;
2332 update_datarate(&c
->datarate
, c
->data_count
);
2334 c
->stream
->bytes_served
+= len
;
2342 static int http_start_receive_data(HTTPContext
*c
)
2346 if (c
->stream
->feed_opened
)
2349 /* Don't permit writing to this one */
2350 if (c
->stream
->readonly
)
2354 fd
= open(c
->stream
->feed_filename
, O_RDWR
);
2359 c
->stream
->feed_write_index
= ffm_read_write_index(fd
);
2360 c
->stream
->feed_size
= lseek(fd
, 0, SEEK_END
);
2361 lseek(fd
, 0, SEEK_SET
);
2363 /* init buffer input */
2364 c
->buffer_ptr
= c
->buffer
;
2365 c
->buffer_end
= c
->buffer
+ FFM_PACKET_SIZE
;
2366 c
->stream
->feed_opened
= 1;
2370 static int http_receive_data(HTTPContext
*c
)
2374 if (c
->buffer_end
> c
->buffer_ptr
) {
2377 len
= recv(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2379 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
2380 ff_neterrno() != FF_NETERROR(EINTR
))
2381 /* error : close connection */
2383 } else if (len
== 0)
2384 /* end of connection : close it */
2387 c
->buffer_ptr
+= len
;
2388 c
->data_count
+= len
;
2389 update_datarate(&c
->datarate
, c
->data_count
);
2393 if (c
->buffer_ptr
- c
->buffer
>= 2 && c
->data_count
> FFM_PACKET_SIZE
) {
2394 if (c
->buffer
[0] != 'f' ||
2395 c
->buffer
[1] != 'm') {
2396 http_log("Feed stream has become desynchronized -- disconnecting\n");
2401 if (c
->buffer_ptr
>= c
->buffer_end
) {
2402 FFStream
*feed
= c
->stream
;
2403 /* a packet has been received : write it in the store, except
2405 if (c
->data_count
> FFM_PACKET_SIZE
) {
2407 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2408 /* XXX: use llseek or url_seek */
2409 lseek(c
->feed_fd
, feed
->feed_write_index
, SEEK_SET
);
2410 write(c
->feed_fd
, c
->buffer
, FFM_PACKET_SIZE
);
2412 feed
->feed_write_index
+= FFM_PACKET_SIZE
;
2413 /* update file size */
2414 if (feed
->feed_write_index
> c
->stream
->feed_size
)
2415 feed
->feed_size
= feed
->feed_write_index
;
2417 /* handle wrap around if max file size reached */
2418 if (c
->stream
->feed_max_size
&& feed
->feed_write_index
>= c
->stream
->feed_max_size
)
2419 feed
->feed_write_index
= FFM_PACKET_SIZE
;
2422 ffm_write_write_index(c
->feed_fd
, feed
->feed_write_index
);
2424 /* wake up any waiting connections */
2425 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
2426 if (c1
->state
== HTTPSTATE_WAIT_FEED
&&
2427 c1
->stream
->feed
== c
->stream
->feed
)
2428 c1
->state
= HTTPSTATE_SEND_DATA
;
2431 /* We have a header in our hands that contains useful data */
2433 AVInputFormat
*fmt_in
;
2436 memset(&s
, 0, sizeof(s
));
2438 url_open_buf(&s
.pb
, c
->buffer
, c
->buffer_end
- c
->buffer
, URL_RDONLY
);
2439 s
.pb
->is_streamed
= 1;
2441 /* use feed output format name to find corresponding input format */
2442 fmt_in
= av_find_input_format(feed
->fmt
->name
);
2446 if (fmt_in
->priv_data_size
> 0) {
2447 s
.priv_data
= av_mallocz(fmt_in
->priv_data_size
);
2453 if (fmt_in
->read_header(&s
, 0) < 0) {
2454 av_freep(&s
.priv_data
);
2458 /* Now we have the actual streams */
2459 if (s
.nb_streams
!= feed
->nb_streams
) {
2460 av_freep(&s
.priv_data
);
2463 for (i
= 0; i
< s
.nb_streams
; i
++)
2464 memcpy(feed
->streams
[i
]->codec
,
2465 s
.streams
[i
]->codec
, sizeof(AVCodecContext
));
2466 av_freep(&s
.priv_data
);
2468 c
->buffer_ptr
= c
->buffer
;
2473 c
->stream
->feed_opened
= 0;
2478 /********************************************************************/
2481 static void rtsp_reply_header(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2488 switch(error_number
) {
2489 case RTSP_STATUS_OK
:
2492 case RTSP_STATUS_METHOD
:
2493 str
= "Method Not Allowed";
2495 case RTSP_STATUS_BANDWIDTH
:
2496 str
= "Not Enough Bandwidth";
2498 case RTSP_STATUS_SESSION
:
2499 str
= "Session Not Found";
2501 case RTSP_STATUS_STATE
:
2502 str
= "Method Not Valid in This State";
2504 case RTSP_STATUS_AGGREGATE
:
2505 str
= "Aggregate operation not allowed";
2507 case RTSP_STATUS_ONLY_AGGREGATE
:
2508 str
= "Only aggregate operation allowed";
2510 case RTSP_STATUS_TRANSPORT
:
2511 str
= "Unsupported transport";
2513 case RTSP_STATUS_INTERNAL
:
2514 str
= "Internal Server Error";
2516 case RTSP_STATUS_SERVICE
:
2517 str
= "Service Unavailable";
2519 case RTSP_STATUS_VERSION
:
2520 str
= "RTSP Version not supported";
2523 str
= "Unknown Error";
2527 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", error_number
, str
);
2528 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2530 /* output GMT time */
2534 p
= buf2
+ strlen(p
) - 1;
2537 url_fprintf(c
->pb
, "Date: %s GMT\r\n", buf2
);
2540 static void rtsp_reply_error(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2542 rtsp_reply_header(c
, error_number
);
2543 url_fprintf(c
->pb
, "\r\n");
2546 static int rtsp_parse_request(HTTPContext
*c
)
2548 const char *p
, *p1
, *p2
;
2554 RTSPHeader header1
, *header
= &header1
;
2556 c
->buffer_ptr
[0] = '\0';
2559 get_word(cmd
, sizeof(cmd
), &p
);
2560 get_word(url
, sizeof(url
), &p
);
2561 get_word(protocol
, sizeof(protocol
), &p
);
2563 av_strlcpy(c
->method
, cmd
, sizeof(c
->method
));
2564 av_strlcpy(c
->url
, url
, sizeof(c
->url
));
2565 av_strlcpy(c
->protocol
, protocol
, sizeof(c
->protocol
));
2567 if (url_open_dyn_buf(&c
->pb
) < 0) {
2568 /* XXX: cannot do more */
2569 c
->pb
= NULL
; /* safety */
2573 /* check version name */
2574 if (strcmp(protocol
, "RTSP/1.0") != 0) {
2575 rtsp_reply_error(c
, RTSP_STATUS_VERSION
);
2579 /* parse each header line */
2580 memset(header
, 0, sizeof(RTSPHeader
));
2581 /* skip to next line */
2582 while (*p
!= '\n' && *p
!= '\0')
2586 while (*p
!= '\0') {
2587 p1
= strchr(p
, '\n');
2591 if (p2
> p
&& p2
[-1] == '\r')
2593 /* skip empty line */
2597 if (len
> sizeof(line
) - 1)
2598 len
= sizeof(line
) - 1;
2599 memcpy(line
, p
, len
);
2601 rtsp_parse_line(header
, line
);
2605 /* handle sequence number */
2606 c
->seq
= header
->seq
;
2608 if (!strcmp(cmd
, "DESCRIBE"))
2609 rtsp_cmd_describe(c
, url
);
2610 else if (!strcmp(cmd
, "OPTIONS"))
2611 rtsp_cmd_options(c
, url
);
2612 else if (!strcmp(cmd
, "SETUP"))
2613 rtsp_cmd_setup(c
, url
, header
);
2614 else if (!strcmp(cmd
, "PLAY"))
2615 rtsp_cmd_play(c
, url
, header
);
2616 else if (!strcmp(cmd
, "PAUSE"))
2617 rtsp_cmd_pause(c
, url
, header
);
2618 else if (!strcmp(cmd
, "TEARDOWN"))
2619 rtsp_cmd_teardown(c
, url
, header
);
2621 rtsp_reply_error(c
, RTSP_STATUS_METHOD
);
2624 len
= url_close_dyn_buf(c
->pb
, &c
->pb_buffer
);
2625 c
->pb
= NULL
; /* safety */
2627 /* XXX: cannot do more */
2630 c
->buffer_ptr
= c
->pb_buffer
;
2631 c
->buffer_end
= c
->pb_buffer
+ len
;
2632 c
->state
= RTSPSTATE_SEND_REPLY
;
2636 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
2637 struct in_addr my_ip
)
2639 AVFormatContext
*avc
;
2640 AVStream avs
[MAX_STREAMS
];
2643 avc
= av_alloc_format_context();
2647 if (stream
->title
[0] != 0) {
2648 av_strlcpy(avc
->title
, stream
->title
, sizeof(avc
->title
));
2650 av_strlcpy(avc
->title
, "No Title", sizeof(avc
->title
));
2652 avc
->nb_streams
= stream
->nb_streams
;
2653 if (stream
->is_multicast
) {
2654 snprintf(avc
->filename
, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2655 inet_ntoa(stream
->multicast_ip
),
2656 stream
->multicast_port
, stream
->multicast_ttl
);
2659 for(i
= 0; i
< stream
->nb_streams
; i
++) {
2660 avc
->streams
[i
] = &avs
[i
];
2661 avc
->streams
[i
]->codec
= stream
->streams
[i
]->codec
;
2663 *pbuffer
= av_mallocz(2048);
2664 avf_sdp_create(&avc
, 1, *pbuffer
, 2048);
2667 return strlen(*pbuffer
);
2670 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
)
2672 // rtsp_reply_header(c, RTSP_STATUS_OK);
2673 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK
, "OK");
2674 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2675 url_fprintf(c
->pb
, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2676 url_fprintf(c
->pb
, "\r\n");
2679 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
)
2685 int content_length
, len
;
2686 struct sockaddr_in my_addr
;
2688 /* find which url is asked */
2689 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2694 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2695 if (!stream
->is_feed
&&
2696 stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp") &&
2697 !strcmp(path
, stream
->filename
)) {
2701 /* no stream found */
2702 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2706 /* prepare the media description in sdp format */
2708 /* get the host IP */
2709 len
= sizeof(my_addr
);
2710 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
2711 content_length
= prepare_sdp_description(stream
, &content
, my_addr
.sin_addr
);
2712 if (content_length
< 0) {
2713 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2716 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2717 url_fprintf(c
->pb
, "Content-Type: application/sdp\r\n");
2718 url_fprintf(c
->pb
, "Content-Length: %d\r\n", content_length
);
2719 url_fprintf(c
->pb
, "\r\n");
2720 put_buffer(c
->pb
, content
, content_length
);
2723 static HTTPContext
*find_rtp_session(const char *session_id
)
2727 if (session_id
[0] == '\0')
2730 for(c
= first_http_ctx
; c
!= NULL
; c
= c
->next
) {
2731 if (!strcmp(c
->session_id
, session_id
))
2737 static RTSPTransportField
*find_transport(RTSPHeader
*h
, enum RTSPProtocol protocol
)
2739 RTSPTransportField
*th
;
2742 for(i
=0;i
<h
->nb_transports
;i
++) {
2743 th
= &h
->transports
[i
];
2744 if (th
->protocol
== protocol
)
2750 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
,
2754 int stream_index
, port
;
2759 RTSPTransportField
*th
;
2760 struct sockaddr_in dest_addr
;
2761 RTSPActionServerSetup setup
;
2763 /* find which url is asked */
2764 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2769 /* now check each stream */
2770 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2771 if (!stream
->is_feed
&&
2772 stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
2773 /* accept aggregate filenames only if single stream */
2774 if (!strcmp(path
, stream
->filename
)) {
2775 if (stream
->nb_streams
!= 1) {
2776 rtsp_reply_error(c
, RTSP_STATUS_AGGREGATE
);
2783 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
2785 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2786 stream
->filename
, stream_index
);
2787 if (!strcmp(path
, buf
))
2792 /* no stream found */
2793 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2797 /* generate session id if needed */
2798 if (h
->session_id
[0] == '\0')
2799 snprintf(h
->session_id
, sizeof(h
->session_id
), "%08x%08x",
2800 av_random(&random_state
), av_random(&random_state
));
2802 /* find rtp session, and create it if none found */
2803 rtp_c
= find_rtp_session(h
->session_id
);
2805 /* always prefer UDP */
2806 th
= find_transport(h
, RTSP_PROTOCOL_RTP_UDP
);
2808 th
= find_transport(h
, RTSP_PROTOCOL_RTP_TCP
);
2810 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2815 rtp_c
= rtp_new_connection(&c
->from_addr
, stream
, h
->session_id
,
2818 rtsp_reply_error(c
, RTSP_STATUS_BANDWIDTH
);
2822 /* open input stream */
2823 if (open_input_stream(rtp_c
, "") < 0) {
2824 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2829 /* test if stream is OK (test needed because several SETUP needs
2830 to be done for a given file) */
2831 if (rtp_c
->stream
!= stream
) {
2832 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
);
2836 /* test if stream is already set up */
2837 if (rtp_c
->rtp_ctx
[stream_index
]) {
2838 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2842 /* check transport */
2843 th
= find_transport(h
, rtp_c
->rtp_protocol
);
2844 if (!th
|| (th
->protocol
== RTSP_PROTOCOL_RTP_UDP
&&
2845 th
->client_port_min
<= 0)) {
2846 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2850 /* setup default options */
2851 setup
.transport_option
[0] = '\0';
2852 dest_addr
= rtp_c
->from_addr
;
2853 dest_addr
.sin_port
= htons(th
->client_port_min
);
2856 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, c
) < 0) {
2857 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2861 /* now everything is OK, so we can send the connection parameters */
2862 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2864 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2866 switch(rtp_c
->rtp_protocol
) {
2867 case RTSP_PROTOCOL_RTP_UDP
:
2868 port
= rtp_get_local_port(rtp_c
->rtp_handles
[stream_index
]);
2869 url_fprintf(c
->pb
, "Transport: RTP/AVP/UDP;unicast;"
2870 "client_port=%d-%d;server_port=%d-%d",
2871 th
->client_port_min
, th
->client_port_min
+ 1,
2874 case RTSP_PROTOCOL_RTP_TCP
:
2875 url_fprintf(c
->pb
, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2876 stream_index
* 2, stream_index
* 2 + 1);
2881 if (setup
.transport_option
[0] != '\0')
2882 url_fprintf(c
->pb
, ";%s", setup
.transport_option
);
2883 url_fprintf(c
->pb
, "\r\n");
2886 url_fprintf(c
->pb
, "\r\n");
2890 /* find an rtp connection by using the session ID. Check consistency
2892 static HTTPContext
*find_rtp_session_with_url(const char *url
,
2893 const char *session_id
)
2901 rtp_c
= find_rtp_session(session_id
);
2905 /* find which url is asked */
2906 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2910 if(!strcmp(path
, rtp_c
->stream
->filename
)) return rtp_c
;
2911 for(s
=0; s
<rtp_c
->stream
->nb_streams
; ++s
) {
2912 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2913 rtp_c
->stream
->filename
, s
);
2914 if(!strncmp(path
, buf
, sizeof(buf
))) {
2915 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2922 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2926 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2928 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
2932 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
2933 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
&&
2934 rtp_c
->state
!= HTTPSTATE_READY
) {
2935 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2940 /* XXX: seek in stream */
2941 if (h
->range_start
!= AV_NOPTS_VALUE
) {
2942 printf("range_start=%0.3f\n", (double)h
->range_start
/ AV_TIME_BASE
);
2943 av_seek_frame(rtp_c
->fmt_in
, -1, h
->range_start
);
2947 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
2949 /* now everything is OK, so we can send the connection parameters */
2950 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2952 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2953 url_fprintf(c
->pb
, "\r\n");
2956 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2960 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2962 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
2966 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
2967 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
) {
2968 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2972 rtp_c
->state
= HTTPSTATE_READY
;
2973 rtp_c
->first_pts
= AV_NOPTS_VALUE
;
2974 /* now everything is OK, so we can send the connection parameters */
2975 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2977 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2978 url_fprintf(c
->pb
, "\r\n");
2981 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2984 char session_id
[32];
2986 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2988 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
2992 av_strlcpy(session_id
, rtp_c
->session_id
, sizeof(session_id
));
2994 /* abort the session */
2995 close_connection(rtp_c
);
2997 /* now everything is OK, so we can send the connection parameters */
2998 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3000 url_fprintf(c
->pb
, "Session: %s\r\n", session_id
);
3001 url_fprintf(c
->pb
, "\r\n");
3005 /********************************************************************/
3008 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
3009 FFStream
*stream
, const char *session_id
,
3010 enum RTSPProtocol rtp_protocol
)
3012 HTTPContext
*c
= NULL
;
3013 const char *proto_str
;
3015 /* XXX: should output a warning page when coming
3016 close to the connection limit */
3017 if (nb_connections
>= nb_max_connections
)
3020 /* add a new connection */
3021 c
= av_mallocz(sizeof(HTTPContext
));
3026 c
->poll_entry
= NULL
;
3027 c
->from_addr
= *from_addr
;
3028 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
3029 c
->buffer
= av_malloc(c
->buffer_size
);
3034 av_strlcpy(c
->session_id
, session_id
, sizeof(c
->session_id
));
3035 c
->state
= HTTPSTATE_READY
;
3036 c
->is_packetized
= 1;
3037 c
->rtp_protocol
= rtp_protocol
;
3039 /* protocol is shown in statistics */
3040 switch(c
->rtp_protocol
) {
3041 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3042 proto_str
= "MCAST";
3044 case RTSP_PROTOCOL_RTP_UDP
:
3047 case RTSP_PROTOCOL_RTP_TCP
:
3054 av_strlcpy(c
->protocol
, "RTP/", sizeof(c
->protocol
));
3055 av_strlcat(c
->protocol
, proto_str
, sizeof(c
->protocol
));
3057 current_bandwidth
+= stream
->bandwidth
;
3059 c
->next
= first_http_ctx
;
3071 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3072 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3074 static int rtp_new_av_stream(HTTPContext
*c
,
3075 int stream_index
, struct sockaddr_in
*dest_addr
,
3076 HTTPContext
*rtsp_c
)
3078 AVFormatContext
*ctx
;
3084 int max_packet_size
;
3086 /* now we can open the relevant output stream */
3087 ctx
= av_alloc_format_context();
3090 ctx
->oformat
= guess_format("rtp", NULL
, NULL
);
3092 st
= av_mallocz(sizeof(AVStream
));
3095 st
->codec
= avcodec_alloc_context();
3096 ctx
->nb_streams
= 1;
3097 ctx
->streams
[0] = st
;
3099 if (!c
->stream
->feed
||
3100 c
->stream
->feed
== c
->stream
)
3101 memcpy(st
, c
->stream
->streams
[stream_index
], sizeof(AVStream
));
3104 c
->stream
->feed
->streams
[c
->stream
->feed_streams
[stream_index
]],
3106 st
->priv_data
= NULL
;
3108 /* build destination RTP address */
3109 ipaddr
= inet_ntoa(dest_addr
->sin_addr
);
3111 switch(c
->rtp_protocol
) {
3112 case RTSP_PROTOCOL_RTP_UDP
:
3113 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3116 /* XXX: also pass as parameter to function ? */
3117 if (c
->stream
->is_multicast
) {
3119 ttl
= c
->stream
->multicast_ttl
;
3122 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3123 "rtp://%s:%d?multicast=1&ttl=%d",
3124 ipaddr
, ntohs(dest_addr
->sin_port
), ttl
);
3126 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3127 "rtp://%s:%d", ipaddr
, ntohs(dest_addr
->sin_port
));
3130 if (url_open(&h
, ctx
->filename
, URL_WRONLY
) < 0)
3132 c
->rtp_handles
[stream_index
] = h
;
3133 max_packet_size
= url_get_max_packet_size(h
);
3135 case RTSP_PROTOCOL_RTP_TCP
:
3138 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
3144 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3145 ipaddr
, ntohs(dest_addr
->sin_port
),
3147 c
->stream
->filename
, stream_index
, c
->protocol
);
3149 /* normally, no packets should be output here, but the packet size may be checked */
3150 if (url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
) < 0) {
3151 /* XXX: close stream */
3154 av_set_parameters(ctx
, NULL
);
3155 if (av_write_header(ctx
) < 0) {
3162 url_close_dyn_buf(ctx
->pb
, &dummy_buf
);
3165 c
->rtp_ctx
[stream_index
] = ctx
;
3169 /********************************************************************/
3170 /* ffserver initialization */
3172 static AVStream
*add_av_stream1(FFStream
*stream
, AVCodecContext
*codec
)
3176 fst
= av_mallocz(sizeof(AVStream
));
3179 fst
->codec
= avcodec_alloc_context();
3180 fst
->priv_data
= av_mallocz(sizeof(FeedData
));
3181 memcpy(fst
->codec
, codec
, sizeof(AVCodecContext
));
3182 fst
->index
= stream
->nb_streams
;
3183 av_set_pts_info(fst
, 33, 1, 90000);
3184 stream
->streams
[stream
->nb_streams
++] = fst
;
3188 /* return the stream number in the feed */
3189 static int add_av_stream(FFStream
*feed
, AVStream
*st
)
3192 AVCodecContext
*av
, *av1
;
3196 for(i
=0;i
<feed
->nb_streams
;i
++) {
3197 st
= feed
->streams
[i
];
3199 if (av1
->codec_id
== av
->codec_id
&&
3200 av1
->codec_type
== av
->codec_type
&&
3201 av1
->bit_rate
== av
->bit_rate
) {
3203 switch(av
->codec_type
) {
3204 case CODEC_TYPE_AUDIO
:
3205 if (av1
->channels
== av
->channels
&&
3206 av1
->sample_rate
== av
->sample_rate
)
3209 case CODEC_TYPE_VIDEO
:
3210 if (av1
->width
== av
->width
&&
3211 av1
->height
== av
->height
&&
3212 av1
->time_base
.den
== av
->time_base
.den
&&
3213 av1
->time_base
.num
== av
->time_base
.num
&&
3214 av1
->gop_size
== av
->gop_size
)
3223 fst
= add_av_stream1(feed
, av
);
3226 return feed
->nb_streams
- 1;
3231 static void remove_stream(FFStream
*stream
)
3235 while (*ps
!= NULL
) {
3243 /* specific mpeg4 handling : we extract the raw parameters */
3244 static void extract_mpeg4_header(AVFormatContext
*infile
)
3246 int mpeg4_count
, i
, size
;
3252 for(i
=0;i
<infile
->nb_streams
;i
++) {
3253 st
= infile
->streams
[i
];
3254 if (st
->codec
->codec_id
== CODEC_ID_MPEG4
&&
3255 st
->codec
->extradata_size
== 0) {
3262 printf("MPEG4 without extra data: trying to find header in %s\n", infile
->filename
);
3263 while (mpeg4_count
> 0) {
3264 if (av_read_packet(infile
, &pkt
) < 0)
3266 st
= infile
->streams
[pkt
.stream_index
];
3267 if (st
->codec
->codec_id
== CODEC_ID_MPEG4
&&
3268 st
->codec
->extradata_size
== 0) {
3269 av_freep(&st
->codec
->extradata
);
3270 /* fill extradata with the header */
3271 /* XXX: we make hard suppositions here ! */
3273 while (p
< pkt
.data
+ pkt
.size
- 4) {
3274 /* stop when vop header is found */
3275 if (p
[0] == 0x00 && p
[1] == 0x00 &&
3276 p
[2] == 0x01 && p
[3] == 0xb6) {
3277 size
= p
- pkt
.data
;
3278 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3279 st
->codec
->extradata
= av_malloc(size
);
3280 st
->codec
->extradata_size
= size
;
3281 memcpy(st
->codec
->extradata
, pkt
.data
, size
);
3288 av_free_packet(&pkt
);
3292 /* compute the needed AVStream for each file */
3293 static void build_file_streams(void)
3295 FFStream
*stream
, *stream_next
;
3296 AVFormatContext
*infile
;
3299 /* gather all streams */
3300 for(stream
= first_stream
; stream
!= NULL
; stream
= stream_next
) {
3301 stream_next
= stream
->next
;
3302 if (stream
->stream_type
== STREAM_TYPE_LIVE
&&
3304 /* the stream comes from a file */
3305 /* try to open the file */
3307 stream
->ap_in
= av_mallocz(sizeof(AVFormatParameters
));
3308 if (stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
3309 /* specific case : if transport stream output to RTP,
3310 we use a raw transport stream reader */
3311 stream
->ap_in
->mpeg2ts_raw
= 1;
3312 stream
->ap_in
->mpeg2ts_compute_pcr
= 1;
3315 if ((ret
= av_open_input_file(&infile
, stream
->feed_filename
,
3316 stream
->ifmt
, 0, stream
->ap_in
)) < 0) {
3317 http_log("could not open %s: %d\n", stream
->feed_filename
, ret
);
3318 /* remove stream (no need to spend more time on it) */
3320 remove_stream(stream
);
3322 /* find all the AVStreams inside and reference them in
3324 if (av_find_stream_info(infile
) < 0) {
3325 http_log("Could not find codec parameters from '%s'",
3326 stream
->feed_filename
);
3327 av_close_input_file(infile
);
3330 extract_mpeg4_header(infile
);
3332 for(i
=0;i
<infile
->nb_streams
;i
++)
3333 add_av_stream1(stream
, infile
->streams
[i
]->codec
);
3335 av_close_input_file(infile
);
3341 /* compute the needed AVStream for each feed */
3342 static void build_feed_streams(void)
3344 FFStream
*stream
, *feed
;
3347 /* gather all streams */
3348 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3349 feed
= stream
->feed
;
3351 if (!stream
->is_feed
) {
3352 /* we handle a stream coming from a feed */
3353 for(i
=0;i
<stream
->nb_streams
;i
++)
3354 stream
->feed_streams
[i
] = add_av_stream(feed
, stream
->streams
[i
]);
3359 /* gather all streams */
3360 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3361 feed
= stream
->feed
;
3363 if (stream
->is_feed
) {
3364 for(i
=0;i
<stream
->nb_streams
;i
++)
3365 stream
->feed_streams
[i
] = i
;
3370 /* create feed files if needed */
3371 for(feed
= first_feed
; feed
!= NULL
; feed
= feed
->next_feed
) {
3374 if (url_exist(feed
->feed_filename
)) {
3375 /* See if it matches */
3379 if (av_open_input_file(&s
, feed
->feed_filename
, NULL
, FFM_PACKET_SIZE
, NULL
) >= 0) {
3380 /* Now see if it matches */
3381 if (s
->nb_streams
== feed
->nb_streams
) {
3383 for(i
=0;i
<s
->nb_streams
;i
++) {
3385 sf
= feed
->streams
[i
];
3388 if (sf
->index
!= ss
->index
||
3390 printf("Index & Id do not match for stream %d (%s)\n",
3391 i
, feed
->feed_filename
);
3394 AVCodecContext
*ccf
, *ccs
;
3398 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3400 if (CHECK_CODEC(codec
) || CHECK_CODEC(codec_type
)) {
3401 printf("Codecs do not match for stream %d\n", i
);
3403 } else if (CHECK_CODEC(bit_rate
) || CHECK_CODEC(flags
)) {
3404 printf("Codec bitrates do not match for stream %d\n", i
);
3406 } else if (ccf
->codec_type
== CODEC_TYPE_VIDEO
) {
3407 if (CHECK_CODEC(time_base
.den
) ||
3408 CHECK_CODEC(time_base
.num
) ||
3409 CHECK_CODEC(width
) ||
3410 CHECK_CODEC(height
)) {
3411 printf("Codec width, height and framerate do not match for stream %d\n", i
);
3414 } else if (ccf
->codec_type
== CODEC_TYPE_AUDIO
) {
3415 if (CHECK_CODEC(sample_rate
) ||
3416 CHECK_CODEC(channels
) ||
3417 CHECK_CODEC(frame_size
)) {
3418 printf("Codec sample_rate, channels, frame_size do not match for stream %d\n", i
);
3422 printf("Unknown codec type\n");
3430 printf("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3431 feed
->feed_filename
, s
->nb_streams
, feed
->nb_streams
);
3433 av_close_input_file(s
);
3435 printf("Deleting feed file '%s' as it appears to be corrupt\n",
3436 feed
->feed_filename
);
3439 if (feed
->readonly
) {
3440 printf("Unable to delete feed file '%s' as it is marked readonly\n",
3441 feed
->feed_filename
);
3444 unlink(feed
->feed_filename
);
3447 if (!url_exist(feed
->feed_filename
)) {
3448 AVFormatContext s1
, *s
= &s1
;
3450 if (feed
->readonly
) {
3451 printf("Unable to create feed file '%s' as it is marked readonly\n",
3452 feed
->feed_filename
);
3456 /* only write the header of the ffm file */
3457 if (url_fopen(&s
->pb
, feed
->feed_filename
, URL_WRONLY
) < 0) {
3458 fprintf(stderr
, "Could not open output feed file '%s'\n",
3459 feed
->feed_filename
);
3462 s
->oformat
= feed
->fmt
;
3463 s
->nb_streams
= feed
->nb_streams
;
3464 for(i
=0;i
<s
->nb_streams
;i
++) {
3466 st
= feed
->streams
[i
];
3469 av_set_parameters(s
, NULL
);
3470 if (av_write_header(s
) < 0) {
3471 fprintf(stderr
, "Container doesn't supports the required parameters\n");
3474 /* XXX: need better api */
3475 av_freep(&s
->priv_data
);
3478 /* get feed size and write index */
3479 fd
= open(feed
->feed_filename
, O_RDONLY
);
3481 fprintf(stderr
, "Could not open output feed file '%s'\n",
3482 feed
->feed_filename
);
3486 feed
->feed_write_index
= ffm_read_write_index(fd
);
3487 feed
->feed_size
= lseek(fd
, 0, SEEK_END
);
3488 /* ensure that we do not wrap before the end of file */
3489 if (feed
->feed_max_size
&& feed
->feed_max_size
< feed
->feed_size
)
3490 feed
->feed_max_size
= feed
->feed_size
;
3496 /* compute the bandwidth used by each stream */
3497 static void compute_bandwidth(void)
3502 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3504 for(i
=0;i
<stream
->nb_streams
;i
++) {
3505 AVStream
*st
= stream
->streams
[i
];
3506 switch(st
->codec
->codec_type
) {
3507 case CODEC_TYPE_AUDIO
:
3508 case CODEC_TYPE_VIDEO
:
3509 bandwidth
+= st
->codec
->bit_rate
;
3515 stream
->bandwidth
= (bandwidth
+ 999) / 1000;
3519 static void get_arg(char *buf
, int buf_size
, const char **pp
)
3526 while (isspace(*p
)) p
++;
3529 if (*p
== '\"' || *p
== '\'')
3541 if ((q
- buf
) < buf_size
- 1)
3546 if (quote
&& *p
== quote
)
3551 /* add a codec and set the default parameters */
3552 static void add_codec(FFStream
*stream
, AVCodecContext
*av
)
3556 /* compute default parameters */
3557 switch(av
->codec_type
) {
3558 case CODEC_TYPE_AUDIO
:
3559 if (av
->bit_rate
== 0)
3560 av
->bit_rate
= 64000;
3561 if (av
->sample_rate
== 0)
3562 av
->sample_rate
= 22050;
3563 if (av
->channels
== 0)
3566 case CODEC_TYPE_VIDEO
:
3567 if (av
->bit_rate
== 0)
3568 av
->bit_rate
= 64000;
3569 if (av
->time_base
.num
== 0){
3570 av
->time_base
.den
= 5;
3571 av
->time_base
.num
= 1;
3573 if (av
->width
== 0 || av
->height
== 0) {
3577 /* Bitrate tolerance is less for streaming */
3578 if (av
->bit_rate_tolerance
== 0)
3579 av
->bit_rate_tolerance
= FFMAX(av
->bit_rate
/ 4,
3580 (int64_t)av
->bit_rate
*av
->time_base
.num
/av
->time_base
.den
);
3585 if (av
->max_qdiff
== 0)
3587 av
->qcompress
= 0.5;
3590 if (!av
->nsse_weight
)
3591 av
->nsse_weight
= 8;
3593 av
->frame_skip_cmp
= FF_CMP_DCTMAX
;
3594 av
->me_method
= ME_EPZS
;
3595 av
->rc_buffer_aggressivity
= 1.0;
3598 av
->rc_eq
= "tex^qComp";
3599 if (!av
->i_quant_factor
)
3600 av
->i_quant_factor
= -0.8;
3601 if (!av
->b_quant_factor
)
3602 av
->b_quant_factor
= 1.25;
3603 if (!av
->b_quant_offset
)
3604 av
->b_quant_offset
= 1.25;
3605 if (!av
->rc_max_rate
)
3606 av
->rc_max_rate
= av
->bit_rate
* 2;
3608 if (av
->rc_max_rate
&& !av
->rc_buffer_size
) {
3609 av
->rc_buffer_size
= av
->rc_max_rate
;
3618 st
= av_mallocz(sizeof(AVStream
));
3621 st
->codec
= avcodec_alloc_context();
3622 stream
->streams
[stream
->nb_streams
++] = st
;
3623 memcpy(st
->codec
, av
, sizeof(AVCodecContext
));
3626 static int opt_audio_codec(const char *arg
)
3628 AVCodec
*p
= avcodec_find_encoder_by_name(arg
);
3630 if (p
== NULL
|| p
->type
!= CODEC_TYPE_AUDIO
)
3631 return CODEC_ID_NONE
;
3636 static int opt_video_codec(const char *arg
)
3638 AVCodec
*p
= avcodec_find_encoder_by_name(arg
);
3640 if (p
== NULL
|| p
->type
!= CODEC_TYPE_VIDEO
)
3641 return CODEC_ID_NONE
;
3646 /* simplistic plugin support */
3649 static void load_module(const char *filename
)
3652 void (*init_func
)(void);
3653 dll
= dlopen(filename
, RTLD_NOW
);
3655 fprintf(stderr
, "Could not load module '%s' - %s\n",
3656 filename
, dlerror());
3660 init_func
= dlsym(dll
, "ffserver_module_init");
3663 "%s: init function 'ffserver_module_init()' not found\n",
3672 static int parse_ffconfig(const char *filename
)
3679 int val
, errors
, line_num
;
3680 FFStream
**last_stream
, *stream
, *redirect
;
3681 FFStream
**last_feed
, *feed
;
3682 AVCodecContext audio_enc
, video_enc
;
3683 int audio_id
, video_id
;
3685 f
= fopen(filename
, "r");
3693 first_stream
= NULL
;
3694 last_stream
= &first_stream
;
3696 last_feed
= &first_feed
;
3700 audio_id
= CODEC_ID_NONE
;
3701 video_id
= CODEC_ID_NONE
;
3703 if (fgets(line
, sizeof(line
), f
) == NULL
)
3709 if (*p
== '\0' || *p
== '#')
3712 get_arg(cmd
, sizeof(cmd
), &p
);
3714 if (!strcasecmp(cmd
, "Port")) {
3715 get_arg(arg
, sizeof(arg
), &p
);
3717 if (val
< 1 || val
> 65536) {
3718 fprintf(stderr
, "%s:%d: Invalid port: %s\n",
3719 filename
, line_num
, arg
);
3722 my_http_addr
.sin_port
= htons(val
);
3723 } else if (!strcasecmp(cmd
, "BindAddress")) {
3724 get_arg(arg
, sizeof(arg
), &p
);
3725 if (resolve_host(&my_http_addr
.sin_addr
, arg
) != 0) {
3726 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
3727 filename
, line_num
, arg
);
3730 } else if (!strcasecmp(cmd
, "NoDaemon")) {
3731 ffserver_daemon
= 0;
3732 } else if (!strcasecmp(cmd
, "RTSPPort")) {
3733 get_arg(arg
, sizeof(arg
), &p
);
3735 if (val
< 1 || val
> 65536) {
3736 fprintf(stderr
, "%s:%d: Invalid port: %s\n",
3737 filename
, line_num
, arg
);
3740 my_rtsp_addr
.sin_port
= htons(atoi(arg
));
3741 } else if (!strcasecmp(cmd
, "RTSPBindAddress")) {
3742 get_arg(arg
, sizeof(arg
), &p
);
3743 if (resolve_host(&my_rtsp_addr
.sin_addr
, arg
) != 0) {
3744 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
3745 filename
, line_num
, arg
);
3748 } else if (!strcasecmp(cmd
, "MaxClients")) {
3749 get_arg(arg
, sizeof(arg
), &p
);
3751 if (val
< 1 || val
> HTTP_MAX_CONNECTIONS
) {
3752 fprintf(stderr
, "%s:%d: Invalid MaxClients: %s\n",
3753 filename
, line_num
, arg
);
3756 nb_max_connections
= val
;
3758 } else if (!strcasecmp(cmd
, "MaxBandwidth")) {
3759 get_arg(arg
, sizeof(arg
), &p
);
3761 if (val
< 10 || val
> 100000) {
3762 fprintf(stderr
, "%s:%d: Invalid MaxBandwidth: %s\n",
3763 filename
, line_num
, arg
);
3766 max_bandwidth
= val
;
3767 } else if (!strcasecmp(cmd
, "CustomLog")) {
3768 get_arg(logfilename
, sizeof(logfilename
), &p
);
3769 } else if (!strcasecmp(cmd
, "<Feed")) {
3770 /*********************************************/
3771 /* Feed related options */
3773 if (stream
|| feed
) {
3774 fprintf(stderr
, "%s:%d: Already in a tag\n",
3775 filename
, line_num
);
3777 feed
= av_mallocz(sizeof(FFStream
));
3778 /* add in stream list */
3779 *last_stream
= feed
;
3780 last_stream
= &feed
->next
;
3781 /* add in feed list */
3783 last_feed
= &feed
->next_feed
;
3785 get_arg(feed
->filename
, sizeof(feed
->filename
), &p
);
3786 q
= strrchr(feed
->filename
, '>');
3789 feed
->fmt
= guess_format("ffm", NULL
, NULL
);
3790 /* defaut feed file */
3791 snprintf(feed
->feed_filename
, sizeof(feed
->feed_filename
),
3792 "/tmp/%s.ffm", feed
->filename
);
3793 feed
->feed_max_size
= 5 * 1024 * 1024;
3795 feed
->feed
= feed
; /* self feeding :-) */
3797 } else if (!strcasecmp(cmd
, "Launch")) {
3801 feed
->child_argv
= av_mallocz(64 * sizeof(char *));
3803 for (i
= 0; i
< 62; i
++) {
3804 get_arg(arg
, sizeof(arg
), &p
);
3808 feed
->child_argv
[i
] = av_strdup(arg
);
3811 feed
->child_argv
[i
] = av_malloc(30 + strlen(feed
->filename
));
3813 snprintf(feed
->child_argv
[i
], 30+strlen(feed
->filename
),
3815 (my_http_addr
.sin_addr
.s_addr
== INADDR_ANY
) ? "127.0.0.1" :
3816 inet_ntoa(my_http_addr
.sin_addr
),
3817 ntohs(my_http_addr
.sin_port
), feed
->filename
);
3822 fprintf(stdout
, "Launch commandline: ");
3823 for (j
= 0; j
<= i
; j
++)
3824 fprintf(stdout
, "%s ", feed
->child_argv
[j
]);
3825 fprintf(stdout
, "\n");
3828 } else if (!strcasecmp(cmd
, "ReadOnlyFile")) {
3830 get_arg(feed
->feed_filename
, sizeof(feed
->feed_filename
), &p
);
3832 } else if (stream
) {
3833 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3835 } else if (!strcasecmp(cmd
, "File")) {
3837 get_arg(feed
->feed_filename
, sizeof(feed
->feed_filename
), &p
);
3839 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3840 } else if (!strcasecmp(cmd
, "FileMaxSize")) {
3845 get_arg(arg
, sizeof(arg
), &p
);
3847 fsize
= strtod(p1
, &p1
);
3848 switch(toupper(*p1
)) {
3853 fsize
*= 1024 * 1024;
3856 fsize
*= 1024 * 1024 * 1024;
3859 feed
->feed_max_size
= (int64_t)fsize
;
3861 } else if (!strcasecmp(cmd
, "</Feed>")) {
3863 fprintf(stderr
, "%s:%d: No corresponding <Feed> for </Feed>\n",
3864 filename
, line_num
);
3868 } else if (!strcasecmp(cmd
, "<Stream")) {
3869 /*********************************************/
3870 /* Stream related options */
3872 if (stream
|| feed
) {
3873 fprintf(stderr
, "%s:%d: Already in a tag\n",
3874 filename
, line_num
);
3876 stream
= av_mallocz(sizeof(FFStream
));
3877 *last_stream
= stream
;
3878 last_stream
= &stream
->next
;
3880 get_arg(stream
->filename
, sizeof(stream
->filename
), &p
);
3881 q
= strrchr(stream
->filename
, '>');
3884 stream
->fmt
= guess_stream_format(NULL
, stream
->filename
, NULL
);
3885 memset(&audio_enc
, 0, sizeof(AVCodecContext
));
3886 memset(&video_enc
, 0, sizeof(AVCodecContext
));
3887 audio_id
= CODEC_ID_NONE
;
3888 video_id
= CODEC_ID_NONE
;
3890 audio_id
= stream
->fmt
->audio_codec
;
3891 video_id
= stream
->fmt
->video_codec
;
3894 } else if (!strcasecmp(cmd
, "Feed")) {
3895 get_arg(arg
, sizeof(arg
), &p
);
3900 while (sfeed
!= NULL
) {
3901 if (!strcmp(sfeed
->filename
, arg
))
3903 sfeed
= sfeed
->next_feed
;
3906 fprintf(stderr
, "%s:%d: feed '%s' not defined\n",
3907 filename
, line_num
, arg
);
3909 stream
->feed
= sfeed
;
3911 } else if (!strcasecmp(cmd
, "Format")) {
3912 get_arg(arg
, sizeof(arg
), &p
);
3913 if (!strcmp(arg
, "status")) {
3914 stream
->stream_type
= STREAM_TYPE_STATUS
;
3917 stream
->stream_type
= STREAM_TYPE_LIVE
;
3918 /* jpeg cannot be used here, so use single frame jpeg */
3919 if (!strcmp(arg
, "jpeg"))
3920 strcpy(arg
, "mjpeg");
3921 stream
->fmt
= guess_stream_format(arg
, NULL
, NULL
);
3923 fprintf(stderr
, "%s:%d: Unknown Format: %s\n",
3924 filename
, line_num
, arg
);
3929 audio_id
= stream
->fmt
->audio_codec
;
3930 video_id
= stream
->fmt
->video_codec
;
3932 } else if (!strcasecmp(cmd
, "InputFormat")) {
3933 get_arg(arg
, sizeof(arg
), &p
);
3934 stream
->ifmt
= av_find_input_format(arg
);
3935 if (!stream
->ifmt
) {
3936 fprintf(stderr
, "%s:%d: Unknown input format: %s\n",
3937 filename
, line_num
, arg
);
3939 } else if (!strcasecmp(cmd
, "FaviconURL")) {
3940 if (stream
&& stream
->stream_type
== STREAM_TYPE_STATUS
) {
3941 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3943 fprintf(stderr
, "%s:%d: FaviconURL only permitted for status streams\n",
3944 filename
, line_num
);
3947 } else if (!strcasecmp(cmd
, "Author")) {
3949 get_arg(stream
->author
, sizeof(stream
->author
), &p
);
3950 } else if (!strcasecmp(cmd
, "Comment")) {
3952 get_arg(stream
->comment
, sizeof(stream
->comment
), &p
);
3953 } else if (!strcasecmp(cmd
, "Copyright")) {
3955 get_arg(stream
->copyright
, sizeof(stream
->copyright
), &p
);
3956 } else if (!strcasecmp(cmd
, "Title")) {
3958 get_arg(stream
->title
, sizeof(stream
->title
), &p
);
3959 } else if (!strcasecmp(cmd
, "Preroll")) {
3960 get_arg(arg
, sizeof(arg
), &p
);
3962 stream
->prebuffer
= atof(arg
) * 1000;
3963 } else if (!strcasecmp(cmd
, "StartSendOnKey")) {
3965 stream
->send_on_key
= 1;
3966 } else if (!strcasecmp(cmd
, "AudioCodec")) {
3967 get_arg(arg
, sizeof(arg
), &p
);
3968 audio_id
= opt_audio_codec(arg
);
3969 if (audio_id
== CODEC_ID_NONE
) {
3970 fprintf(stderr
, "%s:%d: Unknown AudioCodec: %s\n",
3971 filename
, line_num
, arg
);
3974 } else if (!strcasecmp(cmd
, "VideoCodec")) {
3975 get_arg(arg
, sizeof(arg
), &p
);
3976 video_id
= opt_video_codec(arg
);
3977 if (video_id
== CODEC_ID_NONE
) {
3978 fprintf(stderr
, "%s:%d: Unknown VideoCodec: %s\n",
3979 filename
, line_num
, arg
);
3982 } else if (!strcasecmp(cmd
, "MaxTime")) {
3983 get_arg(arg
, sizeof(arg
), &p
);
3985 stream
->max_time
= atof(arg
) * 1000;
3986 } else if (!strcasecmp(cmd
, "AudioBitRate")) {
3987 get_arg(arg
, sizeof(arg
), &p
);
3989 audio_enc
.bit_rate
= atoi(arg
) * 1000;
3990 } else if (!strcasecmp(cmd
, "AudioChannels")) {
3991 get_arg(arg
, sizeof(arg
), &p
);
3993 audio_enc
.channels
= atoi(arg
);
3994 } else if (!strcasecmp(cmd
, "AudioSampleRate")) {
3995 get_arg(arg
, sizeof(arg
), &p
);
3997 audio_enc
.sample_rate
= atoi(arg
);
3998 } else if (!strcasecmp(cmd
, "AudioQuality")) {
3999 get_arg(arg
, sizeof(arg
), &p
);
4001 // audio_enc.quality = atof(arg) * 1000;
4003 } else if (!strcasecmp(cmd
, "VideoBitRateRange")) {
4005 int minrate
, maxrate
;
4007 get_arg(arg
, sizeof(arg
), &p
);
4009 if (sscanf(arg
, "%d-%d", &minrate
, &maxrate
) == 2) {
4010 video_enc
.rc_min_rate
= minrate
* 1000;
4011 video_enc
.rc_max_rate
= maxrate
* 1000;
4013 fprintf(stderr
, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4014 filename
, line_num
, arg
);
4018 } else if (!strcasecmp(cmd
, "Debug")) {
4020 get_arg(arg
, sizeof(arg
), &p
);
4021 video_enc
.debug
= strtol(arg
,0,0);
4023 } else if (!strcasecmp(cmd
, "Strict")) {
4025 get_arg(arg
, sizeof(arg
), &p
);
4026 video_enc
.strict_std_compliance
= atoi(arg
);
4028 } else if (!strcasecmp(cmd
, "VideoBufferSize")) {
4030 get_arg(arg
, sizeof(arg
), &p
);
4031 video_enc
.rc_buffer_size
= atoi(arg
) * 8*1024;
4033 } else if (!strcasecmp(cmd
, "VideoBitRateTolerance")) {
4035 get_arg(arg
, sizeof(arg
), &p
);
4036 video_enc
.bit_rate_tolerance
= atoi(arg
) * 1000;
4038 } else if (!strcasecmp(cmd
, "VideoBitRate")) {
4039 get_arg(arg
, sizeof(arg
), &p
);
4041 video_enc
.bit_rate
= atoi(arg
) * 1000;
4043 } else if (!strcasecmp(cmd
, "VideoSize")) {
4044 get_arg(arg
, sizeof(arg
), &p
);
4046 av_parse_video_frame_size(&video_enc
.width
, &video_enc
.height
, arg
);
4047 if ((video_enc
.width
% 16) != 0 ||
4048 (video_enc
.height
% 16) != 0) {
4049 fprintf(stderr
, "%s:%d: Image size must be a multiple of 16\n",
4050 filename
, line_num
);
4054 } else if (!strcasecmp(cmd
, "VideoFrameRate")) {
4055 get_arg(arg
, sizeof(arg
), &p
);
4057 video_enc
.time_base
.num
= DEFAULT_FRAME_RATE_BASE
;
4058 video_enc
.time_base
.den
= (int)(strtod(arg
, NULL
) * video_enc
.time_base
.num
);
4060 } else if (!strcasecmp(cmd
, "VideoGopSize")) {
4061 get_arg(arg
, sizeof(arg
), &p
);
4063 video_enc
.gop_size
= atoi(arg
);
4064 } else if (!strcasecmp(cmd
, "VideoIntraOnly")) {
4066 video_enc
.gop_size
= 1;
4067 } else if (!strcasecmp(cmd
, "VideoHighQuality")) {
4069 video_enc
.mb_decision
= FF_MB_DECISION_BITS
;
4070 } else if (!strcasecmp(cmd
, "Video4MotionVector")) {
4072 video_enc
.mb_decision
= FF_MB_DECISION_BITS
; //FIXME remove
4073 video_enc
.flags
|= CODEC_FLAG_4MV
;
4075 } else if (!strcasecmp(cmd
, "VideoTag")) {
4076 get_arg(arg
, sizeof(arg
), &p
);
4077 if ((strlen(arg
) == 4) && stream
)
4078 video_enc
.codec_tag
= ff_get_fourcc(arg
);
4079 } else if (!strcasecmp(cmd
, "BitExact")) {
4081 video_enc
.flags
|= CODEC_FLAG_BITEXACT
;
4082 } else if (!strcasecmp(cmd
, "DctFastint")) {
4084 video_enc
.dct_algo
= FF_DCT_FASTINT
;
4085 } else if (!strcasecmp(cmd
, "IdctSimple")) {
4087 video_enc
.idct_algo
= FF_IDCT_SIMPLE
;
4088 } else if (!strcasecmp(cmd
, "Qscale")) {
4089 get_arg(arg
, sizeof(arg
), &p
);
4091 video_enc
.flags
|= CODEC_FLAG_QSCALE
;
4092 video_enc
.global_quality
= FF_QP2LAMBDA
* atoi(arg
);
4094 } else if (!strcasecmp(cmd
, "VideoQDiff")) {
4095 get_arg(arg
, sizeof(arg
), &p
);
4097 video_enc
.max_qdiff
= atoi(arg
);
4098 if (video_enc
.max_qdiff
< 1 || video_enc
.max_qdiff
> 31) {
4099 fprintf(stderr
, "%s:%d: VideoQDiff out of range\n",
4100 filename
, line_num
);
4104 } else if (!strcasecmp(cmd
, "VideoQMax")) {
4105 get_arg(arg
, sizeof(arg
), &p
);
4107 video_enc
.qmax
= atoi(arg
);
4108 if (video_enc
.qmax
< 1 || video_enc
.qmax
> 31) {
4109 fprintf(stderr
, "%s:%d: VideoQMax out of range\n",
4110 filename
, line_num
);
4114 } else if (!strcasecmp(cmd
, "VideoQMin")) {
4115 get_arg(arg
, sizeof(arg
), &p
);
4117 video_enc
.qmin
= atoi(arg
);
4118 if (video_enc
.qmin
< 1 || video_enc
.qmin
> 31) {
4119 fprintf(stderr
, "%s:%d: VideoQMin out of range\n",
4120 filename
, line_num
);
4124 } else if (!strcasecmp(cmd
, "LumaElim")) {
4125 get_arg(arg
, sizeof(arg
), &p
);
4127 video_enc
.luma_elim_threshold
= atoi(arg
);
4128 } else if (!strcasecmp(cmd
, "ChromaElim")) {
4129 get_arg(arg
, sizeof(arg
), &p
);
4131 video_enc
.chroma_elim_threshold
= atoi(arg
);
4132 } else if (!strcasecmp(cmd
, "LumiMask")) {
4133 get_arg(arg
, sizeof(arg
), &p
);
4135 video_enc
.lumi_masking
= atof(arg
);
4136 } else if (!strcasecmp(cmd
, "DarkMask")) {
4137 get_arg(arg
, sizeof(arg
), &p
);
4139 video_enc
.dark_masking
= atof(arg
);
4140 } else if (!strcasecmp(cmd
, "NoVideo")) {
4141 video_id
= CODEC_ID_NONE
;
4142 } else if (!strcasecmp(cmd
, "NoAudio")) {
4143 audio_id
= CODEC_ID_NONE
;
4144 } else if (!strcasecmp(cmd
, "ACL")) {
4147 get_arg(arg
, sizeof(arg
), &p
);
4148 if (strcasecmp(arg
, "allow") == 0)
4149 acl
.action
= IP_ALLOW
;
4150 else if (strcasecmp(arg
, "deny") == 0)
4151 acl
.action
= IP_DENY
;
4153 fprintf(stderr
, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4154 filename
, line_num
, arg
);
4158 get_arg(arg
, sizeof(arg
), &p
);
4160 if (resolve_host(&acl
.first
, arg
) != 0) {
4161 fprintf(stderr
, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4162 filename
, line_num
, arg
);
4165 acl
.last
= acl
.first
;
4167 get_arg(arg
, sizeof(arg
), &p
);
4170 if (resolve_host(&acl
.last
, arg
) != 0) {
4171 fprintf(stderr
, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4172 filename
, line_num
, arg
);
4178 IPAddressACL
*nacl
= av_mallocz(sizeof(*nacl
));
4179 IPAddressACL
**naclp
= 0;
4185 naclp
= &stream
->acl
;
4189 fprintf(stderr
, "%s:%d: ACL found not in <stream> or <feed>\n",
4190 filename
, line_num
);
4196 naclp
= &(*naclp
)->next
;
4201 } else if (!strcasecmp(cmd
, "RTSPOption")) {
4202 get_arg(arg
, sizeof(arg
), &p
);
4204 av_freep(&stream
->rtsp_option
);
4205 stream
->rtsp_option
= av_strdup(arg
);
4207 } else if (!strcasecmp(cmd
, "MulticastAddress")) {
4208 get_arg(arg
, sizeof(arg
), &p
);
4210 if (resolve_host(&stream
->multicast_ip
, arg
) != 0) {
4211 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
4212 filename
, line_num
, arg
);
4215 stream
->is_multicast
= 1;
4216 stream
->loop
= 1; /* default is looping */
4218 } else if (!strcasecmp(cmd
, "MulticastPort")) {
4219 get_arg(arg
, sizeof(arg
), &p
);
4221 stream
->multicast_port
= atoi(arg
);
4222 } else if (!strcasecmp(cmd
, "MulticastTTL")) {
4223 get_arg(arg
, sizeof(arg
), &p
);
4225 stream
->multicast_ttl
= atoi(arg
);
4226 } else if (!strcasecmp(cmd
, "NoLoop")) {
4229 } else if (!strcasecmp(cmd
, "</Stream>")) {
4231 fprintf(stderr
, "%s:%d: No corresponding <Stream> for </Stream>\n",
4232 filename
, line_num
);
4235 if (stream
->feed
&& stream
->fmt
&& strcmp(stream
->fmt
->name
, "ffm") != 0) {
4236 if (audio_id
!= CODEC_ID_NONE
) {
4237 audio_enc
.codec_type
= CODEC_TYPE_AUDIO
;
4238 audio_enc
.codec_id
= audio_id
;
4239 add_codec(stream
, &audio_enc
);
4241 if (video_id
!= CODEC_ID_NONE
) {
4242 video_enc
.codec_type
= CODEC_TYPE_VIDEO
;
4243 video_enc
.codec_id
= video_id
;
4244 add_codec(stream
, &video_enc
);
4248 } else if (!strcasecmp(cmd
, "<Redirect")) {
4249 /*********************************************/
4251 if (stream
|| feed
|| redirect
) {
4252 fprintf(stderr
, "%s:%d: Already in a tag\n",
4253 filename
, line_num
);
4256 redirect
= av_mallocz(sizeof(FFStream
));
4257 *last_stream
= redirect
;
4258 last_stream
= &redirect
->next
;
4260 get_arg(redirect
->filename
, sizeof(redirect
->filename
), &p
);
4261 q
= strrchr(redirect
->filename
, '>');
4264 redirect
->stream_type
= STREAM_TYPE_REDIRECT
;
4266 } else if (!strcasecmp(cmd
, "URL")) {
4268 get_arg(redirect
->feed_filename
, sizeof(redirect
->feed_filename
), &p
);
4269 } else if (!strcasecmp(cmd
, "</Redirect>")) {
4271 fprintf(stderr
, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4272 filename
, line_num
);
4275 if (!redirect
->feed_filename
[0]) {
4276 fprintf(stderr
, "%s:%d: No URL found for <Redirect>\n",
4277 filename
, line_num
);
4281 } else if (!strcasecmp(cmd
, "LoadModule")) {
4282 get_arg(arg
, sizeof(arg
), &p
);
4286 fprintf(stderr
, "%s:%d: Module support not compiled into this version: '%s'\n",
4287 filename
, line_num
, arg
);
4291 fprintf(stderr
, "%s:%d: Incorrect keyword: '%s'\n",
4292 filename
, line_num
, cmd
);
4304 static void handle_child_exit(int sig
)
4309 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
4312 for (feed
= first_feed
; feed
; feed
= feed
->next
) {
4313 if (feed
->pid
== pid
) {
4314 int uptime
= time(0) - feed
->pid_start
;
4317 fprintf(stderr
, "%s: Pid %d exited with status %d after %d seconds\n", feed
->filename
, pid
, status
, uptime
);
4320 /* Turn off any more restarts */
4321 feed
->child_argv
= 0;
4326 need_to_start_children
= 1;
4329 static void opt_debug()
4332 ffserver_daemon
= 0;
4335 static void opt_show_help(void)
4337 printf("usage: ffserver [options]\n"
4338 "Hyper fast multi format Audio/Video streaming server\n");
4340 show_help_options(options
, "Main options:\n", 0, 0);
4343 static const OptionDef options
[] = {
4344 { "h", OPT_EXIT
, {(void*)opt_show_help
}, "show help" },
4345 { "version", OPT_EXIT
, {(void*)show_version
}, "show version" },
4346 { "L", OPT_EXIT
, {(void*)show_license
}, "show license" },
4347 { "formats", OPT_EXIT
, {(void*)show_formats
}, "show available formats, codecs, protocols, ..." },
4348 { "n", OPT_BOOL
, {(void *)&no_launch
}, "enable no-launch mode" },
4349 { "d", 0, {(void*)opt_debug
}, "enable debug mode" },
4350 { "f", HAS_ARG
| OPT_STRING
, {(void*)&config_filename
}, "use configfile instead of /etc/ffserver.conf", "configfile" },
4354 int main(int argc
, char **argv
)
4356 struct sigaction sigact
;
4360 show_banner(program_name
, program_birth_year
);
4362 config_filename
= "/etc/ffserver.conf";
4364 my_program_name
= argv
[0];
4365 my_program_dir
= getcwd(0, 0);
4366 ffserver_daemon
= 1;
4368 parse_options(argc
, argv
, options
, NULL
);
4370 putenv("http_proxy"); /* Kill the http_proxy */
4372 av_init_random(av_gettime() + (getpid() << 16), &random_state
);
4374 /* address on which the server will handle HTTP connections */
4375 my_http_addr
.sin_family
= AF_INET
;
4376 my_http_addr
.sin_port
= htons (8080);
4377 my_http_addr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
4379 /* address on which the server will handle RTSP connections */
4380 my_rtsp_addr
.sin_family
= AF_INET
;
4381 my_rtsp_addr
.sin_port
= htons (5454);
4382 my_rtsp_addr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
4384 nb_max_connections
= 5;
4385 max_bandwidth
= 1000;
4386 first_stream
= NULL
;
4387 logfilename
[0] = '\0';
4389 memset(&sigact
, 0, sizeof(sigact
));
4390 sigact
.sa_handler
= handle_child_exit
;
4391 sigact
.sa_flags
= SA_NOCLDSTOP
| SA_RESTART
;
4392 sigaction(SIGCHLD
, &sigact
, 0);
4394 if (parse_ffconfig(config_filename
) < 0) {
4395 fprintf(stderr
, "Incorrect config file - exiting.\n");
4399 build_file_streams();
4401 build_feed_streams();
4403 compute_bandwidth();
4405 /* put the process in background and detach it from its TTY */
4406 if (ffserver_daemon
) {
4413 } else if (pid
> 0) {
4421 open("/dev/null", O_RDWR
);
4422 if (strcmp(logfilename
, "-") != 0) {
4432 signal(SIGPIPE
, SIG_IGN
);
4434 /* open log file if needed */
4435 if (logfilename
[0] != '\0') {
4436 if (!strcmp(logfilename
, "-"))
4439 logfile
= fopen(logfilename
, "a");
4442 if (http_server() < 0) {
4443 fprintf(stderr
, "Could not start server\n");