Browse code

ffserver_conf: factorize parse function per config tag

Signed-off-by: Lukasz Marek <lukasz.m.luki2@gmail.com>

Lukasz Marek authored on 2014/10/19 23:37:16
Showing 2 changed files
... ...
@@ -334,18 +334,517 @@ static void report_config_error(const char *filename, int line_num, int log_leve
334 334
     (*errors)++;
335 335
 }
336 336
 
337
+#define ERROR(...)   report_config_error(config->filename, line_num, AV_LOG_ERROR,   &config->errors,   __VA_ARGS__)
338
+#define WARNING(...) report_config_error(config->filename, line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
339
+
340
+static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
341
+                                        const char **p, int line_num)
342
+{
343
+    int val;
344
+    char arg[1024];
345
+    if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
346
+        if (!av_strcasecmp(cmd, "Port"))
347
+            WARNING("Port option is deprecated, use HTTPPort instead\n");
348
+        ffserver_get_arg(arg, sizeof(arg), p);
349
+        val = atoi(arg);
350
+        if (val < 1 || val > 65536)
351
+            ERROR("Invalid port: %s\n", arg);
352
+        if (val < 1024)
353
+            WARNING("Trying to use IETF assigned system port: %d\n", val);
354
+        config->http_addr.sin_port = htons(val);
355
+    } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
356
+        if (!av_strcasecmp(cmd, "BindAddress"))
357
+            WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
358
+        ffserver_get_arg(arg, sizeof(arg), p);
359
+        if (resolve_host(&config->http_addr.sin_addr, arg) != 0)
360
+            ERROR("%s:%d: Invalid host/IP address: %s\n", arg);
361
+    } else if (!av_strcasecmp(cmd, "NoDaemon")) {
362
+        WARNING("NoDaemon option has no effect, you should remove it\n");
363
+    } else if (!av_strcasecmp(cmd, "RTSPPort")) {
364
+        ffserver_get_arg(arg, sizeof(arg), p);
365
+        val = atoi(arg);
366
+        if (val < 1 || val > 65536)
367
+            ERROR("%s:%d: Invalid port: %s\n", arg);
368
+        config->rtsp_addr.sin_port = htons(atoi(arg));
369
+    } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
370
+        ffserver_get_arg(arg, sizeof(arg), p);
371
+        if (resolve_host(&config->rtsp_addr.sin_addr, arg) != 0)
372
+            ERROR("Invalid host/IP address: %s\n", arg);
373
+    } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
374
+        ffserver_get_arg(arg, sizeof(arg), p);
375
+        val = atoi(arg);
376
+        if (val < 1 || val > 65536)
377
+            ERROR("Invalid MaxHTTPConnections: %s\n", arg);
378
+        config->nb_max_http_connections = val;
379
+    } else if (!av_strcasecmp(cmd, "MaxClients")) {
380
+        ffserver_get_arg(arg, sizeof(arg), p);
381
+        val = atoi(arg);
382
+        if (val < 1 || val > config->nb_max_http_connections)
383
+            ERROR("Invalid MaxClients: %s\n", arg);
384
+        else
385
+            config->nb_max_connections = val;
386
+    } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
387
+        int64_t llval;
388
+        ffserver_get_arg(arg, sizeof(arg), p);
389
+        llval = strtoll(arg, NULL, 10);
390
+        if (llval < 10 || llval > 10000000)
391
+            ERROR("Invalid MaxBandwidth: %s\n", arg);
392
+        else
393
+            config->max_bandwidth = llval;
394
+    } else if (!av_strcasecmp(cmd, "CustomLog")) {
395
+        if (!config->debug)
396
+            ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
397
+    } else if (!av_strcasecmp(cmd, "LoadModule")) {
398
+        ERROR("Loadable modules no longer supported\n");
399
+    } else
400
+        ERROR("Incorrect keyword: '%s'\n", cmd);
401
+    return 0;
402
+}
403
+
404
+static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
405
+                                      int line_num, FFServerStream **pfeed)
406
+{
407
+    FFServerStream *feed;
408
+    char arg[1024];
409
+    av_assert0(pfeed);
410
+    feed = *pfeed;
411
+    if (!av_strcasecmp(cmd, "<Feed")) {
412
+        char *q;
413
+        FFServerStream *s;
414
+        feed = av_mallocz(sizeof(FFServerStream));
415
+        if (!feed)
416
+            return AVERROR(ENOMEM);
417
+        ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
418
+        q = strrchr(feed->filename, '>');
419
+        if (*q)
420
+            *q = '\0';
421
+
422
+        for (s = config->first_feed; s; s = s->next) {
423
+            if (!strcmp(feed->filename, s->filename))
424
+                ERROR("Feed '%s' already registered\n", s->filename);
425
+        }
426
+
427
+        feed->fmt = av_guess_format("ffm", NULL, NULL);
428
+        /* default feed file */
429
+        snprintf(feed->feed_filename, sizeof(feed->feed_filename),
430
+                 "/tmp/%s.ffm", feed->filename);
431
+        feed->feed_max_size = 5 * 1024 * 1024;
432
+        feed->is_feed = 1;
433
+        feed->feed = feed; /* self feeding :-) */
434
+        *pfeed = feed;
435
+        return 0;
436
+    }
437
+    av_assert0(feed);
438
+    if (!av_strcasecmp(cmd, "Launch")) {
439
+        int i;
440
+
441
+        feed->child_argv = av_mallocz(64 * sizeof(char *));
442
+        if (!feed->child_argv)
443
+            return AVERROR(ENOMEM);
444
+        for (i = 0; i < 62; i++) {
445
+            ffserver_get_arg(arg, sizeof(arg), p);
446
+            if (!arg[0])
447
+                break;
448
+
449
+            feed->child_argv[i] = av_strdup(arg);
450
+            if (!feed->child_argv[i])
451
+                return AVERROR(ENOMEM);
452
+        }
453
+
454
+        feed->child_argv[i] =
455
+            av_asprintf("http://%s:%d/%s",
456
+                        (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
457
+                        inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
458
+                        feed->filename);
459
+        if (!feed->child_argv[i])
460
+            return AVERROR(ENOMEM);
461
+    } else if (!av_strcasecmp(cmd, "ACL")) {
462
+        ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename, line_num);
463
+    } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
464
+        ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
465
+        feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
466
+    } else if (!av_strcasecmp(cmd, "Truncate")) {
467
+        ffserver_get_arg(arg, sizeof(arg), p);
468
+        /* assume Truncate is true in case no argument is specified */
469
+        if (!arg[0]) {
470
+            feed->truncate = 1;
471
+        } else {
472
+            WARNING("Truncate N syntax in configuration file is deprecated, "
473
+                    "use Truncate alone with no arguments\n");
474
+            feed->truncate = strtod(arg, NULL);
475
+        }
476
+    } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
477
+        char *p1;
478
+        double fsize;
479
+
480
+        ffserver_get_arg(arg, sizeof(arg), p);
481
+        p1 = arg;
482
+        fsize = strtod(p1, &p1);
483
+        switch(av_toupper(*p1)) {
484
+        case 'K':
485
+            fsize *= 1024;
486
+            break;
487
+        case 'M':
488
+            fsize *= 1024 * 1024;
489
+            break;
490
+        case 'G':
491
+            fsize *= 1024 * 1024 * 1024;
492
+            break;
493
+        }
494
+        feed->feed_max_size = (int64_t)fsize;
495
+        if (feed->feed_max_size < FFM_PACKET_SIZE*4)
496
+            ERROR("Feed max file size is too small, must be at least %d\n", FFM_PACKET_SIZE*4);
497
+    } else if (!av_strcasecmp(cmd, "</Feed>")) {
498
+        *pfeed = NULL;
499
+    } else {
500
+        ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
501
+    }
502
+    return 0;
503
+}
504
+
505
+static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
506
+                                        int line_num, FFServerStream **pstream)
507
+{
508
+    char arg[1024], arg2[1024];
509
+    FFServerStream *stream;
510
+
511
+    av_assert0(pstream);
512
+    stream = *pstream;
513
+
514
+    if (!av_strcasecmp(cmd, "<Stream")) {
515
+        char *q;
516
+        FFServerStream *s;
517
+        stream = av_mallocz(sizeof(FFServerStream));
518
+        if (!stream)
519
+            return AVERROR(ENOMEM);
520
+        ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
521
+        q = strrchr(stream->filename, '>');
522
+        if (q)
523
+            *q = '\0';
524
+
525
+        for (s = config->first_stream; s; s = s->next) {
526
+            if (!strcmp(stream->filename, s->filename))
527
+                ERROR("Stream '%s' already registered\n", s->filename);
528
+        }
529
+
530
+        stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
531
+        avcodec_get_context_defaults3(&config->video_enc, NULL);
532
+        avcodec_get_context_defaults3(&config->audio_enc, NULL);
533
+
534
+        config->audio_id = AV_CODEC_ID_NONE;
535
+        config->video_id = AV_CODEC_ID_NONE;
536
+        if (stream->fmt) {
537
+            config->audio_id = stream->fmt->audio_codec;
538
+            config->video_id = stream->fmt->video_codec;
539
+        }
540
+        *pstream = stream;
541
+        return 0;
542
+    }
543
+    av_assert0(stream);
544
+    if (!av_strcasecmp(cmd, "Feed")) {
545
+        FFServerStream *sfeed;
546
+        ffserver_get_arg(arg, sizeof(arg), p);
547
+        sfeed = config->first_feed;
548
+        while (sfeed) {
549
+            if (!strcmp(sfeed->filename, arg))
550
+                break;
551
+            sfeed = sfeed->next_feed;
552
+        }
553
+        if (!sfeed)
554
+            ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg, stream->filename);
555
+        else
556
+            stream->feed = sfeed;
557
+    } else if (!av_strcasecmp(cmd, "Format")) {
558
+        ffserver_get_arg(arg, sizeof(arg), p);
559
+        if (!strcmp(arg, "status")) {
560
+            stream->stream_type = STREAM_TYPE_STATUS;
561
+            stream->fmt = NULL;
562
+        } else {
563
+            stream->stream_type = STREAM_TYPE_LIVE;
564
+            /* JPEG cannot be used here, so use single frame MJPEG */
565
+            if (!strcmp(arg, "jpeg"))
566
+                strcpy(arg, "mjpeg");
567
+            stream->fmt = ffserver_guess_format(arg, NULL, NULL);
568
+            if (!stream->fmt)
569
+                ERROR("Unknown Format: %s\n", arg);
570
+        }
571
+        if (stream->fmt) {
572
+            config->audio_id = stream->fmt->audio_codec;
573
+            config->video_id = stream->fmt->video_codec;
574
+        }
575
+    } else if (!av_strcasecmp(cmd, "InputFormat")) {
576
+        ffserver_get_arg(arg, sizeof(arg), p);
577
+        stream->ifmt = av_find_input_format(arg);
578
+        if (!stream->ifmt)
579
+            ERROR("Unknown input format: %s\n", arg);
580
+    } else if (!av_strcasecmp(cmd, "FaviconURL")) {
581
+        if (stream->stream_type == STREAM_TYPE_STATUS)
582
+            ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
583
+        else
584
+            ERROR("FaviconURL only permitted for status streams\n");
585
+    } else if (!av_strcasecmp(cmd, "Author")    ||
586
+               !av_strcasecmp(cmd, "Comment")   ||
587
+               !av_strcasecmp(cmd, "Copyright") ||
588
+               !av_strcasecmp(cmd, "Title")) {
589
+        char key[32];
590
+        int i, ret;
591
+        ffserver_get_arg(arg, sizeof(arg), p);
592
+        for (i = 0; i < strlen(cmd); i++)
593
+            key[i] = av_tolower(cmd[i]);
594
+        key[i] = 0;
595
+        WARNING("'%s' option in configuration file is deprecated, "
596
+                "use 'Metadata %s VALUE' instead\n", cmd, key);
597
+        if ((ret = av_dict_set(&stream->metadata, key, arg, 0)) < 0)
598
+            ERROR("Could not set metadata '%s' to value '%s': %s\n", key, arg, av_err2str(ret));
599
+    } else if (!av_strcasecmp(cmd, "Metadata")) {
600
+        int ret;
601
+        ffserver_get_arg(arg, sizeof(arg), p);
602
+        ffserver_get_arg(arg2, sizeof(arg2), p);
603
+        if ((ret = av_dict_set(&stream->metadata, arg, arg2, 0)) < 0) {
604
+            ERROR("Could not set metadata '%s' to value '%s': %s\n",
605
+                  arg, arg2, av_err2str(ret));
606
+        }
607
+    } else if (!av_strcasecmp(cmd, "Preroll")) {
608
+        ffserver_get_arg(arg, sizeof(arg), p);
609
+        stream->prebuffer = atof(arg) * 1000;
610
+    } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
611
+        stream->send_on_key = 1;
612
+    } else if (!av_strcasecmp(cmd, "AudioCodec")) {
613
+        ffserver_get_arg(arg, sizeof(arg), p);
614
+        config->audio_id = opt_codec(arg, AVMEDIA_TYPE_AUDIO);
615
+        if (config->audio_id == AV_CODEC_ID_NONE)
616
+            ERROR("Unknown AudioCodec: %s\n", arg);
617
+    } else if (!av_strcasecmp(cmd, "VideoCodec")) {
618
+        ffserver_get_arg(arg, sizeof(arg), p);
619
+        config->video_id = opt_codec(arg, AVMEDIA_TYPE_VIDEO);
620
+        if (config->video_id == AV_CODEC_ID_NONE)
621
+            ERROR("Unknown VideoCodec: %s\n", arg);
622
+    } else if (!av_strcasecmp(cmd, "MaxTime")) {
623
+        ffserver_get_arg(arg, sizeof(arg), p);
624
+        stream->max_time = atof(arg) * 1000;
625
+    } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
626
+        ffserver_get_arg(arg, sizeof(arg), p);
627
+        config->audio_enc.bit_rate = lrintf(atof(arg) * 1000);
628
+    } else if (!av_strcasecmp(cmd, "AudioChannels")) {
629
+        ffserver_get_arg(arg, sizeof(arg), p);
630
+        config->audio_enc.channels = atoi(arg);
631
+    } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
632
+        ffserver_get_arg(arg, sizeof(arg), p);
633
+        config->audio_enc.sample_rate = atoi(arg);
634
+    } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
635
+        int minrate, maxrate;
636
+        ffserver_get_arg(arg, sizeof(arg), p);
637
+        if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
638
+            config->video_enc.rc_min_rate = minrate * 1000;
639
+            config->video_enc.rc_max_rate = maxrate * 1000;
640
+        } else
641
+            ERROR("Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n", arg);
642
+    } else if (!av_strcasecmp(cmd, "Debug")) {
643
+        ffserver_get_arg(arg, sizeof(arg), p);
644
+        config->video_enc.debug = strtol(arg,0,0);
645
+    } else if (!av_strcasecmp(cmd, "Strict")) {
646
+        ffserver_get_arg(arg, sizeof(arg), p);
647
+        config->video_enc.strict_std_compliance = atoi(arg);
648
+    } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
649
+        ffserver_get_arg(arg, sizeof(arg), p);
650
+        config->video_enc.rc_buffer_size = atoi(arg) * 8*1024;
651
+    } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
652
+        ffserver_get_arg(arg, sizeof(arg), p);
653
+        config->video_enc.bit_rate_tolerance = atoi(arg) * 1000;
654
+    } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
655
+        ffserver_get_arg(arg, sizeof(arg), p);
656
+        config->video_enc.bit_rate = atoi(arg) * 1000;
657
+    } else if (!av_strcasecmp(cmd, "VideoSize")) {
658
+        int ret;
659
+        ffserver_get_arg(arg, sizeof(arg), p);
660
+        ret = av_parse_video_size(&config->video_enc.width, &config->video_enc.height, arg);
661
+        if (ret < 0)
662
+            ERROR("Invalid video size '%s'\n", arg);
663
+        else if ((config->video_enc.width % 16) != 0 || (config->video_enc.height % 16) != 0)
664
+            ERROR("Image size must be a multiple of 16\n");
665
+    } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
666
+        AVRational frame_rate;
667
+        ffserver_get_arg(arg, sizeof(arg), p);
668
+        if (av_parse_video_rate(&frame_rate, arg) < 0) {
669
+            ERROR("Incorrect frame rate: %s\n", arg);
670
+        } else {
671
+            config->video_enc.time_base.num = frame_rate.den;
672
+            config->video_enc.time_base.den = frame_rate.num;
673
+        }
674
+    } else if (!av_strcasecmp(cmd, "PixelFormat")) {
675
+        ffserver_get_arg(arg, sizeof(arg), p);
676
+        config->video_enc.pix_fmt = av_get_pix_fmt(arg);
677
+        if (config->video_enc.pix_fmt == AV_PIX_FMT_NONE)
678
+            ERROR("Unknown pixel format: %s\n", arg);
679
+    } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
680
+        ffserver_get_arg(arg, sizeof(arg), p);
681
+        config->video_enc.gop_size = atoi(arg);
682
+    } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
683
+        config->video_enc.gop_size = 1;
684
+    } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
685
+        config->video_enc.mb_decision = FF_MB_DECISION_BITS;
686
+    } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
687
+        config->video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
688
+        config->video_enc.flags |= CODEC_FLAG_4MV;
689
+    } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
690
+               !av_strcasecmp(cmd, "AVOptionAudio")) {
691
+        AVCodecContext *avctx;
692
+        int type;
693
+        ffserver_get_arg(arg, sizeof(arg), p);
694
+        ffserver_get_arg(arg2, sizeof(arg2), p);
695
+        if (!av_strcasecmp(cmd, "AVOptionVideo")) {
696
+            avctx = &config->video_enc;
697
+            type = AV_OPT_FLAG_VIDEO_PARAM;
698
+        } else {
699
+            avctx = &config->audio_enc;
700
+            type = AV_OPT_FLAG_AUDIO_PARAM;
701
+        }
702
+        if (ffserver_opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {
703
+            ERROR("Error setting %s option to %s %s\n", cmd, arg, arg2);
704
+        }
705
+    } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
706
+               !av_strcasecmp(cmd, "AVPresetAudio")) {
707
+        AVCodecContext *avctx;
708
+        int type;
709
+        ffserver_get_arg(arg, sizeof(arg), p);
710
+        if (!av_strcasecmp(cmd, "AVPresetVideo")) {
711
+            avctx = &config->video_enc;
712
+            config->video_enc.codec_id = config->video_id;
713
+            type = AV_OPT_FLAG_VIDEO_PARAM;
714
+        } else {
715
+            avctx = &config->audio_enc;
716
+            config->audio_enc.codec_id = config->audio_id;
717
+            type = AV_OPT_FLAG_AUDIO_PARAM;
718
+        }
719
+        if (ffserver_opt_preset(arg, avctx, type|AV_OPT_FLAG_ENCODING_PARAM, &config->audio_id, &config->video_id)) {
720
+            ERROR("AVPreset error: %s\n", arg);
721
+        }
722
+    } else if (!av_strcasecmp(cmd, "VideoTag")) {
723
+        ffserver_get_arg(arg, sizeof(arg), p);
724
+        if (strlen(arg) == 4)
725
+            config->video_enc.codec_tag = MKTAG(arg[0], arg[1], arg[2], arg[3]);
726
+    } else if (!av_strcasecmp(cmd, "BitExact")) {
727
+        config->video_enc.flags |= CODEC_FLAG_BITEXACT;
728
+    } else if (!av_strcasecmp(cmd, "DctFastint")) {
729
+        config->video_enc.dct_algo  = FF_DCT_FASTINT;
730
+    } else if (!av_strcasecmp(cmd, "IdctSimple")) {
731
+        config->video_enc.idct_algo = FF_IDCT_SIMPLE;
732
+    } else if (!av_strcasecmp(cmd, "Qscale")) {
733
+        ffserver_get_arg(arg, sizeof(arg), p);
734
+        config->video_enc.flags |= CODEC_FLAG_QSCALE;
735
+        config->video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
736
+    } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
737
+        ffserver_get_arg(arg, sizeof(arg), p);
738
+        config->video_enc.max_qdiff = atoi(arg);
739
+        if (config->video_enc.max_qdiff < 1 || config->video_enc.max_qdiff > 31)
740
+            ERROR("VideoQDiff out of range\n");
741
+    } else if (!av_strcasecmp(cmd, "VideoQMax")) {
742
+        ffserver_get_arg(arg, sizeof(arg), p);
743
+        config->video_enc.qmax = atoi(arg);
744
+        if (config->video_enc.qmax < 1 || config->video_enc.qmax > 31)
745
+            ERROR("VideoQMax out of range\n");
746
+    } else if (!av_strcasecmp(cmd, "VideoQMin")) {
747
+        ffserver_get_arg(arg, sizeof(arg), p);
748
+        config->video_enc.qmin = atoi(arg);
749
+        if (config->video_enc.qmin < 1 || config->video_enc.qmin > 31)
750
+            ERROR("VideoQMin out of range\n");
751
+    } else if (!av_strcasecmp(cmd, "LumiMask")) {
752
+        ffserver_get_arg(arg, sizeof(arg), p);
753
+        config->video_enc.lumi_masking = atof(arg);
754
+    } else if (!av_strcasecmp(cmd, "DarkMask")) {
755
+        ffserver_get_arg(arg, sizeof(arg), p);
756
+        config->video_enc.dark_masking = atof(arg);
757
+    } else if (!av_strcasecmp(cmd, "NoVideo")) {
758
+        config->video_id = AV_CODEC_ID_NONE;
759
+    } else if (!av_strcasecmp(cmd, "NoAudio")) {
760
+        config->audio_id = AV_CODEC_ID_NONE;
761
+    } else if (!av_strcasecmp(cmd, "ACL")) {
762
+        ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename, line_num);
763
+    } else if (!av_strcasecmp(cmd, "DynamicACL")) {
764
+        ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
765
+    } else if (!av_strcasecmp(cmd, "RTSPOption")) {
766
+        ffserver_get_arg(arg, sizeof(arg), p);
767
+        av_freep(&stream->rtsp_option);
768
+        stream->rtsp_option = av_strdup(arg);
769
+    } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
770
+        ffserver_get_arg(arg, sizeof(arg), p);
771
+        if (resolve_host(&stream->multicast_ip, arg) != 0)
772
+            ERROR("Invalid host/IP address: %s\n", arg);
773
+        stream->is_multicast = 1;
774
+        stream->loop = 1; /* default is looping */
775
+    } else if (!av_strcasecmp(cmd, "MulticastPort")) {
776
+        ffserver_get_arg(arg, sizeof(arg), p);
777
+        stream->multicast_port = atoi(arg);
778
+    } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
779
+        ffserver_get_arg(arg, sizeof(arg), p);
780
+        stream->multicast_ttl = atoi(arg);
781
+    } else if (!av_strcasecmp(cmd, "NoLoop")) {
782
+        stream->loop = 0;
783
+    } else if (!av_strcasecmp(cmd, "</Stream>")) {
784
+        if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
785
+            if (config->audio_id != AV_CODEC_ID_NONE) {
786
+                config->audio_enc.codec_type = AVMEDIA_TYPE_AUDIO;
787
+                config->audio_enc.codec_id = config->audio_id;
788
+                add_codec(stream, &config->audio_enc);
789
+            }
790
+            if (config->video_id != AV_CODEC_ID_NONE) {
791
+                config->video_enc.codec_type = AVMEDIA_TYPE_VIDEO;
792
+                config->video_enc.codec_id = config->video_id;
793
+                add_codec(stream, &config->video_enc);
794
+            }
795
+        }
796
+        *pstream = NULL;
797
+    } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
798
+        ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), p);
799
+    } else {
800
+        ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
801
+    }
802
+    return 0;
803
+}
804
+
805
+static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
806
+                                          int line_num, FFServerStream **predirect)
807
+{
808
+    FFServerStream *redirect;
809
+    av_assert0(predirect);
810
+    redirect = *predirect;
811
+
812
+    if (!av_strcasecmp(cmd, "<Redirect")) {
813
+        char *q;
814
+        redirect = av_mallocz(sizeof(FFServerStream));
815
+        if (!redirect)
816
+            return AVERROR(ENOMEM);
817
+
818
+        ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
819
+        q = strrchr(redirect->filename, '>');
820
+        if (*q)
821
+            *q = '\0';
822
+        redirect->stream_type = STREAM_TYPE_REDIRECT;
823
+        *predirect = redirect;
824
+        return 0;
825
+    }
826
+    av_assert0(redirect);
827
+    if (!av_strcasecmp(cmd, "URL")) {
828
+        ffserver_get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), p);
829
+    } else if (!av_strcasecmp(cmd, "</Redirect>")) {
830
+        if (!redirect->feed_filename[0])
831
+            ERROR("No URL found for <Redirect>\n");
832
+        *predirect = NULL;
833
+    } else {
834
+        ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
835
+    }
836
+    return 0;
837
+}
838
+
337 839
 int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
338 840
 {
339 841
     FILE *f;
340 842
     char line[1024];
341 843
     char cmd[64];
342
-    char arg[1024], arg2[1024];
343 844
     const char *p;
344
-    int val, errors = 0, warnings = 0, line_num = 0;
845
+    int line_num = 0;
345 846
     FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
346 847
     FFServerStream **last_feed, *feed = NULL;
347
-    AVCodecContext audio_enc, video_enc;
348
-    enum AVCodecID audio_id = AV_CODEC_ID_NONE, video_id = AV_CODEC_ID_NONE;
349 848
     int ret = 0;
350 849
 
351 850
     av_assert0(config);
... ...
@@ -361,8 +860,7 @@ int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
361 361
     last_stream = &config->first_stream;
362 362
     config->first_feed = NULL;
363 363
     last_feed = &config->first_feed;
364
-#define ERROR(...)   report_config_error(filename, line_num, AV_LOG_ERROR,   &errors,   __VA_ARGS__)
365
-#define WARNING(...) report_config_error(filename, line_num, AV_LOG_WARNING, &warnings, __VA_ARGS__)
364
+    config->errors = config->warnings = 0;
366 365
 
367 366
     for(;;) {
368 367
         if (fgets(line, sizeof(line), f) == NULL)
... ...
@@ -376,605 +874,61 @@ int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
376 376
 
377 377
         ffserver_get_arg(cmd, sizeof(cmd), &p);
378 378
 
379
-        if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
380
-            if (!av_strcasecmp(cmd, "Port"))
381
-                WARNING("Port option is deprecated, use HTTPPort instead\n");
382
-            ffserver_get_arg(arg, sizeof(arg), &p);
383
-            val = atoi(arg);
384
-            if (val < 1 || val > 65536) {
385
-                ERROR("Invalid port: %s\n", arg);
386
-            }
387
-            if (val < 1024)
388
-                WARNING("Trying to use IETF assigned system port: %d\n", val);
389
-            config->http_addr.sin_port = htons(val);
390
-        } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
391
-            if (!av_strcasecmp(cmd, "BindAddress"))
392
-                WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
393
-            ffserver_get_arg(arg, sizeof(arg), &p);
394
-            if (resolve_host(&config->http_addr.sin_addr, arg) != 0) {
395
-                ERROR("%s:%d: Invalid host/IP address: %s\n", arg);
396
-            }
397
-        } else if (!av_strcasecmp(cmd, "NoDaemon")) {
398
-            WARNING("NoDaemon option has no effect, you should remove it\n");
399
-        } else if (!av_strcasecmp(cmd, "RTSPPort")) {
400
-            ffserver_get_arg(arg, sizeof(arg), &p);
401
-            val = atoi(arg);
402
-            if (val < 1 || val > 65536) {
403
-                ERROR("%s:%d: Invalid port: %s\n", arg);
404
-            }
405
-            config->rtsp_addr.sin_port = htons(atoi(arg));
406
-        } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
407
-            ffserver_get_arg(arg, sizeof(arg), &p);
408
-            if (resolve_host(&config->rtsp_addr.sin_addr, arg) != 0) {
409
-                ERROR("Invalid host/IP address: %s\n", arg);
410
-            }
411
-        } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
412
-            ffserver_get_arg(arg, sizeof(arg), &p);
413
-            val = atoi(arg);
414
-            if (val < 1 || val > 65536) {
415
-                ERROR("Invalid MaxHTTPConnections: %s\n", arg);
416
-            }
417
-            config->nb_max_http_connections = val;
418
-        } else if (!av_strcasecmp(cmd, "MaxClients")) {
419
-            ffserver_get_arg(arg, sizeof(arg), &p);
420
-            val = atoi(arg);
421
-            if (val < 1 || val > config->nb_max_http_connections) {
422
-                ERROR("Invalid MaxClients: %s\n", arg);
423
-            } else {
424
-                config->nb_max_connections = val;
425
-            }
426
-        } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
427
-            int64_t llval;
428
-            ffserver_get_arg(arg, sizeof(arg), &p);
429
-            llval = strtoll(arg, NULL, 10);
430
-            if (llval < 10 || llval > 10000000) {
431
-                ERROR("Invalid MaxBandwidth: %s\n", arg);
432
-            } else
433
-                config->max_bandwidth = llval;
434
-        } else if (!av_strcasecmp(cmd, "CustomLog")) {
435
-            if (!config->debug)
436
-                ffserver_get_arg(config->logfilename, sizeof(config->logfilename), &p);
437
-        } else if (!av_strcasecmp(cmd, "<Feed")) {
438
-            /*********************************************/
439
-            /* Feed related options */
440
-            char *q;
441
-            if (stream || feed) {
379
+        if (feed || !av_strcasecmp(cmd, "<Feed")) {
380
+            int opening = !av_strcasecmp(cmd, "<Feed");
381
+            if (opening && (stream || feed || redirect)) {
442 382
                 ERROR("Already in a tag\n");
443 383
             } else {
444
-                FFServerStream *s;
445
-                feed = av_mallocz(sizeof(FFServerStream));
446
-                if (!feed) {
447
-                    ret = AVERROR(ENOMEM);
448
-                    goto end;
449
-                }
450
-                ffserver_get_arg(feed->filename, sizeof(feed->filename), &p);
451
-                q = strrchr(feed->filename, '>');
452
-                if (*q)
453
-                    *q = '\0';
454
-
455
-                for (s = config->first_feed; s; s = s->next) {
456
-                    if (!strcmp(feed->filename, s->filename)) {
457
-                        ERROR("Feed '%s' already registered\n", s->filename);
458
-                    }
459
-                }
460
-
461
-                feed->fmt = av_guess_format("ffm", NULL, NULL);
462
-                /* default feed file */
463
-                snprintf(feed->feed_filename, sizeof(feed->feed_filename),
464
-                         "/tmp/%s.ffm", feed->filename);
465
-                feed->feed_max_size = 5 * 1024 * 1024;
466
-                feed->is_feed = 1;
467
-                feed->feed = feed; /* self feeding :-) */
468
-
469
-                /* add in stream list */
470
-                *last_stream = feed;
471
-                last_stream = &feed->next;
472
-                /* add in feed list */
473
-                *last_feed = feed;
474
-                last_feed = &feed->next_feed;
475
-            }
476
-        } else if (!av_strcasecmp(cmd, "Launch")) {
477
-            if (feed) {
478
-                int i;
479
-
480
-                feed->child_argv = av_mallocz(64 * sizeof(char *));
481
-                if (!feed->child_argv) {
482
-                    ret = AVERROR(ENOMEM);
483
-                    goto end;
484
-                }
485
-                for (i = 0; i < 62; i++) {
486
-                    ffserver_get_arg(arg, sizeof(arg), &p);
487
-                    if (!arg[0])
488
-                        break;
489
-
490
-                    feed->child_argv[i] = av_strdup(arg);
491
-                    if (!feed->child_argv[i]) {
492
-                        ret = AVERROR(ENOMEM);
493
-                        goto end;
494
-                    }
495
-                }
496
-
497
-                feed->child_argv[i] =
498
-                    av_asprintf("http://%s:%d/%s",
499
-                                (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
500
-                                inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
501
-                                feed->filename);
502
-                if (!feed->child_argv[i]) {
503
-                    ret = AVERROR(ENOMEM);
504
-                    goto end;
505
-                }
506
-            }
507
-        } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
508
-            if (feed) {
509
-                ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), &p);
510
-                feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
511
-            } else if (stream) {
512
-                ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
513
-            }
514
-        } else if (!av_strcasecmp(cmd, "Truncate")) {
515
-            if (feed) {
516
-                ffserver_get_arg(arg, sizeof(arg), &p);
517
-                /* assume Truncate is true in case no argument is specified */
518
-                if (!arg[0]) {
519
-                    feed->truncate = 1;
520
-                } else {
521
-                    WARNING("Truncate N syntax in configuration file is deprecated, "
522
-                            "use Truncate alone with no arguments\n");
523
-                    feed->truncate = strtod(arg, NULL);
524
-                }
525
-            }
526
-        } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
527
-            if (feed) {
528
-                char *p1;
529
-                double fsize;
530
-
531
-                ffserver_get_arg(arg, sizeof(arg), &p);
532
-                p1 = arg;
533
-                fsize = strtod(p1, &p1);
534
-                switch(av_toupper(*p1)) {
535
-                case 'K':
536
-                    fsize *= 1024;
537
-                    break;
538
-                case 'M':
539
-                    fsize *= 1024 * 1024;
540
-                    break;
541
-                case 'G':
542
-                    fsize *= 1024 * 1024 * 1024;
384
+                if ((ret = ffserver_parse_config_feed(config, cmd, &p, line_num, &feed)) < 0)
543 385
                     break;
386
+                if (opening) {
387
+                    /* add in stream list */
388
+                    *last_stream = feed;
389
+                    last_stream = &feed->next;
390
+                    /* add in feed list */
391
+                    *last_feed = feed;
392
+                    last_feed = &feed->next_feed;
544 393
                 }
545
-                feed->feed_max_size = (int64_t)fsize;
546
-                if (feed->feed_max_size < FFM_PACKET_SIZE*4) {
547
-                    ERROR("Feed max file size is too small, must be at least %d\n", FFM_PACKET_SIZE*4);
548
-                }
549
-            }
550
-        } else if (!av_strcasecmp(cmd, "</Feed>")) {
551
-            if (!feed) {
552
-                ERROR("No corresponding <Feed> for </Feed>\n");
553 394
             }
554
-            feed = NULL;
555
-        } else if (!av_strcasecmp(cmd, "<Stream")) {
556
-            /*********************************************/
557
-            /* Stream related options */
558
-            char *q;
559
-            if (stream || feed) {
395
+        } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
396
+            int opening = !av_strcasecmp(cmd, "<Stream");
397
+            if (opening && (stream || feed || redirect)) {
560 398
                 ERROR("Already in a tag\n");
561 399
             } else {
562
-                FFServerStream *s;
563
-                stream = av_mallocz(sizeof(FFServerStream));
564
-                if (!stream) {
565
-                    ret = AVERROR(ENOMEM);
566
-                    goto end;
567
-                }
568
-                ffserver_get_arg(stream->filename, sizeof(stream->filename), &p);
569
-                q = strrchr(stream->filename, '>');
570
-                if (q)
571
-                    *q = '\0';
572
-
573
-                for (s = config->first_stream; s; s = s->next) {
574
-                    if (!strcmp(stream->filename, s->filename)) {
575
-                        ERROR("Stream '%s' already registered\n", s->filename);
576
-                    }
577
-                }
578
-
579
-                stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
580
-                avcodec_get_context_defaults3(&video_enc, NULL);
581
-                avcodec_get_context_defaults3(&audio_enc, NULL);
582
-
583
-                audio_id = AV_CODEC_ID_NONE;
584
-                video_id = AV_CODEC_ID_NONE;
585
-                if (stream->fmt) {
586
-                    audio_id = stream->fmt->audio_codec;
587
-                    video_id = stream->fmt->video_codec;
588
-                }
589
-
590
-                *last_stream = stream;
591
-                last_stream = &stream->next;
592
-            }
593
-        } else if (!av_strcasecmp(cmd, "Feed")) {
594
-            ffserver_get_arg(arg, sizeof(arg), &p);
595
-            if (stream) {
596
-                FFServerStream *sfeed;
597
-
598
-                sfeed = config->first_feed;
599
-                while (sfeed) {
600
-                    if (!strcmp(sfeed->filename, arg))
601
-                        break;
602
-                    sfeed = sfeed->next_feed;
603
-                }
604
-                if (!sfeed)
605
-                    ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg, stream->filename);
606
-                else
607
-                    stream->feed = sfeed;
608
-            }
609
-        } else if (!av_strcasecmp(cmd, "Format")) {
610
-            ffserver_get_arg(arg, sizeof(arg), &p);
611
-            if (stream) {
612
-                if (!strcmp(arg, "status")) {
613
-                    stream->stream_type = STREAM_TYPE_STATUS;
614
-                    stream->fmt = NULL;
615
-                } else {
616
-                    stream->stream_type = STREAM_TYPE_LIVE;
617
-                    /* JPEG cannot be used here, so use single frame MJPEG */
618
-                    if (!strcmp(arg, "jpeg"))
619
-                        strcpy(arg, "mjpeg");
620
-                    stream->fmt = ffserver_guess_format(arg, NULL, NULL);
621
-                    if (!stream->fmt) {
622
-                        ERROR("Unknown Format: %s\n", arg);
623
-                    }
624
-                }
625
-                if (stream->fmt) {
626
-                    audio_id = stream->fmt->audio_codec;
627
-                    video_id = stream->fmt->video_codec;
628
-                }
629
-            }
630
-        } else if (!av_strcasecmp(cmd, "InputFormat")) {
631
-            ffserver_get_arg(arg, sizeof(arg), &p);
632
-            if (stream) {
633
-                stream->ifmt = av_find_input_format(arg);
634
-                if (!stream->ifmt) {
635
-                    ERROR("Unknown input format: %s\n", arg);
636
-                }
637
-            }
638
-        } else if (!av_strcasecmp(cmd, "FaviconURL")) {
639
-            if (stream && stream->stream_type == STREAM_TYPE_STATUS) {
640
-                ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), &p);
641
-            } else {
642
-                ERROR("FaviconURL only permitted for status streams\n");
643
-            }
644
-        } else if (!av_strcasecmp(cmd, "Author")    ||
645
-                   !av_strcasecmp(cmd, "Comment")   ||
646
-                   !av_strcasecmp(cmd, "Copyright") ||
647
-                   !av_strcasecmp(cmd, "Title")) {
648
-            ffserver_get_arg(arg, sizeof(arg), &p);
649
-
650
-            if (stream) {
651
-                char key[32];
652
-                int i, ret;
653
-
654
-                for (i = 0; i < strlen(cmd); i++)
655
-                    key[i] = av_tolower(cmd[i]);
656
-                key[i] = 0;
657
-                WARNING("'%s' option in configuration file is deprecated, "
658
-                        "use 'Metadata %s VALUE' instead\n", cmd, key);
659
-                if ((ret = av_dict_set(&stream->metadata, key, arg, 0)) < 0) {
660
-                    ERROR("Could not set metadata '%s' to value '%s': %s\n",
661
-                          key, arg, av_err2str(ret));
662
-                }
663
-            }
664
-        } else if (!av_strcasecmp(cmd, "Metadata")) {
665
-            ffserver_get_arg(arg, sizeof(arg), &p);
666
-            ffserver_get_arg(arg2, sizeof(arg2), &p);
667
-            if (stream) {
668
-                int ret;
669
-                if ((ret = av_dict_set(&stream->metadata, arg, arg2, 0)) < 0) {
670
-                    ERROR("Could not set metadata '%s' to value '%s': %s\n",
671
-                          arg, arg2, av_err2str(ret));
672
-                }
673
-            }
674
-        } else if (!av_strcasecmp(cmd, "Preroll")) {
675
-            ffserver_get_arg(arg, sizeof(arg), &p);
676
-            if (stream)
677
-                stream->prebuffer = atof(arg) * 1000;
678
-        } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
679
-            if (stream)
680
-                stream->send_on_key = 1;
681
-        } else if (!av_strcasecmp(cmd, "AudioCodec")) {
682
-            ffserver_get_arg(arg, sizeof(arg), &p);
683
-            audio_id = opt_codec(arg, AVMEDIA_TYPE_AUDIO);
684
-            if (audio_id == AV_CODEC_ID_NONE) {
685
-                ERROR("Unknown AudioCodec: %s\n", arg);
686
-            }
687
-        } else if (!av_strcasecmp(cmd, "VideoCodec")) {
688
-            ffserver_get_arg(arg, sizeof(arg), &p);
689
-            video_id = opt_codec(arg, AVMEDIA_TYPE_VIDEO);
690
-            if (video_id == AV_CODEC_ID_NONE) {
691
-                ERROR("Unknown VideoCodec: %s\n", arg);
692
-            }
693
-        } else if (!av_strcasecmp(cmd, "MaxTime")) {
694
-            ffserver_get_arg(arg, sizeof(arg), &p);
695
-            if (stream)
696
-                stream->max_time = atof(arg) * 1000;
697
-        } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
698
-            ffserver_get_arg(arg, sizeof(arg), &p);
699
-            if (stream)
700
-                audio_enc.bit_rate = lrintf(atof(arg) * 1000);
701
-        } else if (!av_strcasecmp(cmd, "AudioChannels")) {
702
-            ffserver_get_arg(arg, sizeof(arg), &p);
703
-            if (stream)
704
-                audio_enc.channels = atoi(arg);
705
-        } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
706
-            ffserver_get_arg(arg, sizeof(arg), &p);
707
-            if (stream)
708
-                audio_enc.sample_rate = atoi(arg);
709
-        } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
710
-            if (stream) {
711
-                int minrate, maxrate;
712
-
713
-                ffserver_get_arg(arg, sizeof(arg), &p);
714
-
715
-                if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
716
-                    video_enc.rc_min_rate = minrate * 1000;
717
-                    video_enc.rc_max_rate = maxrate * 1000;
718
-                } else {
719
-                    ERROR("Incorrect format for VideoBitRateRange -- should be <min>-<max>: %s\n", arg);
720
-                }
721
-            }
722
-        } else if (!av_strcasecmp(cmd, "Debug")) {
723
-            if (stream) {
724
-                ffserver_get_arg(arg, sizeof(arg), &p);
725
-                video_enc.debug = strtol(arg,0,0);
726
-            }
727
-        } else if (!av_strcasecmp(cmd, "Strict")) {
728
-            if (stream) {
729
-                ffserver_get_arg(arg, sizeof(arg), &p);
730
-                video_enc.strict_std_compliance = atoi(arg);
731
-            }
732
-        } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
733
-            if (stream) {
734
-                ffserver_get_arg(arg, sizeof(arg), &p);
735
-                video_enc.rc_buffer_size = atoi(arg) * 8*1024;
736
-            }
737
-        } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
738
-            if (stream) {
739
-                ffserver_get_arg(arg, sizeof(arg), &p);
740
-                video_enc.bit_rate_tolerance = atoi(arg) * 1000;
741
-            }
742
-        } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
743
-            ffserver_get_arg(arg, sizeof(arg), &p);
744
-            if (stream) {
745
-                video_enc.bit_rate = atoi(arg) * 1000;
746
-            }
747
-        } else if (!av_strcasecmp(cmd, "VideoSize")) {
748
-            ffserver_get_arg(arg, sizeof(arg), &p);
749
-            if (stream) {
750
-                ret = av_parse_video_size(&video_enc.width, &video_enc.height, arg);
751
-                if (ret < 0) {
752
-                    ERROR("Invalid video size '%s'\n", arg);
753
-                } else {
754
-                    if ((video_enc.width % 16) != 0 ||
755
-                        (video_enc.height % 16) != 0) {
756
-                        ERROR("Image size must be a multiple of 16\n");
757
-                    }
758
-                }
759
-            }
760
-        } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
761
-            ffserver_get_arg(arg, sizeof(arg), &p);
762
-            if (stream) {
763
-                AVRational frame_rate;
764
-                if (av_parse_video_rate(&frame_rate, arg) < 0) {
765
-                    ERROR("Incorrect frame rate: %s\n", arg);
766
-                } else {
767
-                    video_enc.time_base.num = frame_rate.den;
768
-                    video_enc.time_base.den = frame_rate.num;
769
-                }
770
-            }
771
-        } else if (!av_strcasecmp(cmd, "PixelFormat")) {
772
-            ffserver_get_arg(arg, sizeof(arg), &p);
773
-            if (stream) {
774
-                video_enc.pix_fmt = av_get_pix_fmt(arg);
775
-                if (video_enc.pix_fmt == AV_PIX_FMT_NONE) {
776
-                    ERROR("Unknown pixel format: %s\n", arg);
777
-                }
778
-            }
779
-        } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
780
-            ffserver_get_arg(arg, sizeof(arg), &p);
781
-            if (stream)
782
-                video_enc.gop_size = atoi(arg);
783
-        } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
784
-            if (stream)
785
-                video_enc.gop_size = 1;
786
-        } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
787
-            if (stream)
788
-                video_enc.mb_decision = FF_MB_DECISION_BITS;
789
-        } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
790
-            if (stream) {
791
-                video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove
792
-                video_enc.flags |= CODEC_FLAG_4MV;
793
-            }
794
-        } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
795
-                   !av_strcasecmp(cmd, "AVOptionAudio")) {
796
-            AVCodecContext *avctx;
797
-            int type;
798
-            ffserver_get_arg(arg, sizeof(arg), &p);
799
-            ffserver_get_arg(arg2, sizeof(arg2), &p);
800
-            if (!av_strcasecmp(cmd, "AVOptionVideo")) {
801
-                avctx = &video_enc;
802
-                type = AV_OPT_FLAG_VIDEO_PARAM;
803
-            } else {
804
-                avctx = &audio_enc;
805
-                type = AV_OPT_FLAG_AUDIO_PARAM;
806
-            }
807
-            if (ffserver_opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {
808
-                ERROR("Error setting %s option to %s %s\n", cmd, arg, arg2);
809
-            }
810
-        } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
811
-                   !av_strcasecmp(cmd, "AVPresetAudio")) {
812
-            AVCodecContext *avctx;
813
-            int type;
814
-            ffserver_get_arg(arg, sizeof(arg), &p);
815
-            if (!av_strcasecmp(cmd, "AVPresetVideo")) {
816
-                avctx = &video_enc;
817
-                video_enc.codec_id = video_id;
818
-                type = AV_OPT_FLAG_VIDEO_PARAM;
819
-            } else {
820
-                avctx = &audio_enc;
821
-                audio_enc.codec_id = audio_id;
822
-                type = AV_OPT_FLAG_AUDIO_PARAM;
823
-            }
824
-            if (ffserver_opt_preset(arg, avctx, type|AV_OPT_FLAG_ENCODING_PARAM, &audio_id, &video_id)) {
825
-                ERROR("AVPreset error: %s\n", arg);
826
-            }
827
-        } else if (!av_strcasecmp(cmd, "VideoTag")) {
828
-            ffserver_get_arg(arg, sizeof(arg), &p);
829
-            if ((strlen(arg) == 4) && stream)
830
-                video_enc.codec_tag = MKTAG(arg[0], arg[1], arg[2], arg[3]);
831
-        } else if (!av_strcasecmp(cmd, "BitExact")) {
832
-            if (stream)
833
-                video_enc.flags |= CODEC_FLAG_BITEXACT;
834
-        } else if (!av_strcasecmp(cmd, "DctFastint")) {
835
-            if (stream)
836
-                video_enc.dct_algo  = FF_DCT_FASTINT;
837
-        } else if (!av_strcasecmp(cmd, "IdctSimple")) {
838
-            if (stream)
839
-                video_enc.idct_algo = FF_IDCT_SIMPLE;
840
-        } else if (!av_strcasecmp(cmd, "Qscale")) {
841
-            ffserver_get_arg(arg, sizeof(arg), &p);
842
-            if (stream) {
843
-                video_enc.flags |= CODEC_FLAG_QSCALE;
844
-                video_enc.global_quality = FF_QP2LAMBDA * atoi(arg);
845
-            }
846
-        } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
847
-            ffserver_get_arg(arg, sizeof(arg), &p);
848
-            if (stream) {
849
-                video_enc.max_qdiff = atoi(arg);
850
-                if (video_enc.max_qdiff < 1 || video_enc.max_qdiff > 31) {
851
-                    ERROR("VideoQDiff out of range\n");
852
-                }
853
-            }
854
-        } else if (!av_strcasecmp(cmd, "VideoQMax")) {
855
-            ffserver_get_arg(arg, sizeof(arg), &p);
856
-            if (stream) {
857
-                video_enc.qmax = atoi(arg);
858
-                if (video_enc.qmax < 1 || video_enc.qmax > 31) {
859
-                    ERROR("VideoQMax out of range\n");
860
-                }
861
-            }
862
-        } else if (!av_strcasecmp(cmd, "VideoQMin")) {
863
-            ffserver_get_arg(arg, sizeof(arg), &p);
864
-            if (stream) {
865
-                video_enc.qmin = atoi(arg);
866
-                if (video_enc.qmin < 1 || video_enc.qmin > 31) {
867
-                    ERROR("VideoQMin out of range\n");
868
-                }
869
-            }
870
-        } else if (!av_strcasecmp(cmd, "LumiMask")) {
871
-            ffserver_get_arg(arg, sizeof(arg), &p);
872
-            if (stream)
873
-                video_enc.lumi_masking = atof(arg);
874
-        } else if (!av_strcasecmp(cmd, "DarkMask")) {
875
-            ffserver_get_arg(arg, sizeof(arg), &p);
876
-            if (stream)
877
-                video_enc.dark_masking = atof(arg);
878
-        } else if (!av_strcasecmp(cmd, "NoVideo")) {
879
-            video_id = AV_CODEC_ID_NONE;
880
-        } else if (!av_strcasecmp(cmd, "NoAudio")) {
881
-            audio_id = AV_CODEC_ID_NONE;
882
-        } else if (!av_strcasecmp(cmd, "ACL")) {
883
-            ffserver_parse_acl_row(stream, feed, NULL, p, filename, line_num);
884
-        } else if (!av_strcasecmp(cmd, "DynamicACL")) {
885
-            if (stream) {
886
-                ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), &p);
887
-            }
888
-        } else if (!av_strcasecmp(cmd, "RTSPOption")) {
889
-            ffserver_get_arg(arg, sizeof(arg), &p);
890
-            if (stream) {
891
-                av_freep(&stream->rtsp_option);
892
-                stream->rtsp_option = av_strdup(arg);
893
-            }
894
-        } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
895
-            ffserver_get_arg(arg, sizeof(arg), &p);
896
-            if (stream) {
897
-                if (resolve_host(&stream->multicast_ip, arg) != 0) {
898
-                    ERROR("Invalid host/IP address: %s\n", arg);
899
-                }
900
-                stream->is_multicast = 1;
901
-                stream->loop = 1; /* default is looping */
902
-            }
903
-        } else if (!av_strcasecmp(cmd, "MulticastPort")) {
904
-            ffserver_get_arg(arg, sizeof(arg), &p);
905
-            if (stream)
906
-                stream->multicast_port = atoi(arg);
907
-        } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
908
-            ffserver_get_arg(arg, sizeof(arg), &p);
909
-            if (stream)
910
-                stream->multicast_ttl = atoi(arg);
911
-        } else if (!av_strcasecmp(cmd, "NoLoop")) {
912
-            if (stream)
913
-                stream->loop = 0;
914
-        } else if (!av_strcasecmp(cmd, "</Stream>")) {
915
-            if (!stream) {
916
-                ERROR("No corresponding <Stream> for </Stream>\n");
917
-            } else {
918
-                if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm") != 0) {
919
-                    if (audio_id != AV_CODEC_ID_NONE) {
920
-                        audio_enc.codec_type = AVMEDIA_TYPE_AUDIO;
921
-                        audio_enc.codec_id = audio_id;
922
-                        add_codec(stream, &audio_enc);
923
-                    }
924
-                    if (video_id != AV_CODEC_ID_NONE) {
925
-                        video_enc.codec_type = AVMEDIA_TYPE_VIDEO;
926
-                        video_enc.codec_id = video_id;
927
-                        add_codec(stream, &video_enc);
928
-                    }
400
+                if ((ret = ffserver_parse_config_stream(config, cmd, &p, line_num, &stream)) < 0)
401
+                    break;
402
+                if (opening) {
403
+                    /* add in stream list */
404
+                    *last_stream = stream;
405
+                    last_stream = &stream->next;
929 406
                 }
930
-                stream = NULL;
931 407
             }
932
-        } else if (!av_strcasecmp(cmd, "<Redirect")) {
933
-            /*********************************************/
934
-            char *q;
935
-            if (stream || feed || redirect) {
408
+        } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
409
+            int opening = !av_strcasecmp(cmd, "<Redirect");
410
+            if (opening && (stream || feed || redirect))
936 411
                 ERROR("Already in a tag\n");
937
-            } else {
938
-                redirect = av_mallocz(sizeof(FFServerStream));
939
-                if (!redirect) {
940
-                    ret = AVERROR(ENOMEM);
941
-                    goto end;
942
-                }
943
-                *last_stream = redirect;
944
-                last_stream = &redirect->next;
945
-
946
-                ffserver_get_arg(redirect->filename, sizeof(redirect->filename), &p);
947
-                q = strrchr(redirect->filename, '>');
948
-                if (*q)
949
-                    *q = '\0';
950
-                redirect->stream_type = STREAM_TYPE_REDIRECT;
951
-            }
952
-        } else if (!av_strcasecmp(cmd, "URL")) {
953
-            if (redirect)
954
-                ffserver_get_arg(redirect->feed_filename, sizeof(redirect->feed_filename), &p);
955
-        } else if (!av_strcasecmp(cmd, "</Redirect>")) {
956
-            if (!redirect) {
957
-                ERROR("No corresponding <Redirect> for </Redirect>\n");
958
-            } else {
959
-                if (!redirect->feed_filename[0]) {
960
-                    ERROR("No URL found for <Redirect>\n");
412
+            else {
413
+                if ((ret = ffserver_parse_config_redirect(config, cmd, &p, line_num, &redirect)) < 0)
414
+                    break;
415
+                if (opening) {
416
+                    /* add in stream list */
417
+                    *last_stream = redirect;
418
+                    last_stream = &redirect->next;
961 419
                 }
962
-                redirect = NULL;
963 420
             }
964
-        } else if (!av_strcasecmp(cmd, "LoadModule")) {
965
-            ERROR("Loadable modules no longer supported\n");
966 421
         } else {
967
-            ERROR("Incorrect keyword: '%s'\n", cmd);
422
+            ffserver_parse_config_global(config, cmd, &p, line_num);
968 423
         }
969 424
     }
970
-#undef ERROR
971 425
 
972
-end:
973 426
     fclose(f);
974 427
     if (ret < 0)
975 428
         return ret;
976
-    if (errors)
429
+    if (config->errors)
977 430
         return AVERROR(EINVAL);
978 431
     else
979 432
         return 0;
980 433
 }
434
+
435
+#undef ERROR
436
+#undef WARNING
... ...
@@ -104,6 +104,13 @@ typedef struct FFServerConfig {
104 104
     char logfilename[1024];
105 105
     struct sockaddr_in http_addr;
106 106
     struct sockaddr_in rtsp_addr;
107
+    int errors;
108
+    int warnings;
109
+    // Following variables MUST NOT be used outside configuration parsing code.
110
+    AVCodecContext audio_enc;
111
+    AVCodecContext video_enc;
112
+    enum AVCodecID audio_id;
113
+    enum AVCodecID video_id;
107 114
 
108 115
 } FFServerConfig;
109 116