Skip to content

Commit

Permalink
Merge branch 'master' into new-github-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
murat-dogan committed Jun 30, 2024
2 parents 9f511a9 + b1c04e7 commit 4154f85
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 137 deletions.
37 changes: 17 additions & 20 deletions API.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# API

## PeerConnection Class
## PeerConnection Class

**Constructor**

let pc = new PeerConnection(peerName[,options])
- peerName `<string>` Peer name to use for logs etc..
- options `<Object>` WebRTC Config Options

- peerName `<string>` Peer name to use for logs etc..
- options `<Object>` WebRTC Config Options

```
export interface RtcConfig {
iceServers: (string | IceServer)[];
Expand Down Expand Up @@ -57,6 +59,7 @@ Close Peer Connection & Clear all callbacks
**setRemoteDescription: (sdp: string, type: DescriptionType) => void**

Set Remote Description

```
export const enum DescriptionType {
Unspec = 'Unspec',
Expand All @@ -72,29 +75,21 @@ Add remote candidate info
**createDataChannel: (label: string, config?: DataChannelInitConfig) => DataChannel**

Create new data-channel
* label `<string>` Data channel name
* config `<Object>` Data channel options

- label `<string>` Data channel name
- config `<Object>` Data channel options

```
export interface DataChannelInitConfig {
protocol?: string;
negotiated?: boolean;
id?: number;
ordered?: boolean;
maxPacketLifeTime?: number;
maxRetransmits?: number;
// Deprecated, use ordered, maxPacketLifeTime, and maxRetransmits
reliability?: {
type?: ReliabilityType;
unordered?: boolean;
rexmit?: number;
}
}
export const enum ReliabilityType {
Reliable = 0, Rexmit = 1, Timed = 2
unordered?: boolean; // Reliability
maxPacketLifeTime?: number; // Reliability
maxRetransmits?: number; // Reliability
}
```

**state: () => string**

Get current state
Expand All @@ -110,6 +105,7 @@ Get current gathering state
**onLocalDescription: (cb: (sdp: string, type: DescriptionType) => void) => void**

Local Description Callback

```
export const enum DescriptionType {
Unspec = 'Unspec',
Expand Down Expand Up @@ -153,6 +149,7 @@ Get rtt stat
**getSelectedCandidatePair: () => { local: SelectedCandidateInfo, remote: SelectedCandidateInfo }**

Get info about selected candidate pair

```
export interface SelectedCandidateInfo {
address: string;
Expand All @@ -162,7 +159,7 @@ export interface SelectedCandidateInfo {
}
```

## DataChannel Class
## DataChannel Class

> You can create a new Datachannel instance by calling `PeerConnection.createDataChannel` function.
Expand Down
22 changes: 4 additions & 18 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,13 @@ export enum DescriptionType {
Rollback = 'rollback',
}

export const enum ReliabilityType {
Reliable = 0,
Rexmit = 1,
Timed = 2,
}

export interface DataChannelInitConfig {
protocol?: string;
negotiated?: boolean;
id?: number;
ordered?: boolean;
maxPacketLifeTime?: number;
maxRetransmits?: number;

// Deprecated, use ordered, maxPacketLifeTime, and maxRetransmits
reliability?: {
type?: ReliabilityType;
unordered?: boolean;
rexmit?: number;
};
unordered?: boolean; // Reliability
maxPacketLifeTime?: number; // Reliability
maxRetransmits?: number; // Reliability
}

export interface SelectedCandidateInfo {
Expand All @@ -113,8 +100,7 @@ export const enum Direction {
}

export class RtcpReceivingSession {
requestBitrate(bitRate: number): void;
requestKeyframe(): boolean;
//
}

export class Audio {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-datachannel",
"version": "0.10.0-dev",
"version": "0.10.0",
"description": "libdatachannel node bindings",
"type": "module",
"types": "./lib/index.d.ts",
Expand Down
24 changes: 1 addition & 23 deletions src/media-rtcpreceivingsession-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Napi::Object RtcpReceivingSessionWrapper::Init(Napi::Env env, Napi::Object expor
env,
"RtcpReceivingSession",
{
Napi::ObjectWrap<RtcpReceivingSessionWrapper>::InstanceMethod("requestBitrate", &RtcpReceivingSessionWrapper::requestBitrate),
Napi::ObjectWrap<RtcpReceivingSessionWrapper>::InstanceMethod("requestKeyframe", &RtcpReceivingSessionWrapper::requestKeyframe),
// Instance Methods
});

constructor = Napi::Persistent(func);
Expand All @@ -39,24 +38,3 @@ std::shared_ptr<rtc::RtcpReceivingSession> RtcpReceivingSessionWrapper::getSessi
{
return mSessionPtr;
}

void RtcpReceivingSessionWrapper::requestBitrate(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
int length = info.Length();

if (length < 1 || !info[0].IsNumber())
{
Napi::TypeError::New(env, "We expect (Number) as param").ThrowAsJavaScriptException();
return;
}

unsigned int bitrate = static_cast<uint32_t>(info[0].As<Napi::Number>().ToNumber());
mSessionPtr->requestBitrate(bitrate);
}

Napi::Value RtcpReceivingSessionWrapper::requestKeyframe(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
return Napi::Boolean::New(env, mSessionPtr->requestKeyframe());
}
7 changes: 2 additions & 5 deletions src/media-rtcpreceivingsession-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ class RtcpReceivingSessionWrapper : public Napi::ObjectWrap<RtcpReceivingSession
std::shared_ptr<rtc::RtcpReceivingSession> getSessionInstance();

// Functions
void requestBitrate(const Napi::CallbackInfo &info);
Napi::Value requestKeyframe(const Napi::CallbackInfo &info);

// Callbacks


private:
static std::unordered_set<RtcpReceivingSessionWrapper*> instances;
static std::unordered_set<RtcpReceivingSessionWrapper *> instances;
std::shared_ptr<rtc::RtcpReceivingSession> mSessionPtr = nullptr;
};

#endif // MEDIA_RTCPRECEIVINGSESSION_WRAPPER_H
#endif // MEDIA_RTCPRECEIVINGSESSION_WRAPPER_H
76 changes: 6 additions & 70 deletions src/peer-connection-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ PeerConnectionWrapper::PeerConnectionWrapper(const Napi::CallbackInfo &info) : N
PLOG_DEBUG << "Peer Connection created";

// State change callback must be set to trigger cleanup on close
mOnStateChangeCallback = std::make_unique<ThreadSafeCallback>(Napi::Function::New(info.Env(), [](const Napi::CallbackInfo&){}));
mOnStateChangeCallback = std::make_unique<ThreadSafeCallback>(Napi::Function::New(info.Env(), [](const Napi::CallbackInfo &) {}));

instances.insert(this);
}
Expand Down Expand Up @@ -503,81 +503,17 @@ Napi::Value PeerConnectionWrapper::createDataChannel(const Napi::CallbackInfo &i
init.id = uint16_t(initConfig.Get("id").As<Napi::Number>().Uint32Value());
}

// Deprecated reliability object, kept for retro-compatibility
if (!initConfig.Get("reliability").IsUndefined())
// Reliability.unordered parameter
if (!initConfig.Get("unordered").IsUndefined())
{
if (!initConfig.Get("reliability").IsObject())
if (!initConfig.Get("unordered").IsBoolean())
{
Napi::TypeError::New(env, "Wrong DataChannel Init Config (reliability)").ThrowAsJavaScriptException();
Napi::TypeError::New(env, "Wrong DataChannel Init Config (unordered)").ThrowAsJavaScriptException();
return info.Env().Null();
}

Napi::Object reliability = initConfig.Get("reliability").As<Napi::Object>();
if (!reliability.Get("type").IsUndefined())
{
if (!reliability.Get("type").IsNumber())
{
Napi::TypeError::New(env, "Wrong Reliability Config (type)").ThrowAsJavaScriptException();
return info.Env().Null();
}
switch (reliability.Get("type").As<Napi::Number>().Uint32Value())
{
case 0:
init.reliability.type = rtc::Reliability::Type::Reliable;
break;
case 1:
init.reliability.type = rtc::Reliability::Type::Rexmit;
break;
case 2:
init.reliability.type = rtc::Reliability::Type::Timed;
break;
default:
Napi::TypeError::New(env, "Unknown DataChannel Reliability Type").ThrowAsJavaScriptException();
return info.Env().Null();
}
}

if (!reliability.Get("unordered").IsUndefined())
{
if (!reliability.Get("unordered").IsBoolean())
{
Napi::TypeError::New(env, "Wrong reliability Config (unordered)").ThrowAsJavaScriptException();
return info.Env().Null();
}
init.reliability.unordered = reliability.Get("unordered").As<Napi::Boolean>();
}

if (!reliability.Get("rexmit").IsUndefined())
{
if (!reliability.Get("rexmit").IsNumber())
{
Napi::TypeError::New(env, "Wrong Reliability Config (rexmit)").ThrowAsJavaScriptException();
return info.Env().Null();
}
switch (reliability.Get("type").As<Napi::Number>().Uint32Value())
{
case 1:
init.reliability.rexmit = int(reliability.Get("rexmit").As<Napi::Number>().ToNumber().Uint32Value());
break;
case 2:
init.reliability.rexmit = std::chrono::milliseconds(reliability.Get("rexmit").As<Napi::Number>().Int32Value());
break;
}
}
init.reliability.unordered = !initConfig.Get("unordered").As<Napi::Boolean>();
}

// Reliability parameters
if (!initConfig.Get("ordered").IsUndefined())
{
if (!initConfig.Get("ordered").IsBoolean())
{
Napi::TypeError::New(env, "Wrong DataChannel Init Config (ordered)").ThrowAsJavaScriptException();
return info.Env().Null();
}
init.reliability.unordered = !initConfig.Get("ordered").As<Napi::Boolean>();
}


if (!initConfig.Get("maxPacketLifeTime").IsUndefined() && !initConfig.Get("maxPacketLifeTime").IsNull() &&
!initConfig.Get("maxRetransmits").IsUndefined() && !initConfig.Get("maxRetransmits").IsNull())
{
Expand Down

0 comments on commit 4154f85

Please sign in to comment.