ffserver.c 62 KB

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