You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2062 lines
66KB

  1. /*
  2. * Multiple format streaming server
  3. * Copyright (c) 2000, 2001, 2002 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <netinet/in.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/poll.h>
  28. #include <errno.h>
  29. #include <sys/time.h>
  30. #include <time.h>
  31. #include <getopt.h>
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <arpa/inet.h>
  35. #include <netdb.h>
  36. #include <ctype.h>
  37. #include <signal.h>
  38. #include <assert.h>
  39. #include "bswap.h" // needed for the bitstream writer in common.h which is included in avformat.h
  40. #include "avformat.h"
  41. /* maximum number of simultaneous HTTP connections */
  42. #define HTTP_MAX_CONNECTIONS 2000
  43. enum HTTPState {
  44. HTTPSTATE_WAIT_REQUEST,
  45. HTTPSTATE_SEND_HEADER,
  46. HTTPSTATE_SEND_DATA_HEADER,
  47. HTTPSTATE_SEND_DATA,
  48. HTTPSTATE_SEND_DATA_TRAILER,
  49. HTTPSTATE_RECEIVE_DATA,
  50. HTTPSTATE_WAIT_FEED,
  51. };
  52. const char *http_state[] = {
  53. "WAIT_REQUEST",
  54. "SEND_HEADER",
  55. "SEND_DATA_HEADER",
  56. "SEND_DATA",
  57. "SEND_DATA_TRAILER",
  58. "RECEIVE_DATA",
  59. "WAIT_FEED",
  60. };
  61. #define IOBUFFER_MAX_SIZE 32768
  62. #define PACKET_MAX_SIZE 16384
  63. /* coef for exponential mean for bitrate estimation in statistics */
  64. #define AVG_COEF 0.9
  65. /* timeouts are in ms */
  66. #define REQUEST_TIMEOUT (15 * 1000)
  67. #define SYNC_TIMEOUT (10 * 1000)
  68. /* context associated with one connection */
  69. typedef struct HTTPContext {
  70. enum HTTPState state;
  71. int fd; /* socket file descriptor */
  72. struct sockaddr_in from_addr; /* origin */
  73. struct pollfd *poll_entry; /* used when polling */
  74. long timeout;
  75. UINT8 *buffer_ptr, *buffer_end;
  76. int http_error;
  77. struct HTTPContext *next;
  78. int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
  79. INT64 data_count;
  80. /* feed input */
  81. int feed_fd;
  82. /* input format handling */
  83. AVFormatContext *fmt_in;
  84. /* output format handling */
  85. struct FFStream *stream;
  86. AVFormatContext fmt_ctx;
  87. int last_packet_sent; /* true if last data packet was sent */
  88. int suppress_log;
  89. int bandwidth;
  90. time_t start_time;
  91. char protocol[16];
  92. char method[16];
  93. char url[128];
  94. UINT8 buffer[IOBUFFER_MAX_SIZE];
  95. UINT8 pbuffer[PACKET_MAX_SIZE];
  96. } HTTPContext;
  97. /* each generated stream is described here */
  98. enum StreamType {
  99. STREAM_TYPE_LIVE,
  100. STREAM_TYPE_STATUS,
  101. };
  102. /* description of each stream of the ffserver.conf file */
  103. typedef struct FFStream {
  104. enum StreamType stream_type;
  105. char filename[1024]; /* stream filename */
  106. struct FFStream *feed;
  107. AVOutputFormat *fmt;
  108. int nb_streams;
  109. int prebuffer; /* Number of millseconds early to start */
  110. time_t max_time;
  111. int send_on_key;
  112. AVStream *streams[MAX_STREAMS];
  113. int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
  114. char feed_filename[1024]; /* file name of the feed storage, or
  115. input file name for a stream */
  116. struct FFStream *next;
  117. /* feed specific */
  118. int feed_opened; /* true if someone if writing to feed */
  119. int is_feed; /* true if it is a feed */
  120. int conns_served;
  121. INT64 bytes_served;
  122. INT64 feed_max_size; /* maximum storage size */
  123. INT64 feed_write_index; /* current write position in feed (it wraps round) */
  124. INT64 feed_size; /* current size of feed */
  125. struct FFStream *next_feed;
  126. } FFStream;
  127. typedef struct FeedData {
  128. long long data_count;
  129. float avg_frame_size; /* frame size averraged over last frames with exponential mean */
  130. } FeedData;
  131. struct sockaddr_in my_addr;
  132. char logfilename[1024];
  133. HTTPContext *first_http_ctx;
  134. FFStream *first_feed; /* contains only feeds */
  135. FFStream *first_stream; /* contains all streams, including feeds */
  136. static int handle_http(HTTPContext *c, long cur_time);
  137. static int http_parse_request(HTTPContext *c);
  138. static int http_send_data(HTTPContext *c);
  139. static void compute_stats(HTTPContext *c);
  140. static int open_input_stream(HTTPContext *c, const char *info);
  141. static int http_start_receive_data(HTTPContext *c);
  142. static int http_receive_data(HTTPContext *c);
  143. int nb_max_connections;
  144. int nb_connections;
  145. int nb_max_bandwidth;
  146. int nb_bandwidth;
  147. static long gettime_ms(void)
  148. {
  149. struct timeval tv;
  150. gettimeofday(&tv,NULL);
  151. return (long long)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
  152. }
  153. static FILE *logfile = NULL;
  154. static void http_log(char *fmt, ...)
  155. {
  156. va_list ap;
  157. va_start(ap, fmt);
  158. if (logfile) {
  159. vfprintf(logfile, fmt, ap);
  160. fflush(logfile);
  161. }
  162. va_end(ap);
  163. }
  164. static void log_connection(HTTPContext *c)
  165. {
  166. char buf1[32], buf2[32], *p;
  167. time_t ti;
  168. if (c->suppress_log)
  169. return;
  170. /* XXX: reentrant function ? */
  171. p = inet_ntoa(c->from_addr.sin_addr);
  172. strcpy(buf1, p);
  173. ti = time(NULL);
  174. p = ctime(&ti);
  175. strcpy(buf2, p);
  176. p = buf2 + strlen(p) - 1;
  177. if (*p == '\n')
  178. *p = '\0';
  179. http_log("%s - - [%s] \"%s %s %s\" %d %lld\n",
  180. buf1, buf2, c->method, c->url, c->protocol, (c->http_error ? c->http_error : 200), c->data_count);
  181. }
  182. /* main loop of the http server */
  183. static int http_server(struct sockaddr_in my_addr)
  184. {
  185. int server_fd, tmp, ret;
  186. struct sockaddr_in from_addr;
  187. struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 1], *poll_entry;
  188. HTTPContext *c, **cp;
  189. long cur_time;
  190. server_fd = socket(AF_INET,SOCK_STREAM,0);
  191. if (server_fd < 0) {
  192. perror ("socket");
  193. return -1;
  194. }
  195. tmp = 1;
  196. setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
  197. if (bind (server_fd, (struct sockaddr *) &my_addr, sizeof (my_addr)) < 0) {
  198. perror ("bind");
  199. close(server_fd);
  200. return -1;
  201. }
  202. if (listen (server_fd, 5) < 0) {
  203. perror ("listen");
  204. close(server_fd);
  205. return -1;
  206. }
  207. http_log("ffserver started.\n");
  208. fcntl(server_fd, F_SETFL, O_NONBLOCK);
  209. first_http_ctx = NULL;
  210. nb_connections = 0;
  211. first_http_ctx = NULL;
  212. for(;;) {
  213. poll_entry = poll_table;
  214. poll_entry->fd = server_fd;
  215. poll_entry->events = POLLIN;
  216. poll_entry++;
  217. /* wait for events on each HTTP handle */
  218. c = first_http_ctx;
  219. while (c != NULL) {
  220. int fd;
  221. fd = c->fd;
  222. switch(c->state) {
  223. case HTTPSTATE_WAIT_REQUEST:
  224. c->poll_entry = poll_entry;
  225. poll_entry->fd = fd;
  226. poll_entry->events = POLLIN;
  227. poll_entry++;
  228. break;
  229. case HTTPSTATE_SEND_HEADER:
  230. case HTTPSTATE_SEND_DATA_HEADER:
  231. case HTTPSTATE_SEND_DATA:
  232. case HTTPSTATE_SEND_DATA_TRAILER:
  233. c->poll_entry = poll_entry;
  234. poll_entry->fd = fd;
  235. poll_entry->events = POLLOUT;
  236. poll_entry++;
  237. break;
  238. case HTTPSTATE_RECEIVE_DATA:
  239. c->poll_entry = poll_entry;
  240. poll_entry->fd = fd;
  241. poll_entry->events = POLLIN;
  242. poll_entry++;
  243. break;
  244. case HTTPSTATE_WAIT_FEED:
  245. /* need to catch errors */
  246. c->poll_entry = poll_entry;
  247. poll_entry->fd = fd;
  248. poll_entry->events = POLLIN;/* Maybe this will work */
  249. poll_entry++;
  250. break;
  251. default:
  252. c->poll_entry = NULL;
  253. break;
  254. }
  255. c = c->next;
  256. }
  257. /* wait for an event on one connection. We poll at least every
  258. second to handle timeouts */
  259. do {
  260. ret = poll(poll_table, poll_entry - poll_table, 1000);
  261. } while (ret == -1);
  262. cur_time = gettime_ms();
  263. /* now handle the events */
  264. cp = &first_http_ctx;
  265. while ((*cp) != NULL) {
  266. c = *cp;
  267. if (handle_http (c, cur_time) < 0) {
  268. /* close and free the connection */
  269. log_connection(c);
  270. close(c->fd);
  271. if (c->fmt_in)
  272. av_close_input_file(c->fmt_in);
  273. *cp = c->next;
  274. nb_bandwidth -= c->bandwidth;
  275. av_free(c);
  276. nb_connections--;
  277. } else {
  278. cp = &c->next;
  279. }
  280. }
  281. /* new connection request ? */
  282. poll_entry = poll_table;
  283. if (poll_entry->revents & POLLIN) {
  284. int fd, len;
  285. len = sizeof(from_addr);
  286. fd = accept(server_fd, (struct sockaddr *)&from_addr,
  287. &len);
  288. if (fd >= 0) {
  289. fcntl(fd, F_SETFL, O_NONBLOCK);
  290. /* XXX: should output a warning page when coming
  291. close to the connection limit */
  292. if (nb_connections >= nb_max_connections) {
  293. close(fd);
  294. } else {
  295. /* add a new connection */
  296. c = av_mallocz(sizeof(HTTPContext));
  297. c->next = first_http_ctx;
  298. first_http_ctx = c;
  299. c->fd = fd;
  300. c->poll_entry = NULL;
  301. c->from_addr = from_addr;
  302. c->state = HTTPSTATE_WAIT_REQUEST;
  303. c->buffer_ptr = c->buffer;
  304. c->buffer_end = c->buffer + IOBUFFER_MAX_SIZE;
  305. c->timeout = cur_time + REQUEST_TIMEOUT;
  306. nb_connections++;
  307. }
  308. }
  309. }
  310. poll_entry++;
  311. }
  312. }
  313. static int handle_http(HTTPContext *c, long cur_time)
  314. {
  315. int len;
  316. switch(c->state) {
  317. case HTTPSTATE_WAIT_REQUEST:
  318. /* timeout ? */
  319. if ((c->timeout - cur_time) < 0)
  320. return -1;
  321. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  322. return -1;
  323. /* no need to read if no events */
  324. if (!(c->poll_entry->revents & POLLIN))
  325. return 0;
  326. /* read the data */
  327. len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  328. if (len < 0) {
  329. if (errno != EAGAIN && errno != EINTR)
  330. return -1;
  331. } else if (len == 0) {
  332. return -1;
  333. } else {
  334. /* search for end of request. XXX: not fully correct since garbage could come after the end */
  335. UINT8 *ptr;
  336. c->buffer_ptr += len;
  337. ptr = c->buffer_ptr;
  338. if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
  339. (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
  340. /* request found : parse it and reply */
  341. if (http_parse_request(c) < 0)
  342. return -1;
  343. } else if (ptr >= c->buffer_end) {
  344. /* request too long: cannot do anything */
  345. return -1;
  346. }
  347. }
  348. break;
  349. case HTTPSTATE_SEND_HEADER:
  350. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  351. return -1;
  352. /* no need to read if no events */
  353. if (!(c->poll_entry->revents & POLLOUT))
  354. return 0;
  355. len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  356. if (len < 0) {
  357. if (errno != EAGAIN && errno != EINTR) {
  358. /* error : close connection */
  359. return -1;
  360. }
  361. } else {
  362. c->buffer_ptr += len;
  363. if (c->stream)
  364. c->stream->bytes_served += len;
  365. c->data_count += len;
  366. if (c->buffer_ptr >= c->buffer_end) {
  367. /* if error, exit */
  368. if (c->http_error)
  369. return -1;
  370. /* all the buffer was send : synchronize to the incoming stream */
  371. c->state = HTTPSTATE_SEND_DATA_HEADER;
  372. c->buffer_ptr = c->buffer_end = c->buffer;
  373. }
  374. }
  375. break;
  376. case HTTPSTATE_SEND_DATA:
  377. case HTTPSTATE_SEND_DATA_HEADER:
  378. case HTTPSTATE_SEND_DATA_TRAILER:
  379. /* no need to read if no events */
  380. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  381. return -1;
  382. if (!(c->poll_entry->revents & POLLOUT))
  383. return 0;
  384. if (http_send_data(c) < 0)
  385. return -1;
  386. break;
  387. case HTTPSTATE_RECEIVE_DATA:
  388. /* no need to read if no events */
  389. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  390. return -1;
  391. if (!(c->poll_entry->revents & POLLIN))
  392. return 0;
  393. if (http_receive_data(c) < 0)
  394. return -1;
  395. break;
  396. case HTTPSTATE_WAIT_FEED:
  397. /* no need to read if no events */
  398. if (c->poll_entry->revents & (POLLIN | POLLERR | POLLHUP))
  399. return -1;
  400. /* nothing to do, we'll be waken up by incoming feed packets */
  401. break;
  402. default:
  403. return -1;
  404. }
  405. return 0;
  406. }
  407. /* parse http request and prepare header */
  408. static int http_parse_request(HTTPContext *c)
  409. {
  410. char *p;
  411. int post;
  412. int doing_asx;
  413. int doing_ram;
  414. char cmd[32];
  415. char info[1024], *filename;
  416. char url[1024], *q;
  417. char protocol[32];
  418. char msg[1024];
  419. const char *mime_type;
  420. FFStream *stream;
  421. int i;
  422. p = c->buffer;
  423. q = cmd;
  424. while (!isspace(*p) && *p != '\0') {
  425. if ((q - cmd) < sizeof(cmd) - 1)
  426. *q++ = *p;
  427. p++;
  428. }
  429. *q = '\0';
  430. pstrcpy(c->method, sizeof(c->method), cmd);
  431. if (!strcmp(cmd, "GET"))
  432. post = 0;
  433. else if (!strcmp(cmd, "POST"))
  434. post = 1;
  435. else
  436. return -1;
  437. while (isspace(*p)) p++;
  438. q = url;
  439. while (!isspace(*p) && *p != '\0') {
  440. if ((q - url) < sizeof(url) - 1)
  441. *q++ = *p;
  442. p++;
  443. }
  444. *q = '\0';
  445. pstrcpy(c->url, sizeof(c->url), url);
  446. while (isspace(*p)) p++;
  447. q = protocol;
  448. while (!isspace(*p) && *p != '\0') {
  449. if ((q - protocol) < sizeof(protocol) - 1)
  450. *q++ = *p;
  451. p++;
  452. }
  453. *q = '\0';
  454. if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
  455. return -1;
  456. pstrcpy(c->protocol, sizeof(c->protocol), protocol);
  457. /* find the filename and the optional info string in the request */
  458. p = url;
  459. if (*p == '/')
  460. p++;
  461. filename = p;
  462. p = strchr(p, '?');
  463. if (p) {
  464. pstrcpy(info, sizeof(info), p);
  465. *p = '\0';
  466. } else {
  467. info[0] = '\0';
  468. }
  469. if (strlen(filename) > 4 && strcmp(".asx", filename + strlen(filename) - 4) == 0) {
  470. doing_asx = 1;
  471. filename[strlen(filename)-1] = 'f';
  472. } else {
  473. doing_asx = 0;
  474. }
  475. if (strlen(filename) > 4 &&
  476. (strcmp(".rpm", filename + strlen(filename) - 4) == 0 ||
  477. strcmp(".ram", filename + strlen(filename) - 4) == 0)) {
  478. doing_ram = 1;
  479. strcpy(filename + strlen(filename)-2, "m");
  480. } else {
  481. doing_ram = 0;
  482. }
  483. stream = first_stream;
  484. while (stream != NULL) {
  485. if (!strcmp(stream->filename, filename))
  486. break;
  487. stream = stream->next;
  488. }
  489. if (stream == NULL) {
  490. sprintf(msg, "File '%s' not found", url);
  491. goto send_error;
  492. }
  493. if (post == 0 && stream->stream_type == STREAM_TYPE_LIVE) {
  494. /* See if we meet the bandwidth requirements */
  495. for(i=0;i<stream->nb_streams;i++) {
  496. AVStream *st = stream->streams[i];
  497. switch(st->codec.codec_type) {
  498. case CODEC_TYPE_AUDIO:
  499. c->bandwidth += st->codec.bit_rate;
  500. break;
  501. case CODEC_TYPE_VIDEO:
  502. c->bandwidth += st->codec.bit_rate;
  503. break;
  504. default:
  505. av_abort();
  506. }
  507. }
  508. }
  509. c->bandwidth /= 1000;
  510. nb_bandwidth += c->bandwidth;
  511. if (post == 0 && nb_max_bandwidth < nb_bandwidth) {
  512. c->http_error = 200;
  513. q = c->buffer;
  514. q += sprintf(q, "HTTP/1.0 200 Server too busy\r\n");
  515. q += sprintf(q, "Content-type: text/html\r\n");
  516. q += sprintf(q, "\r\n");
  517. q += sprintf(q, "<html><head><title>Too busy</title></head><body>\r\n");
  518. q += sprintf(q, "The server is too busy to serve your request at this time.<p>\r\n");
  519. q += sprintf(q, "The bandwidth being served (including your stream) is %dkbit/sec, and this exceeds the limit of %dkbit/sec\r\n",
  520. nb_bandwidth, nb_max_bandwidth);
  521. q += sprintf(q, "</body></html>\r\n");
  522. /* prepare output buffer */
  523. c->buffer_ptr = c->buffer;
  524. c->buffer_end = q;
  525. c->state = HTTPSTATE_SEND_HEADER;
  526. return 0;
  527. }
  528. if (doing_asx || doing_ram) {
  529. char *hostinfo = 0;
  530. for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
  531. if (strncasecmp(p, "Host:", 5) == 0) {
  532. hostinfo = p + 5;
  533. break;
  534. }
  535. p = strchr(p, '\n');
  536. if (!p)
  537. break;
  538. p++;
  539. }
  540. if (hostinfo) {
  541. char *eoh;
  542. char hostbuf[260];
  543. while (isspace(*hostinfo))
  544. hostinfo++;
  545. eoh = strchr(hostinfo, '\n');
  546. if (eoh) {
  547. if (eoh[-1] == '\r')
  548. eoh--;
  549. if (eoh - hostinfo < sizeof(hostbuf) - 1) {
  550. memcpy(hostbuf, hostinfo, eoh - hostinfo);
  551. hostbuf[eoh - hostinfo] = 0;
  552. c->http_error = 200;
  553. q = c->buffer;
  554. if (doing_asx) {
  555. q += sprintf(q, "HTTP/1.0 200 ASX Follows\r\n");
  556. q += sprintf(q, "Content-type: video/x-ms-asf\r\n");
  557. q += sprintf(q, "\r\n");
  558. q += sprintf(q, "<ASX Version=\"3\">\r\n");
  559. q += sprintf(q, "<!-- Autogenerated by ffserver -->\r\n");
  560. q += sprintf(q, "<ENTRY><REF HREF=\"http://%s/%s%s\"/></ENTRY>\r\n",
  561. hostbuf, filename, info);
  562. q += sprintf(q, "</ASX>\r\n");
  563. } else if (doing_ram) {
  564. q += sprintf(q, "HTTP/1.0 200 RAM Follows\r\n");
  565. q += sprintf(q, "Content-type: audio/x-pn-realaudio\r\n");
  566. q += sprintf(q, "\r\n");
  567. q += sprintf(q, "# Autogenerated by ffserver\r\n");
  568. q += sprintf(q, "http://%s/%s%s\r\n",
  569. hostbuf, filename, info);
  570. } else
  571. av_abort();
  572. /* prepare output buffer */
  573. c->buffer_ptr = c->buffer;
  574. c->buffer_end = q;
  575. c->state = HTTPSTATE_SEND_HEADER;
  576. return 0;
  577. }
  578. }
  579. }
  580. sprintf(msg, "ASX/RAM file not handled");
  581. goto send_error;
  582. }
  583. c->stream = stream;
  584. stream->conns_served++;
  585. /* XXX: add there authenticate and IP match */
  586. if (post) {
  587. /* if post, it means a feed is being sent */
  588. if (!stream->is_feed) {
  589. /* However it might be a status report from WMP! Lets log the data
  590. * as it might come in handy one day
  591. */
  592. char *logline = 0;
  593. for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) {
  594. if (strncasecmp(p, "Pragma: log-line=", 17) == 0) {
  595. logline = p;
  596. break;
  597. }
  598. p = strchr(p, '\n');
  599. if (!p)
  600. break;
  601. p++;
  602. }
  603. if (logline) {
  604. char *eol = strchr(logline, '\n');
  605. logline += 17;
  606. if (eol) {
  607. if (eol[-1] == '\r')
  608. eol--;
  609. http_log("%.*s\n", eol - logline, logline);
  610. c->suppress_log = 1;
  611. }
  612. }
  613. sprintf(msg, "POST command not handled");
  614. goto send_error;
  615. }
  616. if (http_start_receive_data(c) < 0) {
  617. sprintf(msg, "could not open feed");
  618. goto send_error;
  619. }
  620. c->http_error = 0;
  621. c->state = HTTPSTATE_RECEIVE_DATA;
  622. return 0;
  623. }
  624. if (c->stream->stream_type == STREAM_TYPE_STATUS)
  625. goto send_stats;
  626. /* open input stream */
  627. if (open_input_stream(c, info) < 0) {
  628. sprintf(msg, "Input stream corresponding to '%s' not found", url);
  629. goto send_error;
  630. }
  631. /* prepare http header */
  632. q = c->buffer;
  633. q += sprintf(q, "HTTP/1.0 200 OK\r\n");
  634. mime_type = c->stream->fmt->mime_type;
  635. if (!mime_type)
  636. mime_type = "application/x-octet_stream";
  637. q += sprintf(q, "Pragma: no-cache\r\n");
  638. /* for asf, we need extra headers */
  639. if (!strcmp(c->stream->fmt->name,"asf")) {
  640. q += sprintf(q, "Server: Cougar 4.1.0.3923\r\nCache-Control: no-cache\r\nPragma: client-id=1234\r\nPragma: features=\"broadcast\"\r\n");
  641. /* mime_type = "application/octet-stream"; */
  642. /* video/x-ms-asf seems better -- netscape doesn't crash any more! */
  643. mime_type = "video/x-ms-asf";
  644. }
  645. q += sprintf(q, "Content-Type: %s\r\n", mime_type);
  646. q += sprintf(q, "\r\n");
  647. /* prepare output buffer */
  648. c->http_error = 0;
  649. c->buffer_ptr = c->buffer;
  650. c->buffer_end = q;
  651. c->state = HTTPSTATE_SEND_HEADER;
  652. return 0;
  653. send_error:
  654. c->http_error = 404;
  655. q = c->buffer;
  656. q += sprintf(q, "HTTP/1.0 404 Not Found\r\n");
  657. q += sprintf(q, "Content-type: %s\r\n", "text/html");
  658. q += sprintf(q, "\r\n");
  659. q += sprintf(q, "<HTML>\n");
  660. q += sprintf(q, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
  661. q += sprintf(q, "<BODY>%s</BODY>\n", msg);
  662. q += sprintf(q, "</HTML>\n");
  663. /* prepare output buffer */
  664. c->buffer_ptr = c->buffer;
  665. c->buffer_end = q;
  666. c->state = HTTPSTATE_SEND_HEADER;
  667. return 0;
  668. send_stats:
  669. compute_stats(c);
  670. c->http_error = 200; /* horrible : we use this value to avoid
  671. going to the send data state */
  672. c->state = HTTPSTATE_SEND_HEADER;
  673. return 0;
  674. }
  675. static void compute_stats(HTTPContext *c)
  676. {
  677. HTTPContext *c1;
  678. FFStream *stream;
  679. char *q, *p;
  680. time_t ti;
  681. int i;
  682. q = c->buffer;
  683. q += sprintf(q, "HTTP/1.0 200 OK\r\n");
  684. q += sprintf(q, "Content-type: %s\r\n", "text/html");
  685. q += sprintf(q, "Pragma: no-cache\r\n");
  686. q += sprintf(q, "\r\n");
  687. q += sprintf(q, "<HEAD><TITLE>FFServer Status</TITLE></HEAD>\n<BODY>");
  688. q += sprintf(q, "<H1>FFServer Status</H1>\n");
  689. /* format status */
  690. q += sprintf(q, "<H2>Available Streams</H2>\n");
  691. q += sprintf(q, "<TABLE cellspacing=0 cellpadding=4>\n");
  692. q += sprintf(q, "<TR><Th valign=top>Path<th align=left>Served<br>Conns<Th><br>kbytes<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");
  693. stream = first_stream;
  694. while (stream != NULL) {
  695. char sfilename[1024];
  696. char *eosf;
  697. if (stream->feed != stream) {
  698. pstrcpy(sfilename, sizeof(sfilename) - 1, stream->filename);
  699. eosf = sfilename + strlen(sfilename);
  700. if (eosf - sfilename >= 4) {
  701. if (strcmp(eosf - 4, ".asf") == 0) {
  702. strcpy(eosf - 4, ".asx");
  703. } else if (strcmp(eosf - 3, ".rm") == 0) {
  704. strcpy(eosf - 3, ".ram");
  705. }
  706. }
  707. q += sprintf(q, "<TR><TD><A HREF=\"/%s\">%s</A> ",
  708. sfilename, stream->filename);
  709. q += sprintf(q, "<td align=right> %d <td align=right> %lld",
  710. stream->conns_served, stream->bytes_served / 1000);
  711. switch(stream->stream_type) {
  712. case STREAM_TYPE_LIVE:
  713. {
  714. int audio_bit_rate = 0;
  715. int video_bit_rate = 0;
  716. char *audio_codec_name = "";
  717. char *video_codec_name = "";
  718. char *audio_codec_name_extra = "";
  719. char *video_codec_name_extra = "";
  720. for(i=0;i<stream->nb_streams;i++) {
  721. AVStream *st = stream->streams[i];
  722. AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);
  723. switch(st->codec.codec_type) {
  724. case CODEC_TYPE_AUDIO:
  725. audio_bit_rate += st->codec.bit_rate;
  726. if (codec) {
  727. if (*audio_codec_name)
  728. audio_codec_name_extra = "...";
  729. audio_codec_name = codec->name;
  730. }
  731. break;
  732. case CODEC_TYPE_VIDEO:
  733. video_bit_rate += st->codec.bit_rate;
  734. if (codec) {
  735. if (*video_codec_name)
  736. video_codec_name_extra = "...";
  737. video_codec_name = codec->name;
  738. }
  739. break;
  740. default:
  741. av_abort();
  742. }
  743. }
  744. q += sprintf(q, "<TD align=center> %s <TD align=right> %d <TD align=right> %d <TD> %s %s <TD align=right> %d <TD> %s %s",
  745. stream->fmt->name,
  746. (audio_bit_rate + video_bit_rate) / 1000,
  747. video_bit_rate / 1000, video_codec_name, video_codec_name_extra,
  748. audio_bit_rate / 1000, audio_codec_name, audio_codec_name_extra);
  749. if (stream->feed) {
  750. q += sprintf(q, "<TD>%s", stream->feed->filename);
  751. } else {
  752. q += sprintf(q, "<TD>%s", stream->feed_filename);
  753. }
  754. q += sprintf(q, "\n");
  755. }
  756. break;
  757. default:
  758. q += sprintf(q, "<TD align=center> - <TD align=right> - <TD align=right> - <td><td align=right> - <TD>\n");
  759. break;
  760. }
  761. }
  762. stream = stream->next;
  763. }
  764. q += sprintf(q, "</TABLE>\n");
  765. stream = first_stream;
  766. while (stream != NULL) {
  767. if (stream->feed == stream) {
  768. q += sprintf(q, "<h2>Feed %s</h2>", stream->filename);
  769. q += sprintf(q, "<table cellspacing=0 cellpadding=4><tr><th>Stream<th>type<th>kbits/s<th align=left>codec\n");
  770. for (i = 0; i < stream->nb_streams; i++) {
  771. AVStream *st = stream->streams[i];
  772. AVCodec *codec = avcodec_find_encoder(st->codec.codec_id);
  773. char *type = "unknown";
  774. switch(st->codec.codec_type) {
  775. case CODEC_TYPE_AUDIO:
  776. type = "audio";
  777. break;
  778. case CODEC_TYPE_VIDEO:
  779. type = "video";
  780. break;
  781. default:
  782. av_abort();
  783. }
  784. q += sprintf(q, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s\n",
  785. i, type, st->codec.bit_rate/1000, codec ? codec->name : "");
  786. }
  787. q += sprintf(q, "</table>\n");
  788. }
  789. stream = stream->next;
  790. }
  791. #if 0
  792. {
  793. float avg;
  794. AVCodecContext *enc;
  795. char buf[1024];
  796. /* feed status */
  797. stream = first_feed;
  798. while (stream != NULL) {
  799. q += sprintf(q, "<H1>Feed '%s'</H1>\n", stream->filename);
  800. q += sprintf(q, "<TABLE>\n");
  801. q += sprintf(q, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
  802. for(i=0;i<stream->nb_streams;i++) {
  803. AVStream *st = stream->streams[i];
  804. FeedData *fdata = st->priv_data;
  805. enc = &st->codec;
  806. avcodec_string(buf, sizeof(buf), enc);
  807. avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
  808. if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
  809. avg /= enc->frame_size;
  810. q += sprintf(q, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n",
  811. buf, enc->frame_number, fdata->data_count, avg / 1000.0);
  812. }
  813. q += sprintf(q, "</TABLE>\n");
  814. stream = stream->next_feed;
  815. }
  816. }
  817. #endif
  818. /* connection status */
  819. q += sprintf(q, "<H2>Connection Status</H2>\n");
  820. q += sprintf(q, "Number of connections: %d / %d<BR>\n",
  821. nb_connections, nb_max_connections);
  822. q += sprintf(q, "Bandwidth in use: %dk / %dk<BR>\n",
  823. nb_bandwidth, nb_max_bandwidth);
  824. q += sprintf(q, "<TABLE>\n");
  825. q += sprintf(q, "<TR><TD>#<TD>File<TD>IP<TD>State<TD>Size\n");
  826. c1 = first_http_ctx;
  827. i = 0;
  828. while (c1 != NULL && q < (char *) c->buffer + sizeof(c->buffer) - 2048) {
  829. i++;
  830. p = inet_ntoa(c1->from_addr.sin_addr);
  831. q += sprintf(q, "<TR><TD><B>%d</B><TD>%s%s <TD> %s <TD> %s <TD> %Ld\n",
  832. i, c1->stream->filename,
  833. c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
  834. p,
  835. http_state[c1->state],
  836. c1->data_count);
  837. c1 = c1->next;
  838. }
  839. q += sprintf(q, "</TABLE>\n");
  840. /* date */
  841. ti = time(NULL);
  842. p = ctime(&ti);
  843. q += sprintf(q, "<HR size=1 noshade>Generated at %s", p);
  844. q += sprintf(q, "</BODY>\n</HTML>\n");
  845. c->buffer_ptr = c->buffer;
  846. c->buffer_end = q;
  847. }
  848. static void http_write_packet(void *opaque,
  849. unsigned char *buf, int size)
  850. {
  851. HTTPContext *c = opaque;
  852. if (c->buffer_ptr == c->buffer_end || !c->buffer_ptr)
  853. c->buffer_ptr = c->buffer_end = c->buffer;
  854. if (c->buffer_end - c->buffer + size > IOBUFFER_MAX_SIZE)
  855. av_abort();
  856. memcpy(c->buffer_end, buf, size);
  857. c->buffer_end += size;
  858. }
  859. static int open_input_stream(HTTPContext *c, const char *info)
  860. {
  861. char buf[128];
  862. char input_filename[1024];
  863. AVFormatContext *s;
  864. int buf_size;
  865. INT64 stream_pos;
  866. /* find file name */
  867. if (c->stream->feed) {
  868. strcpy(input_filename, c->stream->feed->feed_filename);
  869. buf_size = FFM_PACKET_SIZE;
  870. /* compute position (absolute time) */
  871. if (find_info_tag(buf, sizeof(buf), "date", info)) {
  872. stream_pos = parse_date(buf, 0);
  873. } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
  874. int prebuffer = strtol(buf, 0, 10);
  875. stream_pos = gettime() - prebuffer * 1000000;
  876. } else {
  877. stream_pos = gettime() - c->stream->prebuffer * 1000;
  878. }
  879. } else {
  880. strcpy(input_filename, c->stream->feed_filename);
  881. buf_size = 0;
  882. /* compute position (relative time) */
  883. if (find_info_tag(buf, sizeof(buf), "date", info)) {
  884. stream_pos = parse_date(buf, 1);
  885. } else {
  886. stream_pos = 0;
  887. }
  888. }
  889. if (input_filename[0] == '\0')
  890. return -1;
  891. /* open stream */
  892. if (av_open_input_file(&s, input_filename, NULL, buf_size, NULL) < 0)
  893. return -1;
  894. c->fmt_in = s;
  895. if (c->fmt_in->iformat->read_seek) {
  896. c->fmt_in->iformat->read_seek(c->fmt_in, stream_pos);
  897. }
  898. // printf("stream %s opened pos=%0.6f\n", input_filename, stream_pos / 1000000.0);
  899. return 0;
  900. }
  901. static int http_prepare_data(HTTPContext *c)
  902. {
  903. int i;
  904. switch(c->state) {
  905. case HTTPSTATE_SEND_DATA_HEADER:
  906. memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
  907. if (c->stream->feed) {
  908. /* open output stream by using specified codecs */
  909. c->fmt_ctx.oformat = c->stream->fmt;
  910. c->fmt_ctx.nb_streams = c->stream->nb_streams;
  911. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  912. AVStream *st;
  913. st = av_mallocz(sizeof(AVStream));
  914. c->fmt_ctx.streams[i] = st;
  915. if (c->stream->feed == c->stream)
  916. memcpy(st, c->stream->streams[i], sizeof(AVStream));
  917. else
  918. memcpy(st, c->stream->feed->streams[c->stream->feed_streams[i]], sizeof(AVStream));
  919. st->codec.frame_number = 0; /* XXX: should be done in
  920. AVStream, not in codec */
  921. }
  922. c->got_key_frame = 0;
  923. } else {
  924. /* open output stream by using codecs in specified file */
  925. c->fmt_ctx.oformat = c->stream->fmt;
  926. c->fmt_ctx.nb_streams = c->fmt_in->nb_streams;
  927. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  928. AVStream *st;
  929. st = av_mallocz(sizeof(AVStream));
  930. c->fmt_ctx.streams[i] = st;
  931. memcpy(st, c->fmt_in->streams[i], sizeof(AVStream));
  932. st->codec.frame_number = 0; /* XXX: should be done in
  933. AVStream, not in codec */
  934. }
  935. c->got_key_frame = 0;
  936. }
  937. init_put_byte(&c->fmt_ctx.pb, c->pbuffer, PACKET_MAX_SIZE,
  938. 1, c, NULL, http_write_packet, NULL);
  939. c->fmt_ctx.pb.is_streamed = 1;
  940. /* prepare header */
  941. av_write_header(&c->fmt_ctx);
  942. c->state = HTTPSTATE_SEND_DATA;
  943. c->last_packet_sent = 0;
  944. break;
  945. case HTTPSTATE_SEND_DATA:
  946. /* find a new packet */
  947. #if 0
  948. fifo_total_size = http_fifo_write_count - c->last_http_fifo_write_count;
  949. if (fifo_total_size >= ((3 * FIFO_MAX_SIZE) / 4)) {
  950. /* overflow : resync. We suppose that wptr is at this
  951. point a pointer to a valid packet */
  952. c->rptr = http_fifo.wptr;
  953. c->got_key_frame = 0;
  954. }
  955. start_rptr = c->rptr;
  956. if (fifo_read(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &c->rptr) < 0)
  957. return 0;
  958. payload_size = ntohs(hdr.payload_size);
  959. payload = av_malloc(payload_size);
  960. if (fifo_read(&http_fifo, payload, payload_size, &c->rptr) < 0) {
  961. /* cannot read all the payload */
  962. av_free(payload);
  963. c->rptr = start_rptr;
  964. return 0;
  965. }
  966. c->last_http_fifo_write_count = http_fifo_write_count -
  967. fifo_size(&http_fifo, c->rptr);
  968. if (c->stream->stream_type != STREAM_TYPE_MASTER) {
  969. /* test if the packet can be handled by this format */
  970. ret = 0;
  971. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  972. AVStream *st = c->fmt_ctx.streams[i];
  973. if (test_header(&hdr, &st->codec)) {
  974. /* only begin sending when got a key frame */
  975. if (st->codec.key_frame)
  976. c->got_key_frame |= 1 << i;
  977. if (c->got_key_frame & (1 << i)) {
  978. ret = c->fmt_ctx.format->write_packet(&c->fmt_ctx, i,
  979. payload, payload_size);
  980. }
  981. break;
  982. }
  983. }
  984. if (ret) {
  985. /* must send trailer now */
  986. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  987. }
  988. } else {
  989. /* master case : send everything */
  990. char *q;
  991. q = c->buffer;
  992. memcpy(q, &hdr, sizeof(hdr));
  993. q += sizeof(hdr);
  994. memcpy(q, payload, payload_size);
  995. q += payload_size;
  996. c->buffer_ptr = c->buffer;
  997. c->buffer_end = q;
  998. }
  999. av_free(payload);
  1000. #endif
  1001. {
  1002. AVPacket pkt;
  1003. /* read a packet from the input stream */
  1004. if (c->stream->feed) {
  1005. ffm_set_write_index(c->fmt_in,
  1006. c->stream->feed->feed_write_index,
  1007. c->stream->feed->feed_size);
  1008. }
  1009. if (c->stream->max_time &&
  1010. c->stream->max_time + c->start_time > time(0)) {
  1011. /* We have timed out */
  1012. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  1013. } else if (av_read_packet(c->fmt_in, &pkt) < 0) {
  1014. if (c->stream->feed && c->stream->feed->feed_opened) {
  1015. /* if coming from feed, it means we reached the end of the
  1016. ffm file, so must wait for more data */
  1017. c->state = HTTPSTATE_WAIT_FEED;
  1018. return 1; /* state changed */
  1019. } else {
  1020. /* must send trailer now because eof or error */
  1021. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  1022. }
  1023. } else {
  1024. /* send it to the appropriate stream */
  1025. if (c->stream->feed) {
  1026. /* if coming from a feed, select the right stream */
  1027. for(i=0;i<c->stream->nb_streams;i++) {
  1028. if (c->stream->feed_streams[i] == pkt.stream_index) {
  1029. pkt.stream_index = i;
  1030. if (pkt.flags & PKT_FLAG_KEY) {
  1031. c->got_key_frame |= 1 << i;
  1032. }
  1033. /* See if we have all the key frames, then
  1034. * we start to send. This logic is not quite
  1035. * right, but it works for the case of a
  1036. * single video stream with one or more
  1037. * audio streams (for which every frame is
  1038. * typically a key frame).
  1039. */
  1040. if (!c->stream->send_on_key || ((c->got_key_frame + 1) >> c->stream->nb_streams)) {
  1041. goto send_it;
  1042. }
  1043. }
  1044. }
  1045. } else {
  1046. AVCodecContext *codec;
  1047. send_it:
  1048. /* Fudge here */
  1049. codec = &c->fmt_ctx.streams[pkt.stream_index]->codec;
  1050. codec->key_frame = ((pkt.flags & PKT_FLAG_KEY) != 0);
  1051. #ifdef PJSG
  1052. if (codec->codec_type == CODEC_TYPE_AUDIO) {
  1053. codec->frame_size = (codec->sample_rate * pkt.duration + 500000) / 1000000;
  1054. /* printf("Calculated size %d, from sr %d, duration %d\n", codec->frame_size, codec->sample_rate, pkt.duration); */
  1055. }
  1056. #endif
  1057. if (av_write_packet(&c->fmt_ctx, &pkt, 0))
  1058. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  1059. codec->frame_number++;
  1060. }
  1061. av_free_packet(&pkt);
  1062. }
  1063. }
  1064. break;
  1065. default:
  1066. case HTTPSTATE_SEND_DATA_TRAILER:
  1067. /* last packet test ? */
  1068. if (c->last_packet_sent)
  1069. return -1;
  1070. /* prepare header */
  1071. av_write_trailer(&c->fmt_ctx);
  1072. c->last_packet_sent = 1;
  1073. break;
  1074. }
  1075. return 0;
  1076. }
  1077. /* should convert the format at the same time */
  1078. static int http_send_data(HTTPContext *c)
  1079. {
  1080. int len, ret;
  1081. while (c->buffer_ptr >= c->buffer_end) {
  1082. ret = http_prepare_data(c);
  1083. if (ret < 0)
  1084. return -1;
  1085. else if (ret == 0) {
  1086. continue;
  1087. } else {
  1088. /* state change requested */
  1089. return 0;
  1090. }
  1091. }
  1092. if (c->buffer_end > c->buffer_ptr) {
  1093. len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  1094. if (len < 0) {
  1095. if (errno != EAGAIN && errno != EINTR) {
  1096. /* error : close connection */
  1097. return -1;
  1098. }
  1099. } else {
  1100. c->buffer_ptr += len;
  1101. c->data_count += len;
  1102. if (c->stream)
  1103. c->stream->bytes_served += len;
  1104. }
  1105. }
  1106. return 0;
  1107. }
  1108. static int http_start_receive_data(HTTPContext *c)
  1109. {
  1110. int fd;
  1111. if (c->stream->feed_opened)
  1112. return -1;
  1113. /* open feed */
  1114. fd = open(c->stream->feed_filename, O_RDWR);
  1115. if (fd < 0)
  1116. return -1;
  1117. c->feed_fd = fd;
  1118. c->stream->feed_write_index = ffm_read_write_index(fd);
  1119. c->stream->feed_size = lseek(fd, 0, SEEK_END);
  1120. lseek(fd, 0, SEEK_SET);
  1121. /* init buffer input */
  1122. c->buffer_ptr = c->buffer;
  1123. c->buffer_end = c->buffer + FFM_PACKET_SIZE;
  1124. c->stream->feed_opened = 1;
  1125. return 0;
  1126. }
  1127. static int http_receive_data(HTTPContext *c)
  1128. {
  1129. HTTPContext *c1;
  1130. if (c->buffer_end > c->buffer_ptr) {
  1131. int len;
  1132. len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  1133. if (len < 0) {
  1134. if (errno != EAGAIN && errno != EINTR) {
  1135. /* error : close connection */
  1136. goto fail;
  1137. }
  1138. } else if (len == 0) {
  1139. /* end of connection : close it */
  1140. goto fail;
  1141. } else {
  1142. c->buffer_ptr += len;
  1143. c->data_count += len;
  1144. }
  1145. }
  1146. if (c->buffer_ptr >= c->buffer_end) {
  1147. FFStream *feed = c->stream;
  1148. /* a packet has been received : write it in the store, except
  1149. if header */
  1150. if (c->data_count > FFM_PACKET_SIZE) {
  1151. // printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
  1152. /* XXX: use llseek or url_seek */
  1153. lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
  1154. write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
  1155. feed->feed_write_index += FFM_PACKET_SIZE;
  1156. /* update file size */
  1157. if (feed->feed_write_index > c->stream->feed_size)
  1158. feed->feed_size = feed->feed_write_index;
  1159. /* handle wrap around if max file size reached */
  1160. if (feed->feed_write_index >= c->stream->feed_max_size)
  1161. feed->feed_write_index = FFM_PACKET_SIZE;
  1162. /* write index */
  1163. ffm_write_write_index(c->feed_fd, feed->feed_write_index);
  1164. /* wake up any waiting connections */
  1165. for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
  1166. if (c1->state == HTTPSTATE_WAIT_FEED &&
  1167. c1->stream->feed == c->stream->feed) {
  1168. c1->state = HTTPSTATE_SEND_DATA;
  1169. }
  1170. }
  1171. } else {
  1172. /* We have a header in our hands that contains useful data */
  1173. AVFormatContext s;
  1174. AVInputFormat *fmt_in;
  1175. ByteIOContext *pb = &s.pb;
  1176. int i;
  1177. memset(&s, 0, sizeof(s));
  1178. url_open_buf(pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
  1179. pb->buf_end = c->buffer_end; /* ?? */
  1180. pb->is_streamed = 1;
  1181. /* use feed output format name to find corresponding input format */
  1182. fmt_in = av_find_input_format(feed->fmt->name);
  1183. if (!fmt_in)
  1184. goto fail;
  1185. s.priv_data = av_mallocz(fmt_in->priv_data_size);
  1186. if (!s.priv_data)
  1187. goto fail;
  1188. if (fmt_in->read_header(&s, 0) < 0) {
  1189. av_freep(&s.priv_data);
  1190. goto fail;
  1191. }
  1192. /* Now we have the actual streams */
  1193. if (s.nb_streams != feed->nb_streams) {
  1194. av_freep(&s.priv_data);
  1195. goto fail;
  1196. }
  1197. for (i = 0; i < s.nb_streams; i++) {
  1198. memcpy(&feed->streams[i]->codec,
  1199. &s.streams[i]->codec, sizeof(AVCodecContext));
  1200. }
  1201. av_freep(&s.priv_data);
  1202. }
  1203. c->buffer_ptr = c->buffer;
  1204. }
  1205. return 0;
  1206. fail:
  1207. c->stream->feed_opened = 0;
  1208. close(c->feed_fd);
  1209. return -1;
  1210. }
  1211. /* return the stream number in the feed */
  1212. int add_av_stream(FFStream *feed,
  1213. AVStream *st)
  1214. {
  1215. AVStream *fst;
  1216. AVCodecContext *av, *av1;
  1217. int i;
  1218. av = &st->codec;
  1219. for(i=0;i<feed->nb_streams;i++) {
  1220. st = feed->streams[i];
  1221. av1 = &st->codec;
  1222. if (av1->codec_id == av->codec_id &&
  1223. av1->codec_type == av->codec_type &&
  1224. av1->bit_rate == av->bit_rate) {
  1225. switch(av->codec_type) {
  1226. case CODEC_TYPE_AUDIO:
  1227. if (av1->channels == av->channels &&
  1228. av1->sample_rate == av->sample_rate)
  1229. goto found;
  1230. break;
  1231. case CODEC_TYPE_VIDEO:
  1232. if (av1->width == av->width &&
  1233. av1->height == av->height &&
  1234. av1->frame_rate == av->frame_rate &&
  1235. av1->gop_size == av->gop_size)
  1236. goto found;
  1237. break;
  1238. default:
  1239. av_abort();
  1240. }
  1241. }
  1242. }
  1243. fst = av_mallocz(sizeof(AVStream));
  1244. if (!fst)
  1245. return -1;
  1246. fst->priv_data = av_mallocz(sizeof(FeedData));
  1247. memcpy(&fst->codec, av, sizeof(AVCodecContext));
  1248. feed->streams[feed->nb_streams++] = fst;
  1249. return feed->nb_streams - 1;
  1250. found:
  1251. return i;
  1252. }
  1253. /* compute the needed AVStream for each feed */
  1254. void build_feed_streams(void)
  1255. {
  1256. FFStream *stream, *feed;
  1257. int i;
  1258. /* gather all streams */
  1259. for(stream = first_stream; stream != NULL; stream = stream->next) {
  1260. feed = stream->feed;
  1261. if (feed) {
  1262. if (!stream->is_feed) {
  1263. for(i=0;i<stream->nb_streams;i++) {
  1264. stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
  1265. }
  1266. } else {
  1267. for(i=0;i<stream->nb_streams;i++) {
  1268. stream->feed_streams[i] = i;
  1269. }
  1270. }
  1271. }
  1272. }
  1273. /* create feed files if needed */
  1274. for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
  1275. int fd;
  1276. if (!url_exist(feed->feed_filename)) {
  1277. AVFormatContext s1, *s = &s1;
  1278. /* only write the header of the ffm file */
  1279. if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
  1280. fprintf(stderr, "Could not open output feed file '%s'\n",
  1281. feed->feed_filename);
  1282. exit(1);
  1283. }
  1284. s->oformat = feed->fmt;
  1285. s->nb_streams = feed->nb_streams;
  1286. for(i=0;i<s->nb_streams;i++) {
  1287. AVStream *st;
  1288. st = feed->streams[i];
  1289. s->streams[i] = st;
  1290. }
  1291. av_write_header(s);
  1292. /* XXX: need better api */
  1293. av_freep(&s->priv_data);
  1294. url_fclose(&s->pb);
  1295. }
  1296. /* get feed size and write index */
  1297. fd = open(feed->feed_filename, O_RDONLY);
  1298. if (fd < 0) {
  1299. fprintf(stderr, "Could not open output feed file '%s'\n",
  1300. feed->feed_filename);
  1301. exit(1);
  1302. }
  1303. feed->feed_write_index = ffm_read_write_index(fd);
  1304. feed->feed_size = lseek(fd, 0, SEEK_END);
  1305. /* ensure that we do not wrap before the end of file */
  1306. if (feed->feed_max_size < feed->feed_size)
  1307. feed->feed_max_size = feed->feed_size;
  1308. close(fd);
  1309. }
  1310. }
  1311. static void get_arg(char *buf, int buf_size, const char **pp)
  1312. {
  1313. const char *p;
  1314. char *q;
  1315. int quote;
  1316. p = *pp;
  1317. while (isspace(*p)) p++;
  1318. q = buf;
  1319. quote = 0;
  1320. if (*p == '\"' || *p == '\'')
  1321. quote = *p++;
  1322. for(;;) {
  1323. if (quote) {
  1324. if (*p == quote)
  1325. break;
  1326. } else {
  1327. if (isspace(*p))
  1328. break;
  1329. }
  1330. if (*p == '\0')
  1331. break;
  1332. if ((q - buf) < buf_size - 1)
  1333. *q++ = *p;
  1334. p++;
  1335. }
  1336. *q = '\0';
  1337. if (quote && *p == quote)
  1338. p++;
  1339. *pp = p;
  1340. }
  1341. /* add a codec and set the default parameters */
  1342. void add_codec(FFStream *stream, AVCodecContext *av)
  1343. {
  1344. AVStream *st;
  1345. /* compute default parameters */
  1346. switch(av->codec_type) {
  1347. case CODEC_TYPE_AUDIO:
  1348. if (av->bit_rate == 0)
  1349. av->bit_rate = 64000;
  1350. if (av->sample_rate == 0)
  1351. av->sample_rate = 22050;
  1352. if (av->channels == 0)
  1353. av->channels = 1;
  1354. break;
  1355. case CODEC_TYPE_VIDEO:
  1356. if (av->bit_rate == 0)
  1357. av->bit_rate = 64000;
  1358. if (av->frame_rate == 0)
  1359. av->frame_rate = 5 * FRAME_RATE_BASE;
  1360. if (av->width == 0 || av->height == 0) {
  1361. av->width = 160;
  1362. av->height = 128;
  1363. }
  1364. /* Bitrate tolerance is less for streaming */
  1365. if (av->bit_rate_tolerance == 0)
  1366. av->bit_rate_tolerance = av->bit_rate / 4;
  1367. if (av->qmin == 0)
  1368. av->qmin = 3;
  1369. if (av->qmax == 0)
  1370. av->qmax = 31;
  1371. if (av->max_qdiff == 0)
  1372. av->max_qdiff = 3;
  1373. av->qcompress = 0.5;
  1374. av->qblur = 0.5;
  1375. break;
  1376. default:
  1377. av_abort();
  1378. }
  1379. st = av_mallocz(sizeof(AVStream));
  1380. if (!st)
  1381. return;
  1382. stream->streams[stream->nb_streams++] = st;
  1383. memcpy(&st->codec, av, sizeof(AVCodecContext));
  1384. }
  1385. int opt_audio_codec(const char *arg)
  1386. {
  1387. AVCodec *p;
  1388. p = first_avcodec;
  1389. while (p) {
  1390. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1391. break;
  1392. p = p->next;
  1393. }
  1394. if (p == NULL) {
  1395. return CODEC_ID_NONE;
  1396. }
  1397. return p->id;
  1398. }
  1399. int opt_video_codec(const char *arg)
  1400. {
  1401. AVCodec *p;
  1402. p = first_avcodec;
  1403. while (p) {
  1404. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1405. break;
  1406. p = p->next;
  1407. }
  1408. if (p == NULL) {
  1409. return CODEC_ID_NONE;
  1410. }
  1411. return p->id;
  1412. }
  1413. int parse_ffconfig(const char *filename)
  1414. {
  1415. FILE *f;
  1416. char line[1024];
  1417. char cmd[64];
  1418. char arg[1024];
  1419. const char *p;
  1420. int val, errors, line_num;
  1421. FFStream **last_stream, *stream;
  1422. FFStream **last_feed, *feed;
  1423. AVCodecContext audio_enc, video_enc;
  1424. int audio_id, video_id;
  1425. f = fopen(filename, "r");
  1426. if (!f) {
  1427. perror(filename);
  1428. return -1;
  1429. }
  1430. errors = 0;
  1431. line_num = 0;
  1432. first_stream = NULL;
  1433. last_stream = &first_stream;
  1434. first_feed = NULL;
  1435. last_feed = &first_feed;
  1436. stream = NULL;
  1437. feed = NULL;
  1438. audio_id = CODEC_ID_NONE;
  1439. video_id = CODEC_ID_NONE;
  1440. for(;;) {
  1441. if (fgets(line, sizeof(line), f) == NULL)
  1442. break;
  1443. line_num++;
  1444. p = line;
  1445. while (isspace(*p))
  1446. p++;
  1447. if (*p == '\0' || *p == '#')
  1448. continue;
  1449. get_arg(cmd, sizeof(cmd), &p);
  1450. if (!strcasecmp(cmd, "Port")) {
  1451. get_arg(arg, sizeof(arg), &p);
  1452. my_addr.sin_port = htons (atoi(arg));
  1453. } else if (!strcasecmp(cmd, "BindAddress")) {
  1454. get_arg(arg, sizeof(arg), &p);
  1455. if (!inet_aton(arg, &my_addr.sin_addr)) {
  1456. fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
  1457. filename, line_num, arg);
  1458. errors++;
  1459. }
  1460. } else if (!strcasecmp(cmd, "MaxClients")) {
  1461. get_arg(arg, sizeof(arg), &p);
  1462. val = atoi(arg);
  1463. if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
  1464. fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
  1465. filename, line_num, arg);
  1466. errors++;
  1467. } else {
  1468. nb_max_connections = val;
  1469. }
  1470. } else if (!strcasecmp(cmd, "MaxBandwidth")) {
  1471. get_arg(arg, sizeof(arg), &p);
  1472. val = atoi(arg);
  1473. if (val < 10 || val > 100000) {
  1474. fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
  1475. filename, line_num, arg);
  1476. errors++;
  1477. } else {
  1478. nb_max_bandwidth = val;
  1479. }
  1480. } else if (!strcasecmp(cmd, "CustomLog")) {
  1481. get_arg(logfilename, sizeof(logfilename), &p);
  1482. } else if (!strcasecmp(cmd, "<Feed")) {
  1483. /*********************************************/
  1484. /* Feed related options */
  1485. char *q;
  1486. if (stream || feed) {
  1487. fprintf(stderr, "%s:%d: Already in a tag\n",
  1488. filename, line_num);
  1489. } else {
  1490. feed = av_mallocz(sizeof(FFStream));
  1491. /* add in stream list */
  1492. *last_stream = feed;
  1493. last_stream = &feed->next;
  1494. /* add in feed list */
  1495. *last_feed = feed;
  1496. last_feed = &feed->next_feed;
  1497. get_arg(feed->filename, sizeof(feed->filename), &p);
  1498. q = strrchr(feed->filename, '>');
  1499. if (*q)
  1500. *q = '\0';
  1501. feed->fmt = guess_format("ffm", NULL, NULL);
  1502. /* defaut feed file */
  1503. snprintf(feed->feed_filename, sizeof(feed->feed_filename),
  1504. "/tmp/%s.ffm", feed->filename);
  1505. feed->feed_max_size = 5 * 1024 * 1024;
  1506. feed->is_feed = 1;
  1507. feed->feed = feed; /* self feeding :-) */
  1508. }
  1509. } else if (!strcasecmp(cmd, "File")) {
  1510. if (feed) {
  1511. get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
  1512. } else if (stream) {
  1513. get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
  1514. }
  1515. } else if (!strcasecmp(cmd, "FileMaxSize")) {
  1516. if (feed) {
  1517. const char *p1;
  1518. double fsize;
  1519. get_arg(arg, sizeof(arg), &p);
  1520. p1 = arg;
  1521. fsize = strtod(p1, (char **)&p1);
  1522. switch(toupper(*p1)) {
  1523. case 'K':
  1524. fsize *= 1024;
  1525. break;
  1526. case 'M':
  1527. fsize *= 1024 * 1024;
  1528. break;
  1529. case 'G':
  1530. fsize *= 1024 * 1024 * 1024;
  1531. break;
  1532. }
  1533. feed->feed_max_size = (INT64)fsize;
  1534. }
  1535. } else if (!strcasecmp(cmd, "</Feed>")) {
  1536. if (!feed) {
  1537. fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
  1538. filename, line_num);
  1539. errors++;
  1540. } else {
  1541. /* Make sure that we start out clean */
  1542. if (unlink(feed->feed_filename) < 0
  1543. && errno != ENOENT) {
  1544. fprintf(stderr, "%s:%d: Unable to clean old feed file '%s': %s\n",
  1545. filename, line_num, feed->feed_filename, strerror(errno));
  1546. errors++;
  1547. }
  1548. }
  1549. feed = NULL;
  1550. } else if (!strcasecmp(cmd, "<Stream")) {
  1551. /*********************************************/
  1552. /* Stream related options */
  1553. char *q;
  1554. if (stream || feed) {
  1555. fprintf(stderr, "%s:%d: Already in a tag\n",
  1556. filename, line_num);
  1557. } else {
  1558. stream = av_mallocz(sizeof(FFStream));
  1559. *last_stream = stream;
  1560. last_stream = &stream->next;
  1561. get_arg(stream->filename, sizeof(stream->filename), &p);
  1562. q = strrchr(stream->filename, '>');
  1563. if (*q)
  1564. *q = '\0';
  1565. stream->fmt = guess_format(NULL, stream->filename, NULL);
  1566. memset(&audio_enc, 0, sizeof(AVCodecContext));
  1567. memset(&video_enc, 0, sizeof(AVCodecContext));
  1568. audio_id = CODEC_ID_NONE;
  1569. video_id = CODEC_ID_NONE;
  1570. if (stream->fmt) {
  1571. audio_id = stream->fmt->audio_codec;
  1572. video_id = stream->fmt->video_codec;
  1573. }
  1574. }
  1575. } else if (!strcasecmp(cmd, "Feed")) {
  1576. get_arg(arg, sizeof(arg), &p);
  1577. if (stream) {
  1578. FFStream *sfeed;
  1579. sfeed = first_feed;
  1580. while (sfeed != NULL) {
  1581. if (!strcmp(sfeed->filename, arg))
  1582. break;
  1583. sfeed = sfeed->next_feed;
  1584. }
  1585. if (!sfeed) {
  1586. fprintf(stderr, "%s:%d: feed '%s' not defined\n",
  1587. filename, line_num, arg);
  1588. } else {
  1589. stream->feed = sfeed;
  1590. }
  1591. }
  1592. } else if (!strcasecmp(cmd, "Format")) {
  1593. get_arg(arg, sizeof(arg), &p);
  1594. if (!strcmp(arg, "status")) {
  1595. stream->stream_type = STREAM_TYPE_STATUS;
  1596. stream->fmt = NULL;
  1597. } else {
  1598. stream->stream_type = STREAM_TYPE_LIVE;
  1599. /* jpeg cannot be used here, so use single frame jpeg */
  1600. if (!strcmp(arg, "jpeg"))
  1601. strcpy(arg, "singlejpeg");
  1602. stream->fmt = guess_format(arg, NULL, NULL);
  1603. if (!stream->fmt) {
  1604. fprintf(stderr, "%s:%d: Unknown Format: %s\n",
  1605. filename, line_num, arg);
  1606. errors++;
  1607. }
  1608. }
  1609. if (stream->fmt) {
  1610. audio_id = stream->fmt->audio_codec;
  1611. video_id = stream->fmt->video_codec;
  1612. }
  1613. } else if (!strcasecmp(cmd, "Preroll")) {
  1614. get_arg(arg, sizeof(arg), &p);
  1615. if (stream) {
  1616. stream->prebuffer = atoi(arg) * 1000;
  1617. }
  1618. } else if (!strcasecmp(cmd, "StartSendOnKey")) {
  1619. if (stream) {
  1620. stream->send_on_key = 1;
  1621. }
  1622. } else if (!strcasecmp(cmd, "AudioCodec")) {
  1623. get_arg(arg, sizeof(arg), &p);
  1624. audio_id = opt_audio_codec(arg);
  1625. if (audio_id == CODEC_ID_NONE) {
  1626. fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
  1627. filename, line_num, arg);
  1628. errors++;
  1629. }
  1630. } else if (!strcasecmp(cmd, "VideoCodec")) {
  1631. get_arg(arg, sizeof(arg), &p);
  1632. video_id = opt_video_codec(arg);
  1633. if (video_id == CODEC_ID_NONE) {
  1634. fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
  1635. filename, line_num, arg);
  1636. errors++;
  1637. }
  1638. } else if (!strcasecmp(cmd, "MaxTime")) {
  1639. get_arg(arg, sizeof(arg), &p);
  1640. if (stream) {
  1641. stream->max_time = atoi(arg);
  1642. }
  1643. } else if (!strcasecmp(cmd, "AudioBitRate")) {
  1644. get_arg(arg, sizeof(arg), &p);
  1645. if (stream) {
  1646. audio_enc.bit_rate = atoi(arg) * 1000;
  1647. }
  1648. } else if (!strcasecmp(cmd, "AudioChannels")) {
  1649. get_arg(arg, sizeof(arg), &p);
  1650. if (stream) {
  1651. audio_enc.channels = atoi(arg);
  1652. }
  1653. } else if (!strcasecmp(cmd, "AudioSampleRate")) {
  1654. get_arg(arg, sizeof(arg), &p);
  1655. if (stream) {
  1656. audio_enc.sample_rate = atoi(arg);
  1657. }
  1658. } else if (!strcasecmp(cmd, "VideoBitRate")) {
  1659. get_arg(arg, sizeof(arg), &p);
  1660. if (stream) {
  1661. video_enc.bit_rate = atoi(arg) * 1000;
  1662. }
  1663. } else if (!strcasecmp(cmd, "VideoSize")) {
  1664. get_arg(arg, sizeof(arg), &p);
  1665. if (stream) {
  1666. parse_image_size(&video_enc.width, &video_enc.height, arg);
  1667. if ((video_enc.width % 16) != 0 ||
  1668. (video_enc.height % 16) != 0) {
  1669. fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
  1670. filename, line_num);
  1671. errors++;
  1672. }
  1673. }
  1674. } else if (!strcasecmp(cmd, "VideoFrameRate")) {
  1675. get_arg(arg, sizeof(arg), &p);
  1676. if (stream) {
  1677. video_enc.frame_rate = (int)(strtod(arg, NULL) * FRAME_RATE_BASE);
  1678. }
  1679. } else if (!strcasecmp(cmd, "VideoGopSize")) {
  1680. get_arg(arg, sizeof(arg), &p);
  1681. if (stream) {
  1682. video_enc.gop_size = atoi(arg);
  1683. }
  1684. } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
  1685. if (stream) {
  1686. video_enc.gop_size = 1;
  1687. }
  1688. } else if (!strcasecmp(cmd, "VideoHighQuality")) {
  1689. if (stream) {
  1690. video_enc.flags |= CODEC_FLAG_HQ;
  1691. }
  1692. } else if (!strcasecmp(cmd, "VideoQDiff")) {
  1693. if (stream) {
  1694. video_enc.max_qdiff = atoi(arg);
  1695. if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
  1696. fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
  1697. filename, line_num);
  1698. errors++;
  1699. }
  1700. }
  1701. } else if (!strcasecmp(cmd, "VideoQMax")) {
  1702. if (stream) {
  1703. video_enc.qmax = atoi(arg);
  1704. if (video_enc.qmax < 1 || video_enc.qmax > 31) {
  1705. fprintf(stderr, "%s:%d: VideoQMax out of range\n",
  1706. filename, line_num);
  1707. errors++;
  1708. }
  1709. }
  1710. } else if (!strcasecmp(cmd, "VideoQMin")) {
  1711. if (stream) {
  1712. video_enc.qmin = atoi(arg);
  1713. if (video_enc.qmin < 1 || video_enc.qmin > 31) {
  1714. fprintf(stderr, "%s:%d: VideoQMin out of range\n",
  1715. filename, line_num);
  1716. errors++;
  1717. }
  1718. }
  1719. } else if (!strcasecmp(cmd, "NoVideo")) {
  1720. video_id = CODEC_ID_NONE;
  1721. } else if (!strcasecmp(cmd, "NoAudio")) {
  1722. audio_id = CODEC_ID_NONE;
  1723. } else if (!strcasecmp(cmd, "</Stream>")) {
  1724. if (!stream) {
  1725. fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
  1726. filename, line_num);
  1727. errors++;
  1728. }
  1729. if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
  1730. if (audio_id != CODEC_ID_NONE) {
  1731. audio_enc.codec_type = CODEC_TYPE_AUDIO;
  1732. audio_enc.codec_id = audio_id;
  1733. add_codec(stream, &audio_enc);
  1734. }
  1735. if (video_id != CODEC_ID_NONE) {
  1736. video_enc.codec_type = CODEC_TYPE_VIDEO;
  1737. video_enc.codec_id = video_id;
  1738. add_codec(stream, &video_enc);
  1739. }
  1740. }
  1741. stream = NULL;
  1742. } else {
  1743. fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
  1744. filename, line_num, cmd);
  1745. errors++;
  1746. }
  1747. }
  1748. fclose(f);
  1749. if (errors)
  1750. return -1;
  1751. else
  1752. return 0;
  1753. }
  1754. void *http_server_thread(void *arg)
  1755. {
  1756. http_server(my_addr);
  1757. return NULL;
  1758. }
  1759. #if 0
  1760. static void write_packet(FFCodec *ffenc,
  1761. UINT8 *buf, int size)
  1762. {
  1763. PacketHeader hdr;
  1764. AVCodecContext *enc = &ffenc->enc;
  1765. UINT8 *wptr;
  1766. mk_header(&hdr, enc, size);
  1767. wptr = http_fifo.wptr;
  1768. fifo_write(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &wptr);
  1769. fifo_write(&http_fifo, buf, size, &wptr);
  1770. /* atomic modification of wptr */
  1771. http_fifo.wptr = wptr;
  1772. ffenc->data_count += size;
  1773. ffenc->avg_frame_size = ffenc->avg_frame_size * AVG_COEF + size * (1.0 - AVG_COEF);
  1774. }
  1775. #endif
  1776. void help(void)
  1777. {
  1778. printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
  1779. "usage: ffserver [-L] [-h] [-f configfile]\n"
  1780. "Hyper fast multi format Audio/Video streaming server\n"
  1781. "\n"
  1782. "-L : print the LICENCE\n"
  1783. "-h : this help\n"
  1784. "-f configfile : use configfile instead of /etc/ffserver.conf\n"
  1785. );
  1786. }
  1787. void licence(void)
  1788. {
  1789. printf(
  1790. "ffserver version " FFMPEG_VERSION "\n"
  1791. "Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
  1792. "This program is free software; you can redistribute it and/or modify\n"
  1793. "it under the terms of the GNU General Public License as published by\n"
  1794. "the Free Software Foundation; either version 2 of the License, or\n"
  1795. "(at your option) any later version.\n"
  1796. "\n"
  1797. "This program is distributed in the hope that it will be useful,\n"
  1798. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1799. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1800. "GNU General Public License for more details.\n"
  1801. "\n"
  1802. "You should have received a copy of the GNU General Public License\n"
  1803. "along with this program; if not, write to the Free Software\n"
  1804. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  1805. );
  1806. }
  1807. int main(int argc, char **argv)
  1808. {
  1809. const char *config_filename;
  1810. int c;
  1811. register_all();
  1812. config_filename = "/etc/ffserver.conf";
  1813. for(;;) {
  1814. c = getopt_long_only(argc, argv, "Lh?f:", NULL, NULL);
  1815. if (c == -1)
  1816. break;
  1817. switch(c) {
  1818. case 'L':
  1819. licence();
  1820. exit(1);
  1821. case '?':
  1822. case 'h':
  1823. help();
  1824. exit(1);
  1825. case 'f':
  1826. config_filename = optarg;
  1827. break;
  1828. default:
  1829. exit(2);
  1830. }
  1831. }
  1832. /* address on which the server will handle connections */
  1833. my_addr.sin_family = AF_INET;
  1834. my_addr.sin_port = htons (8080);
  1835. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  1836. nb_max_connections = 5;
  1837. nb_max_bandwidth = 1000;
  1838. first_stream = NULL;
  1839. logfilename[0] = '\0';
  1840. if (parse_ffconfig(config_filename) < 0) {
  1841. fprintf(stderr, "Incorrect config file - exiting.\n");
  1842. exit(1);
  1843. }
  1844. build_feed_streams();
  1845. /* signal init */
  1846. signal(SIGPIPE, SIG_IGN);
  1847. /* open log file if needed */
  1848. if (logfilename[0] != '\0') {
  1849. if (!strcmp(logfilename, "-"))
  1850. logfile = stdout;
  1851. else
  1852. logfile = fopen(logfilename, "w");
  1853. }
  1854. if (http_server(my_addr) < 0) {
  1855. fprintf(stderr, "Could start http server\n");
  1856. exit(1);
  1857. }
  1858. return 0;
  1859. }