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) != 0) {
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) != 0) {
133 fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n",
134 filename, line_num, arg);
140 FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl));
141 FFServerIPAddressACL **naclp = 0;
147 naclp = &stream->acl;
153 fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n",
160 naclp = &(*naclp)->next;
168 /* add a codec and set the default parameters */
169 static void add_codec(FFServerStream *stream, AVCodecContext *av)
173 if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams))
176 /* compute default parameters */
177 switch(av->codec_type) {
178 case AVMEDIA_TYPE_AUDIO:
179 if (av->bit_rate == 0)
180 av->bit_rate = 64000;
181 if (av->sample_rate == 0)
182 av->sample_rate = 22050;
183 if (av->channels == 0)
186 case AVMEDIA_TYPE_VIDEO:
187 if (av->bit_rate == 0)
188 av->bit_rate = 64000;
189 if (av->time_base.num == 0){
190 av->time_base.den = 5;
191 av->time_base.num = 1;
193 if (av->width == 0 || av->height == 0) {
197 /* Bitrate tolerance is less for streaming */
198 if (av->bit_rate_tolerance == 0)
199 av->bit_rate_tolerance = FFMAX(av->bit_rate / 4,
200 (int64_t)av->bit_rate*av->time_base.num/av->time_base.den);
205 if (av->max_qdiff == 0)
210 if (!av->nsse_weight)
213 av->frame_skip_cmp = FF_CMP_DCTMAX;
215 av->me_method = ME_EPZS;
216 av->rc_buffer_aggressivity = 1.0;
219 av->rc_eq = av_strdup("tex^qComp");
220 if (!av->i_quant_factor)
221 av->i_quant_factor = -0.8;
222 if (!av->b_quant_factor)
223 av->b_quant_factor = 1.25;
224 if (!av->b_quant_offset)
225 av->b_quant_offset = 1.25;
226 if (!av->rc_max_rate)
227 av->rc_max_rate = av->bit_rate * 2;
229 if (av->rc_max_rate && !av->rc_buffer_size) {
230 av->rc_buffer_size = av->rc_max_rate;
239 st = av_mallocz(sizeof(AVStream));
243 stream->streams[stream->nb_streams++] = st;
246 static enum AVCodecID opt_codec(const char *name, enum AVMediaType type)
248 AVCodec *codec = avcodec_find_encoder_by_name(name);
250 if (!codec || codec->type != type)
251 return AV_CODEC_ID_NONE;
255 static int ffserver_opt_default(const char *opt, const char *arg,
256 AVCodecContext *avctx, int type)
259 const AVOption *o = av_opt_find(avctx, opt, NULL, type, 0);
261 ret = av_opt_set(avctx, opt, arg, 0);
265 static int ffserver_opt_preset(const char *arg,
266 AVCodecContext *avctx, int type,
267 enum AVCodecID *audio_id, enum AVCodecID *video_id)
270 char filename[1000], tmp[1000], tmp2[1000], line[1000];
272 AVCodec *codec = NULL;
275 codec = avcodec_find_encoder(avctx->codec_id);
277 if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
278 codec ? codec->name : NULL))) {
279 fprintf(stderr, "File for preset '%s' not found\n", arg);
280 return AVERROR(EINVAL);
284 int e= fscanf(f, "%999[^\n]\n", line) - 1;
285 if(line[0] == '#' && !e)
287 e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
289 fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
290 ret = AVERROR(EINVAL);
293 if (audio_id && !strcmp(tmp, "acodec")) {
294 *audio_id = opt_codec(tmp2, AVMEDIA_TYPE_AUDIO);
295 } else if (video_id && !strcmp(tmp, "vcodec")){
296 *video_id = opt_codec(tmp2, AVMEDIA_TYPE_VIDEO);
297 } else if(!strcmp(tmp, "scodec")) {
298 /* opt_subtitle_codec(tmp2); */
299 } else if (avctx && (ret = ffserver_opt_default(tmp, tmp2, avctx, type)) < 0) {
300 fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
310 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type)
312 AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type);
315 AVOutputFormat *stream_fmt;
316 char stream_format_name[64];
318 snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name);
319 stream_fmt = av_guess_format(stream_format_name, NULL, NULL);
328 static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl)
330 av_log(NULL, log_level, "%s:%d: ", filename, line_num);
331 av_vlog(NULL, log_level, fmt, vl);
335 static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...)
339 vreport_config_error(filename, line_num, log_level, errors, fmt, vl);
343 static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max,
344 FFServerConfig *config, int line_num, const char *error_msg, ...)
348 if (!value || !value[0])
351 tmp = strtol(value, &tailp, 0);
352 if (tmp < min || tmp > max)
355 if (FFABS(tmp) > INT_MAX / FFABS(factor))
359 if (tailp[0] || errno)
367 va_start(vl, error_msg);
368 vreport_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, error_msg, vl);
371 return AVERROR(EINVAL);
374 static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max,
375 FFServerConfig *config, int line_num, const char *error_msg, ...)
379 if (!value || !value[0])
382 tmp = strtod(value, &tailp);
383 if (tmp < min || tmp > max)
387 if (tailp[0] || errno)
395 va_start(vl, error_msg);
396 vreport_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, error_msg, vl);
399 return AVERROR(EINVAL);
402 #define ERROR(...) report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, __VA_ARGS__)
403 #define WARNING(...) report_config_error(config->filename, line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
405 static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
406 const char **p, int line_num)
410 if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
411 if (!av_strcasecmp(cmd, "Port"))
412 WARNING("Port option is deprecated, use HTTPPort instead\n");
413 ffserver_get_arg(arg, sizeof(arg), p);
415 if (val < 1 || val > 65536)
416 ERROR("Invalid port: %s\n", arg);
418 WARNING("Trying to use IETF assigned system port: %d\n", val);
419 config->http_addr.sin_port = htons(val);
420 } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
421 if (!av_strcasecmp(cmd, "BindAddress"))
422 WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
423 ffserver_get_arg(arg, sizeof(arg), p);
424 if (resolve_host(&config->http_addr.sin_addr, arg) != 0)
425 ERROR("%s:%d: Invalid host/IP address: %s\n", arg);
426 } else if (!av_strcasecmp(cmd, "NoDaemon")) {
427 WARNING("NoDaemon option has no effect, you should remove it\n");
428 } else if (!av_strcasecmp(cmd, "RTSPPort")) {
429 ffserver_get_arg(arg, sizeof(arg), p);
431 if (val < 1 || val > 65536)
432 ERROR("%s:%d: Invalid port: %s\n", arg);
433 config->rtsp_addr.sin_port = htons(atoi(arg));
434 } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
435 ffserver_get_arg(arg, sizeof(arg), p);
436 if (resolve_host(&config->rtsp_addr.sin_addr, arg) != 0)
437 ERROR("Invalid host/IP address: %s\n", arg);
438 } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
439 ffserver_get_arg(arg, sizeof(arg), p);
441 if (val < 1 || val > 65536)
442 ERROR("Invalid MaxHTTPConnections: %s\n", arg);
443 config->nb_max_http_connections = val;
444 } else if (!av_strcasecmp(cmd, "MaxClients")) {
445 ffserver_get_arg(arg, sizeof(arg), p);
447 if (val < 1 || val > config->nb_max_http_connections)
448 ERROR("Invalid MaxClients: %s\n", arg);
450 config->nb_max_connections = val;
451 } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
453 ffserver_get_arg(arg, sizeof(arg), p);
454 llval = strtoll(arg, NULL, 10);
455 if (llval < 10 || llval > 10000000)
456 ERROR("Invalid MaxBandwidth: %s\n", arg);
458 config->max_bandwidth = llval;
459 } else if (!av_strcasecmp(cmd, "CustomLog")) {
461 ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
462 } else if (!av_strcasecmp(cmd, "LoadModule")) {
463 ERROR("Loadable modules no longer supported\n");
465 ERROR("Incorrect keyword: '%s'\n", cmd);
469 static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
470 int line_num, FFServerStream **pfeed)
472 FFServerStream *feed;
476 if (!av_strcasecmp(cmd, "<Feed")) {
479 feed = av_mallocz(sizeof(FFServerStream));
481 return AVERROR(ENOMEM);
482 ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
483 q = strrchr(feed->filename, '>');
487 for (s = config->first_feed; s; s = s->next) {
488 if (!strcmp(feed->filename, s->filename))
489 ERROR("Feed '%s' already registered\n", s->filename);
492 feed->fmt = av_guess_format("ffm", NULL, NULL);
493 /* default feed file */
494 snprintf(feed->feed_filename, sizeof(feed->feed_filename),
495 "/tmp/%s.ffm", feed->filename);
496 feed->feed_max_size = 5 * 1024 * 1024;
498 feed->feed = feed; /* self feeding :-) */
503 if (!av_strcasecmp(cmd, "Launch")) {
506 feed->child_argv = av_mallocz(64 * sizeof(char *));
507 if (!feed->child_argv)
508 return AVERROR(ENOMEM);
509 for (i = 0; i < 62; i++) {
510 ffserver_get_arg(arg, sizeof(arg), p);
514 feed->child_argv[i] = av_strdup(arg);
515 if (!feed->child_argv[i])
516 return AVERROR(ENOMEM);
519 feed->child_argv[i] =
520 av_asprintf("http://%s:%d/%s",
521 (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
522 inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
524 if (!feed->child_argv[i])
525 return AVERROR(ENOMEM);
526 } else if (!av_strcasecmp(cmd, "ACL")) {
527 ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename, line_num);
528 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
529 ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
530 feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
531 } else if (!av_strcasecmp(cmd, "Truncate")) {
532 ffserver_get_arg(arg, sizeof(arg), p);
533 /* assume Truncate is true in case no argument is specified */
537 WARNING("Truncate N syntax in configuration file is deprecated, "
538 "use Truncate alone with no arguments\n");
539 feed->truncate = strtod(arg, NULL);
541 } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
545 ffserver_get_arg(arg, sizeof(arg), p);
547 fsize = strtod(p1, &p1);
548 switch(av_toupper(*p1)) {
553 fsize *= 1024 * 1024;
556 fsize *= 1024 * 1024 * 1024;
559 feed->feed_max_size = (int64_t)fsize;
560 if (feed->feed_max_size < FFM_PACKET_SIZE*4)
561 ERROR("Feed max file size is too small, must be at least %d\n", FFM_PACKET_SIZE*4);
562 } else if (!av_strcasecmp(cmd, "</Feed>")) {
565 ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
570 static int ffserver_apply_stream_config(AVCodecContext *enc, const AVDictionary *conf, AVDictionary **opts)
572 AVDictionaryEntry *e;
575 /* Return values from ffserver_set_*_param are ignored.
576 Values are initially parsed and checked before inserting to AVDictionary. */
579 if ((e = av_dict_get(conf, "VideoBitRateRangeMin", NULL, 0)))
580 ffserver_set_int_param(&enc->rc_min_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
581 if ((e = av_dict_get(conf, "VideoBitRateRangeMax", NULL, 0)))
582 ffserver_set_int_param(&enc->rc_max_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
583 if ((e = av_dict_get(conf, "Debug", NULL, 0)))
584 ffserver_set_int_param(&enc->debug, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
585 if ((e = av_dict_get(conf, "Strict", NULL, 0)))
586 ffserver_set_int_param(&enc->strict_std_compliance, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
587 if ((e = av_dict_get(conf, "VideoBufferSize", NULL, 0)))
588 ffserver_set_int_param(&enc->rc_buffer_size, e->value, 8*1024, INT_MIN, INT_MAX, NULL, 0, NULL);
589 if ((e = av_dict_get(conf, "VideoBitRateTolerance", NULL, 0)))
590 ffserver_set_int_param(&enc->bit_rate_tolerance, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
591 if ((e = av_dict_get(conf, "VideoBitRate", NULL, 0)))
592 ffserver_set_int_param(&enc->bit_rate, e->value, 1000, INT_MIN, INT_MAX, NULL, 0, NULL);
593 if ((e = av_dict_get(conf, "VideoSizeWidth", NULL, 0)))
594 ffserver_set_int_param(&enc->width, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
595 if ((e = av_dict_get(conf, "VideoSizeHeight", NULL, 0)))
596 ffserver_set_int_param(&enc->height, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
597 if ((e = av_dict_get(conf, "PixelFormat", NULL, 0)))
598 ffserver_set_int_param(&enc->pix_fmt, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
599 if ((e = av_dict_get(conf, "VideoGopSize", NULL, 0)))
600 ffserver_set_int_param(&enc->gop_size, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
601 if ((e = av_dict_get(conf, "VideoFrameRateNum", NULL, 0)))
602 ffserver_set_int_param(&enc->time_base.num, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
603 if ((e = av_dict_get(conf, "VideoFrameRateDen", NULL, 0)))
604 ffserver_set_int_param(&enc->time_base.den, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
605 if ((e = av_dict_get(conf, "VideoQDiff", NULL, 0)))
606 ffserver_set_int_param(&enc->max_qdiff, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
607 if ((e = av_dict_get(conf, "VideoQMax", NULL, 0)))
608 ffserver_set_int_param(&enc->qmax, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
609 if ((e = av_dict_get(conf, "VideoQMin", NULL, 0)))
610 ffserver_set_int_param(&enc->qmin, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
611 if ((e = av_dict_get(conf, "LumiMask", NULL, 0)))
612 ffserver_set_float_param(&enc->lumi_masking, e->value, 0, -FLT_MAX, FLT_MAX, NULL, 0, NULL);
613 if ((e = av_dict_get(conf, "DarkMask", NULL, 0)))
614 ffserver_set_float_param(&enc->dark_masking, e->value, 0, -FLT_MAX, FLT_MAX, NULL, 0, NULL);
615 if (av_dict_get(conf, "BitExact", NULL, 0))
616 enc->flags |= CODEC_FLAG_BITEXACT;
617 if (av_dict_get(conf, "DctFastint", NULL, 0))
618 enc->dct_algo = FF_DCT_FASTINT;
619 if (av_dict_get(conf, "IdctSimple", NULL, 0))
620 enc->idct_algo = FF_IDCT_SIMPLE;
621 if (av_dict_get(conf, "VideoHighQuality", NULL, 0))
622 enc->mb_decision = FF_MB_DECISION_BITS;
623 if ((e = av_dict_get(conf, "VideoTag", NULL, 0)))
624 enc->codec_tag = MKTAG(e->value[0], e->value[1], e->value[2], e->value[3]);
625 if (av_dict_get(conf, "Qscale", NULL, 0)) {
626 enc->flags |= CODEC_FLAG_QSCALE;
627 ffserver_set_int_param(&enc->global_quality, e->value, FF_QP2LAMBDA, INT_MIN, INT_MAX, NULL, 0, NULL);
629 if (av_dict_get(conf, "Video4MotionVector", NULL, 0)) {
630 enc->mb_decision = FF_MB_DECISION_BITS; //FIXME remove
631 enc->flags |= CODEC_FLAG_4MV;
634 if ((e = av_dict_get(conf, "AudioChannels", NULL, 0)))
635 ffserver_set_int_param(&enc->channels, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
636 if ((e = av_dict_get(conf, "AudioSampleRate", NULL, 0)))
637 ffserver_set_int_param(&enc->sample_rate, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
638 if ((e = av_dict_get(conf, "AudioBitRate", NULL, 0)))
639 ffserver_set_int_param(&enc->bit_rate, e->value, 0, INT_MIN, INT_MAX, NULL, 0, NULL);
641 av_opt_set_dict2(enc, opts, AV_OPT_SEARCH_CHILDREN);
643 while (e = av_dict_get(*opts, "", e, AV_DICT_IGNORE_SUFFIX)) {
644 av_log(NULL, AV_LOG_ERROR, "Provided AVOption '%s' doesn't match any existing option.\n", e->key);
645 ret = AVERROR(EINVAL);
651 static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
652 int line_num, FFServerStream **pstream)
654 char arg[1024], arg2[1024];
655 FFServerStream *stream;
660 if (!av_strcasecmp(cmd, "<Stream")) {
663 stream = av_mallocz(sizeof(FFServerStream));
665 return AVERROR(ENOMEM);
666 ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
667 q = strrchr(stream->filename, '>');
671 for (s = config->first_stream; s; s = s->next) {
672 if (!strcmp(stream->filename, s->filename))
673 ERROR("Stream '%s' already registered\n", s->filename);
676 stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
678 config->audio_id = stream->fmt->audio_codec;
679 config->video_id = stream->fmt->video_codec;
681 config->audio_id = AV_CODEC_ID_NONE;
682 config->video_id = AV_CODEC_ID_NONE;
688 if (!av_strcasecmp(cmd, "Feed")) {
689 FFServerStream *sfeed;
690 ffserver_get_arg(arg, sizeof(arg), p);
691 sfeed = config->first_feed;
693 if (!strcmp(sfeed->filename, arg))
695 sfeed = sfeed->next_feed;
698 ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg, stream->filename);
700 stream->feed = sfeed;
701 } else if (!av_strcasecmp(cmd, "Format")) {
702 ffserver_get_arg(arg, sizeof(arg), p);
703 if (!strcmp(arg, "status")) {
704 stream->stream_type = STREAM_TYPE_STATUS;
707 stream->stream_type = STREAM_TYPE_LIVE;
708 /* JPEG cannot be used here, so use single frame MJPEG */
709 if (!strcmp(arg, "jpeg"))
710 strcpy(arg, "mjpeg");
711 stream->fmt = ffserver_guess_format(arg, NULL, NULL);
713 ERROR("Unknown Format: %s\n", arg);
716 config->audio_id = stream->fmt->audio_codec;
717 config->video_id = stream->fmt->video_codec;
719 } else if (!av_strcasecmp(cmd, "InputFormat")) {
720 ffserver_get_arg(arg, sizeof(arg), p);
721 stream->ifmt = av_find_input_format(arg);
723 ERROR("Unknown input format: %s\n", arg);
724 } else if (!av_strcasecmp(cmd, "FaviconURL")) {
725 if (stream->stream_type == STREAM_TYPE_STATUS)
726 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
728 ERROR("FaviconURL only permitted for status streams\n");
729 } else if (!av_strcasecmp(cmd, "Author") ||
730 !av_strcasecmp(cmd, "Comment") ||
731 !av_strcasecmp(cmd, "Copyright") ||
732 !av_strcasecmp(cmd, "Title")) {
735 ffserver_get_arg(arg, sizeof(arg), p);
736 for (i = 0; i < strlen(cmd); i++)
737 key[i] = av_tolower(cmd[i]);
739 WARNING("'%s' option in configuration file is deprecated, "
740 "use 'Metadata %s VALUE' instead\n", cmd, key);
741 if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
743 } else if (!av_strcasecmp(cmd, "Metadata")) {
744 ffserver_get_arg(arg, sizeof(arg), p);
745 ffserver_get_arg(arg2, sizeof(arg2), p);
746 if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
748 } else if (!av_strcasecmp(cmd, "Preroll")) {
749 ffserver_get_arg(arg, sizeof(arg), p);
750 stream->prebuffer = atof(arg) * 1000;
751 } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
752 stream->send_on_key = 1;
753 } else if (!av_strcasecmp(cmd, "AudioCodec")) {
754 ffserver_get_arg(arg, sizeof(arg), p);
755 config->audio_id = opt_codec(arg, AVMEDIA_TYPE_AUDIO);
756 if (config->audio_id == AV_CODEC_ID_NONE)
757 ERROR("Unknown AudioCodec: %s\n", arg);
758 } else if (!av_strcasecmp(cmd, "VideoCodec")) {
759 ffserver_get_arg(arg, sizeof(arg), p);
760 config->video_id = opt_codec(arg, AVMEDIA_TYPE_VIDEO);
761 if (config->video_id == AV_CODEC_ID_NONE)
762 ERROR("Unknown VideoCodec: %s\n", arg);
763 } else if (!av_strcasecmp(cmd, "MaxTime")) {
764 ffserver_get_arg(arg, sizeof(arg), p);
765 stream->max_time = atof(arg) * 1000;
766 } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
767 ffserver_get_arg(arg, sizeof(arg), p);
769 ffserver_set_float_param(&f, arg, 1000, 0, FLT_MAX, config, line_num, "Invalid %s: %s\n", cmd, arg);
770 if (av_dict_set_int(&config->audio_conf, cmd, lrintf(f), 0) < 0)
772 } else if (!av_strcasecmp(cmd, "AudioChannels")) {
773 ffserver_get_arg(arg, sizeof(arg), p);
774 ffserver_set_int_param(NULL, arg, 0, 1, 8, config, line_num, "Invalid %s: %s, valid range is 1-8.", cmd, arg);
775 if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
777 } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
778 ffserver_get_arg(arg, sizeof(arg), p);
779 ffserver_set_int_param(NULL, arg, 0, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
780 if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
782 } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
783 int minrate, maxrate;
784 ffserver_get_arg(arg, sizeof(arg), p);
785 if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
786 if (av_dict_set_int(&config->video_conf, "VideoBitRateRangeMin", minrate, 0) < 0 ||
787 av_dict_set_int(&config->video_conf, "VideoBitRateRangeMax", maxrate, 0) < 0)
790 ERROR("Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n", arg);
791 } else if (!av_strcasecmp(cmd, "Debug")) {
792 ffserver_get_arg(arg, sizeof(arg), p);
793 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
794 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
796 } else if (!av_strcasecmp(cmd, "Strict")) {
797 ffserver_get_arg(arg, sizeof(arg), p);
798 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
799 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
801 } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
802 ffserver_get_arg(arg, sizeof(arg), p);
803 ffserver_set_int_param(NULL, arg, 8*1024, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
804 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
806 } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
807 ffserver_get_arg(arg, sizeof(arg), p);
808 ffserver_set_int_param(NULL, arg, 1000, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
809 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
811 } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
812 ffserver_get_arg(arg, sizeof(arg), p);
813 ffserver_set_int_param(NULL, arg, 1000, 0, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
814 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
816 } else if (!av_strcasecmp(cmd, "VideoSize")) {
818 ffserver_get_arg(arg, sizeof(arg), p);
819 ret = av_parse_video_size(&w, &h, arg);
821 ERROR("Invalid video size '%s'\n", arg);
822 else if ((w % 16) || (h % 16))
823 ERROR("Image size must be a multiple of 16\n");
824 if (av_dict_set_int(&config->video_conf, "VideoSizeWidth", w, 0) < 0 ||
825 av_dict_set_int(&config->video_conf, "VideoSizeHeight", h, 0) < 0)
827 } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
828 AVRational frame_rate;
829 ffserver_get_arg(arg, sizeof(arg), p);
830 if (av_parse_video_rate(&frame_rate, arg) < 0) {
831 ERROR("Incorrect frame rate: %s\n", arg);
833 if (av_dict_set_int(&config->video_conf, "VideoFrameRateNum", frame_rate.num, 0) < 0 ||
834 av_dict_set_int(&config->video_conf, "VideoFrameRateDen", frame_rate.den, 0) < 0)
837 } else if (!av_strcasecmp(cmd, "PixelFormat")) {
838 enum AVPixelFormat pix_fmt;
839 ffserver_get_arg(arg, sizeof(arg), p);
840 pix_fmt = av_get_pix_fmt(arg);
841 if (pix_fmt == AV_PIX_FMT_NONE)
842 ERROR("Unknown pixel format: %s\n", arg);
843 if (av_dict_set_int(&config->video_conf, cmd, pix_fmt, 0) < 0)
845 } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
846 ffserver_get_arg(arg, sizeof(arg), p);
847 ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
848 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
850 } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
851 if (av_dict_set(&config->video_conf, cmd, "1", 0) < 0)
853 } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
854 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
856 } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
857 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
859 } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
860 !av_strcasecmp(cmd, "AVOptionAudio")) {
862 ffserver_get_arg(arg, sizeof(arg), p);
863 ffserver_get_arg(arg2, sizeof(arg2), p);
864 if (!av_strcasecmp(cmd, "AVOptionVideo"))
865 dict = &config->video_opts;
867 dict = &config->audio_opts;
868 if (av_dict_set(dict, arg, arg2, 0) < 0)
870 } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
871 !av_strcasecmp(cmd, "AVPresetAudio")) {
872 char **preset = NULL;
873 ffserver_get_arg(arg, sizeof(arg), p);
874 if (!av_strcasecmp(cmd, "AVPresetVideo")) {
875 preset = &config->video_preset;
876 ffserver_opt_preset(arg, NULL, 0, NULL, &config->video_id);
878 preset = &config->audio_preset;
879 ffserver_opt_preset(arg, NULL, 0, &config->audio_id, NULL);
881 *preset = av_strdup(arg);
883 return AVERROR(ENOMEM);
884 } else if (!av_strcasecmp(cmd, "VideoTag")) {
885 ffserver_get_arg(arg, sizeof(arg), p);
886 if (strlen(arg) == 4) {
887 if (av_dict_set(&config->video_conf, "VideoTag", "arg", 0) < 0)
890 } else if (!av_strcasecmp(cmd, "BitExact")) {
891 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
893 } else if (!av_strcasecmp(cmd, "DctFastint")) {
894 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
896 } else if (!av_strcasecmp(cmd, "IdctSimple")) {
897 if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
899 } else if (!av_strcasecmp(cmd, "Qscale")) {
900 ffserver_get_arg(arg, sizeof(arg), p);
901 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
903 } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
904 ffserver_get_arg(arg, sizeof(arg), p);
905 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
906 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
908 } else if (!av_strcasecmp(cmd, "VideoQMax")) {
909 ffserver_get_arg(arg, sizeof(arg), p);
910 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
911 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
913 } else if (!av_strcasecmp(cmd, "VideoQMin")) {
914 ffserver_get_arg(arg, sizeof(arg), p);
915 ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num, "%s out of range\n", cmd);
916 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
918 } else if (!av_strcasecmp(cmd, "LumiMask")) {
919 ffserver_get_arg(arg, sizeof(arg), p);
920 ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
921 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
923 } else if (!av_strcasecmp(cmd, "DarkMask")) {
924 ffserver_get_arg(arg, sizeof(arg), p);
925 ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config, line_num, "Invalid %s: %s", cmd, arg);
926 if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
928 } else if (!av_strcasecmp(cmd, "NoVideo")) {
929 config->video_id = AV_CODEC_ID_NONE;
930 } else if (!av_strcasecmp(cmd, "NoAudio")) {
931 config->audio_id = AV_CODEC_ID_NONE;
932 } else if (!av_strcasecmp(cmd, "ACL")) {
933 ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename, line_num);
934 } else if (!av_strcasecmp(cmd, "DynamicACL")) {
935 ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
936 } else if (!av_strcasecmp(cmd, "RTSPOption")) {
937 ffserver_get_arg(arg, sizeof(arg), p);
938 av_freep(&stream->rtsp_option);
939 stream->rtsp_option = av_strdup(arg);
940 } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
941 ffserver_get_arg(arg, sizeof(arg), p);
942 if (resolve_host(&stream->multicast_ip, arg) != 0)
943 ERROR("Invalid host/IP address: %s\n", arg);
944 stream->is_multicast = 1;
945 stream->loop = 1; /* default is looping */
946 } else if (!av_strcasecmp(cmd, "MulticastPort")) {
947 ffserver_get_arg(arg, sizeof(arg), p);
948 stream->multicast_port = atoi(arg);
949 } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
950 ffserver_get_arg(arg, sizeof(arg), p);
951 stream->multicast_ttl = atoi(arg);
952 } else if (!av_strcasecmp(cmd, "NoLoop")) {
954 } else if (!av_strcasecmp(cmd, "</Stream>")) {
955 if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
956 if (config->audio_id != AV_CODEC_ID_NONE) {
957 AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->audio_id));
958 if (config->audio_preset &&
959 ffserver_opt_preset(arg, audio_enc, AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
961 ERROR("Could not apply preset '%s'\n", arg);
962 if (ffserver_apply_stream_config(audio_enc, config->audio_conf, &config->audio_opts) < 0)
964 add_codec(stream, audio_enc);
966 if (config->video_id != AV_CODEC_ID_NONE) {
967 AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->video_id));
968 if (config->video_preset &&
969 ffserver_opt_preset(arg, video_enc, AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_ENCODING_PARAM,
971 ERROR("Could not apply preset '%s'\n", arg);
972 if (ffserver_apply_stream_config(video_enc, config->video_conf, &config->video_opts) < 0)
974 add_codec(stream, video_enc);
977 av_dict_free(&config->video_opts);
978 av_dict_free(&config->video_conf);
979 av_dict_free(&config->audio_opts);
980 av_dict_free(&config->audio_conf);
981 av_freep(&config->video_preset);
982 av_freep(&config->audio_preset);
984 } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
985 ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
987 ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
991 av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
992 av_dict_free(&config->video_opts);
993 av_dict_free(&config->video_conf);
994 av_dict_free(&config->audio_opts);
995 av_dict_free(&config->audio_conf);
996 av_freep(&config->video_preset);
997 av_freep(&config->audio_preset);
998 return AVERROR(ENOMEM);
1001 static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
1002 int line_num, FFServerStream **predirect)
1004 FFServerStream *redirect;
1005 av_assert0(predirect);
1006 redirect = *predirect;
1008 if (!av_strcasecmp(cmd, "<Redirect")) {
1010 redirect = av_mallocz(sizeof(FFServerStream));
1012 return AVERROR(ENOMEM);
1014 ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
1015 q = strrchr(redirect->filename, '>');
1018 redirect->stream_type = STREAM_TYPE_REDIRECT;
1019 *predirect = redirect;
1022 av_assert0(redirect);
1023 if (!av_strcasecmp(cmd, "URL")) {
1024 ffserver_get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), p);
1025 } else if (!av_strcasecmp(cmd, "</Redirect>")) {
1026 if (!redirect->feed_filename[0])
1027 ERROR("No URL found for <Redirect>\n");
1030 ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
1035 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
1042 FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
1043 FFServerStream **last_feed, *feed = NULL;
1048 f = fopen(filename, "r");
1050 ret = AVERROR(errno);
1051 av_log(NULL, AV_LOG_ERROR, "Could not open the configuration file '%s'\n", filename);
1055 config->first_stream = NULL;
1056 last_stream = &config->first_stream;
1057 config->first_feed = NULL;
1058 last_feed = &config->first_feed;
1059 config->errors = config->warnings = 0;
1062 if (fgets(line, sizeof(line), f) == NULL)
1066 while (av_isspace(*p))
1068 if (*p == '\0' || *p == '#')
1071 ffserver_get_arg(cmd, sizeof(cmd), &p);
1073 if (feed || !av_strcasecmp(cmd, "<Feed")) {
1074 int opening = !av_strcasecmp(cmd, "<Feed");
1075 if (opening && (stream || feed || redirect)) {
1076 ERROR("Already in a tag\n");
1078 if ((ret = ffserver_parse_config_feed(config, cmd, &p, line_num, &feed)) < 0)
1081 /* add in stream list */
1082 *last_stream = feed;
1083 last_stream = &feed->next;
1084 /* add in feed list */
1086 last_feed = &feed->next_feed;
1089 } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
1090 int opening = !av_strcasecmp(cmd, "<Stream");
1091 if (opening && (stream || feed || redirect)) {
1092 ERROR("Already in a tag\n");
1094 if ((ret = ffserver_parse_config_stream(config, cmd, &p, line_num, &stream)) < 0)
1097 /* add in stream list */
1098 *last_stream = stream;
1099 last_stream = &stream->next;
1102 } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
1103 int opening = !av_strcasecmp(cmd, "<Redirect");
1104 if (opening && (stream || feed || redirect))
1105 ERROR("Already in a tag\n");
1107 if ((ret = ffserver_parse_config_redirect(config, cmd, &p, line_num, &redirect)) < 0)
1110 /* add in stream list */
1111 *last_stream = redirect;
1112 last_stream = &redirect->next;
1116 ffserver_parse_config_global(config, cmd, &p, line_num);
1124 return AVERROR(EINVAL);