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,
39 int log_level, int *errors, const char *fmt,
41 static void report_config_error(const char *filename, int line_num,
42 int log_level, int *errors, const char *fmt,
45 #define ERROR(...) report_config_error(config->filename, config->line_num,\
46 AV_LOG_ERROR, &config->errors, __VA_ARGS__)
47 #define WARNING(...) report_config_error(config->filename, config->line_num,\
48 AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
50 /* FIXME: make ffserver work with IPv6 */
51 /* resolve host with also IP address parsing */
52 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
55 if (!ff_inet_aton(hostname, sin_addr)) {
57 struct addrinfo *ai, *cur;
58 struct addrinfo hints = { 0 };
59 hints.ai_family = AF_INET;
60 if (getaddrinfo(hostname, NULL, &hints, &ai))
62 /* getaddrinfo returns a linked list of addrinfo structs.
63 * Even if we set ai_family = AF_INET above, make sure
64 * that the returned one actually is of the correct type. */
65 for (cur = ai; cur; cur = cur->ai_next) {
66 if (cur->ai_family == AF_INET) {
67 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
76 hp = gethostbyname(hostname);
79 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
85 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
94 while (av_isspace(*p)) p++;
96 if (*p == '\"' || *p == '\'')
100 if (quote && *p == quote || !quote && av_isspace(*p))
102 if ((q - buf) < buf_size - 1)
108 if (quote && *p == quote)
113 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
114 FFServerIPAddressACL *ext_acl,
115 const char *p, const char *filename, int line_num)
118 FFServerIPAddressACL acl;
121 ffserver_get_arg(arg, sizeof(arg), &p);
122 if (av_strcasecmp(arg, "allow") == 0)
123 acl.action = IP_ALLOW;
124 else if (av_strcasecmp(arg, "deny") == 0)
125 acl.action = IP_DENY;
127 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
128 filename, line_num, arg);
132 ffserver_get_arg(arg, sizeof(arg), &p);
134 if (resolve_host(&acl.first, arg)) {
135 fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
136 filename, line_num, arg);
139 acl.last = acl.first;
141 ffserver_get_arg(arg, sizeof(arg), &p);
144 if (resolve_host(&acl.last, arg)) {
146 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
147 filename, line_num, arg);
153 FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
154 FFServerIPAddressACL **naclp = 0;
160 naclp = &stream->acl;
166 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
173 naclp = &(*naclp)->next;
181 /* add a codec and set the default parameters */
182 static void add_codec(FFServerStream *stream, AVCodecContext *av,
183 FFServerConfig *config)
186 AVDictionary **opts, *recommended = NULL;
189 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
192 opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
193 &config->audio_opts : &config->video_opts;
194 av_dict_copy(&recommended, *opts, 0);
195 av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
196 av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
197 if (av_dict_count(*opts))
198 av_log(NULL, AV_LOG_WARNING,
199 "Something is wrong, %d options are not set!\n", av_dict_count(*opts));
201 if (config->stream_use_defaults) {
203 /* compute default parameters */
204 switch(av->codec_type) {
205 case AVMEDIA_TYPE_AUDIO:
206 if (!av_dict_get(recommended, "ab", NULL, 0)) {
207 av->bit_rate = 64000;
208 av_dict_set_int(&recommended, "ab", av->bit_rate, 0);
209 WARNING("Setting default value for audio bit rate = %d. "
210 "Use NoDefaults to disable it.\n",
213 if (!av_dict_get(recommended, "ar", NULL, 0)) {
214 av->sample_rate = 22050;
215 av_dict_set_int(&recommended, "ar", av->sample_rate, 0);
216 WARNING("Setting default value for audio sample rate = %d. "
217 "Use NoDefaults to disable it.\n",
220 if (!av_dict_get(recommended, "ac", NULL, 0)) {
222 av_dict_set_int(&recommended, "ac", av->channels, 0);
223 WARNING("Setting default value for audio channel count = %d. "
224 "Use NoDefaults to disable it.\n",
228 case AVMEDIA_TYPE_VIDEO:
229 if (!av_dict_get(recommended, "b", NULL, 0)) {
230 av->bit_rate = 64000;
231 av_dict_set_int(&recommended, "b", av->bit_rate, 0);
232 WARNING("Setting default value for video bit rate = %d. "
233 "Use NoDefaults to disable it.\n",
236 if (!av_dict_get(recommended, "time_base", NULL, 0)){
237 av->time_base.den = 5;
238 av->time_base.num = 1;
239 av_dict_set(&recommended, "time_base", "1/5", 0);
240 WARNING("Setting default value for video frame rate = %d. "
241 "Use NoDefaults to disable it.\n",
244 if (!av_dict_get(recommended, "video_size", NULL, 0)) {
247 av_dict_set(&recommended, "video_size", "160x128", 0);
248 WARNING("Setting default value for video size = %dx%d. "
249 "Use NoDefaults to disable it.\n",
250 av->width, av->height);
252 /* Bitrate tolerance is less for streaming */
253 if (!av_dict_get(recommended, "bt", NULL, 0)) {
254 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
255 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
256 av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0);
257 WARNING("Setting default value for video bit rate tolerance = %d. "
258 "Use NoDefaults to disable it.\n",
259 av->bit_rate_tolerance);
262 if (!av_dict_get(recommended, "rc_eq", NULL, 0)) {
263 av->rc_eq = av_strdup("tex^qComp");
264 av_dict_set(&recommended, "rc_eq", "tex^qComp", 0);
265 WARNING("Setting default value for video rate control equation = %s. "
266 "Use NoDefaults to disable it.\n",
269 if (!av_dict_get(recommended, "maxrate", NULL, 0)) {
270 av->rc_max_rate = av->bit_rate * 2;
271 av_dict_set_int(&recommended, "maxrate", av->rc_max_rate, 0);
272 WARNING("Setting default value for video max rate = %d. "
273 "Use NoDefaults to disable it.\n",
277 if (av->rc_max_rate && !av_dict_get(recommended, "bufsize", NULL, 0)) {
278 av->rc_buffer_size = av->rc_max_rate;
279 av_dict_set_int(&recommended, "bufsize", av->rc_buffer_size, 0);
280 WARNING("Setting default value for video buffer size = %d. "
281 "Use NoDefaults to disable it.\n",
289 switch(av->codec_type) {
290 case AVMEDIA_TYPE_AUDIO:
291 if (av->bit_rate == 0)
292 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
293 &config->errors, "audio bit rate is not set\n");
294 if (av->sample_rate == 0)
295 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
296 &config->errors, "audio sample rate is not set\n");
298 case AVMEDIA_TYPE_VIDEO:
299 if (av->width == 0 || av->height == 0)
300 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
301 &config->errors, "video size is not set\n");
308 st = av_mallocz(sizeof(AVStream));
311 av_dict_get_string(recommended, &enc_config, '=', ',');
312 av_dict_free(&recommended);
313 av_stream_set_recommended_encoder_configuration(st, enc_config);
315 stream->streams[stream->nb_streams++] = st;
318 static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name, FFServerConfig *config)
321 AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
322 if (!codec || codec->type != ctx->codec_type) {
323 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
324 &config->errors, "Invalid codec name: %s\n", codec_name);
327 if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
328 if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
332 if (ctx->codec_id != codec->id)
333 report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors,
334 "Inconsistent configuration: trying to set %s codec option, but %s codec is used previously\n",
335 codec_name, avcodec_get_name(ctx->codec_id));
339 static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
342 char filename[1000], tmp[1000], tmp2[1000], line[1000];
344 AVCodecContext *avctx;
345 const AVCodec *codec;
348 case AV_OPT_FLAG_AUDIO_PARAM:
349 avctx = config->dummy_actx;
351 case AV_OPT_FLAG_VIDEO_PARAM:
352 avctx = config->dummy_vctx;
357 codec = avcodec_find_encoder(avctx->codec_id);
359 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
360 codec ? codec->name : NULL))) {
361 av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
362 return AVERROR(EINVAL);
366 int e= fscanf(f, "%999[^\n]\n", line) - 1;
367 if(line[0] == '#' && !e)
369 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
371 av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename, line);
372 ret = AVERROR(EINVAL);
375 if ((!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO) ||
376 !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
378 if (ffserver_set_codec(avctx, tmp2, config) < 0)
380 } else if (!strcmp(tmp, "scodec")) {
381 av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
382 ret = AVERROR(EINVAL);
384 } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
393 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
395 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
398 AVOutputFormat *stream_fmt;
399 char stream_format_name[64];
401 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
403 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
412 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
414 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
415 av_vlog(NULL, log_level, fmt, vl);
420 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
424 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
428 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
429 FFServerConfig *config, const char *error_msg, ...)
433 if (!value || !value[0])
436 tmp = strtol(value, &tailp, 0);
437 if (tmp < min || tmp > max)
440 if (FFABS(tmp) > INT_MAX / FFABS(factor))
444 if (tailp[0] || errno)
452 va_start(vl, error_msg);
453 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
454 &config->errors, error_msg, vl);
457 return AVERROR(EINVAL);
460 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
461 FFServerConfig *config, const char *error_msg, ...)
465 if (!value || !value[0])
468 tmp = strtod(value, &tailp);
469 if (tmp < min || tmp > max)
473 if (tailp[0] || errno)
481 va_start(vl, error_msg);
482 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
483 &config->errors, error_msg, vl);
486 return AVERROR(EINVAL);
489 static int ffserver_save_avoption(const char *opt, const char *arg, int type, FFServerConfig *config)
491 static int hinted = 0;
493 AVDictionaryEntry *e;
494 const AVOption *o = NULL;
495 const char *option = NULL;
496 const char *codec_name = NULL;
500 enum AVCodecID guessed_codec_id;
503 case AV_OPT_FLAG_VIDEO_PARAM:
504 ctx = config->dummy_vctx;
505 dict = &config->video_opts;
506 guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
507 config->guessed_video_codec_id : AV_CODEC_ID_H264;
509 case AV_OPT_FLAG_AUDIO_PARAM:
510 ctx = config->dummy_actx;
511 dict = &config->audio_opts;
512 guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
513 config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
519 if (strchr(opt, ':')) {
520 //explicit private option
521 snprintf(buff, sizeof(buff), "%s", opt);
523 if(!(option = strchr(buff, ':'))){
524 report_config_error(config->filename, config->line_num,
525 AV_LOG_ERROR, &config->errors,
526 "Syntax error. Unmatched ':'\n");
530 buff[option - buff] = '\0';
532 if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
534 if (!ctx->codec || !ctx->priv_data)
540 o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
541 if (!o && (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
542 !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
543 o = av_opt_find(ctx, option, NULL, 0, 0);
545 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
546 &config->errors, "Option not found: %s\n", opt);
547 if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
549 report_config_error(config->filename, config->line_num, AV_LOG_ERROR, NULL,
550 "If '%s' is a codec private option, then prefix it with codec name, "
551 "for example '%s:%s %s' or define codec earlier.\n",
552 opt, avcodec_get_name(guessed_codec_id) ,opt, arg);
554 } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
555 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
556 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
557 arg, av_err2str(ret));
558 } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
559 if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
560 return av_dict_set(dict, option, arg, AV_DICT_APPEND);
561 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
563 "Redeclaring value of the option %s, previous value: %s\n",
565 } else if (av_dict_set(dict, option, arg, 0) < 0) {
566 return AVERROR(ENOMEM);
571 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
572 int type, FFServerConfig *config)
575 snprintf(buf, sizeof(buf), "%"PRId64, arg);
576 return ffserver_save_avoption(opt, buf, type, config);
579 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
584 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
585 if (!av_strcasecmp(cmd, "Port"))
586 WARNING("Port option is deprecated, use HTTPPort instead\n");
587 ffserver_get_arg(arg, sizeof(arg), p);
588 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
589 "Invalid port: %s\n", arg);
591 WARNING("Trying to use IETF assigned system port: %d\n", val);
592 config->http_addr.sin_port = htons(val);
593 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
594 if (!av_strcasecmp(cmd, "BindAddress"))
595 WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
596 ffserver_get_arg(arg, sizeof(arg), p);
597 if (resolve_host(&config->http_addr.sin_addr, arg))
598 ERROR("Invalid host/IP address: %s\n", arg);
599 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
600 WARNING("NoDaemon option has no effect, you should remove it\n");
601 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
602 ffserver_get_arg(arg, sizeof(arg), p);
603 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
604 "Invalid port: %s\n", arg);
605 config->rtsp_addr.sin_port = htons(val);
606 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
607 ffserver_get_arg(arg, sizeof(arg), p);
608 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
609 ERROR("Invalid host/IP address: %s\n", arg);
610 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
611 ffserver_get_arg(arg, sizeof(arg), p);
612 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
613 "Invalid MaxHTTPConnections: %s\n", arg);
614 config->nb_max_http_connections = val;
615 if (config->nb_max_connections > config->nb_max_http_connections)
616 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
617 config->nb_max_connections, config->nb_max_http_connections);
618 } else if (!av_strcasecmp(cmd, "MaxClients")) {
619 ffserver_get_arg(arg, sizeof(arg), p);
620 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
621 "Invalid MaxClients: %s\n", arg);
622 config->nb_max_connections = val;
623 if (config->nb_max_connections > config->nb_max_http_connections)
624 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
625 config->nb_max_connections, config->nb_max_http_connections);
626 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
629 ffserver_get_arg(arg, sizeof(arg), p);
631 llval = strtoll(arg, &tailp, 10);
632 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
633 ERROR("Invalid MaxBandwidth: %s\n", arg);
635 config->max_bandwidth = llval;
636 } else if (!av_strcasecmp(cmd, "CustomLog")) {
638 ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
639 } else if (!av_strcasecmp(cmd, "LoadModule")) {
640 ERROR("Loadable modules are no longer supported\n");
641 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
642 config->use_defaults = 0;
643 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
644 config->use_defaults = 1;
646 ERROR("Incorrect keyword: '%s'\n", cmd);
650 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
651 FFServerStream **pfeed)
653 FFServerStream *feed;
657 if (!av_strcasecmp(cmd, "<Feed")) {
660 feed = av_mallocz(sizeof(FFServerStream));
662 return AVERROR(ENOMEM);
663 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
664 q = strrchr(feed->filename, '>');
668 for (s = config->first_feed; s; s = s->next) {
669 if (!strcmp(feed->filename, s->filename))
670 ERROR("Feed '%s' already registered\n", s->filename);
673 feed->fmt = av_guess_format("ffm", NULL, NULL);
674 /* default feed file */
675 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
676 "/tmp/%s.ffm", feed->filename);
677 feed->feed_max_size = 5 * 1024 * 1024;
679 feed->feed = feed; /* self feeding :-) */
684 if (!av_strcasecmp(cmd, "Launch")) {
687 feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
688 if (!feed->child_argv)
689 return AVERROR(ENOMEM);
690 for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
691 ffserver_get_arg(arg, sizeof(arg), p);
695 feed->child_argv[i] = av_strdup(arg);
696 if (!feed->child_argv[i])
697 return AVERROR(ENOMEM);
700 feed->child_argv[i] =
701 av_asprintf("http://%s:%d/%s",
702 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
703 inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
705 if (!feed->child_argv[i])
706 return AVERROR(ENOMEM);
707 } else if (!av_strcasecmp(cmd, "ACL")) {
708 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
710 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
711 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
712 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
713 } else if (!av_strcasecmp(cmd, "Truncate")) {
714 ffserver_get_arg(arg, sizeof(arg), p);
715 /* assume Truncate is true in case no argument is specified */
719 WARNING("Truncate N syntax in configuration file is deprecated, "
720 "use Truncate alone with no arguments\n");
721 feed->truncate = strtod(arg, NULL);
723 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
727 ffserver_get_arg(arg, sizeof(arg), p);
729 fsize = strtod(p1, &p1);
730 switch(av_toupper(*p1)) {
735 fsize *= 1024 * 1024;
738 fsize *= 1024 * 1024 * 1024;
741 ERROR("Invalid file size: %s\n", arg);
744 feed->feed_max_size = (int64_t)fsize;
745 if (feed->feed_max_size < FFM_PACKET_SIZE*4)
746 ERROR("Feed max file size is too small, must be at least %d\n",
748 } else if (!av_strcasecmp(cmd, "</Feed>")) {
751 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
756 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
757 FFServerStream **pstream)
759 char arg[1024], arg2[1024];
760 FFServerStream *stream;
766 if (!av_strcasecmp(cmd, "<Stream")) {
769 stream = av_mallocz(sizeof(FFServerStream));
771 return AVERROR(ENOMEM);
772 config->dummy_actx = avcodec_alloc_context3(NULL);
773 config->dummy_vctx = avcodec_alloc_context3(NULL);
774 if (!config->dummy_vctx || !config->dummy_actx) {
776 avcodec_free_context(&config->dummy_vctx);
777 avcodec_free_context(&config->dummy_actx);
778 return AVERROR(ENOMEM);
780 config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
781 config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
782 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
783 q = strrchr(stream->filename, '>');
787 for (s = config->first_stream; s; s = s->next) {
788 if (!strcmp(stream->filename, s->filename))
789 ERROR("Stream '%s' already registered\n", s->filename);
792 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
794 config->guessed_audio_codec_id = stream->fmt->audio_codec;
795 config->guessed_video_codec_id = stream->fmt->video_codec;
797 config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
798 config->guessed_video_codec_id = AV_CODEC_ID_NONE;
800 config->stream_use_defaults = config->use_defaults;
805 if (!av_strcasecmp(cmd, "Feed")) {
806 FFServerStream *sfeed;
807 ffserver_get_arg(arg, sizeof(arg), p);
808 sfeed = config->first_feed;
810 if (!strcmp(sfeed->filename, arg))
812 sfeed = sfeed->next_feed;
815 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
818 stream->feed = sfeed;
819 } else if (!av_strcasecmp(cmd, "Format")) {
820 ffserver_get_arg(arg, sizeof(arg), p);
821 if (!strcmp(arg, "status")) {
822 stream->stream_type = STREAM_TYPE_STATUS;
825 stream->stream_type = STREAM_TYPE_LIVE;
826 /* JPEG cannot be used here, so use single frame MJPEG */
827 if (!strcmp(arg, "jpeg"))
828 strcpy(arg, "mjpeg");
829 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
831 ERROR("Unknown Format: %s\n", arg);
834 config->guessed_audio_codec_id = stream->fmt->audio_codec;
835 config->guessed_video_codec_id = stream->fmt->video_codec;
837 } else if (!av_strcasecmp(cmd, "InputFormat")) {
838 ffserver_get_arg(arg, sizeof(arg), p);
839 stream->ifmt = av_find_input_format(arg);
841 ERROR("Unknown input format: %s\n", arg);
842 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
843 if (stream->stream_type == STREAM_TYPE_STATUS)
844 ffserver_get_arg(stream->feed_filename,
845 sizeof(stream->feed_filename), p);
847 ERROR("FaviconURL only permitted for status streams\n");
848 } else if (!av_strcasecmp(cmd, "Author") ||
849 !av_strcasecmp(cmd, "Comment") ||
850 !av_strcasecmp(cmd, "Copyright") ||
851 !av_strcasecmp(cmd, "Title")) {
854 ffserver_get_arg(arg, sizeof(arg), p);
855 for (i = 0; i < strlen(cmd); i++)
856 key[i] = av_tolower(cmd[i]);
858 WARNING("'%s' option in configuration file is deprecated, "
859 "use 'Metadata %s VALUE' instead\n", cmd, key);
860 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
862 } else if (!av_strcasecmp(cmd, "Metadata")) {
863 ffserver_get_arg(arg, sizeof(arg), p);
864 ffserver_get_arg(arg2, sizeof(arg2), p);
865 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
867 } else if (!av_strcasecmp(cmd, "Preroll")) {
868 ffserver_get_arg(arg, sizeof(arg), p);
869 stream->prebuffer = atof(arg) * 1000;
870 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
871 stream->send_on_key = 1;
872 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
873 ffserver_get_arg(arg, sizeof(arg), p);
874 ffserver_set_codec(config->dummy_actx, arg, config);
875 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
876 ffserver_get_arg(arg, sizeof(arg), p);
877 ffserver_set_codec(config->dummy_vctx, arg, config);
878 } else if (!av_strcasecmp(cmd, "MaxTime")) {
879 ffserver_get_arg(arg, sizeof(arg), p);
880 stream->max_time = atof(arg) * 1000;
881 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
883 ffserver_get_arg(arg, sizeof(arg), p);
884 ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
885 "Invalid %s: %s\n", cmd, arg);
886 if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f), AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
888 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
889 ffserver_get_arg(arg, sizeof(arg), p);
890 if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
892 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
893 ffserver_get_arg(arg, sizeof(arg), p);
894 if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
896 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
897 int minrate, maxrate;
899 ffserver_get_arg(arg, sizeof(arg), p);
900 dash = strchr(arg, '-');
904 if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0 &&
905 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0) {
906 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
907 ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
911 ERROR("Incorrect format for VideoBitRateRange -- should be "
912 "<min>-<max>: %s\n", arg);
913 } else if (!av_strcasecmp(cmd, "Debug")) {
914 ffserver_get_arg(arg, sizeof(arg), p);
915 if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
916 ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
918 } else if (!av_strcasecmp(cmd, "Strict")) {
919 ffserver_get_arg(arg, sizeof(arg), p);
920 if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
921 ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
923 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
924 ffserver_get_arg(arg, sizeof(arg), p);
925 ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
926 "Invalid %s: %s", cmd, arg);
927 if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
929 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
930 ffserver_get_arg(arg, sizeof(arg), p);
931 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
932 "Invalid %s: %s", cmd, arg);
933 if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
935 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
936 ffserver_get_arg(arg, sizeof(arg), p);
937 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
938 "Invalid %s: %s", cmd, arg);
939 if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
941 } else if (!av_strcasecmp(cmd, "VideoSize")) {
943 ffserver_get_arg(arg, sizeof(arg), p);
944 ret = av_parse_video_size(&w, &h, arg);
946 ERROR("Invalid video size '%s'\n", arg);
949 WARNING("Image size is not a multiple of 2\n");
950 if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
953 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
954 ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
955 arg[0] = '1'; arg[1] = '/';
956 if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
958 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
959 enum AVPixelFormat pix_fmt;
960 ffserver_get_arg(arg, sizeof(arg), p);
961 pix_fmt = av_get_pix_fmt(arg);
962 if (pix_fmt == AV_PIX_FMT_NONE)
963 ERROR("Unknown pixel format: %s\n", arg);
964 else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
966 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
967 ffserver_get_arg(arg, sizeof(arg), p);
968 if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
970 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
971 if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
973 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
974 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
976 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
977 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
978 ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
980 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
981 !av_strcasecmp(cmd, "AVOptionAudio")) {
983 ffserver_get_arg(arg, sizeof(arg), p);
984 ffserver_get_arg(arg2, sizeof(arg2), p);
985 if (!av_strcasecmp(cmd, "AVOptionVideo"))
986 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config);
988 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config);
991 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
992 !av_strcasecmp(cmd, "AVPresetAudio")) {
993 ffserver_get_arg(arg, sizeof(arg), p);
994 if (!av_strcasecmp(cmd, "AVPresetVideo"))
995 ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
997 ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
998 } else if (!av_strcasecmp(cmd, "VideoTag")) {
999 ffserver_get_arg(arg, sizeof(arg), p);
1000 if (strlen(arg) == 4 &&
1001 ffserver_save_avoption_int("codec_tag", MKTAG(arg[0], arg[1], arg[2], arg[3]),
1002 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1004 } else if (!av_strcasecmp(cmd, "BitExact")) {
1005 if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1007 } else if (!av_strcasecmp(cmd, "DctFastint")) {
1008 if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1010 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1011 if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1013 } else if (!av_strcasecmp(cmd, "Qscale")) {
1014 ffserver_get_arg(arg, sizeof(arg), p);
1015 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1016 "Invalid Qscale: %s\n", arg);
1017 if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1018 ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1019 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1021 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1022 ffserver_get_arg(arg, sizeof(arg), p);
1023 if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1025 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1026 ffserver_get_arg(arg, sizeof(arg), p);
1027 if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1029 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1030 ffserver_get_arg(arg, sizeof(arg), p);
1031 if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1033 } else if (!av_strcasecmp(cmd, "LumiMask")) {
1034 ffserver_get_arg(arg, sizeof(arg), p);
1035 if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1037 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1038 ffserver_get_arg(arg, sizeof(arg), p);
1039 if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1041 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1042 config->no_video = 1;
1043 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1044 config->no_audio = 1;
1045 } else if (!av_strcasecmp(cmd, "ACL")) {
1046 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1048 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1049 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1050 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1051 ffserver_get_arg(arg, sizeof(arg), p);
1052 av_freep(&stream->rtsp_option);
1053 stream->rtsp_option = av_strdup(arg);
1054 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1055 ffserver_get_arg(arg, sizeof(arg), p);
1056 if (resolve_host(&stream->multicast_ip, arg))
1057 ERROR("Invalid host/IP address: %s\n", arg);
1058 stream->is_multicast = 1;
1059 stream->loop = 1; /* default is looping */
1060 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1061 ffserver_get_arg(arg, sizeof(arg), p);
1062 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1063 "Invalid MulticastPort: %s\n", arg);
1064 stream->multicast_port = val;
1065 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1066 ffserver_get_arg(arg, sizeof(arg), p);
1067 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1068 "Invalid MulticastTTL: %s\n", arg);
1069 stream->multicast_ttl = val;
1070 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1072 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1073 config->stream_use_defaults &= 1;
1074 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1075 if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1076 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1077 if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1078 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1079 add_codec(stream, audio_enc, config);
1081 if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1082 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1083 if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1084 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1085 add_codec(stream, video_enc, config);
1088 av_dict_free(&config->video_opts);
1089 av_dict_free(&config->audio_opts);
1090 avcodec_free_context(&config->dummy_vctx);
1091 avcodec_free_context(&config->dummy_actx);
1093 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1094 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1096 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1097 if (config->stream_use_defaults > 1)
1098 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1099 config->stream_use_defaults = 3;
1100 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1101 if (config->stream_use_defaults > 1)
1102 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1103 config->stream_use_defaults = 2;
1105 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1109 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1110 av_dict_free(&config->video_opts);
1111 av_dict_free(&config->audio_opts);
1112 avcodec_free_context(&config->dummy_vctx);
1113 avcodec_free_context(&config->dummy_actx);
1114 return AVERROR(ENOMEM);
1117 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1118 FFServerStream **predirect)
1120 FFServerStream *redirect;
1121 av_assert0(predirect);
1122 redirect = *predirect;
1124 if (!av_strcasecmp(cmd, "<Redirect")) {
1126 redirect = av_mallocz(sizeof(FFServerStream));
1128 return AVERROR(ENOMEM);
1130 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1131 q = strrchr(redirect->filename, '>');
1134 redirect->stream_type = STREAM_TYPE_REDIRECT;
1135 *predirect = redirect;
1138 av_assert0(redirect);
1139 if (!av_strcasecmp(cmd, "URL")) {
1140 ffserver_get_arg(redirect->feed_filename,
1141 sizeof(redirect->feed_filename), p);
1142 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1143 if (!redirect->feed_filename[0])
1144 ERROR("No URL found for <Redirect>\n");
1147 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1152 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1158 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1159 FFServerStream **last_feed, *feed = NULL;
1164 f = fopen(filename, "r");
1166 ret = AVERROR(errno);
1167 av_log(NULL, AV_LOG_ERROR,
1168 "Could not open the configuration file '%s'\n", filename);
1172 config->first_stream = NULL;
1173 config->first_feed = NULL;
1174 config->errors = config->warnings = 0;
1176 last_stream = &config->first_stream;
1177 last_feed = &config->first_feed;
1179 config->line_num = 0;
1180 while (fgets(line, sizeof(line), f) != NULL) {
1183 while (av_isspace(*p))
1185 if (*p == '\0' || *p == '#')
1188 ffserver_get_arg(cmd, sizeof(cmd), &p);
1190 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1191 int opening = !av_strcasecmp(cmd, "<Feed");
1192 if (opening && (stream || feed || redirect)) {
1193 ERROR("Already in a tag\n");
1195 ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
1199 /* add in stream & feed list */
1200 *last_stream = feed;
1202 last_stream = &feed->next;
1203 last_feed = &feed->next_feed;
1206 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1207 int opening = !av_strcasecmp(cmd, "<Stream");
1208 if (opening && (stream || feed || redirect)) {
1209 ERROR("Already in a tag\n");
1211 ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
1215 /* add in stream list */
1216 *last_stream = stream;
1217 last_stream = &stream->next;
1220 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1221 int opening = !av_strcasecmp(cmd, "<Redirect");
1222 if (opening && (stream || feed || redirect))
1223 ERROR("Already in a tag\n");
1225 ret = ffserver_parse_config_redirect(config, cmd, &p,
1230 /* add in stream list */
1231 *last_stream = redirect;
1232 last_stream = &redirect->next;
1236 ffserver_parse_config_global(config, cmd, &p);
1239 if (stream || feed || redirect)
1240 ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>"));
1246 return AVERROR(EINVAL);
1254 void ffserver_free_child_args(void *argsp)
1260 args = *(char ***)argsp;
1263 for (i = 0; i < MAX_CHILD_ARGS; i++)