Skip to content

Commit 9535036

Browse files
author
Clément Charmet
committed
added createAnswer, setRemoteDescription, datachannel
1 parent 402d73a commit 9535036

File tree

7 files changed

+196
-4
lines changed

7 files changed

+196
-4
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'src/rtcicecandidate.cc',
1616
'src/rtcpeerconnection.cc',
1717
'src/rtcsessiondescription.cc',
18+
'src/rtcdatachannel.cc',
1819
],
1920
'include_dirs' : [
2021
'build/include',

src/common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
#define ERROR_ARGUMENT_NOT_FUNCTION(INDEX, NAME) \
6363
"parameter " << INDEX << " ('" << NAME << "') is not a function."
6464

65+
#define ERROR_ARGUMENT_NOT_STRING(INDEX, NAME) \
66+
"parameter " << INDEX << " ('" << NAME << "') is not a string."
67+
6568
#ifdef DEBUG
6669
#define CONSTRUCTOR_HEADER(NAME) \
6770
LOG(LS_INFO) << __PRETTY_FUNCTION__; \
@@ -124,7 +127,7 @@
124127

125128
#define ASSERT_STRING_ARGUMENT(I, N) \
126129
if (!info[I]->IsString()) { \
127-
errorStream << ERROR_ARGUMENT_NOT_FUNCTION(I + 1, #N); \
130+
errorStream << ERROR_ARGUMENT_NOT_STRING(I + 1, #N); \
128131
return Nan::ThrowTypeError(errorStream.str().c_str()); \
129132
} \
130133
\

src/module.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "rtcicecandidate.h"
2222
#include "rtcpeerconnection.h"
2323
#include "rtcsessiondescription.h"
24+
#include "rtcdatachannel.h"
2425

2526
NAN_MODULE_INIT(Init) {
2627
if (!Globals::Init()) {
@@ -31,6 +32,7 @@ NAN_MODULE_INIT(Init) {
3132
RTCIceCandidate::Init(target);
3233
RTCPeerConnection::Init(target);
3334
RTCSessionDescription::Init(target);
35+
RTCDataChannel::Init(target);
3436

3537
node::AtExit(Globals::Cleanup);
3638
}

src/rtcdatachannel.cc

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2017 Axel Isouard <axel@isouard.fr>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <iostream>
18+
#include <webrtc/api/datachannelinterface.h>
19+
#include "rtcdatachannel.h"
20+
#include "common.h"
21+
22+
static const char sRTCDataChannel[] = "RTCDataChannel";
23+
24+
static const char kLabel[] = "label";
25+
static const char kOrdered[] = "ordered";
26+
static const char kReadyState[] = "readyState";
27+
28+
NAN_MODULE_INIT(RTCDataChannel::Init) {
29+
Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(New);
30+
ctor->SetClassName(LOCAL_STRING(sRTCDataChannel));
31+
ctor->InstanceTemplate()->SetInternalFieldCount(1);
32+
33+
Local<ObjectTemplate> prototype = ctor->PrototypeTemplate();
34+
Nan::SetAccessor(prototype, LOCAL_STRING(kLabel), GetLabel);
35+
Nan::SetAccessor(prototype, LOCAL_STRING(kOrdered), GetOrdered);
36+
Nan::SetAccessor(prototype, LOCAL_STRING(kReadyState), GetReadyState);
37+
38+
constructor().Reset(Nan::GetFunction(ctor).ToLocalChecked());
39+
}
40+
41+
RTCDataChannel::RTCDataChannel(
42+
const rtc::scoped_refptr<webrtc::DataChannelInterface> &datachannel)
43+
: _datachannel(datachannel) {
44+
}
45+
46+
RTCDataChannel::~RTCDataChannel() {}
47+
48+
Local<Object> RTCDataChannel::Create(
49+
const rtc::scoped_refptr<webrtc::DataChannelInterface> &datachannel) {
50+
Local<Function> cons = Nan::New(RTCDataChannel::constructor());
51+
Local<Object> instance = Nan::NewInstance(cons, 0, NULL).ToLocalChecked();
52+
53+
RTCDataChannel *_datachannel = new RTCDataChannel(datachannel);
54+
_datachannel->Wrap(instance);
55+
56+
return instance;
57+
}
58+
59+
NAN_METHOD(RTCDataChannel::New) {
60+
}
61+
62+
NAN_GETTER(RTCDataChannel::GetLabel) {
63+
UNWRAP_OBJECT(RTCDataChannel, object);
64+
info.GetReturnValue().Set(LOCAL_STRING(object->_datachannel->label()));
65+
}
66+
67+
NAN_GETTER(RTCDataChannel::GetOrdered) {
68+
UNWRAP_OBJECT(RTCDataChannel, object);
69+
info.GetReturnValue().Set(object->_datachannel->ordered());
70+
}
71+
72+
NAN_GETTER(RTCDataChannel::GetReadyState) {
73+
UNWRAP_OBJECT(RTCDataChannel, object);
74+
const char* readyState = webrtc::DataChannelInterface::DataStateString(object->_datachannel->state());
75+
info.GetReturnValue().Set(LOCAL_STRING(readyState));
76+
}

src/rtcdatachannel.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (c) 2017 Axel Isouard <axel@isouard.fr>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef RTCDATACHANNEL_H_
18+
#define RTCDATACHANNEL_H_
19+
20+
#include <nan.h>
21+
#include <webrtc/api/jsep.h>
22+
#include <string>
23+
24+
using namespace v8;
25+
26+
class RTCDataChannel : public Nan::ObjectWrap {
27+
public:
28+
static NAN_MODULE_INIT(Init);
29+
30+
static NAN_GETTER(GetLabel);
31+
static NAN_GETTER(GetOrdered);
32+
static NAN_GETTER(GetReadyState);
33+
34+
static Local<Object> Create(
35+
const rtc::scoped_refptr<webrtc::DataChannelInterface>& datachannel);
36+
37+
static inline Nan::Persistent<v8::Function>& constructor() {
38+
static Nan::Persistent<v8::Function> _constructor;
39+
return _constructor;
40+
}
41+
42+
const rtc::scoped_refptr<webrtc::DataChannelInterface> _datachannel;
43+
44+
private:
45+
explicit RTCDataChannel(
46+
const rtc::scoped_refptr<webrtc::DataChannelInterface>& datachannel);
47+
~RTCDataChannel();
48+
49+
static NAN_METHOD(New);
50+
51+
};
52+
53+
#endif // RTCDATACHANNEL_H_

src/rtcpeerconnection.cc

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626
#include "rtccertificate.h"
2727
#include "rtcpeerconnection.h"
2828
#include "rtcsessiondescription.h"
29+
#include "rtcdatachannel.h"
2930

3031
Nan::Persistent<FunctionTemplate> RTCPeerConnection::constructor;
3132

3233
static const char sRTCPeerConnection[] = "RTCPeerConnection";
3334

3435
static const char kCreateOffer[] = "createOffer";
36+
static const char kCreateAnswer[] = "createAnswer";
3537
static const char kSetLocalDescription[] = "setLocalDescription";
38+
static const char kSetRemoteDescription[] = "setRemoteDescription";
3639
static const char kCreateDataChannel[] = "createDataChannel";
3740
static const char kGenerateCertificate[] = "generateCertificate";
3841

@@ -94,7 +97,9 @@ NAN_MODULE_INIT(RTCPeerConnection::Init) {
9497

9598
Local<ObjectTemplate> prototype = ctor->InstanceTemplate();
9699
Nan::SetMethod(prototype, kCreateOffer, CreateOffer);
100+
Nan::SetMethod(prototype, kCreateAnswer, CreateAnswer);
97101
Nan::SetMethod(prototype, kSetLocalDescription, SetLocalDescription);
102+
Nan::SetMethod(prototype, kSetRemoteDescription, SetRemoteDescription);
98103
Nan::SetMethod(prototype, kCreateDataChannel, CreateDataChannel);
99104

100105
Local<ObjectTemplate> tpl = ctor->InstanceTemplate();
@@ -159,7 +164,6 @@ NAN_METHOD(RTCPeerConnection::New) {
159164
ASSERT_PROPERTY_STRING(kIceServerUrls, iceServerUrlVal, iceServerUrl);
160165
server.urls.push_back(*iceServerUrl);
161166
}
162-
163167
_config.servers.push_back(server);
164168
}
165169

@@ -228,6 +232,35 @@ NAN_METHOD(RTCPeerConnection::CreateOffer) {
228232
object->_peerConnection->CreateOffer(observer, &constraints);
229233
}
230234

235+
NAN_METHOD(RTCPeerConnection::CreateAnswer) {
236+
METHOD_HEADER("RTCPeerConnection", "createAnswer");
237+
UNWRAP_OBJECT(RTCPeerConnection, object);
238+
239+
unsigned char start = 0;
240+
rtc::scoped_refptr<webrtc::CreateSessionDescriptionObserver> observer;
241+
242+
if (info.Length() < 2) {
243+
DECLARE_PROMISE_RESOLVER;
244+
245+
observer = CreateSessionDescriptionObserver::Create(
246+
new Nan::Persistent<Promise::Resolver>(resolver));
247+
} else if (info.Length() > 1) {
248+
if (info.Length() > 2) {
249+
start = 1;
250+
}
251+
252+
ASSERT_FUNCTION_ARGUMENT(start, successCallback);
253+
ASSERT_FUNCTION_ARGUMENT(start + 1, failureCallback);
254+
255+
observer = CreateSessionDescriptionObserver::Create(
256+
new Nan::Persistent<Function>(successCallback),
257+
new Nan::Persistent<Function>(failureCallback));
258+
}
259+
260+
webrtc::FakeConstraints constraints;
261+
object->_peerConnection->CreateAnswer(observer, &constraints);
262+
}
263+
231264
NAN_METHOD(RTCPeerConnection::SetLocalDescription) {
232265
METHOD_HEADER("RTCPeerConnection", "setLocalDescription");
233266
UNWRAP_OBJECT(RTCPeerConnection, object);
@@ -245,19 +278,41 @@ NAN_METHOD(RTCPeerConnection::SetLocalDescription) {
245278
new Nan::Persistent<Promise::Resolver>(resolver));
246279

247280
object->_peerConnection->SetLocalDescription(observer, _sessionDescription->_sessionDescription);
281+
}
282+
283+
NAN_METHOD(RTCPeerConnection::SetRemoteDescription) {
284+
METHOD_HEADER("RTCPeerConnection", "setRemoteDescription");
285+
UNWRAP_OBJECT(RTCPeerConnection, object);
286+
287+
rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
288+
289+
// FIXME: Promise implementation only
290+
DECLARE_PROMISE_RESOLVER;
291+
ASSERT_REJECT_OBJECT_ARGUMENT(0, sessionDescription);
292+
293+
// FIXME: validate it's a RTCSessionDescription object
294+
RTCSessionDescription* _sessionDescription = Nan::ObjectWrap::Unwrap<RTCSessionDescription>(sessionDescription);
248295

296+
observer = SetSessionDescriptionObserver::Create(
297+
new Nan::Persistent<Promise::Resolver>(resolver));
298+
299+
object->_peerConnection->SetRemoteDescription(observer, _sessionDescription->_sessionDescription);
249300
}
250301

251302
NAN_METHOD(RTCPeerConnection::CreateDataChannel) {
252-
METHOD_HEADER("RTCPeerConnection", "setLocalDescription");
303+
METHOD_HEADER("RTCPeerConnection", "createDataChannel");
253304
UNWRAP_OBJECT(RTCPeerConnection, object);
254305

255306
ASSERT_STRING_ARGUMENT(0, name);
256307

308+
// FIXME: add init options
257309
const webrtc::DataChannelInit init;
258310

259-
object->_peerConnection->CreateDataChannel(*name, &init);
311+
rtc::scoped_refptr<webrtc::DataChannelInterface> _channel = object->_peerConnection->CreateDataChannel(*name, &init);
312+
313+
Local<Object> datachannel = RTCDataChannel::Create(_channel);
260314

315+
info.GetReturnValue().Set(datachannel);
261316
}
262317

263318
NAN_GETTER(RTCPeerConnection::GetConnectionState) {

src/rtcpeerconnection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class RTCPeerConnection : public Nan::ObjectWrap {
3636

3737
static NAN_METHOD(New);
3838
static NAN_METHOD(CreateOffer);
39+
static NAN_METHOD(CreateAnswer);
3940
static NAN_METHOD(SetLocalDescription);
41+
static NAN_METHOD(SetRemoteDescription);
4042
static NAN_METHOD(CreateDataChannel);
4143
static NAN_METHOD(GenerateCertificate);
4244

0 commit comments

Comments
 (0)