Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close without destroy #239

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
cmake_policy(SET CMP0042 NEW)

project(node_datachannel VERSION 0.6.0)
project(node_datachannel VERSION 0.7.0)

# -Dnapi_build_version=8
add_definitions(-DNAPI_VERSION=8)
Expand Down
7 changes: 7 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import('jest').Config} */
const config = {
verbose: true,
testPathIgnorePatterns: ['<rootDir>/node_modules/', 'multiple-run.test'],
};

module.exports = config;
1 change: 0 additions & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ export class WebSocketServer {
export class PeerConnection {
constructor(peerName: string, config: RtcConfig);
close(): void;
destroy(): void;
setLocalDescription(type?: DescriptionType): void;
setRemoteDescription(sdp: string, type: DescriptionType): void;
localDescription(): { type: string; sdp: string } | null;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.6.0",
"version": "0.7.0-dev",
"description": "libdatachannel node bindings",
"type": "module",
"exports": {
Expand Down
1 change: 0 additions & 1 deletion polyfill/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ export default class _RTCPeerConnection extends EventTarget {
});

this.#peerConnection.close();
this.#peerConnection.destroy();
}

createAnswer() {
Expand Down
16 changes: 15 additions & 1 deletion src/data-channel-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ void DataChannelWrapper::CloseAll()
inst->doClose();
}

void DataChannelWrapper::CleanupAll()
{
PLOG_DEBUG << "CleanupAll() called";
auto copy(instances);
for (auto inst : copy)
inst->doCleanup();
}

Napi::Object DataChannelWrapper::Init(Napi::Env env, Napi::Object exports)
{
Napi::HandleScope scope(env);
Expand Down Expand Up @@ -78,11 +86,15 @@ void DataChannelWrapper::doClose()
}

mOnOpenCallback.reset();
mOnClosedCallback.reset();
mOnErrorCallback.reset();
mOnBufferedAmountLowCallback.reset();
mOnMessageCallback.reset();
}

void DataChannelWrapper::doCleanup()
{
PLOG_DEBUG << "doCleanup() called";
mOnClosedCallback.reset();
instances.erase(this);
}

Expand Down Expand Up @@ -350,6 +362,8 @@ void DataChannelWrapper::onClosed(const Napi::CallbackInfo &info)
// This will run in main thread and needs to construct the
// arguments for the call
args = {};
},[this]{
doCleanup();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, doCleanup() won't be called if the user did not set a closed callback.

}); });
}

Expand Down
6 changes: 5 additions & 1 deletion src/data-channel-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ class DataChannelWrapper : public Napi::ObjectWrap<DataChannelWrapper>
// Close all existing DataChannels
static void CloseAll();

// Reset all Callbacks for existing DataChannels
static void CleanupAll();

private:
static std::unordered_set<DataChannelWrapper*> instances;
static std::unordered_set<DataChannelWrapper *> instances;

void doClose();
void doCleanup();

std::string mLabel;
std::shared_ptr<rtc::DataChannel> mDataChannelPtr = nullptr;
Expand Down
30 changes: 25 additions & 5 deletions src/media-track-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "media-direction.h"
#include "media-rtcpreceivingsession-wrapper.h"

#include "plog/Log.h"

Napi::FunctionReference TrackWrapper::constructor;
std::unordered_set<TrackWrapper *> TrackWrapper::instances;

Expand All @@ -12,6 +14,14 @@ void TrackWrapper::CloseAll()
inst->doClose();
}

void TrackWrapper::CleanupAll()
{
PLOG_DEBUG << "CleanupAll() called";
auto copy(instances);
for (auto inst : copy)
inst->doCleanup();
}

Napi::Object TrackWrapper::Init(Napi::Env env, Napi::Object exports)
{
Napi::HandleScope scope(env);
Expand Down Expand Up @@ -73,10 +83,14 @@ void TrackWrapper::doClose()
}

mOnOpenCallback.reset();
mOnClosedCallback.reset();
mOnErrorCallback.reset();
mOnMessageCallback.reset();
}

void TrackWrapper::doCleanup()
{
PLOG_DEBUG << "doCleanup() called";
mOnClosedCallback.reset();
instances.erase(this);
}

Expand Down Expand Up @@ -276,7 +290,8 @@ void TrackWrapper::onOpen(const Napi::CallbackInfo &info)
// Callback
mOnOpenCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());

mTrackPtr->onOpen([&]() {
mTrackPtr->onOpen([&]()
{
if (mOnOpenCallback)
mOnOpenCallback->call([this](Napi::Env env, std::vector<napi_value> &args) {
// Check the track is not closed
Expand Down Expand Up @@ -309,14 +324,17 @@ void TrackWrapper::onClosed(const Napi::CallbackInfo &info)
// Callback
mOnClosedCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());

mTrackPtr->onClosed([&]() {
mTrackPtr->onClosed([&]()
{
if (mOnClosedCallback)
mOnClosedCallback->call([this](Napi::Env env, std::vector<napi_value> &args) {
// Do not check if the data channel has been closed here

// This will run in main thread and needs to construct the
// arguments for the call
args = {};
},[this]{
doCleanup();
}); });
}

Expand All @@ -340,7 +358,8 @@ void TrackWrapper::onError(const Napi::CallbackInfo &info)
// Callback
mOnErrorCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());

mTrackPtr->onError([&](const std::string &error) {
mTrackPtr->onError([&](const std::string &error)
{
if (mOnErrorCallback)
mOnErrorCallback->call([this, error](Napi::Env env, std::vector<napi_value> &args) {
// Check the track is not closed
Expand Down Expand Up @@ -373,7 +392,8 @@ void TrackWrapper::onMessage(const Napi::CallbackInfo &info)
// Callback
mOnMessageCallback = std::make_unique<ThreadSafeCallback>(info[0].As<Napi::Function>());

mTrackPtr->onMessage([&](std::variant<rtc::binary, std::string> message) {
mTrackPtr->onMessage([&](std::variant<rtc::binary, std::string> message)
{
if (mOnMessageCallback)
mOnMessageCallback->call([this, message = std::move(message)](Napi::Env env, std::vector<napi_value> &args) {
// Check the track is not closed
Expand Down
10 changes: 7 additions & 3 deletions src/media-track-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,25 @@ class TrackWrapper : public Napi::ObjectWrap<TrackWrapper>
void onError(const Napi::CallbackInfo &info);
void onMessage(const Napi::CallbackInfo &info);

// Close all existing track
// Close all existing tracks
static void CloseAll();

// Reset all Callbacks for existing tracks
static void CleanupAll();

private:
static std::unordered_set<TrackWrapper *> instances;

void doClose();
void doCleanup();

std::shared_ptr<rtc::Track> mTrackPtr = nullptr;

// Callback Ptrs
// Callback Ptrs
std::unique_ptr<ThreadSafeCallback> mOnOpenCallback = nullptr;
std::unique_ptr<ThreadSafeCallback> mOnClosedCallback = nullptr;
std::unique_ptr<ThreadSafeCallback> mOnErrorCallback = nullptr;
std::unique_ptr<ThreadSafeCallback> mOnMessageCallback = nullptr;
};

#endif // MEDIA_TRACK_WRAPPER_H
#endif // MEDIA_TRACK_WRAPPER_H
48 changes: 21 additions & 27 deletions src/peer-connection-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ void PeerConnectionWrapper::CloseAll()
inst->doClose();
}

void PeerConnectionWrapper::ResetCallbacksAll()
void PeerConnectionWrapper::CleanupAll()
{
PLOG_DEBUG << "ResetCallbacksAll() called";
PLOG_DEBUG << "CleanupAll() called";
auto copy(instances);
for (auto inst : copy)
inst->doResetCallbacks();
inst->doCleanup();
}

Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
Expand All @@ -37,7 +37,6 @@ Napi::Object PeerConnectionWrapper::Init(Napi::Env env, Napi::Object exports)
"PeerConnection",
{
InstanceMethod("close", &PeerConnectionWrapper::close),
InstanceMethod("destroy", &PeerConnectionWrapper::destroy),
InstanceMethod("setLocalDescription", &PeerConnectionWrapper::setLocalDescription),
InstanceMethod("setRemoteDescription", &PeerConnectionWrapper::setRemoteDescription),
InstanceMethod("localDescription", &PeerConnectionWrapper::localDescription),
Expand Down Expand Up @@ -253,7 +252,8 @@ PeerConnectionWrapper::PeerConnectionWrapper(const Napi::CallbackInfo &info) : N
PeerConnectionWrapper::~PeerConnectionWrapper()
{
PLOG_DEBUG << "Destructor called";
doDestroy();
doCleanup();
doClose();
}

void PeerConnectionWrapper::doClose()
Expand All @@ -273,40 +273,27 @@ void PeerConnectionWrapper::doClose()
return;
}
}
}

void PeerConnectionWrapper::close(const Napi::CallbackInfo &info)
{
PLOG_DEBUG << "close() called";
doClose();
}

void PeerConnectionWrapper::doDestroy()
{
PLOG_DEBUG << "doDestroy() called";
doClose();
doResetCallbacks();
}

void PeerConnectionWrapper::doResetCallbacks()
{
PLOG_DEBUG << "doResetCallbacks() called";
mOnLocalDescriptionCallback.reset();
mOnLocalCandidateCallback.reset();
mOnStateChangeCallback.reset();
mOnIceStateChangeCallback.reset();
mOnSignalingStateChangeCallback.reset();
mOnGatheringStateChangeCallback.reset();
mOnDataChannelCallback.reset();
mOnTrackCallback.reset();
}

instances.erase(this);
void PeerConnectionWrapper::close(const Napi::CallbackInfo &info)
{
PLOG_DEBUG << "close() called";
doClose();
}

void PeerConnectionWrapper::destroy(const Napi::CallbackInfo &info)
void PeerConnectionWrapper::doCleanup()
{
PLOG_DEBUG << "destroy() called";
doDestroy();
PLOG_DEBUG << "doCleanup() called";
mOnStateChangeCallback.reset();
instances.erase(this);
}

void PeerConnectionWrapper::setLocalDescription(const Napi::CallbackInfo &info)
Expand Down Expand Up @@ -738,6 +725,13 @@ void PeerConnectionWrapper::onStateChange(const Napi::CallbackInfo &info)
stream << state;
args = {Napi::String::New(env, stream.str())};
PLOG_DEBUG << "mOnStateChangeCallback call(2)";
},[this,state](){
PLOG_DEBUG << "mOnStateChangeCallback cleanup";
// Special case for closed state, we need to reset all callbacks
if(state == rtc::PeerConnection::State::Closed)
{
doCleanup();
}
}); });
}

Expand Down
7 changes: 2 additions & 5 deletions src/peer-connection-wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class PeerConnectionWrapper : public Napi::ObjectWrap<PeerConnectionWrapper>
PeerConnectionWrapper(const Napi::CallbackInfo &info);
~PeerConnectionWrapper();

void destroy(const Napi::CallbackInfo &info);

// Functions
void close(const Napi::CallbackInfo &info);
void setLocalDescription(const Napi::CallbackInfo &info);
Expand Down Expand Up @@ -58,15 +56,14 @@ class PeerConnectionWrapper : public Napi::ObjectWrap<PeerConnectionWrapper>
static void CloseAll();

// Reset all Callbacks for existing Peer Connections
static void ResetCallbacksAll();
static void CleanupAll();

private:
static Napi::FunctionReference constructor;
static std::unordered_set<PeerConnectionWrapper *> instances;

void doClose();
void doDestroy();
void doResetCallbacks();
void doCleanup();

std::string mPeerName;
std::unique_ptr<rtc::PeerConnection> mRtcPeerConnPtr = nullptr;
Expand Down
7 changes: 5 additions & 2 deletions src/rtc-wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ void RtcWrapper::cleanup(const Napi::CallbackInfo &info)
if (rtc::Cleanup().wait_for(std::chrono::seconds(timeout)) == std::future_status::timeout)
throw std::runtime_error("cleanup timeout (possible deadlock)");

// Clear Callbacks
PeerConnectionWrapper::ResetCallbacksAll();
// Cleanup the instances
PeerConnectionWrapper::CleanupAll();
DataChannelWrapper::CleanupAll();
TrackWrapper::CleanupAll();
WebSocketWrapper::CleanupAll();

if (logCallback)
logCallback.reset();
Expand Down
8 changes: 6 additions & 2 deletions src/thread-safe-callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ ThreadSafeCallback::~ThreadSafeCallback()
tsfn.Abort();
}

void ThreadSafeCallback::call(arg_func_t argFunc)
void ThreadSafeCallback::call(arg_func_t argFunc, cleanup_func_t cleanupFunc)
{
CallbackData *data = new CallbackData{std::move(argFunc)};
CallbackData *data = new CallbackData{std::move(argFunc), std::move(cleanupFunc)};
if (tsfn.BlockingCall(data) != napi_ok)
{
delete data;
Expand All @@ -47,6 +47,7 @@ void ThreadSafeCallback::callbackFunc(Napi::Env env,

arg_vector_t args;
arg_func_t argFunc(std::move(data->argFunc));
cleanup_func_t cleanup(std::move(data->cleanupFunc));
delete data;

try
Expand All @@ -59,5 +60,8 @@ void ThreadSafeCallback::callbackFunc(Napi::Env env,
}

if (env && callback)
{
callback.Call(context->Value(), args);
cleanup();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think cleanup() should be called anyway, even if env or callback are not set, otherwise there could be scenarios where cleanup is never done.

}
}
Loading
Loading