2 * Multiple format streaming server
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #define HAVE_AV_CONFIG_H
25 #include <sys/ioctl.h>
29 #undef time //needed because HAVE_AV_CONFIG_H is defined on top
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
38 #ifdef CONFIG_HAVE_DLFCN
45 /* maximum number of simultaneous HTTP connections */
46 #define HTTP_MAX_CONNECTIONS 2000
49 HTTPSTATE_WAIT_REQUEST
,
50 HTTPSTATE_SEND_HEADER
,
51 HTTPSTATE_SEND_DATA_HEADER
,
52 HTTPSTATE_SEND_DATA
, /* sending TCP or UDP data */
53 HTTPSTATE_SEND_DATA_TRAILER
,
54 HTTPSTATE_RECEIVE_DATA
,
55 HTTPSTATE_WAIT_FEED
, /* wait for data from the feed */
58 RTSPSTATE_WAIT_REQUEST
,
60 RTSPSTATE_SEND_PACKET
,
63 const char *http_state
[] = {
79 #define IOBUFFER_INIT_SIZE 8192
81 /* coef for exponential mean for bitrate estimation in statistics */
84 /* timeouts are in ms */
85 #define HTTP_REQUEST_TIMEOUT (15 * 1000)
86 #define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
88 #define SYNC_TIMEOUT (10 * 1000)
91 int64_t count1
, count2
;
95 /* context associated with one connection */
96 typedef struct HTTPContext
{
98 int fd
; /* socket file descriptor */
99 struct sockaddr_in from_addr
; /* origin */
100 struct pollfd
*poll_entry
; /* used when polling */
102 uint8_t *buffer_ptr
, *buffer_end
;
105 struct HTTPContext
*next
;
106 int got_key_frame
; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
110 /* input format handling */
111 AVFormatContext
*fmt_in
;
112 long start_time
; /* In milliseconds - this wraps fairly often */
113 int64_t first_pts
; /* initial pts value */
114 int64_t cur_pts
; /* current pts value from the stream in us */
115 int64_t cur_frame_duration
; /* duration of the current frame in us */
116 int cur_frame_bytes
; /* output frame size, needed to compute
117 the time at which we send each
119 int pts_stream_index
; /* stream we choose as clock reference */
120 int64_t cur_clock
; /* current clock reference value in us */
121 /* output format handling */
122 struct FFStream
*stream
;
123 /* -1 is invalid stream */
124 int feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
125 int switch_feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
127 AVFormatContext fmt_ctx
; /* instance of FFStream for one user */
128 int last_packet_sent
; /* true if last data packet was sent */
130 DataRateData datarate
;
137 int is_packetized
; /* if true, the stream is packetized */
138 int packet_stream_index
; /* current stream for output in state machine */
140 /* RTSP state specific */
141 uint8_t *pb_buffer
; /* XXX: use that in all the code */
143 int seq
; /* RTSP sequence number */
145 /* RTP state specific */
146 enum RTSPProtocol rtp_protocol
;
147 char session_id
[32]; /* session id */
148 AVFormatContext
*rtp_ctx
[MAX_STREAMS
];
150 /* RTP/UDP specific */
151 URLContext
*rtp_handles
[MAX_STREAMS
];
153 /* RTP/TCP specific */
154 struct HTTPContext
*rtsp_c
;
155 uint8_t *packet_buffer
, *packet_buffer_ptr
, *packet_buffer_end
;
158 static AVFrame dummy_frame
;
160 /* each generated stream is described here */
164 STREAM_TYPE_REDIRECT
,
167 enum IPAddressAction
{
172 typedef struct IPAddressACL
{
173 struct IPAddressACL
*next
;
174 enum IPAddressAction action
;
175 /* These are in host order */
176 struct in_addr first
;
180 /* description of each stream of the ffserver.conf file */
181 typedef struct FFStream
{
182 enum StreamType stream_type
;
183 char filename
[1024]; /* stream filename */
184 struct FFStream
*feed
; /* feed we are using (can be null if
186 AVFormatParameters
*ap_in
; /* input parameters */
187 AVInputFormat
*ifmt
; /* if non NULL, force input format */
191 int prebuffer
; /* Number of millseconds early to start */
192 long max_time
; /* Number of milliseconds to run */
194 AVStream
*streams
[MAX_STREAMS
];
195 int feed_streams
[MAX_STREAMS
]; /* index of streams in the feed */
196 char feed_filename
[1024]; /* file name of the feed storage, or
197 input file name for a stream */
202 pid_t pid
; /* Of ffmpeg process */
203 time_t pid_start
; /* Of ffmpeg process */
205 struct FFStream
*next
;
206 int bandwidth
; /* bandwidth, in kbits/s */
209 /* multicast specific */
211 struct in_addr multicast_ip
;
212 int multicast_port
; /* first port used for multicast */
214 int loop
; /* if true, send the stream in loops (only meaningful if file) */
217 int feed_opened
; /* true if someone is writing to the feed */
218 int is_feed
; /* true if it is a feed */
219 int readonly
; /* True if writing is prohibited to the file */
221 int64_t bytes_served
;
222 int64_t feed_max_size
; /* maximum storage size, zero means unlimited */
223 int64_t feed_write_index
; /* current write position in feed (it wraps round) */
224 int64_t feed_size
; /* current size of feed */
225 struct FFStream
*next_feed
;
228 typedef struct FeedData
{
229 long long data_count
;
230 float avg_frame_size
; /* frame size averraged over last frames with exponential mean */
233 struct sockaddr_in my_http_addr
;
234 struct sockaddr_in my_rtsp_addr
;
236 static char logfilename
[1024];
237 static HTTPContext
*first_http_ctx
;
238 static FFStream
*first_feed
; /* contains only feeds */
239 static FFStream
*first_stream
; /* contains all streams, including feeds */
241 static void new_connection(int server_fd
, int is_rtsp
);
242 static void close_connection(HTTPContext
*c
);
245 static int handle_connection(HTTPContext
*c
);
246 static int http_parse_request(HTTPContext
*c
);
247 static int http_send_data(HTTPContext
*c
);
248 static void compute_stats(HTTPContext
*c
);
249 static int open_input_stream(HTTPContext
*c
, const char *info
);
250 static int http_start_receive_data(HTTPContext
*c
);
251 static int http_receive_data(HTTPContext
*c
);
254 static int rtsp_parse_request(HTTPContext
*c
);
255 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
);
256 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
);
257 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
258 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
259 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
260 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPHeader
*h
);
263 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
264 struct in_addr my_ip
);
267 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
268 FFStream
*stream
, const char *session_id
,
269 enum RTSPProtocol rtp_protocol
);
270 static int rtp_new_av_stream(HTTPContext
*c
,
271 int stream_index
, struct sockaddr_in
*dest_addr
,
272 HTTPContext
*rtsp_c
);
274 static const char *my_program_name
;
275 static const char *my_program_dir
;
277 static int ffserver_debug
;
278 static int ffserver_daemon
;
279 static int no_launch
;
280 static int need_to_start_children
;
282 static int nb_max_connections
;
283 static int nb_connections
;
285 static int max_bandwidth
;
286 static int current_bandwidth
;
288 static long cur_time
; // Making this global saves on passing it around everywhere
290 static long gettime_ms(void)
294 gettimeofday(&tv
,NULL
);
295 return (long long)tv
.tv_sec
* 1000 + (tv
.tv_usec
/ 1000);
298 static FILE *logfile
= NULL
;
300 static void __attribute__ ((format (printf
, 1, 2))) http_log(const char *fmt
, ...)
306 vfprintf(logfile
, fmt
, ap
);
312 static char *ctime1(char *buf2
)
320 p
= buf2
+ strlen(p
) - 1;
326 static void log_connection(HTTPContext
*c
)
333 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64
"\n",
334 inet_ntoa(c
->from_addr
.sin_addr
),
335 ctime1(buf2
), c
->method
, c
->url
,
336 c
->protocol
, (c
->http_error ? c
->http_error
: 200), c
->data_count
);
339 static void update_datarate(DataRateData
*drd
, int64_t count
)
341 if (!drd
->time1
&& !drd
->count1
) {
342 drd
->time1
= drd
->time2
= cur_time
;
343 drd
->count1
= drd
->count2
= count
;
345 if (cur_time
- drd
->time2
> 5000) {
346 drd
->time1
= drd
->time2
;
347 drd
->count1
= drd
->count2
;
348 drd
->time2
= cur_time
;
354 /* In bytes per second */
355 static int compute_datarate(DataRateData
*drd
, int64_t count
)
357 if (cur_time
== drd
->time1
)
360 return ((count
- drd
->count1
) * 1000) / (cur_time
- drd
->time1
);
364 static void start_children(FFStream
*feed
)
369 for (; feed
; feed
= feed
->next
) {
370 if (feed
->child_argv
&& !feed
->pid
) {
371 feed
->pid_start
= time(0);
376 fprintf(stderr
, "Unable to create children\n");
385 for (i
= 3; i
< 256; i
++) {
389 if (!ffserver_debug
) {
390 i
= open("/dev/null", O_RDWR
);
399 pstrcpy(pathname
, sizeof(pathname
), my_program_name
);
401 slash
= strrchr(pathname
, '/');
407 strcpy(slash
, "ffmpeg");
409 /* This is needed to make relative pathnames work */
410 chdir(my_program_dir
);
412 signal(SIGPIPE
, SIG_DFL
);
414 execvp(pathname
, feed
->child_argv
);
422 /* open a listening socket */
423 static int socket_open_listen(struct sockaddr_in
*my_addr
)
427 server_fd
= socket(AF_INET
,SOCK_STREAM
,0);
434 setsockopt(server_fd
, SOL_SOCKET
, SO_REUSEADDR
, &tmp
, sizeof(tmp
));
436 if (bind (server_fd
, (struct sockaddr
*) my_addr
, sizeof (*my_addr
)) < 0) {
438 snprintf(bindmsg
, sizeof(bindmsg
), "bind(port %d)", ntohs(my_addr
->sin_port
));
444 if (listen (server_fd
, 5) < 0) {
449 fcntl(server_fd
, F_SETFL
, O_NONBLOCK
);
454 /* start all multicast streams */
455 static void start_multicast(void)
460 struct sockaddr_in dest_addr
;
461 int default_port
, stream_index
;
464 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
465 if (stream
->is_multicast
) {
466 /* open the RTP connection */
467 snprintf(session_id
, sizeof(session_id
),
468 "%08x%08x", (int)random(), (int)random());
470 /* choose a port if none given */
471 if (stream
->multicast_port
== 0) {
472 stream
->multicast_port
= default_port
;
476 dest_addr
.sin_family
= AF_INET
;
477 dest_addr
.sin_addr
= stream
->multicast_ip
;
478 dest_addr
.sin_port
= htons(stream
->multicast_port
);
480 rtp_c
= rtp_new_connection(&dest_addr
, stream
, session_id
,
481 RTSP_PROTOCOL_RTP_UDP_MULTICAST
);
485 if (open_input_stream(rtp_c
, "") < 0) {
486 fprintf(stderr
, "Could not open input stream for stream '%s'\n",
491 /* open each RTP stream */
492 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
494 dest_addr
.sin_port
= htons(stream
->multicast_port
+
496 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, NULL
) < 0) {
497 fprintf(stderr
, "Could not open output stream '%s/streamid=%d'\n",
498 stream
->filename
, stream_index
);
503 /* change state to send data */
504 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
509 /* main loop of the http server */
510 static int http_server(void)
512 int server_fd
, ret
, rtsp_server_fd
, delay
, delay1
;
513 struct pollfd poll_table
[HTTP_MAX_CONNECTIONS
+ 2], *poll_entry
;
514 HTTPContext
*c
, *c_next
;
516 server_fd
= socket_open_listen(&my_http_addr
);
520 rtsp_server_fd
= socket_open_listen(&my_rtsp_addr
);
521 if (rtsp_server_fd
< 0)
524 http_log("ffserver started.\n");
526 start_children(first_feed
);
528 first_http_ctx
= NULL
;
534 poll_entry
= poll_table
;
535 poll_entry
->fd
= server_fd
;
536 poll_entry
->events
= POLLIN
;
539 poll_entry
->fd
= rtsp_server_fd
;
540 poll_entry
->events
= POLLIN
;
543 /* wait for events on each HTTP handle */
550 case HTTPSTATE_SEND_HEADER
:
551 case RTSPSTATE_SEND_REPLY
:
552 case RTSPSTATE_SEND_PACKET
:
553 c
->poll_entry
= poll_entry
;
555 poll_entry
->events
= POLLOUT
;
558 case HTTPSTATE_SEND_DATA_HEADER
:
559 case HTTPSTATE_SEND_DATA
:
560 case HTTPSTATE_SEND_DATA_TRAILER
:
561 if (!c
->is_packetized
) {
562 /* for TCP, we output as much as we can (may need to put a limit) */
563 c
->poll_entry
= poll_entry
;
565 poll_entry
->events
= POLLOUT
;
568 /* when ffserver is doing the timing, we work by
569 looking at which packet need to be sent every
571 delay1
= 10; /* one tick wait XXX: 10 ms assumed */
576 case HTTPSTATE_WAIT_REQUEST
:
577 case HTTPSTATE_RECEIVE_DATA
:
578 case HTTPSTATE_WAIT_FEED
:
579 case RTSPSTATE_WAIT_REQUEST
:
580 /* need to catch errors */
581 c
->poll_entry
= poll_entry
;
583 poll_entry
->events
= POLLIN
;/* Maybe this will work */
587 c
->poll_entry
= NULL
;
593 /* wait for an event on one connection. We poll at least every
594 second to handle timeouts */
596 ret
= poll(poll_table
, poll_entry
- poll_table
, delay
);
597 if (ret
< 0 && errno
!= EAGAIN
&& errno
!= EINTR
)
601 cur_time
= gettime_ms();
603 if (need_to_start_children
) {
604 need_to_start_children
= 0;
605 start_children(first_feed
);
608 /* now handle the events */
609 for(c
= first_http_ctx
; c
!= NULL
; c
= c_next
) {
611 if (handle_connection(c
) < 0) {
612 /* close and free the connection */
618 poll_entry
= poll_table
;
619 /* new HTTP connection request ? */
620 if (poll_entry
->revents
& POLLIN
) {
621 new_connection(server_fd
, 0);
624 /* new RTSP connection request ? */
625 if (poll_entry
->revents
& POLLIN
) {
626 new_connection(rtsp_server_fd
, 1);
631 /* start waiting for a new HTTP/RTSP request */
632 static void start_wait_request(HTTPContext
*c
, int is_rtsp
)
634 c
->buffer_ptr
= c
->buffer
;
635 c
->buffer_end
= c
->buffer
+ c
->buffer_size
- 1; /* leave room for '\0' */
638 c
->timeout
= cur_time
+ RTSP_REQUEST_TIMEOUT
;
639 c
->state
= RTSPSTATE_WAIT_REQUEST
;
641 c
->timeout
= cur_time
+ HTTP_REQUEST_TIMEOUT
;
642 c
->state
= HTTPSTATE_WAIT_REQUEST
;
646 static void new_connection(int server_fd
, int is_rtsp
)
648 struct sockaddr_in from_addr
;
650 HTTPContext
*c
= NULL
;
652 len
= sizeof(from_addr
);
653 fd
= accept(server_fd
, (struct sockaddr
*)&from_addr
,
657 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
659 /* XXX: should output a warning page when coming
660 close to the connection limit */
661 if (nb_connections
>= nb_max_connections
)
664 /* add a new connection */
665 c
= av_mallocz(sizeof(HTTPContext
));
670 c
->poll_entry
= NULL
;
671 c
->from_addr
= from_addr
;
672 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
673 c
->buffer
= av_malloc(c
->buffer_size
);
677 c
->next
= first_http_ctx
;
681 start_wait_request(c
, is_rtsp
);
693 static void close_connection(HTTPContext
*c
)
695 HTTPContext
**cp
, *c1
;
697 AVFormatContext
*ctx
;
701 /* remove connection from list */
702 cp
= &first_http_ctx
;
703 while ((*cp
) != NULL
) {
712 /* remove references, if any (XXX: do it faster) */
713 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
718 /* remove connection associated resources */
722 /* close each frame parser */
723 for(i
=0;i
<c
->fmt_in
->nb_streams
;i
++) {
724 st
= c
->fmt_in
->streams
[i
];
725 if (st
->codec
->codec
) {
726 avcodec_close(st
->codec
);
729 av_close_input_file(c
->fmt_in
);
732 /* free RTP output streams if any */
735 nb_streams
= c
->stream
->nb_streams
;
737 for(i
=0;i
<nb_streams
;i
++) {
740 av_write_trailer(ctx
);
743 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
;
766 av_freep(&c
->pb_buffer
);
767 av_freep(&c
->packet_buffer
);
773 static int handle_connection(HTTPContext
*c
)
778 case HTTPSTATE_WAIT_REQUEST
:
779 case RTSPSTATE_WAIT_REQUEST
:
781 if ((c
->timeout
- cur_time
) < 0)
783 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
786 /* no need to read if no events */
787 if (!(c
->poll_entry
->revents
& POLLIN
))
791 len
= read(c
->fd
, c
->buffer_ptr
, 1);
793 if (errno
!= EAGAIN
&& errno
!= EINTR
)
795 } else if (len
== 0) {
798 /* search for end of request. */
800 c
->buffer_ptr
+= len
;
802 if ((ptr
>= c
->buffer
+ 2 && !memcmp(ptr
-2, "\n\n", 2)) ||
803 (ptr
>= c
->buffer
+ 4 && !memcmp(ptr
-4, "\r\n\r\n", 4))) {
804 /* request found : parse it and reply */
805 if (c
->state
== HTTPSTATE_WAIT_REQUEST
) {
806 ret
= http_parse_request(c
);
808 ret
= rtsp_parse_request(c
);
812 } else if (ptr
>= c
->buffer_end
) {
813 /* request too long: cannot do anything */
815 } else goto read_loop
;
819 case HTTPSTATE_SEND_HEADER
:
820 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
823 /* no need to write if no events */
824 if (!(c
->poll_entry
->revents
& POLLOUT
))
826 len
= write(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
);
828 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
829 /* error : close connection */
830 av_freep(&c
->pb_buffer
);
834 c
->buffer_ptr
+= len
;
836 c
->stream
->bytes_served
+= len
;
837 c
->data_count
+= len
;
838 if (c
->buffer_ptr
>= c
->buffer_end
) {
839 av_freep(&c
->pb_buffer
);
844 /* all the buffer was sent : synchronize to the incoming stream */
845 c
->state
= HTTPSTATE_SEND_DATA_HEADER
;
846 c
->buffer_ptr
= c
->buffer_end
= c
->buffer
;
851 case HTTPSTATE_SEND_DATA
:
852 case HTTPSTATE_SEND_DATA_HEADER
:
853 case HTTPSTATE_SEND_DATA_TRAILER
:
854 /* for packetized output, we consider we can always write (the
855 input streams sets the speed). It may be better to verify
856 that we do not rely too much on the kernel queues */
857 if (!c
->is_packetized
) {
858 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
861 /* no need to read if no events */
862 if (!(c
->poll_entry
->revents
& POLLOUT
))
865 if (http_send_data(c
) < 0)
868 case HTTPSTATE_RECEIVE_DATA
:
869 /* no need to read if no events */
870 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
))
872 if (!(c
->poll_entry
->revents
& POLLIN
))
874 if (http_receive_data(c
) < 0)
877 case HTTPSTATE_WAIT_FEED
:
878 /* no need to read if no events */
879 if (c
->poll_entry
->revents
& (POLLIN
| POLLERR
| POLLHUP
))
882 /* nothing to do, we'll be waken up by incoming feed packets */
885 case RTSPSTATE_SEND_REPLY
:
886 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
887 av_freep(&c
->pb_buffer
);
890 /* no need to write if no events */
891 if (!(c
->poll_entry
->revents
& POLLOUT
))
893 len
= write(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
);
895 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
896 /* error : close connection */
897 av_freep(&c
->pb_buffer
);
901 c
->buffer_ptr
+= len
;
902 c
->data_count
+= len
;
903 if (c
->buffer_ptr
>= c
->buffer_end
) {
904 /* all the buffer was sent : wait for a new request */
905 av_freep(&c
->pb_buffer
);
906 start_wait_request(c
, 1);
910 case RTSPSTATE_SEND_PACKET
:
911 if (c
->poll_entry
->revents
& (POLLERR
| POLLHUP
)) {
912 av_freep(&c
->packet_buffer
);
915 /* no need to write if no events */
916 if (!(c
->poll_entry
->revents
& POLLOUT
))
918 len
= write(c
->fd
, c
->packet_buffer_ptr
,
919 c
->packet_buffer_end
- c
->packet_buffer_ptr
);
921 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
922 /* error : close connection */
923 av_freep(&c
->packet_buffer
);
927 c
->packet_buffer_ptr
+= len
;
928 if (c
->packet_buffer_ptr
>= c
->packet_buffer_end
) {
929 /* all the buffer was sent : wait for a new request */
930 av_freep(&c
->packet_buffer
);
931 c
->state
= RTSPSTATE_WAIT_REQUEST
;
935 case HTTPSTATE_READY
:
944 static int extract_rates(char *rates
, int ratelen
, const char *request
)
948 for (p
= request
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
949 if (strncasecmp(p
, "Pragma:", 7) == 0) {
950 const char *q
= p
+ 7;
952 while (*q
&& *q
!= '\n' && isspace(*q
))
955 if (strncasecmp(q
, "stream-switch-entry=", 20) == 0) {
961 memset(rates
, 0xff, ratelen
);
964 while (*q
&& *q
!= '\n' && *q
!= ':')
967 if (sscanf(q
, ":%d:%d", &stream_no
, &rate_no
) != 2) {
971 if (stream_no
< ratelen
&& stream_no
>= 0) {
972 rates
[stream_no
] = rate_no
;
975 while (*q
&& *q
!= '\n' && !isspace(*q
))
992 static int find_stream_in_feed(FFStream
*feed
, AVCodecContext
*codec
, int bit_rate
)
995 int best_bitrate
= 100000000;
998 for (i
= 0; i
< feed
->nb_streams
; i
++) {
999 AVCodecContext
*feed_codec
= feed
->streams
[i
]->codec
;
1001 if (feed_codec
->codec_id
!= codec
->codec_id
||
1002 feed_codec
->sample_rate
!= codec
->sample_rate
||
1003 feed_codec
->width
!= codec
->width
||
1004 feed_codec
->height
!= codec
->height
) {
1008 /* Potential stream */
1010 /* We want the fastest stream less than bit_rate, or the slowest
1011 * faster than bit_rate
1014 if (feed_codec
->bit_rate
<= bit_rate
) {
1015 if (best_bitrate
> bit_rate
|| feed_codec
->bit_rate
> best_bitrate
) {
1016 best_bitrate
= feed_codec
->bit_rate
;
1020 if (feed_codec
->bit_rate
< best_bitrate
) {
1021 best_bitrate
= feed_codec
->bit_rate
;
1030 static int modify_current_stream(HTTPContext
*c
, char *rates
)
1033 FFStream
*req
= c
->stream
;
1034 int action_required
= 0;
1036 /* Not much we can do for a feed */
1040 for (i
= 0; i
< req
->nb_streams
; i
++) {
1041 AVCodecContext
*codec
= req
->streams
[i
]->codec
;
1045 c
->switch_feed_streams
[i
] = req
->feed_streams
[i
];
1048 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 2);
1051 /* Wants off or slow */
1052 c
->switch_feed_streams
[i
] = find_stream_in_feed(req
->feed
, codec
, codec
->bit_rate
/ 4);
1054 /* This doesn't work well when it turns off the only stream! */
1055 c
->switch_feed_streams
[i
] = -2;
1056 c
->feed_streams
[i
] = -2;
1061 if (c
->switch_feed_streams
[i
] >= 0 && c
->switch_feed_streams
[i
] != c
->feed_streams
[i
])
1062 action_required
= 1;
1065 return action_required
;
1069 static void do_switch_stream(HTTPContext
*c
, int i
)
1071 if (c
->switch_feed_streams
[i
] >= 0) {
1073 c
->feed_streams
[i
] = c
->switch_feed_streams
[i
];
1076 /* Now update the stream */
1078 c
->switch_feed_streams
[i
] = -1;
1081 /* XXX: factorize in utils.c ? */
1082 /* XXX: take care with different space meaning */
1083 static void skip_spaces(const char **pp
)
1087 while (*p
== ' ' || *p
== '\t')
1092 static void get_word(char *buf
, int buf_size
, const char **pp
)
1100 while (!isspace(*p
) && *p
!= '\0') {
1101 if ((q
- buf
) < buf_size
- 1)
1110 static int validate_acl(FFStream
*stream
, HTTPContext
*c
)
1112 enum IPAddressAction last_action
= IP_DENY
;
1114 struct in_addr
*src
= &c
->from_addr
.sin_addr
;
1115 unsigned long src_addr
= ntohl(src
->s_addr
);
1117 for (acl
= stream
->acl
; acl
; acl
= acl
->next
) {
1118 if (src_addr
>= acl
->first
.s_addr
&& src_addr
<= acl
->last
.s_addr
) {
1119 return (acl
->action
== IP_ALLOW
) ?
1 : 0;
1121 last_action
= acl
->action
;
1124 /* Nothing matched, so return not the last action */
1125 return (last_action
== IP_DENY
) ?
1 : 0;
1128 /* compute the real filename of a file by matching it without its
1129 extensions to all the stream filenames */
1130 static void compute_real_filename(char *filename
, int max_size
)
1137 /* compute filename by matching without the file extensions */
1138 pstrcpy(file1
, sizeof(file1
), filename
);
1139 p
= strrchr(file1
, '.');
1142 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
1143 pstrcpy(file2
, sizeof(file2
), stream
->filename
);
1144 p
= strrchr(file2
, '.');
1147 if (!strcmp(file1
, file2
)) {
1148 pstrcpy(filename
, max_size
, stream
->filename
);
1163 /* parse http request and prepare header */
1164 static int http_parse_request(HTTPContext
*c
)
1167 enum RedirType redir_type
;
1169 char info
[1024], *filename
;
1173 const char *mime_type
;
1177 char *useragent
= 0;
1180 get_word(cmd
, sizeof(cmd
), (const char **)&p
);
1181 pstrcpy(c
->method
, sizeof(c
->method
), cmd
);
1183 if (!strcmp(cmd
, "GET"))
1185 else if (!strcmp(cmd
, "POST"))
1190 get_word(url
, sizeof(url
), (const char **)&p
);
1191 pstrcpy(c
->url
, sizeof(c
->url
), url
);
1193 get_word(protocol
, sizeof(protocol
), (const char **)&p
);
1194 if (strcmp(protocol
, "HTTP/1.0") && strcmp(protocol
, "HTTP/1.1"))
1197 pstrcpy(c
->protocol
, sizeof(c
->protocol
), protocol
);
1200 http_log("New connection: %s %s\n", cmd
, url
);
1202 /* find the filename and the optional info string in the request */
1209 pstrcpy(info
, sizeof(info
), p
);
1215 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1216 if (strncasecmp(p
, "User-Agent:", 11) == 0) {
1218 if (*useragent
&& *useragent
!= '\n' && isspace(*useragent
))
1222 p
= strchr(p
, '\n');
1229 redir_type
= REDIR_NONE
;
1230 if (match_ext(filename
, "asx")) {
1231 redir_type
= REDIR_ASX
;
1232 filename
[strlen(filename
)-1] = 'f';
1233 } else if (match_ext(filename
, "asf") &&
1234 (!useragent
|| strncasecmp(useragent
, "NSPlayer", 8) != 0)) {
1235 /* if this isn't WMP or lookalike, return the redirector file */
1236 redir_type
= REDIR_ASF
;
1237 } else if (match_ext(filename
, "rpm,ram")) {
1238 redir_type
= REDIR_RAM
;
1239 strcpy(filename
+ strlen(filename
)-2, "m");
1240 } else if (match_ext(filename
, "rtsp")) {
1241 redir_type
= REDIR_RTSP
;
1242 compute_real_filename(filename
, sizeof(url
) - 1);
1243 } else if (match_ext(filename
, "sdp")) {
1244 redir_type
= REDIR_SDP
;
1245 compute_real_filename(filename
, sizeof(url
) - 1);
1248 stream
= first_stream
;
1249 while (stream
!= NULL
) {
1250 if (!strcmp(stream
->filename
, filename
) && validate_acl(stream
, c
))
1252 stream
= stream
->next
;
1254 if (stream
== NULL
) {
1255 snprintf(msg
, sizeof(msg
), "File '%s' not found", url
);
1260 memcpy(c
->feed_streams
, stream
->feed_streams
, sizeof(c
->feed_streams
));
1261 memset(c
->switch_feed_streams
, -1, sizeof(c
->switch_feed_streams
));
1263 if (stream
->stream_type
== STREAM_TYPE_REDIRECT
) {
1264 c
->http_error
= 301;
1266 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 301 Moved\r\n");
1267 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Location: %s\r\n", stream
->feed_filename
);
1268 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1269 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1270 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Moved</title></head><body>\r\n");
1271 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "You should be <a href=\"%s\">redirected</a>.\r\n", stream
->feed_filename
);
1272 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1274 /* prepare output buffer */
1275 c
->buffer_ptr
= c
->buffer
;
1277 c
->state
= HTTPSTATE_SEND_HEADER
;
1281 /* If this is WMP, get the rate information */
1282 if (extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1283 if (modify_current_stream(c
, ratebuf
)) {
1284 for (i
= 0; i
< sizeof(c
->feed_streams
) / sizeof(c
->feed_streams
[0]); i
++) {
1285 if (c
->switch_feed_streams
[i
] >= 0)
1286 do_switch_stream(c
, i
);
1291 if (c
->post
== 0 && stream
->stream_type
== STREAM_TYPE_LIVE
) {
1292 current_bandwidth
+= stream
->bandwidth
;
1295 if (c
->post
== 0 && max_bandwidth
< current_bandwidth
) {
1296 c
->http_error
= 200;
1298 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 Server too busy\r\n");
1299 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: text/html\r\n");
1300 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1301 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<html><head><title>Too busy</title></head><body>\r\n");
1302 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");
1303 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",
1304 current_bandwidth
, max_bandwidth
);
1305 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</body></html>\r\n");
1307 /* prepare output buffer */
1308 c
->buffer_ptr
= c
->buffer
;
1310 c
->state
= HTTPSTATE_SEND_HEADER
;
1314 if (redir_type
!= REDIR_NONE
) {
1317 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1318 if (strncasecmp(p
, "Host:", 5) == 0) {
1322 p
= strchr(p
, '\n');
1333 while (isspace(*hostinfo
))
1336 eoh
= strchr(hostinfo
, '\n');
1338 if (eoh
[-1] == '\r')
1341 if (eoh
- hostinfo
< sizeof(hostbuf
) - 1) {
1342 memcpy(hostbuf
, hostinfo
, eoh
- hostinfo
);
1343 hostbuf
[eoh
- hostinfo
] = 0;
1345 c
->http_error
= 200;
1347 switch(redir_type
) {
1349 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASX Follows\r\n");
1350 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1351 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1352 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ASX Version=\"3\">\r\n");
1353 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
1354 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
1355 hostbuf
, filename
, info
);
1356 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</ASX>\r\n");
1359 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RAM Follows\r\n");
1360 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: audio/x-pn-realaudio\r\n");
1361 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1362 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "# Autogenerated by ffserver\r\n");
1363 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "http://%s/%s%s\r\n",
1364 hostbuf
, filename
, info
);
1367 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 ASF Redirect follows\r\n");
1368 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: video/x-ms-asf\r\n");
1369 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1370 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "[Reference]\r\n");
1371 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Ref1=http://%s/%s%s\r\n",
1372 hostbuf
, filename
, info
);
1376 char hostname
[256], *p
;
1377 /* extract only hostname */
1378 pstrcpy(hostname
, sizeof(hostname
), hostbuf
);
1379 p
= strrchr(hostname
, ':');
1382 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 RTSP Redirect follows\r\n");
1383 /* XXX: incorrect mime type ? */
1384 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/x-rtsp\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
, "rtsp://%s:%d/%s\r\n",
1387 hostname
, ntohs(my_rtsp_addr
.sin_port
),
1394 int sdp_data_size
, len
;
1395 struct sockaddr_in my_addr
;
1397 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1398 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: application/sdp\r\n");
1399 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1401 len
= sizeof(my_addr
);
1402 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
1404 /* XXX: should use a dynamic buffer */
1405 sdp_data_size
= prepare_sdp_description(stream
,
1408 if (sdp_data_size
> 0) {
1409 memcpy(q
, sdp_data
, sdp_data_size
);
1421 /* prepare output buffer */
1422 c
->buffer_ptr
= c
->buffer
;
1424 c
->state
= HTTPSTATE_SEND_HEADER
;
1430 snprintf(msg
, sizeof(msg
), "ASX/RAM file not handled");
1434 stream
->conns_served
++;
1436 /* XXX: add there authenticate and IP match */
1439 /* if post, it means a feed is being sent */
1440 if (!stream
->is_feed
) {
1441 /* However it might be a status report from WMP! Lets log the data
1442 * as it might come in handy one day
1447 for (p
= c
->buffer
; *p
&& *p
!= '\r' && *p
!= '\n'; ) {
1448 if (strncasecmp(p
, "Pragma: log-line=", 17) == 0) {
1452 if (strncasecmp(p
, "Pragma: client-id=", 18) == 0) {
1453 client_id
= strtol(p
+ 18, 0, 10);
1455 p
= strchr(p
, '\n');
1463 char *eol
= strchr(logline
, '\n');
1468 if (eol
[-1] == '\r')
1470 http_log("%.*s\n", (int) (eol
- logline
), logline
);
1471 c
->suppress_log
= 1;
1476 http_log("\nGot request:\n%s\n", c
->buffer
);
1479 if (client_id
&& extract_rates(ratebuf
, sizeof(ratebuf
), c
->buffer
)) {
1482 /* Now we have to find the client_id */
1483 for (wmpc
= first_http_ctx
; wmpc
; wmpc
= wmpc
->next
) {
1484 if (wmpc
->wmp_client_id
== client_id
)
1489 if (modify_current_stream(wmpc
, ratebuf
)) {
1490 wmpc
->switch_pending
= 1;
1495 snprintf(msg
, sizeof(msg
), "POST command not handled");
1499 if (http_start_receive_data(c
) < 0) {
1500 snprintf(msg
, sizeof(msg
), "could not open feed");
1504 c
->state
= HTTPSTATE_RECEIVE_DATA
;
1509 if (strcmp(stream
->filename
+ strlen(stream
->filename
) - 4, ".asf") == 0) {
1510 http_log("\nGot request:\n%s\n", c
->buffer
);
1514 if (c
->stream
->stream_type
== STREAM_TYPE_STATUS
)
1517 /* open input stream */
1518 if (open_input_stream(c
, info
) < 0) {
1519 snprintf(msg
, sizeof(msg
), "Input stream corresponding to '%s' not found", url
);
1523 /* prepare http header */
1525 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 200 OK\r\n");
1526 mime_type
= c
->stream
->fmt
->mime_type
;
1528 mime_type
= "application/x-octet_stream";
1529 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Pragma: no-cache\r\n");
1531 /* for asf, we need extra headers */
1532 if (!strcmp(c
->stream
->fmt
->name
,"asf_stream")) {
1533 /* Need to allocate a client id */
1535 c
->wmp_client_id
= random() & 0x7fffffff;
1537 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
);
1539 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-Type: %s\r\n", mime_type
);
1540 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1542 /* prepare output buffer */
1544 c
->buffer_ptr
= c
->buffer
;
1546 c
->state
= HTTPSTATE_SEND_HEADER
;
1549 c
->http_error
= 404;
1551 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "HTTP/1.0 404 Not Found\r\n");
1552 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "Content-type: %s\r\n", "text/html");
1553 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "\r\n");
1554 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HTML>\n");
1555 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1556 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "<BODY>%s</BODY>\n", msg
);
1557 q
+= snprintf(q
, q
- (char *) c
->buffer
+ c
->buffer_size
, "</HTML>\n");
1559 /* prepare output buffer */
1560 c
->buffer_ptr
= c
->buffer
;
1562 c
->state
= HTTPSTATE_SEND_HEADER
;
1566 c
->http_error
= 200; /* horrible : we use this value to avoid
1567 going to the send data state */
1568 c
->state
= HTTPSTATE_SEND_HEADER
;
1572 static void fmt_bytecount(ByteIOContext
*pb
, int64_t count
)
1574 static const char *suffix
= " kMGTP";
1577 for (s
= suffix
; count
>= 100000 && s
[1]; count
/= 1000, s
++) {
1580 url_fprintf(pb
, "%"PRId64
"%c", count
, *s
);
1583 static void compute_stats(HTTPContext
*c
)
1590 ByteIOContext pb1
, *pb
= &pb1
;
1592 if (url_open_dyn_buf(pb
) < 0) {
1593 /* XXX: return an error ? */
1594 c
->buffer_ptr
= c
->buffer
;
1595 c
->buffer_end
= c
->buffer
;
1599 url_fprintf(pb
, "HTTP/1.0 200 OK\r\n");
1600 url_fprintf(pb
, "Content-type: %s\r\n", "text/html");
1601 url_fprintf(pb
, "Pragma: no-cache\r\n");
1602 url_fprintf(pb
, "\r\n");
1604 url_fprintf(pb
, "<HEAD><TITLE>FFServer Status</TITLE>\n");
1605 if (c
->stream
->feed_filename
) {
1606 url_fprintf(pb
, "<link rel=\"shortcut icon\" href=\"%s\">\n", c
->stream
->feed_filename
);
1608 url_fprintf(pb
, "</HEAD>\n<BODY>");
1609 url_fprintf(pb
, "<H1>FFServer Status</H1>\n");
1611 url_fprintf(pb
, "<H2>Available Streams</H2>\n");
1612 url_fprintf(pb
, "<TABLE cellspacing=0 cellpadding=4>\n");
1613 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");
1614 stream
= first_stream
;
1615 while (stream
!= NULL
) {
1616 char sfilename
[1024];
1619 if (stream
->feed
!= stream
) {
1620 pstrcpy(sfilename
, sizeof(sfilename
) - 10, stream
->filename
);
1621 eosf
= sfilename
+ strlen(sfilename
);
1622 if (eosf
- sfilename
>= 4) {
1623 if (strcmp(eosf
- 4, ".asf") == 0) {
1624 strcpy(eosf
- 4, ".asx");
1625 } else if (strcmp(eosf
- 3, ".rm") == 0) {
1626 strcpy(eosf
- 3, ".ram");
1627 } else if (stream
->fmt
== &rtp_mux
) {
1628 /* generate a sample RTSP director if
1629 unicast. Generate an SDP redirector if
1631 eosf
= strrchr(sfilename
, '.');
1633 eosf
= sfilename
+ strlen(sfilename
);
1634 if (stream
->is_multicast
)
1635 strcpy(eosf
, ".sdp");
1637 strcpy(eosf
, ".rtsp");
1641 url_fprintf(pb
, "<TR><TD><A HREF=\"/%s\">%s</A> ",
1642 sfilename
, stream
->filename
);
1643 url_fprintf(pb
, "<td align=right> %d <td align=right> ",
1644 stream
->conns_served
);
1645 fmt_bytecount(pb
, stream
->bytes_served
);
1646 switch(stream
->stream_type
) {
1647 case STREAM_TYPE_LIVE
:
1649 int audio_bit_rate
= 0;
1650 int video_bit_rate
= 0;
1651 const char *audio_codec_name
= "";
1652 const char *video_codec_name
= "";
1653 const char *audio_codec_name_extra
= "";
1654 const char *video_codec_name_extra
= "";
1656 for(i
=0;i
<stream
->nb_streams
;i
++) {
1657 AVStream
*st
= stream
->streams
[i
];
1658 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1659 switch(st
->codec
->codec_type
) {
1660 case CODEC_TYPE_AUDIO
:
1661 audio_bit_rate
+= st
->codec
->bit_rate
;
1663 if (*audio_codec_name
)
1664 audio_codec_name_extra
= "...";
1665 audio_codec_name
= codec
->name
;
1668 case CODEC_TYPE_VIDEO
:
1669 video_bit_rate
+= st
->codec
->bit_rate
;
1671 if (*video_codec_name
)
1672 video_codec_name_extra
= "...";
1673 video_codec_name
= codec
->name
;
1676 case CODEC_TYPE_DATA
:
1677 video_bit_rate
+= st
->codec
->bit_rate
;
1683 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",
1686 video_bit_rate
/ 1000, video_codec_name
, video_codec_name_extra
,
1687 audio_bit_rate
/ 1000, audio_codec_name
, audio_codec_name_extra
);
1689 url_fprintf(pb
, "<TD>%s", stream
->feed
->filename
);
1691 url_fprintf(pb
, "<TD>%s", stream
->feed_filename
);
1693 url_fprintf(pb
, "\n");
1697 url_fprintf(pb
, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
1701 stream
= stream
->next
;
1703 url_fprintf(pb
, "</TABLE>\n");
1705 stream
= first_stream
;
1706 while (stream
!= NULL
) {
1707 if (stream
->feed
== stream
) {
1708 url_fprintf(pb
, "<h2>Feed %s</h2>", stream
->filename
);
1710 url_fprintf(pb
, "Running as pid %d.\n", stream
->pid
);
1712 #if defined(linux) && !defined(CONFIG_NOCUTILS)
1717 /* This is somewhat linux specific I guess */
1718 snprintf(ps_cmd
, sizeof(ps_cmd
),
1719 "ps -o \"%%cpu,cputime\" --no-headers %d",
1722 pid_stat
= popen(ps_cmd
, "r");
1727 if (fscanf(pid_stat
, "%10s %64s", cpuperc
,
1729 url_fprintf(pb
, "Currently using %s%% of the cpu. Total time used %s.\n",
1737 url_fprintf(pb
, "<p>");
1739 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");
1741 for (i
= 0; i
< stream
->nb_streams
; i
++) {
1742 AVStream
*st
= stream
->streams
[i
];
1743 AVCodec
*codec
= avcodec_find_encoder(st
->codec
->codec_id
);
1744 const char *type
= "unknown";
1745 char parameters
[64];
1749 switch(st
->codec
->codec_type
) {
1750 case CODEC_TYPE_AUDIO
:
1753 case CODEC_TYPE_VIDEO
:
1755 snprintf(parameters
, sizeof(parameters
), "%dx%d, q=%d-%d, fps=%d", st
->codec
->width
, st
->codec
->height
,
1756 st
->codec
->qmin
, st
->codec
->qmax
, st
->codec
->time_base
.den
/ st
->codec
->time_base
.num
);
1761 url_fprintf(pb
, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
1762 i
, type
, st
->codec
->bit_rate
/1000, codec ? codec
->name
: "", parameters
);
1764 url_fprintf(pb
, "</table>\n");
1767 stream
= stream
->next
;
1773 AVCodecContext
*enc
;
1777 stream
= first_feed
;
1778 while (stream
!= NULL
) {
1779 url_fprintf(pb
, "<H1>Feed '%s'</H1>\n", stream
->filename
);
1780 url_fprintf(pb
, "<TABLE>\n");
1781 url_fprintf(pb
, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
1782 for(i
=0;i
<stream
->nb_streams
;i
++) {
1783 AVStream
*st
= stream
->streams
[i
];
1784 FeedData
*fdata
= st
->priv_data
;
1787 avcodec_string(buf
, sizeof(buf
), enc
);
1788 avg
= fdata
->avg_frame_size
* (float)enc
->rate
* 8.0;
1789 if (enc
->codec
->type
== CODEC_TYPE_AUDIO
&& enc
->frame_size
> 0)
1790 avg
/= enc
->frame_size
;
1791 url_fprintf(pb
, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n",
1792 buf
, enc
->frame_number
, fdata
->data_count
, avg
/ 1000.0);
1794 url_fprintf(pb
, "</TABLE>\n");
1795 stream
= stream
->next_feed
;
1800 /* connection status */
1801 url_fprintf(pb
, "<H2>Connection Status</H2>\n");
1803 url_fprintf(pb
, "Number of connections: %d / %d<BR>\n",
1804 nb_connections
, nb_max_connections
);
1806 url_fprintf(pb
, "Bandwidth in use: %dk / %dk<BR>\n",
1807 current_bandwidth
, max_bandwidth
);
1809 url_fprintf(pb
, "<TABLE>\n");
1810 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");
1811 c1
= first_http_ctx
;
1813 while (c1
!= NULL
) {
1819 for (j
= 0; j
< c1
->stream
->nb_streams
; j
++) {
1820 if (!c1
->stream
->feed
) {
1821 bitrate
+= c1
->stream
->streams
[j
]->codec
->bit_rate
;
1823 if (c1
->feed_streams
[j
] >= 0) {
1824 bitrate
+= c1
->stream
->feed
->streams
[c1
->feed_streams
[j
]]->codec
->bit_rate
;
1831 p
= inet_ntoa(c1
->from_addr
.sin_addr
);
1832 url_fprintf(pb
, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1834 c1
->stream ? c1
->stream
->filename
: "",
1835 c1
->state
== HTTPSTATE_RECEIVE_DATA ?
"(input)" : "",
1838 http_state
[c1
->state
]);
1839 fmt_bytecount(pb
, bitrate
);
1840 url_fprintf(pb
, "<td align=right>");
1841 fmt_bytecount(pb
, compute_datarate(&c1
->datarate
, c1
->data_count
) * 8);
1842 url_fprintf(pb
, "<td align=right>");
1843 fmt_bytecount(pb
, c1
->data_count
);
1844 url_fprintf(pb
, "\n");
1847 url_fprintf(pb
, "</TABLE>\n");
1852 url_fprintf(pb
, "<HR size=1 noshade>Generated at %s", p
);
1853 url_fprintf(pb
, "</BODY>\n</HTML>\n");
1855 len
= url_close_dyn_buf(pb
, &c
->pb_buffer
);
1856 c
->buffer_ptr
= c
->pb_buffer
;
1857 c
->buffer_end
= c
->pb_buffer
+ len
;
1860 /* check if the parser needs to be opened for stream i */
1861 static void open_parser(AVFormatContext
*s
, int i
)
1863 AVStream
*st
= s
->streams
[i
];
1866 if (!st
->codec
->codec
) {
1867 codec
= avcodec_find_decoder(st
->codec
->codec_id
);
1868 if (codec
&& (codec
->capabilities
& CODEC_CAP_PARSE_ONLY
)) {
1869 st
->codec
->parse_only
= 1;
1870 if (avcodec_open(st
->codec
, codec
) < 0) {
1871 st
->codec
->parse_only
= 0;
1877 static int open_input_stream(HTTPContext
*c
, const char *info
)
1880 char input_filename
[1024];
1885 /* find file name */
1886 if (c
->stream
->feed
) {
1887 strcpy(input_filename
, c
->stream
->feed
->feed_filename
);
1888 buf_size
= FFM_PACKET_SIZE
;
1889 /* compute position (absolute time) */
1890 if (find_info_tag(buf
, sizeof(buf
), "date", info
)) {
1891 stream_pos
= parse_date(buf
, 0);
1892 } else if (find_info_tag(buf
, sizeof(buf
), "buffer", info
)) {
1893 int prebuffer
= strtol(buf
, 0, 10);
1894 stream_pos
= av_gettime() - prebuffer
* (int64_t)1000000;
1896 stream_pos
= av_gettime() - c
->stream
->prebuffer
* (int64_t)1000;
1899 strcpy(input_filename
, c
->stream
->feed_filename
);
1901 /* compute position (relative time) */
1902 if (find_info_tag(buf
, sizeof(buf
), "date", info
)) {
1903 stream_pos
= parse_date(buf
, 1);
1908 if (input_filename
[0] == '\0')
1912 { time_t when
= stream_pos
/ 1000000;
1913 http_log("Stream pos = %lld, time=%s", stream_pos
, ctime(&when
));
1918 if (av_open_input_file(&s
, input_filename
, c
->stream
->ifmt
,
1919 buf_size
, c
->stream
->ap_in
) < 0) {
1920 http_log("%s not found", input_filename
);
1925 /* open each parser */
1926 for(i
=0;i
<s
->nb_streams
;i
++)
1929 /* choose stream as clock source (we favorize video stream if
1930 present) for packet sending */
1931 c
->pts_stream_index
= 0;
1932 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
1933 if (c
->pts_stream_index
== 0 &&
1934 c
->stream
->streams
[i
]->codec
->codec_type
== CODEC_TYPE_VIDEO
) {
1935 c
->pts_stream_index
= i
;
1940 if (c
->fmt_in
->iformat
->read_seek
) {
1941 c
->fmt_in
->iformat
->read_seek(c
->fmt_in
, 0, stream_pos
, 0);
1944 /* set the start time (needed for maxtime and RTP packet timing) */
1945 c
->start_time
= cur_time
;
1946 c
->first_pts
= AV_NOPTS_VALUE
;
1950 /* return the server clock (in us) */
1951 static int64_t get_server_clock(HTTPContext
*c
)
1953 /* compute current pts value from system time */
1954 return (int64_t)(cur_time
- c
->start_time
) * 1000LL;
1957 /* return the estimated time at which the current packet must be sent
1959 static int64_t get_packet_send_clock(HTTPContext
*c
)
1961 int bytes_left
, bytes_sent
, frame_bytes
;
1963 frame_bytes
= c
->cur_frame_bytes
;
1964 if (frame_bytes
<= 0) {
1967 bytes_left
= c
->buffer_end
- c
->buffer_ptr
;
1968 bytes_sent
= frame_bytes
- bytes_left
;
1969 return c
->cur_pts
+ (c
->cur_frame_duration
* bytes_sent
) / frame_bytes
;
1974 static int http_prepare_data(HTTPContext
*c
)
1977 AVFormatContext
*ctx
;
1979 av_freep(&c
->pb_buffer
);
1981 case HTTPSTATE_SEND_DATA_HEADER
:
1982 memset(&c
->fmt_ctx
, 0, sizeof(c
->fmt_ctx
));
1983 pstrcpy(c
->fmt_ctx
.author
, sizeof(c
->fmt_ctx
.author
),
1985 pstrcpy(c
->fmt_ctx
.comment
, sizeof(c
->fmt_ctx
.comment
),
1986 c
->stream
->comment
);
1987 pstrcpy(c
->fmt_ctx
.copyright
, sizeof(c
->fmt_ctx
.copyright
),
1988 c
->stream
->copyright
);
1989 pstrcpy(c
->fmt_ctx
.title
, sizeof(c
->fmt_ctx
.title
),
1992 /* open output stream by using specified codecs */
1993 c
->fmt_ctx
.oformat
= c
->stream
->fmt
;
1994 c
->fmt_ctx
.nb_streams
= c
->stream
->nb_streams
;
1995 for(i
=0;i
<c
->fmt_ctx
.nb_streams
;i
++) {
1998 st
= av_mallocz(sizeof(AVStream
));
1999 st
->codec
= avcodec_alloc_context();
2000 c
->fmt_ctx
.streams
[i
] = st
;
2001 /* if file or feed, then just take streams from FFStream struct */
2002 if (!c
->stream
->feed
||
2003 c
->stream
->feed
== c
->stream
)
2004 src
= c
->stream
->streams
[i
];
2006 src
= c
->stream
->feed
->streams
[c
->stream
->feed_streams
[i
]];
2010 st
->codec
->frame_number
= 0; /* XXX: should be done in
2011 AVStream, not in codec */
2012 /* I'm pretty sure that this is not correct...
2013 * However, without it, we crash
2015 st
->codec
->coded_frame
= &dummy_frame
;
2017 c
->got_key_frame
= 0;
2019 /* prepare header and save header data in a stream */
2020 if (url_open_dyn_buf(&c
->fmt_ctx
.pb
) < 0) {
2021 /* XXX: potential leak */
2024 c
->fmt_ctx
.pb
.is_streamed
= 1;
2026 av_set_parameters(&c
->fmt_ctx
, NULL
);
2027 av_write_header(&c
->fmt_ctx
);
2029 len
= url_close_dyn_buf(&c
->fmt_ctx
.pb
, &c
->pb_buffer
);
2030 c
->buffer_ptr
= c
->pb_buffer
;
2031 c
->buffer_end
= c
->pb_buffer
+ len
;
2033 c
->state
= HTTPSTATE_SEND_DATA
;
2034 c
->last_packet_sent
= 0;
2036 case HTTPSTATE_SEND_DATA
:
2037 /* find a new packet */
2041 /* read a packet from the input stream */
2042 if (c
->stream
->feed
) {
2043 ffm_set_write_index(c
->fmt_in
,
2044 c
->stream
->feed
->feed_write_index
,
2045 c
->stream
->feed
->feed_size
);
2048 if (c
->stream
->max_time
&&
2049 c
->stream
->max_time
+ c
->start_time
- cur_time
< 0) {
2050 /* We have timed out */
2051 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2054 if (av_read_frame(c
->fmt_in
, &pkt
) < 0) {
2055 if (c
->stream
->feed
&& c
->stream
->feed
->feed_opened
) {
2056 /* if coming from feed, it means we reached the end of the
2057 ffm file, so must wait for more data */
2058 c
->state
= HTTPSTATE_WAIT_FEED
;
2059 return 1; /* state changed */
2061 if (c
->stream
->loop
) {
2062 av_close_input_file(c
->fmt_in
);
2064 if (open_input_stream(c
, "") < 0)
2069 /* must send trailer now because eof or error */
2070 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2074 /* update first pts if needed */
2075 if (c
->first_pts
== AV_NOPTS_VALUE
) {
2076 c
->first_pts
= av_rescale_q(pkt
.dts
, c
->fmt_in
->streams
[pkt
.stream_index
]->time_base
, AV_TIME_BASE_Q
);
2077 c
->start_time
= cur_time
;
2079 /* send it to the appropriate stream */
2080 if (c
->stream
->feed
) {
2081 /* if coming from a feed, select the right stream */
2082 if (c
->switch_pending
) {
2083 c
->switch_pending
= 0;
2084 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2085 if (c
->switch_feed_streams
[i
] == pkt
.stream_index
) {
2086 if (pkt
.flags
& PKT_FLAG_KEY
) {
2087 do_switch_stream(c
, i
);
2090 if (c
->switch_feed_streams
[i
] >= 0) {
2091 c
->switch_pending
= 1;
2095 for(i
=0;i
<c
->stream
->nb_streams
;i
++) {
2096 if (c
->feed_streams
[i
] == pkt
.stream_index
) {
2097 pkt
.stream_index
= i
;
2098 if (pkt
.flags
& PKT_FLAG_KEY
) {
2099 c
->got_key_frame
|= 1 << i
;
2101 /* See if we have all the key frames, then
2102 * we start to send. This logic is not quite
2103 * right, but it works for the case of a
2104 * single video stream with one or more
2105 * audio streams (for which every frame is
2106 * typically a key frame).
2108 if (!c
->stream
->send_on_key
||
2109 ((c
->got_key_frame
+ 1) >> c
->stream
->nb_streams
)) {
2115 AVCodecContext
*codec
;
2118 /* specific handling for RTP: we use several
2119 output stream (one for each RTP
2120 connection). XXX: need more abstract handling */
2121 if (c
->is_packetized
) {
2123 /* compute send time and duration */
2124 st
= c
->fmt_in
->streams
[pkt
.stream_index
];
2125 c
->cur_pts
= av_rescale_q(pkt
.dts
, st
->time_base
, AV_TIME_BASE_Q
);
2126 if (st
->start_time
!= AV_NOPTS_VALUE
)
2127 c
->cur_pts
-= av_rescale_q(st
->start_time
, st
->time_base
, AV_TIME_BASE_Q
);
2128 c
->cur_frame_duration
= av_rescale_q(pkt
.duration
, st
->time_base
, AV_TIME_BASE_Q
);
2130 printf("index=%d pts=%0.3f duration=%0.6f\n",
2132 (double)c
->cur_pts
/
2134 (double)c
->cur_frame_duration
/
2137 /* find RTP context */
2138 c
->packet_stream_index
= pkt
.stream_index
;
2139 ctx
= c
->rtp_ctx
[c
->packet_stream_index
];
2141 av_free_packet(&pkt
);
2144 codec
= ctx
->streams
[0]->codec
;
2145 /* only one stream per RTP connection */
2146 pkt
.stream_index
= 0;
2150 codec
= ctx
->streams
[pkt
.stream_index
]->codec
;
2153 codec
->coded_frame
->key_frame
= ((pkt
.flags
& PKT_FLAG_KEY
) != 0);
2154 if (c
->is_packetized
) {
2155 int max_packet_size
;
2156 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
)
2157 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
2159 max_packet_size
= url_get_max_packet_size(c
->rtp_handles
[c
->packet_stream_index
]);
2160 ret
= url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
);
2162 ret
= url_open_dyn_buf(&ctx
->pb
);
2165 /* XXX: potential leak */
2168 if (av_write_frame(ctx
, &pkt
)) {
2169 c
->state
= HTTPSTATE_SEND_DATA_TRAILER
;
2172 len
= url_close_dyn_buf(&ctx
->pb
, &c
->pb_buffer
);
2173 c
->cur_frame_bytes
= len
;
2174 c
->buffer_ptr
= c
->pb_buffer
;
2175 c
->buffer_end
= c
->pb_buffer
+ len
;
2177 codec
->frame_number
++;
2181 av_free_packet(&pkt
);
2187 case HTTPSTATE_SEND_DATA_TRAILER
:
2188 /* last packet test ? */
2189 if (c
->last_packet_sent
|| c
->is_packetized
)
2192 /* prepare header */
2193 if (url_open_dyn_buf(&ctx
->pb
) < 0) {
2194 /* XXX: potential leak */
2197 av_write_trailer(ctx
);
2198 len
= url_close_dyn_buf(&ctx
->pb
, &c
->pb_buffer
);
2199 c
->buffer_ptr
= c
->pb_buffer
;
2200 c
->buffer_end
= c
->pb_buffer
+ len
;
2202 c
->last_packet_sent
= 1;
2209 #define SHORT_TERM_BANDWIDTH 8000000
2211 /* should convert the format at the same time */
2212 /* send data starting at c->buffer_ptr to the output connection
2213 (either UDP or TCP connection) */
2214 static int http_send_data(HTTPContext
*c
)
2219 if (c
->buffer_ptr
>= c
->buffer_end
) {
2220 ret
= http_prepare_data(c
);
2223 else if (ret
!= 0) {
2224 /* state change requested */
2228 if (c
->is_packetized
) {
2229 /* RTP data output */
2230 len
= c
->buffer_end
- c
->buffer_ptr
;
2232 /* fail safe - should never happen */
2234 c
->buffer_ptr
= c
->buffer_end
;
2237 len
= (c
->buffer_ptr
[0] << 24) |
2238 (c
->buffer_ptr
[1] << 16) |
2239 (c
->buffer_ptr
[2] << 8) |
2241 if (len
> (c
->buffer_end
- c
->buffer_ptr
))
2243 if ((get_packet_send_clock(c
) - get_server_clock(c
)) > 0) {
2244 /* nothing to send yet: we can wait */
2248 c
->data_count
+= len
;
2249 update_datarate(&c
->datarate
, c
->data_count
);
2251 c
->stream
->bytes_served
+= len
;
2253 if (c
->rtp_protocol
== RTSP_PROTOCOL_RTP_TCP
) {
2254 /* RTP packets are sent inside the RTSP TCP connection */
2255 ByteIOContext pb1
, *pb
= &pb1
;
2256 int interleaved_index
, size
;
2258 HTTPContext
*rtsp_c
;
2261 /* if no RTSP connection left, error */
2264 /* if already sending something, then wait. */
2265 if (rtsp_c
->state
!= RTSPSTATE_WAIT_REQUEST
) {
2268 if (url_open_dyn_buf(pb
) < 0)
2270 interleaved_index
= c
->packet_stream_index
* 2;
2271 /* RTCP packets are sent at odd indexes */
2272 if (c
->buffer_ptr
[1] == 200)
2273 interleaved_index
++;
2274 /* write RTSP TCP header */
2276 header
[1] = interleaved_index
;
2277 header
[2] = len
>> 8;
2279 put_buffer(pb
, header
, 4);
2280 /* write RTP packet data */
2282 put_buffer(pb
, c
->buffer_ptr
, len
);
2283 size
= url_close_dyn_buf(pb
, &c
->packet_buffer
);
2284 /* prepare asynchronous TCP sending */
2285 rtsp_c
->packet_buffer_ptr
= c
->packet_buffer
;
2286 rtsp_c
->packet_buffer_end
= c
->packet_buffer
+ size
;
2287 c
->buffer_ptr
+= len
;
2289 /* send everything we can NOW */
2290 len
= write(rtsp_c
->fd
, rtsp_c
->packet_buffer_ptr
,
2291 rtsp_c
->packet_buffer_end
- rtsp_c
->packet_buffer_ptr
);
2293 rtsp_c
->packet_buffer_ptr
+= len
;
2295 if (rtsp_c
->packet_buffer_ptr
< rtsp_c
->packet_buffer_end
) {
2296 /* if we could not send all the data, we will
2297 send it later, so a new state is needed to
2298 "lock" the RTSP TCP connection */
2299 rtsp_c
->state
= RTSPSTATE_SEND_PACKET
;
2302 /* all data has been sent */
2303 av_freep(&c
->packet_buffer
);
2306 /* send RTP packet directly in UDP */
2308 url_write(c
->rtp_handles
[c
->packet_stream_index
],
2309 c
->buffer_ptr
, len
);
2310 c
->buffer_ptr
+= len
;
2311 /* here we continue as we can send several packets per 10 ms slot */
2314 /* TCP data output */
2315 len
= write(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
);
2317 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
2318 /* error : close connection */
2324 c
->buffer_ptr
+= len
;
2326 c
->data_count
+= len
;
2327 update_datarate(&c
->datarate
, c
->data_count
);
2329 c
->stream
->bytes_served
+= len
;
2337 static int http_start_receive_data(HTTPContext
*c
)
2341 if (c
->stream
->feed_opened
)
2344 /* Don't permit writing to this one */
2345 if (c
->stream
->readonly
)
2349 fd
= open(c
->stream
->feed_filename
, O_RDWR
);
2354 c
->stream
->feed_write_index
= ffm_read_write_index(fd
);
2355 c
->stream
->feed_size
= lseek(fd
, 0, SEEK_END
);
2356 lseek(fd
, 0, SEEK_SET
);
2358 /* init buffer input */
2359 c
->buffer_ptr
= c
->buffer
;
2360 c
->buffer_end
= c
->buffer
+ FFM_PACKET_SIZE
;
2361 c
->stream
->feed_opened
= 1;
2365 static int http_receive_data(HTTPContext
*c
)
2369 if (c
->buffer_end
> c
->buffer_ptr
) {
2372 len
= read(c
->fd
, c
->buffer_ptr
, c
->buffer_end
- c
->buffer_ptr
);
2374 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
2375 /* error : close connection */
2378 } else if (len
== 0) {
2379 /* end of connection : close it */
2382 c
->buffer_ptr
+= len
;
2383 c
->data_count
+= len
;
2384 update_datarate(&c
->datarate
, c
->data_count
);
2388 if (c
->buffer_ptr
- c
->buffer
>= 2 && c
->data_count
> FFM_PACKET_SIZE
) {
2389 if (c
->buffer
[0] != 'f' ||
2390 c
->buffer
[1] != 'm') {
2391 http_log("Feed stream has become desynchronized -- disconnecting\n");
2396 if (c
->buffer_ptr
>= c
->buffer_end
) {
2397 FFStream
*feed
= c
->stream
;
2398 /* a packet has been received : write it in the store, except
2400 if (c
->data_count
> FFM_PACKET_SIZE
) {
2402 // printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
2403 /* XXX: use llseek or url_seek */
2404 lseek(c
->feed_fd
, feed
->feed_write_index
, SEEK_SET
);
2405 write(c
->feed_fd
, c
->buffer
, FFM_PACKET_SIZE
);
2407 feed
->feed_write_index
+= FFM_PACKET_SIZE
;
2408 /* update file size */
2409 if (feed
->feed_write_index
> c
->stream
->feed_size
)
2410 feed
->feed_size
= feed
->feed_write_index
;
2412 /* handle wrap around if max file size reached */
2413 if (c
->stream
->feed_max_size
&& feed
->feed_write_index
>= c
->stream
->feed_max_size
)
2414 feed
->feed_write_index
= FFM_PACKET_SIZE
;
2417 ffm_write_write_index(c
->feed_fd
, feed
->feed_write_index
);
2419 /* wake up any waiting connections */
2420 for(c1
= first_http_ctx
; c1
!= NULL
; c1
= c1
->next
) {
2421 if (c1
->state
== HTTPSTATE_WAIT_FEED
&&
2422 c1
->stream
->feed
== c
->stream
->feed
) {
2423 c1
->state
= HTTPSTATE_SEND_DATA
;
2427 /* We have a header in our hands that contains useful data */
2429 AVInputFormat
*fmt_in
;
2430 ByteIOContext
*pb
= &s
.pb
;
2433 memset(&s
, 0, sizeof(s
));
2435 url_open_buf(pb
, c
->buffer
, c
->buffer_end
- c
->buffer
, URL_RDONLY
);
2436 pb
->buf_end
= c
->buffer_end
; /* ?? */
2437 pb
->is_streamed
= 1;
2439 /* use feed output format name to find corresponding input format */
2440 fmt_in
= av_find_input_format(feed
->fmt
->name
);
2444 if (fmt_in
->priv_data_size
> 0) {
2445 s
.priv_data
= av_mallocz(fmt_in
->priv_data_size
);
2451 if (fmt_in
->read_header(&s
, 0) < 0) {
2452 av_freep(&s
.priv_data
);
2456 /* Now we have the actual streams */
2457 if (s
.nb_streams
!= feed
->nb_streams
) {
2458 av_freep(&s
.priv_data
);
2461 for (i
= 0; i
< s
.nb_streams
; i
++) {
2462 memcpy(feed
->streams
[i
]->codec
,
2463 s
.streams
[i
]->codec
, sizeof(AVCodecContext
));
2465 av_freep(&s
.priv_data
);
2467 c
->buffer_ptr
= c
->buffer
;
2472 c
->stream
->feed_opened
= 0;
2477 /********************************************************************/
2480 static void rtsp_reply_header(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2487 switch(error_number
) {
2488 #define DEF(n, c, s) case c: str = s; break;
2489 #include "rtspcodes.h"
2492 str
= "Unknown Error";
2496 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", error_number
, str
);
2497 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2499 /* output GMT time */
2503 p
= buf2
+ strlen(p
) - 1;
2506 url_fprintf(c
->pb
, "Date: %s GMT\r\n", buf2
);
2509 static void rtsp_reply_error(HTTPContext
*c
, enum RTSPStatusCode error_number
)
2511 rtsp_reply_header(c
, error_number
);
2512 url_fprintf(c
->pb
, "\r\n");
2515 static int rtsp_parse_request(HTTPContext
*c
)
2517 const char *p
, *p1
, *p2
;
2524 RTSPHeader header1
, *header
= &header1
;
2526 c
->buffer_ptr
[0] = '\0';
2529 get_word(cmd
, sizeof(cmd
), &p
);
2530 get_word(url
, sizeof(url
), &p
);
2531 get_word(protocol
, sizeof(protocol
), &p
);
2533 pstrcpy(c
->method
, sizeof(c
->method
), cmd
);
2534 pstrcpy(c
->url
, sizeof(c
->url
), url
);
2535 pstrcpy(c
->protocol
, sizeof(c
->protocol
), protocol
);
2538 if (url_open_dyn_buf(c
->pb
) < 0) {
2539 /* XXX: cannot do more */
2540 c
->pb
= NULL
; /* safety */
2544 /* check version name */
2545 if (strcmp(protocol
, "RTSP/1.0") != 0) {
2546 rtsp_reply_error(c
, RTSP_STATUS_VERSION
);
2550 /* parse each header line */
2551 memset(header
, 0, sizeof(RTSPHeader
));
2552 /* skip to next line */
2553 while (*p
!= '\n' && *p
!= '\0')
2557 while (*p
!= '\0') {
2558 p1
= strchr(p
, '\n');
2562 if (p2
> p
&& p2
[-1] == '\r')
2564 /* skip empty line */
2568 if (len
> sizeof(line
) - 1)
2569 len
= sizeof(line
) - 1;
2570 memcpy(line
, p
, len
);
2572 rtsp_parse_line(header
, line
);
2576 /* handle sequence number */
2577 c
->seq
= header
->seq
;
2579 if (!strcmp(cmd
, "DESCRIBE")) {
2580 rtsp_cmd_describe(c
, url
);
2581 } else if (!strcmp(cmd
, "OPTIONS")) {
2582 rtsp_cmd_options(c
, url
);
2583 } else if (!strcmp(cmd
, "SETUP")) {
2584 rtsp_cmd_setup(c
, url
, header
);
2585 } else if (!strcmp(cmd
, "PLAY")) {
2586 rtsp_cmd_play(c
, url
, header
);
2587 } else if (!strcmp(cmd
, "PAUSE")) {
2588 rtsp_cmd_pause(c
, url
, header
);
2589 } else if (!strcmp(cmd
, "TEARDOWN")) {
2590 rtsp_cmd_teardown(c
, url
, header
);
2592 rtsp_reply_error(c
, RTSP_STATUS_METHOD
);
2595 len
= url_close_dyn_buf(c
->pb
, &c
->pb_buffer
);
2596 c
->pb
= NULL
; /* safety */
2598 /* XXX: cannot do more */
2601 c
->buffer_ptr
= c
->pb_buffer
;
2602 c
->buffer_end
= c
->pb_buffer
+ len
;
2603 c
->state
= RTSPSTATE_SEND_REPLY
;
2607 /* XXX: move that to rtsp.c, but would need to replace FFStream by
2609 static int prepare_sdp_description(FFStream
*stream
, uint8_t **pbuffer
,
2610 struct in_addr my_ip
)
2612 ByteIOContext pb1
, *pb
= &pb1
;
2613 int i
, payload_type
, port
, private_payload_type
, j
;
2614 const char *ipstr
, *title
, *mediatype
;
2617 if (url_open_dyn_buf(pb
) < 0)
2620 /* general media info */
2622 url_fprintf(pb
, "v=0\n");
2623 ipstr
= inet_ntoa(my_ip
);
2624 url_fprintf(pb
, "o=- 0 0 IN IP4 %s\n", ipstr
);
2625 title
= stream
->title
;
2626 if (title
[0] == '\0')
2628 url_fprintf(pb
, "s=%s\n", title
);
2629 if (stream
->comment
[0] != '\0')
2630 url_fprintf(pb
, "i=%s\n", stream
->comment
);
2631 if (stream
->is_multicast
) {
2632 url_fprintf(pb
, "c=IN IP4 %s\n", inet_ntoa(stream
->multicast_ip
));
2634 /* for each stream, we output the necessary info */
2635 private_payload_type
= RTP_PT_PRIVATE
;
2636 for(i
= 0; i
< stream
->nb_streams
; i
++) {
2637 st
= stream
->streams
[i
];
2638 if (st
->codec
->codec_id
== CODEC_ID_MPEG2TS
) {
2639 mediatype
= "video";
2641 switch(st
->codec
->codec_type
) {
2642 case CODEC_TYPE_AUDIO
:
2643 mediatype
= "audio";
2645 case CODEC_TYPE_VIDEO
:
2646 mediatype
= "video";
2649 mediatype
= "application";
2653 /* NOTE: the port indication is not correct in case of
2654 unicast. It is not an issue because RTSP gives it */
2655 payload_type
= rtp_get_payload_type(st
->codec
);
2656 if (payload_type
< 0)
2657 payload_type
= private_payload_type
++;
2658 if (stream
->is_multicast
) {
2659 port
= stream
->multicast_port
+ 2 * i
;
2663 url_fprintf(pb
, "m=%s %d RTP/AVP %d\n",
2664 mediatype
, port
, payload_type
);
2665 if (payload_type
>= RTP_PT_PRIVATE
) {
2666 /* for private payload type, we need to give more info */
2667 switch(st
->codec
->codec_id
) {
2668 case CODEC_ID_MPEG4
:
2671 url_fprintf(pb
, "a=rtpmap:%d MP4V-ES/%d\n",
2672 payload_type
, 90000);
2673 /* we must also add the mpeg4 header */
2674 data
= st
->codec
->extradata
;
2676 url_fprintf(pb
, "a=fmtp:%d config=", payload_type
);
2677 for(j
=0;j
<st
->codec
->extradata_size
;j
++) {
2678 url_fprintf(pb
, "%02x", data
[j
]);
2680 url_fprintf(pb
, "\n");
2685 /* XXX: add other codecs ? */
2689 url_fprintf(pb
, "a=control:streamid=%d\n", i
);
2691 return url_close_dyn_buf(pb
, pbuffer
);
2693 url_close_dyn_buf(pb
, pbuffer
);
2698 static void rtsp_cmd_options(HTTPContext
*c
, const char *url
)
2700 // rtsp_reply_header(c, RTSP_STATUS_OK);
2701 url_fprintf(c
->pb
, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK
, "OK");
2702 url_fprintf(c
->pb
, "CSeq: %d\r\n", c
->seq
);
2703 url_fprintf(c
->pb
, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2704 url_fprintf(c
->pb
, "\r\n");
2707 static void rtsp_cmd_describe(HTTPContext
*c
, const char *url
)
2713 int content_length
, len
;
2714 struct sockaddr_in my_addr
;
2716 /* find which url is asked */
2717 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2722 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2723 if (!stream
->is_feed
&& stream
->fmt
== &rtp_mux
&&
2724 !strcmp(path
, stream
->filename
)) {
2728 /* no stream found */
2729 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2733 /* prepare the media description in sdp format */
2735 /* get the host IP */
2736 len
= sizeof(my_addr
);
2737 getsockname(c
->fd
, (struct sockaddr
*)&my_addr
, &len
);
2738 content_length
= prepare_sdp_description(stream
, &content
, my_addr
.sin_addr
);
2739 if (content_length
< 0) {
2740 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2743 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2744 url_fprintf(c
->pb
, "Content-Type: application/sdp\r\n");
2745 url_fprintf(c
->pb
, "Content-Length: %d\r\n", content_length
);
2746 url_fprintf(c
->pb
, "\r\n");
2747 put_buffer(c
->pb
, content
, content_length
);
2750 static HTTPContext
*find_rtp_session(const char *session_id
)
2754 if (session_id
[0] == '\0')
2757 for(c
= first_http_ctx
; c
!= NULL
; c
= c
->next
) {
2758 if (!strcmp(c
->session_id
, session_id
))
2764 static RTSPTransportField
*find_transport(RTSPHeader
*h
, enum RTSPProtocol protocol
)
2766 RTSPTransportField
*th
;
2769 for(i
=0;i
<h
->nb_transports
;i
++) {
2770 th
= &h
->transports
[i
];
2771 if (th
->protocol
== protocol
)
2777 static void rtsp_cmd_setup(HTTPContext
*c
, const char *url
,
2781 int stream_index
, port
;
2786 RTSPTransportField
*th
;
2787 struct sockaddr_in dest_addr
;
2788 RTSPActionServerSetup setup
;
2790 /* find which url is asked */
2791 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2796 /* now check each stream */
2797 for(stream
= first_stream
; stream
!= NULL
; stream
= stream
->next
) {
2798 if (!stream
->is_feed
&& stream
->fmt
== &rtp_mux
) {
2799 /* accept aggregate filenames only if single stream */
2800 if (!strcmp(path
, stream
->filename
)) {
2801 if (stream
->nb_streams
!= 1) {
2802 rtsp_reply_error(c
, RTSP_STATUS_AGGREGATE
);
2809 for(stream_index
= 0; stream_index
< stream
->nb_streams
;
2811 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2812 stream
->filename
, stream_index
);
2813 if (!strcmp(path
, buf
))
2818 /* no stream found */
2819 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
); /* XXX: right error ? */
2823 /* generate session id if needed */
2824 if (h
->session_id
[0] == '\0') {
2825 snprintf(h
->session_id
, sizeof(h
->session_id
),
2826 "%08x%08x", (int)random(), (int)random());
2829 /* find rtp session, and create it if none found */
2830 rtp_c
= find_rtp_session(h
->session_id
);
2832 /* always prefer UDP */
2833 th
= find_transport(h
, RTSP_PROTOCOL_RTP_UDP
);
2835 th
= find_transport(h
, RTSP_PROTOCOL_RTP_TCP
);
2837 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2842 rtp_c
= rtp_new_connection(&c
->from_addr
, stream
, h
->session_id
,
2845 rtsp_reply_error(c
, RTSP_STATUS_BANDWIDTH
);
2849 /* open input stream */
2850 if (open_input_stream(rtp_c
, "") < 0) {
2851 rtsp_reply_error(c
, RTSP_STATUS_INTERNAL
);
2856 /* test if stream is OK (test needed because several SETUP needs
2857 to be done for a given file) */
2858 if (rtp_c
->stream
!= stream
) {
2859 rtsp_reply_error(c
, RTSP_STATUS_SERVICE
);
2863 /* test if stream is already set up */
2864 if (rtp_c
->rtp_ctx
[stream_index
]) {
2865 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2869 /* check transport */
2870 th
= find_transport(h
, rtp_c
->rtp_protocol
);
2871 if (!th
|| (th
->protocol
== RTSP_PROTOCOL_RTP_UDP
&&
2872 th
->client_port_min
<= 0)) {
2873 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2877 /* setup default options */
2878 setup
.transport_option
[0] = '\0';
2879 dest_addr
= rtp_c
->from_addr
;
2880 dest_addr
.sin_port
= htons(th
->client_port_min
);
2882 /* add transport option if needed */
2883 if (ff_rtsp_callback
) {
2884 setup
.ipaddr
= ntohl(dest_addr
.sin_addr
.s_addr
);
2885 if (ff_rtsp_callback(RTSP_ACTION_SERVER_SETUP
, rtp_c
->session_id
,
2886 (char *)&setup
, sizeof(setup
),
2887 stream
->rtsp_option
) < 0) {
2888 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2891 dest_addr
.sin_addr
.s_addr
= htonl(setup
.ipaddr
);
2895 if (rtp_new_av_stream(rtp_c
, stream_index
, &dest_addr
, c
) < 0) {
2896 rtsp_reply_error(c
, RTSP_STATUS_TRANSPORT
);
2900 /* now everything is OK, so we can send the connection parameters */
2901 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2903 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2905 switch(rtp_c
->rtp_protocol
) {
2906 case RTSP_PROTOCOL_RTP_UDP
:
2907 port
= rtp_get_local_port(rtp_c
->rtp_handles
[stream_index
]);
2908 url_fprintf(c
->pb
, "Transport: RTP/AVP/UDP;unicast;"
2909 "client_port=%d-%d;server_port=%d-%d",
2910 th
->client_port_min
, th
->client_port_min
+ 1,
2913 case RTSP_PROTOCOL_RTP_TCP
:
2914 url_fprintf(c
->pb
, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2915 stream_index
* 2, stream_index
* 2 + 1);
2920 if (setup
.transport_option
[0] != '\0') {
2921 url_fprintf(c
->pb
, ";%s", setup
.transport_option
);
2923 url_fprintf(c
->pb
, "\r\n");
2926 url_fprintf(c
->pb
, "\r\n");
2930 /* find an rtp connection by using the session ID. Check consistency
2932 static HTTPContext
*find_rtp_session_with_url(const char *url
,
2933 const char *session_id
)
2941 rtp_c
= find_rtp_session(session_id
);
2945 /* find which url is asked */
2946 url_split(NULL
, 0, NULL
, 0, NULL
, 0, NULL
, path1
, sizeof(path1
), url
);
2950 if(!strcmp(path
, rtp_c
->stream
->filename
)) return rtp_c
;
2951 for(s
=0; s
<rtp_c
->stream
->nb_streams
; ++s
) {
2952 snprintf(buf
, sizeof(buf
), "%s/streamid=%d",
2953 rtp_c
->stream
->filename
, s
);
2954 if(!strncmp(path
, buf
, sizeof(buf
))) {
2955 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2962 static void rtsp_cmd_play(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
2966 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
2968 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
2972 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
2973 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
&&
2974 rtp_c
->state
!= HTTPSTATE_READY
) {
2975 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
2980 /* XXX: seek in stream */
2981 if (h
->range_start
!= AV_NOPTS_VALUE
) {
2982 printf("range_start=%0.3f\n", (double)h
->range_start
/ AV_TIME_BASE
);
2983 av_seek_frame(rtp_c
->fmt_in
, -1, h
->range_start
);
2987 rtp_c
->state
= HTTPSTATE_SEND_DATA
;
2989 /* now everything is OK, so we can send the connection parameters */
2990 rtsp_reply_header(c
, RTSP_STATUS_OK
);
2992 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
2993 url_fprintf(c
->pb
, "\r\n");
2996 static void rtsp_cmd_pause(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
3000 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
3002 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3006 if (rtp_c
->state
!= HTTPSTATE_SEND_DATA
&&
3007 rtp_c
->state
!= HTTPSTATE_WAIT_FEED
) {
3008 rtsp_reply_error(c
, RTSP_STATUS_STATE
);
3012 rtp_c
->state
= HTTPSTATE_READY
;
3013 rtp_c
->first_pts
= AV_NOPTS_VALUE
;
3014 /* now everything is OK, so we can send the connection parameters */
3015 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3017 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
3018 url_fprintf(c
->pb
, "\r\n");
3021 static void rtsp_cmd_teardown(HTTPContext
*c
, const char *url
, RTSPHeader
*h
)
3025 rtp_c
= find_rtp_session_with_url(url
, h
->session_id
);
3027 rtsp_reply_error(c
, RTSP_STATUS_SESSION
);
3031 /* abort the session */
3032 close_connection(rtp_c
);
3034 if (ff_rtsp_callback
) {
3035 ff_rtsp_callback(RTSP_ACTION_SERVER_TEARDOWN
, rtp_c
->session_id
,
3037 rtp_c
->stream
->rtsp_option
);
3040 /* now everything is OK, so we can send the connection parameters */
3041 rtsp_reply_header(c
, RTSP_STATUS_OK
);
3043 url_fprintf(c
->pb
, "Session: %s\r\n", rtp_c
->session_id
);
3044 url_fprintf(c
->pb
, "\r\n");
3048 /********************************************************************/
3051 static HTTPContext
*rtp_new_connection(struct sockaddr_in
*from_addr
,
3052 FFStream
*stream
, const char *session_id
,
3053 enum RTSPProtocol rtp_protocol
)
3055 HTTPContext
*c
= NULL
;
3056 const char *proto_str
;
3058 /* XXX: should output a warning page when coming
3059 close to the connection limit */
3060 if (nb_connections
>= nb_max_connections
)
3063 /* add a new connection */
3064 c
= av_mallocz(sizeof(HTTPContext
));
3069 c
->poll_entry
= NULL
;
3070 c
->from_addr
= *from_addr
;
3071 c
->buffer_size
= IOBUFFER_INIT_SIZE
;
3072 c
->buffer
= av_malloc(c
->buffer_size
);
3077 pstrcpy(c
->session_id
, sizeof(c
->session_id
), session_id
);
3078 c
->state
= HTTPSTATE_READY
;
3079 c
->is_packetized
= 1;
3080 c
->rtp_protocol
= rtp_protocol
;
3082 /* protocol is shown in statistics */
3083 switch(c
->rtp_protocol
) {
3084 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3085 proto_str
= "MCAST";
3087 case RTSP_PROTOCOL_RTP_UDP
:
3090 case RTSP_PROTOCOL_RTP_TCP
:
3097 pstrcpy(c
->protocol
, sizeof(c
->protocol
), "RTP/");
3098 pstrcat(c
->protocol
, sizeof(c
->protocol
), proto_str
);
3100 current_bandwidth
+= stream
->bandwidth
;
3102 c
->next
= first_http_ctx
;
3114 /* add a new RTP stream in an RTP connection (used in RTSP SETUP
3115 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
3117 static int rtp_new_av_stream(HTTPContext
*c
,
3118 int stream_index
, struct sockaddr_in
*dest_addr
,
3119 HTTPContext
*rtsp_c
)
3121 AVFormatContext
*ctx
;
3127 int max_packet_size
;
3129 /* now we can open the relevant output stream */
3130 ctx
= av_alloc_format_context();
3133 ctx
->oformat
= &rtp_mux
;
3135 st
= av_mallocz(sizeof(AVStream
));
3138 st
->codec
= avcodec_alloc_context();
3139 ctx
->nb_streams
= 1;
3140 ctx
->streams
[0] = st
;
3142 if (!c
->stream
->feed
||
3143 c
->stream
->feed
== c
->stream
) {
3144 memcpy(st
, c
->stream
->streams
[stream_index
], sizeof(AVStream
));
3147 c
->stream
->feed
->streams
[c
->stream
->feed_streams
[stream_index
]],
3151 /* build destination RTP address */
3152 ipaddr
= inet_ntoa(dest_addr
->sin_addr
);
3154 switch(c
->rtp_protocol
) {
3155 case RTSP_PROTOCOL_RTP_UDP
:
3156 case RTSP_PROTOCOL_RTP_UDP_MULTICAST
:
3159 /* XXX: also pass as parameter to function ? */
3160 if (c
->stream
->is_multicast
) {
3162 ttl
= c
->stream
->multicast_ttl
;
3165 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3166 "rtp://%s:%d?multicast=1&ttl=%d",
3167 ipaddr
, ntohs(dest_addr
->sin_port
), ttl
);
3169 snprintf(ctx
->filename
, sizeof(ctx
->filename
),
3170 "rtp://%s:%d", ipaddr
, ntohs(dest_addr
->sin_port
));
3173 if (url_open(&h
, ctx
->filename
, URL_WRONLY
) < 0)
3175 c
->rtp_handles
[stream_index
] = h
;
3176 max_packet_size
= url_get_max_packet_size(h
);
3178 case RTSP_PROTOCOL_RTP_TCP
:
3181 max_packet_size
= RTSP_TCP_MAX_PACKET_SIZE
;
3187 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
3188 ipaddr
, ntohs(dest_addr
->sin_port
),
3190 c
->stream
->filename
, stream_index
, c
->protocol
);
3192 /* normally, no packets should be output here, but the packet size may be checked */
3193 if (url_open_dyn_packet_buf(&ctx
->pb
, max_packet_size
) < 0) {
3194 /* XXX: close stream */
3197 av_set_parameters(ctx
, NULL
);
3198 if (av_write_header(ctx
) < 0) {
3205 url_close_dyn_buf(&ctx
->pb
, &dummy_buf
);
3208 c
->rtp_ctx
[stream_index
] = ctx
;
3212 /********************************************************************/
3213 /* ffserver initialization */
3215 static AVStream
*add_av_stream1(FFStream
*stream
, AVCodecContext
*codec
)
3219 fst
= av_mallocz(sizeof(AVStream
));
3222 fst
->codec
= avcodec_alloc_context();
3223 fst
->priv_data
= av_mallocz(sizeof(FeedData
));
3224 memcpy(fst
->codec
, codec
, sizeof(AVCodecContext
));
3225 fst
->codec
->coded_frame
= &dummy_frame
;
3226 fst
->index
= stream
->nb_streams
;
3227 av_set_pts_info(fst
, 33, 1, 90000);
3228 stream
->streams
[stream
->nb_streams
++] = fst
;
3232 /* return the stream number in the feed */
3233 static int add_av_stream(FFStream
*feed