Skip to content

Commit 402d73a

Browse files
author
Clément Charmet
committed
WIP setLocalDescription + createDataChannel
1 parent 5e079d0 commit 402d73a

10 files changed

+294
-5
lines changed

binding.gyp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
'target_name': 'webrtc',
55
'sources': [
66
'src/event/createsessiondescriptionevent.cc',
7+
'src/event/setsessiondescriptionevent.cc',
78
'src/event/eventqueue.cc',
89
'src/globals.cc',
910
'src/module.cc',
1011
'src/observer/createsessiondescriptionobserver.cc',
12+
'src/observer/setsessiondescriptionobserver.cc',
1113
'src/observer/peerconnectionobserver.cc',
1214
'src/rtccertificate.cc',
1315
'src/rtcicecandidate.cc',

src/common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@
122122
\
123123
Local<Function> N = info[I].As<v8::Function>();
124124

125+
#define ASSERT_STRING_ARGUMENT(I, N) \
126+
if (!info[I]->IsString()) { \
127+
errorStream << ERROR_ARGUMENT_NOT_FUNCTION(I + 1, #N); \
128+
return Nan::ThrowTypeError(errorStream.str().c_str()); \
129+
} \
130+
\
131+
String::Utf8Value N(info[I]->ToString());
132+
125133
#define ASSERT_REJECT_OBJECT_ARGUMENT(I, N) \
126134
if (!info[I]->IsObject()) { \
127135
errorStream << ERROR_ARGUMENT_NOT_OBJECT(I + 1, #N); \
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 "common.h"
18+
#include "setsessiondescriptionevent.h"
19+
#include "rtcsessiondescription.h"
20+
21+
using namespace v8;
22+
23+
SetSessionDescriptionEvent::SetSessionDescriptionEvent(
24+
Persistent<Function> *successCallback,
25+
Persistent<Function> *failureCallback) :
26+
_resolver(NULL),
27+
_successCallback(successCallback),
28+
_failureCallback(failureCallback) {
29+
}
30+
31+
SetSessionDescriptionEvent::SetSessionDescriptionEvent(
32+
Persistent<Promise::Resolver> *resolver) :
33+
_resolver(resolver),
34+
_successCallback(NULL),
35+
_failureCallback(NULL) {
36+
}
37+
38+
void SetSessionDescriptionEvent::Handle() {
39+
Nan::HandleScope scope;
40+
41+
if (_resolver) {
42+
Local<Promise::Resolver> resolver = Nan::New(*_resolver);
43+
44+
if (_succeeded) {
45+
resolver->Resolve(Nan::Undefined());
46+
} else {
47+
resolver->Reject(Nan::Error(_errorMessage.c_str()));
48+
}
49+
50+
Isolate::GetCurrent()->RunMicrotasks();
51+
_resolver->Reset();
52+
delete _resolver;
53+
return;
54+
}
55+
56+
if (_succeeded && _successCallback) {
57+
Local<Function> successCallback = Nan::New(*_successCallback);
58+
Nan::Callback cb(successCallback);
59+
60+
const int argc = 0;
61+
Local<Value> argv[0] = {};
62+
63+
cb.Call(argc, argv);
64+
} else if (!_succeeded && _failureCallback) {
65+
Local<Function> failureCallback = Nan::New(*_failureCallback);
66+
Nan::Callback cb(failureCallback);
67+
68+
const int argc = 1;
69+
Local<Value> argv[1] = { Nan::Error(_errorMessage.c_str()) };
70+
71+
cb.Call(argc, argv);
72+
}
73+
74+
_successCallback->Reset();
75+
_failureCallback->Reset();
76+
77+
delete _successCallback;
78+
delete _failureCallback;
79+
}
80+
81+
void SetSessionDescriptionEvent::SetSucceeded(bool succeeded) {
82+
_succeeded = succeeded;
83+
}
84+
85+
void SetSessionDescriptionEvent::SetErrorMessage(
86+
const std::string &errorMessage) {
87+
_errorMessage = errorMessage;
88+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 EVENT_SETSESSIONDESCRIPTIONEVENT_H_
18+
#define EVENT_SETSESSIONDESCRIPTIONEVENT_H_
19+
20+
#include <nan.h>
21+
#include <webrtc/api/jsep.h>
22+
#include "event.h"
23+
24+
using namespace v8;
25+
26+
class SetSessionDescriptionEvent : public Event {
27+
public:
28+
explicit SetSessionDescriptionEvent(
29+
Persistent<Promise::Resolver> *resolver);
30+
SetSessionDescriptionEvent(Persistent<Function> *successCallback,
31+
Persistent<Function> *failureCallback);
32+
33+
void Handle();
34+
void SetSucceeded(bool succeeded);
35+
void SetErrorMessage(const std::string& errorMessage);
36+
37+
private:
38+
Persistent<Promise::Resolver> *_resolver;
39+
Persistent<Function> *_successCallback;
40+
Persistent<Function> *_failureCallback;
41+
bool _succeeded;
42+
std::string _errorMessage;
43+
};
44+
45+
#endif // EVENT_SETSESSIONDESCRIPTIONEVENT_H_

src/observer/createsessiondescriptionobserver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ class CreateSessionDescriptionObserver :
4747
Persistent<Function> *failureCallback);
4848
};
4949

50-
#endif // OBSERVER_CREATESESSIONDESCRIPTIONOBSERVER_H_
50+
#endif // OBSERVER_SETSESSIONDESCRIPTIONOBSERVER_H_
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 "common.h"
19+
#include "setsessiondescriptionobserver.h"
20+
#include "event/setsessiondescriptionevent.h"
21+
#include "globals.h"
22+
23+
using namespace v8;
24+
25+
SetSessionDescriptionObserver::SetSessionDescriptionObserver(
26+
Persistent<Promise::Resolver> *resolver) {
27+
_event = new SetSessionDescriptionEvent(resolver);
28+
}
29+
30+
SetSessionDescriptionObserver::SetSessionDescriptionObserver(
31+
Persistent<Function> *successCallback,
32+
Persistent<Function> *failureCallback) {
33+
_event = new SetSessionDescriptionEvent(successCallback, failureCallback);
34+
}
35+
36+
SetSessionDescriptionObserver *SetSessionDescriptionObserver::
37+
Create(Persistent<Function> *successCallback,
38+
Persistent<Function> *failureCallback) {
39+
return new rtc::RefCountedObject<SetSessionDescriptionObserver>
40+
(successCallback, failureCallback);
41+
}
42+
43+
SetSessionDescriptionObserver *SetSessionDescriptionObserver::
44+
Create(Persistent<Promise::Resolver> *resolver) {
45+
return new rtc::RefCountedObject<SetSessionDescriptionObserver>
46+
(resolver);
47+
}
48+
49+
void SetSessionDescriptionObserver::OnSuccess() {
50+
_event->SetSucceeded(true);
51+
Globals::GetEventQueue()->PushEvent(_event);
52+
}
53+
54+
void SetSessionDescriptionObserver::OnFailure(const std::string &error) {
55+
_event->SetSucceeded(false);
56+
_event->SetErrorMessage(error);
57+
Globals::GetEventQueue()->PushEvent(_event);
58+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 OBSERVER_SETSESSIONDESCRIPTIONOBSERVER_H_
18+
#define OBSERVER_SETSESSIONDESCRIPTIONOBSERVER_H_
19+
20+
#include <nan.h>
21+
#include <string>
22+
#include <webrtc/api/peerconnectioninterface.h>
23+
24+
using namespace v8;
25+
26+
class SetSessionDescriptionEvent;
27+
class SetSessionDescriptionObserver :
28+
public webrtc::SetSessionDescriptionObserver {
29+
public:
30+
static SetSessionDescriptionObserver *Create(
31+
Persistent<Promise::Resolver> *resolver);
32+
static SetSessionDescriptionObserver *Create(
33+
Persistent<Function> *successCallback,
34+
Persistent<Function> *failureCallback);
35+
36+
void OnSuccess();
37+
void OnFailure(const std::string& error);
38+
39+
private:
40+
SetSessionDescriptionEvent *_event;
41+
42+
protected:
43+
explicit SetSessionDescriptionObserver(
44+
Persistent<Promise::Resolver> *resolver);
45+
46+
SetSessionDescriptionObserver(Persistent<Function> *successCallback,
47+
Persistent<Function> *failureCallback);
48+
};
49+
50+
#endif // OBSERVER_SETSESSIONDESCRIPTIONOBSERVER_H_

src/rtcpeerconnection.cc

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@
2121
#include "common.h"
2222
#include "globals.h"
2323
#include "observer/createsessiondescriptionobserver.h"
24+
#include "observer/setsessiondescriptionobserver.h"
2425
#include "observer/peerconnectionobserver.h"
2526
#include "rtccertificate.h"
2627
#include "rtcpeerconnection.h"
28+
#include "rtcsessiondescription.h"
2729

2830
Nan::Persistent<FunctionTemplate> RTCPeerConnection::constructor;
2931

3032
static const char sRTCPeerConnection[] = "RTCPeerConnection";
3133

3234
static const char kCreateOffer[] = "createOffer";
35+
static const char kSetLocalDescription[] = "setLocalDescription";
36+
static const char kCreateDataChannel[] = "createDataChannel";
3337
static const char kGenerateCertificate[] = "generateCertificate";
3438

3539
static const char kIceServers[] = "iceServers";
@@ -90,6 +94,8 @@ NAN_MODULE_INIT(RTCPeerConnection::Init) {
9094

9195
Local<ObjectTemplate> prototype = ctor->InstanceTemplate();
9296
Nan::SetMethod(prototype, kCreateOffer, CreateOffer);
97+
Nan::SetMethod(prototype, kSetLocalDescription, SetLocalDescription);
98+
Nan::SetMethod(prototype, kCreateDataChannel, CreateDataChannel);
9399

94100
Local<ObjectTemplate> tpl = ctor->InstanceTemplate();
95101
Nan::SetAccessor(tpl, LOCAL_STRING(kConnectionState),
@@ -132,7 +138,6 @@ NAN_METHOD(RTCPeerConnection::New) {
132138
CONSTRUCTOR_HEADER("RTCPeerConnection")
133139
webrtc::FakeConstraints constraints;
134140
webrtc::PeerConnectionInterface::RTCConfiguration _config;
135-
webrtc::PeerConnectionInterface::IceServer server;
136141

137142
if (info.Length() > 0) {
138143
ASSERT_OBJECT_ARGUMENT(0, config);
@@ -223,6 +228,38 @@ NAN_METHOD(RTCPeerConnection::CreateOffer) {
223228
object->_peerConnection->CreateOffer(observer, &constraints);
224229
}
225230

231+
NAN_METHOD(RTCPeerConnection::SetLocalDescription) {
232+
METHOD_HEADER("RTCPeerConnection", "setLocalDescription");
233+
UNWRAP_OBJECT(RTCPeerConnection, object);
234+
235+
rtc::scoped_refptr<webrtc::SetSessionDescriptionObserver> observer;
236+
237+
// FIXME: Promise implementation only
238+
DECLARE_PROMISE_RESOLVER;
239+
ASSERT_REJECT_OBJECT_ARGUMENT(0, sessionDescription);
240+
241+
// FIXME: validate it's a RTCSessionDescription object
242+
RTCSessionDescription* _sessionDescription = Nan::ObjectWrap::Unwrap<RTCSessionDescription>(sessionDescription);
243+
244+
observer = SetSessionDescriptionObserver::Create(
245+
new Nan::Persistent<Promise::Resolver>(resolver));
246+
247+
object->_peerConnection->SetLocalDescription(observer, _sessionDescription->_sessionDescription);
248+
249+
}
250+
251+
NAN_METHOD(RTCPeerConnection::CreateDataChannel) {
252+
METHOD_HEADER("RTCPeerConnection", "setLocalDescription");
253+
UNWRAP_OBJECT(RTCPeerConnection, object);
254+
255+
ASSERT_STRING_ARGUMENT(0, name);
256+
257+
const webrtc::DataChannelInit init;
258+
259+
object->_peerConnection->CreateDataChannel(*name, &init);
260+
261+
}
262+
226263
NAN_GETTER(RTCPeerConnection::GetConnectionState) {
227264
info.GetReturnValue().Set(LOCAL_STRING("new"));
228265
}

src/rtcpeerconnection.h

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

3737
static NAN_METHOD(New);
3838
static NAN_METHOD(CreateOffer);
39+
static NAN_METHOD(SetLocalDescription);
40+
static NAN_METHOD(CreateDataChannel);
3941
static NAN_METHOD(GenerateCertificate);
4042

4143
static NAN_GETTER(GetConnectionState);

src/rtcsessiondescription.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class RTCSessionDescription : public Nan::ObjectWrap {
4242
static const char kPranswer[];
4343
static const char kRollback[];
4444

45+
webrtc::SessionDescriptionInterface *_sessionDescription;
46+
4547
private:
4648
explicit RTCSessionDescription(
4749
webrtc::SessionDescriptionInterface *sessionDescription);
@@ -51,9 +53,6 @@ class RTCSessionDescription : public Nan::ObjectWrap {
5153

5254
static NAN_GETTER(GetType);
5355
static NAN_GETTER(GetSdp);
54-
55-
protected:
56-
webrtc::SessionDescriptionInterface *_sessionDescription;
5756
};
5857

5958
#endif // RTCSESSIONDESCRIPTION_H_

0 commit comments

Comments
 (0)