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.

1578 lines
48KB

  1. /*
  2. * Multiple format streaming server
  3. * Copyright (c) 2000,2001 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 "avformat.h"
  39. /* maximum number of simultaneous HTTP connections */
  40. #define HTTP_MAX_CONNECTIONS 2000
  41. enum HTTPState {
  42. HTTPSTATE_WAIT_REQUEST,
  43. HTTPSTATE_SEND_HEADER,
  44. HTTPSTATE_SEND_DATA_HEADER,
  45. HTTPSTATE_SEND_DATA,
  46. HTTPSTATE_SEND_DATA_TRAILER,
  47. HTTPSTATE_RECEIVE_DATA,
  48. HTTPSTATE_WAIT_FEED,
  49. };
  50. const char *http_state[] = {
  51. "WAIT_REQUEST",
  52. "SEND_HEADER",
  53. "SEND_DATA_HEADER",
  54. "SEND_DATA",
  55. "SEND_DATA_TRAILER",
  56. "RECEIVE_DATA",
  57. "WAIT_FEED",
  58. };
  59. #define IOBUFFER_MAX_SIZE 16384
  60. /* coef for exponential mean for bitrate estimation in statistics */
  61. #define AVG_COEF 0.9
  62. /* timeouts are in ms */
  63. #define REQUEST_TIMEOUT (15 * 1000)
  64. #define SYNC_TIMEOUT (10 * 1000)
  65. /* context associated with one connection */
  66. typedef struct HTTPContext {
  67. enum HTTPState state;
  68. int fd; /* socket file descriptor */
  69. struct sockaddr_in from_addr; /* origin */
  70. struct pollfd *poll_entry; /* used when polling */
  71. long timeout;
  72. UINT8 buffer[IOBUFFER_MAX_SIZE];
  73. UINT8 *buffer_ptr, *buffer_end;
  74. int http_error;
  75. struct HTTPContext *next;
  76. int got_key_frame[MAX_STREAMS]; /* for each type */
  77. INT64 data_count;
  78. /* feed input */
  79. int feed_fd;
  80. /* input format handling */
  81. AVFormatContext *fmt_in;
  82. /* output format handling */
  83. struct FFStream *stream;
  84. AVFormatContext fmt_ctx;
  85. int last_packet_sent; /* true if last data packet was sent */
  86. } HTTPContext;
  87. /* each generated stream is described here */
  88. enum StreamType {
  89. STREAM_TYPE_LIVE,
  90. STREAM_TYPE_STATUS,
  91. };
  92. /* description of each stream of the ffserver.conf file */
  93. typedef struct FFStream {
  94. enum StreamType stream_type;
  95. char filename[1024]; /* stream filename */
  96. struct FFStream *feed;
  97. AVFormat *fmt;
  98. int nb_streams;
  99. AVStream *streams[MAX_STREAMS];
  100. int feed_streams[MAX_STREAMS]; /* index of streams in the feed */
  101. char feed_filename[1024]; /* file name of the feed storage, or
  102. input file name for a stream */
  103. struct FFStream *next;
  104. /* feed specific */
  105. int feed_opened; /* true if someone if writing to feed */
  106. int is_feed; /* true if it is a feed */
  107. INT64 feed_max_size; /* maximum storage size */
  108. INT64 feed_write_index; /* current write position in feed (it wraps round) */
  109. INT64 feed_size; /* current size of feed */
  110. struct FFStream *next_feed;
  111. } FFStream;
  112. typedef struct FeedData {
  113. long long data_count;
  114. float avg_frame_size; /* frame size averraged over last frames with exponential mean */
  115. } FeedData;
  116. struct sockaddr_in my_addr;
  117. char logfilename[1024];
  118. HTTPContext *first_http_ctx;
  119. FFStream *first_feed; /* contains only feeds */
  120. FFStream *first_stream; /* contains all streams, including feeds */
  121. static int handle_http(HTTPContext *c, long cur_time);
  122. static int http_parse_request(HTTPContext *c);
  123. static int http_send_data(HTTPContext *c);
  124. static void compute_stats(HTTPContext *c);
  125. static int open_input_stream(HTTPContext *c, const char *info);
  126. static int http_start_receive_data(HTTPContext *c);
  127. static int http_receive_data(HTTPContext *c);
  128. int nb_max_connections;
  129. int nb_connections;
  130. static long gettime_ms(void)
  131. {
  132. struct timeval tv;
  133. gettimeofday(&tv,NULL);
  134. return (long long)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
  135. }
  136. static FILE *logfile = NULL;
  137. static void http_log(char *fmt, ...)
  138. {
  139. va_list ap;
  140. va_start(ap, fmt);
  141. if (logfile)
  142. vfprintf(logfile, fmt, ap);
  143. va_end(ap);
  144. }
  145. /* main loop of the http server */
  146. static int http_server(struct sockaddr_in my_addr)
  147. {
  148. int server_fd, tmp, ret;
  149. struct sockaddr_in from_addr;
  150. struct pollfd poll_table[HTTP_MAX_CONNECTIONS + 1], *poll_entry;
  151. HTTPContext *c, **cp;
  152. long cur_time;
  153. server_fd = socket(AF_INET,SOCK_STREAM,0);
  154. if (server_fd < 0) {
  155. perror ("socket");
  156. return -1;
  157. }
  158. tmp = 1;
  159. setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp));
  160. if (bind (server_fd, (struct sockaddr *) &my_addr, sizeof (my_addr)) < 0) {
  161. perror ("bind");
  162. close(server_fd);
  163. return -1;
  164. }
  165. if (listen (server_fd, 5) < 0) {
  166. perror ("listen");
  167. close(server_fd);
  168. return -1;
  169. }
  170. http_log("ffserver started.\n");
  171. fcntl(server_fd, F_SETFL, O_NONBLOCK);
  172. first_http_ctx = NULL;
  173. nb_connections = 0;
  174. first_http_ctx = NULL;
  175. for(;;) {
  176. poll_entry = poll_table;
  177. poll_entry->fd = server_fd;
  178. poll_entry->events = POLLIN;
  179. poll_entry++;
  180. /* wait for events on each HTTP handle */
  181. c = first_http_ctx;
  182. while (c != NULL) {
  183. int fd;
  184. fd = c->fd;
  185. switch(c->state) {
  186. case HTTPSTATE_WAIT_REQUEST:
  187. c->poll_entry = poll_entry;
  188. poll_entry->fd = fd;
  189. poll_entry->events = POLLIN;
  190. poll_entry++;
  191. break;
  192. case HTTPSTATE_SEND_HEADER:
  193. case HTTPSTATE_SEND_DATA_HEADER:
  194. case HTTPSTATE_SEND_DATA:
  195. case HTTPSTATE_SEND_DATA_TRAILER:
  196. c->poll_entry = poll_entry;
  197. poll_entry->fd = fd;
  198. poll_entry->events = POLLOUT;
  199. poll_entry++;
  200. break;
  201. case HTTPSTATE_RECEIVE_DATA:
  202. c->poll_entry = poll_entry;
  203. poll_entry->fd = fd;
  204. poll_entry->events = POLLIN;
  205. poll_entry++;
  206. break;
  207. case HTTPSTATE_WAIT_FEED:
  208. /* need to catch errors */
  209. c->poll_entry = poll_entry;
  210. poll_entry->fd = fd;
  211. poll_entry->events = 0;
  212. poll_entry++;
  213. break;
  214. default:
  215. c->poll_entry = NULL;
  216. break;
  217. }
  218. c = c->next;
  219. }
  220. /* wait for an event on one connection. We poll at least every
  221. second to handle timeouts */
  222. do {
  223. ret = poll(poll_table, poll_entry - poll_table, 1000);
  224. } while (ret == -1);
  225. cur_time = gettime_ms();
  226. /* now handle the events */
  227. cp = &first_http_ctx;
  228. while ((*cp) != NULL) {
  229. c = *cp;
  230. if (handle_http (c, cur_time) < 0) {
  231. /* close and free the connection */
  232. close(c->fd);
  233. if (c->fmt_in)
  234. av_close_input_file(c->fmt_in);
  235. *cp = c->next;
  236. free(c);
  237. nb_connections--;
  238. } else {
  239. cp = &c->next;
  240. }
  241. }
  242. /* new connection request ? */
  243. poll_entry = poll_table;
  244. if (poll_entry->revents & POLLIN) {
  245. int fd, len;
  246. len = sizeof(from_addr);
  247. fd = accept(server_fd, (struct sockaddr *)&from_addr,
  248. &len);
  249. if (fd >= 0) {
  250. fcntl(fd, F_SETFL, O_NONBLOCK);
  251. /* XXX: should output a warning page when coming
  252. close to the connection limit */
  253. if (nb_connections >= nb_max_connections) {
  254. close(fd);
  255. } else {
  256. /* add a new connection */
  257. c = av_mallocz(sizeof(HTTPContext));
  258. c->next = first_http_ctx;
  259. first_http_ctx = c;
  260. c->fd = fd;
  261. c->poll_entry = NULL;
  262. c->from_addr = from_addr;
  263. c->state = HTTPSTATE_WAIT_REQUEST;
  264. c->buffer_ptr = c->buffer;
  265. c->buffer_end = c->buffer + IOBUFFER_MAX_SIZE;
  266. c->timeout = cur_time + REQUEST_TIMEOUT;
  267. nb_connections++;
  268. }
  269. }
  270. }
  271. poll_entry++;
  272. }
  273. }
  274. static int handle_http(HTTPContext *c, long cur_time)
  275. {
  276. int len;
  277. switch(c->state) {
  278. case HTTPSTATE_WAIT_REQUEST:
  279. /* timeout ? */
  280. if ((c->timeout - cur_time) < 0)
  281. return -1;
  282. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  283. return -1;
  284. /* no need to read if no events */
  285. if (!(c->poll_entry->revents & POLLIN))
  286. return 0;
  287. /* read the data */
  288. len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  289. if (len < 0) {
  290. if (errno != EAGAIN && errno != EINTR)
  291. return -1;
  292. } else if (len == 0) {
  293. return -1;
  294. } else {
  295. /* search for end of request. XXX: not fully correct since garbage could come after the end */
  296. UINT8 *ptr;
  297. c->buffer_ptr += len;
  298. ptr = c->buffer_ptr;
  299. if ((ptr >= c->buffer + 2 && !memcmp(ptr-2, "\n\n", 2)) ||
  300. (ptr >= c->buffer + 4 && !memcmp(ptr-4, "\r\n\r\n", 4))) {
  301. /* request found : parse it and reply */
  302. if (http_parse_request(c) < 0)
  303. return -1;
  304. } else if (ptr >= c->buffer_end) {
  305. /* request too long: cannot do anything */
  306. return -1;
  307. }
  308. }
  309. break;
  310. case HTTPSTATE_SEND_HEADER:
  311. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  312. return -1;
  313. /* no need to read if no events */
  314. if (!(c->poll_entry->revents & POLLOUT))
  315. return 0;
  316. len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  317. if (len < 0) {
  318. if (errno != EAGAIN && errno != EINTR) {
  319. /* error : close connection */
  320. return -1;
  321. }
  322. } else {
  323. c->buffer_ptr += len;
  324. if (c->buffer_ptr >= c->buffer_end) {
  325. /* if error, exit */
  326. if (c->http_error)
  327. return -1;
  328. /* all the buffer was send : synchronize to the incoming stream */
  329. c->state = HTTPSTATE_SEND_DATA_HEADER;
  330. c->buffer_ptr = c->buffer_end = c->buffer;
  331. }
  332. }
  333. break;
  334. case HTTPSTATE_SEND_DATA:
  335. case HTTPSTATE_SEND_DATA_HEADER:
  336. case HTTPSTATE_SEND_DATA_TRAILER:
  337. /* no need to read if no events */
  338. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  339. return -1;
  340. if (!(c->poll_entry->revents & POLLOUT))
  341. return 0;
  342. if (http_send_data(c) < 0)
  343. return -1;
  344. break;
  345. case HTTPSTATE_RECEIVE_DATA:
  346. /* no need to read if no events */
  347. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  348. return -1;
  349. if (!(c->poll_entry->revents & POLLIN))
  350. return 0;
  351. if (http_receive_data(c) < 0)
  352. return -1;
  353. break;
  354. case HTTPSTATE_WAIT_FEED:
  355. /* no need to read if no events */
  356. if (c->poll_entry->revents & (POLLERR | POLLHUP))
  357. return -1;
  358. /* nothing to do, we'll be waken up by incoming feed packets */
  359. break;
  360. default:
  361. return -1;
  362. }
  363. return 0;
  364. }
  365. /* parse http request and prepare header */
  366. static int http_parse_request(HTTPContext *c)
  367. {
  368. char *p;
  369. int post;
  370. char cmd[32];
  371. char info[1024], *filename;
  372. char url[1024], *q;
  373. char protocol[32];
  374. char msg[1024];
  375. const char *mime_type;
  376. FFStream *stream;
  377. p = c->buffer;
  378. q = cmd;
  379. while (!isspace(*p) && *p != '\0') {
  380. if ((q - cmd) < sizeof(cmd) - 1)
  381. *q++ = *p;
  382. p++;
  383. }
  384. *q = '\0';
  385. if (!strcmp(cmd, "GET"))
  386. post = 0;
  387. else if (!strcmp(cmd, "POST"))
  388. post = 1;
  389. else
  390. return -1;
  391. while (isspace(*p)) p++;
  392. q = url;
  393. while (!isspace(*p) && *p != '\0') {
  394. if ((q - url) < sizeof(url) - 1)
  395. *q++ = *p;
  396. p++;
  397. }
  398. *q = '\0';
  399. while (isspace(*p)) p++;
  400. q = protocol;
  401. while (!isspace(*p) && *p != '\0') {
  402. if ((q - protocol) < sizeof(protocol) - 1)
  403. *q++ = *p;
  404. p++;
  405. }
  406. *q = '\0';
  407. if (strcmp(protocol, "HTTP/1.0") && strcmp(protocol, "HTTP/1.1"))
  408. return -1;
  409. /* find the filename and the optional info string in the request */
  410. p = url;
  411. if (*p == '/')
  412. p++;
  413. filename = p;
  414. p = strchr(p, '?');
  415. if (p) {
  416. strcpy(info, p);
  417. *p = '\0';
  418. } else {
  419. info[0] = '\0';
  420. }
  421. stream = first_stream;
  422. while (stream != NULL) {
  423. if (!strcmp(stream->filename, filename))
  424. break;
  425. stream = stream->next;
  426. }
  427. if (stream == NULL) {
  428. sprintf(msg, "File '%s' not found", url);
  429. goto send_error;
  430. }
  431. c->stream = stream;
  432. /* should do it after so that the size can be computed */
  433. {
  434. char buf1[32], buf2[32], *p;
  435. time_t ti;
  436. /* XXX: reentrant function ? */
  437. p = inet_ntoa(c->from_addr.sin_addr);
  438. strcpy(buf1, p);
  439. ti = time(NULL);
  440. p = ctime(&ti);
  441. strcpy(buf2, p);
  442. p = buf2 + strlen(p) - 1;
  443. if (*p == '\n')
  444. *p = '\0';
  445. http_log("%s - - [%s] \"%s %s %s\" %d %d\n",
  446. buf1, buf2, cmd, url, protocol, 200, 1024);
  447. }
  448. /* XXX: add there authenticate and IP match */
  449. if (post) {
  450. /* if post, it means a feed is being sent */
  451. if (!stream->is_feed) {
  452. sprintf(msg, "POST command not handled");
  453. goto send_error;
  454. }
  455. if (http_start_receive_data(c) < 0) {
  456. sprintf(msg, "could not open feed");
  457. goto send_error;
  458. }
  459. c->http_error = 0;
  460. c->state = HTTPSTATE_RECEIVE_DATA;
  461. return 0;
  462. }
  463. if (c->stream->stream_type == STREAM_TYPE_STATUS)
  464. goto send_stats;
  465. /* open input stream */
  466. if (open_input_stream(c, info) < 0) {
  467. sprintf(msg, "Input stream corresponding to '%s' not found", url);
  468. goto send_error;
  469. }
  470. /* prepare http header */
  471. q = c->buffer;
  472. q += sprintf(q, "HTTP/1.0 200 OK\r\n");
  473. mime_type = c->stream->fmt->mime_type;
  474. if (!mime_type)
  475. mime_type = "application/x-octet_stream";
  476. q += sprintf(q, "Content-type: %s\r\n", mime_type);
  477. q += sprintf(q, "Pragma: no-cache\r\n");
  478. /* for asf, we need extra headers */
  479. if (!strcmp(c->stream->fmt->name,"asf")) {
  480. q += sprintf(q, "Pragma: features=broadcast\r\n");
  481. }
  482. q += sprintf(q, "\r\n");
  483. /* prepare output buffer */
  484. c->http_error = 0;
  485. c->buffer_ptr = c->buffer;
  486. c->buffer_end = q;
  487. c->state = HTTPSTATE_SEND_HEADER;
  488. return 0;
  489. send_error:
  490. c->http_error = 404;
  491. q = c->buffer;
  492. q += sprintf(q, "HTTP/1.0 404 Not Found\r\n");
  493. q += sprintf(q, "Content-type: %s\r\n", "text/html");
  494. q += sprintf(q, "\r\n");
  495. q += sprintf(q, "<HTML>\n");
  496. q += sprintf(q, "<HEAD><TITLE>404 Not Found</TITLE></HEAD>\n");
  497. q += sprintf(q, "<BODY>%s</BODY>\n", msg);
  498. q += sprintf(q, "</HTML>\n");
  499. /* prepare output buffer */
  500. c->buffer_ptr = c->buffer;
  501. c->buffer_end = q;
  502. c->state = HTTPSTATE_SEND_HEADER;
  503. return 0;
  504. send_stats:
  505. compute_stats(c);
  506. c->http_error = 200; /* horrible : we use this value to avoid
  507. going to the send data state */
  508. c->state = HTTPSTATE_SEND_HEADER;
  509. return 0;
  510. }
  511. static void compute_stats(HTTPContext *c)
  512. {
  513. HTTPContext *c1;
  514. FFStream *stream;
  515. char *q, *p;
  516. time_t ti;
  517. int i;
  518. q = c->buffer;
  519. q += sprintf(q, "HTTP/1.0 200 OK\r\n");
  520. q += sprintf(q, "Content-type: %s\r\n", "text/html");
  521. q += sprintf(q, "Pragma: no-cache\r\n");
  522. q += sprintf(q, "\r\n");
  523. q += sprintf(q, "<HEAD><TITLE>FFServer Status</TITLE></HEAD>\n<BODY>");
  524. q += sprintf(q, "<H1>FFServer Status</H1>\n");
  525. /* format status */
  526. q += sprintf(q, "<H1>Available Streams</H1>\n");
  527. q += sprintf(q, "<TABLE>\n");
  528. q += sprintf(q, "<TR><TD>Path<TD>Format<TD>Bit rate (kbits/s)<TD>Video<TD>Audio<TD>Feed\n");
  529. stream = first_stream;
  530. while (stream != NULL) {
  531. q += sprintf(q, "<TR><TD><A HREF=\"/%s\">%s</A> ",
  532. stream->filename, stream->filename);
  533. switch(stream->stream_type) {
  534. case STREAM_TYPE_LIVE:
  535. {
  536. int audio_bit_rate = 0;
  537. int video_bit_rate = 0;
  538. for(i=0;i<stream->nb_streams;i++) {
  539. AVStream *st = stream->streams[i];
  540. switch(st->codec.codec_type) {
  541. case CODEC_TYPE_AUDIO:
  542. audio_bit_rate += st->codec.bit_rate;
  543. break;
  544. case CODEC_TYPE_VIDEO:
  545. video_bit_rate += st->codec.bit_rate;
  546. break;
  547. }
  548. }
  549. q += sprintf(q, "<TD> %s <TD> %d <TD> %d <TD> %d",
  550. stream->fmt->name,
  551. (audio_bit_rate + video_bit_rate) / 1000,
  552. video_bit_rate / 1000, audio_bit_rate / 1000);
  553. if (stream->feed) {
  554. q += sprintf(q, "<TD>%s", stream->feed->filename);
  555. } else {
  556. q += sprintf(q, "<TD>%s", stream->feed_filename);
  557. }
  558. q += sprintf(q, "\n");
  559. }
  560. break;
  561. default:
  562. q += sprintf(q, "<TD> - <TD> - <TD> - <TD> -\n");
  563. break;
  564. }
  565. stream = stream->next;
  566. }
  567. q += sprintf(q, "</TABLE>\n");
  568. #if 0
  569. {
  570. float avg;
  571. AVCodecContext *enc;
  572. char buf[1024];
  573. /* feed status */
  574. stream = first_feed;
  575. while (stream != NULL) {
  576. q += sprintf(q, "<H1>Feed '%s'</H1>\n", stream->filename);
  577. q += sprintf(q, "<TABLE>\n");
  578. q += sprintf(q, "<TR><TD>Parameters<TD>Frame count<TD>Size<TD>Avg bitrate (kbits/s)\n");
  579. for(i=0;i<stream->nb_streams;i++) {
  580. AVStream *st = stream->streams[i];
  581. FeedData *fdata = st->priv_data;
  582. enc = &st->codec;
  583. avcodec_string(buf, sizeof(buf), enc);
  584. avg = fdata->avg_frame_size * (float)enc->rate * 8.0;
  585. if (enc->codec->type == CODEC_TYPE_AUDIO && enc->frame_size > 0)
  586. avg /= enc->frame_size;
  587. q += sprintf(q, "<TR><TD>%s <TD> %d <TD> %Ld <TD> %0.1f\n",
  588. buf, enc->frame_number, fdata->data_count, avg / 1000.0);
  589. }
  590. q += sprintf(q, "</TABLE>\n");
  591. stream = stream->next_feed;
  592. }
  593. }
  594. #endif
  595. /* connection status */
  596. q += sprintf(q, "<H1>Connection Status</H1>\n");
  597. q += sprintf(q, "Number of connections: %d / %d<BR>\n",
  598. nb_connections, nb_max_connections);
  599. q += sprintf(q, "<TABLE>\n");
  600. q += sprintf(q, "<TR><TD>#<TD>File<TD>IP<TD>State<TD>Size\n");
  601. c1 = first_http_ctx;
  602. i = 0;
  603. while (c1 != NULL) {
  604. i++;
  605. p = inet_ntoa(c1->from_addr.sin_addr);
  606. q += sprintf(q, "<TR><TD><B>%d</B><TD>%s%s <TD> %s <TD> %s <TD> %Ld\n",
  607. i, c1->stream->filename,
  608. c1->state == HTTPSTATE_RECEIVE_DATA ? "(input)" : "",
  609. p,
  610. http_state[c1->state],
  611. c1->data_count);
  612. c1 = c1->next;
  613. }
  614. q += sprintf(q, "</TABLE>\n");
  615. /* date */
  616. ti = time(NULL);
  617. p = ctime(&ti);
  618. q += sprintf(q, "<HR>Generated at %s", p);
  619. q += sprintf(q, "</BODY>\n</HTML>\n");
  620. c->buffer_ptr = c->buffer;
  621. c->buffer_end = q;
  622. }
  623. static void http_write_packet(void *opaque,
  624. unsigned char *buf, int size)
  625. {
  626. HTTPContext *c = opaque;
  627. if (size > IOBUFFER_MAX_SIZE)
  628. abort();
  629. memcpy(c->buffer, buf, size);
  630. c->buffer_ptr = c->buffer;
  631. c->buffer_end = c->buffer + size;
  632. }
  633. static int open_input_stream(HTTPContext *c, const char *info)
  634. {
  635. char buf[128];
  636. char input_filename[1024];
  637. AVFormatContext *s;
  638. int buf_size;
  639. INT64 stream_pos;
  640. /* find file name */
  641. if (c->stream->feed) {
  642. strcpy(input_filename, c->stream->feed->feed_filename);
  643. buf_size = FFM_PACKET_SIZE;
  644. /* compute position (absolute time) */
  645. if (find_info_tag(buf, sizeof(buf), "date", info)) {
  646. stream_pos = parse_date(buf, 0);
  647. } else {
  648. stream_pos = gettime();
  649. }
  650. } else {
  651. strcpy(input_filename, c->stream->feed_filename);
  652. buf_size = 0;
  653. /* compute position (relative time) */
  654. if (find_info_tag(buf, sizeof(buf), "date", info)) {
  655. stream_pos = parse_date(buf, 1);
  656. } else {
  657. stream_pos = 0;
  658. }
  659. }
  660. if (input_filename[0] == '\0')
  661. return -1;
  662. /* open stream */
  663. s = av_open_input_file(input_filename, buf_size);
  664. if (!s)
  665. return -1;
  666. c->fmt_in = s;
  667. if (c->fmt_in->format->read_seek) {
  668. c->fmt_in->format->read_seek(c->fmt_in, stream_pos);
  669. }
  670. // printf("stream %s opened pos=%0.6f\n", input_filename, stream_pos / 1000000.0);
  671. return 0;
  672. }
  673. static int http_prepare_data(HTTPContext *c)
  674. {
  675. int i;
  676. switch(c->state) {
  677. case HTTPSTATE_SEND_DATA_HEADER:
  678. memset(&c->fmt_ctx, 0, sizeof(c->fmt_ctx));
  679. if (c->stream->feed) {
  680. /* open output stream by using specified codecs */
  681. c->fmt_ctx.format = c->stream->fmt;
  682. c->fmt_ctx.nb_streams = c->stream->nb_streams;
  683. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  684. AVStream *st;
  685. st = av_mallocz(sizeof(AVStream));
  686. c->fmt_ctx.streams[i] = st;
  687. memcpy(st, c->stream->streams[i], sizeof(AVStream));
  688. st->codec.frame_number = 0; /* XXX: should be done in
  689. AVStream, not in codec */
  690. c->got_key_frame[i] = 0;
  691. }
  692. } else {
  693. /* open output stream by using codecs in specified file */
  694. c->fmt_ctx.format = c->stream->fmt;
  695. c->fmt_ctx.nb_streams = c->fmt_in->nb_streams;
  696. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  697. AVStream *st;
  698. st = av_mallocz(sizeof(AVStream));
  699. c->fmt_ctx.streams[i] = st;
  700. memcpy(st, c->fmt_in->streams[i], sizeof(AVStream));
  701. st->codec.frame_number = 0; /* XXX: should be done in
  702. AVStream, not in codec */
  703. c->got_key_frame[i] = 0;
  704. }
  705. }
  706. init_put_byte(&c->fmt_ctx.pb, c->buffer, IOBUFFER_MAX_SIZE,
  707. 1, c, NULL, http_write_packet, NULL);
  708. c->fmt_ctx.pb.is_streamed = 1;
  709. /* prepare header */
  710. c->fmt_ctx.format->write_header(&c->fmt_ctx);
  711. c->state = HTTPSTATE_SEND_DATA;
  712. c->last_packet_sent = 0;
  713. break;
  714. case HTTPSTATE_SEND_DATA:
  715. /* find a new packet */
  716. #if 0
  717. fifo_total_size = http_fifo_write_count - c->last_http_fifo_write_count;
  718. if (fifo_total_size >= ((3 * FIFO_MAX_SIZE) / 4)) {
  719. /* overflow : resync. We suppose that wptr is at this
  720. point a pointer to a valid packet */
  721. c->rptr = http_fifo.wptr;
  722. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  723. c->got_key_frame[i] = 0;
  724. }
  725. }
  726. start_rptr = c->rptr;
  727. if (fifo_read(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &c->rptr) < 0)
  728. return 0;
  729. payload_size = ntohs(hdr.payload_size);
  730. payload = malloc(payload_size);
  731. if (fifo_read(&http_fifo, payload, payload_size, &c->rptr) < 0) {
  732. /* cannot read all the payload */
  733. free(payload);
  734. c->rptr = start_rptr;
  735. return 0;
  736. }
  737. c->last_http_fifo_write_count = http_fifo_write_count -
  738. fifo_size(&http_fifo, c->rptr);
  739. if (c->stream->stream_type != STREAM_TYPE_MASTER) {
  740. /* test if the packet can be handled by this format */
  741. ret = 0;
  742. for(i=0;i<c->fmt_ctx.nb_streams;i++) {
  743. AVStream *st = c->fmt_ctx.streams[i];
  744. if (test_header(&hdr, &st->codec)) {
  745. /* only begin sending when got a key frame */
  746. if (st->codec.key_frame)
  747. c->got_key_frame[i] = 1;
  748. if (c->got_key_frame[i]) {
  749. ret = c->fmt_ctx.format->write_packet(&c->fmt_ctx, i,
  750. payload, payload_size);
  751. }
  752. break;
  753. }
  754. }
  755. if (ret) {
  756. /* must send trailer now */
  757. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  758. }
  759. } else {
  760. /* master case : send everything */
  761. char *q;
  762. q = c->buffer;
  763. memcpy(q, &hdr, sizeof(hdr));
  764. q += sizeof(hdr);
  765. memcpy(q, payload, payload_size);
  766. q += payload_size;
  767. c->buffer_ptr = c->buffer;
  768. c->buffer_end = q;
  769. }
  770. free(payload);
  771. #endif
  772. {
  773. AVPacket pkt;
  774. /* read a packet from the input stream */
  775. if (c->stream->feed) {
  776. ffm_set_write_index(c->fmt_in,
  777. c->stream->feed->feed_write_index,
  778. c->stream->feed->feed_size);
  779. }
  780. if (av_read_packet(c->fmt_in, &pkt) < 0) {
  781. if (c->stream->feed && c->stream->feed->feed_opened) {
  782. /* if coming from feed, it means we reached the end of the
  783. ffm file, so must wait for more data */
  784. c->state = HTTPSTATE_WAIT_FEED;
  785. return 1; /* state changed */
  786. } else {
  787. /* must send trailer now because eof or error */
  788. c->state = HTTPSTATE_SEND_DATA_TRAILER;
  789. }
  790. } else {
  791. /* send it to the appropriate stream */
  792. if (c->stream->feed) {
  793. /* if coming from a feed, select the right stream */
  794. for(i=0;i<c->stream->nb_streams;i++) {
  795. if (c->stream->feed_streams[i] == pkt.stream_index) {
  796. pkt.stream_index = i;
  797. goto send_it;
  798. }
  799. }
  800. } else {
  801. send_it:
  802. av_write_packet(&c->fmt_ctx, &pkt);
  803. }
  804. av_free_packet(&pkt);
  805. }
  806. }
  807. break;
  808. default:
  809. case HTTPSTATE_SEND_DATA_TRAILER:
  810. /* last packet test ? */
  811. if (c->last_packet_sent)
  812. return -1;
  813. /* prepare header */
  814. c->fmt_ctx.format->write_trailer(&c->fmt_ctx);
  815. c->last_packet_sent = 1;
  816. break;
  817. }
  818. return 0;
  819. }
  820. /* should convert the format at the same time */
  821. static int http_send_data(HTTPContext *c)
  822. {
  823. int len, ret;
  824. while (c->buffer_ptr >= c->buffer_end) {
  825. ret = http_prepare_data(c);
  826. if (ret < 0)
  827. return -1;
  828. else if (ret == 0) {
  829. break;
  830. } else {
  831. /* state change requested */
  832. return 0;
  833. }
  834. }
  835. len = write(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  836. if (len < 0) {
  837. if (errno != EAGAIN && errno != EINTR) {
  838. /* error : close connection */
  839. return -1;
  840. }
  841. } else {
  842. c->buffer_ptr += len;
  843. c->data_count += len;
  844. }
  845. return 0;
  846. }
  847. static int http_start_receive_data(HTTPContext *c)
  848. {
  849. int fd;
  850. if (c->stream->feed_opened)
  851. return -1;
  852. /* open feed */
  853. fd = open(c->stream->feed_filename, O_RDWR);
  854. if (fd < 0)
  855. return -1;
  856. c->feed_fd = fd;
  857. c->stream->feed_write_index = ffm_read_write_index(fd);
  858. c->stream->feed_size = lseek(fd, 0, SEEK_END);
  859. lseek(fd, 0, SEEK_SET);
  860. /* init buffer input */
  861. c->buffer_ptr = c->buffer;
  862. c->buffer_end = c->buffer + FFM_PACKET_SIZE;
  863. c->stream->feed_opened = 1;
  864. return 0;
  865. }
  866. static int http_receive_data(HTTPContext *c)
  867. {
  868. int len;
  869. HTTPContext *c1;
  870. if (c->buffer_ptr >= c->buffer_end) {
  871. /* a packet has been received : write it in the store, except
  872. if header */
  873. if (c->data_count > FFM_PACKET_SIZE) {
  874. FFStream *feed = c->stream;
  875. // printf("writing pos=0x%Lx size=0x%Lx\n", feed->feed_write_index, feed->feed_size);
  876. /* XXX: use llseek or url_seek */
  877. lseek(c->feed_fd, feed->feed_write_index, SEEK_SET);
  878. write(c->feed_fd, c->buffer, FFM_PACKET_SIZE);
  879. feed->feed_write_index += FFM_PACKET_SIZE;
  880. /* update file size */
  881. if (feed->feed_write_index > c->stream->feed_size)
  882. feed->feed_size = feed->feed_write_index;
  883. /* handle wrap around if max file size reached */
  884. if (feed->feed_write_index >= c->stream->feed_max_size)
  885. feed->feed_write_index = FFM_PACKET_SIZE;
  886. /* write index */
  887. ffm_write_write_index(c->feed_fd, feed->feed_write_index);
  888. /* wake up any waiting connections */
  889. for(c1 = first_http_ctx; c1 != NULL; c1 = c1->next) {
  890. if (c1->state == HTTPSTATE_WAIT_FEED &&
  891. c1->stream->feed == c->stream->feed) {
  892. c1->state = HTTPSTATE_SEND_DATA;
  893. }
  894. }
  895. }
  896. c->buffer_ptr = c->buffer;
  897. }
  898. len = read(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr);
  899. if (len < 0) {
  900. if (errno != EAGAIN && errno != EINTR) {
  901. /* error : close connection */
  902. goto fail;
  903. }
  904. } else if (len == 0) {
  905. /* end of connection : close it */
  906. goto fail;
  907. } else {
  908. c->buffer_ptr += len;
  909. c->data_count += len;
  910. }
  911. return 0;
  912. fail:
  913. c->stream->feed_opened = 0;
  914. close(c->feed_fd);
  915. return -1;
  916. }
  917. /* return the stream number in the feed */
  918. int add_av_stream(FFStream *feed,
  919. AVStream *st)
  920. {
  921. AVStream *fst;
  922. AVCodecContext *av, *av1;
  923. int i;
  924. av = &st->codec;
  925. for(i=0;i<feed->nb_streams;i++) {
  926. st = feed->streams[i];
  927. av1 = &st->codec;
  928. if (av1->codec == av->codec &&
  929. av1->bit_rate == av->bit_rate) {
  930. switch(av->codec_type) {
  931. case CODEC_TYPE_AUDIO:
  932. if (av1->channels == av->channels &&
  933. av1->sample_rate == av->sample_rate)
  934. goto found;
  935. break;
  936. case CODEC_TYPE_VIDEO:
  937. if (av1->width == av->width &&
  938. av1->height == av->height &&
  939. av1->frame_rate == av->frame_rate &&
  940. av1->gop_size == av->gop_size)
  941. goto found;
  942. break;
  943. }
  944. }
  945. }
  946. fst = av_mallocz(sizeof(AVStream));
  947. if (!fst)
  948. return -1;
  949. fst->priv_data = av_mallocz(sizeof(FeedData));
  950. memcpy(&fst->codec, av, sizeof(AVCodecContext));
  951. feed->streams[feed->nb_streams++] = fst;
  952. return feed->nb_streams - 1;
  953. found:
  954. return i;
  955. }
  956. /* compute the needed AVStream for each feed */
  957. void build_feed_streams(void)
  958. {
  959. FFStream *stream, *feed;
  960. int i;
  961. /* gather all streams */
  962. for(stream = first_stream; stream != NULL; stream = stream->next) {
  963. feed = stream->feed;
  964. if (feed) {
  965. if (!stream->is_feed) {
  966. for(i=0;i<stream->nb_streams;i++) {
  967. stream->feed_streams[i] = add_av_stream(feed, stream->streams[i]);
  968. }
  969. } else {
  970. for(i=0;i<stream->nb_streams;i++) {
  971. stream->feed_streams[i] = i;
  972. }
  973. }
  974. }
  975. }
  976. /* create feed files if needed */
  977. for(feed = first_feed; feed != NULL; feed = feed->next_feed) {
  978. int fd;
  979. if (!url_exist(feed->feed_filename)) {
  980. AVFormatContext s1, *s = &s1;
  981. /* only write the header of the ffm file */
  982. if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
  983. fprintf(stderr, "Could not open output feed file '%s'\n",
  984. feed->feed_filename);
  985. exit(1);
  986. }
  987. s->format = feed->fmt;
  988. s->nb_streams = feed->nb_streams;
  989. for(i=0;i<s->nb_streams;i++) {
  990. AVStream *st;
  991. st = feed->streams[i];
  992. s->streams[i] = st;
  993. }
  994. s->format->write_header(s);
  995. url_fclose(&s->pb);
  996. }
  997. /* get feed size and write index */
  998. fd = open(feed->feed_filename, O_RDONLY);
  999. if (fd < 0) {
  1000. fprintf(stderr, "Could not open output feed file '%s'\n",
  1001. feed->feed_filename);
  1002. exit(1);
  1003. }
  1004. feed->feed_write_index = ffm_read_write_index(fd);
  1005. feed->feed_size = lseek(fd, 0, SEEK_END);
  1006. /* ensure that we do not wrap before the end of file */
  1007. if (feed->feed_max_size < feed->feed_size)
  1008. feed->feed_max_size = feed->feed_size;
  1009. close(fd);
  1010. }
  1011. }
  1012. static void get_arg(char *buf, int buf_size, const char **pp)
  1013. {
  1014. const char *p;
  1015. char *q;
  1016. int quote;
  1017. p = *pp;
  1018. while (isspace(*p)) p++;
  1019. q = buf;
  1020. quote = 0;
  1021. if (*p == '\"' || *p == '\'')
  1022. quote = *p++;
  1023. for(;;) {
  1024. if (quote) {
  1025. if (*p == quote)
  1026. break;
  1027. } else {
  1028. if (isspace(*p))
  1029. break;
  1030. }
  1031. if (*p == '\0')
  1032. break;
  1033. if ((q - buf) < buf_size - 1)
  1034. *q++ = *p;
  1035. p++;
  1036. }
  1037. *q = '\0';
  1038. if (quote && *p == quote)
  1039. p++;
  1040. *pp = p;
  1041. }
  1042. /* add a codec and set the default parameters */
  1043. void add_codec(FFStream *stream, AVCodecContext *av)
  1044. {
  1045. AVStream *st;
  1046. /* compute default parameters */
  1047. switch(av->codec_type) {
  1048. case CODEC_TYPE_AUDIO:
  1049. if (av->bit_rate == 0)
  1050. av->bit_rate = 64000;
  1051. if (av->sample_rate == 0)
  1052. av->sample_rate = 22050;
  1053. if (av->channels == 0)
  1054. av->channels = 1;
  1055. break;
  1056. case CODEC_TYPE_VIDEO:
  1057. if (av->bit_rate == 0)
  1058. av->bit_rate = 64000;
  1059. if (av->frame_rate == 0)
  1060. av->frame_rate = 5 * FRAME_RATE_BASE;
  1061. if (av->width == 0 || av->height == 0) {
  1062. av->width = 160;
  1063. av->height = 128;
  1064. }
  1065. break;
  1066. }
  1067. st = av_mallocz(sizeof(AVStream));
  1068. if (!st)
  1069. return;
  1070. stream->streams[stream->nb_streams++] = st;
  1071. memcpy(&st->codec, av, sizeof(AVCodecContext));
  1072. }
  1073. int parse_ffconfig(const char *filename)
  1074. {
  1075. FILE *f;
  1076. char line[1024];
  1077. char cmd[64];
  1078. char arg[1024];
  1079. const char *p;
  1080. int val, errors, line_num;
  1081. FFStream **last_stream, *stream;
  1082. FFStream **last_feed, *feed;
  1083. AVCodecContext audio_enc, video_enc;
  1084. int audio_id, video_id;
  1085. f = fopen(filename, "r");
  1086. if (!f) {
  1087. perror(filename);
  1088. return -1;
  1089. }
  1090. errors = 0;
  1091. line_num = 0;
  1092. first_stream = NULL;
  1093. last_stream = &first_stream;
  1094. first_feed = NULL;
  1095. last_feed = &first_feed;
  1096. stream = NULL;
  1097. feed = NULL;
  1098. audio_id = CODEC_ID_NONE;
  1099. video_id = CODEC_ID_NONE;
  1100. for(;;) {
  1101. if (fgets(line, sizeof(line), f) == NULL)
  1102. break;
  1103. line_num++;
  1104. p = line;
  1105. while (isspace(*p))
  1106. p++;
  1107. if (*p == '\0' || *p == '#')
  1108. continue;
  1109. get_arg(cmd, sizeof(cmd), &p);
  1110. if (!strcasecmp(cmd, "Port")) {
  1111. get_arg(arg, sizeof(arg), &p);
  1112. my_addr.sin_port = htons (atoi(arg));
  1113. } else if (!strcasecmp(cmd, "BindAddress")) {
  1114. get_arg(arg, sizeof(arg), &p);
  1115. if (!inet_aton(arg, &my_addr.sin_addr)) {
  1116. fprintf(stderr, "%s:%d: Invalid IP address: %s\n",
  1117. filename, line_num, arg);
  1118. errors++;
  1119. }
  1120. } else if (!strcasecmp(cmd, "MaxClients")) {
  1121. get_arg(arg, sizeof(arg), &p);
  1122. val = atoi(arg);
  1123. if (val < 1 || val > HTTP_MAX_CONNECTIONS) {
  1124. fprintf(stderr, "%s:%d: Invalid MaxClients: %s\n",
  1125. filename, line_num, arg);
  1126. errors++;
  1127. } else {
  1128. nb_max_connections = val;
  1129. }
  1130. } else if (!strcasecmp(cmd, "CustomLog")) {
  1131. get_arg(logfilename, sizeof(logfilename), &p);
  1132. } else if (!strcasecmp(cmd, "<Feed")) {
  1133. /*********************************************/
  1134. /* Feed related options */
  1135. char *q;
  1136. if (stream || feed) {
  1137. fprintf(stderr, "%s:%d: Already in a tag\n",
  1138. filename, line_num);
  1139. } else {
  1140. feed = av_mallocz(sizeof(FFStream));
  1141. /* add in stream list */
  1142. *last_stream = feed;
  1143. last_stream = &feed->next;
  1144. /* add in feed list */
  1145. *last_feed = feed;
  1146. last_feed = &feed->next_feed;
  1147. get_arg(feed->filename, sizeof(feed->filename), &p);
  1148. q = strrchr(feed->filename, '>');
  1149. if (*q)
  1150. *q = '\0';
  1151. feed->fmt = guess_format("ffm", NULL, NULL);
  1152. /* defaut feed file */
  1153. snprintf(feed->feed_filename, sizeof(feed->feed_filename),
  1154. "/tmp/%s.ffm", feed->filename);
  1155. feed->feed_max_size = 5 * 1024 * 1024;
  1156. feed->is_feed = 1;
  1157. feed->feed = feed; /* self feeding :-) */
  1158. }
  1159. } else if (!strcasecmp(cmd, "File")) {
  1160. if (feed) {
  1161. get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
  1162. } else if (stream) {
  1163. get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
  1164. }
  1165. } else if (!strcasecmp(cmd, "FileMaxSize")) {
  1166. if (feed) {
  1167. const char *p1;
  1168. double fsize;
  1169. get_arg(arg, sizeof(arg), &p);
  1170. p1 = arg;
  1171. fsize = strtod(p1, (char **)&p1);
  1172. switch(toupper(*p1)) {
  1173. case 'K':
  1174. fsize *= 1024;
  1175. break;
  1176. case 'M':
  1177. fsize *= 1024 * 1024;
  1178. break;
  1179. case 'G':
  1180. fsize *= 1024 * 1024 * 1024;
  1181. break;
  1182. }
  1183. feed->feed_max_size = (INT64)fsize;
  1184. }
  1185. } else if (!strcasecmp(cmd, "</Feed>")) {
  1186. if (!feed) {
  1187. fprintf(stderr, "%s:%d: No corresponding <Feed> for </Feed>\n",
  1188. filename, line_num);
  1189. errors++;
  1190. }
  1191. feed = NULL;
  1192. } else if (!strcasecmp(cmd, "<Stream")) {
  1193. /*********************************************/
  1194. /* Stream related options */
  1195. char *q;
  1196. if (stream || feed) {
  1197. fprintf(stderr, "%s:%d: Already in a tag\n",
  1198. filename, line_num);
  1199. } else {
  1200. stream = av_mallocz(sizeof(FFStream));
  1201. *last_stream = stream;
  1202. last_stream = &stream->next;
  1203. get_arg(stream->filename, sizeof(stream->filename), &p);
  1204. q = strrchr(stream->filename, '>');
  1205. if (*q)
  1206. *q = '\0';
  1207. stream->fmt = guess_format(NULL, stream->filename, NULL);
  1208. memset(&audio_enc, 0, sizeof(AVCodecContext));
  1209. memset(&video_enc, 0, sizeof(AVCodecContext));
  1210. audio_id = CODEC_ID_NONE;
  1211. video_id = CODEC_ID_NONE;
  1212. if (stream->fmt) {
  1213. audio_id = stream->fmt->audio_codec;
  1214. video_id = stream->fmt->video_codec;
  1215. }
  1216. }
  1217. } else if (!strcasecmp(cmd, "Feed")) {
  1218. get_arg(arg, sizeof(arg), &p);
  1219. if (stream) {
  1220. FFStream *sfeed;
  1221. sfeed = first_feed;
  1222. while (sfeed != NULL) {
  1223. if (!strcmp(sfeed->filename, arg))
  1224. break;
  1225. sfeed = sfeed->next_feed;
  1226. }
  1227. if (!sfeed) {
  1228. fprintf(stderr, "%s:%d: feed '%s' not defined\n",
  1229. filename, line_num, arg);
  1230. } else {
  1231. stream->feed = sfeed;
  1232. }
  1233. }
  1234. } else if (!strcasecmp(cmd, "Format")) {
  1235. get_arg(arg, sizeof(arg), &p);
  1236. if (!strcmp(arg, "status")) {
  1237. stream->stream_type = STREAM_TYPE_STATUS;
  1238. stream->fmt = NULL;
  1239. } else {
  1240. stream->stream_type = STREAM_TYPE_LIVE;
  1241. stream->fmt = guess_format(arg, NULL, NULL);
  1242. if (!stream->fmt) {
  1243. fprintf(stderr, "%s:%d: Unknown Format: %s\n",
  1244. filename, line_num, arg);
  1245. errors++;
  1246. }
  1247. }
  1248. if (stream->fmt) {
  1249. audio_id = stream->fmt->audio_codec;
  1250. video_id = stream->fmt->video_codec;
  1251. }
  1252. } else if (!strcasecmp(cmd, "AudioBitRate")) {
  1253. get_arg(arg, sizeof(arg), &p);
  1254. if (stream) {
  1255. audio_enc.bit_rate = atoi(arg) * 1000;
  1256. }
  1257. } else if (!strcasecmp(cmd, "AudioChannels")) {
  1258. get_arg(arg, sizeof(arg), &p);
  1259. if (stream) {
  1260. audio_enc.channels = atoi(arg);
  1261. }
  1262. } else if (!strcasecmp(cmd, "AudioSampleRate")) {
  1263. get_arg(arg, sizeof(arg), &p);
  1264. if (stream) {
  1265. audio_enc.sample_rate = atoi(arg);
  1266. }
  1267. } else if (!strcasecmp(cmd, "VideoBitRate")) {
  1268. get_arg(arg, sizeof(arg), &p);
  1269. if (stream) {
  1270. video_enc.bit_rate = atoi(arg) * 1000;
  1271. }
  1272. } else if (!strcasecmp(cmd, "VideoSize")) {
  1273. get_arg(arg, sizeof(arg), &p);
  1274. if (stream) {
  1275. parse_image_size(&video_enc.width, &video_enc.height, arg);
  1276. if ((video_enc.width % 16) != 0 ||
  1277. (video_enc.height % 16) != 0) {
  1278. fprintf(stderr, "%s:%d: Image size must be a multiple of 16\n",
  1279. filename, line_num);
  1280. errors++;
  1281. }
  1282. }
  1283. } else if (!strcasecmp(cmd, "VideoFrameRate")) {
  1284. get_arg(arg, sizeof(arg), &p);
  1285. if (stream) {
  1286. video_enc.frame_rate = (int)(strtod(arg, NULL) * FRAME_RATE_BASE);
  1287. }
  1288. } else if (!strcasecmp(cmd, "VideoGopSize")) {
  1289. get_arg(arg, sizeof(arg), &p);
  1290. if (stream) {
  1291. video_enc.gop_size = atoi(arg);
  1292. }
  1293. } else if (!strcasecmp(cmd, "VideoIntraOnly")) {
  1294. if (stream) {
  1295. video_enc.gop_size = 1;
  1296. }
  1297. } else if (!strcasecmp(cmd, "NoVideo")) {
  1298. video_id = CODEC_ID_NONE;
  1299. } else if (!strcasecmp(cmd, "NoAudio")) {
  1300. audio_id = CODEC_ID_NONE;
  1301. } else if (!strcasecmp(cmd, "</Stream>")) {
  1302. if (!stream) {
  1303. fprintf(stderr, "%s:%d: No corresponding <Stream> for </Stream>\n",
  1304. filename, line_num);
  1305. errors++;
  1306. }
  1307. if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
  1308. if (audio_id != CODEC_ID_NONE) {
  1309. audio_enc.codec_type = CODEC_TYPE_AUDIO;
  1310. audio_enc.codec_id = audio_id;
  1311. add_codec(stream, &audio_enc);
  1312. }
  1313. if (video_id != CODEC_ID_NONE) {
  1314. video_enc.codec_type = CODEC_TYPE_VIDEO;
  1315. video_enc.codec_id = video_id;
  1316. add_codec(stream, &video_enc);
  1317. }
  1318. }
  1319. stream = NULL;
  1320. } else {
  1321. fprintf(stderr, "%s:%d: Incorrect keyword: '%s'\n",
  1322. filename, line_num, cmd);
  1323. errors++;
  1324. }
  1325. }
  1326. fclose(f);
  1327. if (errors)
  1328. return -1;
  1329. else
  1330. return 0;
  1331. }
  1332. void *http_server_thread(void *arg)
  1333. {
  1334. http_server(my_addr);
  1335. return NULL;
  1336. }
  1337. #if 0
  1338. static void write_packet(FFCodec *ffenc,
  1339. UINT8 *buf, int size)
  1340. {
  1341. PacketHeader hdr;
  1342. AVCodecContext *enc = &ffenc->enc;
  1343. UINT8 *wptr;
  1344. mk_header(&hdr, enc, size);
  1345. wptr = http_fifo.wptr;
  1346. fifo_write(&http_fifo, (UINT8 *)&hdr, sizeof(hdr), &wptr);
  1347. fifo_write(&http_fifo, buf, size, &wptr);
  1348. /* atomic modification of wptr */
  1349. http_fifo.wptr = wptr;
  1350. ffenc->data_count += size;
  1351. ffenc->avg_frame_size = ffenc->avg_frame_size * AVG_COEF + size * (1.0 - AVG_COEF);
  1352. }
  1353. #endif
  1354. void help(void)
  1355. {
  1356. printf("ffserver version " FFMPEG_VERSION ", Copyright (c) 2000,2001 Gerard Lantau\n"
  1357. "usage: ffserver [-L] [-h] [-f configfile]\n"
  1358. "Hyper fast multi format Audio/Video streaming server\n"
  1359. "\n"
  1360. "-L : print the LICENCE\n"
  1361. "-h : this help\n"
  1362. "-f configfile : use configfile instead of /etc/ffserver.conf\n"
  1363. );
  1364. }
  1365. void licence(void)
  1366. {
  1367. printf(
  1368. "ffserver version " FFMPEG_VERSION "\n"
  1369. "Copyright (c) 2000,2001 Gerard Lantau\n"
  1370. "This program is free software; you can redistribute it and/or modify\n"
  1371. "it under the terms of the GNU General Public License as published by\n"
  1372. "the Free Software Foundation; either version 2 of the License, or\n"
  1373. "(at your option) any later version.\n"
  1374. "\n"
  1375. "This program is distributed in the hope that it will be useful,\n"
  1376. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  1377. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  1378. "GNU General Public License for more details.\n"
  1379. "\n"
  1380. "You should have received a copy of the GNU General Public License\n"
  1381. "along with this program; if not, write to the Free Software\n"
  1382. "Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
  1383. );
  1384. }
  1385. int main(int argc, char **argv)
  1386. {
  1387. const char *config_filename;
  1388. int c;
  1389. register_all();
  1390. config_filename = "/etc/ffserver.conf";
  1391. for(;;) {
  1392. c = getopt_long_only(argc, argv, "Lh?f:", NULL, NULL);
  1393. if (c == -1)
  1394. break;
  1395. switch(c) {
  1396. case 'L':
  1397. licence();
  1398. exit(1);
  1399. case '?':
  1400. case 'h':
  1401. help();
  1402. exit(1);
  1403. case 'f':
  1404. config_filename = optarg;
  1405. break;
  1406. default:
  1407. exit(2);
  1408. }
  1409. }
  1410. /* address on which the server will handle connections */
  1411. my_addr.sin_family = AF_INET;
  1412. my_addr.sin_port = htons (8080);
  1413. my_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  1414. nb_max_connections = 5;
  1415. first_stream = NULL;
  1416. logfilename[0] = '\0';
  1417. if (parse_ffconfig(config_filename) < 0) {
  1418. fprintf(stderr, "Incorrect config file - exiting.\n");
  1419. exit(1);
  1420. }
  1421. build_feed_streams();
  1422. /* signal init */
  1423. signal(SIGPIPE, SIG_IGN);
  1424. /* open log file if needed */
  1425. if (logfilename[0] != '\0') {
  1426. if (!strcmp(logfilename, "-"))
  1427. logfile = stdout;
  1428. else
  1429. logfile = fopen(logfilename, "w");
  1430. }
  1431. if (http_server(my_addr) < 0) {
  1432. fprintf(stderr, "Could start http server\n");
  1433. exit(1);
  1434. }
  1435. return 0;
  1436. }