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 option = strchr(buff, ':');
524 buff[option - buff] = '\0';
526 if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
528 if (!ctx->codec || !ctx->priv_data)
534 o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
535 if (!o && (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
536 !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
537 o = av_opt_find(ctx, option, NULL, 0, 0);
539 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
540 &config->errors, "Option not found: %s\n", opt);
541 if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
543 report_config_error(config->filename, config->line_num, AV_LOG_ERROR, NULL,
544 "If '%s' is a codec private option, then prefix it with codec name, "
545 "for example '%s:%s %s' or define codec earlier.\n",
546 opt, avcodec_get_name(guessed_codec_id) ,opt, arg);
548 } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
549 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
550 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
551 arg, av_err2str(ret));
552 } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
553 if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
554 return av_dict_set(dict, option, arg, AV_DICT_APPEND);
555 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
557 "Redeclaring value of the option %s, previous value: %s\n",
559 } else if (av_dict_set(dict, option, arg, 0) < 0) {
560 return AVERROR(ENOMEM);
565 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
566 int type, FFServerConfig *config)
569 snprintf(buf, sizeof(buf), "%"PRId64, arg);
570 return ffserver_save_avoption(opt, buf, type, config);
573 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
578 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
579 if (!av_strcasecmp(cmd, "Port"))
580 WARNING("Port option is deprecated, use HTTPPort instead\n");
581 ffserver_get_arg(arg, sizeof(arg), p);
582 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
583 "Invalid port: %s\n", arg);
585 WARNING("Trying to use IETF assigned system port: %d\n", val);
586 config->http_addr.sin_port = htons(val);
587 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
588 if (!av_strcasecmp(cmd, "BindAddress"))
589 WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
590 ffserver_get_arg(arg, sizeof(arg), p);
591 if (resolve_host(&config->http_addr.sin_addr, arg))
592 ERROR("Invalid host/IP address: %s\n", arg);
593 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
594 WARNING("NoDaemon option has no effect, you should remove it\n");
595 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
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);
599 config->rtsp_addr.sin_port = htons(val);
600 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
601 ffserver_get_arg(arg, sizeof(arg), p);
602 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
603 ERROR("Invalid host/IP address: %s\n", arg);
604 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
605 ffserver_get_arg(arg, sizeof(arg), p);
606 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
607 "Invalid MaxHTTPConnections: %s\n", arg);
608 config->nb_max_http_connections = val;
609 if (config->nb_max_connections > config->nb_max_http_connections)
610 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
611 config->nb_max_connections, config->nb_max_http_connections);
612 } else if (!av_strcasecmp(cmd, "MaxClients")) {
613 ffserver_get_arg(arg, sizeof(arg), p);
614 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
615 "Invalid MaxClients: %s\n", arg);
616 config->nb_max_connections = val;
617 if (config->nb_max_connections > config->nb_max_http_connections)
618 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
619 config->nb_max_connections, config->nb_max_http_connections);
620 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
623 ffserver_get_arg(arg, sizeof(arg), p);
625 llval = strtoll(arg, &tailp, 10);
626 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
627 ERROR("Invalid MaxBandwidth: %s\n", arg);
629 config->max_bandwidth = llval;
630 } else if (!av_strcasecmp(cmd, "CustomLog")) {
632 ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
633 } else if (!av_strcasecmp(cmd, "LoadModule")) {
634 ERROR("Loadable modules are no longer supported\n");
635 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
636 config->use_defaults = 0;
637 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
638 config->use_defaults = 1;
640 ERROR("Incorrect keyword: '%s'\n", cmd);
644 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
645 FFServerStream **pfeed)
647 FFServerStream *feed;
651 if (!av_strcasecmp(cmd, "<Feed")) {
654 feed = av_mallocz(sizeof(FFServerStream));
656 return AVERROR(ENOMEM);
657 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
658 q = strrchr(feed->filename, '>');
662 for (s = config->first_feed; s; s = s->next) {
663 if (!strcmp(feed->filename, s->filename))
664 ERROR("Feed '%s' already registered\n", s->filename);
667 feed->fmt = av_guess_format("ffm", NULL, NULL);
668 /* default feed file */
669 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
670 "/tmp/%s.ffm", feed->filename);
671 feed->feed_max_size = 5 * 1024 * 1024;
673 feed->feed = feed; /* self feeding :-) */
678 if (!av_strcasecmp(cmd, "Launch")) {
681 feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
682 if (!feed->child_argv)
683 return AVERROR(ENOMEM);
684 for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
685 ffserver_get_arg(arg, sizeof(arg), p);
689 feed->child_argv[i] = av_strdup(arg);
690 if (!feed->child_argv[i])
691 return AVERROR(ENOMEM);
694 feed->child_argv[i] =
695 av_asprintf("http://%s:%d/%s",
696 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
697 inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
699 if (!feed->child_argv[i])
700 return AVERROR(ENOMEM);
701 } else if (!av_strcasecmp(cmd, "ACL")) {
702 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
704 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
705 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
706 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
707 } else if (!av_strcasecmp(cmd, "Truncate")) {
708 ffserver_get_arg(arg, sizeof(arg), p);
709 /* assume Truncate is true in case no argument is specified */
713 WARNING("Truncate N syntax in configuration file is deprecated, "
714 "use Truncate alone with no arguments\n");
715 feed->truncate = strtod(arg, NULL);
717 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
721 ffserver_get_arg(arg, sizeof(arg), p);
723 fsize = strtod(p1, &p1);
724 switch(av_toupper(*p1)) {
729 fsize *= 1024 * 1024;
732 fsize *= 1024 * 1024 * 1024;
735 ERROR("Invalid file size: %s\n", arg);
738 feed->feed_max_size = (int64_t)fsize;
739 if (feed->feed_max_size < FFM_PACKET_SIZE*4)
740 ERROR("Feed max file size is too small, must be at least %d\n",
742 } else if (!av_strcasecmp(cmd, "</Feed>")) {
745 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
750 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
751 FFServerStream **pstream)
753 char arg[1024], arg2[1024];
754 FFServerStream *stream;
760 if (!av_strcasecmp(cmd, "<Stream")) {
763 stream = av_mallocz(sizeof(FFServerStream));
765 return AVERROR(ENOMEM);
766 config->dummy_actx = avcodec_alloc_context3(NULL);
767 config->dummy_vctx = avcodec_alloc_context3(NULL);
768 if (!config->dummy_vctx || !config->dummy_actx) {
770 avcodec_free_context(&config->dummy_vctx);
771 avcodec_free_context(&config->dummy_actx);
772 return AVERROR(ENOMEM);
774 config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
775 config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
776 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
777 q = strrchr(stream->filename, '>');
781 for (s = config->first_stream; s; s = s->next) {
782 if (!strcmp(stream->filename, s->filename))
783 ERROR("Stream '%s' already registered\n", s->filename);
786 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
788 config->guessed_audio_codec_id = stream->fmt->audio_codec;
789 config->guessed_video_codec_id = stream->fmt->video_codec;
791 config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
792 config->guessed_video_codec_id = AV_CODEC_ID_NONE;
794 config->stream_use_defaults = config->use_defaults;
799 if (!av_strcasecmp(cmd, "Feed")) {
800 FFServerStream *sfeed;
801 ffserver_get_arg(arg, sizeof(arg), p);
802 sfeed = config->first_feed;
804 if (!strcmp(sfeed->filename, arg))
806 sfeed = sfeed->next_feed;
809 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
812 stream->feed = sfeed;
813 } else if (!av_strcasecmp(cmd, "Format")) {
814 ffserver_get_arg(arg, sizeof(arg), p);
815 if (!strcmp(arg, "status")) {
816 stream->stream_type = STREAM_TYPE_STATUS;
819 stream->stream_type = STREAM_TYPE_LIVE;
820 /* JPEG cannot be used here, so use single frame MJPEG */
821 if (!strcmp(arg, "jpeg"))
822 strcpy(arg, "mjpeg");
823 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
825 ERROR("Unknown Format: %s\n", arg);
828 config->guessed_audio_codec_id = stream->fmt->audio_codec;
829 config->guessed_video_codec_id = stream->fmt->video_codec;
831 } else if (!av_strcasecmp(cmd, "InputFormat")) {
832 ffserver_get_arg(arg, sizeof(arg), p);
833 stream->ifmt = av_find_input_format(arg);
835 ERROR("Unknown input format: %s\n", arg);
836 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
837 if (stream->stream_type == STREAM_TYPE_STATUS)
838 ffserver_get_arg(stream->feed_filename,
839 sizeof(stream->feed_filename), p);
841 ERROR("FaviconURL only permitted for status streams\n");
842 } else if (!av_strcasecmp(cmd, "Author") ||
843 !av_strcasecmp(cmd, "Comment") ||
844 !av_strcasecmp(cmd, "Copyright") ||
845 !av_strcasecmp(cmd, "Title")) {
848 ffserver_get_arg(arg, sizeof(arg), p);
849 for (i = 0; i < strlen(cmd); i++)
850 key[i] = av_tolower(cmd[i]);
852 WARNING("'%s' option in configuration file is deprecated, "
853 "use 'Metadata %s VALUE' instead\n", cmd, key);
854 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
856 } else if (!av_strcasecmp(cmd, "Metadata")) {
857 ffserver_get_arg(arg, sizeof(arg), p);
858 ffserver_get_arg(arg2, sizeof(arg2), p);
859 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
861 } else if (!av_strcasecmp(cmd, "Preroll")) {
862 ffserver_get_arg(arg, sizeof(arg), p);
863 stream->prebuffer = atof(arg) * 1000;
864 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
865 stream->send_on_key = 1;
866 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
867 ffserver_get_arg(arg, sizeof(arg), p);
868 ffserver_set_codec(config->dummy_actx, arg, config);
869 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
870 ffserver_get_arg(arg, sizeof(arg), p);
871 ffserver_set_codec(config->dummy_vctx, arg, config);
872 } else if (!av_strcasecmp(cmd, "MaxTime")) {
873 ffserver_get_arg(arg, sizeof(arg), p);
874 stream->max_time = atof(arg) * 1000;
875 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
877 ffserver_get_arg(arg, sizeof(arg), p);
878 ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
879 "Invalid %s: %s\n", cmd, arg);
880 if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f), AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
882 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
883 ffserver_get_arg(arg, sizeof(arg), p);
884 if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
886 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
887 ffserver_get_arg(arg, sizeof(arg), p);
888 if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
890 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
891 int minrate, maxrate;
893 ffserver_get_arg(arg, sizeof(arg), p);
894 dash = strchr(arg, '-');
898 if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0 &&
899 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0) {
900 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
901 ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
905 ERROR("Incorrect format for VideoBitRateRange -- should be "
906 "<min>-<max>: %s\n", arg);
907 } else if (!av_strcasecmp(cmd, "Debug")) {
908 ffserver_get_arg(arg, sizeof(arg), p);
909 if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
910 ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
912 } else if (!av_strcasecmp(cmd, "Strict")) {
913 ffserver_get_arg(arg, sizeof(arg), p);
914 if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
915 ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
917 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
918 ffserver_get_arg(arg, sizeof(arg), p);
919 ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
920 "Invalid %s: %s", cmd, arg);
921 if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
923 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
924 ffserver_get_arg(arg, sizeof(arg), p);
925 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
926 "Invalid %s: %s", cmd, arg);
927 if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
929 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
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("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
935 } else if (!av_strcasecmp(cmd, "VideoSize")) {
937 ffserver_get_arg(arg, sizeof(arg), p);
938 ret = av_parse_video_size(&w, &h, arg);
940 ERROR("Invalid video size '%s'\n", arg);
943 WARNING("Image size is not a multiple of 2\n");
944 if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
947 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
948 ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
949 arg[0] = '1'; arg[1] = '/';
950 if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
952 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
953 enum AVPixelFormat pix_fmt;
954 ffserver_get_arg(arg, sizeof(arg), p);
955 pix_fmt = av_get_pix_fmt(arg);
956 if (pix_fmt == AV_PIX_FMT_NONE)
957 ERROR("Unknown pixel format: %s\n", arg);
958 else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
960 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
961 ffserver_get_arg(arg, sizeof(arg), p);
962 if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
964 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
965 if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
967 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
968 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
970 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
971 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
972 ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
974 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
975 !av_strcasecmp(cmd, "AVOptionAudio")) {
977 ffserver_get_arg(arg, sizeof(arg), p);
978 ffserver_get_arg(arg2, sizeof(arg2), p);
979 if (!av_strcasecmp(cmd, "AVOptionVideo"))
980 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config);
982 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config);
985 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
986 !av_strcasecmp(cmd, "AVPresetAudio")) {
987 ffserver_get_arg(arg, sizeof(arg), p);
988 if (!av_strcasecmp(cmd, "AVPresetVideo"))
989 ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
991 ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
992 } else if (!av_strcasecmp(cmd, "VideoTag")) {
993 ffserver_get_arg(arg, sizeof(arg), p);
994 if (strlen(arg) == 4 &&
995 ffserver_save_avoption_int("codec_tag", MKTAG(arg[0], arg[1], arg[2], arg[3]),
996 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
998 } else if (!av_strcasecmp(cmd, "BitExact")) {
999 if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1001 } else if (!av_strcasecmp(cmd, "DctFastint")) {
1002 if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1004 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1005 if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1007 } else if (!av_strcasecmp(cmd, "Qscale")) {
1008 ffserver_get_arg(arg, sizeof(arg), p);
1009 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1010 "Invalid Qscale: %s\n", arg);
1011 if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1012 ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1013 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1015 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1016 ffserver_get_arg(arg, sizeof(arg), p);
1017 if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1019 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1020 ffserver_get_arg(arg, sizeof(arg), p);
1021 if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1023 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1024 ffserver_get_arg(arg, sizeof(arg), p);
1025 if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1027 } else if (!av_strcasecmp(cmd, "LumiMask")) {
1028 ffserver_get_arg(arg, sizeof(arg), p);
1029 if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1031 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1032 ffserver_get_arg(arg, sizeof(arg), p);
1033 if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1035 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1036 config->no_video = 1;
1037 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1038 config->no_audio = 1;
1039 } else if (!av_strcasecmp(cmd, "ACL")) {
1040 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1042 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1043 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1044 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1045 ffserver_get_arg(arg, sizeof(arg), p);
1046 av_freep(&stream->rtsp_option);
1047 stream->rtsp_option = av_strdup(arg);
1048 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1049 ffserver_get_arg(arg, sizeof(arg), p);
1050 if (resolve_host(&stream->multicast_ip, arg))
1051 ERROR("Invalid host/IP address: %s\n", arg);
1052 stream->is_multicast = 1;
1053 stream->loop = 1; /* default is looping */
1054 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1055 ffserver_get_arg(arg, sizeof(arg), p);
1056 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1057 "Invalid MulticastPort: %s\n", arg);
1058 stream->multicast_port = val;
1059 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1060 ffserver_get_arg(arg, sizeof(arg), p);
1061 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1062 "Invalid MulticastTTL: %s\n", arg);
1063 stream->multicast_ttl = val;
1064 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1066 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1067 config->stream_use_defaults &= 1;
1068 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1069 if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1070 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1071 if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1072 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1073 add_codec(stream, audio_enc, config);
1075 if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1076 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1077 if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1078 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1079 add_codec(stream, video_enc, config);
1082 av_dict_free(&config->video_opts);
1083 av_dict_free(&config->audio_opts);
1084 avcodec_free_context(&config->dummy_vctx);
1085 avcodec_free_context(&config->dummy_actx);
1087 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1088 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1090 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1091 if (config->stream_use_defaults > 1)
1092 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1093 config->stream_use_defaults = 3;
1094 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1095 if (config->stream_use_defaults > 1)
1096 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1097 config->stream_use_defaults = 2;
1099 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1103 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1104 av_dict_free(&config->video_opts);
1105 av_dict_free(&config->audio_opts);
1106 avcodec_free_context(&config->dummy_vctx);
1107 avcodec_free_context(&config->dummy_actx);
1108 return AVERROR(ENOMEM);
1111 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1112 FFServerStream **predirect)
1114 FFServerStream *redirect;
1115 av_assert0(predirect);
1116 redirect = *predirect;
1118 if (!av_strcasecmp(cmd, "<Redirect")) {
1120 redirect = av_mallocz(sizeof(FFServerStream));
1122 return AVERROR(ENOMEM);
1124 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1125 q = strrchr(redirect->filename, '>');
1128 redirect->stream_type = STREAM_TYPE_REDIRECT;
1129 *predirect = redirect;
1132 av_assert0(redirect);
1133 if (!av_strcasecmp(cmd, "URL")) {
1134 ffserver_get_arg(redirect->feed_filename,
1135 sizeof(redirect->feed_filename), p);
1136 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1137 if (!redirect->feed_filename[0])
1138 ERROR("No URL found for <Redirect>\n");
1141 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1146 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1152 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1153 FFServerStream **last_feed, *feed = NULL;
1158 f = fopen(filename, "r");
1160 ret = AVERROR(errno);
1161 av_log(NULL, AV_LOG_ERROR,
1162 "Could not open the configuration file '%s'\n", filename);
1166 config->first_stream = NULL;
1167 config->first_feed = NULL;
1168 config->errors = config->warnings = 0;
1170 last_stream = &config->first_stream;
1171 last_feed = &config->first_feed;
1173 config->line_num = 0;
1174 while (fgets(line, sizeof(line), f) != NULL) {
1177 while (av_isspace(*p))
1179 if (*p == '\0' || *p == '#')
1182 ffserver_get_arg(cmd, sizeof(cmd), &p);
1184 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1185 int opening = !av_strcasecmp(cmd, "<Feed");
1186 if (opening && (stream || feed || redirect)) {
1187 ERROR("Already in a tag\n");
1189 ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
1193 /* add in stream & feed list */
1194 *last_stream = feed;
1196 last_stream = &feed->next;
1197 last_feed = &feed->next_feed;
1200 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1201 int opening = !av_strcasecmp(cmd, "<Stream");
1202 if (opening && (stream || feed || redirect)) {
1203 ERROR("Already in a tag\n");
1205 ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
1209 /* add in stream list */
1210 *last_stream = stream;
1211 last_stream = &stream->next;
1214 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1215 int opening = !av_strcasecmp(cmd, "<Redirect");
1216 if (opening && (stream || feed || redirect))
1217 ERROR("Already in a tag\n");
1219 ret = ffserver_parse_config_redirect(config, cmd, &p,
1224 /* add in stream list */
1225 *last_stream = redirect;
1226 last_stream = &redirect->next;
1230 ffserver_parse_config_global(config, cmd, &p);
1233 if (stream || feed || redirect)
1234 ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>"));
1240 return AVERROR(EINVAL);
1248 void ffserver_free_child_args(void *argsp)
1254 args = *(char ***)argsp;
1257 for (i = 0; i < MAX_CHILD_ARGS; i++)