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.

2067 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<th align=left>Parameters\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. char parameters[64];
  775. parameters[0] = 0;
  776. switch(st->codec.codec_type) {
  777. case CODEC_TYPE_AUDIO:
  778. type = "audio";
  779. break;
  780. case CODEC_TYPE_VIDEO:
  781. type = "video";
  782. sprintf(parameters, "%dx%d, q=%d-%d", st->codec.width, st->codec.height,
  783. st->codec.qmin, st->codec.qmax);
  784. break;
  785. default:
  786. av_abort();
  787. }
  788. q += sprintf(q, "<tr><td align=right>%d<td>%s<td align=right>%d<td>%s<td>%s\n",
  789. i, type, st->codec.bit_rate/1000, codec ? codec->name : "", parameters);
  790. }
  791. q += sprintf(q, "</table>\n");
  792. }
  793. stream = stream->next;
  794. }
  795. #if 0
  796. {
  797. float avg;
  798. AVCodecContext *enc;
  799. char buf[1024];
  800. /* feed status */
  801. stream = first_feed;
  802. while (stream != NULL) {
  803. q += sprintf(q, "<H1>Feed '%s'</H1>\n", stream->filename);
  804. q += sprintf(q, "<TABLE>\n");
  805. q += sprintf(q, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
  806. for(i=0;i<stream->nb_streams;i++) {
  807. AVStream *st = stream->streams[i];
  808. FeedData *fdata = st->priv_data;
  809. enc = &st->codec;
  810. avcodec_string(buf, sizeof(buf), enc);
  811. avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
  812. if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
  813. avg /= enc->frame_size;
  814. q += sprintf(q, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n",
  815. buf, enc->frame_number, fdata->data_count, avg / 1000.0);
  816. }
  817. q += sprintf(q, "</TABLE>\n");
  818. stream = stream->next_feed;
  819. }
  820. }
  821. #endif
  822. /* connection status */
  823. q += sprintf(q, "<H2>Connection Status</H2>\n");
  824. q += sprintf(q, "Number of connections: %d / %d<BR>\n",
  825. nb_connections, nb_max_connections);
  826. q += sprintf(q, "Bandwidth in use: %dk / %dk<BR>\n",
  827. nb_bandwidth, nb_max_bandwidth);
  828. q += sprintf(q, "<TABLE>\n");
  829. q += sprintf(q, "<TR><TD>#<TD>File<TD>IP<TD>State<TD>Size\n");
  830. c1 = first_http_ctx;
  831. i = 0;
  832. while (c1 != NULL && q < (char *) c->buffer + sizeof(c->buffer) - 2048) {
  833. i++;
  834. p = inet_ntoa(c1->from_addr.sin_addr);
  835. q += sprintf(q, "<TR><TD><B>%d</B><TD>%s%s <TD> %s <TD> %s <TD> %Ld\n",
  836. i, c1->stream->filename,
  837. c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
  838. p,
  839. http_state[c1->state],
  840. c1->data_count);
  841. c1 = c1->next;
  842. }
  843. q += sprintf(q, "</TABLE>\n");
  844. /* date */
  845. ti = time(NULL);
  846. p = ctime(&ti);
  847. q += sprintf(q, "<HR size=1 noshade>Generated at %s", p);
  848. q += sprintf(q, "</BODY>\n</HTML>\n");
  849. c->buffer_ptr = c->buffer;
  850. c->buffer_end = q;
  851. }
  852. static void http_write_packet(void *opaque,
  853. unsigned char *buf, int size)
  854. {
  855. HTTPContext *c = opaque;
  856. if (c->buffer_ptr == c->buffer_end || !c->buffer_ptr)
  857. c->buffer_ptr = c->buffer_end = c->buffer;
  858. if (c->buffer_end - c->buffer + size > IOBUFFER_MAX_SIZE)
  859. av_abort();
  860. memcpy(c->buffer_end, buf, size);
  861. c->buffer_end += size;
  862. }
  863. static int open_input_stream(HTTPContext *c, const char *info)
  864. {
  865. char buf[128];
  866. char input_filename[1024];
  867. AVFormatContext *s;
  868. int buf_size;
  869. INT64 stream_pos;
  870. /* find file name */
  871. if (c->stream->feed) {
  872. strcpy(input_filename, c->stream->feed->feed_filename);
  873. buf_size = FFM_PACKET_SIZE;
  874. /* compute position (absolute time) */
  875. if (find_info_tag(buf, sizeof(buf), "date", info)) {
  876. stream_pos = parse_date(buf, 0);
  877. } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
  878. int prebuffer = strtol(buf, 0, 10);
  879. stream_pos = gettime() - prebuffer * 1000000;
  880. } else {
  881. stream_pos = gettime() - c->stream->prebuffer * 1000;
  882. }
  883. } else {
  884. strcpy(input_filename, c->stream->feed_filename);
  885. buf_size = 0;
  886. /* compute position (relative time) */
  887. if (find_info_tag(buf, sizeof(buf), "date", info)) {
  888. stream_pos = parse_date(buf, 1);
  889. } else {
  890. stream_pos = 0;
  891. }
  892. }
  893. if (input_filename[0] == '\0')
  894. return -1;
  895. /* open stream */
  896. if (av_open_input_file(&s, input_filename, NULL, buf_size, NULL) < 0)
  897. return -1;
  898. c->fmt_in = s;
  899. if (c->fmt_in->iformat->read_seek) {
  900. c->fmt_in->iformat->read_seek(c->fmt_in, stream_pos);
  901. }
  902. // printf("stream %s opened pos=%0.6f\n", input_filename, stream_pos / 1000000.0);
  903. return 0;
  904. }
  905. static int http_prepare_data(HTTPContext *c)
  906. {
  907. int i;
  908. switch(c->state) {
  909. case HTTPSTATE_SEND_DATA_HEADER:
  910. memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
  911. if (c->stream->feed) {
  912. /* open output stream by using specified codecs */
  913. c->fmt_ctx.oformat = c->stream->fmt;
  914. c->fmt_ctx.nb_streams = c->stream->nb_streams;
  915. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  916. AVStream *st;
  917. st = av_mallocz(sizeof(AVStream));
  918. c->fmt_ctx.streams[i] = st;
  919. if (c->stream->feed == c->stream)
  920. memcpy(st, c->stream->streams[i], sizeof(AVStream));
  921. else
  922. memcpy(st, c->stream->feed->streams[c->stream->feed_streams[i]], sizeof(AVStream));
  923. st->codec.frame_number = 0; /* XXX: should be done in
  924. AVStream, not in codec */
  925. }
  926. c->got_key_frame = 0;
  927. } else {
  928. /* open output stream by using codecs in specified file */
  929. c->fmt_ctx.oformat = c->stream->fmt;
  930. c->fmt_ctx.nb_streams = c->fmt_in->nb_streams;
  931. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  932. AVStream *st;
  933. st = av_mallocz(sizeof(AVStream));
  934. c->fmt_ctx.streams[i] = st;
  935. memcpy(st, c->fmt_in->streams[i], sizeof(AVStream));
  936. st->codec.frame_number = 0; /* XXX: should be done in
  937. AVStream, not in codec */
  938. }
  939. c->got_key_frame = 0;
  940. }
  941. init_put_byte(&c->fmt_ctx.pb, c->pbuffer, PACKET_MAX_SIZE,
  942. 1, c, NULL, http_write_packet, NULL);
  943. c->fmt_ctx.pb.is_streamed = 1;
  944. /* prepare header */
  945. av_write_header(&c->fmt_ctx);
  946. c->state = HTTPSTATE_SEND_DATA;
  947. c->last_packet_sent = 0;
  948. break;
  949. case HTTPSTATE_SEND_DATA:
  950. /* find a new packet */
  951. #if 0
  952. fifo_total_size = http_fifo_write_count - c->last_http_fifo_write_count;
  953. if (fifo_total_size >= ((3 * FIFO_MAX_SIZE) / 4)) {
  954. /* overflow : resync. We suppose that wptr is at this
  955. point a pointer to a valid packet */
  956. c->rptr = http_fifo.wptr;
  957. c->got_key_frame = 0;
  958. }
  959. start_rptr = c->rptr;
  960. if (fifo_read(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &c->rptr) < 0)
  961. return 0;
  962. payload_size = ntohs(hdr.payload_size);
  963. payload = av_malloc(payload_size);
  964. if (fifo_read(&http_fifo, payload, payload_size, &c->rptr) < 0) {
  965. /* cannot read all the payload */
  966. av_free(payload);
  967. c->rptr = start_rptr;
  968. return 0;
  969. }
  970. c->last_http_fifo_write_count = http_fifo_write_count -
  971. fifo_size(&http_fifo, c->rptr);
  972. if (c->stream->stream_type != STREAM_TYPE_MASTER) {
  973. /* test if the packet can be handled by this format */
  974. ret = 0;
  975. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  976. AVStream *st = c->fmt_ctx.streams[i];
  977. if (test_header(&hdr, &st->codec)) {
  978. /* only begin sending when got a key frame */
  979. if (st->codec.key_frame)
  980. c->got_key_frame |= 1 << i;
  981. if (c->got_key_frame & (1 << i)) {
  982. ret = c->fmt_ctx.format->write_packet(&c->fmt_ctx, i,
  983. payload, payload_size);
  984. }
  985. break;
  986. }
  987. }
  988. if (ret) {
  989. /* must send trailer now */
  990. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  991. }
  992. } else {
  993. /* master case : send everything */
  994. char *q;
  995. q = c->buffer;
  996. memcpy(q, &hdr, sizeof(hdr));
  997. q += sizeof(hdr);
  998. memcpy(q, payload, payload_size);
  999. q += payload_size;
  1000. c->buffer_ptr = c->buffer;
  1001. c->buffer_end = q;
  1002. }
  1003. av_free(payload);
  1004. #endif
  1005. {
  1006. AVPacket pkt;
  1007. /* read a packet from the input stream */
  1008. if (c->stream->feed) {
  1009. ffm_set_write_index(c->fmt_in,
  1010. c->stream->feed->feed_write_index,
  1011. c->stream->feed->feed_size);
  1012. }
  1013. if (c->stream->max_time &&
  1014. c->stream->max_time + c->start_time > time(0)) {
  1015. /* We have timed out */
  1016. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  1017. } else if (av_read_packet(c->fmt_in, &pkt) < 0) {
  1018. if (c->stream->feed && c->stream->feed->feed_opened) {
  1019. /* if coming from feed, it means we reached the end of the
  1020. ffm file, so must wait for more data */
  1021. c->state = HTTPSTATE_WAIT_FEED;
  1022. return 1; /* state changed */
  1023. } else {
  1024. /* must send trailer now because eof or error */
  1025. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  1026. }
  1027. } else {
  1028. /* send it to the appropriate stream */
  1029. if (c->stream->feed) {
  1030. /* if coming from a feed, select the right stream */
  1031. for(i=0;i<c->stream->nb_streams;i++) {
  1032. if (c->stream->feed_streams[i] == pkt.stream_index) {
  1033. pkt.stream_index = i;
  1034. if (pkt.flags & PKT_FLAG_KEY) {
  1035. c->got_key_frame |= 1 << i;
  1036. }
  1037. /* See if we have all the key frames, then
  1038. * we start to send. This logic is not quite
  1039. * right, but it works for the case of a
  1040. * single video stream with one or more
  1041. * audio streams (for which every frame is
  1042. * typically a key frame).
  1043. */
  1044. if (!c->stream->send_on_key || ((c->got_key_frame + 1) >> c->stream->nb_streams)) {
  1045. goto send_it;
  1046. }
  1047. }
  1048. }
  1049. } else {
  1050. AVCodecContext *codec;
  1051. send_it:
  1052. /* Fudge here */
  1053. codec = &c->fmt_ctx.streams[pkt.stream_index]->codec;
  1054. codec->key_frame = ((pkt.flags & PKT_FLAG_KEY) != 0);
  1055. #ifdef PJSG
  1056. if (codec->codec_type == CODEC_TYPE_AUDIO) {
  1057. codec->frame_size = (codec->sample_rate * pkt.duration + 500000) / 1000000;
  1058. /* printf("Calculated size %d, from sr %d, duration %d\n", codec->frame_size, codec->sample_rate, pkt.duration); */
  1059. }
  1060. #endif
  1061. if (av_write_packet(&c->fmt_ctx, &pkt, 0))
  1062. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  1063. codec->frame_number++;
  1064. }
  1065. av_free_packet(&pkt);
  1066. }
  1067. }
  1068. break;
  1069. default:
  1070. case HTTPSTATE_SEND_DATA_TRAILER:
  1071. /* last packet test ? */
  1072. if (c->last_packet_sent)
  1073. return -1;
  1074. /* prepare header */
  1075. av_write_trailer(&c->fmt_ctx);
  1076. c->last_packet_sent = 1;
  1077. break;
  1078. }
  1079. return 0;
  1080. }
  1081. /* should convert the format at the same time */
  1082. static int http_send_data(HTTPContext *c)
  1083. {
  1084. int len, ret;
  1085. while (c->buffer_ptr >= c->buffer_end) {
  1086. ret = http_prepare_data(c);
  1087. if (ret < 0)
  1088. return -1;
  1089. else if (ret == 0) {
  1090. continue;
  1091. } else {
  1092. /* state change requested */
  1093. return 0;
  1094. }
  1095. }
  1096. if (c->buffer_end > c->buffer_ptr) {
  1097. len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  1098. if (len < 0) {
  1099. if (errno != EAGAIN && errno != EINTR) {
  1100. /* error : close connection */
  1101. return -1;
  1102. }
  1103. } else {
  1104. c->buffer_ptr += len;
  1105. c->data_count += len;
  1106. if (c->stream)
  1107. c->stream->bytes_served += len;
  1108. }
  1109. }
  1110. return 0;
  1111. }
  1112. static int http_start_receive_data(HTTPContext *c)
  1113. {
  1114. int fd;
  1115. if (c->stream->feed_opened)
  1116. return -1;
  1117. /* open feed */
  1118. fd = open(c->stream->feed_filename, O_RDWR);
  1119. if (fd < 0)
  1120. return -1;
  1121. c->feed_fd = fd;
  1122. c->stream->feed_write_index = ffm_read_write_index(fd);
  1123. c->stream->feed_size = lseek(fd, 0, SEEK_END);
  1124. lseek(fd, 0, SEEK_SET);
  1125. /* init buffer input */
  1126. c->buffer_ptr = c->buffer;
  1127. c->buffer_end = c->buffer + FFM_PACKET_SIZE;
  1128. c->stream->feed_opened = 1;
  1129. return 0;
  1130. }
  1131. static int http_receive_data(HTTPContext *c)
  1132. {
  1133. HTTPContext *c1;
  1134. if (c->buffer_end > c->buffer_ptr) {
  1135. int len;
  1136. len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  1137. if (len < 0) {
  1138. if (errno != EAGAIN && errno != EINTR) {
  1139. /* error : close connection */
  1140. goto fail;
  1141. }
  1142. } else if (len == 0) {
  1143. /* end of connection : close it */
  1144. goto fail;
  1145. } else {
  1146. c->buffer_ptr += len;
  1147. c->data_count += len;
  1148. }
  1149. }
  1150. if (c->buffer_ptr >= c->buffer_end) {
  1151. FFStream *feed = c->stream;
  1152. /* a packet has been received : write it in the store, except
  1153. if header */
  1154. if (c->data_count > FFM_PACKET_SIZE) {
  1155. // printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
  1156. /* XXX: use llseek or url_seek */
  1157. lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
  1158. write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
  1159. feed->feed_write_index += FFM_PACKET_SIZE;
  1160. /* update file size */
  1161. if (feed->feed_write_index > c->stream->feed_size)
  1162. feed->feed_size = feed->feed_write_index;
  1163. /* handle wrap around if max file size reached */
  1164. if (feed->feed_write_index >= c->stream->feed_max_size)
  1165. feed->feed_write_index = FFM_PACKET_SIZE;
  1166. /* write index */
  1167. ffm_write_write_index(c->feed_fd, feed->feed_write_index);
  1168. /* wake up any waiting connections */
  1169. for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
  1170. if (c1->state == HTTPSTATE_WAIT_FEED &&
  1171. c1->stream->feed == c->stream->feed) {
  1172. c1->state = HTTPSTATE_SEND_DATA;
  1173. }
  1174. }
  1175. } else {
  1176. /* We have a header in our hands that contains useful data */
  1177. AVFormatContext s;
  1178. AVInputFormat *fmt_in;
  1179. ByteIOContext *pb = &s.pb;
  1180. int i;
  1181. memset(&s, 0, sizeof(s));
  1182. url_open_buf(pb, c->buffer, c->buffer_end - c->buffer, URL_RDONLY);
  1183. pb->buf_end = c->buffer_end; /* ?? */
  1184. pb->is_streamed = 1;
  1185. /* use feed output format name to find corresponding input format */
  1186. fmt_in = av_find_input_format(feed->fmt->name);
  1187. if (!fmt_in)
  1188. goto fail;
  1189. s.priv_data = av_mallocz(fmt_in->priv_data_size);
  1190. if (!s.priv_data)
  1191. goto fail;
  1192. if (fmt_in->read_header(&s, 0) < 0) {
  1193. av_freep(&s.priv_data);
  1194. goto fail;
  1195. }
  1196. /* Now we have the actual streams */
  1197. if (s.nb_streams != feed->nb_streams) {
  1198. av_freep(&s.priv_data);
  1199. goto fail;
  1200. }
  1201. for (i = 0; i < s.nb_streams; i++) {
  1202. memcpy(&feed->streams[i]->codec,
  1203. &s.streams[i]->codec, sizeof(AVCodecContext));
  1204. }
  1205. av_freep(&s.priv_data);
  1206. }
  1207. c->buffer_ptr = c->buffer;
  1208. }
  1209. return 0;
  1210. fail:
  1211. c->stream->feed_opened = 0;
  1212. close(c->feed_fd);
  1213. return -1;
  1214. }
  1215. /* return the stream number in the feed */
  1216. int add_av_stream(FFStream *feed,
  1217. AVStream *st)
  1218. {
  1219. AVStream *fst;
  1220. AVCodecContext *av, *av1;
  1221. int i;
  1222. av = &st->codec;
  1223. for(i=0;i<feed->nb_streams;i++) {
  1224. st = feed->streams[i];
  1225. av1 = &st->codec;
  1226. if (av1->codec_id == av->codec_id &&
  1227. av1->codec_type == av->codec_type &&
  1228. av1->bit_rate == av->bit_rate) {
  1229. switch(av->codec_type) {
  1230. case CODEC_TYPE_AUDIO:
  1231. if (av1->channels == av->channels &&
  1232. av1->sample_rate == av->sample_rate)
  1233. goto found;
  1234. break;
  1235. case CODEC_TYPE_VIDEO:
  1236. if (av1->width == av->width &&
  1237. av1->height == av->height &&
  1238. av1->frame_rate == av->frame_rate &&
  1239. av1->gop_size == av->gop_size)
  1240. goto found;
  1241. break;
  1242. default:
  1243. av_abort();
  1244. }
  1245. }
  1246. }
  1247. fst = av_mallocz(sizeof(AVStream));
  1248. if (!fst)
  1249. return -1;
  1250. fst->priv_data = av_mallocz(sizeof(FeedData));
  1251. memcpy(&fst->codec, av, sizeof(AVCodecContext));
  1252. feed->streams[feed->nb_streams++] = fst;
  1253. return feed->nb_streams - 1;
  1254. found:
  1255. return i;
  1256. }
  1257. /* compute the needed AVStream for each feed */
  1258. void build_feed_streams(void)
  1259. {
  1260. FFStream *stream, *feed;
  1261. int i;
  1262. /* gather all streams */
  1263. for(stream = first_stream; stream != NULL; stream = stream->next) {
  1264. feed = stream->feed;
  1265. if (feed) {
  1266. if (!stream->is_feed) {
  1267. for(i=0;i<stream->nb_streams;i++) {
  1268. stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
  1269. }
  1270. } else {
  1271. for(i=0;i<stream->nb_streams;i++) {
  1272. stream->feed_streams[i] = i;
  1273. }
  1274. }
  1275. }
  1276. }
  1277. /* create feed files if needed */
  1278. for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
  1279. int fd;
  1280. if (!url_exist(feed->feed_filename)) {
  1281. AVFormatContext s1, *s = &s1;
  1282. /* only write the header of the ffm file */
  1283. if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
  1284. fprintf(stderr, "Could not open output feed file '%s'\n",
  1285. feed->feed_filename);
  1286. exit(1);
  1287. }
  1288. s->oformat = feed->fmt;
  1289. s->nb_streams = feed->nb_streams;
  1290. for(i=0;i<s->nb_streams;i++) {
  1291. AVStream *st;
  1292. st = feed->streams[i];
  1293. s->streams[i] = st;
  1294. }
  1295. av_write_header(s);
  1296. /* XXX: need better api */
  1297. av_freep(&s->priv_data);
  1298. url_fclose(&s->pb);
  1299. }
  1300. /* get feed size and write index */
  1301. fd = open(feed->feed_filename, O_RDONLY);
  1302. if (fd < 0) {
  1303. fprintf(stderr, "Could not open output feed file '%s'\n",
  1304. feed->feed_filename);
  1305. exit(1);
  1306. }
  1307. feed->feed_write_index = ffm_read_write_index(fd);
  1308. feed->feed_size = lseek(fd, 0, SEEK_END);
  1309. /* ensure that we do not wrap before the end of file */
  1310. if (feed->feed_max_size < feed->feed_size)
  1311. feed->feed_max_size = feed->feed_size;
  1312. close(fd);
  1313. }
  1314. }
  1315. static void get_arg(char *buf, int buf_size, const char **pp)
  1316. {
  1317. const char *p;
  1318. char *q;
  1319. int quote;
  1320. p = *pp;
  1321. while (isspace(*p)) p++;
  1322. q = buf;
  1323. quote = 0;
  1324. if (*p == '\"' || *p == '\'')
  1325. quote = *p++;
  1326. for(;;) {
  1327. if (quote) {
  1328. if (*p == quote)
  1329. break;
  1330. } else {
  1331. if (isspace(*p))
  1332. break;
  1333. }
  1334. if (*p == '\0')
  1335. break;
  1336. if ((q - buf) < buf_size - 1)
  1337. *q++ = *p;
  1338. p++;
  1339. }
  1340. *q = '\0';
  1341. if (quote && *p == quote)
  1342. p++;
  1343. *pp = p;
  1344. }
  1345. /* add a codec and set the default parameters */
  1346. void add_codec(FFStream *stream, AVCodecContext *av)
  1347. {
  1348. AVStream *st;
  1349. /* compute default parameters */
  1350. switch(av->codec_type) {
  1351. case CODEC_TYPE_AUDIO:
  1352. if (av->bit_rate == 0)
  1353. av->bit_rate = 64000;
  1354. if (av->sample_rate == 0)
  1355. av->sample_rate = 22050;
  1356. if (av->channels == 0)
  1357. av->channels = 1;
  1358. break;
  1359. case CODEC_TYPE_VIDEO:
  1360. if (av->bit_rate == 0)
  1361. av->bit_rate = 64000;
  1362. if (av->frame_rate == 0)
  1363. av->frame_rate = 5 * FRAME_RATE_BASE;
  1364. if (av->width == 0 || av->height == 0) {
  1365. av->width = 160;
  1366. av->height = 128;
  1367. }
  1368. /* Bitrate tolerance is less for streaming */
  1369. if (av->bit_rate_tolerance == 0)
  1370. av->bit_rate_tolerance = av->bit_rate / 4;
  1371. if (av->qmin == 0)
  1372. av->qmin = 3;
  1373. if (av->qmax == 0)
  1374. av->qmax = 31;
  1375. if (av->max_qdiff == 0)
  1376. av->max_qdiff = 3;
  1377. av->qcompress = 0.5;
  1378. av->qblur = 0.5;
  1379. break;
  1380. default:
  1381. av_abort();
  1382. }
  1383. st = av_mallocz(sizeof(AVStream));
  1384. if (!st)
  1385. return;
  1386. stream->streams[stream->nb_streams++] = st;
  1387. memcpy(&st->codec, av, sizeof(AVCodecContext));
  1388. }
  1389. int opt_audio_codec(const char *arg)
  1390. {
  1391. AVCodec *p;
  1392. p = first_avcodec;
  1393. while (p) {
  1394. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1395. break;
  1396. p = p->next;
  1397. }
  1398. if (p == NULL) {
  1399. return CODEC_ID_NONE;
  1400. }
  1401. return p->id;
  1402. }
  1403. int opt_video_codec(const char *arg)
  1404. {
  1405. AVCodec *p;
  1406. p = first_avcodec;
  1407. while (p) {
  1408. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1409. break;
  1410. p = p->next;
  1411. }
  1412. if (p == NULL) {
  1413. return CODEC_ID_NONE;
  1414. }
  1415. return p->id;
  1416. }
  1417. int parse_ffconfig(const char *filename)
  1418. {
  1419. FILE *f;
  1420. char line[1024];
  1421. char cmd[64];
  1422. char arg[1024];
  1423. const char *p;
  1424. int val, errors, line_num;
  1425. FFStream **last_stream, *stream;
  1426. FFStream **last_feed, *feed;
  1427. AVCodecContext audio_enc, video_enc;
  1428. int audio_id, video_id;
  1429. f = fopen(filename, "r");
  1430. if (!f) {
  1431. perror(filename);
  1432. return -1;
  1433. }
  1434. errors = 0;
  1435. line_num = 0;
  1436. first_stream = NULL;
  1437. last_stream = &first_stream;
  1438. first_feed = NULL;
  1439. last_feed = &first_feed;
  1440. stream = NULL;
  1441. feed = NULL;
  1442. audio_id = CODEC_ID_NONE;
  1443. video_id = CODEC_ID_NONE;
  1444. for(;;) {
  1445. if (fgets(line, sizeof(line), f) == NULL)
  1446. break;
  1447. line_num++;
  1448. p = line;
  1449. while (isspace(*p))
  1450. p++;
  1451. if (*p == '\0' || *p == '#')
  1452. continue;
  1453. get_arg(cmd, sizeof(cmd), &p);
  1454. if (!strcasecmp(cmd, "Port")) {
  1455. get_arg(arg, sizeof(arg), &p);
  1456. my_addr.sin_port = htons (atoi(arg));
  1457. } else if (!strcasecmp(cmd, "BindAddress")) {
  1458. get_arg(arg, sizeof(arg), &p);
  1459. if (!inet_aton(arg, &my_addr.sin_addr)) {
  1460. fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
  1461. filename, line_num, arg);
  1462. errors++;
  1463. }
  1464. } else if (!strcasecmp(cmd, "MaxClients")) {
  1465. get_arg(arg, sizeof(arg), &p);
  1466. val = atoi(arg);
  1467. if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
  1468. fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
  1469. filename, line_num, arg);
  1470. errors++;
  1471. } else {
  1472. nb_max_connections = val;
  1473. }
  1474. } else if (!strcasecmp(cmd, "MaxBandwidth")) {
  1475. get_arg(arg, sizeof(arg), &p);
  1476. val = atoi(arg);
  1477. if (val < 10 || val > 100000) {
  1478. fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
  1479. filename, line_num, arg);
  1480. errors++;
  1481. } else {
  1482. nb_max_bandwidth = val;
  1483. }
  1484. } else if (!strcasecmp(cmd, "CustomLog")) {
  1485. get_arg(logfilename, sizeof(logfilename), &p);
  1486. } else if (!strcasecmp(cmd, "<Feed")) {
  1487. /*********************************************/
  1488. /* Feed related options */
  1489. char *q;
  1490. if (stream || feed) {
  1491. fprintf(stderr, "%s:%d: Already in a tag\n",
  1492. filename, line_num);
  1493. } else {
  1494. feed = av_mallocz(sizeof(FFStream));
  1495. /* add in stream list */
  1496. *last_stream = feed;
  1497. last_stream = &feed->next;
  1498. /* add in feed list */
  1499. *last_feed = feed;
  1500. last_feed = &feed->next_feed;
  1501. get_arg(feed->filename, sizeof(feed->filename), &p);
  1502. q = strrchr(feed->filename, '>');
  1503. if (*q)
  1504. *q = '\0';
  1505. feed->fmt = guess_format("ffm", NULL, NULL);
  1506. /* defaut feed file */
  1507. snprintf(feed->feed_filename, sizeof(feed->feed_filename),
  1508. "/tmp/%s.ffm", feed->filename);
  1509. feed->feed_max_size = 5 * 1024 * 1024;
  1510. feed->is_feed = 1;
  1511. feed->feed = feed; /* self feeding :-) */
  1512. }
  1513. } else if (!strcasecmp(cmd, "File")) {
  1514. if (feed) {
  1515. get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
  1516. } else if (stream) {
  1517. get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
  1518. }
  1519. } else if (!strcasecmp(cmd, "FileMaxSize")) {
  1520. if (feed) {
  1521. const char *p1;
  1522. double fsize;
  1523. get_arg(arg, sizeof(arg), &p);
  1524. p1 = arg;
  1525. fsize = strtod(p1, (char **)&p1);
  1526. switch(toupper(*p1)) {
  1527. case 'K':
  1528. fsize *= 1024;
  1529. break;
  1530. case 'M':
  1531. fsize *= 1024 * 1024;
  1532. break;
  1533. case 'G':
  1534. fsize *= 1024 * 1024 * 1024;
  1535. break;
  1536. }
  1537. feed->feed_max_size = (INT64)fsize;
  1538. }
  1539. } else if (!strcasecmp(cmd, "</Feed>")) {
  1540. if (!feed) {
  1541. fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
  1542. filename, line_num);
  1543. errors++;
  1544. } else {
  1545. /* Make sure that we start out clean */
  1546. if (unlink(feed->feed_filename) < 0
  1547. && errno != ENOENT) {
  1548. fprintf(stderr, "%s:%d: Unable to clean old feed file '%s': %s\n",
  1549. filename, line_num, feed->feed_filename, strerror(errno));
  1550. errors++;
  1551. }
  1552. }
  1553. feed = NULL;
  1554. } else if (!strcasecmp(cmd, "<Stream")) {
  1555. /*********************************************/
  1556. /* Stream related options */
  1557. char *q;
  1558. if (stream || feed) {
  1559. fprintf(stderr, "%s:%d: Already in a tag\n",
  1560. filename, line_num);
  1561. } else {
  1562. stream = av_mallocz(sizeof(FFStream));
  1563. *last_stream = stream;
  1564. last_stream = &stream->next;
  1565. get_arg(stream->filename, sizeof(stream->filename), &p);
  1566. q = strrchr(stream->filename, '>');
  1567. if (*q)
  1568. *q = '\0';
  1569. stream->fmt = guess_format(NULL, stream->filename, NULL);
  1570. memset(&audio_enc, 0, sizeof(AVCodecContext));
  1571. memset(&video_enc, 0, sizeof(AVCodecContext));
  1572. audio_id = CODEC_ID_NONE;
  1573. video_id = CODEC_ID_NONE;
  1574. if (stream->fmt) {
  1575. audio_id = stream->fmt->audio_codec;
  1576. video_id = stream->fmt->video_codec;
  1577. }
  1578. }
  1579. } else if (!strcasecmp(cmd, "Feed")) {
  1580. get_arg(arg, sizeof(arg), &p);
  1581. if (stream) {
  1582. FFStream *sfeed;
  1583. sfeed = first_feed;
  1584. while (sfeed != NULL) {
  1585. if (!strcmp(sfeed->filename, arg))
  1586. break;
  1587. sfeed = sfeed->next_feed;
  1588. }
  1589. if (!sfeed) {
  1590. fprintf(stderr, "%s:%d: feed '%s' not defined\n",
  1591. filename, line_num, arg);
  1592. } else {
  1593. stream->feed = sfeed;
  1594. }
  1595. }
  1596. } else if (!strcasecmp(cmd, "Format")) {
  1597. get_arg(arg, sizeof(arg), &p);
  1598. if (!strcmp(arg, "status")) {
  1599. stream->stream_type = STREAM_TYPE_STATUS;
  1600. stream->fmt = NULL;
  1601. } else {
  1602. stream->stream_type = STREAM_TYPE_LIVE;
  1603. /* jpeg cannot be used here, so use single frame jpeg */
  1604. if (!strcmp(arg, "jpeg"))
  1605. strcpy(arg, "singlejpeg");
  1606. stream->fmt = guess_format(arg, NULL, NULL);
  1607. if (!stream->fmt) {
  1608. fprintf(stderr, "%s:%d: Unknown Format: %s\n",
  1609. filename, line_num, arg);
  1610. errors++;
  1611. }
  1612. }
  1613. if (stream->fmt) {
  1614. audio_id = stream->fmt->audio_codec;
  1615. video_id = stream->fmt->video_codec;
  1616. }
  1617. } else if (!strcasecmp(cmd, "Preroll")) {
  1618. get_arg(arg, sizeof(arg), &p);
  1619. if (stream) {
  1620. stream->prebuffer = atoi(arg) * 1000;
  1621. }
  1622. } else if (!strcasecmp(cmd, "StartSendOnKey")) {
  1623. if (stream) {
  1624. stream->send_on_key = 1;
  1625. }
  1626. } else if (!strcasecmp(cmd, "AudioCodec")) {
  1627. get_arg(arg, sizeof(arg), &p);
  1628. audio_id = opt_audio_codec(arg);
  1629. if (audio_id == CODEC_ID_NONE) {
  1630. fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
  1631. filename, line_num, arg);
  1632. errors++;
  1633. }
  1634. } else if (!strcasecmp(cmd, "VideoCodec")) {
  1635. get_arg(arg, sizeof(arg), &p);
  1636. video_id = opt_video_codec(arg);
  1637. if (video_id == CODEC_ID_NONE) {
  1638. fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
  1639. filename, line_num, arg);
  1640. errors++;
  1641. }
  1642. } else if (!strcasecmp(cmd, "MaxTime")) {
  1643. get_arg(arg, sizeof(arg), &p);
  1644. if (stream) {
  1645. stream->max_time = atoi(arg);
  1646. }
  1647. } else if (!strcasecmp(cmd, "AudioBitRate")) {
  1648. get_arg(arg, sizeof(arg), &p);
  1649. if (stream) {
  1650. audio_enc.bit_rate = atoi(arg) * 1000;
  1651. }
  1652. } else if (!strcasecmp(cmd, "AudioChannels")) {
  1653. get_arg(arg, sizeof(arg), &p);
  1654. if (stream) {
  1655. audio_enc.channels = atoi(arg);
  1656. }
  1657. } else if (!strcasecmp(cmd, "AudioSampleRate")) {
  1658. get_arg(arg, sizeof(arg), &p);
  1659. if (stream) {
  1660. audio_enc.sample_rate = atoi(arg);
  1661. }
  1662. } else if (!strcasecmp(cmd, "VideoBitRate")) {
  1663. get_arg(arg, sizeof(arg), &p);
  1664. if (stream) {
  1665. video_enc.bit_rate = atoi(arg) * 1000;
  1666. }
  1667. } else if (!strcasecmp(cmd, "VideoSize")) {
  1668. get_arg(arg, sizeof(arg), &p);
  1669. if (stream) {
  1670. parse_image_size(&video_enc.width, &video_enc.height, arg);
  1671. if ((video_enc.width % 16) != 0 ||
  1672. (video_enc.height % 16) != 0) {
  1673. fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
  1674. filename, line_num);
  1675. errors++;
  1676. }
  1677. }
  1678. } else if (!strcasecmp(cmd, "VideoFrameRate")) {
  1679. get_arg(arg, sizeof(arg), &p);
  1680. if (stream) {
  1681. video_enc.frame_rate = (int)(strtod(arg, NULL) * FRAME_RATE_BASE);
  1682. }
  1683. } else if (!strcasecmp(cmd, "VideoGopSize")) {
  1684. get_arg(arg, sizeof(arg), &p);
  1685. if (stream) {
  1686. video_enc.gop_size = atoi(arg);
  1687. }
  1688. } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
  1689. if (stream) {
  1690. video_enc.gop_size = 1;
  1691. }
  1692. } else if (!strcasecmp(cmd, "VideoHighQuality")) {
  1693. if (stream) {
  1694. video_enc.flags |= CODEC_FLAG_HQ;
  1695. }
  1696. } else if (!strcasecmp(cmd, "VideoQDiff")) {
  1697. if (stream) {
  1698. video_enc.max_qdiff = atoi(arg);
  1699. if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
  1700. fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
  1701. filename, line_num);
  1702. errors++;
  1703. }
  1704. }
  1705. } else if (!strcasecmp(cmd, "VideoQMax")) {
  1706. if (stream) {
  1707. video_enc.qmax = atoi(arg);
  1708. if (video_enc.qmax < 1 || video_enc.qmax > 31) {
  1709. fprintf(stderr, "%s:%d: VideoQMax out of range\n",
  1710. filename, line_num);
  1711. errors++;
  1712. }
  1713. }
  1714. } else if (!strcasecmp(cmd, "VideoQMin")) {
  1715. if (stream) {
  1716. video_enc.qmin = atoi(arg);
  1717. if (video_enc.qmin < 1 || video_enc.qmin > 31) {
  1718. fprintf(stderr, "%s:%d: VideoQMin out of range\n",
  1719. filename, line_num);
  1720. errors++;
  1721. }
  1722. }
  1723. } else if (!strcasecmp(cmd, "NoVideo")) {
  1724. video_id = CODEC_ID_NONE;
  1725. } else if (!strcasecmp(cmd, "NoAudio")) {
  1726. audio_id = CODEC_ID_NONE;
  1727. } else if (!strcasecmp(cmd, "</Stream>")) {
  1728. if (!stream) {
  1729. fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
  1730. filename, line_num);
  1731. errors++;
  1732. }
  1733. if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
  1734. if (audio_id != CODEC_ID_NONE) {
  1735. audio_enc.codec_type = CODEC_TYPE_AUDIO;
  1736. audio_enc.codec_id = audio_id;
  1737. add_codec(stream, &audio_enc);
  1738. }
  1739. if (video_id != CODEC_ID_NONE) {
  1740. video_enc.codec_type = CODEC_TYPE_VIDEO;
  1741. video_enc.codec_id = video_id;
  1742. add_codec(stream, &video_enc);
  1743. }
  1744. }
  1745. stream = NULL;
  1746. } else {
  1747. fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
  1748. filename, line_num, cmd);
  1749. errors++;
  1750. }
  1751. }
  1752. fclose(f);
  1753. if (errors)
  1754. return -1;
  1755. else
  1756. return 0;
  1757. }
  1758. void *http_server_thread(void *arg)
  1759. {
  1760. http_server(my_addr);
  1761. return NULL;
  1762. }
  1763. #if 0
  1764. static void write_packet(FFCodec *ffenc,
  1765. UINT8 *buf, int size)
  1766. {
  1767. PacketHeader hdr;
  1768. AVCodecContext *enc = &ffenc->enc;
  1769. UINT8 *wptr;
  1770. mk_header(&hdr, enc, size);
  1771. wptr = http_fifo.wptr;
  1772. fifo_write(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &wptr);
  1773. fifo_write(&http_fifo, buf, size, &wptr);
  1774. /* atomic modification of wptr */
  1775. http_fifo.wptr = wptr;
  1776. ffenc->data_count += size;
  1777. ffenc->avg_frame_size = ffenc->avg_frame_size * AVG_COEF + size * (1.0 - AVG_COEF);
  1778. }
  1779. #endif
  1780. void help(void)
  1781. {
  1782. printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
  1783. "usage: ffserver [-L] [-h] [-f configfile]\n"
  1784. "Hyper fast multi format Audio/Video streaming server\n"
  1785. "\n"
  1786. "-L : print the LICENCE\n"
  1787. "-h : this help\n"
  1788. "-f configfile : use configfile instead of /etc/ffserver.conf\n"
  1789. );
  1790. }
  1791. void licence(void)
  1792. {
  1793. printf(
  1794. "ffserver version " FFMPEG_VERSION "\n"
  1795. "Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
  1796. "This program is free software; you can redistribute it and/or modify\n"
  1797. "it under the terms of the GNU General Public License as published by\n"
  1798. "the Free Software Foundation; either version 2 of the License, or\n"
  1799. "(at your option) any later version.\n"
  1800. "\n"
  1801. "This program is distributed in the hope that it will be useful,\n"
  1802. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1803. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1804. "GNU General Public License for more details.\n"
  1805. "\n"
  1806. "You should have received a copy of the GNU General Public License\n"
  1807. "along with this program; if not, write to the Free Software\n"
  1808. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  1809. );
  1810. }
  1811. int main(int argc, char **argv)
  1812. {
  1813. const char *config_filename;
  1814. int c;
  1815. register_all();
  1816. config_filename = "/etc/ffserver.conf";
  1817. for(;;) {
  1818. c = getopt_long_only(argc, argv, "Lh?f:", NULL, NULL);
  1819. if (c == -1)
  1820. break;
  1821. switch(c) {
  1822. case 'L':
  1823. licence();
  1824. exit(1);
  1825. case '?':
  1826. case 'h':
  1827. help();
  1828. exit(1);
  1829. case 'f':
  1830. config_filename = optarg;
  1831. break;
  1832. default:
  1833. exit(2);
  1834. }
  1835. }
  1836. /* address on which the server will handle connections */
  1837. my_addr.sin_family = AF_INET;
  1838. my_addr.sin_port = htons (8080);
  1839. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  1840. nb_max_connections = 5;
  1841. nb_max_bandwidth = 1000;
  1842. first_stream = NULL;
  1843. logfilename[0] = '\0';
  1844. if (parse_ffconfig(config_filename) < 0) {
  1845. fprintf(stderr, "Incorrect config file - exiting.\n");
  1846. exit(1);
  1847. }
  1848. build_feed_streams();
  1849. /* signal init */
  1850. signal(SIGPIPE, SIG_IGN);
  1851. /* open log file if needed */
  1852. if (logfilename[0] != '\0') {
  1853. if (!strcmp(logfilename, "-"))
  1854. logfile = stdout;
  1855. else
  1856. logfile = fopen(logfilename, "w");
  1857. }
  1858. if (http_server(my_addr) < 0) {
  1859. fprintf(stderr, "Could start http server\n");
  1860. exit(1);
  1861. }
  1862. return 0;
  1863. }