播放器(reader)setGetInfoCB时统一使用Session对象 (#4195)
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
Linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Windows / build (push) Has been cancelled

Co-authored-by: custompal <chenrengen@gosuncn.com>
This commit is contained in:
custompal 2025-03-21 15:22:48 +08:00 committed by GitHub
parent fd89e0d801
commit f0204ea3b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 54 additions and 54 deletions

View File

@ -1009,9 +1009,9 @@ void installWebApi() {
},
[](toolkit::Any &&info) -> toolkit::Any {
auto obj = std::make_shared<Value>();
auto &sock = info.get<SockInfo>();
fillSockInfo(*obj, &sock);
(*obj)["typeid"] = toolkit::demangle(typeid(sock).name());
auto &session = info.get<Session>();
fillSockInfo(*obj, &session);
(*obj)["typeid"] = toolkit::demangle(typeid(session).name());
toolkit::Any ret;
ret.set(obj);
return ret;

View File

@ -315,37 +315,6 @@ static bool emitHlsPlayed(const Parser &parser, const MediaInfo &media_info, con
return flag;
}
class SockInfoImp : public SockInfo{
public:
using Ptr = std::shared_ptr<SockInfoImp>;
string get_local_ip() override {
return _local_ip;
}
uint16_t get_local_port() override {
return _local_port;
}
string get_peer_ip() override {
return _peer_ip;
}
uint16_t get_peer_port() override {
return _peer_port;
}
string getIdentifier() const override {
return _identifier;
}
string _local_ip;
string _peer_ip;
string _identifier;
uint16_t _local_port;
uint16_t _peer_port;
};
/**
* http客户端是否有权限访问文件的逻辑步骤
* 1http请求头查找cookie3
@ -415,17 +384,18 @@ static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo
bool is_hls = media_info.schema == HLS_SCHEMA || media_info.schema == HLS_FMP4_SCHEMA;
SockInfoImp::Ptr info = std::make_shared<SockInfoImp>();
info->_identifier = sender.getIdentifier();
info->_peer_ip = sender.get_peer_ip();
info->_peer_port = sender.get_peer_port();
info->_local_ip = sender.get_local_ip();
info->_local_port = sender.get_local_port();
weak_ptr<Session> weak_session = static_pointer_cast<Session>(sender.shared_from_this());
// 该用户从来未获取过cookie这个时候我们广播是否允许该用户访问该http目录 [AUTO-TRANSLATED:8f4b3dd2]
// This user has never obtained a cookie, at this time we broadcast whether to allow this user to access this http directory
HttpSession::HttpAccessPathInvoker accessPathInvoker = [callback, uid, path, is_dir, is_hls, media_info, info]
HttpSession::HttpAccessPathInvoker accessPathInvoker = [callback, uid, path, is_dir, is_hls, media_info, weak_session]
(const string &err_msg, const string &cookie_path_in, int life_second) {
auto strong_session = weak_session.lock();
if (!strong_session) {
// http客户端已经断开不需要回复 [AUTO-TRANSLATED:9a252e21]
// The http client has disconnected and does not need to reply
return;
}
HttpServerCookie::Ptr cookie;
if (life_second) {
// 本次鉴权设置了有效期我们把鉴权结果缓存在cookie中 [AUTO-TRANSLATED:5a12f48e]
@ -447,7 +417,7 @@ static void canAccessPath(Session &sender, const Parser &parser, const MediaInfo
if (is_hls) {
// hls相关信息 [AUTO-TRANSLATED:37893a71]
// hls related information
attach->_hls_data = std::make_shared<HlsCookieData>(media_info, info);
attach->_hls_data = std::make_shared<HlsCookieData>(media_info, strong_session);
}
toolkit::Any any;
any.set(std::move(attach));

View File

@ -394,7 +394,7 @@ bool HttpSession::checkLiveStreamFMP4(const function<void()> &cb) {
_fmp4_reader = fmp4_src->getRing()->attach(getPoller());
_fmp4_reader->setGetInfoCB([weak_self]() {
Any ret;
ret.set(static_pointer_cast<SockInfo>(weak_self.lock()));
ret.set(static_pointer_cast<Session>(weak_self.lock()));
return ret;
});
_fmp4_reader->setDetachCB([weak_self]() {
@ -444,7 +444,7 @@ bool HttpSession::checkLiveStreamTS(const function<void()> &cb) {
_ts_reader = ts_src->getRing()->attach(getPoller());
_ts_reader->setGetInfoCB([weak_self]() {
Any ret;
ret.set(static_pointer_cast<SockInfo>(weak_self.lock()));
ret.set(static_pointer_cast<Session>(weak_self.lock()));
return ret;
});
_ts_reader->setDetachCB([weak_self]() {

View File

@ -15,9 +15,37 @@ using namespace toolkit;
namespace mediakit {
HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockInfo> &sock_info) {
class SockInfoImp : public SockInfo {
public:
using Ptr = std::shared_ptr<SockInfoImp>;
std::string get_local_ip() override { return _local_ip; }
uint16_t get_local_port() override { return _local_port; }
std::string get_peer_ip() override { return _peer_ip; }
uint16_t get_peer_port() override { return _peer_port; }
std::string getIdentifier() const override { return _identifier; }
std::string _local_ip;
std::string _peer_ip;
std::string _identifier;
uint16_t _local_port;
uint16_t _peer_port;
};
HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<Session> &session) {
_info = info;
auto sock_info = std::make_shared<SockInfoImp>();
sock_info->_identifier = session->getIdentifier();
sock_info->_peer_ip = session->get_peer_ip();
sock_info->_peer_port = session->get_peer_port();
sock_info->_local_ip = session->get_local_ip();
sock_info->_local_port = session->get_local_port();
_sock_info = sock_info;
_session = session;
_added = std::make_shared<bool>(false);
addReaderCount();
}
@ -34,10 +62,10 @@ void HlsCookieData::addReaderCount() {
// HlsMediaSource has been destroyed
*added = false;
});
auto info = _sock_info;
_ring_reader->setGetInfoCB([info]() {
std::weak_ptr<Session> weak_session = _session;
_ring_reader->setGetInfoCB([weak_session]() {
Any ret;
ret.set(info);
ret.set(std::static_pointer_cast<Session>(weak_session.lock()));
return ret;
});
}

View File

@ -14,6 +14,7 @@
#include "Common/MediaSource.h"
#include "Util/TimeTicker.h"
#include "Util/RingBuffer.h"
#include "Network/Session.h"
#include <atomic>
namespace mediakit {
@ -89,7 +90,7 @@ class HlsCookieData {
public:
using Ptr = std::shared_ptr<HlsCookieData>;
HlsCookieData(const MediaInfo &info, const std::shared_ptr<toolkit::SockInfo> &sock_info);
HlsCookieData(const MediaInfo &info, const std::shared_ptr<toolkit::Session> &session);
~HlsCookieData();
void addByteUsage(size_t bytes);
@ -106,6 +107,7 @@ private:
toolkit::Ticker _ticker;
std::weak_ptr<HlsMediaSource> _src;
std::shared_ptr<toolkit::SockInfo> _sock_info;
std::weak_ptr<toolkit::Session> _session;
HlsMediaSource::RingType::RingReader::Ptr _ring_reader;
};

View File

@ -49,7 +49,7 @@ void FlvMuxer::start(const EventPoller::Ptr &poller, const RtmpMediaSource::Ptr
_ring_reader = media->getRing()->attach(poller);
_ring_reader->setGetInfoCB([weak_self]() {
Any ret;
ret.set(dynamic_pointer_cast<SockInfo>(weak_self.lock()));
ret.set(dynamic_pointer_cast<Session>(weak_self.lock()));
return ret;
});
_ring_reader->setDetachCB([weak_self]() {

View File

@ -308,7 +308,7 @@ void RtmpSession::sendPlayResponse(const string &err, const RtmpMediaSource::Ptr
weak_ptr<RtmpSession> weak_self = static_pointer_cast<RtmpSession>(shared_from_this());
_ring_reader->setGetInfoCB([weak_self]() {
Any ret;
ret.set(static_pointer_cast<SockInfo>(weak_self.lock()));
ret.set(static_pointer_cast<Session>(weak_self.lock()));
return ret;
});
_ring_reader->setReadCB([weak_self](const RtmpMediaSource::RingDataType &pkt) {

View File

@ -863,7 +863,7 @@ void RtspSession::handleReq_Play(const Parser &parser) {
_play_reader = play_src->getRing()->attach(getPoller(), use_gop);
_play_reader->setGetInfoCB([weak_self]() {
Any ret;
ret.set(static_pointer_cast<SockInfo>(weak_self.lock()));
ret.set(static_pointer_cast<Session>(weak_self.lock()));
return ret;
});
_play_reader->setDetachCB([weak_self]() {

View File

@ -253,7 +253,7 @@ void SrtTransportImp::doPlay() {
weak_ptr<Session> weak_session = strong_self->getSession();
strong_self->_ts_reader->setGetInfoCB([weak_session]() {
Any ret;
ret.set(static_pointer_cast<SockInfo>(weak_session.lock()));
ret.set(static_pointer_cast<Session>(weak_session.lock()));
return ret;
});
strong_self->_ts_reader->setDetachCB([weak_self]() {

View File

@ -205,7 +205,7 @@ void WebRtcPlayer::onStartWebRTC() {
weak_ptr<Session> weak_session = static_pointer_cast<Session>(getSession());
_reader->setGetInfoCB([weak_session]() {
Any ret;
ret.set(static_pointer_cast<SockInfo>(weak_session.lock()));
ret.set(static_pointer_cast<Session>(weak_session.lock()));
return ret;
});
_reader->setReadCB([weak_self](const RtspMediaSource::RingDataType &pkt) {