From 0c5fa36e4df836662dc807854d0dc9a967cbf9e1 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Wed, 7 Apr 2021 18:18:27 +0800 Subject: [PATCH] =?UTF-8?q?rtc=E6=8E=A8=E6=B5=81=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AA=92=E4=BD=93=E4=BA=8B=E4=BB=B6=E7=9B=B8=E5=85=B3=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 1 + webrtc/WebRtcTransport.cpp | 32 ++++++++++++++++++++++++++++++++ webrtc/WebRtcTransport.h | 16 +++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index b1cc6935..5fadf3bb 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1137,6 +1137,7 @@ void installWebApi() { auto push_src = std::make_shared(info._vhost, info._app, info._streamid); push_src->setProtocolTranslation(enableHls, enableMP4); auto rtc = WebRtcTransportImp::create(EventPollerPool::Instance().getPoller()); + push_src->setListener(rtc); rtc->attach(push_src, false); val["sdp"] = rtc->getAnswerSdp(offer_sdp); val["type"] = "answer"; diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index 6a9a925a..9930d8f4 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -559,3 +559,35 @@ void WebRtcTransportImp::onShutdown(const SockException &ex){ _self = nullptr; } +///////////////////////////////////////////////////////////////////////////////////////////// + +bool WebRtcTransportImp::close(MediaSource &sender, bool force) { + //此回调在其他线程触发 + if(!_push_src || (!force && _push_src->totalReaderCount())){ + return false; + } + string err = StrPrinter << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force; + onShutdown(SockException(Err_shutdown,err)); + return true; +} + +int WebRtcTransportImp::totalReaderCount(MediaSource &sender) { + return _push_src ? _push_src->totalReaderCount() : sender.readerCount(); +} + +MediaOriginType WebRtcTransportImp::getOriginType(MediaSource &sender) const { + return MediaOriginType::rtc_push; +} + +string WebRtcTransportImp::getOriginUrl(MediaSource &sender) const { + return ""; +} + +std::shared_ptr WebRtcTransportImp::getOriginSock(MediaSource &sender) const { + return const_cast(this)->_socket; +} + +void WebRtcTransportImp::OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) { + WebRtcTransport::OnIceServerSelectedTuple(iceServer, tuple); + _socket->setSendPeerAddr(tuple); +} diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index c42317ab..1cd63583 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -110,7 +110,7 @@ private: class RtpReceiverImp; -class WebRtcTransportImp : public WebRtcTransport, public std::enable_shared_from_this{ +class WebRtcTransportImp : public WebRtcTransport, public MediaSourceEvent, public std::enable_shared_from_this{ public: using Ptr = std::shared_ptr; ~WebRtcTransportImp() override; @@ -139,6 +139,20 @@ protected: void onRtcp(const char *buf, size_t len) override; void onShutdown(const SockException &ex) override; + void OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) override; + + ///////MediaSourceEvent override/////// + // 关闭 + bool close(MediaSource &sender, bool force) override; + // 播放总人数 + int totalReaderCount(MediaSource &sender) override; + // 获取媒体源类型 + MediaOriginType getOriginType(MediaSource &sender) const override; + // 获取媒体源url或者文件路径 + string getOriginUrl(MediaSource &sender) const override; + // 获取媒体源客户端相关信息 + std::shared_ptr getOriginSock(MediaSource &sender) const override; + private: WebRtcTransportImp(const EventPoller::Ptr &poller); void onCreate() override;