set is_streamed before writing data to output file
[libav.git] / ffserver.c
CommitLineData
85f07f22
FB
1/*
2 * Multiple format streaming server
773a21b8 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
85f07f22 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
773a21b8
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
85f07f22 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
85f07f22 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
773a21b8
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
85f07f22 16 *
773a21b8 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
85f07f22 20 */
364a9607 21
0f4e8165 22#include "config.h"
3cf9ea7f 23#ifndef HAVE_CLOSESOCKET
0f4e8165
RB
24#define closesocket close
25#endif
26#include <string.h>
27#include <stdlib.h>
245976da
DB
28#include "libavutil/random.h"
29#include "libavutil/avstring.h"
30#include "libavformat/avformat.h"
31#include "libavformat/network.h"
32#include "libavformat/os_support.h"
33#include "libavformat/rtp.h"
34#include "libavformat/rtsp.h"
773a21b8 35
85f07f22 36#include <stdarg.h>
85f07f22
FB
37#include <unistd.h>
38#include <fcntl.h>
39#include <sys/ioctl.h>
f8cda19e
LA
40#ifdef HAVE_POLL_H
41#include <poll.h>
b0c858d8 42#endif
85f07f22
FB
43#include <errno.h>
44#include <sys/time.h>
4568325a 45#undef time //needed because HAVE_AV_CONFIG_H is defined on top
85f07f22 46#include <time.h>
5eb765ef 47#include <sys/wait.h>
85f07f22 48#include <signal.h>
18b67ae5 49#ifdef HAVE_DLFCN_H
2effd274 50#include <dlfcn.h>
6638d424 51#endif
2effd274 52
4ce5df08 53#include "cmdutils.h"
85f07f22 54
c367d067
MN
55#undef exit
56
64555bd9 57const char program_name[] = "FFserver";
ea9c581f 58const int program_birth_year = 2000;
86074ed1 59
5a635bc7
SS
60static const OptionDef options[];
61
85f07f22
FB
62/* maximum number of simultaneous HTTP connections */
63#define HTTP_MAX_CONNECTIONS 2000
64
65enum HTTPState {
66 HTTPSTATE_WAIT_REQUEST,
67 HTTPSTATE_SEND_HEADER,
68 HTTPSTATE_SEND_DATA_HEADER,
2effd274 69 HTTPSTATE_SEND_DATA, /* sending TCP or UDP data */
85f07f22 70 HTTPSTATE_SEND_DATA_TRAILER,
115329f1 71 HTTPSTATE_RECEIVE_DATA,
2effd274 72 HTTPSTATE_WAIT_FEED, /* wait for data from the feed */
2effd274
FB
73 HTTPSTATE_READY,
74
75 RTSPSTATE_WAIT_REQUEST,
76 RTSPSTATE_SEND_REPLY,
bc351386 77 RTSPSTATE_SEND_PACKET,
85f07f22
FB
78};
79
80const char *http_state[] = {
2effd274
FB
81 "HTTP_WAIT_REQUEST",
82 "HTTP_SEND_HEADER",
83
85f07f22
FB
84 "SEND_DATA_HEADER",
85 "SEND_DATA",
86 "SEND_DATA_TRAILER",
87 "RECEIVE_DATA",
88 "WAIT_FEED",
2effd274
FB
89 "READY",
90
91 "RTSP_WAIT_REQUEST",
92 "RTSP_SEND_REPLY",
bc351386 93 "RTSP_SEND_PACKET",
85f07f22
FB
94};
95
cde25790 96#define IOBUFFER_INIT_SIZE 8192
85f07f22 97
85f07f22 98/* timeouts are in ms */
2effd274
FB
99#define HTTP_REQUEST_TIMEOUT (15 * 1000)
100#define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)
101
85f07f22
FB
102#define SYNC_TIMEOUT (10 * 1000)
103
5eb765ef 104typedef struct {
0c1a9eda 105 int64_t count1, count2;
c3f58185 106 int64_t time1, time2;
5eb765ef
PG
107} DataRateData;
108
85f07f22
FB
109/* context associated with one connection */
110typedef struct HTTPContext {
111 enum HTTPState state;
112 int fd; /* socket file descriptor */
113 struct sockaddr_in from_addr; /* origin */
114 struct pollfd *poll_entry; /* used when polling */
c3f58185 115 int64_t timeout;
0c1a9eda 116 uint8_t *buffer_ptr, *buffer_end;
85f07f22 117 int http_error;
edfdd798 118 int post;
85f07f22 119 struct HTTPContext *next;
42a63c6a 120 int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
0c1a9eda 121 int64_t data_count;
85f07f22
FB
122 /* feed input */
123 int feed_fd;
124 /* input format handling */
125 AVFormatContext *fmt_in;
c3f58185 126 int64_t start_time; /* In milliseconds - this wraps fairly often */
0c1a9eda 127 int64_t first_pts; /* initial pts value */
e240a0bb
FB
128 int64_t cur_pts; /* current pts value from the stream in us */
129 int64_t cur_frame_duration; /* duration of the current frame in us */
130 int cur_frame_bytes; /* output frame size, needed to compute
131 the time at which we send each
132 packet */
133 int pts_stream_index; /* stream we choose as clock reference */
134 int64_t cur_clock; /* current clock reference value in us */
85f07f22
FB
135 /* output format handling */
136 struct FFStream *stream;
cde25790
PG
137 /* -1 is invalid stream */
138 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
139 int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */
140 int switch_pending;
2effd274 141 AVFormatContext fmt_ctx; /* instance of FFStream for one user */
85f07f22 142 int last_packet_sent; /* true if last data packet was sent */
7434ba6d 143 int suppress_log;
5eb765ef 144 DataRateData datarate;
3120d2a2 145 int wmp_client_id;
7434ba6d
PG
146 char protocol[16];
147 char method[16];
148 char url[128];
cde25790 149 int buffer_size;
0c1a9eda 150 uint8_t *buffer;
2effd274
FB
151 int is_packetized; /* if true, the stream is packetized */
152 int packet_stream_index; /* current stream for output in state machine */
115329f1 153
2effd274 154 /* RTSP state specific */
0c1a9eda 155 uint8_t *pb_buffer; /* XXX: use that in all the code */
2effd274
FB
156 ByteIOContext *pb;
157 int seq; /* RTSP sequence number */
115329f1 158
2effd274
FB
159 /* RTP state specific */
160 enum RTSPProtocol rtp_protocol;
161 char session_id[32]; /* session id */
162 AVFormatContext *rtp_ctx[MAX_STREAMS];
e240a0bb 163
bc351386
FB
164 /* RTP/UDP specific */
165 URLContext *rtp_handles[MAX_STREAMS];
166
167 /* RTP/TCP specific */
168 struct HTTPContext *rtsp_c;
169 uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;
85f07f22
FB
170} HTTPContext;
171
172/* each generated stream is described here */
173enum StreamType {
174 STREAM_TYPE_LIVE,
175 STREAM_TYPE_STATUS,
cde25790 176 STREAM_TYPE_REDIRECT,
85f07f22
FB
177};
178
8256c0a3
PG
179enum IPAddressAction {
180 IP_ALLOW = 1,
181 IP_DENY,
182};
183
184typedef struct IPAddressACL {
185 struct IPAddressACL *next;
186 enum IPAddressAction action;
efa04ce2 187 /* These are in host order */
8256c0a3
PG
188 struct in_addr first;
189 struct in_addr last;
190} IPAddressACL;
191
85f07f22
FB
192/* description of each stream of the ffserver.conf file */
193typedef struct FFStream {
194 enum StreamType stream_type;
195 char filename[1024]; /* stream filename */
2effd274
FB
196 struct FFStream *feed; /* feed we are using (can be null if
197 coming from file) */
e240a0bb
FB
198 AVFormatParameters *ap_in; /* input parameters */
199 AVInputFormat *ifmt; /* if non NULL, force input format */
bd7cf6ad 200 AVOutputFormat *fmt;
8256c0a3 201 IPAddressACL *acl;
85f07f22 202 int nb_streams;
42a63c6a 203 int prebuffer; /* Number of millseconds early to start */
c3f58185 204 int64_t max_time; /* Number of milliseconds to run */
79c4ea3c 205 int send_on_key;
85f07f22
FB
206 AVStream *streams[MAX_STREAMS];
207 int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
208 char feed_filename[1024]; /* file name of the feed storage, or
209 input file name for a stream */
2ac887ba
PG
210 char author[512];
211 char title[512];
212 char copyright[512];
213 char comment[512];
cde25790 214 pid_t pid; /* Of ffmpeg process */
5eb765ef 215 time_t pid_start; /* Of ffmpeg process */
cde25790 216 char **child_argv;
85f07f22 217 struct FFStream *next;
6edd6884 218 int bandwidth; /* bandwidth, in kbits/s */
2effd274
FB
219 /* RTSP options */
220 char *rtsp_option;
829ac53d
FB
221 /* multicast specific */
222 int is_multicast;
223 struct in_addr multicast_ip;
224 int multicast_port; /* first port used for multicast */
6edd6884
FB
225 int multicast_ttl;
226 int loop; /* if true, send the stream in loops (only meaningful if file) */
829ac53d 227
85f07f22 228 /* feed specific */
2effd274 229 int feed_opened; /* true if someone is writing to the feed */
85f07f22 230 int is_feed; /* true if it is a feed */
e322ea48 231 int readonly; /* True if writing is prohibited to the file */
a6e14edd 232 int conns_served;
0c1a9eda 233 int64_t bytes_served;
6b0bdc75 234 int64_t feed_max_size; /* maximum storage size, zero means unlimited */
8bfb108b 235 int64_t feed_write_index; /* current write position in feed (it wraps around) */
0c1a9eda 236 int64_t feed_size; /* current size of feed */
85f07f22
FB
237 struct FFStream *next_feed;
238} FFStream;
239
240typedef struct FeedData {
241 long long data_count;
8bfb108b 242 float avg_frame_size; /* frame size averaged over last frames with exponential mean */
85f07f22
FB
243} FeedData;
244
18405874
AB
245static struct sockaddr_in my_http_addr;
246static struct sockaddr_in my_rtsp_addr;
2effd274 247
33f5e2ec
AB
248static char logfilename[1024];
249static HTTPContext *first_http_ctx;
250static FFStream *first_feed; /* contains only feeds */
251static FFStream *first_stream; /* contains all streams, including feeds */
85f07f22 252
2effd274
FB
253static void new_connection(int server_fd, int is_rtsp);
254static void close_connection(HTTPContext *c);
255
256/* HTTP handling */
257static int handle_connection(HTTPContext *c);
85f07f22 258static int http_parse_request(HTTPContext *c);
5eb765ef 259static int http_send_data(HTTPContext *c);
85f07f22
FB
260static void compute_stats(HTTPContext *c);
261static int open_input_stream(HTTPContext *c, const char *info);
262static int http_start_receive_data(HTTPContext *c);
263static int http_receive_data(HTTPContext *c);
2effd274
FB
264
265/* RTSP handling */
266static int rtsp_parse_request(HTTPContext *c);
267static void rtsp_cmd_describe(HTTPContext *c, const char *url);
0df65975 268static void rtsp_cmd_options(HTTPContext *c, const char *url);
2effd274
FB
269static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPHeader *h);
270static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h);
271static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h);
272static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h);
273
829ac53d 274/* SDP handling */
115329f1 275static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
829ac53d
FB
276 struct in_addr my_ip);
277
2effd274 278/* RTP handling */
115329f1 279static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
bc351386
FB
280 FFStream *stream, const char *session_id,
281 enum RTSPProtocol rtp_protocol);
115329f1 282static int rtp_new_av_stream(HTTPContext *c,
bc351386
FB
283 int stream_index, struct sockaddr_in *dest_addr,
284 HTTPContext *rtsp_c);
85f07f22 285
cde25790 286static const char *my_program_name;
d6562d2c 287static const char *my_program_dir;
cde25790 288
5a635bc7 289static const char *config_filename;
2ac887ba 290static int ffserver_debug;
2effd274 291static int ffserver_daemon;
2ac887ba 292static int no_launch;
5eb765ef 293static int need_to_start_children;
2ac887ba 294
33f5e2ec
AB
295static int nb_max_connections;
296static int nb_connections;
85f07f22 297
33f5e2ec
AB
298static int max_bandwidth;
299static int current_bandwidth;
42a63c6a 300
c3f58185 301static int64_t cur_time; // Making this global saves on passing it around everywhere
5eb765ef 302
1df93ae9
AB
303static AVRandomState random_state;
304
85f07f22
FB
305static FILE *logfile = NULL;
306
115329f1 307static void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
85f07f22
FB
308{
309 va_list ap;
310 va_start(ap, fmt);
115329f1 311
7434ba6d 312 if (logfile) {
85f07f22 313 vfprintf(logfile, fmt, ap);
7434ba6d
PG
314 fflush(logfile);
315 }
85f07f22
FB
316 va_end(ap);
317}
318
6edd6884 319static char *ctime1(char *buf2)
7434ba6d 320{
7434ba6d 321 time_t ti;
6edd6884 322 char *p;
7434ba6d 323
7434ba6d
PG
324 ti = time(NULL);
325 p = ctime(&ti);
326 strcpy(buf2, p);
327 p = buf2 + strlen(p) - 1;
328 if (*p == '\n')
329 *p = '\0';
6edd6884
FB
330 return buf2;
331}
332
333static void log_connection(HTTPContext *c)
334{
335 char buf2[32];
336
115329f1 337 if (c->suppress_log)
6edd6884
FB
338 return;
339
115329f1
DB
340 http_log("%s - - [%s] \"%s %s %s\" %d %"PRId64"\n",
341 inet_ntoa(c->from_addr.sin_addr),
342 ctime1(buf2), c->method, c->url,
6edd6884 343 c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
cde25790
PG
344}
345
0c1a9eda 346static void update_datarate(DataRateData *drd, int64_t count)
5eb765ef
PG
347{
348 if (!drd->time1 && !drd->count1) {
349 drd->time1 = drd->time2 = cur_time;
350 drd->count1 = drd->count2 = count;
eeffbdea 351 } else if (cur_time - drd->time2 > 5000) {
33a4ecbe
AB
352 drd->time1 = drd->time2;
353 drd->count1 = drd->count2;
354 drd->time2 = cur_time;
355 drd->count2 = count;
5eb765ef
PG
356 }
357}
358
359/* In bytes per second */
0c1a9eda 360static int compute_datarate(DataRateData *drd, int64_t count)
5eb765ef
PG
361{
362 if (cur_time == drd->time1)
363 return 0;
115329f1 364
5eb765ef
PG
365 return ((count - drd->count1) * 1000) / (cur_time - drd->time1);
366}
367
a782f209 368
cde25790
PG
369static void start_children(FFStream *feed)
370{
2ac887ba
PG
371 if (no_launch)
372 return;
373
cde25790 374 for (; feed; feed = feed->next) {
5eb765ef
PG
375 if (feed->child_argv && !feed->pid) {
376 feed->pid_start = time(0);
377
cde25790
PG
378 feed->pid = fork();
379
380 if (feed->pid < 0) {
381 fprintf(stderr, "Unable to create children\n");
382 exit(1);
383 }
384 if (!feed->pid) {
385 /* In child */
386 char pathname[1024];
387 char *slash;
388 int i;
389
611c5741 390 for (i = 3; i < 256; i++)
5eb765ef 391 close(i);
cde25790 392
5eb765ef 393 if (!ffserver_debug) {
2ac887ba
PG
394 i = open("/dev/null", O_RDWR);
395 if (i)
396 dup2(i, 0);
397 dup2(i, 1);
398 dup2(i, 2);
5eb765ef
PG
399 if (i)
400 close(i);
2ac887ba 401 }
cde25790 402
f7d78f36 403 av_strlcpy(pathname, my_program_name, sizeof(pathname));
cde25790
PG
404
405 slash = strrchr(pathname, '/');
611c5741 406 if (!slash)
cde25790 407 slash = pathname;
611c5741 408 else
cde25790 409 slash++;
cde25790
PG
410 strcpy(slash, "ffmpeg");
411
d6562d2c
PG
412 /* This is needed to make relative pathnames work */
413 chdir(my_program_dir);
414
a4d70941
PG
415 signal(SIGPIPE, SIG_DFL);
416
cde25790
PG
417 execvp(pathname, feed->child_argv);
418
419 _exit(1);
420 }
421 }
422 }
7434ba6d
PG
423}
424
2effd274
FB
425/* open a listening socket */
426static int socket_open_listen(struct sockaddr_in *my_addr)
85f07f22 427{
2effd274 428 int server_fd, tmp;
85f07f22
FB
429
430 server_fd = socket(AF_INET,SOCK_STREAM,0);
431 if (server_fd < 0) {
432 perror ("socket");
433 return -1;
434 }
115329f1 435
85f07f22
FB
436 tmp = 1;
437 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
438
2effd274 439 if (bind (server_fd, (struct sockaddr *) my_addr, sizeof (*my_addr)) < 0) {
b17d099d
PG
440 char bindmsg[32];
441 snprintf(bindmsg, sizeof(bindmsg), "bind(port %d)", ntohs(my_addr->sin_port));
442 perror (bindmsg);
d96633bb 443 closesocket(server_fd);
85f07f22
FB
444 return -1;
445 }
115329f1 446
85f07f22
FB
447 if (listen (server_fd, 5) < 0) {
448 perror ("listen");
d96633bb 449 closesocket(server_fd);
85f07f22
FB
450 return -1;
451 }
ba472aaf 452 ff_socket_nonblock(server_fd, 1);
2effd274
FB
453
454 return server_fd;
455}
456
6edd6884
FB
457/* start all multicast streams */
458static void start_multicast(void)
459{
460 FFStream *stream;
461 char session_id[32];
462 HTTPContext *rtp_c;
463 struct sockaddr_in dest_addr;
464 int default_port, stream_index;
465
466 default_port = 6000;
467 for(stream = first_stream; stream != NULL; stream = stream->next) {
468 if (stream->is_multicast) {
469 /* open the RTP connection */
1df93ae9
AB
470 snprintf(session_id, sizeof(session_id), "%08x%08x",
471 av_random(&random_state), av_random(&random_state));
6edd6884
FB
472
473 /* choose a port if none given */
474 if (stream->multicast_port == 0) {
475 stream->multicast_port = default_port;
476 default_port += 100;
477 }
478
479 dest_addr.sin_family = AF_INET;
480 dest_addr.sin_addr = stream->multicast_ip;
481 dest_addr.sin_port = htons(stream->multicast_port);
482
115329f1 483 rtp_c = rtp_new_connection(&dest_addr, stream, session_id,
bc351386 484 RTSP_PROTOCOL_RTP_UDP_MULTICAST);
611c5741 485 if (!rtp_c)
6edd6884 486 continue;
611c5741 487
6edd6884 488 if (open_input_stream(rtp_c, "") < 0) {
115329f1 489 fprintf(stderr, "Could not open input stream for stream '%s'\n",
6edd6884
FB
490 stream->filename);
491 continue;
492 }
493
6edd6884 494 /* open each RTP stream */
115329f1 495 for(stream_index = 0; stream_index < stream->nb_streams;
6edd6884 496 stream_index++) {
115329f1 497 dest_addr.sin_port = htons(stream->multicast_port +
6edd6884 498 2 * stream_index);
bc351386 499 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, NULL) < 0) {
115329f1 500 fprintf(stderr, "Could not open output stream '%s/streamid=%d'\n",
0fa45e19
FB
501 stream->filename, stream_index);
502 exit(1);
6edd6884
FB
503 }
504 }
505
506 /* change state to send data */
507 rtp_c->state = HTTPSTATE_SEND_DATA;
508 }
509 }
510}
2effd274
FB
511
512/* main loop of the http server */
513static int http_server(void)
514{
515 int server_fd, ret, rtsp_server_fd, delay, delay1;
516 struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 2], *poll_entry;
517 HTTPContext *c, *c_next;
518
519 server_fd = socket_open_listen(&my_http_addr);
520 if (server_fd < 0)
521 return -1;
85f07f22 522
2effd274
FB
523 rtsp_server_fd = socket_open_listen(&my_rtsp_addr);
524 if (rtsp_server_fd < 0)
525 return -1;
115329f1 526
85f07f22
FB
527 http_log("ffserver started.\n");
528
cde25790
PG
529 start_children(first_feed);
530
85f07f22
FB
531 first_http_ctx = NULL;
532 nb_connections = 0;
6edd6884
FB
533
534 start_multicast();
535
85f07f22
FB
536 for(;;) {
537 poll_entry = poll_table;
538 poll_entry->fd = server_fd;
539 poll_entry->events = POLLIN;
540 poll_entry++;
541
2effd274
FB
542 poll_entry->fd = rtsp_server_fd;
543 poll_entry->events = POLLIN;
544 poll_entry++;
545
85f07f22
FB
546 /* wait for events on each HTTP handle */
547 c = first_http_ctx;
2effd274 548 delay = 1000;
85f07f22
FB
549 while (c != NULL) {
550 int fd;
551 fd = c->fd;
552 switch(c->state) {
2effd274
FB
553 case HTTPSTATE_SEND_HEADER:
554 case RTSPSTATE_SEND_REPLY:
bc351386 555 case RTSPSTATE_SEND_PACKET:
85f07f22
FB
556 c->poll_entry = poll_entry;
557 poll_entry->fd = fd;
2effd274 558 poll_entry->events = POLLOUT;
85f07f22
FB
559 poll_entry++;
560 break;
85f07f22
FB
561 case HTTPSTATE_SEND_DATA_HEADER:
562 case HTTPSTATE_SEND_DATA:
563 case HTTPSTATE_SEND_DATA_TRAILER:
2effd274
FB
564 if (!c->is_packetized) {
565 /* for TCP, we output as much as we can (may need to put a limit) */
566 c->poll_entry = poll_entry;
567 poll_entry->fd = fd;
568 poll_entry->events = POLLOUT;
569 poll_entry++;
570 } else {
e240a0bb
FB
571 /* when ffserver is doing the timing, we work by
572 looking at which packet need to be sent every
573 10 ms */
574 delay1 = 10; /* one tick wait XXX: 10 ms assumed */
575 if (delay1 < delay)
576 delay = delay1;
2effd274 577 }
85f07f22 578 break;
2effd274 579 case HTTPSTATE_WAIT_REQUEST:
85f07f22 580 case HTTPSTATE_RECEIVE_DATA:
85f07f22 581 case HTTPSTATE_WAIT_FEED:
2effd274 582 case RTSPSTATE_WAIT_REQUEST:
85f07f22
FB
583 /* need to catch errors */
584 c->poll_entry = poll_entry;
585 poll_entry->fd = fd;
a6e14edd 586 poll_entry->events = POLLIN;/* Maybe this will work */
85f07f22
FB
587 poll_entry++;
588 break;
589 default:
590 c->poll_entry = NULL;
591 break;
592 }
593 c = c->next;
594 }
595
596 /* wait for an event on one connection. We poll at least every
597 second to handle timeouts */
598 do {
2effd274 599 ret = poll(poll_table, poll_entry - poll_table, delay);
8da4034f
AB
600 if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
601 ff_neterrno() != FF_NETERROR(EINTR))
53e2f9ca 602 return -1;
e8d658df 603 } while (ret < 0);
115329f1 604
c3f58185 605 cur_time = av_gettime() / 1000;
85f07f22 606
5eb765ef
PG
607 if (need_to_start_children) {
608 need_to_start_children = 0;
609 start_children(first_feed);
610 }
611
85f07f22 612 /* now handle the events */
2effd274
FB
613 for(c = first_http_ctx; c != NULL; c = c_next) {
614 c_next = c->next;
615 if (handle_connection(c) < 0) {
85f07f22 616 /* close and free the connection */
7434ba6d 617 log_connection(c);
2effd274 618 close_connection(c);
85f07f22
FB
619 }
620 }
621
85f07f22 622 poll_entry = poll_table;
2effd274 623 /* new HTTP connection request ? */
611c5741 624 if (poll_entry->revents & POLLIN)
2effd274 625 new_connection(server_fd, 0);
85f07f22 626 poll_entry++;
2effd274 627 /* new RTSP connection request ? */
611c5741 628 if (poll_entry->revents & POLLIN)
2effd274 629 new_connection(rtsp_server_fd, 1);
85f07f22
FB
630 }
631}
632
2effd274
FB
633/* start waiting for a new HTTP/RTSP request */
634static void start_wait_request(HTTPContext *c, int is_rtsp)
85f07f22 635{
2effd274
FB
636 c->buffer_ptr = c->buffer;
637 c->buffer_end = c->buffer + c->buffer_size - 1; /* leave room for '\0' */
638
639 if (is_rtsp) {
640 c->timeout = cur_time + RTSP_REQUEST_TIMEOUT;
641 c->state = RTSPSTATE_WAIT_REQUEST;
642 } else {
643 c->timeout = cur_time + HTTP_REQUEST_TIMEOUT;
644 c->state = HTTPSTATE_WAIT_REQUEST;
645 }
646}
647
648static void new_connection(int server_fd, int is_rtsp)
649{
650 struct sockaddr_in from_addr;
651 int fd, len;
652 HTTPContext *c = NULL;
653
654 len = sizeof(from_addr);
115329f1 655 fd = accept(server_fd, (struct sockaddr *)&from_addr,
2effd274
FB
656 &len);
657 if (fd < 0)
658 return;
ba472aaf 659 ff_socket_nonblock(fd, 1);
2effd274
FB
660
661 /* XXX: should output a warning page when coming
662 close to the connection limit */
663 if (nb_connections >= nb_max_connections)
664 goto fail;
115329f1 665
2effd274
FB
666 /* add a new connection */
667 c = av_mallocz(sizeof(HTTPContext));
668 if (!c)
669 goto fail;
115329f1 670
2effd274
FB
671 c->fd = fd;
672 c->poll_entry = NULL;
673 c->from_addr = from_addr;
674 c->buffer_size = IOBUFFER_INIT_SIZE;
675 c->buffer = av_malloc(c->buffer_size);
676 if (!c->buffer)
677 goto fail;
8bc80f8b
PG
678
679 c->next = first_http_ctx;
680 first_http_ctx = c;
2effd274 681 nb_connections++;
115329f1 682
2effd274
FB
683 start_wait_request(c, is_rtsp);
684
685 return;
686
687 fail:
688 if (c) {
689 av_free(c->buffer);
690 av_free(c);
691 }
d96633bb 692 closesocket(fd);
2effd274
FB
693}
694
695static void close_connection(HTTPContext *c)
696{
697 HTTPContext **cp, *c1;
698 int i, nb_streams;
699 AVFormatContext *ctx;
700 URLContext *h;
701 AVStream *st;
702
703 /* remove connection from list */
704 cp = &first_http_ctx;
705 while ((*cp) != NULL) {
706 c1 = *cp;
611c5741 707 if (c1 == c)
2effd274 708 *cp = c->next;
611c5741 709 else
2effd274 710 cp = &c1->next;
2effd274
FB
711 }
712
bc351386
FB
713 /* remove references, if any (XXX: do it faster) */
714 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
715 if (c1->rtsp_c == c)
716 c1->rtsp_c = NULL;
717 }
718
2effd274
FB
719 /* remove connection associated resources */
720 if (c->fd >= 0)
d96633bb 721 closesocket(c->fd);
2effd274
FB
722 if (c->fmt_in) {
723 /* close each frame parser */
724 for(i=0;i<c->fmt_in->nb_streams;i++) {
725 st = c->fmt_in->streams[i];
611c5741 726 if (st->codec->codec)
01f4895c 727 avcodec_close(st->codec);
2effd274
FB
728 }
729 av_close_input_file(c->fmt_in);
730 }
731
732 /* free RTP output streams if any */
733 nb_streams = 0;
115329f1 734 if (c->stream)
2effd274 735 nb_streams = c->stream->nb_streams;
115329f1 736
2effd274
FB
737 for(i=0;i<nb_streams;i++) {
738 ctx = c->rtp_ctx[i];
739 if (ctx) {
740 av_write_trailer(ctx);
741 av_free(ctx);
742 }
743 h = c->rtp_handles[i];
611c5741 744 if (h)
2effd274 745 url_close(h);
2effd274 746 }
115329f1 747
b88ba823
MH
748 ctx = &c->fmt_ctx;
749
87638494 750 if (!c->last_packet_sent) {
87638494
PG
751 if (ctx->oformat) {
752 /* prepare header */
753 if (url_open_dyn_buf(&ctx->pb) >= 0) {
754 av_write_trailer(ctx);
899681cd 755 url_close_dyn_buf(ctx->pb, &c->pb_buffer);
87638494
PG
756 }
757 }
758 }
759
115329f1 760 for(i=0; i<ctx->nb_streams; i++)
0bd53967 761 av_free(ctx->streams[i]);
f0ef6240 762
edfdd798 763 if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE)
6edd6884 764 current_bandwidth -= c->stream->bandwidth;
5400e092
AB
765
766 /* signal that there is no feed if we are the feeder socket */
767 if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) {
768 c->stream->feed_opened = 0;
769 close(c->feed_fd);
770 }
771
2effd274 772 av_freep(&c->pb_buffer);
bc351386 773 av_freep(&c->packet_buffer);
2effd274
FB
774 av_free(c->buffer);
775 av_free(c);
776 nb_connections--;
777}
778
779static int handle_connection(HTTPContext *c)
780{
781 int len, ret;
115329f1 782
85f07f22
FB
783 switch(c->state) {
784 case HTTPSTATE_WAIT_REQUEST:
2effd274 785 case RTSPSTATE_WAIT_REQUEST:
85f07f22
FB
786 /* timeout ? */
787 if ((c->timeout - cur_time) < 0)
788 return -1;
789 if (c->poll_entry->revents & (POLLERR | POLLHUP))
790 return -1;
791
792 /* no need to read if no events */
793 if (!(c->poll_entry->revents & POLLIN))
794 return 0;
795 /* read the data */
1bc1cfdd 796 read_loop:
c60202df 797 len = recv(c->fd, c->buffer_ptr, 1, 0);
85f07f22 798 if (len < 0) {
8da4034f
AB
799 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
800 ff_neterrno() != FF_NETERROR(EINTR))
85f07f22
FB
801 return -1;
802 } else if (len == 0) {
803 return -1;
804 } else {
94d9ad5f 805 /* search for end of request. */
0c1a9eda 806 uint8_t *ptr;
85f07f22
FB
807 c->buffer_ptr += len;
808 ptr = c->buffer_ptr;
809 if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
810 (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
811 /* request found : parse it and reply */
2effd274
FB
812 if (c->state == HTTPSTATE_WAIT_REQUEST) {
813 ret = http_parse_request(c);
814 } else {
815 ret = rtsp_parse_request(c);
816 }
817 if (ret < 0)
85f07f22
FB
818 return -1;
819 } else if (ptr >= c->buffer_end) {
820 /* request too long: cannot do anything */
821 return -1;
1bc1cfdd 822 } else goto read_loop;
85f07f22
FB
823 }
824 break;
825
826 case HTTPSTATE_SEND_HEADER:
827 if (c->poll_entry->revents & (POLLERR | POLLHUP))
828 return -1;
829
2effd274 830 /* no need to write if no events */
85f07f22
FB
831 if (!(c->poll_entry->revents & POLLOUT))
832 return 0;
c60202df 833 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
85f07f22 834 if (len < 0) {
8da4034f
AB
835 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
836 ff_neterrno() != FF_NETERROR(EINTR)) {
85f07f22 837 /* error : close connection */
2effd274 838 av_freep(&c->pb_buffer);
85f07f22
FB
839 return -1;
840 }
841 } else {
842 c->buffer_ptr += len;
2e04edb3
PG
843 if (c->stream)
844 c->stream->bytes_served += len;
a6e14edd 845 c->data_count += len;
85f07f22 846 if (c->buffer_ptr >= c->buffer_end) {
2effd274 847 av_freep(&c->pb_buffer);
85f07f22 848 /* if error, exit */
611c5741 849 if (c->http_error)
85f07f22 850 return -1;
2effd274 851 /* all the buffer was sent : synchronize to the incoming stream */
85f07f22
FB
852 c->state = HTTPSTATE_SEND_DATA_HEADER;
853 c->buffer_ptr = c->buffer_end = c->buffer;
854 }
855 }
856 break;
857
858 case HTTPSTATE_SEND_DATA:
859 case HTTPSTATE_SEND_DATA_HEADER:
860 case HTTPSTATE_SEND_DATA_TRAILER:
2effd274
FB
861 /* for packetized output, we consider we can always write (the
862 input streams sets the speed). It may be better to verify
863 that we do not rely too much on the kernel queues */
864 if (!c->is_packetized) {
865 if (c->poll_entry->revents & (POLLERR | POLLHUP))
866 return -1;
115329f1 867
2effd274
FB
868 /* no need to read if no events */
869 if (!(c->poll_entry->revents & POLLOUT))
870 return 0;
871 }
5eb765ef 872 if (http_send_data(c) < 0)
85f07f22 873 return -1;
638831aa
AB
874 /* close connection if trailer sent */
875 if (c->state == HTTPSTATE_SEND_DATA_TRAILER)
876 return -1;
85f07f22
FB
877 break;
878 case HTTPSTATE_RECEIVE_DATA:
879 /* no need to read if no events */
880 if (c->poll_entry->revents & (POLLERR | POLLHUP))
881 return -1;
882 if (!(c->poll_entry->revents & POLLIN))
883 return 0;
884 if (http_receive_data(c) < 0)
885 return -1;
886 break;
887 case HTTPSTATE_WAIT_FEED:
888 /* no need to read if no events */
a6e14edd 889 if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
85f07f22
FB
890 return -1;
891
892 /* nothing to do, we'll be waken up by incoming feed packets */
893 break;
2effd274 894
2effd274
FB
895 case RTSPSTATE_SEND_REPLY:
896 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
897 av_freep(&c->pb_buffer);
898 return -1;
899 }
900 /* no need to write if no events */
901 if (!(c->poll_entry->revents & POLLOUT))
902 return 0;
c60202df 903 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
2effd274 904 if (len < 0) {
8da4034f
AB
905 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
906 ff_neterrno() != FF_NETERROR(EINTR)) {
2effd274
FB
907 /* error : close connection */
908 av_freep(&c->pb_buffer);
909 return -1;
910 }
911 } else {
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);
918 }
919 }
920 break;
bc351386
FB
921 case RTSPSTATE_SEND_PACKET:
922 if (c->poll_entry->revents & (POLLERR | POLLHUP)) {
923 av_freep(&c->packet_buffer);
924 return -1;
925 }
926 /* no need to write if no events */
927 if (!(c->poll_entry->revents & POLLOUT))
928 return 0;
c60202df
AB
929 len = send(c->fd, c->packet_buffer_ptr,
930 c->packet_buffer_end - c->packet_buffer_ptr, 0);
bc351386 931 if (len < 0) {
8da4034f
AB
932 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
933 ff_neterrno() != FF_NETERROR(EINTR)) {
bc351386
FB
934 /* error : close connection */
935 av_freep(&c->packet_buffer);
936 return -1;
937 }
938 } else {
939 c->packet_buffer_ptr += len;
940 if (c->packet_buffer_ptr >= c->packet_buffer_end) {
941 /* all the buffer was sent : wait for a new request */
942 av_freep(&c->packet_buffer);
943 c->state = RTSPSTATE_WAIT_REQUEST;
944 }
945 }
946 break;
2effd274
FB
947 case HTTPSTATE_READY:
948 /* nothing to do */
949 break;
85f07f22
FB
950 default:
951 return -1;
952 }
953 return 0;
954}
955
3120d2a2
PG
956static int extract_rates(char *rates, int ratelen, const char *request)
957{
958 const char *p;
959
960 for (p = request; *p && *p != '\r' && *p != '\n'; ) {
961 if (strncasecmp(p, "Pragma:", 7) == 0) {
962 const char *q = p + 7;
963
964 while (*q && *q != '\n' && isspace(*q))
965 q++;
966
967 if (strncasecmp(q, "stream-switch-entry=", 20) == 0) {
968 int stream_no;
969 int rate_no;
970
971 q += 20;
972
cde25790 973 memset(rates, 0xff, ratelen);
3120d2a2
PG
974
975 while (1) {
976 while (*q && *q != '\n' && *q != ':')
977 q++;
978
611c5741 979 if (sscanf(q, ":%d:%d", &stream_no, &rate_no) != 2)
3120d2a2 980 break;
611c5741 981
3120d2a2 982 stream_no--;
611c5741 983 if (stream_no < ratelen && stream_no >= 0)
3120d2a2 984 rates[stream_no] = rate_no;
3120d2a2
PG
985
986 while (*q && *q != '\n' && !isspace(*q))
987 q++;
988 }
989
990 return 1;
991 }
992 }
993 p = strchr(p, '\n');
994 if (!p)
995 break;
996
997 p++;
998 }
999
1000 return 0;
1001}
1002
cde25790 1003static int find_stream_in_feed(FFStream *feed, AVCodecContext *codec, int bit_rate)
3120d2a2
PG
1004{
1005 int i;
cde25790
PG
1006 int best_bitrate = 100000000;
1007 int best = -1;
1008
1009 for (i = 0; i < feed->nb_streams; i++) {
01f4895c 1010 AVCodecContext *feed_codec = feed->streams[i]->codec;
cde25790
PG
1011
1012 if (feed_codec->codec_id != codec->codec_id ||
1013 feed_codec->sample_rate != codec->sample_rate ||
1014 feed_codec->width != codec->width ||
611c5741 1015 feed_codec->height != codec->height)
cde25790 1016 continue;
cde25790
PG
1017
1018 /* Potential stream */
1019
115329f1 1020 /* We want the fastest stream less than bit_rate, or the slowest
cde25790
PG
1021 * faster than bit_rate
1022 */
1023
1024 if (feed_codec->bit_rate <= bit_rate) {
1025 if (best_bitrate > bit_rate || feed_codec->bit_rate > best_bitrate) {
1026 best_bitrate = feed_codec->bit_rate;
1027 best = i;
1028 }
1029 } else {
1030 if (feed_codec->bit_rate < best_bitrate) {
1031 best_bitrate = feed_codec->bit_rate;
1032 best = i;
1033 }
1034 }
1035 }
1036
1037 return best;
1038}
1039
1040static int modify_current_stream(HTTPContext *c, char *rates)
1041{
1042 int i;
1043 FFStream *req = c->stream;
1044 int action_required = 0;
3120d2a2 1045
001bcd29
PG
1046 /* Not much we can do for a feed */
1047 if (!req->feed)
1048 return 0;
1049
3120d2a2 1050 for (i = 0; i < req->nb_streams; i++) {
01f4895c 1051 AVCodecContext *codec = req->streams[i]->codec;
3120d2a2 1052
3120d2a2
PG
1053 switch(rates[i]) {
1054 case 0:
cde25790 1055 c->switch_feed_streams[i] = req->feed_streams[i];
3120d2a2
PG
1056 break;
1057 case 1:
cde25790 1058 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 2);
3120d2a2
PG
1059 break;
1060 case 2:
cde25790
PG
1061 /* Wants off or slow */
1062 c->switch_feed_streams[i] = find_stream_in_feed(req->feed, codec, codec->bit_rate / 4);
1063#ifdef WANTS_OFF
1064 /* This doesn't work well when it turns off the only stream! */
1065 c->switch_feed_streams[i] = -2;
1066 c->feed_streams[i] = -2;
1067#endif
3120d2a2
PG
1068 break;
1069 }
3120d2a2 1070
cde25790
PG
1071 if (c->switch_feed_streams[i] >= 0 && c->switch_feed_streams[i] != c->feed_streams[i])
1072 action_required = 1;
1073 }
3120d2a2 1074
cde25790
PG
1075 return action_required;
1076}
3120d2a2 1077
3120d2a2 1078
cde25790
PG
1079static void do_switch_stream(HTTPContext *c, int i)
1080{
1081 if (c->switch_feed_streams[i] >= 0) {
115329f1 1082#ifdef PHILIP
cde25790
PG
1083 c->feed_streams[i] = c->switch_feed_streams[i];
1084#endif
3120d2a2 1085
cde25790 1086 /* Now update the stream */
3120d2a2 1087 }
cde25790 1088 c->switch_feed_streams[i] = -1;
3120d2a2 1089}
7434ba6d 1090
2effd274
FB
1091/* XXX: factorize in utils.c ? */
1092/* XXX: take care with different space meaning */
1093static void skip_spaces(const char **pp)
1094{
1095 const char *p;
1096 p = *pp;
1097 while (*p == ' ' || *p == '\t')
1098 p++;
1099 *pp = p;
1100}
1101
1102static void get_word(char *buf, int buf_size, const char **pp)
1103{
1104 const char *p;
1105 char *q;
1106
1107 p = *pp;
1108 skip_spaces(&p);
1109 q = buf;
1110 while (!isspace(*p) && *p != '\0') {
1111 if ((q - buf) < buf_size - 1)
1112 *q++ = *p;
1113 p++;
1114 }
1115 if (buf_size > 0)
1116 *q = '\0';
1117 *pp = p;
1118}
1119
8256c0a3
PG
1120static int validate_acl(FFStream *stream, HTTPContext *c)
1121{
1122 enum IPAddressAction last_action = IP_DENY;
1123 IPAddressACL *acl;
1124 struct in_addr *src = &c->from_addr.sin_addr;
2bd8416e 1125 unsigned long src_addr = src->s_addr;
8256c0a3
PG
1126
1127 for (acl = stream->acl; acl; acl = acl->next) {
611c5741 1128 if (src_addr >= acl->first.s_addr && src_addr <= acl->last.s_addr)
8256c0a3 1129 return (acl->action == IP_ALLOW) ? 1 : 0;
8256c0a3
PG
1130 last_action = acl->action;
1131 }
1132
1133 /* Nothing matched, so return not the last action */
1134 return (last_action == IP_DENY) ? 1 : 0;
1135}
1136
829ac53d
FB
1137/* compute the real filename of a file by matching it without its
1138 extensions to all the stream filenames */
1139static void compute_real_filename(char *filename, int max_size)
1140{
1141 char file1[1024];
1142 char file2[1024];
1143 char *p;
1144 FFStream *stream;
1145
1146 /* compute filename by matching without the file extensions */
f7d78f36 1147 av_strlcpy(file1, filename, sizeof(file1));
829ac53d
FB
1148 p = strrchr(file1, '.');
1149 if (p)
1150 *p = '\0';
1151 for(stream = first_stream; stream != NULL; stream = stream->next) {
f7d78f36 1152 av_strlcpy(file2, stream->filename, sizeof(file2));
829ac53d
FB
1153 p = strrchr(file2, '.');
1154 if (p)
1155 *p = '\0';
1156 if (!strcmp(file1, file2)) {
f7d78f36 1157 av_strlcpy(filename, stream->filename, max_size);
829ac53d
FB
1158 break;
1159 }
1160 }
1161}
1162
1163enum RedirType {
1164 REDIR_NONE,
1165 REDIR_ASX,
1166 REDIR_RAM,
1167 REDIR_ASF,
1168 REDIR_RTSP,
1169 REDIR_SDP,
1170};
1171
85f07f22
FB
1172/* parse http request and prepare header */
1173static int http_parse_request(HTTPContext *c)
1174{
1175 char *p;
829ac53d 1176 enum RedirType redir_type;
85f07f22 1177 char cmd[32];
bae79c04 1178 char info[1024], filename[1024];
85f07f22
FB
1179 char url[1024], *q;
1180 char protocol[32];
1181 char msg[1024];
1182 const char *mime_type;
1183 FFStream *stream;
42a63c6a 1184 int i;
3120d2a2 1185 char ratebuf[32];
cde25790 1186 char *useragent = 0;
85f07f22
FB
1187
1188 p = c->buffer;
2effd274 1189 get_word(cmd, sizeof(cmd), (const char **)&p);
f7d78f36 1190 av_strlcpy(c->method, cmd, sizeof(c->method));
7434ba6d 1191
85f07f22 1192 if (!strcmp(cmd, "GET"))
edfdd798 1193 c->post = 0;
85f07f22 1194 else if (!strcmp(cmd, "POST"))
edfdd798 1195 c->post = 1;
85f07f22
FB
1196 else
1197 return -1;
1198
2effd274 1199 get_word(url, sizeof(url), (const char **)&p);
f7d78f36 1200 av_strlcpy(c->url, url, sizeof(c->url));
7434ba6d 1201
2effd274 1202 get_word(protocol, sizeof(protocol), (const char **)&p);
85f07f22
FB
1203 if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
1204 return -1;
7434ba6d 1205
f7d78f36 1206 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
90f9c440
AB
1207
1208 if (ffserver_debug)
bb270c08 1209 http_log("New connection: %s %s\n", cmd, url);
115329f1 1210
85f07f22 1211 /* find the filename and the optional info string in the request */
bae79c04 1212 p = strchr(url, '?');
85f07f22 1213 if (p) {
f7d78f36 1214 av_strlcpy(info, p, sizeof(info));
85f07f22 1215 *p = '\0';
611c5741 1216 } else
85f07f22 1217 info[0] = '\0';
85f07f22 1218
f7d78f36 1219 av_strlcpy(filename, url + ((*url == '/') ? 1 : 0), sizeof(filename)-1);
bae79c04 1220
cde25790
PG
1221 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1222 if (strncasecmp(p, "User-Agent:", 11) == 0) {
1223 useragent = p + 11;
1224 if (*useragent && *useragent != '\n' && isspace(*useragent))
1225 useragent++;
1226 break;
1227 }
1228 p = strchr(p, '\n');
1229 if (!p)
1230 break;
1231
1232 p++;
1233 }
1234
829ac53d
FB
1235 redir_type = REDIR_NONE;
1236 if (match_ext(filename, "asx")) {
1237 redir_type = REDIR_ASX;
7434ba6d 1238 filename[strlen(filename)-1] = 'f';
c2ce254c 1239 } else if (match_ext(filename, "asf") &&
cde25790
PG
1240 (!useragent || strncasecmp(useragent, "NSPlayer", 8) != 0)) {
1241 /* if this isn't WMP or lookalike, return the redirector file */
829ac53d
FB
1242 redir_type = REDIR_ASF;
1243 } else if (match_ext(filename, "rpm,ram")) {
1244 redir_type = REDIR_RAM;
42a63c6a 1245 strcpy(filename + strlen(filename)-2, "m");
829ac53d
FB
1246 } else if (match_ext(filename, "rtsp")) {
1247 redir_type = REDIR_RTSP;
bae79c04 1248 compute_real_filename(filename, sizeof(filename) - 1);
829ac53d
FB
1249 } else if (match_ext(filename, "sdp")) {
1250 redir_type = REDIR_SDP;
bae79c04 1251 compute_real_filename(filename, sizeof(filename) - 1);
42a63c6a 1252 }
115329f1 1253
bae79c04
AB
1254 // "redirect" / request to index.html
1255 if (!strlen(filename))
f7d78f36 1256 av_strlcpy(filename, "index.html", sizeof(filename) - 1);
bae79c04 1257
85f07f22
FB
1258 stream = first_stream;
1259 while (stream != NULL) {
8256c0a3 1260 if (!strcmp(stream->filename, filename) && validate_acl(stream, c))
85f07f22
FB
1261 break;
1262 stream = stream->next;
1263 }
1264 if (stream == NULL) {
d445a7e9 1265 snprintf(msg, sizeof(msg), "File '%s' not found", url);
85f07f22
FB
1266 goto send_error;
1267 }
42a63c6a 1268
cde25790
PG
1269 c->stream = stream;
1270 memcpy(c->feed_streams, stream->feed_streams, sizeof(c->feed_streams));
1271 memset(c->switch_feed_streams, -1, sizeof(c->switch_feed_streams));
1272
1273 if (stream->stream_type == STREAM_TYPE_REDIRECT) {
1274 c->http_error = 301;
1275 q = c->buffer;
d445a7e9
PG
1276 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 301 Moved\r\n");
1277 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Location: %s\r\n", stream->feed_filename);
1278 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1279 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1280 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Moved</title></head><body>\r\n");
1281 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "You should be <a href=\"%s\">redirected</a>.\r\n", stream->feed_filename);
1282 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
cde25790
PG
1283
1284 /* prepare output buffer */
1285 c->buffer_ptr = c->buffer;
1286 c->buffer_end = q;
1287 c->state = HTTPSTATE_SEND_HEADER;
1288 return 0;
1289 }
1290
3120d2a2
PG
1291 /* If this is WMP, get the rate information */
1292 if (extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
cde25790
PG
1293 if (modify_current_stream(c, ratebuf)) {
1294 for (i = 0; i < sizeof(c->feed_streams) / sizeof(c->feed_streams[0]); i++) {
1295 if (c->switch_feed_streams[i] >= 0)
1296 do_switch_stream(c, i);
1297 }
1298 }
3120d2a2
PG
1299 }
1300
755bfeab 1301 /* If already streaming this feed, do not let start another feeder. */
d0a5513b
AB
1302 if (stream->feed_opened) {
1303 snprintf(msg, sizeof(msg), "This feed is already being received.");
1304 goto send_error;
1305 }
1306
611c5741 1307 if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE)
6edd6884 1308 current_bandwidth += stream->bandwidth;
115329f1 1309
edfdd798 1310 if (c->post == 0 && max_bandwidth < current_bandwidth) {
42a63c6a
PG
1311 c->http_error = 200;
1312 q = c->buffer;
d445a7e9
PG
1313 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 Server too busy\r\n");
1314 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: text/html\r\n");
1315 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1316 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<html><head><title>Too busy</title></head><body>\r\n");
33f5e2ec
AB
1317 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");
1318 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",
6edd6884 1319 current_bandwidth, max_bandwidth);
d445a7e9 1320 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</body></html>\r\n");
42a63c6a
PG
1321
1322 /* prepare output buffer */
1323 c->buffer_ptr = c->buffer;
1324 c->buffer_end = q;
1325 c->state = HTTPSTATE_SEND_HEADER;
1326 return 0;
1327 }
115329f1 1328
829ac53d 1329 if (redir_type != REDIR_NONE) {
7434ba6d 1330 char *hostinfo = 0;
115329f1 1331
7434ba6d
PG
1332 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1333 if (strncasecmp(p, "Host:", 5) == 0) {
1334 hostinfo = p + 5;
1335 break;
1336 }
1337 p = strchr(p, '\n');
1338 if (!p)
1339 break;
1340
1341 p++;
1342 }
1343
1344 if (hostinfo) {
1345 char *eoh;
1346 char hostbuf[260];
1347
1348 while (isspace(*hostinfo))
1349 hostinfo++;
1350
1351 eoh = strchr(hostinfo, '\n');
1352 if (eoh) {
1353 if (eoh[-1] == '\r')
1354 eoh--;
1355
1356 if (eoh - hostinfo < sizeof(hostbuf) - 1) {
1357 memcpy(hostbuf, hostinfo, eoh - hostinfo);
1358 hostbuf[eoh - hostinfo] = 0;
1359
1360 c->http_error = 200;
1361 q = c->buffer;
829ac53d
FB
1362 switch(redir_type) {
1363 case REDIR_ASX:
d445a7e9
PG
1364 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASX Follows\r\n");
1365 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1366 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1367 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ASX Version=\"3\">\r\n");
7c054ea7 1368 //q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<!-- Autogenerated by ffserver -->\r\n");
115329f1 1369 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
42a63c6a 1370 hostbuf, filename, info);
d445a7e9 1371 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</ASX>\r\n");
829ac53d
FB
1372 break;
1373 case REDIR_RAM:
d445a7e9
PG
1374 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RAM Follows\r\n");
1375 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: audio/x-pn-realaudio\r\n");
1376 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1377 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "# Autogenerated by ffserver\r\n");
115329f1 1378 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "http://%s/%s%s\r\n",
42a63c6a 1379 hostbuf, filename, info);
829ac53d
FB
1380 break;
1381 case REDIR_ASF:
d445a7e9
PG
1382 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 ASF Redirect follows\r\n");
1383 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: video/x-ms-asf\r\n");
1384 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1385 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "[Reference]\r\n");
115329f1 1386 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Ref1=http://%s/%s%s\r\n",
cde25790 1387 hostbuf, filename, info);
829ac53d
FB
1388 break;
1389 case REDIR_RTSP:
1390 {
1391 char hostname[256], *p;
1392 /* extract only hostname */
f7d78f36 1393 av_strlcpy(hostname, hostbuf, sizeof(hostname));
829ac53d
FB
1394 p = strrchr(hostname, ':');
1395 if (p)
1396 *p = '\0';
d445a7e9 1397 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 RTSP Redirect follows\r\n");
829ac53d 1398 /* XXX: incorrect mime type ? */
d445a7e9
PG
1399 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/x-rtsp\r\n");
1400 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
115329f1
DB
1401 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "rtsp://%s:%d/%s\r\n",
1402 hostname, ntohs(my_rtsp_addr.sin_port),
829ac53d
FB
1403 filename);
1404 }
1405 break;
1406 case REDIR_SDP:
1407 {
0c1a9eda 1408 uint8_t *sdp_data;
829ac53d
FB
1409 int sdp_data_size, len;
1410 struct sockaddr_in my_addr;
1411
d445a7e9
PG
1412 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
1413 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: application/sdp\r\n");
1414 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
829ac53d
FB
1415
1416 len = sizeof(my_addr);
1417 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
115329f1 1418
829ac53d 1419 /* XXX: should use a dynamic buffer */
115329f1
DB
1420 sdp_data_size = prepare_sdp_description(stream,
1421 &sdp_data,
829ac53d
FB
1422 my_addr.sin_addr);
1423 if (sdp_data_size > 0) {
1424 memcpy(q, sdp_data, sdp_data_size);
1425 q += sdp_data_size;
1426 *q = '\0';
1427 av_free(sdp_data);
1428 }
1429 }
1430 break;
1431 default:
0f4e8165 1432 abort();
829ac53d 1433 break;
2effd274 1434 }
7434ba6d
PG
1435
1436 /* prepare output buffer */
1437 c->buffer_ptr = c->buffer;
1438 c->buffer_end = q;
1439 c->state = HTTPSTATE_SEND_HEADER;
1440 return 0;
1441 }
1442 }
1443 }
1444
d445a7e9 1445 snprintf(msg, sizeof(msg), "ASX/RAM file not handled");
7434ba6d 1446 goto send_error;
85f07f22
FB
1447 }
1448
a6e14edd 1449 stream->conns_served++;
7434ba6d 1450
85f07f22
FB
1451 /* XXX: add there authenticate and IP match */
1452
edfdd798 1453 if (c->post) {
85f07f22
FB
1454 /* if post, it means a feed is being sent */
1455 if (!stream->is_feed) {
7434ba6d
PG
1456 /* However it might be a status report from WMP! Lets log the data
1457 * as it might come in handy one day
1458 */
1459 char *logline = 0;
3120d2a2 1460 int client_id = 0;
115329f1 1461
7434ba6d
PG
1462 for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
1463 if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
1464 logline = p;
1465 break;
1466 }
611c5741 1467 if (strncasecmp(p, "Pragma: client-id=", 18) == 0)
3120d2a2 1468 client_id = strtol(p + 18, 0, 10);
7434ba6d
PG
1469 p = strchr(p, '\n');
1470 if (!p)
1471 break;
1472
1473 p++;
1474 }
1475
1476 if (logline) {
1477 char *eol = strchr(logline, '\n');
1478
1479 logline += 17;
1480
1481 if (eol) {
1482 if (eol[-1] == '\r')
1483 eol--;
7906085f 1484 http_log("%.*s\n", (int) (eol - logline), logline);
7434ba6d
PG
1485 c->suppress_log = 1;
1486 }
1487 }
3120d2a2 1488
cde25790
PG
1489#ifdef DEBUG_WMP
1490 http_log("\nGot request:\n%s\n", c->buffer);
3120d2a2
PG
1491#endif
1492
1493 if (client_id && extract_rates(ratebuf, sizeof(ratebuf), c->buffer)) {
1494 HTTPContext *wmpc;
1495
1496 /* Now we have to find the client_id */
1497 for (wmpc = first_http_ctx; wmpc; wmpc = wmpc->next) {
1498 if (wmpc->wmp_client_id == client_id)
1499 break;
1500 }
1501
2d563d2f
AB
1502 if (wmpc && modify_current_stream(wmpc, ratebuf))
1503 wmpc->switch_pending = 1;
3120d2a2 1504 }
115329f1 1505
d445a7e9 1506 snprintf(msg, sizeof(msg), "POST command not handled");
cb275dd9 1507 c->stream = 0;
85f07f22
FB
1508 goto send_error;
1509 }
1510 if (http_start_receive_data(c) < 0) {
d445a7e9 1511 snprintf(msg, sizeof(msg), "could not open feed");
85f07f22
FB
1512 goto send_error;
1513 }
1514 c->http_error = 0;
1515 c->state = HTTPSTATE_RECEIVE_DATA;
1516 return 0;
1517 }
1518
cde25790 1519#ifdef DEBUG_WMP
611c5741 1520 if (strcmp(stream->filename + strlen(stream->filename) - 4, ".asf") == 0)
cde25790 1521 http_log("\nGot request:\n%s\n", c->buffer);
3120d2a2
PG
1522#endif
1523
85f07f22
FB
1524 if (c->stream->stream_type == STREAM_TYPE_STATUS)
1525 goto send_stats;
1526
1527 /* open input stream */
1528 if (open_input_stream(c, info) < 0) {
d445a7e9 1529 snprintf(msg, sizeof(msg), "Input stream corresponding to '%s' not found", url);
85f07f22
FB
1530 goto send_error;
1531 }
1532
1533 /* prepare http header */
1534 q = c->buffer;
d445a7e9 1535 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 200 OK\r\n");
85f07f22
FB
1536 mime_type = c->stream->fmt->mime_type;
1537 if (!mime_type)
087fa475 1538 mime_type = "application/x-octet-stream";
d445a7e9 1539 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Pragma: no-cache\r\n");
85f07f22
FB
1540
1541 /* for asf, we need extra headers */
8256c0a3 1542 if (!strcmp(c->stream->fmt->name,"asf_stream")) {
3120d2a2 1543 /* Need to allocate a client id */
3120d2a2 1544
1df93ae9 1545 c->wmp_client_id = av_random(&random_state) & 0x7fffffff;
3120d2a2 1546
d445a7e9 1547 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);
85f07f22 1548 }
d445a7e9
PG
1549 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-Type: %s\r\n", mime_type);
1550 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
115329f1 1551
85f07f22
FB
1552 /* prepare output buffer */
1553 c->http_error = 0;
1554 c->buffer_ptr = c->buffer;
1555 c->buffer_end = q;
1556 c->state = HTTPSTATE_SEND_HEADER;
1557 return 0;
1558 send_error:
1559 c->http_error = 404;
1560 q = c->buffer;
d445a7e9
PG
1561 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "HTTP/1.0 404 Not Found\r\n");
1562 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "Content-type: %s\r\n", "text/html");
1563 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "\r\n");
1564 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HTML>\n");
1565 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
1566 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "<BODY>%s</BODY>\n", msg);
1567 q += snprintf(q, q - (char *) c->buffer + c->buffer_size, "</HTML>\n");
85f07f22
FB
1568
1569 /* prepare output buffer */
1570 c->buffer_ptr = c->buffer;
1571 c->buffer_end = q;
1572 c->state = HTTPSTATE_SEND_HEADER;
1573 return 0;
1574 send_stats:
1575 compute_stats(c);
1576 c->http_error = 200; /* horrible : we use this value to avoid
1577 going to the send data state */
1578 c->state = HTTPSTATE_SEND_HEADER;
1579 return 0;
1580}
1581
0c1a9eda 1582static void fmt_bytecount(ByteIOContext *pb, int64_t count)
2ac887ba
PG
1583{
1584 static const char *suffix = " kMGTP";
1585 const char *s;
1586
611c5741 1587 for (s = suffix; count >= 100000 && s[1]; count /= 1000, s++);
2ac887ba 1588
4733abcb 1589 url_fprintf(pb, "%"PRId64"%c", count, *s);
2ac887ba
PG
1590}
1591
85f07f22
FB
1592static void compute_stats(HTTPContext *c)
1593{
1594 HTTPContext *c1;
1595 FFStream *stream;
2effd274 1596 char *p;
85f07f22 1597 time_t ti;
2effd274 1598 int i, len;
899681cd 1599 ByteIOContext *pb;
cde25790 1600
899681cd 1601 if (url_open_dyn_buf(&pb) < 0) {
2effd274 1602 /* XXX: return an error ? */
cde25790 1603 c->buffer_ptr = c->buffer;
2effd274
FB
1604 c->buffer_end = c->buffer;
1605 return;
cde25790 1606 }
85f07f22 1607
2effd274
FB
1608 url_fprintf(pb, "HTTP/1.0 200 OK\r\n");
1609 url_fprintf(pb, "Content-type: %s\r\n", "text/html");
1610 url_fprintf(pb, "Pragma: no-cache\r\n");
1611 url_fprintf(pb, "\r\n");
115329f1 1612
2effd274 1613 url_fprintf(pb, "<HEAD><TITLE>FFServer Status</TITLE>\n");
611c5741 1614 if (c->stream->feed_filename)
2effd274 1615 url_fprintf(pb, "<link rel=\"shortcut icon\" href=\"%s\">\n", c->stream->feed_filename);
2effd274
FB
1616 url_fprintf(pb, "</HEAD>\n<BODY>");
1617 url_fprintf(pb, "<H1>FFServer Status</H1>\n");
85f07f22 1618 /* format status */
2effd274
FB
1619 url_fprintf(pb, "<H2>Available Streams</H2>\n");
1620 url_fprintf(pb, "<TABLE cellspacing=0 cellpadding=4>\n");
1621 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");
85f07f22
FB
1622 stream = first_stream;
1623 while (stream != NULL) {
42a63c6a
PG
1624 char sfilename[1024];
1625 char *eosf;
1626
a6e14edd 1627 if (stream->feed != stream) {
f7d78f36 1628 av_strlcpy(sfilename, stream->filename, sizeof(sfilename) - 10);
a6e14edd
PG
1629 eosf = sfilename + strlen(sfilename);
1630 if (eosf - sfilename >= 4) {
611c5741 1631 if (strcmp(eosf - 4, ".asf") == 0)
a6e14edd 1632 strcpy(eosf - 4, ".asx");
611c5741 1633 else if (strcmp(eosf - 3, ".rm") == 0)
a6e14edd 1634 strcpy(eosf - 3, ".ram");
25e3e53d 1635 else if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
829ac53d
FB
1636 /* generate a sample RTSP director if
1637 unicast. Generate an SDP redirector if
1638 multicast */
2effd274
FB
1639 eosf = strrchr(sfilename, '.');
1640 if (!eosf)
1641 eosf = sfilename + strlen(sfilename);
829ac53d
FB
1642 if (stream->is_multicast)
1643 strcpy(eosf, ".sdp");
1644 else
1645 strcpy(eosf, ".rtsp");
a6e14edd 1646 }
42a63c6a 1647 }
115329f1
DB
1648
1649 url_fprintf(pb, "<TR><TD><A HREF=\"/%s\">%s</A> ",
a6e14edd 1650 sfilename, stream->filename);
2effd274 1651 url_fprintf(pb, "<td align=right> %d <td align=right> ",
2ac887ba 1652 stream->conns_served);
2effd274 1653 fmt_bytecount(pb, stream->bytes_served);
a6e14edd
PG
1654 switch(stream->stream_type) {
1655 case STREAM_TYPE_LIVE:
1656 {
1657 int audio_bit_rate = 0;
1658 int video_bit_rate = 0;
58445440
ZK
1659 const char *audio_codec_name = "";
1660 const char *video_codec_name = "";
1661 const char *audio_codec_name_extra = "";
1662 const char *video_codec_name_extra = "";
a6e14edd
PG
1663
1664 for(i=0;i<stream->nb_streams;i++) {
1665 AVStream *st = stream->streams[i];
01f4895c
MN
1666 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
1667 switch(st->codec->codec_type) {
a6e14edd 1668 case CODEC_TYPE_AUDIO:
01f4895c 1669 audio_bit_rate += st->codec->bit_rate;
a6e14edd
PG
1670 if (codec) {
1671 if (*audio_codec_name)
1672 audio_codec_name_extra = "...";
1673 audio_codec_name = codec->name;
1674 }
1675 break;
1676 case CODEC_TYPE_VIDEO:
01f4895c 1677 video_bit_rate += st->codec->bit_rate;
a6e14edd
PG
1678 if (codec) {
1679 if (*video_codec_name)
1680 video_codec_name_extra = "...";
1681 video_codec_name = codec->name;
1682 }
1683 break;
e240a0bb 1684 case CODEC_TYPE_DATA:
01f4895c 1685 video_bit_rate += st->codec->bit_rate;
e240a0bb 1686 break;
a6e14edd 1687 default:
0f4e8165 1688 abort();
79c4ea3c 1689 }
85f07f22 1690 }
115329f1 1691 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",
a6e14edd 1692 stream->fmt->name,
6edd6884 1693 stream->bandwidth,
a6e14edd
PG
1694 video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
1695 audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
611c5741 1696 if (stream->feed)
2effd274 1697 url_fprintf(pb, "<TD>%s", stream->feed->filename);
611c5741 1698 else
2effd274 1699 url_fprintf(pb, "<TD>%s", stream->feed_filename);
2effd274 1700 url_fprintf(pb, "\n");
85f07f22 1701 }
a6e14edd
PG
1702 break;
1703 default:
2effd274 1704 url_fprintf(pb, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
a6e14edd 1705 break;
85f07f22 1706 }
85f07f22
FB
1707 }
1708 stream = stream->next;
1709 }
2effd274 1710 url_fprintf(pb, "</TABLE>\n");
a6e14edd
PG
1711
1712 stream = first_stream;
1713 while (stream != NULL) {
1714 if (stream->feed == stream) {
2effd274 1715 url_fprintf(pb, "<h2>Feed %s</h2>", stream->filename);
cde25790 1716 if (stream->pid) {
2effd274 1717 url_fprintf(pb, "Running as pid %d.\n", stream->pid);
cde25790 1718
2effd274
FB
1719#if defined(linux) && !defined(CONFIG_NOCUTILS)
1720 {
1721 FILE *pid_stat;
1722 char ps_cmd[64];
1723
1724 /* This is somewhat linux specific I guess */
115329f1
DB
1725 snprintf(ps_cmd, sizeof(ps_cmd),
1726 "ps -o \"%%cpu,cputime\" --no-headers %d",
2effd274 1727 stream->pid);
115329f1 1728
2effd274
FB
1729 pid_stat = popen(ps_cmd, "r");
1730 if (pid_stat) {
1731 char cpuperc[10];
1732 char cpuused[64];
115329f1
DB
1733
1734 if (fscanf(pid_stat, "%10s %64s", cpuperc,
2effd274
FB
1735 cpuused) == 2) {
1736 url_fprintf(pb, "Currently using %s%% of the cpu. Total time used %s.\n",
1737 cpuperc, cpuused);
1738 }
1739 fclose(pid_stat);
cde25790 1740 }
cde25790
PG
1741 }
1742#endif
1743
2effd274 1744 url_fprintf(pb, "<p>");
cde25790 1745 }
2effd274 1746 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");
a6e14edd
PG
1747
1748 for (i = 0; i < stream->nb_streams; i++) {
1749 AVStream *st = stream->streams[i];
01f4895c 1750 AVCodec *codec = avcodec_find_encoder(st->codec->codec_id);
b29f97d1 1751 const char *type = "unknown";
b582f314
PG
1752 char parameters[64];
1753
1754 parameters[0] = 0;
a6e14edd 1755
01f4895c 1756 switch(st->codec->codec_type) {
a6e14edd
PG
1757 case CODEC_TYPE_AUDIO:
1758 type = "audio";
acdc8520 1759 snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate);
a6e14edd
PG
1760 break;
1761 case CODEC_TYPE_VIDEO:
1762 type = "video";
01f4895c
MN
1763 snprintf(parameters, sizeof(parameters), "%dx%d, q=%d-%d, fps=%d", st->codec->width, st->codec->height,
1764 st->codec->qmin, st->codec->qmax, st->codec->time_base.den / st->codec->time_base.num);
a6e14edd
PG
1765 break;
1766 default:
0f4e8165 1767 abort();
a6e14edd 1768 }
2effd274 1769 url_fprintf(pb, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
01f4895c 1770 i, type, st->codec->bit_rate/1000, codec ? codec->name : "", parameters);
a6e14edd 1771 }
2effd274 1772 url_fprintf(pb, "</table>\n");
a6e14edd 1773
115329f1 1774 }
a6e14edd
PG
1775 stream = stream->next;
1776 }
115329f1 1777
85f07f22
FB
1778#if 0
1779 {
1780 float avg;
1781 AVCodecContext *enc;
1782 char buf[1024];
115329f1 1783
85f07f22
FB
1784 /* feed status */
1785 stream = first_feed;
1786 while (stream != NULL) {
2effd274
FB
1787 url_fprintf(pb, "<H1>Feed '%s'</H1>\n", stream->filename);
1788 url_fprintf(pb, "<TABLE>\n");
1789 url_fprintf(pb, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
85f07f22
FB
1790 for(i=0;i<stream->nb_streams;i++) {
1791 AVStream *st = stream->streams[i];
1792 FeedData *fdata = st->priv_data;
01f4895c 1793 enc = st->codec;
115329f1 1794
85f07f22
FB
1795 avcodec_string(buf, sizeof(buf), enc);
1796 avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
1797 if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
1798 avg /= enc->frame_size;
949b1a13 1799 url_fprintf(pb, "<TR><TD>%s <TD> %d <TD> %"PRId64" <TD> %0.1f\n",
85f07f22
FB
1800 buf, enc->frame_number, fdata->data_count, avg / 1000.0);
1801 }
2effd274 1802 url_fprintf(pb, "</TABLE>\n");
85f07f22
FB
1803 stream = stream->next_feed;
1804 }
1805 }
1806#endif
1807
1808 /* connection status */
2effd274 1809 url_fprintf(pb, "<H2>Connection Status</H2>\n");
85f07f22 1810
2effd274 1811 url_fprintf(pb, "Number of connections: %d / %d<BR>\n",
85f07f22
FB
1812 nb_connections, nb_max_connections);
1813
2effd274 1814 url_fprintf(pb, "Bandwidth in use: %dk / %dk<BR>\n",
6edd6884 1815 current_bandwidth, max_bandwidth);
42a63c6a 1816
2effd274
FB
1817 url_fprintf(pb, "<TABLE>\n");
1818 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");
85f07f22
FB
1819 c1 = first_http_ctx;
1820 i = 0;
2effd274 1821 while (c1 != NULL) {
cde25790
PG
1822 int bitrate;
1823 int j;
1824
1825 bitrate = 0;
2effd274
FB
1826 if (c1->stream) {
1827 for (j = 0; j < c1->stream->nb_streams; j++) {
2d563d2f 1828 if (!c1->stream->feed)
01f4895c 1829 bitrate += c1->stream->streams[j]->codec->bit_rate;
2d563d2f
AB
1830 else if (c1->feed_streams[j] >= 0)
1831 bitrate += c1->stream->feed->streams[c1->feed_streams[j]]->codec->bit_rate;
cde25790
PG
1832 }
1833 }
1834
85f07f22
FB
1835 i++;
1836 p = inet_ntoa(c1->from_addr.sin_addr);
115329f1
DB
1837 url_fprintf(pb, "<TR><TD><B>%d</B><TD>%s%s<TD>%s<TD>%s<TD>%s<td align=right>",
1838 i,
1839 c1->stream ? c1->stream->filename : "",
2effd274 1840 c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
115329f1 1841 p,
2effd274
FB
1842 c1->protocol,
1843 http_state[c1->state]);
1844 fmt_bytecount(pb, bitrate);
1845 url_fprintf(pb, "<td align=right>");
1846 fmt_bytecount(pb, compute_datarate(&c1->datarate, c1->data_count) * 8);
1847 url_fprintf(pb, "<td align=right>");
1848 fmt_bytecount(pb, c1->data_count);
1849 url_fprintf(pb, "\n");
85f07f22
FB
1850 c1 = c1->next;
1851 }
2effd274 1852 url_fprintf(pb, "</TABLE>\n");
115329f1 1853
85f07f22
FB
1854 /* date */
1855 ti = time(NULL);
1856 p = ctime(&ti);
2effd274
FB
1857 url_fprintf(pb, "<HR size=1 noshade>Generated at %s", p);
1858 url_fprintf(pb, "</BODY>\n</HTML>\n");
85f07f22 1859
2effd274
FB
1860 len = url_close_dyn_buf(pb, &c->pb_buffer);
1861 c->buffer_ptr = c->pb_buffer;
1862 c->buffer_end = c->pb_buffer + len;
85f07f22
FB
1863}
1864
2effd274
FB
1865/* check if the parser needs to be opened for stream i */
1866static void open_parser(AVFormatContext *s, int i)
85f07f22 1867{
2effd274
FB
1868 AVStream *st = s->streams[i];
1869 AVCodec *codec;
31def229 1870
01f4895c
MN
1871 if (!st->codec->codec) {
1872 codec = avcodec_find_decoder(st->codec->codec_id);
2effd274 1873 if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
01f4895c 1874 st->codec->parse_only = 1;
611c5741 1875 if (avcodec_open(st->codec, codec) < 0)
01f4895c 1876 st->codec->parse_only = 0;
cde25790
PG
1877 }
1878 }
85f07f22
FB
1879}
1880
1881static int open_input_stream(HTTPContext *c, const char *info)
1882{
1883 char buf[128];
1884 char input_filename[1024];
1885 AVFormatContext *s;
c351cc7f 1886 int buf_size, i, ret;
0c1a9eda 1887 int64_t stream_pos;
85f07f22
FB
1888
1889 /* find file name */
1890 if (c->stream->feed) {
1891 strcpy(input_filename, c->stream->feed->feed_filename);
1892 buf_size = FFM_PACKET_SIZE;
1893 /* compute position (absolute time) */
611c5741 1894 if (find_info_tag(buf, sizeof(buf), "date", info))
f9436161 1895 {
85f07f22 1896 stream_pos = parse_date(buf, 0);
f9436161
SS
1897 if (stream_pos == INT64_MIN)
1898 return -1;
1899 }
611c5741 1900 else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
f747e6d3 1901 int prebuffer = strtol(buf, 0, 10);
0c1a9eda 1902 stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
611c5741 1903 } else
0c1a9eda 1904 stream_pos = av_gettime() - c->stream->prebuffer * (int64_t)1000;
85f07f22
FB
1905 } else {
1906 strcpy(input_filename, c->stream->feed_filename);
1907 buf_size = 0;
1908 /* compute position (relative time) */
611c5741 1909 if (find_info_tag(buf, sizeof(buf), "date", info))
f9436161 1910 {
85f07f22 1911 stream_pos = parse_date(buf, 1);
f9436161
SS
1912 if (stream_pos == INT64_MIN)
1913 return -1;
1914 }
611c5741 1915 else
85f07f22 1916 stream_pos = 0;
85f07f22
FB
1917 }
1918 if (input_filename[0] == '\0')
1919 return -1;
1920
8256c0a3
PG
1921#if 0
1922 { time_t when = stream_pos / 1000000;
949b1a13 1923 http_log("Stream pos = %"PRId64", time=%s", stream_pos, ctime(&when));
8256c0a3
PG
1924 }
1925#endif
1926
85f07f22 1927 /* open stream */
c351cc7f
BC
1928 if ((ret = av_open_input_file(&s, input_filename, c->stream->ifmt,
1929 buf_size, c->stream->ap_in)) < 0) {
1930 http_log("could not open %s: %d\n", input_filename, ret);
85f07f22 1931 return -1;
2effd274 1932 }
9dc0bc3d 1933 s->flags |= AVFMT_FLAG_GENPTS;
85f07f22 1934 c->fmt_in = s;
89da5781 1935 av_find_stream_info(c->fmt_in);
115329f1 1936
2effd274
FB
1937 /* open each parser */
1938 for(i=0;i<s->nb_streams;i++)
1939 open_parser(s, i);
1940
1941 /* choose stream as clock source (we favorize video stream if
1942 present) for packet sending */
1943 c->pts_stream_index = 0;
1944 for(i=0;i<c->stream->nb_streams;i++) {
115329f1 1945 if (c->pts_stream_index == 0 &&
01f4895c 1946 c->stream->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
2effd274
FB
1947 c->pts_stream_index = i;
1948 }
1949 }
85f07f22 1950
e8d27bc3 1951#if 1
611c5741 1952 if (c->fmt_in->iformat->read_seek)
e8d27bc3 1953 c->fmt_in->iformat->read_seek(c->fmt_in, 0, stream_pos, 0);
e240a0bb 1954#endif
2effd274
FB
1955 /* set the start time (needed for maxtime and RTP packet timing) */
1956 c->start_time = cur_time;
1957 c->first_pts = AV_NOPTS_VALUE;
85f07f22
FB
1958 return 0;
1959}
1960
e240a0bb
FB
1961/* return the server clock (in us) */
1962static int64_t get_server_clock(HTTPContext *c)
2effd274 1963{
e240a0bb 1964 /* compute current pts value from system time */
c3f58185 1965 return (cur_time - c->start_time) * 1000;
2effd274
FB
1966}
1967
e240a0bb
FB
1968/* return the estimated time at which the current packet must be sent
1969 (in us) */
1970static int64_t get_packet_send_clock(HTTPContext *c)
2effd274 1971{
e240a0bb 1972 int bytes_left, bytes_sent, frame_bytes;
115329f1 1973
e240a0bb 1974 frame_bytes = c->cur_frame_bytes;
611c5741 1975 if (frame_bytes <= 0)
e240a0bb 1976 return c->cur_pts;
611c5741 1977 else {
e240a0bb
FB
1978 bytes_left = c->buffer_end - c->buffer_ptr;
1979 bytes_sent = frame_bytes - bytes_left;
1980 return c->cur_pts + (c->cur_frame_duration * bytes_sent) / frame_bytes;
2effd274 1981 }
2effd274 1982}
2effd274 1983
2effd274 1984
2effd274
FB
1985static int http_prepare_data(HTTPContext *c)
1986{
1987 int i, len, ret;
1988 AVFormatContext *ctx;
1989
bc351386 1990 av_freep(&c->pb_buffer);
2effd274
FB
1991 switch(c->state) {
1992 case HTTPSTATE_SEND_DATA_HEADER:
1993 memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
f7d78f36
MR
1994 av_strlcpy(c->fmt_ctx.author, c->stream->author,
1995 sizeof(c->fmt_ctx.author));
1996 av_strlcpy(c->fmt_ctx.comment, c->stream->comment,
1997 sizeof(c->fmt_ctx.comment));
1998 av_strlcpy(c->fmt_ctx.copyright, c->stream->copyright,
1999 sizeof(c->fmt_ctx.copyright));
2000 av_strlcpy(c->fmt_ctx.title, c->stream->title,
2001 sizeof(c->fmt_ctx.title));
2effd274
FB
2002
2003 /* open output stream by using specified codecs */
2004 c->fmt_ctx.oformat = c->stream->fmt;
2005 c->fmt_ctx.nb_streams = c->stream->nb_streams;
2006 for(i=0;i<c->fmt_ctx.nb_streams;i++) {
2007 AVStream *st;
bb270c08 2008 AVStream *src;
2effd274 2009 st = av_mallocz(sizeof(AVStream));
8d931070 2010 st->codec= avcodec_alloc_context();
2effd274
FB
2011 c->fmt_ctx.streams[i] = st;
2012 /* if file or feed, then just take streams from FFStream struct */
115329f1 2013 if (!c->stream->feed ||
2effd274 2014 c->stream->feed == c->stream)
7c054ea7 2015 src = c->stream->streams[i];
2effd274 2016 else
7c054ea7
PG
2017 src = c->stream->feed->streams[c->stream->feed_streams[i]];
2018
bb270c08
DB
2019 *st = *src;
2020 st->priv_data = 0;
01f4895c 2021 st->codec->frame_number = 0; /* XXX: should be done in
2effd274
FB
2022 AVStream, not in codec */
2023 }
2024 c->got_key_frame = 0;
2025
2026 /* prepare header and save header data in a stream */
2027 if (url_open_dyn_buf(&c->fmt_ctx.pb) < 0) {
2028 /* XXX: potential leak */
2029 return -1;
2030 }
899681cd 2031 c->fmt_ctx.pb->is_streamed = 1;
2effd274 2032
3c27199b 2033 av_set_parameters(&c->fmt_ctx, NULL);
f75cdda7
AB
2034 if (av_write_header(&c->fmt_ctx) < 0)
2035 return -1;
2effd274 2036
899681cd 2037 len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
2effd274
FB
2038 c->buffer_ptr = c->pb_buffer;
2039 c->buffer_end = c->pb_buffer + len;
2040
2041 c->state = HTTPSTATE_SEND_DATA;
85f07f22
FB
2042 c->last_packet_sent = 0;
2043 break;
2044 case HTTPSTATE_SEND_DATA:
2045 /* find a new packet */
3b371676
BC
2046 /* read a packet from the input stream */
2047 if (c->stream->feed)
2048 ffm_set_write_index(c->fmt_in,
2049 c->stream->feed->feed_write_index,
2050 c->stream->feed->feed_size);
2051
2052 if (c->stream->max_time &&
2053 c->stream->max_time + c->start_time - cur_time < 0)
2054 /* We have timed out */
2055 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2056 else {
2057 AVPacket pkt;
2058 redo:
2059 if (av_read_frame(c->fmt_in, &pkt) < 0) {
2060 if (c->stream->feed && c->stream->feed->feed_opened) {
2061 /* if coming from feed, it means we reached the end of the
2062 ffm file, so must wait for more data */
2063 c->state = HTTPSTATE_WAIT_FEED;
2064 return 1; /* state changed */
2effd274 2065 } else {
3b371676
BC
2066 if (c->stream->loop) {
2067 av_close_input_file(c->fmt_in);
2068 c->fmt_in = NULL;
2069 if (open_input_stream(c, "") < 0)
2070 goto no_loop;
2071 goto redo;
2072 } else {
2073 no_loop:
2074 /* must send trailer now because eof or error */
2075 c->state = HTTPSTATE_SEND_DATA_TRAILER;
1bc1cfdd 2076 }
3b371676
BC
2077 }
2078 } else {
2079 /* update first pts if needed */
2080 if (c->first_pts == AV_NOPTS_VALUE) {
2081 c->first_pts = av_rescale_q(pkt.dts, c->fmt_in->streams[pkt.stream_index]->time_base, AV_TIME_BASE_Q);
2082 c->start_time = cur_time;
2083 }
2084 /* send it to the appropriate stream */
2085 if (c->stream->feed) {
2086 /* if coming from a feed, select the right stream */
2087 if (c->switch_pending) {
2088 c->switch_pending = 0;
cde25790 2089 for(i=0;i<c->stream->nb_streams;i++) {
3b371676 2090 if (c->switch_feed_streams[i] == pkt.stream_index)
611c5741 2091 if (pkt.flags & PKT_FLAG_KEY)
3b371676
BC
2092 do_switch_stream(c, i);
2093 if (c->switch_feed_streams[i] >= 0)
2094 c->switch_pending = 1;
cde25790 2095 }
3b371676
BC
2096 }
2097 for(i=0;i<c->stream->nb_streams;i++) {
2098 if (c->feed_streams[i] == pkt.stream_index) {
2099 pkt.stream_index = i;
2100 if (pkt.flags & PKT_FLAG_KEY)
2101 c->got_key_frame |= 1 << i;
2102 /* See if we have all the key frames, then
2103 * we start to send. This logic is not quite
2104 * right, but it works for the case of a
2105 * single video stream with one or more
2106 * audio streams (for which every frame is
2107 * typically a key frame).
2108 */
2109 if (!c->stream->send_on_key ||
2110 ((c->got_key_frame + 1) >> c->stream->nb_streams))
2111 goto send_it;
2112 }
2113 }
2114 } else {
2115 AVCodecContext *codec;
2116
2117 send_it:
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) {
2122 AVStream *st;
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);
e240a0bb 2129#if 0
3b371676
BC
2130 printf("index=%d pts=%0.3f duration=%0.6f\n",
2131 pkt.stream_index,
2132 (double)c->cur_pts /
2133 AV_TIME_BASE,
2134 (double)c->cur_frame_duration /
2135 AV_TIME_BASE);
e240a0bb 2136#endif
3b371676
BC
2137 /* find RTP context */
2138 c->packet_stream_index = pkt.stream_index;
2139 ctx = c->rtp_ctx[c->packet_stream_index];
2140 if(!ctx) {
8a0b55ff 2141 av_free_packet(&pkt);
3b371676 2142 break;
8a0b55ff 2143 }
3b371676
BC
2144 codec = ctx->streams[0]->codec;
2145 /* only one stream per RTP connection */
2146 pkt.stream_index = 0;
2147 } else {
2148 ctx = &c->fmt_ctx;
2149 /* Fudge here */
2150 codec = ctx->streams[pkt.stream_index]->codec;
2151 }
2152
2153 if (c->is_packetized) {
2154 int max_packet_size;
2155 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP)
2156 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
2157 else
2158 max_packet_size = url_get_max_packet_size(c->rtp_handles[c->packet_stream_index]);
2159 ret = url_open_dyn_packet_buf(&ctx->pb, max_packet_size);
2160 } else {
2161 ret = url_open_dyn_buf(&ctx->pb);
2162 }
2163 if (ret < 0) {
2164 /* XXX: potential leak */
2165 return -1;
2166 }
58bd615f 2167 c->fmt_ctx.pb->is_streamed = 1;
3b371676
BC
2168 if (pkt.dts != AV_NOPTS_VALUE)
2169 pkt.dts = av_rescale_q(pkt.dts,
2170 c->fmt_in->streams[pkt.stream_index]->time_base,
2171 ctx->streams[pkt.stream_index]->time_base);
2172 if (pkt.pts != AV_NOPTS_VALUE)
2173 pkt.pts = av_rescale_q(pkt.pts,
2174 c->fmt_in->streams[pkt.stream_index]->time_base,
2175 ctx->streams[pkt.stream_index]->time_base);
2176 if (av_write_frame(ctx, &pkt))
2177 c->state = HTTPSTATE_SEND_DATA_TRAILER;
2178
2179 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2180 c->cur_frame_bytes = len;
2181 c->buffer_ptr = c->pb_buffer;
2182 c->buffer_end = c->pb_buffer + len;
2183
2184 codec->frame_number++;
2185 if (len == 0) {
2186 av_free_packet(&pkt);
2187 goto redo;
f747e6d3 2188 }
85f07f22 2189 }
3b371676 2190 av_free_packet(&pkt);
85f07f22 2191 }
3b371676 2192 }
85f07f22
FB
2193 break;
2194 default:
2195 case HTTPSTATE_SEND_DATA_TRAILER:
2196 /* last packet test ? */
2effd274 2197 if (c->last_packet_sent || c->is_packetized)
85f07f22 2198 return -1;
2effd274 2199 ctx = &c->fmt_ctx;
85f07f22 2200 /* prepare header */
2effd274
FB
2201 if (url_open_dyn_buf(&ctx->pb) < 0) {
2202 /* XXX: potential leak */
2203 return -1;
2204 }
58bd615f 2205 c->fmt_ctx.pb->is_streamed = 1;
2effd274 2206 av_write_trailer(ctx);
899681cd 2207 len = url_close_dyn_buf(ctx->pb, &c->pb_buffer);
2effd274
FB
2208 c->buffer_ptr = c->pb_buffer;
2209 c->buffer_end = c->pb_buffer + len;
2210
85f07f22
FB
2211 c->last_packet_sent = 1;
2212 break;
2213 }
2214 return 0;
2215}
2216
2217/* should convert the format at the same time */
bc351386
FB
2218/* send data starting at c->buffer_ptr to the output connection
2219 (either UDP or TCP connection) */
5eb765ef 2220static int http_send_data(HTTPContext *c)
85f07f22 2221{
e240a0bb 2222 int len, ret;
85f07f22 2223
bc351386
FB
2224 for(;;) {
2225 if (c->buffer_ptr >= c->buffer_end) {
2226 ret = http_prepare_data(c);
2227 if (ret < 0)
2228 return -1;
611c5741 2229 else if (ret != 0)
bc351386
FB
2230 /* state change requested */
2231 break;
2effd274 2232 } else {
bc351386
FB
2233 if (c->is_packetized) {
2234 /* RTP data output */
2235 len = c->buffer_end - c->buffer_ptr;
2236 if (len < 4) {
2237 /* fail safe - should never happen */
2238 fail1:
2239 c->buffer_ptr = c->buffer_end;
2effd274
FB
2240 return 0;
2241 }
bc351386
FB
2242 len = (c->buffer_ptr[0] << 24) |
2243 (c->buffer_ptr[1] << 16) |
2244 (c->buffer_ptr[2] << 8) |
2245 (c->buffer_ptr[3]);
2246 if (len > (c->buffer_end - c->buffer_ptr))
2247 goto fail1;
e240a0bb
FB
2248 if ((get_packet_send_clock(c) - get_server_clock(c)) > 0) {
2249 /* nothing to send yet: we can wait */
2250 return 0;
2251 }
2252
2253 c->data_count += len;
2254 update_datarate(&c->datarate, c->data_count);
2255 if (c->stream)
2256 c->stream->bytes_served += len;
2257
bc351386
FB
2258 if (c->rtp_protocol == RTSP_PROTOCOL_RTP_TCP) {
2259 /* RTP packets are sent inside the RTSP TCP connection */
899681cd 2260 ByteIOContext *pb;
bc351386
FB
2261 int interleaved_index, size;
2262 uint8_t header[4];
2263 HTTPContext *rtsp_c;
115329f1 2264
bc351386
FB
2265 rtsp_c = c->rtsp_c;
2266 /* if no RTSP connection left, error */
2267 if (!rtsp_c)
2268 return -1;
2269 /* if already sending something, then wait. */
611c5741 2270 if (rtsp_c->state != RTSPSTATE_WAIT_REQUEST)
bc351386 2271 break;
899681cd 2272 if (url_open_dyn_buf(&pb) < 0)
bc351386
FB
2273 goto fail1;
2274 interleaved_index = c->packet_stream_index * 2;
2275 /* RTCP packets are sent at odd indexes */
2276 if (c->buffer_ptr[1] == 200)
2277 interleaved_index++;
2278 /* write RTSP TCP header */
2279 header[0] = '$';
2280 header[1] = interleaved_index;
2281 header[2] = len >> 8;
2282 header[3] = len;
2283 put_buffer(pb, header, 4);
2284 /* write RTP packet data */
2285 c->buffer_ptr += 4;
2286 put_buffer(pb, c->buffer_ptr, len);
2287 size = url_close_dyn_buf(pb, &c->packet_buffer);
2288 /* prepare asynchronous TCP sending */
2289 rtsp_c->packet_buffer_ptr = c->packet_buffer;
2290 rtsp_c->packet_buffer_end = c->packet_buffer + size;
e240a0bb 2291 c->buffer_ptr += len;
115329f1 2292
e240a0bb 2293 /* send everything we can NOW */
c60202df
AB
2294 len = send(rtsp_c->fd, rtsp_c->packet_buffer_ptr,
2295 rtsp_c->packet_buffer_end - rtsp_c->packet_buffer_ptr, 0);
611c5741 2296 if (len > 0)
e240a0bb 2297 rtsp_c->packet_buffer_ptr += len;
e240a0bb
FB
2298 if (rtsp_c->packet_buffer_ptr < rtsp_c->packet_buffer_end) {
2299 /* if we could not send all the data, we will
2300 send it later, so a new state is needed to
2301 "lock" the RTSP TCP connection */
2302 rtsp_c->state = RTSPSTATE_SEND_PACKET;
2303 break;
611c5741 2304 } else
e240a0bb
FB
2305 /* all data has been sent */
2306 av_freep(&c->packet_buffer);
e240a0bb
FB
2307 } else {
2308 /* send RTP packet directly in UDP */
bc351386 2309 c->buffer_ptr += 4;
115329f1 2310 url_write(c->rtp_handles[c->packet_stream_index],
bc351386 2311 c->buffer_ptr, len);
e240a0bb
FB
2312 c->buffer_ptr += len;
2313 /* here we continue as we can send several packets per 10 ms slot */
bc351386 2314 }
bc351386
FB
2315 } else {
2316 /* TCP data output */
c60202df 2317 len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
bc351386 2318 if (len < 0) {
8da4034f 2319 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
611c5741 2320 ff_neterrno() != FF_NETERROR(EINTR))
bc351386
FB
2321 /* error : close connection */
2322 return -1;
611c5741 2323 else
bc351386 2324 return 0;
611c5741 2325 } else
bc351386 2326 c->buffer_ptr += len;
611c5741 2327
e240a0bb
FB
2328 c->data_count += len;
2329 update_datarate(&c->datarate, c->data_count);
2330 if (c->stream)
2331 c->stream->bytes_served += len;
2332 break;
2effd274 2333 }
85f07f22 2334 }
bc351386 2335 } /* for(;;) */
85f07f22
FB
2336 return 0;
2337}
2338
2339static int http_start_receive_data(HTTPContext *c)
2340{
2341 int fd;
2342
2343 if (c->stream->feed_opened)
2344 return -1;
2345
e322ea48
PG
2346 /* Don't permit writing to this one */
2347 if (c->stream->readonly)
2348 return -1;
2349
85f07f22
FB
2350 /* open feed */
2351 fd = open(c->stream->feed_filename, O_RDWR);
2352 if (fd < 0)
2353 return -1;
2354 c->feed_fd = fd;
115329f1 2355
85f07f22
FB
2356 c->stream->feed_write_index = ffm_read_write_index(fd);
2357 c->stream->feed_size = lseek(fd, 0, SEEK_END);
2358 lseek(fd, 0, SEEK_SET);
2359
2360 /* init buffer input */
2361 c->buffer_ptr = c->buffer;
2362 c->buffer_end = c->buffer + FFM_PACKET_SIZE;
2363 c->stream->feed_opened = 1;
2364 return 0;
2365}
115329f1 2366
85f07f22
FB
2367static int http_receive_data(HTTPContext *c)
2368{
85f07f22
FB
2369 HTTPContext *c1;
2370
a6e14edd
PG
2371 if (c->buffer_end > c->buffer_ptr) {
2372 int len;
2373
c60202df 2374 len = recv(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
a6e14edd 2375 if (len < 0) {
8da4034f 2376 if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
611c5741 2377 ff_neterrno() != FF_NETERROR(EINTR))
a6e14edd
PG
2378 /* error : close connection */
2379 goto fail;
611c5741 2380 } else if (len == 0)
a6e14edd
PG
2381 /* end of connection : close it */
2382 goto fail;
611c5741 2383 else {
a6e14edd
PG
2384 c->buffer_ptr += len;
2385 c->data_count += len;
5eb765ef 2386 update_datarate(&c->datarate, c->data_count);
a6e14edd
PG
2387 }
2388 }
2389
d445a7e9
PG
2390 if (c->buffer_ptr - c->buffer >= 2 && c->data_count > FFM_PACKET_SIZE) {
2391 if (c->buffer[0] != 'f' ||
2392 c->buffer[1] != 'm') {
2393 http_log("Feed stream has become desynchronized -- disconnecting\n");
2394 goto fail;
2395 }
2396 }
2397
85f07f22 2398 if (c->buffer_ptr >= c->buffer_end) {
f747e6d3 2399 FFStream *feed = c->stream;
85f07f22
FB
2400 /* a packet has been received : write it in the store, except
2401 if header */
2402 if (c->data_count > FFM_PACKET_SIZE) {
115329f1 2403
949b1a13 2404 // printf("writing pos=0x%"PRIx64" size=0x%"PRIx64"\n", feed->feed_write_index, feed->feed_size);
85f07f22
FB
2405 /* XXX: use llseek or url_seek */
2406 lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
2407 write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
115329f1 2408
85f07f22
FB
2409 feed->feed_write_index += FFM_PACKET_SIZE;
2410 /* update file size */
2411 if (feed->feed_write_index > c->stream->feed_size)
2412 feed->feed_size = feed->feed_write_index;
2413
2414 /* handle wrap around if max file size reached */
6b0bdc75 2415 if (c->stream->feed_max_size && feed->feed_write_index >= c->stream->feed_max_size)
85f07f22
FB
2416 feed->feed_write_index = FFM_PACKET_SIZE;
2417
2418 /* write index */
2419 ffm_write_write_index(c->feed_fd, feed->feed_write_index);
2420
2421 /* wake up any waiting connections */
2422 for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
115329f1 2423 if (c1->state == HTTPSTATE_WAIT_FEED &&
611c5741 2424 c1->stream->feed == c->stream->feed)
85f07f22 2425 c1->state = HTTPSTATE_SEND_DATA;
85f07f22 2426 }
f747e6d3
PG
2427 } else {
2428 /* We have a header in our hands that contains useful data */
2429 AVFormatContext s;
bd7cf6ad 2430 AVInputFormat *fmt_in;
f747e6d3
PG
2431 int i;
2432
2433 memset(&s, 0, sizeof(s));
2434
899681cd 2435 url_open_buf(&s.pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
899681cd 2436 s.pb->is_streamed = 1;
f747e6d3 2437
bd7cf6ad
FB
2438 /* use feed output format name to find corresponding input format */
2439 fmt_in = av_find_input_format(feed->fmt->name);
2440 if (!fmt_in)
2441 goto fail;
2442
98486a6b
RS
2443 if (fmt_in->priv_data_size > 0) {
2444 s.priv_data = av_mallocz(fmt_in->priv_data_size);
2445 if (!s.priv_data)
2446 goto fail;
bb270c08
DB
2447 } else
2448 s.priv_data = NULL;
ec3b2232 2449
bd7cf6ad 2450 if (fmt_in->read_header(&s, 0) < 0) {
ec3b2232 2451 av_freep(&s.priv_data);
f747e6d3
PG
2452 goto fail;
2453 }
2454
2455 /* Now we have the actual streams */
2456 if (s.nb_streams != feed->nb_streams) {
ec3b2232 2457 av_freep(&s.priv_data);
f747e6d3
PG
2458 goto fail;
2459 }
611c5741 2460 for (i = 0; i < s.nb_streams; i++)
115329f1 2461 memcpy(feed->streams[i]->codec,
01f4895c 2462 s.streams[i]->codec, sizeof(AVCodecContext));
ec3b2232 2463 av_freep(&s.priv_data);
85f07f22
FB
2464 }
2465 c->buffer_ptr = c->buffer;
2466 }
2467
85f07f22
FB
2468 return 0;
2469 fail:
2470 c->stream->feed_opened = 0;
2471 close(c->feed_fd);
2472 return -1;
2473}
2474
2effd274
FB
2475/********************************************************************/
2476/* RTSP handling */
2477
2478static void rtsp_reply_header(HTTPContext *c, enum RTSPStatusCode error_number)
2479{
2480 const char *str;
2481 time_t ti;
2482 char *p;
2483 char buf2[32];
2484
2485 switch(error_number) {
7e665cd3
LA
2486 case RTSP_STATUS_OK:
2487 str = "OK";
2488 break;
2489 case RTSP_STATUS_METHOD:
2490 str = "Method Not Allowed";
2491 break;
2492 case RTSP_STATUS_BANDWIDTH:
2493 str = "Not Enough Bandwidth";
2494 break;
2495 case RTSP_STATUS_SESSION:
2496 str = "Session Not Found";
2497 break;
2498 case RTSP_STATUS_STATE:
2499 str = "Method Not Valid in This State";
2500 break;
2501 case RTSP_STATUS_AGGREGATE:
2502 str = "Aggregate operation not allowed";
2503 break;
2504 case RTSP_STATUS_ONLY_AGGREGATE:
2505 str = "Only aggregate operation allowed";
2506 break;
2507 case RTSP_STATUS_TRANSPORT:
2508 str = "Unsupported transport";
2509 break;
2510 case RTSP_STATUS_INTERNAL:
2511 str = "Internal Server Error";
2512 break;
2513 case RTSP_STATUS_SERVICE:
2514 str = "Service Unavailable";
2515 break;
2516 case RTSP_STATUS_VERSION:
2517 str = "RTSP Version not supported";
2518 break;
2effd274
FB
2519 default:
2520 str = "Unknown Error";
2521 break;
2522 }
115329f1 2523
2effd274
FB
2524 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", error_number, str);
2525 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2526
2527 /* output GMT time */
2528 ti = time(NULL);
2529 p = ctime(&ti);
2530 strcpy(buf2, p);
2531 p = buf2 + strlen(p) - 1;
2532 if (*p == '\n')
2533 *p = '\0';
2534 url_fprintf(c->pb, "Date: %s GMT\r\n", buf2);
2535}
2536
2537static void rtsp_reply_error(HTTPContext *c, enum RTSPStatusCode error_number)
2538{
2539 rtsp_reply_header(c, error_number);
2540 url_fprintf(c->pb, "\r\n");
2541}
2542
2543static int rtsp_parse_request(HTTPContext *c)
2544{
2545 const char *p, *p1, *p2;
2546 char cmd[32];
2547 char url[1024];
2548 char protocol[32];
2549 char line[1024];
2effd274
FB
2550 int len;
2551 RTSPHeader header1, *header = &header1;
115329f1 2552
2effd274
FB
2553 c->buffer_ptr[0] = '\0';
2554 p = c->buffer;
115329f1 2555
2effd274
FB
2556 get_word(cmd, sizeof(cmd), &p);
2557 get_word(url, sizeof(url), &p);
2558 get_word(protocol, sizeof(protocol), &p);
2559
f7d78f36
MR
2560 av_strlcpy(c->method, cmd, sizeof(c->method));
2561 av_strlcpy(c->url, url, sizeof(c->url));
2562 av_strlcpy(c->protocol, protocol, sizeof(c->protocol));
2effd274 2563
899681cd 2564 if (url_open_dyn_buf(&c->pb) < 0) {
2effd274
FB
2565 /* XXX: cannot do more */
2566 c->pb = NULL; /* safety */
2567 return -1;
2568 }
2569
2570 /* check version name */
2571 if (strcmp(protocol, "RTSP/1.0") != 0) {
2572 rtsp_reply_error(c, RTSP_STATUS_VERSION);
2573 goto the_end;
2574 }
2575
2576 /* parse each header line */
2577 memset(header, 0, sizeof(RTSPHeader));
2578 /* skip to next line */
2579 while (*p != '\n' && *p != '\0')
2580 p++;
2581 if (*p == '\n')
2582 p++;
2583 while (*p != '\0') {
2584 p1 = strchr(p, '\n');
2585 if (!p1)
2586 break;
2587 p2 = p1;
2588 if (p2 > p && p2[-1] == '\r')
2589 p2--;
2590 /* skip empty line */
2591 if (p2 == p)
2592 break;
2593 len = p2 - p;
2594 if (len > sizeof(line) - 1)
2595 len = sizeof(line) - 1;
2596 memcpy(line, p, len);
2597 line[len] = '\0';
2598 rtsp_parse_line(header, line);
2599 p = p1 + 1;
2600 }
2601
2602 /* handle sequence number */
2603 c->seq = header->seq;
2604
611c5741 2605 if (!strcmp(cmd, "DESCRIBE"))
2effd274 2606 rtsp_cmd_describe(c, url);
611c5741 2607 else if (!strcmp(cmd, "OPTIONS"))
0df65975 2608 rtsp_cmd_options(c, url);
611c5741 2609 else if (!strcmp(cmd, "SETUP"))
2effd274 2610 rtsp_cmd_setup(c, url, header);
611c5741 2611 else if (!strcmp(cmd, "PLAY"))
2effd274 2612 rtsp_cmd_play(c, url, header);
611c5741 2613 else if (!strcmp(cmd, "PAUSE"))
2effd274 2614 rtsp_cmd_pause(c, url, header);
611c5741 2615 else if (!strcmp(cmd, "TEARDOWN"))
2effd274 2616 rtsp_cmd_teardown(c, url, header);
611c5741 2617 else
2effd274 2618 rtsp_reply_error(c, RTSP_STATUS_METHOD);
611c5741 2619
2effd274
FB
2620 the_end:
2621 len = url_close_dyn_buf(c->pb, &c->pb_buffer);
2622 c->pb = NULL; /* safety */
2623 if (len < 0) {
2624 /* XXX: cannot do more */
2625 return -1;
2626 }
2627 c->buffer_ptr = c->pb_buffer;
2628 c->buffer_end = c->pb_buffer + len;
2629 c->state = RTSPSTATE_SEND_REPLY;
2630 return 0;
2631}
2632
115329f1 2633static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,
829ac53d 2634 struct in_addr my_ip)
2effd274 2635{
dd723472
LA
2636 AVFormatContext *avc;
2637 AVStream avs[MAX_STREAMS];
2638 int i;
115329f1 2639
dd723472
LA
2640 avc = av_alloc_format_context();
2641 if (avc == NULL) {
2effd274 2642 return -1;
dd723472
LA
2643 }
2644 if (stream->title[0] != 0) {
2645 av_strlcpy(avc->title, stream->title, sizeof(avc->title));
2646 } else {
2647 av_strlcpy(avc->title, "No Title", sizeof(avc->title));
2648 }
2649 avc->nb_streams = stream->nb_streams;
2650 if (stream->is_multicast) {
2651 snprintf(avc->filename, 1024, "rtp://%s:%d?multicast=1?ttl=%d",
2652 inet_ntoa(stream->multicast_ip),
2653 stream->multicast_port, stream->multicast_ttl);
2654 }
115329f1 2655
2effd274 2656 for(i = 0; i < stream->nb_streams; i++) {
dd723472
LA
2657 avc->streams[i] = &avs[i];
2658 avc->streams[i]->codec = stream->streams[i]->codec;
2effd274 2659 }
dd723472
LA
2660 *pbuffer = av_mallocz(2048);
2661 avf_sdp_create(&avc, 1, *pbuffer, 2048);
2662 av_free(avc);
2663
2664 return strlen(*pbuffer);
2effd274
FB
2665}
2666
0df65975
AR
2667static void rtsp_cmd_options(HTTPContext *c, const char *url)
2668{
2669// rtsp_reply_header(c, RTSP_STATUS_OK);
2670 url_fprintf(c->pb, "RTSP/1.0 %d %s\r\n", RTSP_STATUS_OK, "OK");
2671 url_fprintf(c->pb, "CSeq: %d\r\n", c->seq);
2672 url_fprintf(c->pb, "Public: %s\r\n", "OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
2673 url_fprintf(c->pb, "\r\n");
2674}
2675
2effd274
FB
2676static void rtsp_cmd_describe(HTTPContext *c, const char *url)
2677{
2678 FFStream *stream;
2679 char path1[1024];
2680 const char *path;
0c1a9eda 2681 uint8_t *content;
829ac53d
FB
2682 int content_length, len;
2683 struct sockaddr_in my_addr;
115329f1 2684
2effd274 2685 /* find which url is asked */
6ba5cbc6 2686 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2effd274
FB
2687 path = path1;
2688 if (*path == '/')
2689 path++;
2690
2691 for(stream = first_stream; stream != NULL; stream = stream->next) {
25e3e53d
LA
2692 if (!stream->is_feed &&
2693 stream->fmt && !strcmp(stream->fmt->name, "rtp") &&
2effd274
FB
2694 !strcmp(path, stream->filename)) {
2695 goto found;
2696 }
2697 }
2698 /* no stream found */
2699 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2700 return;
2701
2702 found:
2703 /* prepare the media description in sdp format */
829ac53d
FB
2704
2705 /* get the host IP */
2706 len = sizeof(my_addr);
2707 getsockname(c->fd, (struct sockaddr *)&my_addr, &len);
829ac53d 2708 content_length = prepare_sdp_description(stream, &content, my_addr.sin_addr);
2effd274
FB
2709 if (content_length < 0) {
2710 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2711 return;
2712 }
2713 rtsp_reply_header(c, RTSP_STATUS_OK);
2714 url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
2715 url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
2716 url_fprintf(c->pb, "\r\n");
2717 put_buffer(c->pb, content, content_length);
2718}
2719
2720static HTTPContext *find_rtp_session(const char *session_id)
2721{
2722 HTTPContext *c;
2723
2724 if (session_id[0] == '\0')
2725 return NULL;
2726
2727 for(c = first_http_ctx; c != NULL; c = c->next) {
2728 if (!strcmp(c->session_id, session_id))
2729 return c;
2730 }
2731 return NULL;
2732}
2733
b29f97d1 2734static RTSPTransportField *find_transport(RTSPHeader *h, enum RTSPProtocol protocol)
2effd274
FB
2735{
2736 RTSPTransportField *th;
2737 int i;
2738
2739 for(i=0;i<h->nb_transports;i++) {
2740 th = &h->transports[i];
2741 if (th->protocol == protocol)
2742 return th;
2743 }
2744 return NULL;
2745}
2746
115329f1 2747static void rtsp_cmd_setup(HTTPContext *c, const char *url,
2effd274
FB
2748 RTSPHeader *h)
2749{
2750 FFStream *stream;
2751 int stream_index, port;
2752 char buf[1024];
2753 char path1[1024];
2754 const char *path;
2755 HTTPContext *rtp_c;
2756 RTSPTransportField *th;
2757 struct sockaddr_in dest_addr;
2758 RTSPActionServerSetup setup;
115329f1 2759
2effd274 2760 /* find which url is asked */
6ba5cbc6 2761 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2effd274
FB
2762 path = path1;
2763 if (*path == '/')
2764 path++;
2765
2766 /* now check each stream */
2767 for(stream = first_stream; stream != NULL; stream = stream->next) {
25e3e53d
LA
2768 if (!stream->is_feed &&
2769 stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
2effd274
FB
2770 /* accept aggregate filenames only if single stream */
2771 if (!strcmp(path, stream->filename)) {
2772 if (stream->nb_streams != 1) {
2773 rtsp_reply_error(c, RTSP_STATUS_AGGREGATE);
2774 return;
2775 }
2776 stream_index = 0;
2777 goto found;
2778 }
115329f1 2779
2effd274
FB
2780 for(stream_index = 0; stream_index < stream->nb_streams;
2781 stream_index++) {
115329f1 2782 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2effd274
FB
2783 stream->filename, stream_index);
2784 if (!strcmp(path, buf))
2785 goto found;
2786 }
2787 }
2788 }
2789 /* no stream found */
2790 rtsp_reply_error(c, RTSP_STATUS_SERVICE); /* XXX: right error ? */
2791 return;
2792 found:
2793
2794 /* generate session id if needed */
611c5741 2795 if (h->session_id[0] == '\0')
1df93ae9
AB
2796 snprintf(h->session_id, sizeof(h->session_id), "%08x%08x",
2797 av_random(&random_state), av_random(&random_state));
2effd274
FB
2798
2799 /* find rtp session, and create it if none found */
2800 rtp_c = find_rtp_session(h->session_id);
2801 if (!rtp_c) {
bc351386
FB
2802 /* always prefer UDP */
2803 th = find_transport(h, RTSP_PROTOCOL_RTP_UDP);
2804 if (!th) {
2805 th = find_transport(h, RTSP_PROTOCOL_RTP_TCP);
2806 if (!th) {
2807 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2808 return;
2809 }
2810 }
2811
2812 rtp_c = rtp_new_connection(&c->from_addr, stream, h->session_id,
2813 th->protocol);
2effd274
FB
2814 if (!rtp_c) {
2815 rtsp_reply_error(c, RTSP_STATUS_BANDWIDTH);
2816 return;
2817 }
2818
2819 /* open input stream */
2820 if (open_input_stream(rtp_c, "") < 0) {
2821 rtsp_reply_error(c, RTSP_STATUS_INTERNAL);
2822 return;
2823 }
2effd274 2824 }
115329f1 2825
2effd274
FB
2826 /* test if stream is OK (test needed because several SETUP needs
2827 to be done for a given file) */
2828 if (rtp_c->stream != stream) {
2829 rtsp_reply_error(c, RTSP_STATUS_SERVICE);
2830 return;
2831 }
115329f1 2832
2effd274
FB
2833 /* test if stream is already set up */
2834 if (rtp_c->rtp_ctx[stream_index]) {
2835 rtsp_reply_error(c, RTSP_STATUS_STATE);
2836 return;
2837 }
2838
2839 /* check transport */
2840 th = find_transport(h, rtp_c->rtp_protocol);
115329f1 2841 if (!th || (th->protocol == RTSP_PROTOCOL_RTP_UDP &&
2effd274
FB
2842 th->client_port_min <= 0)) {
2843 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2844 return;
2845 }
2846
2847 /* setup default options */
2848 setup.transport_option[0] = '\0';
2849 dest_addr = rtp_c->from_addr;
2850 dest_addr.sin_port = htons(th->client_port_min);
115329f1 2851
2effd274 2852 /* setup stream */
bc351386 2853 if (rtp_new_av_stream(rtp_c, stream_index, &dest_addr, c) < 0) {
2effd274
FB
2854 rtsp_reply_error(c, RTSP_STATUS_TRANSPORT);
2855 return;
2856 }
2857
2858 /* now everything is OK, so we can send the connection parameters */
2859 rtsp_reply_header(c, RTSP_STATUS_OK);
2860 /* session ID */
2861 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2862
2863 switch(rtp_c->rtp_protocol) {
2864 case RTSP_PROTOCOL_RTP_UDP:
2865 port = rtp_get_local_port(rtp_c->rtp_handles[stream_index]);
2866 url_fprintf(c->pb, "Transport: RTP/AVP/UDP;unicast;"
2867 "client_port=%d-%d;server_port=%d-%d",
2868 th->client_port_min, th->client_port_min + 1,
2869 port, port + 1);
2870 break;
2871 case RTSP_PROTOCOL_RTP_TCP:
2872 url_fprintf(c->pb, "Transport: RTP/AVP/TCP;interleaved=%d-%d",
2873 stream_index * 2, stream_index * 2 + 1);
2874 break;
2875 default:
2876 break;
2877 }
611c5741 2878 if (setup.transport_option[0] != '\0')
2effd274 2879 url_fprintf(c->pb, ";%s", setup.transport_option);
2effd274 2880 url_fprintf(c->pb, "\r\n");
115329f1 2881
2effd274
FB
2882
2883 url_fprintf(c->pb, "\r\n");
2884}
2885
2886
2887/* find an rtp connection by using the session ID. Check consistency
2888 with filename */
115329f1 2889static HTTPContext *find_rtp_session_with_url(const char *url,
2effd274
FB
2890 const char *session_id)
2891{
2892 HTTPContext *rtp_c;
2893 char path1[1024];
2894 const char *path;
94d9ad5f
GF
2895 char buf[1024];
2896 int s;
2effd274
FB
2897
2898 rtp_c = find_rtp_session(session_id);
2899 if (!rtp_c)
2900 return NULL;
2901
2902 /* find which url is asked */
6ba5cbc6 2903 url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
2effd274
FB
2904 path = path1;
2905 if (*path == '/')
2906 path++;
94d9ad5f
GF
2907 if(!strcmp(path, rtp_c->stream->filename)) return rtp_c;
2908 for(s=0; s<rtp_c->stream->nb_streams; ++s) {
2909 snprintf(buf, sizeof(buf), "%s/streamid=%d",
2910 rtp_c->stream->filename, s);
2911 if(!strncmp(path, buf, sizeof(buf))) {
2912 // XXX: Should we reply with RTSP_STATUS_ONLY_AGGREGATE if nb_streams>1?
2913 return rtp_c;
2914 }
2915 }
2916 return NULL;
2effd274
FB
2917}
2918
2919static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPHeader *h)
2920{
2921 HTTPContext *rtp_c;
2922
2923 rtp_c = find_rtp_session_with_url(url, h->session_id);
2924 if (!rtp_c) {
2925 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2926 return;
2927 }
115329f1 2928
2effd274
FB
2929 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
2930 rtp_c->state != HTTPSTATE_WAIT_FEED &&
2931 rtp_c->state != HTTPSTATE_READY) {
2932 rtsp_reply_error(c, RTSP_STATUS_STATE);
2933 return;
2934 }
2935
e240a0bb
FB
2936#if 0
2937 /* XXX: seek in stream */
2938 if (h->range_start != AV_NOPTS_VALUE) {
2939 printf("range_start=%0.3f\n", (double)h->range_start / AV_TIME_BASE);
2940 av_seek_frame(rtp_c->fmt_in, -1, h->range_start);
2941 }
2942#endif
2943
2effd274 2944 rtp_c->state = HTTPSTATE_SEND_DATA;
115329f1 2945
2effd274
FB
2946 /* now everything is OK, so we can send the connection parameters */
2947 rtsp_reply_header(c, RTSP_STATUS_OK);
2948 /* session ID */
2949 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2950 url_fprintf(c->pb, "\r\n");
2951}
2952
2953static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPHeader *h)
2954{
2955 HTTPContext *rtp_c;
2956
2957 rtp_c = find_rtp_session_with_url(url, h->session_id);
2958 if (!rtp_c) {
2959 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2960 return;
2961 }
115329f1 2962
2effd274
FB
2963 if (rtp_c->state != HTTPSTATE_SEND_DATA &&
2964 rtp_c->state != HTTPSTATE_WAIT_FEED) {
2965 rtsp_reply_error(c, RTSP_STATUS_STATE);
2966 return;
2967 }
115329f1 2968
2effd274 2969 rtp_c->state = HTTPSTATE_READY;
1bc1cfdd 2970 rtp_c->first_pts = AV_NOPTS_VALUE;
2effd274
FB
2971 /* now everything is OK, so we can send the connection parameters */
2972 rtsp_reply_header(c, RTSP_STATUS_OK);
2973 /* session ID */
2974 url_fprintf(c->pb, "Session: %s\r\n", rtp_c->session_id);
2975 url_fprintf(c->pb, "\r\n");
2976}
2977
2978static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPHeader *h)
2979{
2980 HTTPContext *rtp_c;
b0b2faa7 2981 char session_id[32];
2effd274
FB
2982
2983 rtp_c = find_rtp_session_with_url(url, h->session_id);
2984 if (!rtp_c) {
2985 rtsp_reply_error(c, RTSP_STATUS_SESSION);
2986 return;
2987 }
115329f1 2988
f7d78f36 2989 av_strlcpy(session_id, rtp_c->session_id, sizeof(session_id));
b0b2faa7 2990
2effd274
FB
2991 /* abort the session */
2992 close_connection(rtp_c);
2993
2effd274
FB
2994 /* now everything is OK, so we can send the connection parameters */
2995 rtsp_reply_header(c, RTSP_STATUS_OK);
2996 /* session ID */
b0b2faa7 2997 url_fprintf(c->pb, "Session: %s\r\n", session_id);
2effd274
FB
2998 url_fprintf(c->pb, "\r\n");
2999}
3000
3001
3002/********************************************************************/
3003/* RTP handling */
3004
115329f1 3005static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,
bc351386
FB
3006 FFStream *stream, const char *session_id,
3007 enum RTSPProtocol rtp_protocol)
2effd274
FB
3008{
3009 HTTPContext *c = NULL;
bc351386 3010 const char *proto_str;
115329f1 3011
2effd274
FB
3012 /* XXX: should output a warning page when coming
3013 close to the connection limit */
3014 if (nb_connections >= nb_max_connections)
3015 goto fail;
115329f1 3016
2effd274
FB
3017 /* add a new connection */
3018 c = av_mallocz(sizeof(HTTPContext));
3019 if (!c)
3020 goto fail;
115329f1 3021
2effd274
FB
3022 c->fd = -1;
3023 c->poll_entry = NULL;
6edd6884 3024 c->from_addr = *from_addr;
2effd274
FB
3025 c->buffer_size = IOBUFFER_INIT_SIZE;
3026 c->buffer = av_malloc(c->buffer_size);
3027 if (!c->buffer)
3028 goto fail;
3029 nb_connections++;
3030 c->stream = stream;
f7d78f36 3031 av_strlcpy(c->session_id, session_id, sizeof(c->session_id));
2effd274
FB
3032 c->state = HTTPSTATE_READY;
3033 c->is_packetized = 1;
bc351386
FB
3034 c->rtp_protocol = rtp_protocol;
3035
2effd274 3036 /* protocol is shown in statistics */
bc351386
FB
3037 switch(c->rtp_protocol) {
3038 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3039 proto_str = "MCAST";
3040 break;
3041 case RTSP_PROTOCOL_RTP_UDP:
3042 proto_str = "UDP";
3043 break;
3044 case RTSP_PROTOCOL_RTP_TCP:
3045 proto_str = "TCP";
3046 break;
3047 default:
3048 proto_str = "???";
3049 break;
3050 }
f7d78f36
MR
3051 av_strlcpy(c->protocol, "RTP/", sizeof(c->protocol));
3052 av_strlcat(c->protocol, proto_str, sizeof(c->protocol));
2effd274 3053
6edd6884
FB
3054 current_bandwidth += stream->bandwidth;
3055
2effd274
FB
3056 c->next = first_http_ctx;
3057 first_http_ctx = c;
3058 return c;
115329f1 3059
2effd274
FB
3060 fail:
3061 if (c) {
3062 av_free(c->buffer);
3063 av_free(c);
3064 }
3065 return NULL;
3066}
3067
3068/* add a new RTP stream in an RTP connection (used in RTSP SETUP
bc351386 3069 command). If RTP/TCP protocol is used, TCP connection 'rtsp_c' is
2effd274 3070 used. */
115329f1 3071static int rtp_new_av_stream(HTTPContext *c,
bc351386
FB
3072 int stream_index, struct sockaddr_in *dest_addr,
3073 HTTPContext *rtsp_c)
2effd274
FB
3074{
3075 AVFormatContext *ctx;
3076 AVStream *st;
3077 char *ipaddr;
75480e86 3078 URLContext *h = NULL;
0c1a9eda 3079 uint8_t *dummy_buf;
6edd6884 3080 char buf2[32];
bc351386 3081 int max_packet_size;
115329f1 3082
2effd274 3083 /* now we can open the relevant output stream */
bc874dae 3084 ctx = av_alloc_format_context();
2effd274
FB
3085 if (!ctx)
3086 return -1;
b156b88c 3087 ctx->oformat = guess_format("rtp", NULL, NULL);
2effd274
FB
3088
3089 st = av_mallocz(sizeof(AVStream));
3090 if (!st)
3091 goto fail;
8d931070 3092 st->codec= avcodec_alloc_context();
2effd274
FB
3093 ctx->nb_streams = 1;
3094 ctx->streams[0] = st;
3095
115329f1 3096 if (!c->stream->feed ||
611c5741 3097 c->stream->feed == c->stream)
2effd274 3098 memcpy(st, c->stream->streams[stream_index], sizeof(AVStream));
611c5741 3099 else
115329f1 3100 memcpy(st,
2effd274
FB
3101 c->stream->feed->streams[c->stream->feed_streams[stream_index]],
3102 sizeof(AVStream));
57dbe08b 3103 st->priv_data = NULL;
115329f1 3104
bc351386
FB
3105 /* build destination RTP address */
3106 ipaddr = inet_ntoa(dest_addr->sin_addr);
3107
3108 switch(c->rtp_protocol) {
3109 case RTSP_PROTOCOL_RTP_UDP:
3110 case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
3111 /* RTP/UDP case */
115329f1 3112
6edd6884
FB
3113 /* XXX: also pass as parameter to function ? */
3114 if (c->stream->is_multicast) {
3115 int ttl;
3116 ttl = c->stream->multicast_ttl;
3117 if (!ttl)
3118 ttl = 16;
3119 snprintf(ctx->filename, sizeof(ctx->filename),
115329f1 3120 "rtp://%s:%d?multicast=1&ttl=%d",
6edd6884
FB
3121 ipaddr, ntohs(dest_addr->sin_port), ttl);
3122 } else {
3123 snprintf(ctx->filename, sizeof(ctx->filename),
3124 "rtp://%s:%d", ipaddr, ntohs(dest_addr->sin_port));
3125 }
2effd274
FB
3126
3127 if (url_open(&h, ctx->filename, URL_WRONLY) < 0)
3128 goto fail;
3129 c->rtp_handles[stream_index] = h;
bc351386
FB
3130 max_packet_size = url_get_max_packet_size(h);
3131 break;
3132 case RTSP_PROTOCOL_RTP_TCP:
3133 /* RTP/TCP case */
3134 c->rtsp_c = rtsp_c;
3135 max_packet_size = RTSP_TCP_MAX_PACKET_SIZE;
3136 break;
3137 default:
2effd274
FB
3138 goto fail;
3139 }
3140
bc351386 3141 http_log("%s:%d - - [%s] \"PLAY %s/streamid=%d %s\"\n",
115329f1
DB
3142 ipaddr, ntohs(dest_addr->sin_port),
3143 ctime1(buf2),
bc351386 3144 c->stream->filename, stream_index, c->protocol);
6edd6884 3145
2effd274 3146 /* normally, no packets should be output here, but the packet size may be checked */
bc351386 3147 if (url_open_dyn_packet_buf(&ctx->pb, max_packet_size) < 0) {
2effd274
FB
3148 /* XXX: close stream */
3149 goto fail;
3150 }
3c27199b 3151 av_set_parameters(ctx, NULL);
2effd274
FB
3152 if (av_write_header(ctx) < 0) {
3153 fail:
3154 if (h)
3155 url_close(h);
3156 av_free(ctx);
3157 return -1;
3158 }
899681cd 3159 url_close_dyn_buf(ctx->pb, &dummy_buf);
2effd274 3160 av_free(dummy_buf);
115329f1 3161
2effd274
FB
3162 c->rtp_ctx[stream_index] = ctx;
3163 return 0;
3164}
3165
3166/********************************************************************/
3167/* ffserver initialization */
3168
b29f97d1 3169static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
2effd274
FB
3170{
3171 AVStream *fst;
3172
3173 fst = av_mallocz(sizeof(AVStream));
3174 if (!fst)
3175 return NULL;
8d931070 3176 fst->codec= avcodec_alloc_context();
2effd274 3177 fst->priv_data = av_mallocz(sizeof(FeedData));
01f4895c 3178 memcpy(fst->codec, codec, sizeof(AVCodecContext));
d445a7e9 3179 fst->index = stream->nb_streams;
7c054ea7 3180 av_set_pts_info(fst, 33, 1, 90000);
2effd274
FB
3181 stream->streams[stream->nb_streams++] = fst;
3182 return fst;
3183}
3184
85f07f22 3185/* return the stream number in the feed */
b29f97d1 3186static int add_av_stream(FFStream *feed, AVStream *st)
85f07f22
FB
3187{
3188 AVStream *fst;
3189 AVCodecContext *av, *av1;
3190 int i;
3191
01f4895c 3192 av = st->codec;
85f07f22
FB
3193 for(i=0;i<feed->nb_streams;i++) {
3194 st = feed->streams[i];
01f4895c 3195 av1 = st->codec;
f747e6d3
PG
3196 if (av1->codec_id == av->codec_id &&
3197 av1->codec_type == av->codec_type &&
85f07f22
FB
3198 av1->bit_rate == av->bit_rate) {
3199
3200 switch(av->codec_type) {
3201 case CODEC_TYPE_AUDIO:
3202 if (av1->channels == av->channels &&
3203 av1->sample_rate == av->sample_rate)
3204 goto found;
3205 break;
3206 case CODEC_TYPE_VIDEO:
3207 if (av1->width == av->width &&
3208 av1->height == av->height &&
c0df9d75
MN
3209 av1->time_base.den == av->time_base.den &&
3210 av1->time_base.num == av->time_base.num &&
85f07f22
FB
3211 av1->gop_size == av->gop_size)
3212 goto found;
3213 break;
f747e6d3 3214 default:
0f4e8165 3215 abort();
85f07f22
FB
3216 }
3217 }
3218 }
115329f1 3219
2effd274 3220 fst = add_av_stream1(feed, av);
85f07f22
FB
3221 if (!fst)
3222 return -1;
85f07f22
FB
3223 return feed->nb_streams - 1;
3224 found:
3225 return i;
3226}
3227
b29f97d1 3228static void remove_stream(FFStream *stream)
2effd274
FB
3229{
3230 FFStream **ps;
3231 ps = &first_stream;
3232 while (*ps != NULL) {
611c5741 3233 if (*ps == stream)
2effd274 3234 *ps = (*ps)->next;
611c5741 3235 else
2effd274 3236 ps = &(*ps)->next;
2effd274
FB
3237 }
3238}
3239
0fa45e19 3240/* specific mpeg4 handling : we extract the raw parameters */
b29f97d1 3241static void extract_mpeg4_header(AVFormatContext *infile)
0fa45e19
FB
3242{
3243 int mpeg4_count, i, size;
3244 AVPacket pkt;
3245 AVStream *st;
0c1a9eda 3246 const uint8_t *p;
0fa45e19
FB
3247
3248 mpeg4_count = 0;
3249 for(i=0;i<infile->nb_streams;i++) {
3250 st = infile->streams[i];
01f4895c
MN
3251 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3252 st->codec->extradata_size == 0) {
0fa45e19
FB
3253 mpeg4_count++;
3254 }
3255 }
3256 if (!mpeg4_count)
3257 return;
3258
d445a7e9 3259 printf("MPEG4 without extra data: trying to find header in %s\n", infile->filename);
0fa45e19
FB
3260 while (mpeg4_count > 0) {
3261 if (av_read_packet(infile, &pkt) < 0)
3262 break;
3263 st = infile->streams[pkt.stream_index];
01f4895c
MN
3264 if (st->codec->codec_id == CODEC_ID_MPEG4 &&
3265 st->codec->extradata_size == 0) {
3266 av_freep(&st->codec->extradata);
0fa45e19
FB
3267 /* fill extradata with the header */
3268 /* XXX: we make hard suppositions here ! */
3269 p = pkt.data;
3270 while (p < pkt.data + pkt.size - 4) {
3271 /* stop when vop header is found */
115329f1 3272 if (p[0] == 0x00 && p[1] == 0x00 &&
0fa45e19
FB
3273 p[2] == 0x01 && p[3] == 0xb6) {
3274 size = p - pkt.data;
750f0e1f 3275 // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size);
01f4895c
MN
3276 st->codec->extradata = av_malloc(size);
3277 st->codec->extradata_size = size;
3278 memcpy(st->codec->extradata, pkt.data, size);
0fa45e19
FB
3279 break;
3280 }
3281 p++;
3282 }
3283 mpeg4_count--;
3284 }
3285 av_free_packet(&pkt);
3286 }
3287}
3288
2effd274 3289/* compute the needed AVStream for each file */
b29f97d1 3290static void build_file_streams(void)
2effd274
FB
3291{
3292 FFStream *stream, *stream_next;
3293 AVFormatContext *infile;
f61d45c9 3294 int i, ret;
2effd274
FB
3295
3296 /* gather all streams */
3297 for(stream = first_stream; stream != NULL; stream = stream_next) {
3298 stream_next = stream->next;
3299 if (stream->stream_type == STREAM_TYPE_LIVE &&
3300 !stream->feed) {
3301 /* the stream comes from a file */
3302 /* try to open the file */
3303 /* open stream */
e240a0bb 3304 stream->ap_in = av_mallocz(sizeof(AVFormatParameters));
25e3e53d 3305 if (stream->fmt && !strcmp(stream->fmt->name, "rtp")) {
e240a0bb
FB
3306 /* specific case : if transport stream output to RTP,
3307 we use a raw transport stream reader */
3308 stream->ap_in->mpeg2ts_raw = 1;
3309 stream->ap_in->mpeg2ts_compute_pcr = 1;
3310 }
115329f1 3311
f61d45c9
BC
3312 if ((ret = av_open_input_file(&infile, stream->feed_filename,
3313 stream->ifmt, 0, stream->ap_in)) < 0) {
3314 http_log("could not open %s: %d\n", stream->feed_filename, ret);
2effd274
FB
3315 /* remove stream (no need to spend more time on it) */
3316 fail:
3317 remove_stream(stream);
3318 } else {
3319 /* find all the AVStreams inside and reference them in
3320 'stream' */
3321 if (av_find_stream_info(infile) < 0) {
115329f1 3322 http_log("Could not find codec parameters from '%s'",
2effd274
FB
3323 stream->feed_filename);
3324 av_close_input_file(infile);
3325 goto fail;
3326 }
0fa45e19
FB
3327 extract_mpeg4_header(infile);
3328
611c5741 3329 for(i=0;i<infile->nb_streams;i++)
01f4895c 3330 add_av_stream1(stream, infile->streams[i]->codec);
611c5741 3331
2effd274
FB
3332 av_close_input_file(infile);
3333 }
3334 }
3335 }
3336}
3337
85f07f22 3338/* compute the needed AVStream for each feed */
b29f97d1 3339static void build_feed_streams(void)
85f07f22
FB
3340{
3341 FFStream *stream, *feed;
3342 int i;
3343
3344 /* gather all streams */
3345 for(stream = first_stream; stream != NULL; stream = stream->next) {
3346 feed = stream->feed;
3347 if (feed) {
3348 if (!stream->is_feed) {
2effd274 3349 /* we handle a stream coming from a feed */
611c5741 3350 for(i=0;i<stream->nb_streams;i++)
85f07f22 3351 stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
cde25790
PG
3352 }
3353 }
3354 }
3355
3356 /* gather all streams */
3357 for(stream = first_stream; stream != NULL; stream = stream->next) {
3358 feed = stream->feed;
3359 if (feed) {
3360 if (stream->is_feed) {
611c5741 3361 for(i=0;i<stream->nb_streams;i++)
85f07f22 3362 stream->feed_streams[i] = i;
85f07f22
FB
3363 }
3364 }
3365 }
3366
3367 /* create feed files if needed */
3368 for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
3369 int fd;
3370
59eb2ed1
PG
3371 if (url_exist(feed->feed_filename)) {
3372 /* See if it matches */
3373 AVFormatContext *s;
3374 int matches = 0;
3375
3376 if (av_open_input_file(&s, feed->feed_filename, NULL, FFM_PACKET_SIZE, NULL) >= 0) {
3377 /* Now see if it matches */
3378 if (s->nb_streams == feed->nb_streams) {
3379 matches = 1;
3380 for(i=0;i<s->nb_streams;i++) {
3381 AVStream *sf, *ss;
3382 sf = feed->streams[i];
3383 ss = s->streams[i];
3384
3385 if (sf->index != ss->index ||
3386 sf->id != ss->id) {
115329f1 3387 printf("Index & Id do not match for stream %d (%s)\n",
e240a0bb 3388 i, feed->feed_filename);
59eb2ed1
PG
3389 matches = 0;
3390 } else {
3391 AVCodecContext *ccf, *ccs;
3392
01f4895c
MN
3393 ccf = sf->codec;
3394 ccs = ss->codec;
59eb2ed1
PG
3395#define CHECK_CODEC(x) (ccf->x != ccs->x)
3396
3397 if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
3398 printf("Codecs do not match for stream %d\n", i);
3399 matches = 0;
3400 } else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
3401 printf("Codec bitrates do not match for stream %d\n", i);
3402 matches = 0;
3403 } else if (ccf->codec_type == CODEC_TYPE_VIDEO) {
c0df9d75
MN
3404 if (CHECK_CODEC(time_base.den) ||
3405 CHECK_CODEC(time_base.num) ||
59eb2ed1
PG
3406 CHECK_CODEC(width) ||
3407 CHECK_CODEC(height)) {
3408 printf("Codec width, height and framerate do not match for stream %d\n", i);