rtsp/http类型播放协议支持自定义header

This commit is contained in:
xia-chu
2025-09-10 21:51:26 +08:00
parent 88b422db08
commit a8f5cedd84
8 changed files with 40 additions and 15 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -36,6 +36,7 @@ void HlsPlayer::fetchIndexFile() {
}
setCompleteTimeout((*this)[Client::kTimeoutMS].as<int>());
setMethod("GET");
addCustomHeader(this);
sendRequest(_play_url);
}

View File

@@ -25,6 +25,7 @@ void TsPlayer::play(const string &url) {
setHeaderTimeout((*this)[Client::kTimeoutMS].as<int>());
setBodyTimeout((*this)[Client::kMediaTimeoutMS].as<int>());
setMethod("GET");
addCustomHeader(this);
sendRequest(url);
}

View File

@@ -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 <typename Type>
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<PlayerBase>;

View File

@@ -26,6 +26,7 @@ void FlvPlayer::play(const string &url) {
setHeaderTimeout((*this)[Client::kTimeoutMS].as<int>());
setBodyTimeout((*this)[Client::kMediaTimeoutMS].as<int>());
setMethod("GET");
addCustomHeader(this);
sendRequest(url);
}

View File

@@ -91,22 +91,22 @@ void RtspPlayer::play(const string &strUrl) {
_speed = (*this)[Client::kRtspSpeed].as<float>();
DebugL << url._url << " " << (url._user.size() ? url._user : "null") << " " << (url._passwd.size() ? url._passwd : "null") << " " << _rtp_type;
weak_ptr<RtspPlayer> weakSelf = static_pointer_cast<RtspPlayer>(shared_from_this());
weak_ptr<RtspPlayer> weak_self = static_pointer_cast<RtspPlayer>(shared_from_this());
float playTimeOutSec = (*this)[Client::kTimeoutMS].as<int>() / 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);
}

View File

@@ -203,6 +203,8 @@ private:
// 统计rtp并发送rtcp [AUTO-TRANSLATED:0ac2b665]
// Statistics rtp and send rtcp
std::vector<RtcpContext::Ptr> _rtcp_context;
// 用户自定义rtsp头
StrCaseMap _custom_header;
};
} /* namespace mediakit */