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.

1591 lines
49KB

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