Skip to content

Commit deee46e

Browse files
author
Clément Charmet
committed
added datachannel.send, emit empty icecandidate on gathering complete, clean
1 parent 9ffe9b6 commit deee46e

File tree

9 files changed

+93
-33
lines changed

9 files changed

+93
-33
lines changed

src/event/peerconnectioniceevent.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ void PeerConnectionIceEvent::Handle() {
3535

3636
// FIXME: make proper PeerConnectionIceEvent ?
3737
Local<Object> e = Nan::New<Object>();
38-
e->Set(LOCAL_STRING(kCandidate),
38+
39+
if(_candidate.length() && _sdpMid.length()) {
40+
e->Set(LOCAL_STRING(kCandidate),
3941
RTCIceCandidate::Create(_sdpMid, _sdpMLineIndex, _candidate));
42+
} else {
43+
e->Set(LOCAL_STRING(kCandidate), Nan::Null());
44+
}
4045

4146
_eventEmitter->EmitData(LOCAL_STRING(_type),e);
4247
}

src/event/peerconnectioniceevent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class PeerConnectionIceEvent : public EmitterEvent {
3131
void SetCandidate(const webrtc::IceCandidateInterface* candidate);
3232

3333
private:
34-
std::string _candidate = "";
35-
std::string _sdpMid = "";
34+
std::string _candidate;
35+
std::string _sdpMid;
3636
int _sdpMLineIndex = 0;
3737
//std::string _ufrag;
3838

src/event/setsessiondescriptionevent.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "common.h"
1818
#include "setsessiondescriptionevent.h"
1919
#include "rtcsessiondescription.h"
20+
#include <iostream>
2021

2122
using namespace v8;
2223

src/module.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@
2222
#include "rtcpeerconnection.h"
2323
#include "rtcsessiondescription.h"
2424
#include "rtcdatachannel.h"
25+
#include <webrtc/base/logging.h>
2526

2627
NAN_MODULE_INIT(Init) {
2728
if (!Globals::Init()) {
2829
return;
2930
}
3031

32+
rtc::LogMessage::LogToDebug(rtc::LS_NONE);
33+
//rtc::LogMessage::LogToDebug(rtc::LS_VERBOSE);
34+
3135
RTCCertificate::Init(target);
3236
RTCIceCandidate::Init(target);
3337
RTCPeerConnection::Init(target);

src/observer/peerconnectionobserver.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ PeerConnectionObserver::~PeerConnectionObserver() {
3434

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

5253
void PeerConnectionObserver::OnDataChannel(
5354
rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
55+
//std::cout << "OnDataChannel" << std::endl;
5456
DataChannelEvent* _event = new DataChannelEvent(_eventEmitter, data_channel);
5557
_event->SetType(kDataChannel);
5658
Globals::GetEventQueue()->PushEvent(_event);
5759
}
5860

5961
void PeerConnectionObserver::OnRenegotiationNeeded() {
62+
std::cout << "OnRenegotiationNeeded" << std::endl;
6063
EmitterEvent* _event = new EmitterEvent(_eventEmitter);
6164
_event->SetType(kNegociationNeeded);
6265
Globals::GetEventQueue()->PushEvent(_event);
6366
}
6467

6568
void PeerConnectionObserver::OnIceConnectionChange(
6669
webrtc::PeerConnectionInterface::IceConnectionState new_state) {
70+
//std::cout << "OnIceConnectionChange" << std::endl;
6771
EmitterEvent* _event = new EmitterEvent(_eventEmitter);
6872
_event->SetType(kIceConnectionStateChange);
6973
Globals::GetEventQueue()->PushEvent(_event);
7074
}
7175

7276
void PeerConnectionObserver::OnIceGatheringChange(
7377
webrtc::PeerConnectionInterface::IceGatheringState new_state) {
78+
//std::cout << "OnAddStream" << std::endl;
79+
switch (new_state) {
80+
case webrtc::PeerConnectionInterface::kIceGatheringNew:
81+
break;
82+
83+
case webrtc::PeerConnectionInterface::kIceGatheringGathering:
84+
break;
85+
86+
case webrtc::PeerConnectionInterface::kIceGatheringComplete:
87+
{
88+
// emit null ice candidate as per https://www.w3.org/TR/webrtc/#dom-rtcpeerconnectioniceevent
89+
PeerConnectionIceEvent* _iceEvent = new PeerConnectionIceEvent(_eventEmitter);
90+
_iceEvent->SetType(kIceCandidate);
91+
Globals::GetEventQueue()->PushEvent(_iceEvent);
92+
break;
93+
}
94+
95+
default:
96+
break;
97+
}
98+
7499
EmitterEvent* _event = new EmitterEvent(_eventEmitter);
75100
_event->SetType(kIceGatheringStateChange);
76101
Globals::GetEventQueue()->PushEvent(_event);
77102
}
78103

79104
void PeerConnectionObserver::OnIceCandidate(
80105
const webrtc::IceCandidateInterface *candidate) {
106+
//std::cout << "OnIceCandidate" << std::endl;
81107
PeerConnectionIceEvent* _event = new PeerConnectionIceEvent(_eventEmitter);
82108
_event->SetType(kIceCandidate);
83109
_event->SetCandidate(candidate);

src/rtcdatachannel.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static const char sRTCDataChannel[] = "RTCDataChannel";
2424
static const char kLabel[] = "label";
2525
static const char kOrdered[] = "ordered";
2626
static const char kReadyState[] = "readyState";
27+
static const char kSend[] = "send";
2728

2829
NAN_MODULE_INIT(RTCDataChannel::Init) {
2930
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(New);
@@ -34,6 +35,8 @@ NAN_MODULE_INIT(RTCDataChannel::Init) {
3435
Nan::SetAccessor(prototype, LOCAL_STRING(kLabel), GetLabel);
3536
Nan::SetAccessor(prototype, LOCAL_STRING(kOrdered), GetOrdered);
3637
Nan::SetAccessor(prototype, LOCAL_STRING(kReadyState), GetReadyState);
38+
39+
Nan::SetMethod(prototype, kSend, Send);
3740

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

@@ -76,3 +79,16 @@ NAN_GETTER(RTCDataChannel::GetReadyState) {
7679
const char* readyState = webrtc::DataChannelInterface::DataStateString(object->_datachannel->state());
7780
info.GetReturnValue().Set(LOCAL_STRING(readyState));
7881
}
82+
83+
NAN_METHOD(RTCDataChannel::Send) {
84+
METHOD_HEADER("RTCDataChannel", "send");
85+
UNWRAP_OBJECT(RTCDataChannel, object);
86+
87+
// FIXME: implement for ArrayBuffer, others?
88+
ASSERT_STRING_ARGUMENT(0, data);
89+
90+
webrtc::DataBuffer _buffer(*data);
91+
object->_datachannel->Send(_buffer);
92+
93+
info.GetReturnValue().Set(Nan::Null());
94+
}

src/rtcdatachannel.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@
2020
#include <nan.h>
2121
#include <webrtc/api/jsep.h>
2222
#include <string>
23+
#include "eventemitter.h"
2324

2425
using namespace v8;
2526

26-
class RTCDataChannel : public Nan::ObjectWrap {
27+
class RTCDataChannel : public EventEmitter {
2728
public:
2829
static NAN_MODULE_INIT(Init);
2930

3031
static NAN_GETTER(GetLabel);
3132
static NAN_GETTER(GetOrdered);
3233
static NAN_GETTER(GetReadyState);
3334

35+
static NAN_METHOD(Send);
36+
3437
static Local<Object> Create(
3538
const rtc::scoped_refptr<webrtc::DataChannelInterface>& datachannel);
3639

src/rtcpeerconnection.cc

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ RTCPeerConnection::RTCPeerConnection(
137137
_peerConnectionObserver = PeerConnectionObserver::Create();
138138
_peerConnection = Globals::GetPeerConnectionFactory()->CreatePeerConnection(
139139
config, &constraints, NULL, NULL, _peerConnectionObserver);
140-
//_peerConnectionObserver->SetPeerConnection(_peerConnection);
141140
}
142141

143142
RTCPeerConnection::~RTCPeerConnection() {
@@ -191,18 +190,9 @@ NAN_METHOD(RTCPeerConnection::New) {
191190
RTCPeerConnection *rtcPeerConnection = new RTCPeerConnection(_config,
192191
constraints);
193192
rtcPeerConnection->Wrap(info.This());
194-
195-
// "access" emit function inherited from EventEmitter
196-
//rtcPeerConnection->emit = new Nan::Persistent<v8::Function>(v8::Local<v8::Function>::Cast(rtcPeerConnection->handle()->Get(Nan::New("emit2").ToLocalChecked())));
197-
// v8::Local<v8::Function>::Cast(rtcPeerConnection->handle()->Get(Nan::New("emit").ToLocalChecked())));
198-
199-
//rtcPeerConnection->_peerConnectionObserver->SetEmit(rtcPeerConnection->emit);
193+
200194
rtcPeerConnection->_peerConnectionObserver->SetEventEmitter(rtcPeerConnection);
201-
// rtcPeerConnection->_peerConnectionObserver->SetEmit(
202-
// new Nan::Persistent()
203-
// );
204-
205-
195+
206196
info.GetReturnValue().Set(info.This());
207197
}
208198

@@ -278,26 +268,14 @@ NAN_METHOD(RTCPeerConnection::CreateAnswer) {
278268
object->_peerConnection->CreateAnswer(observer, &constraints);
279269
}
280270

281-
NAN_METHOD(RTCPeerConnection::TestEmit) {
282-
METHOD_HEADER("RTCPeerConnection", "testEmit");
283-
// UNWRAP_OBJECT(RTCPeerConnection, object);
284-
285-
// Local<Value> argv[] = { Nan::New("test").ToLocalChecked() };
286-
// Nan::New(*object->emit)->Call(object->handle(), 1, argv);
287-
288-
info.GetReturnValue().Set(Nan::Null());
289-
}
271+
// FIXME: factorize SetLocalDescription and SetRemoteDescription
290272

291273
NAN_METHOD(RTCPeerConnection::SetLocalDescription) {
292274
METHOD_HEADER("RTCPeerConnection", "setLocalDescription");
293275
UNWRAP_OBJECT(RTCPeerConnection, object);
294276

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

297-
// Local<Function> emit = Nan::New(*object->emit);
298-
// Local<Value> argv[] = { Nan::New("test").ToLocalChecked() };
299-
// emit->Call( object->handle(), 1 , argv );
300-
301279
// FIXME: Promise implementation only
302280
DECLARE_PROMISE_RESOLVER;
303281
ASSERT_REJECT_OBJECT_ARGUMENT(0, sessionDescription);
@@ -308,7 +286,20 @@ NAN_METHOD(RTCPeerConnection::SetLocalDescription) {
308286
observer = SetSessionDescriptionObserver::Create(
309287
new Nan::Persistent<Promise::Resolver>(resolver));
310288

311-
object->_peerConnection->SetLocalDescription(observer, _sessionDescription->_sessionDescription);
289+
std::string sdp;
290+
_sessionDescription->_sessionDescription->ToString(&sdp);
291+
std::string type = _sessionDescription->_sessionDescription->type();
292+
293+
webrtc::SdpParseError error;
294+
webrtc::SessionDescriptionInterface *desc;
295+
desc = webrtc::CreateSessionDescription(type, sdp, &error);
296+
297+
if (!desc) {
298+
errorStream << error.description;
299+
return Nan::ThrowTypeError(errorStream.str().c_str());
300+
}
301+
302+
object->_peerConnection->SetLocalDescription(observer, desc);
312303
}
313304

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

330-
object->_peerConnection->SetRemoteDescription(observer, _sessionDescription->_sessionDescription);
321+
std::string sdp;
322+
_sessionDescription->_sessionDescription->ToString(&sdp);
323+
std::string type = _sessionDescription->_sessionDescription->type();
324+
325+
webrtc::SdpParseError error;
326+
webrtc::SessionDescriptionInterface *desc;
327+
desc = webrtc::CreateSessionDescription(type, sdp, &error);
328+
329+
if (!desc) {
330+
errorStream << error.description;
331+
return Nan::ThrowTypeError(errorStream.str().c_str());
332+
}
333+
334+
object->_peerConnection->SetRemoteDescription(observer, desc);
335+
331336
}
332337

333338
NAN_METHOD(RTCPeerConnection::CreateDataChannel) {
@@ -347,6 +352,7 @@ NAN_METHOD(RTCPeerConnection::CreateDataChannel) {
347352
}
348353

349354
NAN_GETTER(RTCPeerConnection::GetConnectionState) {
355+
// FIXME: implement
350356
info.GetReturnValue().Set(LOCAL_STRING("new"));
351357
}
352358

src/rtcpeerconnection.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class RTCPeerConnection : public EventEmitter {
4242
static NAN_METHOD(SetRemoteDescription);
4343
static NAN_METHOD(CreateDataChannel);
4444
static NAN_METHOD(GenerateCertificate);
45-
static NAN_METHOD(TestEmit);
46-
45+
4746
static NAN_GETTER(GetConnectionState);
4847
static NAN_GETTER(GetCurrentLocalDescription);
4948
static NAN_GETTER(GetCurrentRemoteDescription);

0 commit comments

Comments
 (0)