@osimatic/helpers-js 1.0.54 → 1.0.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/web_rtc.js +28 -8
package/package.json
CHANGED
package/web_rtc.js
CHANGED
|
@@ -8,26 +8,35 @@ class WebRTC {
|
|
|
8
8
|
this.turnSecret = turnSecret;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
static offer(stream, iceCandidateCallback) {
|
|
11
|
+
static offer(stream, iceCandidateCallback, connectionStateChangeCallback, iceFailureCallback) {
|
|
12
12
|
return new Promise(async (resolve, reject) => {
|
|
13
13
|
try {
|
|
14
14
|
let { username, password } = this.getTurnCredentials();
|
|
15
15
|
let peerConn = new RTCPeerConnection(
|
|
16
16
|
{
|
|
17
17
|
iceServers: [
|
|
18
|
-
{
|
|
18
|
+
{
|
|
19
|
+
urls: [this.turnUrl + '?transport=udp', this.turnUrl + '?transport=tcp'],
|
|
20
|
+
username: username,
|
|
21
|
+
credential: password
|
|
22
|
+
},
|
|
19
23
|
{ urls: this.stunUrl }
|
|
20
24
|
]
|
|
21
25
|
}
|
|
22
26
|
);
|
|
23
|
-
|
|
24
|
-
|
|
27
|
+
|
|
28
|
+
peerConn.oniceconnectionstatechange = (event) => connectionStateChangeCallback(event);
|
|
29
|
+
|
|
25
30
|
peerConn.onicecandidate = ((event) => {
|
|
26
31
|
if (event.candidate) {
|
|
27
32
|
iceCandidateCallback(event.candidate);
|
|
28
33
|
}
|
|
29
34
|
});
|
|
30
35
|
|
|
36
|
+
peerConn.onicecandidateerror = (event) => iceFailureCallback(event);
|
|
37
|
+
|
|
38
|
+
stream.getTracks().forEach(track => peerConn.addTrack(track, stream));
|
|
39
|
+
|
|
31
40
|
const offer = await peerConn.createOffer();
|
|
32
41
|
await peerConn.setLocalDescription(offer);
|
|
33
42
|
|
|
@@ -38,26 +47,35 @@ class WebRTC {
|
|
|
38
47
|
});
|
|
39
48
|
}
|
|
40
49
|
|
|
41
|
-
static answer(remoteDescription, onTrackCallback, iceCandidateCallback) {
|
|
50
|
+
static answer(remoteDescription, onTrackCallback, iceCandidateCallback, connectionStateChangeCallback, iceFailureCallback) {
|
|
42
51
|
return new Promise(async (resolve, reject) => {
|
|
43
52
|
try {
|
|
44
53
|
let { username, password } = this.getTurnCredentials();
|
|
45
54
|
let peerConn = new RTCPeerConnection(
|
|
46
55
|
{
|
|
47
56
|
iceServers: [
|
|
48
|
-
{
|
|
57
|
+
{
|
|
58
|
+
urls: [this.turnUrl + '?transport=udp', this.turnUrl + '?transport=tcp'],
|
|
59
|
+
username: username,
|
|
60
|
+
credential: password
|
|
61
|
+
},
|
|
49
62
|
{ urls: this.stunUrl }
|
|
50
63
|
]
|
|
51
64
|
}
|
|
52
65
|
);
|
|
53
|
-
|
|
54
|
-
peerConn.
|
|
66
|
+
|
|
67
|
+
peerConn.oniceconnectionstatechange = (event) => connectionStateChangeCallback(event);
|
|
68
|
+
|
|
55
69
|
peerConn.onicecandidate = ((event) => {
|
|
56
70
|
if (event.candidate) {
|
|
57
71
|
iceCandidateCallback(event.candidate);
|
|
58
72
|
}
|
|
59
73
|
});
|
|
60
74
|
|
|
75
|
+
peerConn.onicecandidateerror = (event) => iceFailureCallback(event);
|
|
76
|
+
|
|
77
|
+
peerConn.ontrack = (event) => onTrackCallback(event.streams);
|
|
78
|
+
|
|
61
79
|
peerConn.setRemoteDescription(new RTCSessionDescription(remoteDescription))
|
|
62
80
|
|
|
63
81
|
const answer = await peerConn.createAnswer();
|
|
@@ -72,6 +90,8 @@ class WebRTC {
|
|
|
72
90
|
|
|
73
91
|
static disconnectPeer(peerConn) {
|
|
74
92
|
if (peerConn) {
|
|
93
|
+
peerConn.oniceconnectionstatechange = null;
|
|
94
|
+
peerConn.onicecandidateerror = null;
|
|
75
95
|
peerConn.onicecandidate = null;
|
|
76
96
|
peerConn.ontrack = null;
|
|
77
97
|
|