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

Merge branch 'develop' into min

This commit is contained in:
winlin 2020-02-09 18:33:10 +08:00
commit cd48bf8aed
18 changed files with 188 additions and 81 deletions

View file

@ -159,6 +159,8 @@ For previous versions, please read:
## V3 changes
* v3.0, 2020-02-05, For [#665][bug #665], fix HTTP-FLV reloading bug. 3.0.116
* v3.0, 2020-02-05, For [#1592][bug #1592], fix terminal echo off by redirect process stdin. 3.0.115
* v3.0, 2020-02-04, For [#1186][bug #1186], refactor security check. 3.0.114
* v3.0, 2020-02-04, Fix [#939][bug #939], response right A/V flag in FLV header. 3.0.113
* v3.0, 2020-02-04, For [#939][bug #939], always enable fast FLV streaming.
@ -1664,6 +1666,8 @@ Winlin
[bug #1206]: https://github.com/ossrs/srs/issues/1206
[bug #939]: https://github.com/ossrs/srs/issues/939
[bug #1186]: https://github.com/ossrs/srs/issues/1186
[bug #1592]: https://github.com/ossrs/srs/issues/1592
[bug #665]: https://github.com/ossrs/srs/issues/665
[bug #xxxxxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxxxxx
[exo #828]: https://github.com/google/ExoPlayer/pull/828

View file

@ -19,11 +19,16 @@ pid ./objs/srs.pid;
# performance about 10%.
# default: 60000
chunk_size 60000;
# the logs dir.
# the log dir for FFMPEG.
# if enabled ffmpeg, each transcoding stream will create a log file.
# /dev/null to disable the log.
# default: ./objs
ff_log_dir ./objs;
# the log level for FFMPEG.
# info warning error fatal panic quiet
# trace debug verbose
# default: info
ff_log_level info;
# the log tank, console or file.
# if console, print log to console.
# if file, write log to file. requires srs_log_file if log to file.

View file

@ -3488,6 +3488,7 @@ srs_error_t SrsConfig::check_normal_config()
&& n != "http_api" && n != "stats" && n != "vhost" && n != "pithy_print_ms"
&& n != "http_server" && n != "stream_caster" && n != "srt_server"
&& n != "utc_time" && n != "work_dir" && n != "asprocess"
&& n != "ff_log_level"
) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "illegal directive %s", n.c_str());
}
@ -5775,13 +5776,13 @@ string SrsConfig::get_log_file()
return conf->arg0();
}
bool SrsConfig::get_ffmpeg_log_enabled()
bool SrsConfig::get_ff_log_enabled()
{
string log = get_ffmpeg_log_dir();
string log = get_ff_log_dir();
return log != SRS_CONSTS_NULL_FILE;
}
string SrsConfig::get_ffmpeg_log_dir()
string SrsConfig::get_ff_log_dir()
{
static string DEFAULT = "./objs";
@ -5793,6 +5794,18 @@ string SrsConfig::get_ffmpeg_log_dir()
return conf->arg0();
}
string SrsConfig::get_ff_log_level()
{
static string DEFAULT = "info";
SrsConfDirective* conf = root->get("ff_log_level");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return conf->arg0();
}
SrsConfDirective* SrsConfig::get_dash(string vhost)
{
SrsConfDirective* conf = get_vhost(vhost);

View file

@ -786,10 +786,12 @@ public:
// Get the log file path.
virtual std::string get_log_file();
// Whether ffmpeg log enabled
virtual bool get_ffmpeg_log_enabled();
virtual bool get_ff_log_enabled();
// The ffmpeg log dir.
// @remark, /dev/null to disable it.
virtual std::string get_ffmpeg_log_dir();
virtual std::string get_ff_log_dir();
// The ffmpeg log level.
virtual std::string get_ff_log_level();
// The MPEG-DASH section.
private:
virtual SrsConfDirective* get_dash(std::string vhost);

View file

@ -285,8 +285,8 @@ srs_error_t SrsEncoder::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsRequest* req, Sr
std::string log_file = SRS_CONSTS_NULL_FILE; // disabled
// write ffmpeg info to log file.
if (_srs_config->get_ffmpeg_log_enabled()) {
log_file = _srs_config->get_ffmpeg_log_dir();
if (_srs_config->get_ff_log_enabled()) {
log_file = _srs_config->get_ff_log_dir();
log_file += "/";
log_file += "ffmpeg-encoder";
log_file += "-";

View file

@ -82,9 +82,9 @@ SrsFFMPEG::~SrsFFMPEG()
srs_freep(process);
}
void SrsFFMPEG::set_iparams(string iparams)
void SrsFFMPEG::append_iparam(string iparam)
{
_iparams = iparams;
iparams.push_back(iparam);
}
void SrsFFMPEG::set_oformat(string format)
@ -230,8 +230,11 @@ srs_error_t SrsFFMPEG::start()
params.push_back(ffmpeg);
// input params
if (!_iparams.empty()) {
params.push_back(_iparams);
for (int i = 0; i < iparams.size(); i++) {
string iparam = iparams.at(i);
if (!iparam.empty()) {
params.push_back(iparam);
}
}
// build the perfile

View file

@ -45,7 +45,7 @@ private:
std::string log_file;
private:
std::string ffmpeg;
std::string _iparams;
std::vector<std::string> iparams;
std::vector<std::string> perfile;
std::string iformat;
std::string input;
@ -70,7 +70,7 @@ public:
SrsFFMPEG(std::string ffmpeg_bin);
virtual ~SrsFFMPEG();
public:
virtual void set_iparams(std::string iparams);
virtual void append_iparam(std::string iparam);
virtual void set_oformat(std::string format);
virtual std::string output();
public:

View file

@ -809,6 +809,11 @@ SrsLiveEntry::SrsLiveEntry(std::string m)
_is_aac = (ext == ".aac");
}
SrsLiveEntry::~SrsLiveEntry()
{
srs_freep(req);
}
bool SrsLiveEntry::is_flv()
{
return _is_flv;
@ -846,7 +851,6 @@ SrsHttpStreamServer::~SrsHttpStreamServer()
std::map<std::string, SrsLiveEntry*>::iterator it;
for (it = tflvs.begin(); it != tflvs.end(); ++it) {
SrsLiveEntry* entry = it->second;
srs_freep(entry->req);
srs_freep(entry);
}
tflvs.clear();
@ -901,7 +905,9 @@ srs_error_t SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r)
mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
entry = new SrsLiveEntry(mount);
entry->source = s;
entry->req = r->copy()->as_http();
entry->cache = new SrsBufferCache(s, r);
entry->stream = new SrsLiveStream(s, r, entry->cache);
@ -971,7 +977,8 @@ srs_error_t SrsHttpStreamServer::on_reload_vhost_added(string vhost)
srs_error_t SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost)
{
srs_error_t err = srs_success;
// Create new vhost.
if (tflvs.find(vhost) == tflvs.end()) {
if ((err = initialize_flv_entry(vhost)) != srs_success) {
return srs_error_wrap(err, "init flv entry");
@ -981,41 +988,27 @@ srs_error_t SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost
// and do mount automatically on playing http flv if this stream is a new http_remux stream.
return err;
}
SrsLiveEntry* tmpl = tflvs[vhost];
SrsRequest* req = tmpl->req;
SrsSource* source = tmpl->source;
if (source && req) {
// cleanup the exists http remux.
http_unmount(source, req);
}
if (!_srs_config->get_vhost_http_remux_enabled(vhost)) {
return err;
}
string old_tmpl_mount = tmpl->mount;
string new_tmpl_mount = _srs_config->get_vhost_http_remux_mount(vhost);
/**
* TODO: not support to reload different mount url for the time being.
* if the mount is change, need more logical thing to deal with.
* such as erase stream from sflvs and free all related resource.
*/
srs_assert(old_tmpl_mount == new_tmpl_mount);
// do http mount directly with SrsRequest and SrsSource if stream is played already.
if (req) {
std::string sid = req->get_stream_url();
// remount stream.
if ((err = http_mount(source, req)) != srs_success) {
return srs_error_wrap(err, "vhost %s http_remux reload failed", vhost.c_str());
// Update all streams for exists vhost.
// TODO: FIMXE: If url changed, needs more things to deal with.
std::map<std::string, SrsLiveEntry*>::iterator it;
for (it = sflvs.begin(); it != sflvs.end(); ++it) {
SrsLiveEntry* entry = it->second;
if (!entry || !entry->req || !entry->source) {
continue;
}
SrsRequest* req = entry->req;
if (!req || req->vhost != vhost) {
continue;
}
SrsSource* source = entry->source;
if (_srs_config->get_vhost_http_remux_enabled(vhost)) {
http_mount(source, req);
} else {
http_unmount(source, req);
}
} else {
// for without SrsRequest and SrsSource if stream is not played yet, do http mount automatically
// when start play this http flv stream.
}
srs_trace("vhost %s http_remux reload success", vhost.c_str());

View file

@ -207,7 +207,9 @@ private:
bool _is_aac;
bool _is_mp3;
public:
// We will free the request.
SrsRequest* req;
// Shared source.
SrsSource* source;
public:
// For template, the mount contains variables.
@ -218,6 +220,7 @@ public:
SrsBufferCache* cache;
SrsLiveEntry(std::string m);
virtual ~SrsLiveEntry();
bool is_flv();
bool is_ts();

View file

@ -397,8 +397,8 @@ srs_error_t SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective*
std::string log_file = SRS_CONSTS_NULL_FILE; // disabled
// write ffmpeg info to log file.
if (_srs_config->get_ffmpeg_log_enabled()) {
log_file = _srs_config->get_ffmpeg_log_dir();
if (_srs_config->get_ff_log_enabled()) {
log_file = _srs_config->get_ff_log_dir();
log_file += "/";
log_file += "ffmpeg-ingest";
log_file += "-";
@ -409,7 +409,13 @@ srs_error_t SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective*
log_file += stream;
log_file += ".log";
}
std::string log_level = _srs_config->get_ff_log_level();
if (!log_level.empty()) {
ffmpeg->append_iparam("-loglevel");
ffmpeg->append_iparam(log_level);
}
// input
std::string input_type = _srs_config->get_ingest_input_type(ingest);
if (input_type.empty()) {
@ -423,7 +429,7 @@ srs_error_t SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective*
}
// for file, set re.
ffmpeg->set_iparams("-re");
ffmpeg->append_iparam("-re");
if ((err = ffmpeg->initialize(input_url, output, log_file)) != srs_success) {
return srs_error_wrap(err, "init ffmpeg");
@ -435,7 +441,7 @@ srs_error_t SrsIngester::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsConfDirective*
}
// for stream, no re.
ffmpeg->set_iparams("");
ffmpeg->append_iparam("");
if ((err = ffmpeg->initialize(input_url, output, log_file)) != srs_success) {
return srs_error_wrap(err, "init ffmpeg");

View file

@ -152,7 +152,7 @@ srs_error_t srs_redirect_output(string from_file, int to_fd)
// redirect the fd to file.
int fd = -1;
int flags = O_CREAT|O_WRONLY|O_APPEND;
int flags = O_CREAT|O_RDWR|O_APPEND;
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;
if ((fd = ::open(from_file.c_str(), flags, mode)) < 0) {
@ -197,10 +197,7 @@ srs_error_t SrsProcess::start()
// ignore the SIGINT and SIGTERM
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
// for the stdin,
// should never close it or ffmpeg will error.
// for the stdout, ignore when not specified.
// redirect stdout to file if possible.
if ((err = srs_redirect_output(stdout_file, STDOUT_FILENO)) != srs_success) {
@ -212,16 +209,22 @@ srs_error_t SrsProcess::start()
if ((err = srs_redirect_output(stderr_file, STDERR_FILENO)) != srs_success) {
return srs_error_wrap(err, "redirect output");
}
// No stdin for process, @bug https://github.com/ossrs/srs/issues/1592
if ((err = srs_redirect_output("/dev/null", STDIN_FILENO)) != srs_success) {
return srs_error_wrap(err, "redirect input");
}
// should never close the fd 3+, for it myabe used.
// for fd should close at exec, use fnctl to set it.
// log basic info to stderr.
if (true) {
fprintf(stderr, "\n");
fprintf(stderr, "process ppid=%d, cid=%d, pid=%d\n", ppid, cid, getpid());
fprintf(stderr, "process binary=%s, cli: %s\n", bin.c_str(), cli.c_str());
fprintf(stderr, "process actual cli: %s\n", actual_cli.c_str());
fprintf(stdout, "\n");
fprintf(stdout, "process ppid=%d, cid=%d, pid=%d, in=%d, out=%d, err=%d\n",
ppid, cid, getpid(), STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO);
fprintf(stdout, "process binary=%s, cli: %s\n", bin.c_str(), cli.c_str());
fprintf(stdout, "process actual cli: %s\n", actual_cli.c_str());
}
// memory leak in child process, it's ok.

View file

@ -24,6 +24,6 @@
#ifndef SRS_CORE_VERSION3_HPP
#define SRS_CORE_VERSION3_HPP
#define SRS_VERSION3_REVISION 114
#define SRS_VERSION3_REVISION 116
#endif

View file

@ -1,13 +1,22 @@
#include "srt_data.hpp"
#include <string.h>
SRT_DATA_MSG::SRT_DATA_MSG(unsigned int len, const std::string& path):_len(len)
SRT_DATA_MSG::SRT_DATA_MSG(const std::string& path, unsigned int msg_type):_msg_type(msg_type)
,_len(0)
,_data_p(nullptr)
,_key_path(path) {
}
SRT_DATA_MSG::SRT_DATA_MSG(unsigned int len, const std::string& path, unsigned int msg_type):_msg_type(msg_type)
,_len(len)
,_key_path(path) {
_data_p = new unsigned char[len];
memset(_data_p, 0, len);
}
SRT_DATA_MSG::SRT_DATA_MSG(unsigned char* data_p, unsigned int len, const std::string& path):_len(len)
SRT_DATA_MSG::SRT_DATA_MSG(unsigned char* data_p, unsigned int len, const std::string& path, unsigned int msg_type):_msg_type(msg_type)
,_len(len)
,_key_path(path)
{
_data_p = new unsigned char[len];
@ -15,7 +24,13 @@ SRT_DATA_MSG::SRT_DATA_MSG(unsigned char* data_p, unsigned int len, const std::s
}
SRT_DATA_MSG::~SRT_DATA_MSG() {
delete _data_p;
if (_data_p && (_len > 0)) {
delete _data_p;
}
}
unsigned int SRT_DATA_MSG::msg_type() {
return _msg_type;
}
std::string SRT_DATA_MSG::get_path() {

View file

@ -3,17 +3,23 @@
#include <string>
#include <memory>
#define SRT_MSG_DATA_TYPE 0x01
#define SRT_MSG_CLOSE_TYPE 0x02
class SRT_DATA_MSG {
public:
SRT_DATA_MSG(unsigned int len, const std::string& path);
SRT_DATA_MSG(unsigned char* data_p, unsigned int len, const std::string& path);
SRT_DATA_MSG(const std::string& path, unsigned int msg_type=SRT_MSG_DATA_TYPE);
SRT_DATA_MSG(unsigned int len, const std::string& path, unsigned int msg_type=SRT_MSG_DATA_TYPE);
SRT_DATA_MSG(unsigned char* data_p, unsigned int len, const std::string& path, unsigned int msg_type=SRT_MSG_DATA_TYPE);
~SRT_DATA_MSG();
unsigned int msg_type();
unsigned int data_len();
unsigned char* get_data();
std::string get_path();
private:
unsigned int _msg_type;
unsigned int _len;
unsigned char* _data_p;
std::string _key_path;

View file

@ -17,8 +17,8 @@
static bool MONITOR_STATICS_ENABLE = false;
static long long MONITOR_TIMEOUT = 5000;
const unsigned int DEF_DATA_SIZE = 188*7;
const long long CHECK_ALIVE_INTERVAL = 10*1000;
const long long CHECK_ALIVE_TIMEOUT = 15*1000;
const long long CHECK_ALIVE_INTERVAL = 5*1000;
const long long CHECK_ALIVE_TIMEOUT = 5*1000;
long long srt_now_ms = 0;
@ -236,6 +236,10 @@ void srt_handle::onwork()
add_newconn(msg.conn_ptr, msg.events);
}
if (_conn_map.empty()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue;
}
check_alive();
ret = srt_epoll_wait(_handle_pollid, read_fds, &rfd_num, write_fds, &wfd_num,
@ -243,7 +247,6 @@ void srt_handle::onwork()
if (ret < 0) {
srs_info("srt handle epoll is timeout, ret=%d, srt_now_ms=%ld",
ret, srt_now_ms);
std::this_thread::sleep_for(std::chrono::milliseconds(30));
continue;
}
@ -376,6 +379,7 @@ void srt_handle::close_push_conn(SRTSOCKET srtsocket) {
_push_conn_map.erase(push_iter);
}
_conn_map.erase(iter);
srt2rtmp::get_instance()->insert_ctrl_message(SRT_MSG_CLOSE_TYPE, conn_ptr->get_subpath());
conn_ptr->close();
}

View file

@ -61,6 +61,14 @@ void srt2rtmp::insert_data_message(unsigned char* data_p, unsigned int len, cons
return;
}
void srt2rtmp::insert_ctrl_message(unsigned int msg_type, const std::string& key_path) {
std::unique_lock<std::mutex> locker(_mutex);
SRT_DATA_MSG_PTR msg_ptr = std::make_shared<SRT_DATA_MSG>(key_path, msg_type);
_msg_queue.push(msg_ptr);
//_notify_cond.notify_one();
return;
}
SRT_DATA_MSG_PTR srt2rtmp::get_data_message() {
std::unique_lock<std::mutex> locker(_mutex);
SRT_DATA_MSG_PTR msg_ptr;
@ -79,8 +87,8 @@ SRT_DATA_MSG_PTR srt2rtmp::get_data_message() {
}
void srt2rtmp::check_rtmp_alive() {
const int64_t CHECK_INTERVAL = 15*1000;
const int64_t ALIVE_TIMEOUT_MAX = 20*1000;
const int64_t CHECK_INTERVAL = 5*1000;
const int64_t ALIVE_TIMEOUT_MAX = 5*1000;
if (_lastcheck_ts == 0) {
_lastcheck_ts = now_ms();
@ -108,6 +116,22 @@ void srt2rtmp::check_rtmp_alive() {
return;
}
void srt2rtmp::handle_close_rtmpsession(const std::string& key_path) {
RTMP_CLIENT_PTR rtmp_ptr;
auto iter = _rtmp_client_map.find(key_path);
if (iter == _rtmp_client_map.end()) {
srs_error("fail to close rtmp session fail, can't find session by key_path:%s",
key_path.c_str());
return;
}
rtmp_ptr = iter->second;
_rtmp_client_map.erase(iter);
srs_trace("close rtmp session which key_path is %s", key_path.c_str());
rtmp_ptr->close();
return;
}
//the cycle is running in srs coroutine
srs_error_t srt2rtmp::cycle() {
srs_error_t err = srs_success;
@ -119,7 +143,25 @@ srs_error_t srt2rtmp::cycle() {
if (!msg_ptr) {
srs_usleep((30 * SRS_UTIME_MILLISECONDS));
} else {
handle_ts_data(msg_ptr);
switch (msg_ptr->msg_type()) {
case SRT_MSG_DATA_TYPE:
{
handle_ts_data(msg_ptr);
break;
}
case SRT_MSG_CLOSE_TYPE:
{
handle_close_rtmpsession(msg_ptr->get_path());
break;
}
default:
{
srs_error("srt to rtmp get wrong message type(%u), path:%s",
msg_ptr->msg_type(), msg_ptr->get_path().c_str());
assert(0);
}
}
}
check_rtmp_alive();
if ((err = _trd_ptr->pull()) != srs_success) {

View file

@ -85,11 +85,13 @@ public:
void release();
void insert_data_message(unsigned char* data_p, unsigned int len, const std::string& key_path);
void insert_ctrl_message(unsigned int msg_type, const std::string& key_path);
private:
SRT_DATA_MSG_PTR get_data_message();
virtual srs_error_t cycle();
void handle_ts_data(SRT_DATA_MSG_PTR data_ptr);
void handle_close_rtmpsession(const std::string& key_path);
void check_rtmp_alive();
private:

View file

@ -888,6 +888,11 @@ VOID TEST(ConfigMainTest, CheckConf_ff_log_dir)
MockSrsConfig conf;
HELPER_ASSERT_FAILED(conf.parse(_MIN_OK_CONF "ff_log_dirs ./objs;"));
}
if (true) {
MockSrsConfig conf;
HELPER_ASSERT_FAILED(conf.parse(_MIN_OK_CONF "ff_log_levels info;"));
}
}
VOID TEST(ConfigMainTest, CheckConf_srs_log_level)
@ -3503,12 +3508,13 @@ VOID TEST(ConfigMainTest, CheckVhostConfig5)
if (true) {
MockSrsConfig conf;
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "srs_log_tank xxx;srs_log_level xxx2;srs_log_file xxx3;ff_log_dir xxx4;"));
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "srs_log_tank xxx;srs_log_level xxx2;srs_log_file xxx3;ff_log_dir xxx4; ff_log_level xxx5;"));
EXPECT_TRUE(conf.get_log_tank_file());
EXPECT_STREQ("xxx2", conf.get_log_level().c_str());
EXPECT_STREQ("xxx3", conf.get_log_file().c_str());
EXPECT_STREQ("xxx4", conf.get_ffmpeg_log_dir().c_str());
EXPECT_TRUE(conf.get_ffmpeg_log_enabled());
EXPECT_STREQ("xxx4", conf.get_ff_log_dir().c_str());
EXPECT_STREQ("xxx5", conf.get_ff_log_level().c_str());
EXPECT_TRUE(conf.get_ff_log_enabled());
}
if (true) {