From 87e1209d0aa767ef658507a5e06a04280a2f6fd4 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 7 Apr 2021 20:33:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9F=90=E4=BA=9B=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E4=B8=8B=EF=BC=8Cudp=20connect=E5=90=8Esendto?= =?UTF-8?q?=E6=97=B6=E6=8C=87=E5=AE=9A=E5=AF=B9=E7=AB=AF=E5=9C=B0=E5=9D=80?= =?UTF-8?q?=E5=8F=91=E9=80=81=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webrtc/WebRtcTransport.cpp | 29 +++++++++++++++++++++++++---- webrtc/WebRtcTransport.h | 17 ++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/webrtc/WebRtcTransport.cpp b/webrtc/WebRtcTransport.cpp index a229171d..9f1a615f 100644 --- a/webrtc/WebRtcTransport.cpp +++ b/webrtc/WebRtcTransport.cpp @@ -120,6 +120,10 @@ const RtcSession& WebRtcTransport::getSdp(SdpType type) const{ } } +RTC::TransportTuple* WebRtcTransport::getSelectedTuple() const{ + return _ice_server->GetSelectedTuple(); +} + string getFingerprint(const string &algorithm_str, const std::shared_ptr &transport){ auto algorithm = RTC::DtlsTransport::GetFingerprintAlgorithm(algorithm_str); for (auto &finger_prints : transport->GetLocalFingerprints()) { @@ -614,10 +618,27 @@ string WebRtcTransportImp::getOriginUrl(MediaSource &sender) const { } std::shared_ptr WebRtcTransportImp::getOriginSock(MediaSource &sender) const { - return const_cast(this)->_socket; + return const_cast(this)->shared_from_this(); } -void WebRtcTransportImp::OnIceServerSelectedTuple(const RTC::IceServer *iceServer, RTC::TransportTuple *tuple) { - WebRtcTransport::OnIceServerSelectedTuple(iceServer, tuple); - _socket->setSendPeerAddr(tuple); +///////////////////////////////////////////////////////////////////////////////////////////// + +string WebRtcTransportImp::get_local_ip() { + return getSdp(SdpType::answer).media[0].candidate[0].address; } + +uint16_t WebRtcTransportImp::get_local_port() { + return _socket->get_local_port(); +} + +string WebRtcTransportImp::get_peer_ip() { + return SockUtil::inet_ntoa(((struct sockaddr_in *) getSelectedTuple())->sin_addr); +} + +uint16_t WebRtcTransportImp::get_peer_port() { + return ntohs(((struct sockaddr_in *) getSelectedTuple())->sin_port); +} + +string WebRtcTransportImp::getIdentifier() const { + return StrPrinter << this; +} \ No newline at end of file diff --git a/webrtc/WebRtcTransport.h b/webrtc/WebRtcTransport.h index 6c2ea591..e418cdf8 100644 --- a/webrtc/WebRtcTransport.h +++ b/webrtc/WebRtcTransport.h @@ -93,6 +93,7 @@ protected: protected: const RtcSession& getSdp(SdpType type) const; + RTC::TransportTuple* getSelectedTuple() const; private: void onSendSockData(const char *buf, size_t len, bool flush = true); @@ -110,7 +111,7 @@ private: class RtpReceiverImp; -class WebRtcTransportImp : public WebRtcTransport, public MediaSourceEvent, public std::enable_shared_from_this{ +class WebRtcTransportImp : public WebRtcTransport, public MediaSourceEvent, public SockInfo, public std::enable_shared_from_this{ public: using Ptr = std::shared_ptr; ~WebRtcTransportImp() override; @@ -139,8 +140,6 @@ 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; @@ -153,6 +152,18 @@ protected: // 获取媒体源客户端相关信息 std::shared_ptr getOriginSock(MediaSource &sender) const override; + ///////SockInfo override/////// + //获取本机ip + string get_local_ip() override; + //获取本机端口号 + uint16_t get_local_port() override; + //获取对方ip + string get_peer_ip() override; + //获取对方端口号 + uint16_t get_peer_port() override; + //获取标识符 + string getIdentifier() const override; + private: WebRtcTransportImp(const EventPoller::Ptr &poller); void onCreate() override;