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.

1268 lines
49KB

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