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.

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