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
21 #define HAVE_AV_CONFIG_H
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_POLL_H
33 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
35 #include <sys/types.h>
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
52 /* maximum number of simultaneous HTTP connections */
53 #define HTTP_MAX_CONNECTIONS 2000
56 HTTPSTATE_WAIT_REQUEST
,
57 HTTPSTATE_SEND_HEADER
,
58 HTTPSTATE_SEND_DATA_HEADER
,
59 HTTPSTATE_SEND_DATA
, /* sending TCP or UDP data */
60 HTTPSTATE_SEND_DATA_TRAILER
,
61 HTTPSTATE_RECEIVE_DATA
,
62 HTTPSTATE_WAIT_FEED
, /* wait for data from the feed */
65 RTSPSTATE_WAIT_REQUEST
,
67 RTSPSTATE_SEND_PACKET
,
70 const char *http_state
[] = {
86 #define IOBUFFER_INIT_SIZE 8192
88 /* coef for exponential mean for bitrate estimation in statistics */
91 /* timeouts are in ms */
92 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
93 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
95 #define SYNC_TIMEOUT (10 * 1000)
98 int64_t count1
, count2
;
102 /* context associated with one connection */
103 typedef struct HTTPContext
{
104 enum HTTPState state
;
105 int fd
; /* socket file descriptor */
106 struct sockaddr_in from_addr
; /* origin */
107 struct pollfd
*poll_entry
; /* used when polling */
109 uint8_t *buffer_ptr
, *buffer_end
;
112 struct HTTPContext
*next
;
113 int got_key_frame
; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
117 /* input format handling */
118 AVFormatContext
*fmt_in
;
119 int64_t start_time
; /* In milliseconds - this wraps fairly often */
120 int64_t first_pts
; /* initial pts value */
121 int64_t cur_pts
; /* current pts value from the stream in us */
122 int64_t cur_frame_duration
; /* duration of the current frame in us */
123 int cur_frame_bytes
; /* output frame size, needed to compute
124 the time at which we send each
126 int pts_stream_index
; /* stream we choose as clock reference */
127 int64_t cur_clock
; /* current clock reference value in us */
128 /* output format handling */
129 struct FFStream
*stream
;
130 /* -1 is invalid stream */
131 int feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
132 int switch_feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
134 AVFormatContext fmt_ctx
; /* instance of FFStream for one user */
135 int last_packet_sent
; /* true if last data packet was sent */
137 DataRateData datarate
;
144 int is_packetized
; /* if true, the stream is packetized */
145 int packet_stream_index
; /* current stream for output in state machine */
147 /* RTSP state specific */
148 uint8_t *pb_buffer
; /* XXX: use that in all the code */
150 int seq
; /* RTSP sequence number */
152 /* RTP state specific */
153 enum RTSPProtocol rtp_protocol
;
154 char session_id
[32]; /* session id */
155 AVFormatContext
*rtp_ctx
[MAX_STREAMS
];
157 /* RTP/UDP specific */
158 URLContext
*rtp_handles
[MAX_STREAMS
];
160 /* RTP/TCP specific */
161 struct HTTPContext
*rtsp_c
;
162 uint8_t *packet_buffer
, *packet_buffer_ptr
, *packet_buffer_end
;
165 static AVFrame dummy_frame
;
167 /* each generated stream is described here */
171 STREAM_TYPE_REDIRECT
,
174 enum IPAddressAction
{
179 typedef struct IPAddressACL
{
180 struct IPAddressACL
*next
;
181 enum IPAddressAction action
;
182 /* These are in host order */
183 struct in_addr first
;
187 /* description of each stream of the ffserver.conf file */
188 typedef struct FFStream
{
189 enum StreamType stream_type
;
190 char filename
[1024]; /* stream filename */
191 struct FFStream
*feed
; /* feed we are using (can be null if
193 AVFormatParameters
*ap_in
; /* input parameters */
194 AVInputFormat
*ifmt
; /* if non NULL, force input format */
198 int prebuffer
; /* Number of millseconds early to start */
199 int64_t max_time
; /* Number of milliseconds to run */
201 AVStream
*streams
[MAX_STREAMS
];
202 int feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
203 char feed_filename
[1024]; /* file name of the feed storage, or
204 input file name for a stream */
209 pid_t pid
; /* Of ffmpeg process */
210 time_t pid_start
; /* Of ffmpeg process */
212 struct FFStream
*next
;
213 int bandwidth
; /* bandwidth, in kbits/s */
216 /* multicast specific */
218 struct in_addr multicast_ip
;
219 int multicast_port
; /* first port used for multicast */
221 int loop
; /* if true, send the stream in loops (only meaningful if file) */
224 int feed_opened
; /* true if someone is writing to the feed */
225 int is_feed
; /* true if it is a feed */
226 int readonly
; /* True if writing is prohibited to the file */
228 int64_t bytes_served
;
229 int64_t feed_max_size
; /* maximum storage size, zero means unlimited */
230 int64_t feed_write_index
; /* current write position in feed (it wraps round) */
231 int64_t feed_size
; /* current size of feed */
232 struct FFStream
*next_feed
;
235 typedef struct FeedData
{
236 long long data_count
;
237 float avg_frame_size
; /* frame size averraged over last frames with exponential mean */
240 struct sockaddr_in my_http_addr
;
241 struct sockaddr_in my_rtsp_addr
;
243 static char logfilename
[1024];
244 static HTTPContext
*first_http_ctx
;
245 static FFStream
*first_feed
; /* contains only feeds */
246 static FFStream
*first_stream
; /* contains all streams, including feeds */
248 static void new_connection(int server_fd
, int is_rtsp
);
249 static void close_connection(HTTPContext
*c
);
252 static int handle_connection(HTTPContext
*c
);
253 static int http_parse_request(HTTPContext
*c
);
254 static int http_send_data(HTTPContext
*c
);
255 static void compute_stats(HTTPContext
*c
);
256 static int open_input_stream(HTTPContext
*c
, const char *info
);
257 static int http_start_receive_data(HTTPContext
*c
);
258 static int http_receive_data(HTTPContext
*c
);
261 static int rtsp_parse_request(HTTPContext
*c
);
262 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
);
263 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
);
264 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
265 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
266 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
267 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
270 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
271 struct in_addr my_ip
);
274 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
275 FFStream
*stream
, const char *session_id
,
276 enum RTSPProtocol rtp_protocol
);
277 static int rtp_new_av_stream(HTTPContext
*c
,
278 int stream_index
, struct sockaddr_in
*dest_addr
,
279 HTTPContext
*rtsp_c
);
281 static const char *my_program_name
;
282 static const char *my_program_dir
;
284 static int ffserver_debug
;
285 static int ffserver_daemon
;
286 static int no_launch
;
287 static int need_to_start_children
;
289 static int nb_max_connections
;
290 static int nb_connections
;
292 static int max_bandwidth
;
293 static int current_bandwidth
;
295 static int64_t cur_time
; // Making this global saves on passing it around everywhere
297 static AVRandomState random_state
;
299 static FILE *logfile
= NULL
;
301 static void __attribute__ ((format (printf
, 1, 2))) http_log(const char *fmt
, ...)
307 vfprintf(logfile
, fmt
, ap
);
313 static char *ctime1(char *buf2
)
321 p
= buf2
+ strlen(p
) - 1;
327 static void log_connection(HTTPContext
*c
)
334 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64
"\n",
335 inet_ntoa(c
->from_addr
.sin_addr
),
336 ctime1(buf2
), c
->method
, c
->url
,
337 c
->protocol
, (c
->http_error ? c
->http_error
: 200), c
->data_count
);
340 static void update_datarate(DataRateData
*drd
, int64_t count
)
342 if (!drd
->time1
&& !drd
->count1
) {
343 drd
->time1
= drd
->time2
= cur_time
;
344 drd
->count1
= drd
->count2
= count
;
346 if (cur_time
- drd
->time2
> 5000) {
347 drd
->time1
= drd
->time2
;
348 drd
->count1
= drd
->count2
;
349 drd
->time2
= cur_time
;
355 /* In bytes per second */
356 static int compute_datarate(DataRateData
*drd
, int64_t count
)
358 if (cur_time
== drd
->time1
)
361 return ((count
- drd
->count1
) * 1000) / (cur_time
- drd
->time1
);
365 static void start_children(FFStream
*feed
)
370 for (; feed
; feed
= feed
->next
) {
371 if (feed
->child_argv
&& !feed
->pid
) {
372 feed
->pid_start
= time(0);
377 fprintf(stderr
, "Unable to create children\n");
386 for (i
= 3; i
< 256; i
++) {
390 if (!ffserver_debug
) {
391 i
= open("/dev/null", O_RDWR
);
400 pstrcpy(pathname
, sizeof(pathname
), my_program_name
);
402 slash
= strrchr(pathname
, '/');
408 strcpy(slash
, "ffmpeg");
410 /* This is needed to make relative pathnames work */
411 chdir(my_program_dir
);
413 signal(SIGPIPE
, SIG_DFL
);
415 execvp(pathname
, feed
->child_argv
);
423 /* open a listening socket */
424 static int socket_open_listen(struct sockaddr_in
*my_addr
)
428 server_fd
= socket(AF_INET
,SOCK_STREAM
,0);
435 setsockopt(server_fd
, SOL_SOCKET
, SO_REUSEADDR
, &tmp
, sizeof(tmp
));
437 if (bind (server_fd
, (struct sockaddr
*) my_addr
, sizeof (*my_addr
)) < 0) {
439 snprintf(bindmsg
, sizeof(bindmsg
), "bind(port %d)", ntohs(my_addr
->sin_port
));
441 closesocket(server_fd
);
445 if (listen (server_fd
, 5) < 0) {
447 closesocket(server_fd
);
450 fcntl(server_fd
, F_SETFL
, O_NONBLOCK
);
455 /* start all multicast streams */
456 static void start_multicast(void)
461 struct sockaddr_in dest_addr
;
462 int default_port
, stream_index
;
465 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
466 if (stream
->is_multicast
) {
467 /* open the RTP connection */
468 snprintf(session_id
, sizeof(session_id
), "%08x%08x",
469 av_random(&random_state
), av_random(&random_state
));
471 /* choose a port if none given */
472 if (stream
->multicast_port
== 0) {
473 stream
->multicast_port
= default_port
;
477 dest_addr
.sin_family
= AF_INET
;
478 dest_addr
.sin_addr
= stream
->multicast_ip
;
479 dest_addr
.sin_port
= htons(stream
->multicast_port
);
481 rtp_c
= rtp_new_connection(&dest_addr
, stream
, session_id
,
482 RTSP_PROTOCOL_RTP_UDP_MULTICAST
);
486 if (open_input_stream(rtp_c
, "") < 0) {
487 fprintf(stderr
, "Could not open input stream for stream '%s'\n",
492 /* open each RTP stream */
493 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
495 dest_addr
.sin_port
= htons(stream
->multicast_port
+
497 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, NULL
) < 0) {
498 fprintf(stderr
, "Could not open output stream '%s/streamid=%d'\n",
499 stream
->filename
, stream_index
);
504 /* change state to send data */
505 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
510 /* main loop of the http server */
511 static int http_server(void)
513 int server_fd
, ret
, rtsp_server_fd
, delay
, delay1
;
514 struct pollfd poll_table
[HTTP_MAX_CONNECTIONS
+ 2], *poll_entry
;
515 HTTPContext
*c
, *c_next
;
517 server_fd
= socket_open_listen(&my_http_addr
);
521 rtsp_server_fd
= socket_open_listen(&my_rtsp_addr
);
522 if (rtsp_server_fd
< 0)
525 http_log("ffserver started.\n");
527 start_children(first_feed
);
529 first_http_ctx
= NULL
;
535 poll_entry
= poll_table
;
536 poll_entry
->fd
= server_fd
;
537 poll_entry
->events
= POLLIN
;
540 poll_entry
->fd
= rtsp_server_fd
;
541 poll_entry
->events
= POLLIN
;
544 /* wait for events on each HTTP handle */
551 case HTTPSTATE_SEND_HEADER
:
552 case RTSPSTATE_SEND_REPLY
:
553 case RTSPSTATE_SEND_PACKET
:
554 c
->poll_entry
= poll_entry
;
556 poll_entry
->events
= POLLOUT
;
559 case HTTPSTATE_SEND_DATA_HEADER
:
560 case HTTPSTATE_SEND_DATA
:
561 case HTTPSTATE_SEND_DATA_TRAILER
:
562 if (!c
->is_packetized
) {
563 /* for TCP, we output as much as we can (may need to put a limit) */
564 c
->poll_entry
= poll_entry
;
566 poll_entry
->events
= POLLOUT
;
569 /* when ffserver is doing the timing, we work by
570 looking at which packet need to be sent every
572 delay1
= 10; /* one tick wait XXX: 10 ms assumed */
577 case HTTPSTATE_WAIT_REQUEST
:
578 case HTTPSTATE_RECEIVE_DATA
:
579 case HTTPSTATE_WAIT_FEED
:
580 case RTSPSTATE_WAIT_REQUEST
:
581 /* need to catch errors */
582 c
->poll_entry
= poll_entry
;
584 poll_entry
->events
= POLLIN
;/* Maybe this will work */
588 c
->poll_entry
= NULL
;
594 /* wait for an event on one connection. We poll at least every
595 second to handle timeouts */
597 ret
= poll(poll_table
, poll_entry
- poll_table
, delay
);
598 if (ret
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
602 cur_time
= av_gettime() / 1000;
604 if (need_to_start_children
) {
605 need_to_start_children
= 0;
606 start_children(first_feed
);
609 /* now handle the events */
610 for(c
= first_http_ctx
; c
!= NULL
; c
= c_next
) {
612 if (handle_connection(c
) < 0) {
613 /* close and free the connection */
619 poll_entry
= poll_table
;
620 /* new HTTP connection request ? */
621 if (poll_entry
->revents
& POLLIN
) {
622 new_connection(server_fd
, 0);
625 /* new RTSP connection request ? */
626 if (poll_entry
->revents
& POLLIN
) {
627 new_connection(rtsp_server_fd
, 1);
632 /* start waiting for a new HTTP/RTSP request */
633 static void start_wait_request(HTTPContext
*c
, int is_rtsp
)
635 c
->buffer_ptr
= c
->buffer
;
636 c
->buffer_end
= c
->buffer
+ c
->buffer_size
- 1; /* leave room for '\0' */
639 c
->timeout
= cur_time
+ RTSP_REQUEST_TIMEOUT
;
640 c
->state
= RTSPSTATE_WAIT_REQUEST
;
642 c
->timeout
= cur_time
+ HTTP_REQUEST_TIMEOUT
;
643 c
->state
= HTTPSTATE_WAIT_REQUEST
;
647 static void new_connection(int server_fd
, int is_rtsp
)
649 struct sockaddr_in from_addr
;
651 HTTPContext
*c
= NULL
;
653 len
= sizeof(from_addr
);
654 fd
= accept(server_fd
, (struct sockaddr
*)&from_addr
,
658 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
660 /* XXX: should output a warning page when coming
661 close to the connection limit */
662 if (nb_connections
>= nb_max_connections
)
665 /* add a new connection */
666 c
= av_mallocz(sizeof(HTTPContext
));
671 c
->poll_entry
= NULL
;
672 c
->from_addr
= from_addr
;
673 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
674 c
->buffer
= av_malloc(c
->buffer_size
);
678 c
->next
= first_http_ctx
;
682 start_wait_request(c
, is_rtsp
);
694 static void close_connection(HTTPContext
*c
)
696 HTTPContext
**cp
, *c1
;
698 AVFormatContext
*ctx
;
702 /* remove connection from list */
703 cp
= &first_http_ctx
;
704 while ((*cp
) != NULL
) {
713 /* remove references, if any (XXX: do it faster) */
714 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
719 /* remove connection associated resources */
723 /* close each frame parser */
724 for(i
=0;i
<c
->fmt_in
->nb_streams
;i
++) {
725 st
= c
->fmt_in
->streams
[i
];
726 if (st
->codec
->codec
) {
727 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
];
752 if (!c
->last_packet_sent
) {
755 if (url_open_dyn_buf(&ctx
->pb
) >= 0) {
756 av_write_trailer(ctx
);
757 url_close_dyn_buf(&ctx
->pb
, &c
->pb_buffer
);
762 for(i
=0; i
<ctx
->nb_streams
; i
++)
763 av_free(ctx
->streams
[i
]) ;
765 if (c
->stream
&& !c
->post
&& c
->stream
->stream_type
== STREAM_TYPE_LIVE
)
766 current_bandwidth
-= c
->stream
->bandwidth
;
768 /* signal that there is no feed if we are the feeder socket */
769 if (c
->state
== HTTPSTATE_RECEIVE_DATA
&& c
->stream
) {
770 c
->stream
->feed_opened
= 0;
774 av_freep(&c
->pb_buffer
);
775 av_freep(&c
->packet_buffer
);
781 static int handle_connection(HTTPContext
*c
)
786 case HTTPSTATE_WAIT_REQUEST
:
787 case RTSPSTATE_WAIT_REQUEST
:
789 if ((c
->timeout
- cur_time
) < 0)
791 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
794 /* no need to read if no events */
795 if (!(c
->poll_entry
->revents
& POLLIN
))
799 len
= recv(c
->fd
, c
->buffer_ptr
, 1, 0);
801 if (errno
!= EAGAIN
&& errno
!= 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 (errno
!= EAGAIN
&& errno
!= EINTR
) {
837 /* error : close connection */
838 av_freep(&c
->pb_buffer
);
842 c
->buffer_ptr
+= len
;
844 c
->stream
->bytes_served
+= len
;
845 c
->data_count
+= len
;
846 if (c
->buffer_ptr
>= c
->buffer_end
) {
847 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 (errno
!= EAGAIN
&& errno
!= EINTR
) {
907 /* error : close connection */
908 av_freep(&c
->pb_buffer
);
912 c
->buffer_ptr
+= len
;
913 c
->data_count
+= len
;
914 if (c
->buffer_ptr
>= c
->buffer_end
) {
915 /* all the buffer was sent : wait for a new request */
916 av_freep(&c
->pb_buffer
);
917 start_wait_request(c
, 1);
921 case RTSPSTATE_SEND_PACKET
:
922 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
923 av_freep(&c
->packet_buffer
);
926 /* no need to write if no events */
927 if (!(c
->poll_entry
->revents
& POLLOUT
))
929 len
= send(c
->fd
, c
->packet_buffer_ptr
,
930 c
->packet_buffer_end
- c
->packet_buffer_ptr
, 0);
932 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
933 /* error : close connection */
934 av_freep(&c
->packet_buffer
);
938 c
->packet_buffer_ptr
+= len
;
939 if (c
->packet_buffer_ptr
>= c
->packet_buffer_end
) {
940 /* all the buffer was sent : wait for a new request */
941 av_freep(&c
->packet_buffer
);
942 c
->state
= RTSPSTATE_WAIT_REQUEST
;
946 case HTTPSTATE_READY
:
955 static int extract_rates(char *rates
, int ratelen
, const char *request
)
959 for (p
= request
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
960 if (strncasecmp(p
, "Pragma:", 7) == 0) {
961 const char *q
= p
+ 7;
963 while (*q
&& *q
!= '\n' && isspace(*q
))
966 if (strncasecmp(q
, "stream-switch-entry=", 20) == 0) {
972 memset(rates
, 0xff, ratelen
);
975 while (*q
&& *q
!= '\n' && *q
!= ':')
978 if (sscanf(q
, ":%d:%d", &stream_no
, &rate_no
) != 2) {
982 if (stream_no
< ratelen
&& stream_no
>= 0) {
983 rates
[stream_no
] = rate_no
;
986 while (*q
&& *q
!= '\n' && !isspace(*q
))
1003 static int find_stream_in_feed(FFStream
*feed
, AVCodecContext
*codec
, int bit_rate
)
1006 int best_bitrate
= 100000000;
1009 for (i
= 0; i
< feed
->nb_streams
; i
++) {
1010 AVCodecContext
*feed_codec
= feed
->streams
[i
]->codec
;
1012 if (feed_codec
->codec_id
!= codec
->codec_id
||
1013 feed_codec
->sample_rate
!= codec
->sample_rate
||
1014 feed_codec
->width
!= codec
->width
||
1015 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
= ntohl(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;
1132 last_action
= acl
->action
;
1135 /* Nothing matched, so return not the last action */
1136 return (last_action
== IP_DENY
) ?
1 : 0;
1139 /* compute the real filename of a file by matching it without its
1140 extensions to all the stream filenames */
1141 static void compute_real_filename(char *filename
, int max_size
)
1148 /* compute filename by matching without the file extensions */
1149 pstrcpy(file1
, sizeof(file1
), filename
);
1150 p
= strrchr(file1
, '.');
1153 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
1154 pstrcpy(file2
, sizeof(file2
), stream
->filename
);
1155 p
= strrchr(file2
, '.');
1158 if (!strcmp(file1
, file2
)) {
1159 pstrcpy(filename
, max_size
, stream
->filename
);
1174 /* parse http request and prepare header */
1175 static int http_parse_request(HTTPContext
*c
)
1178 enum RedirType redir_type
;
1180 char info
[1024], filename
[1024];
1184 const char *mime_type
;
1188 char *useragent
= 0;
1191 get_word(cmd
, sizeof(cmd
), (const char **)&p
);
1192 pstrcpy(c
->method
, sizeof(c
->method
), cmd
);
1194 if (!strcmp(cmd
, "GET"))
1196 else if (!strcmp(cmd
, "POST"))
1201 get_word(url
, sizeof(url
), (const char **)&p
);
1202 pstrcpy(c
->url
, sizeof(c
->url
), url
);
1204 get_word(protocol
, sizeof(protocol
), (const char **)&p
);
1205 if (strcmp(protocol
, "HTTP/1.0") && strcmp(protocol
, "HTTP/1.1"))
1208 pstrcpy(c
->protocol
, sizeof(c
->protocol
), protocol
);
1211 http_log("New connection: %s %s\n", cmd
, url
);
1213 /* find the filename and the optional info string in the request */
1214 p
= strchr(url
, '?');
1216 pstrcpy(info
, sizeof(info
), p
);
1222 pstrcpy(filename
, sizeof(filename
)-1, url
+ ((*url
== '/') ?
1 : 0));
1224 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1225 if (strncasecmp(p
, "User-Agent:", 11) == 0) {
1227 if (*useragent
&& *useragent
!= '\n' && isspace(*useragent
))
1231 p
= strchr(p
, '\n');
1238 redir_type
= REDIR_NONE
;
1239 if (match_ext(filename
, "asx")) {
1240 redir_type
= REDIR_ASX
;
1241 filename
[strlen(filename
)-1] = 'f';
1242 } else if (match_ext(filename
, "asf") &&
1243 (!useragent
|| strncasecmp(useragent
, "NSPlayer", 8) != 0)) {
1244 /* if this isn't WMP or lookalike, return the redirector file */
1245 redir_type
= REDIR_ASF
;
1246 } else if (match_ext(filename
, "rpm,ram")) {
1247 redir_type
= REDIR_RAM
;
1248 strcpy(filename
+ strlen(filename
)-2, "m");
1249 } else if (match_ext(filename
, "rtsp")) {
1250 redir_type
= REDIR_RTSP
;
1251 compute_real_filename(filename
, sizeof(filename
) - 1);
1252 } else if (match_ext(filename
, "sdp")) {
1253 redir_type
= REDIR_SDP
;
1254 compute_real_filename(filename
, sizeof(filename
) - 1);
1257 // "redirect" / request to index.html
1258 if (!strlen(filename
))
1259 pstrcpy(filename
, sizeof(filename
) - 1, "index.html");
1261 stream
= first_stream
;
1262 while (stream
!= NULL
) {
1263 if (!strcmp(stream
->filename
, filename
) && validate_acl(stream
, c
))
1265 stream
= stream
->next
;
1267 if (stream
== NULL
) {
1268 snprintf(msg
, sizeof(msg
), "File '%s' not found", url
);
1273 memcpy(c
->feed_streams
, stream
->feed_streams
, sizeof(c
->feed_streams
));
1274 memset(c
->switch_feed_streams
, -1, sizeof(c
->switch_feed_streams
));
1276 if (stream
->stream_type
== STREAM_TYPE_REDIRECT
) {
1277 c
->http_error
= 301;
1279 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 301 Moved\r\n");
1280 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Location: %s\r\n", stream
->feed_filename
);
1281 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1282 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1283 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Moved</title></head><body>\r\n");
1284 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "You should be <a href=\"%s\">redirected</a>.\r\n", stream
->feed_filename
);
1285 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1287 /* prepare output buffer */
1288 c
->buffer_ptr
= c
->buffer
;
1290 c
->state
= HTTPSTATE_SEND_HEADER
;
1294 /* If this is WMP, get the rate information */
1295 if (extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1296 if (modify_current_stream(c
, ratebuf
)) {
1297 for (i
= 0; i
< sizeof(c
->feed_streams
) / sizeof(c
->feed_streams
[0]); i
++) {
1298 if (c
->switch_feed_streams
[i
] >= 0)
1299 do_switch_stream(c
, i
);
1304 /* If already streaming this feed, dont let start an another feeder */
1305 if (stream
->feed_opened
) {
1306 snprintf(msg
, sizeof(msg
), "This feed is already being received.");
1310 if (c
->post
== 0 && stream
->stream_type
== STREAM_TYPE_LIVE
) {
1311 current_bandwidth
+= stream
->bandwidth
;
1314 if (c
->post
== 0 && max_bandwidth
< current_bandwidth
) {
1315 c
->http_error
= 200;
1317 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 Server too busy\r\n");
1318 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1319 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1320 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Too busy</title></head><body>\r\n");
1321 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");
1322 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",
1323 current_bandwidth
, max_bandwidth
);
1324 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1326 /* prepare output buffer */
1327 c
->buffer_ptr
= c
->buffer
;
1329 c
->state
= HTTPSTATE_SEND_HEADER
;
1333 if (redir_type
!= REDIR_NONE
) {
1336 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1337 if (strncasecmp(p
, "Host:", 5) == 0) {
1341 p
= strchr(p
, '\n');
1352 while (isspace(*hostinfo
))
1355 eoh
= strchr(hostinfo
, '\n');
1357 if (eoh
[-1] == '\r')
1360 if (eoh
- hostinfo
< sizeof(hostbuf
) - 1) {
1361 memcpy(hostbuf
, hostinfo
, eoh
- hostinfo
);
1362 hostbuf
[eoh
- hostinfo
] = 0;
1364 c
->http_error
= 200;
1366 switch(redir_type
) {
1368 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASX Follows\r\n");
1369 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1370 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1371 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ASX Version=\"3\">\r\n");
1372 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1373 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1374 hostbuf
, filename
, info
);
1375 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</ASX>\r\n");
1378 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RAM Follows\r\n");
1379 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: audio/x-pn-realaudio\r\n");
1380 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1381 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "# Autogenerated by ffserver\r\n");
1382 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "http://%s/%s%s\r\n",
1383 hostbuf
, filename
, info
);
1386 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASF Redirect follows\r\n");
1387 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1388 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1389 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "[Reference]\r\n");
1390 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Ref1=http://%s/%s%s\r\n",
1391 hostbuf
, filename
, info
);
1395 char hostname
[256], *p
;
1396 /* extract only hostname */
1397 pstrcpy(hostname
, sizeof(hostname
), hostbuf
);
1398 p
= strrchr(hostname
, ':');
1401 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1402 /* XXX: incorrect mime type ? */
1403 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/x-rtsp\r\n");
1404 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1405 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "rtsp://%s:%d/%s\r\n",
1406 hostname
, ntohs(my_rtsp_addr
.sin_port
),
1413 int sdp_data_size
, len
;
1414 struct sockaddr_in my_addr
;
1416 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1417 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/sdp\r\n");
1418 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1420 len
= sizeof(my_addr
);
1421 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
1423 /* XXX: should use a dynamic buffer */
1424 sdp_data_size
= prepare_sdp_description(stream
,
1427 if (sdp_data_size
> 0) {
1428 memcpy(q
, sdp_data
, sdp_data_size
);
1440 /* prepare output buffer */
1441 c
->buffer_ptr
= c
->buffer
;
1443 c
->state
= HTTPSTATE_SEND_HEADER
;
1449 snprintf(msg
, sizeof(msg
), "ASX/RAM file not handled");
1453 stream
->conns_served
++;
1455 /* XXX: add there authenticate and IP match */
1458 /* if post, it means a feed is being sent */
1459 if (!stream
->is_feed
) {
1460 /* However it might be a status report from WMP! Lets log the data
1461 * as it might come in handy one day
1466 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1467 if (strncasecmp(p
, "Pragma: log-line=", 17) == 0) {
1471 if (strncasecmp(p
, "Pragma: client-id=", 18) == 0) {
1472 client_id
= strtol(p
+ 18, 0, 10);
1474 p
= strchr(p
, '\n');
1482 char *eol
= strchr(logline
, '\n');
1487 if (eol
[-1] == '\r')
1489 http_log("%.*s\n", (int) (eol
- logline
), logline
);
1490 c
->suppress_log
= 1;
1495 http_log("\nGot request:\n%s\n", c
->buffer
);
1498 if (client_id
&& extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1501 /* Now we have to find the client_id */
1502 for (wmpc
= first_http_ctx
; wmpc
; wmpc
= wmpc
->next
) {
1503 if (wmpc
->wmp_client_id
== client_id
)
1508 if (modify_current_stream(wmpc
, ratebuf
)) {
1509 wmpc
->switch_pending
= 1;
1514 snprintf(msg
, sizeof(msg
), "POST command not handled");
1518 if (http_start_receive_data(c
) < 0) {
1519 snprintf(msg
, sizeof(msg
), "could not open feed");
1523 c
->state
= HTTPSTATE_RECEIVE_DATA
;
1528 if (strcmp(stream
->filename
+ strlen(stream
->filename
) - 4, ".asf") == 0) {
1529 http_log("\nGot request:\n%s\n", c
->buffer
);
1533 if (c
->stream
->stream_type
== STREAM_TYPE_STATUS
)
1536 /* open input stream */
1537 if (open_input_stream(c
, info
) < 0) {
1538 snprintf(msg
, sizeof(msg
), "Input stream corresponding to '%s' not found", url
);
1542 /* prepare http header */
1544 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1545 mime_type
= c
->stream
->fmt
->mime_type
;
1547 mime_type
= "application/x-octet_stream";
1548 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Pragma: no-cache\r\n");
1550 /* for asf, we need extra headers */
1551 if (!strcmp(c
->stream
->fmt
->name
,"asf_stream")) {
1552 /* Need to allocate a client id */
1554 c
->wmp_client_id
= av_random(&random_state
) & 0x7fffffff;
1556 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
);
1558 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-Type: %s\r\n", mime_type
);
1559 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1561 /* prepare output buffer */
1563 c
->buffer_ptr
= c
->buffer
;
1565 c
->state
= HTTPSTATE_SEND_HEADER
;
1568 c
->http_error
= 404;
1570 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 404 Not Found\r\n");
1571 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: %s\r\n", "text/html");
1572 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1573 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HTML>\n");
1574 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1575 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<BODY>%s</BODY>\n", msg
);
1576 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</HTML>\n");
1578 /* prepare output buffer */
1579 c
->buffer_ptr
= c
->buffer
;
1581 c
->state
= HTTPSTATE_SEND_HEADER
;
1585 c
->http_error
= 200; /* horrible : we use this value to avoid
1586 going to the send data state */
1587 c
->state
= HTTPSTATE_SEND_HEADER
;
1591 static void fmt_bytecount(ByteIOContext
*pb
, int64_t count
)
1593 static const char *suffix
= " kMGTP";
1596 for (s
= suffix
; count
>= 100000 && s
[1]; count
/= 1000, s
++) {
1599 url_fprintf(pb
, "%"PRId64
"%c", count
, *s
);
1602 static void compute_stats(HTTPContext
*c
)
1609 ByteIOContext pb1
, *pb
= &pb1
;
1611 if (url_open_dyn_buf(pb
) < 0) {
1612 /* XXX: return an error ? */
1613 c
->buffer_ptr
= c
->buffer
;
1614 c
->buffer_end
= c
->buffer
;
1618 url_fprintf(pb
, "HTTP/1.0 200 OK\r\n");
1619 url_fprintf(pb
, "Content-type: %s\r\n", "text/html");
1620 url_fprintf(pb
, "Pragma: no-cache\r\n");
1621 url_fprintf(pb
, "\r\n");
1623 url_fprintf(pb
, "<HEAD><TITLE>FFServer Status</TITLE>\n");
1624 if (c
->stream
->feed_filename
) {
1625 url_fprintf(pb
, "<link rel=\"shortcut icon\" href=\"%s\">\n", c
->stream
->feed_filename
);
1627 url_fprintf(pb
, "</HEAD>\n<BODY>");
1628 url_fprintf(pb
, "<H1>FFServer Status</H1>\n");
1630 url_fprintf(pb
, "<H2>Available Streams</H2>\n");
1631 url_fprintf(pb
, "<TABLE cellspacing=0 cellpadding=4>\n");
1632 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");
1633 stream
= first_stream
;
1634 while (stream
!= NULL
) {
1635 char sfilename
[1024];
1638 if (stream
->feed
!= stream
) {
1639 pstrcpy(sfilename
, sizeof(sfilename
) - 10, stream
->filename
);
1640 eosf
= sfilename
+ strlen(sfilename
);
1641 if (eosf
- sfilename
>= 4) {
1642 if (strcmp(eosf
- 4, ".asf") == 0) {
1643 strcpy(eosf
- 4, ".asx");
1644 } else if (strcmp(eosf
- 3, ".rm") == 0) {
1645 strcpy(eosf
- 3, ".ram");
1646 } else if (stream
->fmt
== &rtp_muxer
) {
1647 /* generate a sample RTSP director if
1648 unicast. Generate an SDP redirector if
1650 eosf
= strrchr(sfilename
, '.');
1652 eosf
= sfilename
+ strlen(sfilename
);
1653 if (stream
->is_multicast
)
1654 strcpy(eosf
, ".sdp");
1656 strcpy(eosf
, ".rtsp");
1660 url_fprintf(pb
, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1661 sfilename
, stream
->filename
);
1662 url_fprintf(pb
, "<td align=right> %d <td align=right> ",
1663 stream
->conns_served
);
1664 fmt_bytecount(pb
, stream
->bytes_served
);
1665 switch(stream
->stream_type
) {
1666 case STREAM_TYPE_LIVE
:
1668 int audio_bit_rate
= 0;
1669 int video_bit_rate
= 0;
1670 const char *audio_codec_name
= "";
1671 const char *video_codec_name
= "";
1672 const char *audio_codec_name_extra
= "";
1673 const char *video_codec_name_extra
= "";
1675 for(i
=0;i
<stream
->nb_streams
;i
++) {
1676 AVStream
*st
= stream
->streams
[i
];
1677 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1678 switch(st
->codec
->codec_type
) {
1679 case CODEC_TYPE_AUDIO
:
1680 audio_bit_rate
+= st
->codec
->bit_rate
;
1682 if (*audio_codec_name
)
1683 audio_codec_name_extra
= "...";
1684 audio_codec_name
= codec
->name
;
1687 case CODEC_TYPE_VIDEO
:
1688 video_bit_rate
+= st
->codec
->bit_rate
;
1690 if (*video_codec_name
)
1691 video_codec_name_extra
= "...";
1692 video_codec_name
= codec
->name
;
1695 case CODEC_TYPE_DATA
:
1696 video_bit_rate
+= st
->codec
->bit_rate
;
1702 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",
1705 video_bit_rate
/ 1000, video_codec_name
, video_codec_name_extra
,
1706 audio_bit_rate
/ 1000, audio_codec_name
, audio_codec_name_extra
);
1708 url_fprintf(pb
, "<TD>%s", stream
->feed
->filename
);
1710 url_fprintf(pb
, "<TD>%s", stream
->feed_filename
);
1712 url_fprintf(pb
, "\n");
1716 url_fprintf(pb
, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1720 stream
= stream
->next
;
1722 url_fprintf(pb
, "</TABLE>\n");
1724 stream
= first_stream
;
1725 while (stream
!= NULL
) {
1726 if (stream
->feed
== stream
) {
1727 url_fprintf(pb
, "<h2>Feed %s</h2>", stream
->filename
);
1729 url_fprintf(pb
, "Running as pid %d.\n", stream
->pid
);
1731 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1736 /* This is somewhat linux specific I guess */
1737 snprintf(ps_cmd
, sizeof(ps_cmd
),
1738 "ps -o \"%%cpu,cputime\" --no-headers %d",
1741 pid_stat
= popen(ps_cmd
, "r");
1746 if (fscanf(pid_stat
, "%10s %64s", cpuperc
,
1748 url_fprintf(pb
, "Currently using %s%% of the cpu. Total time used %s.\n",
1756 url_fprintf(pb
, "<p>");
1758 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");
1760 for (i
= 0; i
< stream
->nb_streams
; i
++) {
1761 AVStream
*st
= stream
->streams
[i
];
1762 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1763 const char *type
= "unknown";
1764 char parameters
[64];
1768 switch(st
->codec
->codec_type
) {
1769 case CODEC_TYPE_AUDIO
:
1771 snprintf(parameters
, sizeof(parameters
), "%d channel(s), %d Hz", st
->codec
->channels
, st
->codec
->sample_rate
);
1773 case CODEC_TYPE_VIDEO
:
1775 snprintf(parameters
, sizeof(parameters
), "%dx%d, q=%d-%d, fps=%d", st
->codec
->width
, st
->codec
->height
,
1776 st
->codec
->qmin
, st
->codec
->qmax
, st
->codec
->time_base
.den
/ st
->codec
->time_base
.num
);
1781 url_fprintf(pb
, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1782 i
, type
, st
->codec
->bit_rate
/1000, codec ? codec
->name
: "", parameters
);
1784 url_fprintf(pb
, "</table>\n");
1787 stream
= stream
->next
;
1793 AVCodecContext
*enc
;
1797 stream
= first_feed
;
1798 while (stream
!= NULL
) {
1799 url_fprintf(pb
, "<H1>Feed '%s'</H1>\n", stream
->filename
);
1800 url_fprintf(pb
, "<TABLE>\n");
1801 url_fprintf(pb
, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1802 for(i
=0;i
<stream
->nb_streams
;i
++) {
1803 AVStream
*st
= stream
->streams
[i
];
1804 FeedData
*fdata
= st
->priv_data
;
1807 avcodec_string(buf
, sizeof(buf
), enc
);
1808 avg
= fdata
->avg_frame_size
* (float)enc
->rate
* 8.0;
1809 if (enc
->codec
->type
== CODEC_TYPE_AUDIO
&& enc
->frame_size
> 0)
1810 avg
/= enc
->frame_size
;
1811 url_fprintf(pb
, "<TR><TD>%s <TD> %d <TD> %"PRId64
" <TD> %0.1f\n",
1812 buf
, enc
->frame_number
, fdata
->data_count
, avg
/ 1000.0);
1814 url_fprintf(pb
, "</TABLE>\n");
1815 stream
= stream
->next_feed
;
1820 /* connection status */
1821 url_fprintf(pb
, "<H2>Connection Status</H2>\n");
1823 url_fprintf(pb
, "Number of connections: %d / %d<BR>\n",
1824 nb_connections
, nb_max_connections
);
1826 url_fprintf(pb
, "Bandwidth in use: %dk / %dk<BR>\n",
1827 current_bandwidth
, max_bandwidth
);
1829 url_fprintf(pb
, "<TABLE>\n");
1830 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");
1831 c1
= first_http_ctx
;
1833 while (c1
!= NULL
) {
1839 for (j
= 0; j
< c1
->stream
->nb_streams
; j
++) {
1840 if (!c1
->stream
->feed
) {
1841 bitrate
+= c1
->stream
->streams
[j
]->codec
->bit_rate
;
1843 if (c1
->feed_streams
[j
] >= 0) {
1844 bitrate
+= c1
->stream
->feed
->streams
[c1
->feed_streams
[j
]]->codec
->bit_rate
;
1851 p
= inet_ntoa(c1
->from_addr
.sin_addr
);
1852 url_fprintf(pb
, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1854 c1
->stream ? c1
->stream
->filename
: "",
1855 c1
->state
== HTTPSTATE_RECEIVE_DATA ?
"(input)" : "",
1858 http_state
[c1
->state
]);
1859 fmt_bytecount(pb
, bitrate
);
1860 url_fprintf(pb
, "<td align=right>");
1861 fmt_bytecount(pb
, compute_datarate(&c1
->datarate
, c1
->data_count
) * 8);
1862 url_fprintf(pb
, "<td align=right>");
1863 fmt_bytecount(pb
, c1
->data_count
);
1864 url_fprintf(pb
, "\n");
1867 url_fprintf(pb
, "</TABLE>\n");
1872 url_fprintf(pb
, "<HR size=1 noshade>Generated at %s", p
);
1873 url_fprintf(pb
, "</BODY>\n</HTML>\n");
1875 len
= url_close_dyn_buf(pb
, &c
->pb_buffer
);
1876 c
->buffer_ptr
= c
->pb_buffer
;
1877 c
->buffer_end
= c
->pb_buffer
+ len
;
1880 /* check if the parser needs to be opened for stream i */
1881 static void open_parser(AVFormatContext
*s
, int i
)
1883 AVStream
*st
= s
->streams
[i
];
1886 if (!st
->codec
->codec
) {
1887 codec
= avcodec_find_decoder(st
->codec
->codec_id
);
1888 if (codec
&& (codec
->capabilities
& CODEC_CAP_PARSE_ONLY
)) {
1889 st
->codec
->parse_only
= 1;
1890 if (avcodec_open(st
->codec
, codec
) < 0) {
1891 st
->codec
->parse_only
= 0;
1897 static int open_input_stream(HTTPContext
*c
, const char *info
)
1900 char input_filename
[1024];
1905 /* find file name */
1906 if (c
->stream
->feed
) {
1907 strcpy(input_filename
, c
->stream
->feed
->feed_filename
);
1908 buf_size
= FFM_PACKET_SIZE
;
1909 /* compute position (absolute time) */
1910 if (find_info_tag(buf
, sizeof(buf
), "date", info
)) {
1911 stream_pos
= parse_date(buf
, 0);
1912 } else if (find_info_tag(buf
, sizeof(buf
), "buffer", info
)) {
1913 int prebuffer
= strtol(buf
, 0, 10);
1914 stream_pos
= av_gettime() - prebuffer
* (int64_t)1000000;
1916 stream_pos
= av_gettime() - c
->stream
->prebuffer
* (int64_t)1000;
1919 strcpy(input_filename
, c
->stream
->feed_filename
);
1921 /* compute position (relative time) */
1922 if (find_info_tag(buf
, sizeof(buf
), "date", info
)) {
1923 stream_pos
= parse_date(buf
, 1);
1928 if (input_filename
[0] == '\0')
1932 { time_t when
= stream_pos
/ 1000000;
1933 http_log("Stream pos = %"PRId64
", time=%s", stream_pos
, ctime(&when
));
1938 if (av_open_input_file(&s
, input_filename
, c
->stream
->ifmt
,
1939 buf_size
, c
->stream
->ap_in
) < 0) {
1940 http_log("%s not found", input_filename
);
1945 /* open each parser */
1946 for(i
=0;i
<s
->nb_streams
;i
++)
1949 /* choose stream as clock source (we favorize video stream if
1950 present) for packet sending */
1951 c
->pts_stream_index
= 0;
1952 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
1953 if (c
->pts_stream_index
== 0 &&
1954 c
->stream
->streams
[i
]->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
1955 c
->pts_stream_index
= i
;
1960 if (c
->fmt_in
->iformat
->read_seek
) {
1961 c
->fmt_in
->iformat
->read_seek(c
->fmt_in
, 0, stream_pos
, 0);
1964 /* set the start time (needed for maxtime and RTP packet timing) */
1965 c
->start_time
= cur_time
;
1966 c
->first_pts
= AV_NOPTS_VALUE
;
1970 /* return the server clock (in us) */
1971 static int64_t get_server_clock(HTTPContext
*c
)
1973 /* compute current pts value from system time */
1974 return (cur_time
- c
->start_time
) * 1000;
1977 /* return the estimated time at which the current packet must be sent
1979 static int64_t get_packet_send_clock(HTTPContext
*c
)
1981 int bytes_left
, bytes_sent
, frame_bytes
;
1983 frame_bytes
= c
->cur_frame_bytes
;
1984 if (frame_bytes
<= 0) {
1987 bytes_left
= c
->buffer_end
- c
->buffer_ptr
;
1988 bytes_sent
= frame_bytes
- bytes_left
;
1989 return c
->cur_pts
+ (c
->cur_frame_duration
* bytes_sent
) / frame_bytes
;
1994 static int http_prepare_data(HTTPContext
*c
)
1997 AVFormatContext
*ctx
;
1999 av_freep(&c
->pb_buffer
);
2001 case HTTPSTATE_SEND_DATA_HEADER
:
2002 memset(&c
->fmt_ctx
, 0, sizeof(c
->fmt_ctx
));
2003 pstrcpy(c
->fmt_ctx
.author
, sizeof(c
->fmt_ctx
.author
),
2005 pstrcpy(c
->fmt_ctx
.comment
, sizeof(c
->fmt_ctx
.comment
),
2006 c
->stream
->comment
);
2007 pstrcpy(c
->fmt_ctx
.copyright
, sizeof(c
->fmt_ctx
.copyright
),
2008 c
->stream
->copyright
);
2009 pstrcpy(c
->fmt_ctx
.title
, sizeof(c
->fmt_ctx
.title
),
2012 /* open output stream by using specified codecs */
2013 c
->fmt_ctx
.oformat
= c
->stream
->fmt
;
2014 c
->fmt_ctx
.nb_streams
= c
->stream
->nb_streams
;
2015 for(i
=0;i
<c
->fmt_ctx
.nb_streams
;i
++) {
2018 st
= av_mallocz(sizeof(AVStream
));
2019 st
->codec
= avcodec_alloc_context();
2020 c
->fmt_ctx
.streams
[i
] = st
;
2021 /* if file or feed, then just take streams from FFStream struct */
2022 if (!c
->stream
->feed
||
2023 c
->stream
->feed
== c
->stream
)
2024 src
= c
->stream
->streams
[i
];
2026 src
= c
->stream
->feed
->streams
[c
->stream
->feed_streams
[i
]];
2030 st
->codec
->frame_number
= 0; /* XXX: should be done in
2031 AVStream, not in codec */
2032 /* I'm pretty sure that this is not correct...
2033 * However, without it, we crash
2035 st
->codec
->coded_frame
= &dummy_frame
;
2037 c
->got_key_frame
= 0;
2039 /* prepare header and save header data in a stream */
2040 if (url_open_dyn_buf(&c
->fmt_ctx
.pb
) < 0) {
2041 /* XXX: potential leak */
2044 c
->fmt_ctx
.pb
.is_streamed
= 1;
2046 av_set_parameters(&c
->fmt_ctx
, NULL
);
2047 if (av_write_header(&c
->fmt_ctx
) < 0)
2050 len
= url_close_dyn_buf(&c
->fmt_ctx
.pb
, &c
->pb_buffer
);
2051 c
->buffer_ptr
= c
->pb_buffer
;
2052 c
->buffer_end
= c
->pb_buffer
+ len
;
2054 c
->state
= HTTPSTATE_SEND_DATA
;
2055 c
->last_packet_sent
= 0;
2057 case HTTPSTATE_SEND_DATA
:
2058 /* find a new packet */
2062 /* read a packet from the input stream */
2063 if (c
->stream
->feed
) {
2064 ffm_set_write_index(c
->fmt_in
,
2065 c
->stream
->feed
->feed_write_index
,
2066 c
->stream
->feed
->feed_size
);
2069 if (c
->stream
->max_time
&&
2070 c
->stream
->max_time
+ c
->start_time
- cur_time
< 0) {
2071 /* We have timed out */
2072 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2075 if (av_read_frame(c
->fmt_in
, &pkt
) < 0) {
2076 if (c
->stream
->feed
&& c
->stream
->feed
->feed_opened
) {
2077 /* if coming from feed, it means we reached the end of the
2078 ffm file, so must wait for more data */
2079 c
->state
= HTTPSTATE_WAIT_FEED
;
2080 return 1; /* state changed */
2082 if (c
->stream
->loop
) {
2083 av_close_input_file(c
->fmt_in
);
2085 if (open_input_stream(c
, "") < 0)
2090 /* must send trailer now because eof or error */
2091 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2095 /* update first pts if needed */
2096 if (c
->first_pts
== AV_NOPTS_VALUE
) {
2097 c
->first_pts
= av_rescale_q(pkt
.dts
, c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
, AV_TIME_BASE_Q
);
2098 c
->start_time
= cur_time
;
2100 /* send it to the appropriate stream */
2101 if (c
->stream
->feed
) {
2102 /* if coming from a feed, select the right stream */
2103 if (c
->switch_pending
) {
2104 c
->switch_pending
= 0;
2105 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2106 if (c
->switch_feed_streams
[i
] == pkt
.stream_index
) {
2107 if (pkt
.flags
& PKT_FLAG_KEY
) {
2108 do_switch_stream(c
, i
);
2111 if (c
->switch_feed_streams
[i
] >= 0) {
2112 c
->switch_pending
= 1;
2116 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2117 if (c
->feed_streams
[i
] == pkt
.stream_index
) {
2118 pkt
.stream_index
= i
;
2119 if (pkt
.flags
& PKT_FLAG_KEY
) {
2120 c
->got_key_frame
|= 1 << i
;
2122 /* See if we have all the key frames, then
2123 * we start to send. This logic is not quite
2124 * right, but it works for the case of a
2125 * single video stream with one or more
2126 * audio streams (for which every frame is
2127 * typically a key frame).
2129 if (!c
->stream
->send_on_key
||
2130 ((c
->got_key_frame
+ 1) >> c
->stream
->nb_streams
)) {
2136 AVCodecContext
*codec
;
2139 /* specific handling for RTP: we use several
2140 output stream (one for each RTP
2141 connection). XXX: need more abstract handling */
2142 if (c
->is_packetized
) {
2144 /* compute send time and duration */
2145 st
= c
->fmt_in
->streams
[pkt
.stream_index
];
2146 c
->cur_pts
= av_rescale_q(pkt
.dts
, st
->time_base
, AV_TIME_BASE_Q
);
2147 if (st
->start_time
!= AV_NOPTS_VALUE
)
2148 c
->cur_pts
-= av_rescale_q(st
->start_time
, st
->time_base
, AV_TIME_BASE_Q
);
2149 c
->cur_frame_duration
= av_rescale_q(pkt
.duration
, st
->time_base
, AV_TIME_BASE_Q
);
2151 printf("index=%d pts=%0.3f duration=%0.6f\n",
2153 (double)c
->cur_pts
/
2155 (double)c
->cur_frame_duration
/
2158 /* find RTP context */
2159 c
->packet_stream_index
= pkt
.stream_index
;
2160 ctx
= c
->rtp_ctx
[c
->packet_stream_index
];
2162 av_free_packet(&pkt
);
2165 codec
= ctx
->streams
[0]->codec
;
2166 /* only one stream per RTP connection */
2167 pkt
.stream_index
= 0;
2171 codec
= ctx
->streams
[pkt
.stream_index
]->codec
;
2174 codec
->coded_frame
->key_frame
= ((pkt
.flags
& PKT_FLAG_KEY
) != 0);
2175 if (c
->is_packetized
) {
2176 int max_packet_size
;
2177 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
)
2178 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
2180 max_packet_size
= url_get_max_packet_size(c
->rtp_handles
[c
->packet_stream_index
]);
2181 ret
= url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
);
2183 ret
= url_open_dyn_buf(&ctx
->pb
);
2186 /* XXX: potential leak */
2189 if (pkt
.dts
!= AV_NOPTS_VALUE
)
2190 pkt
.dts
= av_rescale_q(pkt
.dts
,
2191 c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
,
2192 ctx
->streams
[pkt
.stream_index
]->time_base
);
2193 if (pkt
.pts
!= AV_NOPTS_VALUE
)
2194 pkt
.pts
= av_rescale_q(pkt
.pts
,
2195 c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
,
2196 ctx
->streams
[pkt
.stream_index
]->time_base
);
2197 if (av_write_frame(ctx
, &pkt
)) {
2198 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2201 len
= url_close_dyn_buf(&ctx
->pb
, &c
->pb_buffer
);
2202 c
->cur_frame_bytes
= len
;
2203 c
->buffer_ptr
= c
->pb_buffer
;
2204 c
->buffer_end
= c
->pb_buffer
+ len
;
2206 codec
->frame_number
++;
2210 av_free_packet(&pkt
);
2216 case HTTPSTATE_SEND_DATA_TRAILER
:
2217 /* last packet test ? */
2218 if (c
->last_packet_sent
|| c
->is_packetized
)
2221 /* prepare header */
2222 if (url_open_dyn_buf(&ctx
->pb
) < 0) {
2223 /* XXX: potential leak */
2226 av_write_trailer(ctx
);
2227 len
= url_close_dyn_buf(&ctx
->pb
, &c
->pb_buffer
);
2228 c
->buffer_ptr
= c
->pb_buffer
;
2229 c
->buffer_end
= c
->pb_buffer
+ len
;
2231 c
->last_packet_sent
= 1;
2238 #define SHORT_TERM_BANDWIDTH 8000000
2240 /* should convert the format at the same time */
2241 /* send data starting at c->buffer_ptr to the output connection
2242 (either UDP or TCP connection) */
2243 static int http_send_data(HTTPContext
*c
)
2248 if (c
->buffer_ptr
>= c
->buffer_end
) {
2249 ret
= http_prepare_data(c
);
2252 else if (ret
!= 0) {
2253 /* state change requested */
2257 if (c
->is_packetized
) {
2258 /* RTP data output */
2259 len
= c
->buffer_end
- c
->buffer_ptr
;
2261 /* fail safe - should never happen */
2263 c
->buffer_ptr
= c
->buffer_end
;
2266 len
= (c
->buffer_ptr
[0] << 24) |
2267 (c
->buffer_ptr
[1] << 16) |
2268 (c
->buffer_ptr
[2] << 8) |
2270 if (len
> (c
->buffer_end
- c
->buffer_ptr
))
2272 if ((get_packet_send_clock(c
) - get_server_clock(c
)) > 0) {
2273 /* nothing to send yet: we can wait */
2277 c
->data_count
+= len
;
2278 update_datarate(&c
->datarate
, c
->data_count
);
2280 c
->stream
->bytes_served
+= len
;
2282 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
) {
2283 /* RTP packets are sent inside the RTSP TCP connection */
2284 ByteIOContext pb1
, *pb
= &pb1
;
2285 int interleaved_index
, size
;
2287 HTTPContext
*rtsp_c
;
2290 /* if no RTSP connection left, error */
2293 /* if already sending something, then wait. */
2294 if (rtsp_c
->state
!= RTSPSTATE_WAIT_REQUEST
) {
2297 if (url_open_dyn_buf(pb
) < 0)
2299 interleaved_index
= c
->packet_stream_index
* 2;
2300 /* RTCP packets are sent at odd indexes */
2301 if (c
->buffer_ptr
[1] == 200)
2302 interleaved_index
++;
2303 /* write RTSP TCP header */
2305 header
[1] = interleaved_index
;
2306 header
[2] = len
>> 8;
2308 put_buffer(pb
, header
, 4);
2309 /* write RTP packet data */
2311 put_buffer(pb
, c
->buffer_ptr
, len
);
2312 size
= url_close_dyn_buf(pb
, &c
->packet_buffer
);
2313 /* prepare asynchronous TCP sending */
2314 rtsp_c
->packet_buffer_ptr
= c
->packet_buffer
;
2315 rtsp_c
->packet_buffer_end
= c
->packet_buffer
+ size
;
2316 c
->buffer_ptr
+= len
;
2318 /* send everything we can NOW */
2319 len
= send(rtsp_c
->fd
, rtsp_c
->packet_buffer_ptr
,
2320 rtsp_c
->packet_buffer_end
- rtsp_c
->packet_buffer_ptr
, 0);
2322 rtsp_c
->packet_buffer_ptr
+= len
;
2324 if (rtsp_c
->packet_buffer_ptr
< rtsp_c
->packet_buffer_end
) {
2325 /* if we could not send all the data, we will
2326 send it later, so a new state is needed to
2327 "lock" the RTSP TCP connection */
2328 rtsp_c
->state
= RTSPSTATE_SEND_PACKET
;
2331 /* all data has been sent */
2332 av_freep(&c
->packet_buffer
);
2335 /* send RTP packet directly in UDP */
2337 url_write(c
->rtp_handles
[c
->packet_stream_index
],
2338 c
->buffer_ptr
, len
);
2339 c
->buffer_ptr
+= len
;
2340 /* here we continue as we can send several packets per 10 ms slot */
2343 /* TCP data output */
2344 len
= send(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2346 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
2347 /* error : close connection */
2353 c
->buffer_ptr
+= len
;
2355 c
->data_count
+= len
;
2356 update_datarate(&c
->datarate
, c
->data_count
);
2358 c
->stream
->bytes_served
+= len
;
2366 static int http_start_receive_data(HTTPContext
*c
)
2370 if (c
->stream
->feed_opened
)
2373 /* Don't permit writing to this one */
2374 if (c
->stream
->readonly
)
2378 fd
= open(c
->stream
->feed_filename
, O_RDWR
);
2383 c
->stream
->feed_write_index
= ffm_read_write_index(fd
);
2384 c
->stream
->feed_size
= lseek(fd
, 0, SEEK_END
);
2385 lseek(fd
, 0, SEEK_SET
);
2387 /* init buffer input */
2388 c
->buffer_ptr
= c
->buffer
;
2389 c
->buffer_end
= c
->buffer
+ FFM_PACKET_SIZE
;
2390 c
->stream
->feed_opened
= 1;
2394 static int http_receive_data(HTTPContext
*c
)
2398 if (c
->buffer_end
> c
->buffer_ptr
) {
2401 len
= recv(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
, 0);
2403 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
2404 /* error : close connection */
2407 } else if (len
== 0) {
2408 /* end of connection : close it */
2411 c
->buffer_ptr
+= len
;
2412 c
->data_count
+= len
;
2413 update_datarate(&c
->datarate
, c
->data_count
);
2417 if (c
->buffer_ptr
- c
->buffer
>= 2 && c
->data_count
> FFM_PACKET_SIZE
) {
2418 if (c
->buffer
[0] != 'f' ||
2419 c
->buffer
[1] != 'm') {
2420 http_log("Feed stream has become desynchronized -- disconnecting\n");
2425 if (c
->buffer_ptr
>= c
->buffer_end
) {
2426 FFStream
*feed
= c
->stream
;
2427 /* a packet has been received : write it in the store, except
2429 if (c
->data_count
> FFM_PACKET_SIZE
) {
2431 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
2432 /* XXX: use llseek or url_seek */
2433 lseek(c
->feed_fd
, feed
->feed_write_index
, SEEK_SET
);
2434 write(c
->feed_fd
, c
->buffer
, FFM_PACKET_SIZE
);
2436 feed
->feed_write_index
+= FFM_PACKET_SIZE
;
2437 /* update file size */
2438 if (feed
->feed_write_index
> c
->stream
->feed_size
)
2439 feed
->feed_size
= feed
->feed_write_index
;
2441 /* handle wrap around if max file size reached */
2442 if (c
->stream
->feed_max_size
&& feed
->feed_write_index
>= c
->stream
->feed_max_size
)
2443 feed
->feed_write_index
= FFM_PACKET_SIZE
;
2446 ffm_write_write_index(c
->feed_fd
, feed
->feed_write_index
);
2448 /* wake up any waiting connections */
2449 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
2450 if (c1
->state
== HTTPSTATE_WAIT_FEED
&&
2451 c1
->stream
->feed
== c
->stream
->feed
) {
2452 c1
->state
= HTTPSTATE_SEND_DATA
;
2456 /* We have a header in our hands that contains useful data */
2458 AVInputFormat
*fmt_in
;
2459 ByteIOContext
*pb
= &s
.pb
;
2462 memset(&s
, 0, sizeof(s
));
2464 url_open_buf(pb
, c
->buffer
, c
->buffer_end
- c
->buffer
, URL_RDONLY
);
2465 pb
->buf_end
= c
->buffer_end
; /* ?? */
2466 pb
->is_streamed
= 1;
2468 /* use feed output format name to find corresponding input format */
2469 fmt_in
= av_find_input_format(feed
->fmt
->name
);
2473 if (fmt_in
->priv_data_size
> 0) {
2474 s
.priv_data
= av_mallocz(fmt_in
->priv_data_size
);
2480 if (fmt_in
->read_header(&s
, 0) < 0) {
2481 av_freep(&s
.priv_data
);
2485 /* Now we have the actual streams */
2486 if (s
.nb_streams
!= feed
->nb_streams
) {
2487 av_freep(&s
.priv_data
);
2490 for (i
= 0; i
< s
.nb_streams
; i
++) {
2491 memcpy(feed
->streams
[i
]->codec
,
2492 s
.streams
[i
]->codec
, sizeof(AVCodecContext
));
2494 av_freep(&s
.priv_data
);
2496 c
->buffer_ptr
= c
->buffer
;
2501 c
->stream
->feed_opened
= 0;
2506 /********************************************************************/
2509 static void rtsp_reply_header(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2516 switch(error_number
) {
2517 #define DEF(n, c, s) case c: str = s; break;
2518 #include "rtspcodes.h"
2521 str
= "Unknown Error";
2525 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", error_number
, str
);
2526 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2528 /* output GMT time */
2532 p
= buf2
+ strlen(p
) - 1;
2535 url_fprintf(c
->pb
, "Date: %s GMT\r\n", buf2
);
2538 static void rtsp_reply_error(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2540 rtsp_reply_header(c
, error_number
);
2541 url_fprintf(c
->pb
, "\r\n");
2544 static int rtsp_parse_request(HTTPContext
*c
)
2546 const char *p
, *p1
, *p2
;
2553 RTSPHeader header1
, *header
= &header1
;
2555 c
->buffer_ptr
[0] = '\0';
2558 get_word(cmd
, sizeof(cmd
), &p
);
2559 get_word(url
, sizeof(url
), &p
);
2560 get_word(protocol
, sizeof(protocol
), &p
);
2562 pstrcpy(c
->method
, sizeof(c
->method
), cmd
);
2563 pstrcpy(c
->url
, sizeof(c
->url
), url
);
2564 pstrcpy(c
->protocol
, sizeof(c
->protocol
), protocol
);
2567 if (url_open_dyn_buf(c
->pb
) < 0) {
2568 /* XXX: cannot do more */
2569 c
->pb
= NULL
; /* safety */
2573 /* check version name */
2574 if (strcmp(protocol
, "RTSP/1.0") != 0) {
2575 rtsp_reply_error(c
, RTSP_STATUS_VERSION
);
2579 /* parse each header line */
2580 memset(header
, 0, sizeof(RTSPHeader
));
2581 /* skip to next line */
2582 while (*p
!= '\n' && *p
!= '\0')
2586 while (*p
!= '\0') {
2587 p1
= strchr(p
, '\n');
2591 if (p2
> p
&& p2
[-1] == '\r')
2593 /* skip empty line */
2597 if (len
> sizeof(line
) - 1)
2598 len
= sizeof(line
) - 1;
2599 memcpy(line
, p
, len
);
2601 rtsp_parse_line(header
, line
);
2605 /* handle sequence number */
2606 c
->seq
= header
->seq
;
2608 if (!strcmp(cmd
, "DESCRIBE")) {
2609 rtsp_cmd_describe(c
, url
);
2610 } else if (!strcmp(cmd
, "OPTIONS")) {
2611 rtsp_cmd_options(c
, url
);
2612 } else if (!strcmp(cmd
, "SETUP")) {
2613 rtsp_cmd_setup(c
, url
, header
);
2614 } else if (!strcmp(cmd
, "PLAY")) {
2615 rtsp_cmd_play(c
, url
, header
);
2616 } else if (!strcmp(cmd
, "PAUSE")) {
2617 rtsp_cmd_pause(c
, url
, header
);
2618 } else if (!strcmp(cmd
, "TEARDOWN")) {
2619 rtsp_cmd_teardown(c
, url
, header
);
2621 rtsp_reply_error(c
, RTSP_STATUS_METHOD
);
2624 len
= url_close_dyn_buf(c
->pb
, &c
->pb_buffer
);
2625 c
->pb
= NULL
; /* safety */
2627 /* XXX: cannot do more */
2630 c
->buffer_ptr
= c
->pb_buffer
;
2631 c
->buffer_end
= c
->pb_buffer
+ len
;
2632 c
->state
= RTSPSTATE_SEND_REPLY
;
2636 /* XXX: move that to rtsp.c, but would need to replace FFStream by
2638 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
2639 struct in_addr my_ip
)
2641 ByteIOContext pb1
, *pb
= &pb1
;
2642 int i
, payload_type
, port
, private_payload_type
, j
;
2643 const char *ipstr
, *title
, *mediatype
;
2646 if (url_open_dyn_buf(pb
) < 0)
2649 /* general media info */
2651 url_fprintf(pb
, "v=0\n");
2652 ipstr
= inet_ntoa(my_ip
);
2653 url_fprintf(pb
, "o=- 0 0 IN IP4 %s\n", ipstr
);
2654 title
= stream
->title
;
2655 if (title
[0] == '\0')
2657 url_fprintf(pb
, "s=%s\n", title
);
2658 if (stream
->comment
[0] != '\0')
2659 url_fprintf(pb
, "i=%s\n", stream
->comment
);
2660 if (stream
->is_multicast
) {
2661 url_fprintf(pb
, "c=IN IP4 %s\n", inet_ntoa(stream
->multicast_ip
));
2663 /* for each stream, we output the necessary info */
2664 private_payload_type
= RTP_PT_PRIVATE
;
2665 for(i
= 0; i
< stream
->nb_streams
; i
++) {
2666 st
= stream
->streams
[i
];
2667 if (st
->codec
->codec_id
== CODEC_ID_MPEG2TS
) {
2668 mediatype
= "video";
2670 switch(st
->codec
->codec_type
) {
2671 case CODEC_TYPE_AUDIO
:
2672 mediatype
= "audio";
2674 case CODEC_TYPE_VIDEO
:
2675 mediatype
= "video";
2678 mediatype
= "application";
2682 /* NOTE: the port indication is not correct in case of
2683 unicast. It is not an issue because RTSP gives it */
2684 payload_type
= rtp_get_payload_type(st
->codec
);
2685 if (payload_type
< 0)
2686 payload_type
= private_payload_type
++;
2687 if (stream
->is_multicast
) {
2688 port
= stream
->multicast_port
+ 2 * i
;
2692 url_fprintf(pb
, "m=%s %d RTP/AVP %d\n",
2693 mediatype
, port
, payload_type
);
2694 if (payload_type
>= RTP_PT_PRIVATE
) {
2695 /* for private payload type, we need to give more info */
2696 switch(st
->codec
->codec_id
) {
2697 case CODEC_ID_MPEG4
:
2700 url_fprintf(pb
, "a=rtpmap:%d MP4V-ES/%d\n",
2701 payload_type
, 90000);
2702 /* we must also add the mpeg4 header */
2703 data
= st
->codec
->extradata
;
2705 url_fprintf(pb
, "a=fmtp:%d config=", payload_type
);
2706 for(j
=0;j
<st
->codec
->extradata_size
;j
++) {
2707 url_fprintf(pb
, "%02x", data
[j
]);
2709 url_fprintf(pb
, "\n");
2714 /* XXX: add other codecs ? */
2718 url_fprintf(pb
, "a=control:streamid=%d\n", i
);
2720 return url_close_dyn_buf(pb
, pbuffer
);
2722 url_close_dyn_buf(pb
, pbuffer
);
2727 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
)
2729 // rtsp_reply_header(c, RTSP_STATUS_OK);
2730 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK
, "OK");
2731 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2732 url_fprintf(c
->pb
, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2733 url_fprintf(c
->pb
, "\r\n");
2736 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
)
2742 int content_length
, len
;
2743 struct sockaddr_in my_addr
;
2745 /* find which url is asked */
2746 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2751 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2752 if (!stream
->is_feed
&& stream
->fmt
== &rtp_muxer
&&
2753 !strcmp(path
, stream
->filename
)) {
2757 /* no stream found */
2758 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2762 /* prepare the media description in sdp format */
2764 /* get the host IP */
2765 len
= sizeof(my_addr
);
2766 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
2767 content_length
= prepare_sdp_description(stream
, &content
, my_addr
.sin_addr
);
2768 if (content_length
< 0) {
2769 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2772 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2773 url_fprintf(c
->pb
, "Content-Type: application/sdp\r\n");
2774 url_fprintf(c
->pb
, "Content-Length: %d\r\n", content_length
);
2775 url_fprintf(c
->pb
, "\r\n");
2776 put_buffer(c
->pb
, content
, content_length
);
2779 static HTTPContext
*find_rtp_session(const char *session_id
)
2783 if (session_id
[0] == '\0')
2786 for(c
= first_http_ctx
; c
!= NULL
; c
= c
->next
) {
2787 if (!strcmp(c
->session_id
, session_id
))
2793 static RTSPTransportField
*find_transport(RTSPHeader
*h
, enum RTSPProtocol protocol
)
2795 RTSPTransportField
*th
;
2798 for(i
=0;i
<h
->nb_transports
;i
++) {
2799 th
= &h
->transports
[i
];
2800 if (th
->protocol
== protocol
)
2806 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
,
2810 int stream_index
, port
;
2815 RTSPTransportField
*th
;
2816 struct sockaddr_in dest_addr
;
2817 RTSPActionServerSetup setup
;
2819 /* find which url is asked */
2820 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2825 /* now check each stream */
2826 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2827 if (!stream
->is_feed
&& stream
->fmt
== &rtp_muxer
) {
2828 /* accept aggregate filenames only if single stream */
2829 if (!strcmp(path
, stream
->filename
)) {
2830 if (stream
->nb_streams
!= 1) {
2831 rtsp_reply_error(c
, RTSP_STATUS_AGGREGATE
);
2838 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
2840 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2841 stream
->filename
, stream_index
);
2842 if (!strcmp(path
, buf
))
2847 /* no stream found */
2848 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2852 /* generate session id if needed */
2853 if (h
->session_id
[0] == '\0') {
2854 snprintf(h
->session_id
, sizeof(h
->session_id
), "%08x%08x",
2855 av_random(&random_state
), av_random(&random_state
));
2858 /* find rtp session, and create it if none found */
2859 rtp_c
= find_rtp_session(h
->session_id
);
2861 /* always prefer UDP */
2862 th
= find_transport(h
, RTSP_PROTOCOL_RTP_UDP
);
2864 th
= find_transport(h
, RTSP_PROTOCOL_RTP_TCP
);
2866 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2871 rtp_c
= rtp_new_connection(&c
->from_addr
, stream
, h
->session_id
,
2874 rtsp_reply_error(c
, RTSP_STATUS_BANDWIDTH
);
2878 /* open input stream */
2879 if (open_input_stream(rtp_c
, "") < 0) {
2880 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2885 /* test if stream is OK (test needed because several SETUP needs
2886 to be done for a given file) */
2887 if (rtp_c
->stream
!= stream
) {
2888 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
);
2892 /* test if stream is already set up */
2893 if (rtp_c
->rtp_ctx
[stream_index
]) {
2894 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2898 /* check transport */
2899 th
= find_transport(h
, rtp_c
->rtp_protocol
);
2900 if (!th
|| (th
->protocol
== RTSP_PROTOCOL_RTP_UDP
&&
2901 th
->client_port_min
<= 0)) {
2902 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2906 /* setup default options */
2907 setup
.transport_option
[0] = '\0';
2908 dest_addr
= rtp_c
->from_addr
;
2909 dest_addr
.sin_port
= htons(th
->client_port_min
);
2911 /* add transport option if needed */
2912 if (ff_rtsp_callback
) {
2913 setup
.ipaddr
= ntohl(dest_addr
.sin_addr
.s_addr
);
2914 if (ff_rtsp_callback(RTSP_ACTION_SERVER_SETUP
, rtp_c
->session_id
,
2915 (char *)&setup
, sizeof(setup
),
2916 stream
->rtsp_option
) < 0) {
2917 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2920 dest_addr
.sin_addr
.s_addr
= htonl(setup
.ipaddr
);
2924 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, c
) < 0) {
2925 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2929 /* now everything is OK, so we can send the connection parameters */
2930 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2932 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2934 switch(rtp_c
->rtp_protocol
) {
2935 case RTSP_PROTOCOL_RTP_UDP
:
2936 port
= rtp_get_local_port(rtp_c
->rtp_handles
[stream_index
]);
2937 url_fprintf(c
->pb
, "Transport: RTP/AVP/UDP;unicast;"
2938 "client_port=%d-%d;server_port=%d-%d",
2939 th
->client_port_min
, th
->client_port_min
+ 1,
2942 case RTSP_PROTOCOL_RTP_TCP
:
2943 url_fprintf(c
->pb
, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2944 stream_index
* 2, stream_index
* 2 + 1);
2949 if (setup
.transport_option
[0] != '\0') {
2950 url_fprintf(c
->pb
, ";%s", setup
.transport_option
);
2952 url_fprintf(c
->pb
, "\r\n");
2955 url_fprintf(c
->pb
, "\r\n");
2959 /* find an rtp connection by using the session ID. Check consistency
2961 static HTTPContext
*find_rtp_session_with_url(const char *url
,
2962 const char *session_id
)
2970 rtp_c
= find_rtp_session(session_id
);
2974 /* find which url is asked */
2975 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2979 if(!strcmp(path
, rtp_c
->stream
->filename
)) return rtp_c
;
2980 for(s
=0; s
<rtp_c
->stream
->nb_streams
; ++s
) {
2981 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2982 rtp_c
->stream
->filename
, s
);
2983 if(!strncmp(path
, buf
, sizeof(buf
))) {
2984 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2991 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2995 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2997 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3001 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
3002 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
&&
3003 rtp_c
->state
!= HTTPSTATE_READY
) {
3004 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
3009 /* XXX: seek in stream */
3010 if (h
->range_start
!= AV_NOPTS_VALUE
) {
3011 printf("range_start=%0.3f\n", (double)h
->range_start
/ AV_TIME_BASE
);
3012 av_seek_frame(rtp_c
->fmt_in
, -1, h
->range_start
);
3016 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
3018 /* now everything is OK, so we can send the connection parameters */
3019 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3021 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
3022 url_fprintf(c
->pb
, "\r\n");
3025 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
3029 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
3031 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3035 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
3036 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
) {
3037 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
3041 rtp_c
->state
= HTTPSTATE_READY
;
3042 rtp_c
->first_pts
= AV_NOPTS_VALUE
;
3043 /* now everything is OK, so we can send the connection parameters */
3044 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3046 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
3047 url_fprintf(c
->pb
, "\r\n");
3050 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
3054 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
3056 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3060 /* abort the session */
3061 close_connection(rtp_c
);
3063 if (ff_rtsp_callback
) {
3064 ff_rtsp_callback(RTSP_ACTION_SERVER_TEARDOWN
, rtp_c
->session_id
,
3066 rtp_c
->stream
->rtsp_option
);
3069 /* now everything is OK, so we can send the connection parameters */
3070 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3072 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
3073 url_fprintf(c
->pb
, "\r\n");
3077 /********************************************************************/
3080 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
3081 FFStream
*stream
, const char *session_id
,
3082 enum RTSPProtocol rtp_protocol
)
3084 HTTPContext
*c
= NULL
;
3085 const char *proto_str
;
3087 /* XXX: should output a warning page when coming
3088 close to the connection limit */
3089 if (nb_connections
>= nb_max_connections
)
3092 /* add a new connection */
3093 c
= av_mallocz(sizeof(HTTPContext
));
3098 c
->poll_entry
= NULL
;
3099 c
->from_addr
= *from_addr
;
3100 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
3101 c
->buffer
= av_malloc(c
->buffer_size
);
3106 pstrcpy(c
->session_id
, sizeof(c
->session_id
), session_id
);
3107 c
->state
= HTTPSTATE_READY
;
3108 c
->is_packetized
= 1;
3109 c
->rtp_protocol
= rtp_protocol
;
3111 /* protocol is shown in statistics */
3112 switch(c
->rtp_protocol
) {
3113 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3114 proto_str
= "MCAST";
3116 case RTSP_PROTOCOL_RTP_UDP
:
3119 case RTSP_PROTOCOL_RTP_TCP
:
3126 pstrcpy(c
->protocol
, sizeof(c
->protocol
), "RTP/");
3127 pstrcat(c
->protocol
, sizeof(c
->protocol
), proto_str
);
3129 current_bandwidth
+= stream
->bandwidth
;
3131 c
->next
= first_http_ctx
;
3143 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3144 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3146 static int rtp_new_av_stream(HTTPContext
*c
,
3147 int stream_index
, struct sockaddr_in
*dest_addr
,
3148 HTTPContext
*rtsp_c
)
3150 AVFormatContext
*ctx
;
3156 int max_packet_size
;
3158 /* now we can open the relevant output stream */
3159 ctx
= av_alloc_format_context();
3162 ctx
->oformat
= &rtp_muxer
;
3164 st
= av_mallocz(sizeof(AVStream
));
3167 st
->codec
= avcodec_alloc_context();
3168 ctx
->nb_streams
= 1;
3169 ctx
->streams
[0] = st
;
3171 if (!c
->stream
->feed
||
3172 c
->stream
->feed
== c
->stream
) {
3173 memcpy(st
, c
->stream
->streams
[stream_index
], sizeof(AVStream
));
3176 c
->stream
->feed
->streams
[c
->stream
->feed_streams
[stream_index
]],
3180 /* build destination RTP address */
3181 ipaddr
= inet_ntoa(dest_addr
->sin_addr
);
3183 switch(c
->rtp_protocol
) {
3184 case RTSP_PROTOCOL_RTP_UDP
:
3185 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3188 /* XXX: also pass as parameter to function ? */
3189 if (c
->stream
->is_multicast
) {
3191 ttl
= c
->stream
->multicast_ttl
;
3194 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3195 "rtp://%s:%d?multicast=1&ttl=%d",
3196 ipaddr
, ntohs(dest_addr
->sin_port
), ttl
);
3198 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3199 "rtp://%s:%d", ipaddr
, ntohs(dest_addr
->sin_port
));
3202 if (url_open(&h
, ctx
->filename
, URL_WRONLY
) < 0)
3204 c
->rtp_handles
[stream_index
] = h
;
3205 max_packet_size
= url_get_max_packet_size(h
);
3207 case RTSP_PROTOCOL_RTP_TCP
:
3210 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
3216 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3217 ipaddr
, ntohs(dest_addr
->sin_port
),
3219 c
->stream
->filename
, stream_index
, c
->protocol
);
3221 /* normally, no packets should be output here, but the packet size may be checked */
3222 if (url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
) < 0) {
3223 /* XXX: close stream */
3226 av_set_parameters(ctx
, NULL
);