Skip to content

Commit

Permalink
Merge pull request #3 from Faless/channels_pr
Browse files Browse the repository at this point in the history
Update to new DataChannel API, singleton load.
  • Loading branch information
Faless authored May 21, 2019
2 parents f2cf2e5 + 599ed98 commit 4234540
Show file tree
Hide file tree
Showing 22 changed files with 943 additions and 442 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ bin/*
*.lib
.sconsign.dblite
*.obj
*.swp
13 changes: 12 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ def add_sources(sources, dirpath, extension):
sources.append(dirpath + '/' + f)


def get_arch_dir(name):
if name == '32':
return 'x86'
elif name == '64':
return 'x64'
return name

env = Environment()
customs = ['custom.py']
opts = Variables(customs, ARGUMENTS)
Expand Down Expand Up @@ -107,7 +114,7 @@ else:
# Godot CPP bindings
env.Append(CPPPATH=[godot_headers])
env.Append(CPPPATH=[godot_cpp_headers, godot_cpp_headers + '/core', godot_cpp_headers + '/gen'])
env.Append(LIBPATH=[godot_cpp_lib_dir + '/' + target])
env.Append(LIBPATH=[godot_cpp_lib_dir + '/' + target + '/' + get_arch_dir(target_arch)])
env.Append(LIBS=['godot-cpp'])

# WebRTC stuff
Expand Down Expand Up @@ -156,6 +163,10 @@ sources = []
add_sources(sources, 'src/', 'cpp')
add_sources(sources, 'src/net/', 'cpp')

# Suffix
suffix = '.%s.%s' % (target, target_arch)
env["SHOBJSUFFIX"] = suffix + env["SHOBJSUFFIX"]

# Make the shared library
library = env.SharedLibrary(target=os.path.join(result_path, result_name), source=sources)
Default(library)
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions lib/godot-cpp/release/x64/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions lib/godot-cpp/release/x86/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
10 changes: 5 additions & 5 deletions src/GodotCreateSessionDescriptionObserver.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "WebRTCLibPeer.hpp"
#include "WebRTCLibPeerConnection.hpp"

using namespace godot_webrtc;

WebRTCLibPeer::GodotCSDO::GodotCSDO(WebRTCLibPeer *parent) {
WebRTCLibPeerConnection::GodotCSDO::GodotCSDO(WebRTCLibPeerConnection *parent) {
this->parent = parent;
}

void WebRTCLibPeer::GodotCSDO::OnSuccess(webrtc::SessionDescriptionInterface *desc) {
void WebRTCLibPeerConnection::GodotCSDO::OnSuccess(webrtc::SessionDescriptionInterface *desc) {
// serialize this offer and send it to the remote peer:
std::string sdp; // sdp = session description protocol
desc->ToString(&sdp);
parent->queue_signal("offer_created", 2, desc->type().c_str(), sdp.c_str());
parent->queue_signal("session_description_created", 2, desc->type().c_str(), sdp.c_str());
};

void WebRTCLibPeer::GodotCSDO::OnFailure(const std::string &error){};
void WebRTCLibPeerConnection::GodotCSDO::OnFailure(const std::string &error){};
19 changes: 0 additions & 19 deletions src/GodotDataChannelObserver.cpp

This file was deleted.

24 changes: 13 additions & 11 deletions src/GodotPeerConnectionObserver.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
#include "WebRTCLibPeer.hpp"
#include "WebRTCLibPeerConnection.hpp"
#include "WebRTCLibDataChannel.hpp"

using namespace godot_webrtc;

WebRTCLibPeer::GodotPCO::GodotPCO(WebRTCLibPeer *parent) {
WebRTCLibPeerConnection::GodotPCO::GodotPCO(WebRTCLibPeerConnection *parent) {
this->parent = parent;
}

void WebRTCLibPeer::GodotPCO::OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) {
void WebRTCLibPeerConnection::GodotPCO::OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state) {
}

void WebRTCLibPeer::GodotPCO::OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
void WebRTCLibPeerConnection::GodotPCO::OnAddStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
}

void WebRTCLibPeer::GodotPCO::OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
void WebRTCLibPeerConnection::GodotPCO::OnRemoveStream(rtc::scoped_refptr<webrtc::MediaStreamInterface> stream) {
}

void WebRTCLibPeer::GodotPCO::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
void WebRTCLibPeerConnection::GodotPCO::OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
parent->queue_signal("data_channel_received", 1, WebRTCLibDataChannel::new_data_channel(data_channel));
}

void WebRTCLibPeer::GodotPCO::OnRenegotiationNeeded() {
void WebRTCLibPeerConnection::GodotPCO::OnRenegotiationNeeded() {
}

void WebRTCLibPeer::GodotPCO::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) {
void WebRTCLibPeerConnection::GodotPCO::OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state) {
}

void WebRTCLibPeer::GodotPCO::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) {
void WebRTCLibPeerConnection::GodotPCO::OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state) {
}

void WebRTCLibPeer::GodotPCO::OnIceCandidate(const webrtc::IceCandidateInterface *candidate) {
void WebRTCLibPeerConnection::GodotPCO::OnIceCandidate(const webrtc::IceCandidateInterface *candidate) {
// Serialize the candidate and send it to the remote peer:

godot::Dictionary candidateSDP;
Expand All @@ -38,7 +40,7 @@ void WebRTCLibPeer::GodotPCO::OnIceCandidate(const webrtc::IceCandidateInterface
candidate->ToString(&sdp);
godot::String candidateSdpName = sdp.c_str();

parent->queue_signal("new_ice_candidate",
parent->queue_signal("ice_candidate_created",
3,
candidateSdpMidName,
candidateSdpMlineIndexName,
Expand Down
8 changes: 4 additions & 4 deletions src/GodotSetSessionDescriptionObserver.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "WebRTCLibPeer.hpp"
#include "WebRTCLibPeerConnection.hpp"

using namespace godot_webrtc;

WebRTCLibPeer::GodotSSDO::GodotSSDO(WebRTCLibPeer *parent) {
WebRTCLibPeerConnection::GodotSSDO::GodotSSDO(WebRTCLibPeerConnection *parent) {
this->parent = parent;
}

void WebRTCLibPeer::GodotSSDO::OnSuccess(){};
void WebRTCLibPeerConnection::GodotSSDO::OnSuccess(){};

void WebRTCLibPeer::GodotSSDO::OnFailure(const std::string &error){};
void WebRTCLibPeerConnection::GodotSSDO::OnFailure(const std::string &error){};
177 changes: 177 additions & 0 deletions src/WebRTCLibDataChannel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#include "WebRTCLibDataChannel.hpp"

using namespace godot_webrtc;

// Channel observer
WebRTCLibDataChannel::ChannelObserver::ChannelObserver(WebRTCLibDataChannel *parent) {
this->parent = parent;
}

void WebRTCLibDataChannel::ChannelObserver::OnMessage(const webrtc::DataBuffer &buffer) {
parent->queue_packet(buffer.data.data<uint8_t>(), buffer.data.size());
}

void WebRTCLibDataChannel::ChannelObserver::OnStateChange() {
}

void WebRTCLibDataChannel::ChannelObserver::OnBufferedAmountChange(uint64_t previous_amount) {
}

// DataChannel
WebRTCLibDataChannel *WebRTCLibDataChannel::new_data_channel(rtc::scoped_refptr<webrtc::DataChannelInterface> p_channel) {
// Invalid channel result in NULL return
ERR_FAIL_COND_V(p_channel.get() == nullptr, NULL);

// Instance a WebRTCDataChannelGDNative object
godot::WebRTCDataChannelGDNative *out = godot::WebRTCDataChannelGDNative::_new();
// Set our implementation as it's script
godot::NativeScript *script = godot::NativeScript::_new();
script->set_library(godot::get_wrapper<godot::GDNativeLibrary>((godot_object *)godot::gdnlib));
script->set_class_name("WebRTCLibDataChannel");
out->set_script(script);

// Bind the data channel to the ScriptInstance userdata (our script)
WebRTCLibDataChannel *tmp = godot::as<WebRTCLibDataChannel>(out);
tmp->bind_channel(p_channel);

return tmp;
}


void WebRTCLibDataChannel::bind_channel(rtc::scoped_refptr<webrtc::DataChannelInterface> p_channel) {
ERR_FAIL_COND(p_channel.get() == nullptr);

channel = p_channel;
label = p_channel->label();
protocol = p_channel->protocol();
channel->RegisterObserver(&observer);
}

void WebRTCLibDataChannel::queue_packet(const uint8_t *data, uint32_t size) {
mutex->lock();

godot::PoolByteArray packet;
packet.resize(size);
{
godot::PoolByteArray::Write w = packet.write();
memcpy(w.ptr(), data, size);
}
packet_queue.push(packet);

mutex->unlock();
}

void WebRTCLibDataChannel::set_write_mode(godot_int mode) {
}

godot_int WebRTCLibDataChannel::get_write_mode() const {
return 0;
}

bool WebRTCLibDataChannel::was_string_packet() const {
return false;
}

WebRTCLibDataChannel::ChannelState WebRTCLibDataChannel::get_ready_state() const {
ERR_FAIL_COND_V(channel.get() == nullptr, STATE_CLOSED);
return (ChannelState)channel->state();
}

const char *WebRTCLibDataChannel::get_label() const {
ERR_FAIL_COND_V(channel.get() == nullptr, "");
return label.c_str();
}

bool WebRTCLibDataChannel::is_ordered() const {
ERR_FAIL_COND_V(channel.get() == nullptr, false);
return channel->ordered();
}

int WebRTCLibDataChannel::get_id() const {
ERR_FAIL_COND_V(channel.get() == nullptr, -1);
return channel->id();
}

int WebRTCLibDataChannel::get_max_packet_life_time() const {
ERR_FAIL_COND_V(channel.get() == nullptr, 0);
return channel->maxRetransmitTime();
}

int WebRTCLibDataChannel::get_max_retransmits() const {
ERR_FAIL_COND_V(channel.get() == nullptr, 0);
return channel->maxRetransmits();
}

const char *WebRTCLibDataChannel::get_protocol() const {
ERR_FAIL_COND_V(channel.get() == nullptr, "");
return protocol.c_str();
}

bool WebRTCLibDataChannel::is_negotiated() const {
ERR_FAIL_COND_V(channel.get() == nullptr, false);
return channel->negotiated();
}

godot_error WebRTCLibDataChannel::poll() {
return GODOT_OK;
}

void WebRTCLibDataChannel::close() {
if(channel.get() != nullptr) {
channel->Close();
channel->UnregisterObserver();
}
}

godot_error WebRTCLibDataChannel::get_packet(const uint8_t **r_buffer, int *r_len) {
ERR_FAIL_COND_V(packet_queue.empty(), GODOT_ERR_UNAVAILABLE);

mutex->lock();

// Update current packet and pop queue
current_packet = packet_queue.front();
packet_queue.pop();
// Set out buffer and size (buffer will be gone at next get_packet or close)
*r_buffer = current_packet.read().ptr();
*r_len = current_packet.size();

mutex->unlock();

return GODOT_OK;
}

godot_error WebRTCLibDataChannel::put_packet(const uint8_t *p_buffer, int p_len) {
ERR_FAIL_COND_V(channel.get() == nullptr, GODOT_ERR_UNAVAILABLE);

webrtc::DataBuffer webrtc_buffer(rtc::CopyOnWriteBuffer(p_buffer, p_len), true);
ERR_FAIL_COND_V(!channel->Send(webrtc_buffer), GODOT_FAILED);

return GODOT_OK;
}

godot_int WebRTCLibDataChannel::get_available_packet_count() const {
return packet_queue.size();
}

godot_int WebRTCLibDataChannel::get_max_packet_size() const {
return 1200;
}

void WebRTCLibDataChannel::_register_methods() {
}

void WebRTCLibDataChannel::_init() {
register_interface(&interface);
}

WebRTCLibDataChannel::WebRTCLibDataChannel() : observer(this) {
mutex = new std::mutex;
}

WebRTCLibDataChannel::~WebRTCLibDataChannel() {
close();
if (_owner) {
register_interface(NULL);
}
delete mutex;
}
Loading

0 comments on commit 4234540

Please sign in to comment.