mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-22 08:52:20 +08:00
优化tcp推流
This commit is contained in:
@@ -10,8 +10,8 @@ add_executable(MediaServer ${MediaServer_src_list})
|
||||
|
||||
if(WIN32)
|
||||
set_target_properties(MediaServer PROPERTIES COMPILE_FLAGS ${VS_FALGS} )
|
||||
else()
|
||||
install(TARGETS MediaServer DESTINATION ${INSTALL_PATH_EXECUTABLE})
|
||||
endif()
|
||||
|
||||
target_link_libraries(MediaServer jsoncpp ${LINK_LIB_LIST})
|
||||
|
||||
|
||||
|
||||
@@ -59,6 +59,11 @@ static bool is_local_ip(const string &ip){
|
||||
return false;
|
||||
}
|
||||
|
||||
void FFmpegSource::setupRecord(bool enable_hls, bool enable_mp4){
|
||||
_enable_hls = enable_hls;
|
||||
_enable_mp4 = enable_mp4;
|
||||
}
|
||||
|
||||
void FFmpegSource::play(const string &src_url,const string &dst_url,int timeout_ms,const onPlay &cb) {
|
||||
GET_CONFIG(string,ffmpeg_bin,FFmpeg::kBin);
|
||||
GET_CONFIG(string,ffmpeg_cmd,FFmpeg::kCmd);
|
||||
@@ -263,6 +268,12 @@ void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) {
|
||||
//防止多次进入onGetMediaSource函数导致无限递归调用的bug
|
||||
setDelegate(listener);
|
||||
src->setListener(shared_from_this());
|
||||
if (_enable_hls) {
|
||||
src->setupRecord(Recorder::type_hls, true, "");
|
||||
}
|
||||
if (_enable_mp4) {
|
||||
src->setupRecord(Recorder::type_mp4, true, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,13 +46,29 @@ public:
|
||||
typedef function<void(const SockException &ex)> onPlay;
|
||||
|
||||
FFmpegSource();
|
||||
virtual ~FFmpegSource();
|
||||
~FFmpegSource();
|
||||
|
||||
/**
|
||||
* 设置主动关闭回调
|
||||
* @param cb
|
||||
*/
|
||||
void setOnClose(const function<void()> &cb);
|
||||
void play(const string &src_url,const string &dst_url,int timeout_ms,const onPlay &cb);
|
||||
|
||||
/**
|
||||
* 开始播放url
|
||||
* @param src_url FFmpeg拉流地址
|
||||
* @param dst_url FFmpeg推流地址
|
||||
* @param timeout_ms 等待结果超时时间,单位毫秒
|
||||
* @param cb 成功与否回调
|
||||
*/
|
||||
void play(const string &src_url, const string &dst_url, int timeout_ms, const onPlay &cb);
|
||||
|
||||
/**
|
||||
* 设置录制
|
||||
* @param enable_hls 是否开启hls直播或录制
|
||||
* @param enable_mp4 是否录制mp4
|
||||
*/
|
||||
void setupRecord(bool enable_hls, bool enable_mp4);
|
||||
|
||||
private:
|
||||
void findAsync(int maxWaitMS ,const function<void(const MediaSource::Ptr &src)> &cb);
|
||||
void startTimer(int timeout_ms);
|
||||
@@ -69,6 +85,8 @@ private:
|
||||
std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const override;
|
||||
|
||||
private:
|
||||
bool _enable_hls = false;
|
||||
bool _enable_mp4 = false;
|
||||
Process _process;
|
||||
Timer::Ptr _timer;
|
||||
EventPoller::Ptr _poller;
|
||||
|
||||
@@ -830,28 +830,31 @@ void installWebApi() {
|
||||
static auto addFFmpegSource = [](const string &src_url,
|
||||
const string &dst_url,
|
||||
int timeout_ms,
|
||||
const function<void(const SockException &ex,const string &key)> &cb){
|
||||
bool enable_hls,
|
||||
bool enable_mp4,
|
||||
const function<void(const SockException &ex, const string &key)> &cb) {
|
||||
auto key = MD5(dst_url).hexdigest();
|
||||
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
|
||||
if(s_ffmpegMap.find(key) != s_ffmpegMap.end()){
|
||||
if (s_ffmpegMap.find(key) != s_ffmpegMap.end()) {
|
||||
//已经在拉流了
|
||||
cb(SockException(Err_success),key);
|
||||
cb(SockException(Err_success), key);
|
||||
return;
|
||||
}
|
||||
|
||||
FFmpegSource::Ptr ffmpeg = std::make_shared<FFmpegSource>();
|
||||
s_ffmpegMap[key] = ffmpeg;
|
||||
|
||||
ffmpeg->setOnClose([key](){
|
||||
ffmpeg->setOnClose([key]() {
|
||||
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
|
||||
s_ffmpegMap.erase(key);
|
||||
});
|
||||
ffmpeg->play(src_url, dst_url,timeout_ms,[cb , key](const SockException &ex){
|
||||
if(ex){
|
||||
ffmpeg->setupRecord(enable_hls, enable_mp4);
|
||||
ffmpeg->play(src_url, dst_url, timeout_ms, [cb, key](const SockException &ex) {
|
||||
if (ex) {
|
||||
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
|
||||
s_ffmpegMap.erase(key);
|
||||
}
|
||||
cb(ex,key);
|
||||
cb(ex, key);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -863,12 +866,15 @@ void installWebApi() {
|
||||
auto src_url = allArgs["src_url"];
|
||||
auto dst_url = allArgs["dst_url"];
|
||||
int timeout_ms = allArgs["timeout_ms"];
|
||||
auto enable_hls = allArgs["enable_hls"].as<int>();
|
||||
auto enable_mp4 = allArgs["enable_mp4"].as<int>();
|
||||
|
||||
addFFmpegSource(src_url,dst_url,timeout_ms,[invoker,val,headerOut](const SockException &ex,const string &key){
|
||||
if(ex){
|
||||
addFFmpegSource(src_url, dst_url, timeout_ms, enable_hls, enable_mp4,
|
||||
[invoker, val, headerOut](const SockException &ex, const string &key) {
|
||||
if (ex) {
|
||||
const_cast<Value &>(val)["code"] = API::OtherFailed;
|
||||
const_cast<Value &>(val)["msg"] = ex.what();
|
||||
}else{
|
||||
} else {
|
||||
const_cast<Value &>(val)["data"]["key"] = key;
|
||||
}
|
||||
invoker("200 OK", headerOut, val.toStyledString());
|
||||
@@ -1234,6 +1240,8 @@ void installWebApi() {
|
||||
addFFmpegSource("http://hls-ott-zhibo.wasu.tv/live/272/index.m3u8",/** ffmpeg拉流支持任意编码格式任意协议 **/
|
||||
dst_url,
|
||||
(1000 * timeout_sec) - 500,
|
||||
false,
|
||||
false,
|
||||
[invoker,val,headerOut](const SockException &ex,const string &key){
|
||||
if(ex){
|
||||
const_cast<Value &>(val)["code"] = API::OtherFailed;
|
||||
|
||||
Reference in New Issue
Block a user