diff options
author | Roxie Linden <roxie@lindenlab.com> | 2023-09-19 10:14:29 -0700 |
---|---|---|
committer | Roxie Linden <roxie@lindenlab.com> | 2024-02-22 23:11:34 -0800 |
commit | 8859312b1f0d975793c6c2a3d7b23b9880c657c5 (patch) | |
tree | 2ed9a2b5430a842e46e5ec78cf6c081e0b7d3b0f /indra/llwebrtc | |
parent | 639e63faab239b88d41c8e2c755509e9dcdc6251 (diff) |
add datachannel support
Diffstat (limited to 'indra/llwebrtc')
-rw-r--r-- | indra/llwebrtc/llwebrtc.cpp | 50 | ||||
-rw-r--r-- | indra/llwebrtc/llwebrtc.h | 16 | ||||
-rw-r--r-- | indra/llwebrtc/llwebrtc_impl.h | 20 |
3 files changed, 84 insertions, 2 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<LLWebRTCDataObserver *>::iterator it = + std::find(mDataObserverList.begin(), mDataObserverList.end(), observer); + if (it != mDataObserverList.end()) + { + mDataObserverList.erase(it); + } +} + rtc::RefCountedObject<LLWebRTCImpl> *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<webrtc::PeerConnectionInterface> mPeerConnection; + + std::vector<LLWebRTCDataObserver *> mDataObserverList; + rtc::scoped_refptr<webrtc::DataChannelInterface> mDataChannel; }; } |