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;
119 FFServerIPAddressACL *nacl;
120 FFServerIPAddressACL **naclp;
122 ffserver_get_arg(arg, sizeof(arg), &p);
123 if (av_strcasecmp(arg, "allow") == 0)
124 acl.action = IP_ALLOW;
125 else if (av_strcasecmp(arg, "deny") == 0)
126 acl.action = IP_DENY;
128 fprintf(stderr, "%s:%d: ACL action '%s' should be ALLOW or DENY.\n",
129 filename, line_num, arg);
133 ffserver_get_arg(arg, sizeof(arg), &p);
135 if (resolve_host(&acl.first, arg)) {
137 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
138 filename, line_num, arg);
142 acl.last = acl.first;
144 ffserver_get_arg(arg, sizeof(arg), &p);
147 if (resolve_host(&acl.last, arg)) {
149 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
150 filename, line_num, arg);
155 nacl = av_mallocz(sizeof(*nacl));
162 naclp = &stream->acl;
168 fprintf(stderr, "%s:%d: ACL found not in <Stream> or <Feed>\n",
173 naclp = &(*naclp)->next;
184 /* add a codec and set the default parameters */
185 static void add_codec(FFServerStream *stream, AVCodecContext *av,
186 FFServerConfig *config)
189 AVDictionary **opts, *recommended = NULL;
192 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
195 opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
196 &config->audio_opts : &config->video_opts;
197 av_dict_copy(&recommended, *opts, 0);
198 av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
199 av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
201 if (av_dict_count(*opts))
202 av_log(NULL, AV_LOG_WARNING,
203 "Something is wrong, %d options are not set!\n",
204 av_dict_count(*opts));
206 if (!config->stream_use_defaults) {
207 switch(av->codec_type) {
208 case AVMEDIA_TYPE_AUDIO:
209 if (av->bit_rate == 0)
210 report_config_error(config->filename, config->line_num,
211 AV_LOG_ERROR, &config->errors,
212 "audio bit rate is not set\n");
213 if (av->sample_rate == 0)
214 report_config_error(config->filename, config->line_num,
215 AV_LOG_ERROR, &config->errors,
216 "audio sample rate is not set\n");
218 case AVMEDIA_TYPE_VIDEO:
219 if (av->width == 0 || av->height == 0)
220 report_config_error(config->filename, config->line_num,
221 AV_LOG_ERROR, &config->errors,
222 "video size is not set\n");
230 /* stream_use_defaults = true */
232 /* compute default parameters */
233 switch(av->codec_type) {
234 case AVMEDIA_TYPE_AUDIO:
235 if (!av_dict_get(recommended, "b", NULL, 0)) {
236 av->bit_rate = 64000;
237 av_dict_set_int(&recommended, "b", av->bit_rate, 0);
238 WARNING("Setting default value for audio bit rate = %d. "
239 "Use NoDefaults to disable it.\n",
242 if (!av_dict_get(recommended, "ar", NULL, 0)) {
243 av->sample_rate = 22050;
244 av_dict_set_int(&recommended, "ar", av->sample_rate, 0);
245 WARNING("Setting default value for audio sample rate = %d. "
246 "Use NoDefaults to disable it.\n",
249 if (!av_dict_get(recommended, "ac", NULL, 0)) {
251 av_dict_set_int(&recommended, "ac", av->channels, 0);
252 WARNING("Setting default value for audio channel count = %d. "
253 "Use NoDefaults to disable it.\n",
257 case AVMEDIA_TYPE_VIDEO:
258 if (!av_dict_get(recommended, "b", NULL, 0)) {
259 av->bit_rate = 64000;
260 av_dict_set_int(&recommended, "b", av->bit_rate, 0);
261 WARNING("Setting default value for video bit rate = %d. "
262 "Use NoDefaults to disable it.\n",
265 if (!av_dict_get(recommended, "time_base", NULL, 0)){
266 av->time_base.den = 5;
267 av->time_base.num = 1;
268 av_dict_set(&recommended, "time_base", "1/5", 0);
269 WARNING("Setting default value for video frame rate = %d. "
270 "Use NoDefaults to disable it.\n",
273 if (!av_dict_get(recommended, "video_size", NULL, 0)) {
276 av_dict_set(&recommended, "video_size", "160x128", 0);
277 WARNING("Setting default value for video size = %dx%d. "
278 "Use NoDefaults to disable it.\n",
279 av->width, av->height);
281 /* Bitrate tolerance is less for streaming */
282 if (!av_dict_get(recommended, "bt", NULL, 0)) {
283 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
284 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
285 av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0);
286 WARNING("Setting default value for video bit rate tolerance = %d. "
287 "Use NoDefaults to disable it.\n",
288 av->bit_rate_tolerance);
291 if (!av_dict_get(recommended, "rc_eq", NULL, 0)) {
292 av->rc_eq = av_strdup("tex^qComp");
293 av_dict_set(&recommended, "rc_eq", "tex^qComp", 0);
294 WARNING("Setting default value for video rate control equation = "
295 "%s. 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(AVStream));
322 av_dict_get_string(recommended, &enc_config, '=', ',');
323 av_dict_free(&recommended);
324 av_stream_set_recommended_encoder_configuration(st, enc_config);
326 stream->streams[stream->nb_streams++] = st;
329 static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name,
330 FFServerConfig *config)
333 AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
334 if (!codec || codec->type != ctx->codec_type) {
335 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
337 "Invalid codec name: '%s'\n", codec_name);
340 if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
341 if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
345 if (ctx->codec_id != codec->id)
346 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
348 "Inconsistent configuration: trying to set '%s' "
349 "codec option, but '%s' codec is used previously\n",
350 codec_name, avcodec_get_name(ctx->codec_id));
354 static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
357 char filename[1000], tmp[1000], tmp2[1000], line[1000];
359 AVCodecContext *avctx;
360 const AVCodec *codec;
363 case AV_OPT_FLAG_AUDIO_PARAM:
364 avctx = config->dummy_actx;
366 case AV_OPT_FLAG_VIDEO_PARAM:
367 avctx = config->dummy_vctx;
372 codec = avcodec_find_encoder(avctx->codec_id);
374 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
375 codec ? codec->name : NULL))) {
376 av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
377 return AVERROR(EINVAL);
381 int e= fscanf(f, "%999[^\n]\n", line) - 1;
382 if(line[0] == '#' && !e)
384 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
386 av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename,
388 ret = AVERROR(EINVAL);
391 if (!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO ||
392 !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
394 if (ffserver_set_codec(avctx, tmp2, config) < 0)
396 } else if (!strcmp(tmp, "scodec")) {
397 av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
398 ret = AVERROR(EINVAL);
400 } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
409 static AVOutputFormat *ffserver_guess_format(const char *short_name,
410 const char *filename,
411 const char *mime_type)
413 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
416 AVOutputFormat *stream_fmt;
417 char stream_format_name[64];
419 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
421 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
430 static void vreport_config_error(const char *filename, int line_num,
431 int log_level, int *errors, const char *fmt,
434 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
435 av_vlog(NULL, log_level, fmt, vl);
440 static void report_config_error(const char *filename, int line_num,
441 int log_level, int *errors,
442 const char *fmt, ...)
446 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
450 static int ffserver_set_int_param(int *dest, const char *value, int factor,
451 int min, int max, FFServerConfig *config,
452 const char *error_msg, ...)
456 if (!value || !value[0])
459 tmp = strtol(value, &tailp, 0);
460 if (tmp < min || tmp > max)
463 if (tmp == INT_MIN || FFABS(tmp) > INT_MAX / FFABS(factor))
467 if (tailp[0] || errno)
475 va_start(vl, error_msg);
476 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
477 &config->errors, error_msg, vl);
480 return AVERROR(EINVAL);
483 static int ffserver_set_float_param(float *dest, const char *value,
484 float factor, float min, float max,
485 FFServerConfig *config,
486 const char *error_msg, ...)
490 if (!value || !value[0])
493 tmp = strtod(value, &tailp);
494 if (tmp < min || tmp > max)
498 if (tailp[0] || errno)
506 va_start(vl, error_msg);
507 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
508 &config->errors, error_msg, vl);
511 return AVERROR(EINVAL);
514 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
515 FFServerConfig *config)
517 static int hinted = 0;
519 AVDictionaryEntry *e;
520 const AVOption *o = NULL;
521 const char *option = NULL;
522 const char *codec_name = NULL;
526 enum AVCodecID guessed_codec_id;
529 case AV_OPT_FLAG_VIDEO_PARAM:
530 ctx = config->dummy_vctx;
531 dict = &config->video_opts;
532 guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
533 config->guessed_video_codec_id : AV_CODEC_ID_H264;
535 case AV_OPT_FLAG_AUDIO_PARAM:
536 ctx = config->dummy_actx;
537 dict = &config->audio_opts;
538 guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
539 config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
545 if (strchr(opt, ':')) {
546 //explicit private option
547 snprintf(buff, sizeof(buff), "%s", opt);
549 if(!(option = strchr(buff, ':'))){
550 report_config_error(config->filename, config->line_num,
551 AV_LOG_ERROR, &config->errors,
552 "Syntax error. Unmatched ':'\n");
556 buff[option - buff] = '\0';
558 if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
560 if (!ctx->codec || !ctx->priv_data)
566 o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM,
567 AV_OPT_SEARCH_CHILDREN);
569 (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
570 !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
571 o = av_opt_find(ctx, option, NULL, 0, 0);
573 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
574 &config->errors, "Option not found: '%s'\n", opt);
575 if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
577 report_config_error(config->filename, config->line_num,
578 AV_LOG_ERROR, NULL, "If '%s' is a codec private"
579 "option, then prefix it with codec name, for "
580 "example '%s:%s %s' or define codec earlier.\n",
581 opt, avcodec_get_name(guessed_codec_id) ,opt,
584 } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
585 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
586 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
587 arg, av_err2str(ret));
588 } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
589 if ((o->type == AV_OPT_TYPE_FLAGS) && arg &&
590 (arg[0] == '+' || arg[0] == '-'))
591 return av_dict_set(dict, option, arg, AV_DICT_APPEND);
592 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
593 &config->errors, "Redeclaring value of option '%s'."
594 "Previous value was: '%s'.\n", opt, e->value);
595 } else if (av_dict_set(dict, option, arg, 0) < 0) {
596 return AVERROR(ENOMEM);
601 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
602 int type, FFServerConfig *config)
605 snprintf(buf, sizeof(buf), "%"PRId64, arg);
606 return ffserver_save_avoption(opt, buf, type, config);
609 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
614 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
615 if (!av_strcasecmp(cmd, "Port"))
616 WARNING("Port option is deprecated. Use HTTPPort instead.\n");
617 ffserver_get_arg(arg, sizeof(arg), p);
618 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
619 "Invalid port: %s\n", arg);
621 WARNING("Trying to use IETF assigned system port: '%d'\n", val);
622 config->http_addr.sin_port = htons(val);
623 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") ||
624 !av_strcasecmp(cmd, "BindAddress")) {
625 if (!av_strcasecmp(cmd, "BindAddress"))
626 WARNING("BindAddress option is deprecated. Use HTTPBindAddress "
628 ffserver_get_arg(arg, sizeof(arg), p);
629 if (resolve_host(&config->http_addr.sin_addr, arg))
630 ERROR("Invalid host/IP address: '%s'\n", arg);
631 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
632 WARNING("NoDaemon option has no effect. You should remove it.\n");
633 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
634 ffserver_get_arg(arg, sizeof(arg), p);
635 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
636 "Invalid port: %s\n", arg);
637 config->rtsp_addr.sin_port = htons(val);
638 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
639 ffserver_get_arg(arg, sizeof(arg), p);
640 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
641 ERROR("Invalid host/IP address: %s\n", arg);
642 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
643 ffserver_get_arg(arg, sizeof(arg), p);
644 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
645 "Invalid MaxHTTPConnections: %s\n", arg);
646 config->nb_max_http_connections = val;
647 if (config->nb_max_connections > config->nb_max_http_connections) {
648 ERROR("Inconsistent configuration: MaxClients(%d) > "
649 "MaxHTTPConnections(%d)\n", config->nb_max_connections,
650 config->nb_max_http_connections);
652 } else if (!av_strcasecmp(cmd, "MaxClients")) {
653 ffserver_get_arg(arg, sizeof(arg), p);
654 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
655 "Invalid MaxClients: '%s'\n", arg);
656 config->nb_max_connections = val;
657 if (config->nb_max_connections > config->nb_max_http_connections) {
658 ERROR("Inconsistent configuration: MaxClients(%d) > "
659 "MaxHTTPConnections(%d)\n", config->nb_max_connections,
660 config->nb_max_http_connections);
662 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
665 ffserver_get_arg(arg, sizeof(arg), p);
667 llval = strtoll(arg, &tailp, 10);
668 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
669 ERROR("Invalid MaxBandwidth: '%s'\n", arg);
671 config->max_bandwidth = llval;
672 } else if (!av_strcasecmp(cmd, "CustomLog")) {
673 if (!config->debug) {
674 ffserver_get_arg(config->logfilename, sizeof(config->logfilename),
677 } else if (!av_strcasecmp(cmd, "LoadModule")) {
678 ERROR("Loadable modules are no longer supported\n");
679 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
680 config->use_defaults = 0;
681 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
682 config->use_defaults = 1;
684 ERROR("Incorrect keyword: '%s'\n", cmd);
688 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd,
689 const char **p, FFServerStream **pfeed)
691 FFServerStream *feed;
695 if (!av_strcasecmp(cmd, "<Feed")) {
698 feed = av_mallocz(sizeof(FFServerStream));
700 return AVERROR(ENOMEM);
701 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
702 q = strrchr(feed->filename, '>');
706 for (s = config->first_feed; s; s = s->next) {
707 if (!strcmp(feed->filename, s->filename))
708 ERROR("Feed '%s' already registered\n", s->filename);
711 feed->fmt = av_guess_format("ffm", NULL, NULL);
712 /* default feed file */
713 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
714 "/tmp/%s.ffm", feed->filename);
715 feed->feed_max_size = 5 * 1024 * 1024;
717 feed->feed = feed; /* self feeding :-) */
722 if (!av_strcasecmp(cmd, "Launch")) {
725 feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
726 if (!feed->child_argv)
727 return AVERROR(ENOMEM);
728 for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
729 ffserver_get_arg(arg, sizeof(arg), p);
733 feed->child_argv[i] = av_strdup(arg);
734 if (!feed->child_argv[i])
735 return AVERROR(ENOMEM);
738 feed->child_argv[i] =
739 av_asprintf("http://%s:%d/%s",
740 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ?
741 "127.0.0.1" : inet_ntoa(config->http_addr.sin_addr),
742 ntohs(config->http_addr.sin_port), feed->filename);
743 if (!feed->child_argv[i])
744 return AVERROR(ENOMEM);
745 } else if (!av_strcasecmp(cmd, "ACL")) {
746 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
748 } else if (!av_strcasecmp(cmd, "File") ||
749 !av_strcasecmp(cmd, "ReadOnlyFile")) {
750 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
751 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
752 } else if (!av_strcasecmp(cmd, "Truncate")) {
753 ffserver_get_arg(arg, sizeof(arg), p);
754 /* assume Truncate is true in case no argument is specified */
758 WARNING("Truncate N syntax in configuration file is deprecated. "
759 "Use Truncate alone with no arguments.\n");
760 feed->truncate = strtod(arg, NULL);
762 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
766 ffserver_get_arg(arg, sizeof(arg), p);
768 fsize = strtod(p1, &p1);
769 switch(av_toupper(*p1)) {
774 fsize *= 1024 * 1024;
777 fsize *= 1024 * 1024 * 1024;
780 ERROR("Invalid file size: '%s'\n", arg);
783 feed->feed_max_size = (int64_t)fsize;
784 if (feed->feed_max_size < FFM_PACKET_SIZE*4) {
785 ERROR("Feed max file size is too small. Must be at least %d.\n",
788 } else if (!av_strcasecmp(cmd, "</Feed>")) {
791 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
796 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd,
798 FFServerStream **pstream)
800 char arg[1024], arg2[1024];
801 FFServerStream *stream;
807 if (!av_strcasecmp(cmd, "<Stream")) {
810 stream = av_mallocz(sizeof(FFServerStream));
812 return AVERROR(ENOMEM);
813 config->dummy_actx = avcodec_alloc_context3(NULL);
814 config->dummy_vctx = avcodec_alloc_context3(NULL);
815 if (!config->dummy_vctx || !config->dummy_actx) {
817 avcodec_free_context(&config->dummy_vctx);
818 avcodec_free_context(&config->dummy_actx);
819 return AVERROR(ENOMEM);
821 config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
822 config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
823 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
824 q = strrchr(stream->filename, '>');
828 for (s = config->first_stream; s; s = s->next) {
829 if (!strcmp(stream->filename, s->filename))
830 ERROR("Stream '%s' already registered\n", s->filename);
833 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
835 config->guessed_audio_codec_id = stream->fmt->audio_codec;
836 config->guessed_video_codec_id = stream->fmt->video_codec;
838 config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
839 config->guessed_video_codec_id = AV_CODEC_ID_NONE;
841 config->stream_use_defaults = config->use_defaults;
846 if (!av_strcasecmp(cmd, "Feed")) {
847 FFServerStream *sfeed;
848 ffserver_get_arg(arg, sizeof(arg), p);
849 sfeed = config->first_feed;
851 if (!strcmp(sfeed->filename, arg))
853 sfeed = sfeed->next_feed;
856 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
859 stream->feed = sfeed;
860 } else if (!av_strcasecmp(cmd, "Format")) {
861 ffserver_get_arg(arg, sizeof(arg), p);
862 if (!strcmp(arg, "status")) {
863 stream->stream_type = STREAM_TYPE_STATUS;
866 stream->stream_type = STREAM_TYPE_LIVE;
867 /* JPEG cannot be used here, so use single frame MJPEG */
868 if (!strcmp(arg, "jpeg")) {
869 strcpy(arg, "singlejpeg");
870 stream->single_frame=1;
872 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
874 ERROR("Unknown Format: '%s'\n", arg);
877 config->guessed_audio_codec_id = stream->fmt->audio_codec;
878 config->guessed_video_codec_id = stream->fmt->video_codec;
880 } else if (!av_strcasecmp(cmd, "InputFormat")) {
881 ffserver_get_arg(arg, sizeof(arg), p);
882 stream->ifmt = av_find_input_format(arg);
884 ERROR("Unknown input format: '%s'\n", arg);
885 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
886 if (stream->stream_type == STREAM_TYPE_STATUS)
887 ffserver_get_arg(stream->feed_filename,
888 sizeof(stream->feed_filename), p);
890 ERROR("FaviconURL only permitted for status streams\n");
891 } else if (!av_strcasecmp(cmd, "Author") ||
892 !av_strcasecmp(cmd, "Comment") ||
893 !av_strcasecmp(cmd, "Copyright") ||
894 !av_strcasecmp(cmd, "Title")) {
897 ffserver_get_arg(arg, sizeof(arg), p);
898 for (i = 0; i < strlen(cmd); i++)
899 key[i] = av_tolower(cmd[i]);
901 WARNING("Deprecated '%s' option in configuration file. Use "
902 "'Metadata %s VALUE' instead.\n", cmd, key);
903 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
905 } else if (!av_strcasecmp(cmd, "Metadata")) {
906 ffserver_get_arg(arg, sizeof(arg), p);
907 ffserver_get_arg(arg2, sizeof(arg2), p);
908 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
910 } else if (!av_strcasecmp(cmd, "Preroll")) {
911 ffserver_get_arg(arg, sizeof(arg), p);
912 stream->prebuffer = atof(arg) * 1000;
913 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
914 stream->send_on_key = 1;
915 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
916 ffserver_get_arg(arg, sizeof(arg), p);
917 ffserver_set_codec(config->dummy_actx, arg, config);
918 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
919 ffserver_get_arg(arg, sizeof(arg), p);
920 ffserver_set_codec(config->dummy_vctx, arg, config);
921 } else if (!av_strcasecmp(cmd, "MaxTime")) {
922 ffserver_get_arg(arg, sizeof(arg), p);
923 stream->max_time = atof(arg) * 1000;
924 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
926 ffserver_get_arg(arg, sizeof(arg), p);
927 ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
928 "Invalid %s: '%s'\n", cmd, arg);
929 if (ffserver_save_avoption_int("b", (int64_t)lrintf(f),
930 AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
932 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
933 ffserver_get_arg(arg, sizeof(arg), p);
934 if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
936 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
937 ffserver_get_arg(arg, sizeof(arg), p);
938 if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
940 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
941 int minrate, maxrate;
943 ffserver_get_arg(arg, sizeof(arg), p);
944 dash = strchr(arg, '-');
948 if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0 &&
949 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0) {
950 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
951 ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
955 ERROR("Incorrect format for VideoBitRateRange. It should be "
956 "<min>-<max>: '%s'.\n", arg);
957 } else if (!av_strcasecmp(cmd, "Debug")) {
958 ffserver_get_arg(arg, sizeof(arg), p);
959 if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
960 ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
962 } else if (!av_strcasecmp(cmd, "Strict")) {
963 ffserver_get_arg(arg, sizeof(arg), p);
964 if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
965 ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
967 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
968 ffserver_get_arg(arg, sizeof(arg), p);
969 ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
970 "Invalid %s: '%s'", cmd, arg);
971 if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
973 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
974 ffserver_get_arg(arg, sizeof(arg), p);
975 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
976 "Invalid %s: '%s'", cmd, arg);
977 if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
979 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
980 ffserver_get_arg(arg, sizeof(arg), p);
981 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
982 "Invalid %s: '%s'", cmd, arg);
983 if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
985 } else if (!av_strcasecmp(cmd, "VideoSize")) {
987 ffserver_get_arg(arg, sizeof(arg), p);
988 ret = av_parse_video_size(&w, &h, arg);
990 ERROR("Invalid video size '%s'\n", arg);
993 WARNING("Image size is not a multiple of 2\n");
994 if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
997 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
998 ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
999 arg[0] = '1'; arg[1] = '/';
1000 if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1002 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
1003 enum AVPixelFormat pix_fmt;
1004 ffserver_get_arg(arg, sizeof(arg), p);
1005 pix_fmt = av_get_pix_fmt(arg);
1006 if (pix_fmt == AV_PIX_FMT_NONE)
1007 ERROR("Unknown pixel format: '%s'\n", arg);
1008 else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1010 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
1011 ffserver_get_arg(arg, sizeof(arg), p);
1012 if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1014 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
1015 if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1017 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
1018 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1020 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
1021 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
1022 ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1024 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
1025 !av_strcasecmp(cmd, "AVOptionAudio")) {
1027 ffserver_get_arg(arg, sizeof(arg), p);
1028 ffserver_get_arg(arg2, sizeof(arg2), p);
1029 if (!av_strcasecmp(cmd, "AVOptionVideo"))
1030 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM,
1033 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM,
1037 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
1038 !av_strcasecmp(cmd, "AVPresetAudio")) {
1039 ffserver_get_arg(arg, sizeof(arg), p);
1040 if (!av_strcasecmp(cmd, "AVPresetVideo"))
1041 ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
1043 ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
1044 } else if (!av_strcasecmp(cmd, "VideoTag")) {
1045 ffserver_get_arg(arg, sizeof(arg), p);
1046 if (strlen(arg) == 4 &&
1047 ffserver_save_avoption_int("codec_tag",
1048 MKTAG(arg[0], arg[1], arg[2], arg[3]),
1049 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1051 } else if (!av_strcasecmp(cmd, "BitExact")) {
1052 if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1054 } else if (!av_strcasecmp(cmd, "DctFastint")) {
1055 if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1057 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1058 if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1060 } else if (!av_strcasecmp(cmd, "Qscale")) {
1061 ffserver_get_arg(arg, sizeof(arg), p);
1062 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1063 "Invalid Qscale: '%s'\n", arg);
1064 if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1065 ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1066 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1068 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1069 ffserver_get_arg(arg, sizeof(arg), p);
1070 if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1072 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1073 ffserver_get_arg(arg, sizeof(arg), p);
1074 if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1076 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1077 ffserver_get_arg(arg, sizeof(arg), p);
1078 if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1080 } else if (!av_strcasecmp(cmd, "LumiMask")) {
1081 ffserver_get_arg(arg, sizeof(arg), p);
1082 if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1084 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1085 ffserver_get_arg(arg, sizeof(arg), p);
1086 if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1088 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1089 config->no_video = 1;
1090 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1091 config->no_audio = 1;
1092 } else if (!av_strcasecmp(cmd, "ACL")) {
1093 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1095 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1096 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1097 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1098 ffserver_get_arg(arg, sizeof(arg), p);
1099 av_freep(&stream->rtsp_option);
1100 stream->rtsp_option = av_strdup(arg);
1101 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1102 ffserver_get_arg(arg, sizeof(arg), p);
1103 if (resolve_host(&stream->multicast_ip, arg))
1104 ERROR("Invalid host/IP address: '%s'\n", arg);
1105 stream->is_multicast = 1;
1106 stream->loop = 1; /* default is looping */
1107 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1108 ffserver_get_arg(arg, sizeof(arg), p);
1109 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1110 "Invalid MulticastPort: '%s'\n", arg);
1111 stream->multicast_port = val;
1112 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1113 ffserver_get_arg(arg, sizeof(arg), p);
1114 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1115 "Invalid MulticastTTL: '%s'\n", arg);
1116 stream->multicast_ttl = val;
1117 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1119 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1120 config->stream_use_defaults &= 1;
1121 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1122 if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1123 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1124 if (!config->no_audio &&
1125 config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1126 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1127 add_codec(stream, audio_enc, config);
1129 if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1130 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1131 if (!config->no_video &&
1132 config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1133 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1134 add_codec(stream, video_enc, config);
1137 av_dict_free(&config->video_opts);
1138 av_dict_free(&config->audio_opts);
1139 avcodec_free_context(&config->dummy_vctx);
1140 avcodec_free_context(&config->dummy_actx);
1141 config->no_video = 0;
1142 config->no_audio = 0;
1144 } else if (!av_strcasecmp(cmd, "File") ||
1145 !av_strcasecmp(cmd, "ReadOnlyFile")) {
1146 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1148 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1149 if (config->stream_use_defaults > 1)
1150 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1151 config->stream_use_defaults = 3;
1152 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1153 if (config->stream_use_defaults > 1)
1154 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1155 config->stream_use_defaults = 2;
1157 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1161 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1162 av_dict_free(&config->video_opts);
1163 av_dict_free(&config->audio_opts);
1164 avcodec_free_context(&config->dummy_vctx);
1165 avcodec_free_context(&config->dummy_actx);
1166 return AVERROR(ENOMEM);
1169 static int ffserver_parse_config_redirect(FFServerConfig *config,
1170 const char *cmd, const char **p,
1171 FFServerStream **predirect)
1173 FFServerStream *redirect;
1174 av_assert0(predirect);
1175 redirect = *predirect;
1177 if (!av_strcasecmp(cmd, "<Redirect")) {
1179 redirect = av_mallocz(sizeof(FFServerStream));
1181 return AVERROR(ENOMEM);
1183 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1184 q = strrchr(redirect->filename, '>');
1187 redirect->stream_type = STREAM_TYPE_REDIRECT;
1188 *predirect = redirect;
1191 av_assert0(redirect);
1192 if (!av_strcasecmp(cmd, "URL")) {
1193 ffserver_get_arg(redirect->feed_filename,
1194 sizeof(redirect->feed_filename), p);
1195 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1196 if (!redirect->feed_filename[0])
1197 ERROR("No URL found for <Redirect>\n");
1200 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1205 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1211 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1212 FFServerStream **last_feed, *feed = NULL;
1217 f = fopen(filename, "r");
1219 ret = AVERROR(errno);
1220 av_log(NULL, AV_LOG_ERROR,
1221 "Could not open the configuration file '%s'\n", filename);
1225 config->first_stream = NULL;
1226 config->first_feed = NULL;
1227 config->errors = config->warnings = 0;
1229 last_stream = &config->first_stream;
1230 last_feed = &config->first_feed;
1232 config->line_num = 0;
1233 while (fgets(line, sizeof(line), f) != NULL) {
1236 while (av_isspace(*p))
1238 if (*p == '\0' || *p == '#')
1241 ffserver_get_arg(cmd, sizeof(cmd), &p);
1243 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1244 int opening = !av_strcasecmp(cmd, "<Feed");
1245 if (opening && (stream || feed || redirect)) {
1246 ERROR("Already in a tag\n");
1248 ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
1252 /* add in stream & feed list */
1253 *last_stream = feed;
1255 last_stream = &feed->next;
1256 last_feed = &feed->next_feed;
1259 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1260 int opening = !av_strcasecmp(cmd, "<Stream");
1261 if (opening && (stream || feed || redirect)) {
1262 ERROR("Already in a tag\n");
1264 ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
1268 /* add in stream list */
1269 *last_stream = stream;
1270 last_stream = &stream->next;
1273 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1274 int opening = !av_strcasecmp(cmd, "<Redirect");
1275 if (opening && (stream || feed || redirect))
1276 ERROR("Already in a tag\n");
1278 ret = ffserver_parse_config_redirect(config, cmd, &p,
1283 /* add in stream list */
1284 *last_stream = redirect;
1285 last_stream = &redirect->next;
1289 ffserver_parse_config_global(config, cmd, &p);
1292 if (stream || feed || redirect)
1293 ERROR("Missing closing </%s> tag\n",
1294 stream ? "Stream" : (feed ? "Feed" : "Redirect"));
1300 return AVERROR(EINVAL);
1308 void ffserver_free_child_args(void *argsp)
1314 args = *(char ***)argsp;
1317 for (i = 0; i < MAX_CHILD_ARGS; i++)