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 /* FIXME: make ffserver work with IPv6 */
35 /* resolve host with also IP address parsing */
36 static int resolve_host(struct in_addr *sin_addr, const char *hostname)
39 if (!ff_inet_aton(hostname, sin_addr)) {
41 struct addrinfo *ai, *cur;
42 struct addrinfo hints = { 0 };
43 hints.ai_family = AF_INET;
44 if (getaddrinfo(hostname, NULL, &hints, &ai))
46 /* getaddrinfo returns a linked list of addrinfo structs.
47 * Even if we set ai_family = AF_INET above, make sure
48 * that the returned one actually is of the correct type. */
49 for (cur = ai; cur; cur = cur->ai_next) {
50 if (cur->ai_family == AF_INET) {
51 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
60 hp = gethostbyname(hostname);
63 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
69 void ffserver_get_arg(char *buf, int buf_size, const char **pp)
76 while (av_isspace(*p)) p++;
79 if (*p == '\"' || *p == '\'')
91 if ((q - buf) < buf_size - 1)
96 if (quote && *p == quote)
101 void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed,
102 FFServerIPAddressACL *ext_acl,
103 const char *p, const char *filename, int line_num)
106 FFServerIPAddressACL acl;
109 ffserver_get_arg(arg, sizeof(arg), &p);
110 if (av_strcasecmp(arg, "allow") == 0)
111 acl.action = IP_ALLOW;
112 else if (av_strcasecmp(arg, "deny") == 0)
113 acl.action = IP_DENY;
115 fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n",
116 filename, line_num, arg);
120 ffserver_get_arg(arg, sizeof(arg), &p);
122 if (resolve_host(&acl.first, arg)) {
123 fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
124 filename, line_num, arg);
127 acl.last = acl.first;
129 ffserver_get_arg(arg, sizeof(arg), &p);
132 if (resolve_host(&acl.last, arg)) {
134 "%s:%d: ACL refers to invalid host or IP address '%s'\n",
135 filename, line_num, arg);
141 FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
142 FFServerIPAddressACL **naclp = 0;
148 naclp = &stream->acl;
154 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
161 naclp = &(*naclp)->next;
169 /* add a codec and set the default parameters */
170 static void add_codec(FFServerStream *stream, AVCodecContext *av)
174 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
177 /* compute default parameters */
178 switch(av->codec_type) {
179 case AVMEDIA_TYPE_AUDIO:
180 if (av->bit_rate == 0)
181 av->bit_rate = 64000;
182 if (av->sample_rate == 0)
183 av->sample_rate = 22050;
184 if (av->channels == 0)
187 case AVMEDIA_TYPE_VIDEO:
188 if (av->bit_rate == 0)
189 av->bit_rate = 64000;
190 if (av->time_base.num == 0){
191 av->time_base.den = 5;
192 av->time_base.num = 1;
194 if (av->width == 0 || av->height == 0) {
198 /* Bitrate tolerance is less for streaming */
199 if (av->bit_rate_tolerance == 0)
200 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
201 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
206 if (av->max_qdiff == 0)
211 if (!av->nsse_weight)
214 av->frame_skip_cmp = FF_CMP_DCTMAX;
216 av->me_method = ME_EPZS;
217 av->rc_buffer_aggressivity = 1.0;
220 av->rc_eq = av_strdup("tex^qComp");
221 if (!av->i_quant_factor)
222 av->i_quant_factor = -0.8;
223 if (!av->b_quant_factor)
224 av->b_quant_factor = 1.25;
225 if (!av->b_quant_offset)
226 av->b_quant_offset = 1.25;
227 if (!av->rc_max_rate)
228 av->rc_max_rate = av->bit_rate * 2;
230 if (av->rc_max_rate && !av->rc_buffer_size) {
231 av->rc_buffer_size = av->rc_max_rate;
240 st = av_mallocz(sizeof(AVStream));
244 stream->streams[stream->nb_streams++] = st;
247 static enum AVCodecID opt_codec(const char *name, enum AVMediaType type)
249 AVCodec *codec = avcodec_find_encoder_by_name(name);
251 if (!codec || codec->type != type)
252 return AV_CODEC_ID_NONE;
256 static int ffserver_opt_default(const char *opt, const char *arg,
257 AVCodecContext *avctx, int type)
260 const AVOption *o = av_opt_find(avctx, opt, NULL, type, 0);
262 ret = av_opt_set(avctx, opt, arg, 0);
266 static int ffserver_opt_preset(const char *arg,
267 AVCodecContext *avctx, int type,
268 enum AVCodecID *audio_id, enum AVCodecID *video_id)
271 char filename[1000], tmp[1000], tmp2[1000], line[1000];
273 AVCodec *codec = NULL;
276 codec = avcodec_find_encoder(avctx->codec_id);
278 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
279 codec ? codec->name : NULL))) {
280 fprintf(stderr, "File for preset '%s' not found\n", arg);
281 return AVERROR(EINVAL);
285 int e= fscanf(f, "%999[^\n]\n", line) - 1;
286 if(line[0] == '#' && !e)
288 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
290 fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
291 ret = AVERROR(EINVAL);
294 if (audio_id && !strcmp(tmp, "acodec")) {
295 *audio_id = opt_codec(tmp2, AVMEDIA_TYPE_AUDIO);
296 } else if (video_id && !strcmp(tmp, "vcodec")){
297 *video_id = opt_codec(tmp2, AVMEDIA_TYPE_VIDEO);
298 } else if(!strcmp(tmp, "scodec")) {
299 /* opt_subtitle_codec(tmp2); */
300 } else if (avctx && (ret = ffserver_opt_default(tmp, tmp2, avctx, type)) < 0) {
301 fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as "
302 "'%s' = '%s'\n", filename, line, tmp, tmp2);
312 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
314 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
317 AVOutputFormat *stream_fmt;
318 char stream_format_name[64];
320 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream",
322 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
331 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
333 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
334 av_vlog(NULL, log_level, fmt, vl);
338 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
342 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
346 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
347 FFServerConfig *config, int line_num, const char *error_msg, ...)
351 if (!value || !value[0])
354 tmp = strtol(value, &tailp, 0);
355 if (tmp < min || tmp > max)
358 if (FFABS(tmp) > INT_MAX / FFABS(factor))
362 if (tailp[0] || errno)
370 va_start(vl, error_msg);
371 vreport_config_error(config->filename, line_num, AV_LOG_ERROR,
372 &config->errors, error_msg, vl);
375 return AVERROR(EINVAL);
378 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
379 FFServerConfig *config, int line_num, const char *error_msg, ...)
383 if (!value || !value[0])
386 tmp = strtod(value, &tailp);
387 if (tmp < min || tmp > max)
391 if (tailp[0] || errno)
399 va_start(vl, error_msg);
400 vreport_config_error(config->filename, line_num, AV_LOG_ERROR,
401 &config->errors, error_msg, vl);
404 return AVERROR(EINVAL);
407 static int ffserver_save_avoption(const char *opt, const char *arg, AVDictionary **dict,
408 int type, FFServerConfig *config, int line_num)
411 AVDictionaryEntry *e;
412 const AVOption *o = av_opt_find(config->dummy_ctx, opt, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN);
414 report_config_error(config->filename, line_num, AV_LOG_ERROR,
415 &config->errors, "Option not found: %s\n", opt);
416 } else if ((ret = av_opt_set(config->dummy_ctx, opt, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
417 report_config_error(config->filename, line_num, AV_LOG_ERROR,
418 &config->errors, "Invalid value for option %s (%s): %s\n", opt,
419 arg, av_err2str(ret));
420 } else if ((e = av_dict_get(*dict, opt, NULL, 0))) {
421 if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
422 return av_dict_set(dict, opt, arg, AV_DICT_APPEND);
423 report_config_error(config->filename, line_num, AV_LOG_ERROR,
425 "Redeclaring value of the option %s, previous value: %s\n",
427 } else if (av_dict_set(dict, opt, arg, 0) < 0) {
428 return AVERROR(ENOMEM);
433 #define ERROR(...) report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, __VA_ARGS__)
434 #define WARNING(...) report_config_error(config->filename, line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
436 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
437 const char **p, int line_num)
441 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
442 if (!av_strcasecmp(cmd, "Port"))
443 WARNING("Port option is deprecated, use HTTPPort instead\n");
444 ffserver_get_arg(arg, sizeof(arg), p);
445 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
446 "Invalid port: %s\n", arg);
448 WARNING("Trying to use IETF assigned system port: %d\n", val);
449 config->http_addr.sin_port = htons(val);
450 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
451 if (!av_strcasecmp(cmd, "BindAddress"))
452 WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
453 ffserver_get_arg(arg, sizeof(arg), p);
454 if (resolve_host(&config->http_addr.sin_addr, arg))
455 ERROR("Invalid host/IP address: %s\n", arg);
456 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
457 WARNING("NoDaemon option has no effect, you should remove it\n");
458 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
459 ffserver_get_arg(arg, sizeof(arg), p);
460 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
461 "Invalid port: %s\n", arg);
462 config->rtsp_addr.sin_port = htons(val);
463 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
464 ffserver_get_arg(arg, sizeof(arg), p);
465 if (resolve_host(&config->rtsp_addr.sin_addr, arg))
466 ERROR("Invalid host/IP address: %s\n", arg);
467 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
468 ffserver_get_arg(arg, sizeof(arg), p);
469 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
470 "Invalid MaxHTTPConnections: %s\n", arg);
471 config->nb_max_http_connections = val;
472 if (config->nb_max_connections > config->nb_max_http_connections)
473 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
474 config->nb_max_connections, config->nb_max_http_connections);
475 } else if (!av_strcasecmp(cmd, "MaxClients")) {
476 ffserver_get_arg(arg, sizeof(arg), p);
477 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
478 "Invalid MaxClients: %s\n", arg);
479 config->nb_max_connections = val;
480 if (config->nb_max_connections > config->nb_max_http_connections)
481 ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
482 config->nb_max_connections, config->nb_max_http_connections);
483 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
486 ffserver_get_arg(arg, sizeof(arg), p);
488 llval = strtoll(arg, &tailp, 10);
489 if (llval < 10 || llval > 10000000 || tailp[0] || errno)
490 ERROR("Invalid MaxBandwidth: %s\n", arg);
492 config->max_bandwidth = llval;
493 } else if (!av_strcasecmp(cmd, "CustomLog")) {
495 ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
496 } else if (!av_strcasecmp(cmd, "LoadModule")) {
497 ERROR("Loadable modules are no longer supported\n");
499 ERROR("Incorrect keyword: '%s'\n", cmd);
503 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
504 int line_num, FFServerStream **pfeed)
506 FFServerStream *feed;
510 if (!av_strcasecmp(cmd, "<Feed")) {
513 feed = av_mallocz(sizeof(FFServerStream));
515 return AVERROR(ENOMEM);
516 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
517 q = strrchr(feed->filename, '>');
521 for (s = config->first_feed; s; s = s->next) {
522 if (!strcmp(feed->filename, s->filename))
523 ERROR("Feed '%s' already registered\n", s->filename);
526 feed->fmt = av_guess_format("ffm", NULL, NULL);
527 /* default feed file */
528 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
529 "/tmp/%s.ffm", feed->filename);
530 feed->feed_max_size = 5 * 1024 * 1024;
532 feed->feed = feed; /* self feeding :-) */
537 if (!av_strcasecmp(cmd, "Launch")) {
540 feed->child_argv = av_mallocz(64 * sizeof(char *));
541 if (!feed->child_argv)
542 return AVERROR(ENOMEM);
543 for (i = 0; i < 62; i++) {
544 ffserver_get_arg(arg, sizeof(arg), p);
548 feed->child_argv[i] = av_strdup(arg);
549 if (!feed->child_argv[i])
550 return AVERROR(ENOMEM);
553 feed->child_argv[i] =
554 av_asprintf("http://%s:%d/%s",
555 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
556 inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
558 if (!feed->child_argv[i])
559 return AVERROR(ENOMEM);
560 } else if (!av_strcasecmp(cmd, "ACL")) {
561 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
563 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
564 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
565 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
566 } else if (!av_strcasecmp(cmd, "Truncate")) {
567 ffserver_get_arg(arg, sizeof(arg), p);
568 /* assume Truncate is true in case no argument is specified */
572 WARNING("Truncate N syntax in configuration file is deprecated, "
573 "use Truncate alone with no arguments\n");
574 feed->truncate = strtod(arg, NULL);
576 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
580 ffserver_get_arg(arg, sizeof(arg), p);
582 fsize = strtod(p1, &p1);
583 switch(av_toupper(*p1)) {
588 fsize *= 1024 * 1024;
591 fsize *= 1024 * 1024 * 1024;
594 ERROR("Invalid file size: %s\n", arg);
597 feed->feed_max_size = (int64_t)fsize;
598 if (feed->feed_max_size < FFM_PACKET_SIZE*4)
599 ERROR("Feed max file size is too small, must be at least %d\n",
601 } else if (!av_strcasecmp(cmd, "</Feed>")) {
604 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
609 static void ffserver_apply_stream_config(AVCodecContext *enc, const AVDictionary *conf, AVDictionary **opts)
611 AVDictionaryEntry *e;
613 /* Return values from ffserver_set_*_param are ignored.
614 Values are initially parsed and checked before inserting to
618 if ((e = av_dict_get(conf, "VideoBitRateRangeMin", NULL, 0)))
619 ffserver_set_int_param(&enc->rc_min_rate, e->value, 1000, INT_MIN,
620 INT_MAX, NULL, 0, NULL);
621 if ((e = av_dict_get(conf, "VideoBitRateRangeMax", NULL, 0)))
622 ffserver_set_int_param(&enc->rc_max_rate, e->value, 1000, INT_MIN,
623 INT_MAX, NULL, 0, NULL);
624 if ((e = av_dict_get(conf, "Debug", NULL, 0)))
625 ffserver_set_int_param(&enc->debug, e->value, 0, INT_MIN, INT_MAX,
627 if ((e = av_dict_get(conf, "Strict", NULL, 0)))
628 ffserver_set_int_param(&enc->strict_std_compliance, e->value, 0,
629 INT_MIN, INT_MAX, NULL, 0, NULL);
630 if ((e = av_dict_get(conf, "VideoBufferSize", NULL, 0)))
631 ffserver_set_int_param(&enc->rc_buffer_size, e->value, 8*1024,
632 INT_MIN, INT_MAX, NULL, 0, NULL);
633 if ((e = av_dict_get(conf, "VideoBitRateTolerance", NULL, 0)))
634 ffserver_set_int_param(&enc->bit_rate_tolerance, e->value, 1000,
635 INT_MIN, INT_MAX, NULL, 0, NULL);
636 if ((e = av_dict_get(conf, "VideoBitRate", NULL, 0)))
637 ffserver_set_int_param(&enc->bit_rate, e->value, 1000, INT_MIN,
638 INT_MAX, NULL, 0, NULL);
639 if ((e = av_dict_get(conf, "VideoSizeWidth", NULL, 0)))
640 ffserver_set_int_param(&enc->width, e->value, 0, INT_MIN, INT_MAX,
642 if ((e = av_dict_get(conf, "VideoSizeHeight", NULL, 0)))
643 ffserver_set_int_param(&enc->height, e->value, 0, INT_MIN, INT_MAX,
645 if ((e = av_dict_get(conf, "PixelFormat", NULL, 0))) {
647 ffserver_set_int_param(&val, e->value, 0, INT_MIN, INT_MAX, NULL, 0,
651 if ((e = av_dict_get(conf, "VideoGopSize", NULL, 0)))
652 ffserver_set_int_param(&enc->gop_size, e->value, 0, INT_MIN, INT_MAX,
654 if ((e = av_dict_get(conf, "VideoFrameRateNum", NULL, 0)))
655 ffserver_set_int_param(&enc->time_base.num, e->value, 0, INT_MIN,
656 INT_MAX, NULL, 0, NULL);
657 if ((e = av_dict_get(conf, "VideoFrameRateDen", NULL, 0)))
658 ffserver_set_int_param(&enc->time_base.den, e->value, 0, INT_MIN,
659 INT_MAX, NULL, 0, NULL);
660 if ((e = av_dict_get(conf, "VideoQDiff", NULL, 0)))
661 ffserver_set_int_param(&enc->max_qdiff, e->value, 0, INT_MIN, INT_MAX,
663 if ((e = av_dict_get(conf, "VideoQMax", NULL, 0)))
664 ffserver_set_int_param(&enc->qmax, e->value, 0, INT_MIN, INT_MAX, NULL,
666 if ((e = av_dict_get(conf, "VideoQMin", NULL, 0)))
667 ffserver_set_int_param(&enc->qmin, e->value, 0, INT_MIN, INT_MAX, NULL,
669 if ((e = av_dict_get(conf, "LumiMask", NULL, 0)))
670 ffserver_set_float_param(&enc->lumi_masking, e->value, 0, -FLT_MAX,
671 FLT_MAX, NULL, 0, NULL);
672 if ((e = av_dict_get(conf, "DarkMask", NULL, 0)))
673 ffserver_set_float_param(&enc->dark_masking, e->value, 0, -FLT_MAX,
674 FLT_MAX, NULL, 0, NULL);
675 if (av_dict_get(conf, "BitExact", NULL, 0))
676 enc->flags |= CODEC_FLAG_BITEXACT;
677 if (av_dict_get(conf, "DctFastint", NULL, 0))
678 enc->dct_algo = FF_DCT_FASTINT;
679 if (av_dict_get(conf, "IdctSimple", NULL, 0))
680 enc->idct_algo = FF_IDCT_SIMPLE;
681 if (av_dict_get(conf, "VideoHighQuality", NULL, 0))
682 enc->mb_decision = FF_MB_DECISION_BITS;
683 if ((e = av_dict_get(conf, "VideoTag", NULL, 0)))
684 enc->codec_tag = MKTAG(e->value[0], e->value[1], e->value[2], e->value[3]);
685 if (av_dict_get(conf, "Qscale", NULL, 0)) {
686 enc->flags |= CODEC_FLAG_QSCALE;
687 ffserver_set_int_param(&enc->global_quality, e->value, FF_QP2LAMBDA,
688 INT_MIN, INT_MAX, NULL, 0, NULL);
690 if (av_dict_get(conf, "Video4MotionVector", NULL, 0)) {
691 enc->mb_decision = FF_MB_DECISION_BITS; //FIXME remove
692 enc->flags |= CODEC_FLAG_4MV;
695 if ((e = av_dict_get(conf, "AudioChannels", NULL, 0)))
696 ffserver_set_int_param(&enc->channels, e->value, 0, INT_MIN, INT_MAX,
698 if ((e = av_dict_get(conf, "AudioSampleRate", NULL, 0)))
699 ffserver_set_int_param(&enc->sample_rate, e->value, 0, INT_MIN,
700 INT_MAX, NULL, 0, NULL);
701 if ((e = av_dict_get(conf, "AudioBitRate", NULL, 0)))
702 ffserver_set_int_param(&enc->bit_rate, e->value, 0, INT_MIN, INT_MAX,
705 av_opt_set_dict2(enc, opts, AV_OPT_SEARCH_CHILDREN);
708 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
709 int line_num, FFServerStream **pstream)
711 char arg[1024], arg2[1024];
712 FFServerStream *stream;
718 if (!av_strcasecmp(cmd, "<Stream")) {
721 stream = av_mallocz(sizeof(FFServerStream));
723 return AVERROR(ENOMEM);
724 config->dummy_ctx = avcodec_alloc_context3(NULL);
725 if (!config->dummy_ctx) {
727 return AVERROR(ENOMEM);
729 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
730 q = strrchr(stream->filename, '>');
734 for (s = config->first_stream; s; s = s->next) {
735 if (!strcmp(stream->filename, s->filename))
736 ERROR("Stream '%s' already registered\n", s->filename);
739 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
741 config->audio_id = stream->fmt->audio_codec;
742 config->video_id = stream->fmt->video_codec;
744 config->audio_id = AV_CODEC_ID_NONE;
745 config->video_id = AV_CODEC_ID_NONE;
751 if (!av_strcasecmp(cmd, "Feed")) {
752 FFServerStream *sfeed;
753 ffserver_get_arg(arg, sizeof(arg), p);
754 sfeed = config->first_feed;
756 if (!strcmp(sfeed->filename, arg))
758 sfeed = sfeed->next_feed;
761 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
764 stream->feed = sfeed;
765 } else if (!av_strcasecmp(cmd, "Format")) {
766 ffserver_get_arg(arg, sizeof(arg), p);
767 if (!strcmp(arg, "status")) {
768 stream->stream_type = STREAM_TYPE_STATUS;
771 stream->stream_type = STREAM_TYPE_LIVE;
772 /* JPEG cannot be used here, so use single frame MJPEG */
773 if (!strcmp(arg, "jpeg"))
774 strcpy(arg, "mjpeg");
775 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
777 ERROR("Unknown Format: %s\n", arg);
780 config->audio_id = stream->fmt->audio_codec;
781 config->video_id = stream->fmt->video_codec;
783 } else if (!av_strcasecmp(cmd, "InputFormat")) {
784 ffserver_get_arg(arg, sizeof(arg), p);
785 stream->ifmt = av_find_input_format(arg);
787 ERROR("Unknown input format: %s\n", arg);
788 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
789 if (stream->stream_type == STREAM_TYPE_STATUS)
790 ffserver_get_arg(stream->feed_filename,
791 sizeof(stream->feed_filename), p);
793 ERROR("FaviconURL only permitted for status streams\n");
794 } else if (!av_strcasecmp(cmd, "Author") ||
795 !av_strcasecmp(cmd, "Comment") ||
796 !av_strcasecmp(cmd, "Copyright") ||
797 !av_strcasecmp(cmd, "Title")) {
800 ffserver_get_arg(arg, sizeof(arg), p);
801 for (i = 0; i < strlen(cmd); i++)
802 key[i] = av_tolower(cmd[i]);
804 WARNING("'%s' option in configuration file is deprecated, "
805 "use 'Metadata %s VALUE' instead\n", cmd, key);
806 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
808 } else if (!av_strcasecmp(cmd, "Metadata")) {
809 ffserver_get_arg(arg, sizeof(arg), p);
810 ffserver_get_arg(arg2, sizeof(arg2), p);
811 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
813 } else if (!av_strcasecmp(cmd, "Preroll")) {
814 ffserver_get_arg(arg, sizeof(arg), p);
815 stream->prebuffer = atof(arg) * 1000;
816 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
817 stream->send_on_key = 1;
818 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
819 ffserver_get_arg(arg, sizeof(arg), p);
820 config->audio_id = opt_codec(arg, AVMEDIA_TYPE_AUDIO);
821 if (config->audio_id == AV_CODEC_ID_NONE)
822 ERROR("Unknown AudioCodec: %s\n", arg);
823 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
824 ffserver_get_arg(arg, sizeof(arg), p);
825 config->video_id = opt_codec(arg, AVMEDIA_TYPE_VIDEO);
826 if (config->video_id == AV_CODEC_ID_NONE)
827 ERROR("Unknown VideoCodec: %s\n", arg);
828 } else if (!av_strcasecmp(cmd, "MaxTime")) {
829 ffserver_get_arg(arg, sizeof(arg), p);
830 stream->max_time = atof(arg) * 1000;
831 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
833 ffserver_get_arg(arg, sizeof(arg), p);
834 ffserver_set_float_param(&f, arg, 1000, 0, FLT_MAX, config, line_num,
835 "Invalid %s: %s\n", cmd, arg);
836 if (av_dict_set_int(&config->audio_conf, cmd, lrintf(f), 0) < 0)
838 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
839 ffserver_get_arg(arg, sizeof(arg), p);
840 ffserver_set_int_param(NULL, arg, 0, 1, 8, config, line_num,
841 "Invalid %s: %s, valid range is 1-8.", cmd, arg);
842 if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
844 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
845 ffserver_get_arg(arg, sizeof(arg), p);
846 ffserver_set_int_param(NULL, arg, 0, 0, INT_MAX, config, line_num,
847 "Invalid %s: %s", cmd, arg);
848 if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
850 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
851 int minrate, maxrate;
852 ffserver_get_arg(arg, sizeof(arg), p);
853 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
854 if (av_dict_set_int(&config->video_conf, "VideoBitRateRangeMin", minrate, 0) < 0 ||
855 av_dict_set_int(&config->video_conf, "VideoBitRateRangeMax", maxrate, 0) < 0)
858 ERROR("Incorrect format for VideoBitRateRange -- should be "
859 "<min>-<max>: %s\n", arg);
860 } else if (!av_strcasecmp(cmd, "Debug")) {
861 ffserver_get_arg(arg, sizeof(arg), p);
862 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num,
863 "Invalid %s: %s", cmd, arg);
864 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
866 } else if (!av_strcasecmp(cmd, "Strict")) {
867 ffserver_get_arg(arg, sizeof(arg), p);
868 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num,
869 "Invalid %s: %s", cmd, arg);
870 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
872 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
873 ffserver_get_arg(arg, sizeof(arg), p);
874 ffserver_set_int_param(NULL, arg, 8*1024, 0, INT_MAX, config, line_num,
875 "Invalid %s: %s", cmd, arg);
876 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
878 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
879 ffserver_get_arg(arg, sizeof(arg), p);
880 ffserver_set_int_param(NULL, arg, 1000, INT_MIN, INT_MAX, config,
881 line_num, "Invalid %s: %s", cmd, arg);
882 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
884 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
885 ffserver_get_arg(arg, sizeof(arg), p);
886 ffserver_set_int_param(NULL, arg, 1000, 0, INT_MAX, config, line_num,
887 "Invalid %s: %s", cmd, arg);
888 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
890 } else if (!av_strcasecmp(cmd, "VideoSize")) {
892 ffserver_get_arg(arg, sizeof(arg), p);
893 ret = av_parse_video_size(&w, &h, arg);
895 ERROR("Invalid video size '%s'\n", arg);
896 else if ((w % 16) || (h % 16))
897 ERROR("Image size must be a multiple of 16\n");
898 if (av_dict_set_int(&config->video_conf, "VideoSizeWidth", w, 0) < 0 ||
899 av_dict_set_int(&config->video_conf, "VideoSizeHeight", h, 0) < 0)
901 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
902 AVRational frame_rate;
903 ffserver_get_arg(arg, sizeof(arg), p);
904 if (av_parse_video_rate(&frame_rate, arg) < 0) {
905 ERROR("Incorrect frame rate: %s\n", arg);
907 if (av_dict_set_int(&config->video_conf, "VideoFrameRateNum", frame_rate.num, 0) < 0 ||
908 av_dict_set_int(&config->video_conf, "VideoFrameRateDen", frame_rate.den, 0) < 0)
911 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
912 enum AVPixelFormat pix_fmt;
913 ffserver_get_arg(arg, sizeof(arg), p);
914 pix_fmt = av_get_pix_fmt(arg);
915 if (pix_fmt == AV_PIX_FMT_NONE)
916 ERROR("Unknown pixel format: %s\n", arg);
917 if (av_dict_set_int(&config->video_conf, cmd, pix_fmt, 0) < 0)
919 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
920 ffserver_get_arg(arg, sizeof(arg), p);
921 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num,
922 "Invalid %s: %s", cmd, arg);
923 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
925 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
926 if (av_dict_set(&config->video_conf, cmd, "1", 0) < 0)
928 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
929 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
931 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
932 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
934 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
935 !av_strcasecmp(cmd, "AVOptionAudio")) {
937 ffserver_get_arg(arg, sizeof(arg), p);
938 ffserver_get_arg(arg2, sizeof(arg2), p);
939 if (!av_strcasecmp(cmd, "AVOptionVideo"))
940 ret = ffserver_save_avoption(arg, arg2, &config->video_opts, AV_OPT_FLAG_VIDEO_PARAM ,config, line_num);
942 ret = ffserver_save_avoption(arg, arg2, &config->audio_opts, AV_OPT_FLAG_AUDIO_PARAM ,config, line_num);
945 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
946 !av_strcasecmp(cmd, "AVPresetAudio")) {
947 char **preset = NULL;
948 ffserver_get_arg(arg, sizeof(arg), p);
949 if (!av_strcasecmp(cmd, "AVPresetVideo")) {
950 preset = &config->video_preset;
951 ffserver_opt_preset(arg, NULL, 0, NULL, &config->video_id);
953 preset = &config->audio_preset;
954 ffserver_opt_preset(arg, NULL, 0, &config->audio_id, NULL);
956 *preset = av_strdup(arg);
958 return AVERROR(ENOMEM);
959 } else if (!av_strcasecmp(cmd, "VideoTag")) {
960 ffserver_get_arg(arg, sizeof(arg), p);
961 if (strlen(arg) == 4) {
962 if (av_dict_set(&config->video_conf, "VideoTag", "arg", 0) < 0)
965 } else if (!av_strcasecmp(cmd, "BitExact")) {
966 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
968 } else if (!av_strcasecmp(cmd, "DctFastint")) {
969 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
971 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
972 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
974 } else if (!av_strcasecmp(cmd, "Qscale")) {
975 ffserver_get_arg(arg, sizeof(arg), p);
976 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
978 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
979 ffserver_get_arg(arg, sizeof(arg), p);
980 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num,
981 "%s out of range\n", cmd);
982 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
984 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
985 ffserver_get_arg(arg, sizeof(arg), p);
986 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num,
987 "%s out of range\n", cmd);
988 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
990 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
991 ffserver_get_arg(arg, sizeof(arg), p);
992 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num,
993 "%s out of range\n", cmd);
994 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
996 } else if (!av_strcasecmp(cmd, "LumiMask")) {
997 ffserver_get_arg(arg, sizeof(arg), p);
998 ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config,
999 line_num, "Invalid %s: %s", cmd, arg);
1000 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
1002 } else if (!av_strcasecmp(cmd, "DarkMask")) {
1003 ffserver_get_arg(arg, sizeof(arg), p);
1004 ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config,
1005 line_num, "Invalid %s: %s", cmd, arg);
1006 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
1008 } else if (!av_strcasecmp(cmd, "NoVideo")) {
1009 config->video_id = AV_CODEC_ID_NONE;
1010 } else if (!av_strcasecmp(cmd, "NoAudio")) {
1011 config->audio_id = AV_CODEC_ID_NONE;
1012 } else if (!av_strcasecmp(cmd, "ACL")) {
1013 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
1015 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
1016 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
1017 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
1018 ffserver_get_arg(arg, sizeof(arg), p);
1019 av_freep(&stream->rtsp_option);
1020 stream->rtsp_option = av_strdup(arg);
1021 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
1022 ffserver_get_arg(arg, sizeof(arg), p);
1023 if (resolve_host(&stream->multicast_ip, arg))
1024 ERROR("Invalid host/IP address: %s\n", arg);
1025 stream->is_multicast = 1;
1026 stream->loop = 1; /* default is looping */
1027 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
1028 ffserver_get_arg(arg, sizeof(arg), p);
1029 ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
1030 "Invalid MulticastPort: %s\n", arg);
1031 stream->multicast_port = val;
1032 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
1033 ffserver_get_arg(arg, sizeof(arg), p);
1034 ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
1035 line_num, "Invalid MulticastTTL: %s\n", arg);
1036 stream->multicast_ttl = val;
1037 } else if (!av_strcasecmp(cmd, "NoLoop")) {
1039 } else if (!av_strcasecmp(cmd, "</Stream>")) {
1040 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
1041 if (config->audio_id != AV_CODEC_ID_NONE) {
1042 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->audio_id));
1043 if (config->audio_preset &&
1044 ffserver_opt_preset(arg, audio_enc, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
1046 ERROR("Could not apply preset '%s'\n", arg);
1047 ffserver_apply_stream_config(audio_enc, config->audio_conf,
1048 &config->audio_opts);
1049 add_codec(stream, audio_enc);
1051 if (config->video_id != AV_CODEC_ID_NONE) {
1052 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->video_id));
1053 if (config->video_preset &&
1054 ffserver_opt_preset(arg, video_enc, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
1056 ERROR("Could not apply preset '%s'\n", arg);
1057 ffserver_apply_stream_config(video_enc, config->video_conf,
1058 &config->video_opts);
1059 add_codec(stream, video_enc);
1062 av_dict_free(&config->video_opts);
1063 av_dict_free(&config->video_conf);
1064 av_dict_free(&config->audio_opts);
1065 av_dict_free(&config->audio_conf);
1066 av_freep(&config->video_preset);
1067 av_freep(&config->audio_preset);
1068 avcodec_free_context(&config->dummy_ctx);
1070 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
1071 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
1074 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
1078 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
1079 av_dict_free(&config->video_opts);
1080 av_dict_free(&config->video_conf);
1081 av_dict_free(&config->audio_opts);
1082 av_dict_free(&config->audio_conf);
1083 av_freep(&config->video_preset);
1084 av_freep(&config->audio_preset);
1085 avcodec_free_context(&config->dummy_ctx);
1086 return AVERROR(ENOMEM);
1089 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1090 int line_num, FFServerStream **predirect)
1092 FFServerStream *redirect;
1093 av_assert0(predirect);
1094 redirect = *predirect;
1096 if (!av_strcasecmp(cmd, "<Redirect")) {
1098 redirect = av_mallocz(sizeof(FFServerStream));
1100 return AVERROR(ENOMEM);
1102 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1103 q = strrchr(redirect->filename, '>');
1106 redirect->stream_type = STREAM_TYPE_REDIRECT;
1107 *predirect = redirect;
1110 av_assert0(redirect);
1111 if (!av_strcasecmp(cmd, "URL")) {
1112 ffserver_get_arg(redirect->feed_filename,
1113 sizeof(redirect->feed_filename), p);
1114 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1115 if (!redirect->feed_filename[0])
1116 ERROR("No URL found for <Redirect>\n");
1119 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1124 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1131 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1132 FFServerStream **last_feed, *feed = NULL;
1137 f = fopen(filename, "r");
1139 ret = AVERROR(errno);
1140 av_log(NULL, AV_LOG_ERROR,
1141 "Could not open the configuration file '%s'\n", filename);
1145 config->first_stream = NULL;
1146 last_stream = &config->first_stream;
1147 config->first_feed = NULL;
1148 last_feed = &config->first_feed;
1149 config->errors = config->warnings = 0;
1152 if (fgets(line, sizeof(line), f) == NULL)
1156 while (av_isspace(*p))
1158 if (*p == '\0' || *p == '#')
1161 ffserver_get_arg(cmd, sizeof(cmd), &p);
1163 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1164 int opening = !av_strcasecmp(cmd, "<Feed");
1165 if (opening && (stream || feed || redirect)) {
1166 ERROR("Already in a tag\n");
1168 if ((ret = ffserver_parse_config_feed(config, cmd, &p, line_num, &feed)) < 0)
1171 /* add in stream list */
1172 *last_stream = feed;
1173 last_stream = &feed->next;
1174 /* add in feed list */
1176 last_feed = &feed->next_feed;
1179 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1180 int opening = !av_strcasecmp(cmd, "<Stream");
1181 if (opening && (stream || feed || redirect)) {
1182 ERROR("Already in a tag\n");
1184 if ((ret = ffserver_parse_config_stream(config, cmd, &p, line_num, &stream)) < 0)
1187 /* add in stream list */
1188 *last_stream = stream;
1189 last_stream = &stream->next;
1192 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1193 int opening = !av_strcasecmp(cmd, "<Redirect");
1194 if (opening && (stream || feed || redirect))
1195 ERROR("Already in a tag\n");
1197 if ((ret = ffserver_parse_config_redirect(config, cmd, &p, line_num, &redirect)) < 0)
1200 /* add in stream list */
1201 *last_stream = redirect;
1202 last_stream = &redirect->next;
1206 ffserver_parse_config_global(config, cmd, &p, line_num);
1214 return AVERROR(EINVAL);