Blame view

trunk/src/app/srs_app_server.cpp 37.5 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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_server.hpp>
winlin authored
25 26 27

#include <sys/types.h>
#include <signal.h>
winlin authored
28 29 30
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
winlin authored
31 32

#include <algorithm>
33
using namespace std;
winlin authored
34
35
#include <srs_kernel_log.hpp>
36
#include <srs_kernel_error.hpp>
37
#include <srs_app_rtmp_conn.hpp>
38
#include <srs_app_config.hpp>
39
#include <srs_kernel_utility.hpp>
40 41
#include <srs_app_http_api.hpp>
#include <srs_app_http_conn.hpp>
winlin authored
42
#include <srs_app_ingest.hpp>
43
#include <srs_app_source.hpp>
44
#include <srs_app_utility.hpp>
45
#include <srs_app_heartbeat.hpp>
46
#include <srs_app_mpegts_udp.hpp>
47
#include <srs_app_rtsp.hpp>
48
#include <srs_app_statistic.hpp>
49
#include <srs_app_caster_flv.hpp>
winlin authored
50
#include <srs_core_mem_watch.hpp>
winlin authored
51
52 53 54
// signal defines.
#define SIGNAL_RELOAD SIGHUP
55
// system interval in ms,
winlin authored
56
// all resolution times should be times togother,
57 58 59
// for example, system-interval is x=1s(1000ms),
// then rusage can be 3*x, for instance, 3*1=3s,
// the meminfo canbe 6*x, for instance, 6*1=6s,
60
// for performance refine, @see: https://github.com/simple-rtmp-server/srs/issues/194
61
// @remark, recomment to 1000ms.
62
#define SRS_SYS_CYCLE_INTERVAL 1000
winlin authored
63 64 65

// update time interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_TIME_RESOLUTION_MS_TIMES
66
// @see SYS_TIME_RESOLUTION_US
67
#define SRS_SYS_TIME_RESOLUTION_MS_TIMES 1
winlin authored
68 69 70

// update rusage interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_RUSAGE_RESOLUTION_TIMES
71
#define SRS_SYS_RUSAGE_RESOLUTION_TIMES 3
winlin authored
72
73 74
// update network devices info interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES
75
#define SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES 3
76
winlin authored
77 78
// update rusage interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_CPU_STAT_RESOLUTION_TIMES
79
#define SRS_SYS_CPU_STAT_RESOLUTION_TIMES 3
winlin authored
80
81 82
// update the disk iops interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_DISK_STAT_RESOLUTION_TIMES
83
#define SRS_SYS_DISK_STAT_RESOLUTION_TIMES 6
84
winlin authored
85 86
// update rusage interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_MEMINFO_RESOLUTION_TIMES
87
#define SRS_SYS_MEMINFO_RESOLUTION_TIMES 6
winlin authored
88
89 90
// update platform info interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES
91
#define SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES 9
92
93 94
// update network devices info interval:
//      SRS_SYS_CYCLE_INTERVAL * SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES
95
#define SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES 9
96
97
std::string srs_listener_type2string(SrsListenerType type) 
98 99 100 101 102 103 104 105 106 107
{
    switch (type) {
    case SrsListenerRtmpStream:
        return "RTMP";
    case SrsListenerHttpApi:
        return "HTTP-API";
    case SrsListenerHttpStream:
        return "HTTP-Server";
    case SrsListenerMpegTsOverUdp:
        return "MPEG-TS over UDP";
108 109
    case SrsListenerRtsp:
        return "RTSP";
110 111
    case SrsListenerFlv:
        return "HTTP-FLV";
112 113 114 115 116
    default:
        return "UNKONWN";
    }
}
117
SrsListener::SrsListener(SrsServer* svr, SrsListenerType t)
winlin authored
118
{
119 120 121
    port = 0;
    server = svr;
    type = t;
winlin authored
122 123 124 125 126 127
}

SrsListener::~SrsListener()
{
}
128
SrsListenerType SrsListener::listen_type()
129
{
130
    return type;
131 132
}
133
SrsStreamListener::SrsStreamListener(SrsServer* svr, SrsListenerType t) : SrsListener(svr, t)
134 135 136 137 138 139 140 141 142
{
    listener = NULL;
}

SrsStreamListener::~SrsStreamListener()
{
    srs_freep(listener);
}
143
int SrsStreamListener::listen(string i, int p)
winlin authored
144
{
145 146
    int ret = ERROR_SUCCESS;
    
147 148
    ip = i;
    port = p;
149 150

    srs_freep(listener);
151
    listener = new SrsTcpListener(this, ip, port);
152 153 154

    if ((ret = listener->listen()) != ERROR_SUCCESS) {
        srs_error("tcp listen failed. ret=%d", ret);
winlin authored
155 156 157
        return ret;
    }
    
winlin authored
158
    srs_info("listen thread current_cid=%d, "
159
        "listen at port=%d, type=%d, fd=%d started success, ep=%s:%d",
winlin authored
160
        _srs_context->get_id(), p, type, listener->fd(), i.c_str(), p);
161
162
    srs_trace("%s listen at tcp://%s:%d, fd=%d", srs_listener_type2string(type).c_str(), ip.c_str(), port, listener->fd());
163
164
    return ret;
winlin authored
165 166
}
167
int SrsStreamListener::on_tcp_client(st_netfd_t stfd)
winlin authored
168
{
169 170
    int ret = ERROR_SUCCESS;
    
171
    if ((ret = server->accept_client(type, stfd)) != ERROR_SUCCESS) {
172 173 174
        srs_warn("accept client error. ret=%d", ret);
        return ret;
    }
175
176
    return ret;
winlin authored
177 178
}
179
#ifdef SRS_AUTO_STREAM_CASTER
180
SrsRtspListener::SrsRtspListener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c) : SrsListener(svr, t)
181
{
182
    listener = NULL;
183 184 185

    // the caller already ensure the type is ok,
    // we just assert here for unknown stream caster.
186 187
    srs_assert(type == SrsListenerRtsp);
    if (type == SrsListenerRtsp) {
188
        caster = new SrsRtspCaster(c);
189 190 191 192 193 194
    }
}

SrsRtspListener::~SrsRtspListener()
{
    srs_freep(caster);
195
    srs_freep(listener);
196 197
}
198
int SrsRtspListener::listen(string i, int p)
199 200
{
    int ret = ERROR_SUCCESS;
201 202 203

    // the caller already ensure the type is ok,
    // we just assert here for unknown stream caster.
204
    srs_assert(type == SrsListenerRtsp);
205
    
206 207
    ip = i;
    port = p;
208 209

    srs_freep(listener);
210
    listener = new SrsTcpListener(this, ip, port);
211 212

    if ((ret = listener->listen()) != ERROR_SUCCESS) {
213
        srs_error("rtsp caster listen failed. ret=%d", ret);
214 215 216
        return ret;
    }
    
217
    srs_info("listen thread cid=%d, current_cid=%d, "
218
        "listen at port=%d, type=%d, fd=%d started success, ep=%s:%d",
219
        pthread->cid(), _srs_context->get_id(), port, type, fd, ip.c_str(), port);
220
221
    srs_trace("%s listen at tcp://%s:%d, fd=%d", srs_listener_type2string(type).c_str(), ip.c_str(), port, listener->fd());
222 223 224 225 226 227 228 229

    return ret;
}

int SrsRtspListener::on_tcp_client(st_netfd_t stfd)
{
    int ret = ERROR_SUCCESS;
    
230
    if ((ret = caster->on_tcp_client(stfd)) != ERROR_SUCCESS) {
231 232 233
        srs_warn("accept client error. ret=%d", ret);
        return ret;
    }
234
235 236 237
    return ret;
}
238
SrsHttpFlvListener::SrsHttpFlvListener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c) : SrsListener(svr, t)
239 240 241 242 243
{
    listener = NULL;
    
    // the caller already ensure the type is ok,
    // we just assert here for unknown stream caster.
244 245
    srs_assert(type == SrsListenerFlv);
    if (type == SrsListenerFlv) {
246 247 248 249 250 251 252 253 254 255
        caster = new SrsAppCasterFlv(c);
    }
}

SrsHttpFlvListener::~SrsHttpFlvListener()
{
    srs_freep(caster);
    srs_freep(listener);
}
256
int SrsHttpFlvListener::listen(string i, int p)
257 258 259 260 261
{
    int ret = ERROR_SUCCESS;
    
    // the caller already ensure the type is ok,
    // we just assert here for unknown stream caster.
262
    srs_assert(type == SrsListenerFlv);
263
    
264 265
    ip = i;
    port = p;
266
    
267 268 269 270
    if ((ret = caster->initialize()) != ERROR_SUCCESS) {
        return ret;
    }
    
271 272 273 274 275 276 277 278 279 280
    srs_freep(listener);
    listener = new SrsTcpListener(this, ip, port);
    
    if ((ret = listener->listen()) != ERROR_SUCCESS) {
        srs_error("flv caster listen failed. ret=%d", ret);
        return ret;
    }
    
    srs_info("listen thread cid=%d, current_cid=%d, "
             "listen at port=%d, type=%d, fd=%d started success, ep=%s:%d",
281
             pthread->cid(), _srs_context->get_id(), port, type, fd, ip.c_str(), port);
282
    
283
    srs_trace("%s listen at tcp://%s:%d, fd=%d", srs_listener_type2string(type).c_str(), ip.c_str(), port, listener->fd());
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    
    return ret;
}

int SrsHttpFlvListener::on_tcp_client(st_netfd_t stfd)
{
    int ret = ERROR_SUCCESS;
    
    if ((ret = caster->on_tcp_client(stfd)) != ERROR_SUCCESS) {
        srs_warn("accept client error. ret=%d", ret);
        return ret;
    }
    
    return ret;
}
299
#endif
300
301
SrsUdpStreamListener::SrsUdpStreamListener(SrsServer* svr, SrsListenerType t, ISrsUdpHandler* c) : SrsListener(svr, t)
302
{
303
    listener = NULL;
304
    caster = c;
305 306
}
307
SrsUdpStreamListener::~SrsUdpStreamListener()
308
{
309
    srs_freep(listener);
310 311
}
312
int SrsUdpStreamListener::listen(string i, int p)
313 314 315 316 317
{
    int ret = ERROR_SUCCESS;

    // the caller already ensure the type is ok,
    // we just assert here for unknown stream caster.
318
    srs_assert(type == SrsListenerMpegTsOverUdp);
319
    
320 321
    ip = i;
    port = p;
322 323

    srs_freep(listener);
324
    listener = new SrsUdpListener(caster, ip, port);
325 326 327

    if ((ret = listener->listen()) != ERROR_SUCCESS) {
        srs_error("udp caster listen failed. ret=%d", ret);
328 329 330
        return ret;
    }
    
winlin authored
331
    srs_info("listen thread current_cid=%d, "
332
        "listen at port=%d, type=%d, fd=%d started success, ep=%s:%d",
winlin authored
333
        _srs_context->get_id(), p, type, listener->fd(), i.c_str(), p);
334 335 336 337 338 339
    
    // notify the handler the fd changed.
    if ((ret = caster->on_stfd_change(listener->stfd())) != ERROR_SUCCESS) {
        srs_error("notify handler fd changed. ret=%d", ret);
        return ret;
    }
340
341
    srs_trace("%s listen at udp://%s:%d, fd=%d", srs_listener_type2string(type).c_str(), ip.c_str(), port, listener->fd());
342
343 344
    return ret;
}
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

#ifdef SRS_AUTO_STREAM_CASTER
SrsUdpCasterListener::SrsUdpCasterListener(SrsServer* svr, SrsListenerType t, SrsConfDirective* c) : SrsUdpStreamListener(svr, t, NULL)
{
    // the caller already ensure the type is ok,
    // we just assert here for unknown stream caster.
    srs_assert(type == SrsListenerMpegTsOverUdp);
    if (type == SrsListenerMpegTsOverUdp) {
        caster = new SrsMpegtsOverUdp(c);
    }
}

SrsUdpCasterListener::~SrsUdpCasterListener()
{
    srs_freep(caster);
}
361
#endif
362
363 364 365 366 367 368 369 370
SrsSignalManager* SrsSignalManager::instance = NULL;

SrsSignalManager::SrsSignalManager(SrsServer* server)
{
    SrsSignalManager::instance = this;
    
    _server = server;
    sig_pipe[0] = sig_pipe[1] = -1;
371
    pthread = new SrsEndlessThread("signal", this);
372 373 374 375 376 377 378 379 380 381 382 383 384
    signal_read_stfd = NULL;
}

SrsSignalManager::~SrsSignalManager()
{
    srs_close_stfd(signal_read_stfd);
    
    if (sig_pipe[0] > 0) {
        ::close(sig_pipe[0]);
    }
    if (sig_pipe[1] > 0) {
        ::close(sig_pipe[1]);
    }
winlin authored
385 386
    
    srs_freep(pthread);
387 388 389 390 391
}

int SrsSignalManager::initialize()
{
    int ret = ERROR_SUCCESS;
winlin authored
392 393 394 395 396 397 398 399 400 401 402 403 404 405
    
    /* Create signal pipe */
    if (pipe(sig_pipe) < 0) {
        ret = ERROR_SYSTEM_CREATE_PIPE;
        srs_error("create signal manager pipe failed. ret=%d", ret);
        return ret;
    }
    
    if ((signal_read_stfd = st_netfd_open(sig_pipe[0])) == NULL) {
        ret = ERROR_SYSTEM_CREATE_PIPE;
        srs_error("create signal manage st pipe failed. ret=%d", ret);
        return ret;
    }
    
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    return ret;
}

int SrsSignalManager::start()
{
    /**
    * Note that if multiple processes are used (see below), 
    * the signal pipe should be initialized after the fork(2) call 
    * so that each process has its own private pipe.
    */
    struct sigaction sa;
    
    /* Install sig_catcher() as a signal handler */
    sa.sa_handler = SrsSignalManager::sig_catcher;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGNAL_RELOAD, &sa, NULL);
    
    sa.sa_handler = SrsSignalManager::sig_catcher;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGTERM, &sa, NULL);
    
    sa.sa_handler = SrsSignalManager::sig_catcher;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGINT, &sa, NULL);
    
434 435 436 437 438 439 440
    sa.sa_handler = SrsSignalManager::sig_catcher;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGUSR2, &sa, NULL);
    
    srs_trace("signal installed");
    
441 442 443 444 445 446 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
    return pthread->start();
}

int SrsSignalManager::cycle()
{
    int ret = ERROR_SUCCESS;

    int signo;
    
    /* Read the next signal from the pipe */
    st_read(signal_read_stfd, &signo, sizeof(int), ST_UTIME_NO_TIMEOUT);
    
    /* Process signal synchronously */
    _server->on_signal(signo);
    
    return ret;
}

void SrsSignalManager::sig_catcher(int signo)
{
    int err;
    
    /* Save errno to restore it after the write() */
    err = errno;
    
    /* write() is reentrant/async-safe */
    int fd = SrsSignalManager::instance->sig_pipe[1];
    write(fd, &signo, sizeof(int));
    
    errno = err;
}
473 474 475 476 477 478 479 480
ISrsServerCycle::ISrsServerCycle()
{
}

ISrsServerCycle::~ISrsServerCycle()
{
}
winlin authored
481 482
SrsServer::SrsServer()
{
483 484
    signal_reload = false;
    signal_gmc_stop = false;
485
    signal_gracefully_quit = false;
winlin authored
486
    pid_fd = -1;
487
    
488
    signal_manager = NULL;
489
    
490 491
    handler = NULL;
    
492 493 494
    // donot new object in constructor,
    // for some global instance is not ready now,
    // new these objects in initialize instead.
495
#ifdef SRS_AUTO_HTTP_API
496
    http_api_mux = new SrsHttpServeMux();
winlin authored
497
#endif
498
#ifdef SRS_AUTO_HTTP_SERVER
499
    http_server = new SrsHttpServer(this);
winlin authored
500
#endif
winlin authored
501
#ifdef SRS_AUTO_HTTP_CORE
502 503
    http_heartbeat = NULL;
#endif
504
#ifdef SRS_AUTO_INGEST
505
    ingester = NULL;
506
#endif
winlin authored
507 508 509 510
}

SrsServer::~SrsServer()
{
511 512 513 514 515
    destroy();
}

void SrsServer::destroy()
{
516
    srs_warn("start destroy server");
517
    
518
    dispose();
winlin authored
519
    
520
#ifdef SRS_AUTO_HTTP_API
521
    srs_freep(http_api_mux);
winlin authored
522 523
#endif
524
#ifdef SRS_AUTO_HTTP_SERVER
525
    srs_freep(http_server);
winlin authored
526
#endif
527
winlin authored
528
#ifdef SRS_AUTO_HTTP_CORE
529 530 531
    srs_freep(http_heartbeat);
#endif
532
#ifdef SRS_AUTO_INGEST
533 534
    srs_freep(ingester);
#endif
535 536 537 538 539 540 541
    
    if (pid_fd > 0) {
        ::close(pid_fd);
        pid_fd = -1;
    }
    
    srs_freep(signal_manager);
winlin authored
542 543
}
544 545 546 547
void SrsServer::dispose()
{
    _srs_config->unsubscribe(this);
    
548 549 550 551 552 553 554 555
    // prevent fresh clients.
    close_listeners(SrsListenerRtmpStream);
    close_listeners(SrsListenerHttpApi);
    close_listeners(SrsListenerHttpStream);
    close_listeners(SrsListenerMpegTsOverUdp);
    close_listeners(SrsListenerRtsp);
    close_listeners(SrsListenerFlv);
    
556 557 558 559
#ifdef SRS_AUTO_INGEST
    ingester->dispose();
#endif
    
560 561
    SrsSource::dispose_all();
    
562 563 564 565 566 567 568 569 570
    while (!conns.empty()) {
        std::vector<SrsConnection*>::iterator it;
        for (it = conns.begin(); it != conns.end(); ++it) {
            SrsConnection* conn = *it;
            conn->dispose();
        }
        
        st_usleep(100 * 1000);
    }
winlin authored
571
    
572
#ifdef SRS_AUTO_MEM_WATCH
winlin authored
573 574
    srs_memory_report();
#endif
575 576
}
577
int SrsServer::initialize(ISrsServerCycle* cycle_handler)
winlin authored
578
{
579
    int ret = ERROR_SUCCESS;
winlin authored
580
    
581 582 583
    // ensure the time is ok.
    srs_update_system_time_ms();
    
winlin authored
584
    // for the main objects(server, config, log, context),
585 586 587 588 589
    // never subscribe handler in constructor,
    // instead, subscribe handler in initialize method.
    srs_assert(_srs_config);
    _srs_config->subscribe(this);
    
590 591 592
    srs_assert(!signal_manager);
    signal_manager = new SrsSignalManager(this);
    
593 594 595 596 597
    handler = cycle_handler;
    if(handler && (ret = handler->initialize()) != ERROR_SUCCESS){
        return ret;
    }
    
598
#ifdef SRS_AUTO_HTTP_API
599 600 601 602
    if ((ret = http_api_mux->initialize()) != ERROR_SUCCESS) {
        return ret;
    }
#endif
603
604
#ifdef SRS_AUTO_HTTP_SERVER
605 606
    srs_assert(http_server);
    if ((ret = http_server->initialize()) != ERROR_SUCCESS) {
607 608
        return ret;
    }
609
#endif
610
winlin authored
611
#ifdef SRS_AUTO_HTTP_CORE
612 613 614
    srs_assert(!http_heartbeat);
    http_heartbeat = new SrsHttpHeartbeat();
#endif
615
616
#ifdef SRS_AUTO_INGEST
617 618 619
    srs_assert(!ingester);
    ingester = new SrsIngester();
#endif
winlin authored
620
621
    return ret;
winlin authored
622 623
}
624 625 626 627 628
int SrsServer::initialize_st()
{
    int ret = ERROR_SUCCESS;
    
    // init st
winlin authored
629
    if ((ret = srs_st_init()) != ERROR_SUCCESS) {
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
        srs_error("init st failed. ret=%d", ret);
        return ret;
    }
    
    // @remark, st alloc segment use mmap, which only support 32757 threads,
    // if need to support more, for instance, 100k threads, define the macro MALLOC_STACK.
    // TODO: FIXME: maybe can use "sysctl vm.max_map_count" to refine.
    if (_srs_config->get_max_connections() > 32756) {
        ret = ERROR_ST_EXCEED_THREADS;
        srs_error("st mmap for stack allocation must <= %d threads, "
                  "@see Makefile of st for MALLOC_STACK, please build st manually by "
                  "\"make EXTRA_CFLAGS=-DMALLOC_STACK linux-debug\", ret=%d", ret);
        return ret;
    }
    
    // set current log id.
    _srs_context->generate_id();
    srs_trace("server main cid=%d", _srs_context->get_id());
    
    return ret;
}
652 653 654 655 656
int SrsServer::initialize_signal()
{
    return signal_manager->initialize();
}
winlin authored
657 658 659 660
int SrsServer::acquire_pid_file()
{
    int ret = ERROR_SUCCESS;
    
winlin authored
661 662 663 664 665
    // when srs in dolphin mode, no need the pid file.
    if (_srs_config->is_dolphin()) {
        return ret;
    }
    
winlin authored
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
    std::string pid_file = _srs_config->get_pid_file();
    
    // -rw-r--r-- 
    // 644
    int mode = S_IRUSR | S_IWUSR |  S_IRGRP | S_IROTH;
    
    int fd;
    // open pid file
    if ((fd = ::open(pid_file.c_str(), O_WRONLY | O_CREAT, mode)) < 0) {
        ret = ERROR_SYSTEM_PID_ACQUIRE;
        srs_error("open pid file %s error, ret=%#x", pid_file.c_str(), ret);
        return ret;
    }
    
    // require write lock
681
    struct flock lock;
winlin authored
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 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

    lock.l_type = F_WRLCK; // F_RDLCK, F_WRLCK, F_UNLCK
    lock.l_start = 0; // type offset, relative to l_whence
    lock.l_whence = SEEK_SET;  // SEEK_SET, SEEK_CUR, SEEK_END
    lock.l_len = 0;
    
    if (fcntl(fd, F_SETLK, &lock) < 0) {
        if(errno == EACCES || errno == EAGAIN) {
            ret = ERROR_SYSTEM_PID_ALREADY_RUNNING;
            srs_error("srs is already running! ret=%#x", ret);
            return ret;
        }
        
        ret = ERROR_SYSTEM_PID_LOCK;
        srs_error("require lock for file %s error! ret=%#x", pid_file.c_str(), ret);
        return ret;
    }

    // truncate file
    if (ftruncate(fd, 0) < 0) {
        ret = ERROR_SYSTEM_PID_TRUNCATE_FILE;
        srs_error("truncate pid file %s error! ret=%#x", pid_file.c_str(), ret);
        return ret;
    }

    int pid = (int)getpid();
    
    // write the pid
    char buf[512];
    snprintf(buf, sizeof(buf), "%d", pid);
    if (write(fd, buf, strlen(buf)) != (int)strlen(buf)) {
        ret = ERROR_SYSTEM_PID_WRITE_FILE;
        srs_error("write our pid error! pid=%d file=%s ret=%#x", pid, pid_file.c_str(), ret);
        return ret;
    }

    // auto close when fork child process.
    int val;
    if ((val = fcntl(fd, F_GETFD, 0)) < 0) {
        ret = ERROR_SYSTEM_PID_GET_FILE_INFO;
        srs_error("fnctl F_GETFD error! file=%s ret=%#x", pid_file.c_str(), ret);
        return ret;
    }
    val |= FD_CLOEXEC;
    if (fcntl(fd, F_SETFD, val) < 0) {
        ret = ERROR_SYSTEM_PID_SET_FILE_INFO;
        srs_error("fcntl F_SETFD error! file=%s ret=%#x", pid_file.c_str(), ret);
        return ret;
    }
    
    srs_trace("write pid=%d to %s success!", pid, pid_file.c_str());
winlin authored
733
    pid_fd = fd;
winlin authored
734 735 736 737
    
    return ret;
}
winlin authored
738 739
int SrsServer::listen()
{
740 741
    int ret = ERROR_SUCCESS;
    
742 743
    if ((ret = listen_rtmp()) != ERROR_SUCCESS) {
        return ret;
744
    }
745 746 747
    
    if ((ret = listen_http_api()) != ERROR_SUCCESS) {
        return ret;
748 749
    }
    
750 751
    if ((ret = listen_http_stream()) != ERROR_SUCCESS) {
        return ret;
752 753
    }
    
754 755 756 757
    if ((ret = listen_stream_caster()) != ERROR_SUCCESS) {
        return ret;
    }
    
758
    return ret;
winlin authored
759 760
}
761 762 763 764 765 766
int SrsServer::register_signal()
{
    // start signal process thread.
    return signal_manager->start();
}
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
int SrsServer::http_handle()
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_API
    srs_assert(http_api_mux);
    if ((ret = http_api_mux->handle("/", new SrsGoApiRoot())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api", new SrsGoApiApi())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1", new SrsGoApiV1())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/summaries", new SrsGoApiSummaries())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/rusages", new SrsGoApiRusages())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/self_proc_stats", new SrsGoApiSelfProcStats())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/system_proc_stats", new SrsGoApiSystemProcStats())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/meminfos", new SrsGoApiMemInfos())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/authors", new SrsGoApiAuthors())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/requests", new SrsGoApiRequests())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/vhosts", new SrsGoApiVhosts())) != ERROR_SUCCESS) {
        return ret;
    }
    if ((ret = http_api_mux->handle("/api/v1/streams", new SrsGoApiStreams())) != ERROR_SUCCESS) {
        return ret;
    }
#endif

    return ret;
}
winlin authored
817
int SrsServer::ingest()
winlin authored
818
{
819 820
    int ret = ERROR_SUCCESS;
    
821
#ifdef SRS_AUTO_INGEST
822 823
    if ((ret = ingester->start()) != ERROR_SUCCESS) {
        srs_error("start ingest streams failed. ret=%d", ret);
824 825
        return ret;
    }
826
#endif
winlin authored
827 828 829 830 831 832 833

    return ret;
}

int SrsServer::cycle()
{
    int ret = ERROR_SUCCESS;
834 835 836

    ret = do_cycle();
837
#ifdef SRS_AUTO_GPERF_MC
838
    destroy();
839
    
winlin authored
840
    // remark, for gmc, never invoke the exit().
841 842 843
    srs_warn("sleep a long time for system st-threads to cleanup.");
    st_usleep(3 * 1000 * 1000);
    srs_warn("system quit");
844 845
#else
    srs_warn("main cycle terminated, system quit normally.");
846
    dispose();
winlin authored
847
    srs_trace("srs terminated");
848
    exit(0);
849 850
#endif
    
851 852 853 854 855 856 857
    return ret;
}

void SrsServer::remove(SrsConnection* conn)
{
    std::vector<SrsConnection*>::iterator it = std::find(conns.begin(), conns.end(), conn);
    
858 859 860 861
    // removed by destroy, ignore.
    if (it == conns.end()) {
        srs_warn("server moved connection, ignore.");
        return;
862 863
    }
    
864 865
    conns.erase(it);
    
866 867
    srs_info("conn removed. conns=%d", (int)conns.size());
    
868 869
    SrsStatistic* stat = SrsStatistic::instance();
    stat->kbps_add_delta(conn);
870
    
871 872 873 874 875 876 877 878 879 880 881 882
    // all connections are created by server,
    // so we free it here.
    srs_freep(conn);
}

void SrsServer::on_signal(int signo)
{
    if (signo == SIGNAL_RELOAD) {
        signal_reload = true;
        return;
    }
    
883
    if (signo == SIGINT || signo == SIGUSR2) {
884 885 886 887 888
#ifdef SRS_AUTO_GPERF_MC
        srs_trace("gmc is on, main cycle will terminate normally.");
        signal_gmc_stop = true;
#else
        srs_trace("user terminate program");
889
#ifdef SRS_AUTO_MEM_WATCH
winlin authored
890 891
        srs_memory_report();
#endif
892 893 894 895 896
        exit(0);
#endif
        return;
    }
    
897 898 899
    if (signo == SIGTERM && !signal_gracefully_quit) {
        srs_trace("user terminate program, gracefully quit.");
        signal_gracefully_quit = true;
900 901 902 903 904 905 906
        return;
    }
}

int SrsServer::do_cycle()
{
    int ret = ERROR_SUCCESS;
907
    
winlin authored
908 909
    // find the max loop
    int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES);
910
    
911
#ifdef SRS_AUTO_STAT
winlin authored
912 913
    max = srs_max(max, SRS_SYS_RUSAGE_RESOLUTION_TIMES);
    max = srs_max(max, SRS_SYS_CPU_STAT_RESOLUTION_TIMES);
914
    max = srs_max(max, SRS_SYS_DISK_STAT_RESOLUTION_TIMES);
winlin authored
915
    max = srs_max(max, SRS_SYS_MEMINFO_RESOLUTION_TIMES);
916
    max = srs_max(max, SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES);
917
    max = srs_max(max, SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES);
918
    max = srs_max(max, SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES);
919
#endif
winlin authored
920
    
921 922
    // the deamon thread, update the time cache
    while (true) {
923
        if(handler && (ret = handler->on_cycle((int)conns.size())) != ERROR_SUCCESS){
924 925 926 927
            srs_error("cycle handle failed. ret=%d", ret);
            return ret;
        }
            
928
        // the interval in config.
929
        int heartbeat_max_resolution = (int)(_srs_config->get_heartbeat_interval() / SRS_SYS_CYCLE_INTERVAL);
930 931
        
        // dynamic fetch the max.
932 933
        int temp_max = max;
        temp_max = srs_max(temp_max, heartbeat_max_resolution);
934
        
935
        for (int i = 0; i < temp_max; i++) {
winlin authored
936
            st_usleep(SRS_SYS_CYCLE_INTERVAL * 1000);
937 938 939 940 941 942
            
            // gracefully quit for SIGINT or SIGTERM.
            if (signal_gracefully_quit) {
                srs_trace("cleanup for gracefully terminate.");
                return ret;
            }
943
        
944 945 946 947 948
            // for gperf heap checker,
            // @see: research/gperftools/heap-checker/heap_checker.cc
            // if user interrupt the program, exit to check mem leak.
            // but, if gperf, use reload to ensure main return normally,
            // because directly exit will cause core-dump.
949
#ifdef SRS_AUTO_GPERF_MC
winlin authored
950
            if (signal_gmc_stop) {
951
                srs_warn("gmc got singal to stop server.");
952
                return ret;
winlin authored
953
            }
954
#endif
955
        
956
            // do reload the config.
winlin authored
957 958 959 960 961 962 963 964 965 966
            if (signal_reload) {
                signal_reload = false;
                srs_info("get signal reload, to reload the config.");
                
                if ((ret = _srs_config->reload()) != ERROR_SUCCESS) {
                    srs_error("reload config failed. ret=%d", ret);
                    return ret;
                }
                srs_trace("reload config success.");
            }
967
            
968 969 970 971 972
            // notice the stream sources to cycle.
            if ((ret = SrsSource::cycle_all()) != ERROR_SUCCESS) {
                return ret;
            }
            
973
            // update the cache time
winlin authored
974
            if ((i % SRS_SYS_TIME_RESOLUTION_MS_TIMES) == 0) {
975
                srs_info("update current time cache.");
winlin authored
976 977
                srs_update_system_time_ms();
            }
978
            
979
#ifdef SRS_AUTO_STAT
winlin authored
980
            if ((i % SRS_SYS_RUSAGE_RESOLUTION_TIMES) == 0) {
981
                srs_info("update resource info, rss.");
winlin authored
982 983
                srs_update_system_rusage();
            }
winlin authored
984
            if ((i % SRS_SYS_CPU_STAT_RESOLUTION_TIMES) == 0) {
985
                srs_info("update cpu info, cpu usage.");
986
                srs_update_proc_stat();
987
            }
988 989 990 991
            if ((i % SRS_SYS_DISK_STAT_RESOLUTION_TIMES) == 0) {
                srs_info("update disk info, disk iops.");
                srs_update_disk_stat();
            }
winlin authored
992
            if ((i % SRS_SYS_MEMINFO_RESOLUTION_TIMES) == 0) {
993
                srs_info("update memory info, usage/free.");
winlin authored
994 995
                srs_update_meminfo();
            }
996
            if ((i % SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES) == 0) {
997
                srs_info("update platform info, uptime/load.");
998 999
                srs_update_platform_info();
            }
1000
            if ((i % SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES) == 0) {
1001
                srs_info("update network devices info.");
1002 1003
                srs_update_network_devices();
            }
1004
            if ((i % SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES) == 0) {
winlin authored
1005 1006
                srs_info("update network server kbps info.");
                resample_kbps();
1007
            }
winlin authored
1008
    #ifdef SRS_AUTO_HTTP_CORE
1009 1010
            if (_srs_config->get_heartbeat_enabled()) {
                if ((i % heartbeat_max_resolution) == 0) {
1011
                    srs_info("do http heartbeat, for internal server to report.");
1012 1013 1014
                    http_heartbeat->heartbeat();
                }
            }
1015
    #endif
1016
#endif
1017
            
1018
            srs_info("server main thread loop");
1019 1020
        }
    }
1021
1022
    return ret;
winlin authored
1023 1024
}
1025 1026 1027 1028 1029
int SrsServer::listen_rtmp()
{
    int ret = ERROR_SUCCESS;
    
    // stream service port.
1030 1031
    std::vector<std::string> ip_ports = _srs_config->get_listens();
    srs_assert((int)ip_ports.size() > 0);
1032 1033 1034
    
    close_listeners(SrsListenerRtmpStream);
    
1035
    for (int i = 0; i < (int)ip_ports.size(); i++) {
1036
        SrsListener* listener = new SrsStreamListener(this, SrsListenerRtmpStream);
1037 1038
        listeners.push_back(listener);
        
1039 1040 1041 1042 1043 1044
        std::string ip;
        int port;
        srs_parse_endpoint(ip_ports[i], ip, port);
        
        if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) {
            srs_error("RTMP stream listen at %s:%d failed. ret=%d", ip.c_str(), port, ret);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
            return ret;
        }
    }
    
    return ret;
}

int SrsServer::listen_http_api()
{
    int ret = ERROR_SUCCESS;
    
1056
#ifdef SRS_AUTO_HTTP_API
1057 1058
    close_listeners(SrsListenerHttpApi);
    if (_srs_config->get_http_api_enabled()) {
1059
        SrsListener* listener = new SrsStreamListener(this, SrsListenerHttpApi);
1060 1061
        listeners.push_back(listener);
        
1062 1063 1064 1065 1066 1067 1068 1069
        std::string ep = _srs_config->get_http_api_listen();
        
        std::string ip;
        int port;
        srs_parse_endpoint(ep, ip, port);
        
        if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) {
            srs_error("HTTP api listen at %s:%d failed. ret=%d", ip.c_str(), port, ret);
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
            return ret;
        }
    }
#endif
    
    return ret;
}

int SrsServer::listen_http_stream()
{
    int ret = ERROR_SUCCESS;
    
1082
#ifdef SRS_AUTO_HTTP_SERVER
1083 1084
    close_listeners(SrsListenerHttpStream);
    if (_srs_config->get_http_stream_enabled()) {
1085
        SrsListener* listener = new SrsStreamListener(this, SrsListenerHttpStream);
1086 1087
        listeners.push_back(listener);
        
1088 1089 1090 1091 1092 1093 1094 1095
        std::string ep = _srs_config->get_http_stream_listen();
        
        std::string ip;
        int port;
        srs_parse_endpoint(ep, ip, port);
        
        if ((ret = listener->listen(ip, port)) != ERROR_SUCCESS) {
            srs_error("HTTP stream listen at %s:%d failed. ret=%d", ip.c_str(), port, ret);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
            return ret;
        }
    }
#endif
    
    return ret;
}

int SrsServer::listen_stream_caster()
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_STREAM_CASTER
    close_listeners(SrsListenerMpegTsOverUdp);
    
    std::vector<SrsConfDirective*>::iterator it;
    std::vector<SrsConfDirective*> stream_casters = _srs_config->get_stream_casters();

    for (it = stream_casters.begin(); it != stream_casters.end(); ++it) {
        SrsConfDirective* stream_caster = *it;
        if (!_srs_config->get_stream_caster_enabled(stream_caster)) {
            continue;
        }
1120
        SrsListener* listener = NULL;
1121 1122 1123

        std::string caster = _srs_config->get_stream_caster_engine(stream_caster);
        if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_MPEGTS_OVER_UDP) {
1124
            listener = new SrsUdpCasterListener(this, SrsListenerMpegTsOverUdp, stream_caster);
1125 1126
        } else if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_RTSP) {
            listener = new SrsRtspListener(this, SrsListenerRtsp, stream_caster);
1127 1128
        } else if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_FLV) {
            listener = new SrsHttpFlvListener(this, SrsListenerFlv, stream_caster);
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
        } else {
            ret = ERROR_STREAM_CASTER_ENGINE;
            srs_error("unsupported stream caster %s. ret=%d", caster.c_str(), ret);
            return ret;
        }
        srs_assert(listener != NULL);

        listeners.push_back(listener);
        
        int port = _srs_config->get_stream_caster_listen(stream_caster);
        if (port <= 0) {
            ret = ERROR_STREAM_CASTER_PORT;
            srs_error("invalid stream caster port %d. ret=%d", port, ret);
            return ret;
        }
winlin authored
1144 1145 1146
        
        // TODO: support listen at <[ip:]port>
        if ((ret = listener->listen("0.0.0.0", port)) != ERROR_SUCCESS) {
1147
            srs_error("StreamCaster listen at port %d failed. ret=%d", port, ret);
1148 1149 1150 1151 1152 1153 1154 1155 1156
            return ret;
        }
    }
#endif
    
    return ret;
}

void SrsServer::close_listeners(SrsListenerType type)
winlin authored
1157
{
1158
    std::vector<SrsListener*>::iterator it;
1159
    for (it = listeners.begin(); it != listeners.end();) {
1160
        SrsListener* listener = *it;
1161
        
1162
        if (listener->listen_type() != type) {
1163 1164 1165 1166
            ++it;
            continue;
        }
        
1167
        srs_freep(listener);
1168
        it = listeners.erase(it);
1169
    }
winlin authored
1170 1171
}
winlin authored
1172
void SrsServer::resample_kbps()
1173
{
1174 1175
    SrsStatistic* stat = SrsStatistic::instance();
    
winlin authored
1176 1177 1178
    // collect delta from all clients.
    for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) {
        SrsConnection* conn = *it;
1179
        
winlin authored
1180 1181
        // add delta of connection to server kbps.,
        // for next sample() of server kbps can get the stat.
1182
        stat->kbps_add_delta(conn);
1183 1184
    }
    
winlin authored
1185 1186 1187
    // TODO: FXME: support all other connections.

    // sample the kbps, get the stat.
1188 1189 1190
    SrsKbps* kbps = stat->kbps_sample();
    
    srs_update_rtmp_server((int)conns.size(), kbps);
1191 1192
}
winlin authored
1193 1194
int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
{
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
    int ret = ERROR_SUCCESS;
    
    int max_connections = _srs_config->get_max_connections();
    if ((int)conns.size() >= max_connections) {
        int fd = st_netfd_fileno(client_stfd);
        
        srs_error("exceed the max connections, drop client: "
            "clients=%d, max=%d, fd=%d", (int)conns.size(), max_connections, fd);
            
        srs_close_stfd(client_stfd);
        
        return ret;
    }
    
    SrsConnection* conn = NULL;
1210 1211
    if (type == SrsListenerRtmpStream) {
        conn = new SrsRtmpConn(this, client_stfd);
1212
    } else if (type == SrsListenerHttpApi) {
1213
#ifdef SRS_AUTO_HTTP_API
1214
        conn = new SrsHttpApi(this, client_stfd, http_api_mux);
winlin authored
1215 1216 1217 1218 1219
#else
        srs_warn("close http client for server not support http-api");
        srs_close_stfd(client_stfd);
        return ret;
#endif
1220
    } else if (type == SrsListenerHttpStream) {
1221
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1222
        conn = new SrsResponseOnlyHttpConn(this, client_stfd, http_server);
1223 1224 1225 1226 1227
#else
        srs_warn("close http client for server not support http-server");
        srs_close_stfd(client_stfd);
        return ret;
#endif
1228
    } else {
1229
        // TODO: FIXME: handler others
1230 1231 1232 1233 1234
    }
    srs_assert(conn);
    
    // directly enqueue, the cycle thread will remove the client.
    conns.push_back(conn);
1235
    srs_verbose("add conn to vector.");
1236 1237
    
    // cycle will start process thread and when finished remove the client.
1238
    // @remark never use the conn, for it maybe destroyed.
1239 1240 1241
    if ((ret = conn->start()) != ERROR_SUCCESS) {
        return ret;
    }
1242
    srs_verbose("conn started success.");
1243 1244

    srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
1245 1246
    
    return ret;
winlin authored
1247 1248 1249 1250
}

int SrsServer::on_reload_listen()
{
1251
    return listen();
winlin authored
1252
}
winlin authored
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262

int SrsServer::on_reload_pid()
{
    if (pid_fd > 0) {
        ::close(pid_fd);
        pid_fd = -1;
    }
    
    return acquire_pid_file();
}
winlin authored
1263
winlin authored
1264 1265 1266 1267
int SrsServer::on_reload_vhost_added(std::string vhost)
{
    int ret = ERROR_SUCCESS;
    
1268
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    if (!_srs_config->get_vhost_http_enabled(vhost)) {
        return ret;
    }
    
    if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
        return ret;
    }
#endif

    return ret;
}
1281
int SrsServer::on_reload_vhost_removed(std::string /*vhost*/)
winlin authored
1282 1283 1284
{
    int ret = ERROR_SUCCESS;
    
1285
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1286 1287 1288 1289 1290 1291 1292 1293
    if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
        return ret;
    }
#endif

    return ret;
}
winlin authored
1294 1295
int SrsServer::on_reload_http_api_enabled()
{
winlin authored
1296 1297
    int ret = ERROR_SUCCESS;
    
1298
#ifdef SRS_AUTO_HTTP_API
winlin authored
1299 1300 1301 1302
    ret = listen_http_api();
#endif
    
    return ret;
winlin authored
1303 1304 1305 1306 1307 1308
}

int SrsServer::on_reload_http_api_disabled()
{
    int ret = ERROR_SUCCESS;
    
1309
#ifdef SRS_AUTO_HTTP_API
winlin authored
1310
    close_listeners(SrsListenerHttpApi);
winlin authored
1311 1312 1313 1314 1315 1316 1317 1318 1319
#endif
    
    return ret;
}

int SrsServer::on_reload_http_stream_enabled()
{
    int ret = ERROR_SUCCESS;
    
1320
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1321 1322
    ret = listen_http_stream();
#endif
1323
    
winlin authored
1324 1325 1326 1327 1328 1329 1330
    return ret;
}

int SrsServer::on_reload_http_stream_disabled()
{
    int ret = ERROR_SUCCESS;
    
1331
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
    close_listeners(SrsListenerHttpStream);
#endif

    return ret;
}

int SrsServer::on_reload_http_stream_updated()
{
    int ret = ERROR_SUCCESS;
    
1342
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1343 1344 1345 1346 1347 1348 1349 1350
    if ((ret = on_reload_http_stream_enabled()) != ERROR_SUCCESS) {
        return ret;
    }
    
    if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
        return ret;
    }
#endif
winlin authored
1351 1352 1353
    
    return ret;
}
1354
1355 1356 1357 1358 1359
int SrsServer::on_publish(SrsSource* s, SrsRequest* r)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
1360
    if ((ret = http_server->http_mount(s, r)) != ERROR_SUCCESS) {
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
        return ret;
    }
#endif
    
    return ret;
}

void SrsServer::on_unpublish(SrsSource* s, SrsRequest* r)
{
#ifdef SRS_AUTO_HTTP_SERVER
1371
    http_server->http_unmount(s, r);
1372 1373 1374
#endif
}
1375 1376 1377 1378 1379
int SrsServer::on_hls_publish(SrsRequest* r)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
1380
    if ((ret = http_server->mount_hls(r)) != ERROR_SUCCESS) {
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
        return ret;
    }
#endif
    
    return ret;
}

int SrsServer::on_update_m3u8(SrsRequest* r, string m3u8)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
1393
    if ((ret = http_server->hls_update_m3u8(r, m3u8)) != ERROR_SUCCESS) {
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
        return ret;
    }
#endif
    
    return ret;
}

int SrsServer::on_update_ts(SrsRequest* r, string uri, string ts)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
1406
    if ((ret = http_server->hls_update_ts(r, uri, ts)) != ERROR_SUCCESS) {
1407 1408 1409 1410 1411 1412 1413
        return ret;
    }
#endif
    
    return ret;
}
1414 1415 1416 1417 1418 1419

int SrsServer::on_remove_ts(SrsRequest* r, string uri)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
1420
    if ((ret = http_server->hls_remove_ts(r, uri)) != ERROR_SUCCESS) {
1421 1422 1423 1424 1425 1426 1427
        return ret;
    }
#endif
    
    return ret;
}
1428 1429 1430 1431 1432
int SrsServer::on_hls_unpublish(SrsRequest* r)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
1433
    http_server->unmount_hls(r);
1434 1435 1436 1437 1438
#endif
    
    return ret;
}