确保安全释放资源

This commit is contained in:
ziyue 2021-03-27 10:16:49 +08:00
parent cc960a324f
commit 272ab706b0
3 changed files with 27 additions and 15 deletions

View File

@ -1062,15 +1062,11 @@ void installWebApi() {
} }
headerOut["Content-Type"] = "text/plain"; headerOut["Content-Type"] = "text/plain";
headerOut["Access-Control-Allow-Origin"] = "*"; headerOut["Access-Control-Allow-Origin"] = "*";
auto poller = EventPollerPool::Instance().getFirstPoller(); auto rtc = WebRtcTransportImp::create(EventPollerPool::Instance().getPoller());
auto rtc = std::make_shared<WebRtcTransportImp>(poller);
poller->async([invoker, rtc, headerOut, src]() {
rtc->attach(src); rtc->attach(src);
auto sdp = rtc->GetLocalSdp(); invoker(200, headerOut, rtc->GetLocalSdp());
invoker(200, headerOut, sdp);
rtcs.emplace_back(rtc); rtcs.emplace_back(rtc);
}); });
});
#endif #endif
////////////以下是注册的Hook API//////////// ////////////以下是注册的Hook API////////////

View File

@ -2,12 +2,12 @@
#include <iostream> #include <iostream>
#include "Rtcp/Rtcp.h" #include "Rtcp/Rtcp.h"
WebRtcTransport::WebRtcTransport() { WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) {
dtls_transport_ = std::make_shared<RTC::DtlsTransport>(EventPollerPool::Instance().getFirstPoller(), this); dtls_transport_ = std::make_shared<RTC::DtlsTransport>(poller, this);
ice_server_ = std::make_shared<RTC::IceServer>(this, makeRandStr(4), makeRandStr(24)); ice_server_ = std::make_shared<RTC::IceServer>(this, makeRandStr(4), makeRandStr(24));
} }
WebRtcTransport::~WebRtcTransport() { void WebRtcTransport::onDestory(){
dtls_transport_ = nullptr; dtls_transport_ = nullptr;
ice_server_ = nullptr; ice_server_ = nullptr;
} }
@ -163,8 +163,15 @@ void WebRtcTransport::WritRtpPacket(char *buf, size_t len) {
} }
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
WebRtcTransportImp::Ptr WebRtcTransportImp::create(const EventPoller::Ptr &poller){
WebRtcTransportImp::Ptr ret(new WebRtcTransportImp(poller), [](WebRtcTransportImp *ptr){
ptr->onDestory();
delete ptr;
});
return ret;
}
WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) { WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) : WebRtcTransport(poller) {
_socket = Socket::createSocket(poller, false); _socket = Socket::createSocket(poller, false);
//随机端口,绑定全部网卡 //随机端口,绑定全部网卡
_socket->bindUdpSock(0); _socket->bindUdpSock(0);
@ -173,6 +180,10 @@ WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) {
}); });
} }
void WebRtcTransportImp::onDestory() {
WebRtcTransport::onDestory();
}
void WebRtcTransportImp::attach(const RtspMediaSource::Ptr &src) { void WebRtcTransportImp::attach(const RtspMediaSource::Ptr &src) {
assert(src); assert(src);
_src = src; _src = src;

View File

@ -11,8 +11,11 @@
class WebRtcTransport : public RTC::DtlsTransport::Listener, public RTC::IceServer::Listener { class WebRtcTransport : public RTC::DtlsTransport::Listener, public RTC::IceServer::Listener {
public: public:
using Ptr = std::shared_ptr<WebRtcTransport>; using Ptr = std::shared_ptr<WebRtcTransport>;
WebRtcTransport(); WebRtcTransport(const EventPoller::Ptr &poller);
virtual ~WebRtcTransport(); ~WebRtcTransport() override = default;
/// 销毁对象
virtual void onDestory();
/// 获取本地sdp /// 获取本地sdp
/// \return /// \return
@ -85,7 +88,7 @@ class WebRtcTransportImp : public WebRtcTransport, public std::enable_shared_fro
public: public:
using Ptr = std::shared_ptr<WebRtcTransportImp>; using Ptr = std::shared_ptr<WebRtcTransportImp>;
WebRtcTransportImp(const EventPoller::Ptr &poller); static Ptr create(const EventPoller::Ptr &poller);
~WebRtcTransportImp() override = default; ~WebRtcTransportImp() override = default;
void attach(const RtspMediaSource::Ptr &src); void attach(const RtspMediaSource::Ptr &src);
@ -97,6 +100,8 @@ protected:
uint16_t getPort() const override; uint16_t getPort() const override;
std::string getIP() const override; std::string getIP() const override;
void onDtlsConnected() override; void onDtlsConnected() override;
WebRtcTransportImp(const EventPoller::Ptr &poller);
void onDestory() override;
private: private:
Socket::Ptr _socket; Socket::Ptr _socket;