From a8f5cedd8469b887971eeeab7c8e4963f0aff76e Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Wed, 10 Sep 2025 21:51:26 +0800 Subject: [PATCH] =?UTF-8?q?rtsp/http=E7=B1=BB=E5=9E=8B=E6=92=AD=E6=94=BE?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?header?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Common/config.cpp | 1 + src/Common/config.h | 2 ++ src/Http/HlsPlayer.cpp | 1 + src/Http/TsPlayer.cpp | 1 + src/Player/PlayerBase.h | 13 +++++++++++++ src/Rtmp/FlvPlayer.cpp | 1 + src/Rtsp/RtspPlayer.cpp | 34 +++++++++++++++++++--------------- src/Rtsp/RtspPlayer.h | 2 ++ 8 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 2b2f716f..6726f843 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -400,6 +400,7 @@ const string kProxyUrl = "proxy_url"; const string kRtspSpeed = "rtsp_speed"; const string kLatency = "latency"; const string kPassPhrase = "passPhrase"; +const string kCustomHeader = "custom_header"; } // namespace Client } // namespace mediakit diff --git a/src/Common/config.h b/src/Common/config.h index 7fb85d4a..c6c0b2db 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -630,6 +630,8 @@ extern const std::string kRtspSpeed; extern const std::string kLatency; // Set SRT PassPhrase extern const std::string kPassPhrase; +// 自定义rtsp/http头 +extern const std::string kCustomHeader; } // namespace Client } // namespace mediakit diff --git a/src/Http/HlsPlayer.cpp b/src/Http/HlsPlayer.cpp index 57721834..38e4e263 100644 --- a/src/Http/HlsPlayer.cpp +++ b/src/Http/HlsPlayer.cpp @@ -36,6 +36,7 @@ void HlsPlayer::fetchIndexFile() { } setCompleteTimeout((*this)[Client::kTimeoutMS].as()); setMethod("GET"); + addCustomHeader(this); sendRequest(_play_url); } diff --git a/src/Http/TsPlayer.cpp b/src/Http/TsPlayer.cpp index 7774b6fb..f8fdb0ab 100644 --- a/src/Http/TsPlayer.cpp +++ b/src/Http/TsPlayer.cpp @@ -25,6 +25,7 @@ void TsPlayer::play(const string &url) { setHeaderTimeout((*this)[Client::kTimeoutMS].as()); setBodyTimeout((*this)[Client::kMediaTimeoutMS].as()); setMethod("GET"); + addCustomHeader(this); sendRequest(url); } diff --git a/src/Player/PlayerBase.h b/src/Player/PlayerBase.h index 1f1dc504..bd4623b1 100644 --- a/src/Player/PlayerBase.h +++ b/src/Player/PlayerBase.h @@ -21,9 +21,22 @@ #include "Common/MediaSink.h" #include "Extension/Frame.h" #include "Extension/Track.h" +#include "Common/config.h" +#include "Common/Parser.h" namespace mediakit { +template +void addCustomHeader(Type *c) { + auto &custom_header = (*c)[Client::kCustomHeader]; + if (!custom_header.empty()) { + auto args = mediakit::Parser::parseArgs(custom_header); + for (auto &pr : args) { + c->addHeader(pr.first, pr.second); + } + } +} + class PlayerBase : public TrackSource, public toolkit::mINI { public: using Ptr = std::shared_ptr; diff --git a/src/Rtmp/FlvPlayer.cpp b/src/Rtmp/FlvPlayer.cpp index c5e968f6..f890ca9c 100644 --- a/src/Rtmp/FlvPlayer.cpp +++ b/src/Rtmp/FlvPlayer.cpp @@ -26,6 +26,7 @@ void FlvPlayer::play(const string &url) { setHeaderTimeout((*this)[Client::kTimeoutMS].as()); setBodyTimeout((*this)[Client::kMediaTimeoutMS].as()); setMethod("GET"); + addCustomHeader(this); sendRequest(url); } diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index fba173ea..60d4e78d 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -91,22 +91,22 @@ void RtspPlayer::play(const string &strUrl) { _speed = (*this)[Client::kRtspSpeed].as(); DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " " << (url._passwd.size() ? url._passwd : "null") << " " << _rtp_type; - weak_ptr weakSelf = static_pointer_cast(shared_from_this()); + weak_ptr weak_self = static_pointer_cast(shared_from_this()); float playTimeOutSec = (*this)[Client::kTimeoutMS].as() / 1000.0f; - _play_check_timer.reset(new Timer( - playTimeOutSec, - [weakSelf]() { - auto strongSelf = weakSelf.lock(); - if (!strongSelf) { - return false; - } - strongSelf->onPlayResult_l(SockException(Err_timeout, "play rtsp timeout"), false); - return false; - }, - getPoller())); + _play_check_timer.reset(new Timer(playTimeOutSec,[weak_self]() { + if (auto strong_self = weak_self.lock()) { + strong_self->onPlayResult_l(SockException(Err_timeout, "play rtsp timeout"), false); + } + return false; + }, getPoller())); - if (!(*this)[Client::kNetAdapter].empty()) { - setNetAdapter((*this)[Client::kNetAdapter]); + auto &adapter = (*this)[Client::kNetAdapter]; + if (!adapter.empty()) { + setNetAdapter(std::move(adapter)); + } + auto &custom_header = (*this)[Client::kCustomHeader]; + if (!custom_header.empty()) { + _custom_header = mediakit::Parser::parseArgs(custom_header); } startConnect(url._host, url._port, playTimeOutSec); } @@ -632,7 +632,11 @@ void RtspPlayer::sendRtspRequest(const string &cmd, const string &url, const std key = val; } } - + if (cmd == "PLAY") { + for (auto &pr : _custom_header) { + header_map.emplace(pr.first, pr.second); + } + } sendRtspRequest(cmd, url, header_map); } diff --git a/src/Rtsp/RtspPlayer.h b/src/Rtsp/RtspPlayer.h index c24d4806..8e269729 100644 --- a/src/Rtsp/RtspPlayer.h +++ b/src/Rtsp/RtspPlayer.h @@ -203,6 +203,8 @@ private: // 统计rtp并发送rtcp [AUTO-TRANSLATED:0ac2b665] // Statistics rtp and send rtcp std::vector _rtcp_context; + // 用户自定义rtsp头 + StrCaseMap _custom_header; }; } /* namespace mediakit */