2 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "libavutil/opt.h"
23 #include "libavutil/parseutils.h"
24 #include "libavutil/avstring.h"
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/avassert.h"
28 // FIXME those are internal headers, ffserver _really_ shouldn't use them
29 #include "libavformat/ffm.h"
32 #include "ffserver_config.h"
34 #define MAX_CHILD_ARGS 64
36 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
37 FFServerConfig *config);
38 static void vreport_config_error(const char *filename, int line_num,
39 int log_level, int *errors, const char *fmt,
41 static void report_config_error(const char *filename, int line_num,
42 int log_level, int *errors, const char *fmt,
45 #define ERROR(...) report_config_error(config->filename, config->line_num,\
46 AV_LOG_ERROR, &config->errors, __VA_ARGS__)
47 #define WARNING(...) report_config_error(config->filename, config->line_num,\
48 AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
50 /* FIXME: make ffserver work with IPv6 */
51 /* resolve host with also IP address parsing */
52 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
55 if (!ff_inet_aton(hostname, sin_addr)) {
57 struct addrinfo *ai, *cur;
58 struct addrinfo hints = { 0 };
59 hints.ai_family = AF_INET;
60 if (getaddrinfo(hostname, NULL, &hints, &ai))
62 /* getaddrinfo returns a linked list of addrinfo structs.
63 * Even if we set ai_family = AF_INET above, make sure
64 * that the returned one actually is of the correct type. */
65 for (cur = ai; cur; cur = cur->ai_next) {
66 if (cur->ai_family == AF_INET) {
67 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
76 hp = gethostbyname(hostname);
79 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
85 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
94 while (av_isspace(*p)) p++;
96 if (*p == '\"' || *p == '\'')
100 if (quote && *p == quote || !quote && av_isspace(*p))
102 if ((q - buf) < buf_size - 1)
108 if (quote && *p == quote)
113 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
114 FFServerIPAddressACL *ext_acl,
115 const char *p, const char *filename, int line_num)
118 FFServerIPAddressACL acl;
121 ffserver_get_arg(arg, sizeof(arg), &p);
122 if (av_strcasecmp(arg, "allow") == 0)
123 acl.action = IP_ALLOW;
124 else if (av_strcasecmp(arg, "deny") == 0)
125 acl.action = IP_DENY;
127 fprintf(stderr, "%s:%d: ACL action '%s' should be ALLOW or DENY.\n",
128 filename, line_num, arg);
132 ffserver_get_arg(arg, sizeof(arg), &p);
134 if (resolve_host(&acl.first, arg)) {
136 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
137 filename, line_num, arg);
140 acl.last = acl.first;
142 ffserver_get_arg(arg, sizeof(arg), &p);
145 if (resolve_host(&acl.last, arg)) {
147 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
148 filename, line_num, arg);
154 FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
155 FFServerIPAddressACL **naclp = 0;
161 naclp = &stream->acl;
167 fprintf(stderr, "%s:%d: ACL found not in <Stream> or <Feed>\n",
174 naclp = &(*naclp)->next;
182 /* add a codec and set the default parameters */
183 static void add_codec(FFServerStream *stream, AVCodecContext *av,
184 FFServerConfig *config)
187 AVDictionary **opts, *recommended = NULL;
190 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
193 opts = av->codec_type == AVMEDIA_TYPE_AUDIO ?
194 &config->audio_opts : &config->video_opts;
195 av_dict_copy(&recommended, *opts, 0);
196 av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
197 av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN);
199 if (av_dict_count(*opts))
200 av_log(NULL, AV_LOG_WARNING,
201 "Something is wrong, %d options are not set!\n",
202 av_dict_count(*opts));
204 if (!config->stream_use_defaults) {
205 switch(av->codec_type) {
206 case AVMEDIA_TYPE_AUDIO:
207 if (av->bit_rate == 0)
208 report_config_error(config->filename, config->line_num,
209 AV_LOG_ERROR, &config->errors,
210 "audio bit rate is not set\n");
211 if (av->sample_rate == 0)
212 report_config_error(config->filename, config->line_num,
213 AV_LOG_ERROR, &config->errors,
214 "audio sample rate is not set\n");
216 case AVMEDIA_TYPE_VIDEO:
217 if (av->width == 0 || av->height == 0)
218 report_config_error(config->filename, config->line_num,
219 AV_LOG_ERROR, &config->errors,
220 "video size is not set\n");
228 /* stream_use_defaults = true */
230 /* compute default parameters */
231 switch(av->codec_type) {
232 case AVMEDIA_TYPE_AUDIO:
233 if (!av_dict_get(recommended, "ab", NULL, 0)) {
234 av->bit_rate = 64000;
235 av_dict_set_int(&recommended, "ab", av->bit_rate, 0);
236 WARNING("Setting default value for audio bit rate = %d. "
237 "Use NoDefaults to disable it.\n",
240 if (!av_dict_get(recommended, "ar", NULL, 0)) {
241 av->sample_rate = 22050;
242 av_dict_set_int(&recommended, "ar", av->sample_rate, 0);
243 WARNING("Setting default value for audio sample rate = %d. "
244 "Use NoDefaults to disable it.\n",
247 if (!av_dict_get(recommended, "ac", NULL, 0)) {
249 av_dict_set_int(&recommended, "ac", av->channels, 0);
250 WARNING("Setting default value for audio channel count = %d. "
251 "Use NoDefaults to disable it.\n",
255 case AVMEDIA_TYPE_VIDEO:
256 if (!av_dict_get(recommended, "b", NULL, 0)) {
257 av->bit_rate = 64000;
258 av_dict_set_int(&recommended, "b", av->bit_rate, 0);
259 WARNING("Setting default value for video bit rate = %d. "
260 "Use NoDefaults to disable it.\n",
263 if (!av_dict_get(recommended, "time_base", NULL, 0)){
264 av->time_base.den = 5;
265 av->time_base.num = 1;
266 av_dict_set(&recommended, "time_base", "1/5", 0);
267 WARNING("Setting default value for video frame rate = %d. "
268 "Use NoDefaults to disable it.\n",
271 if (!av_dict_get(recommended, "video_size", NULL, 0)) {
274 av_dict_set(&recommended, "video_size", "160x128", 0);
275 WARNING("Setting default value for video size = %dx%d. "
276 "Use NoDefaults to disable it.\n",
277 av->width, av->height);
279 /* Bitrate tolerance is less for streaming */
280 if (!av_dict_get(recommended, "bt", NULL, 0)) {
281 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
282 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
283 av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0);
284 WARNING("Setting default value for video bit rate tolerance = %d. "
285 "Use NoDefaults to disable it.\n",
286 av->bit_rate_tolerance);
289 if (!av_dict_get(recommended, "rc_eq", NULL, 0)) {
290 av->rc_eq = av_strdup("tex^qComp");
291 av_dict_set(&recommended, "rc_eq", "tex^qComp", 0);
292 WARNING("Setting default value for video rate control equation = "
293 "%s. Use NoDefaults to disable it.\n",
296 if (!av_dict_get(recommended, "maxrate", NULL, 0)) {
297 av->rc_max_rate = av->bit_rate * 2;
298 av_dict_set_int(&recommended, "maxrate", av->rc_max_rate, 0);
299 WARNING("Setting default value for video max rate = %d. "
300 "Use NoDefaults to disable it.\n",
304 if (av->rc_max_rate && !av_dict_get(recommended, "bufsize", NULL, 0)) {
305 av->rc_buffer_size = av->rc_max_rate;
306 av_dict_set_int(&recommended, "bufsize", av->rc_buffer_size, 0);
307 WARNING("Setting default value for video buffer size = %d. "
308 "Use NoDefaults to disable it.\n",
317 st = av_mallocz(sizeof(AVStream));
320 av_dict_get_string(recommended, &enc_config, '=', ',');
321 av_dict_free(&recommended);
322 av_stream_set_recommended_encoder_configuration(st, enc_config);
324 stream->streams[stream->nb_streams++] = st;
327 static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name,
328 FFServerConfig *config)
331 AVCodec *codec = avcodec_find_encoder_by_name(codec_name);
332 if (!codec || codec->type != ctx->codec_type) {
333 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
335 "Invalid codec name: '%s'\n", codec_name);
338 if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) {
339 if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0)
343 if (ctx->codec_id != codec->id)
344 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
346 "Inconsistent configuration: trying to set '%s' "
347 "codec option, but '%s' codec is used previously\n",
348 codec_name, avcodec_get_name(ctx->codec_id));
352 static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config)
355 char filename[1000], tmp[1000], tmp2[1000], line[1000];
357 AVCodecContext *avctx;
358 const AVCodec *codec;
361 case AV_OPT_FLAG_AUDIO_PARAM:
362 avctx = config->dummy_actx;
364 case AV_OPT_FLAG_VIDEO_PARAM:
365 avctx = config->dummy_vctx;
370 codec = avcodec_find_encoder(avctx->codec_id);
372 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
373 codec ? codec->name : NULL))) {
374 av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg);
375 return AVERROR(EINVAL);
379 int e= fscanf(f, "%999[^\n]\n", line) - 1;
380 if(line[0] == '#' && !e)
382 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
384 av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename,
386 ret = AVERROR(EINVAL);
389 if (!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO ||
390 !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO)
392 if (ffserver_set_codec(avctx, tmp2, config) < 0)
394 } else if (!strcmp(tmp, "scodec")) {
395 av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n");
396 ret = AVERROR(EINVAL);
398 } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0)
407 static AVOutputFormat *ffserver_guess_format(const char *short_name,
408 const char *filename,
409 const char *mime_type)
411 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
414 AVOutputFormat *stream_fmt;
415 char stream_format_name[64];
417 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
419 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
428 static void vreport_config_error(const char *filename, int line_num,
429 int log_level, int *errors, const char *fmt,
432 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
433 av_vlog(NULL, log_level, fmt, vl);
438 static void report_config_error(const char *filename, int line_num,
439 int log_level, int *errors,
440 const char *fmt, ...)
444 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
448 static int ffserver_set_int_param(int *dest, const char *value, int factor,
449 int min, int max, FFServerConfig *config,
450 const char *error_msg, ...)
454 if (!value || !value[0])
457 tmp = strtol(value, &tailp, 0);
458 if (tmp < min || tmp > max)
461 if (FFABS(tmp) > INT_MAX / FFABS(factor))
465 if (tailp[0] || errno)
473 va_start(vl, error_msg);
474 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
475 &config->errors, error_msg, vl);
478 return AVERROR(EINVAL);
481 static int ffserver_set_float_param(float *dest, const char *value,
482 float factor, float min, float max,
483 FFServerConfig *config,
484 const char *error_msg, ...)
488 if (!value || !value[0])
491 tmp = strtod(value, &tailp);
492 if (tmp < min || tmp > max)
496 if (tailp[0] || errno)
504 va_start(vl, error_msg);
505 vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR,
506 &config->errors, error_msg, vl);
509 return AVERROR(EINVAL);
512 static int ffserver_save_avoption(const char *opt, const char *arg, int type,
513 FFServerConfig *config)
515 static int hinted = 0;
517 AVDictionaryEntry *e;
518 const AVOption *o = NULL;
519 const char *option = NULL;
520 const char *codec_name = NULL;
524 enum AVCodecID guessed_codec_id;
527 case AV_OPT_FLAG_VIDEO_PARAM:
528 ctx = config->dummy_vctx;
529 dict = &config->video_opts;
530 guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ?
531 config->guessed_video_codec_id : AV_CODEC_ID_H264;
533 case AV_OPT_FLAG_AUDIO_PARAM:
534 ctx = config->dummy_actx;
535 dict = &config->audio_opts;
536 guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ?
537 config->guessed_audio_codec_id : AV_CODEC_ID_AAC;
543 if (strchr(opt, ':')) {
544 //explicit private option
545 snprintf(buff, sizeof(buff), "%s", opt);
547 if(!(option = strchr(buff, ':'))){
548 report_config_error(config->filename, config->line_num,
549 AV_LOG_ERROR, &config->errors,
550 "Syntax error. Unmatched ':'\n");
554 buff[option - buff] = '\0';
556 if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0)
558 if (!ctx->codec || !ctx->priv_data)
564 o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM,
565 AV_OPT_SEARCH_CHILDREN);
567 (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") ||
568 !strcmp(option, "video_size") || !strcmp(option, "codec_tag")))
569 o = av_opt_find(ctx, option, NULL, 0, 0);
571 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
572 &config->errors, "Option not found: '%s'\n", opt);
573 if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
575 report_config_error(config->filename, config->line_num,
576 AV_LOG_ERROR, NULL, "If '%s' is a codec private"
577 "option, then prefix it with codec name, for "
578 "example '%s:%s %s' or define codec earlier.\n",
579 opt, avcodec_get_name(guessed_codec_id) ,opt,
582 } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
583 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
584 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
585 arg, av_err2str(ret));
586 } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
587 if ((o->type == AV_OPT_TYPE_FLAGS) && arg &&
588 (arg[0] == '+' || arg[0] == '-'))
589 return av_dict_set(dict, option, arg, AV_DICT_APPEND);
590 report_config_error(config->filename, config->line_num, AV_LOG_ERROR,
591 &config->errors, "Redeclaring value of option '%s'."
592 "Previous value was: '%s'.\n", opt, e->value);
593 } else if (av_dict_set(dict, option, arg, 0) < 0) {
594 return AVERROR(ENOMEM);
599 static int ffserver_save_avoption_int(const char *opt, int64_t arg,
600 int type, FFServerConfig *config)
603 snprintf(buf, sizeof(buf), "%"PRId64, arg);
604 return ffserver_save_avoption(opt, buf, type, config);
607 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
612 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
613 if (!av_strcasecmp(cmd, "Port"))
614 WARNING("Port option is deprecated. Use HTTPPort instead.\n");
615 ffserver_get_arg(arg, sizeof(arg), p);
616 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
617 "Invalid port: %s\n", arg);
619 WARNING("Trying to use IETF assigned system port: '%d'\n", val);
620 config->http_addr.sin_port = htons(val);
621 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") ||
622 !av_strcasecmp(cmd, "BindAddress")) {
623 if (!av_strcasecmp(cmd, "BindAddress"))
624 WARNING("BindAddress option is deprecated. Use HTTPBindAddress "
626 ffserver_get_arg(arg, sizeof(arg), p);
627 if (resolve_host(&config->http_addr.sin_addr, arg))
628 ERROR("Invalid host/IP address: '%s'\n", arg);
629 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
630 WARNING("NoDaemon option has no effect. You should remove it.\n");
631 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
632 ffserver_get_arg(arg, sizeof(arg), p);
633 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
634 "Invalid port: %s\n", arg);
635 config->rtsp_addr.sin_port = htons(val);
636 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
637 ffserver_get_arg(arg, sizeof(arg), p);
638 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
639 ERROR("Invalid host/IP address: %s\n", arg);
640 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
641 ffserver_get_arg(arg, sizeof(arg), p);
642 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
643 "Invalid MaxHTTPConnections: %s\n", arg);
644 config->nb_max_http_connections = val;
645 if (config->nb_max_connections > config->nb_max_http_connections) {
646 ERROR("Inconsistent configuration: MaxClients(%d) > "
647 "MaxHTTPConnections(%d)\n", config->nb_max_connections,
648 config->nb_max_http_connections);
650 } else if (!av_strcasecmp(cmd, "MaxClients")) {
651 ffserver_get_arg(arg, sizeof(arg), p);
652 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
653 "Invalid MaxClients: '%s'\n", arg);
654 config->nb_max_connections = val;
655 if (config->nb_max_connections > config->nb_max_http_connections) {
656 ERROR("Inconsistent configuration: MaxClients(%d) > "
657 "MaxHTTPConnections(%d)\n", config->nb_max_connections,
658 config->nb_max_http_connections);
660 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
663 ffserver_get_arg(arg, sizeof(arg), p);
665 llval = strtoll(arg, &tailp, 10);
666 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
667 ERROR("Invalid MaxBandwidth: '%s'\n", arg);
669 config->max_bandwidth = llval;
670 } else if (!av_strcasecmp(cmd, "CustomLog")) {
671 if (!config->debug) {
672 ffserver_get_arg(config->logfilename, sizeof(config->logfilename),
675 } else if (!av_strcasecmp(cmd, "LoadModule")) {
676 ERROR("Loadable modules are no longer supported\n");
677 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
678 config->use_defaults = 0;
679 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
680 config->use_defaults = 1;
682 ERROR("Incorrect keyword: '%s'\n", cmd);
686 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
687 FFServerStream **pfeed)
689 FFServerStream *feed;
693 if (!av_strcasecmp(cmd, "<Feed")) {
696 feed = av_mallocz(sizeof(FFServerStream));
698 return AVERROR(ENOMEM);
699 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
700 q = strrchr(feed->filename, '>');
704 for (s = config->first_feed; s; s = s->next) {
705 if (!strcmp(feed->filename, s->filename))
706 ERROR("Feed '%s' already registered\n", s->filename);
709 feed->fmt = av_guess_format("ffm", NULL, NULL);
710 /* default feed file */
711 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
712 "/tmp/%s.ffm", feed->filename);
713 feed->feed_max_size = 5 * 1024 * 1024;
715 feed->feed = feed; /* self feeding :-) */
720 if (!av_strcasecmp(cmd, "Launch")) {
723 feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *));
724 if (!feed->child_argv)
725 return AVERROR(ENOMEM);
726 for (i = 0; i < MAX_CHILD_ARGS - 2; i++) {
727 ffserver_get_arg(arg, sizeof(arg), p);
731 feed->child_argv[i] = av_strdup(arg);
732 if (!feed->child_argv[i])
733 return AVERROR(ENOMEM);
736 feed->child_argv[i] =
737 av_asprintf("http://%s:%d/%s",
738 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ?
739 "127.0.0.1" : inet_ntoa(config->http_addr.sin_addr),
740 ntohs(config->http_addr.sin_port), feed->filename);
741 if (!feed->child_argv[i])
742 return AVERROR(ENOMEM);
743 } else if (!av_strcasecmp(cmd, "ACL")) {
744 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
746 } else if (!av_strcasecmp(cmd, "File") ||
747 !av_strcasecmp(cmd, "ReadOnlyFile")) {
748 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
749 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
750 } else if (!av_strcasecmp(cmd, "Truncate")) {
751 ffserver_get_arg(arg, sizeof(arg), p);
752 /* assume Truncate is true in case no argument is specified */
756 WARNING("Truncate N syntax in configuration file is deprecated. "
757 "Use Truncate alone with no arguments.\n");
758 feed->truncate = strtod(arg, NULL);
760 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
764 ffserver_get_arg(arg, sizeof(arg), p);
766 fsize = strtod(p1, &p1);
767 switch(av_toupper(*p1)) {
772 fsize *= 1024 * 1024;
775 fsize *= 1024 * 1024 * 1024;
778 ERROR("Invalid file size: '%s'\n", arg);
781 feed->feed_max_size = (int64_t)fsize;
782 if (feed->feed_max_size < FFM_PACKET_SIZE*4) {
783 ERROR("Feed max file size is too small. Must be at least %d.\n",
786 } else if (!av_strcasecmp(cmd, "</Feed>")) {
789 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
794 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
795 FFServerStream **pstream)
797 char arg[1024], arg2[1024];
798 FFServerStream *stream;
804 if (!av_strcasecmp(cmd, "<Stream")) {
807 stream = av_mallocz(sizeof(FFServerStream));
809 return AVERROR(ENOMEM);
810 config->dummy_actx = avcodec_alloc_context3(NULL);
811 config->dummy_vctx = avcodec_alloc_context3(NULL);
812 if (!config->dummy_vctx || !config->dummy_actx) {
814 avcodec_free_context(&config->dummy_vctx);
815 avcodec_free_context(&config->dummy_actx);
816 return AVERROR(ENOMEM);
818 config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
819 config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
820 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
821 q = strrchr(stream->filename, '>');
825 for (s = config->first_stream; s; s = s->next) {
826 if (!strcmp(stream->filename, s->filename))
827 ERROR("Stream '%s' already registered\n", s->filename);
830 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
832 config->guessed_audio_codec_id = stream->fmt->audio_codec;
833 config->guessed_video_codec_id = stream->fmt->video_codec;
835 config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
836 config->guessed_video_codec_id = AV_CODEC_ID_NONE;
838 config->stream_use_defaults = config->use_defaults;
843 if (!av_strcasecmp(cmd, "Feed")) {
844 FFServerStream *sfeed;
845 ffserver_get_arg(arg, sizeof(arg), p);
846 sfeed = config->first_feed;
848 if (!strcmp(sfeed->filename, arg))
850 sfeed = sfeed->next_feed;
853 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
856 stream->feed = sfeed;
857 } else if (!av_strcasecmp(cmd, "Format")) {
858 ffserver_get_arg(arg, sizeof(arg), p);
859 if (!strcmp(arg, "status")) {
860 stream->stream_type = STREAM_TYPE_STATUS;
863 stream->stream_type = STREAM_TYPE_LIVE;
864 /* JPEG cannot be used here, so use single frame MJPEG */
865 if (!strcmp(arg, "jpeg")) {
866 strcpy(arg, "singlejpeg");
867 stream->single_frame=1;
869 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
871 ERROR("Unknown Format: '%s'\n", arg);
874 config->guessed_audio_codec_id = stream->fmt->audio_codec;
875 config->guessed_video_codec_id = stream->fmt->video_codec;
877 } else if (!av_strcasecmp(cmd, "InputFormat")) {
878 ffserver_get_arg(arg, sizeof(arg), p);
879 stream->ifmt = av_find_input_format(arg);
881 ERROR("Unknown input format: '%s'\n", arg);
882 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
883 if (stream->stream_type == STREAM_TYPE_STATUS)
884 ffserver_get_arg(stream->feed_filename,
885 sizeof(stream->feed_filename), p);
887 ERROR("FaviconURL only permitted for status streams\n");
888 } else if (!av_strcasecmp(cmd, "Author") ||
889 !av_strcasecmp(cmd, "Comment") ||
890 !av_strcasecmp(cmd, "Copyright") ||
891 !av_strcasecmp(cmd, "Title")) {
894 ffserver_get_arg(arg, sizeof(arg), p);
895 for (i = 0; i < strlen(cmd); i++)
896 key[i] = av_tolower(cmd[i]);
898 WARNING("Deprecated '%s' option in configuration file. Use "
899 "'Metadata %s VALUE' instead.\n", cmd, key);
900 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
902 } else if (!av_strcasecmp(cmd, "Metadata")) {
903 ffserver_get_arg(arg, sizeof(arg), p);
904 ffserver_get_arg(arg2, sizeof(arg2), p);
905 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
907 } else if (!av_strcasecmp(cmd, "Preroll")) {
908 ffserver_get_arg(arg, sizeof(arg), p);
909 stream->prebuffer = atof(arg) * 1000;
910 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
911 stream->send_on_key = 1;
912 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
913 ffserver_get_arg(arg, sizeof(arg), p);
914 ffserver_set_codec(config->dummy_actx, arg, config);
915 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
916 ffserver_get_arg(arg, sizeof(arg), p);
917 ffserver_set_codec(config->dummy_vctx, arg, config);
918 } else if (!av_strcasecmp(cmd, "MaxTime")) {
919 ffserver_get_arg(arg, sizeof(arg), p);
920 stream->max_time = atof(arg) * 1000;
921 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
923 ffserver_get_arg(arg, sizeof(arg), p);
924 ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config,
925 "Invalid %s: '%s'\n", cmd, arg);
926 if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f),
927 AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
929 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
930 ffserver_get_arg(arg, sizeof(arg), p);
931 if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
933 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
934 ffserver_get_arg(arg, sizeof(arg), p);
935 if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0)
937 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
938 int minrate, maxrate;
940 ffserver_get_arg(arg, sizeof(arg), p);
941 dash = strchr(arg, '-');
945 if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0 &&
946 ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: '%s'", cmd, arg) >= 0) {
947 if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
948 ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
952 ERROR("Incorrect format for VideoBitRateRange. It should be "
953 "<min>-<max>: '%s'.\n", arg);
954 } else if (!av_strcasecmp(cmd, "Debug")) {
955 ffserver_get_arg(arg, sizeof(arg), p);
956 if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
957 ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
959 } else if (!av_strcasecmp(cmd, "Strict")) {
960 ffserver_get_arg(arg, sizeof(arg), p);
961 if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 ||
962 ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
964 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
965 ffserver_get_arg(arg, sizeof(arg), p);
966 ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config,
967 "Invalid %s: '%s'", cmd, arg);
968 if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
970 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
971 ffserver_get_arg(arg, sizeof(arg), p);
972 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
973 "Invalid %s: '%s'", cmd, arg);
974 if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
976 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
977 ffserver_get_arg(arg, sizeof(arg), p);
978 ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config,
979 "Invalid %s: '%s'", cmd, arg);
980 if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
982 } else if (!av_strcasecmp(cmd, "VideoSize")) {
984 ffserver_get_arg(arg, sizeof(arg), p);
985 ret = av_parse_video_size(&w, &h, arg);
987 ERROR("Invalid video size '%s'\n", arg);
990 WARNING("Image size is not a multiple of 2\n");
991 if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
994 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
995 ffserver_get_arg(&arg[2], sizeof(arg) - 2, p);
996 arg[0] = '1'; arg[1] = '/';
997 if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
999 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
1000 enum AVPixelFormat pix_fmt;
1001 ffserver_get_arg(arg, sizeof(arg), p);
1002 pix_fmt = av_get_pix_fmt(arg);
1003 if (pix_fmt == AV_PIX_FMT_NONE)
1004 ERROR("Unknown pixel format: '%s'\n", arg);
1005 else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1007 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
1008 ffserver_get_arg(arg, sizeof(arg), p);
1009 if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1011 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
1012 if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1014 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
1015 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1017 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
1018 if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove
1019 ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1021 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
1022 !av_strcasecmp(cmd, "AVOptionAudio")) {
1024 ffserver_get_arg(arg, sizeof(arg), p);
1025 ffserver_get_arg(arg2, sizeof(arg2), p);
1026 if (!av_strcasecmp(cmd, "AVOptionVideo"))
1027 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM,
1030 ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM,
1034 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
1035 !av_strcasecmp(cmd, "AVPresetAudio")) {
1036 ffserver_get_arg(arg, sizeof(arg), p);
1037 if (!av_strcasecmp(cmd, "AVPresetVideo"))
1038 ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config);
1040 ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config);
1041 } else if (!av_strcasecmp(cmd, "VideoTag")) {
1042 ffserver_get_arg(arg, sizeof(arg), p);
1043 if (strlen(arg) == 4 &&
1044 ffserver_save_avoption_int("codec_tag",
1045 MKTAG(arg[0], arg[1], arg[2], arg[3]),
1046 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1048 } else if (!av_strcasecmp(cmd, "BitExact")) {
1049 if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1051 } else if (!av_strcasecmp(cmd, "DctFastint")) {
1052 if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1054 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
1055 if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1057 } else if (!av_strcasecmp(cmd, "Qscale")) {
1058 ffserver_get_arg(arg, sizeof(arg), p);
1059 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1060 "Invalid Qscale: '%s'\n", arg);
1061 if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 ||
1062 ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val,
1063 AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1065 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
1066 ffserver_get_arg(arg, sizeof(arg), p);
1067 if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1069 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
1070 ffserver_get_arg(arg, sizeof(arg), p);
1071 if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1073 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
1074 ffserver_get_arg(arg, sizeof(arg), p);
1075 if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1077 } else if (!av_strcasecmp(cmd, "LumiMask")) {
1078 ffserver_get_arg(arg, sizeof(arg), p);
1079 if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1081 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1082 ffserver_get_arg(arg, sizeof(arg), p);
1083 if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0)
1085 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1086 config->no_video = 1;
1087 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1088 config->no_audio = 1;
1089 } else if (!av_strcasecmp(cmd, "ACL")) {
1090 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1092 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1093 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1094 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1095 ffserver_get_arg(arg, sizeof(arg), p);
1096 av_freep(&stream->rtsp_option);
1097 stream->rtsp_option = av_strdup(arg);
1098 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1099 ffserver_get_arg(arg, sizeof(arg), p);
1100 if (resolve_host(&stream->multicast_ip, arg))
1101 ERROR("Invalid host/IP address: '%s'\n", arg);
1102 stream->is_multicast = 1;
1103 stream->loop = 1; /* default is looping */
1104 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1105 ffserver_get_arg(arg, sizeof(arg), p);
1106 ffserver_set_int_param(&val, arg, 0, 1, 65535, config,
1107 "Invalid MulticastPort: '%s'\n", arg);
1108 stream->multicast_port = val;
1109 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1110 ffserver_get_arg(arg, sizeof(arg), p);
1111 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1112 "Invalid MulticastTTL: '%s'\n", arg);
1113 stream->multicast_ttl = val;
1114 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1116 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1117 config->stream_use_defaults &= 1;
1118 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1119 if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
1120 config->dummy_actx->codec_id = config->guessed_audio_codec_id;
1121 if (!config->no_audio &&
1122 config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
1123 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
1124 add_codec(stream, audio_enc, config);
1126 if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
1127 config->dummy_vctx->codec_id = config->guessed_video_codec_id;
1128 if (!config->no_video &&
1129 config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
1130 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
1131 add_codec(stream, video_enc, config);
1134 av_dict_free(&config->video_opts);
1135 av_dict_free(&config->audio_opts);
1136 avcodec_free_context(&config->dummy_vctx);
1137 avcodec_free_context(&config->dummy_actx);
1139 } else if (!av_strcasecmp(cmd, "File") ||
1140 !av_strcasecmp(cmd, "ReadOnlyFile")) {
1141 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1143 } else if (!av_strcasecmp(cmd, "UseDefaults")) {
1144 if (config->stream_use_defaults > 1)
1145 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1146 config->stream_use_defaults = 3;
1147 } else if (!av_strcasecmp(cmd, "NoDefaults")) {
1148 if (config->stream_use_defaults > 1)
1149 WARNING("Multiple UseDefaults/NoDefaults entries.\n");
1150 config->stream_use_defaults = 2;
1152 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1156 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1157 av_dict_free(&config->video_opts);
1158 av_dict_free(&config->audio_opts);
1159 avcodec_free_context(&config->dummy_vctx);
1160 avcodec_free_context(&config->dummy_actx);
1161 return AVERROR(ENOMEM);
1164 static int ffserver_parse_config_redirect(FFServerConfig *config,
1165 const char *cmd, const char **p,
1166 FFServerStream **predirect)
1168 FFServerStream *redirect;
1169 av_assert0(predirect);
1170 redirect = *predirect;
1172 if (!av_strcasecmp(cmd, "<Redirect")) {
1174 redirect = av_mallocz(sizeof(FFServerStream));
1176 return AVERROR(ENOMEM);
1178 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1179 q = strrchr(redirect->filename, '>');
1182 redirect->stream_type = STREAM_TYPE_REDIRECT;
1183 *predirect = redirect;
1186 av_assert0(redirect);
1187 if (!av_strcasecmp(cmd, "URL")) {
1188 ffserver_get_arg(redirect->feed_filename,
1189 sizeof(redirect->feed_filename), p);
1190 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1191 if (!redirect->feed_filename[0])
1192 ERROR("No URL found for <Redirect>\n");
1195 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1200 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1206 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1207 FFServerStream **last_feed, *feed = NULL;
1212 f = fopen(filename, "r");
1214 ret = AVERROR(errno);
1215 av_log(NULL, AV_LOG_ERROR,
1216 "Could not open the configuration file '%s'\n", filename);
1220 config->first_stream = NULL;
1221 config->first_feed = NULL;
1222 config->errors = config->warnings = 0;
1224 last_stream = &config->first_stream;
1225 last_feed = &config->first_feed;
1227 config->line_num = 0;
1228 while (fgets(line, sizeof(line), f) != NULL) {
1231 while (av_isspace(*p))
1233 if (*p == '\0' || *p == '#')
1236 ffserver_get_arg(cmd, sizeof(cmd), &p);
1238 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1239 int opening = !av_strcasecmp(cmd, "<Feed");
1240 if (opening && (stream || feed || redirect)) {
1241 ERROR("Already in a tag\n");
1243 ret = ffserver_parse_config_feed(config, cmd, &p, &feed);
1247 /* add in stream & feed list */
1248 *last_stream = feed;
1250 last_stream = &feed->next;
1251 last_feed = &feed->next_feed;
1254 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1255 int opening = !av_strcasecmp(cmd, "<Stream");
1256 if (opening && (stream || feed || redirect)) {
1257 ERROR("Already in a tag\n");
1259 ret = ffserver_parse_config_stream(config, cmd, &p, &stream);
1263 /* add in stream list */
1264 *last_stream = stream;
1265 last_stream = &stream->next;
1268 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1269 int opening = !av_strcasecmp(cmd, "<Redirect");
1270 if (opening && (stream || feed || redirect))
1271 ERROR("Already in a tag\n");
1273 ret = ffserver_parse_config_redirect(config, cmd, &p,
1278 /* add in stream list */
1279 *last_stream = redirect;
1280 last_stream = &redirect->next;
1284 ffserver_parse_config_global(config, cmd, &p);
1287 if (stream || feed || redirect)
1288 ERROR("Missing closing </%s> tag\n",
1289 stream ? "Stream" : (feed ? "Feed" : "Redirect"));
1295 return AVERROR(EINVAL);
1303 void ffserver_free_child_args(void *argsp)
1309 args = *(char ***)argsp;
1312 for (i = 0; i < MAX_CHILD_ARGS; i++)