2017-10-21 14:27:29 +00:00
|
|
|
/* vim: set sts=4 sw=4 et :
|
|
|
|
*
|
|
|
|
* Demo Javascript app for negotiating and streaming a sendrecv webrtc stream
|
|
|
|
* with a GStreamer app. Runs only in passive mode, i.e., responds to offers
|
|
|
|
* with answers, exchanges ICE candidates, and streams.
|
|
|
|
*
|
|
|
|
* Author: Nirbheek Chauhan <nirbheek@centricular.com>
|
|
|
|
*/
|
|
|
|
|
2018-03-31 19:09:48 +00:00
|
|
|
// Set this to override the automatic detection in websocketServerConnect()
|
|
|
|
var ws_server;
|
|
|
|
var ws_port;
|
|
|
|
// Set this to use a specific peer id instead of a random one
|
2018-03-31 19:58:02 +00:00
|
|
|
var default_peer_id;
|
2018-03-31 19:09:48 +00:00
|
|
|
// Override with your own STUN servers if you want
|
2017-10-21 14:27:29 +00:00
|
|
|
var rtc_configuration = {iceServers: [{urls: "stun:stun.services.mozilla.com"},
|
|
|
|
{urls: "stun:stun.l.google.com:19302"}]};
|
2018-03-31 19:37:51 +00:00
|
|
|
// The default constraints that will be attempted. Can be overriden by the user.
|
|
|
|
var default_constraints = {video: true, audio: true};
|
2018-03-31 19:09:48 +00:00
|
|
|
|
|
|
|
var connect_attempts = 0;
|
|
|
|
var peer_connection;
|
2018-09-21 13:03:43 +00:00
|
|
|
var send_channel;
|
2017-10-21 14:27:29 +00:00
|
|
|
var ws_conn;
|
2018-03-31 08:22:02 +00:00
|
|
|
// Promise for local stream after constraints are approved by the user
|
2018-03-31 17:31:32 +00:00
|
|
|
var local_stream_promise;
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2021-02-09 09:46:11 +00:00
|
|
|
function setConnectButtonState(value) {
|
|
|
|
document.getElementById("peer-connect-button").value = value;
|
|
|
|
}
|
|
|
|
|
2021-02-09 09:58:57 +00:00
|
|
|
function wantRemoteOfferer() {
|
|
|
|
return document.getElementById("remote-offerer").checked;
|
|
|
|
}
|
|
|
|
|
2020-11-27 09:16:52 +00:00
|
|
|
function onConnectClicked() {
|
2021-02-09 09:46:11 +00:00
|
|
|
if (document.getElementById("peer-connect-button").value == "Disconnect") {
|
|
|
|
resetState();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-27 09:16:52 +00:00
|
|
|
var id = document.getElementById("peer-connect").value;
|
|
|
|
if (id == "") {
|
|
|
|
alert("Peer id must be filled out");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws_conn.send("SESSION " + id);
|
2021-02-09 09:46:11 +00:00
|
|
|
setConnectButtonState("Disconnect");
|
2020-11-27 09:16:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
function getOurId() {
|
|
|
|
return Math.floor(Math.random() * (9000 - 10) + 10).toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetState() {
|
|
|
|
// This will call onServerClose()
|
|
|
|
ws_conn.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleIncomingError(error) {
|
2018-03-31 16:54:15 +00:00
|
|
|
setError("ERROR: " + error);
|
2017-10-21 14:27:29 +00:00
|
|
|
resetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVideoElement() {
|
|
|
|
return document.getElementById("stream");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setStatus(text) {
|
|
|
|
console.log(text);
|
2018-03-31 20:23:44 +00:00
|
|
|
var span = document.getElementById("status")
|
2018-03-31 16:54:15 +00:00
|
|
|
// Don't set the status if it already contains an error
|
|
|
|
if (!span.classList.contains('error'))
|
|
|
|
span.textContent = text;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setError(text) {
|
2018-03-31 20:23:44 +00:00
|
|
|
console.error(text);
|
|
|
|
var span = document.getElementById("status")
|
2018-03-31 16:54:15 +00:00
|
|
|
span.textContent = text;
|
|
|
|
span.classList.add('error');
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
2018-03-31 17:31:32 +00:00
|
|
|
function resetVideo() {
|
|
|
|
// Release the webcam and mic
|
2018-03-31 20:22:46 +00:00
|
|
|
if (local_stream_promise)
|
2018-09-21 13:02:55 +00:00
|
|
|
local_stream_promise.then(stream => {
|
|
|
|
if (stream) {
|
|
|
|
stream.getTracks().forEach(function (track) { track.stop(); });
|
|
|
|
}
|
|
|
|
});
|
2018-03-31 17:31:32 +00:00
|
|
|
|
|
|
|
// Reset the video element and stop showing the last received frame
|
2017-10-21 14:27:29 +00:00
|
|
|
var videoElement = getVideoElement();
|
|
|
|
videoElement.pause();
|
|
|
|
videoElement.src = "";
|
|
|
|
videoElement.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
// SDP offer received from peer, set remote description and create an answer
|
|
|
|
function onIncomingSDP(sdp) {
|
|
|
|
peer_connection.setRemoteDescription(sdp).then(() => {
|
|
|
|
setStatus("Remote SDP set");
|
|
|
|
if (sdp.type != "offer")
|
|
|
|
return;
|
2018-03-31 16:54:15 +00:00
|
|
|
setStatus("Got SDP offer");
|
2018-03-31 17:31:32 +00:00
|
|
|
local_stream_promise.then((stream) => {
|
2018-03-31 16:54:15 +00:00
|
|
|
setStatus("Got local stream, creating answer");
|
2018-03-31 08:22:02 +00:00
|
|
|
peer_connection.createAnswer()
|
2018-03-31 16:54:15 +00:00
|
|
|
.then(onLocalDescription).catch(setError);
|
|
|
|
}).catch(setError);
|
|
|
|
}).catch(setError);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Local description was set, send it to peer
|
|
|
|
function onLocalDescription(desc) {
|
|
|
|
console.log("Got local description: " + JSON.stringify(desc));
|
|
|
|
peer_connection.setLocalDescription(desc).then(function() {
|
2020-03-04 16:03:17 +00:00
|
|
|
setStatus("Sending SDP " + desc.type);
|
2018-03-23 06:06:40 +00:00
|
|
|
sdp = {'sdp': peer_connection.localDescription}
|
|
|
|
ws_conn.send(JSON.stringify(sdp));
|
2017-10-21 14:27:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
function generateOffer() {
|
|
|
|
peer_connection.createOffer().then(onLocalDescription).catch(setError);
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
// ICE candidate received from peer, add it to the peer connection
|
|
|
|
function onIncomingICE(ice) {
|
|
|
|
var candidate = new RTCIceCandidate(ice);
|
2018-03-31 16:54:15 +00:00
|
|
|
peer_connection.addIceCandidate(candidate).catch(setError);
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onServerMessage(event) {
|
|
|
|
console.log("Received " + event.data);
|
|
|
|
switch (event.data) {
|
|
|
|
case "HELLO":
|
|
|
|
setStatus("Registered with server, waiting for call");
|
|
|
|
return;
|
2020-11-27 09:16:52 +00:00
|
|
|
case "SESSION_OK":
|
|
|
|
setStatus("Starting negotiation");
|
2021-02-09 09:58:57 +00:00
|
|
|
if (wantRemoteOfferer()) {
|
|
|
|
ws_conn.send("OFFER_REQUEST");
|
|
|
|
setStatus("Sent OFFER_REQUEST, waiting for offer");
|
|
|
|
return;
|
|
|
|
}
|
2020-11-27 09:16:52 +00:00
|
|
|
if (!peer_connection)
|
|
|
|
createCall(null).then (generateOffer);
|
|
|
|
return;
|
2021-02-09 08:57:31 +00:00
|
|
|
case "OFFER_REQUEST":
|
|
|
|
// The peer wants us to set up and then send an offer
|
|
|
|
if (!peer_connection)
|
|
|
|
createCall(null).then (generateOffer);
|
|
|
|
return;
|
2017-10-21 14:27:29 +00:00
|
|
|
default:
|
|
|
|
if (event.data.startsWith("ERROR")) {
|
|
|
|
handleIncomingError(event.data);
|
|
|
|
return;
|
|
|
|
}
|
2021-02-09 08:57:31 +00:00
|
|
|
// Handle incoming JSON SDP and ICE messages
|
|
|
|
try {
|
|
|
|
msg = JSON.parse(event.data);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof SyntaxError) {
|
|
|
|
handleIncomingError("Error parsing incoming JSON: " + event.data);
|
2020-03-04 16:03:17 +00:00
|
|
|
} else {
|
2021-02-09 08:57:31 +00:00
|
|
|
handleIncomingError("Unknown error parsing response: " + event.data);
|
2020-03-04 16:03:17 +00:00
|
|
|
}
|
2021-02-09 08:57:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Incoming JSON signals the beginning of a call
|
|
|
|
if (!peer_connection)
|
|
|
|
createCall(msg);
|
|
|
|
|
|
|
|
if (msg.sdp != null) {
|
|
|
|
onIncomingSDP(msg.sdp);
|
|
|
|
} else if (msg.ice != null) {
|
|
|
|
onIncomingICE(msg.ice);
|
|
|
|
} else {
|
|
|
|
handleIncomingError("Unknown incoming JSON: " + msg);
|
2020-11-25 17:34:48 +00:00
|
|
|
}
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onServerClose(event) {
|
2018-03-31 20:23:44 +00:00
|
|
|
setStatus('Disconnected from server');
|
2018-03-31 17:31:32 +00:00
|
|
|
resetVideo();
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2018-03-31 20:22:46 +00:00
|
|
|
if (peer_connection) {
|
2017-10-21 14:27:29 +00:00
|
|
|
peer_connection.close();
|
|
|
|
peer_connection = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset after a second
|
|
|
|
window.setTimeout(websocketServerConnect, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onServerError(event) {
|
2018-03-31 16:54:15 +00:00
|
|
|
setError("Unable to connect to server, did you add an exception for the certificate?")
|
2017-10-21 14:27:29 +00:00
|
|
|
// Retry after 3 seconds
|
|
|
|
window.setTimeout(websocketServerConnect, 3000);
|
|
|
|
}
|
|
|
|
|
2018-03-31 08:22:02 +00:00
|
|
|
function getLocalStream() {
|
2018-03-31 20:23:44 +00:00
|
|
|
var constraints;
|
2018-03-31 19:37:51 +00:00
|
|
|
var textarea = document.getElementById('constraints');
|
2018-03-31 20:23:44 +00:00
|
|
|
try {
|
|
|
|
constraints = JSON.parse(textarea.value);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
setError('ERROR parsing constraints: ' + e.message + ', using default constraints');
|
|
|
|
constraints = default_constraints;
|
|
|
|
}
|
2018-03-31 19:37:51 +00:00
|
|
|
console.log(JSON.stringify(constraints));
|
2018-03-31 08:22:02 +00:00
|
|
|
|
|
|
|
// Add local stream
|
|
|
|
if (navigator.mediaDevices.getUserMedia) {
|
|
|
|
return navigator.mediaDevices.getUserMedia(constraints);
|
|
|
|
} else {
|
|
|
|
errorUserMediaHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
function websocketServerConnect() {
|
|
|
|
connect_attempts++;
|
|
|
|
if (connect_attempts > 3) {
|
2018-03-31 16:54:15 +00:00
|
|
|
setError("Too many connection attempts, aborting. Refresh page to try again");
|
2017-10-21 14:27:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-03-31 20:23:44 +00:00
|
|
|
// Clear errors in the status span
|
|
|
|
var span = document.getElementById("status");
|
|
|
|
span.classList.remove('error');
|
|
|
|
span.textContent = '';
|
2018-03-31 19:37:51 +00:00
|
|
|
// Populate constraints
|
|
|
|
var textarea = document.getElementById('constraints');
|
|
|
|
if (textarea.value == '')
|
|
|
|
textarea.value = JSON.stringify(default_constraints);
|
2018-03-31 19:09:48 +00:00
|
|
|
// Fetch the peer id to use
|
2018-03-31 19:58:02 +00:00
|
|
|
peer_id = default_peer_id || getOurId();
|
2018-03-31 19:09:48 +00:00
|
|
|
ws_port = ws_port || '8443';
|
2017-11-22 13:21:36 +00:00
|
|
|
if (window.location.protocol.startsWith ("file")) {
|
2018-03-31 19:09:48 +00:00
|
|
|
ws_server = ws_server || "127.0.0.1";
|
2017-11-22 13:21:36 +00:00
|
|
|
} else if (window.location.protocol.startsWith ("http")) {
|
2018-03-31 19:09:48 +00:00
|
|
|
ws_server = ws_server || window.location.hostname;
|
2017-11-22 13:21:36 +00:00
|
|
|
} else {
|
|
|
|
throw new Error ("Don't know how to connect to the signalling server with uri" + window.location);
|
|
|
|
}
|
2018-03-31 19:09:48 +00:00
|
|
|
var ws_url = 'wss://' + ws_server + ':' + ws_port
|
|
|
|
setStatus("Connecting to server " + ws_url);
|
|
|
|
ws_conn = new WebSocket(ws_url);
|
2017-10-21 14:27:29 +00:00
|
|
|
/* When connected, immediately register with the server */
|
|
|
|
ws_conn.addEventListener('open', (event) => {
|
|
|
|
document.getElementById("peer-id").textContent = peer_id;
|
|
|
|
ws_conn.send('HELLO ' + peer_id);
|
|
|
|
setStatus("Registering with server");
|
2021-02-09 09:46:11 +00:00
|
|
|
setConnectButtonState("Connect");
|
2017-10-21 14:27:29 +00:00
|
|
|
});
|
|
|
|
ws_conn.addEventListener('error', onServerError);
|
|
|
|
ws_conn.addEventListener('message', onServerMessage);
|
|
|
|
ws_conn.addEventListener('close', onServerClose);
|
|
|
|
}
|
|
|
|
|
2019-04-11 21:33:50 +00:00
|
|
|
function onRemoteTrack(event) {
|
|
|
|
if (getVideoElement().srcObject !== event.streams[0]) {
|
|
|
|
console.log('Incoming stream');
|
|
|
|
getVideoElement().srcObject = event.streams[0];
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorUserMediaHandler() {
|
2018-03-31 16:54:15 +00:00
|
|
|
setError("Browser doesn't support getUserMedia!");
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 13:03:43 +00:00
|
|
|
const handleDataChannelOpen = (event) =>{
|
|
|
|
console.log("dataChannel.OnOpen", event);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDataChannelMessageReceived = (event) =>{
|
|
|
|
console.log("dataChannel.OnMessage:", event, event.data.type);
|
|
|
|
|
|
|
|
setStatus("Received data channel message");
|
|
|
|
if (typeof event.data === 'string' || event.data instanceof String) {
|
|
|
|
console.log('Incoming string message: ' + event.data);
|
|
|
|
textarea = document.getElementById("text")
|
|
|
|
textarea.value = textarea.value + '\n' + event.data
|
|
|
|
} else {
|
|
|
|
console.log('Incoming data message');
|
|
|
|
}
|
|
|
|
send_channel.send("Hi! (from browser)");
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDataChannelError = (error) =>{
|
|
|
|
console.log("dataChannel.OnError:", error);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleDataChannelClose = (event) =>{
|
|
|
|
console.log("dataChannel.OnClose", event);
|
|
|
|
};
|
|
|
|
|
|
|
|
function onDataChannel(event) {
|
|
|
|
setStatus("Data channel created");
|
|
|
|
let receiveChannel = event.channel;
|
|
|
|
receiveChannel.onopen = handleDataChannelOpen;
|
|
|
|
receiveChannel.onmessage = handleDataChannelMessageReceived;
|
|
|
|
receiveChannel.onerror = handleDataChannelError;
|
|
|
|
receiveChannel.onclose = handleDataChannelClose;
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
function createCall(msg) {
|
|
|
|
// Reset connection attempts because we connected successfully
|
|
|
|
connect_attempts = 0;
|
|
|
|
|
2018-03-31 16:54:15 +00:00
|
|
|
console.log('Creating RTCPeerConnection');
|
|
|
|
|
2017-10-21 14:27:29 +00:00
|
|
|
peer_connection = new RTCPeerConnection(rtc_configuration);
|
2018-09-21 13:03:43 +00:00
|
|
|
send_channel = peer_connection.createDataChannel('label', null);
|
|
|
|
send_channel.onopen = handleDataChannelOpen;
|
|
|
|
send_channel.onmessage = handleDataChannelMessageReceived;
|
|
|
|
send_channel.onerror = handleDataChannelError;
|
|
|
|
send_channel.onclose = handleDataChannelClose;
|
|
|
|
peer_connection.ondatachannel = onDataChannel;
|
2019-04-11 21:33:50 +00:00
|
|
|
peer_connection.ontrack = onRemoteTrack;
|
2017-10-21 14:27:29 +00:00
|
|
|
/* Send our video/audio to the other peer */
|
2018-03-31 17:31:32 +00:00
|
|
|
local_stream_promise = getLocalStream().then((stream) => {
|
2018-03-31 08:22:02 +00:00
|
|
|
console.log('Adding local stream');
|
|
|
|
peer_connection.addStream(stream);
|
2018-03-31 19:11:40 +00:00
|
|
|
return stream;
|
2018-03-31 16:54:15 +00:00
|
|
|
}).catch(setError);
|
2017-10-21 14:27:29 +00:00
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
if (msg != null && !msg.sdp) {
|
2017-10-21 14:27:29 +00:00
|
|
|
console.log("WARNING: First message wasn't an SDP message!?");
|
|
|
|
}
|
|
|
|
|
|
|
|
peer_connection.onicecandidate = (event) => {
|
2020-11-25 17:34:48 +00:00
|
|
|
// We have a candidate, send it to the remote party with the
|
|
|
|
// same uuid
|
|
|
|
if (event.candidate == null) {
|
|
|
|
console.log("ICE Candidate was null, done");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ws_conn.send(JSON.stringify({'ice': event.candidate}));
|
2017-10-21 14:27:29 +00:00
|
|
|
};
|
|
|
|
|
2020-03-04 16:03:17 +00:00
|
|
|
if (msg != null)
|
|
|
|
setStatus("Created peer connection for call, waiting for SDP");
|
|
|
|
|
2021-02-09 09:46:11 +00:00
|
|
|
setConnectButtonState("Disconnect");
|
2020-03-04 16:03:17 +00:00
|
|
|
return local_stream_promise;
|
2017-10-21 14:27:29 +00:00
|
|
|
}
|