Blame view

trunk/src/main/srs_main_server.cpp 5.0 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
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_core.hpp>
winlin authored
25 26

#include <stdlib.h>
winlin authored
27 28 29
#include <sys/types.h>
#include <sys/wait.h>
30
#ifdef SRS_AUTO_GPERF_MP
31
    #include <gperftools/heap-profiler.h>
32
#endif
33
#ifdef SRS_AUTO_GPERF_CP
34
    #include <gperftools/profiler.h>
35 36
#endif
37 38 39 40 41 42
#include <srs_kernel_error.hpp>
#include <srs_app_server.hpp>
#include <srs_app_config.hpp>
#include <srs_app_log.hpp>
#include <srs_app_utility.hpp>
winlin authored
43 44 45 46 47 48 49
// pre-declare
int run();
int run_master();

// for the main objects(server, config, log, context),
// never subscribe handler in constructor,
// instead, subscribe handler in initialize method.
50 51 52 53 54 55 56
// kernel module.
ISrsLog* _srs_log = new SrsFastLog();
ISrsThreadContext* _srs_context = new SrsThreadContext();
// app module.
SrsConfig* _srs_config = new SrsConfig();
SrsServer* _srs_server = new SrsServer();
winlin authored
57
// main entrance.
58
int main(int argc, char** argv) 
winlin authored
59
{
60
    int ret = ERROR_SUCCESS;
61
    
62 63 64
    // TODO: support both little and big endian.
    srs_assert(srs_is_little_endian());
65
#ifdef SRS_AUTO_GPERF_MP
66 67
    HeapProfilerStart("gperf.srs.gmp");
#endif
68
#ifdef SRS_AUTO_GPERF_CP
69 70 71
    ProfilerStart("gperf.srs.gcp");
#endif
72 73
#ifdef SRS_AUTO_GPERF_MC
    #ifdef SRS_AUTO_GPERF_MP
74 75 76 77 78 79 80
    srs_error("option --with-gmc confict with --with-gmp, "
        "@see: http://google-perftools.googlecode.com/svn/trunk/doc/heap_checker.html\n"
        "Note that since the heap-checker uses the heap-profiling framework internally, "
        "it is not possible to run both the heap-checker and heap profiler at the same time");
    return -1;
    #endif
#endif
winlin authored
81
    
82 83 84
    // never use srs log(srs_trace, srs_error, etc) before config parse the option,
    // which will load the log config and apply it.
    if ((ret = _srs_config->parse_options(argc, argv)) != ERROR_SUCCESS) {
winlin authored
85 86 87
        return ret;
    }
    
88 89
    // config parsed, initialize log.
    if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) {
90 91
        return ret;
    }
winlin authored
92
    
93
    srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
94 95
    srs_trace("uname: "SRS_AUTO_UNAME);
    srs_trace("build: %s, %s", SRS_AUTO_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
96 97
    srs_trace("configure: "SRS_AUTO_USER_CONFIGURE);
    srs_trace("features: "SRS_AUTO_CONFIGURE);
98 99
#ifdef SRS_AUTO_ARM_UBUNTU12
    srs_trace("arm tool chain: "SRS_AUTO_ARM_TOOL_CHAIN);
100
#endif
101
    
102
    if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {
103 104 105
        return ret;
    }
    
106
    return run();
winlin authored
107
}
winlin authored
108
109
int run()
winlin authored
110 111 112 113 114 115 116 117 118 119
{
    // if not deamon, directly run master.
    if (!_srs_config->get_deamon()) {
        return run_master();
    }
    
    srs_trace("start deamon mode...");
    
    int pid = fork();
    
120
    if(pid < 0) {
winlin authored
121 122 123 124 125
        srs_error("create process error. ret=-1"); //ret=0
        return -1;
    }

    // grandpa
126
    if(pid > 0) {
winlin authored
127
        int status = 0;
128
        if(waitpid(pid, &status, 0) == -1) {
winlin authored
129 130 131 132 133 134 135 136 137 138
            srs_error("wait child process error! ret=-1"); //ret=0
        }
        srs_trace("grandpa process exit.");
        exit(0);
        return 0;
    }

    // father
    pid = fork();
    
139 140
    if(pid < 0) {
        srs_error("create process error. ret=0");
winlin authored
141 142 143
        return -1;
    }
144
    if(pid > 0) {
145
        srs_trace("father process exit. ret=0");
winlin authored
146 147 148 149 150 151 152 153 154 155
        exit(0);
        return 0;
    }

    // son
    srs_trace("son(deamon) process running.");
    
    return run_master();
}
156
int run_master()
winlin authored
157 158 159
{
    int ret = ERROR_SUCCESS;
    
160 161 162
    if ((ret = _srs_server->initialize_signal()) != ERROR_SUCCESS) {
        return ret;
    }
winlin authored
163
    
164
    if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
winlin authored
165 166
        return ret;
    }
167
    
168 169 170
    if ((ret = _srs_server->initialize_st()) != ERROR_SUCCESS) {
        return ret;
    }
171
    
172 173 174
    if ((ret = _srs_server->listen()) != ERROR_SUCCESS) {
        return ret;
    }
175
    
176 177 178 179
    if ((ret = _srs_server->register_signal()) != ERROR_SUCCESS) {
        return ret;
    }
    
180
    if ((ret = _srs_server->ingest()) != ERROR_SUCCESS) {
181 182 183
        return ret;
    }
    
184 185 186 187 188
    if ((ret = _srs_server->cycle()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return 0;
winlin authored
189
}