diff --git a/src/event/peerconnectioniceevent.cc b/src/event/peerconnectioniceevent.cc index 2b236e0..5c35940 100644 --- a/src/event/peerconnectioniceevent.cc +++ b/src/event/peerconnectioniceevent.cc @@ -35,8 +35,13 @@ void PeerConnectionIceEvent::Handle() { // FIXME: make proper PeerConnectionIceEvent ? Local e = Nan::New(); - 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); } diff --git a/src/event/peerconnectioniceevent.h b/src/event/peerconnectioniceevent.h index 9489c3b..65bd86e 100644 --- a/src/event/peerconnectioniceevent.h +++ b/src/event/peerconnectioniceevent.h @@ -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; diff --git a/src/event/setsessiondescriptionevent.cc b/src/event/setsessiondescriptionevent.cc index 4f7253e..23638e4 100644 --- a/src/event/setsessiondescriptionevent.cc +++ b/src/event/setsessiondescriptionevent.cc @@ -17,6 +17,7 @@ #include "common.h" #include "setsessiondescriptionevent.h" #include "rtcsessiondescription.h" +#include using namespace v8; diff --git a/src/module.cc b/src/module.cc index 1ec0930..2628c96 100644 --- a/src/module.cc +++ b/src/module.cc @@ -22,12 +22,16 @@ #include "rtcpeerconnection.h" #include "rtcsessiondescription.h" #include "rtcdatachannel.h" +#include 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); diff --git a/src/observer/peerconnectionobserver.cc b/src/observer/peerconnectionobserver.cc index e252caf..c433afe 100644 --- a/src/observer/peerconnectionobserver.cc +++ b/src/observer/peerconnectionobserver.cc @@ -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); @@ -51,12 +52,14 @@ void PeerConnectionObserver::OnRemoveStream( void PeerConnectionObserver::OnDataChannel( rtc::scoped_refptr 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); @@ -64,6 +67,7 @@ void PeerConnectionObserver::OnRenegotiationNeeded() { 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); @@ -71,6 +75,27 @@ void PeerConnectionObserver::OnIceConnectionChange( 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); @@ -78,6 +103,7 @@ void PeerConnectionObserver::OnIceGatheringChange( void PeerConnectionObserver::OnIceCandidate( const webrtc::IceCandidateInterface *candidate) { + //std::cout << "OnIceCandidate" << std::endl; PeerConnectionIceEvent* _event = new PeerConnectionIceEvent(_eventEmitter); _event->SetType(kIceCandidate); _event->SetCandidate(candidate); diff --git a/src/rtcdatachannel.cc b/src/rtcdatachannel.cc index 16e2ae5..26b83f7 100644 --- a/src/rtcdatachannel.cc +++ b/src/rtcdatachannel.cc @@ -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 ctor = Nan::New(New); @@ -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()); @@ -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()); +} diff --git a/src/rtcdatachannel.h b/src/rtcdatachannel.h index 09c6307..cd13520 100644 --- a/src/rtcdatachannel.h +++ b/src/rtcdatachannel.h @@ -20,10 +20,11 @@ #include #include #include +#include "eventemitter.h" using namespace v8; -class RTCDataChannel : public Nan::ObjectWrap { +class RTCDataChannel : public EventEmitter { public: static NAN_MODULE_INIT(Init); @@ -31,6 +32,8 @@ class RTCDataChannel : public Nan::ObjectWrap { static NAN_GETTER(GetOrdered); static NAN_GETTER(GetReadyState); + static NAN_METHOD(Send); + static Local Create( const rtc::scoped_refptr& datachannel); diff --git a/src/rtcpeerconnection.cc b/src/rtcpeerconnection.cc index 8863c77..40a9888 100644 --- a/src/rtcpeerconnection.cc +++ b/src/rtcpeerconnection.cc @@ -137,7 +137,6 @@ RTCPeerConnection::RTCPeerConnection( _peerConnectionObserver = PeerConnectionObserver::Create(); _peerConnection = Globals::GetPeerConnectionFactory()->CreatePeerConnection( config, &constraints, NULL, NULL, _peerConnectionObserver); - //_peerConnectionObserver->SetPeerConnection(_peerConnection); } RTCPeerConnection::~RTCPeerConnection() { @@ -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::Local::Cast(rtcPeerConnection->handle()->Get(Nan::New("emit2").ToLocalChecked()))); - // v8::Local::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()); } @@ -278,15 +268,7 @@ NAN_METHOD(RTCPeerConnection::CreateAnswer) { object->_peerConnection->CreateAnswer(observer, &constraints); } -NAN_METHOD(RTCPeerConnection::TestEmit) { - METHOD_HEADER("RTCPeerConnection", "testEmit"); - // UNWRAP_OBJECT(RTCPeerConnection, object); - - // Local 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"); @@ -294,10 +276,6 @@ NAN_METHOD(RTCPeerConnection::SetLocalDescription) { rtc::scoped_refptr observer; - // Local emit = Nan::New(*object->emit); - // Local argv[] = { Nan::New("test").ToLocalChecked() }; - // emit->Call( object->handle(), 1 , argv ); - // FIXME: Promise implementation only DECLARE_PROMISE_RESOLVER; ASSERT_REJECT_OBJECT_ARGUMENT(0, sessionDescription); @@ -308,7 +286,20 @@ NAN_METHOD(RTCPeerConnection::SetLocalDescription) { observer = SetSessionDescriptionObserver::Create( new Nan::Persistent(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) { @@ -327,7 +318,21 @@ NAN_METHOD(RTCPeerConnection::SetRemoteDescription) { observer = SetSessionDescriptionObserver::Create( new Nan::Persistent(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) { @@ -347,6 +352,7 @@ NAN_METHOD(RTCPeerConnection::CreateDataChannel) { } NAN_GETTER(RTCPeerConnection::GetConnectionState) { + // FIXME: implement info.GetReturnValue().Set(LOCAL_STRING("new")); } diff --git a/src/rtcpeerconnection.h b/src/rtcpeerconnection.h index d3145d1..a2d6b4a 100644 --- a/src/rtcpeerconnection.h +++ b/src/rtcpeerconnection.h @@ -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);