Blame view

trunk/src/app/srs_app_config.cpp 42.2 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
24
#include <srs_app_config.hpp>
winlin authored
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
// file operations.
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <vector>
#include <algorithm>
using namespace std;
40
#include <srs_kernel_error.hpp>
41
#include <srs_kernel_log.hpp>
42
#include <srs_protocol_utility.hpp>
winlin authored
43 44 45 46 47 48
#include <srs_core_autofree.hpp>

#define FILE_OFFSET(fd) lseek(fd, 0, SEEK_CUR)

int64_t FILE_SIZE(int fd)
{
49 50 51 52
    int64_t pre = FILE_OFFSET(fd);
    int64_t pos = lseek(fd, 0, SEEK_END);
    lseek(fd, pre, SEEK_SET);
    return pos;
winlin authored
53 54 55 56 57 58 59
}

#define LF (char)0x0a
#define CR (char)0x0d

bool is_common_space(char ch)
{
60
    return (ch == ' ' || ch == '\t' || ch == CR || ch == LF);
winlin authored
61 62 63 64 65
}

class SrsFileBuffer
{
private:
66 67 68 69 70 71
    // last available position.
    char* last;
    // end of buffer.
    char* end;
    // start of buffer.
    char* start;
winlin authored
72
public:
73 74 75 76 77 78 79 80 81
    // current consumed position.
    char* pos;
    // current parsed line.
    int line;
    
    SrsFileBuffer();
    virtual ~SrsFileBuffer();
    virtual int fullfill(const char* filename);
    virtual bool empty();
winlin authored
82 83 84 85
};

SrsFileBuffer::SrsFileBuffer()
{
86
    line = 0;
winlin authored
87
88 89
    pos = last = start = NULL;
    end = start;
winlin authored
90 91 92 93
}

SrsFileBuffer::~SrsFileBuffer()
{
94
    srs_freepa(start);
winlin authored
95 96 97 98
}

int SrsFileBuffer::fullfill(const char* filename)
{
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    int ret = ERROR_SUCCESS;
    
    int fd = -1;
    int nread = 0;
    int filesize = 0;
    
    if ((fd = ::open(filename, O_RDONLY, 0)) < 0) {
        ret = ERROR_SYSTEM_CONFIG_INVALID;
        srs_error("open conf file error. ret=%d", ret);
        goto finish;
    }
    
    if ((filesize = FILE_SIZE(fd) - FILE_OFFSET(fd)) <= 0) {
        ret = ERROR_SYSTEM_CONFIG_EOF;
        srs_error("read conf file error. ret=%d", ret);
        goto finish;
    }

    srs_freepa(start);
    pos = last = start = new char[filesize];
    end = start + filesize;
    
    if ((nread = read(fd, start, filesize)) != filesize) {
        ret = ERROR_SYSTEM_CONFIG_INVALID;
        srs_error("read file read error. expect %d, actual %d bytes, ret=%d", 
            filesize, nread, ret);
        goto finish;
    }
    
    line = 1;
    
winlin authored
130
finish:
131 132 133 134 135
    if (fd > 0) {
        ::close(fd);
    }
    
    return ret;
winlin authored
136 137 138 139
}

bool SrsFileBuffer::empty()
{
140
    return pos >= end;
winlin authored
141 142 143 144 145 146 147 148
}

SrsConfDirective::SrsConfDirective()
{
}

SrsConfDirective::~SrsConfDirective()
{
149 150 151 152 153 154
    std::vector<SrsConfDirective*>::iterator it;
    for (it = directives.begin(); it != directives.end(); ++it) {
        SrsConfDirective* directive = *it;
        srs_freep(directive);
    }
    directives.clear();
winlin authored
155 156 157 158
}

string SrsConfDirective::arg0()
{
159 160 161 162 163
    if (args.size() > 0) {
        return args.at(0);
    }
    
    return "";
winlin authored
164 165 166 167
}

string SrsConfDirective::arg1()
{
168 169 170 171 172
    if (args.size() > 1) {
        return args.at(1);
    }
    
    return "";
winlin authored
173 174 175 176
}

string SrsConfDirective::arg2()
{
177 178 179 180 181
    if (args.size() > 2) {
        return args.at(2);
    }
    
    return "";
winlin authored
182 183 184 185
}

SrsConfDirective* SrsConfDirective::at(int index)
{
186
    return directives.at(index);
winlin authored
187 188 189 190
}

SrsConfDirective* SrsConfDirective::get(string _name)
{
191 192 193 194 195 196 197 198 199
    std::vector<SrsConfDirective*>::iterator it;
    for (it = directives.begin(); it != directives.end(); ++it) {
        SrsConfDirective* directive = *it;
        if (directive->name == _name) {
            return directive;
        }
    }
    
    return NULL;
winlin authored
200 201 202 203
}

SrsConfDirective* SrsConfDirective::get(string _name, string _arg0)
{
204 205 206 207 208 209 210 211 212
    std::vector<SrsConfDirective*>::iterator it;
    for (it = directives.begin(); it != directives.end(); ++it) {
        SrsConfDirective* directive = *it;
        if (directive->name == _name && directive->arg0() == _arg0) {
            return directive;
        }
    }
    
    return NULL;
winlin authored
213 214 215 216
}

int SrsConfDirective::parse(const char* filename)
{
217 218 219 220 221 222 223 224 225
    int ret = ERROR_SUCCESS;
    
    SrsFileBuffer buffer;
    
    if ((ret = buffer.fullfill(filename)) != ERROR_SUCCESS) {
        return ret;
    }
    
    return parse_conf(&buffer, parse_file);
winlin authored
226 227 228 229 230
}

// see: ngx_conf_parse
int SrsConfDirective::parse_conf(SrsFileBuffer* buffer, SrsDirectiveType type)
{
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    int ret = ERROR_SUCCESS;
    
    while (true) {
        std::vector<string> args;
        ret = read_token(buffer, args);
        
        /**
        * ret maybe:
        * ERROR_SYSTEM_CONFIG_INVALID         error.
        * ERROR_SYSTEM_CONFIG_DIRECTIVE        directive terminated by ';' found
        * ERROR_SYSTEM_CONFIG_BLOCK_START    token terminated by '{' found
        * ERROR_SYSTEM_CONFIG_BLOCK_END        the '}' found
        * ERROR_SYSTEM_CONFIG_EOF            the config file is done
        */
        if (ret == ERROR_SYSTEM_CONFIG_INVALID) {
            return ret;
        }
        if (ret == ERROR_SYSTEM_CONFIG_BLOCK_END) {
            if (type != parse_block) {
                srs_error("line %d: unexpected \"}\"", buffer->line);
                return ret;
            }
            return ERROR_SUCCESS;
        }
        if (ret == ERROR_SYSTEM_CONFIG_EOF) {
            if (type == parse_block) {
                srs_error("line %d: unexpected end of file, expecting \"}\"", buffer->line);
                return ret;
            }
            return ERROR_SUCCESS;
        }
        
        if (args.empty()) {
            srs_error("line %d: empty directive.", buffer->line);
            return ret;
        }
        
        // build directive tree.
        SrsConfDirective* directive = new SrsConfDirective();

        directive->conf_line = buffer->line;
        directive->name = args[0];
        args.erase(args.begin());
        directive->args.swap(args);
        
        directives.push_back(directive);
        
        if (ret == ERROR_SYSTEM_CONFIG_BLOCK_START) {
            if ((ret = directive->parse_conf(buffer, parse_block)) != ERROR_SUCCESS) {
                return ret;
            }
        }
    }
    
    return ret;
winlin authored
286 287 288 289 290
}

// see: ngx_conf_read_token
int SrsConfDirective::read_token(SrsFileBuffer* buffer, std::vector<string>& args)
{
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    int ret = ERROR_SUCCESS;

    char* pstart = buffer->pos;

    bool sharp_comment = false;
    
    bool d_quoted = false;
    bool s_quoted = false;
    
    bool need_space = false;
    bool last_space = true;
    
    while (true) {
        if (buffer->empty()) {
            ret = ERROR_SYSTEM_CONFIG_EOF;
            
            if (!args.empty() || !last_space) {
                srs_error("line %d: unexpected end of file, expecting ; or \"}\"", buffer->line);
                return ERROR_SYSTEM_CONFIG_INVALID;
            }
311
            srs_trace("config parsed EOF");
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
            
            return ret;
        }
        
        char ch = *buffer->pos++;
        
        if (ch == LF) {
            buffer->line++;
            sharp_comment = false;
        }
        
        if (sharp_comment) {
            continue;
        }
        
        if (need_space) {
            if (is_common_space(ch)) {
                last_space = true;
                need_space = false;
                continue;
            }
            if (ch == ';') {
                return ERROR_SYSTEM_CONFIG_DIRECTIVE;
            }
            if (ch == '{') {
                return ERROR_SYSTEM_CONFIG_BLOCK_START;
            }
            srs_error("line %d: unexpected '%c'", buffer->line, ch);
            return ERROR_SYSTEM_CONFIG_INVALID; 
        }
        
        // last charecter is space.
        if (last_space) {
            if (is_common_space(ch)) {
                continue;
            }
            pstart = buffer->pos - 1;
            switch (ch) {
                case ';':
                    if (args.size() == 0) {
                        srs_error("line %d: unexpected ';'", buffer->line);
                        return ERROR_SYSTEM_CONFIG_INVALID;
                    }
                    return ERROR_SYSTEM_CONFIG_DIRECTIVE;
                case '{':
                    if (args.size() == 0) {
                        srs_error("line %d: unexpected '{'", buffer->line);
                        return ERROR_SYSTEM_CONFIG_INVALID;
                    }
                    return ERROR_SYSTEM_CONFIG_BLOCK_START;
                case '}':
                    if (args.size() != 0) {
                        srs_error("line %d: unexpected '}'", buffer->line);
                        return ERROR_SYSTEM_CONFIG_INVALID;
                    }
                    return ERROR_SYSTEM_CONFIG_BLOCK_END;
                case '#':
                    sharp_comment = 1;
                    continue;
                case '"':
                    pstart++;
                    d_quoted = true;
                    last_space = 0;
                    continue;
                case '\'':
                    pstart++;
                    s_quoted = true;
                    last_space = 0;
                    continue;
                default:
                    last_space = 0;
                    continue;
            }
        } else {
        // last charecter is not space
            bool found = false;
            if (d_quoted) {
                if (ch == '"') {
                    d_quoted = false;
                    need_space = true;
                    found = true;
                }
            } else if (s_quoted) {
                if (ch == '\'') {
                    s_quoted = false;
                    need_space = true;
                    found = true;
                }
            } else if (is_common_space(ch) || ch == ';' || ch == '{') {
                last_space = true;
                found = 1;
            }
            
            if (found) {
                int len = buffer->pos - pstart;
                char* word = new char[len];
                memcpy(word, pstart, len);
                word[len - 1] = 0;
                
                string word_str = word;
                if (!word_str.empty()) {
                    args.push_back(word_str);
                }
                srs_freepa(word);
                
                if (ch == ';') {
                    return ERROR_SYSTEM_CONFIG_DIRECTIVE;
                }
                if (ch == '{') {
                    return ERROR_SYSTEM_CONFIG_BLOCK_START;
                }
            }
        }
    }
    
    return ret;
winlin authored
428 429 430 431
}

SrsConfig::SrsConfig()
{
432 433 434 435 436 437
    show_help = false;
    show_version = false;
    
    root = new SrsConfDirective();
    root->conf_line = 0;
    root->name = "root";
winlin authored
438 439 440 441
}

SrsConfig::~SrsConfig()
{
442
    srs_freep(root);
winlin authored
443 444 445 446
}

int SrsConfig::reload()
{
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    int ret = ERROR_SUCCESS;

    SrsConfig conf;
    if ((ret = conf.parse_file(config_file.c_str())) != ERROR_SUCCESS) {
        srs_error("config reloader parse file failed. ret=%d", ret);
        return ret;
    }
    srs_info("config reloader parse file success.");
    
    // store current root to old_root,
    // and reap the root from conf to current root.
    SrsConfDirective* old_root = root;
    SrsAutoFree(SrsConfDirective, old_root, false);
    
    root = conf.root;
    conf.root = NULL;
    
    // merge config.
    std::vector<ISrsReloadHandler*>::iterator it;

    // merge config: listen
    if (!srs_directive_equals(root->get("listen"), old_root->get("listen"))) {
        for (it = subscribes.begin(); it != subscribes.end(); ++it) {
            ISrsReloadHandler* subscribe = *it;
            if ((ret = subscribe->on_reload_listen()) != ERROR_SUCCESS) {
                srs_error("notify subscribes reload listen failed. ret=%d", ret);
                return ret;
            }
        }
        srs_trace("reload listen success.");
    }
    
    // merge config: pithy_print
    if (!srs_directive_equals(root->get("pithy_print"), old_root->get("pithy_print"))) {
        for (it = subscribes.begin(); it != subscribes.end(); ++it) {
            ISrsReloadHandler* subscribe = *it;
            if ((ret = subscribe->on_reload_pithy_print()) != ERROR_SUCCESS) {
                srs_error("notify subscribes pithy_print listen failed. ret=%d", ret);
                return ret;
            }
        }
        srs_trace("reload pithy_print success.");
    }

    // merge config: vhost added, directly supported.
        
    // merge config: vhost removed/disabled/modified.
    for (int i = 0; i < (int)old_root->directives.size(); i++) {
        SrsConfDirective* old_vhost = old_root->at(i);
        // only process vhost directives.
        if (old_vhost->name != "vhost") {
            continue;
        }
        
        std::string vhost = old_vhost->arg0();

        SrsConfDirective* new_vhost = root->get("vhost", vhost);
        // ignore if absolutely equal
        if (new_vhost && srs_directive_equals(old_vhost, new_vhost)) {
            srs_trace("vhost %s absolutely equal, ignore.", vhost.c_str());
            continue;
        }
        // ignore if enable the new vhost when old vhost is disabled.
        if (get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {
            srs_trace("vhost %s disabled=>enabled, ignore.", vhost.c_str());
            continue;
        }
        // ignore if both old and new vhost are disabled.
        if (!get_vhost_enabled(new_vhost) && !get_vhost_enabled(old_vhost)) {
            srs_trace("vhost %s disabled=>disabled, ignore.", vhost.c_str());
            continue;
        }

        // merge config: vhost removed/disabled.
        if (!get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) {
            srs_trace("vhost %s disabled, reload it.", vhost.c_str());
            for (it = subscribes.begin(); it != subscribes.end(); ++it) {
                ISrsReloadHandler* subscribe = *it;
                if ((ret = subscribe->on_reload_vhost_removed(vhost)) != ERROR_SUCCESS) {
                    srs_error("notify subscribes pithy_print remove "
                        "vhost %s failed. ret=%d", vhost.c_str(), ret);
                    return ret;
                }
            }
            srs_trace("reload remove vhost %s success.", vhost.c_str());
        }
        
        // merge config: vhost modified.
        srs_trace("vhost %s modified, reload its detail.", vhost.c_str());
        if (get_vhost_enabled(new_vhost) && get_vhost_enabled(old_vhost)) {
            // gop_cache
            if (!srs_directive_equals(new_vhost->get("gop_cache"), old_vhost->get("gop_cache"))) {
                for (it = subscribes.begin(); it != subscribes.end(); ++it) {
                    ISrsReloadHandler* subscribe = *it;
                    if ((ret = subscribe->on_reload_gop_cache(vhost)) != ERROR_SUCCESS) {
                        srs_error("vhost %s notify subscribes gop_cache failed. ret=%d", vhost.c_str(), ret);
                        return ret;
                    }
                }
                srs_trace("vhost %s reload gop_cache success.", vhost.c_str());
            }
            // queue_length
            if (!srs_directive_equals(new_vhost->get("queue_length"), old_vhost->get("queue_length"))) {
                for (it = subscribes.begin(); it != subscribes.end(); ++it) {
                    ISrsReloadHandler* subscribe = *it;
                    if ((ret = subscribe->on_reload_queue_length(vhost)) != ERROR_SUCCESS) {
                        srs_error("vhost %s notify subscribes queue_length failed. ret=%d", vhost.c_str(), ret);
                        return ret;
                    }
                }
                srs_trace("vhost %s reload queue_length success.", vhost.c_str());
            }
            // forward
            if (!srs_directive_equals(new_vhost->get("forward"), old_vhost->get("forward"))) {
                for (it = subscribes.begin(); it != subscribes.end(); ++it) {
                    ISrsReloadHandler* subscribe = *it;
                    if ((ret = subscribe->on_reload_forward(vhost)) != ERROR_SUCCESS) {
                        srs_error("vhost %s notify subscribes forward failed. ret=%d", vhost.c_str(), ret);
                        return ret;
                    }
                }
                srs_trace("vhost %s reload forward success.", vhost.c_str());
            }
            // hls
            if (!srs_directive_equals(new_vhost->get("hls"), old_vhost->get("hls"))) {
                for (it = subscribes.begin(); it != subscribes.end(); ++it) {
                    ISrsReloadHandler* subscribe = *it;
                    if ((ret = subscribe->on_reload_hls(vhost)) != ERROR_SUCCESS) {
                        srs_error("vhost %s notify subscribes hls failed. ret=%d", vhost.c_str(), ret);
                        return ret;
                    }
                }
                srs_trace("vhost %s reload hls success.", vhost.c_str());
            }
            // transcode
            if (!srs_directive_equals(new_vhost->get("transcode"), old_vhost->get("transcode"))) {
                for (it = subscribes.begin(); it != subscribes.end(); ++it) {
                    ISrsReloadHandler* subscribe = *it;
                    if ((ret = subscribe->on_reload_transcode(vhost)) != ERROR_SUCCESS) {
                        srs_error("vhost %s notify subscribes transcode failed. ret=%d", vhost.c_str(), ret);
                        return ret;
                    }
                }
                srs_trace("vhost %s reload transcode success.", vhost.c_str());
            }
            // TODO: suppor reload hls/forward/ffmpeg/http
            continue;
        }
        srs_warn("invalid reload path, enabled old: %d, new: %d", 
            get_vhost_enabled(old_vhost), get_vhost_enabled(new_vhost));
    }
    
    return ret;
winlin authored
600 601 602 603
}

void SrsConfig::subscribe(ISrsReloadHandler* handler)
{
604 605 606 607 608 609 610 611
    std::vector<ISrsReloadHandler*>::iterator it;
    
    it = std::find(subscribes.begin(), subscribes.end(), handler);
    if (it != subscribes.end()) {
        return;
    }
    
    subscribes.push_back(handler);
winlin authored
612 613 614 615
}

void SrsConfig::unsubscribe(ISrsReloadHandler* handler)
{
616 617 618 619 620 621 622 623
    std::vector<ISrsReloadHandler*>::iterator it;
    
    it = std::find(subscribes.begin(), subscribes.end(), handler);
    if (it == subscribes.end()) {
        return;
    }
    
    subscribes.erase(it);
winlin authored
624 625 626 627 628
}

// see: ngx_get_options
int SrsConfig::parse_options(int argc, char** argv)
{
629 630 631 632 633 634 635 636 637 638 639 640 641
    int ret = ERROR_SUCCESS;
    
    for (int i = 1; i < argc; i++) {
        if ((ret = parse_argv(i, argv)) != ERROR_SUCCESS) {
            return ret;
        }
    }
    
    if (show_help) {
        print_help(argv);
    }
    
    if (show_version) {
642
        fprintf(stderr, "%s\n", RTMP_SIG_SRS_VERSION);
643 644 645 646 647 648 649 650 651 652 653 654 655
    }
    
    if (show_help || show_version) {
        exit(0);
    }
    
    if (config_file.empty()) {
        ret = ERROR_SYSTEM_CONFIG_INVALID;
        srs_error("config file not specified, see help: %s -h, ret=%d", argv[0], ret);
        return ret;
    }

    return parse_file(config_file.c_str());
winlin authored
656 657 658 659
}

int SrsConfig::parse_file(const char* filename)
{
660 661 662 663 664 665 666 667 668 669 670 671 672
    int ret = ERROR_SUCCESS;
    
    config_file = filename;
    
    if (config_file.empty()) {
        return ERROR_SYSTEM_CONFIG_INVALID;
    }
    
    if ((ret = root->parse(config_file.c_str())) != ERROR_SUCCESS) {
        return ret;
    }
    
    SrsConfDirective* conf = NULL;
673
    // check rtmp port specified by directive listen.
674 675 676 677 678 679 680 681 682 683 684
    if ((conf = get_listen()) == NULL || conf->args.size() == 0) {
        ret = ERROR_SYSTEM_CONFIG_INVALID;
        srs_error("line %d: conf error, "
            "directive \"listen\" is empty, ret=%d", (conf? conf->conf_line:0), ret);
        return ret;
    }
    
    // TODO: check the hls.
    // TODO: check forward.
    // TODO: check ffmpeg.
    // TODO: check http.
winlin authored
685
    // TODO: check pid.
686
    
687
    // check log
688
    std::string log_filename = this->get_srs_log_file();
689 690 691 692 693
    if (this->get_srs_log_tank_file() && log_filename.empty()) {
        ret = ERROR_SYSTEM_CONFIG_INVALID;
        srs_error("must specifies the file to write log to. ret=%d", ret);
        return ret;
    }
694
    if (!log_filename.empty()) {
winlin authored
695
        srs_trace("log file is %s", log_filename.c_str());
696 697
    }
    
698
    return ret;
winlin authored
699 700 701 702
}

int SrsConfig::parse_argv(int& i, char** argv)
{
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
    int ret = ERROR_SUCCESS;

    char* p = argv[i];
        
    if (*p++ != '-') {
        ret = ERROR_SYSTEM_CONFIG_INVALID;
        srs_error("invalid options(index=%d, value=%s), "
            "must starts with -, see help: %s -h, ret=%d", i, argv[i], argv[0], ret);
        return ret;
    }
    
    while (*p) {
        switch (*p++) {
            case '?':
            case 'h':
                show_help = true;
                break;
            case 'v':
            case 'V':
                show_version = true;
                break;
            case 'c':
                if (*p) {
                    config_file = p;
                    return ret;
                }
                if (argv[++i]) {
                    config_file = argv[i];
                    return ret;
                }
                ret = ERROR_SYSTEM_CONFIG_INVALID;
                srs_error("option \"-c\" requires parameter, ret=%d", ret);
                return ret;
            default:
                ret = ERROR_SYSTEM_CONFIG_INVALID;
                srs_error("invalid option: \"%c\", see help: %s -h, ret=%d", *(p - 1), argv[0], ret);
                return ret;
        }
    }
    
    return ret;
winlin authored
744 745 746 747
}

void SrsConfig::print_help(char** argv)
{
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    printf(
        RTMP_SIG_SRS_NAME" "RTMP_SIG_SRS_VERSION" "RTMP_SIG_SRS_COPYRIGHT"\n" 
        "Primary Authors: "RTMP_SIG_SRS_PRIMARY_AUTHROS"\n"
        "Build: "SRS_BUILD_DATE" Configuration:"SRS_CONFIGURE"\n"
        "Usage: %s [-h?vV] [-c <filename>]\n" 
        "\n"
        "Options:\n"
        "   -?-h            : show help\n"
        "   -v-V            : show version and exit\n"
        "   -c filename     : set configuration file\n"
        "\n"
        RTMP_SIG_SRS_WEB"\n"
        RTMP_SIG_SRS_URL"\n"
        "Email: "RTMP_SIG_SRS_EMAIL"\n"
        "\n",
        argv[0]);
winlin authored
764 765 766 767
}

SrsConfDirective* SrsConfig::get_vhost(string vhost)
{
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
    srs_assert(root);
    
    for (int i = 0; i < (int)root->directives.size(); i++) {
        SrsConfDirective* conf = root->at(i);
        
        if (conf->name != "vhost") {
            continue;
        }
        
        if (conf->arg0() == vhost) {
            return conf;
        }
    }
    
    if (vhost != RTMP_VHOST_DEFAULT) {
        return get_vhost(RTMP_VHOST_DEFAULT);
    }
    
    return NULL;
winlin authored
787 788 789 790
}

SrsConfDirective* SrsConfig::get_vhost_on_connect(string vhost)
{
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) { 
        return NULL;
    }
    
    conf = conf->get("http_hooks");
    if (!conf) {
        return NULL;
    }
    
    SrsConfDirective* enabled = conf->get("enabled");
    if (!enabled || enabled->arg0() != "on") {
        return NULL;
    }
    
    return conf->get("on_connect");
winlin authored
808 809 810 811
}

SrsConfDirective* SrsConfig::get_vhost_on_close(string vhost)
{
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) { 
        return NULL;
    }
    
    conf = conf->get("http_hooks");
    if (!conf) {
        return NULL;
    }
    
    SrsConfDirective* enabled = conf->get("enabled");
    if (!enabled || enabled->arg0() != "on") {
        return NULL;
    }
    
    return conf->get("on_close");
winlin authored
829 830 831 832
}

SrsConfDirective* SrsConfig::get_vhost_on_publish(string vhost)
{
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) { 
        return NULL;
    }
    
    conf = conf->get("http_hooks");
    if (!conf) {
        return NULL;
    }
    
    SrsConfDirective* enabled = conf->get("enabled");
    if (!enabled || enabled->arg0() != "on") {
        return NULL;
    }
    
    return conf->get("on_publish");
winlin authored
850 851 852 853
}

SrsConfDirective* SrsConfig::get_vhost_on_unpublish(string vhost)
{
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) { 
        return NULL;
    }
    
    conf = conf->get("http_hooks");
    if (!conf) {
        return NULL;
    }
    
    SrsConfDirective* enabled = conf->get("enabled");
    if (!enabled || enabled->arg0() != "on") {
        return NULL;
    }
    
    return conf->get("on_unpublish");
winlin authored
871 872 873 874
}

SrsConfDirective* SrsConfig::get_vhost_on_play(string vhost)
{
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) { 
        return NULL;
    }
    
    conf = conf->get("http_hooks");
    if (!conf) {
        return NULL;
    }
    
    SrsConfDirective* enabled = conf->get("enabled");
    if (!enabled || enabled->arg0() != "on") {
        return NULL;
    }
    
    return conf->get("on_play");
winlin authored
892 893 894 895
}

SrsConfDirective* SrsConfig::get_vhost_on_stop(string vhost)
{
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) { 
        return NULL;
    }
    
    conf = conf->get("http_hooks");
    if (!conf) {
        return NULL;
    }
    
    SrsConfDirective* enabled = conf->get("enabled");
    if (!enabled || enabled->arg0() != "on") {
        return NULL;
    }
    
    return conf->get("on_stop");
winlin authored
913 914 915 916
}

bool SrsConfig::get_vhost_enabled(string vhost)
{
917 918 919
    SrsConfDirective* vhost_conf = get_vhost(vhost);
    
    return get_vhost_enabled(vhost_conf);
winlin authored
920 921 922 923
}

bool SrsConfig::get_vhost_enabled(SrsConfDirective* vhost)
{
924 925 926 927 928 929 930 931 932 933 934 935 936 937
    if (!vhost) {
        return false;
    }
    
    SrsConfDirective* conf = vhost->get("enabled");
    if (!conf) {
        return true;
    }
    
    if (conf->arg0() == "off") {
        return false;
    }
    
    return true;
winlin authored
938 939 940 941
}

SrsConfDirective* SrsConfig::get_transcode(string vhost, string scope)
{
942
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
943
944 945 946 947 948 949 950 951 952 953 954 955 956 957
    if (!conf) {
        return NULL;
    }
    
    SrsConfDirective* transcode = conf->get("transcode");
    if (!transcode) {
        return NULL;
    }
    
    if (transcode->arg0() == scope) {
        return transcode;
    }
    
    return NULL;
winlin authored
958 959 960 961
}

bool SrsConfig::get_transcode_enabled(SrsConfDirective* transcode)
{
962 963 964 965 966 967 968 969 970 971
    if (!transcode) {
        return false;
    }
    
    SrsConfDirective* conf = transcode->get("enabled");
    if (!conf || conf->arg0() != "on") {
        return false;
    }
    
    return true;
winlin authored
972 973 974 975
}

string SrsConfig::get_transcode_ffmpeg(SrsConfDirective* transcode)
{
976 977 978 979 980 981 982 983 984 985
    if (!transcode) {
        return "";
    }
    
    SrsConfDirective* conf = transcode->get("ffmpeg");
    if (!conf) {
        return "";
    }
    
    return conf->arg0();
winlin authored
986 987 988 989
}

void SrsConfig::get_transcode_engines(SrsConfDirective* transcode, std::vector<SrsConfDirective*>& engines)
{
990 991 992 993 994 995 996 997 998 999 1000 1001 1002
    if (!transcode) {
        return;
    }
    
    for (int i = 0; i < (int)transcode->directives.size(); i++) {
        SrsConfDirective* conf = transcode->directives[i];
        
        if (conf->name == "engine") {
            engines.push_back(conf);
        }
    }
    
    return;
winlin authored
1003 1004 1005 1006
}

bool SrsConfig::get_engine_enabled(SrsConfDirective* engine)
{
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    if (!engine) {
        return false;
    }
    
    SrsConfDirective* conf = engine->get("enabled");
    if (!conf || conf->arg0() != "on") {
        return false;
    }
    
    return true;
winlin authored
1017 1018 1019 1020
}

string SrsConfig::get_engine_vcodec(SrsConfDirective* engine)
{
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
    if (!engine) {
        return "";
    }
    
    SrsConfDirective* conf = engine->get("vcodec");
    if (!conf) {
        return "";
    }
    
    return conf->arg0();
winlin authored
1031 1032 1033 1034
}

int SrsConfig::get_engine_vbitrate(SrsConfDirective* engine)
{
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("vbitrate");
    if (!conf) {
        return 0;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1045 1046 1047 1048
}

double SrsConfig::get_engine_vfps(SrsConfDirective* engine)
{
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("vfps");
    if (!conf) {
        return 0;
    }
    
    return ::atof(conf->arg0().c_str());
winlin authored
1059 1060 1061 1062
}

int SrsConfig::get_engine_vwidth(SrsConfDirective* engine)
{
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("vwidth");
    if (!conf) {
        return 0;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1073 1074 1075 1076
}

int SrsConfig::get_engine_vheight(SrsConfDirective* engine)
{
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("vheight");
    if (!conf) {
        return 0;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1087 1088 1089 1090
}

int SrsConfig::get_engine_vthreads(SrsConfDirective* engine)
{
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("vthreads");
    if (!conf) {
        return 0;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1101 1102 1103 1104
}

string SrsConfig::get_engine_vprofile(SrsConfDirective* engine)
{
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
    if (!engine) {
        return "";
    }
    
    SrsConfDirective* conf = engine->get("vprofile");
    if (!conf) {
        return "";
    }
    
    return conf->arg0();
winlin authored
1115 1116 1117 1118
}

string SrsConfig::get_engine_vpreset(SrsConfDirective* engine)
{
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
    if (!engine) {
        return "";
    }
    
    SrsConfDirective* conf = engine->get("vpreset");
    if (!conf) {
        return "";
    }
    
    return conf->arg0();
winlin authored
1129 1130 1131 1132
}

void SrsConfig::get_engine_vparams(SrsConfDirective* engine, std::vector<string>& vparams)
{
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
    if (!engine) {
        return;
    }
    
    SrsConfDirective* conf = engine->get("vparams");
    if (!conf) {
        return;
    }
    
    for (int i = 0; i < (int)conf->directives.size(); i++) {
        SrsConfDirective* p = conf->directives[i];
        if (!p) {
            continue;
        }
        
        vparams.push_back("-" + p->name);
        vparams.push_back(p->arg0());
    }
winlin authored
1151 1152 1153 1154
}

void SrsConfig::get_engine_vfilter(SrsConfDirective* engine, std::vector<string>& vfilter)
{
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
    if (!engine) {
        return;
    }
    
    SrsConfDirective* conf = engine->get("vfilter");
    if (!conf) {
        return;
    }
    
    for (int i = 0; i < (int)conf->directives.size(); i++) {
        SrsConfDirective* p = conf->directives[i];
        if (!p) {
            continue;
        }
        
        vfilter.push_back("-" + p->name);
        vfilter.push_back(p->arg0());
    }
winlin authored
1173 1174 1175 1176
}

string SrsConfig::get_engine_acodec(SrsConfDirective* engine)
{
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    if (!engine) {
        return "";
    }
    
    SrsConfDirective* conf = engine->get("acodec");
    if (!conf) {
        return "";
    }
    
    return conf->arg0();
winlin authored
1187 1188 1189 1190
}

int SrsConfig::get_engine_abitrate(SrsConfDirective* engine)
{
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("abitrate");
    if (!conf) {
        return 0;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1201 1202 1203 1204
}

int SrsConfig::get_engine_asample_rate(SrsConfDirective* engine)
{
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("asample_rate");
    if (!conf) {
        return 0;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1215 1216 1217 1218
}

int SrsConfig::get_engine_achannels(SrsConfDirective* engine)
{
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
    if (!engine) {
        return 0;
    }
    
    SrsConfDirective* conf = engine->get("achannels");
    if (!conf) {
        return 0;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1229 1230 1231 1232
}

void SrsConfig::get_engine_aparams(SrsConfDirective* engine, std::vector<string>& aparams)
{
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    if (!engine) {
        return;
    }
    
    SrsConfDirective* conf = engine->get("aparams");
    if (!conf) {
        return;
    }
    
    for (int i = 0; i < (int)conf->directives.size(); i++) {
        SrsConfDirective* p = conf->directives[i];
        if (!p) {
            continue;
        }
        
        aparams.push_back("-" + p->name);
        aparams.push_back(p->arg0());
    }
winlin authored
1251 1252 1253 1254
}

string SrsConfig::get_engine_output(SrsConfDirective* engine)
{
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
    if (!engine) {
        return "";
    }
    
    SrsConfDirective* conf = engine->get("output");
    if (!conf) {
        return "";
    }
    
    return conf->arg0();
winlin authored
1265 1266
}
1267
string SrsConfig::get_ffmpeg_log_dir()
winlin authored
1268
{
1269 1270
    srs_assert(root);
    
1271
    SrsConfDirective* conf = root->get("ff_log_dir");
1272 1273 1274 1275 1276
    if (!conf || conf->arg0().empty()) {
        return "./objs/logs";
    }
    
    return conf->arg0();
winlin authored
1277 1278
}
1279 1280 1281 1282 1283 1284
string SrsConfig::get_srs_log_file()
{
    srs_assert(root);
    
    SrsConfDirective* conf = root->get("srs_log_file");
    if (!conf || conf->arg0().empty()) {
winlin authored
1285
        return "./objs/srs.log";
1286 1287 1288 1289 1290
    }
    
    return conf->arg0();
}
1291 1292 1293 1294 1295
bool SrsConfig::get_srs_log_tank_file()
{
    srs_assert(root);
    
    SrsConfDirective* conf = root->get("srs_log_tank");
winlin authored
1296 1297
    if (conf && conf->arg0() == "console") {
        return false;
1298 1299
    }
    
winlin authored
1300
    return true;
1301 1302
}
winlin authored
1303 1304 1305 1306
bool SrsConfig::get_deamon()
{
    srs_assert(root);
    
winlin authored
1307
    SrsConfDirective* conf = root->get("daemon");
winlin authored
1308 1309 1310 1311 1312 1313 1314
    if (conf && conf->arg0() == "off") {
        return false;
    }
    
    return true;
}
winlin authored
1315 1316
int SrsConfig::get_max_connections()
{
1317 1318 1319 1320 1321 1322 1323 1324
    srs_assert(root);
    
    SrsConfDirective* conf = root->get("max_connections");
    if (!conf || conf->arg0().empty()) {
        return 2000;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1325 1326 1327 1328
}

bool SrsConfig::get_gop_cache(string vhost)
{
1329
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
1330
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
    if (!conf) {
        return true;
    }
    
    conf = conf->get("gop_cache");
    if (conf && conf->arg0() == "off") {
        return false;
    }
    
    return true;
winlin authored
1341 1342
}
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
bool SrsConfig::get_atc(string vhost)
{
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) {
        return true;
    }
    
    conf = conf->get("atc");
    if (conf && conf->arg0() == "on") {
        return true;
    }
    
    return false;
}
winlin authored
1359 1360
double SrsConfig::get_queue_length(string vhost)
{
1361
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
1362
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
    if (!conf) {
        return SRS_CONF_DEFAULT_QUEUE_LENGTH;
    }
    
    conf = conf->get("queue_length");
    if (!conf || conf->arg0().empty()) {
        return SRS_CONF_DEFAULT_QUEUE_LENGTH;
    }
    
    return ::atoi(conf->arg0().c_str());
winlin authored
1373 1374 1375 1376
}

SrsConfDirective* SrsConfig::get_forward(string vhost)
{
1377
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
1378
1379 1380 1381 1382 1383
    if (!conf) {
        return NULL;
    }
    
    return conf->get("forward");
winlin authored
1384 1385 1386 1387
}

SrsConfDirective* SrsConfig::get_hls(string vhost)
{
1388
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
1389
1390 1391 1392 1393 1394
    if (!conf) {
        return NULL;
    }
    
    return conf->get("hls");
winlin authored
1395 1396 1397 1398
}

bool SrsConfig::get_hls_enabled(string vhost)
{
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
    SrsConfDirective* hls = get_hls(vhost);
    
    if (!hls) {
        return false;
    }
    
    SrsConfDirective* conf = hls->get("enabled");
    
    if (!conf) {
        return false;
    }
    
    if (conf->arg0() == "on") {
        return true;
    }
    
    return false;
winlin authored
1416 1417 1418 1419
}

string SrsConfig::get_hls_path(string vhost)
{
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
    SrsConfDirective* hls = get_hls(vhost);
    
    if (!hls) {
        return SRS_CONF_DEFAULT_HLS_PATH;
    }
    
    SrsConfDirective* conf = hls->get("hls_path");
    
    if (!conf) {
        return SRS_CONF_DEFAULT_HLS_PATH;
    }
winlin authored
1431
1432
    return conf->arg0();
winlin authored
1433 1434 1435 1436
}

double SrsConfig::get_hls_fragment(string vhost)
{
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
    SrsConfDirective* hls = get_hls(vhost);
    
    if (!hls) {
        return SRS_CONF_DEFAULT_HLS_FRAGMENT;
    }
    
    SrsConfDirective* conf = hls->get("hls_fragment");
    
    if (!conf) {
        return SRS_CONF_DEFAULT_HLS_FRAGMENT;
    }
winlin authored
1448
1449
    return ::atof(conf->arg0().c_str());
winlin authored
1450 1451 1452 1453
}

double SrsConfig::get_hls_window(string vhost)
{
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    SrsConfDirective* hls = get_hls(vhost);
    
    if (!hls) {
        return SRS_CONF_DEFAULT_HLS_WINDOW;
    }
    
    SrsConfDirective* conf = hls->get("hls_window");
    
    if (!conf) {
        return SRS_CONF_DEFAULT_HLS_WINDOW;
    }
winlin authored
1465
1466
    return ::atof(conf->arg0().c_str());
winlin authored
1467 1468
}
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
SrsConfDirective* SrsConfig::get_http_api()
{
    return root->get("http_api");
}

bool SrsConfig::get_http_api_enabled()
{
    SrsConfDirective* conf = get_http_api();
    
    if (!conf) {
        return false;
    }
    
    conf = conf->get("enabled");
    if (conf && conf->arg0() == "on") {
        return true;
    }
    
    return false;
}

int SrsConfig::get_http_api_listen()
{
    SrsConfDirective* conf = get_http_api();
    
    if (conf) {
        conf = conf->get("listen");
        
        if (conf && !conf->arg0().empty()) {
            return ::atoi(conf->arg0().c_str());
        }
    }
    
    return 1985;
}

SrsConfDirective* SrsConfig::get_http_stream()
{
    return root->get("http_stream");
}

bool SrsConfig::get_http_stream_enabled()
{
    SrsConfDirective* conf = get_http_stream();
    
    if (!conf) {
        return false;
    }
    
    conf = conf->get("enabled");
    
    if (conf && conf->arg0() == "on") {
        return true;
    }
    
    return false;
}

int SrsConfig::get_http_stream_listen()
{
    SrsConfDirective* conf = get_http_stream();
    
    if (conf) {
        conf = conf->get("listen");
        
        if (conf && !conf->arg0().empty()) {
            return ::atoi(conf->arg0().c_str());
        }
    }
    
    return 8080;
}
winlin authored
1542 1543
SrsConfDirective* SrsConfig::get_refer(string vhost)
{
1544
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
1545
1546 1547 1548 1549 1550
    if (!conf) {
        return NULL;
    }
    
    return conf->get("refer");
winlin authored
1551 1552 1553 1554
}

SrsConfDirective* SrsConfig::get_refer_play(string vhost)
{
1555
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
1556
1557 1558 1559 1560 1561
    if (!conf) {
        return NULL;
    }
    
    return conf->get("refer_play");
winlin authored
1562 1563 1564 1565
}

SrsConfDirective* SrsConfig::get_refer_publish(string vhost)
{
1566
    SrsConfDirective* conf = get_vhost(vhost);
winlin authored
1567
1568 1569 1570 1571 1572
    if (!conf) {
        return NULL;
    }
    
    return conf->get("refer_publish");
winlin authored
1573 1574 1575 1576
}

SrsConfDirective* SrsConfig::get_listen()
{
1577
    return root->get("listen");
winlin authored
1578 1579
}
winlin authored
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
string SrsConfig::get_pid_file()
{
    SrsConfDirective* conf = root->get("pid");
    
    if (!conf) {
        return SRS_CONF_DEFAULT_PID_FILE;
    }
    
    return conf->arg0();
}
winlin authored
1591 1592 1593 1594 1595
int SrsConfig::get_chunk_size(const std::string &vhost)
{
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) {
1596
        return SRS_CONF_DEFAULT_CHUNK_SIZE;
winlin authored
1597 1598 1599 1600 1601 1602
    }

    conf = conf->get("chunk_size");
    if (!conf) {
        // vhost does not specify the chunk size,
        // use the global instead.
1603 1604 1605 1606 1607 1608
        conf = root->get("chunk_size");
        if (!conf) {
            return SRS_CONF_DEFAULT_CHUNK_SIZE;
        }
        
        return ::atoi(conf->arg0().c_str());
winlin authored
1609 1610 1611 1612 1613 1614 1615
    }

    return ::atoi(conf->arg0().c_str());
}

int SrsConfig::get_pithy_print_publish()
{
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
    SrsConfDirective* pithy = root->get("pithy_print");
    if (!pithy) {
        return SRS_STAGE_PUBLISH_USER_INTERVAL_MS;
    }
    
    pithy = pithy->get("publish");
    if (!pithy) {
        return SRS_STAGE_PUBLISH_USER_INTERVAL_MS;
    }
    
    return ::atoi(pithy->arg0().c_str());
winlin authored
1627 1628 1629 1630
}

int SrsConfig::get_pithy_print_forwarder()
{
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
    SrsConfDirective* pithy = root->get("pithy_print");
    if (!pithy) {
        return SRS_STAGE_FORWARDER_INTERVAL_MS;
    }
    
    pithy = pithy->get("forwarder");
    if (!pithy) {
        return SRS_STAGE_FORWARDER_INTERVAL_MS;
    }
    
    return ::atoi(pithy->arg0().c_str());
winlin authored
1642 1643 1644 1645
}

int SrsConfig::get_pithy_print_hls()
{
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
    SrsConfDirective* pithy = root->get("pithy_print");
    if (!pithy) {
        return SRS_STAGE_HLS_INTERVAL_MS;
    }
    
    pithy = pithy->get("hls");
    if (!pithy) {
        return SRS_STAGE_HLS_INTERVAL_MS;
    }
    
    return ::atoi(pithy->arg0().c_str());
winlin authored
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
}

bool SrsConfig::get_bw_check_enabled(const string &vhost)
{
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) {
        return false;
    }

    conf = conf->get("bandcheck");
    if (!conf) {
        return false;
    }

    conf = conf->get("enabled");
    if (!conf || conf->arg0() != "on") {
        return false;
    }

    return true;
}

string SrsConfig::get_bw_check_key(const string &vhost)
{
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) {
        return "";
    }

    conf = conf->get("bandcheck");
    if (!conf) {
        return "";
    }
    
    conf = conf->get("key");
    if (!conf) {
        return "";
    }

    return conf->arg0();
}

int SrsConfig::get_bw_check_interval_ms(const string &vhost)
{
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) {
        return SRS_CONF_DEFAULT_BANDWIDTH_INTERVAL;
    }

    conf = conf->get("bandcheck");
    if (!conf) {
        return SRS_CONF_DEFAULT_BANDWIDTH_INTERVAL;
    }
    
    conf = conf->get("interval_ms");
    if (!conf) {
        return SRS_CONF_DEFAULT_BANDWIDTH_INTERVAL;
    }

    return ::atoi(conf->arg0().c_str()) * 1000;
}

int SrsConfig::get_bw_check_limit_kbps(const string &vhost)
{
    SrsConfDirective* conf = get_vhost(vhost);

    if (!conf) {
        return SRS_CONF_DEFAULT_BANDWIDTH_LIMIT_KBPS;
    }

    conf = conf->get("bandcheck");
    if (!conf) {
        return SRS_CONF_DEFAULT_BANDWIDTH_LIMIT_KBPS;
    }
    
    conf = conf->get("limit_kbps");
    if (!conf) {
        return SRS_CONF_DEFAULT_BANDWIDTH_LIMIT_KBPS;
    }

    return ::atoi(conf->arg0().c_str());
}

int SrsConfig::get_pithy_print_encoder()
{
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
    SrsConfDirective* pithy = root->get("encoder");
    if (!pithy) {
        return SRS_STAGE_ENCODER_INTERVAL_MS;
    }
    
    pithy = pithy->get("forwarder");
    if (!pithy) {
        return SRS_STAGE_ENCODER_INTERVAL_MS;
    }
    
    return ::atoi(pithy->arg0().c_str());
winlin authored
1756 1757 1758 1759
}

int SrsConfig::get_pithy_print_play()
{
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
    SrsConfDirective* pithy = root->get("pithy_print");
    if (!pithy) {
        return SRS_STAGE_PLAY_USER_INTERVAL_MS;
    }
    
    pithy = pithy->get("play");
    if (!pithy) {
        return SRS_STAGE_PLAY_USER_INTERVAL_MS;
    }
    
    return ::atoi(pithy->arg0().c_str());
winlin authored
1771 1772 1773 1774
}

bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b)
{
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
    // both NULL, equal.
    if (!a && !b) {
        return true;
    }
    
    if (!a || !b) {
        return false;
    }
    
    if (a->name != b->name) {
        return false;
    }
    
    if (a->args.size() != b->args.size()) {
        return false;
    }
    
    for (int i = 0; i < (int)a->args.size(); i++) {
        if (a->args.at(i) != b->args.at(i)) {
            return false;
        }
    }
    
    if (a->directives.size() != b->directives.size()) {
        return false;
    }
    
    for (int i = 0; i < (int)a->directives.size(); i++) {
        SrsConfDirective* a0 = a->at(i);
        SrsConfDirective* b0 = b->at(i);
        
        if (!srs_directive_equals(a0, b0)) {
            return false;
        }
    }
    
    return true;
winlin authored
1812
}