mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 02:34:26 +08:00
Add ServiceController to manage PlayerProxy/PusherProxy/FFmpegSource/RtpServer services (#3337)
This commit is contained in:
parent
03c93d0b23
commit
2f50344e7b
@ -33,12 +33,11 @@ static TcpServer::Ptr shell_server;
|
|||||||
|
|
||||||
#ifdef ENABLE_RTPPROXY
|
#ifdef ENABLE_RTPPROXY
|
||||||
#include "Rtp/RtpServer.h"
|
#include "Rtp/RtpServer.h"
|
||||||
static std::shared_ptr<RtpServer> rtpServer;
|
static RtpServer::Ptr rtpServer;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef ENABLE_WEBRTC
|
#ifdef ENABLE_WEBRTC
|
||||||
#include "../webrtc/WebRtcSession.h"
|
#include "../webrtc/WebRtcSession.h"
|
||||||
#include "../webrtc/WebRtcTransport.h"
|
|
||||||
static UdpServer::Ptr rtcServer_udp;
|
static UdpServer::Ptr rtcServer_udp;
|
||||||
static TcpServer::Ptr rtcServer_tcp;
|
static TcpServer::Ptr rtcServer_tcp;
|
||||||
#endif
|
#endif
|
||||||
|
@ -38,16 +38,12 @@ bool G711RtpEncoder::inputFrame(const Frame::Ptr &frame) {
|
|||||||
auto ptr = _cache_frame->data() + _cache_frame->prefixSize();
|
auto ptr = _cache_frame->data() + _cache_frame->prefixSize();
|
||||||
auto len = _cache_frame->size() - _cache_frame->prefixSize();
|
auto len = _cache_frame->size() - _cache_frame->prefixSize();
|
||||||
auto remain_size = len;
|
auto remain_size = len;
|
||||||
auto max_size = 160 * _channels * _pkt_dur_ms / 20; // 20 ms per 160 byte
|
size_t max_size = 160 * _channels * _pkt_dur_ms / 20; // 20 ms per 160 byte
|
||||||
uint32_t n = 0;
|
size_t n = 0;
|
||||||
bool mark = true;
|
bool mark = true;
|
||||||
while (remain_size >= max_size) {
|
while (remain_size >= max_size) {
|
||||||
size_t rtp_size;
|
assert(remain_size >= max_size);
|
||||||
if (remain_size >= max_size) {
|
const size_t rtp_size = max_size;
|
||||||
rtp_size = max_size;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
n++;
|
n++;
|
||||||
stamp += _pkt_dur_ms;
|
stamp += _pkt_dur_ms;
|
||||||
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, ptr, rtp_size, mark, stamp), true);
|
RtpCodec::inputRtp(getRtpInfo().makeRtp(TrackAudio, ptr, rtp_size, mark, stamp), true);
|
||||||
|
@ -297,22 +297,71 @@ static inline void addHttpListener(){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename Type>
|
||||||
|
class ServiceController {
|
||||||
|
public:
|
||||||
|
using Pointer = std::shared_ptr<Type>;
|
||||||
|
std::unordered_map<std::string, Pointer> _map;
|
||||||
|
mutable std::recursive_mutex _mtx;
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
decltype(_map) copy;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
||||||
|
copy.swap(_map);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t erase(const std::string &key) {
|
||||||
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
||||||
|
return _map.erase(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pointer find(const std::string &key) const {
|
||||||
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
||||||
|
auto it = _map.find(key);
|
||||||
|
if (it == _map.end()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ..._Args>
|
||||||
|
Pointer make(const std::string &key, _Args&& ...__args) {
|
||||||
|
// assert(!find(key));
|
||||||
|
|
||||||
|
auto server = std::make_shared<Type>(std::forward<_Args>(__args)...);
|
||||||
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
||||||
|
auto it = _map.emplace(key, server);
|
||||||
|
assert(it.second);
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class ..._Args>
|
||||||
|
Pointer makeWithAction(const std::string &key, function<void(Pointer)> action, _Args&& ...__args) {
|
||||||
|
// assert(!find(key));
|
||||||
|
|
||||||
|
auto server = std::make_shared<Type>(std::forward<_Args>(__args)...);
|
||||||
|
action(server);
|
||||||
|
std::lock_guard<std::recursive_mutex> lck(_mtx);
|
||||||
|
auto it = _map.emplace(key, server);
|
||||||
|
assert(it.second);
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
//拉流代理器列表
|
//拉流代理器列表
|
||||||
static unordered_map<string, PlayerProxy::Ptr> s_proxyMap;
|
static ServiceController<PlayerProxy> s_player_proxy;
|
||||||
static recursive_mutex s_proxyMapMtx;
|
|
||||||
|
|
||||||
//推流代理器列表
|
//推流代理器列表
|
||||||
static unordered_map<string, PusherProxy::Ptr> s_proxyPusherMap;
|
static ServiceController<PusherProxy> s_pusher_proxy;
|
||||||
static recursive_mutex s_proxyPusherMapMtx;
|
|
||||||
|
|
||||||
//FFmpeg拉流代理器列表
|
//FFmpeg拉流代理器列表
|
||||||
static unordered_map<string, FFmpegSource::Ptr> s_ffmpegMap;
|
static ServiceController<FFmpegSource> s_ffmpeg_src;
|
||||||
static recursive_mutex s_ffmpegMapMtx;
|
|
||||||
|
|
||||||
#if defined(ENABLE_RTPPROXY)
|
#if defined(ENABLE_RTPPROXY)
|
||||||
//rtp服务器列表
|
//rtp服务器列表
|
||||||
static unordered_map<string, RtpServer::Ptr> s_rtpServerMap;
|
static ServiceController<RtpServer> s_rtp_server;
|
||||||
static recursive_mutex s_rtpServerMapMtx;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static inline string getProxyKey(const string &vhost, const string &app, const string &stream) {
|
static inline string getProxyKey(const string &vhost, const string &app, const string &stream) {
|
||||||
@ -416,46 +465,23 @@ Value makeMediaSourceJson(MediaSource &media){
|
|||||||
|
|
||||||
#if defined(ENABLE_RTPPROXY)
|
#if defined(ENABLE_RTPPROXY)
|
||||||
uint16_t openRtpServer(uint16_t local_port, const string &stream_id, int tcp_mode, const string &local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) {
|
uint16_t openRtpServer(uint16_t local_port, const string &stream_id, int tcp_mode, const string &local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) {
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
if (s_rtp_server.find(stream_id)) {
|
||||||
if (s_rtpServerMap.find(stream_id) != s_rtpServerMap.end()) {
|
|
||||||
//为了防止RtpProcess所有权限混乱的问题,不允许重复添加相同的stream_id
|
//为了防止RtpProcess所有权限混乱的问题,不允许重复添加相同的stream_id
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
RtpServer::Ptr server = std::make_shared<RtpServer>();
|
auto server = s_rtp_server.makeWithAction(stream_id, [&](RtpServer::Ptr server) {
|
||||||
server->start(local_port, stream_id, (RtpServer::TcpMode)tcp_mode, local_ip.c_str(), re_use_port, ssrc, only_track, multiplex);
|
server->start(local_port, stream_id, (RtpServer::TcpMode)tcp_mode, local_ip.c_str(), re_use_port, ssrc, only_track, multiplex);
|
||||||
|
});
|
||||||
server->setOnDetach([stream_id]() {
|
server->setOnDetach([stream_id]() {
|
||||||
//设置rtp超时移除事件
|
//设置rtp超时移除事件
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
s_rtp_server.erase(stream_id);
|
||||||
s_rtpServerMap.erase(stream_id);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//保存对象
|
|
||||||
s_rtpServerMap.emplace(stream_id, server);
|
|
||||||
//回复json
|
//回复json
|
||||||
return server->getPort();
|
return server->getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
void connectRtpServer(const string &stream_id, const string &dst_url, uint16_t dst_port, const function<void(const SockException &ex)> &cb) {
|
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
|
||||||
auto it = s_rtpServerMap.find(stream_id);
|
|
||||||
if (it == s_rtpServerMap.end()) {
|
|
||||||
cb(SockException(Err_other, "未找到rtp服务"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
it->second->connectToServer(dst_url, dst_port, cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool closeRtpServer(const string &stream_id) {
|
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
|
||||||
auto it = s_rtpServerMap.find(stream_id);
|
|
||||||
if (it == s_rtpServerMap.end()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
auto server = it->second;
|
|
||||||
s_rtpServerMap.erase(it);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void getStatisticJson(const function<void(Value &val)> &cb) {
|
void getStatisticJson(const function<void(Value &val)> &cb) {
|
||||||
@ -546,15 +572,13 @@ void addStreamProxy(const string &vhost, const string &app, const string &stream
|
|||||||
const ProtocolOption &option, int rtp_type, float timeout_sec, const mINI &args,
|
const ProtocolOption &option, int rtp_type, float timeout_sec, const mINI &args,
|
||||||
const function<void(const SockException &ex, const string &key)> &cb) {
|
const function<void(const SockException &ex, const string &key)> &cb) {
|
||||||
auto key = getProxyKey(vhost, app, stream);
|
auto key = getProxyKey(vhost, app, stream);
|
||||||
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
if (s_player_proxy.find(key)) {
|
||||||
if (s_proxyMap.find(key) != s_proxyMap.end()) {
|
|
||||||
//已经在拉流了
|
//已经在拉流了
|
||||||
cb(SockException(Err_other, "This stream already exists"), key);
|
cb(SockException(Err_other, "This stream already exists"), key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
//添加拉流代理
|
//添加拉流代理
|
||||||
auto player = std::make_shared<PlayerProxy>(vhost, app, stream, option, retry_count);
|
auto player = s_player_proxy.make(key, vhost, app, stream, option, retry_count);
|
||||||
s_proxyMap[key] = player;
|
|
||||||
|
|
||||||
// 先透传参数
|
// 先透传参数
|
||||||
player->mINI::operator=(args);
|
player->mINI::operator=(args);
|
||||||
@ -562,7 +586,7 @@ void addStreamProxy(const string &vhost, const string &app, const string &stream
|
|||||||
//指定RTP over TCP(播放rtsp时有效)
|
//指定RTP over TCP(播放rtsp时有效)
|
||||||
(*player)[Client::kRtpType] = rtp_type;
|
(*player)[Client::kRtpType] = rtp_type;
|
||||||
|
|
||||||
if (timeout_sec > 0.1) {
|
if (timeout_sec > 0.1f) {
|
||||||
//播放握手超时时间
|
//播放握手超时时间
|
||||||
(*player)[Client::kTimeoutMS] = timeout_sec * 1000;
|
(*player)[Client::kTimeoutMS] = timeout_sec * 1000;
|
||||||
}
|
}
|
||||||
@ -570,20 +594,68 @@ void addStreamProxy(const string &vhost, const string &app, const string &stream
|
|||||||
//开始播放,如果播放失败或者播放中止,将会自动重试若干次,默认一直重试
|
//开始播放,如果播放失败或者播放中止,将会自动重试若干次,默认一直重试
|
||||||
player->setPlayCallbackOnce([cb, key](const SockException &ex) {
|
player->setPlayCallbackOnce([cb, key](const SockException &ex) {
|
||||||
if (ex) {
|
if (ex) {
|
||||||
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
s_player_proxy.erase(key);
|
||||||
s_proxyMap.erase(key);
|
|
||||||
}
|
}
|
||||||
cb(ex, key);
|
cb(ex, key);
|
||||||
});
|
});
|
||||||
|
|
||||||
//被主动关闭拉流
|
//被主动关闭拉流
|
||||||
player->setOnClose([key](const SockException &ex) {
|
player->setOnClose([key](const SockException &ex) {
|
||||||
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
s_player_proxy.erase(key);
|
||||||
s_proxyMap.erase(key);
|
|
||||||
});
|
});
|
||||||
player->play(url);
|
player->play(url);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void addStreamPusherProxy(const string &schema,
|
||||||
|
const string &vhost,
|
||||||
|
const string &app,
|
||||||
|
const string &stream,
|
||||||
|
const string &url,
|
||||||
|
int retry_count,
|
||||||
|
int rtp_type,
|
||||||
|
float timeout_sec,
|
||||||
|
const function<void(const SockException &ex, const string &key)> &cb) {
|
||||||
|
auto key = getPusherKey(schema, vhost, app, stream, url);
|
||||||
|
auto src = MediaSource::find(schema, vhost, app, stream);
|
||||||
|
if (!src) {
|
||||||
|
cb(SockException(Err_other, "can not find the source stream"), key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (s_pusher_proxy.find(key)) {
|
||||||
|
//已经在推流了
|
||||||
|
cb(SockException(Err_success), key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//添加推流代理
|
||||||
|
auto pusher = s_pusher_proxy.make(key, src, retry_count);
|
||||||
|
|
||||||
|
//指定RTP over TCP(播放rtsp时有效)
|
||||||
|
pusher->emplace(Client::kRtpType, rtp_type);
|
||||||
|
|
||||||
|
if (timeout_sec > 0.1f) {
|
||||||
|
//推流握手超时时间
|
||||||
|
pusher->emplace(Client::kTimeoutMS, timeout_sec * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
//开始推流,如果推流失败或者推流中止,将会自动重试若干次,默认一直重试
|
||||||
|
pusher->setPushCallbackOnce([cb, key, url](const SockException &ex) {
|
||||||
|
if (ex) {
|
||||||
|
WarnL << "Push " << url << " failed, key: " << key << ", err: " << ex;
|
||||||
|
s_pusher_proxy.erase(key);
|
||||||
|
}
|
||||||
|
cb(ex, key);
|
||||||
|
});
|
||||||
|
|
||||||
|
//被主动关闭推流
|
||||||
|
pusher->setOnClose([key, url](const SockException &ex) {
|
||||||
|
WarnL << "Push " << url << " failed, key: " << key << ", err: " << ex;
|
||||||
|
s_pusher_proxy.erase(key);
|
||||||
|
});
|
||||||
|
pusher->publish(url);
|
||||||
|
}
|
||||||
|
|
||||||
template <typename Type>
|
template <typename Type>
|
||||||
static void getArgsValue(const HttpAllArgs<ApiArgsType> &allArgs, const string &key, Type &value) {
|
static void getArgsValue(const HttpAllArgs<ApiArgsType> &allArgs, const string &key, Type &value) {
|
||||||
auto val = allArgs[key];
|
auto val = allArgs[key];
|
||||||
@ -973,59 +1045,6 @@ void installWebApi() {
|
|||||||
val["count_hit"] = (Json::UInt64)count_hit;
|
val["count_hit"] = (Json::UInt64)count_hit;
|
||||||
});
|
});
|
||||||
|
|
||||||
static auto addStreamPusherProxy = [](const string &schema,
|
|
||||||
const string &vhost,
|
|
||||||
const string &app,
|
|
||||||
const string &stream,
|
|
||||||
const string &url,
|
|
||||||
int retry_count,
|
|
||||||
int rtp_type,
|
|
||||||
float timeout_sec,
|
|
||||||
const function<void(const SockException &ex, const string &key)> &cb) {
|
|
||||||
auto key = getPusherKey(schema, vhost, app, stream, url);
|
|
||||||
auto src = MediaSource::find(schema, vhost, app, stream);
|
|
||||||
if (!src) {
|
|
||||||
cb(SockException(Err_other, "can not find the source stream"), key);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
lock_guard<recursive_mutex> lck(s_proxyPusherMapMtx);
|
|
||||||
if (s_proxyPusherMap.find(key) != s_proxyPusherMap.end()) {
|
|
||||||
//已经在推流了
|
|
||||||
cb(SockException(Err_success), key);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//添加推流代理
|
|
||||||
auto pusher = std::make_shared<PusherProxy>(src, retry_count);
|
|
||||||
s_proxyPusherMap[key] = pusher;
|
|
||||||
|
|
||||||
//指定RTP over TCP(播放rtsp时有效)
|
|
||||||
(*pusher)[Client::kRtpType] = rtp_type;
|
|
||||||
|
|
||||||
if (timeout_sec > 0.1) {
|
|
||||||
//推流握手超时时间
|
|
||||||
(*pusher)[Client::kTimeoutMS] = timeout_sec * 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
//开始推流,如果推流失败或者推流中止,将会自动重试若干次,默认一直重试
|
|
||||||
pusher->setPushCallbackOnce([cb, key, url](const SockException &ex) {
|
|
||||||
if (ex) {
|
|
||||||
WarnL << "Push " << url << " failed, key: " << key << ", err: " << ex;
|
|
||||||
lock_guard<recursive_mutex> lck(s_proxyPusherMapMtx);
|
|
||||||
s_proxyPusherMap.erase(key);
|
|
||||||
}
|
|
||||||
cb(ex, key);
|
|
||||||
});
|
|
||||||
|
|
||||||
//被主动关闭推流
|
|
||||||
pusher->setOnClose([key, url](const SockException &ex) {
|
|
||||||
WarnL << "Push " << url << " failed, key: " << key << ", err: " << ex;
|
|
||||||
lock_guard<recursive_mutex> lck(s_proxyPusherMapMtx);
|
|
||||||
s_proxyPusherMap.erase(key);
|
|
||||||
});
|
|
||||||
pusher->publish(url);
|
|
||||||
};
|
|
||||||
|
|
||||||
//动态添加rtsp/rtmp推流代理
|
//动态添加rtsp/rtmp推流代理
|
||||||
//测试url http://127.0.0.1/index/api/addStreamPusherProxy?schema=rtmp&vhost=__defaultVhost__&app=proxy&stream=0&dst_url=rtmp://127.0.0.1/live/obs
|
//测试url http://127.0.0.1/index/api/addStreamPusherProxy?schema=rtmp&vhost=__defaultVhost__&app=proxy&stream=0&dst_url=rtmp://127.0.0.1/live/obs
|
||||||
api_regist("/index/api/addStreamPusherProxy", [](API_ARGS_MAP_ASYNC) {
|
api_regist("/index/api/addStreamPusherProxy", [](API_ARGS_MAP_ASYNC) {
|
||||||
@ -1058,8 +1077,7 @@ void installWebApi() {
|
|||||||
api_regist("/index/api/delStreamPusherProxy", [](API_ARGS_MAP) {
|
api_regist("/index/api/delStreamPusherProxy", [](API_ARGS_MAP) {
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("key");
|
CHECK_ARGS("key");
|
||||||
lock_guard<recursive_mutex> lck(s_proxyPusherMapMtx);
|
val["data"]["flag"] = s_pusher_proxy.erase(allArgs["key"]) == 1;
|
||||||
val["data"]["flag"] = s_proxyPusherMap.erase(allArgs["key"]) == 1;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//动态添加rtsp/rtmp拉流代理
|
//动态添加rtsp/rtmp拉流代理
|
||||||
@ -1100,8 +1118,7 @@ void installWebApi() {
|
|||||||
api_regist("/index/api/delStreamProxy",[](API_ARGS_MAP){
|
api_regist("/index/api/delStreamProxy",[](API_ARGS_MAP){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("key");
|
CHECK_ARGS("key");
|
||||||
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
val["data"]["flag"] = s_player_proxy.erase(allArgs["key"]) == 1;
|
||||||
val["data"]["flag"] = s_proxyMap.erase(allArgs["key"]) == 1;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
static auto addFFmpegSource = [](const string &ffmpeg_cmd_key,
|
static auto addFFmpegSource = [](const string &ffmpeg_cmd_key,
|
||||||
@ -1112,25 +1129,21 @@ void installWebApi() {
|
|||||||
bool enable_mp4,
|
bool enable_mp4,
|
||||||
const function<void(const SockException &ex, const string &key)> &cb) {
|
const function<void(const SockException &ex, const string &key)> &cb) {
|
||||||
auto key = MD5(dst_url).hexdigest();
|
auto key = MD5(dst_url).hexdigest();
|
||||||
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
|
if (s_ffmpeg_src.find(key)) {
|
||||||
if (s_ffmpegMap.find(key) != s_ffmpegMap.end()) {
|
|
||||||
//已经在拉流了
|
//已经在拉流了
|
||||||
cb(SockException(Err_success), key);
|
cb(SockException(Err_success), key);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FFmpegSource::Ptr ffmpeg = std::make_shared<FFmpegSource>();
|
auto ffmpeg = s_ffmpeg_src.make(key);
|
||||||
s_ffmpegMap[key] = ffmpeg;
|
|
||||||
|
|
||||||
ffmpeg->setOnClose([key]() {
|
ffmpeg->setOnClose([key]() {
|
||||||
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
|
s_ffmpeg_src.erase(key);
|
||||||
s_ffmpegMap.erase(key);
|
|
||||||
});
|
});
|
||||||
ffmpeg->setupRecordFlag(enable_hls, enable_mp4);
|
ffmpeg->setupRecordFlag(enable_hls, enable_mp4);
|
||||||
ffmpeg->play(ffmpeg_cmd_key, src_url, dst_url, timeout_ms, [cb, key](const SockException &ex) {
|
ffmpeg->play(ffmpeg_cmd_key, src_url, dst_url, timeout_ms, [cb, key](const SockException &ex) {
|
||||||
if (ex) {
|
if (ex) {
|
||||||
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
|
s_ffmpeg_src.erase(key);
|
||||||
s_ffmpegMap.erase(key);
|
|
||||||
}
|
}
|
||||||
cb(ex, key);
|
cb(ex, key);
|
||||||
});
|
});
|
||||||
@ -1164,8 +1177,7 @@ void installWebApi() {
|
|||||||
api_regist("/index/api/delFFmpegSource",[](API_ARGS_MAP){
|
api_regist("/index/api/delFFmpegSource",[](API_ARGS_MAP){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("key");
|
CHECK_ARGS("key");
|
||||||
lock_guard<decltype(s_ffmpegMapMtx)> lck(s_ffmpegMapMtx);
|
val["data"]["flag"] = s_ffmpeg_src.erase(allArgs["key"]) == 1;
|
||||||
val["data"]["flag"] = s_ffmpegMap.erase(allArgs["key"]) == 1;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//新增http api下载可执行程序文件接口
|
//新增http api下载可执行程序文件接口
|
||||||
@ -1245,22 +1257,27 @@ void installWebApi() {
|
|||||||
api_regist("/index/api/connectRtpServer", [](API_ARGS_MAP_ASYNC) {
|
api_regist("/index/api/connectRtpServer", [](API_ARGS_MAP_ASYNC) {
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("stream_id", "dst_url", "dst_port");
|
CHECK_ARGS("stream_id", "dst_url", "dst_port");
|
||||||
connectRtpServer(
|
auto cb = [val, headerOut, invoker](const SockException &ex) mutable {
|
||||||
allArgs["stream_id"], allArgs["dst_url"], allArgs["dst_port"],
|
if (ex) {
|
||||||
[val, headerOut, invoker](const SockException &ex) mutable {
|
val["code"] = API::OtherFailed;
|
||||||
if (ex) {
|
val["msg"] = ex.what();
|
||||||
val["code"] = API::OtherFailed;
|
}
|
||||||
val["msg"] = ex.what();
|
invoker(200, headerOut, val.toStyledString());
|
||||||
}
|
};
|
||||||
invoker(200, headerOut, val.toStyledString());
|
|
||||||
});
|
auto server = s_rtp_server.find(allArgs["stream_id"]);
|
||||||
|
if (!server) {
|
||||||
|
cb(SockException(Err_other, "未找到rtp服务"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
server->connectToServer(allArgs["dst_url"], allArgs["dst_port"], cb);
|
||||||
});
|
});
|
||||||
|
|
||||||
api_regist("/index/api/closeRtpServer",[](API_ARGS_MAP){
|
api_regist("/index/api/closeRtpServer",[](API_ARGS_MAP){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("stream_id");
|
CHECK_ARGS("stream_id");
|
||||||
|
|
||||||
if(!closeRtpServer(allArgs["stream_id"])){
|
if(s_rtp_server.erase(allArgs["stream_id"]) == 0){
|
||||||
val["hit"] = 0;
|
val["hit"] = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -1271,19 +1288,18 @@ void installWebApi() {
|
|||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("stream_id", "ssrc");
|
CHECK_ARGS("stream_id", "ssrc");
|
||||||
|
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
auto server = s_rtp_server.find(allArgs["stream_id"]);
|
||||||
auto it = s_rtpServerMap.find(allArgs["stream_id"]);
|
if (!server) {
|
||||||
if (it == s_rtpServerMap.end()) {
|
|
||||||
throw ApiRetException("RtpServer not found by stream_id", API::NotFound);
|
throw ApiRetException("RtpServer not found by stream_id", API::NotFound);
|
||||||
}
|
}
|
||||||
it->second->updateSSRC(allArgs["ssrc"]);
|
server->updateSSRC(allArgs["ssrc"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
api_regist("/index/api/listRtpServer",[](API_ARGS_MAP){
|
api_regist("/index/api/listRtpServer",[](API_ARGS_MAP){
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
|
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
std::lock_guard<std::recursive_mutex> lck(s_rtp_server._mtx);
|
||||||
for (auto &pr : s_rtpServerMap) {
|
for (auto &pr : s_rtp_server._map) {
|
||||||
Value obj;
|
Value obj;
|
||||||
obj["stream_id"] = pr.first;
|
obj["stream_id"] = pr.first;
|
||||||
obj["port"] = pr.second->getPort();
|
obj["port"] = pr.second->getPort();
|
||||||
@ -1518,18 +1534,11 @@ void installWebApi() {
|
|||||||
api_regist("/index/api/getProxyPusherInfo", [](API_ARGS_MAP_ASYNC) {
|
api_regist("/index/api/getProxyPusherInfo", [](API_ARGS_MAP_ASYNC) {
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("key");
|
CHECK_ARGS("key");
|
||||||
decltype(s_proxyPusherMap.end()) it;
|
auto pusher = s_pusher_proxy.find(allArgs["key"]);
|
||||||
{
|
if (!pusher) {
|
||||||
lock_guard<recursive_mutex> lck(s_proxyPusherMapMtx);
|
|
||||||
it = s_proxyPusherMap.find(allArgs["key"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (it == s_proxyPusherMap.end()) {
|
|
||||||
throw ApiRetException("can not find pusher", API::NotFound);
|
throw ApiRetException("can not find pusher", API::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto pusher = it->second;
|
|
||||||
|
|
||||||
val["data"]["status"] = pusher->getStatus();
|
val["data"]["status"] = pusher->getStatus();
|
||||||
val["data"]["liveSecs"] = pusher->getLiveSecs();
|
val["data"]["liveSecs"] = pusher->getLiveSecs();
|
||||||
val["data"]["rePublishCount"] = pusher->getRePublishCount();
|
val["data"]["rePublishCount"] = pusher->getRePublishCount();
|
||||||
@ -1539,18 +1548,11 @@ void installWebApi() {
|
|||||||
api_regist("/index/api/getProxyInfo", [](API_ARGS_MAP_ASYNC) {
|
api_regist("/index/api/getProxyInfo", [](API_ARGS_MAP_ASYNC) {
|
||||||
CHECK_SECRET();
|
CHECK_SECRET();
|
||||||
CHECK_ARGS("key");
|
CHECK_ARGS("key");
|
||||||
decltype(s_proxyMap.end()) it;
|
auto proxy = s_player_proxy.find(allArgs["key"]);
|
||||||
{
|
if (!proxy) {
|
||||||
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
|
||||||
it = s_proxyMap.find(allArgs["key"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (it == s_proxyMap.end()) {
|
|
||||||
throw ApiRetException("can not find the proxy", API::NotFound);
|
throw ApiRetException("can not find the proxy", API::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto proxy = it->second;
|
|
||||||
|
|
||||||
val["data"]["status"] = proxy->getStatus();
|
val["data"]["status"] = proxy->getStatus();
|
||||||
val["data"]["liveSecs"] = proxy->getLiveSecs();
|
val["data"]["liveSecs"] = proxy->getLiveSecs();
|
||||||
val["data"]["rePullCount"] = proxy->getRePullCount();
|
val["data"]["rePullCount"] = proxy->getRePullCount();
|
||||||
@ -1926,31 +1928,12 @@ void installWebApi() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void unInstallWebApi(){
|
void unInstallWebApi(){
|
||||||
{
|
s_player_proxy.clear();
|
||||||
lock_guard<recursive_mutex> lck(s_proxyMapMtx);
|
s_ffmpeg_src.clear();
|
||||||
auto proxyMap(std::move(s_proxyMap));
|
s_pusher_proxy.clear();
|
||||||
proxyMap.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
lock_guard<recursive_mutex> lck(s_ffmpegMapMtx);
|
|
||||||
auto ffmpegMap(std::move(s_ffmpegMap));
|
|
||||||
ffmpegMap.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
lock_guard<recursive_mutex> lck(s_proxyPusherMapMtx);
|
|
||||||
auto proxyPusherMap(std::move(s_proxyPusherMap));
|
|
||||||
proxyPusherMap.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
#if defined(ENABLE_RTPPROXY)
|
#if defined(ENABLE_RTPPROXY)
|
||||||
RtpSelector::Instance().clear();
|
s_rtp_server.clear();
|
||||||
lock_guard<recursive_mutex> lck(s_rtpServerMapMtx);
|
|
||||||
auto rtpServerMap(std::move(s_rtpServerMap));
|
|
||||||
rtpServerMap.clear();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
NoticeCenter::Instance().delListener(&web_api_tag);
|
NoticeCenter::Instance().delListener(&web_api_tag);
|
||||||
}
|
}
|
||||||
|
@ -392,8 +392,8 @@ int start_main(int argc,char *argv[]) {
|
|||||||
#endif//defined(ENABLE_WEBRTC)
|
#endif//defined(ENABLE_WEBRTC)
|
||||||
|
|
||||||
#if defined(ENABLE_SRT)
|
#if defined(ENABLE_SRT)
|
||||||
// srt udp服务器
|
// srt udp服务器
|
||||||
if(srtPort) { srtSrv->start<SRT::SrtSession>(srtPort); }
|
if (srtPort) { srtSrv->start<SRT::SrtSession>(srtPort); }
|
||||||
#endif//defined(ENABLE_SRT)
|
#endif//defined(ENABLE_SRT)
|
||||||
|
|
||||||
} catch (std::exception &ex) {
|
} catch (std::exception &ex) {
|
||||||
|
Loading…
Reference in New Issue
Block a user