1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_ingest.cpp

191 lines
4.9 KiB
C++
Raw Normal View History

2014-04-07 00:32:28 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013-2014 winlin
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>
#ifdef SRS_INGEST
2014-04-07 01:07:12 +00:00
#include <srs_kernel_error.hpp>
2014-04-07 05:13:57 +00:00
#include <srs_app_config.hpp>
#include <srs_kernel_log.hpp>
#include <srs_app_ffmpeg.hpp>
2014-04-07 01:07:12 +00:00
// when error, ingester sleep for a while and retry.
#define SRS_INGESTER_SLEEP_US (int64_t)(3*1000*1000LL)
SrsIngester::SrsIngester()
{
// TODO: FIXME: support reload.
2014-04-07 01:07:12 +00:00
pthread = new SrsThread(this, SRS_INGESTER_SLEEP_US);
}
SrsIngester::~SrsIngester()
{
srs_freep(pthread);
2014-04-07 05:13:57 +00:00
clear_engines();
2014-04-07 01:07:12 +00:00
}
int SrsIngester::start()
{
int ret = ERROR_SUCCESS;
2014-04-07 05:13:57 +00:00
2014-04-07 05:32:00 +00:00
if ((ret = parse()) != ERROR_SUCCESS) {
clear_engines();
ret = ERROR_SUCCESS;
return ret;
2014-04-07 05:13:57 +00:00
}
return ret;
}
int SrsIngester::parse_ingesters(SrsConfDirective* vhost)
{
int ret = ERROR_SUCCESS;
std::vector<SrsConfDirective*> ingesters;
_srs_config->get_ingesters(vhost->arg0(), ingesters);
// create engine
for (int i = 0; i < (int)ingesters.size(); i++) {
SrsConfDirective* ingest = ingesters[i];
if (!_srs_config->get_ingest_enabled(ingest)) {
continue;
}
2014-04-07 05:32:00 +00:00
if ((ret = parse_engines(vhost, ingest)) != ERROR_SUCCESS) {
return ret;
2014-04-07 05:13:57 +00:00
}
2014-04-07 05:32:00 +00:00
}
2014-04-07 05:13:57 +00:00
2014-04-07 05:32:00 +00:00
return ret;
}
int SrsIngester::parse_engines(SrsConfDirective* vhost, SrsConfDirective* ingest)
{
int ret = ERROR_SUCCESS;
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;
}
2014-04-07 05:13:57 +00:00
2014-04-07 05:32:00 +00:00
// get all engines.
std::vector<SrsConfDirective*> engines;
_srs_config->get_transcode_engines(ingest, engines);
if (engines.empty()) {
SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
if ((ret = initialize_ffmpeg(ffmpeg, ingest, NULL)) != ERROR_SUCCESS) {
srs_freep(ffmpeg);
if (ret != ERROR_ENCODER_LOOP) {
srs_error("invalid ingest engine. ret=%d", ret);
2014-04-07 05:13:57 +00:00
}
2014-04-07 05:32:00 +00:00
return ret;
}
ffmpegs.push_back(ffmpeg);
return ret;
}
// create engine
for (int i = 0; i < (int)engines.size(); i++) {
SrsConfDirective* engine = engines[i];
SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
if ((ret = initialize_ffmpeg(ffmpeg, ingest, engine)) != ERROR_SUCCESS) {
srs_freep(ffmpeg);
if (ret != ERROR_ENCODER_LOOP) {
srs_error("invalid ingest engine: %s %s", ingest->arg0().c_str(), engine->arg0().c_str());
}
return ret;
2014-04-07 05:13:57 +00:00
}
2014-04-07 05:32:00 +00:00
ffmpegs.push_back(ffmpeg);
2014-04-07 05:13:57 +00:00
}
return ret;
}
void SrsIngester::stop()
{
}
2014-04-07 01:07:12 +00:00
int SrsIngester::cycle()
{
int ret = ERROR_SUCCESS;
return ret;
}
void SrsIngester::on_thread_stop()
{
}
2014-04-07 05:13:57 +00:00
void SrsIngester::clear_engines()
{
std::vector<SrsFFMPEG*>::iterator it;
for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
SrsFFMPEG* ffmpeg = *it;
srs_freep(ffmpeg);
}
ffmpegs.clear();
}
2014-04-07 05:32:00 +00:00
int SrsIngester::parse()
{
int ret = ERROR_SUCCESS;
// parse ingesters
std::vector<SrsConfDirective*> vhosts;
_srs_config->get_vhosts(vhosts);
for (int i = 0; i < (int)vhosts.size(); i++) {
SrsConfDirective* vhost = vhosts[i];
if ((ret = parse_ingesters(vhost)) != ERROR_SUCCESS) {
return ret;
}
}
return ret;
}
2014-04-07 05:13:57 +00:00
int SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective* ingest, SrsConfDirective* engine)
{
int ret = ERROR_SUCCESS;
2014-04-07 05:32:00 +00:00
std::string input = _srs_config->get_ingest_input(ingest);
if (input.empty()) {
ret = ERROR_ENCODER_NO_INPUT;
srs_trace("empty ingest intput. ret=%d", ret);
return ret;
}
if (!engine || !_srs_config->get_engine_enabled(engine)) {
2014-04-07 05:13:57 +00:00
}
return ret;
}
2014-04-07 00:32:28 +00:00
#endif