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 #define MAX_CHILD_ARGS 64
36 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
37 FFServerConfig *config);
38 static void vreport_config_error(const char *filename, int line_num, int log_level,
39 int *errors, const char *fmt, va_list vl);
40 static void report_config_error(const char *filename, int line_num, int log_level,
41 int *errors, const char *fmt, ...);
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)
48 if (!ff_inet_aton(hostname, sin_addr)) {
50 struct addrinfo *ai, *cur;
51 struct addrinfo hints = { 0 };
52 hints.ai_family = AF_INET;
53 if (getaddrinfo(hostname, NULL, &hints, &ai))
55 /* getaddrinfo returns a linked list of addrinfo structs.
56 * Even if we set ai_family = AF_INET above, make sure
57 * that the returned one actually is of the correct type. */
58 for (cur = ai; cur; cur = cur->ai_next) {
59 if (cur->ai_family == AF_INET) {
60 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
69 hp = gethostbyname(hostname);
72 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
78 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
85 while (av_isspace(*p)) p++;
88 if (*p == '\"' || *p == '\'')
100 if ((q - buf) < buf_size - 1)
105 if (quote && *p == quote)
110 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
111 FFServerIPAddressACL *ext_acl,
112 const char *p, const char *filename, int line_num)
115 FFServerIPAddressACL acl;
118 ffserver_get_arg(arg, sizeof(arg), &p);
119 if (av_strcasecmp(arg, "allow") == 0)
120 acl.action = IP_ALLOW;
121 else if (av_strcasecmp(arg, "deny") == 0)
122 acl.action = IP_DENY;
124 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
125 filename, line_num, arg);
129 ffserver_get_arg(arg, sizeof(arg), &p);
131 if (resolve_host(&acl.first, arg)) {
132 fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
133 filename, line_num, arg);
136 acl.last = acl.first;
138 ffserver_get_arg(arg, sizeof(arg), &p);
141 if (resolve_host(&acl.last, arg)) {
143 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
144 filename, line_num, arg);
150 FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
151 FFServerIPAddressACL **naclp = 0;
157 naclp = &stream->acl;
163 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
170 naclp = &(*naclp)->next;
178 /* add a codec and set the default parameters */
179 static void add_codec(FFServerStream *stream, AVCodecContext *av,
180 FFServerConfig *config)
183 AVDictionary **opts, *recommended = NULL;
186 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
189 opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
190 &config->audio_opts : &config->video_opts;
191 av_dict_copy(&recommended, *opts, 0);
192 av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
193 av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
194 if (av_dict_count(*opts))
195 av_log(NULL, AV_LOG_WARNING,
196 "Something is wrong, %d options are not set!\n", av_dict_count(*opts));
198 if (config->stream_use_defaults) {
200 /* compute default parameters */
201 switch(av->codec_type) {
202 case AVMEDIA_TYPE_AUDIO:
203 if (av->bit_rate == 0) {
204 av->bit_rate = 64000;
205 av_dict_set_int(&recommended, "ab", av->bit_rate, 0);
207 if (av->sample_rate == 0) {
208 av->sample_rate = 22050;
209 av_dict_set_int(&recommended, "ar", av->sample_rate, 0);
211 if (av->channels == 0) {
213 av_dict_set_int(&recommended, "ac", av->channels, 0);
216 case AVMEDIA_TYPE_VIDEO:
217 if (av->bit_rate == 0) {
218 av->bit_rate = 64000;
219 av_dict_set_int(&recommended, "b", av->bit_rate, 0);
221 if (av->time_base.num == 0){
222 av->time_base.den = 5;
223 av->time_base.num = 1;
224 av_dict_set(&recommended, "time_base", "1/5", 0);
226 if (av->width == 0 || av->height == 0) {
229 av_dict_set(&recommended, "video_size", "160x128", 0);
231 /* Bitrate tolerance is less for streaming */
232 if (av->bit_rate_tolerance == 0) {
233 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
234 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
235 av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0);
239 av_dict_set_int(&recommended, "qmin", av->qmin, 0);
243 av_dict_set_int(&recommended, "qmax", av->qmax, 0);
245 if (av->max_qdiff == 0) {
247 av_dict_set_int(&recommended, "qdiff", av->max_qdiff, 0);
249 /*FIXME: 0.5 is a default for these two, it is a dead code */
251 av_dict_set(&recommended, "qcomp", "0.5", 0);
253 av_dict_set(&recommended, "qblur", "0.5", 0);
255 if (!av->nsse_weight) {
257 av_dict_set_int(&recommended, "nssew", av->nsse_weight, 0);
260 av->frame_skip_cmp = FF_CMP_DCTMAX;
261 av_dict_set_int(&recommended, "skipcmp", FF_CMP_DCTMAX, 0);
262 if (!av->me_method) {
263 av->me_method = ME_EPZS;
264 av_dict_set_int(&recommended, "me_method", ME_EPZS, 0);
267 /* FIXME: rc_buffer_aggressivity and rc_eq are deprecated */
268 av->rc_buffer_aggressivity = 1.0;
269 av_dict_set(&recommended, "rc_buf_aggressivity", "1.0", 0);
272 av->rc_eq = av_strdup("tex^qComp");
273 av_dict_set(&recommended, "rc_eq", "tex^qComp", 0);
275 if (!av->i_quant_factor) {
276 av->i_quant_factor = -0.8;
277 av_dict_set(&recommended, "i_qfactor", "-0.8", 0);
279 if (!av->b_quant_factor) {
280 av->b_quant_factor = 1.25;
281 av_dict_set(&recommended, "b_qfactor", "1.25", 0);
283 if (!av->b_quant_offset) {
284 av->b_quant_offset = 1.25;
285 av_dict_set(&recommended, "b_qoffset", "1.25", 0);
287 if (!av->rc_max_rate) {
288 av->rc_max_rate = av->bit_rate * 2;
289 av_dict_set_int(&recommended, "maxrate", av->rc_max_rate, 0);
292 if (av->rc_max_rate && !av->rc_buffer_size) {
293 av->rc_buffer_size = av->rc_max_rate;
294 av_dict_set_int(&recommended, "bufsize", av->rc_buffer_size, 0);
301 switch(av->codec_type) {
302 case AVMEDIA_TYPE_AUDIO:
303 if (av->bit_rate == 0)
304 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
305 &config->errors, "audio bit rate is not set\n");
306 if (av->sample_rate == 0)
307 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
308 &config->errors, "audio sample rate is not set\n");
310 case AVMEDIA_TYPE_VIDEO:
311 if (av->width == 0 || av->height == 0)
312 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
313 &config->errors, "video size is not set\n");
320 st = av_mallocz(sizeof(AVStream));
323 av_dict_get_string(recommended, &enc_config, '=', ',');
324 av_dict_free(&recommended);
325 av_stream_set_recommended_encoder_configuration(st, enc_config);
327 stream->streams[stream->nb_streams++] = st;
330 static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name, FFServerConfig *config)
333 AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
334 if (!codec || codec->type != ctx->codec_type) {
335 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
336 &config->errors, "Invalid codec name: %s\n", codec_name);
339 if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
340 if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
344 if (ctx->codec_id != codec->id)
345 report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors,
346 "Inconsistent configuration: trying to set %s codec option, but %s codec is used previously\n",
347 codec_name, avcodec_get_name(ctx->codec_id));
351 static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
354 char filename[1000], tmp[1000], tmp2[1000], line[1000];
356 AVCodecContext *avctx;
357 const AVCodec *codec;
360 case AV_OPT_FLAG_AUDIO_PARAM:
361 avctx = config->dummy_actx;
363 case AV_OPT_FLAG_VIDEO_PARAM:
364 avctx = config->dummy_vctx;
369 codec = avcodec_find_encoder(avctx->codec_id);
371 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
372 codec ? codec->name : NULL))) {
373 av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
374 return AVERROR(EINVAL);
378 int e= fscanf(f, "%999[^\n]\n", line) - 1;
379 if(line[0] == '#' && !e)
381 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
383 av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename, line);
384 ret = AVERROR(EINVAL);
387 if ((!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO) ||
388 !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
390 if (ffserver_set_codec(avctx, tmp2, config) < 0)
392 } else if (!strcmp(tmp, "scodec")) {
393 av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
394 ret = AVERROR(EINVAL);
396 } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
405 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
407 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
410 AVOutputFormat *stream_fmt;
411 char stream_format_name[64];
413 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
415 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
424 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
426 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
427 av_vlog(NULL, log_level, fmt, vl);
432 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
436 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
440 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
441 FFServerConfig *config, const char *error_msg, ...)
445 if (!value || !value[0])
448 tmp = strtol(value, &tailp, 0);
449 if (tmp < min || tmp > max)
452 if (FFABS(tmp) > INT_MAX / FFABS(factor))
456 if (tailp[0] || errno)
464 va_start(vl, error_msg);
465 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
466 &config->errors, error_msg, vl);
469 return AVERROR(EINVAL);
472 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
473 FFServerConfig *config, const char *error_msg, ...)
477 if (!value || !value[0])
480 tmp = strtod(value, &tailp);
481 if (tmp < min || tmp > max)
485 if (tailp[0] || errno)
493 va_start(vl, error_msg);
494 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
495 &config->errors, error_msg, vl);
498 return AVERROR(EINVAL);
501 static int ffserver_save_avoption(const char *opt, const char *arg, int type, FFServerConfig *config)
503 static int hinted = 0;
505 AVDictionaryEntry *e;
506 const AVOption *o = NULL;
507 const char *option = NULL;
508 const char *codec_name = NULL;
512 enum AVCodecID guessed_codec_id;
515 case AV_OPT_FLAG_VIDEO_PARAM:
516 ctx = config->dummy_vctx;
517 dict = &config->video_opts;
518 guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
519 config->guessed_video_codec_id : AV_CODEC_ID_H264;
521 case AV_OPT_FLAG_AUDIO_PARAM:
522 ctx = config->dummy_actx;
523 dict = &config->audio_opts;
524 guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
525 config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
531 if (strchr(opt, ':')) {
532 //explicit private option
533 snprintf(buff, sizeof(buff), "%s", opt);
535 option = strchr(buff, ':');
536 buff[option - buff] = '\0';
538 if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
540 if (!ctx->codec || !ctx->priv_data)
546 o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
547 if (!o && (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
548 !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
549 o = av_opt_find(ctx, option, NULL, 0, 0);
551 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
552 &config->errors, "Option not found: %s\n", opt);
553 if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
555 report_config_error(config->filename, config->line_num, AV_LOG_ERROR, NULL,
556 "If '%s' is a codec private option, then prefix it with codec name, "
557 "for example '%s:%s %s' or define codec earlier.\n",
558 opt, avcodec_get_name(guessed_codec_id) ,opt, arg);
560 } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
561 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
562 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
563 arg, av_err2str(ret));
564 } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
565 if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
566 return av_dict_set(dict, option, arg, AV_DICT_APPEND);
567 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
569 "Redeclaring value of the option %s, previous value: %s\n",
571 } else if (av_dict_set(dict, option, arg, 0) < 0) {
572 return AVERROR(ENOMEM);
577 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
578 int type, FFServerConfig *config)
581 snprintf(buf, sizeof(buf), "%"PRId64, arg);
582 return ffserver_save_avoption(opt, buf, type, config);
585 #define ERROR(...) report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors, __VA_ARGS__)
586 #define WARNING(...) report_config_error(config->filename, config->line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
588 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
593 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
594 if (!av_strcasecmp(cmd, "Port"))
595 WARNING("Port option is deprecated, use HTTPPort instead\n");
596 ffserver_get_arg(arg, sizeof(arg), p);
597 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
598 "Invalid port: %s\n", arg);
600 WARNING("Trying to use IETF assigned system port: %d\n", val);
601 config->http_addr.sin_port = htons(val);
602 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
603 if (!av_strcasecmp(cmd, "BindAddress"))
604 WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
605 ffserver_get_arg(arg, sizeof(arg), p);
606 if (resolve_host(&config->http_addr.sin_addr, arg))
607 ERROR("Invalid host/IP address: %s\n", arg);
608 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
609 WARNING("NoDaemon option has no effect, you should remove it\n");
610 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
611 ffserver_get_arg(arg, sizeof(arg), p);
612 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
613 "Invalid port: %s\n", arg);
614 config->rtsp_addr.sin_port = htons(val);
615 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
616 ffserver_get_arg(arg, sizeof(arg), p);
617 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
618 ERROR("Invalid host/IP address: %s\n", arg);
619 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
620 ffserver_get_arg(arg, sizeof(arg), p);
621 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
622 "Invalid MaxHTTPConnections: %s\n", arg);
623 config->nb_max_http_connections = val;
624 if (config->nb_max_connections > config->nb_max_http_connections)
625 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
626 config->nb_max_connections, config->nb_max_http_connections);
627 } else if (!av_strcasecmp(cmd, "MaxClients")) {
628 ffserver_get_arg(arg, sizeof(arg), p);
629 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
630 "Invalid MaxClients: %s\n", arg);
631 config->nb_max_connections = val;
632 if (config->nb_max_connections > config->nb_max_http_connections)
633 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
634 config->nb_max_connections, config->nb_max_http_connections);
635 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
638 ffserver_get_arg(arg, sizeof(arg), p);
640 llval = strtoll(arg, &tailp, 10);
641 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
642 ERROR("Invalid MaxBandwidth: %s\n", arg);
644 config->max_bandwidth = llval;
645 } else if (!av_strcasecmp(cmd, "CustomLog")) {
647 ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
648 } else if (!av_strcasecmp(cmd, "LoadModule")) {
649 ERROR("Loadable modules are no longer supported\n");
650 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
651 config->use_defaults = 0;
652 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
653 config->use_defaults = 1;
655 ERROR("Incorrect keyword: '%s'\n", cmd);
659 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
660 FFServerStream **pfeed)
662 FFServerStream *feed;
666 if (!av_strcasecmp(cmd, "<Feed")) {
669 feed = av_mallocz(sizeof(FFServerStream));
671 return AVERROR(ENOMEM);
672 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
673 q = strrchr(feed->filename, '>');
677 for (s = config->first_feed; s; s = s->next) {
678 if (!strcmp(feed->filename, s->filename))
679 ERROR("Feed '%s' already registered\n", s->filename);
682 feed->fmt = av_guess_format("ffm", NULL, NULL);
683 /* default feed file */
684 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
685 "/tmp/%s.ffm", feed->filename);
686 feed->feed_max_size = 5 * 1024 * 1024;
688 feed->feed = feed; /* self feeding :-) */
693 if (!av_strcasecmp(cmd, "Launch")) {
696 feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
697 if (!feed->child_argv)
698 return AVERROR(ENOMEM);
699 for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
700 ffserver_get_arg(arg, sizeof(arg), p);
704 feed->child_argv[i] = av_strdup(arg);
705 if (!feed->child_argv[i])
706 return AVERROR(ENOMEM);
709 feed->child_argv[i] =
710 av_asprintf("http://%s:%d/%s",
711 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
712 inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
714 if (!feed->child_argv[i])
715 return AVERROR(ENOMEM);
716 } else if (!av_strcasecmp(cmd, "ACL")) {
717 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
719 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
720 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
721 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
722 } else if (!av_strcasecmp(cmd, "Truncate")) {
723 ffserver_get_arg(arg, sizeof(arg), p);
724 /* assume Truncate is true in case no argument is specified */
728 WARNING("Truncate N syntax in configuration file is deprecated, "
729 "use Truncate alone with no arguments\n");
730 feed->truncate = strtod(arg, NULL);
732 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
736 ffserver_get_arg(arg, sizeof(arg), p);
738 fsize = strtod(p1, &p1);
739 switch(av_toupper(*p1)) {
744 fsize *= 1024 * 1024;
747 fsize *= 1024 * 1024 * 1024;
750 ERROR("Invalid file size: %s\n", arg);
753 feed->feed_max_size = (int64_t)fsize;
754 if (feed->feed_max_size < FFM_PACKET_SIZE*4)
755 ERROR("Feed max file size is too small, must be at least %d\n",
757 } else if (!av_strcasecmp(cmd, "</Feed>")) {
760 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
765 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
766 FFServerStream **pstream)
768 char arg[1024], arg2[1024];
769 FFServerStream *stream;
775 if (!av_strcasecmp(cmd, "<Stream")) {
778 stream = av_mallocz(sizeof(FFServerStream));
780 return AVERROR(ENOMEM);
781 config->dummy_actx = avcodec_alloc_context3(NULL);
782 config->dummy_vctx = avcodec_alloc_context3(NULL);
783 if (!config->dummy_vctx || !config->dummy_actx) {
785 avcodec_free_context(&config->dummy_vctx);
786 avcodec_free_context(&config->dummy_actx);
787 return AVERROR(ENOMEM);
789 config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
790 config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
791 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
792 q = strrchr(stream->filename, '>');
796 for (s = config->first_stream; s; s = s->next) {
797 if (!strcmp(stream->filename, s->filename))
798 ERROR("Stream '%s' already registered\n", s->filename);
801 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
803 config->guessed_audio_codec_id = stream->fmt->audio_codec;
804 config->guessed_video_codec_id = stream->fmt->video_codec;
806 config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
807 config->guessed_video_codec_id = AV_CODEC_ID_NONE;
809 config->stream_use_defaults = config->use_defaults;
814 if (!av_strcasecmp(cmd, "Feed")) {
815 FFServerStream *sfeed;
816 ffserver_get_arg(arg, sizeof(arg), p);
817 sfeed = config->first_feed;
819 if (!strcmp(sfeed->filename, arg))
821 sfeed = sfeed->next_feed;
824 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
827 stream->feed = sfeed;
828 } else if (!av_strcasecmp(cmd, "Format")) {
829 ffserver_get_arg(arg, sizeof(arg), p);
830 if (!strcmp(arg, "status")) {
831 stream->stream_type = STREAM_TYPE_STATUS;
834 stream->stream_type = STREAM_TYPE_LIVE;
835 /* JPEG cannot be used here, so use single frame MJPEG */
836 if (!strcmp(arg, "jpeg"))
837 strcpy(arg, "mjpeg");
838 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
840 ERROR("Unknown Format: %s\n", arg);
843 config->guessed_audio_codec_id = stream->fmt->audio_codec;
844 config->guessed_video_codec_id = stream->fmt->video_codec;
846 } else if (!av_strcasecmp(cmd, "InputFormat")) {
847 ffserver_get_arg(arg, sizeof(arg), p);
848 stream->ifmt = av_find_input_format(arg);
850 ERROR("Unknown input format: %s\n", arg);
851 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
852 if (stream->stream_type == STREAM_TYPE_STATUS)
853 ffserver_get_arg(stream->feed_filename,
854 sizeof(stream->feed_filename), p);
856 ERROR("FaviconURL only permitted for status streams\n");
857 } else if (!av_strcasecmp(cmd, "Author") ||
858 !av_strcasecmp(cmd, "Comment") ||
859 !av_strcasecmp(cmd, "Copyright") ||
860 !av_strcasecmp(cmd, "Title")) {
863 ffserver_get_arg(arg, sizeof(arg), p);
864 for (i = 0; i < strlen(cmd); i++)
865 key[i] = av_tolower(cmd[i]);
867 WARNING("'%s' option in configuration file is deprecated, "
868 "use 'Metadata %s VALUE' instead\n", cmd, key);
869 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
871 } else if (!av_strcasecmp(cmd, "Metadata")) {
872 ffserver_get_arg(arg, sizeof(arg), p);
873 ffserver_get_arg(arg2, sizeof(arg2), p);
874 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
876 } else if (!av_strcasecmp(cmd, "Preroll")) {
877 ffserver_get_arg(arg, sizeof(arg), p);
878 stream->prebuffer = atof(arg) * 1000;
879 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
880 stream->send_on_key = 1;
881 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
882 ffserver_get_arg(arg, sizeof(arg), p);
883 ffserver_set_codec(config->dummy_actx, arg, config);
884 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
885 ffserver_get_arg(arg, sizeof(arg), p);
886 ffserver_set_codec(config->dummy_vctx, arg, config);
887 } else if (!av_strcasecmp(cmd, "MaxTime")) {
888 ffserver_get_arg(arg, sizeof(arg), p);
889 stream->max_time = atof(arg) * 1000;
890 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
892 ffserver_get_arg(arg, sizeof(arg), p);
893 ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
894 "Invalid %s: %s\n", cmd, arg);
895 if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f), AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
897 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
898 ffserver_get_arg(arg, sizeof(arg), p);
899 if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
901 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
902 ffserver_get_arg(arg, sizeof(arg), p);
903 if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
905 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
906 int minrate, maxrate;
908 ffserver_get_arg(arg, sizeof(arg), p);
909 dash = strchr(arg, '-');
913 if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0 &&
914 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0) {
915 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
916 ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
920 ERROR("Incorrect format for VideoBitRateRange -- should be "
921 "<min>-<max>: %s\n", arg);
922 } else if (!av_strcasecmp(cmd, "Debug")) {
923 ffserver_get_arg(arg, sizeof(arg), p);
924 if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
925 ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
927 } else if (!av_strcasecmp(cmd, "Strict")) {
928 ffserver_get_arg(arg, sizeof(arg), p);
929 if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
930 ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
932 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
933 ffserver_get_arg(arg, sizeof(arg), p);
934 ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
935 "Invalid %s: %s", cmd, arg);
936 if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
938 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
939 ffserver_get_arg(arg, sizeof(arg), p);
940 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
941 "Invalid %s: %s", cmd, arg);
942 if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
944 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
945 ffserver_get_arg(arg, sizeof(arg), p);
946 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
947 "Invalid %s: %s", cmd, arg);
948 if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
950 } else if (!av_strcasecmp(cmd, "VideoSize")) {
952 ffserver_get_arg(arg, sizeof(arg), p);
953 ret = av_parse_video_size(&w, &h, arg);
955 ERROR("Invalid video size '%s'\n", arg);
958 WARNING("Image size is not a multiple of 2\n");
959 if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
962 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
963 ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
964 arg[0] = '1'; arg[1] = '/';
965 if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
967 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
968 enum AVPixelFormat pix_fmt;
969 ffserver_get_arg(arg, sizeof(arg), p);
970 pix_fmt = av_get_pix_fmt(arg);
971 if (pix_fmt == AV_PIX_FMT_NONE)
972 ERROR("Unknown pixel format: %s\n", arg);
973 else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
975 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
976 ffserver_get_arg(arg, sizeof(arg), p);
977 if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
979 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
980 if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
982 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
983 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
985 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
986 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
987 ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
989 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
990 !av_strcasecmp(cmd, "AVOptionAudio")) {
992 ffserver_get_arg(arg, sizeof(arg), p);
993 ffserver_get_arg(arg2, sizeof(arg2), p);
994 if (!av_strcasecmp(cmd, "AVOptionVideo"))
995 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config);
997 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config);
1000 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
1001 !av_strcasecmp(cmd, "AVPresetAudio")) {
1002 ffserver_get_arg(arg, sizeof(arg), p);
1003 if (!av_strcasecmp(cmd, "AVPresetVideo"))
1004 ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
1006 ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
1007 } else if (!av_strcasecmp(cmd, "VideoTag")) {
1008 ffserver_get_arg(arg, sizeof(arg), p);
1009 if (strlen(arg) == 4 &&
1010 ffserver_save_avoption_int("codec_tag", MKTAG(arg[0], arg[1], arg[2], arg[3]),
1011 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1013 } else if (!av_strcasecmp(cmd, "BitExact")) {
1014 if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1016 } else if (!av_strcasecmp(cmd, "DctFastint")) {
1017 if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1019 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1020 if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1022 } else if (!av_strcasecmp(cmd, "Qscale")) {
1023 ffserver_get_arg(arg, sizeof(arg), p);
1024 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1025 "Invalid Qscale: %s\n", arg);
1026 if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1027 ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1028 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1030 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1031 ffserver_get_arg(arg, sizeof(arg), p);
1032 if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1034 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1035 ffserver_get_arg(arg, sizeof(arg), p);
1036 if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1038 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1039 ffserver_get_arg(arg, sizeof(arg), p);
1040 if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1042 } else if (!av_strcasecmp(cmd, "LumiMask")) {
1043 ffserver_get_arg(arg, sizeof(arg), p);
1044 if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1046 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1047 ffserver_get_arg(arg, sizeof(arg), p);
1048 if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1050 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1051 config->no_video = 1;
1052 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1053 config->no_audio = 1;
1054 } else if (!av_strcasecmp(cmd, "ACL")) {
1055 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1057 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1058 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1059 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1060 ffserver_get_arg(arg, sizeof(arg), p);
1061 av_freep(&stream->rtsp_option);
1062 stream->rtsp_option = av_strdup(arg);
1063 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1064 ffserver_get_arg(arg, sizeof(arg), p);
1065 if (resolve_host(&stream->multicast_ip, arg))
1066 ERROR("Invalid host/IP address: %s\n", arg);
1067 stream->is_multicast = 1;
1068 stream->loop = 1; /* default is looping */
1069 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1070 ffserver_get_arg(arg, sizeof(arg), p);
1071 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1072 "Invalid MulticastPort: %s\n", arg);
1073 stream->multicast_port = val;
1074 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1075 ffserver_get_arg(arg, sizeof(arg), p);
1076 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1077 "Invalid MulticastTTL: %s\n", arg);
1078 stream->multicast_ttl = val;
1079 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1081 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1082 config->stream_use_defaults &= 1;
1083 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1084 if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1085 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1086 if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1087 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1088 add_codec(stream, audio_enc, config);
1090 if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1091 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1092 if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1093 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1094 add_codec(stream, video_enc, config);
1097 av_dict_free(&config->video_opts);
1098 av_dict_free(&config->audio_opts);
1099 avcodec_free_context(&config->dummy_vctx);
1100 avcodec_free_context(&config->dummy_actx);
1102 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1103 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1105 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1106 if (config->stream_use_defaults > 1)
1107 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1108 config->stream_use_defaults = 3;
1109 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1110 if (config->stream_use_defaults > 1)
1111 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1112 config->stream_use_defaults = 2;
1114 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1118 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1119 av_dict_free(&config->video_opts);
1120 av_dict_free(&config->audio_opts);
1121 avcodec_free_context(&config->dummy_vctx);
1122 avcodec_free_context(&config->dummy_actx);
1123 return AVERROR(ENOMEM);
1126 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1127 FFServerStream **predirect)
1129 FFServerStream *redirect;
1130 av_assert0(predirect);
1131 redirect = *predirect;
1133 if (!av_strcasecmp(cmd, "<Redirect")) {
1135 redirect = av_mallocz(sizeof(FFServerStream));
1137 return AVERROR(ENOMEM);
1139 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1140 q = strrchr(redirect->filename, '>');
1143 redirect->stream_type = STREAM_TYPE_REDIRECT;
1144 *predirect = redirect;
1147 av_assert0(redirect);
1148 if (!av_strcasecmp(cmd, "URL")) {
1149 ffserver_get_arg(redirect->feed_filename,
1150 sizeof(redirect->feed_filename), p);
1151 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1152 if (!redirect->feed_filename[0])
1153 ERROR("No URL found for <Redirect>\n");
1156 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1161 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1167 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1168 FFServerStream **last_feed, *feed = NULL;
1173 config->line_num = 0;
1174 f = fopen(filename, "r");
1176 ret = AVERROR(errno);
1177 av_log(NULL, AV_LOG_ERROR,
1178 "Could not open the configuration file '%s'\n", filename);
1182 config->first_stream = NULL;
1183 last_stream = &config->first_stream;
1184 config->first_feed = NULL;
1185 last_feed = &config->first_feed;
1186 config->errors = config->warnings = 0;
1189 if (fgets(line, sizeof(line), f) == NULL)
1193 while (av_isspace(*p))
1195 if (*p == '\0' || *p == '#')
1198 ffserver_get_arg(cmd, sizeof(cmd), &p);
1200 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1201 int opening = !av_strcasecmp(cmd, "<Feed");
1202 if (opening && (stream || feed || redirect)) {
1203 ERROR("Already in a tag\n");
1205 if ((ret = ffserver_parse_config_feed(config, cmd, &p, &feed)) < 0)
1208 /* add in stream list */
1209 *last_stream = feed;
1210 last_stream = &feed->next;
1211 /* add in feed list */
1213 last_feed = &feed->next_feed;
1216 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1217 int opening = !av_strcasecmp(cmd, "<Stream");
1218 if (opening && (stream || feed || redirect)) {
1219 ERROR("Already in a tag\n");
1221 if ((ret = ffserver_parse_config_stream(config, cmd, &p, &stream)) < 0)
1224 /* add in stream list */
1225 *last_stream = stream;
1226 last_stream = &stream->next;
1229 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1230 int opening = !av_strcasecmp(cmd, "<Redirect");
1231 if (opening && (stream || feed || redirect))
1232 ERROR("Already in a tag\n");
1234 if ((ret = ffserver_parse_config_redirect(config, cmd, &p, &redirect)) < 0)
1237 /* add in stream list */
1238 *last_stream = redirect;
1239 last_stream = &redirect->next;
1243 ffserver_parse_config_global(config, cmd, &p);
1246 if (stream || feed || redirect)
1247 ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>"));
1253 return AVERROR(EINVAL);
1261 void ffserver_free_child_args(void *argsp)
1267 args = *(char ***)argsp;
1270 for (i = 0; i < MAX_CHILD_ARGS; i++)