Skip to content

Commit

Permalink
feat: support current remote description
Browse files Browse the repository at this point in the history
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: paullouisageneau/libdatachannel#1204
  • Loading branch information
achingbrain committed Jun 7, 2024
1 parent d59c64c commit 60f587f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion polyfill/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
19 changes: 19 additions & 0 deletions src/peer-connection-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<rtc::Description> 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";
Expand Down
1 change: 1 addition & 0 deletions src/peer-connection-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PeerConnectionWrapper : public Napi::ObjectWrap<PeerConnectionWrapper>
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);
Expand Down

0 comments on commit 60f587f

Please sign in to comment.