mirror of
https://github.com/godotengine/webrtc-native.git
synced 2026-01-01 05:48:15 +03:00
Use a class instead of lambdas for signals.
This commit is contained in:
@@ -174,14 +174,12 @@ godot_error WebRTCLibPeerConnection::add_ice_candidate(const char *sdpMidName, i
|
||||
godot_error WebRTCLibPeerConnection::poll() {
|
||||
ERR_FAIL_COND_V(peer_connection.get() == nullptr, GODOT_ERR_UNCONFIGURED);
|
||||
|
||||
std::function<void()> signal;
|
||||
while (!signal_queue.empty()) {
|
||||
mutex_signal_queue->lock();
|
||||
signal = signal_queue.front();
|
||||
Signal signal = signal_queue.front();
|
||||
signal_queue.pop();
|
||||
mutex_signal_queue->unlock();
|
||||
|
||||
signal();
|
||||
signal.emit(this);
|
||||
}
|
||||
return GODOT_OK;
|
||||
}
|
||||
@@ -244,15 +242,7 @@ WebRTCLibPeerConnection::~WebRTCLibPeerConnection() {
|
||||
|
||||
void WebRTCLibPeerConnection::queue_signal(godot::String p_name, int p_argc, const godot::Variant &p_arg1, const godot::Variant &p_arg2, const godot::Variant &p_arg3) {
|
||||
mutex_signal_queue->lock();
|
||||
signal_queue.push(
|
||||
[this, p_name, p_argc, p_arg1, p_arg2, p_arg3] {
|
||||
if (p_argc == 1) {
|
||||
emit_signal(p_name, p_arg1);
|
||||
} else if (p_argc == 2) {
|
||||
emit_signal(p_name, p_arg1, p_arg2);
|
||||
} else {
|
||||
emit_signal(p_name, p_arg1, p_arg2, p_arg3);
|
||||
}
|
||||
});
|
||||
const godot::Variant argv[3] = { p_arg1, p_arg2, p_arg3 };
|
||||
signal_queue.push(Signal(p_name, p_argc, argv));
|
||||
mutex_signal_queue->unlock();
|
||||
}
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
|
||||
#include "api/peer_connection_interface.h" // interface for all things needed from WebRTC
|
||||
#include "media/base/media_engine.h" // needed for CreateModularPeerConnectionFactory
|
||||
#include <functional> // std::function
|
||||
#include <mutex> // mutex @TODO replace std::mutex with Godot mutex
|
||||
#include <mutex>
|
||||
|
||||
#include "net/WebRTCPeerConnectionNative.hpp"
|
||||
|
||||
@@ -43,7 +42,6 @@ public:
|
||||
/* helper functions */
|
||||
|
||||
void queue_signal(godot::String p_name, int p_argc, const godot::Variant &p_arg1 = godot::Variant(), const godot::Variant &p_arg2 = godot::Variant(), const godot::Variant &p_arg3 = godot::Variant());
|
||||
// void queue_signal(godot::StringName p_name, Variant_ARG_LIST);
|
||||
void queue_packet(uint8_t *, int);
|
||||
|
||||
/** PeerConnectionObserver callback functions **/
|
||||
@@ -82,12 +80,38 @@ public:
|
||||
void OnFailure(webrtc::RTCError error) override;
|
||||
};
|
||||
|
||||
class Signal {
|
||||
godot::String method;
|
||||
godot::Variant argv[3];
|
||||
int argc = 0;
|
||||
public:
|
||||
Signal(godot::String p_method, int p_argc, const godot::Variant *p_argv) {
|
||||
method = p_method;
|
||||
argc = p_argc;
|
||||
for (int i = 0; i < argc; i++) {
|
||||
argv[i] = p_argv[i];
|
||||
}
|
||||
}
|
||||
|
||||
void emit(godot::Object *p_object) {
|
||||
if (argc == 0) {
|
||||
p_object->emit_signal(method);
|
||||
} else if (argc == 1) {
|
||||
p_object->emit_signal(method, argv[0]);
|
||||
} else if (argc == 2) {
|
||||
p_object->emit_signal(method, argv[0], argv[1]);
|
||||
} else if (argc == 3) {
|
||||
p_object->emit_signal(method, argv[0], argv[1], argv[2]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
GodotPCO pco;
|
||||
rtc::scoped_refptr<GodotSSDO> ptr_ssdo;
|
||||
rtc::scoped_refptr<GodotCSDO> ptr_csdo;
|
||||
|
||||
std::mutex *mutex_signal_queue = nullptr;
|
||||
std::queue<std::function<void()> > signal_queue;
|
||||
std::queue<Signal> signal_queue;
|
||||
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> pc_factory;
|
||||
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection;
|
||||
|
||||
Reference in New Issue
Block a user