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.

2035 lines
65KB

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