From fd8119c550bea19bbcf26e5426f259010f54c43e Mon Sep 17 00:00:00 2001 From: Roxie Linden Date: Tue, 19 Sep 2023 10:14:29 -0700 Subject: add datachannel support --- indra/llwebrtc/llwebrtc.cpp | 50 ++++++++++++++++++++++++++++++++++++++++- indra/llwebrtc/llwebrtc.h | 16 +++++++++++++ indra/llwebrtc/llwebrtc_impl.h | 20 ++++++++++++++++- indra/newview/llvoicewebrtc.cpp | 9 ++++++++ indra/newview/llvoicewebrtc.h | 13 +++++++++-- 5 files changed, 104 insertions(+), 4 deletions(-) diff --git a/indra/llwebrtc/llwebrtc.cpp b/indra/llwebrtc/llwebrtc.cpp index 96be2c1f0b..ac5870eab3 100644 --- a/indra/llwebrtc/llwebrtc.cpp +++ b/indra/llwebrtc/llwebrtc.cpp @@ -314,7 +314,22 @@ bool LLWebRTCImpl::initializeConnectionThreaded() RTC_LOG(LS_INFO) << __FUNCTION__ << " " << mPeerConnection->signaling_state(); - return mPeerConnection != nullptr; + webrtc::DataChannelInit init; + init.ordered = true; + init.reliable = true; + auto data_channel_or_error = mPeerConnection->CreateDataChannelOrError("SLData", &init); + if (data_channel_or_error.ok()) + { + mDataChannel = std::move(data_channel_or_error.value()); + } + else + { + shutdownConnection(); + return false; + } + mDataChannel->RegisterObserver(this); + + return true; } void LLWebRTCImpl::shutdownConnection() @@ -574,9 +589,42 @@ void LLWebRTCImpl::OnSetLocalDescriptionComplete(webrtc::RTCError error) } } +// +// DataChannelObserver implementation +// +void LLWebRTCImpl::OnMessage(const webrtc::DataBuffer& buffer) +{ + std::string data((const char*)buffer.data.cdata(), buffer.size()); + for (auto &observer : mDataObserverList) + { + observer->OnDataReceived(data, buffer.binary); + } +} + +void LLWebRTCImpl::sendData(const std::string& data, bool binary) +{ + rtc::CopyOnWriteBuffer cowBuffer(data.data(), data.length()); + webrtc::DataBuffer buffer(cowBuffer, binary); + mDataChannel->Send(buffer); +} + +void LLWebRTCImpl::setDataObserver(LLWebRTCDataObserver* observer) { mDataObserverList.emplace_back(observer); } + +void LLWebRTCImpl::unsetDataObserver(LLWebRTCDataObserver* observer) +{ + std::vector::iterator it = + std::find(mDataObserverList.begin(), mDataObserverList.end(), observer); + if (it != mDataObserverList.end()) + { + mDataObserverList.erase(it); + } +} + rtc::RefCountedObject *gWebRTCImpl = nullptr; LLWebRTCDeviceInterface *getDeviceInterface() { return gWebRTCImpl; } LLWebRTCSignalInterface *getSignalingInterface() { return gWebRTCImpl; } +LLWebRTCDataInterface *getDataInterface() { return gWebRTCImpl; } + void init() { diff --git a/indra/llwebrtc/llwebrtc.h b/indra/llwebrtc/llwebrtc.h index ca558add01..a6e754684e 100644 --- a/indra/llwebrtc/llwebrtc.h +++ b/indra/llwebrtc/llwebrtc.h @@ -96,6 +96,21 @@ class LLWebRTCAudioInterface virtual void setSpeakerVolume(float volume) = 0; // volume between 0.0 and 1.0 }; +class LLWebRTCDataObserver +{ +public: + virtual void OnDataReceived(const std::string& data, bool binary) = 0; +}; + +class LLWebRTCDataInterface +{ +public: + virtual void sendData(const std::string& data, bool binary=false) = 0; + + virtual void setDataObserver(LLWebRTCDataObserver *observer) = 0; + virtual void unsetDataObserver(LLWebRTCDataObserver *observer) = 0; +}; + class LLWebRTCSignalingObserver { public: @@ -124,6 +139,7 @@ class LLWebRTCSignalInterface LLSYMEXPORT LLWebRTCDeviceInterface* getDeviceInterface(); LLSYMEXPORT LLWebRTCSignalInterface* getSignalingInterface(); +LLSYMEXPORT LLWebRTCDataInterface* getDataInterface(); } #endif // LLWEBRTC_H diff --git a/indra/llwebrtc/llwebrtc_impl.h b/indra/llwebrtc/llwebrtc_impl.h index 3f9b06cae7..1ad117c7f3 100644 --- a/indra/llwebrtc/llwebrtc_impl.h +++ b/indra/llwebrtc/llwebrtc_impl.h @@ -66,11 +66,13 @@ namespace llwebrtc class LLWebRTCImpl : public LLWebRTCDeviceInterface, public LLWebRTCSignalInterface, public LLWebRTCAudioInterface, + public LLWebRTCDataInterface, public webrtc::AudioDeviceDataObserver, public webrtc::PeerConnectionObserver, public webrtc::CreateSessionDescriptionObserver, public webrtc::SetRemoteDescriptionObserverInterface, - public webrtc::SetLocalDescriptionObserverInterface + public webrtc::SetLocalDescriptionObserverInterface, + public webrtc::DataChannelObserver { public: @@ -126,6 +128,13 @@ class LLWebRTCImpl : public LLWebRTCDeviceInterface, // void setMute(bool mute) override; void setSpeakerVolume(float folume) override; // range 0.0-1.0 + + // + // LLWebRTCDataInterface + // + void sendData(const std::string& data, bool binary=false) override; + void setDataObserver(LLWebRTCDataObserver *observer) override; + void unsetDataObserver(LLWebRTCDataObserver *observer) override; // // PeerConnectionObserver implementation. @@ -158,6 +167,12 @@ class LLWebRTCImpl : public LLWebRTCDeviceInterface, // SetLocalDescriptionObserverInterface implementation. // void OnSetLocalDescriptionComplete(webrtc::RTCError error) override; + + // + // DataChannelObserver implementation. + // + void OnStateChange() override {} + void OnMessage(const webrtc::DataBuffer& buffer) override; protected: @@ -184,6 +199,9 @@ class LLWebRTCImpl : public LLWebRTCDeviceInterface, bool mAnswerReceived; rtc::scoped_refptr mPeerConnection; + + std::vector mDataObserverList; + rtc::scoped_refptr mDataChannel; }; } diff --git a/indra/newview/llvoicewebrtc.cpp b/indra/newview/llvoicewebrtc.cpp index 66bed5f0b5..9013de67f5 100644 --- a/indra/newview/llvoicewebrtc.cpp +++ b/indra/newview/llvoicewebrtc.cpp @@ -361,6 +361,9 @@ void LLWebRTCVoiceClient::init(LLPumpIO *pump) mWebRTCSignalingInterface = llwebrtc::getSignalingInterface(); mWebRTCSignalingInterface->setSignalingObserver(this); + + mWebRTCDataInterface = llwebrtc::getDataInterface(); + mWebRTCDataInterface->setDataObserver(this); } void LLWebRTCVoiceClient::terminate() @@ -2707,6 +2710,12 @@ void LLWebRTCVoiceClient::OnAudioEstablished(llwebrtc::LLWebRTCAudioInterface * setVoiceControlStateUnless(VOICE_STATE_SESSION_ESTABLISHED, VOICE_STATE_SESSION_RETRY); } +void LLWebRTCVoiceClient::OnDataReceived(const std::string& data, bool binary) +{ + +} + + void LLWebRTCVoiceClient::OnRenegotiationNeeded() { LL_INFOS("Voice") << "On Renegotiation Needed." << LL_ENDL; diff --git a/indra/newview/llvoicewebrtc.h b/indra/newview/llvoicewebrtc.h index eb3893fe2b..0d6988e1ef 100644 --- a/indra/newview/llvoicewebrtc.h +++ b/indra/newview/llvoicewebrtc.h @@ -59,7 +59,8 @@ class LLWebRTCVoiceClient : public LLSingleton, virtual public LLVoiceModuleInterface, virtual public LLVoiceEffectInterface, public llwebrtc::LLWebRTCDevicesObserver, - public llwebrtc::LLWebRTCSignalingObserver + public llwebrtc::LLWebRTCSignalingObserver, + public llwebrtc::LLWebRTCDataObserver { LLSINGLETON_C11(LLWebRTCVoiceClient); LOG_CLASS(LLWebRTCVoiceClient); @@ -254,6 +255,13 @@ public: void OnOfferAvailable(const std::string &sdp) override; void OnRenegotiationNeeded() override; void OnAudioEstablished(llwebrtc::LLWebRTCAudioInterface *audio_interface) override; + //@} + + ///////////////////////// + /// @name Data Notification + /// LLWebRTCDataObserver + //@{ + void OnDataReceived(const std::string& data, bool binary) override; //@} void processIceUpdates(); @@ -761,7 +769,8 @@ private: llwebrtc::LLWebRTCDeviceInterface *mWebRTCDeviceInterface; llwebrtc::LLWebRTCSignalInterface *mWebRTCSignalingInterface; - llwebrtc::LLWebRTCAudioInterface *mWebRTCAudioInterface; + llwebrtc::LLWebRTCAudioInterface *mWebRTCAudioInterface; + llwebrtc::LLWebRTCDataInterface *mWebRTCDataInterface; LLVoiceDeviceList mCaptureDevices; LLVoiceDeviceList mRenderDevices; -- cgit v1.2.3