Skip to content

Commit

Permalink
added datachannel.send, emit empty icecandidate on gathering complete…
Browse files Browse the repository at this point in the history
…, clean
  • Loading branch information
Clément Charmet committed Mar 24, 2017
1 parent 9ffe9b6 commit deee46e
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 33 deletions.
7 changes: 6 additions & 1 deletion src/event/peerconnectioniceevent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ void PeerConnectionIceEvent::Handle() {

// FIXME: make proper PeerConnectionIceEvent ?
Local<Object> e = Nan::New<Object>();
e->Set(LOCAL_STRING(kCandidate),

if(_candidate.length() && _sdpMid.length()) {
e->Set(LOCAL_STRING(kCandidate),
RTCIceCandidate::Create(_sdpMid, _sdpMLineIndex, _candidate));
} else {
e->Set(LOCAL_STRING(kCandidate), Nan::Null());
}

_eventEmitter->EmitData(LOCAL_STRING(_type),e);
}
Expand Down
4 changes: 2 additions & 2 deletions src/event/peerconnectioniceevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class PeerConnectionIceEvent : public EmitterEvent {
void SetCandidate(const webrtc::IceCandidateInterface* candidate);

private:
std::string _candidate = "";
std::string _sdpMid = "";
std::string _candidate;
std::string _sdpMid;
int _sdpMLineIndex = 0;
//std::string _ufrag;

Expand Down
1 change: 1 addition & 0 deletions src/event/setsessiondescriptionevent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "common.h"
#include "setsessiondescriptionevent.h"
#include "rtcsessiondescription.h"
#include <iostream>

using namespace v8;

Expand Down
4 changes: 4 additions & 0 deletions src/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
#include "rtcpeerconnection.h"
#include "rtcsessiondescription.h"
#include "rtcdatachannel.h"
#include <webrtc/base/logging.h>

NAN_MODULE_INIT(Init) {
if (!Globals::Init()) {
return;
}

rtc::LogMessage::LogToDebug(rtc::LS_NONE);
//rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);

RTCCertificate::Init(target);
RTCIceCandidate::Init(target);
RTCPeerConnection::Init(target);
Expand Down
26 changes: 26 additions & 0 deletions src/observer/peerconnectionobserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ PeerConnectionObserver::~PeerConnectionObserver() {

void PeerConnectionObserver::OnSignalingChange(
webrtc::PeerConnectionInterface::SignalingState new_state) {
//std::cout << "OnSignalingChange" << std::endl;
EmitterEvent* _event = new EmitterEvent(_eventEmitter);
_event->SetType(kSignalingStateChange);
Globals::GetEventQueue()->PushEvent(_event);
Expand All @@ -51,33 +52,58 @@ void PeerConnectionObserver::OnRemoveStream(

void PeerConnectionObserver::OnDataChannel(
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
//std::cout << "OnDataChannel" << std::endl;
DataChannelEvent* _event = new DataChannelEvent(_eventEmitter, data_channel);
_event->SetType(kDataChannel);
Globals::GetEventQueue()->PushEvent(_event);
}

void PeerConnectionObserver::OnRenegotiationNeeded() {
std::cout << "OnRenegotiationNeeded" << std::endl;
EmitterEvent* _event = new EmitterEvent(_eventEmitter);
_event->SetType(kNegociationNeeded);
Globals::GetEventQueue()->PushEvent(_event);
}

void PeerConnectionObserver::OnIceConnectionChange(
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
//std::cout << "OnIceConnectionChange" << std::endl;
EmitterEvent* _event = new EmitterEvent(_eventEmitter);
_event->SetType(kIceConnectionStateChange);
Globals::GetEventQueue()->PushEvent(_event);
}

void PeerConnectionObserver::OnIceGatheringChange(
webrtc::PeerConnectionInterface::IceGatheringState new_state) {
//std::cout << "OnAddStream" << std::endl;
switch (new_state) {
case webrtc::PeerConnectionInterface::kIceGatheringNew:
break;

case webrtc::PeerConnectionInterface::kIceGatheringGathering:
break;

case webrtc::PeerConnectionInterface::kIceGatheringComplete:
{
// emit null ice candidate as per https://www.w3.org/TR/webrtc/#dom-rtcpeerconnectioniceevent
PeerConnectionIceEvent* _iceEvent = new PeerConnectionIceEvent(_eventEmitter);
_iceEvent->SetType(kIceCandidate);
Globals::GetEventQueue()->PushEvent(_iceEvent);
break;
}

default:
break;
}

EmitterEvent* _event = new EmitterEvent(_eventEmitter);
_event->SetType(kIceGatheringStateChange);
Globals::GetEventQueue()->PushEvent(_event);
}

void PeerConnectionObserver::OnIceCandidate(
const webrtc::IceCandidateInterface *candidate) {
//std::cout << "OnIceCandidate" << std::endl;
PeerConnectionIceEvent* _event = new PeerConnectionIceEvent(_eventEmitter);
_event->SetType(kIceCandidate);
_event->SetCandidate(candidate);
Expand Down
16 changes: 16 additions & 0 deletions src/rtcdatachannel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static const char sRTCDataChannel[] = "RTCDataChannel";
static const char kLabel[] = "label";
static const char kOrdered[] = "ordered";
static const char kReadyState[] = "readyState";
static const char kSend[] = "send";

NAN_MODULE_INIT(RTCDataChannel::Init) {
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(New);
Expand All @@ -34,6 +35,8 @@ NAN_MODULE_INIT(RTCDataChannel::Init) {
Nan::SetAccessor(prototype, LOCAL_STRING(kLabel), GetLabel);
Nan::SetAccessor(prototype, LOCAL_STRING(kOrdered), GetOrdered);
Nan::SetAccessor(prototype, LOCAL_STRING(kReadyState), GetReadyState);

Nan::SetMethod(prototype, kSend, Send);

constructor().Reset(Nan::GetFunction(ctor).ToLocalChecked());

Expand Down Expand Up @@ -76,3 +79,16 @@ NAN_GETTER(RTCDataChannel::GetReadyState) {
const char* readyState = webrtc::DataChannelInterface::DataStateString(object->_datachannel->state());
info.GetReturnValue().Set(LOCAL_STRING(readyState));
}

NAN_METHOD(RTCDataChannel::Send) {
METHOD_HEADER("RTCDataChannel", "send");
UNWRAP_OBJECT(RTCDataChannel, object);

// FIXME: implement for ArrayBuffer, others?
ASSERT_STRING_ARGUMENT(0, data);

webrtc::DataBuffer _buffer(*data);
object->_datachannel->Send(_buffer);

info.GetReturnValue().Set(Nan::Null());
}
5 changes: 4 additions & 1 deletion src/rtcdatachannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
#include <nan.h>
#include <webrtc/api/jsep.h>
#include <string>
#include "eventemitter.h"

using namespace v8;

class RTCDataChannel : public Nan::ObjectWrap {
class RTCDataChannel : public EventEmitter {
public:
static NAN_MODULE_INIT(Init);

static NAN_GETTER(GetLabel);
static NAN_GETTER(GetOrdered);
static NAN_GETTER(GetReadyState);

static NAN_METHOD(Send);

static Local<Object> Create(
const rtc::scoped_refptr<webrtc::DataChannelInterface>& datachannel);

Expand Down
60 changes: 33 additions & 27 deletions src/rtcpeerconnection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ RTCPeerConnection::RTCPeerConnection(
_peerConnectionObserver = PeerConnectionObserver::Create();
_peerConnection = Globals::GetPeerConnectionFactory()->CreatePeerConnection(
config, &constraints, NULL, NULL, _peerConnectionObserver);
//_peerConnectionObserver->SetPeerConnection(_peerConnection);
}

RTCPeerConnection::~RTCPeerConnection() {
Expand Down Expand Up @@ -191,18 +190,9 @@ NAN_METHOD(RTCPeerConnection::New) {
RTCPeerConnection *rtcPeerConnection = new RTCPeerConnection(_config,
constraints);
rtcPeerConnection->Wrap(info.This());

// "access" emit function inherited from EventEmitter
//rtcPeerConnection->emit = new Nan::Persistent<v8::Function>(v8::Local<v8::Function>::Cast(rtcPeerConnection->handle()->Get(Nan::New("emit2").ToLocalChecked())));
// v8::Local<v8::Function>::Cast(rtcPeerConnection->handle()->Get(Nan::New("emit").ToLocalChecked())));

//rtcPeerConnection->_peerConnectionObserver->SetEmit(rtcPeerConnection->emit);

rtcPeerConnection->_peerConnectionObserver->SetEventEmitter(rtcPeerConnection);
// rtcPeerConnection->_peerConnectionObserver->SetEmit(
// new Nan::Persistent()
// );



info.GetReturnValue().Set(info.This());
}

Expand Down Expand Up @@ -278,26 +268,14 @@ NAN_METHOD(RTCPeerConnection::CreateAnswer) {
object->_peerConnection->CreateAnswer(observer, &constraints);
}

NAN_METHOD(RTCPeerConnection::TestEmit) {
METHOD_HEADER("RTCPeerConnection", "testEmit");
// UNWRAP_OBJECT(RTCPeerConnection, object);

// Local<Value> argv[] = { Nan::New("test").ToLocalChecked() };
// Nan::New(*object->emit)->Call(object->handle(), 1, argv);

info.GetReturnValue().Set(Nan::Null());
}
// FIXME: factorize SetLocalDescription and SetRemoteDescription

NAN_METHOD(RTCPeerConnection::SetLocalDescription) {
METHOD_HEADER("RTCPeerConnection", "setLocalDescription");
UNWRAP_OBJECT(RTCPeerConnection, object);

rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;

// Local<Function> emit = Nan::New(*object->emit);
// Local<Value> argv[] = { Nan::New("test").ToLocalChecked() };
// emit->Call( object->handle(), 1 , argv );

// FIXME: Promise implementation only
DECLARE_PROMISE_RESOLVER;
ASSERT_REJECT_OBJECT_ARGUMENT(0, sessionDescription);
Expand All @@ -308,7 +286,20 @@ NAN_METHOD(RTCPeerConnection::SetLocalDescription) {
observer = SetSessionDescriptionObserver::Create(
new Nan::Persistent<Promise::Resolver>(resolver));

object->_peerConnection->SetLocalDescription(observer, _sessionDescription->_sessionDescription);
std::string sdp;
_sessionDescription->_sessionDescription->ToString(&sdp);
std::string type = _sessionDescription->_sessionDescription->type();

webrtc::SdpParseError error;
webrtc::SessionDescriptionInterface *desc;
desc = webrtc::CreateSessionDescription(type, sdp, &error);

if (!desc) {
errorStream << error.description;
return Nan::ThrowTypeError(errorStream.str().c_str());
}

object->_peerConnection->SetLocalDescription(observer, desc);
}

NAN_METHOD(RTCPeerConnection::SetRemoteDescription) {
Expand All @@ -327,7 +318,21 @@ NAN_METHOD(RTCPeerConnection::SetRemoteDescription) {
observer = SetSessionDescriptionObserver::Create(
new Nan::Persistent<Promise::Resolver>(resolver));

object->_peerConnection->SetRemoteDescription(observer, _sessionDescription->_sessionDescription);
std::string sdp;
_sessionDescription->_sessionDescription->ToString(&sdp);
std::string type = _sessionDescription->_sessionDescription->type();

webrtc::SdpParseError error;
webrtc::SessionDescriptionInterface *desc;
desc = webrtc::CreateSessionDescription(type, sdp, &error);

if (!desc) {
errorStream << error.description;
return Nan::ThrowTypeError(errorStream.str().c_str());
}

object->_peerConnection->SetRemoteDescription(observer, desc);

}

NAN_METHOD(RTCPeerConnection::CreateDataChannel) {
Expand All @@ -347,6 +352,7 @@ NAN_METHOD(RTCPeerConnection::CreateDataChannel) {
}

NAN_GETTER(RTCPeerConnection::GetConnectionState) {
// FIXME: implement
info.GetReturnValue().Set(LOCAL_STRING("new"));
}

Expand Down
3 changes: 1 addition & 2 deletions src/rtcpeerconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class RTCPeerConnection : public EventEmitter {
static NAN_METHOD(SetRemoteDescription);
static NAN_METHOD(CreateDataChannel);
static NAN_METHOD(GenerateCertificate);
static NAN_METHOD(TestEmit);


static NAN_GETTER(GetConnectionState);
static NAN_GETTER(GetCurrentLocalDescription);
static NAN_GETTER(GetCurrentRemoteDescription);
Expand Down

0 comments on commit deee46e

Please sign in to comment.