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.

1261 lines
48KB

  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' is not 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. option = strchr(buff, ':');
  473. buff[option - buff] = '\0';
  474. option++;
  475. if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
  476. return ret;
  477. if (!ctx->codec || !ctx->priv_data)
  478. return -1;
  479. } else {
  480. option = opt;
  481. }
  482. o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
  483. if (!o && (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
  484. !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
  485. o = av_opt_find(ctx, option, NULL, 0, 0);
  486. if (!o) {
  487. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  488. &config->errors, "Option not found: %s\n", opt);
  489. if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
  490. hinted = 1;
  491. report_config_error(config->filename, config->line_num, AV_LOG_ERROR, NULL,
  492. "If '%s' is a codec private option, then prefix it with codec name, "
  493. "for example '%s:%s %s' or define codec earlier.\n",
  494. opt, avcodec_get_name(guessed_codec_id) ,opt, arg);
  495. }
  496. } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
  497. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  498. &config->errors, "Invalid value for option %s (%s): %s\n", opt,
  499. arg, av_err2str(ret));
  500. } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
  501. if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
  502. return av_dict_set(dict, option, arg, AV_DICT_APPEND);
  503. report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
  504. &config->errors,
  505. "Redeclaring value of the option %s, previous value: %s\n",
  506. opt, e->value);
  507. } else if (av_dict_set(dict, option, arg, 0) < 0) {
  508. return AVERROR(ENOMEM);
  509. }
  510. return 0;
  511. }
  512. static int ffserver_save_avoption_int(const char *opt, int64_t arg,
  513. int type, FFServerConfig *config)
  514. {
  515. char buf[22];
  516. snprintf(buf, sizeof(buf), "%"PRId64, arg);
  517. return ffserver_save_avoption(opt, buf, type, config);
  518. }
  519. static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
  520. const char **p)
  521. {
  522. int val;
  523. char arg[1024];
  524. if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
  525. if (!av_strcasecmp(cmd, "Port"))
  526. WARNING("Port option is deprecated, use HTTPPort instead\n");
  527. ffserver_get_arg(arg, sizeof(arg), p);
  528. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  529. "Invalid port: %s\n", arg);
  530. if (val < 1024)
  531. WARNING("Trying to use IETF assigned system port: %d\n", val);
  532. config->http_addr.sin_port = htons(val);
  533. } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
  534. if (!av_strcasecmp(cmd, "BindAddress"))
  535. WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
  536. ffserver_get_arg(arg, sizeof(arg), p);
  537. if (resolve_host(&config->http_addr.sin_addr, arg))
  538. ERROR("Invalid host/IP address: %s\n", arg);
  539. } else if (!av_strcasecmp(cmd, "NoDaemon")) {
  540. WARNING("NoDaemon option has no effect, you should remove it\n");
  541. } else if (!av_strcasecmp(cmd, "RTSPPort")) {
  542. ffserver_get_arg(arg, sizeof(arg), p);
  543. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  544. "Invalid port: %s\n", arg);
  545. config->rtsp_addr.sin_port = htons(val);
  546. } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
  547. ffserver_get_arg(arg, sizeof(arg), p);
  548. if (resolve_host(&config->rtsp_addr.sin_addr, arg))
  549. ERROR("Invalid host/IP address: %s\n", arg);
  550. } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
  551. ffserver_get_arg(arg, sizeof(arg), p);
  552. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  553. "Invalid MaxHTTPConnections: %s\n", arg);
  554. config->nb_max_http_connections = val;
  555. if (config->nb_max_connections > config->nb_max_http_connections)
  556. ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
  557. config->nb_max_connections, config->nb_max_http_connections);
  558. } else if (!av_strcasecmp(cmd, "MaxClients")) {
  559. ffserver_get_arg(arg, sizeof(arg), p);
  560. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  561. "Invalid MaxClients: %s\n", arg);
  562. config->nb_max_connections = val;
  563. if (config->nb_max_connections > config->nb_max_http_connections)
  564. ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
  565. config->nb_max_connections, config->nb_max_http_connections);
  566. } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
  567. int64_t llval;
  568. char *tailp;
  569. ffserver_get_arg(arg, sizeof(arg), p);
  570. errno = 0;
  571. llval = strtoll(arg, &tailp, 10);
  572. if (llval < 10 || llval > 10000000 || tailp[0] || errno)
  573. ERROR("Invalid MaxBandwidth: %s\n", arg);
  574. else
  575. config->max_bandwidth = llval;
  576. } else if (!av_strcasecmp(cmd, "CustomLog")) {
  577. if (!config->debug)
  578. ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
  579. } else if (!av_strcasecmp(cmd, "LoadModule")) {
  580. ERROR("Loadable modules are no longer supported\n");
  581. } else if (!av_strcasecmp(cmd, "NoDefaults")) {
  582. config->use_defaults = 0;
  583. } else if (!av_strcasecmp(cmd, "UseDefaults")) {
  584. config->use_defaults = 1;
  585. } else
  586. ERROR("Incorrect keyword: '%s'\n", cmd);
  587. return 0;
  588. }
  589. static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
  590. FFServerStream **pfeed)
  591. {
  592. FFServerStream *feed;
  593. char arg[1024];
  594. av_assert0(pfeed);
  595. feed = *pfeed;
  596. if (!av_strcasecmp(cmd, "<Feed")) {
  597. char *q;
  598. FFServerStream *s;
  599. feed = av_mallocz(sizeof(FFServerStream));
  600. if (!feed)
  601. return AVERROR(ENOMEM);
  602. ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
  603. q = strrchr(feed->filename, '>');
  604. if (*q)
  605. *q = '\0';
  606. for (s = config->first_feed; s; s = s->next) {
  607. if (!strcmp(feed->filename, s->filename))
  608. ERROR("Feed '%s' already registered\n", s->filename);
  609. }
  610. feed->fmt = av_guess_format("ffm", NULL, NULL);
  611. /* default feed file */
  612. snprintf(feed->feed_filename, sizeof(feed->feed_filename),
  613. "/tmp/%s.ffm", feed->filename);
  614. feed->feed_max_size = 5 * 1024 * 1024;
  615. feed->is_feed = 1;
  616. feed->feed = feed; /* self feeding :-) */
  617. *pfeed = feed;
  618. return 0;
  619. }
  620. av_assert0(feed);
  621. if (!av_strcasecmp(cmd, "Launch")) {
  622. int i;
  623. feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
  624. if (!feed->child_argv)
  625. return AVERROR(ENOMEM);
  626. for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
  627. ffserver_get_arg(arg, sizeof(arg), p);
  628. if (!arg[0])
  629. break;
  630. feed->child_argv[i] = av_strdup(arg);
  631. if (!feed->child_argv[i])
  632. return AVERROR(ENOMEM);
  633. }
  634. feed->child_argv[i] =
  635. av_asprintf("http://%s:%d/%s",
  636. (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
  637. inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
  638. feed->filename);
  639. if (!feed->child_argv[i])
  640. return AVERROR(ENOMEM);
  641. } else if (!av_strcasecmp(cmd, "ACL")) {
  642. ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
  643. config->line_num);
  644. } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
  645. ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
  646. feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
  647. } else if (!av_strcasecmp(cmd, "Truncate")) {
  648. ffserver_get_arg(arg, sizeof(arg), p);
  649. /* assume Truncate is true in case no argument is specified */
  650. if (!arg[0]) {
  651. feed->truncate = 1;
  652. } else {
  653. WARNING("Truncate N syntax in configuration file is deprecated, "
  654. "use Truncate alone with no arguments\n");
  655. feed->truncate = strtod(arg, NULL);
  656. }
  657. } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
  658. char *p1;
  659. double fsize;
  660. ffserver_get_arg(arg, sizeof(arg), p);
  661. p1 = arg;
  662. fsize = strtod(p1, &p1);
  663. switch(av_toupper(*p1)) {
  664. case 'K':
  665. fsize *= 1024;
  666. break;
  667. case 'M':
  668. fsize *= 1024 * 1024;
  669. break;
  670. case 'G':
  671. fsize *= 1024 * 1024 * 1024;
  672. break;
  673. default:
  674. ERROR("Invalid file size: %s\n", arg);
  675. break;
  676. }
  677. feed->feed_max_size = (int64_t)fsize;
  678. if (feed->feed_max_size < FFM_PACKET_SIZE*4)
  679. ERROR("Feed max file size is too small, must be at least %d\n",
  680. FFM_PACKET_SIZE*4);
  681. } else if (!av_strcasecmp(cmd, "</Feed>")) {
  682. *pfeed = NULL;
  683. } else {
  684. ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
  685. }
  686. return 0;
  687. }
  688. static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
  689. FFServerStream **pstream)
  690. {
  691. char arg[1024], arg2[1024];
  692. FFServerStream *stream;
  693. int val;
  694. av_assert0(pstream);
  695. stream = *pstream;
  696. if (!av_strcasecmp(cmd, "<Stream")) {
  697. char *q;
  698. FFServerStream *s;
  699. stream = av_mallocz(sizeof(FFServerStream));
  700. if (!stream)
  701. return AVERROR(ENOMEM);
  702. config->dummy_actx = avcodec_alloc_context3(NULL);
  703. config->dummy_vctx = avcodec_alloc_context3(NULL);
  704. if (!config->dummy_vctx || !config->dummy_actx) {
  705. av_free(stream);
  706. avcodec_free_context(&config->dummy_vctx);
  707. avcodec_free_context(&config->dummy_actx);
  708. return AVERROR(ENOMEM);
  709. }
  710. config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
  711. config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
  712. ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
  713. q = strrchr(stream->filename, '>');
  714. if (q)
  715. *q = '\0';
  716. for (s = config->first_stream; s; s = s->next) {
  717. if (!strcmp(stream->filename, s->filename))
  718. ERROR("Stream '%s' already registered\n", s->filename);
  719. }
  720. stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
  721. if (stream->fmt) {
  722. config->guessed_audio_codec_id = stream->fmt->audio_codec;
  723. config->guessed_video_codec_id = stream->fmt->video_codec;
  724. } else {
  725. config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
  726. config->guessed_video_codec_id = AV_CODEC_ID_NONE;
  727. }
  728. config->stream_use_defaults = config->use_defaults;
  729. *pstream = stream;
  730. return 0;
  731. }
  732. av_assert0(stream);
  733. if (!av_strcasecmp(cmd, "Feed")) {
  734. FFServerStream *sfeed;
  735. ffserver_get_arg(arg, sizeof(arg), p);
  736. sfeed = config->first_feed;
  737. while (sfeed) {
  738. if (!strcmp(sfeed->filename, arg))
  739. break;
  740. sfeed = sfeed->next_feed;
  741. }
  742. if (!sfeed)
  743. ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
  744. stream->filename);
  745. else
  746. stream->feed = sfeed;
  747. } else if (!av_strcasecmp(cmd, "Format")) {
  748. ffserver_get_arg(arg, sizeof(arg), p);
  749. if (!strcmp(arg, "status")) {
  750. stream->stream_type = STREAM_TYPE_STATUS;
  751. stream->fmt = NULL;
  752. } else {
  753. stream->stream_type = STREAM_TYPE_LIVE;
  754. /* JPEG cannot be used here, so use single frame MJPEG */
  755. if (!strcmp(arg, "jpeg"))
  756. strcpy(arg, "mjpeg");
  757. stream->fmt = ffserver_guess_format(arg, NULL, NULL);
  758. if (!stream->fmt)
  759. ERROR("Unknown Format: %s\n", arg);
  760. }
  761. if (stream->fmt) {
  762. config->guessed_audio_codec_id = stream->fmt->audio_codec;
  763. config->guessed_video_codec_id = stream->fmt->video_codec;
  764. }
  765. } else if (!av_strcasecmp(cmd, "InputFormat")) {
  766. ffserver_get_arg(arg, sizeof(arg), p);
  767. stream->ifmt = av_find_input_format(arg);
  768. if (!stream->ifmt)
  769. ERROR("Unknown input format: %s\n", arg);
  770. } else if (!av_strcasecmp(cmd, "FaviconURL")) {
  771. if (stream->stream_type == STREAM_TYPE_STATUS)
  772. ffserver_get_arg(stream->feed_filename,
  773. sizeof(stream->feed_filename), p);
  774. else
  775. ERROR("FaviconURL only permitted for status streams\n");
  776. } else if (!av_strcasecmp(cmd, "Author") ||
  777. !av_strcasecmp(cmd, "Comment") ||
  778. !av_strcasecmp(cmd, "Copyright") ||
  779. !av_strcasecmp(cmd, "Title")) {
  780. char key[32];
  781. int i;
  782. ffserver_get_arg(arg, sizeof(arg), p);
  783. for (i = 0; i < strlen(cmd); i++)
  784. key[i] = av_tolower(cmd[i]);
  785. key[i] = 0;
  786. WARNING("'%s' option in configuration file is deprecated, "
  787. "use 'Metadata %s VALUE' instead\n", cmd, key);
  788. if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
  789. goto nomem;
  790. } else if (!av_strcasecmp(cmd, "Metadata")) {
  791. ffserver_get_arg(arg, sizeof(arg), p);
  792. ffserver_get_arg(arg2, sizeof(arg2), p);
  793. if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
  794. goto nomem;
  795. } else if (!av_strcasecmp(cmd, "Preroll")) {
  796. ffserver_get_arg(arg, sizeof(arg), p);
  797. stream->prebuffer = atof(arg) * 1000;
  798. } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
  799. stream->send_on_key = 1;
  800. } else if (!av_strcasecmp(cmd, "AudioCodec")) {
  801. ffserver_get_arg(arg, sizeof(arg), p);
  802. ffserver_set_codec(config->dummy_actx, arg, config);
  803. } else if (!av_strcasecmp(cmd, "VideoCodec")) {
  804. ffserver_get_arg(arg, sizeof(arg), p);
  805. ffserver_set_codec(config->dummy_vctx, arg, config);
  806. } else if (!av_strcasecmp(cmd, "MaxTime")) {
  807. ffserver_get_arg(arg, sizeof(arg), p);
  808. stream->max_time = atof(arg) * 1000;
  809. } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
  810. float f;
  811. ffserver_get_arg(arg, sizeof(arg), p);
  812. ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
  813. "Invalid %s: %s\n", cmd, arg);
  814. if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f), AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
  815. goto nomem;
  816. } else if (!av_strcasecmp(cmd, "AudioChannels")) {
  817. ffserver_get_arg(arg, sizeof(arg), p);
  818. if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
  819. goto nomem;
  820. } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
  821. ffserver_get_arg(arg, sizeof(arg), p);
  822. if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
  823. goto nomem;
  824. } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
  825. int minrate, maxrate;
  826. char *dash;
  827. ffserver_get_arg(arg, sizeof(arg), p);
  828. dash = strchr(arg, '-');
  829. if (dash) {
  830. *dash = '\0';
  831. dash++;
  832. if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0 &&
  833. ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0) {
  834. if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
  835. ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  836. goto nomem;
  837. }
  838. } else
  839. ERROR("Incorrect format for VideoBitRateRange -- should be "
  840. "<min>-<max>: %s\n", arg);
  841. } else if (!av_strcasecmp(cmd, "Debug")) {
  842. ffserver_get_arg(arg, sizeof(arg), p);
  843. if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
  844. ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  845. goto nomem;
  846. } else if (!av_strcasecmp(cmd, "Strict")) {
  847. ffserver_get_arg(arg, sizeof(arg), p);
  848. if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
  849. ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  850. goto nomem;
  851. } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
  852. ffserver_get_arg(arg, sizeof(arg), p);
  853. ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
  854. "Invalid %s: %s", cmd, arg);
  855. if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  856. goto nomem;
  857. } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
  858. ffserver_get_arg(arg, sizeof(arg), p);
  859. ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
  860. "Invalid %s: %s", cmd, arg);
  861. if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  862. goto nomem;
  863. } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
  864. ffserver_get_arg(arg, sizeof(arg), p);
  865. ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
  866. "Invalid %s: %s", cmd, arg);
  867. if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  868. goto nomem;
  869. } else if (!av_strcasecmp(cmd, "VideoSize")) {
  870. int ret, w, h;
  871. ffserver_get_arg(arg, sizeof(arg), p);
  872. ret = av_parse_video_size(&w, &h, arg);
  873. if (ret < 0)
  874. ERROR("Invalid video size '%s'\n", arg);
  875. else {
  876. if (w % 2 || h % 2)
  877. WARNING("Image size is not a multiple of 2\n");
  878. if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  879. goto nomem;
  880. }
  881. } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
  882. ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
  883. arg[0] = '1'; arg[1] = '/';
  884. if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  885. goto nomem;
  886. } else if (!av_strcasecmp(cmd, "PixelFormat")) {
  887. enum AVPixelFormat pix_fmt;
  888. ffserver_get_arg(arg, sizeof(arg), p);
  889. pix_fmt = av_get_pix_fmt(arg);
  890. if (pix_fmt == AV_PIX_FMT_NONE)
  891. ERROR("Unknown pixel format: %s\n", arg);
  892. else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  893. goto nomem;
  894. } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
  895. ffserver_get_arg(arg, sizeof(arg), p);
  896. if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  897. goto nomem;
  898. } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
  899. if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  900. goto nomem;
  901. } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
  902. if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  903. goto nomem;
  904. } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
  905. if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
  906. ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  907. goto nomem;
  908. } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
  909. !av_strcasecmp(cmd, "AVOptionAudio")) {
  910. int ret;
  911. ffserver_get_arg(arg, sizeof(arg), p);
  912. ffserver_get_arg(arg2, sizeof(arg2), p);
  913. if (!av_strcasecmp(cmd, "AVOptionVideo"))
  914. ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config);
  915. else
  916. ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config);
  917. if (ret < 0)
  918. goto nomem;
  919. } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
  920. !av_strcasecmp(cmd, "AVPresetAudio")) {
  921. ffserver_get_arg(arg, sizeof(arg), p);
  922. if (!av_strcasecmp(cmd, "AVPresetVideo"))
  923. ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
  924. else
  925. ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
  926. } else if (!av_strcasecmp(cmd, "VideoTag")) {
  927. ffserver_get_arg(arg, sizeof(arg), p);
  928. if (strlen(arg) == 4 &&
  929. ffserver_save_avoption_int("codec_tag", MKTAG(arg[0], arg[1], arg[2], arg[3]),
  930. AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  931. goto nomem;
  932. } else if (!av_strcasecmp(cmd, "BitExact")) {
  933. if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  934. goto nomem;
  935. } else if (!av_strcasecmp(cmd, "DctFastint")) {
  936. if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  937. goto nomem;
  938. } else if (!av_strcasecmp(cmd, "IdctSimple")) {
  939. if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  940. goto nomem;
  941. } else if (!av_strcasecmp(cmd, "Qscale")) {
  942. ffserver_get_arg(arg, sizeof(arg), p);
  943. ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
  944. "Invalid Qscale: %s\n", arg);
  945. if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
  946. ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
  947. AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  948. goto nomem;
  949. } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
  950. ffserver_get_arg(arg, sizeof(arg), p);
  951. if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  952. goto nomem;
  953. } else if (!av_strcasecmp(cmd, "VideoQMax")) {
  954. ffserver_get_arg(arg, sizeof(arg), p);
  955. if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  956. goto nomem;
  957. } else if (!av_strcasecmp(cmd, "VideoQMin")) {
  958. ffserver_get_arg(arg, sizeof(arg), p);
  959. if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  960. goto nomem;
  961. } else if (!av_strcasecmp(cmd, "LumiMask")) {
  962. ffserver_get_arg(arg, sizeof(arg), p);
  963. if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  964. goto nomem;
  965. } else if (!av_strcasecmp(cmd, "DarkMask")) {
  966. ffserver_get_arg(arg, sizeof(arg), p);
  967. if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
  968. goto nomem;
  969. } else if (!av_strcasecmp(cmd, "NoVideo")) {
  970. config->no_video = 1;
  971. } else if (!av_strcasecmp(cmd, "NoAudio")) {
  972. config->no_audio = 1;
  973. } else if (!av_strcasecmp(cmd, "ACL")) {
  974. ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
  975. config->line_num);
  976. } else if (!av_strcasecmp(cmd, "DynamicACL")) {
  977. ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
  978. } else if (!av_strcasecmp(cmd, "RTSPOption")) {
  979. ffserver_get_arg(arg, sizeof(arg), p);
  980. av_freep(&stream->rtsp_option);
  981. stream->rtsp_option = av_strdup(arg);
  982. } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
  983. ffserver_get_arg(arg, sizeof(arg), p);
  984. if (resolve_host(&stream->multicast_ip, arg))
  985. ERROR("Invalid host/IP address: %s\n", arg);
  986. stream->is_multicast = 1;
  987. stream->loop = 1; /* default is looping */
  988. } else if (!av_strcasecmp(cmd, "MulticastPort")) {
  989. ffserver_get_arg(arg, sizeof(arg), p);
  990. ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
  991. "Invalid MulticastPort: %s\n", arg);
  992. stream->multicast_port = val;
  993. } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
  994. ffserver_get_arg(arg, sizeof(arg), p);
  995. ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
  996. "Invalid MulticastTTL: %s\n", arg);
  997. stream->multicast_ttl = val;
  998. } else if (!av_strcasecmp(cmd, "NoLoop")) {
  999. stream->loop = 0;
  1000. } else if (!av_strcasecmp(cmd, "</Stream>")) {
  1001. config->stream_use_defaults &= 1;
  1002. if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
  1003. if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
  1004. config->dummy_actx->codec_id = config->guessed_audio_codec_id;
  1005. if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
  1006. AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
  1007. add_codec(stream, audio_enc, config);
  1008. }
  1009. if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
  1010. config->dummy_vctx->codec_id = config->guessed_video_codec_id;
  1011. if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
  1012. AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
  1013. add_codec(stream, video_enc, config);
  1014. }
  1015. }
  1016. av_dict_free(&config->video_opts);
  1017. av_dict_free(&config->audio_opts);
  1018. avcodec_free_context(&config->dummy_vctx);
  1019. avcodec_free_context(&config->dummy_actx);
  1020. *pstream = NULL;
  1021. } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
  1022. ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
  1023. p);
  1024. } else if (!av_strcasecmp(cmd, "UseDefaults")) {
  1025. if (config->stream_use_defaults > 1)
  1026. WARNING("Multiple UseDefaults/NoDefaults entries.\n");
  1027. config->stream_use_defaults = 3;
  1028. } else if (!av_strcasecmp(cmd, "NoDefaults")) {
  1029. if (config->stream_use_defaults > 1)
  1030. WARNING("Multiple UseDefaults/NoDefaults entries.\n");
  1031. config->stream_use_defaults = 2;
  1032. } else {
  1033. ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
  1034. }
  1035. return 0;
  1036. nomem:
  1037. av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
  1038. av_dict_free(&config->video_opts);
  1039. av_dict_free(&config->audio_opts);
  1040. avcodec_free_context(&config->dummy_vctx);
  1041. avcodec_free_context(&config->dummy_actx);
  1042. return AVERROR(ENOMEM);
  1043. }
  1044. static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
  1045. FFServerStream **predirect)
  1046. {
  1047. FFServerStream *redirect;
  1048. av_assert0(predirect);
  1049. redirect = *predirect;
  1050. if (!av_strcasecmp(cmd, "<Redirect")) {
  1051. char *q;
  1052. redirect = av_mallocz(sizeof(FFServerStream));
  1053. if (!redirect)
  1054. return AVERROR(ENOMEM);
  1055. ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
  1056. q = strrchr(redirect->filename, '>');
  1057. if (*q)
  1058. *q = '\0';
  1059. redirect->stream_type = STREAM_TYPE_REDIRECT;
  1060. *predirect = redirect;
  1061. return 0;
  1062. }
  1063. av_assert0(redirect);
  1064. if (!av_strcasecmp(cmd, "URL")) {
  1065. ffserver_get_arg(redirect->feed_filename,
  1066. sizeof(redirect->feed_filename), p);
  1067. } else if (!av_strcasecmp(cmd, "</Redirect>")) {
  1068. if (!redirect->feed_filename[0])
  1069. ERROR("No URL found for <Redirect>\n");
  1070. *predirect = NULL;
  1071. } else {
  1072. ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
  1073. }
  1074. return 0;
  1075. }
  1076. int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
  1077. {
  1078. FILE *f;
  1079. char line[1024];
  1080. char cmd[64];
  1081. const char *p;
  1082. FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
  1083. FFServerStream **last_feed, *feed = NULL;
  1084. int ret = 0;
  1085. av_assert0(config);
  1086. f = fopen(filename, "r");
  1087. if (!f) {
  1088. ret = AVERROR(errno);
  1089. av_log(NULL, AV_LOG_ERROR,
  1090. "Could not open the configuration file '%s'\n", filename);
  1091. return ret;
  1092. }
  1093. config->first_stream = NULL;
  1094. config->first_feed = NULL;
  1095. config->errors = config->warnings = 0;
  1096. last_stream = &config->first_stream;
  1097. last_feed = &config->first_feed;
  1098. config->line_num = 0;
  1099. while (fgets(line, sizeof(line), f) != NULL) {
  1100. config->line_num++;
  1101. p = line;
  1102. while (av_isspace(*p))
  1103. p++;
  1104. if (*p == '\0' || *p == '#')
  1105. continue;
  1106. ffserver_get_arg(cmd, sizeof(cmd), &p);
  1107. if (feed || !av_strcasecmp(cmd, "<Feed")) {
  1108. int opening = !av_strcasecmp(cmd, "<Feed");
  1109. if (opening && (stream || feed || redirect)) {
  1110. ERROR("Already in a tag\n");
  1111. } else {
  1112. ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
  1113. if (ret < 0)
  1114. break;
  1115. if (opening) {
  1116. /* add in stream & feed list */
  1117. *last_stream = feed;
  1118. *last_feed = feed;
  1119. last_stream = &feed->next;
  1120. last_feed = &feed->next_feed;
  1121. }
  1122. }
  1123. } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
  1124. int opening = !av_strcasecmp(cmd, "<Stream");
  1125. if (opening && (stream || feed || redirect)) {
  1126. ERROR("Already in a tag\n");
  1127. } else {
  1128. ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
  1129. if (ret < 0)
  1130. break;
  1131. if (opening) {
  1132. /* add in stream list */
  1133. *last_stream = stream;
  1134. last_stream = &stream->next;
  1135. }
  1136. }
  1137. } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
  1138. int opening = !av_strcasecmp(cmd, "<Redirect");
  1139. if (opening && (stream || feed || redirect))
  1140. ERROR("Already in a tag\n");
  1141. else {
  1142. ret = ffserver_parse_config_redirect(config, cmd, &p,
  1143. &redirect);
  1144. if (ret < 0)
  1145. break;
  1146. if (opening) {
  1147. /* add in stream list */
  1148. *last_stream = redirect;
  1149. last_stream = &redirect->next;
  1150. }
  1151. }
  1152. } else {
  1153. ffserver_parse_config_global(config, cmd, &p);
  1154. }
  1155. }
  1156. if (stream || feed || redirect)
  1157. ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>"));
  1158. fclose(f);
  1159. if (ret < 0)
  1160. return ret;
  1161. if (config->errors)
  1162. return AVERROR(EINVAL);
  1163. else
  1164. return 0;
  1165. }
  1166. #undef ERROR
  1167. #undef WARNING
  1168. void ffserver_free_child_args(void *argsp)
  1169. {
  1170. int i;
  1171. char **args;
  1172. if (!argsp)
  1173. return;
  1174. args = *(char ***)argsp;
  1175. if (!args)
  1176. return;
  1177. for (i = 0; i < MAX_CHILD_ARGS; i++)
  1178. av_free(args[i]);
  1179. av_freep(argsp);
  1180. }