From 79b2aa6adc6e0ada78535b8c2fa93c26ebfd7d7c Mon Sep 17 00:00:00 2001 From: waken <33921191+mc373906408@users.noreply.github.com> Date: Tue, 5 Mar 2024 17:06:31 +0800 Subject: [PATCH] =?UTF-8?q?openRtpServer=E6=8E=A5=E5=8F=A3=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=8D=95=E8=A7=86=E9=A2=91=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=8A=A0=E5=BF=AB=E5=8D=95=E8=A7=86=E9=A2=91=E6=B5=81=E6=B3=A8?= =?UTF-8?q?=E5=86=8C=E9=80=9F=E5=BA=A6=20(#3342)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit only_audio -> only_track --- postman/ZLMediaKit.postman_collection.json | 12 ++++++------ server/WebApi.cpp | 18 ++++++++++++++---- server/WebApi.h | 2 +- src/Common/MediaSink.cpp | 2 +- src/Rtp/RtpProcess.cpp | 10 ++++++---- src/Rtp/RtpProcess.h | 7 ++++--- src/Rtp/RtpServer.cpp | 16 ++++++++-------- src/Rtp/RtpServer.h | 4 ++-- src/Rtp/RtpSession.cpp | 6 +++--- src/Rtp/RtpSession.h | 4 ++-- 10 files changed, 47 insertions(+), 34 deletions(-) diff --git a/postman/ZLMediaKit.postman_collection.json b/postman/ZLMediaKit.postman_collection.json index a6dc237f..e0d4c80d 100644 --- a/postman/ZLMediaKit.postman_collection.json +++ b/postman/ZLMediaKit.postman_collection.json @@ -1,11 +1,11 @@ { "info": { - "_postman_id": "08e3bc35-5318-4949-81bb-90d854706194", + "_postman_id": "08c77fc3-7670-428c-bde4-80c8cc9f389f", "name": "ZLMediaKit", "description": "媒体服务器", "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", "_exporter_id": "29185956", - "_collection_link": "https://lively-station-598157.postman.co/workspace/%E6%B5%81%E5%AA%92%E4%BD%93%E6%9C%8D%E5%8A%A1~1e119172-45b0-4ed6-b1fc-8a15d0e2d5f8/collection/29185956-08e3bc35-5318-4949-81bb-90d854706194?action=share&source=collection_link&creator=29185956" + "_collection_link": "https://lively-station-598157.postman.co/workspace/%E6%B5%81%E5%AA%92%E4%BD%93%E6%9C%8D%E5%8A%A1~1e119172-45b0-4ed6-b1fc-8a15d0e2d5f8/collection/29185956-08c77fc3-7670-428c-bde4-80c8cc9f389f?action=share&source=collection_link&creator=29185956" }, "item": [ { @@ -1470,9 +1470,9 @@ "disabled": true }, { - "key": "only_audio", + "key": "only_track", "value": "1", - "description": "是否为单音频track,用于语音对讲", + "description": "是否为单音频/单视频track,0:不设置,1:单音频,2:单视频", "disabled": true }, { @@ -1523,9 +1523,9 @@ "description": "该端口绑定的流id\n" }, { - "key": "only_audio", + "key": "only_track", "value": "0", - "description": "是否为单音频track,用于语音对讲", + "description": "是否为单音频/单视频track,0:不设置,1:单音频,2:单视频", "disabled": true }, { diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 8b53e948..4ca72759 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -415,7 +415,7 @@ Value makeMediaSourceJson(MediaSource &media){ } #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, bool only_audio, 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 lck(s_rtpServerMapMtx); if (s_rtpServerMap.find(stream_id) != s_rtpServerMap.end()) { //为了防止RtpProcess所有权限混乱的问题,不允许重复添加相同的stream_id @@ -423,7 +423,7 @@ uint16_t openRtpServer(uint16_t local_port, const string &stream_id, int tcp_mod } RtpServer::Ptr server = std::make_shared(); - server->start(local_port, stream_id, (RtpServer::TcpMode)tcp_mode, local_ip.c_str(), re_use_port, ssrc, only_audio, 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]() { //设置rtp超时移除事件 lock_guard lck(s_rtpServerMapMtx); @@ -1198,12 +1198,17 @@ void installWebApi() { //兼容老版本请求,新版本去除enable_tcp参数并新增tcp_mode参数 tcp_mode = 1; } + auto only_track = allArgs["only_track"].as(); + if (allArgs["only_audio"].as()) { + // 兼容老版本请求,新版本去除only_audio参数并新增only_track参数 + only_track = 1; + } std::string local_ip = "::"; if (!allArgs["local_ip"].empty()) { local_ip = allArgs["local_ip"]; } auto port = openRtpServer(allArgs["port"], stream_id, tcp_mode, local_ip, allArgs["re_use_port"].as(), - allArgs["ssrc"].as(), allArgs["only_audio"].as()); + allArgs["ssrc"].as(), only_track); if (port == 0) { throw InvalidArgsException("该stream_id已存在"); } @@ -1220,11 +1225,16 @@ void installWebApi() { // 兼容老版本请求,新版本去除enable_tcp参数并新增tcp_mode参数 tcp_mode = 1; } + auto only_track = allArgs["only_track"].as(); + if (allArgs["only_audio"].as()) { + // 兼容老版本请求,新版本去除only_audio参数并新增only_track参数 + only_track = 1; + } std::string local_ip = "::"; if (!allArgs["local_ip"].empty()) { local_ip = allArgs["local_ip"]; } - auto port = openRtpServer(allArgs["port"], stream_id, tcp_mode, local_ip, true, 0, allArgs["only_audio"].as(),true); + auto port = openRtpServer(allArgs["port"], stream_id, tcp_mode, local_ip, true, 0, only_track,true); if (port == 0) { throw InvalidArgsException("该stream_id已存在"); } diff --git a/server/WebApi.h b/server/WebApi.h index 43516463..eff4c93f 100755 --- a/server/WebApi.h +++ b/server/WebApi.h @@ -233,7 +233,7 @@ void installWebApi(); void unInstallWebApi(); #if defined(ENABLE_RTPPROXY) -uint16_t openRtpServer(uint16_t local_port, const std::string &stream_id, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc, bool only_audio, bool multiplex=false); +uint16_t openRtpServer(uint16_t local_port, const std::string &stream_id, int tcp_mode, const std::string &local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex=false); void connectRtpServer(const std::string &stream_id, const std::string &dst_url, uint16_t dst_port, const std::function &cb); bool closeRtpServer(const std::string &stream_id); #endif diff --git a/src/Common/MediaSink.cpp b/src/Common/MediaSink.cpp index 837024af..41b4f415 100644 --- a/src/Common/MediaSink.cpp +++ b/src/Common/MediaSink.cpp @@ -133,7 +133,7 @@ void MediaSink::checkTrackIfReady() { } GET_CONFIG(uint32_t, kMaxAddTrackMS, General::kWaitAddTrackMS); - if (_track_map.size() == 1 && _ticker.elapsedTime() > kMaxAddTrackMS) { + if (_track_map.size() == 1 && (_ticker.elapsedTime() > kMaxAddTrackMS || !_enable_audio)) { // 如果只有一个Track,那么在该Track添加后,我们最多还等待若干时间(可能后面还会添加Track) emitAllTrackReady(); return; diff --git a/src/Rtp/RtpProcess.cpp b/src/Rtp/RtpProcess.cpp index 1d95f6b0..b0161d91 100644 --- a/src/Rtp/RtpProcess.cpp +++ b/src/Rtp/RtpProcess.cpp @@ -199,8 +199,8 @@ void RtpProcess::setStopCheckRtp(bool is_check){ } } -void RtpProcess::setOnlyAudio(bool only_audio){ - _only_audio = only_audio; +void RtpProcess::setOnlyTrack(OnlyTrack only_track) { + _only_track = only_track; } void RtpProcess::onDetach() { @@ -259,8 +259,10 @@ void RtpProcess::emitOnPublish() { if (!option.stream_replace.empty()) { RtpSelector::Instance().addStreamReplace(strong_self->_media_info.stream, option.stream_replace); } - if (strong_self->_only_audio) { - strong_self->_muxer->setOnlyAudio(); + switch (strong_self->_only_track) { + case kOnlyAudio: strong_self->_muxer->setOnlyAudio(); break; + case kOnlyVideo: strong_self->_muxer->enableAudio(false); break; + default: break; } strong_self->_muxer->setMediaListener(strong_self); strong_self->doCachedFunc(); diff --git a/src/Rtp/RtpProcess.h b/src/Rtp/RtpProcess.h index b9d5009b..b680936c 100644 --- a/src/Rtp/RtpProcess.h +++ b/src/Rtp/RtpProcess.h @@ -24,6 +24,7 @@ public: friend class RtpProcessHelper; RtpProcess(const std::string &stream_id); ~RtpProcess(); + enum OnlyTrack { kAll = 0, kOnlyAudio = 1, kOnlyVideo = 2 }; /** * 输入rtp @@ -58,10 +59,10 @@ public: void setStopCheckRtp(bool is_check=false); /** - * 设置为单track,单音频时可以加快媒体注册速度 + * 设置为单track,单音频/单视频时可以加快媒体注册速度 * 请在inputRtp前调用此方法,否则可能会是空操作 */ - void setOnlyAudio(bool only_audio); + void setOnlyTrack(OnlyTrack only_track); /** * flush输出缓存 @@ -93,7 +94,7 @@ private: void doCachedFunc(); private: - bool _only_audio = false; + OnlyTrack _only_track = kAll; std::string _auth_err; uint64_t _dts = 0; uint64_t _total_bytes = 0; diff --git a/src/Rtp/RtpServer.cpp b/src/Rtp/RtpServer.cpp index 560792b3..84809488 100644 --- a/src/Rtp/RtpServer.cpp +++ b/src/Rtp/RtpServer.cpp @@ -42,12 +42,12 @@ public: } } - void setRtpServerInfo(uint16_t local_port,RtpServer::TcpMode mode,bool re_use_port,uint32_t ssrc, bool only_audio) { + void setRtpServerInfo(uint16_t local_port, RtpServer::TcpMode mode, bool re_use_port, uint32_t ssrc, int only_track) { _local_port = local_port; _tcp_mode = mode; _re_use_port = re_use_port; _ssrc = ssrc; - _only_audio = only_audio; + _only_track = only_track; } void setOnDetach(function cb) { @@ -61,7 +61,7 @@ public: void onRecvRtp(const Socket::Ptr &sock, const Buffer::Ptr &buf, struct sockaddr *addr) { if (!_process) { _process = RtpSelector::Instance().getProcess(_stream_id, true); - _process->setOnlyAudio(_only_audio); + _process->setOnlyTrack((RtpProcess::OnlyTrack)_only_track); _process->setOnDetach(std::move(_on_detach)); cancelDelayTask(); } @@ -142,7 +142,7 @@ private: private: bool _re_use_port = false; - bool _only_audio = false; + int _only_track = 0; uint16_t _local_port = 0; uint32_t _ssrc = 0; RtpServer::TcpMode _tcp_mode = RtpServer::NONE; @@ -156,7 +156,7 @@ private: EventPoller::DelayTask::Ptr _delay_task; }; -void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_mode, const char *local_ip, bool re_use_port, uint32_t ssrc, bool only_audio, bool multiplex) { +void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_mode, const char *local_ip, bool re_use_port, uint32_t ssrc, int only_track, bool multiplex) { //创建udp服务器 Socket::Ptr rtp_socket = Socket::createSocket(nullptr, true); Socket::Ptr rtcp_socket = Socket::createSocket(nullptr, true); @@ -184,7 +184,7 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ tcp_server = std::make_shared(rtp_socket->getPoller()); (*tcp_server)[RtpSession::kStreamID] = stream_id; (*tcp_server)[RtpSession::kSSRC] = ssrc; - (*tcp_server)[RtpSession::kOnlyAudio] = only_audio; + (*tcp_server)[RtpSession::kOnlyTrack] = only_track; if (tcp_mode == PASSIVE) { tcp_server->start(local_port, local_ip); } else if (stream_id.empty()) { @@ -201,7 +201,7 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ //指定了流id,那么一个端口一个流(不管是否包含多个ssrc的多个流,绑定rtp源后,会筛选掉ip端口不匹配的流) helper = std::make_shared(std::move(rtcp_socket), stream_id); helper->startRtcp(); - helper->setRtpServerInfo(local_port, tcp_mode, re_use_port, ssrc, only_audio); + helper->setRtpServerInfo(local_port, tcp_mode, re_use_port, ssrc, only_track); bool bind_peer_addr = false; auto ssrc_ptr = std::make_shared(ssrc); _ssrc = ssrc_ptr; @@ -223,7 +223,7 @@ void RtpServer::start(uint16_t local_port, const string &stream_id, TcpMode tcp_ } else { //单端口多线程接收多个流,根据ssrc区分流 udp_server = std::make_shared(rtp_socket->getPoller()); - (*udp_server)[RtpSession::kOnlyAudio] = only_audio; + (*udp_server)[RtpSession::kOnlyTrack] = only_track; (*udp_server)[RtpSession::kUdpRecvBuffer] = udpRecvSocketBuffer; udp_server->start(local_port, local_ip); rtp_socket = nullptr; diff --git a/src/Rtp/RtpServer.h b/src/Rtp/RtpServer.h index 74250d6f..3654828e 100644 --- a/src/Rtp/RtpServer.h +++ b/src/Rtp/RtpServer.h @@ -44,7 +44,7 @@ public: * @param multiplex 多路复用 */ void start(uint16_t local_port, const std::string &stream_id = "", TcpMode tcp_mode = PASSIVE, - const char *local_ip = "::", bool re_use_port = true, uint32_t ssrc = 0, bool only_audio = false, bool multiplex = false); + const char *local_ip = "::", bool re_use_port = true, uint32_t ssrc = 0, int only_track = 0, bool multiplex = false); /** * 连接到tcp服务(tcp主动模式) @@ -81,7 +81,7 @@ protected: std::shared_ptr _rtcp_helper; std::function _on_cleanup; - bool _only_audio = false; + int _only_track = 0; //用于tcp主动模式 TcpMode _tcp_mode = NONE; }; diff --git a/src/Rtp/RtpSession.cpp b/src/Rtp/RtpSession.cpp index 41c76287..95807637 100644 --- a/src/Rtp/RtpSession.cpp +++ b/src/Rtp/RtpSession.cpp @@ -23,7 +23,7 @@ namespace mediakit{ const string RtpSession::kStreamID = "stream_id"; const string RtpSession::kSSRC = "ssrc"; -const string RtpSession::kOnlyAudio = "only_audio"; +const string RtpSession::kOnlyTrack = "only_track"; const string RtpSession::kUdpRecvBuffer = "udp_recv_socket_buffer"; void RtpSession::attachServer(const Server &server) { @@ -33,7 +33,7 @@ void RtpSession::attachServer(const Server &server) { void RtpSession::setParams(mINI &ini) { _stream_id = ini[kStreamID]; _ssrc = ini[kSSRC]; - _only_audio = ini[kOnlyAudio]; + _only_track = ini[kOnlyTrack]; int udp_socket_buffer = ini[kUdpRecvBuffer]; if (_is_udp) { // 设置udp socket读缓存 @@ -125,7 +125,7 @@ void RtpSession::onRtpPacket(const char *data, size_t len) { _delay_close = true; return; } - _process->setOnlyAudio(_only_audio); + _process->setOnlyTrack((RtpProcess::OnlyTrack)_only_track); _process->setDelegate(static_pointer_cast(shared_from_this())); } try { diff --git a/src/Rtp/RtpSession.h b/src/Rtp/RtpSession.h index de2fa75f..2bff4f5f 100644 --- a/src/Rtp/RtpSession.h +++ b/src/Rtp/RtpSession.h @@ -24,7 +24,7 @@ class RtpSession : public toolkit::Session, public RtpSplitter, public MediaSour public: static const std::string kStreamID; static const std::string kSSRC; - static const std::string kOnlyAudio; + static const std::string kOnlyTrack; static const std::string kUdpRecvBuffer; RtpSession(const toolkit::Socket::Ptr &sock); @@ -52,7 +52,7 @@ private: bool _is_udp = false; bool _search_rtp = false; bool _search_rtp_finished = false; - bool _only_audio = false; + int _only_track = 0; uint32_t _ssrc = 0; toolkit::Ticker _ticker; std::string _stream_id;