2 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/avassert.h"
28 // FIXME those are internal headers, ffserver _really_ shouldn't use them
29 #include "libavformat/ffm.h"
32 #include "ffserver_config.h"
34 /* FIXME: make ffserver work with IPv6 */
35 /* resolve host with also IP address parsing */
36 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
39 if (!ff_inet_aton(hostname, sin_addr)) {
41 struct addrinfo *ai, *cur;
42 struct addrinfo hints = { 0 };
43 hints.ai_family = AF_INET;
44 if (getaddrinfo(hostname, NULL, &hints, &ai))
46 /* getaddrinfo returns a linked list of addrinfo structs.
47 * Even if we set ai_family = AF_INET above, make sure
48 * that the returned one actually is of the correct type. */
49 for (cur = ai; cur; cur = cur->ai_next) {
50 if (cur->ai_family == AF_INET) {
51 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
60 hp = gethostbyname(hostname);
63 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
69 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
76 while (av_isspace(*p)) p++;
79 if (*p == '\"' || *p == '\'')
91 if ((q - buf) < buf_size - 1)
96 if (quote && *p == quote)
101 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
102 FFServerIPAddressACL *ext_acl,
103 const char *p, const char *filename, int line_num)
106 FFServerIPAddressACL acl;
109 ffserver_get_arg(arg, sizeof(arg), &p);
110 if (av_strcasecmp(arg, "allow") == 0)
111 acl.action = IP_ALLOW;
112 else if (av_strcasecmp(arg, "deny") == 0)
113 acl.action = IP_DENY;
115 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
116 filename, line_num, arg);
120 ffserver_get_arg(arg, sizeof(arg), &p);
122 if (resolve_host(&acl.first, arg) != 0) {
123 fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
124 filename, line_num, arg);
127 acl.last = acl.first;
129 ffserver_get_arg(arg, sizeof(arg), &p);
132 if (resolve_host(&acl.last, arg) != 0) {
133 fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
134 filename, line_num, arg);
140 FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
141 FFServerIPAddressACL **naclp = 0;
147 naclp = &stream->acl;
153 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
160 naclp = &(*naclp)->next;
168 /* add a codec and set the default parameters */
169 static void add_codec(FFServerStream *stream, AVCodecContext *av)
173 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
176 /* compute default parameters */
177 switch(av->codec_type) {
178 case AVMEDIA_TYPE_AUDIO:
179 if (av->bit_rate == 0)
180 av->bit_rate = 64000;
181 if (av->sample_rate == 0)
182 av->sample_rate = 22050;
183 if (av->channels == 0)
186 case AVMEDIA_TYPE_VIDEO:
187 if (av->bit_rate == 0)
188 av->bit_rate = 64000;
189 if (av->time_base.num == 0){
190 av->time_base.den = 5;
191 av->time_base.num = 1;
193 if (av->width == 0 || av->height == 0) {
197 /* Bitrate tolerance is less for streaming */
198 if (av->bit_rate_tolerance == 0)
199 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
200 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
205 if (av->max_qdiff == 0)
210 if (!av->nsse_weight)
213 av->frame_skip_cmp = FF_CMP_DCTMAX;
215 av->me_method = ME_EPZS;
216 av->rc_buffer_aggressivity = 1.0;
219 av->rc_eq = av_strdup("tex^qComp");
220 if (!av->i_quant_factor)
221 av->i_quant_factor = -0.8;
222 if (!av->b_quant_factor)
223 av->b_quant_factor = 1.25;
224 if (!av->b_quant_offset)
225 av->b_quant_offset = 1.25;
226 if (!av->rc_max_rate)
227 av->rc_max_rate = av->bit_rate * 2;
229 if (av->rc_max_rate && !av->rc_buffer_size) {
230 av->rc_buffer_size = av->rc_max_rate;
239 st = av_mallocz(sizeof(AVStream));
243 stream->streams[stream->nb_streams++] = st;
246 static enum AVCodecID opt_codec(const char *name, enum AVMediaType type)
248 AVCodec *codec = avcodec_find_encoder_by_name(name);
250 if (!codec || codec->type != type)
251 return AV_CODEC_ID_NONE;
255 static int ffserver_opt_default(const char *opt, const char *arg,
256 AVCodecContext *avctx, int type)
259 const AVOption *o = av_opt_find(avctx, opt, NULL, type, 0);
261 ret = av_opt_set(avctx, opt, arg, 0);
265 static int ffserver_opt_preset(const char *arg,
266 AVCodecContext *avctx, int type,
267 enum AVCodecID *audio_id, enum AVCodecID *video_id)
270 char filename[1000], tmp[1000], tmp2[1000], line[1000];
272 AVCodec *codec = NULL;
275 codec = avcodec_find_encoder(avctx->codec_id);
277 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
278 codec ? codec->name : NULL))) {
279 fprintf(stderr, "File for preset '%s' not found\n", arg);
280 return AVERROR(EINVAL);
284 int e= fscanf(f, "%999[^\n]\n", line) - 1;
285 if(line[0] == '#' && !e)
287 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
289 fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
290 ret = AVERROR(EINVAL);
293 if (audio_id && !strcmp(tmp, "acodec")) {
294 *audio_id = opt_codec(tmp2, AVMEDIA_TYPE_AUDIO);
295 } else if (video_id && !strcmp(tmp, "vcodec")){
296 *video_id = opt_codec(tmp2, AVMEDIA_TYPE_VIDEO);
297 } else if(!strcmp(tmp, "scodec")) {
298 /* opt_subtitle_codec(tmp2); */
299 } else if (avctx && (ret = ffserver_opt_default(tmp, tmp2, avctx, type)) < 0) {
300 fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
310 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
312 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
315 AVOutputFormat *stream_fmt;
316 char stream_format_name[64];
318 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
319 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
328 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
330 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
331 av_vlog(NULL, log_level, fmt, vl);
335 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
339 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
343 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
344 FFServerConfig *config, int line_num, const char *error_msg, ...)
348 if (!value || !value[0])
351 tmp = strtol(value, &tailp, 0);
352 if (tmp < min || tmp > max)
355 if (FFABS(tmp) > INT_MAX / FFABS(factor))
359 if (tailp[0] || errno)
367 va_start(vl, error_msg);
368 vreport_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, error_msg, vl);
371 return AVERROR(EINVAL);
374 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
375 FFServerConfig *config, int line_num, const char *error_msg, ...)
379 if (!value || !value[0])
382 tmp = strtod(value, &tailp);
383 if (tmp < min || tmp > max)
387 if (tailp[0] || errno)
395 va_start(vl, error_msg);
396 vreport_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, error_msg, vl);
399 return AVERROR(EINVAL);
402 static int ffserver_save_avoption(const char *opt, const char *arg, AVDictionary **dict,
403 int type, FFServerConfig *config, int line_num)
406 AVDictionaryEntry *e;
407 const AVOption *o = av_opt_find(config->dummy_ctx, opt, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
409 report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, "Option not found: %s\n", opt);
410 } else if ((ret = av_opt_set(config->dummy_ctx, opt, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
411 report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, "Invalid value for option %s (%s): %s\n", opt, arg, av_err2str(ret));
412 } else if ((e = av_dict_get(*dict, opt, NULL, 0))) {
413 if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
414 return av_dict_set(dict, opt, arg, AV_DICT_APPEND);
415 report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors,
416 "Redeclaring value of the option %s, previous value: %s\n", opt, e->value);
417 } else if (av_dict_set(dict, opt, arg, 0) < 0) {
418 return AVERROR(ENOMEM);
423 #define ERROR(...) report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, __VA_ARGS__)
424 #define WARNING(...) report_config_error(config->filename, line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
426 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
427 const char **p, int line_num)
431 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
432 if (!av_strcasecmp(cmd, "Port"))
433 WARNING("Port option is deprecated, use HTTPPort instead\n");
434 ffserver_get_arg(arg, sizeof(arg), p);
435 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid port: %s\n", arg);
437 WARNING("Trying to use IETF assigned system port: %d\n", val);
438 config->http_addr.sin_port = htons(val);
439 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
440 if (!av_strcasecmp(cmd, "BindAddress"))
441 WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
442 ffserver_get_arg(arg, sizeof(arg), p);
443 if (resolve_host(&config->http_addr.sin_addr, arg) != 0)
444 ERROR("Invalid host/IP address: %s\n", arg);
445 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
446 WARNING("NoDaemon option has no effect, you should remove it\n");
447 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
448 ffserver_get_arg(arg, sizeof(arg), p);
449 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid port: %s\n", arg);
450 config->rtsp_addr.sin_port = htons(val);
451 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
452 ffserver_get_arg(arg, sizeof(arg), p);
453 if (resolve_host(&config->rtsp_addr.sin_addr, arg) != 0)
454 ERROR("Invalid host/IP address: %s\n", arg);
455 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
456 ffserver_get_arg(arg, sizeof(arg), p);
457 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid MaxHTTPConnections: %s\n", arg);
458 config->nb_max_http_connections = val;
459 if (config->nb_max_connections > config->nb_max_http_connections)
460 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
461 config->nb_max_connections, config->nb_max_http_connections);
462 } else if (!av_strcasecmp(cmd, "MaxClients")) {
463 ffserver_get_arg(arg, sizeof(arg), p);
464 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid MaxClients: %s\n", arg);
465 config->nb_max_connections = val;
466 if (config->nb_max_connections > config->nb_max_http_connections)
467 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
468 config->nb_max_connections, config->nb_max_http_connections);
469 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
472 ffserver_get_arg(arg, sizeof(arg), p);
474 llval = strtoll(arg, &tailp, 10);
475 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
476 ERROR("Invalid MaxBandwidth: %s\n", arg);
478 config->max_bandwidth = llval;
479 } else if (!av_strcasecmp(cmd, "CustomLog")) {
481 ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
482 } else if (!av_strcasecmp(cmd, "LoadModule")) {
483 ERROR("Loadable modules are no longer supported\n");
485 ERROR("Incorrect keyword: '%s'\n", cmd);
489 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
490 int line_num, FFServerStream **pfeed)
492 FFServerStream *feed;
496 if (!av_strcasecmp(cmd, "<Feed")) {
499 feed = av_mallocz(sizeof(FFServerStream));
501 return AVERROR(ENOMEM);
502 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
503 q = strrchr(feed->filename, '>');
507 for (s = config->first_feed; s; s = s->next) {
508 if (!strcmp(feed->filename, s->filename))
509 ERROR("Feed '%s' already registered\n", s->filename);
512 feed->fmt = av_guess_format("ffm", NULL, NULL);
513 /* default feed file */
514 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
515 "/tmp/%s.ffm", feed->filename);
516 feed->feed_max_size = 5 * 1024 * 1024;
518 feed->feed = feed; /* self feeding :-) */
523 if (!av_strcasecmp(cmd, "Launch")) {
526 feed->child_argv = av_mallocz(64 * sizeof(char *));
527 if (!feed->child_argv)
528 return AVERROR(ENOMEM);
529 for (i = 0; i < 62; i++) {
530 ffserver_get_arg(arg, sizeof(arg), p);
534 feed->child_argv[i] = av_strdup(arg);
535 if (!feed->child_argv[i])
536 return AVERROR(ENOMEM);
539 feed->child_argv[i] =
540 av_asprintf("http://%s:%d/%s",
541 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
542 inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
544 if (!feed->child_argv[i])
545 return AVERROR(ENOMEM);
546 } else if (!av_strcasecmp(cmd, "ACL")) {
547 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename, line_num);
548 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
549 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
550 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
551 } else if (!av_strcasecmp(cmd, "Truncate")) {
552 ffserver_get_arg(arg, sizeof(arg), p);
553 /* assume Truncate is true in case no argument is specified */
557 WARNING("Truncate N syntax in configuration file is deprecated, "
558 "use Truncate alone with no arguments\n");
559 feed->truncate = strtod(arg, NULL);
561 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
565 ffserver_get_arg(arg, sizeof(arg), p);
567 fsize = strtod(p1, &p1);
568 switch(av_toupper(*p1)) {
573 fsize *= 1024 * 1024;
576 fsize *= 1024 * 1024 * 1024;
579 ERROR("Invalid file size: %s\n", arg);
582 feed->feed_max_size = (int64_t)fsize;
583 if (feed->feed_max_size < FFM_PACKET_SIZE*4)
584 ERROR("Feed max file size is too small, must be at least %d\n", FFM_PACKET_SIZE*4);
585 } else if (!av_strcasecmp(cmd, "</Feed>")) {
588 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
593 static void ffserver_apply_stream_config(AVCodecContext *enc, const AVDictionary *conf, AVDictionary **opts)
595 AVDictionaryEntry *e;
597 /* Return values from ffserver_set_*_param are ignored.
598 Values are initially parsed and checked before inserting to AVDictionary. */
601 if ((e = av_dict_get(conf, "VideoBitRateRangeMin", NULL, 0)))
602 ffserver_set_int_param(&enc->rc_min_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
603 if ((e = av_dict_get(conf, "VideoBitRateRangeMax", NULL, 0)))
604 ffserver_set_int_param(&enc->rc_max_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
605 if ((e = av_dict_get(conf, "Debug", NULL, 0)))
606 ffserver_set_int_param(&enc->debug, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
607 if ((e = av_dict_get(conf, "Strict", NULL, 0)))
608 ffserver_set_int_param(&enc->strict_std_compliance, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
609 if ((e = av_dict_get(conf, "VideoBufferSize", NULL, 0)))
610 ffserver_set_int_param(&enc->rc_buffer_size, e->value, 8*1024, INT_MIN, INT_MAX, NULL, 0, NULL);
611 if ((e = av_dict_get(conf, "VideoBitRateTolerance", NULL, 0)))
612 ffserver_set_int_param(&enc->bit_rate_tolerance, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
613 if ((e = av_dict_get(conf, "VideoBitRate", NULL, 0)))
614 ffserver_set_int_param(&enc->bit_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
615 if ((e = av_dict_get(conf, "VideoSizeWidth", NULL, 0)))
616 ffserver_set_int_param(&enc->width, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
617 if ((e = av_dict_get(conf, "VideoSizeHeight", NULL, 0)))
618 ffserver_set_int_param(&enc->height, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
619 if ((e = av_dict_get(conf, "PixelFormat", NULL, 0))) {
621 ffserver_set_int_param(&val, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
624 if ((e = av_dict_get(conf, "VideoGopSize", NULL, 0)))
625 ffserver_set_int_param(&enc->gop_size, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
626 if ((e = av_dict_get(conf, "VideoFrameRateNum", NULL, 0)))
627 ffserver_set_int_param(&enc->time_base.num, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
628 if ((e = av_dict_get(conf, "VideoFrameRateDen", NULL, 0)))
629 ffserver_set_int_param(&enc->time_base.den, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
630 if ((e = av_dict_get(conf, "VideoQDiff", NULL, 0)))
631 ffserver_set_int_param(&enc->max_qdiff, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
632 if ((e = av_dict_get(conf, "VideoQMax", NULL, 0)))
633 ffserver_set_int_param(&enc->qmax, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
634 if ((e = av_dict_get(conf, "VideoQMin", NULL, 0)))
635 ffserver_set_int_param(&enc->qmin, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
636 if ((e = av_dict_get(conf, "LumiMask", NULL, 0)))
637 ffserver_set_float_param(&enc->lumi_masking, e->value, 0, -FLT_MAX, FLT_MAX, NULL, 0, NULL);
638 if ((e = av_dict_get(conf, "DarkMask", NULL, 0)))
639 ffserver_set_float_param(&enc->dark_masking, e->value, 0, -FLT_MAX, FLT_MAX, NULL, 0, NULL);
640 if (av_dict_get(conf, "BitExact", NULL, 0))
641 enc->flags |= CODEC_FLAG_BITEXACT;
642 if (av_dict_get(conf, "DctFastint", NULL, 0))
643 enc->dct_algo = FF_DCT_FASTINT;
644 if (av_dict_get(conf, "IdctSimple", NULL, 0))
645 enc->idct_algo = FF_IDCT_SIMPLE;
646 if (av_dict_get(conf, "VideoHighQuality", NULL, 0))
647 enc->mb_decision = FF_MB_DECISION_BITS;
648 if ((e = av_dict_get(conf, "VideoTag", NULL, 0)))
649 enc->codec_tag = MKTAG(e->value[0], e->value[1], e->value[2], e->value[3]);
650 if (av_dict_get(conf, "Qscale", NULL, 0)) {
651 enc->flags |= CODEC_FLAG_QSCALE;
652 ffserver_set_int_param(&enc->global_quality, e->value, FF_QP2LAMBDA, INT_MIN, INT_MAX, NULL, 0, NULL);
654 if (av_dict_get(conf, "Video4MotionVector", NULL, 0)) {
655 enc->mb_decision = FF_MB_DECISION_BITS; //FIXME remove
656 enc->flags |= CODEC_FLAG_4MV;
659 if ((e = av_dict_get(conf, "AudioChannels", NULL, 0)))
660 ffserver_set_int_param(&enc->channels, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
661 if ((e = av_dict_get(conf, "AudioSampleRate", NULL, 0)))
662 ffserver_set_int_param(&enc->sample_rate, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
663 if ((e = av_dict_get(conf, "AudioBitRate", NULL, 0)))
664 ffserver_set_int_param(&enc->bit_rate, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
666 av_opt_set_dict2(enc, opts, AV_OPT_SEARCH_CHILDREN);
669 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
670 int line_num, FFServerStream **pstream)
672 char arg[1024], arg2[1024];
673 FFServerStream *stream;
679 if (!av_strcasecmp(cmd, "<Stream")) {
682 stream = av_mallocz(sizeof(FFServerStream));
684 return AVERROR(ENOMEM);
685 config->dummy_ctx = avcodec_alloc_context3(NULL);
686 if (!config->dummy_ctx) {
688 return AVERROR(ENOMEM);
690 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
691 q = strrchr(stream->filename, '>');
695 for (s = config->first_stream; s; s = s->next) {
696 if (!strcmp(stream->filename, s->filename))
697 ERROR("Stream '%s' already registered\n", s->filename);
700 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
702 config->audio_id = stream->fmt->audio_codec;
703 config->video_id = stream->fmt->video_codec;
705 config->audio_id = AV_CODEC_ID_NONE;
706 config->video_id = AV_CODEC_ID_NONE;
712 if (!av_strcasecmp(cmd, "Feed")) {
713 FFServerStream *sfeed;
714 ffserver_get_arg(arg, sizeof(arg), p);
715 sfeed = config->first_feed;
717 if (!strcmp(sfeed->filename, arg))
719 sfeed = sfeed->next_feed;
722 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg, stream->filename);
724 stream->feed = sfeed;
725 } else if (!av_strcasecmp(cmd, "Format")) {
726 ffserver_get_arg(arg, sizeof(arg), p);
727 if (!strcmp(arg, "status")) {
728 stream->stream_type = STREAM_TYPE_STATUS;
731 stream->stream_type = STREAM_TYPE_LIVE;
732 /* JPEG cannot be used here, so use single frame MJPEG */
733 if (!strcmp(arg, "jpeg"))
734 strcpy(arg, "mjpeg");
735 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
737 ERROR("Unknown Format: %s\n", arg);
740 config->audio_id = stream->fmt->audio_codec;
741 config->video_id = stream->fmt->video_codec;
743 } else if (!av_strcasecmp(cmd, "InputFormat")) {
744 ffserver_get_arg(arg, sizeof(arg), p);
745 stream->ifmt = av_find_input_format(arg);
747 ERROR("Unknown input format: %s\n", arg);
748 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
749 if (stream->stream_type == STREAM_TYPE_STATUS)
750 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
752 ERROR("FaviconURL only permitted for status streams\n");
753 } else if (!av_strcasecmp(cmd, "Author") ||
754 !av_strcasecmp(cmd, "Comment") ||
755 !av_strcasecmp(cmd, "Copyright") ||
756 !av_strcasecmp(cmd, "Title")) {
759 ffserver_get_arg(arg, sizeof(arg), p);
760 for (i = 0; i < strlen(cmd); i++)
761 key[i] = av_tolower(cmd[i]);
763 WARNING("'%s' option in configuration file is deprecated, "
764 "use 'Metadata %s VALUE' instead\n", cmd, key);
765 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
767 } else if (!av_strcasecmp(cmd, "Metadata")) {
768 ffserver_get_arg(arg, sizeof(arg), p);
769 ffserver_get_arg(arg2, sizeof(arg2), p);
770 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
772 } else if (!av_strcasecmp(cmd, "Preroll")) {
773 ffserver_get_arg(arg, sizeof(arg), p);
774 stream->prebuffer = atof(arg) * 1000;
775 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
776 stream->send_on_key = 1;
777 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
778 ffserver_get_arg(arg, sizeof(arg), p);
779 config->audio_id = opt_codec(arg, AVMEDIA_TYPE_AUDIO);
780 if (config->audio_id == AV_CODEC_ID_NONE)
781 ERROR("Unknown AudioCodec: %s\n", arg);
782 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
783 ffserver_get_arg(arg, sizeof(arg), p);
784 config->video_id = opt_codec(arg, AVMEDIA_TYPE_VIDEO);
785 if (config->video_id == AV_CODEC_ID_NONE)
786 ERROR("Unknown VideoCodec: %s\n", arg);
787 } else if (!av_strcasecmp(cmd, "MaxTime")) {
788 ffserver_get_arg(arg, sizeof(arg), p);
789 stream->max_time = atof(arg) * 1000;
790 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
792 ffserver_get_arg(arg, sizeof(arg), p);
793 ffserver_set_float_param(&f, arg, 1000, 0, FLT_MAX, config, line_num, "Invalid %s: %s\n", cmd, arg);
794 if (av_dict_set_int(&config->audio_conf, cmd, lrintf(f), 0) < 0)
796 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
797 ffserver_get_arg(arg, sizeof(arg), p);
798 ffserver_set_int_param(NULL, arg, 0, 1, 8, config, line_num, "Invalid %s: %s, valid range is 1-8.", cmd, arg);
799 if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
801 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
802 ffserver_get_arg(arg, sizeof(arg), p);
803 ffserver_set_int_param(NULL, arg, 0, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
804 if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
806 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
807 int minrate, maxrate;
808 ffserver_get_arg(arg, sizeof(arg), p);
809 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
810 if (av_dict_set_int(&config->video_conf, "VideoBitRateRangeMin", minrate, 0) < 0 ||
811 av_dict_set_int(&config->video_conf, "VideoBitRateRangeMax", maxrate, 0) < 0)
814 ERROR("Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n", arg);
815 } else if (!av_strcasecmp(cmd, "Debug")) {
816 ffserver_get_arg(arg, sizeof(arg), p);
817 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
818 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
820 } else if (!av_strcasecmp(cmd, "Strict")) {
821 ffserver_get_arg(arg, sizeof(arg), p);
822 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
823 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
825 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
826 ffserver_get_arg(arg, sizeof(arg), p);
827 ffserver_set_int_param(NULL, arg, 8*1024, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
828 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
830 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
831 ffserver_get_arg(arg, sizeof(arg), p);
832 ffserver_set_int_param(NULL, arg, 1000, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
833 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
835 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
836 ffserver_get_arg(arg, sizeof(arg), p);
837 ffserver_set_int_param(NULL, arg, 1000, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
838 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
840 } else if (!av_strcasecmp(cmd, "VideoSize")) {
842 ffserver_get_arg(arg, sizeof(arg), p);
843 ret = av_parse_video_size(&w, &h, arg);
845 ERROR("Invalid video size '%s'\n", arg);
846 else if ((w % 16) || (h % 16))
847 ERROR("Image size must be a multiple of 16\n");
848 if (av_dict_set_int(&config->video_conf, "VideoSizeWidth", w, 0) < 0 ||
849 av_dict_set_int(&config->video_conf, "VideoSizeHeight", h, 0) < 0)
851 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
852 AVRational frame_rate;
853 ffserver_get_arg(arg, sizeof(arg), p);
854 if (av_parse_video_rate(&frame_rate, arg) < 0) {
855 ERROR("Incorrect frame rate: %s\n", arg);
857 if (av_dict_set_int(&config->video_conf, "VideoFrameRateNum", frame_rate.num, 0) < 0 ||
858 av_dict_set_int(&config->video_conf, "VideoFrameRateDen", frame_rate.den, 0) < 0)
861 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
862 enum AVPixelFormat pix_fmt;
863 ffserver_get_arg(arg, sizeof(arg), p);
864 pix_fmt = av_get_pix_fmt(arg);
865 if (pix_fmt == AV_PIX_FMT_NONE)
866 ERROR("Unknown pixel format: %s\n", arg);
867 if (av_dict_set_int(&config->video_conf, cmd, pix_fmt, 0) < 0)
869 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
870 ffserver_get_arg(arg, sizeof(arg), p);
871 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
872 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
874 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
875 if (av_dict_set(&config->video_conf, cmd, "1", 0) < 0)
877 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
878 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
880 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
881 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
883 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
884 !av_strcasecmp(cmd, "AVOptionAudio")) {
886 ffserver_get_arg(arg, sizeof(arg), p);
887 ffserver_get_arg(arg2, sizeof(arg2), p);
888 if (!av_strcasecmp(cmd, "AVOptionVideo"))
889 ret = ffserver_save_avoption(arg, arg2, &config->video_opts, AV_OPT_FLAG_VIDEO_PARAM ,config, line_num);
891 ret = ffserver_save_avoption(arg, arg2, &config->audio_opts, AV_OPT_FLAG_AUDIO_PARAM ,config, line_num);
894 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
895 !av_strcasecmp(cmd, "AVPresetAudio")) {
896 char **preset = NULL;
897 ffserver_get_arg(arg, sizeof(arg), p);
898 if (!av_strcasecmp(cmd, "AVPresetVideo")) {
899 preset = &config->video_preset;
900 ffserver_opt_preset(arg, NULL, 0, NULL, &config->video_id);
902 preset = &config->audio_preset;
903 ffserver_opt_preset(arg, NULL, 0, &config->audio_id, NULL);
905 *preset = av_strdup(arg);
907 return AVERROR(ENOMEM);
908 } else if (!av_strcasecmp(cmd, "VideoTag")) {
909 ffserver_get_arg(arg, sizeof(arg), p);
910 if (strlen(arg) == 4) {
911 if (av_dict_set(&config->video_conf, "VideoTag", "arg", 0) < 0)
914 } else if (!av_strcasecmp(cmd, "BitExact")) {
915 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
917 } else if (!av_strcasecmp(cmd, "DctFastint")) {
918 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
920 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
921 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
923 } else if (!av_strcasecmp(cmd, "Qscale")) {
924 ffserver_get_arg(arg, sizeof(arg), p);
925 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
927 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
928 ffserver_get_arg(arg, sizeof(arg), p);
929 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
930 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
932 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
933 ffserver_get_arg(arg, sizeof(arg), p);
934 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
935 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
937 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
938 ffserver_get_arg(arg, sizeof(arg), p);
939 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
940 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
942 } else if (!av_strcasecmp(cmd, "LumiMask")) {
943 ffserver_get_arg(arg, sizeof(arg), p);
944 ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
945 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
947 } else if (!av_strcasecmp(cmd, "DarkMask")) {
948 ffserver_get_arg(arg, sizeof(arg), p);
949 ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
950 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
952 } else if (!av_strcasecmp(cmd, "NoVideo")) {
953 config->video_id = AV_CODEC_ID_NONE;
954 } else if (!av_strcasecmp(cmd, "NoAudio")) {
955 config->audio_id = AV_CODEC_ID_NONE;
956 } else if (!av_strcasecmp(cmd, "ACL")) {
957 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename, line_num);
958 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
959 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
960 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
961 ffserver_get_arg(arg, sizeof(arg), p);
962 av_freep(&stream->rtsp_option);
963 stream->rtsp_option = av_strdup(arg);
964 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
965 ffserver_get_arg(arg, sizeof(arg), p);
966 if (resolve_host(&stream->multicast_ip, arg) != 0)
967 ERROR("Invalid host/IP address: %s\n", arg);
968 stream->is_multicast = 1;
969 stream->loop = 1; /* default is looping */
970 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
971 ffserver_get_arg(arg, sizeof(arg), p);
972 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num, "Invalid MulticastPort: %s\n", arg);
973 stream->multicast_port = val;
974 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
975 ffserver_get_arg(arg, sizeof(arg), p);
976 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid MulticastTTL: %s\n", arg);
977 stream->multicast_ttl = val;
978 } else if (!av_strcasecmp(cmd, "NoLoop")) {
980 } else if (!av_strcasecmp(cmd, "</Stream>")) {
981 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
982 if (config->audio_id != AV_CODEC_ID_NONE) {
983 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->audio_id));
984 if (config->audio_preset &&
985 ffserver_opt_preset(arg, audio_enc, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
987 ERROR("Could not apply preset '%s'\n", arg);
988 ffserver_apply_stream_config(audio_enc, config->audio_conf, &config->audio_opts);
989 add_codec(stream, audio_enc);
991 if (config->video_id != AV_CODEC_ID_NONE) {
992 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->video_id));
993 if (config->video_preset &&
994 ffserver_opt_preset(arg, video_enc, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
996 ERROR("Could not apply preset '%s'\n", arg);
997 ffserver_apply_stream_config(video_enc, config->video_conf, &config->video_opts);
998 add_codec(stream, video_enc);
1001 av_dict_free(&config->video_opts);
1002 av_dict_free(&config->video_conf);
1003 av_dict_free(&config->audio_opts);
1004 av_dict_free(&config->audio_conf);
1005 av_freep(&config->video_preset);
1006 av_freep(&config->audio_preset);
1007 avcodec_free_context(&config->dummy_ctx);
1009 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1010 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
1012 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1016 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1017 av_dict_free(&config->video_opts);
1018 av_dict_free(&config->video_conf);
1019 av_dict_free(&config->audio_opts);
1020 av_dict_free(&config->audio_conf);
1021 av_freep(&config->video_preset);
1022 av_freep(&config->audio_preset);
1023 avcodec_free_context(&config->dummy_ctx);
1024 return AVERROR(ENOMEM);
1027 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1028 int line_num, FFServerStream **predirect)
1030 FFServerStream *redirect;
1031 av_assert0(predirect);
1032 redirect = *predirect;
1034 if (!av_strcasecmp(cmd, "<Redirect")) {
1036 redirect = av_mallocz(sizeof(FFServerStream));
1038 return AVERROR(ENOMEM);
1040 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1041 q = strrchr(redirect->filename, '>');
1044 redirect->stream_type = STREAM_TYPE_REDIRECT;
1045 *predirect = redirect;
1048 av_assert0(redirect);
1049 if (!av_strcasecmp(cmd, "URL")) {
1050 ffserver_get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), p);
1051 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1052 if (!redirect->feed_filename[0])
1053 ERROR("No URL found for <Redirect>\n");
1056 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1061 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1068 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1069 FFServerStream **last_feed, *feed = NULL;
1074 f = fopen(filename, "r");
1076 ret = AVERROR(errno);
1077 av_log(NULL, AV_LOG_ERROR, "Could not open the configuration file '%s'\n", filename);
1081 config->first_stream = NULL;
1082 last_stream = &config->first_stream;
1083 config->first_feed = NULL;
1084 last_feed = &config->first_feed;
1085 config->errors = config->warnings = 0;
1088 if (fgets(line, sizeof(line), f) == NULL)
1092 while (av_isspace(*p))
1094 if (*p == '\0' || *p == '#')
1097 ffserver_get_arg(cmd, sizeof(cmd), &p);
1099 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1100 int opening = !av_strcasecmp(cmd, "<Feed");
1101 if (opening && (stream || feed || redirect)) {
1102 ERROR("Already in a tag\n");
1104 if ((ret = ffserver_parse_config_feed(config, cmd, &p, line_num, &feed)) < 0)
1107 /* add in stream list */
1108 *last_stream = feed;
1109 last_stream = &feed->next;
1110 /* add in feed list */
1112 last_feed = &feed->next_feed;
1115 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1116 int opening = !av_strcasecmp(cmd, "<Stream");
1117 if (opening && (stream || feed || redirect)) {
1118 ERROR("Already in a tag\n");
1120 if ((ret = ffserver_parse_config_stream(config, cmd, &p, line_num, &stream)) < 0)
1123 /* add in stream list */
1124 *last_stream = stream;
1125 last_stream = &stream->next;
1128 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1129 int opening = !av_strcasecmp(cmd, "<Redirect");
1130 if (opening && (stream || feed || redirect))
1131 ERROR("Already in a tag\n");
1133 if ((ret = ffserver_parse_config_redirect(config, cmd, &p, line_num, &redirect)) < 0)
1136 /* add in stream list */
1137 *last_stream = redirect;
1138 last_stream = &redirect->next;
1142 ffserver_parse_config_global(config, cmd, &p, line_num);
1150 return AVERROR(EINVAL);