ffserver_config.c 48 KB

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