Blame view

trunk/src/app/srs_app_server.cpp 36.3 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
51 52 53
// signal defines.
#define SIGNAL_RELOAD SIGHUP
54
// system interval in ms,
winlin authored
55
// all resolution times should be times togother,
56 57 58
// 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,
59
// for performance refine, @see: https://github.com/simple-rtmp-server/srs/issues/194
60
// @remark, recomment to 1000ms.
61
#define SRS_SYS_CYCLE_INTERVAL 1000
winlin authored
62 63 64

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

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

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

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

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

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

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

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

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

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

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

    return ret;
}

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

SrsHttpFlvListener::~SrsHttpFlvListener()
{
    srs_freep(caster);
    srs_freep(listener);
}
255
int SrsHttpFlvListener::listen(string i, int p)
256 257 258 259 260
{
    int ret = ERROR_SUCCESS;
    
    // the caller already ensure the type is ok,
    // we just assert here for unknown stream caster.
261
    srs_assert(type == SrsListenerFlv);
262
    
263 264
    ip = i;
    port = p;
265
    
266 267 268 269
    if ((ret = caster->initialize()) != ERROR_SUCCESS) {
        return ret;
    }
    
270 271 272 273 274 275 276 277 278 279
    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",
280
             pthread->cid(), _srs_context->get_id(), port, type, fd, ip.c_str(), port);
281
    
282
    srs_trace("%s listen at tcp://%s:%d, fd=%d", srs_listener_type2string(type).c_str(), ip.c_str(), port, listener->fd());
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    
    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;
}
298
#endif
299
300
SrsUdpStreamListener::SrsUdpStreamListener(SrsServer* svr, SrsListenerType t, ISrsUdpHandler* c) : SrsListener(svr, t)
301
{
302
    listener = NULL;
303
    caster = c;
304 305
}
306
SrsUdpStreamListener::~SrsUdpStreamListener()
307
{
308
    srs_freep(listener);
309 310
}
311
int SrsUdpStreamListener::listen(string i, int p)
312 313 314 315 316
{
    int ret = ERROR_SUCCESS;

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

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

    if ((ret = listener->listen()) != ERROR_SUCCESS) {
        srs_error("udp caster listen failed. ret=%d", ret);
327 328 329 330
        return ret;
    }
    
    srs_info("listen thread cid=%d, current_cid=%d, "
331
        "listen at port=%d, type=%d, fd=%d started success, ep=%s:%d",
332
        pthread->cid(), _srs_context->get_id(), port, type, fd, ip.c_str(), port);
333 334 335 336 337 338
    
    // 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;
    }
339
340
    srs_trace("%s listen at udp://%s:%d, fd=%d", srs_listener_type2string(type).c_str(), ip.c_str(), port, listener->fd());
341
342 343
    return ret;
}
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

#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);
}
360
#endif
361
362 363 364 365 366 367 368 369
SrsSignalManager* SrsSignalManager::instance = NULL;

SrsSignalManager::SrsSignalManager(SrsServer* server)
{
    SrsSignalManager::instance = this;
    
    _server = server;
    sig_pipe[0] = sig_pipe[1] = -1;
370
    pthread = new SrsEndlessThread("signal", this);
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
    signal_read_stfd = NULL;
}

SrsSignalManager::~SrsSignalManager()
{
    srs_freep(pthread);
    
    srs_close_stfd(signal_read_stfd);
    
    if (sig_pipe[0] > 0) {
        ::close(sig_pipe[0]);
    }
    if (sig_pipe[1] > 0) {
        ::close(sig_pipe[1]);
    }
}

int SrsSignalManager::initialize()
{
    int ret = ERROR_SUCCESS;
    return ret;
}

int SrsSignalManager::start()
{
    int ret = ERROR_SUCCESS;
    
    /**
    * 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;
    
    /* 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;
    }
    
    /* 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);
    
428 429 430 431 432 433 434
    sa.sa_handler = SrsSignalManager::sig_catcher;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    sigaction(SIGUSR2, &sa, NULL);
    
    srs_trace("signal installed");
    
435 436 437 438 439 440 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
    return pthread->start();
}

int SrsSignalManager::cycle()
{
    int ret = ERROR_SUCCESS;
    
    if (signal_read_stfd == NULL) {
        signal_read_stfd = st_netfd_open(sig_pipe[0]);
    }

    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;
}
471 472 473 474 475 476 477 478
ISrsServerCycle::ISrsServerCycle()
{
}

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

SrsServer::~SrsServer()
{
508 509 510 511 512
    destroy();
}

void SrsServer::destroy()
{
513
    srs_warn("start destroy server");
514
    
515
    _srs_config->unsubscribe(this);
516
    
517 518 519
    close_listeners(SrsListenerRtmpStream);
    close_listeners(SrsListenerHttpApi);
    close_listeners(SrsListenerHttpStream);
520 521 522 523

#ifdef SRS_AUTO_INGEST
    ingester->stop();
#endif
524
    
525
#ifdef SRS_AUTO_HTTP_API
526
    srs_freep(http_api_mux);
winlin authored
527 528
#endif
529
#ifdef SRS_AUTO_HTTP_SERVER
530
    srs_freep(http_stream_mux);
winlin authored
531
#endif
532 533 534 535 536

#ifdef SRS_AUTO_HTTP_PARSER
    srs_freep(http_heartbeat);
#endif
537
#ifdef SRS_AUTO_INGEST
538 539
    srs_freep(ingester);
#endif
540 541 542 543 544 545 546 547
    
    if (pid_fd > 0) {
        ::close(pid_fd);
        pid_fd = -1;
    }
    
    srs_freep(signal_manager);
    
548 549
    srs_freep(handler);
    
winlin authored
550 551
    // @remark never destroy the connections, 
    // for it's still alive.
552
winlin authored
553
    // @remark never destroy the source, 
554 555
    // when we free all sources, the fmle publish may retry
    // and segment fault.
winlin authored
556 557
}
558
int SrsServer::initialize(ISrsServerCycle* cycle_handler)
winlin authored
559
{
560
    int ret = ERROR_SUCCESS;
winlin authored
561
    
562 563 564
    // ensure the time is ok.
    srs_update_system_time_ms();
    
winlin authored
565
    // for the main objects(server, config, log, context),
566 567 568 569 570
    // never subscribe handler in constructor,
    // instead, subscribe handler in initialize method.
    srs_assert(_srs_config);
    _srs_config->subscribe(this);
    
571 572 573
    srs_assert(!signal_manager);
    signal_manager = new SrsSignalManager(this);
    
574 575 576 577 578
    handler = cycle_handler;
    if(handler && (ret = handler->initialize()) != ERROR_SUCCESS){
        return ret;
    }
    
579
#ifdef SRS_AUTO_HTTP_API
580 581 582 583
    if ((ret = http_api_mux->initialize()) != ERROR_SUCCESS) {
        return ret;
    }
#endif
584
585
#ifdef SRS_AUTO_HTTP_SERVER
586 587 588 589
    srs_assert(http_stream_mux);
    if ((ret = http_stream_mux->initialize()) != ERROR_SUCCESS) {
        return ret;
    }
590
#endif
591
592 593 594 595
#ifdef SRS_AUTO_HTTP_PARSER
    srs_assert(!http_heartbeat);
    http_heartbeat = new SrsHttpHeartbeat();
#endif
596
597
#ifdef SRS_AUTO_INGEST
598 599 600
    srs_assert(!ingester);
    ingester = new SrsIngester();
#endif
winlin authored
601
602
    return ret;
winlin authored
603 604
}
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
int SrsServer::initialize_st()
{
    int ret = ERROR_SUCCESS;
    
    // init st
    if ((ret = srs_init_st()) != ERROR_SUCCESS) {
        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;
}
633 634 635 636 637
int SrsServer::initialize_signal()
{
    return signal_manager->initialize();
}
winlin authored
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
int SrsServer::acquire_pid_file()
{
    int ret = ERROR_SUCCESS;
    
    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
657
    struct flock lock;
winlin authored
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 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

    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
709
    pid_fd = fd;
winlin authored
710 711 712 713
    
    return ret;
}
winlin authored
714 715
int SrsServer::listen()
{
716 717
    int ret = ERROR_SUCCESS;
    
718 719
    if ((ret = listen_rtmp()) != ERROR_SUCCESS) {
        return ret;
720
    }
721 722 723
    
    if ((ret = listen_http_api()) != ERROR_SUCCESS) {
        return ret;
724 725
    }
    
726 727
    if ((ret = listen_http_stream()) != ERROR_SUCCESS) {
        return ret;
728 729
    }
    
730 731 732 733
    if ((ret = listen_stream_caster()) != ERROR_SUCCESS) {
        return ret;
    }
    
734
    return ret;
winlin authored
735 736
}
737 738 739 740 741 742
int SrsServer::register_signal()
{
    // start signal process thread.
    return signal_manager->start();
}
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
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
789 790 791 792 793 794 795
    
#ifdef SRS_AUTO_HTTP_SERVER
    // for SRS go-sharp to detect the status of HTTP server of SRS HTTP FLV Cluster.
    if ((ret = http_stream_mux->mux.handle("/api/v1/versions", new SrsGoApiVersion())) != ERROR_SUCCESS) {
        return ret;
    }
#endif
796 797 798 799

    return ret;
}
winlin authored
800
int SrsServer::ingest()
winlin authored
801
{
802 803
    int ret = ERROR_SUCCESS;
    
804
#ifdef SRS_AUTO_INGEST
805 806
    if ((ret = ingester->start()) != ERROR_SUCCESS) {
        srs_error("start ingest streams failed. ret=%d", ret);
807 808
        return ret;
    }
809
#endif
winlin authored
810 811 812 813 814 815 816

    return ret;
}

int SrsServer::cycle()
{
    int ret = ERROR_SUCCESS;
817 818 819

    ret = do_cycle();
820
#ifdef SRS_AUTO_GPERF_MC
821
    destroy();
822
    
winlin authored
823
    // remark, for gmc, never invoke the exit().
824 825 826
    srs_warn("sleep a long time for system st-threads to cleanup.");
    st_usleep(3 * 1000 * 1000);
    srs_warn("system quit");
827 828 829
#else
    srs_warn("main cycle terminated, system quit normally.");
    exit(0);
830 831
#endif
    
832 833 834 835 836 837 838
    return ret;
}

void SrsServer::remove(SrsConnection* conn)
{
    std::vector<SrsConnection*>::iterator it = std::find(conns.begin(), conns.end(), conn);
    
839 840 841 842
    // removed by destroy, ignore.
    if (it == conns.end()) {
        srs_warn("server moved connection, ignore.");
        return;
843 844
    }
    
845 846
    conns.erase(it);
    
847 848
    srs_info("conn removed. conns=%d", (int)conns.size());
    
849 850
    SrsStatistic* stat = SrsStatistic::instance();
    stat->kbps_add_delta(conn);
851
    
852 853 854 855 856 857 858 859 860 861 862 863
    // 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;
    }
    
864
    if (signo == SIGINT || signo == SIGUSR2) {
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
#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");
        exit(0);
#endif
        return;
    }
    
    if (signo == SIGTERM) {
        srs_trace("user terminate program");
        exit(0);
        return;
    }
}

int SrsServer::do_cycle()
{
    int ret = ERROR_SUCCESS;
885
    
winlin authored
886 887
    // find the max loop
    int max = srs_max(0, SRS_SYS_TIME_RESOLUTION_MS_TIMES);
888
    
889
#ifdef SRS_AUTO_STAT
winlin authored
890 891
    max = srs_max(max, SRS_SYS_RUSAGE_RESOLUTION_TIMES);
    max = srs_max(max, SRS_SYS_CPU_STAT_RESOLUTION_TIMES);
892
    max = srs_max(max, SRS_SYS_DISK_STAT_RESOLUTION_TIMES);
winlin authored
893
    max = srs_max(max, SRS_SYS_MEMINFO_RESOLUTION_TIMES);
894
    max = srs_max(max, SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES);
895
    max = srs_max(max, SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES);
896
    max = srs_max(max, SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES);
897
#endif
winlin authored
898
    
899 900
    // the deamon thread, update the time cache
    while (true) {
901 902 903 904 905
        if(handler && (ret = handler->on_cycle(conns.size())) != ERROR_SUCCESS){
            srs_error("cycle handle failed. ret=%d", ret);
            return ret;
        }
            
906
        // the interval in config.
907
        int heartbeat_max_resolution = (int)(_srs_config->get_heartbeat_interval() / SRS_SYS_CYCLE_INTERVAL);
908 909
        
        // dynamic fetch the max.
910 911
        int temp_max = max;
        temp_max = srs_max(temp_max, heartbeat_max_resolution);
912
        
913
        for (int i = 0; i < temp_max; i++) {
winlin authored
914
            st_usleep(SRS_SYS_CYCLE_INTERVAL * 1000);
915
        
916 917 918
// for gperf heap checker,
// @see: research/gperftools/heap-checker/heap_checker.cc
// if user interrupt the program, exit to check mem leak.
919 920
// but, if gperf, use reload to ensure main return normally,
// because directly exit will cause core-dump.
921
#ifdef SRS_AUTO_GPERF_MC
winlin authored
922
            if (signal_gmc_stop) {
923
                srs_warn("gmc got singal to stop server.");
924
                return ret;
winlin authored
925
            }
926
#endif
927
        
winlin authored
928 929 930 931 932 933 934 935 936 937
            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.");
            }
938
            
939
            // update the cache time
winlin authored
940
            if ((i % SRS_SYS_TIME_RESOLUTION_MS_TIMES) == 0) {
941
                srs_info("update current time cache.");
winlin authored
942 943
                srs_update_system_time_ms();
            }
944
            
945
#ifdef SRS_AUTO_STAT
winlin authored
946
            if ((i % SRS_SYS_RUSAGE_RESOLUTION_TIMES) == 0) {
947
                srs_info("update resource info, rss.");
winlin authored
948 949
                srs_update_system_rusage();
            }
winlin authored
950
            if ((i % SRS_SYS_CPU_STAT_RESOLUTION_TIMES) == 0) {
951
                srs_info("update cpu info, cpu usage.");
952
                srs_update_proc_stat();
953
            }
954 955 956 957
            if ((i % SRS_SYS_DISK_STAT_RESOLUTION_TIMES) == 0) {
                srs_info("update disk info, disk iops.");
                srs_update_disk_stat();
            }
winlin authored
958
            if ((i % SRS_SYS_MEMINFO_RESOLUTION_TIMES) == 0) {
959
                srs_info("update memory info, usage/free.");
winlin authored
960 961
                srs_update_meminfo();
            }
962
            if ((i % SRS_SYS_PLATFORM_INFO_RESOLUTION_TIMES) == 0) {
963
                srs_info("update platform info, uptime/load.");
964 965
                srs_update_platform_info();
            }
966
            if ((i % SRS_SYS_NETWORK_DEVICE_RESOLUTION_TIMES) == 0) {
967
                srs_info("update network devices info.");
968 969
                srs_update_network_devices();
            }
970
            if ((i % SRS_SYS_NETWORK_RTMP_SERVER_RESOLUTION_TIMES) == 0) {
winlin authored
971 972
                srs_info("update network server kbps info.");
                resample_kbps();
973
            }
974
    #ifdef SRS_AUTO_HTTP_PARSER
975 976
            if (_srs_config->get_heartbeat_enabled()) {
                if ((i % heartbeat_max_resolution) == 0) {
977
                    srs_info("do http heartbeat, for internal server to report.");
978 979 980
                    http_heartbeat->heartbeat();
                }
            }
981
    #endif
982
#endif
983
            
984
            srs_info("server main thread loop");
985 986
        }
    }
987
988
    return ret;
winlin authored
989 990
}
991 992 993 994 995
int SrsServer::listen_rtmp()
{
    int ret = ERROR_SUCCESS;
    
    // stream service port.
996 997
    std::vector<std::string> ip_ports = _srs_config->get_listens();
    srs_assert((int)ip_ports.size() > 0);
998 999 1000
    
    close_listeners(SrsListenerRtmpStream);
    
1001
    for (int i = 0; i < (int)ip_ports.size(); i++) {
1002
        SrsListener* listener = new SrsStreamListener(this, SrsListenerRtmpStream);
1003 1004
        listeners.push_back(listener);
        
1005 1006 1007 1008 1009 1010
        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);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
            return ret;
        }
    }
    
    return ret;
}

int SrsServer::listen_http_api()
{
    int ret = ERROR_SUCCESS;
    
1022
#ifdef SRS_AUTO_HTTP_API
1023 1024
    close_listeners(SrsListenerHttpApi);
    if (_srs_config->get_http_api_enabled()) {
1025
        SrsListener* listener = new SrsStreamListener(this, SrsListenerHttpApi);
1026 1027
        listeners.push_back(listener);
        
1028 1029 1030 1031 1032 1033 1034 1035
        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);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
            return ret;
        }
    }
#endif
    
    return ret;
}

int SrsServer::listen_http_stream()
{
    int ret = ERROR_SUCCESS;
    
1048
#ifdef SRS_AUTO_HTTP_SERVER
1049 1050
    close_listeners(SrsListenerHttpStream);
    if (_srs_config->get_http_stream_enabled()) {
1051
        SrsListener* listener = new SrsStreamListener(this, SrsListenerHttpStream);
1052 1053
        listeners.push_back(listener);
        
1054 1055 1056 1057 1058 1059 1060 1061
        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);
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
            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;
        }
1086
        SrsListener* listener = NULL;
1087 1088 1089

        std::string caster = _srs_config->get_stream_caster_engine(stream_caster);
        if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_MPEGTS_OVER_UDP) {
1090
            listener = new SrsUdpCasterListener(this, SrsListenerMpegTsOverUdp, stream_caster);
1091 1092
        } else if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_RTSP) {
            listener = new SrsRtspListener(this, SrsListenerRtsp, stream_caster);
1093 1094
        } else if (caster == SRS_CONF_DEFAULT_STREAM_CASTER_FLV) {
            listener = new SrsHttpFlvListener(this, SrsListenerFlv, stream_caster);
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
        } 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
1110 1111 1112
        
        // TODO: support listen at <[ip:]port>
        if ((ret = listener->listen("0.0.0.0", port)) != ERROR_SUCCESS) {
1113
            srs_error("StreamCaster listen at port %d failed. ret=%d", port, ret);
1114 1115 1116 1117 1118 1119 1120 1121 1122
            return ret;
        }
    }
#endif
    
    return ret;
}

void SrsServer::close_listeners(SrsListenerType type)
winlin authored
1123
{
1124
    std::vector<SrsListener*>::iterator it;
1125
    for (it = listeners.begin(); it != listeners.end();) {
1126
        SrsListener* listener = *it;
1127
        
1128
        if (listener->listen_type() != type) {
1129 1130 1131 1132
            ++it;
            continue;
        }
        
1133
        srs_freep(listener);
1134
        it = listeners.erase(it);
1135
    }
winlin authored
1136 1137
}
winlin authored
1138
void SrsServer::resample_kbps()
1139
{
1140 1141
    SrsStatistic* stat = SrsStatistic::instance();
    
winlin authored
1142 1143 1144
    // collect delta from all clients.
    for (std::vector<SrsConnection*>::iterator it = conns.begin(); it != conns.end(); ++it) {
        SrsConnection* conn = *it;
1145
        
winlin authored
1146 1147
        // add delta of connection to server kbps.,
        // for next sample() of server kbps can get the stat.
1148
        stat->kbps_add_delta(conn);
1149 1150
    }
    
winlin authored
1151 1152 1153
    // TODO: FXME: support all other connections.

    // sample the kbps, get the stat.
1154 1155 1156
    SrsKbps* kbps = stat->kbps_sample();
    
    srs_update_rtmp_server((int)conns.size(), kbps);
1157 1158
}
winlin authored
1159 1160
int SrsServer::accept_client(SrsListenerType type, st_netfd_t client_stfd)
{
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
    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;
1176 1177
    if (type == SrsListenerRtmpStream) {
        conn = new SrsRtmpConn(this, client_stfd);
1178
    } else if (type == SrsListenerHttpApi) {
1179
#ifdef SRS_AUTO_HTTP_API
1180
        conn = new SrsHttpApi(this, client_stfd, http_api_mux);
winlin authored
1181 1182 1183 1184 1185
#else
        srs_warn("close http client for server not support http-api");
        srs_close_stfd(client_stfd);
        return ret;
#endif
1186
    } else if (type == SrsListenerHttpStream) {
1187
#ifdef SRS_AUTO_HTTP_SERVER
1188
        conn = new SrsStaticHttpConn(this, client_stfd, &http_stream_mux->mux);
1189 1190 1191 1192 1193
#else
        srs_warn("close http client for server not support http-server");
        srs_close_stfd(client_stfd);
        return ret;
#endif
1194
    } else {
1195
        // TODO: FIXME: handler others
1196 1197 1198 1199 1200
    }
    srs_assert(conn);
    
    // directly enqueue, the cycle thread will remove the client.
    conns.push_back(conn);
1201
    srs_verbose("add conn to vector.");
1202 1203
    
    // cycle will start process thread and when finished remove the client.
1204
    // @remark never use the conn, for it maybe destroyed.
1205 1206 1207
    if ((ret = conn->start()) != ERROR_SUCCESS) {
        return ret;
    }
1208
    srs_verbose("conn started success.");
1209 1210

    srs_verbose("accept client finished. conns=%d, ret=%d", (int)conns.size(), ret);
1211 1212
    
    return ret;
winlin authored
1213 1214 1215 1216
}

int SrsServer::on_reload_listen()
{
1217
    return listen();
winlin authored
1218
}
winlin authored
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228

int SrsServer::on_reload_pid()
{
    if (pid_fd > 0) {
        ::close(pid_fd);
        pid_fd = -1;
    }
    
    return acquire_pid_file();
}
winlin authored
1229
winlin authored
1230 1231 1232 1233
int SrsServer::on_reload_vhost_added(std::string vhost)
{
    int ret = ERROR_SUCCESS;
    
1234
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    if (!_srs_config->get_vhost_http_enabled(vhost)) {
        return ret;
    }
    
    if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
        return ret;
    }
#endif

    return ret;
}
1247
int SrsServer::on_reload_vhost_removed(std::string /*vhost*/)
winlin authored
1248 1249 1250
{
    int ret = ERROR_SUCCESS;
    
1251
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1252 1253 1254 1255 1256 1257 1258 1259
    if ((ret = on_reload_vhost_http_updated()) != ERROR_SUCCESS) {
        return ret;
    }
#endif

    return ret;
}
winlin authored
1260 1261
int SrsServer::on_reload_http_api_enabled()
{
winlin authored
1262 1263
    int ret = ERROR_SUCCESS;
    
1264
#ifdef SRS_AUTO_HTTP_API
winlin authored
1265 1266 1267 1268
    ret = listen_http_api();
#endif
    
    return ret;
winlin authored
1269 1270 1271 1272 1273 1274
}

int SrsServer::on_reload_http_api_disabled()
{
    int ret = ERROR_SUCCESS;
    
1275
#ifdef SRS_AUTO_HTTP_API
winlin authored
1276
    close_listeners(SrsListenerHttpApi);
winlin authored
1277 1278 1279 1280 1281 1282 1283 1284 1285
#endif
    
    return ret;
}

int SrsServer::on_reload_http_stream_enabled()
{
    int ret = ERROR_SUCCESS;
    
1286
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1287 1288
    ret = listen_http_stream();
#endif
1289
    
winlin authored
1290 1291 1292 1293 1294 1295 1296
    return ret;
}

int SrsServer::on_reload_http_stream_disabled()
{
    int ret = ERROR_SUCCESS;
    
1297
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    close_listeners(SrsListenerHttpStream);
#endif

    return ret;
}

int SrsServer::on_reload_http_stream_updated()
{
    int ret = ERROR_SUCCESS;
    
1308
#ifdef SRS_AUTO_HTTP_SERVER
winlin authored
1309 1310 1311 1312 1313 1314 1315 1316
    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
1317 1318 1319
    
    return ret;
}
1320
1321 1322 1323 1324 1325
int SrsServer::on_publish(SrsSource* s, SrsRequest* r)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
1326
    if ((ret = http_stream_mux->http_mount(s, r)) != ERROR_SUCCESS) {
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
        return ret;
    }
#endif
    
    return ret;
}

void SrsServer::on_unpublish(SrsSource* s, SrsRequest* r)
{
#ifdef SRS_AUTO_HTTP_SERVER
1337
    http_stream_mux->http_unmount(s, r);
1338 1339 1340
#endif
}
1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
int SrsServer::on_hls_publish(SrsRequest* r)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
    if ((ret = http_stream_mux->mount_hls(r)) != ERROR_SUCCESS) {
        return ret;
    }
#endif
    
    return ret;
}

int SrsServer::on_update_m3u8(SrsRequest* r, string m3u8)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
    if ((ret = http_stream_mux->hls_update_m3u8(r, m3u8)) != ERROR_SUCCESS) {
        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
    if ((ret = http_stream_mux->hls_update_ts(r, uri, ts)) != ERROR_SUCCESS) {
        return ret;
    }
#endif
    
    return ret;
}

int SrsServer::on_hls_unpublish(SrsRequest* r)
{
    int ret = ERROR_SUCCESS;
    
#ifdef SRS_AUTO_HTTP_SERVER
    http_stream_mux->unmount_hls(r);
#endif
    
    return ret;
}