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.

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