ffserver_config.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*
  2. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <float.h>
  21. #include "libavutil/opt.h"
  22. #include "libavutil/parseutils.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/pixdesc.h"
  25. #include "libavutil/avassert.h"
  26. #include "cmdutils.h"
  27. #include "ffserver_config.h"
  28. #define MAX_CHILD_ARGS 64
  29. static int ffserver_save_avoption(const char *opt, const char *arg, int type,
  30. FFServerConfig *config);
  31. static void vreport_config_error(const char *filename, int line_num,
  32. int log_level, int *errors, const char *fmt,
  33. va_list vl);
  34. static void report_config_error(const char *filename, int line_num,
  35. int log_level, int *errors, const char *fmt,
  36. ...);
  37. #define ERROR(...) report_config_error(config->filename, config->line_num,\
  38. AV_LOG_ERROR, &config->errors, __VA_ARGS__)
  39. #define WARNING(...) report_config_error(config->filename, config->line_num,\
  40. AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
  41. /* FIXME: make ffserver work with IPv6 */
  42. /* resolve host with also IP address parsing */
  43. static int resolve_host(struct in_addr *sin_addr, const char *hostname)
  44. {
  45. if (!ff_inet_aton(hostname, sin_addr)) {
  46. #if HAVE_GETADDRINFO
  47. struct addrinfo *ai, *cur;
  48. struct addrinfo hints = { 0 };
  49. hints.ai_family = AF_INET;
  50. if (getaddrinfo(hostname, NULL, &hints, &ai))
  51. return -1;
  52. /* getaddrinfo returns a linked list of addrinfo structs.
  53. * Even if we set ai_family = AF_INET above, make sure
  54. * that the returned one actually is of the correct type. */
  55. for (cur = ai; cur; cur = cur->ai_next) {
  56. if (cur->ai_family == AF_INET) {
  57. *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
  58. freeaddrinfo(ai);
  59. return 0;
  60. }
  61. }
  62. freeaddrinfo(ai);
  63. return -1;
  64. #else
  65. struct hostent *hp;
  66. hp = gethostbyname(hostname);
  67. if (!hp)
  68. return -1;
  69. memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
  70. #endif
  71. }
  72. return 0;
  73. }
  74. void ffserver_get_arg(char *buf, int buf_size, const char **pp)
  75. {
  76. const char *p;
  77. char *q;
  78. int quote = 0;
  79. p = *pp;
  80. q = buf;
  81. while (av_isspace(*p)) p++;
  82. if (*p == '\"' || *p == '\'')
  83. quote = *p++;
  84. while (*p != '\0') {
  85. if (quote && *p == quote || !quote && av_isspace(*p))
  86. break;
  87. if ((q - buf) < buf_size - 1)
  88. *q++ = *p;
  89. p++;
  90. }
  91. *q = '\0';
  92. if (quote && *p == quote)
  93. p++;
  94. *pp = p;
  95. }
  96. void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
  97. FFServerIPAddressACL *ext_acl,
  98. const char *p, const char *filename, int line_num)
  99. {
  100. char arg[1024];
  101. FFServerIPAddressACL acl;
  102. FFServerIPAddressACL *nacl;
  103. FFServerIPAddressACL **naclp;
  104. ffserver_get_arg(arg, sizeof(arg), &p);
  105. if (av_strcasecmp(arg, "allow") == 0)
  106. acl.action = IP_ALLOW;
  107. else if (av_strcasecmp(arg, "deny") == 0)
  108. acl.action = IP_DENY;
  109. else {
  110. fprintf(stderr, "%s:%d: ACL action '%s' should be ALLOW or DENY.\n",
  111. filename, line_num, arg);
  112. goto bail;
  113. }
  114. ffserver_get_arg(arg, sizeof(arg), &p);
  115. if (resolve_host(&acl.first, arg)) {
  116. fprintf(stderr,
  117. "%s:%d: ACL refers to invalid host or IP address '%s'\n",
  118. filename, line_num, arg);
  119. goto bail;
  120. }
  121. acl.last = acl.first;
  122. ffserver_get_arg(arg, sizeof(arg), &p);
  123. if (arg[0]) {
  124. if (resolve_host(&acl.last, arg)) {
  125. fprintf(stderr,
  126. "%s:%d: ACL refers to invalid host or IP address '%s'\n",
  127. filename, line_num, arg);
  128. goto bail;
  129. }
  130. }
  131. nacl = av_mallocz(sizeof(*nacl));
  132. naclp = 0;
  133. acl.next = 0;
  134. *nacl = acl;
  135. if (stream)
  136. naclp = &stream->acl;
  137. else if (feed)
  138. naclp = &feed->acl;
  139. else if (ext_acl)
  140. naclp = &ext_acl;
  141. else
  142. fprintf(stderr, "%s:%d: ACL found not in <Stream> or <Feed>\n",
  143. filename, line_num);
  144. if (naclp) {
  145. while (*naclp)
  146. naclp = &(*naclp)->next;
  147. *naclp = nacl;
  148. } else
  149. av_free(nacl);
  150. bail:
  151. return;
  152. }
  153. /* add a codec and set the default parameters */
  154. static void add_codec(FFServerStream *stream, AVCodecContext *av,
  155. FFServerConfig *config)
  156. {
  157. AVStream *st;
  158. AVDictionary **opts, *recommended = NULL;
  159. char *enc_config;
  160. if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
  161. return;
  162. opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
  163. &config->audio_opts : &config->video_opts;
  164. av_dict_copy(&recommended, *opts, 0);
  165. av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
  166. av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
  167. if (av_dict_count(*opts))
  168. av_log(NULL, AV_LOG_WARNING,
  169. "Something is wrong, %d options are not set!\n",
  170. av_dict_count(*opts));
  171. if (!config->stream_use_defaults) {
  172. switch(av->codec_type) {
  173. case AVMEDIA_TYPE_AUDIO:
  174. if (av->bit_rate == 0)
  175. report_config_error(config->filename, config->line_num,
  176. AV_LOG_ERROR, &config->errors,
  177. "audio bit rate is not set\n");
  178. if (av->sample_rate == 0)
  179. report_config_error(config->filename, config->line_num,
  180. AV_LOG_ERROR, &config->errors,
  181. "audio sample rate is not set\n");
  182. break;
  183. case AVMEDIA_TYPE_VIDEO:
  184. if (av->width == 0 || av->height == 0)
  185. report_config_error(config->filename, config->line_num,
  186. AV_LOG_ERROR, &config->errors,
  187. "video size is not set\n");
  188. break;
  189. default:
  190. av_assert0(0);
  191. }
  192. goto done;
  193. }
  194. /* stream_use_defaults = true */
  195. /* compute default parameters */
  196. switch(av->codec_type) {
  197. case AVMEDIA_TYPE_AUDIO:
  198. if (!av_dict_get(recommended, "b", NULL, 0)) {
  199. av->bit_rate = 64000;
  200. av_dict_set_int(&recommended, "b", av->bit_rate, 0);
  201. WARNING("Setting default value for audio bit rate = %d. "
  202. "Use NoDefaults to disable it.\n",
  203. av->bit_rate);
  204. }
  205. if (!av_dict_get(recommended, "ar", NULL, 0)) {
  206. av->sample_rate = 22050;
  207. av_dict_set_int(&recommended, "ar", av->sample_rate, 0);
  208. WARNING("Setting default value for audio sample rate = %d. "
  209. "Use NoDefaults to disable it.\n",
  210. av->sample_rate);
  211. }
  212. if (!av_dict_get(recommended, "ac", NULL, 0)) {
  213. av->channels = 1;
  214. av_dict_set_int(&recommended, "ac", av->channels, 0);
  215. WARNING("Setting default value for audio channel count = %d. "
  216. "Use NoDefaults to disable it.\n",
  217. av->channels);
  218. }
  219. break;
  220. case AVMEDIA_TYPE_VIDEO:
  221. if (!av_dict_get(recommended, "b", NULL, 0)) {
  222. av->bit_rate = 64000;
  223. av_dict_set_int(&recommended, "b", av->bit_rate, 0);
  224. WARNING("Setting default value for video bit rate = %d. "
  225. "Use NoDefaults to disable it.\n",
  226. av->bit_rate);
  227. }
  228. if (!av_dict_get(recommended, "time_base", NULL, 0)){
  229. av->time_base.den = 5;
  230. av->time_base.num = 1;
  231. av_dict_set(&recommended, "time_base", "1/5", 0);
  232. WARNING("Setting default value for video frame rate = %d. "
  233. "Use NoDefaults to disable it.\n",
  234. av->time_base.den);
  235. }
  236. if (!av_dict_get(recommended, "video_size", NULL, 0)) {
  237. av->width = 160;
  238. av->height = 128;
  239. av_dict_set(&recommended, "video_size", "160x128", 0);
  240. WARNING("Setting default value for video size = %dx%d. "
  241. "Use NoDefaults to disable it.\n",
  242. av->width, av->height);
  243. }
  244. /* Bitrate tolerance is less for streaming */
  245. if (!av_dict_get(recommended, "bt", NULL, 0)) {
  246. av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
  247. (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
  248. av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0);
  249. WARNING("Setting default value for video bit rate tolerance = %d. "
  250. "Use NoDefaults to disable it.\n",
  251. av->bit_rate_tolerance);
  252. }
  253. if (!av_dict_get(recommended, "rc_eq", NULL, 0)) {
  254. av->rc_eq = av_strdup("tex^qComp");
  255. av_dict_set(&recommended, "rc_eq", "tex^qComp", 0);
  256. WARNING("Setting default value for video rate control equation = "
  257. "%s. Use NoDefaults to disable it.\n",
  258. av->rc_eq);
  259. }
  260. if (!av_dict_get(recommended, "maxrate", NULL, 0)) {
  261. av->rc_max_rate = av->bit_rate * 2;
  262. av_dict_set_int(&recommended, "maxrate", av->rc_max_rate, 0);
  263. WARNING("Setting default value for video max rate = %d. "
  264. "Use NoDefaults to disable it.\n",
  265. av->rc_max_rate);
  266. }
  267. if (av->rc_max_rate && !av_dict_get(recommended, "bufsize", NULL, 0)) {
  268. av->rc_buffer_size = av->rc_max_rate;
  269. av_dict_set_int(&recommended, "bufsize", av->rc_buffer_size, 0);
  270. WARNING("Setting default value for video buffer size = %d. "
  271. "Use NoDefaults to disable it.\n",
  272. av->rc_buffer_size);
  273. }
  274. break;
  275. default:
  276. abort();
  277. }
  278. done:
  279. st = av_mallocz(sizeof(AVStream));
  280. if (!st)
  281. return;
  282. av_dict_get_string(recommended, &enc_config, '=', ',');
  283. av_dict_free(&recommended);
  284. av_stream_set_recommended_encoder_configuration(st, enc_config);
  285. st->codec = av;
  286. stream->streams[stream->nb_streams++] = st;
  287. }
  288. static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name,
  289. FFServerConfig *config)
  290. {
  291. int ret;
  292. AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
  293. if (!codec || codec->type != ctx->codec_type) {
  294. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  295. &config->errors,
  296. "Invalid codec name: '%s'\n", codec_name);
  297. return 0;
  298. }
  299. if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
  300. if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
  301. return ret;
  302. ctx->codec = codec;
  303. }
  304. if (ctx->codec_id != codec->id)
  305. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  306. &config->errors,
  307. "Inconsistent configuration: trying to set '%s' "
  308. "codec option, but '%s' codec is used previously\n",
  309. codec_name, avcodec_get_name(ctx->codec_id));
  310. return 0;
  311. }
  312. static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
  313. {
  314. FILE *f=NULL;
  315. char filename[1000], tmp[1000], tmp2[1000], line[1000];
  316. int ret = 0;
  317. AVCodecContext *avctx;
  318. const AVCodec *codec;
  319. switch(type) {
  320. case AV_OPT_FLAG_AUDIO_PARAM:
  321. avctx = config->dummy_actx;
  322. break;
  323. case AV_OPT_FLAG_VIDEO_PARAM:
  324. avctx = config->dummy_vctx;
  325. break;
  326. default:
  327. av_assert0(0);
  328. }
  329. codec = avcodec_find_encoder(avctx->codec_id);
  330. if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
  331. codec ? codec->name : NULL))) {
  332. av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
  333. return AVERROR(EINVAL);
  334. }
  335. while(!feof(f)){
  336. int e= fscanf(f, "%999[^\n]\n", line) - 1;
  337. if(line[0] == '#' && !e)
  338. continue;
  339. e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
  340. if(e){
  341. av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename,
  342. line);
  343. ret = AVERROR(EINVAL);
  344. break;
  345. }
  346. if (!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO ||
  347. !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
  348. {
  349. if (ffserver_set_codec(avctx, tmp2, config) < 0)
  350. break;
  351. } else if (!strcmp(tmp, "scodec")) {
  352. av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
  353. ret = AVERROR(EINVAL);
  354. break;
  355. } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
  356. break;
  357. }
  358. fclose(f);
  359. return ret;
  360. }
  361. static AVOutputFormat *ffserver_guess_format(const char *short_name,
  362. const char *filename,
  363. const char *mime_type)
  364. {
  365. AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
  366. if (fmt) {
  367. AVOutputFormat *stream_fmt;
  368. char stream_format_name[64];
  369. snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
  370. fmt->name);
  371. stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
  372. if (stream_fmt)
  373. fmt = stream_fmt;
  374. }
  375. return fmt;
  376. }
  377. static void vreport_config_error(const char *filename, int line_num,
  378. int log_level, int *errors, const char *fmt,
  379. va_list vl)
  380. {
  381. av_log(NULL, log_level, "%s:%d: ", filename, line_num);
  382. av_vlog(NULL, log_level, fmt, vl);
  383. if (errors)
  384. (*errors)++;
  385. }
  386. static void report_config_error(const char *filename, int line_num,
  387. int log_level, int *errors,
  388. const char *fmt, ...)
  389. {
  390. va_list vl;
  391. va_start(vl, fmt);
  392. vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
  393. va_end(vl);
  394. }
  395. static int ffserver_set_int_param(int *dest, const char *value, int factor,
  396. int min, int max, FFServerConfig *config,
  397. const char *error_msg, ...)
  398. {
  399. int tmp;
  400. char *tailp;
  401. if (!value || !value[0])
  402. goto error;
  403. errno = 0;
  404. tmp = strtol(value, &tailp, 0);
  405. if (tmp < min || tmp > max)
  406. goto error;
  407. if (factor) {
  408. if (tmp == INT_MIN || FFABS(tmp) > INT_MAX / FFABS(factor))
  409. goto error;
  410. tmp *= factor;
  411. }
  412. if (tailp[0] || errno)
  413. goto error;
  414. if (dest)
  415. *dest = tmp;
  416. return 0;
  417. error:
  418. if (config) {
  419. va_list vl;
  420. va_start(vl, error_msg);
  421. vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  422. &config->errors, error_msg, vl);
  423. va_end(vl);
  424. }
  425. return AVERROR(EINVAL);
  426. }
  427. static int ffserver_set_float_param(float *dest, const char *value,
  428. float factor, float min, float max,
  429. FFServerConfig *config,
  430. const char *error_msg, ...)
  431. {
  432. double tmp;
  433. char *tailp;
  434. if (!value || !value[0])
  435. goto error;
  436. errno = 0;
  437. tmp = strtod(value, &tailp);
  438. if (tmp < min || tmp > max)
  439. goto error;
  440. if (factor)
  441. tmp *= factor;
  442. if (tailp[0] || errno)
  443. goto error;
  444. if (dest)
  445. *dest = tmp;
  446. return 0;
  447. error:
  448. if (config) {
  449. va_list vl;
  450. va_start(vl, error_msg);
  451. vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  452. &config->errors, error_msg, vl);
  453. va_end(vl);
  454. }
  455. return AVERROR(EINVAL);
  456. }
  457. static int ffserver_save_avoption(const char *opt, const char *arg, int type,
  458. FFServerConfig *config)
  459. {
  460. static int hinted = 0;
  461. int ret = 0;
  462. AVDictionaryEntry *e;
  463. const AVOption *o = NULL;
  464. const char *option = NULL;
  465. const char *codec_name = NULL;
  466. char buff[1024];
  467. AVCodecContext *ctx;
  468. AVDictionary **dict;
  469. enum AVCodecID guessed_codec_id;
  470. switch (type) {
  471. case AV_OPT_FLAG_VIDEO_PARAM:
  472. ctx = config->dummy_vctx;
  473. dict = &config->video_opts;
  474. guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
  475. config->guessed_video_codec_id : AV_CODEC_ID_H264;
  476. break;
  477. case AV_OPT_FLAG_AUDIO_PARAM:
  478. ctx = config->dummy_actx;
  479. dict = &config->audio_opts;
  480. guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
  481. config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
  482. break;
  483. default:
  484. av_assert0(0);
  485. }
  486. if (strchr(opt, ':')) {
  487. //explicit private option
  488. snprintf(buff, sizeof(buff), "%s", opt);
  489. codec_name = buff;
  490. if(!(option = strchr(buff, ':'))){
  491. report_config_error(config->filename, config->line_num,
  492. AV_LOG_ERROR, &config->errors,
  493. "Syntax error. Unmatched ':'\n");
  494. return -1;
  495. }
  496. buff[option - buff] = '\0';
  497. option++;
  498. if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
  499. return ret;
  500. if (!ctx->codec || !ctx->priv_data)
  501. return -1;
  502. } else {
  503. option = opt;
  504. }
  505. o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM,
  506. AV_OPT_SEARCH_CHILDREN);
  507. if (!o &&
  508. (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
  509. !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
  510. o = av_opt_find(ctx, option, NULL, 0, 0);
  511. if (!o) {
  512. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  513. &config->errors, "Option not found: '%s'\n", opt);
  514. if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
  515. hinted = 1;
  516. report_config_error(config->filename, config->line_num,
  517. AV_LOG_ERROR, NULL, "If '%s' is a codec private"
  518. "option, then prefix it with codec name, for "
  519. "example '%s:%s %s' or define codec earlier.\n",
  520. opt, avcodec_get_name(guessed_codec_id) ,opt,
  521. arg);
  522. }
  523. } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
  524. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  525. &config->errors, "Invalid value for option %s (%s): %s\n", opt,
  526. arg, av_err2str(ret));
  527. } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
  528. if ((o->type == AV_OPT_TYPE_FLAGS) && arg &&
  529. (arg[0] == '+' || arg[0] == '-'))
  530. return av_dict_set(dict, option, arg, AV_DICT_APPEND);
  531. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  532. &config->errors, "Redeclaring value of option '%s'."
  533. "Previous value was: '%s'.\n", opt, e->value);
  534. } else if (av_dict_set(dict, option, arg, 0) < 0) {
  535. return AVERROR(ENOMEM);
  536. }
  537. return 0;
  538. }
  539. static int ffserver_save_avoption_int(const char *opt, int64_t arg,
  540. int type, FFServerConfig *config)
  541. {
  542. char buf[22];
  543. snprintf(buf, sizeof(buf), "%"PRId64, arg);
  544. return ffserver_save_avoption(opt, buf, type, config);
  545. }
  546. static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
  547. const char **p)
  548. {
  549. int val;
  550. char arg[1024];
  551. if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
  552. if (!av_strcasecmp(cmd, "Port"))
  553. WARNING("Port option is deprecated. Use HTTPPort instead.\n");
  554. ffserver_get_arg(arg, sizeof(arg), p);
  555. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  556. "Invalid port: %s\n", arg);
  557. if (val < 1024)
  558. WARNING("Trying to use IETF assigned system port: '%d'\n", val);
  559. config->http_addr.sin_port = htons(val);
  560. } else if (!av_strcasecmp(cmd, "HTTPBindAddress") ||
  561. !av_strcasecmp(cmd, "BindAddress")) {
  562. if (!av_strcasecmp(cmd, "BindAddress"))
  563. WARNING("BindAddress option is deprecated. Use HTTPBindAddress "
  564. "instead.\n");
  565. ffserver_get_arg(arg, sizeof(arg), p);
  566. if (resolve_host(&config->http_addr.sin_addr, arg))
  567. ERROR("Invalid host/IP address: '%s'\n", arg);
  568. } else if (!av_strcasecmp(cmd, "NoDaemon")) {
  569. WARNING("NoDaemon option has no effect. You should remove it.\n");
  570. } else if (!av_strcasecmp(cmd, "RTSPPort")) {
  571. ffserver_get_arg(arg, sizeof(arg), p);
  572. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  573. "Invalid port: %s\n", arg);
  574. config->rtsp_addr.sin_port = htons(val);
  575. } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
  576. ffserver_get_arg(arg, sizeof(arg), p);
  577. if (resolve_host(&config->rtsp_addr.sin_addr, arg))
  578. ERROR("Invalid host/IP address: %s\n", arg);
  579. } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
  580. ffserver_get_arg(arg, sizeof(arg), p);
  581. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  582. "Invalid MaxHTTPConnections: %s\n", arg);
  583. config->nb_max_http_connections = val;
  584. if (config->nb_max_connections > config->nb_max_http_connections) {
  585. ERROR("Inconsistent configuration: MaxClients(%d) > "
  586. "MaxHTTPConnections(%d)\n", config->nb_max_connections,
  587. config->nb_max_http_connections);
  588. }
  589. } else if (!av_strcasecmp(cmd, "MaxClients")) {
  590. ffserver_get_arg(arg, sizeof(arg), p);
  591. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  592. "Invalid MaxClients: '%s'\n", arg);
  593. config->nb_max_connections = val;
  594. if (config->nb_max_connections > config->nb_max_http_connections) {
  595. ERROR("Inconsistent configuration: MaxClients(%d) > "
  596. "MaxHTTPConnections(%d)\n", config->nb_max_connections,
  597. config->nb_max_http_connections);
  598. }
  599. } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
  600. int64_t llval;
  601. char *tailp;
  602. ffserver_get_arg(arg, sizeof(arg), p);
  603. errno = 0;
  604. llval = strtoll(arg, &tailp, 10);
  605. if (llval < 10 || llval > 10000000 || tailp[0] || errno)
  606. ERROR("Invalid MaxBandwidth: '%s'\n", arg);
  607. else
  608. config->max_bandwidth = llval;
  609. } else if (!av_strcasecmp(cmd, "CustomLog")) {
  610. if (!config->debug) {
  611. ffserver_get_arg(config->logfilename, sizeof(config->logfilename),
  612. p);
  613. }
  614. } else if (!av_strcasecmp(cmd, "LoadModule")) {
  615. ERROR("Loadable modules are no longer supported\n");
  616. } else if (!av_strcasecmp(cmd, "NoDefaults")) {
  617. config->use_defaults = 0;
  618. } else if (!av_strcasecmp(cmd, "UseDefaults")) {
  619. config->use_defaults = 1;
  620. } else
  621. ERROR("Incorrect keyword: '%s'\n", cmd);
  622. return 0;
  623. }
  624. static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd,
  625. const char **p, FFServerStream **pfeed)
  626. {
  627. FFServerStream *feed;
  628. char arg[1024];
  629. av_assert0(pfeed);
  630. feed = *pfeed;
  631. if (!av_strcasecmp(cmd, "<Feed")) {
  632. char *q;
  633. FFServerStream *s;
  634. feed = av_mallocz(sizeof(FFServerStream));
  635. if (!feed)
  636. return AVERROR(ENOMEM);
  637. ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
  638. q = strrchr(feed->filename, '>');
  639. if (*q)
  640. *q = '\0';
  641. for (s = config->first_feed; s; s = s->next) {
  642. if (!strcmp(feed->filename, s->filename))
  643. ERROR("Feed '%s' already registered\n", s->filename);
  644. }
  645. feed->fmt = av_guess_format("ffm", NULL, NULL);
  646. /* default feed file */
  647. snprintf(feed->feed_filename, sizeof(feed->feed_filename),
  648. "/tmp/%s.ffm", feed->filename);
  649. feed->feed_max_size = 5 * 1024 * 1024;
  650. feed->is_feed = 1;
  651. feed->feed = feed; /* self feeding :-) */
  652. *pfeed = feed;
  653. return 0;
  654. }
  655. av_assert0(feed);
  656. if (!av_strcasecmp(cmd, "Launch")) {
  657. int i;
  658. feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
  659. if (!feed->child_argv)
  660. return AVERROR(ENOMEM);
  661. for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
  662. ffserver_get_arg(arg, sizeof(arg), p);
  663. if (!arg[0])
  664. break;
  665. feed->child_argv[i] = av_strdup(arg);
  666. if (!feed->child_argv[i])
  667. return AVERROR(ENOMEM);
  668. }
  669. feed->child_argv[i] =
  670. av_asprintf("http://%s:%d/%s",
  671. (config->http_addr.sin_addr.s_addr == INADDR_ANY) ?
  672. "127.0.0.1" : inet_ntoa(config->http_addr.sin_addr),
  673. ntohs(config->http_addr.sin_port), feed->filename);
  674. if (!feed->child_argv[i])
  675. return AVERROR(ENOMEM);
  676. } else if (!av_strcasecmp(cmd, "ACL")) {
  677. ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
  678. config->line_num);
  679. } else if (!av_strcasecmp(cmd, "File") ||
  680. !av_strcasecmp(cmd, "ReadOnlyFile")) {
  681. ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
  682. feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
  683. } else if (!av_strcasecmp(cmd, "Truncate")) {
  684. ffserver_get_arg(arg, sizeof(arg), p);
  685. /* assume Truncate is true in case no argument is specified */
  686. if (!arg[0]) {
  687. feed->truncate = 1;
  688. } else {
  689. WARNING("Truncate N syntax in configuration file is deprecated. "
  690. "Use Truncate alone with no arguments.\n");
  691. feed->truncate = strtod(arg, NULL);
  692. }
  693. } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
  694. char *p1;
  695. double fsize;
  696. ffserver_get_arg(arg, sizeof(arg), p);
  697. p1 = arg;
  698. fsize = strtod(p1, &p1);
  699. switch(av_toupper(*p1)) {
  700. case 'K':
  701. fsize *= 1024;
  702. break;
  703. case 'M':
  704. fsize *= 1024 * 1024;
  705. break;
  706. case 'G':
  707. fsize *= 1024 * 1024 * 1024;
  708. break;
  709. default:
  710. ERROR("Invalid file size: '%s'\n", arg);
  711. break;
  712. }
  713. feed->feed_max_size = (int64_t)fsize;
  714. if (feed->feed_max_size < FFM_PACKET_SIZE*4) {
  715. ERROR("Feed max file size is too small. Must be at least %d.\n",
  716. FFM_PACKET_SIZE*4);
  717. }
  718. } else if (!av_strcasecmp(cmd, "</Feed>")) {
  719. *pfeed = NULL;
  720. } else {
  721. ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
  722. }
  723. return 0;
  724. }
  725. static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd,
  726. const char **p,
  727. FFServerStream **pstream)
  728. {
  729. char arg[1024], arg2[1024];
  730. FFServerStream *stream;
  731. int val;
  732. av_assert0(pstream);
  733. stream = *pstream;
  734. if (!av_strcasecmp(cmd, "<Stream")) {
  735. char *q;
  736. FFServerStream *s;
  737. stream = av_mallocz(sizeof(FFServerStream));
  738. if (!stream)
  739. return AVERROR(ENOMEM);
  740. config->dummy_actx = avcodec_alloc_context3(NULL);
  741. config->dummy_vctx = avcodec_alloc_context3(NULL);
  742. if (!config->dummy_vctx || !config->dummy_actx) {
  743. av_free(stream);
  744. avcodec_free_context(&config->dummy_vctx);
  745. avcodec_free_context(&config->dummy_actx);
  746. return AVERROR(ENOMEM);
  747. }
  748. config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
  749. config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
  750. ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
  751. q = strrchr(stream->filename, '>');
  752. if (q)
  753. *q = '\0';
  754. for (s = config->first_stream; s; s = s->next) {
  755. if (!strcmp(stream->filename, s->filename))
  756. ERROR("Stream '%s' already registered\n", s->filename);
  757. }
  758. stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
  759. if (stream->fmt) {
  760. config->guessed_audio_codec_id = stream->fmt->audio_codec;
  761. config->guessed_video_codec_id = stream->fmt->video_codec;
  762. } else {
  763. config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
  764. config->guessed_video_codec_id = AV_CODEC_ID_NONE;
  765. }
  766. config->stream_use_defaults = config->use_defaults;
  767. *pstream = stream;
  768. return 0;
  769. }
  770. av_assert0(stream);
  771. if (!av_strcasecmp(cmd, "Feed")) {
  772. FFServerStream *sfeed;
  773. ffserver_get_arg(arg, sizeof(arg), p);
  774. sfeed = config->first_feed;
  775. while (sfeed) {
  776. if (!strcmp(sfeed->filename, arg))
  777. break;
  778. sfeed = sfeed->next_feed;
  779. }
  780. if (!sfeed)
  781. ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
  782. stream->filename);
  783. else
  784. stream->feed = sfeed;
  785. } else if (!av_strcasecmp(cmd, "Format")) {
  786. ffserver_get_arg(arg, sizeof(arg), p);
  787. if (!strcmp(arg, "status")) {
  788. stream->stream_type = STREAM_TYPE_STATUS;
  789. stream->fmt = NULL;
  790. } else {
  791. stream->stream_type = STREAM_TYPE_LIVE;
  792. /* JPEG cannot be used here, so use single frame MJPEG */
  793. if (!strcmp(arg, "jpeg")) {
  794. strcpy(arg, "singlejpeg");
  795. stream->single_frame=1;
  796. }
  797. stream->fmt = ffserver_guess_format(arg, NULL, NULL);
  798. if (!stream->fmt)
  799. ERROR("Unknown Format: '%s'\n", arg);
  800. }
  801. if (stream->fmt) {
  802. config->guessed_audio_codec_id = stream->fmt->audio_codec;
  803. config->guessed_video_codec_id = stream->fmt->video_codec;
  804. }
  805. } else if (!av_strcasecmp(cmd, "InputFormat")) {
  806. ffserver_get_arg(arg, sizeof(arg), p);
  807. stream->ifmt = av_find_input_format(arg);
  808. if (!stream->ifmt)
  809. ERROR("Unknown input format: '%s'\n", arg);
  810. } else if (!av_strcasecmp(cmd, "FaviconURL")) {
  811. if (stream->stream_type == STREAM_TYPE_STATUS)
  812. ffserver_get_arg(stream->feed_filename,
  813. sizeof(stream->feed_filename), p);
  814. else
  815. ERROR("FaviconURL only permitted for status streams\n");
  816. } else if (!av_strcasecmp(cmd, "Author") ||
  817. !av_strcasecmp(cmd, "Comment") ||
  818. !av_strcasecmp(cmd, "Copyright") ||
  819. !av_strcasecmp(cmd, "Title")) {
  820. char key[32];
  821. int i;
  822. ffserver_get_arg(arg, sizeof(arg), p);
  823. for (i = 0; i < strlen(cmd); i++)
  824. key[i] = av_tolower(cmd[i]);
  825. key[i] = 0;
  826. WARNING("Deprecated '%s' option in configuration file. Use "
  827. "'Metadata %s VALUE' instead.\n", cmd, key);
  828. if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
  829. goto nomem;
  830. } else if (!av_strcasecmp(cmd, "Metadata")) {
  831. ffserver_get_arg(arg, sizeof(arg), p);
  832. ffserver_get_arg(arg2, sizeof(arg2), p);
  833. if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
  834. goto nomem;
  835. } else if (!av_strcasecmp(cmd, "Preroll")) {
  836. ffserver_get_arg(arg, sizeof(arg), p);
  837. stream->prebuffer = atof(arg) * 1000;
  838. } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
  839. stream->send_on_key = 1;
  840. } else if (!av_strcasecmp(cmd, "AudioCodec")) {
  841. ffserver_get_arg(arg, sizeof(arg), p);
  842. ffserver_set_codec(config->dummy_actx, arg, config);
  843. } else if (!av_strcasecmp(cmd, "VideoCodec")) {
  844. ffserver_get_arg(arg, sizeof(arg), p);
  845. ffserver_set_codec(config->dummy_vctx, arg, config);
  846. } else if (!av_strcasecmp(cmd, "MaxTime")) {
  847. ffserver_get_arg(arg, sizeof(arg), p);
  848. stream->max_time = atof(arg) * 1000;
  849. } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
  850. float f;
  851. ffserver_get_arg(arg, sizeof(arg), p);
  852. ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
  853. "Invalid %s: '%s'\n", cmd, arg);
  854. if (ffserver_save_avoption_int("b", (int64_t)lrintf(f),
  855. AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
  856. goto nomem;
  857. } else if (!av_strcasecmp(cmd, "AudioChannels")) {
  858. ffserver_get_arg(arg, sizeof(arg), p);
  859. if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
  860. goto nomem;
  861. } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
  862. ffserver_get_arg(arg, sizeof(arg), p);
  863. if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
  864. goto nomem;
  865. } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
  866. int minrate, maxrate;
  867. char *dash;
  868. ffserver_get_arg(arg, sizeof(arg), p);
  869. dash = strchr(arg, '-');
  870. if (dash) {
  871. *dash = '\0';
  872. dash++;
  873. if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0 &&
  874. ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0) {
  875. if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
  876. ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  877. goto nomem;
  878. }
  879. } else
  880. ERROR("Incorrect format for VideoBitRateRange. It should be "
  881. "<min>-<max>: '%s'.\n", arg);
  882. } else if (!av_strcasecmp(cmd, "Debug")) {
  883. ffserver_get_arg(arg, sizeof(arg), p);
  884. if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
  885. ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  886. goto nomem;
  887. } else if (!av_strcasecmp(cmd, "Strict")) {
  888. ffserver_get_arg(arg, sizeof(arg), p);
  889. if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
  890. ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  891. goto nomem;
  892. } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
  893. ffserver_get_arg(arg, sizeof(arg), p);
  894. ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
  895. "Invalid %s: '%s'", cmd, arg);
  896. if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  897. goto nomem;
  898. } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
  899. ffserver_get_arg(arg, sizeof(arg), p);
  900. ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
  901. "Invalid %s: '%s'", cmd, arg);
  902. if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  903. goto nomem;
  904. } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
  905. ffserver_get_arg(arg, sizeof(arg), p);
  906. ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
  907. "Invalid %s: '%s'", cmd, arg);
  908. if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  909. goto nomem;
  910. } else if (!av_strcasecmp(cmd, "VideoSize")) {
  911. int ret, w, h;
  912. ffserver_get_arg(arg, sizeof(arg), p);
  913. ret = av_parse_video_size(&w, &h, arg);
  914. if (ret < 0)
  915. ERROR("Invalid video size '%s'\n", arg);
  916. else {
  917. if (w % 2 || h % 2)
  918. WARNING("Image size is not a multiple of 2\n");
  919. if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  920. goto nomem;
  921. }
  922. } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
  923. ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
  924. arg[0] = '1'; arg[1] = '/';
  925. if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  926. goto nomem;
  927. } else if (!av_strcasecmp(cmd, "PixelFormat")) {
  928. enum AVPixelFormat pix_fmt;
  929. ffserver_get_arg(arg, sizeof(arg), p);
  930. pix_fmt = av_get_pix_fmt(arg);
  931. if (pix_fmt == AV_PIX_FMT_NONE)
  932. ERROR("Unknown pixel format: '%s'\n", arg);
  933. else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  934. goto nomem;
  935. } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
  936. ffserver_get_arg(arg, sizeof(arg), p);
  937. if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  938. goto nomem;
  939. } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
  940. if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  941. goto nomem;
  942. } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
  943. if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  944. goto nomem;
  945. } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
  946. if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
  947. ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  948. goto nomem;
  949. } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
  950. !av_strcasecmp(cmd, "AVOptionAudio")) {
  951. int ret;
  952. ffserver_get_arg(arg, sizeof(arg), p);
  953. ffserver_get_arg(arg2, sizeof(arg2), p);
  954. if (!av_strcasecmp(cmd, "AVOptionVideo"))
  955. ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM,
  956. config);
  957. else
  958. ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM,
  959. config);
  960. if (ret < 0)
  961. goto nomem;
  962. } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
  963. !av_strcasecmp(cmd, "AVPresetAudio")) {
  964. ffserver_get_arg(arg, sizeof(arg), p);
  965. if (!av_strcasecmp(cmd, "AVPresetVideo"))
  966. ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
  967. else
  968. ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
  969. } else if (!av_strcasecmp(cmd, "VideoTag")) {
  970. ffserver_get_arg(arg, sizeof(arg), p);
  971. if (strlen(arg) == 4 &&
  972. ffserver_save_avoption_int("codec_tag",
  973. MKTAG(arg[0], arg[1], arg[2], arg[3]),
  974. AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  975. goto nomem;
  976. } else if (!av_strcasecmp(cmd, "BitExact")) {
  977. if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  978. goto nomem;
  979. } else if (!av_strcasecmp(cmd, "DctFastint")) {
  980. if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  981. goto nomem;
  982. } else if (!av_strcasecmp(cmd, "IdctSimple")) {
  983. if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  984. goto nomem;
  985. } else if (!av_strcasecmp(cmd, "Qscale")) {
  986. ffserver_get_arg(arg, sizeof(arg), p);
  987. ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
  988. "Invalid Qscale: '%s'\n", arg);
  989. if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
  990. ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
  991. AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  992. goto nomem;
  993. } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
  994. ffserver_get_arg(arg, sizeof(arg), p);
  995. if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  996. goto nomem;
  997. } else if (!av_strcasecmp(cmd, "VideoQMax")) {
  998. ffserver_get_arg(arg, sizeof(arg), p);
  999. if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  1000. goto nomem;
  1001. } else if (!av_strcasecmp(cmd, "VideoQMin")) {
  1002. ffserver_get_arg(arg, sizeof(arg), p);
  1003. if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  1004. goto nomem;
  1005. } else if (!av_strcasecmp(cmd, "LumiMask")) {
  1006. ffserver_get_arg(arg, sizeof(arg), p);
  1007. if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  1008. goto nomem;
  1009. } else if (!av_strcasecmp(cmd, "DarkMask")) {
  1010. ffserver_get_arg(arg, sizeof(arg), p);
  1011. if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  1012. goto nomem;
  1013. } else if (!av_strcasecmp(cmd, "NoVideo")) {
  1014. config->no_video = 1;
  1015. } else if (!av_strcasecmp(cmd, "NoAudio")) {
  1016. config->no_audio = 1;
  1017. } else if (!av_strcasecmp(cmd, "ACL")) {
  1018. ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
  1019. config->line_num);
  1020. } else if (!av_strcasecmp(cmd, "DynamicACL")) {
  1021. ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
  1022. } else if (!av_strcasecmp(cmd, "RTSPOption")) {
  1023. ffserver_get_arg(arg, sizeof(arg), p);
  1024. av_freep(&stream->rtsp_option);
  1025. stream->rtsp_option = av_strdup(arg);
  1026. } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
  1027. ffserver_get_arg(arg, sizeof(arg), p);
  1028. if (resolve_host(&stream->multicast_ip, arg))
  1029. ERROR("Invalid host/IP address: '%s'\n", arg);
  1030. stream->is_multicast = 1;
  1031. stream->loop = 1; /* default is looping */
  1032. } else if (!av_strcasecmp(cmd, "MulticastPort")) {
  1033. ffserver_get_arg(arg, sizeof(arg), p);
  1034. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  1035. "Invalid MulticastPort: '%s'\n", arg);
  1036. stream->multicast_port = val;
  1037. } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
  1038. ffserver_get_arg(arg, sizeof(arg), p);
  1039. ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
  1040. "Invalid MulticastTTL: '%s'\n", arg);
  1041. stream->multicast_ttl = val;
  1042. } else if (!av_strcasecmp(cmd, "NoLoop")) {
  1043. stream->loop = 0;
  1044. } else if (!av_strcasecmp(cmd, "</Stream>")) {
  1045. config->stream_use_defaults &= 1;
  1046. if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
  1047. if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
  1048. config->dummy_actx->codec_id = config->guessed_audio_codec_id;
  1049. if (!config->no_audio &&
  1050. config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
  1051. AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
  1052. add_codec(stream, audio_enc, config);
  1053. }
  1054. if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
  1055. config->dummy_vctx->codec_id = config->guessed_video_codec_id;
  1056. if (!config->no_video &&
  1057. config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
  1058. AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
  1059. add_codec(stream, video_enc, config);
  1060. }
  1061. }
  1062. av_dict_free(&config->video_opts);
  1063. av_dict_free(&config->audio_opts);
  1064. avcodec_free_context(&config->dummy_vctx);
  1065. avcodec_free_context(&config->dummy_actx);
  1066. config->no_video = 0;
  1067. config->no_audio = 0;
  1068. *pstream = NULL;
  1069. } else if (!av_strcasecmp(cmd, "File") ||
  1070. !av_strcasecmp(cmd, "ReadOnlyFile")) {
  1071. ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
  1072. p);
  1073. } else if (!av_strcasecmp(cmd, "UseDefaults")) {
  1074. if (config->stream_use_defaults > 1)
  1075. WARNING("Multiple UseDefaults/NoDefaults entries.\n");
  1076. config->stream_use_defaults = 3;
  1077. } else if (!av_strcasecmp(cmd, "NoDefaults")) {
  1078. if (config->stream_use_defaults > 1)
  1079. WARNING("Multiple UseDefaults/NoDefaults entries.\n");
  1080. config->stream_use_defaults = 2;
  1081. } else {
  1082. ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
  1083. }
  1084. return 0;
  1085. nomem:
  1086. av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
  1087. av_dict_free(&config->video_opts);
  1088. av_dict_free(&config->audio_opts);
  1089. avcodec_free_context(&config->dummy_vctx);
  1090. avcodec_free_context(&config->dummy_actx);
  1091. return AVERROR(ENOMEM);
  1092. }
  1093. static int ffserver_parse_config_redirect(FFServerConfig *config,
  1094. const char *cmd, const char **p,
  1095. FFServerStream **predirect)
  1096. {
  1097. FFServerStream *redirect;
  1098. av_assert0(predirect);
  1099. redirect = *predirect;
  1100. if (!av_strcasecmp(cmd, "<Redirect")) {
  1101. char *q;
  1102. redirect = av_mallocz(sizeof(FFServerStream));
  1103. if (!redirect)
  1104. return AVERROR(ENOMEM);
  1105. ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
  1106. q = strrchr(redirect->filename, '>');
  1107. if (*q)
  1108. *q = '\0';
  1109. redirect->stream_type = STREAM_TYPE_REDIRECT;
  1110. *predirect = redirect;
  1111. return 0;
  1112. }
  1113. av_assert0(redirect);
  1114. if (!av_strcasecmp(cmd, "URL")) {
  1115. ffserver_get_arg(redirect->feed_filename,
  1116. sizeof(redirect->feed_filename), p);
  1117. } else if (!av_strcasecmp(cmd, "</Redirect>")) {
  1118. if (!redirect->feed_filename[0])
  1119. ERROR("No URL found for <Redirect>\n");
  1120. *predirect = NULL;
  1121. } else {
  1122. ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
  1123. }
  1124. return 0;
  1125. }
  1126. int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
  1127. {
  1128. FILE *f;
  1129. char line[1024];
  1130. char cmd[64];
  1131. const char *p;
  1132. FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
  1133. FFServerStream **last_feed, *feed = NULL;
  1134. int ret = 0;
  1135. av_assert0(config);
  1136. f = fopen(filename, "r");
  1137. if (!f) {
  1138. ret = AVERROR(errno);
  1139. av_log(NULL, AV_LOG_ERROR,
  1140. "Could not open the configuration file '%s'\n", filename);
  1141. return ret;
  1142. }
  1143. config->first_stream = NULL;
  1144. config->first_feed = NULL;
  1145. config->errors = config->warnings = 0;
  1146. last_stream = &config->first_stream;
  1147. last_feed = &config->first_feed;
  1148. config->line_num = 0;
  1149. while (fgets(line, sizeof(line), f) != NULL) {
  1150. config->line_num++;
  1151. p = line;
  1152. while (av_isspace(*p))
  1153. p++;
  1154. if (*p == '\0' || *p == '#')
  1155. continue;
  1156. ffserver_get_arg(cmd, sizeof(cmd), &p);
  1157. if (feed || !av_strcasecmp(cmd, "<Feed")) {
  1158. int opening = !av_strcasecmp(cmd, "<Feed");
  1159. if (opening && (stream || feed || redirect)) {
  1160. ERROR("Already in a tag\n");
  1161. } else {
  1162. ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
  1163. if (ret < 0)
  1164. break;
  1165. if (opening) {
  1166. /* add in stream & feed list */
  1167. *last_stream = feed;
  1168. *last_feed = feed;
  1169. last_stream = &feed->next;
  1170. last_feed = &feed->next_feed;
  1171. }
  1172. }
  1173. } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
  1174. int opening = !av_strcasecmp(cmd, "<Stream");
  1175. if (opening && (stream || feed || redirect)) {
  1176. ERROR("Already in a tag\n");
  1177. } else {
  1178. ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
  1179. if (ret < 0)
  1180. break;
  1181. if (opening) {
  1182. /* add in stream list */
  1183. *last_stream = stream;
  1184. last_stream = &stream->next;
  1185. }
  1186. }
  1187. } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
  1188. int opening = !av_strcasecmp(cmd, "<Redirect");
  1189. if (opening && (stream || feed || redirect))
  1190. ERROR("Already in a tag\n");
  1191. else {
  1192. ret = ffserver_parse_config_redirect(config, cmd, &p,
  1193. &redirect);
  1194. if (ret < 0)
  1195. break;
  1196. if (opening) {
  1197. /* add in stream list */
  1198. *last_stream = redirect;
  1199. last_stream = &redirect->next;
  1200. }
  1201. }
  1202. } else {
  1203. ffserver_parse_config_global(config, cmd, &p);
  1204. }
  1205. }
  1206. if (stream || feed || redirect)
  1207. ERROR("Missing closing </%s> tag\n",
  1208. stream ? "Stream" : (feed ? "Feed" : "Redirect"));
  1209. fclose(f);
  1210. if (ret < 0)
  1211. return ret;
  1212. if (config->errors)
  1213. return AVERROR(EINVAL);
  1214. else
  1215. return 0;
  1216. }
  1217. #undef ERROR
  1218. #undef WARNING
  1219. void ffserver_free_child_args(void *argsp)
  1220. {
  1221. int i;
  1222. char **args;
  1223. if (!argsp)
  1224. return;
  1225. args = *(char ***)argsp;
  1226. if (!args)
  1227. return;
  1228. for (i = 0; i < MAX_CHILD_ARGS; i++)
  1229. av_free(args[i]);
  1230. av_freep(argsp);
  1231. }