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.

2044 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. AVOutputFormat *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. pstrcpy(c->method, sizeof(c->method), cmd);
  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. pstrcpy(c->url, sizeof(c->url), 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. pstrcpy(c->protocol, sizeof(c->protocol), 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. pstrcpy(info, sizeof(info), p);
  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. pstrcpy(sfilename, sizeof(sfilename) - 1, stream->filename);
  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. if (av_open_input_file(&s, input_filename, NULL, buf_size, NULL) < 0)
  891. return -1;
  892. c->fmt_in = s;
  893. if (c->fmt_in->iformat->read_seek) {
  894. c->fmt_in->iformat->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.oformat = 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.oformat = 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. av_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. av_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. if (c->stream)
  1097. c->stream->bytes_served += len;
  1098. }
  1099. }
  1100. return 0;
  1101. }
  1102. static int http_start_receive_data(HTTPContext *c)
  1103. {
  1104. int fd;
  1105. if (c->stream->feed_opened)
  1106. return -1;
  1107. /* open feed */
  1108. fd = open(c->stream->feed_filename, O_RDWR);
  1109. if (fd < 0)
  1110. return -1;
  1111. c->feed_fd = fd;
  1112. c->stream->feed_write_index = ffm_read_write_index(fd);
  1113. c->stream->feed_size = lseek(fd, 0, SEEK_END);
  1114. lseek(fd, 0, SEEK_SET);
  1115. /* init buffer input */
  1116. c->buffer_ptr = c->buffer;
  1117. c->buffer_end = c->buffer + FFM_PACKET_SIZE;
  1118. c->stream->feed_opened = 1;
  1119. return 0;
  1120. }
  1121. static int http_receive_data(HTTPContext *c)
  1122. {
  1123. HTTPContext *c1;
  1124. if (c->buffer_end > c->buffer_ptr) {
  1125. int len;
  1126. len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  1127. if (len < 0) {
  1128. if (errno != EAGAIN && errno != EINTR) {
  1129. /* error : close connection */
  1130. goto fail;
  1131. }
  1132. } else if (len == 0) {
  1133. /* end of connection : close it */
  1134. goto fail;
  1135. } else {
  1136. c->buffer_ptr += len;
  1137. c->data_count += len;
  1138. }
  1139. }
  1140. if (c->buffer_ptr >= c->buffer_end) {
  1141. FFStream *feed = c->stream;
  1142. /* a packet has been received : write it in the store, except
  1143. if header */
  1144. if (c->data_count > FFM_PACKET_SIZE) {
  1145. // printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
  1146. /* XXX: use llseek or url_seek */
  1147. lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
  1148. write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
  1149. feed->feed_write_index += FFM_PACKET_SIZE;
  1150. /* update file size */
  1151. if (feed->feed_write_index > c->stream->feed_size)
  1152. feed->feed_size = feed->feed_write_index;
  1153. /* handle wrap around if max file size reached */
  1154. if (feed->feed_write_index >= c->stream->feed_max_size)
  1155. feed->feed_write_index = FFM_PACKET_SIZE;
  1156. /* write index */
  1157. ffm_write_write_index(c->feed_fd, feed->feed_write_index);
  1158. /* wake up any waiting connections */
  1159. for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
  1160. if (c1->state == HTTPSTATE_WAIT_FEED &&
  1161. c1->stream->feed == c->stream->feed) {
  1162. c1->state = HTTPSTATE_SEND_DATA;
  1163. }
  1164. }
  1165. } else {
  1166. /* We have a header in our hands that contains useful data */
  1167. AVFormatContext s;
  1168. AVInputFormat *fmt_in;
  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. /* use feed output format name to find corresponding input format */
  1176. fmt_in = av_find_input_format(feed->fmt->name);
  1177. if (!fmt_in)
  1178. goto fail;
  1179. if (fmt_in->read_header(&s, 0) < 0) {
  1180. goto fail;
  1181. }
  1182. /* Now we have the actual streams */
  1183. if (s.nb_streams != feed->nb_streams) {
  1184. goto fail;
  1185. }
  1186. for (i = 0; i < s.nb_streams; i++) {
  1187. memcpy(&feed->streams[i]->codec,
  1188. &s.streams[i]->codec, sizeof(AVCodecContext));
  1189. }
  1190. }
  1191. c->buffer_ptr = c->buffer;
  1192. }
  1193. return 0;
  1194. fail:
  1195. c->stream->feed_opened = 0;
  1196. close(c->feed_fd);
  1197. return -1;
  1198. }
  1199. /* return the stream number in the feed */
  1200. int add_av_stream(FFStream *feed,
  1201. AVStream *st)
  1202. {
  1203. AVStream *fst;
  1204. AVCodecContext *av, *av1;
  1205. int i;
  1206. av = &st->codec;
  1207. for(i=0;i<feed->nb_streams;i++) {
  1208. st = feed->streams[i];
  1209. av1 = &st->codec;
  1210. if (av1->codec_id == av->codec_id &&
  1211. av1->codec_type == av->codec_type &&
  1212. av1->bit_rate == av->bit_rate) {
  1213. switch(av->codec_type) {
  1214. case CODEC_TYPE_AUDIO:
  1215. if (av1->channels == av->channels &&
  1216. av1->sample_rate == av->sample_rate)
  1217. goto found;
  1218. break;
  1219. case CODEC_TYPE_VIDEO:
  1220. if (av1->width == av->width &&
  1221. av1->height == av->height &&
  1222. av1->frame_rate == av->frame_rate &&
  1223. av1->gop_size == av->gop_size)
  1224. goto found;
  1225. break;
  1226. default:
  1227. abort();
  1228. }
  1229. }
  1230. }
  1231. fst = av_mallocz(sizeof(AVStream));
  1232. if (!fst)
  1233. return -1;
  1234. fst->priv_data = av_mallocz(sizeof(FeedData));
  1235. memcpy(&fst->codec, av, sizeof(AVCodecContext));
  1236. feed->streams[feed->nb_streams++] = fst;
  1237. return feed->nb_streams - 1;
  1238. found:
  1239. return i;
  1240. }
  1241. /* compute the needed AVStream for each feed */
  1242. void build_feed_streams(void)
  1243. {
  1244. FFStream *stream, *feed;
  1245. int i;
  1246. /* gather all streams */
  1247. for(stream = first_stream; stream != NULL; stream = stream->next) {
  1248. feed = stream->feed;
  1249. if (feed) {
  1250. if (!stream->is_feed) {
  1251. for(i=0;i<stream->nb_streams;i++) {
  1252. stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
  1253. }
  1254. } else {
  1255. for(i=0;i<stream->nb_streams;i++) {
  1256. stream->feed_streams[i] = i;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. /* create feed files if needed */
  1262. for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
  1263. int fd;
  1264. if (!url_exist(feed->feed_filename)) {
  1265. AVFormatContext s1, *s = &s1;
  1266. /* only write the header of the ffm file */
  1267. if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
  1268. fprintf(stderr, "Could not open output feed file '%s'\n",
  1269. feed->feed_filename);
  1270. exit(1);
  1271. }
  1272. s->oformat = feed->fmt;
  1273. s->nb_streams = feed->nb_streams;
  1274. for(i=0;i<s->nb_streams;i++) {
  1275. AVStream *st;
  1276. st = feed->streams[i];
  1277. s->streams[i] = st;
  1278. }
  1279. av_write_header(s);
  1280. /* XXX: need better api */
  1281. av_freep(&s->priv_data);
  1282. url_fclose(&s->pb);
  1283. }
  1284. /* get feed size and write index */
  1285. fd = open(feed->feed_filename, O_RDONLY);
  1286. if (fd < 0) {
  1287. fprintf(stderr, "Could not open output feed file '%s'\n",
  1288. feed->feed_filename);
  1289. exit(1);
  1290. }
  1291. feed->feed_write_index = ffm_read_write_index(fd);
  1292. feed->feed_size = lseek(fd, 0, SEEK_END);
  1293. /* ensure that we do not wrap before the end of file */
  1294. if (feed->feed_max_size < feed->feed_size)
  1295. feed->feed_max_size = feed->feed_size;
  1296. close(fd);
  1297. }
  1298. }
  1299. static void get_arg(char *buf, int buf_size, const char **pp)
  1300. {
  1301. const char *p;
  1302. char *q;
  1303. int quote;
  1304. p = *pp;
  1305. while (isspace(*p)) p++;
  1306. q = buf;
  1307. quote = 0;
  1308. if (*p == '\"' || *p == '\'')
  1309. quote = *p++;
  1310. for(;;) {
  1311. if (quote) {
  1312. if (*p == quote)
  1313. break;
  1314. } else {
  1315. if (isspace(*p))
  1316. break;
  1317. }
  1318. if (*p == '\0')
  1319. break;
  1320. if ((q - buf) < buf_size - 1)
  1321. *q++ = *p;
  1322. p++;
  1323. }
  1324. *q = '\0';
  1325. if (quote && *p == quote)
  1326. p++;
  1327. *pp = p;
  1328. }
  1329. /* add a codec and set the default parameters */
  1330. void add_codec(FFStream *stream, AVCodecContext *av)
  1331. {
  1332. AVStream *st;
  1333. /* compute default parameters */
  1334. switch(av->codec_type) {
  1335. case CODEC_TYPE_AUDIO:
  1336. if (av->bit_rate == 0)
  1337. av->bit_rate = 64000;
  1338. if (av->sample_rate == 0)
  1339. av->sample_rate = 22050;
  1340. if (av->channels == 0)
  1341. av->channels = 1;
  1342. break;
  1343. case CODEC_TYPE_VIDEO:
  1344. if (av->bit_rate == 0)
  1345. av->bit_rate = 64000;
  1346. if (av->frame_rate == 0)
  1347. av->frame_rate = 5 * FRAME_RATE_BASE;
  1348. if (av->width == 0 || av->height == 0) {
  1349. av->width = 160;
  1350. av->height = 128;
  1351. }
  1352. /* Bitrate tolerance is less for streaming */
  1353. if (av->bit_rate_tolerance == 0)
  1354. av->bit_rate_tolerance = av->bit_rate / 4;
  1355. if (av->qmin == 0)
  1356. av->qmin = 3;
  1357. if (av->qmax == 0)
  1358. av->qmax = 31;
  1359. if (av->max_qdiff == 0)
  1360. av->max_qdiff = 3;
  1361. av->qcompress = 0.5;
  1362. av->qblur = 0.5;
  1363. break;
  1364. default:
  1365. abort();
  1366. }
  1367. st = av_mallocz(sizeof(AVStream));
  1368. if (!st)
  1369. return;
  1370. stream->streams[stream->nb_streams++] = st;
  1371. memcpy(&st->codec, av, sizeof(AVCodecContext));
  1372. }
  1373. int opt_audio_codec(const char *arg)
  1374. {
  1375. AVCodec *p;
  1376. p = first_avcodec;
  1377. while (p) {
  1378. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_AUDIO)
  1379. break;
  1380. p = p->next;
  1381. }
  1382. if (p == NULL) {
  1383. return CODEC_ID_NONE;
  1384. }
  1385. return p->id;
  1386. }
  1387. int opt_video_codec(const char *arg)
  1388. {
  1389. AVCodec *p;
  1390. p = first_avcodec;
  1391. while (p) {
  1392. if (!strcmp(p->name, arg) && p->type == CODEC_TYPE_VIDEO)
  1393. break;
  1394. p = p->next;
  1395. }
  1396. if (p == NULL) {
  1397. return CODEC_ID_NONE;
  1398. }
  1399. return p->id;
  1400. }
  1401. int parse_ffconfig(const char *filename)
  1402. {
  1403. FILE *f;
  1404. char line[1024];
  1405. char cmd[64];
  1406. char arg[1024];
  1407. const char *p;
  1408. int val, errors, line_num;
  1409. FFStream **last_stream, *stream;
  1410. FFStream **last_feed, *feed;
  1411. AVCodecContext audio_enc, video_enc;
  1412. int audio_id, video_id;
  1413. f = fopen(filename, "r");
  1414. if (!f) {
  1415. perror(filename);
  1416. return -1;
  1417. }
  1418. errors = 0;
  1419. line_num = 0;
  1420. first_stream = NULL;
  1421. last_stream = &first_stream;
  1422. first_feed = NULL;
  1423. last_feed = &first_feed;
  1424. stream = NULL;
  1425. feed = NULL;
  1426. audio_id = CODEC_ID_NONE;
  1427. video_id = CODEC_ID_NONE;
  1428. for(;;) {
  1429. if (fgets(line, sizeof(line), f) == NULL)
  1430. break;
  1431. line_num++;
  1432. p = line;
  1433. while (isspace(*p))
  1434. p++;
  1435. if (*p == '\0' || *p == '#')
  1436. continue;
  1437. get_arg(cmd, sizeof(cmd), &p);
  1438. if (!strcasecmp(cmd, "Port")) {
  1439. get_arg(arg, sizeof(arg), &p);
  1440. my_addr.sin_port = htons (atoi(arg));
  1441. } else if (!strcasecmp(cmd, "BindAddress")) {
  1442. get_arg(arg, sizeof(arg), &p);
  1443. if (!inet_aton(arg, &my_addr.sin_addr)) {
  1444. fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
  1445. filename, line_num, arg);
  1446. errors++;
  1447. }
  1448. } else if (!strcasecmp(cmd, "MaxClients")) {
  1449. get_arg(arg, sizeof(arg), &p);
  1450. val = atoi(arg);
  1451. if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
  1452. fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
  1453. filename, line_num, arg);
  1454. errors++;
  1455. } else {
  1456. nb_max_connections = val;
  1457. }
  1458. } else if (!strcasecmp(cmd, "MaxBandwidth")) {
  1459. get_arg(arg, sizeof(arg), &p);
  1460. val = atoi(arg);
  1461. if (val < 10 || val > 100000) {
  1462. fprintf(stderr, "%s:%d: Invalid MaxBandwidth: %s\n",
  1463. filename, line_num, arg);
  1464. errors++;
  1465. } else {
  1466. nb_max_bandwidth = val;
  1467. }
  1468. } else if (!strcasecmp(cmd, "CustomLog")) {
  1469. get_arg(logfilename, sizeof(logfilename), &p);
  1470. } else if (!strcasecmp(cmd, "<Feed")) {
  1471. /*********************************************/
  1472. /* Feed related options */
  1473. char *q;
  1474. if (stream || feed) {
  1475. fprintf(stderr, "%s:%d: Already in a tag\n",
  1476. filename, line_num);
  1477. } else {
  1478. feed = av_mallocz(sizeof(FFStream));
  1479. /* add in stream list */
  1480. *last_stream = feed;
  1481. last_stream = &feed->next;
  1482. /* add in feed list */
  1483. *last_feed = feed;
  1484. last_feed = &feed->next_feed;
  1485. get_arg(feed->filename, sizeof(feed->filename), &p);
  1486. q = strrchr(feed->filename, '>');
  1487. if (*q)
  1488. *q = '\0';
  1489. feed->fmt = guess_format("ffm", NULL, NULL);
  1490. /* defaut feed file */
  1491. snprintf(feed->feed_filename, sizeof(feed->feed_filename),
  1492. "/tmp/%s.ffm", feed->filename);
  1493. feed->feed_max_size = 5 * 1024 * 1024;
  1494. feed->is_feed = 1;
  1495. feed->feed = feed; /* self feeding :-) */
  1496. }
  1497. } else if (!strcasecmp(cmd, "File")) {
  1498. if (feed) {
  1499. get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
  1500. } else if (stream) {
  1501. get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
  1502. }
  1503. } else if (!strcasecmp(cmd, "FileMaxSize")) {
  1504. if (feed) {
  1505. const char *p1;
  1506. double fsize;
  1507. get_arg(arg, sizeof(arg), &p);
  1508. p1 = arg;
  1509. fsize = strtod(p1, (char **)&p1);
  1510. switch(toupper(*p1)) {
  1511. case 'K':
  1512. fsize *= 1024;
  1513. break;
  1514. case 'M':
  1515. fsize *= 1024 * 1024;
  1516. break;
  1517. case 'G':
  1518. fsize *= 1024 * 1024 * 1024;
  1519. break;
  1520. }
  1521. feed->feed_max_size = (INT64)fsize;
  1522. }
  1523. } else if (!strcasecmp(cmd, "</Feed>")) {
  1524. if (!feed) {
  1525. fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
  1526. filename, line_num);
  1527. errors++;
  1528. } else {
  1529. /* Make sure that we start out clean */
  1530. if (unlink(feed->feed_filename) < 0
  1531. && errno != ENOENT) {
  1532. fprintf(stderr, "%s:%d: Unable to clean old feed file '%s': %s\n",
  1533. filename, line_num, feed->feed_filename, strerror(errno));
  1534. errors++;
  1535. }
  1536. }
  1537. feed = NULL;
  1538. } else if (!strcasecmp(cmd, "<Stream")) {
  1539. /*********************************************/
  1540. /* Stream related options */
  1541. char *q;
  1542. if (stream || feed) {
  1543. fprintf(stderr, "%s:%d: Already in a tag\n",
  1544. filename, line_num);
  1545. } else {
  1546. stream = av_mallocz(sizeof(FFStream));
  1547. *last_stream = stream;
  1548. last_stream = &stream->next;
  1549. get_arg(stream->filename, sizeof(stream->filename), &p);
  1550. q = strrchr(stream->filename, '>');
  1551. if (*q)
  1552. *q = '\0';
  1553. stream->fmt = guess_format(NULL, stream->filename, NULL);
  1554. memset(&audio_enc, 0, sizeof(AVCodecContext));
  1555. memset(&video_enc, 0, sizeof(AVCodecContext));
  1556. audio_id = CODEC_ID_NONE;
  1557. video_id = CODEC_ID_NONE;
  1558. if (stream->fmt) {
  1559. audio_id = stream->fmt->audio_codec;
  1560. video_id = stream->fmt->video_codec;
  1561. }
  1562. }
  1563. } else if (!strcasecmp(cmd, "Feed")) {
  1564. get_arg(arg, sizeof(arg), &p);
  1565. if (stream) {
  1566. FFStream *sfeed;
  1567. sfeed = first_feed;
  1568. while (sfeed != NULL) {
  1569. if (!strcmp(sfeed->filename, arg))
  1570. break;
  1571. sfeed = sfeed->next_feed;
  1572. }
  1573. if (!sfeed) {
  1574. fprintf(stderr, "%s:%d: feed '%s' not defined\n",
  1575. filename, line_num, arg);
  1576. } else {
  1577. stream->feed = sfeed;
  1578. }
  1579. }
  1580. } else if (!strcasecmp(cmd, "Format")) {
  1581. get_arg(arg, sizeof(arg), &p);
  1582. if (!strcmp(arg, "status")) {
  1583. stream->stream_type = STREAM_TYPE_STATUS;
  1584. stream->fmt = NULL;
  1585. } else {
  1586. stream->stream_type = STREAM_TYPE_LIVE;
  1587. /* jpeg cannot be used here, so use single frame jpeg */
  1588. if (!strcmp(arg, "jpeg"))
  1589. strcpy(arg, "singlejpeg");
  1590. stream->fmt = guess_format(arg, NULL, NULL);
  1591. if (!stream->fmt) {
  1592. fprintf(stderr, "%s:%d: Unknown Format: %s\n",
  1593. filename, line_num, arg);
  1594. errors++;
  1595. }
  1596. }
  1597. if (stream->fmt) {
  1598. audio_id = stream->fmt->audio_codec;
  1599. video_id = stream->fmt->video_codec;
  1600. }
  1601. } else if (!strcasecmp(cmd, "Preroll")) {
  1602. get_arg(arg, sizeof(arg), &p);
  1603. if (stream) {
  1604. stream->prebuffer = atoi(arg) * 1000;
  1605. }
  1606. } else if (!strcasecmp(cmd, "StartSendOnKey")) {
  1607. if (stream) {
  1608. stream->send_on_key = 1;
  1609. }
  1610. } else if (!strcasecmp(cmd, "AudioCodec")) {
  1611. get_arg(arg, sizeof(arg), &p);
  1612. audio_id = opt_audio_codec(arg);
  1613. if (audio_id == CODEC_ID_NONE) {
  1614. fprintf(stderr, "%s:%d: Unknown AudioCodec: %s\n",
  1615. filename, line_num, arg);
  1616. errors++;
  1617. }
  1618. } else if (!strcasecmp(cmd, "VideoCodec")) {
  1619. get_arg(arg, sizeof(arg), &p);
  1620. video_id = opt_video_codec(arg);
  1621. if (video_id == CODEC_ID_NONE) {
  1622. fprintf(stderr, "%s:%d: Unknown VideoCodec: %s\n",
  1623. filename, line_num, arg);
  1624. errors++;
  1625. }
  1626. } else if (!strcasecmp(cmd, "AudioBitRate")) {
  1627. get_arg(arg, sizeof(arg), &p);
  1628. if (stream) {
  1629. audio_enc.bit_rate = atoi(arg) * 1000;
  1630. }
  1631. } else if (!strcasecmp(cmd, "AudioChannels")) {
  1632. get_arg(arg, sizeof(arg), &p);
  1633. if (stream) {
  1634. audio_enc.channels = atoi(arg);
  1635. }
  1636. } else if (!strcasecmp(cmd, "AudioSampleRate")) {
  1637. get_arg(arg, sizeof(arg), &p);
  1638. if (stream) {
  1639. audio_enc.sample_rate = atoi(arg);
  1640. }
  1641. } else if (!strcasecmp(cmd, "VideoBitRate")) {
  1642. get_arg(arg, sizeof(arg), &p);
  1643. if (stream) {
  1644. video_enc.bit_rate = atoi(arg) * 1000;
  1645. }
  1646. } else if (!strcasecmp(cmd, "VideoSize")) {
  1647. get_arg(arg, sizeof(arg), &p);
  1648. if (stream) {
  1649. parse_image_size(&video_enc.width, &video_enc.height, arg);
  1650. if ((video_enc.width % 16) != 0 ||
  1651. (video_enc.height % 16) != 0) {
  1652. fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
  1653. filename, line_num);
  1654. errors++;
  1655. }
  1656. }
  1657. } else if (!strcasecmp(cmd, "VideoFrameRate")) {
  1658. get_arg(arg, sizeof(arg), &p);
  1659. if (stream) {
  1660. video_enc.frame_rate = (int)(strtod(arg, NULL) * FRAME_RATE_BASE);
  1661. }
  1662. } else if (!strcasecmp(cmd, "VideoGopSize")) {
  1663. get_arg(arg, sizeof(arg), &p);
  1664. if (stream) {
  1665. video_enc.gop_size = atoi(arg);
  1666. }
  1667. } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
  1668. if (stream) {
  1669. video_enc.gop_size = 1;
  1670. }
  1671. } else if (!strcasecmp(cmd, "VideoHighQuality")) {
  1672. if (stream) {
  1673. video_enc.flags |= CODEC_FLAG_HQ;
  1674. }
  1675. } else if (!strcasecmp(cmd, "VideoQDiff")) {
  1676. if (stream) {
  1677. video_enc.max_qdiff = atoi(arg);
  1678. if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
  1679. fprintf(stderr, "%s:%d: VideoQDiff out of range\n",
  1680. filename, line_num);
  1681. errors++;
  1682. }
  1683. }
  1684. } else if (!strcasecmp(cmd, "VideoQMax")) {
  1685. if (stream) {
  1686. video_enc.qmax = atoi(arg);
  1687. if (video_enc.qmax < 1 || video_enc.qmax > 31) {
  1688. fprintf(stderr, "%s:%d: VideoQMax out of range\n",
  1689. filename, line_num);
  1690. errors++;
  1691. }
  1692. }
  1693. } else if (!strcasecmp(cmd, "VideoQMin")) {
  1694. if (stream) {
  1695. video_enc.qmin = atoi(arg);
  1696. if (video_enc.qmin < 1 || video_enc.qmin > 31) {
  1697. fprintf(stderr, "%s:%d: VideoQMin out of range\n",
  1698. filename, line_num);
  1699. errors++;
  1700. }
  1701. }
  1702. } else if (!strcasecmp(cmd, "NoVideo")) {
  1703. video_id = CODEC_ID_NONE;
  1704. } else if (!strcasecmp(cmd, "NoAudio")) {
  1705. audio_id = CODEC_ID_NONE;
  1706. } else if (!strcasecmp(cmd, "</Stream>")) {
  1707. if (!stream) {
  1708. fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
  1709. filename, line_num);
  1710. errors++;
  1711. }
  1712. if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
  1713. if (audio_id != CODEC_ID_NONE) {
  1714. audio_enc.codec_type = CODEC_TYPE_AUDIO;
  1715. audio_enc.codec_id = audio_id;
  1716. add_codec(stream, &audio_enc);
  1717. }
  1718. if (video_id != CODEC_ID_NONE) {
  1719. video_enc.codec_type = CODEC_TYPE_VIDEO;
  1720. video_enc.codec_id = video_id;
  1721. add_codec(stream, &video_enc);
  1722. }
  1723. }
  1724. stream = NULL;
  1725. } else {
  1726. fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
  1727. filename, line_num, cmd);
  1728. errors++;
  1729. }
  1730. }
  1731. fclose(f);
  1732. if (errors)
  1733. return -1;
  1734. else
  1735. return 0;
  1736. }
  1737. void *http_server_thread(void *arg)
  1738. {
  1739. http_server(my_addr);
  1740. return NULL;
  1741. }
  1742. #if 0
  1743. static void write_packet(FFCodec *ffenc,
  1744. UINT8 *buf, int size)
  1745. {
  1746. PacketHeader hdr;
  1747. AVCodecContext *enc = &ffenc->enc;
  1748. UINT8 *wptr;
  1749. mk_header(&hdr, enc, size);
  1750. wptr = http_fifo.wptr;
  1751. fifo_write(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &wptr);
  1752. fifo_write(&http_fifo, buf, size, &wptr);
  1753. /* atomic modification of wptr */
  1754. http_fifo.wptr = wptr;
  1755. ffenc->data_count += size;
  1756. ffenc->avg_frame_size = ffenc->avg_frame_size * AVG_COEF + size * (1.0 - AVG_COEF);
  1757. }
  1758. #endif
  1759. void help(void)
  1760. {
  1761. printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
  1762. "usage: ffserver [-L] [-h] [-f configfile]\n"
  1763. "Hyper fast multi format Audio/Video streaming server\n"
  1764. "\n"
  1765. "-L : print the LICENCE\n"
  1766. "-h : this help\n"
  1767. "-f configfile : use configfile instead of /etc/ffserver.conf\n"
  1768. );
  1769. }
  1770. void licence(void)
  1771. {
  1772. printf(
  1773. "ffserver version " FFMPEG_VERSION "\n"
  1774. "Copyright (c) 2000, 2001, 2002 Gerard Lantau\n"
  1775. "This program is free software; you can redistribute it and/or modify\n"
  1776. "it under the terms of the GNU General Public License as published by\n"
  1777. "the Free Software Foundation; either version 2 of the License, or\n"
  1778. "(at your option) any later version.\n"
  1779. "\n"
  1780. "This program is distributed in the hope that it will be useful,\n"
  1781. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1782. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1783. "GNU General Public License for more details.\n"
  1784. "\n"
  1785. "You should have received a copy of the GNU General Public License\n"
  1786. "along with this program; if not, write to the Free Software\n"
  1787. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  1788. );
  1789. }
  1790. int main(int argc, char **argv)
  1791. {
  1792. const char *config_filename;
  1793. int c;
  1794. register_all();
  1795. config_filename = "/etc/ffserver.conf";
  1796. for(;;) {
  1797. c = getopt_long_only(argc, argv, "Lh?f:", NULL, NULL);
  1798. if (c == -1)
  1799. break;
  1800. switch(c) {
  1801. case 'L':
  1802. licence();
  1803. exit(1);
  1804. case '?':
  1805. case 'h':
  1806. help();
  1807. exit(1);
  1808. case 'f':
  1809. config_filename = optarg;
  1810. break;
  1811. default:
  1812. exit(2);
  1813. }
  1814. }
  1815. /* address on which the server will handle connections */
  1816. my_addr.sin_family = AF_INET;
  1817. my_addr.sin_port = htons (8080);
  1818. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  1819. nb_max_connections = 5;
  1820. nb_max_bandwidth = 1000;
  1821. first_stream = NULL;
  1822. logfilename[0] = '\0';
  1823. if (parse_ffconfig(config_filename) < 0) {
  1824. fprintf(stderr, "Incorrect config file - exiting.\n");
  1825. exit(1);
  1826. }
  1827. build_feed_streams();
  1828. /* signal init */
  1829. signal(SIGPIPE, SIG_IGN);
  1830. /* open log file if needed */
  1831. if (logfilename[0] != '\0') {
  1832. if (!strcmp(logfilename, "-"))
  1833. logfile = stdout;
  1834. else
  1835. logfile = fopen(logfilename, "w");
  1836. }
  1837. if (http_server(my_addr) < 0) {
  1838. fprintf(stderr, "Could start http server\n");
  1839. exit(1);
  1840. }
  1841. return 0;
  1842. }