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 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
35 FFServerConfig *config);
36 static void vreport_config_error(const char *filename, int line_num, int log_level,
37 int *errors, const char *fmt, va_list vl);
38 static void report_config_error(const char *filename, int line_num, int log_level,
39 int *errors, const char *fmt, ...);
41 /* FIXME: make ffserver work with IPv6 */
42 /* resolve host with also IP address parsing */
43 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
46 if (!ff_inet_aton(hostname, sin_addr)) {
48 struct addrinfo *ai, *cur;
49 struct addrinfo hints = { 0 };
50 hints.ai_family = AF_INET;
51 if (getaddrinfo(hostname, NULL, &hints, &ai))
53 /* getaddrinfo returns a linked list of addrinfo structs.
54 * Even if we set ai_family = AF_INET above, make sure
55 * that the returned one actually is of the correct type. */
56 for (cur = ai; cur; cur = cur->ai_next) {
57 if (cur->ai_family == AF_INET) {
58 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
67 hp = gethostbyname(hostname);
70 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
76 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
83 while (av_isspace(*p)) p++;
86 if (*p == '\"' || *p == '\'')
98 if ((q - buf) < buf_size - 1)
103 if (quote && *p == quote)
108 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
109 FFServerIPAddressACL *ext_acl,
110 const char *p, const char *filename, int line_num)
113 FFServerIPAddressACL acl;
116 ffserver_get_arg(arg, sizeof(arg), &p);
117 if (av_strcasecmp(arg, "allow") == 0)
118 acl.action = IP_ALLOW;
119 else if (av_strcasecmp(arg, "deny") == 0)
120 acl.action = IP_DENY;
122 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
123 filename, line_num, arg);
127 ffserver_get_arg(arg, sizeof(arg), &p);
129 if (resolve_host(&acl.first, arg)) {
130 fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
131 filename, line_num, arg);
134 acl.last = acl.first;
136 ffserver_get_arg(arg, sizeof(arg), &p);
139 if (resolve_host(&acl.last, arg)) {
141 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
142 filename, line_num, arg);
148 FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
149 FFServerIPAddressACL **naclp = 0;
155 naclp = &stream->acl;
161 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
168 naclp = &(*naclp)->next;
176 /* add a codec and set the default parameters */
177 static void add_codec(FFServerStream *stream, AVCodecContext *av,
178 FFServerConfig *config)
183 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
186 opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
187 &config->audio_opts : &config->video_opts;
188 av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
189 av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
190 if (av_dict_count(*opts))
191 av_log(NULL, AV_LOG_WARNING,
192 "Something is wrong, %d options are not set!\n", av_dict_count(*opts));
194 if (config->stream_use_defaults) {
196 /* compute default parameters */
197 switch(av->codec_type) {
198 case AVMEDIA_TYPE_AUDIO:
199 if (av->bit_rate == 0)
200 av->bit_rate = 64000;
201 if (av->sample_rate == 0)
202 av->sample_rate = 22050;
203 if (av->channels == 0)
206 case AVMEDIA_TYPE_VIDEO:
207 if (av->bit_rate == 0)
208 av->bit_rate = 64000;
209 if (av->time_base.num == 0){
210 av->time_base.den = 5;
211 av->time_base.num = 1;
213 if (av->width == 0 || av->height == 0) {
217 /* Bitrate tolerance is less for streaming */
218 if (av->bit_rate_tolerance == 0)
219 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
220 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
225 if (av->max_qdiff == 0)
230 if (!av->nsse_weight)
233 av->frame_skip_cmp = FF_CMP_DCTMAX;
235 av->me_method = ME_EPZS;
237 /* FIXME: rc_buffer_aggressivity and rc_eq are deprecated */
238 av->rc_buffer_aggressivity = 1.0;
241 av->rc_eq = av_strdup("tex^qComp");
242 if (!av->i_quant_factor)
243 av->i_quant_factor = -0.8;
244 if (!av->b_quant_factor)
245 av->b_quant_factor = 1.25;
246 if (!av->b_quant_offset)
247 av->b_quant_offset = 1.25;
248 if (!av->rc_max_rate)
249 av->rc_max_rate = av->bit_rate * 2;
251 if (av->rc_max_rate && !av->rc_buffer_size) {
252 av->rc_buffer_size = av->rc_max_rate;
261 switch(av->codec_type) {
262 case AVMEDIA_TYPE_AUDIO:
263 if (av->bit_rate == 0)
264 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
265 &config->errors, "audio bit rate is not set\n");
266 if (av->sample_rate == 0)
267 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
268 &config->errors, "audio sample rate is not set\n");
270 case AVMEDIA_TYPE_VIDEO:
271 if (av->width == 0 || av->height == 0)
272 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
273 &config->errors, "video size is not set\n");
280 st = av_mallocz(sizeof(AVStream));
284 stream->streams[stream->nb_streams++] = st;
287 static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name, FFServerConfig *config)
290 AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
291 if (!codec || codec->type != ctx->codec_type) {
292 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
293 &config->errors, "Invalid codec name: %s\n", codec_name);
296 if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
297 if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
301 if (ctx->codec_id != codec->id)
302 report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors,
303 "Inconsistent configuration: trying to set %s codec option, but %s codec is used previously\n",
304 codec_name, avcodec_get_name(ctx->codec_id));
308 static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
311 char filename[1000], tmp[1000], tmp2[1000], line[1000];
313 AVCodecContext *avctx;
314 const AVCodec *codec;
317 case AV_OPT_FLAG_AUDIO_PARAM:
318 avctx = config->dummy_actx;
320 case AV_OPT_FLAG_VIDEO_PARAM:
321 avctx = config->dummy_vctx;
326 codec = avcodec_find_encoder(avctx->codec_id);
328 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
329 codec ? codec->name : NULL))) {
330 av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
331 return AVERROR(EINVAL);
335 int e= fscanf(f, "%999[^\n]\n", line) - 1;
336 if(line[0] == '#' && !e)
338 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
340 av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename, line);
341 ret = AVERROR(EINVAL);
344 if ((!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO) ||
345 !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
347 if (ffserver_set_codec(avctx, tmp2, config) < 0)
349 } else if (!strcmp(tmp, "scodec")) {
350 av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
351 ret = AVERROR(EINVAL);
353 } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
362 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
364 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
367 AVOutputFormat *stream_fmt;
368 char stream_format_name[64];
370 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
372 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
381 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
383 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
384 av_vlog(NULL, log_level, fmt, vl);
389 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
393 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
397 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
398 FFServerConfig *config, const char *error_msg, ...)
402 if (!value || !value[0])
405 tmp = strtol(value, &tailp, 0);
406 if (tmp < min || tmp > max)
409 if (FFABS(tmp) > INT_MAX / FFABS(factor))
413 if (tailp[0] || errno)
421 va_start(vl, error_msg);
422 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
423 &config->errors, error_msg, vl);
426 return AVERROR(EINVAL);
429 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
430 FFServerConfig *config, const char *error_msg, ...)
434 if (!value || !value[0])
437 tmp = strtod(value, &tailp);
438 if (tmp < min || tmp > max)
442 if (tailp[0] || errno)
450 va_start(vl, error_msg);
451 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
452 &config->errors, error_msg, vl);
455 return AVERROR(EINVAL);
458 static int ffserver_save_avoption(const char *opt, const char *arg, int type, FFServerConfig *config)
460 static int hinted = 0;
462 AVDictionaryEntry *e;
463 const AVOption *o = NULL;
464 const char *option = NULL;
465 const char *codec_name = NULL;
469 enum AVCodecID guessed_codec_id;
472 case AV_OPT_FLAG_VIDEO_PARAM:
473 ctx = config->dummy_vctx;
474 dict = &config->video_opts;
475 guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
476 config->guessed_video_codec_id : AV_CODEC_ID_H264;
478 case AV_OPT_FLAG_AUDIO_PARAM:
479 ctx = config->dummy_actx;
480 dict = &config->audio_opts;
481 guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
482 config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
488 if (strchr(opt, ':')) {
489 //explicit private option
490 snprintf(buff, sizeof(buff), "%s", opt);
492 option = strchr(buff, ':');
493 buff[option - buff] = '\0';
495 if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
497 if (!ctx->codec || !ctx->priv_data)
503 o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
504 if (!o && (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
505 !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
506 o = av_opt_find(ctx, option, NULL, 0, 0);
508 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
509 &config->errors, "Option not found: %s\n", opt);
510 if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
512 report_config_error(config->filename, config->line_num, AV_LOG_ERROR, NULL,
513 "If '%s' is a codec private option, then prefix it with codec name, "
514 "for example '%s:%s %s' or define codec earlier.\n",
515 opt, avcodec_get_name(guessed_codec_id) ,opt, arg);
517 } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
518 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
519 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
520 arg, av_err2str(ret));
521 } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
522 if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
523 return av_dict_set(dict, option, arg, AV_DICT_APPEND);
524 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
526 "Redeclaring value of the option %s, previous value: %s\n",
528 } else if (av_dict_set(dict, option, arg, 0) < 0) {
529 return AVERROR(ENOMEM);
534 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
535 int type, FFServerConfig *config)
538 snprintf(buf, sizeof(buf), "%"PRId64, arg);
539 return ffserver_save_avoption(opt, buf, type, config);
542 #define ERROR(...) report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors, __VA_ARGS__)
543 #define WARNING(...) report_config_error(config->filename, config->line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
545 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
550 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
551 if (!av_strcasecmp(cmd, "Port"))
552 WARNING("Port option is deprecated, use HTTPPort instead\n");
553 ffserver_get_arg(arg, sizeof(arg), p);
554 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
555 "Invalid port: %s\n", arg);
557 WARNING("Trying to use IETF assigned system port: %d\n", val);
558 config->http_addr.sin_port = htons(val);
559 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
560 if (!av_strcasecmp(cmd, "BindAddress"))
561 WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
562 ffserver_get_arg(arg, sizeof(arg), p);
563 if (resolve_host(&config->http_addr.sin_addr, arg))
564 ERROR("Invalid host/IP address: %s\n", arg);
565 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
566 WARNING("NoDaemon option has no effect, you should remove it\n");
567 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
568 ffserver_get_arg(arg, sizeof(arg), p);
569 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
570 "Invalid port: %s\n", arg);
571 config->rtsp_addr.sin_port = htons(val);
572 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
573 ffserver_get_arg(arg, sizeof(arg), p);
574 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
575 ERROR("Invalid host/IP address: %s\n", arg);
576 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
577 ffserver_get_arg(arg, sizeof(arg), p);
578 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
579 "Invalid MaxHTTPConnections: %s\n", arg);
580 config->nb_max_http_connections = val;
581 if (config->nb_max_connections > config->nb_max_http_connections)
582 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
583 config->nb_max_connections, config->nb_max_http_connections);
584 } else if (!av_strcasecmp(cmd, "MaxClients")) {
585 ffserver_get_arg(arg, sizeof(arg), p);
586 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
587 "Invalid MaxClients: %s\n", arg);
588 config->nb_max_connections = val;
589 if (config->nb_max_connections > config->nb_max_http_connections)
590 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
591 config->nb_max_connections, config->nb_max_http_connections);
592 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
595 ffserver_get_arg(arg, sizeof(arg), p);
597 llval = strtoll(arg, &tailp, 10);
598 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
599 ERROR("Invalid MaxBandwidth: %s\n", arg);
601 config->max_bandwidth = llval;
602 } else if (!av_strcasecmp(cmd, "CustomLog")) {
604 ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
605 } else if (!av_strcasecmp(cmd, "LoadModule")) {
606 ERROR("Loadable modules are no longer supported\n");
607 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
608 config->use_defaults = 0;
609 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
610 config->use_defaults = 1;
612 ERROR("Incorrect keyword: '%s'\n", cmd);
616 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
617 FFServerStream **pfeed)
619 FFServerStream *feed;
623 if (!av_strcasecmp(cmd, "<Feed")) {
626 feed = av_mallocz(sizeof(FFServerStream));
628 return AVERROR(ENOMEM);
629 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
630 q = strrchr(feed->filename, '>');
634 for (s = config->first_feed; s; s = s->next) {
635 if (!strcmp(feed->filename, s->filename))
636 ERROR("Feed '%s' already registered\n", s->filename);
639 feed->fmt = av_guess_format("ffm", NULL, NULL);
640 /* default feed file */
641 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
642 "/tmp/%s.ffm", feed->filename);
643 feed->feed_max_size = 5 * 1024 * 1024;
645 feed->feed = feed; /* self feeding :-) */
650 if (!av_strcasecmp(cmd, "Launch")) {
653 feed->child_argv = av_mallocz(64 * sizeof(char *));
654 if (!feed->child_argv)
655 return AVERROR(ENOMEM);
656 for (i = 0; i < 62; i++) {
657 ffserver_get_arg(arg, sizeof(arg), p);
661 feed->child_argv[i] = av_strdup(arg);
662 if (!feed->child_argv[i])
663 return AVERROR(ENOMEM);
666 feed->child_argv[i] =
667 av_asprintf("http://%s:%d/%s",
668 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
669 inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
671 if (!feed->child_argv[i])
672 return AVERROR(ENOMEM);
673 } else if (!av_strcasecmp(cmd, "ACL")) {
674 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
676 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
677 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
678 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
679 } else if (!av_strcasecmp(cmd, "Truncate")) {
680 ffserver_get_arg(arg, sizeof(arg), p);
681 /* assume Truncate is true in case no argument is specified */
685 WARNING("Truncate N syntax in configuration file is deprecated, "
686 "use Truncate alone with no arguments\n");
687 feed->truncate = strtod(arg, NULL);
689 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
693 ffserver_get_arg(arg, sizeof(arg), p);
695 fsize = strtod(p1, &p1);
696 switch(av_toupper(*p1)) {
701 fsize *= 1024 * 1024;
704 fsize *= 1024 * 1024 * 1024;
707 ERROR("Invalid file size: %s\n", arg);
710 feed->feed_max_size = (int64_t)fsize;
711 if (feed->feed_max_size < FFM_PACKET_SIZE*4)
712 ERROR("Feed max file size is too small, must be at least %d\n",
714 } else if (!av_strcasecmp(cmd, "</Feed>")) {
717 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
722 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
723 FFServerStream **pstream)
725 char arg[1024], arg2[1024];
726 FFServerStream *stream;
732 if (!av_strcasecmp(cmd, "<Stream")) {
735 stream = av_mallocz(sizeof(FFServerStream));
737 return AVERROR(ENOMEM);
738 config->dummy_actx = avcodec_alloc_context3(NULL);
739 config->dummy_vctx = avcodec_alloc_context3(NULL);
740 if (!config->dummy_vctx || !config->dummy_actx) {
742 avcodec_free_context(&config->dummy_vctx);
743 avcodec_free_context(&config->dummy_actx);
744 return AVERROR(ENOMEM);
746 config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
747 config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
748 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
749 q = strrchr(stream->filename, '>');
753 for (s = config->first_stream; s; s = s->next) {
754 if (!strcmp(stream->filename, s->filename))
755 ERROR("Stream '%s' already registered\n", s->filename);
758 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
760 config->guessed_audio_codec_id = stream->fmt->audio_codec;
761 config->guessed_video_codec_id = stream->fmt->video_codec;
763 config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
764 config->guessed_video_codec_id = AV_CODEC_ID_NONE;
766 config->stream_use_defaults = config->use_defaults;
771 if (!av_strcasecmp(cmd, "Feed")) {
772 FFServerStream *sfeed;
773 ffserver_get_arg(arg, sizeof(arg), p);
774 sfeed = config->first_feed;
776 if (!strcmp(sfeed->filename, arg))
778 sfeed = sfeed->next_feed;
781 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
784 stream->feed = sfeed;
785 } else if (!av_strcasecmp(cmd, "Format")) {
786 ffserver_get_arg(arg, sizeof(arg), p);
787 if (!strcmp(arg, "status")) {
788 stream->stream_type = STREAM_TYPE_STATUS;
791 stream->stream_type = STREAM_TYPE_LIVE;
792 /* JPEG cannot be used here, so use single frame MJPEG */
793 if (!strcmp(arg, "jpeg"))
794 strcpy(arg, "mjpeg");
795 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
797 ERROR("Unknown Format: %s\n", arg);
800 config->guessed_audio_codec_id = stream->fmt->audio_codec;
801 config->guessed_video_codec_id = stream->fmt->video_codec;
803 } else if (!av_strcasecmp(cmd, "InputFormat")) {
804 ffserver_get_arg(arg, sizeof(arg), p);
805 stream->ifmt = av_find_input_format(arg);
807 ERROR("Unknown input format: %s\n", arg);
808 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
809 if (stream->stream_type == STREAM_TYPE_STATUS)
810 ffserver_get_arg(stream->feed_filename,
811 sizeof(stream->feed_filename), p);
813 ERROR("FaviconURL only permitted for status streams\n");
814 } else if (!av_strcasecmp(cmd, "Author") ||
815 !av_strcasecmp(cmd, "Comment") ||
816 !av_strcasecmp(cmd, "Copyright") ||
817 !av_strcasecmp(cmd, "Title")) {
820 ffserver_get_arg(arg, sizeof(arg), p);
821 for (i = 0; i < strlen(cmd); i++)
822 key[i] = av_tolower(cmd[i]);
824 WARNING("'%s' option in configuration file is deprecated, "
825 "use 'Metadata %s VALUE' instead\n", cmd, key);
826 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
828 } else if (!av_strcasecmp(cmd, "Metadata")) {
829 ffserver_get_arg(arg, sizeof(arg), p);
830 ffserver_get_arg(arg2, sizeof(arg2), p);
831 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
833 } else if (!av_strcasecmp(cmd, "Preroll")) {
834 ffserver_get_arg(arg, sizeof(arg), p);
835 stream->prebuffer = atof(arg) * 1000;
836 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
837 stream->send_on_key = 1;
838 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
839 ffserver_get_arg(arg, sizeof(arg), p);
840 ffserver_set_codec(config->dummy_actx, arg, config);
841 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
842 ffserver_get_arg(arg, sizeof(arg), p);
843 ffserver_set_codec(config->dummy_vctx, arg, config);
844 } else if (!av_strcasecmp(cmd, "MaxTime")) {
845 ffserver_get_arg(arg, sizeof(arg), p);
846 stream->max_time = atof(arg) * 1000;
847 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
849 ffserver_get_arg(arg, sizeof(arg), p);
850 ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
851 "Invalid %s: %s\n", cmd, arg);
852 if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f), AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
854 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
855 ffserver_get_arg(arg, sizeof(arg), p);
856 if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
858 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
859 ffserver_get_arg(arg, sizeof(arg), p);
860 if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
862 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
863 int minrate, maxrate;
865 ffserver_get_arg(arg, sizeof(arg), p);
866 dash = strchr(arg, '-');
870 if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0 &&
871 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0) {
872 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
873 ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
877 ERROR("Incorrect format for VideoBitRateRange -- should be "
878 "<min>-<max>: %s\n", arg);
879 } else if (!av_strcasecmp(cmd, "Debug")) {
880 ffserver_get_arg(arg, sizeof(arg), p);
881 if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
882 ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
884 } else if (!av_strcasecmp(cmd, "Strict")) {
885 ffserver_get_arg(arg, sizeof(arg), p);
886 if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
887 ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
889 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
890 ffserver_get_arg(arg, sizeof(arg), p);
891 ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
892 "Invalid %s: %s", cmd, arg);
893 if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
895 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
896 ffserver_get_arg(arg, sizeof(arg), p);
897 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
898 "Invalid %s: %s", cmd, arg);
899 if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
901 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
902 ffserver_get_arg(arg, sizeof(arg), p);
903 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
904 "Invalid %s: %s", cmd, arg);
905 if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
907 } else if (!av_strcasecmp(cmd, "VideoSize")) {
909 ffserver_get_arg(arg, sizeof(arg), p);
910 ret = av_parse_video_size(&w, &h, arg);
912 ERROR("Invalid video size '%s'\n", arg);
915 WARNING("Image size is not a multiple of 2\n");
916 if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
919 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
920 ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
921 arg[0] = '1'; arg[1] = '/';
922 if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
924 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
925 enum AVPixelFormat pix_fmt;
926 ffserver_get_arg(arg, sizeof(arg), p);
927 pix_fmt = av_get_pix_fmt(arg);
928 if (pix_fmt == AV_PIX_FMT_NONE)
929 ERROR("Unknown pixel format: %s\n", arg);
930 else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
932 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
933 ffserver_get_arg(arg, sizeof(arg), p);
934 if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
936 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
937 if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
939 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
940 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
942 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
943 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
944 ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
946 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
947 !av_strcasecmp(cmd, "AVOptionAudio")) {
949 ffserver_get_arg(arg, sizeof(arg), p);
950 ffserver_get_arg(arg2, sizeof(arg2), p);
951 if (!av_strcasecmp(cmd, "AVOptionVideo"))
952 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config);
954 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config);
957 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
958 !av_strcasecmp(cmd, "AVPresetAudio")) {
959 ffserver_get_arg(arg, sizeof(arg), p);
960 if (!av_strcasecmp(cmd, "AVPresetVideo"))
961 ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
963 ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
964 } else if (!av_strcasecmp(cmd, "VideoTag")) {
965 ffserver_get_arg(arg, sizeof(arg), p);
966 if (strlen(arg) == 4 &&
967 ffserver_save_avoption_int("codec_tag", MKTAG(arg[0], arg[1], arg[2], arg[3]),
968 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
970 } else if (!av_strcasecmp(cmd, "BitExact")) {
971 if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
973 } else if (!av_strcasecmp(cmd, "DctFastint")) {
974 if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
976 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
977 if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
979 } else if (!av_strcasecmp(cmd, "Qscale")) {
980 ffserver_get_arg(arg, sizeof(arg), p);
981 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
982 "Invalid Qscale: %s\n", arg);
983 if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
984 ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
985 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
987 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
988 ffserver_get_arg(arg, sizeof(arg), p);
989 if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
991 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
992 ffserver_get_arg(arg, sizeof(arg), p);
993 if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
995 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
996 ffserver_get_arg(arg, sizeof(arg), p);
997 if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
999 } else if (!av_strcasecmp(cmd, "LumiMask")) {
1000 ffserver_get_arg(arg, sizeof(arg), p);
1001 if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1003 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1004 ffserver_get_arg(arg, sizeof(arg), p);
1005 if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1007 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1008 config->no_video = 1;
1009 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1010 config->no_audio = 1;
1011 } else if (!av_strcasecmp(cmd, "ACL")) {
1012 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1014 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1015 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1016 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1017 ffserver_get_arg(arg, sizeof(arg), p);
1018 av_freep(&stream->rtsp_option);
1019 stream->rtsp_option = av_strdup(arg);
1020 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1021 ffserver_get_arg(arg, sizeof(arg), p);
1022 if (resolve_host(&stream->multicast_ip, arg))
1023 ERROR("Invalid host/IP address: %s\n", arg);
1024 stream->is_multicast = 1;
1025 stream->loop = 1; /* default is looping */
1026 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1027 ffserver_get_arg(arg, sizeof(arg), p);
1028 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1029 "Invalid MulticastPort: %s\n", arg);
1030 stream->multicast_port = val;
1031 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1032 ffserver_get_arg(arg, sizeof(arg), p);
1033 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1034 "Invalid MulticastTTL: %s\n", arg);
1035 stream->multicast_ttl = val;
1036 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1038 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1039 config->stream_use_defaults &= 1;
1040 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1041 if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1042 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1043 if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1044 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1045 add_codec(stream, audio_enc, config);
1047 if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1048 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1049 if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1050 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1051 add_codec(stream, video_enc, config);
1054 av_dict_free(&config->video_opts);
1055 av_dict_free(&config->audio_opts);
1056 avcodec_free_context(&config->dummy_vctx);
1057 avcodec_free_context(&config->dummy_actx);
1059 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1060 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1062 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1063 if (config->stream_use_defaults > 1)
1064 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1065 config->stream_use_defaults = 3;
1066 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1067 if (config->stream_use_defaults > 1)
1068 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1069 config->stream_use_defaults = 2;
1071 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1075 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1076 av_dict_free(&config->video_opts);
1077 av_dict_free(&config->audio_opts);
1078 avcodec_free_context(&config->dummy_vctx);
1079 avcodec_free_context(&config->dummy_actx);
1080 return AVERROR(ENOMEM);
1083 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1084 FFServerStream **predirect)
1086 FFServerStream *redirect;
1087 av_assert0(predirect);
1088 redirect = *predirect;
1090 if (!av_strcasecmp(cmd, "<Redirect")) {
1092 redirect = av_mallocz(sizeof(FFServerStream));
1094 return AVERROR(ENOMEM);
1096 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1097 q = strrchr(redirect->filename, '>');
1100 redirect->stream_type = STREAM_TYPE_REDIRECT;
1101 *predirect = redirect;
1104 av_assert0(redirect);
1105 if (!av_strcasecmp(cmd, "URL")) {
1106 ffserver_get_arg(redirect->feed_filename,
1107 sizeof(redirect->feed_filename), p);
1108 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1109 if (!redirect->feed_filename[0])
1110 ERROR("No URL found for <Redirect>\n");
1113 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1118 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1124 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1125 FFServerStream **last_feed, *feed = NULL;
1130 config->line_num = 0;
1131 f = fopen(filename, "r");
1133 ret = AVERROR(errno);
1134 av_log(NULL, AV_LOG_ERROR,
1135 "Could not open the configuration file '%s'\n", filename);
1139 config->first_stream = NULL;
1140 last_stream = &config->first_stream;
1141 config->first_feed = NULL;
1142 last_feed = &config->first_feed;
1143 config->errors = config->warnings = 0;
1146 if (fgets(line, sizeof(line), f) == NULL)
1150 while (av_isspace(*p))
1152 if (*p == '\0' || *p == '#')
1155 ffserver_get_arg(cmd, sizeof(cmd), &p);
1157 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1158 int opening = !av_strcasecmp(cmd, "<Feed");
1159 if (opening && (stream || feed || redirect)) {
1160 ERROR("Already in a tag\n");
1162 if ((ret = ffserver_parse_config_feed(config, cmd, &p, &feed)) < 0)
1165 /* add in stream list */
1166 *last_stream = feed;
1167 last_stream = &feed->next;
1168 /* add in feed list */
1170 last_feed = &feed->next_feed;
1173 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1174 int opening = !av_strcasecmp(cmd, "<Stream");
1175 if (opening && (stream || feed || redirect)) {
1176 ERROR("Already in a tag\n");
1178 if ((ret = ffserver_parse_config_stream(config, cmd, &p, &stream)) < 0)
1181 /* add in stream list */
1182 *last_stream = stream;
1183 last_stream = &stream->next;
1186 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1187 int opening = !av_strcasecmp(cmd, "<Redirect");
1188 if (opening && (stream || feed || redirect))
1189 ERROR("Already in a tag\n");
1191 if ((ret = ffserver_parse_config_redirect(config, cmd, &p, &redirect)) < 0)
1194 /* add in stream list */
1195 *last_stream = redirect;
1196 last_stream = &redirect->next;
1200 ffserver_parse_config_global(config, cmd, &p);
1203 if (stream || feed || redirect)
1204 ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>"));
1210 return AVERROR(EINVAL);