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;