@osimatic/helpers-js 1.1.32 → 1.1.33
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 +52 -44
package/package.json
CHANGED
package/web_rtc.js
CHANGED
|
@@ -8,68 +8,76 @@ class WebRTC {
|
|
|
8
8
|
this.turnSecret = turnSecret;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
static
|
|
11
|
+
static setSignalingBus(signalingBus) {
|
|
12
|
+
this.signalingBus = signalingBus;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static offer(videoBroadcasterId, stream, iceCandidateCallback, onIceCandidateErrorCallback, connectionStateChangeCallback) {
|
|
12
16
|
return new Promise(async (resolve, reject) => {
|
|
13
17
|
try {
|
|
14
18
|
let { username, password } = this.getTurnCredentials();
|
|
15
|
-
let peerConn = new RTCPeerConnection(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
let peerConn = new RTCPeerConnection({
|
|
20
|
+
sdpSemantics: 'unified-plan', //newer implementation of WebRTC,
|
|
21
|
+
iceCandidatePoolSize: 2,
|
|
22
|
+
iceServers: [{
|
|
23
|
+
urls: [this.turnUrl + '?transport=udp', this.turnUrl + '?transport=tcp'],
|
|
24
|
+
username: username,
|
|
25
|
+
credential: password
|
|
26
|
+
}, {
|
|
27
|
+
urls: this.stunUrl
|
|
28
|
+
}]
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
peerConn.onnegotiationneeded = async () => {
|
|
32
|
+
await peerConn.setLocalDescription(await peerConn.createOffer());
|
|
33
|
+
this.signalingBus.subscribe('answer', (payload) => peerConn.setRemoteDescription(payload.description));
|
|
34
|
+
this.signalingBus.subscribe('candidate', (payload) => peerConn.addIceCandidate(new RTCIceCandidate(payload.candidate)));
|
|
35
|
+
this.signalingBus.publish('offer', { id: videoBroadcasterId, description: peerConn.localDescription });
|
|
36
|
+
};
|
|
27
37
|
|
|
28
|
-
peerConn.
|
|
29
|
-
peerConn.
|
|
30
|
-
peerConn.
|
|
38
|
+
peerConn.onconnectionstatechange = (event) => connectionStateChangeCallback(event, peerConn.connectionState);
|
|
39
|
+
peerConn.onicecandidate = (event) => iceCandidateCallback(event);
|
|
40
|
+
peerConn.onicecandidateerror = (event) => onIceCandidateErrorCallback(event.url, event.errorText);
|
|
31
41
|
|
|
32
42
|
stream.getTracks().forEach(track => peerConn.addTrack(track, stream));
|
|
33
|
-
|
|
34
|
-
const offer = await peerConn.createOffer();
|
|
35
|
-
await peerConn.setLocalDescription(offer);
|
|
36
|
-
|
|
43
|
+
|
|
37
44
|
resolve(peerConn);
|
|
38
45
|
} catch (error) {
|
|
46
|
+
console.error(error);
|
|
39
47
|
reject(error);
|
|
40
48
|
}
|
|
41
49
|
});
|
|
42
50
|
}
|
|
43
|
-
|
|
44
|
-
static answer(remoteDescription, onTrackCallback, iceCandidateCallback,
|
|
51
|
+
|
|
52
|
+
static answer(videoBroadcasterId, remoteDescription, onTrackCallback, iceCandidateCallback, onIceCandidateErrorCallback, connectionStateChangeCallback) {
|
|
45
53
|
return new Promise(async (resolve, reject) => {
|
|
46
54
|
try {
|
|
47
55
|
let { username, password } = this.getTurnCredentials();
|
|
48
|
-
let peerConn = new RTCPeerConnection(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
);
|
|
56
|
+
let peerConn = new RTCPeerConnection({
|
|
57
|
+
sdpSemantics: 'unified-plan', //newer implementation of WebRTC,
|
|
58
|
+
iceCandidatePoolSize: 2,
|
|
59
|
+
iceServers: [{
|
|
60
|
+
urls: [this.turnUrl + '?transport=udp', this.turnUrl + '?transport=tcp'],
|
|
61
|
+
username: username,
|
|
62
|
+
credential: password
|
|
63
|
+
}, {
|
|
64
|
+
urls: this.stunUrl
|
|
65
|
+
}]
|
|
66
|
+
});
|
|
60
67
|
|
|
61
|
-
peerConn.
|
|
62
|
-
peerConn.
|
|
63
|
-
peerConn.
|
|
64
|
-
peerConn.ontrack = (
|
|
65
|
-
|
|
66
|
-
peerConn.setRemoteDescription(new RTCSessionDescription(remoteDescription))
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
peerConn.onconnectionstatechange = (event) => connectionStateChangeCallback(event, peerConn.connectionState);
|
|
69
|
+
peerConn.onicecandidate = async (event) => iceCandidateCallback(event);
|
|
70
|
+
peerConn.onicecandidateerror = (event) => onIceCandidateErrorCallback(event.url, event.errorText);
|
|
71
|
+
peerConn.ontrack = (event) => onTrackCallback(event.streams);
|
|
72
|
+
|
|
73
|
+
peerConn.setRemoteDescription(new RTCSessionDescription(remoteDescription));
|
|
74
|
+
await peerConn.setLocalDescription(await peerConn.createAnswer());
|
|
75
|
+
this.signalingBus.subscribe('candidate', (payload) => peerConn.addIceCandidate(new RTCIceCandidate(payload.candidate)));
|
|
76
|
+
this.signalingBus.publish('answer', { id: videoBroadcasterId, description: peerConn.localDescription });
|
|
77
|
+
|
|
71
78
|
resolve(peerConn);
|
|
72
79
|
} catch (error) {
|
|
80
|
+
console.error(error);
|
|
73
81
|
reject(error);
|
|
74
82
|
}
|
|
75
83
|
});
|