Blame view

trunk/src/app/srs_app_ingest.cpp 13.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 24 25

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.
*/

#include <srs_app_ingest.hpp>
26
#ifdef SRS_AUTO_INGEST
winlin authored
27
28 29
using namespace std;
winlin authored
30
#include <srs_kernel_error.hpp>
winlin authored
31 32 33
#include <srs_app_config.hpp>
#include <srs_kernel_log.hpp>
#include <srs_app_ffmpeg.hpp>
34
#include <srs_app_pithy_print.hpp>
35
#include <srs_kernel_utility.hpp>
36
#include <srs_app_utility.hpp>
winlin authored
37 38

// when error, ingester sleep for a while and retry.
39
// ingest never sleep a long time, for we must start the stream ASAP.
40
#define SRS_AUTO_INGESTER_SLEEP_US (int64_t)(3*1000*1000LL)
winlin authored
41
42 43 44 45 46 47 48 49 50 51 52 53
SrsIngesterFFMPEG::SrsIngesterFFMPEG(SrsFFMPEG* _ffmpeg, string _vhost, string _id)
{
    ffmpeg = _ffmpeg;
    vhost = _vhost;
    id = _id;
}

SrsIngesterFFMPEG::~SrsIngesterFFMPEG()
{
    srs_freep(ffmpeg);
}
winlin authored
54 55
SrsIngester::SrsIngester()
{
56 57
    _srs_config->subscribe(this);
    
58
    pthread = new SrsThread("ingest", this, SRS_AUTO_INGESTER_SLEEP_US, true);
59
    pprint = SrsPithyPrint::create_ingester();
winlin authored
60 61 62 63
}

SrsIngester::~SrsIngester()
{
64 65
    _srs_config->unsubscribe(this);
    
winlin authored
66
    srs_freep(pthread);
winlin authored
67
    clear_engines();
winlin authored
68 69
}
70 71 72
int SrsIngester::start()
{
    int ret = ERROR_SUCCESS;
winlin authored
73
    
74 75 76 77
    if ((ret = parse()) != ERROR_SUCCESS) {
        clear_engines();
        ret = ERROR_SUCCESS;
        return ret;
winlin authored
78 79
    }
    
80 81
    // even no ingesters, we must also start it,
    // for the reload may add more ingesters.
82 83 84 85 86 87
    
    // start thread to run all encoding engines.
    if ((ret = pthread->start()) != ERROR_SUCCESS) {
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
winlin authored
88
    srs_trace("ingest thread cid=%d, current_cid=%d", pthread->cid(), _srs_context->get_id());
89
    
winlin authored
90 91 92 93 94 95 96
    return ret;
}

int SrsIngester::parse_ingesters(SrsConfDirective* vhost)
{
    int ret = ERROR_SUCCESS;
    
winlin authored
97
    std::vector<SrsConfDirective*> ingesters = _srs_config->get_ingesters(vhost->arg0());
winlin authored
98 99 100 101
    
    // create engine
    for (int i = 0; i < (int)ingesters.size(); i++) {
        SrsConfDirective* ingest = ingesters[i];
102 103
        if ((ret = parse_engines(vhost, ingest)) != ERROR_SUCCESS) {
            return ret;
winlin authored
104
        }
105
    }
winlin authored
106
    
107 108 109 110 111 112
    return ret;
}

int SrsIngester::parse_engines(SrsConfDirective* vhost, SrsConfDirective* ingest)
{
    int ret = ERROR_SUCCESS;
113 114 115 116 117

    if (!_srs_config->get_ingest_enabled(ingest)) {
        return ret;
    }
    
118 119 120 121 122 123
    std::string ffmpeg_bin = _srs_config->get_ingest_ffmpeg(ingest);
    if (ffmpeg_bin.empty()) {
        ret = ERROR_ENCODER_PARSE;
        srs_trace("empty ffmpeg ret=%d", ret);
        return ret;
    }
winlin authored
124
    
125
    // get all engines.
126
    std::vector<SrsConfDirective*> engines = _srs_config->get_transcode_engines(ingest);
127 128
    if (engines.empty()) {
        SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
129
        if ((ret = initialize_ffmpeg(ffmpeg, vhost, ingest, NULL)) != ERROR_SUCCESS) {
130 131 132
            srs_freep(ffmpeg);
            if (ret != ERROR_ENCODER_LOOP) {
                srs_error("invalid ingest engine. ret=%d", ret);
winlin authored
133
            }
134 135 136
            return ret;
        }
137 138
        SrsIngesterFFMPEG* ingester = new SrsIngesterFFMPEG(ffmpeg, vhost->arg0(), ingest->arg0());
        ingesters.push_back(ingester);
139 140 141 142 143 144 145
        return ret;
    }

    // create engine
    for (int i = 0; i < (int)engines.size(); i++) {
        SrsConfDirective* engine = engines[i];
        SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
146
        if ((ret = initialize_ffmpeg(ffmpeg, vhost, ingest, engine)) != ERROR_SUCCESS) {
147 148
            srs_freep(ffmpeg);
            if (ret != ERROR_ENCODER_LOOP) {
winlin authored
149 150
                srs_error("invalid ingest engine: %s %s, ret=%d", 
                    ingest->arg0().c_str(), engine->arg0().c_str(), ret);
151 152
            }
            return ret;
winlin authored
153
        }
154
155 156
        SrsIngesterFFMPEG* ingester = new SrsIngesterFFMPEG(ffmpeg, vhost->arg0(), ingest->arg0());
        ingesters.push_back(ingester);
winlin authored
157 158
    }
    
159 160 161 162 163
    return ret;
}

void SrsIngester::stop()
{
164 165
    pthread->stop();
    clear_engines();
166 167
}
winlin authored
168 169 170
int SrsIngester::cycle()
{
    int ret = ERROR_SUCCESS;
171
    
172 173 174
    std::vector<SrsIngesterFFMPEG*>::iterator it;
    for (it = ingesters.begin(); it != ingesters.end(); ++it) {
        SrsIngesterFFMPEG* ingester = *it;
175 176
        
        // start all ffmpegs.
177
        if ((ret = ingester->ffmpeg->start()) != ERROR_SUCCESS) {
178 179 180 181 182
            srs_error("ingest ffmpeg start failed. ret=%d", ret);
            return ret;
        }

        // check ffmpeg status.
183
        if ((ret = ingester->ffmpeg->cycle()) != ERROR_SUCCESS) {
184 185 186 187 188 189
            srs_error("ingest ffmpeg cycle failed. ret=%d", ret);
            return ret;
        }
    }

    // pithy print
190
    show_ingest_log_message();
191
    
winlin authored
192 193 194 195 196 197 198
    return ret;
}

void SrsIngester::on_thread_stop()
{
}
winlin authored
199 200
void SrsIngester::clear_engines()
{
201
    std::vector<SrsIngesterFFMPEG*>::iterator it;
winlin authored
202
    
203 204 205
    for (it = ingesters.begin(); it != ingesters.end(); ++it) {
        SrsIngesterFFMPEG* ingester = *it;
        srs_freep(ingester);
winlin authored
206 207
    }
208
    ingesters.clear();
winlin authored
209 210
}
211 212 213 214 215
int SrsIngester::parse()
{
    int ret = ERROR_SUCCESS;
    
    // parse ingesters
216
    std::vector<SrsConfDirective*> vhosts = _srs_config->get_vhosts();
217 218 219 220 221 222 223 224 225 226 227
    
    for (int i = 0; i < (int)vhosts.size(); i++) {
        SrsConfDirective* vhost = vhosts[i];
        if ((ret = parse_ingesters(vhost)) != ERROR_SUCCESS) {
            return ret;
        }
    }
    
    return ret;
}
228
int SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective* vhost, SrsConfDirective* ingest, SrsConfDirective* engine)
winlin authored
229 230 231
{
    int ret = ERROR_SUCCESS;
    
232 233 234 235 236 237 238 239 240
    std::string port;
    if (true) {
        std::vector<std::string> ip_ports = _srs_config->get_listens();
        srs_assert(ip_ports.size() > 0);
        
        std::string ep = ip_ports[0];
        std::string ip;
        srs_parse_endpoint(ep, ip, port);
    }
241 242 243
    
    std::string output = _srs_config->get_engine_output(engine);
    // output stream, to other/self server
winlin authored
244
    // ie. rtmp://localhost:1935/live/livestream_sd
245 246 247 248
    output = srs_string_replace(output, "[vhost]", vhost->arg0());
    output = srs_string_replace(output, "[port]", port);
    if (output.empty()) {
        ret = ERROR_ENCODER_NO_OUTPUT;
winlin authored
249
        srs_trace("empty output url, ingest=%s. ret=%d", ingest->arg0().c_str(), ret);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
        return ret;
    }
    
    // find the app and stream in rtmp url
    std::string url = output;
    std::string app, stream;
    size_t pos = std::string::npos;
    if ((pos = url.rfind("/")) != std::string::npos) {
        stream = url.substr(pos + 1);
        url = url.substr(0, pos);
    }
    if ((pos = url.rfind("/")) != std::string::npos) {
        app = url.substr(pos + 1);
        url = url.substr(0, pos);
    }
    if ((pos = app.rfind("?")) != std::string::npos) {
        app = app.substr(0, pos);
    }
    
winlin authored
269
    std::string log_file = SRS_CONSTS_NULL_FILE; // disabled
270
    // write ffmpeg info to log file.
271 272 273 274 275 276 277 278 279 280 281 282
    if (_srs_config->get_ffmpeg_log_enabled()) {
        log_file = _srs_config->get_ffmpeg_log_dir();
        log_file += "/";
        log_file += "ffmpeg-ingest";
        log_file += "-";
        log_file += vhost->arg0();
        log_file += "-";
        log_file += app;
        log_file += "-";
        log_file += stream;
        log_file += ".log";
    }
283 284 285 286
    
    // input
    std::string input_type = _srs_config->get_ingest_input_type(ingest);
    if (input_type.empty()) {
287
        ret = ERROR_ENCODER_NO_INPUT;
winlin authored
288
        srs_trace("empty intput type, ingest=%s. ret=%d", ingest->arg0().c_str(), ret);
289 290
        return ret;
    }
291
winlin authored
292
    if (input_type == SRS_CONF_DEFAULT_INGEST_TYPE_FILE) {
293 294 295
        std::string input_url = _srs_config->get_ingest_input_url(ingest);
        if (input_url.empty()) {
            ret = ERROR_ENCODER_NO_INPUT;
winlin authored
296
            srs_trace("empty intput url, ingest=%s. ret=%d", ingest->arg0().c_str(), ret);
297 298 299 300 301 302 303 304 305
            return ret;
        }
        
        // for file, set re.
        ffmpeg->set_iparams("-re");
    
        if ((ret = ffmpeg->initialize(input_url, output, log_file)) != ERROR_SUCCESS) {
            return ret;
        }
winlin authored
306
    } else if (input_type == SRS_CONF_DEFAULT_INGEST_TYPE_STREAM) {
winlin authored
307 308 309
        std::string input_url = _srs_config->get_ingest_input_url(ingest);
        if (input_url.empty()) {
            ret = ERROR_ENCODER_NO_INPUT;
winlin authored
310
            srs_trace("empty intput url, ingest=%s. ret=%d", ingest->arg0().c_str(), ret);
winlin authored
311 312 313 314 315 316 317 318 319
            return ret;
        }
        
        // for stream, no re.
        ffmpeg->set_iparams("");
    
        if ((ret = ffmpeg->initialize(input_url, output, log_file)) != ERROR_SUCCESS) {
            return ret;
        }
320 321
    } else {
        ret = ERROR_ENCODER_INPUT_TYPE;
winlin authored
322 323
        srs_error("invalid ingest=%s type=%s, ret=%d", 
            ingest->arg0().c_str(), input_type.c_str(), ret);
324
    }
325
    
326 327 328
    // set output format to flv for RTMP
    ffmpeg->set_oformat("flv");
    
winlin authored
329 330
    std::string vcodec = _srs_config->get_engine_vcodec(engine);
    std::string acodec = _srs_config->get_engine_acodec(engine);
331
    // whatever the engine config, use copy as default.
332 333
    bool engine_disabled = !engine || !_srs_config->get_engine_enabled(engine);
    if (engine_disabled || vcodec.empty() || acodec.empty()) {
334 335 336 337 338 339 340
        if ((ret = ffmpeg->initialize_copy()) != ERROR_SUCCESS) {
            return ret;
        }
    } else {
        if ((ret = ffmpeg->initialize_transcode(engine)) != ERROR_SUCCESS) {
            return ret;
        }
winlin authored
341 342
    }
    
winlin authored
343 344 345
    srs_trace("parse success, ingest=%s, vhost=%s", 
        ingest->arg0().c_str(), vhost->arg0().c_str());
    
winlin authored
346 347 348
    return ret;
}
349
void SrsIngester::show_ingest_log_message()
350
{
351 352
    pprint->elapse();
353 354 355 356
    if ((int)ingesters.size() <= 0) {
        return;
    }
    
357
    // reportable
358
    if (pprint->can_print()) {
winlin authored
359
        // TODO: FIXME: show more info.
360
        srs_trace("-> "SRS_CONSTS_LOG_INGESTER
361
            " time=%"PRId64", ingesters=%d", pprint->age(), (int)ingesters.size());
362 363 364
    }
}
365 366 367 368 369 370 371 372
int SrsIngester::on_reload_vhost_added(string vhost)
{
    int ret = ERROR_SUCCESS;
    
    SrsConfDirective* _vhost = _srs_config->get_vhost(vhost);
    if ((ret = parse_ingesters(_vhost)) != ERROR_SUCCESS) {
        return ret;
    }
373 374
    
    srs_trace("reload add vhost ingesters, vhost=%s", vhost.c_str());
375 376 377 378

    return ret;
}
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
int SrsIngester::on_reload_vhost_removed(string vhost)
{
    int ret = ERROR_SUCCESS;
    
    std::vector<SrsIngesterFFMPEG*>::iterator it;
    
    for (it = ingesters.begin(); it != ingesters.end();) {
        SrsIngesterFFMPEG* ingester = *it;
        
        if (ingester->vhost != vhost) {
            ++it;
            continue;
        }
        
        // stop the ffmpeg and free it.
        ingester->ffmpeg->stop();
        
        srs_trace("reload stop ingester, "
            "vhost=%s, id=%s", vhost.c_str(), ingester->id.c_str());
            
        srs_freep(ingester);
        
        // remove the item from ingesters.
        it = ingesters.erase(it);
    }

    return ret;
}
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 434 435 436 437 438 439 440 441
int SrsIngester::on_reload_ingest_removed(string vhost, string ingest_id)
{
    int ret = ERROR_SUCCESS;
    
    std::vector<SrsIngesterFFMPEG*>::iterator it;
    
    for (it = ingesters.begin(); it != ingesters.end();) {
        SrsIngesterFFMPEG* ingester = *it;
        
        if (ingester->vhost != vhost || ingester->id != ingest_id) {
            ++it;
            continue;
        }
        
        // stop the ffmpeg and free it.
        ingester->ffmpeg->stop();
        
        srs_trace("reload stop ingester, "
            "vhost=%s, id=%s", vhost.c_str(), ingester->id.c_str());
            
        srs_freep(ingester);
        
        // remove the item from ingesters.
        it = ingesters.erase(it);
    }
    
    return ret;
}

int SrsIngester::on_reload_ingest_added(string vhost, string ingest_id)
{
    int ret = ERROR_SUCCESS;
    
    SrsConfDirective* _vhost = _srs_config->get_vhost(vhost);
442
    SrsConfDirective* _ingester = _srs_config->get_ingest_by_id(vhost, ingest_id);
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
    
    if ((ret = parse_engines(_vhost, _ingester)) != ERROR_SUCCESS) {
        return ret;
    }
    
    srs_trace("reload add ingester, "
        "vhost=%s, id=%s", vhost.c_str(), ingest_id.c_str());
    
    return ret;
}

int SrsIngester::on_reload_ingest_updated(string vhost, string ingest_id)
{
    int ret = ERROR_SUCCESS;

    if ((ret = on_reload_ingest_removed(vhost, ingest_id)) != ERROR_SUCCESS) {
        return ret;
    }

    if ((ret = on_reload_ingest_added(vhost, ingest_id)) != ERROR_SUCCESS) {
        return ret;
    }
    
    srs_trace("reload updated ingester, "
        "vhost=%s, id=%s", vhost.c_str(), ingest_id.c_str());
    
    return ret;
}
winlin authored
472
#endif
473