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"
29 #include "ffserver_config.h"
31 #define MAX_CHILD_ARGS 64
33 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
34 FFServerConfig *config);
35 static void vreport_config_error(const char *filename, int line_num,
36 int log_level, int *errors, const char *fmt,
38 static void report_config_error(const char *filename, int line_num,
39 int log_level, int *errors, const char *fmt,
42 #define ERROR(...) report_config_error(config->filename, config->line_num,\
43 AV_LOG_ERROR, &config->errors, __VA_ARGS__)
44 #define WARNING(...) report_config_error(config->filename, config->line_num,\
45 AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
47 /* FIXME: make ffserver work with IPv6 */
48 /* resolve host with also IP address parsing */
49 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
52 if (!ff_inet_aton(hostname, sin_addr)) {
54 struct addrinfo *ai, *cur;
55 struct addrinfo hints = { 0 };
56 hints.ai_family = AF_INET;
57 if (getaddrinfo(hostname, NULL, &hints, &ai))
59 /* getaddrinfo returns a linked list of addrinfo structs.
60 * Even if we set ai_family = AF_INET above, make sure
61 * that the returned one actually is of the correct type. */
62 for (cur = ai; cur; cur = cur->ai_next) {
63 if (cur->ai_family == AF_INET) {
64 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
73 hp = gethostbyname(hostname);
76 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
82 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
91 while (av_isspace(*p)) p++;
93 if (*p == '\"' || *p == '\'')
97 if (quote && *p == quote || !quote && av_isspace(*p))
99 if ((q - buf) < buf_size - 1)
105 if (quote && *p == quote)
110 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
111 FFServerIPAddressACL *ext_acl,
112 const char *p, const char *filename, int line_num)
115 FFServerIPAddressACL acl;
116 FFServerIPAddressACL *nacl;
117 FFServerIPAddressACL **naclp;
119 ffserver_get_arg(arg, sizeof(arg), &p);
120 if (av_strcasecmp(arg, "allow") == 0)
121 acl.action = IP_ALLOW;
122 else if (av_strcasecmp(arg, "deny") == 0)
123 acl.action = IP_DENY;
125 fprintf(stderr, "%s:%d: ACL action '%s' should be ALLOW or DENY.\n",
126 filename, line_num, arg);
130 ffserver_get_arg(arg, sizeof(arg), &p);
132 if (resolve_host(&acl.first, arg)) {
134 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
135 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);
152 nacl = av_mallocz(sizeof(*nacl));
154 fprintf(stderr, "Failed to allocate FFServerIPAddressACL\n");
164 naclp = &stream->acl;
170 fprintf(stderr, "%s:%d: ACL found not in <Stream> or <Feed>\n",
175 naclp = &(*naclp)->next;
186 /* add a codec and set the default parameters */
187 static void add_codec(FFServerStream *stream, AVCodecContext *av,
188 FFServerConfig *config)
191 AVDictionary **opts, *recommended = NULL;
194 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
197 opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
198 &config->audio_opts : &config->video_opts;
199 av_dict_copy(&recommended, *opts, 0);
200 av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
201 av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
203 if (av_dict_count(*opts))
204 av_log(NULL, AV_LOG_WARNING,
205 "Something is wrong, %d options are not set!\n",
206 av_dict_count(*opts));
208 if (!config->stream_use_defaults) {
209 switch(av->codec_type) {
210 case AVMEDIA_TYPE_AUDIO:
211 if (av->bit_rate == 0)
212 report_config_error(config->filename, config->line_num,
213 AV_LOG_ERROR, &config->errors,
214 "audio bit rate is not set\n");
215 if (av->sample_rate == 0)
216 report_config_error(config->filename, config->line_num,
217 AV_LOG_ERROR, &config->errors,
218 "audio sample rate is not set\n");
220 case AVMEDIA_TYPE_VIDEO:
221 if (av->width == 0 || av->height == 0)
222 report_config_error(config->filename, config->line_num,
223 AV_LOG_ERROR, &config->errors,
224 "video size is not set\n");
232 /* stream_use_defaults = true */
234 /* compute default parameters */
235 switch(av->codec_type) {
236 case AVMEDIA_TYPE_AUDIO:
237 if (!av_dict_get(recommended, "b", NULL, 0)) {
238 av->bit_rate = 64000;
239 av_dict_set_int(&recommended, "b", av->bit_rate, 0);
240 WARNING("Setting default value for audio bit rate = %d. "
241 "Use NoDefaults to disable it.\n",
244 if (!av_dict_get(recommended, "ar", NULL, 0)) {
245 av->sample_rate = 22050;
246 av_dict_set_int(&recommended, "ar", av->sample_rate, 0);
247 WARNING("Setting default value for audio sample rate = %d. "
248 "Use NoDefaults to disable it.\n",
251 if (!av_dict_get(recommended, "ac", NULL, 0)) {
253 av_dict_set_int(&recommended, "ac", av->channels, 0);
254 WARNING("Setting default value for audio channel count = %d. "
255 "Use NoDefaults to disable it.\n",
259 case AVMEDIA_TYPE_VIDEO:
260 if (!av_dict_get(recommended, "b", NULL, 0)) {
261 av->bit_rate = 64000;
262 av_dict_set_int(&recommended, "b", av->bit_rate, 0);
263 WARNING("Setting default value for video bit rate = %d. "
264 "Use NoDefaults to disable it.\n",
267 if (!av_dict_get(recommended, "time_base", NULL, 0)){
268 av->time_base.den = 5;
269 av->time_base.num = 1;
270 av_dict_set(&recommended, "time_base", "1/5", 0);
271 WARNING("Setting default value for video frame rate = %d. "
272 "Use NoDefaults to disable it.\n",
275 if (!av_dict_get(recommended, "video_size", NULL, 0)) {
278 av_dict_set(&recommended, "video_size", "160x128", 0);
279 WARNING("Setting default value for video size = %dx%d. "
280 "Use NoDefaults to disable it.\n",
281 av->width, av->height);
283 /* Bitrate tolerance is less for streaming */
284 if (!av_dict_get(recommended, "bt", NULL, 0)) {
285 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
286 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
287 av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0);
288 WARNING("Setting default value for video bit rate tolerance = %d. "
289 "Use NoDefaults to disable it.\n",
290 av->bit_rate_tolerance);
293 if (!av_dict_get(recommended, "rc_eq", NULL, 0)) {
294 av_dict_set(&recommended, "rc_eq", "tex^qComp", 0);
295 WARNING("Setting default value for video rate control equation = "
296 "tex^qComp. Use NoDefaults to disable it.\n");
298 if (!av_dict_get(recommended, "maxrate", NULL, 0)) {
299 av->rc_max_rate = av->bit_rate * 2;
300 av_dict_set_int(&recommended, "maxrate", av->rc_max_rate, 0);
301 WARNING("Setting default value for video max rate = %d. "
302 "Use NoDefaults to disable it.\n",
306 if (av->rc_max_rate && !av_dict_get(recommended, "bufsize", NULL, 0)) {
307 av->rc_buffer_size = av->rc_max_rate;
308 av_dict_set_int(&recommended, "bufsize", av->rc_buffer_size, 0);
309 WARNING("Setting default value for video buffer size = %d. "
310 "Use NoDefaults to disable it.\n",
319 st = av_mallocz(sizeof(*st));
322 av_dict_get_string(recommended, &enc_config, '=', ',');
323 av_dict_free(&recommended);
324 st->recommended_encoder_configuration = enc_config;
326 st->codecpar = avcodec_parameters_alloc();
327 avcodec_parameters_from_context(st->codecpar, av);
328 stream->streams[stream->nb_streams++] = st;
331 static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name,
332 FFServerConfig *config)
335 AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
336 if (!codec || codec->type != ctx->codec_type) {
337 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
339 "Invalid codec name: '%s'\n", codec_name);
342 if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
343 if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
347 if (ctx->codec_id != codec->id)
348 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
350 "Inconsistent configuration: trying to set '%s' "
351 "codec option, but '%s' codec is used previously\n",
352 codec_name, avcodec_get_name(ctx->codec_id));
356 static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
359 char filename[1000], tmp[1000], tmp2[1000], line[1000];
361 AVCodecContext *avctx;
362 const AVCodec *codec;
365 case AV_OPT_FLAG_AUDIO_PARAM:
366 avctx = config->dummy_actx;
368 case AV_OPT_FLAG_VIDEO_PARAM:
369 avctx = config->dummy_vctx;
374 codec = avcodec_find_encoder(avctx->codec_id);
376 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
377 codec ? codec->name : NULL))) {
378 av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
379 return AVERROR(EINVAL);
383 int e= fscanf(f, "%999[^\n]\n", line) - 1;
384 if(line[0] == '#' && !e)
386 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
388 av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename,
390 ret = AVERROR(EINVAL);
393 if (!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO ||
394 !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
396 if (ffserver_set_codec(avctx, tmp2, config) < 0)
398 } else if (!strcmp(tmp, "scodec")) {
399 av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
400 ret = AVERROR(EINVAL);
402 } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
411 static AVOutputFormat *ffserver_guess_format(const char *short_name,
412 const char *filename,
413 const char *mime_type)
415 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
418 AVOutputFormat *stream_fmt;
419 char stream_format_name[64];
421 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
423 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
432 static void vreport_config_error(const char *filename, int line_num,
433 int log_level, int *errors, const char *fmt,
436 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
437 av_vlog(NULL, log_level, fmt, vl);
442 static void report_config_error(const char *filename, int line_num,
443 int log_level, int *errors,
444 const char *fmt, ...)
448 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
452 static int ffserver_set_int_param(int *dest, const char *value, int factor,
453 int min, int max, FFServerConfig *config,
454 const char *error_msg, ...)
458 if (!value || !value[0])
461 tmp = strtol(value, &tailp, 0);
462 if (tmp < min || tmp > max)
465 if (tmp == INT_MIN || FFABS(tmp) > INT_MAX / FFABS(factor))
469 if (tailp[0] || errno)
477 va_start(vl, error_msg);
478 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
479 &config->errors, error_msg, vl);
482 return AVERROR(EINVAL);
485 static int ffserver_set_float_param(float *dest, const char *value,
486 float factor, float min, float max,
487 FFServerConfig *config,
488 const char *error_msg, ...)
492 if (!value || !value[0])
495 tmp = strtod(value, &tailp);
496 if (tmp < min || tmp > max)
500 if (tailp[0] || errno)
508 va_start(vl, error_msg);
509 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
510 &config->errors, error_msg, vl);
513 return AVERROR(EINVAL);
516 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
517 FFServerConfig *config)
519 static int hinted = 0;
521 AVDictionaryEntry *e;
522 const AVOption *o = NULL;
523 const char *option = NULL;
524 const char *codec_name = NULL;
528 enum AVCodecID guessed_codec_id;
531 case AV_OPT_FLAG_VIDEO_PARAM:
532 ctx = config->dummy_vctx;
533 dict = &config->video_opts;
534 guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
535 config->guessed_video_codec_id : AV_CODEC_ID_H264;
537 case AV_OPT_FLAG_AUDIO_PARAM:
538 ctx = config->dummy_actx;
539 dict = &config->audio_opts;
540 guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
541 config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
547 if (strchr(opt, ':')) {
548 //explicit private option
549 snprintf(buff, sizeof(buff), "%s", opt);
551 if(!(option = strchr(buff, ':'))){
552 report_config_error(config->filename, config->line_num,
553 AV_LOG_ERROR, &config->errors,
554 "Syntax error. Unmatched ':'\n");
558 buff[option - buff] = '\0';
560 if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
562 if (!ctx->codec || !ctx->priv_data)
568 o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM,
569 AV_OPT_SEARCH_CHILDREN);
571 (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
572 !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
573 o = av_opt_find(ctx, option, NULL, 0, 0);
575 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
576 &config->errors, "Option not found: '%s'\n", opt);
577 if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
579 report_config_error(config->filename, config->line_num,
580 AV_LOG_ERROR, NULL, "If '%s' is a codec private"
581 "option, then prefix it with codec name, for "
582 "example '%s:%s %s' or define codec earlier.\n",
583 opt, avcodec_get_name(guessed_codec_id) ,opt,
586 } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
587 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
588 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
589 arg, av_err2str(ret));
590 } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
591 if ((o->type == AV_OPT_TYPE_FLAGS) && arg &&
592 (arg[0] == '+' || arg[0] == '-'))
593 return av_dict_set(dict, option, arg, AV_DICT_APPEND);
594 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
595 &config->errors, "Redeclaring value of option '%s'."
596 "Previous value was: '%s'.\n", opt, e->value);
597 } else if (av_dict_set(dict, option, arg, 0) < 0) {
598 return AVERROR(ENOMEM);
603 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
604 int type, FFServerConfig *config)
607 snprintf(buf, sizeof(buf), "%"PRId64, arg);
608 return ffserver_save_avoption(opt, buf, type, config);
611 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
616 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
617 if (!av_strcasecmp(cmd, "Port"))
618 WARNING("Port option is deprecated. Use HTTPPort instead.\n");
619 ffserver_get_arg(arg, sizeof(arg), p);
620 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
621 "Invalid port: %s\n", arg);
623 WARNING("Trying to use IETF assigned system port: '%d'\n", val);
624 config->http_addr.sin_port = htons(val);
625 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") ||
626 !av_strcasecmp(cmd, "BindAddress")) {
627 if (!av_strcasecmp(cmd, "BindAddress"))
628 WARNING("BindAddress option is deprecated. Use HTTPBindAddress "
630 ffserver_get_arg(arg, sizeof(arg), p);
631 if (resolve_host(&config->http_addr.sin_addr, arg))
632 ERROR("Invalid host/IP address: '%s'\n", arg);
633 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
634 WARNING("NoDaemon option has no effect. You should remove it.\n");
635 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
636 ffserver_get_arg(arg, sizeof(arg), p);
637 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
638 "Invalid port: %s\n", arg);
639 config->rtsp_addr.sin_port = htons(val);
640 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
641 ffserver_get_arg(arg, sizeof(arg), p);
642 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
643 ERROR("Invalid host/IP address: %s\n", arg);
644 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
645 ffserver_get_arg(arg, sizeof(arg), p);
646 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
647 "Invalid MaxHTTPConnections: %s\n", arg);
648 config->nb_max_http_connections = val;
649 if (config->nb_max_connections > config->nb_max_http_connections) {
650 ERROR("Inconsistent configuration: MaxClients(%d) > "
651 "MaxHTTPConnections(%d)\n", config->nb_max_connections,
652 config->nb_max_http_connections);
654 } else if (!av_strcasecmp(cmd, "MaxClients")) {
655 ffserver_get_arg(arg, sizeof(arg), p);
656 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
657 "Invalid MaxClients: '%s'\n", arg);
658 config->nb_max_connections = val;
659 if (config->nb_max_connections > config->nb_max_http_connections) {
660 ERROR("Inconsistent configuration: MaxClients(%d) > "
661 "MaxHTTPConnections(%d)\n", config->nb_max_connections,
662 config->nb_max_http_connections);
664 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
667 ffserver_get_arg(arg, sizeof(arg), p);
669 llval = strtoll(arg, &tailp, 10);
670 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
671 ERROR("Invalid MaxBandwidth: '%s'\n", arg);
673 config->max_bandwidth = llval;
674 } else if (!av_strcasecmp(cmd, "CustomLog")) {
675 if (!config->debug) {
676 ffserver_get_arg(config->logfilename, sizeof(config->logfilename),
679 } else if (!av_strcasecmp(cmd, "LoadModule")) {
680 ERROR("Loadable modules are no longer supported\n");
681 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
682 config->use_defaults = 0;
683 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
684 config->use_defaults = 1;
686 ERROR("Incorrect keyword: '%s'\n", cmd);
690 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd,
691 const char **p, FFServerStream **pfeed)
693 FFServerStream *feed;
697 if (!av_strcasecmp(cmd, "<Feed")) {
700 feed = av_mallocz(sizeof(FFServerStream));
702 return AVERROR(ENOMEM);
703 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
704 q = strrchr(feed->filename, '>');
708 for (s = config->first_feed; s; s = s->next) {
709 if (!strcmp(feed->filename, s->filename))
710 ERROR("Feed '%s' already registered\n", s->filename);
713 feed->fmt = av_guess_format("ffm", NULL, NULL);
714 /* default feed file */
715 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
716 "/tmp/%s.ffm", feed->filename);
717 feed->feed_max_size = 5 * 1024 * 1024;
719 feed->feed = feed; /* self feeding :-) */
724 if (!av_strcasecmp(cmd, "Launch")) {
727 feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
728 if (!feed->child_argv)
729 return AVERROR(ENOMEM);
730 for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
731 ffserver_get_arg(arg, sizeof(arg), p);
735 feed->child_argv[i] = av_strdup(arg);
736 if (!feed->child_argv[i])
737 return AVERROR(ENOMEM);
740 feed->child_argv[i] =
741 av_asprintf("http://%s:%d/%s",
742 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ?
743 "127.0.0.1" : inet_ntoa(config->http_addr.sin_addr),
744 ntohs(config->http_addr.sin_port), feed->filename);
745 if (!feed->child_argv[i])
746 return AVERROR(ENOMEM);
747 } else if (!av_strcasecmp(cmd, "ACL")) {
748 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
750 } else if (!av_strcasecmp(cmd, "File") ||
751 !av_strcasecmp(cmd, "ReadOnlyFile")) {
752 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
753 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
754 } else if (!av_strcasecmp(cmd, "Truncate")) {
755 ffserver_get_arg(arg, sizeof(arg), p);
756 /* assume Truncate is true in case no argument is specified */
760 WARNING("Truncate N syntax in configuration file is deprecated. "
761 "Use Truncate alone with no arguments.\n");
762 feed->truncate = strtod(arg, NULL);
764 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
768 ffserver_get_arg(arg, sizeof(arg), p);
770 fsize = strtod(p1, &p1);
771 switch(av_toupper(*p1)) {
776 fsize *= 1024 * 1024;
779 fsize *= 1024 * 1024 * 1024;
782 ERROR("Invalid file size: '%s'\n", arg);
785 feed->feed_max_size = (int64_t)fsize;
786 if (feed->feed_max_size < FFM_PACKET_SIZE*4) {
787 ERROR("Feed max file size is too small. Must be at least %d.\n",
790 } else if (!av_strcasecmp(cmd, "</Feed>")) {
793 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
798 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd,
800 FFServerStream **pstream)
802 char arg[1024], arg2[1024];
803 FFServerStream *stream;
809 if (!av_strcasecmp(cmd, "<Stream")) {
812 stream = av_mallocz(sizeof(FFServerStream));
814 return AVERROR(ENOMEM);
815 config->dummy_actx = avcodec_alloc_context3(NULL);
816 config->dummy_vctx = avcodec_alloc_context3(NULL);
817 if (!config->dummy_vctx || !config->dummy_actx) {
819 avcodec_free_context(&config->dummy_vctx);
820 avcodec_free_context(&config->dummy_actx);
821 return AVERROR(ENOMEM);
823 config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
824 config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
825 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
826 q = strrchr(stream->filename, '>');
830 for (s = config->first_stream; s; s = s->next) {
831 if (!strcmp(stream->filename, s->filename))
832 ERROR("Stream '%s' already registered\n", s->filename);
835 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
837 config->guessed_audio_codec_id = stream->fmt->audio_codec;
838 config->guessed_video_codec_id = stream->fmt->video_codec;
840 config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
841 config->guessed_video_codec_id = AV_CODEC_ID_NONE;
843 config->stream_use_defaults = config->use_defaults;
848 if (!av_strcasecmp(cmd, "Feed")) {
849 FFServerStream *sfeed;
850 ffserver_get_arg(arg, sizeof(arg), p);
851 sfeed = config->first_feed;
853 if (!strcmp(sfeed->filename, arg))
855 sfeed = sfeed->next_feed;
858 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
861 stream->feed = sfeed;
862 } else if (!av_strcasecmp(cmd, "Format")) {
863 ffserver_get_arg(arg, sizeof(arg), p);
864 if (!strcmp(arg, "status")) {
865 stream->stream_type = STREAM_TYPE_STATUS;
868 stream->stream_type = STREAM_TYPE_LIVE;
869 /* JPEG cannot be used here, so use single frame MJPEG */
870 if (!strcmp(arg, "jpeg")) {
871 strcpy(arg, "singlejpeg");
872 stream->single_frame=1;
874 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
876 ERROR("Unknown Format: '%s'\n", arg);
879 config->guessed_audio_codec_id = stream->fmt->audio_codec;
880 config->guessed_video_codec_id = stream->fmt->video_codec;
882 } else if (!av_strcasecmp(cmd, "InputFormat")) {
883 ffserver_get_arg(arg, sizeof(arg), p);
884 stream->ifmt = av_find_input_format(arg);
886 ERROR("Unknown input format: '%s'\n", arg);
887 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
888 if (stream->stream_type == STREAM_TYPE_STATUS)
889 ffserver_get_arg(stream->feed_filename,
890 sizeof(stream->feed_filename), p);
892 ERROR("FaviconURL only permitted for status streams\n");
893 } else if (!av_strcasecmp(cmd, "Author") ||
894 !av_strcasecmp(cmd, "Comment") ||
895 !av_strcasecmp(cmd, "Copyright") ||
896 !av_strcasecmp(cmd, "Title")) {
899 ffserver_get_arg(arg, sizeof(arg), p);
900 for (i = 0; i < strlen(cmd); i++)
901 key[i] = av_tolower(cmd[i]);
903 WARNING("Deprecated '%s' option in configuration file. Use "
904 "'Metadata %s VALUE' instead.\n", cmd, key);
905 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
907 } else if (!av_strcasecmp(cmd, "Metadata")) {
908 ffserver_get_arg(arg, sizeof(arg), p);
909 ffserver_get_arg(arg2, sizeof(arg2), p);
910 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
912 } else if (!av_strcasecmp(cmd, "Preroll")) {
913 ffserver_get_arg(arg, sizeof(arg), p);
914 stream->prebuffer = atof(arg) * 1000;
915 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
916 stream->send_on_key = 1;
917 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
918 ffserver_get_arg(arg, sizeof(arg), p);
919 ffserver_set_codec(config->dummy_actx, arg, config);
920 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
921 ffserver_get_arg(arg, sizeof(arg), p);
922 ffserver_set_codec(config->dummy_vctx, arg, config);
923 } else if (!av_strcasecmp(cmd, "MaxTime")) {
924 ffserver_get_arg(arg, sizeof(arg), p);
925 stream->max_time = atof(arg) * 1000;
926 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
928 ffserver_get_arg(arg, sizeof(arg), p);
929 ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
930 "Invalid %s: '%s'\n", cmd, arg);
931 if (ffserver_save_avoption_int("b", (int64_t)lrintf(f),
932 AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
934 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
935 ffserver_get_arg(arg, sizeof(arg), p);
936 if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
938 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
939 ffserver_get_arg(arg, sizeof(arg), p);
940 if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
942 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
943 int minrate, maxrate;
945 ffserver_get_arg(arg, sizeof(arg), p);
946 dash = strchr(arg, '-');
950 if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0 &&
951 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0) {
952 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
953 ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
957 ERROR("Incorrect format for VideoBitRateRange. It should be "
958 "<min>-<max>: '%s'.\n", arg);
959 } else if (!av_strcasecmp(cmd, "Debug")) {
960 ffserver_get_arg(arg, sizeof(arg), p);
961 if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
962 ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
964 } else if (!av_strcasecmp(cmd, "Strict")) {
965 ffserver_get_arg(arg, sizeof(arg), p);
966 if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
967 ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
969 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
970 ffserver_get_arg(arg, sizeof(arg), p);
971 ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
972 "Invalid %s: '%s'", cmd, arg);
973 if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
975 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
976 ffserver_get_arg(arg, sizeof(arg), p);
977 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
978 "Invalid %s: '%s'", cmd, arg);
979 if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
981 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
982 ffserver_get_arg(arg, sizeof(arg), p);
983 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
984 "Invalid %s: '%s'", cmd, arg);
985 if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
987 } else if (!av_strcasecmp(cmd, "VideoSize")) {
989 ffserver_get_arg(arg, sizeof(arg), p);
990 ret = av_parse_video_size(&w, &h, arg);
992 ERROR("Invalid video size '%s'\n", arg);
995 WARNING("Image size is not a multiple of 2\n");
996 if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
999 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
1000 ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
1001 arg[0] = '1'; arg[1] = '/';
1002 if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1004 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
1005 enum AVPixelFormat pix_fmt;
1006 ffserver_get_arg(arg, sizeof(arg), p);
1007 pix_fmt = av_get_pix_fmt(arg);
1008 if (pix_fmt == AV_PIX_FMT_NONE)
1009 ERROR("Unknown pixel format: '%s'\n", arg);
1010 else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1012 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
1013 ffserver_get_arg(arg, sizeof(arg), p);
1014 if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1016 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
1017 if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1019 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
1020 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1022 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
1023 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
1024 ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1026 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
1027 !av_strcasecmp(cmd, "AVOptionAudio")) {
1029 ffserver_get_arg(arg, sizeof(arg), p);
1030 ffserver_get_arg(arg2, sizeof(arg2), p);
1031 if (!av_strcasecmp(cmd, "AVOptionVideo"))
1032 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM,
1035 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM,
1039 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
1040 !av_strcasecmp(cmd, "AVPresetAudio")) {
1041 ffserver_get_arg(arg, sizeof(arg), p);
1042 if (!av_strcasecmp(cmd, "AVPresetVideo"))
1043 ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
1045 ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
1046 } else if (!av_strcasecmp(cmd, "VideoTag")) {
1047 ffserver_get_arg(arg, sizeof(arg), p);
1048 if (strlen(arg) == 4 &&
1049 ffserver_save_avoption_int("codec_tag",
1050 MKTAG(arg[0], arg[1], arg[2], arg[3]),
1051 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1053 } else if (!av_strcasecmp(cmd, "BitExact")) {
1054 config->bitexact = 1;
1055 if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1057 } else if (!av_strcasecmp(cmd, "DctFastint")) {
1058 if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1060 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1061 if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1063 } else if (!av_strcasecmp(cmd, "Qscale")) {
1064 ffserver_get_arg(arg, sizeof(arg), p);
1065 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1066 "Invalid Qscale: '%s'\n", arg);
1067 if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1068 ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1069 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1071 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1072 ffserver_get_arg(arg, sizeof(arg), p);
1073 if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1075 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1076 ffserver_get_arg(arg, sizeof(arg), p);
1077 if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1079 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1080 ffserver_get_arg(arg, sizeof(arg), p);
1081 if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1083 } else if (!av_strcasecmp(cmd, "LumiMask")) {
1084 ffserver_get_arg(arg, sizeof(arg), p);
1085 if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1087 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1088 ffserver_get_arg(arg, sizeof(arg), p);
1089 if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1091 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1092 config->no_video = 1;
1093 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1094 config->no_audio = 1;
1095 } else if (!av_strcasecmp(cmd, "ACL")) {
1096 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1098 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1099 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1100 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1101 ffserver_get_arg(arg, sizeof(arg), p);
1102 av_freep(&stream->rtsp_option);
1103 stream->rtsp_option = av_strdup(arg);
1104 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1105 ffserver_get_arg(arg, sizeof(arg), p);
1106 if (resolve_host(&stream->multicast_ip, arg))
1107 ERROR("Invalid host/IP address: '%s'\n", arg);
1108 stream->is_multicast = 1;
1109 stream->loop = 1; /* default is looping */
1110 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1111 ffserver_get_arg(arg, sizeof(arg), p);
1112 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1113 "Invalid MulticastPort: '%s'\n", arg);
1114 stream->multicast_port = val;
1115 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1116 ffserver_get_arg(arg, sizeof(arg), p);
1117 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1118 "Invalid MulticastTTL: '%s'\n", arg);
1119 stream->multicast_ttl = val;
1120 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1122 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1123 config->stream_use_defaults &= 1;
1124 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1125 if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1126 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1127 if (!config->no_audio &&
1128 config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1129 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1130 add_codec(stream, audio_enc, config);
1132 if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1133 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1134 if (!config->no_video &&
1135 config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1136 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1137 add_codec(stream, video_enc, config);
1140 av_dict_free(&config->video_opts);
1141 av_dict_free(&config->audio_opts);
1142 avcodec_free_context(&config->dummy_vctx);
1143 avcodec_free_context(&config->dummy_actx);
1144 config->no_video = 0;
1145 config->no_audio = 0;
1147 } else if (!av_strcasecmp(cmd, "File") ||
1148 !av_strcasecmp(cmd, "ReadOnlyFile")) {
1149 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1151 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1152 if (config->stream_use_defaults > 1)
1153 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1154 config->stream_use_defaults = 3;
1155 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1156 if (config->stream_use_defaults > 1)
1157 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1158 config->stream_use_defaults = 2;
1160 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1164 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1165 av_dict_free(&config->video_opts);
1166 av_dict_free(&config->audio_opts);
1167 avcodec_free_context(&config->dummy_vctx);
1168 avcodec_free_context(&config->dummy_actx);
1169 return AVERROR(ENOMEM);
1172 static int ffserver_parse_config_redirect(FFServerConfig *config,
1173 const char *cmd, const char **p,
1174 FFServerStream **predirect)
1176 FFServerStream *redirect;
1177 av_assert0(predirect);
1178 redirect = *predirect;
1180 if (!av_strcasecmp(cmd, "<Redirect")) {
1182 redirect = av_mallocz(sizeof(FFServerStream));
1184 return AVERROR(ENOMEM);
1186 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1187 q = strrchr(redirect->filename, '>');
1190 redirect->stream_type = STREAM_TYPE_REDIRECT;
1191 *predirect = redirect;
1194 av_assert0(redirect);
1195 if (!av_strcasecmp(cmd, "URL")) {
1196 ffserver_get_arg(redirect->feed_filename,
1197 sizeof(redirect->feed_filename), p);
1198 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1199 if (!redirect->feed_filename[0])
1200 ERROR("No URL found for <Redirect>\n");
1203 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1208 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1214 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1215 FFServerStream **last_feed, *feed = NULL;
1220 f = fopen(filename, "r");
1222 ret = AVERROR(errno);
1223 av_log(NULL, AV_LOG_ERROR,
1224 "Could not open the configuration file '%s'\n", filename);
1228 config->first_stream = NULL;
1229 config->first_feed = NULL;
1230 config->errors = config->warnings = 0;
1232 last_stream = &config->first_stream;
1233 last_feed = &config->first_feed;
1235 config->line_num = 0;
1236 while (fgets(line, sizeof(line), f) != NULL) {
1239 while (av_isspace(*p))
1241 if (*p == '\0' || *p == '#')
1244 ffserver_get_arg(cmd, sizeof(cmd), &p);
1246 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1247 int opening = !av_strcasecmp(cmd, "<Feed");
1248 if (opening && (stream || feed || redirect)) {
1249 ERROR("Already in a tag\n");
1251 ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
1255 /* add in stream & feed list */
1256 *last_stream = feed;
1258 last_stream = &feed->next;
1259 last_feed = &feed->next_feed;
1262 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1263 int opening = !av_strcasecmp(cmd, "<Stream");
1264 if (opening && (stream || feed || redirect)) {
1265 ERROR("Already in a tag\n");
1267 ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
1271 /* add in stream list */
1272 *last_stream = stream;
1273 last_stream = &stream->next;
1276 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1277 int opening = !av_strcasecmp(cmd, "<Redirect");
1278 if (opening && (stream || feed || redirect))
1279 ERROR("Already in a tag\n");
1281 ret = ffserver_parse_config_redirect(config, cmd, &p,
1286 /* add in stream list */
1287 *last_stream = redirect;
1288 last_stream = &redirect->next;
1292 ffserver_parse_config_global(config, cmd, &p);
1295 if (stream || feed || redirect)
1296 ERROR("Missing closing </%s> tag\n",
1297 stream ? "Stream" : (feed ? "Feed" : "Redirect"));
1303 return AVERROR(EINVAL);
1311 void ffserver_free_child_args(void *argsp)
1317 args = *(char ***)argsp;
1320 for (i = 0; i < MAX_CHILD_ARGS; i++)