Blame view

trunk/src/main/srs_main_server.cpp 7.8 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
#include <srs_kernel_error.hpp>
#include <srs_app_server.hpp>
#include <srs_app_config.hpp>
#include <srs_app_log.hpp>
41
#include <srs_kernel_utility.hpp>
42
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();
57 58 59 60
/**
* show the features by macro, the actual macro values.
*/
void show_macro_features()
61 62
{
#ifdef SRS_AUTO_SSL
63
    srs_trace("check feature rtmp handshake: on");
64
#else
65
    srs_warn("check feature rtmp handshake: off");
66 67 68
#endif

#ifdef SRS_AUTO_HLS
69
    srs_trace("check feature hls: on");
70
#else
71
    srs_warn("check feature hls: off");
72 73 74
#endif

#ifdef SRS_AUTO_HTTP_CALLBACK
75
    srs_trace("check feature http callback: on");
76
#else
77
    srs_warn("check feature http callback: off");
78 79 80
#endif

#ifdef SRS_AUTO_HTTP_API
81
    srs_trace("check feature http api: on");
82
#else
83
    srs_warn("check feature http api: off");
84 85 86
#endif

#ifdef SRS_AUTO_HTTP_SERVER
87
    srs_trace("check feature http server: on");
88
#else
89
    srs_warn("check feature http server: off");
90 91 92
#endif

#ifdef SRS_AUTO_HTTP_PARSER
93
    srs_trace("check feature http parser: on");
94
#else
95
    srs_warn("check feature http parser: off");
96 97 98
#endif

#ifdef SRS_AUTO_DVR
99
    srs_trace("check feature dvr: on");
100
#else
101
    srs_warn("check feature dvr: off");
102 103 104
#endif

#ifdef SRS_AUTO_TRANSCODE
105
    srs_trace("check feature transcode: on");
106
#else
107
    srs_warn("check feature transcode: off");
108 109 110
#endif

#ifdef SRS_AUTO_INGEST
111
    srs_trace("check feature ingest: on");
112
#else
113
    srs_warn("check feature ingest: off");
114 115 116
#endif

#ifdef SRS_AUTO_STAT
117
    srs_trace("check feature system stat: on");
118
#else
119
    srs_warn("check feature system stat: off");
120 121 122
#endif

#ifdef SRS_AUTO_NGINX
123
    srs_trace("check feature compile nginx: on");
124
#else
125
    srs_warn("check feature compile nginx: off");
126 127
#endif
128
#ifdef SRS_AUTO_FFMPEG_TOOL
129
    srs_trace("check feature compile ffmpeg: on");
130
#else
131 132 133 134 135 136 137 138 139 140 141 142
    srs_warn("check feature compile ffmpeg: off");
#endif
}

void check_macro_features()
{
    // for special features.
#ifdef SRS_AUTO_HTTP_SERVER
    srs_warn("http server is dev feature, @see %s", RTMP_SIG_SRS_HTTP_SERVER);
#endif

#if VERSION_MAJOR > 1
143
    #warning "using develop SRS, please use release instead."
144
    srs_warn("SRS %s is develop branch, please use %s instead", RTMP_SIG_SRS_VERSION, RTMP_SIG_SRS_RELEASE);
145 146 147
#endif
}
148 149 150
/**
* main entrance.
*/
151
int main(int argc, char** argv) 
winlin authored
152
{
153
    int ret = ERROR_SUCCESS;
154
155 156 157
    // TODO: support both little and big endian.
    srs_assert(srs_is_little_endian());
158 159
    // for gperf gmp or gcp, 
    // should never enable it when not enabled for performance issue.
160
#ifdef SRS_AUTO_GPERF_MP
161 162
    HeapProfilerStart("gperf.srs.gmp");
#endif
163
#ifdef SRS_AUTO_GPERF_CP
164 165 166
    ProfilerStart("gperf.srs.gcp");
#endif
167
    // directly compile error when these two macro defines.
168
#if defined(SRS_AUTO_GPERF_MC) && defined(SRS_AUTO_GPERF_MP)
169
    #error ("option --with-gmc confict with --with-gmp, "
170 171 172 173
        "@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");
#endif
winlin authored
174
    
175 176 177
    // 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
178 179 180
        return ret;
    }
    
181 182
    // config parsed, initialize log.
    if ((ret = _srs_log->initialize()) != ERROR_SUCCESS) {
183 184
        return ret;
    }
185 186 187 188 189

    // we check the config when the log initialized.
    if ((ret = _srs_config->check_config()) != ERROR_SUCCESS) {
        return ret;
    }
winlin authored
190
    
191
    srs_trace("srs(simple-rtmp-server) "RTMP_SIG_SRS_VERSION);
winlin authored
192
    srs_trace("license: "RTMP_SIG_SRS_LICENSE);
193 194
    srs_trace("primary: "RTMP_SIG_SRS_PRIMARY);
    srs_trace("authors: "RTMP_SIG_SRS_AUTHROS);
195
    srs_trace("contributors: "SRS_AUTO_CONSTRIBUTORS);
196 197
    srs_trace("uname: "SRS_AUTO_UNAME);
    srs_trace("build: %s, %s", SRS_AUTO_BUILD_DATE, srs_is_little_endian()? "little-endian":"big-endian");
198 199
    srs_trace("configure: "SRS_AUTO_USER_CONFIGURE);
    srs_trace("features: "SRS_AUTO_CONFIGURE);
200
#ifdef SRS_AUTO_ARM_UBUNTU12
winlin authored
201
    srs_trace("arm tool chain: "SRS_AUTO_EMBEDED_TOOL_CHAIN);
202
#endif
203
    srs_trace("conf: %s, limit: %d", _srs_config->config().c_str(), _srs_config->get_max_connections());
204
    
205
    // features
206
    show_macro_features();
207
    check_macro_features();
208
    
winlin authored
209 210 211 212 213
    /**
    * we do nothing in the constructor of server,
    * and use initialize to create members, set hooks for instance the reload handler,
    * all initialize will done in this stage.
    */
214
    if ((ret = _srs_server->initialize()) != ERROR_SUCCESS) {
215 216 217
        return ret;
    }
    
218
    return run();
winlin authored
219
}
winlin authored
220
221
int run()
winlin authored
222 223 224 225 226 227 228 229 230 231
{
    // if not deamon, directly run master.
    if (!_srs_config->get_deamon()) {
        return run_master();
    }
    
    srs_trace("start deamon mode...");
    
    int pid = fork();
    
232
    if(pid < 0) {
winlin authored
233 234 235 236 237
        srs_error("create process error. ret=-1"); //ret=0
        return -1;
    }

    // grandpa
238
    if(pid > 0) {
winlin authored
239
        int status = 0;
240
        if(waitpid(pid, &status, 0) == -1) {
winlin authored
241 242 243 244 245 246 247 248 249
            srs_error("wait child process error! ret=-1"); //ret=0
        }
        srs_trace("grandpa process exit.");
        exit(0);
    }

    // father
    pid = fork();
    
250 251
    if(pid < 0) {
        srs_error("create process error. ret=0");
winlin authored
252 253 254
        return -1;
    }
255
    if(pid > 0) {
256
        srs_trace("father process exit. ret=0");
winlin authored
257 258 259 260 261 262 263 264 265
        exit(0);
    }

    // son
    srs_trace("son(deamon) process running.");
    
    return run_master();
}
266
int run_master()
winlin authored
267 268 269
{
    int ret = ERROR_SUCCESS;
    
270 271 272
    if ((ret = _srs_server->initialize_signal()) != ERROR_SUCCESS) {
        return ret;
    }
winlin authored
273
    
274
    if ((ret = _srs_server->acquire_pid_file()) != ERROR_SUCCESS) {
winlin authored
275 276
        return ret;
    }
277
    
278 279 280
    if ((ret = _srs_server->initialize_st()) != ERROR_SUCCESS) {
        return ret;
    }
281
    
282 283 284
    if ((ret = _srs_server->listen()) != ERROR_SUCCESS) {
        return ret;
    }
285
    
286 287 288 289
    if ((ret = _srs_server->register_signal()) != ERROR_SUCCESS) {
        return ret;
    }
    
290
    if ((ret = _srs_server->ingest()) != ERROR_SUCCESS) {
291 292 293
        return ret;
    }
    
294 295 296 297 298
    if ((ret = _srs_server->cycle()) != ERROR_SUCCESS) {
        return ret;
    }
    
    return 0;
winlin authored
299
}
300