ffserver_config.c 50 KB

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