From 60f587f5bbb5744bf2faa920703b4fbee73ffeab Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 7 Jun 2024 15:14:41 +0100 Subject: [PATCH] feat: support current remote description Retrieves the current remote description from libdatachannel to get the current state of the connection negotiation instead of returning the previously set remote description. Depends on: https://github.com/paullouisageneau/libdatachannel/pull/1204 --- lib/index.d.ts | 1 + polyfill/RTCPeerConnection.js | 2 +- src/peer-connection-wrapper.cpp | 19 +++++++++++++++++++ src/peer-connection-wrapper.h | 1 + 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 717c313..fe175c4 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -284,6 +284,7 @@ export class PeerConnection { setRemoteDescription(sdp: string, type: DescriptionType): void; localDescription(): { type: string; sdp: string } | null; remoteDescription(): { type: string; sdp: string } | null; + currentRemoteDescription(): { type: string; sdp: string } | null; addRemoteCandidate(candidate: string, mid: string): void; createDataChannel(label: string, config?: DataChannelInitConfig): DataChannel; addTrack(media: Video | Audio): Track; diff --git a/polyfill/RTCPeerConnection.js b/polyfill/RTCPeerConnection.js index be9913b..8e5749f 100644 --- a/polyfill/RTCPeerConnection.js +++ b/polyfill/RTCPeerConnection.js @@ -165,7 +165,7 @@ export default class _RTCPeerConnection extends EventTarget { } get currentRemoteDescription() { - return new RTCSessionDescription(this.#peerConnection.remoteDescription()); + return new RTCSessionDescription(this.#peerConnection.currentRemoteDescription()); } get localDescription() { diff --git a/src/peer-connection-wrapper.cpp b/src/peer-connection-wrapper.cpp index cbce8cd..0aed1e4 100644 --- a/src/peer-connection-wrapper.cpp +++ b/src/peer-connection-wrapper.cpp @@ -41,6 +41,7 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports) InstanceMethod("setRemoteDescription", &PeerConnectionWrapper::setRemoteDescription), InstanceMethod("localDescription", &PeerConnectionWrapper::localDescription), InstanceMethod("remoteDescription", &PeerConnectionWrapper::remoteDescription), + InstanceMethod("currentRemoteDescription", &PeerConnectionWrapper::currentRemoteDescription), InstanceMethod("addRemoteCandidate", &PeerConnectionWrapper::addRemoteCandidate), InstanceMethod("createDataChannel", &PeerConnectionWrapper::createDataChannel), InstanceMethod("addTrack", &PeerConnectionWrapper::addTrack), @@ -412,6 +413,24 @@ Napi::Value PeerConnectionWrapper::remoteDescription(const Napi::CallbackInfo &i return obj; } +Napi::Value PeerConnectionWrapper::currentRemoteDescription(const Napi::CallbackInfo &info) +{ + Napi::Env env = info.Env(); + + std::optional desc = mRtcPeerConnPtr ? mRtcPeerConnPtr->currentRemoteDescription() : std::nullopt; + + // Return JS null if no description + if (!desc.has_value()) + { + return env.Null(); + } + + Napi::Object obj = Napi::Object::New(env); + obj.Set("type", desc->typeString()); + obj.Set("sdp", desc.value()); + return obj; +} + void PeerConnectionWrapper::addRemoteCandidate(const Napi::CallbackInfo &info) { PLOG_DEBUG << "addRemoteCandidate() called"; diff --git a/src/peer-connection-wrapper.h b/src/peer-connection-wrapper.h index 5896e30..b363f09 100644 --- a/src/peer-connection-wrapper.h +++ b/src/peer-connection-wrapper.h @@ -25,6 +25,7 @@ class PeerConnectionWrapper : public Napi::ObjectWrap void setRemoteDescription(const Napi::CallbackInfo &info); Napi::Value localDescription(const Napi::CallbackInfo &info); Napi::Value remoteDescription(const Napi::CallbackInfo &info); + Napi::Value currentRemoteDescription(const Napi::CallbackInfo &info); void addRemoteCandidate(const Napi::CallbackInfo &info); Napi::Value createDataChannel(const Napi::CallbackInfo &info); Napi::Value addTrack(const Napi::CallbackInfo &info);