确保安全释放资源

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,14 +1062,10 @@ void installWebApi() {
}
headerOut["Content-Type"] = "text/plain";
headerOut["Access-Control-Allow-Origin"] = "*";
auto poller = EventPollerPool::Instance().getFirstPoller();
auto rtc = std::make_shared<WebRtcTransportImp>(poller);
poller->async([invoker, rtc, headerOut, src]() {
rtc->attach(src);
auto sdp = rtc->GetLocalSdp();
invoker(200, headerOut, sdp);
rtcs.emplace_back(rtc);
});
auto rtc = WebRtcTransportImp::create(EventPollerPool::Instance().getPoller());
rtc->attach(src);
invoker(200, headerOut, rtc->GetLocalSdp());
rtcs.emplace_back(rtc);
});
#endif

View File

@ -2,12 +2,12 @@
#include <iostream>
#include "Rtcp/Rtcp.h"
WebRtcTransport::WebRtcTransport() {
dtls_transport_ = std::make_shared<RTC::DtlsTransport>(EventPollerPool::Instance().getFirstPoller(), this);
WebRtcTransport::WebRtcTransport(const EventPoller::Ptr &poller) {
dtls_transport_ = std::make_shared<RTC::DtlsTransport>(poller, this);
ice_server_ = std::make_shared<RTC::IceServer>(this, makeRandStr(4), makeRandStr(24));
}
WebRtcTransport::~WebRtcTransport() {
void WebRtcTransport::onDestory(){
dtls_transport_ = 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->bindUdpSock(0);
@ -173,6 +180,10 @@ WebRtcTransportImp::WebRtcTransportImp(const EventPoller::Ptr &poller) {
});
}
void WebRtcTransportImp::onDestory() {
WebRtcTransport::onDestory();
}
void WebRtcTransportImp::attach(const RtspMediaSource::Ptr &src) {
assert(src);
_src = src;

View File

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