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
31 #include "os_support.h"
36 #include <sys/ioctl.h>
37 #ifdef HAVE_SYS_POLL_H
42 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
59 static const char program_name
[] = "FFserver";
60 static const int program_birth_year
= 2000;
62 /* maximum number of simultaneous HTTP connections */
63 #define HTTP_MAX_CONNECTIONS 2000
66 HTTPSTATE_WAIT_REQUEST
,
67 HTTPSTATE_SEND_HEADER
,
68 HTTPSTATE_SEND_DATA_HEADER
,
69 HTTPSTATE_SEND_DATA
, /* sending TCP or UDP data */
70 HTTPSTATE_SEND_DATA_TRAILER
,
71 HTTPSTATE_RECEIVE_DATA
,
72 HTTPSTATE_WAIT_FEED
, /* wait for data from the feed */
75 RTSPSTATE_WAIT_REQUEST
,
77 RTSPSTATE_SEND_PACKET
,
80 const char *http_state
[] = {
96 #define IOBUFFER_INIT_SIZE 8192
98 /* timeouts are in ms */
99 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
100 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
102 #define SYNC_TIMEOUT (10 * 1000)
105 int64_t count1
, count2
;
106 int64_t time1
, time2
;
109 /* context associated with one connection */
110 typedef struct HTTPContext
{
111 enum HTTPState state
;
112 int fd
; /* socket file descriptor */
113 struct sockaddr_in from_addr
; /* origin */
114 struct pollfd
*poll_entry
; /* used when polling */
116 uint8_t *buffer_ptr
, *buffer_end
;
119 struct HTTPContext
*next
;
120 int got_key_frame
; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
124 /* input format handling */
125 AVFormatContext
*fmt_in
;
126 int64_t start_time
; /* In milliseconds - this wraps fairly often */
127 int64_t first_pts
; /* initial pts value */
128 int64_t cur_pts
; /* current pts value from the stream in us */
129 int64_t cur_frame_duration
; /* duration of the current frame in us */
130 int cur_frame_bytes
; /* output frame size, needed to compute
131 the time at which we send each
133 int pts_stream_index
; /* stream we choose as clock reference */
134 int64_t cur_clock
; /* current clock reference value in us */
135 /* output format handling */
136 struct FFStream
*stream
;
137 /* -1 is invalid stream */
138 int feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
139 int switch_feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
141 AVFormatContext fmt_ctx
; /* instance of FFStream for one user */
142 int last_packet_sent
; /* true if last data packet was sent */
144 DataRateData datarate
;
151 int is_packetized
; /* if true, the stream is packetized */
152 int packet_stream_index
; /* current stream for output in state machine */
154 /* RTSP state specific */
155 uint8_t *pb_buffer
; /* XXX: use that in all the code */
157 int seq
; /* RTSP sequence number */
159 /* RTP state specific */
160 enum RTSPProtocol rtp_protocol
;
161 char session_id
[32]; /* session id */
162 AVFormatContext
*rtp_ctx
[MAX_STREAMS
];
164 /* RTP/UDP specific */
165 URLContext
*rtp_handles
[MAX_STREAMS
];
167 /* RTP/TCP specific */
168 struct HTTPContext
*rtsp_c
;
169 uint8_t *packet_buffer
, *packet_buffer_ptr
, *packet_buffer_end
;
172 static AVFrame dummy_frame
;
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 int ffserver_debug
;
292 static int ffserver_daemon
;
293 static int no_launch
;
294 static int need_to_start_children
;
296 static int nb_max_connections
;
297 static int nb_connections
;
299 static int max_bandwidth
;
300 static int current_bandwidth
;
302 static int64_t cur_time
; // Making this global saves on passing it around everywhere
304 static AVRandomState random_state
;
306 static FILE *logfile
= NULL
;
308 static void __attribute__ ((format (printf
, 1, 2))) http_log(const char *fmt
, ...)
314 vfprintf(logfile
, fmt
, ap
);
320 static char *ctime1(char *buf2
)
328 p
= buf2
+ strlen(p
) - 1;
334 static void log_connection(HTTPContext
*c
)
341 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64
"\n",
342 inet_ntoa(c
->from_addr
.sin_addr
),
343 ctime1(buf2
), c
->method
, c
->url
,
344 c
->protocol
, (c
->http_error
? c
->http_error
: 200), c
->data_count
);
347 static void update_datarate(DataRateData
*drd
, int64_t count
)
349 if (!drd
->time1
&& !drd
->count1
) {
350 drd
->time1
= drd
->time2
= cur_time
;
351 drd
->count1
= drd
->count2
= count
;
352 } else if (cur_time
- drd
->time2
> 5000) {
353 drd
->time1
= drd
->time2
;
354 drd
->count1
= drd
->count2
;
355 drd
->time2
= cur_time
;
360 /* In bytes per second */
361 static int compute_datarate(DataRateData
*drd
, int64_t count
)
363 if (cur_time
== drd
->time1
)
366 return ((count
- drd
->count1
) * 1000) / (cur_time
- drd
->time1
);
370 static void start_children(FFStream
*feed
)
375 for (; feed
; feed
= feed
->next
) {
376 if (feed
->child_argv
&& !feed
->pid
) {
377 feed
->pid_start
= time(0);
382 fprintf(stderr
, "Unable to create children\n");
391 for (i
= 3; i
< 256; i
++)
394 if (!ffserver_debug
) {
395 i
= open("/dev/null", O_RDWR
);
404 av_strlcpy(pathname
, my_program_name
, sizeof(pathname
));
406 slash
= strrchr(pathname
, '/');
411 strcpy(slash
, "ffmpeg");
413 /* This is needed to make relative pathnames work */
414 chdir(my_program_dir
);
416 signal(SIGPIPE
, SIG_DFL
);
418 execvp(pathname
, feed
->child_argv
);
426 /* open a listening socket */
427 static int socket_open_listen(struct sockaddr_in
*my_addr
)
431 server_fd
= socket(AF_INET
,SOCK_STREAM
,0);
438 setsockopt(server_fd
, SOL_SOCKET
, SO_REUSEADDR
, &tmp
, sizeof(tmp
));
440 if (bind (server_fd
, (struct sockaddr
*) my_addr
, sizeof (*my_addr
)) < 0) {
442 snprintf(bindmsg
, sizeof(bindmsg
), "bind(port %d)", ntohs(my_addr
->sin_port
));
444 closesocket(server_fd
);
448 if (listen (server_fd
, 5) < 0) {
450 closesocket(server_fd
);
453 ff_socket_nonblock(server_fd
, 1);
458 /* start all multicast streams */
459 static void start_multicast(void)
464 struct sockaddr_in dest_addr
;
465 int default_port
, stream_index
;
468 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
469 if (stream
->is_multicast
) {
470 /* open the RTP connection */
471 snprintf(session_id
, sizeof(session_id
), "%08x%08x",
472 av_random(&random_state
), av_random(&random_state
));
474 /* choose a port if none given */
475 if (stream
->multicast_port
== 0) {
476 stream
->multicast_port
= default_port
;
480 dest_addr
.sin_family
= AF_INET
;
481 dest_addr
.sin_addr
= stream
->multicast_ip
;
482 dest_addr
.sin_port
= htons(stream
->multicast_port
);
484 rtp_c
= rtp_new_connection(&dest_addr
, stream
, session_id
,
485 RTSP_PROTOCOL_RTP_UDP_MULTICAST
);
489 if (open_input_stream(rtp_c
, "") < 0) {
490 fprintf(stderr
, "Could not open input stream for stream '%s'\n",
495 /* open each RTP stream */
496 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
498 dest_addr
.sin_port
= htons(stream
->multicast_port
+
500 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, NULL
) < 0) {
501 fprintf(stderr
, "Could not open output stream '%s/streamid=%d'\n",
502 stream
->filename
, stream_index
);
507 /* change state to send data */
508 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
513 /* main loop of the http server */
514 static int http_server(void)
516 int server_fd
, ret
, rtsp_server_fd
, delay
, delay1
;
517 struct pollfd poll_table
[HTTP_MAX_CONNECTIONS
+ 2], *poll_entry
;
518 HTTPContext
*c
, *c_next
;
520 server_fd
= socket_open_listen(&my_http_addr
);
524 rtsp_server_fd
= socket_open_listen(&my_rtsp_addr
);
525 if (rtsp_server_fd
< 0)
528 http_log("ffserver started.\n");
530 start_children(first_feed
);
532 first_http_ctx
= NULL
;
538 poll_entry
= poll_table
;
539 poll_entry
->fd
= server_fd
;
540 poll_entry
->events
= POLLIN
;
543 poll_entry
->fd
= rtsp_server_fd
;
544 poll_entry
->events
= POLLIN
;
547 /* wait for events on each HTTP handle */
554 case HTTPSTATE_SEND_HEADER
:
555 case RTSPSTATE_SEND_REPLY
:
556 case RTSPSTATE_SEND_PACKET
:
557 c
->poll_entry
= poll_entry
;
559 poll_entry
->events
= POLLOUT
;
562 case HTTPSTATE_SEND_DATA_HEADER
:
563 case HTTPSTATE_SEND_DATA
:
564 case HTTPSTATE_SEND_DATA_TRAILER
:
565 if (!c
->is_packetized
) {
566 /* for TCP, we output as much as we can (may need to put a limit) */
567 c
->poll_entry
= poll_entry
;
569 poll_entry
->events
= POLLOUT
;
572 /* when ffserver is doing the timing, we work by
573 looking at which packet need to be sent every
575 delay1
= 10; /* one tick wait XXX: 10 ms assumed */
580 case HTTPSTATE_WAIT_REQUEST
:
581 case HTTPSTATE_RECEIVE_DATA
:
582 case HTTPSTATE_WAIT_FEED
:
583 case RTSPSTATE_WAIT_REQUEST
:
584 /* need to catch errors */
585 c
->poll_entry
= poll_entry
;
587 poll_entry
->events
= POLLIN
;/* Maybe this will work */
591 c
->poll_entry
= NULL
;
597 /* wait for an event on one connection. We poll at least every
598 second to handle timeouts */
600 ret
= poll(poll_table
, poll_entry
- poll_table
, delay
);
601 if (ret
< 0 && ff_neterrno() != FF_NETERROR(EAGAIN
) &&
602 ff_neterrno() != FF_NETERROR(EINTR
))
606 cur_time
= av_gettime() / 1000;
608 if (need_to_start_children
) {
609 need_to_start_children
= 0;
610 start_children(first_feed
);
613 /* now handle the events */
614 for(c
= first_http_ctx
; c
!= NULL
; c
= c_next
) {
616 if (handle_connection(c
) < 0) {
617 /* close and free the connection */
623 poll_entry
= poll_table
;
624 /* new HTTP connection request ? */
625 if (poll_entry
->revents
& POLLIN
)
626 new_connection(server_fd
, 0);
628 /* new RTSP connection request ? */
629 if (poll_entry
->revents
& POLLIN
)
630 new_connection(rtsp_server_fd
, 1);
634 /* start waiting for a new HTTP/RTSP request */
635 static void start_wait_request(HTTPContext
*c
, int is_rtsp
)
637 c
->buffer_ptr
= c
->buffer
;
638 c
->buffer_end
= c
->buffer
+ c
->buffer_size
- 1; /* leave room for '\0' */
641 c
->timeout
= cur_time
+ RTSP_REQUEST_TIMEOUT
;
642 c
->state
= RTSPSTATE_WAIT_REQUEST
;
644 c
->timeout
= cur_time
+ HTTP_REQUEST_TIMEOUT
;
645 c
->state
= HTTPSTATE_WAIT_REQUEST
;
649 static void new_connection(int server_fd
, int is_rtsp
)
651 struct sockaddr_in from_addr
;
653 HTTPContext
*c
= NULL
;
655 len
= sizeof(from_addr
);
656 fd
= accept(server_fd
, (struct sockaddr
*)&from_addr
,
660 ff_socket_nonblock(fd
, 1);
662 /* XXX: should output a warning page when coming
663 close to the connection limit */
664 if (nb_connections
>= nb_max_connections
)
667 /* add a new connection */
668 c
= av_mallocz(sizeof(HTTPContext
));
673 c
->poll_entry
= NULL
;
674 c
->from_addr
= from_addr
;
675 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
676 c
->buffer
= av_malloc(c
->buffer_size
);
680 c
->next
= first_http_ctx
;
684 start_wait_request(c
, is_rtsp
);
696 static void close_connection(HTTPContext
*c
)
698 HTTPContext
**cp
, *c1
;
700 AVFormatContext
*ctx
;
704 /* remove connection from list */
705 cp
= &first_http_ctx
;
706 while ((*cp
) != NULL
) {
714 /* remove references, if any (XXX: do it faster) */
715 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
720 /* remove connection associated resources */
724 /* close each frame parser */
725 for(i
=0;i
<c
->fmt_in
->nb_streams
;i
++) {
726 st
= c
->fmt_in
->streams
[i
];
727 if (st
->codec
->codec
)
728 avcodec_close(st
->codec
);
730 av_close_input_file(c
->fmt_in
);
733 /* free RTP output streams if any */
736 nb_streams
= c
->stream
->nb_streams
;
738 for(i
=0;i
<nb_streams
;i
++) {
741 av_write_trailer(ctx
);
744 h
= c
->rtp_handles
[i
];
751 if (!c
->last_packet_sent
) {
754 if (url_open_dyn_buf(&ctx
->pb
) >= 0) {
755 av_write_trailer(ctx
);
756 url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
761 for(i
=0; i
<ctx
->nb_streams
; i
++)
762 av_free(ctx
->streams
[i
]);
764 if (c
->stream
&& !c
->post
&& c
->stream
->stream_type
== STREAM_TYPE_LIVE
)
765 current_bandwidth
-= c
->stream
->bandwidth
;
767 /* signal that there is no feed if we are the feeder socket */
768 if (c
->state
== HTTPSTATE_RECEIVE_DATA
&& c
->stream
) {
769 c
->stream
->feed_opened
= 0;
773 av_freep(&c
->pb_buffer
);
774 av_freep(&c
->packet_buffer
);
780 static int handle_connection(HTTPContext
*c
)
785 case HTTPSTATE_WAIT_REQUEST
:
786 case RTSPSTATE_WAIT_REQUEST
:
788 if ((c
->timeout
- cur_time
) < 0)
790 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
793 /* no need to read if no events */
794 if (!(c
->poll_entry
->revents
& POLLIN
))
798 len
= recv(c
->fd
, c
->buffer_ptr
, 1, 0);
800 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
801 ff_neterrno() != FF_NETERROR(EINTR
))
803 } else if (len
== 0) {
806 /* search for end of request. */
808 c
->buffer_ptr
+= len
;
810 if ((ptr
>= c
->buffer
+ 2 && !memcmp(ptr
-2, "\n\n", 2)) ||
811 (ptr
>= c
->buffer
+ 4 && !memcmp(ptr
-4, "\r\n\r\n", 4))) {
812 /* request found : parse it and reply */
813 if (c
->state
== HTTPSTATE_WAIT_REQUEST
) {
814 ret
= http_parse_request(c
);
816 ret
= rtsp_parse_request(c
);
820 } else if (ptr
>= c
->buffer_end
) {
821 /* request too long: cannot do anything */
823 } else goto read_loop
;
827 case HTTPSTATE_SEND_HEADER
:
828 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
831 /* no need to write if no events */
832 if (!(c
->poll_entry
->revents
& POLLOUT
))
834 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
836 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
837 ff_neterrno() != FF_NETERROR(EINTR
)) {
838 /* error : close connection */
839 av_freep(&c
->pb_buffer
);
843 c
->buffer_ptr
+= len
;
845 c
->stream
->bytes_served
+= len
;
846 c
->data_count
+= len
;
847 if (c
->buffer_ptr
>= c
->buffer_end
) {
848 av_freep(&c
->pb_buffer
);
852 /* all the buffer was sent : synchronize to the incoming stream */
853 c
->state
= HTTPSTATE_SEND_DATA_HEADER
;
854 c
->buffer_ptr
= c
->buffer_end
= c
->buffer
;
859 case HTTPSTATE_SEND_DATA
:
860 case HTTPSTATE_SEND_DATA_HEADER
:
861 case HTTPSTATE_SEND_DATA_TRAILER
:
862 /* for packetized output, we consider we can always write (the
863 input streams sets the speed). It may be better to verify
864 that we do not rely too much on the kernel queues */
865 if (!c
->is_packetized
) {
866 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
869 /* no need to read if no events */
870 if (!(c
->poll_entry
->revents
& POLLOUT
))
873 if (http_send_data(c
) < 0)
875 /* close connection if trailer sent */
876 if (c
->state
== HTTPSTATE_SEND_DATA_TRAILER
)
879 case HTTPSTATE_RECEIVE_DATA
:
880 /* no need to read if no events */
881 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
883 if (!(c
->poll_entry
->revents
& POLLIN
))
885 if (http_receive_data(c
) < 0)
888 case HTTPSTATE_WAIT_FEED
:
889 /* no need to read if no events */
890 if (c
->poll_entry
->revents
& (POLLIN
| POLLERR
| POLLHUP
))
893 /* nothing to do, we'll be waken up by incoming feed packets */
896 case RTSPSTATE_SEND_REPLY
:
897 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
898 av_freep(&c
->pb_buffer
);
901 /* no need to write if no events */
902 if (!(c
->poll_entry
->revents
& POLLOUT
))
904 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
906 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
907 ff_neterrno() != FF_NETERROR(EINTR
)) {
908 /* error : close connection */
909 av_freep(&c
->pb_buffer
);
913 c
->buffer_ptr
+= len
;
914 c
->data_count
+= len
;
915 if (c
->buffer_ptr
>= c
->buffer_end
) {
916 /* all the buffer was sent : wait for a new request */
917 av_freep(&c
->pb_buffer
);
918 start_wait_request(c
, 1);
922 case RTSPSTATE_SEND_PACKET
:
923 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
924 av_freep(&c
->packet_buffer
);
927 /* no need to write if no events */
928 if (!(c
->poll_entry
->revents
& POLLOUT
))
930 len
= send(c
->fd
, c
->packet_buffer_ptr
,
931 c
->packet_buffer_end
- c
->packet_buffer_ptr
, 0);
933 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
934 ff_neterrno() != FF_NETERROR(EINTR
)) {
935 /* error : close connection */
936 av_freep(&c
->packet_buffer
);
940 c
->packet_buffer_ptr
+= len
;
941 if (c
->packet_buffer_ptr
>= c
->packet_buffer_end
) {
942 /* all the buffer was sent : wait for a new request */
943 av_freep(&c
->packet_buffer
);
944 c
->state
= RTSPSTATE_WAIT_REQUEST
;
948 case HTTPSTATE_READY
:
957 static int extract_rates(char *rates
, int ratelen
, const char *request
)
961 for (p
= request
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
962 if (strncasecmp(p
, "Pragma:", 7) == 0) {
963 const char *q
= p
+ 7;
965 while (*q
&& *q
!= '\n' && isspace(*q
))
968 if (strncasecmp(q
, "stream-switch-entry=", 20) == 0) {
974 memset(rates
, 0xff, ratelen
);
977 while (*q
&& *q
!= '\n' && *q
!= ':')
980 if (sscanf(q
, ":%d:%d", &stream_no
, &rate_no
) != 2)
984 if (stream_no
< ratelen
&& stream_no
>= 0)
985 rates
[stream_no
] = rate_no
;
987 while (*q
&& *q
!= '\n' && !isspace(*q
))
1004 static int find_stream_in_feed(FFStream
*feed
, AVCodecContext
*codec
, int bit_rate
)
1007 int best_bitrate
= 100000000;
1010 for (i
= 0; i
< feed
->nb_streams
; i
++) {
1011 AVCodecContext
*feed_codec
= feed
->streams
[i
]->codec
;
1013 if (feed_codec
->codec_id
!= codec
->codec_id
||
1014 feed_codec
->sample_rate
!= codec
->sample_rate
||
1015 feed_codec
->width
!= codec
->width
||
1016 feed_codec
->height
!= codec
->height
)
1019 /* Potential stream */
1021 /* We want the fastest stream less than bit_rate, or the slowest
1022 * faster than bit_rate
1025 if (feed_codec
->bit_rate
<= bit_rate
) {
1026 if (best_bitrate
> bit_rate
|| feed_codec
->bit_rate
> best_bitrate
) {
1027 best_bitrate
= feed_codec
->bit_rate
;
1031 if (feed_codec
->bit_rate
< best_bitrate
) {
1032 best_bitrate
= feed_codec
->bit_rate
;
1041 static int modify_current_stream(HTTPContext
*c
, char *rates
)
1044 FFStream
*req
= c
->stream
;
1045 int action_required
= 0;
1047 /* Not much we can do for a feed */
1051 for (i
= 0; i
< req
->nb_streams
; i
++) {
1052 AVCodecContext
*codec
= req
->streams
[i
]->codec
;
1056 c
->switch_feed_streams
[i
] = req
->feed_streams
[i
];
1059 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 2);
1062 /* Wants off or slow */
1063 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 4);
1065 /* This doesn't work well when it turns off the only stream! */
1066 c
->switch_feed_streams
[i
] = -2;
1067 c
->feed_streams
[i
] = -2;
1072 if (c
->switch_feed_streams
[i
] >= 0 && c
->switch_feed_streams
[i
] != c
->feed_streams
[i
])
1073 action_required
= 1;
1076 return action_required
;
1080 static void do_switch_stream(HTTPContext
*c
, int i
)
1082 if (c
->switch_feed_streams
[i
] >= 0) {
1084 c
->feed_streams
[i
] = c
->switch_feed_streams
[i
];
1087 /* Now update the stream */
1089 c
->switch_feed_streams
[i
] = -1;
1092 /* XXX: factorize in utils.c ? */
1093 /* XXX: take care with different space meaning */
1094 static void skip_spaces(const char **pp
)
1098 while (*p
== ' ' || *p
== '\t')
1103 static void get_word(char *buf
, int buf_size
, const char **pp
)
1111 while (!isspace(*p
) && *p
!= '\0') {
1112 if ((q
- buf
) < buf_size
- 1)
1121 static int validate_acl(FFStream
*stream
, HTTPContext
*c
)
1123 enum IPAddressAction last_action
= IP_DENY
;
1125 struct in_addr
*src
= &c
->from_addr
.sin_addr
;
1126 unsigned long src_addr
= src
->s_addr
;
1128 for (acl
= stream
->acl
; acl
; acl
= acl
->next
) {
1129 if (src_addr
>= acl
->first
.s_addr
&& src_addr
<= acl
->last
.s_addr
)
1130 return (acl
->action
== IP_ALLOW
) ? 1 : 0;
1131 last_action
= acl
->action
;
1134 /* Nothing matched, so return not the last action */
1135 return (last_action
== IP_DENY
) ? 1 : 0;
1138 /* compute the real filename of a file by matching it without its
1139 extensions to all the stream filenames */
1140 static void compute_real_filename(char *filename
, int max_size
)
1147 /* compute filename by matching without the file extensions */
1148 av_strlcpy(file1
, filename
, sizeof(file1
));
1149 p
= strrchr(file1
, '.');
1152 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
1153 av_strlcpy(file2
, stream
->filename
, sizeof(file2
));
1154 p
= strrchr(file2
, '.');
1157 if (!strcmp(file1
, file2
)) {
1158 av_strlcpy(filename
, stream
->filename
, max_size
);
1173 /* parse http request and prepare header */
1174 static int http_parse_request(HTTPContext
*c
)
1177 enum RedirType redir_type
;
1179 char info
[1024], filename
[1024];
1183 const char *mime_type
;
1187 char *useragent
= 0;
1190 get_word(cmd
, sizeof(cmd
), (const char **)&p
);
1191 av_strlcpy(c
->method
, cmd
, sizeof(c
->method
));
1193 if (!strcmp(cmd
, "GET"))
1195 else if (!strcmp(cmd
, "POST"))
1200 get_word(url
, sizeof(url
), (const char **)&p
);
1201 av_strlcpy(c
->url
, url
, sizeof(c
->url
));
1203 get_word(protocol
, sizeof(protocol
), (const char **)&p
);
1204 if (strcmp(protocol
, "HTTP/1.0") && strcmp(protocol
, "HTTP/1.1"))
1207 av_strlcpy(c
->protocol
, protocol
, sizeof(c
->protocol
));
1210 http_log("New connection: %s %s\n", cmd
, url
);
1212 /* find the filename and the optional info string in the request */
1213 p
= strchr(url
, '?');
1215 av_strlcpy(info
, p
, sizeof(info
));
1220 av_strlcpy(filename
, url
+ ((*url
== '/') ? 1 : 0), sizeof(filename
)-1);
1222 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1223 if (strncasecmp(p
, "User-Agent:", 11) == 0) {
1225 if (*useragent
&& *useragent
!= '\n' && isspace(*useragent
))
1229 p
= strchr(p
, '\n');
1236 redir_type
= REDIR_NONE
;
1237 if (match_ext(filename
, "asx")) {
1238 redir_type
= REDIR_ASX
;
1239 filename
[strlen(filename
)-1] = 'f';
1240 } else if (match_ext(filename
, "asf") &&
1241 (!useragent
|| strncasecmp(useragent
, "NSPlayer", 8) != 0)) {
1242 /* if this isn't WMP or lookalike, return the redirector file */
1243 redir_type
= REDIR_ASF
;
1244 } else if (match_ext(filename
, "rpm,ram")) {
1245 redir_type
= REDIR_RAM
;
1246 strcpy(filename
+ strlen(filename
)-2, "m");
1247 } else if (match_ext(filename
, "rtsp")) {
1248 redir_type
= REDIR_RTSP
;
1249 compute_real_filename(filename
, sizeof(filename
) - 1);
1250 } else if (match_ext(filename
, "sdp")) {
1251 redir_type
= REDIR_SDP
;
1252 compute_real_filename(filename
, sizeof(filename
) - 1);
1255 // "redirect" / request to index.html
1256 if (!strlen(filename
))
1257 av_strlcpy(filename
, "index.html", sizeof(filename
) - 1);
1259 stream
= first_stream
;
1260 while (stream
!= NULL
) {
1261 if (!strcmp(stream
->filename
, filename
) && validate_acl(stream
, c
))
1263 stream
= stream
->next
;
1265 if (stream
== NULL
) {
1266 snprintf(msg
, sizeof(msg
), "File '%s' not found", url
);
1271 memcpy(c
->feed_streams
, stream
->feed_streams
, sizeof(c
->feed_streams
));
1272 memset(c
->switch_feed_streams
, -1, sizeof(c
->switch_feed_streams
));
1274 if (stream
->stream_type
== STREAM_TYPE_REDIRECT
) {
1275 c
->http_error
= 301;
1277 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 301 Moved\r\n");
1278 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Location: %s\r\n", stream
->feed_filename
);
1279 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1280 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1281 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Moved</title></head><body>\r\n");
1282 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "You should be <a href=\"%s\">redirected</a>.\r\n", stream
->feed_filename
);
1283 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1285 /* prepare output buffer */
1286 c
->buffer_ptr
= c
->buffer
;
1288 c
->state
= HTTPSTATE_SEND_HEADER
;
1292 /* If this is WMP, get the rate information */
1293 if (extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1294 if (modify_current_stream(c
, ratebuf
)) {
1295 for (i
= 0; i
< sizeof(c
->feed_streams
) / sizeof(c
->feed_streams
[0]); i
++) {
1296 if (c
->switch_feed_streams
[i
] >= 0)
1297 do_switch_stream(c
, i
);
1302 /* If already streaming this feed, do not let start another feeder. */
1303 if (stream
->feed_opened
) {
1304 snprintf(msg
, sizeof(msg
), "This feed is already being received.");
1308 if (c
->post
== 0 && stream
->stream_type
== STREAM_TYPE_LIVE
)
1309 current_bandwidth
+= stream
->bandwidth
;
1311 if (c
->post
== 0 && max_bandwidth
< current_bandwidth
) {
1312 c
->http_error
= 200;
1314 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 Server too busy\r\n");
1315 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1316 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1317 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Too busy</title></head><body>\r\n");
1318 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");
1319 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",
1320 current_bandwidth
, max_bandwidth
);
1321 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1323 /* prepare output buffer */
1324 c
->buffer_ptr
= c
->buffer
;
1326 c
->state
= HTTPSTATE_SEND_HEADER
;
1330 if (redir_type
!= REDIR_NONE
) {
1333 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1334 if (strncasecmp(p
, "Host:", 5) == 0) {
1338 p
= strchr(p
, '\n');
1349 while (isspace(*hostinfo
))
1352 eoh
= strchr(hostinfo
, '\n');
1354 if (eoh
[-1] == '\r')
1357 if (eoh
- hostinfo
< sizeof(hostbuf
) - 1) {
1358 memcpy(hostbuf
, hostinfo
, eoh
- hostinfo
);
1359 hostbuf
[eoh
- hostinfo
] = 0;
1361 c
->http_error
= 200;
1363 switch(redir_type
) {
1365 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASX Follows\r\n");
1366 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1367 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1368 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ASX Version=\"3\">\r\n");
1369 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1370 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1371 hostbuf
, filename
, info
);
1372 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</ASX>\r\n");
1375 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RAM Follows\r\n");
1376 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: audio/x-pn-realaudio\r\n");
1377 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1378 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "# Autogenerated by ffserver\r\n");
1379 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "http://%s/%s%s\r\n",
1380 hostbuf
, filename
, info
);
1383 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASF Redirect follows\r\n");
1384 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1385 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1386 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "[Reference]\r\n");
1387 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Ref1=http://%s/%s%s\r\n",
1388 hostbuf
, filename
, info
);
1392 char hostname
[256], *p
;
1393 /* extract only hostname */
1394 av_strlcpy(hostname
, hostbuf
, sizeof(hostname
));
1395 p
= strrchr(hostname
, ':');
1398 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1399 /* XXX: incorrect mime type ? */
1400 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/x-rtsp\r\n");
1401 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1402 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "rtsp://%s:%d/%s\r\n",
1403 hostname
, ntohs(my_rtsp_addr
.sin_port
),
1410 int sdp_data_size
, len
;
1411 struct sockaddr_in my_addr
;
1413 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1414 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/sdp\r\n");
1415 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1417 len
= sizeof(my_addr
);
1418 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
1420 /* XXX: should use a dynamic buffer */
1421 sdp_data_size
= prepare_sdp_description(stream
,
1424 if (sdp_data_size
> 0) {
1425 memcpy(q
, sdp_data
, sdp_data_size
);
1437 /* prepare output buffer */
1438 c
->buffer_ptr
= c
->buffer
;
1440 c
->state
= HTTPSTATE_SEND_HEADER
;
1446 snprintf(msg
, sizeof(msg
), "ASX/RAM file not handled");
1450 stream
->conns_served
++;
1452 /* XXX: add there authenticate and IP match */
1455 /* if post, it means a feed is being sent */
1456 if (!stream
->is_feed
) {
1457 /* However it might be a status report from WMP! Lets log the data
1458 * as it might come in handy one day
1463 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1464 if (strncasecmp(p
, "Pragma: log-line=", 17) == 0) {
1468 if (strncasecmp(p
, "Pragma: client-id=", 18) == 0)
1469 client_id
= strtol(p
+ 18, 0, 10);
1470 p
= strchr(p
, '\n');
1478 char *eol
= strchr(logline
, '\n');
1483 if (eol
[-1] == '\r')
1485 http_log("%.*s\n", (int) (eol
- logline
), logline
);
1486 c
->suppress_log
= 1;
1491 http_log("\nGot request:\n%s\n", c
->buffer
);
1494 if (client_id
&& extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1497 /* Now we have to find the client_id */
1498 for (wmpc
= first_http_ctx
; wmpc
; wmpc
= wmpc
->next
) {
1499 if (wmpc
->wmp_client_id
== client_id
)
1503 if (wmpc
&& modify_current_stream(wmpc
, ratebuf
))
1504 wmpc
->switch_pending
= 1;
1507 snprintf(msg
, sizeof(msg
), "POST command not handled");
1511 if (http_start_receive_data(c
) < 0) {
1512 snprintf(msg
, sizeof(msg
), "could not open feed");
1516 c
->state
= HTTPSTATE_RECEIVE_DATA
;
1521 if (strcmp(stream
->filename
+ strlen(stream
->filename
) - 4, ".asf") == 0)
1522 http_log("\nGot request:\n%s\n", c
->buffer
);
1525 if (c
->stream
->stream_type
== STREAM_TYPE_STATUS
)
1528 /* open input stream */
1529 if (open_input_stream(c
, info
) < 0) {
1530 snprintf(msg
, sizeof(msg
), "Input stream corresponding to '%s' not found", url
);
1534 /* prepare http header */
1536 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1537 mime_type
= c
->stream
->fmt
->mime_type
;
1539 mime_type
= "application/x-octet-stream";
1540 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Pragma: no-cache\r\n");
1542 /* for asf, we need extra headers */
1543 if (!strcmp(c
->stream
->fmt
->name
,"asf_stream")) {
1544 /* Need to allocate a client id */
1546 c
->wmp_client_id
= av_random(&random_state
) & 0x7fffffff;
1548 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
);
1550 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-Type: %s\r\n", mime_type
);
1551 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1553 /* prepare output buffer */
1555 c
->buffer_ptr
= c
->buffer
;
1557 c
->state
= HTTPSTATE_SEND_HEADER
;
1560 c
->http_error
= 404;
1562 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 404 Not Found\r\n");
1563 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: %s\r\n", "text/html");
1564 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1565 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HTML>\n");
1566 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1567 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<BODY>%s</BODY>\n", msg
);
1568 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</HTML>\n");
1570 /* prepare output buffer */
1571 c
->buffer_ptr
= c
->buffer
;
1573 c
->state
= HTTPSTATE_SEND_HEADER
;
1577 c
->http_error
= 200; /* horrible : we use this value to avoid
1578 going to the send data state */
1579 c
->state
= HTTPSTATE_SEND_HEADER
;
1583 static void fmt_bytecount(ByteIOContext
*pb
, int64_t count
)
1585 static const char *suffix
= " kMGTP";
1588 for (s
= suffix
; count
>= 100000 && s
[1]; count
/= 1000, s
++);
1590 url_fprintf(pb
, "%"PRId64
"%c", count
, *s
);
1593 static void compute_stats(HTTPContext
*c
)
1602 if (url_open_dyn_buf(&pb
) < 0) {
1603 /* XXX: return an error ? */
1604 c
->buffer_ptr
= c
->buffer
;
1605 c
->buffer_end
= c
->buffer
;
1609 url_fprintf(pb
, "HTTP/1.0 200 OK\r\n");
1610 url_fprintf(pb
, "Content-type: %s\r\n", "text/html");
1611 url_fprintf(pb
, "Pragma: no-cache\r\n");
1612 url_fprintf(pb
, "\r\n");
1614 url_fprintf(pb
, "<HEAD><TITLE>FFServer Status</TITLE>\n");
1615 if (c
->stream
->feed_filename
)
1616 url_fprintf(pb
, "<link rel=\"shortcut icon\" href=\"%s\">\n", c
->stream
->feed_filename
);
1617 url_fprintf(pb
, "</HEAD>\n<BODY>");
1618 url_fprintf(pb
, "<H1>FFServer Status</H1>\n");
1620 url_fprintf(pb
, "<H2>Available Streams</H2>\n");
1621 url_fprintf(pb
, "<TABLE cellspacing=0 cellpadding=4>\n");
1622 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");
1623 stream
= first_stream
;
1624 while (stream
!= NULL
) {
1625 char sfilename
[1024];
1628 if (stream
->feed
!= stream
) {
1629 av_strlcpy(sfilename
, stream
->filename
, sizeof(sfilename
) - 10);
1630 eosf
= sfilename
+ strlen(sfilename
);
1631 if (eosf
- sfilename
>= 4) {
1632 if (strcmp(eosf
- 4, ".asf") == 0)
1633 strcpy(eosf
- 4, ".asx");
1634 else if (strcmp(eosf
- 3, ".rm") == 0)
1635 strcpy(eosf
- 3, ".ram");
1636 else if (stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
1637 /* generate a sample RTSP director if
1638 unicast. Generate an SDP redirector if
1640 eosf
= strrchr(sfilename
, '.');
1642 eosf
= sfilename
+ strlen(sfilename
);
1643 if (stream
->is_multicast
)
1644 strcpy(eosf
, ".sdp");
1646 strcpy(eosf
, ".rtsp");
1650 url_fprintf(pb
, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1651 sfilename
, stream
->filename
);
1652 url_fprintf(pb
, "<td align=right> %d <td align=right> ",
1653 stream
->conns_served
);
1654 fmt_bytecount(pb
, stream
->bytes_served
);
1655 switch(stream
->stream_type
) {
1656 case STREAM_TYPE_LIVE
:
1658 int audio_bit_rate
= 0;
1659 int video_bit_rate
= 0;
1660 const char *audio_codec_name
= "";
1661 const char *video_codec_name
= "";
1662 const char *audio_codec_name_extra
= "";
1663 const char *video_codec_name_extra
= "";
1665 for(i
=0;i
<stream
->nb_streams
;i
++) {
1666 AVStream
*st
= stream
->streams
[i
];
1667 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1668 switch(st
->codec
->codec_type
) {
1669 case CODEC_TYPE_AUDIO
:
1670 audio_bit_rate
+= st
->codec
->bit_rate
;
1672 if (*audio_codec_name
)
1673 audio_codec_name_extra
= "...";
1674 audio_codec_name
= codec
->name
;
1677 case CODEC_TYPE_VIDEO
:
1678 video_bit_rate
+= st
->codec
->bit_rate
;
1680 if (*video_codec_name
)
1681 video_codec_name_extra
= "...";
1682 video_codec_name
= codec
->name
;
1685 case CODEC_TYPE_DATA
:
1686 video_bit_rate
+= st
->codec
->bit_rate
;
1692 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",
1695 video_bit_rate
/ 1000, video_codec_name
, video_codec_name_extra
,
1696 audio_bit_rate
/ 1000, audio_codec_name
, audio_codec_name_extra
);
1698 url_fprintf(pb
, "<TD>%s", stream
->feed
->filename
);
1700 url_fprintf(pb
, "<TD>%s", stream
->feed_filename
);
1701 url_fprintf(pb
, "\n");
1705 url_fprintf(pb
, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1709 stream
= stream
->next
;
1711 url_fprintf(pb
, "</TABLE>\n");
1713 stream
= first_stream
;
1714 while (stream
!= NULL
) {
1715 if (stream
->feed
== stream
) {
1716 url_fprintf(pb
, "<h2>Feed %s</h2>", stream
->filename
);
1718 url_fprintf(pb
, "Running as pid %d.\n", stream
->pid
);
1720 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1725 /* This is somewhat linux specific I guess */
1726 snprintf(ps_cmd
, sizeof(ps_cmd
),
1727 "ps -o \"%%cpu,cputime\" --no-headers %d",
1730 pid_stat
= popen(ps_cmd
, "r");
1735 if (fscanf(pid_stat
, "%10s %64s", cpuperc
,
1737 url_fprintf(pb
, "Currently using %s%% of the cpu. Total time used %s.\n",
1745 url_fprintf(pb
, "<p>");
1747 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");
1749 for (i
= 0; i
< stream
->nb_streams
; i
++) {
1750 AVStream
*st
= stream
->streams
[i
];
1751 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1752 const char *type
= "unknown";
1753 char parameters
[64];
1757 switch(st
->codec
->codec_type
) {
1758 case CODEC_TYPE_AUDIO
:
1760 snprintf(parameters
, sizeof(parameters
), "%d channel(s), %d Hz", st
->codec
->channels
, st
->codec
->sample_rate
);
1762 case CODEC_TYPE_VIDEO
:
1764 snprintf(parameters
, sizeof(parameters
), "%dx%d, q=%d-%d, fps=%d", st
->codec
->width
, st
->codec
->height
,
1765 st
->codec
->qmin
, st
->codec
->qmax
, st
->codec
->time_base
.den
/ st
->codec
->time_base
.num
);
1770 url_fprintf(pb
, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1771 i
, type
, st
->codec
->bit_rate
/1000, codec
? codec
->name
: "", parameters
);
1773 url_fprintf(pb
, "</table>\n");
1776 stream
= stream
->next
;
1782 AVCodecContext
*enc
;
1786 stream
= first_feed
;
1787 while (stream
!= NULL
) {
1788 url_fprintf(pb
, "<H1>Feed '%s'</H1>\n", stream
->filename
);
1789 url_fprintf(pb
, "<TABLE>\n");
1790 url_fprintf(pb
, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1791 for(i
=0;i
<stream
->nb_streams
;i
++) {
1792 AVStream
*st
= stream
->streams
[i
];
1793 FeedData
*fdata
= st
->priv_data
;
1796 avcodec_string(buf
, sizeof(buf
), enc
);
1797 avg
= fdata
->avg_frame_size
* (float)enc
->rate
* 8.0;
1798 if (enc
->codec
->type
== CODEC_TYPE_AUDIO
&& enc
->frame_size
> 0)
1799 avg
/= enc
->frame_size
;
1800 url_fprintf(pb
, "<TR><TD>%s <TD> %d <TD> %"PRId64
" <TD> %0.1f\n",
1801 buf
, enc
->frame_number
, fdata
->data_count
, avg
/ 1000.0);
1803 url_fprintf(pb
, "</TABLE>\n");
1804 stream
= stream
->next_feed
;
1809 /* connection status */
1810 url_fprintf(pb
, "<H2>Connection Status</H2>\n");
1812 url_fprintf(pb
, "Number of connections: %d / %d<BR>\n",
1813 nb_connections
, nb_max_connections
);
1815 url_fprintf(pb
, "Bandwidth in use: %dk / %dk<BR>\n",
1816 current_bandwidth
, max_bandwidth
);
1818 url_fprintf(pb
, "<TABLE>\n");
1819 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");
1820 c1
= first_http_ctx
;
1822 while (c1
!= NULL
) {
1828 for (j
= 0; j
< c1
->stream
->nb_streams
; j
++) {
1829 if (!c1
->stream
->feed
)
1830 bitrate
+= c1
->stream
->streams
[j
]->codec
->bit_rate
;
1831 else if (c1
->feed_streams
[j
] >= 0)
1832 bitrate
+= c1
->stream
->feed
->streams
[c1
->feed_streams
[j
]]->codec
->bit_rate
;
1837 p
= inet_ntoa(c1
->from_addr
.sin_addr
);
1838 url_fprintf(pb
, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1840 c1
->stream
? c1
->stream
->filename
: "",
1841 c1
->state
== HTTPSTATE_RECEIVE_DATA
? "(input)" : "",
1844 http_state
[c1
->state
]);
1845 fmt_bytecount(pb
, bitrate
);
1846 url_fprintf(pb
, "<td align=right>");
1847 fmt_bytecount(pb
, compute_datarate(&c1
->datarate
, c1
->data_count
) * 8);
1848 url_fprintf(pb
, "<td align=right>");
1849 fmt_bytecount(pb
, c1
->data_count
);
1850 url_fprintf(pb
, "\n");
1853 url_fprintf(pb
, "</TABLE>\n");
1858 url_fprintf(pb
, "<HR size=1 noshade>Generated at %s", p
);
1859 url_fprintf(pb
, "</BODY>\n</HTML>\n");
1861 len
= url_close_dyn_buf(pb
, &c
->pb_buffer
);
1862 c
->buffer_ptr
= c
->pb_buffer
;
1863 c
->buffer_end
= c
->pb_buffer
+ len
;
1866 /* check if the parser needs to be opened for stream i */
1867 static void open_parser(AVFormatContext
*s
, int i
)
1869 AVStream
*st
= s
->streams
[i
];
1872 if (!st
->codec
->codec
) {
1873 codec
= avcodec_find_decoder(st
->codec
->codec_id
);
1874 if (codec
&& (codec
->capabilities
& CODEC_CAP_PARSE_ONLY
)) {
1875 st
->codec
->parse_only
= 1;
1876 if (avcodec_open(st
->codec
, codec
) < 0)
1877 st
->codec
->parse_only
= 0;
1882 static int open_input_stream(HTTPContext
*c
, const char *info
)
1885 char input_filename
[1024];
1890 /* find file name */
1891 if (c
->stream
->feed
) {
1892 strcpy(input_filename
, c
->stream
->feed
->feed_filename
);
1893 buf_size
= FFM_PACKET_SIZE
;
1894 /* compute position (absolute time) */
1895 if (find_info_tag(buf
, sizeof(buf
), "date", info
))
1897 stream_pos
= parse_date(buf
, 0);
1898 if (stream_pos
== INT64_MIN
)
1901 else if (find_info_tag(buf
, sizeof(buf
), "buffer", info
)) {
1902 int prebuffer
= strtol(buf
, 0, 10);
1903 stream_pos
= av_gettime() - prebuffer
* (int64_t)1000000;
1905 stream_pos
= av_gettime() - c
->stream
->prebuffer
* (int64_t)1000;
1907 strcpy(input_filename
, c
->stream
->feed_filename
);
1909 /* compute position (relative time) */
1910 if (find_info_tag(buf
, sizeof(buf
), "date", info
))
1912 stream_pos
= parse_date(buf
, 1);
1913 if (stream_pos
== INT64_MIN
)
1919 if (input_filename
[0] == '\0')
1923 { time_t when
= stream_pos
/ 1000000;
1924 http_log("Stream pos = %"PRId64
", time=%s", stream_pos
, ctime(&when
));
1929 if (av_open_input_file(&s
, input_filename
, c
->stream
->ifmt
,
1930 buf_size
, c
->stream
->ap_in
) < 0) {
1931 http_log("%s not found", input_filename
);
1934 s
->flags
|= AVFMT_FLAG_GENPTS
;
1936 av_find_stream_info(c
->fmt_in
);
1938 /* open each parser */
1939 for(i
=0;i
<s
->nb_streams
;i
++)
1942 /* choose stream as clock source (we favorize video stream if
1943 present) for packet sending */
1944 c
->pts_stream_index
= 0;
1945 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
1946 if (c
->pts_stream_index
== 0 &&
1947 c
->stream
->streams
[i
]->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
1948 c
->pts_stream_index
= i
;
1953 if (c
->fmt_in
->iformat
->read_seek
)
1954 c
->fmt_in
->iformat
->read_seek(c
->fmt_in
, 0, stream_pos
, 0);
1956 /* set the start time (needed for maxtime and RTP packet timing) */
1957 c
->start_time
= cur_time
;
1958 c
->first_pts
= AV_NOPTS_VALUE
;
1962 /* return the server clock (in us) */
1963 static int64_t get_server_clock(HTTPContext
*c
)
1965 /* compute current pts value from system time */
1966 return (cur_time
- c
->start_time
) * 1000;
1969 /* return the estimated time at which the current packet must be sent
1971 static int64_t get_packet_send_clock(HTTPContext
*c
)
1973 int bytes_left
, bytes_sent
, frame_bytes
;
1975 frame_bytes
= c
->cur_frame_bytes
;
1976 if (frame_bytes
<= 0)
1979 bytes_left
= c
->buffer_end
- c
->buffer_ptr
;
1980 bytes_sent
= frame_bytes
- bytes_left
;
1981 return c
->cur_pts
+ (c
->cur_frame_duration
* bytes_sent
) / frame_bytes
;
1986 static int http_prepare_data(HTTPContext
*c
)
1989 AVFormatContext
*ctx
;
1991 av_freep(&c
->pb_buffer
);
1993 case HTTPSTATE_SEND_DATA_HEADER
:
1994 memset(&c
->fmt_ctx
, 0, sizeof(c
->fmt_ctx
));
1995 av_strlcpy(c
->fmt_ctx
.author
, c
->stream
->author
,
1996 sizeof(c
->fmt_ctx
.author
));
1997 av_strlcpy(c
->fmt_ctx
.comment
, c
->stream
->comment
,
1998 sizeof(c
->fmt_ctx
.comment
));
1999 av_strlcpy(c
->fmt_ctx
.copyright
, c
->stream
->copyright
,
2000 sizeof(c
->fmt_ctx
.copyright
));
2001 av_strlcpy(c
->fmt_ctx
.title
, c
->stream
->title
,
2002 sizeof(c
->fmt_ctx
.title
));
2004 /* open output stream by using specified codecs */
2005 c
->fmt_ctx
.oformat
= c
->stream
->fmt
;
2006 c
->fmt_ctx
.nb_streams
= c
->stream
->nb_streams
;
2007 for(i
=0;i
<c
->fmt_ctx
.nb_streams
;i
++) {
2010 st
= av_mallocz(sizeof(AVStream
));
2011 st
->codec
= avcodec_alloc_context();
2012 c
->fmt_ctx
.streams
[i
] = st
;
2013 /* if file or feed, then just take streams from FFStream struct */
2014 if (!c
->stream
->feed
||
2015 c
->stream
->feed
== c
->stream
)
2016 src
= c
->stream
->streams
[i
];
2018 src
= c
->stream
->feed
->streams
[c
->stream
->feed_streams
[i
]];
2022 st
->codec
->frame_number
= 0; /* XXX: should be done in
2023 AVStream, not in codec */
2024 /* I'm pretty sure that this is not correct...
2025 * However, without it, we crash
2027 st
->codec
->coded_frame
= &dummy_frame
;
2029 c
->got_key_frame
= 0;
2031 /* prepare header and save header data in a stream */
2032 if (url_open_dyn_buf(&c
->fmt_ctx
.pb
) < 0) {
2033 /* XXX: potential leak */
2036 c
->fmt_ctx
.pb
->is_streamed
= 1;
2038 av_set_parameters(&c
->fmt_ctx
, NULL
);
2039 if (av_write_header(&c
->fmt_ctx
) < 0)
2042 len
= url_close_dyn_buf(c
->fmt_ctx
.pb
, &c
->pb_buffer
);
2043 c
->buffer_ptr
= c
->pb_buffer
;
2044 c
->buffer_end
= c
->pb_buffer
+ len
;
2046 c
->state
= HTTPSTATE_SEND_DATA
;
2047 c
->last_packet_sent
= 0;
2049 case HTTPSTATE_SEND_DATA
:
2050 /* find a new packet */
2054 /* read a packet from the input stream */
2055 if (c
->stream
->feed
)
2056 ffm_set_write_index(c
->fmt_in
,
2057 c
->stream
->feed
->feed_write_index
,
2058 c
->stream
->feed
->feed_size
);
2060 if (c
->stream
->max_time
&&
2061 c
->stream
->max_time
+ c
->start_time
- cur_time
< 0)
2062 /* We have timed out */
2063 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2066 if (av_read_frame(c
->fmt_in
, &pkt
) < 0) {
2067 if (c
->stream
->feed
&& c
->stream
->feed
->feed_opened
) {
2068 /* if coming from feed, it means we reached the end of the
2069 ffm file, so must wait for more data */
2070 c
->state
= HTTPSTATE_WAIT_FEED
;
2071 return 1; /* state changed */
2073 if (c
->stream
->loop
) {
2074 av_close_input_file(c
->fmt_in
);
2076 if (open_input_stream(c
, "") < 0)
2081 /* must send trailer now because eof or error */
2082 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2086 /* update first pts if needed */
2087 if (c
->first_pts
== AV_NOPTS_VALUE
) {
2088 c
->first_pts
= av_rescale_q(pkt
.dts
, c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
, AV_TIME_BASE_Q
);
2089 c
->start_time
= cur_time
;
2091 /* send it to the appropriate stream */
2092 if (c
->stream
->feed
) {
2093 /* if coming from a feed, select the right stream */
2094 if (c
->switch_pending
) {
2095 c
->switch_pending
= 0;
2096 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2097 if (c
->switch_feed_streams
[i
] == pkt
.stream_index
)
2098 if (pkt
.flags
& PKT_FLAG_KEY
)
2099 do_switch_stream(c
, i
);
2100 if (c
->switch_feed_streams
[i
] >= 0)
2101 c
->switch_pending
= 1;
2104 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2105 if (c
->feed_streams
[i
] == pkt
.stream_index
) {
2106 pkt
.stream_index
= i
;
2107 if (pkt
.flags
& PKT_FLAG_KEY
)
2108 c
->got_key_frame
|= 1 << i
;
2109 /* See if we have all the key frames, then
2110 * we start to send. This logic is not quite
2111 * right, but it works for the case of a
2112 * single video stream with one or more
2113 * audio streams (for which every frame is
2114 * typically a key frame).
2116 if (!c
->stream
->send_on_key
||
2117 ((c
->got_key_frame
+ 1) >> c
->stream
->nb_streams
))
2122 AVCodecContext
*codec
;
2125 /* specific handling for RTP: we use several
2126 output stream (one for each RTP
2127 connection). XXX: need more abstract handling */
2128 if (c
->is_packetized
) {
2130 /* compute send time and duration */
2131 st
= c
->fmt_in
->streams
[pkt
.stream_index
];
2132 c
->cur_pts
= av_rescale_q(pkt
.dts
, st
->time_base
, AV_TIME_BASE_Q
);
2133 if (st
->start_time
!= AV_NOPTS_VALUE
)
2134 c
->cur_pts
-= av_rescale_q(st
->start_time
, st
->time_base
, AV_TIME_BASE_Q
);
2135 c
->cur_frame_duration
= av_rescale_q(pkt
.duration
, st
->time_base
, AV_TIME_BASE_Q
);
2137 printf("index=%d pts=%0.3f duration=%0.6f\n",
2139 (double)c
->cur_pts
/
2141 (double)c
->cur_frame_duration
/
2144 /* find RTP context */
2145 c
->packet_stream_index
= pkt
.stream_index
;
2146 ctx
= c
->rtp_ctx
[c
->packet_stream_index
];
2148 av_free_packet(&pkt
);
2151 codec
= ctx
->streams
[0]->codec
;
2152 /* only one stream per RTP connection */
2153 pkt
.stream_index
= 0;
2157 codec
= ctx
->streams
[pkt
.stream_index
]->codec
;
2160 codec
->coded_frame
->key_frame
= ((pkt
.flags
& PKT_FLAG_KEY
) != 0);
2161 if (c
->is_packetized
) {
2162 int max_packet_size
;
2163 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
)
2164 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
2166 max_packet_size
= url_get_max_packet_size(c
->rtp_handles
[c
->packet_stream_index
]);
2167 ret
= url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
);
2169 ret
= url_open_dyn_buf(&ctx
->pb
);
2172 /* XXX: potential leak */
2175 if (pkt
.dts
!= AV_NOPTS_VALUE
)
2176 pkt
.dts
= av_rescale_q(pkt
.dts
,
2177 c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
,
2178 ctx
->streams
[pkt
.stream_index
]->time_base
);
2179 if (pkt
.pts
!= AV_NOPTS_VALUE
)
2180 pkt
.pts
= av_rescale_q(pkt
.pts
,
2181 c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
,
2182 ctx
->streams
[pkt
.stream_index
]->time_base
);
2183 if (av_write_frame(ctx
, &pkt
))
2184 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2186 len
= url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
2187 c
->cur_frame_bytes
= len
;
2188 c
->buffer_ptr
= c
->pb_buffer
;
2189 c
->buffer_end
= c
->pb_buffer
+ len
;
2191 codec
->frame_number
++;
2195 av_free_packet(&pkt
);
2201 case HTTPSTATE_SEND_DATA_TRAILER
:
2202 /* last packet test ? */
2203 if (c
->last_packet_sent
|| c
->is_packetized
)
2206 /* prepare header */
2207 if (url_open_dyn_buf(&ctx
->pb
) < 0) {
2208 /* XXX: potential leak */
2211 av_write_trailer(ctx
);
2212 len
= url_close_dyn_buf(ctx
->pb
, &c
->pb_buffer
);
2213 c
->buffer_ptr
= c
->pb_buffer
;
2214 c
->buffer_end
= c
->pb_buffer
+ len
;
2216 c
->last_packet_sent
= 1;
2222 /* should convert the format at the same time */
2223 /* send data starting at c->buffer_ptr to the output connection
2224 (either UDP or TCP connection) */
2225 static int http_send_data(HTTPContext
*c
)
2230 if (c
->buffer_ptr
>= c
->buffer_end
) {
2231 ret
= http_prepare_data(c
);
2235 /* state change requested */
2238 if (c
->is_packetized
) {
2239 /* RTP data output */
2240 len
= c
->buffer_end
- c
->buffer_ptr
;
2242 /* fail safe - should never happen */
2244 c
->buffer_ptr
= c
->buffer_end
;
2247 len
= (c
->buffer_ptr
[0] << 24) |
2248 (c
->buffer_ptr
[1] << 16) |
2249 (c
->buffer_ptr
[2] << 8) |
2251 if (len
> (c
->buffer_end
- c
->buffer_ptr
))
2253 if ((get_packet_send_clock(c
) - get_server_clock(c
)) > 0) {
2254 /* nothing to send yet: we can wait */
2258 c
->data_count
+= len
;
2259 update_datarate(&c
->datarate
, c
->data_count
);
2261 c
->stream
->bytes_served
+= len
;
2263 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
) {
2264 /* RTP packets are sent inside the RTSP TCP connection */
2266 int interleaved_index
, size
;
2268 HTTPContext
*rtsp_c
;
2271 /* if no RTSP connection left, error */
2274 /* if already sending something, then wait. */
2275 if (rtsp_c
->state
!= RTSPSTATE_WAIT_REQUEST
)
2277 if (url_open_dyn_buf(&pb
) < 0)
2279 interleaved_index
= c
->packet_stream_index
* 2;
2280 /* RTCP packets are sent at odd indexes */
2281 if (c
->buffer_ptr
[1] == 200)
2282 interleaved_index
++;
2283 /* write RTSP TCP header */
2285 header
[1] = interleaved_index
;
2286 header
[2] = len
>> 8;
2288 put_buffer(pb
, header
, 4);
2289 /* write RTP packet data */
2291 put_buffer(pb
, c
->buffer_ptr
, len
);
2292 size
= url_close_dyn_buf(pb
, &c
->packet_buffer
);
2293 /* prepare asynchronous TCP sending */
2294 rtsp_c
->packet_buffer_ptr
= c
->packet_buffer
;
2295 rtsp_c
->packet_buffer_end
= c
->packet_buffer
+ size
;
2296 c
->buffer_ptr
+= len
;
2298 /* send everything we can NOW */
2299 len
= send(rtsp_c
->fd
, rtsp_c
->packet_buffer_ptr
,
2300 rtsp_c
->packet_buffer_end
- rtsp_c
->packet_buffer_ptr
, 0);
2302 rtsp_c
->packet_buffer_ptr
+= len
;
2303 if (rtsp_c
->packet_buffer_ptr
< rtsp_c
->packet_buffer_end
) {
2304 /* if we could not send all the data, we will
2305 send it later, so a new state is needed to
2306 "lock" the RTSP TCP connection */
2307 rtsp_c
->state
= RTSPSTATE_SEND_PACKET
;
2310 /* all data has been sent */
2311 av_freep(&c
->packet_buffer
);
2313 /* send RTP packet directly in UDP */
2315 url_write(c
->rtp_handles
[c
->packet_stream_index
],
2316 c
->buffer_ptr
, len
);
2317 c
->buffer_ptr
+= len
;
2318 /* here we continue as we can send several packets per 10 ms slot */
2321 /* TCP data output */
2322 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2324 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
2325 ff_neterrno() != FF_NETERROR(EINTR
))
2326 /* error : close connection */
2331 c
->buffer_ptr
+= len
;
2333 c
->data_count
+= len
;
2334 update_datarate(&c
->datarate
, c
->data_count
);
2336 c
->stream
->bytes_served
+= len
;
2344 static int http_start_receive_data(HTTPContext
*c
)
2348 if (c
->stream
->feed_opened
)
2351 /* Don't permit writing to this one */
2352 if (c
->stream
->readonly
)
2356 fd
= open(c
->stream
->feed_filename
, O_RDWR
);
2361 c
->stream
->feed_write_index
= ffm_read_write_index(fd
);
2362 c
->stream
->feed_size
= lseek(fd
, 0, SEEK_END
);
2363 lseek(fd
, 0, SEEK_SET
);
2365 /* init buffer input */
2366 c
->buffer_ptr
= c
->buffer
;
2367 c
->buffer_end
= c
->buffer
+ FFM_PACKET_SIZE
;
2368 c
->stream
->feed_opened
= 1;
2372 static int http_receive_data(HTTPContext
*c
)
2376 if (c
->buffer_end
> c
->buffer_ptr
) {
2379 len
= recv(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2381 if (ff_neterrno() != FF_NETERROR(EAGAIN
) &&
2382 ff_neterrno() != FF_NETERROR(EINTR
))
2383 /* error : close connection */
2385 } else if (len
== 0)
2386 /* end of connection : close it */
2389 c
->buffer_ptr
+= len
;
2390 c
->data_count
+= len
;
2391 update_datarate(&c
->datarate
, c
->data_count
);
2395 if (c
->buffer_ptr
- c
->buffer
>= 2 && c
->data_count
> FFM_PACKET_SIZE
) {
2396 if (c
->buffer
[0] != 'f' ||
2397 c
->buffer
[1] != 'm') {
2398 http_log("Feed stream has become desynchronized -- disconnecting\n");
2403 if (c
->buffer_ptr
>= c
->buffer_end
) {
2404 FFStream
*feed
= c
->stream
;
2405 /* a packet has been received : write it in the store, except
2407 if (c
->data_count
> FFM_PACKET_SIZE
) {
2409 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2410 /* XXX: use llseek or url_seek */
2411 lseek(c
->feed_fd
, feed
->feed_write_index
, SEEK_SET
);
2412 write(c
->feed_fd
, c
->buffer
, FFM_PACKET_SIZE
);
2414 feed
->feed_write_index
+= FFM_PACKET_SIZE
;
2415 /* update file size */
2416 if (feed
->feed_write_index
> c
->stream
->feed_size
)
2417 feed
->feed_size
= feed
->feed_write_index
;
2419 /* handle wrap around if max file size reached */
2420 if (c
->stream
->feed_max_size
&& feed
->feed_write_index
>= c
->stream
->feed_max_size
)
2421 feed
->feed_write_index
= FFM_PACKET_SIZE
;
2424 ffm_write_write_index(c
->feed_fd
, feed
->feed_write_index
);
2426 /* wake up any waiting connections */
2427 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
2428 if (c1
->state
== HTTPSTATE_WAIT_FEED
&&
2429 c1
->stream
->feed
== c
->stream
->feed
)
2430 c1
->state
= HTTPSTATE_SEND_DATA
;
2433 /* We have a header in our hands that contains useful data */
2435 AVInputFormat
*fmt_in
;
2438 memset(&s
, 0, sizeof(s
));
2440 url_open_buf(&s
.pb
, c
->buffer
, c
->buffer_end
- c
->buffer
, URL_RDONLY
);
2441 s
.pb
->is_streamed
= 1;
2443 /* use feed output format name to find corresponding input format */
2444 fmt_in
= av_find_input_format(feed
->fmt
->name
);
2448 if (fmt_in
->priv_data_size
> 0) {
2449 s
.priv_data
= av_mallocz(fmt_in
->priv_data_size
);
2455 if (fmt_in
->read_header(&s
, 0) < 0) {
2456 av_freep(&s
.priv_data
);
2460 /* Now we have the actual streams */
2461 if (s
.nb_streams
!= feed
->nb_streams
) {
2462 av_freep(&s
.priv_data
);
2465 for (i
= 0; i
< s
.nb_streams
; i
++)
2466 memcpy(feed
->streams
[i
]->codec
,
2467 s
.streams
[i
]->codec
, sizeof(AVCodecContext
));
2468 av_freep(&s
.priv_data
);
2470 c
->buffer_ptr
= c
->buffer
;
2475 c
->stream
->feed_opened
= 0;
2480 /********************************************************************/
2483 static void rtsp_reply_header(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2490 switch(error_number
) {
2491 case RTSP_STATUS_OK
:
2494 case RTSP_STATUS_METHOD
:
2495 str
= "Method Not Allowed";
2497 case RTSP_STATUS_BANDWIDTH
:
2498 str
= "Not Enough Bandwidth";
2500 case RTSP_STATUS_SESSION
:
2501 str
= "Session Not Found";
2503 case RTSP_STATUS_STATE
:
2504 str
= "Method Not Valid in This State";
2506 case RTSP_STATUS_AGGREGATE
:
2507 str
= "Aggregate operation not allowed";
2509 case RTSP_STATUS_ONLY_AGGREGATE
:
2510 str
= "Only aggregate operation allowed";
2512 case RTSP_STATUS_TRANSPORT
:
2513 str
= "Unsupported transport";
2515 case RTSP_STATUS_INTERNAL
:
2516 str
= "Internal Server Error";
2518 case RTSP_STATUS_SERVICE
:
2519 str
= "Service Unavailable";
2521 case RTSP_STATUS_VERSION
:
2522 str
= "RTSP Version not supported";
2525 str
= "Unknown Error";
2529 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", error_number
, str
);
2530 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2532 /* output GMT time */
2536 p
= buf2
+ strlen(p
) - 1;
2539 url_fprintf(c
->pb
, "Date: %s GMT\r\n", buf2
);
2542 static void rtsp_reply_error(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2544 rtsp_reply_header(c
, error_number
);
2545 url_fprintf(c
->pb
, "\r\n");
2548 static int rtsp_parse_request(HTTPContext
*c
)
2550 const char *p
, *p1
, *p2
;
2556 RTSPHeader header1
, *header
= &header1
;
2558 c
->buffer_ptr
[0] = '\0';
2561 get_word(cmd
, sizeof(cmd
), &p
);
2562 get_word(url
, sizeof(url
), &p
);
2563 get_word(protocol
, sizeof(protocol
), &p
);
2565 av_strlcpy(c
->method
, cmd
, sizeof(c
->method
));
2566 av_strlcpy(c
->url
, url
, sizeof(c
->url
));
2567 av_strlcpy(c
->protocol
, protocol
, sizeof(c
->protocol
));
2569 if (url_open_dyn_buf(&c
->pb
) < 0) {
2570 /* XXX: cannot do more */
2571 c
->pb
= NULL
; /* safety */
2575 /* check version name */
2576 if (strcmp(protocol
, "RTSP/1.0") != 0) {
2577 rtsp_reply_error(c
, RTSP_STATUS_VERSION
);
2581 /* parse each header line */
2582 memset(header
, 0, sizeof(RTSPHeader
));
2583 /* skip to next line */
2584 while (*p
!= '\n' && *p
!= '\0')
2588 while (*p
!= '\0') {
2589 p1
= strchr(p
, '\n');
2593 if (p2
> p
&& p2
[-1] == '\r')
2595 /* skip empty line */
2599 if (len
> sizeof(line
) - 1)
2600 len
= sizeof(line
) - 1;
2601 memcpy(line
, p
, len
);
2603 rtsp_parse_line(header
, line
);
2607 /* handle sequence number */
2608 c
->seq
= header
->seq
;
2610 if (!strcmp(cmd
, "DESCRIBE"))
2611 rtsp_cmd_describe(c
, url
);
2612 else if (!strcmp(cmd
, "OPTIONS"))
2613 rtsp_cmd_options(c
, url
);
2614 else if (!strcmp(cmd
, "SETUP"))
2615 rtsp_cmd_setup(c
, url
, header
);
2616 else if (!strcmp(cmd
, "PLAY"))
2617 rtsp_cmd_play(c
, url
, header
);
2618 else if (!strcmp(cmd
, "PAUSE"))
2619 rtsp_cmd_pause(c
, url
, header
);
2620 else if (!strcmp(cmd
, "TEARDOWN"))
2621 rtsp_cmd_teardown(c
, url
, header
);
2623 rtsp_reply_error(c
, RTSP_STATUS_METHOD
);
2626 len
= url_close_dyn_buf(c
->pb
, &c
->pb_buffer
);
2627 c
->pb
= NULL
; /* safety */
2629 /* XXX: cannot do more */
2632 c
->buffer_ptr
= c
->pb_buffer
;
2633 c
->buffer_end
= c
->pb_buffer
+ len
;
2634 c
->state
= RTSPSTATE_SEND_REPLY
;
2638 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
2639 struct in_addr my_ip
)
2641 AVFormatContext
*avc
;
2642 AVStream avs
[MAX_STREAMS
];
2645 avc
= av_alloc_format_context();
2649 if (stream
->title
[0] != 0) {
2650 av_strlcpy(avc
->title
, stream
->title
, sizeof(avc
->title
));
2652 av_strlcpy(avc
->title
, "No Title", sizeof(avc
->title
));
2654 avc
->nb_streams
= stream
->nb_streams
;
2655 if (stream
->is_multicast
) {
2656 snprintf(avc
->filename
, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2657 inet_ntoa(stream
->multicast_ip
),
2658 stream
->multicast_port
, stream
->multicast_ttl
);
2661 for(i
= 0; i
< stream
->nb_streams
; i
++) {
2662 avc
->streams
[i
] = &avs
[i
];
2663 avc
->streams
[i
]->codec
= stream
->streams
[i
]->codec
;
2665 *pbuffer
= av_mallocz(2048);
2666 avf_sdp_create(&avc
, 1, *pbuffer
, 2048);
2669 return strlen(*pbuffer
);
2672 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
)
2674 // rtsp_reply_header(c, RTSP_STATUS_OK);
2675 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK
, "OK");
2676 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2677 url_fprintf(c
->pb
, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2678 url_fprintf(c
->pb
, "\r\n");
2681 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
)
2687 int content_length
, len
;
2688 struct sockaddr_in my_addr
;
2690 /* find which url is asked */
2691 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2696 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2697 if (!stream
->is_feed
&&
2698 stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp") &&
2699 !strcmp(path
, stream
->filename
)) {
2703 /* no stream found */
2704 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2708 /* prepare the media description in sdp format */
2710 /* get the host IP */
2711 len
= sizeof(my_addr
);
2712 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
2713 content_length
= prepare_sdp_description(stream
, &content
, my_addr
.sin_addr
);
2714 if (content_length
< 0) {
2715 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2718 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2719 url_fprintf(c
->pb
, "Content-Type: application/sdp\r\n");
2720 url_fprintf(c
->pb
, "Content-Length: %d\r\n", content_length
);
2721 url_fprintf(c
->pb
, "\r\n");
2722 put_buffer(c
->pb
, content
, content_length
);
2725 static HTTPContext
*find_rtp_session(const char *session_id
)
2729 if (session_id
[0] == '\0')
2732 for(c
= first_http_ctx
; c
!= NULL
; c
= c
->next
) {
2733 if (!strcmp(c
->session_id
, session_id
))
2739 static RTSPTransportField
*find_transport(RTSPHeader
*h
, enum RTSPProtocol protocol
)
2741 RTSPTransportField
*th
;
2744 for(i
=0;i
<h
->nb_transports
;i
++) {
2745 th
= &h
->transports
[i
];
2746 if (th
->protocol
== protocol
)
2752 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
,
2756 int stream_index
, port
;
2761 RTSPTransportField
*th
;
2762 struct sockaddr_in dest_addr
;
2763 RTSPActionServerSetup setup
;
2765 /* find which url is asked */
2766 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2771 /* now check each stream */
2772 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2773 if (!stream
->is_feed
&&
2774 stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
2775 /* accept aggregate filenames only if single stream */
2776 if (!strcmp(path
, stream
->filename
)) {
2777 if (stream
->nb_streams
!= 1) {
2778 rtsp_reply_error(c
, RTSP_STATUS_AGGREGATE
);
2785 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
2787 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2788 stream
->filename
, stream_index
);
2789 if (!strcmp(path
, buf
))
2794 /* no stream found */
2795 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2799 /* generate session id if needed */
2800 if (h
->session_id
[0] == '\0')
2801 snprintf(h
->session_id
, sizeof(h
->session_id
), "%08x%08x",
2802 av_random(&random_state
), av_random(&random_state
));
2804 /* find rtp session, and create it if none found */
2805 rtp_c
= find_rtp_session(h
->session_id
);
2807 /* always prefer UDP */
2808 th
= find_transport(h
, RTSP_PROTOCOL_RTP_UDP
);
2810 th
= find_transport(h
, RTSP_PROTOCOL_RTP_TCP
);
2812 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2817 rtp_c
= rtp_new_connection(&c
->from_addr
, stream
, h
->session_id
,
2820 rtsp_reply_error(c
, RTSP_STATUS_BANDWIDTH
);
2824 /* open input stream */
2825 if (open_input_stream(rtp_c
, "") < 0) {
2826 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2831 /* test if stream is OK (test needed because several SETUP needs
2832 to be done for a given file) */
2833 if (rtp_c
->stream
!= stream
) {
2834 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
);
2838 /* test if stream is already set up */
2839 if (rtp_c
->rtp_ctx
[stream_index
]) {
2840 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2844 /* check transport */
2845 th
= find_transport(h
, rtp_c
->rtp_protocol
);
2846 if (!th
|| (th
->protocol
== RTSP_PROTOCOL_RTP_UDP
&&
2847 th
->client_port_min
<= 0)) {
2848 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2852 /* setup default options */
2853 setup
.transport_option
[0] = '\0';
2854 dest_addr
= rtp_c
->from_addr
;
2855 dest_addr
.sin_port
= htons(th
->client_port_min
);
2858 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, c
) < 0) {
2859 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2863 /* now everything is OK, so we can send the connection parameters */
2864 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2866 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2868 switch(rtp_c
->rtp_protocol
) {
2869 case RTSP_PROTOCOL_RTP_UDP
:
2870 port
= rtp_get_local_port(rtp_c
->rtp_handles
[stream_index
]);
2871 url_fprintf(c
->pb
, "Transport: RTP/AVP/UDP;unicast;"
2872 "client_port=%d-%d;server_port=%d-%d",
2873 th
->client_port_min
, th
->client_port_min
+ 1,
2876 case RTSP_PROTOCOL_RTP_TCP
:
2877 url_fprintf(c
->pb
, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2878 stream_index
* 2, stream_index
* 2 + 1);
2883 if (setup
.transport_option
[0] != '\0')
2884 url_fprintf(c
->pb
, ";%s", setup
.transport_option
);
2885 url_fprintf(c
->pb
, "\r\n");
2888 url_fprintf(c
->pb
, "\r\n");
2892 /* find an rtp connection by using the session ID. Check consistency
2894 static HTTPContext
*find_rtp_session_with_url(const char *url
,
2895 const char *session_id
)
2903 rtp_c
= find_rtp_session(session_id
);
2907 /* find which url is asked */
2908 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2912 if(!strcmp(path
, rtp_c
->stream
->filename
)) return rtp_c
;
2913 for(s
=0; s
<rtp_c
->stream
->nb_streams
; ++s
) {
2914 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2915 rtp_c
->stream
->filename
, s
);
2916 if(!strncmp(path
, buf
, sizeof(buf
))) {
2917 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2924 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2928 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2930 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
2934 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
2935 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
&&
2936 rtp_c
->state
!= HTTPSTATE_READY
) {
2937 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2942 /* XXX: seek in stream */
2943 if (h
->range_start
!= AV_NOPTS_VALUE
) {
2944 printf("range_start=%0.3f\n", (double)h
->range_start
/ AV_TIME_BASE
);
2945 av_seek_frame(rtp_c
->fmt_in
, -1, h
->range_start
);
2949 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
2951 /* now everything is OK, so we can send the connection parameters */
2952 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2954 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2955 url_fprintf(c
->pb
, "\r\n");
2958 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2962 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2964 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
2968 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
2969 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
) {
2970 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2974 rtp_c
->state
= HTTPSTATE_READY
;
2975 rtp_c
->first_pts
= AV_NOPTS_VALUE
;
2976 /* now everything is OK, so we can send the connection parameters */
2977 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2979 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2980 url_fprintf(c
->pb
, "\r\n");
2983 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2986 char session_id
[32];
2988 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2990 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
2994 av_strlcpy(session_id
, rtp_c
->session_id
, sizeof(session_id
));
2996 /* abort the session */
2997 close_connection(rtp_c
);
2999 /* now everything is OK, so we can send the connection parameters */
3000 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3002 url_fprintf(c
->pb
, "Session: %s\r\n", session_id
);
3003 url_fprintf(c
->pb
, "\r\n");
3007 /********************************************************************/
3010 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
3011 FFStream
*stream
, const char *session_id
,
3012 enum RTSPProtocol rtp_protocol
)
3014 HTTPContext
*c
= NULL
;
3015 const char *proto_str
;
3017 /* XXX: should output a warning page when coming
3018 close to the connection limit */
3019 if (nb_connections
>= nb_max_connections
)
3022 /* add a new connection */
3023 c
= av_mallocz(sizeof(HTTPContext
));
3028 c
->poll_entry
= NULL
;
3029 c
->from_addr
= *from_addr
;
3030 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
3031 c
->buffer
= av_malloc(c
->buffer_size
);
3036 av_strlcpy(c
->session_id
, session_id
, sizeof(c
->session_id
));
3037 c
->state
= HTTPSTATE_READY
;
3038 c
->is_packetized
= 1;
3039 c
->rtp_protocol
= rtp_protocol
;
3041 /* protocol is shown in statistics */
3042 switch(c
->rtp_protocol
) {
3043 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3044 proto_str
= "MCAST";
3046 case RTSP_PROTOCOL_RTP_UDP
:
3049 case RTSP_PROTOCOL_RTP_TCP
:
3056 av_strlcpy(c
->protocol
, "RTP/", sizeof(c
->protocol
));
3057 av_strlcat(c
->protocol
, proto_str
, sizeof(c
->protocol
));
3059 current_bandwidth
+= stream
->bandwidth
;
3061 c
->next
= first_http_ctx
;
3073 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3074 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3076 static int rtp_new_av_stream(HTTPContext
*c
,
3077 int stream_index
, struct sockaddr_in
*dest_addr
,
3078 HTTPContext
*rtsp_c
)
3080 AVFormatContext
*ctx
;
3086 int max_packet_size
;
3088 /* now we can open the relevant output stream */
3089 ctx
= av_alloc_format_context();
3092 ctx
->oformat
= guess_format("rtp", NULL
, NULL
);
3094 st
= av_mallocz(sizeof(AVStream
));
3097 st
->codec
= avcodec_alloc_context();
3098 ctx
->nb_streams
= 1;
3099 ctx
->streams
[0] = st
;
3101 if (!c
->stream
->feed
||
3102 c
->stream
->feed
== c
->stream
)
3103 memcpy(st
, c
->stream
->streams
[stream_index
], sizeof(AVStream
));
3106 c
->stream
->feed
->streams
[c
->stream
->feed_streams
[stream_index
]],
3108 st
->priv_data
= NULL
;
3110 /* build destination RTP address */
3111 ipaddr
= inet_ntoa(dest_addr
->sin_addr
);
3113 switch(c
->rtp_protocol
) {
3114 case RTSP_PROTOCOL_RTP_UDP
:
3115 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3118 /* XXX: also pass as parameter to function ? */
3119 if (c
->stream
->is_multicast
) {
3121 ttl
= c
->stream
->multicast_ttl
;
3124 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3125 "rtp://%s:%d?multicast=1&ttl=%d",
3126 ipaddr
, ntohs(dest_addr
->sin_port
), ttl
);
3128 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3129 "rtp://%s:%d", ipaddr
, ntohs(dest_addr
->sin_port
));
3132 if (url_open(&h
, ctx
->filename
, URL_WRONLY
) < 0)
3134 c
->rtp_handles
[stream_index
] = h
;
3135 max_packet_size
= url_get_max_packet_size(h
);
3137 case RTSP_PROTOCOL_RTP_TCP
:
3140 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
3146 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3147 ipaddr
, ntohs(dest_addr
->sin_port
),
3149 c
->stream
->filename
, stream_index
, c
->protocol
);
3151 /* normally, no packets should be output here, but the packet size may be checked */
3152 if (url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
) < 0) {
3153 /* XXX: close stream */
3156 av_set_parameters(ctx
, NULL
);
3157 if (av_write_header(ctx
) < 0) {
3164 url_close_dyn_buf(ctx
->pb
, &dummy_buf
);
3167 c
->rtp_ctx
[stream_index
] = ctx
;
3171 /********************************************************************/
3172 /* ffserver initialization */
3174 static AVStream
*add_av_stream1(FFStream
*stream
, AVCodecContext
*codec
)
3178 fst
= av_mallocz(sizeof(AVStream
));
3181 fst
->codec
= avcodec_alloc_context();
3182 fst
->priv_data
= av_mallocz(sizeof(FeedData
));
3183 memcpy(fst
->codec
, codec
, sizeof(AVCodecContext
));
3184 fst
->codec
->coded_frame
= &dummy_frame
;
3185 fst
->index
= stream
->nb_streams
;
3186 av_set_pts_info(fst
, 33, 1, 90000);
3187 stream
->streams
[stream
->nb_streams
++] = fst
;
3191 /* return the stream number in the feed */
3192 static int add_av_stream(FFStream
*feed
, AVStream
*st
)
3195 AVCodecContext
*av
, *av1
;
3199 for(i
=0;i
<feed
->nb_streams
;i
++) {
3200 st
= feed
->streams
[i
];
3202 if (av1
->codec_id
== av
->codec_id
&&
3203 av1
->codec_type
== av
->codec_type
&&
3204 av1
->bit_rate
== av
->bit_rate
) {
3206 switch(av
->codec_type
) {
3207 case CODEC_TYPE_AUDIO
:
3208 if (av1
->channels
== av
->channels
&&
3209 av1
->sample_rate
== av
->sample_rate
)
3212 case CODEC_TYPE_VIDEO
:
3213 if (av1
->width
== av
->width
&&
3214 av1
->height
== av
->height
&&
3215 av1
->time_base
.den
== av
->time_base
.den
&&
3216 av1
->time_base
.num
== av
->time_base
.num
&&
3217 av1
->gop_size
== av
->gop_size
)
3226 fst
= add_av_stream1(feed
, av
);
3229 return feed
->nb_streams
- 1;
3234 static void remove_stream(FFStream
*stream
)
3238 while (*ps
!= NULL
) {
3246 /* specific mpeg4 handling : we extract the raw parameters */
3247 static void extract_mpeg4_header(AVFormatContext
*infile
)
3249 int mpeg4_count
, i
, size
;
3255 for(i
=0;i
<infile
->nb_streams
;i
++) {
3256 st
= infile
->streams
[i
];
3257 if (st
->codec
->codec_id
== CODEC_ID_MPEG4
&&
3258 st
->codec
->extradata_size
== 0) {
3265 printf("MPEG4 without extra data: trying to find header in %s\n", infile
->filename
);
3266 while (mpeg4_count
> 0) {
3267 if (av_read_packet(infile
, &pkt
) < 0)
3269 st
= infile
->streams
[pkt
.stream_index
];
3270 if (st
->codec
->codec_id
== CODEC_ID_MPEG4
&&
3271 st
->codec
->extradata_size
== 0) {
3272 av_freep(&st
->codec
->extradata
);
3273 /* fill extradata with the header */
3274 /* XXX: we make hard suppositions here ! */
3276 while (p
< pkt
.data
+ pkt
.size
- 4) {
3277 /* stop when vop header is found */
3278 if (p
[0] == 0x00 && p
[1] == 0x00 &&
3279 p
[2] == 0x01 && p
[3] == 0xb6) {
3280 size
= p
- pkt
.data
;
3281 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
3282 st
->codec
->extradata
= av_malloc(size
);
3283 st
->codec
->extradata_size
= size
;
3284 memcpy(st
->codec
->extradata
, pkt
.data
, size
);
3291 av_free_packet(&pkt
);
3295 /* compute the needed AVStream for each file */
3296 static void build_file_streams(void)
3298 FFStream
*stream
, *stream_next
;
3299 AVFormatContext
*infile
;
3302 /* gather all streams */
3303 for(stream
= first_stream
; stream
!= NULL
; stream
= stream_next
) {
3304 stream_next
= stream
->next
;
3305 if (stream
->stream_type
== STREAM_TYPE_LIVE
&&
3307 /* the stream comes from a file */
3308 /* try to open the file */
3310 stream
->ap_in
= av_mallocz(sizeof(AVFormatParameters
));
3311 if (stream
->fmt
&& !strcmp(stream
->fmt
->name
, "rtp")) {
3312 /* specific case : if transport stream output to RTP,
3313 we use a raw transport stream reader */
3314 stream
->ap_in
->mpeg2ts_raw
= 1;
3315 stream
->ap_in
->mpeg2ts_compute_pcr
= 1;
3318 if (av_open_input_file(&infile
, stream
->feed_filename
,
3319 stream
->ifmt
, 0, stream
->ap_in
) < 0) {
3320 http_log("%s not found", stream
->feed_filename
);
3321 /* remove stream (no need to spend more time on it) */
3323 remove_stream(stream
);
3325 /* find all the AVStreams inside and reference them in
3327 if (av_find_stream_info(infile
) < 0) {
3328 http_log("Could not find codec parameters from '%s'",
3329 stream
->feed_filename
);
3330 av_close_input_file(infile
);
3333 extract_mpeg4_header(infile
);
3335 for(i
=0;i
<infile
->nb_streams
;i
++)
3336 add_av_stream1(stream
, infile
->streams
[i
]->codec
);
3338 av_close_input_file(infile
);
3344 /* compute the needed AVStream for each feed */
3345 static void build_feed_streams(void)
3347 FFStream
*stream
, *feed
;
3350 /* gather all streams */
3351 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3352 feed
= stream
->feed
;
3354 if (!stream
->is_feed
) {
3355 /* we handle a stream coming from a feed */
3356 for(i
=0;i
<stream
->nb_streams
;i
++)
3357 stream
->feed_streams
[i
] = add_av_stream(feed
, stream
->streams
[i
]);
3362 /* gather all streams */
3363 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3364 feed
= stream
->feed
;
3366 if (stream
->is_feed
) {
3367 for(i
=0;i
<stream
->nb_streams
;i
++)
3368 stream
->feed_streams
[i
] = i
;
3373 /* create feed files if needed */
3374 for(feed
= first_feed
; feed
!= NULL
; feed
= feed
->next_feed
) {
3377 if (url_exist(feed
->feed_filename
)) {
3378 /* See if it matches */
3382 if (av_open_input_file(&s
, feed
->feed_filename
, NULL
, FFM_PACKET_SIZE
, NULL
) >= 0) {
3383 /* Now see if it matches */
3384 if (s
->nb_streams
== feed
->nb_streams
) {
3386 for(i
=0;i
<s
->nb_streams
;i
++) {
3388 sf
= feed
->streams
[i
];
3391 if (sf
->index
!= ss
->index
||
3393 printf("Index & Id do not match for stream %d (%s)\n",
3394 i
, feed
->feed_filename
);
3397 AVCodecContext
*ccf
, *ccs
;
3401 #define CHECK_CODEC(x) (ccf->x != ccs->x)
3403 if (CHECK_CODEC(codec
) || CHECK_CODEC(codec_type
)) {
3404 printf("Codecs do not match for stream %d\n", i
);
3406 } else if (CHECK_CODEC(bit_rate
) || CHECK_CODEC(flags
)) {
3407 printf("Codec bitrates do not match for stream %d\n", i
);
3409 } else if (ccf
->codec_type
== CODEC_TYPE_VIDEO
) {
3410 if (CHECK_CODEC(time_base
.den
) ||
3411 CHECK_CODEC(time_base
.num
) ||
3412 CHECK_CODEC(width
) ||
3413 CHECK_CODEC(height
)) {
3414 printf("Codec width, height and framerate do not match for stream %d\n", i
);
3417 } else if (ccf
->codec_type
== CODEC_TYPE_AUDIO
) {
3418 if (CHECK_CODEC(sample_rate
) ||
3419 CHECK_CODEC(channels
) ||
3420 CHECK_CODEC(frame_size
)) {
3421 printf("Codec sample_rate, channels, frame_size do not match for stream %d\n", i
);
3425 printf("Unknown codec type\n");
3433 printf("Deleting feed file '%s' as stream counts differ (%d != %d)\n",
3434 feed
->feed_filename
, s
->nb_streams
, feed
->nb_streams
);
3436 av_close_input_file(s
);
3438 printf("Deleting feed file '%s' as it appears to be corrupt\n",
3439 feed
->feed_filename
);
3442 if (feed
->readonly
) {
3443 printf("Unable to delete feed file '%s' as it is marked readonly\n",
3444 feed
->feed_filename
);
3447 unlink(feed
->feed_filename
);
3450 if (!url_exist(feed
->feed_filename
)) {
3451 AVFormatContext s1
, *s
= &s1
;
3453 if (feed
->readonly
) {
3454 printf("Unable to create feed file '%s' as it is marked readonly\n",
3455 feed
->feed_filename
);
3459 /* only write the header of the ffm file */
3460 if (url_fopen(&s
->pb
, feed
->feed_filename
, URL_WRONLY
) < 0) {
3461 fprintf(stderr
, "Could not open output feed file '%s'\n",
3462 feed
->feed_filename
);
3465 s
->oformat
= feed
->fmt
;
3466 s
->nb_streams
= feed
->nb_streams
;
3467 for(i
=0;i
<s
->nb_streams
;i
++) {
3469 st
= feed
->streams
[i
];
3472 av_set_parameters(s
, NULL
);
3473 if (av_write_header(s
) < 0) {
3474 fprintf(stderr
, "Container doesn't supports the required parameters\n");
3477 /* XXX: need better api */
3478 av_freep(&s
->priv_data
);
3481 /* get feed size and write index */
3482 fd
= open(feed
->feed_filename
, O_RDONLY
);
3484 fprintf(stderr
, "Could not open output feed file '%s'\n",
3485 feed
->feed_filename
);
3489 feed
->feed_write_index
= ffm_read_write_index(fd
);
3490 feed
->feed_size
= lseek(fd
, 0, SEEK_END
);
3491 /* ensure that we do not wrap before the end of file */
3492 if (feed
->feed_max_size
&& feed
->feed_max_size
< feed
->feed_size
)
3493 feed
->feed_max_size
= feed
->feed_size
;
3499 /* compute the bandwidth used by each stream */
3500 static void compute_bandwidth(void)
3505 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
3507 for(i
=0;i
<stream
->nb_streams
;i
++) {
3508 AVStream
*st
= stream
->streams
[i
];
3509 switch(st
->codec
->codec_type
) {
3510 case CODEC_TYPE_AUDIO
:
3511 case CODEC_TYPE_VIDEO
:
3512 bandwidth
+= st
->codec
->bit_rate
;
3518 stream
->bandwidth
= (bandwidth
+ 999) / 1000;
3522 static void get_arg(char *buf
, int buf_size
, const char **pp
)
3529 while (isspace(*p
)) p
++;
3532 if (*p
== '\"' || *p
== '\'')
3544 if ((q
- buf
) < buf_size
- 1)
3549 if (quote
&& *p
== quote
)
3554 /* add a codec and set the default parameters */
3555 static void add_codec(FFStream
*stream
, AVCodecContext
*av
)
3559 /* compute default parameters */
3560 switch(av
->codec_type
) {
3561 case CODEC_TYPE_AUDIO
:
3562 if (av
->bit_rate
== 0)
3563 av
->bit_rate
= 64000;
3564 if (av
->sample_rate
== 0)
3565 av
->sample_rate
= 22050;
3566 if (av
->channels
== 0)
3569 case CODEC_TYPE_VIDEO
:
3570 if (av
->bit_rate
== 0)
3571 av
->bit_rate
= 64000;
3572 if (av
->time_base
.num
== 0){
3573 av
->time_base
.den
= 5;
3574 av
->time_base
.num
= 1;
3576 if (av
->width
== 0 || av
->height
== 0) {
3580 /* Bitrate tolerance is less for streaming */
3581 if (av
->bit_rate_tolerance
== 0)
3582 av
->bit_rate_tolerance
= av
->bit_rate
/ 4;
3587 if (av
->max_qdiff
== 0)
3589 av
->qcompress
= 0.5;
3592 if (!av
->nsse_weight
)
3593 av
->nsse_weight
= 8;
3595 av
->frame_skip_cmp
= FF_CMP_DCTMAX
;
3596 av
->me_method
= ME_EPZS
;
3597 av
->rc_buffer_aggressivity
= 1.0;
3600 av
->rc_eq
= "tex^qComp";
3601 if (!av
->i_quant_factor
)
3602 av
->i_quant_factor
= -0.8;
3603 if (!av
->b_quant_factor
)
3604 av
->b_quant_factor
= 1.25;
3605 if (!av
->b_quant_offset
)
3606 av
->b_quant_offset
= 1.25;
3607 if (!av
->rc_max_rate
)
3608 av
->rc_max_rate
= av
->bit_rate
* 2;
3610 if (av
->rc_max_rate
&& !av
->rc_buffer_size
) {
3611 av
->rc_buffer_size
= av
->rc_max_rate
;
3620 st
= av_mallocz(sizeof(AVStream
));
3623 st
->codec
= avcodec_alloc_context();
3624 stream
->streams
[stream
->nb_streams
++] = st
;
3625 memcpy(st
->codec
, av
, sizeof(AVCodecContext
));
3628 static int opt_audio_codec(const char *arg
)
3630 AVCodec
*p
= avcodec_find_encoder_by_name(arg
);
3632 if (p
== NULL
|| p
->type
!= CODEC_TYPE_AUDIO
)
3633 return CODEC_ID_NONE
;
3638 static int opt_video_codec(const char *arg
)
3640 AVCodec
*p
= avcodec_find_encoder_by_name(arg
);
3642 if (p
== NULL
|| p
->type
!= CODEC_TYPE_VIDEO
)
3643 return CODEC_ID_NONE
;
3648 /* simplistic plugin support */
3651 static void load_module(const char *filename
)
3654 void (*init_func
)(void);
3655 dll
= dlopen(filename
, RTLD_NOW
);
3657 fprintf(stderr
, "Could not load module '%s' - %s\n",
3658 filename
, dlerror());
3662 init_func
= dlsym(dll
, "ffserver_module_init");
3665 "%s: init function 'ffserver_module_init()' not found\n",
3674 static int parse_ffconfig(const char *filename
)
3681 int val
, errors
, line_num
;
3682 FFStream
**last_stream
, *stream
, *redirect
;
3683 FFStream
**last_feed
, *feed
;
3684 AVCodecContext audio_enc
, video_enc
;
3685 int audio_id
, video_id
;
3687 f
= fopen(filename
, "r");
3695 first_stream
= NULL
;
3696 last_stream
= &first_stream
;
3698 last_feed
= &first_feed
;
3702 audio_id
= CODEC_ID_NONE
;
3703 video_id
= CODEC_ID_NONE
;
3705 if (fgets(line
, sizeof(line
), f
) == NULL
)
3711 if (*p
== '\0' || *p
== '#')
3714 get_arg(cmd
, sizeof(cmd
), &p
);
3716 if (!strcasecmp(cmd
, "Port")) {
3717 get_arg(arg
, sizeof(arg
), &p
);
3719 if (val
< 1 || val
> 65536) {
3720 fprintf(stderr
, "%s:%d: Invalid port: %s\n",
3721 filename
, line_num
, arg
);
3724 my_http_addr
.sin_port
= htons(val
);
3725 } else if (!strcasecmp(cmd
, "BindAddress")) {
3726 get_arg(arg
, sizeof(arg
), &p
);
3727 if (resolve_host(&my_http_addr
.sin_addr
, arg
) != 0) {
3728 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
3729 filename
, line_num
, arg
);
3732 } else if (!strcasecmp(cmd
, "NoDaemon")) {
3733 ffserver_daemon
= 0;
3734 } else if (!strcasecmp(cmd
, "RTSPPort")) {
3735 get_arg(arg
, sizeof(arg
), &p
);
3737 if (val
< 1 || val
> 65536) {
3738 fprintf(stderr
, "%s:%d: Invalid port: %s\n",
3739 filename
, line_num
, arg
);
3742 my_rtsp_addr
.sin_port
= htons(atoi(arg
));
3743 } else if (!strcasecmp(cmd
, "RTSPBindAddress")) {
3744 get_arg(arg
, sizeof(arg
), &p
);
3745 if (resolve_host(&my_rtsp_addr
.sin_addr
, arg
) != 0) {
3746 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
3747 filename
, line_num
, arg
);
3750 } else if (!strcasecmp(cmd
, "MaxClients")) {
3751 get_arg(arg
, sizeof(arg
), &p
);
3753 if (val
< 1 || val
> HTTP_MAX_CONNECTIONS
) {
3754 fprintf(stderr
, "%s:%d: Invalid MaxClients: %s\n",
3755 filename
, line_num
, arg
);
3758 nb_max_connections
= val
;
3760 } else if (!strcasecmp(cmd
, "MaxBandwidth")) {
3761 get_arg(arg
, sizeof(arg
), &p
);
3763 if (val
< 10 || val
> 100000) {
3764 fprintf(stderr
, "%s:%d: Invalid MaxBandwidth: %s\n",
3765 filename
, line_num
, arg
);
3768 max_bandwidth
= val
;
3769 } else if (!strcasecmp(cmd
, "CustomLog")) {
3770 get_arg(logfilename
, sizeof(logfilename
), &p
);
3771 } else if (!strcasecmp(cmd
, "<Feed")) {
3772 /*********************************************/
3773 /* Feed related options */
3775 if (stream
|| feed
) {
3776 fprintf(stderr
, "%s:%d: Already in a tag\n",
3777 filename
, line_num
);
3779 feed
= av_mallocz(sizeof(FFStream
));
3780 /* add in stream list */
3781 *last_stream
= feed
;
3782 last_stream
= &feed
->next
;
3783 /* add in feed list */
3785 last_feed
= &feed
->next_feed
;
3787 get_arg(feed
->filename
, sizeof(feed
->filename
), &p
);
3788 q
= strrchr(feed
->filename
, '>');
3791 feed
->fmt
= guess_format("ffm", NULL
, NULL
);
3792 /* defaut feed file */
3793 snprintf(feed
->feed_filename
, sizeof(feed
->feed_filename
),
3794 "/tmp/%s.ffm", feed
->filename
);
3795 feed
->feed_max_size
= 5 * 1024 * 1024;
3797 feed
->feed
= feed
; /* self feeding :-) */
3799 } else if (!strcasecmp(cmd
, "Launch")) {
3803 feed
->child_argv
= av_mallocz(64 * sizeof(char *));
3805 for (i
= 0; i
< 62; i
++) {
3806 get_arg(arg
, sizeof(arg
), &p
);
3810 feed
->child_argv
[i
] = av_strdup(arg
);
3813 feed
->child_argv
[i
] = av_malloc(30 + strlen(feed
->filename
));
3815 snprintf(feed
->child_argv
[i
], 30+strlen(feed
->filename
),
3817 (my_http_addr
.sin_addr
.s_addr
== INADDR_ANY
) ? "127.0.0.1" :
3818 inet_ntoa(my_http_addr
.sin_addr
),
3819 ntohs(my_http_addr
.sin_port
), feed
->filename
);
3824 fprintf(stdout
, "Launch commandline: ");
3825 for (j
= 0; j
<= i
; j
++)
3826 fprintf(stdout
, "%s ", feed
->child_argv
[j
]);
3827 fprintf(stdout
, "\n");
3830 } else if (!strcasecmp(cmd
, "ReadOnlyFile")) {
3832 get_arg(feed
->feed_filename
, sizeof(feed
->feed_filename
), &p
);
3834 } else if (stream
) {
3835 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3837 } else if (!strcasecmp(cmd
, "File")) {
3839 get_arg(feed
->feed_filename
, sizeof(feed
->feed_filename
), &p
);
3841 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3842 } else if (!strcasecmp(cmd
, "FileMaxSize")) {
3847 get_arg(arg
, sizeof(arg
), &p
);
3849 fsize
= strtod(p1
, (char **)&p1
);
3850 switch(toupper(*p1
)) {
3855 fsize
*= 1024 * 1024;
3858 fsize
*= 1024 * 1024 * 1024;
3861 feed
->feed_max_size
= (int64_t)fsize
;
3863 } else if (!strcasecmp(cmd
, "</Feed>")) {
3865 fprintf(stderr
, "%s:%d: No corresponding <Feed> for </Feed>\n",
3866 filename
, line_num
);
3870 } else if (!strcasecmp(cmd
, "<Stream")) {
3871 /*********************************************/
3872 /* Stream related options */
3874 if (stream
|| feed
) {
3875 fprintf(stderr
, "%s:%d: Already in a tag\n",
3876 filename
, line_num
);
3878 stream
= av_mallocz(sizeof(FFStream
));
3879 *last_stream
= stream
;
3880 last_stream
= &stream
->next
;
3882 get_arg(stream
->filename
, sizeof(stream
->filename
), &p
);
3883 q
= strrchr(stream
->filename
, '>');
3886 stream
->fmt
= guess_stream_format(NULL
, stream
->filename
, NULL
);
3887 memset(&audio_enc
, 0, sizeof(AVCodecContext
));
3888 memset(&video_enc
, 0, sizeof(AVCodecContext
));
3889 audio_id
= CODEC_ID_NONE
;
3890 video_id
= CODEC_ID_NONE
;
3892 audio_id
= stream
->fmt
->audio_codec
;
3893 video_id
= stream
->fmt
->video_codec
;
3896 } else if (!strcasecmp(cmd
, "Feed")) {
3897 get_arg(arg
, sizeof(arg
), &p
);
3902 while (sfeed
!= NULL
) {
3903 if (!strcmp(sfeed
->filename
, arg
))
3905 sfeed
= sfeed
->next_feed
;
3908 fprintf(stderr
, "%s:%d: feed '%s' not defined\n",
3909 filename
, line_num
, arg
);
3911 stream
->feed
= sfeed
;
3913 } else if (!strcasecmp(cmd
, "Format")) {
3914 get_arg(arg
, sizeof(arg
), &p
);
3915 if (!strcmp(arg
, "status")) {
3916 stream
->stream_type
= STREAM_TYPE_STATUS
;
3919 stream
->stream_type
= STREAM_TYPE_LIVE
;
3920 /* jpeg cannot be used here, so use single frame jpeg */
3921 if (!strcmp(arg
, "jpeg"))
3922 strcpy(arg
, "mjpeg");
3923 stream
->fmt
= guess_stream_format(arg
, NULL
, NULL
);
3925 fprintf(stderr
, "%s:%d: Unknown Format: %s\n",
3926 filename
, line_num
, arg
);
3931 audio_id
= stream
->fmt
->audio_codec
;
3932 video_id
= stream
->fmt
->video_codec
;
3934 } else if (!strcasecmp(cmd
, "InputFormat")) {
3935 get_arg(arg
, sizeof(arg
), &p
);
3936 stream
->ifmt
= av_find_input_format(arg
);
3937 if (!stream
->ifmt
) {
3938 fprintf(stderr
, "%s:%d: Unknown input format: %s\n",
3939 filename
, line_num
, arg
);
3941 } else if (!strcasecmp(cmd
, "FaviconURL")) {
3942 if (stream
&& stream
->stream_type
== STREAM_TYPE_STATUS
) {
3943 get_arg(stream
->feed_filename
, sizeof(stream
->feed_filename
), &p
);
3945 fprintf(stderr
, "%s:%d: FaviconURL only permitted for status streams\n",
3946 filename
, line_num
);
3949 } else if (!strcasecmp(cmd
, "Author")) {
3951 get_arg(stream
->author
, sizeof(stream
->author
), &p
);
3952 } else if (!strcasecmp(cmd
, "Comment")) {
3954 get_arg(stream
->comment
, sizeof(stream
->comment
), &p
);
3955 } else if (!strcasecmp(cmd
, "Copyright")) {
3957 get_arg(stream
->copyright
, sizeof(stream
->copyright
), &p
);
3958 } else if (!strcasecmp(cmd
, "Title")) {
3960 get_arg(stream
->title
, sizeof(stream
->title
), &p
);
3961 } else if (!strcasecmp(cmd
, "Preroll")) {
3962 get_arg(arg
, sizeof(arg
), &p
);
3964 stream
->prebuffer
= atof(arg
) * 1000;
3965 } else if (!strcasecmp(cmd
, "StartSendOnKey")) {
3967 stream
->send_on_key
= 1;
3968 } else if (!strcasecmp(cmd
, "AudioCodec")) {
3969 get_arg(arg
, sizeof(arg
), &p
);
3970 audio_id
= opt_audio_codec(arg
);
3971 if (audio_id
== CODEC_ID_NONE
) {
3972 fprintf(stderr
, "%s:%d: Unknown AudioCodec: %s\n",
3973 filename
, line_num
, arg
);
3976 } else if (!strcasecmp(cmd
, "VideoCodec")) {
3977 get_arg(arg
, sizeof(arg
), &p
);
3978 video_id
= opt_video_codec(arg
);
3979 if (video_id
== CODEC_ID_NONE
) {
3980 fprintf(stderr
, "%s:%d: Unknown VideoCodec: %s\n",
3981 filename
, line_num
, arg
);
3984 } else if (!strcasecmp(cmd
, "MaxTime")) {
3985 get_arg(arg
, sizeof(arg
), &p
);
3987 stream
->max_time
= atof(arg
) * 1000;
3988 } else if (!strcasecmp(cmd
, "AudioBitRate")) {
3989 get_arg(arg
, sizeof(arg
), &p
);
3991 audio_enc
.bit_rate
= atoi(arg
) * 1000;
3992 } else if (!strcasecmp(cmd
, "AudioChannels")) {
3993 get_arg(arg
, sizeof(arg
), &p
);
3995 audio_enc
.channels
= atoi(arg
);
3996 } else if (!strcasecmp(cmd
, "AudioSampleRate")) {
3997 get_arg(arg
, sizeof(arg
), &p
);
3999 audio_enc
.sample_rate
= atoi(arg
);
4000 } else if (!strcasecmp(cmd
, "AudioQuality")) {
4001 get_arg(arg
, sizeof(arg
), &p
);
4003 // audio_enc.quality = atof(arg) * 1000;
4005 } else if (!strcasecmp(cmd
, "VideoBitRateRange")) {
4007 int minrate
, maxrate
;
4009 get_arg(arg
, sizeof(arg
), &p
);
4011 if (sscanf(arg
, "%d-%d", &minrate
, &maxrate
) == 2) {
4012 video_enc
.rc_min_rate
= minrate
* 1000;
4013 video_enc
.rc_max_rate
= maxrate
* 1000;
4015 fprintf(stderr
, "%s:%d: Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n",
4016 filename
, line_num
, arg
);
4020 } else if (!strcasecmp(cmd
, "Debug")) {
4022 get_arg(arg
, sizeof(arg
), &p
);
4023 video_enc
.debug
= strtol(arg
,0,0);
4025 } else if (!strcasecmp(cmd
, "Strict")) {
4027 get_arg(arg
, sizeof(arg
), &p
);
4028 video_enc
.strict_std_compliance
= atoi(arg
);
4030 } else if (!strcasecmp(cmd
, "VideoBufferSize")) {
4032 get_arg(arg
, sizeof(arg
), &p
);
4033 video_enc
.rc_buffer_size
= atoi(arg
) * 8*1024;
4035 } else if (!strcasecmp(cmd
, "VideoBitRateTolerance")) {
4037 get_arg(arg
, sizeof(arg
), &p
);
4038 video_enc
.bit_rate_tolerance
= atoi(arg
) * 1000;
4040 } else if (!strcasecmp(cmd
, "VideoBitRate")) {
4041 get_arg(arg
, sizeof(arg
), &p
);
4043 video_enc
.bit_rate
= atoi(arg
) * 1000;
4045 } else if (!strcasecmp(cmd
, "VideoSize")) {
4046 get_arg(arg
, sizeof(arg
), &p
);
4048 av_parse_video_frame_size(&video_enc
.width
, &video_enc
.height
, arg
);
4049 if ((video_enc
.width
% 16) != 0 ||
4050 (video_enc
.height
% 16) != 0) {
4051 fprintf(stderr
, "%s:%d: Image size must be a multiple of 16\n",
4052 filename
, line_num
);
4056 } else if (!strcasecmp(cmd
, "VideoFrameRate")) {
4057 get_arg(arg
, sizeof(arg
), &p
);
4059 video_enc
.time_base
.num
= DEFAULT_FRAME_RATE_BASE
;
4060 video_enc
.time_base
.den
= (int)(strtod(arg
, NULL
) * video_enc
.time_base
.num
);
4062 } else if (!strcasecmp(cmd
, "VideoGopSize")) {
4063 get_arg(arg
, sizeof(arg
), &p
);
4065 video_enc
.gop_size
= atoi(arg
);
4066 } else if (!strcasecmp(cmd
, "VideoIntraOnly")) {
4068 video_enc
.gop_size
= 1;
4069 } else if (!strcasecmp(cmd
, "VideoHighQuality")) {
4071 video_enc
.mb_decision
= FF_MB_DECISION_BITS
;
4072 } else if (!strcasecmp(cmd
, "Video4MotionVector")) {
4074 video_enc
.mb_decision
= FF_MB_DECISION_BITS
; //FIXME remove
4075 video_enc
.flags
|= CODEC_FLAG_4MV
;
4077 } else if (!strcasecmp(cmd
, "VideoTag")) {
4078 get_arg(arg
, sizeof(arg
), &p
);
4079 if ((strlen(arg
) == 4) && stream
)
4080 video_enc
.codec_tag
= ff_get_fourcc(arg
);
4081 } else if (!strcasecmp(cmd
, "BitExact")) {
4083 video_enc
.flags
|= CODEC_FLAG_BITEXACT
;
4084 } else if (!strcasecmp(cmd
, "DctFastint")) {
4086 video_enc
.dct_algo
= FF_DCT_FASTINT
;
4087 } else if (!strcasecmp(cmd
, "IdctSimple")) {
4089 video_enc
.idct_algo
= FF_IDCT_SIMPLE
;
4090 } else if (!strcasecmp(cmd
, "Qscale")) {
4091 get_arg(arg
, sizeof(arg
), &p
);
4093 video_enc
.flags
|= CODEC_FLAG_QSCALE
;
4094 video_enc
.global_quality
= FF_QP2LAMBDA
* atoi(arg
);
4096 } else if (!strcasecmp(cmd
, "VideoQDiff")) {
4097 get_arg(arg
, sizeof(arg
), &p
);
4099 video_enc
.max_qdiff
= atoi(arg
);
4100 if (video_enc
.max_qdiff
< 1 || video_enc
.max_qdiff
> 31) {
4101 fprintf(stderr
, "%s:%d: VideoQDiff out of range\n",
4102 filename
, line_num
);
4106 } else if (!strcasecmp(cmd
, "VideoQMax")) {
4107 get_arg(arg
, sizeof(arg
), &p
);
4109 video_enc
.qmax
= atoi(arg
);
4110 if (video_enc
.qmax
< 1 || video_enc
.qmax
> 31) {
4111 fprintf(stderr
, "%s:%d: VideoQMax out of range\n",
4112 filename
, line_num
);
4116 } else if (!strcasecmp(cmd
, "VideoQMin")) {
4117 get_arg(arg
, sizeof(arg
), &p
);
4119 video_enc
.qmin
= atoi(arg
);
4120 if (video_enc
.qmin
< 1 || video_enc
.qmin
> 31) {
4121 fprintf(stderr
, "%s:%d: VideoQMin out of range\n",
4122 filename
, line_num
);
4126 } else if (!strcasecmp(cmd
, "LumaElim")) {
4127 get_arg(arg
, sizeof(arg
), &p
);
4129 video_enc
.luma_elim_threshold
= atoi(arg
);
4130 } else if (!strcasecmp(cmd
, "ChromaElim")) {
4131 get_arg(arg
, sizeof(arg
), &p
);
4133 video_enc
.chroma_elim_threshold
= atoi(arg
);
4134 } else if (!strcasecmp(cmd
, "LumiMask")) {
4135 get_arg(arg
, sizeof(arg
), &p
);
4137 video_enc
.lumi_masking
= atof(arg
);
4138 } else if (!strcasecmp(cmd
, "DarkMask")) {
4139 get_arg(arg
, sizeof(arg
), &p
);
4141 video_enc
.dark_masking
= atof(arg
);
4142 } else if (!strcasecmp(cmd
, "NoVideo")) {
4143 video_id
= CODEC_ID_NONE
;
4144 } else if (!strcasecmp(cmd
, "NoAudio")) {
4145 audio_id
= CODEC_ID_NONE
;
4146 } else if (!strcasecmp(cmd
, "ACL")) {
4149 get_arg(arg
, sizeof(arg
), &p
);
4150 if (strcasecmp(arg
, "allow") == 0)
4151 acl
.action
= IP_ALLOW
;
4152 else if (strcasecmp(arg
, "deny") == 0)
4153 acl
.action
= IP_DENY
;
4155 fprintf(stderr
, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
4156 filename
, line_num
, arg
);
4160 get_arg(arg
, sizeof(arg
), &p
);
4162 if (resolve_host(&acl
.first
, arg
) != 0) {
4163 fprintf(stderr
, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4164 filename
, line_num
, arg
);
4167 acl
.last
= acl
.first
;
4169 get_arg(arg
, sizeof(arg
), &p
);
4172 if (resolve_host(&acl
.last
, arg
) != 0) {
4173 fprintf(stderr
, "%s:%d: ACL refers to invalid host or ip address '%s'\n",
4174 filename
, line_num
, arg
);
4180 IPAddressACL
*nacl
= av_mallocz(sizeof(*nacl
));
4181 IPAddressACL
**naclp
= 0;
4187 naclp
= &stream
->acl
;
4191 fprintf(stderr
, "%s:%d: ACL found not in <stream> or <feed>\n",
4192 filename
, line_num
);
4198 naclp
= &(*naclp
)->next
;
4203 } else if (!strcasecmp(cmd
, "RTSPOption")) {
4204 get_arg(arg
, sizeof(arg
), &p
);
4206 av_freep(&stream
->rtsp_option
);
4207 stream
->rtsp_option
= av_strdup(arg
);
4209 } else if (!strcasecmp(cmd
, "MulticastAddress")) {
4210 get_arg(arg
, sizeof(arg
), &p
);
4212 if (resolve_host(&stream
->multicast_ip
, arg
) != 0) {
4213 fprintf(stderr
, "%s:%d: Invalid host/IP address: %s\n",
4214 filename
, line_num
, arg
);
4217 stream
->is_multicast
= 1;
4218 stream
->loop
= 1; /* default is looping */
4220 } else if (!strcasecmp(cmd
, "MulticastPort")) {
4221 get_arg(arg
, sizeof(arg
), &p
);
4223 stream
->multicast_port
= atoi(arg
);
4224 } else if (!strcasecmp(cmd
, "MulticastTTL")) {
4225 get_arg(arg
, sizeof(arg
), &p
);
4227 stream
->multicast_ttl
= atoi(arg
);
4228 } else if (!strcasecmp(cmd
, "NoLoop")) {
4231 } else if (!strcasecmp(cmd
, "</Stream>")) {
4233 fprintf(stderr
, "%s:%d: No corresponding <Stream> for </Stream>\n",
4234 filename
, line_num
);
4237 if (stream
->feed
&& stream
->fmt
&& strcmp(stream
->fmt
->name
, "ffm") != 0) {
4238 if (audio_id
!= CODEC_ID_NONE
) {
4239 audio_enc
.codec_type
= CODEC_TYPE_AUDIO
;
4240 audio_enc
.codec_id
= audio_id
;
4241 add_codec(stream
, &audio_enc
);
4243 if (video_id
!= CODEC_ID_NONE
) {
4244 video_enc
.codec_type
= CODEC_TYPE_VIDEO
;
4245 video_enc
.codec_id
= video_id
;
4246 add_codec(stream
, &video_enc
);
4250 } else if (!strcasecmp(cmd
, "<Redirect")) {
4251 /*********************************************/
4253 if (stream
|| feed
|| redirect
) {
4254 fprintf(stderr
, "%s:%d: Already in a tag\n",
4255 filename
, line_num
);
4258 redirect
= av_mallocz(sizeof(FFStream
));
4259 *last_stream
= redirect
;
4260 last_stream
= &redirect
->next
;
4262 get_arg(redirect
->filename
, sizeof(redirect
->filename
), &p
);
4263 q
= strrchr(redirect
->filename
, '>');
4266 redirect
->stream_type
= STREAM_TYPE_REDIRECT
;
4268 } else if (!strcasecmp(cmd
, "URL")) {
4270 get_arg(redirect
->feed_filename
, sizeof(redirect
->feed_filename
), &p
);
4271 } else if (!strcasecmp(cmd
, "</Redirect>")) {
4273 fprintf(stderr
, "%s:%d: No corresponding <Redirect> for </Redirect>\n",
4274 filename
, line_num
);
4277 if (!redirect
->feed_filename
[0]) {
4278 fprintf(stderr
, "%s:%d: No URL found for <Redirect>\n",
4279 filename
, line_num
);
4283 } else if (!strcasecmp(cmd
, "LoadModule")) {
4284 get_arg(arg
, sizeof(arg
), &p
);
4288 fprintf(stderr
, "%s:%d: Module support not compiled into this version: '%s'\n",
4289 filename
, line_num
, arg
);
4293 fprintf(stderr
, "%s:%d: Incorrect keyword: '%s'\n",
4294 filename
, line_num
, cmd
);
4306 static void show_help(void)
4308 printf("usage: ffserver [-L] [-h] [-f configfile]\n"
4309 "Hyper fast multi format Audio/Video streaming server\n"
4311 "-L : print the LICENSE\n"
4313 "-f configfile : use configfile instead of /etc/ffserver.conf\n"
4317 static void handle_child_exit(int sig
)
4322 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
4325 for (feed
= first_feed
; feed
; feed
= feed
->next
) {
4326 if (feed
->pid
== pid
) {
4327 int uptime
= time(0) - feed
->pid_start
;
4330 fprintf(stderr
, "%s: Pid %d exited with status %d after %d seconds\n", feed
->filename
, pid
, status
, uptime
);
4333 /* Turn off any more restarts */
4334 feed
->child_argv
= 0;
4339 need_to_start_children
= 1;
4342 int main(int argc
, char **argv
)
4344 const char *config_filename
;
4346 struct sigaction sigact
;
4350 show_banner(program_name
, program_birth_year
);
4352 config_filename
= "/etc/ffserver.conf";
4354 my_program_name
= argv
[0];
4355 my_program_dir
= getcwd(0, 0);
4356 ffserver_daemon
= 1;
4359 c
= getopt(argc
, argv
, "ndLh?f:");
4375 ffserver_daemon
= 0;
4378 config_filename
= optarg
;
4385 putenv("http_proxy"); /* Kill the http_proxy */
4387 av_init_random(av_gettime() + (getpid() << 16), &random_state
);
4389 /* address on which the server will handle HTTP connections */
4390 my_http_addr
.sin_family
= AF_INET
;
4391 my_http_addr
.sin_port
= htons (8080);
4392 my_http_addr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
4394 /* address on which the server will handle RTSP connections */
4395 my_rtsp_addr
.sin_family
= AF_INET
;
4396 my_rtsp_addr
.sin_port
= htons (5454);
4397 my_rtsp_addr
.sin_addr
.s_addr
= htonl (INADDR_ANY
);
4399 nb_max_connections
= 5;
4400 max_bandwidth
= 1000;
4401 first_stream
= NULL
;
4402 logfilename
[0] = '\0';
4404 memset(&sigact
, 0, sizeof(sigact
));
4405 sigact
.sa_handler
= handle_child_exit
;
4406 sigact
.sa_flags
= SA_NOCLDSTOP
| SA_RESTART
;
4407 sigaction(SIGCHLD
, &sigact
, 0);
4409 if (parse_ffconfig(config_filename
) < 0) {
4410 fprintf(stderr
, "Incorrect config file - exiting.\n");
4414 build_file_streams();
4416 build_feed_streams();
4418 compute_bandwidth();
4420 /* put the process in background and detach it from its TTY */
4421 if (ffserver_daemon
) {
4428 } else if (pid
> 0) {
4436 open("/dev/null", O_RDWR
);
4437 if (strcmp(logfilename
, "-") != 0) {
4447 signal(SIGPIPE
, SIG_IGN
);
4449 /* open log file if needed */
4450 if (logfilename
[0] != '\0') {
4451 if (!strcmp(logfilename
, "-"))
4454 logfile
= fopen(logfilename
, "w");
4457 if (http_server() < 0) {
4458 fprintf(stderr
, "Could not start server\n");