You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1310 lines
50KB

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