-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
124 lines (112 loc) · 3.41 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
let room;
let pc;
function onSuccess() {};
function onError(error) {
console.error(error);
};
// Send signaling data via Scaledrone
function sendMessage(message) {
drone.publish({
room: roomName,
message
});
}
function startWebRTC(isOfferer) {
pc = new RTCPeerConnection(configuration);
// 'onicecandidate' notifies us whenever an ICE agent needs to deliver a
// message to the other peer through the signaling server
pc.onicecandidate = event => {
if (event.candidate) {
sendMessage({'candidate': event.candidate});
}
};
// isOfferer == member.length === 2, i.e. there are 2 people in the room
if (isOfferer) {
pc.onnegotiationneeded = () => {
pc.createOffer().then(localDescCreated).catch(onError);
}
}
// Display the remote video stream in the #remoteVideo element
pc.onaddstream = event => {
remoteVideo.srcObject = event.stream;
};
navigator.mediaDevices.getUserMedia({
audio: true,
video: true,
}).then(stream => {
// Display your local video in #localVideo element
localVideo.srcObject = stream;
// Add your stream to be sent to the other peer
pc.addStream(stream);
}, onError);
// Listen to signaling data from Scaledrone
room.on('data', (message, client) => {
// If the message was sent by us
if (client.id === drone.clientId) {
return;
}
if (message.sdp) {
//When you're the 2nd person
//This is called after receiving an offer or answer from another peer
pc.setRemoteDescription(new RTCSessionDescription(message.sdp), () => {
// When receiving an offer let's answer it
if (pc.remoteDescription.type === 'offer') {
pc.createAnswer().then(localDescCreated).catch(onError);
}
}, onError);
} else if (message.candidate) {
//When you're the 1st one
//Add the new ICE candidate to our connections remote description
pc.addIceCandidate(
new RTCIceCandidate(message.candidate), onSuccess, onError
);
}
});
}
function localDescCreated(desc) {
pc.setLocalDescription(
desc,
() => sendMessage({'sdp': pc.localDescription}),
onError
);
}
//------------------- main code -----------------------
//Using google's public STUN server
//Using ScaleDrone's server as signaling server
//Using RTCPeerConnection, getUserMedia API
// Generate random room name if needed
if (!location.hash) {
location.hash = Math.floor(Math.random() * 0xFFFFFF).toString(16);
}
const roomHash = location.hash.substring(1);
// Our own channel ID registered in ScaleDrone
const drone = new ScaleDrone('lThnE5uuEod3c5cI');
// Room name needs to be prefixed with 'observable-'
const roomName = 'observable-' + roomHash;
// Using google's public STUN Server
const configuration = {
iceServers: [{
urls: 'stun:stun.l.google.com:19302'
}]
};
//Subscribe a roomName in ScaleDrone
drone.on('open', error => {
if (error) {
return console.error(error);
}
room = drone.subscribe(roomName);
room.on('open', error => {
if (error) {
onError(error);
}
});
// Connect to the room and received an array of 'members'
// Signaling server is ready.
room.on('members', members => {
console.log('MEMBERS', members);
// If we are the second user to connect to the room we will be creating the offer
const isOfferer = members.length === 2;
startWebRTC(isOfferer);
});
});
//-------------------------------------------------------