Squashed 'third_party/rawrtc/rawrtc/' content from commit aa3ae4b24
Change-Id: I38a655a4259b62f591334e90a1315bd4e7e4d8ec
git-subtree-dir: third_party/rawrtc/rawrtc
git-subtree-split: aa3ae4b247275cc6e69c30613b3a4ba7fdc82d1b
diff --git a/htdocs/ortc/index.html b/htdocs/ortc/index.html
new file mode 100644
index 0000000..f5e46c6
--- /dev/null
+++ b/htdocs/ortc/index.html
@@ -0,0 +1,92 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>ORTC/WebRTC Interop Data Channel Example</title>
+</head>
+<style>
+textarea {
+}
+</style>
+<body>
+ <textarea cols="120" rows="20" id="local-parameters" onclick="this.select();" readonly></textarea>
+ <textarea cols="120" rows="20" id="remote-parameters"></textarea><br />
+ <button type="button" onclick="start(peer);" id="start" disabled>Start</button>
+
+ <script type="text/javascript" src="sdp.js"></script>
+ <script type="text/javascript" src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
+ <script type="text/javascript" src="webrtc-rawrtc.js"></script>
+ <script type="text/javascript">
+ 'use strict';
+
+ let localParameters = document.getElementById('local-parameters');
+ let remoteParameters = document.getElementById('remote-parameters');
+ let startButton = document.getElementById('start');
+
+ function getURLParameter(name) {
+ return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
+ }
+
+ function setRemoteParameters(peer) {
+ // Parse and apply the remote parameters
+ let parameters = JSON.parse(remoteParameters.value);
+ console.log('Remote parameters:', parameters);
+ peer.setRemoteParameters(parameters)
+ .then((parameters) => {
+ // Generate local parameters if controlled
+ if (peer instanceof ControlledPeer) {
+ getLocalParameters(peer);
+ }
+ })
+ .catch((error) => {
+ console.error(error);
+ });
+ };
+
+ function getLocalParameters(peer) {
+ // Generate and show the local parameters
+ peer.getLocalParameters()
+ .then((parameters) => {
+ console.log('Local parameters:', parameters);
+ localParameters.value = JSON.stringify(parameters);
+ });
+ }
+
+ function createPeer(controllingParameter) {
+ let controlling = controllingParameter == 'true' || controllingParameter == '1';
+ console.log('Role: ICE-Controll' + (controlling ? 'ing' : 'ed'));
+
+ // Create peer depending on the role
+ let peer = controlling ? new ControllingPeer() : new ControlledPeer();
+ peer.createPeerConnection();
+ let cat = peer.createDataChannel(peer.pc.createDataChannel('cat-noises', {
+ ordered: true,
+ negotiated: true,
+ id: 0
+ }));
+ peer.createDataChannel();
+
+ // Create local parameters if we are the controlling peer.
+ // Keep in mind this still uses offer/answer in the background, thus this
+ // limitation which does not exist for ORTC but does for WebRTC.
+ if (controlling) {
+ getLocalParameters(peer);
+ }
+
+ return peer;
+ }
+
+ function start(peer) {
+ // Apply remote parameters
+ // For the controlled peer, this will automatically generate local parameters
+ setRemoteParameters(peer);
+ startButton.disabled = true;
+ }
+
+ // Create peer
+ // Determine role from GET parameter (?controlling=true|false)
+ let peer = createPeer(getURLParameter('controlling'));
+ startButton.disabled = false;
+ </script>
+</body>
+</html>
diff --git a/htdocs/ortc/sdp.js b/htdocs/ortc/sdp.js
new file mode 100644
index 0000000..0dd2472
--- /dev/null
+++ b/htdocs/ortc/sdp.js
@@ -0,0 +1,549 @@
+/*
+ * Copyright Philipp Hancke
+ * License: MIT
+ * Source: https://github.com/fippo/sdp
+ *
+ * Extended by Lennart Grahl
+ */
+
+/* eslint-env node */
+'use strict';
+
+// SDP helpers.
+var SDPUtils = {};
+
+// Generate an alphanumeric identifier for cname or mids.
+// TODO: use UUIDs instead? https://gist.github.com/jed/982883
+SDPUtils.generateIdentifier = function() {
+ return Math.random().toString(36).substr(2, 10);
+};
+
+// The RTCP CNAME used by all peerconnections from the same JS.
+SDPUtils.localCName = SDPUtils.generateIdentifier();
+
+// Splits SDP into lines, dealing with both CRLF and LF.
+SDPUtils.splitLines = function(blob) {
+ return blob.trim().split('\n').map(function(line) {
+ return line.trim();
+ });
+};
+// Splits SDP into sessionpart and mediasections. Ensures CRLF.
+SDPUtils.splitSections = function(blob) {
+ var parts = blob.split('\nm=');
+ return parts.map(function(part, index) {
+ return (index > 0 ? 'm=' + part : part).trim() + '\r\n';
+ });
+};
+
+// Returns lines that start with a certain prefix.
+SDPUtils.matchPrefix = function(blob, prefix) {
+ return SDPUtils.splitLines(blob).filter(function(line) {
+ return line.indexOf(prefix) === 0;
+ });
+};
+
+// Parses an ICE candidate line. Sample input:
+// candidate:702786350 2 udp 41819902 8.8.8.8 60769 typ relay raddr 8.8.8.8
+// rport 55996"
+SDPUtils.parseCandidate = function(line) {
+ var parts;
+ // Parse both variants.
+ if (line.indexOf('a=candidate:') === 0) {
+ parts = line.substring(12).split(' ');
+ } else {
+ parts = line.substring(10).split(' ');
+ }
+
+ var candidate = {
+ foundation: parts[0],
+ component: parts[1],
+ protocol: parts[2].toLowerCase(),
+ priority: parseInt(parts[3], 10),
+ ip: parts[4],
+ port: parseInt(parts[5], 10),
+ // skip parts[6] == 'typ'
+ type: parts[7]
+ };
+
+ for (var i = 8; i < parts.length; i += 2) {
+ switch (parts[i]) {
+ case 'raddr':
+ candidate.relatedAddress = parts[i + 1];
+ break;
+ case 'rport':
+ candidate.relatedPort = parseInt(parts[i + 1], 10);
+ break;
+ case 'tcptype':
+ candidate.tcpType = parts[i + 1];
+ break;
+ default: // Unknown extensions are silently ignored.
+ break;
+ }
+ }
+ return candidate;
+};
+
+// Translates a candidate object into SDP candidate attribute.
+SDPUtils.writeCandidate = function(candidate) {
+ var sdp = [];
+ sdp.push(candidate.foundation);
+ sdp.push(candidate.component);
+ sdp.push(candidate.protocol.toUpperCase());
+ sdp.push(candidate.priority);
+ sdp.push(candidate.ip);
+ sdp.push(candidate.port);
+
+ var type = candidate.type;
+ sdp.push('typ');
+ sdp.push(type);
+ if (type !== 'host' && candidate.relatedAddress &&
+ candidate.relatedPort) {
+ sdp.push('raddr');
+ sdp.push(candidate.relatedAddress); // was: relAddr
+ sdp.push('rport');
+ sdp.push(candidate.relatedPort); // was: relPort
+ }
+ if (candidate.tcpType && candidate.protocol.toLowerCase() === 'tcp') {
+ sdp.push('tcptype');
+ sdp.push(candidate.tcpType);
+ }
+ return 'candidate:' + sdp.join(' ');
+};
+
+// Parses an rtpmap line, returns RTCRtpCoddecParameters. Sample input:
+// a=rtpmap:111 opus/48000/2
+SDPUtils.parseRtpMap = function(line) {
+ var parts = line.substr(9).split(' ');
+ var parsed = {
+ payloadType: parseInt(parts.shift(), 10) // was: id
+ };
+
+ parts = parts[0].split('/');
+
+ parsed.name = parts[0];
+ parsed.clockRate = parseInt(parts[1], 10); // was: clockrate
+ // was: channels
+ parsed.numChannels = parts.length === 3 ? parseInt(parts[2], 10) : 1;
+ return parsed;
+};
+
+// Generate an a=rtpmap line from RTCRtpCodecCapability or
+// RTCRtpCodecParameters.
+SDPUtils.writeRtpMap = function(codec) {
+ var pt = codec.payloadType;
+ if (codec.preferredPayloadType !== undefined) {
+ pt = codec.preferredPayloadType;
+ }
+ return 'a=rtpmap:' + pt + ' ' + codec.name + '/' + codec.clockRate +
+ (codec.numChannels !== 1 ? '/' + codec.numChannels : '') + '\r\n';
+};
+
+// Parses an a=extmap line (headerextension from RFC 5285). Sample input:
+// a=extmap:2 urn:ietf:params:rtp-hdrext:toffset
+SDPUtils.parseExtmap = function(line) {
+ var parts = line.substr(9).split(' ');
+ return {
+ id: parseInt(parts[0], 10),
+ uri: parts[1]
+ };
+};
+
+// Generates a=extmap line from RTCRtpHeaderExtensionParameters or
+// RTCRtpHeaderExtension.
+SDPUtils.writeExtmap = function(headerExtension) {
+ return 'a=extmap:' + (headerExtension.id || headerExtension.preferredId) +
+ ' ' + headerExtension.uri + '\r\n';
+};
+
+// Parses an ftmp line, returns dictionary. Sample input:
+// a=fmtp:96 vbr=on;cng=on
+// Also deals with vbr=on; cng=on
+SDPUtils.parseFmtp = function(line) {
+ var parsed = {};
+ var kv;
+ var parts = line.substr(line.indexOf(' ') + 1).split(';');
+ for (var j = 0; j < parts.length; j++) {
+ kv = parts[j].trim().split('=');
+ parsed[kv[0].trim()] = kv[1];
+ }
+ return parsed;
+};
+
+// Generates an a=ftmp line from RTCRtpCodecCapability or RTCRtpCodecParameters.
+SDPUtils.writeFmtp = function(codec) {
+ var line = '';
+ var pt = codec.payloadType;
+ if (codec.preferredPayloadType !== undefined) {
+ pt = codec.preferredPayloadType;
+ }
+ if (codec.parameters && Object.keys(codec.parameters).length) {
+ var params = [];
+ Object.keys(codec.parameters).forEach(function(param) {
+ params.push(param + '=' + codec.parameters[param]);
+ });
+ line += 'a=fmtp:' + pt + ' ' + params.join(';') + '\r\n';
+ }
+ return line;
+};
+
+// Parses an rtcp-fb line, returns RTCPRtcpFeedback object. Sample input:
+// a=rtcp-fb:98 nack rpsi
+SDPUtils.parseRtcpFb = function(line) {
+ var parts = line.substr(line.indexOf(' ') + 1).split(' ');
+ return {
+ type: parts.shift(),
+ parameter: parts.join(' ')
+ };
+};
+// Generate a=rtcp-fb lines from RTCRtpCodecCapability or RTCRtpCodecParameters.
+SDPUtils.writeRtcpFb = function(codec) {
+ var lines = '';
+ var pt = codec.payloadType;
+ if (codec.preferredPayloadType !== undefined) {
+ pt = codec.preferredPayloadType;
+ }
+ if (codec.rtcpFeedback && codec.rtcpFeedback.length) {
+ // FIXME: special handling for trr-int?
+ codec.rtcpFeedback.forEach(function(fb) {
+ lines += 'a=rtcp-fb:' + pt + ' ' + fb.type +
+ (fb.parameter && fb.parameter.length ? ' ' + fb.parameter : '') +
+ '\r\n';
+ });
+ }
+ return lines;
+};
+
+// Parses an RFC 5576 ssrc media attribute. Sample input:
+// a=ssrc:3735928559 cname:something
+SDPUtils.parseSsrcMedia = function(line) {
+ var sp = line.indexOf(' ');
+ var parts = {
+ ssrc: parseInt(line.substr(7, sp - 7), 10)
+ };
+ var colon = line.indexOf(':', sp);
+ if (colon > -1) {
+ parts.attribute = line.substr(sp + 1, colon - sp - 1);
+ parts.value = line.substr(colon + 1);
+ } else {
+ parts.attribute = line.substr(sp + 1);
+ }
+ return parts;
+};
+
+// Extracts SCTP capabilities from SDP media section or sessionpart.
+SDPUtils.getSctpCapabilities = function(mediaSection, sessionpart) {
+ var lines = SDPUtils.splitLines(mediaSection);
+ // Search in session part, too.
+ lines = lines.concat(SDPUtils.splitLines(sessionpart));
+ var maxMessageSize = lines.filter(function(line) {
+ return line.indexOf('a=max-message-size:') === 0;
+ });
+ // TODO: Use 65536 once Firefox has disabled PPID-based fragmentation
+ // see: https://lgrahl.de/articles/demystifying-webrtc-dc-size-limit.html
+ maxMessageSize = maxMessageSize.length ? parseInt(maxMessageSize[0].substr(19)) : 16384;
+ return {
+ maxMessageSize: maxMessageSize
+ };
+};
+
+// Serializes SCTP capabilities to SDP.
+SDPUtils.writeSctpCapabilities = function(capabilities) {
+ return 'a=max-message-size:' + capabilities.maxMessageSize + '\r\n'; // (03)
+};
+
+// Extracts SCTP port from SDP media section or sessionpart.
+SDPUtils.getSctpPort = function(mediaSection, sessionpart) {
+ var lines = SDPUtils.splitLines(mediaSection);
+ // Search in session part, too.
+ lines = lines.concat(SDPUtils.splitLines(sessionpart));
+ var port = lines.filter(function(line) {
+ return line.indexOf('a=sctp-port:') === 0;
+ });
+ port = port.length ? parseInt(port[0].substr(12), 10) : 5000;
+ return port;
+};
+
+// Serializes SCTP port to SDP.
+SDPUtils.writeSctpPort = function(port) {
+ // TODO: Enable (chromium can't cope with it)
+ // return 'a=sctp-port:' + (port ? port : 5000) + '\r\n'; // (03)
+ return '';
+};
+
+// Extracts DTLS parameters from SDP media section or sessionpart.
+// FIXME: for consistency with other functions this should only
+// get the fingerprint line as input. See also getIceParameters.
+SDPUtils.getDtlsParameters = function(mediaSection, sessionpart) {
+ var lines = SDPUtils.splitLines(mediaSection);
+ // Search in session part, too.
+ lines = lines.concat(SDPUtils.splitLines(sessionpart));
+ var fpLine = lines.filter(function(line) {
+ return line.indexOf('a=fingerprint:') === 0;
+ })[0].substr(14);
+ // Note: a=setup line is ignored since we use the 'auto' role.
+ var dtlsParameters = {
+ role: 'auto',
+ fingerprints: [{
+ algorithm: fpLine.split(' ')[0],
+ value: fpLine.split(' ')[1]
+ }]
+ };
+ return dtlsParameters;
+};
+
+// Serializes DTLS parameters to SDP.
+SDPUtils.writeDtlsParameters = function(params, setupType) {
+ var sdp = 'a=setup:' + setupType + '\r\n';
+ params.fingerprints.forEach(function(fp) {
+ sdp += 'a=fingerprint:' + fp.algorithm + ' ' + fp.value + '\r\n';
+ });
+ return sdp;
+};
+// Parses ICE information from SDP media section or sessionpart.
+// FIXME: for consistency with other functions this should only
+// get the ice-ufrag and ice-pwd lines as input.
+SDPUtils.getIceParameters = function(mediaSection, sessionpart) {
+ var lines = SDPUtils.splitLines(mediaSection);
+ // Search in session part, too.
+ lines = lines.concat(SDPUtils.splitLines(sessionpart));
+ var iceParameters = {
+ usernameFragment: lines.filter(function(line) {
+ return line.indexOf('a=ice-ufrag:') === 0;
+ })[0].substr(12),
+ password: lines.filter(function(line) {
+ return line.indexOf('a=ice-pwd:') === 0;
+ })[0].substr(10)
+ };
+ return iceParameters;
+};
+
+// Serializes ICE parameters to SDP.
+SDPUtils.writeIceParameters = function(params) {
+ return 'a=ice-ufrag:' + params.usernameFragment + '\r\n' +
+ 'a=ice-pwd:' + params.password + '\r\n';
+};
+
+// Parses the SDP media section and returns RTCRtpParameters.
+SDPUtils.parseRtpParameters = function(mediaSection) {
+ var description = {
+ codecs: [],
+ headerExtensions: [],
+ fecMechanisms: [],
+ rtcp: []
+ };
+ var lines = SDPUtils.splitLines(mediaSection);
+ var mline = lines[0].split(' ');
+ for (var i = 3; i < mline.length; i++) { // find all codecs from mline[3..]
+ var pt = mline[i];
+ var rtpmapline = SDPUtils.matchPrefix(
+ mediaSection, 'a=rtpmap:' + pt + ' ')[0];
+ if (rtpmapline) {
+ var codec = SDPUtils.parseRtpMap(rtpmapline);
+ var fmtps = SDPUtils.matchPrefix(
+ mediaSection, 'a=fmtp:' + pt + ' ');
+ // Only the first a=fmtp:<pt> is considered.
+ codec.parameters = fmtps.length ? SDPUtils.parseFmtp(fmtps[0]) : {};
+ codec.rtcpFeedback = SDPUtils.matchPrefix(
+ mediaSection, 'a=rtcp-fb:' + pt + ' ')
+ .map(SDPUtils.parseRtcpFb);
+ description.codecs.push(codec);
+ // parse FEC mechanisms from rtpmap lines.
+ switch (codec.name.toUpperCase()) {
+ case 'RED':
+ case 'ULPFEC':
+ description.fecMechanisms.push(codec.name.toUpperCase());
+ break;
+ default: // only RED and ULPFEC are recognized as FEC mechanisms.
+ break;
+ }
+ }
+ }
+ SDPUtils.matchPrefix(mediaSection, 'a=extmap:').forEach(function(line) {
+ description.headerExtensions.push(SDPUtils.parseExtmap(line));
+ });
+ // FIXME: parse rtcp.
+ return description;
+};
+
+// Generates parts of the SDP media section describing the capabilities /
+// parameters.
+SDPUtils.writeRtpDescription = function(kind, caps) {
+ var sdp = '';
+
+ // Build the mline.
+ sdp += 'm=' + kind + ' ';
+ sdp += caps.codecs.length > 0 ? '9' : '0'; // reject if no codecs.
+ sdp += ' UDP/TLS/RTP/SAVPF ';
+ sdp += caps.codecs.map(function(codec) {
+ if (codec.preferredPayloadType !== undefined) {
+ return codec.preferredPayloadType;
+ }
+ return codec.payloadType;
+ }).join(' ') + '\r\n';
+
+ sdp += 'c=IN IP4 0.0.0.0\r\n';
+ sdp += 'a=rtcp:9 IN IP4 0.0.0.0\r\n';
+
+ // Add a=rtpmap lines for each codec. Also fmtp and rtcp-fb.
+ caps.codecs.forEach(function(codec) {
+ sdp += SDPUtils.writeRtpMap(codec);
+ sdp += SDPUtils.writeFmtp(codec);
+ sdp += SDPUtils.writeRtcpFb(codec);
+ });
+ var maxptime = 0;
+ caps.codecs.forEach(function(codec) {
+ if (codec.maxptime > maxptime) {
+ maxptime = codec.maxptime;
+ }
+ });
+ if (maxptime > 0) {
+ sdp += 'a=maxptime:' + maxptime + '\r\n';
+ }
+ sdp += 'a=rtcp-mux\r\n';
+
+ caps.headerExtensions.forEach(function(extension) {
+ sdp += SDPUtils.writeExtmap(extension);
+ });
+ // FIXME: write fecMechanisms.
+ return sdp;
+};
+
+// Parses the SDP media section and returns an array of
+// RTCRtpEncodingParameters.
+SDPUtils.parseRtpEncodingParameters = function(mediaSection) {
+ var encodingParameters = [];
+ var description = SDPUtils.parseRtpParameters(mediaSection);
+ var hasRed = description.fecMechanisms.indexOf('RED') !== -1;
+ var hasUlpfec = description.fecMechanisms.indexOf('ULPFEC') !== -1;
+
+ // filter a=ssrc:... cname:, ignore PlanB-msid
+ var ssrcs = SDPUtils.matchPrefix(mediaSection, 'a=ssrc:')
+ .map(function(line) {
+ return SDPUtils.parseSsrcMedia(line);
+ })
+ .filter(function(parts) {
+ return parts.attribute === 'cname';
+ });
+ var primarySsrc = ssrcs.length > 0 && ssrcs[0].ssrc;
+ var secondarySsrc;
+
+ var flows = SDPUtils.matchPrefix(mediaSection, 'a=ssrc-group:FID')
+ .map(function(line) {
+ var parts = line.split(' ');
+ parts.shift();
+ return parts.map(function(part) {
+ return parseInt(part, 10);
+ });
+ });
+ if (flows.length > 0 && flows[0].length > 1 && flows[0][0] === primarySsrc) {
+ secondarySsrc = flows[0][1];
+ }
+
+ description.codecs.forEach(function(codec) {
+ if (codec.name.toUpperCase() === 'RTX' && codec.parameters.apt) {
+ var encParam = {
+ ssrc: primarySsrc,
+ codecPayloadType: parseInt(codec.parameters.apt, 10),
+ rtx: {
+ payloadType: codec.payloadType,
+ ssrc: secondarySsrc
+ }
+ };
+ encodingParameters.push(encParam);
+ if (hasRed) {
+ encParam = JSON.parse(JSON.stringify(encParam));
+ encParam.fec = {
+ ssrc: secondarySsrc,
+ mechanism: hasUlpfec ? 'red+ulpfec' : 'red'
+ };
+ encodingParameters.push(encParam);
+ }
+ }
+ });
+ if (encodingParameters.length === 0 && primarySsrc) {
+ encodingParameters.push({
+ ssrc: primarySsrc
+ });
+ }
+
+ // we support both b=AS and b=TIAS but interpret AS as TIAS.
+ var bandwidth = SDPUtils.matchPrefix(mediaSection, 'b=');
+ if (bandwidth.length) {
+ if (bandwidth[0].indexOf('b=TIAS:') === 0) {
+ bandwidth = parseInt(bandwidth[0].substr(7), 10);
+ } else if (bandwidth[0].indexOf('b=AS:') === 0) {
+ bandwidth = parseInt(bandwidth[0].substr(5), 10);
+ }
+ encodingParameters.forEach(function(params) {
+ params.maxBitrate = bandwidth;
+ });
+ }
+ return encodingParameters;
+};
+
+SDPUtils.writeSessionBoilerplate = function() {
+ // FIXME: sess-id should be an NTP timestamp.
+ return 'v=0\r\n' +
+ 'o=thisisadapterortc 8169639915646943137 2 IN IP4 127.0.0.1\r\n' +
+ 's=-\r\n' +
+ 't=0 0\r\n';
+};
+
+SDPUtils.writeMediaSection = function(transceiver, caps, type, stream) {
+ var sdp = SDPUtils.writeRtpDescription(transceiver.kind, caps);
+
+ // Map ICE parameters (ufrag, pwd) to SDP.
+ sdp += SDPUtils.writeIceParameters(
+ transceiver.iceGatherer.getLocalParameters());
+
+ // Map DTLS parameters to SDP.
+ sdp += SDPUtils.writeDtlsParameters(
+ transceiver.dtlsTransport.getLocalParameters(),
+ type === 'offer' ? 'actpass' : 'active');
+
+ sdp += 'a=mid:' + transceiver.mid + '\r\n';
+
+ if (transceiver.rtpSender && transceiver.rtpReceiver) {
+ sdp += 'a=sendrecv\r\n';
+ } else if (transceiver.rtpSender) {
+ sdp += 'a=sendonly\r\n';
+ } else if (transceiver.rtpReceiver) {
+ sdp += 'a=recvonly\r\n';
+ } else {
+ sdp += 'a=inactive\r\n';
+ }
+
+ // FIXME: for RTX there might be multiple SSRCs. Not implemented in Edge yet.
+ if (transceiver.rtpSender) {
+ var msid = 'msid:' + stream.id + ' ' +
+ transceiver.rtpSender.track.id + '\r\n';
+ sdp += 'a=' + msid;
+ sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
+ ' ' + msid;
+ }
+ // FIXME: this should be written by writeRtpDescription.
+ sdp += 'a=ssrc:' + transceiver.sendEncodingParameters[0].ssrc +
+ ' cname:' + SDPUtils.localCName + '\r\n';
+ return sdp;
+};
+
+// Gets the direction from the mediaSection or the sessionpart.
+SDPUtils.getDirection = function(mediaSection, sessionpart) {
+ // Look for sendrecv, sendonly, recvonly, inactive, default to sendrecv.
+ var lines = SDPUtils.splitLines(mediaSection);
+ for (var i = 0; i < lines.length; i++) {
+ switch (lines[i]) {
+ case 'a=sendrecv':
+ case 'a=sendonly':
+ case 'a=recvonly':
+ case 'a=inactive':
+ return lines[i].substr(2);
+ default:
+ // FIXME: What should happen here?
+ }
+ }
+ if (sessionpart) {
+ return SDPUtils.getDirection(sessionpart);
+ }
+ return 'sendrecv';
+};
diff --git a/htdocs/ortc/webrtc-rawrtc.js b/htdocs/ortc/webrtc-rawrtc.js
new file mode 100644
index 0000000..bad70e0
--- /dev/null
+++ b/htdocs/ortc/webrtc-rawrtc.js
@@ -0,0 +1,396 @@
+'use strict';
+
+class Peer {
+ constructor() {
+ this.pc = null;
+ this.localMid = null;
+ this.localCandidates = [];
+ this.localParameters = null;
+ this.localDescription = null;
+ this.remoteParameters = null;
+ this.remoteDescription = null;
+ var _waitGatheringComplete = {};
+ _waitGatheringComplete.promise = new Promise((resolve, reject) => {
+ _waitGatheringComplete.resolve = resolve;
+ _waitGatheringComplete.reject = reject;
+ });
+ this._waitGatheringComplete = _waitGatheringComplete;
+ this.dc = {}
+ }
+
+ createPeerConnection() {
+ if (this.pc) {
+ console.warn('RTCPeerConnection already created');
+ return this.pc;
+ }
+
+ var self = this;
+
+ // Create peer connection
+ var pc = new RTCPeerConnection({
+ iceServers: [{
+ urls: 'stun:stun.l.google.com:19302'
+ }]
+ });
+
+ // Bind peer connection events
+ pc.onnegotiationneeded = function(event) {
+ console.log('Negotiation needed')
+ };
+ pc.onicecandidate = function(event) {
+ if (event.candidate) {
+ console.log('Gathered candidate:', event.candidate);
+ self.localCandidates.push(event.candidate);
+ } else {
+ console.log('Gathering complete');
+ self._waitGatheringComplete.resolve();
+ }
+ };
+ pc.onicecandidateerror = function(event) {
+ console.error('ICE candidate error:', event.errorText);
+ };
+ pc.onsignalingstatechange = function(event) {
+ console.log('Signaling state changed to:', pc.signalingState);
+ };
+ pc.oniceconnectionstatechange = function(event) {
+ console.log('ICE connection state changed to:', pc.iceConnectionState);
+ };
+ pc.onicegatheringstatechange = function(event) {
+ console.log('ICE gathering state changed to:', pc.iceGatheringState);
+ };
+ pc.onconnectionstatechange = function(event) {
+ console.log('Connection state changed to:', pc.connectionState);
+ };
+ pc.ondatachannel = function(event) {
+ self.createDataChannel(event.channel);
+ };
+
+ this.pc = pc;
+ return pc;
+ }
+
+ createDataChannel(dc) {
+ // Create data channel
+ dc = (typeof dc !== 'undefined') ? dc : this.pc.createDataChannel('example-channel', {
+ ordered: true
+ });
+
+ // Bind data channel events
+ dc.onopen = function(event) {
+ console.log('Data channel', dc.label, '(', dc.id, ')', 'open');
+ // Send 'hello'
+ dc.send('Hello from WebRTC on', navigator.userAgent);
+ };
+ dc.onbufferedamountlow = function(event) {
+ console.log('Data channel', dc.label, '(', dc.id, ')', 'buffered amount low');
+ };
+ dc.onerror = function(event) {
+ console.error('Data channel', dc.label, '(', dc.id, ')', 'error:', event);
+ };
+ dc.onclose = function(event) {
+ console.log('Data channel', dc.label, '(', dc.id, ')', 'closed');
+ };
+ dc.onmessage = function(event) {
+ var length = event.data.size || event.data.byteLength || event.data.length;
+ console.info('Data channel', dc.label, '(', dc.id, ')', 'message size:', length);
+ };
+
+ // Store channel
+ this.dc[dc.label] = dc;
+
+ return dc;
+ }
+
+ getLocalParameters() {
+ return new Promise((resolve, reject) => {
+ var error;
+ var self = this;
+
+ if (!this.localDescription) {
+ error = 'Must create offer/answer';
+ console.error(error);
+ reject(error);
+ return;
+ }
+
+ // Initialise parameters
+ var parameters = {
+ iceParameters: null,
+ iceCandidates: [],
+ dtlsParameters: null,
+ sctpParameters: null,
+ };
+
+ // Split sections
+ var sections = SDPUtils.splitSections(this.localDescription.sdp);
+ var session = sections.shift();
+
+ // Go through media sections
+ sections.forEach(function(mediaSection, sdpMLineIndex) {
+ // TODO: Ignore anything else but data transports
+
+ // Get mid
+ // TODO: This breaks with multiple transceivers
+ if (!self.localMid) {
+ var mid = SDPUtils.matchPrefix(mediaSection, 'a=mid:');
+ if (mid.length > 0) {
+ self.localMid = mid[0].substr(6);
+ }
+ }
+
+ // Get ICE parameters
+ if (!parameters.iceParameters) {
+ parameters.iceParameters = SDPUtils.getIceParameters(mediaSection, session);
+ }
+
+ // Get DTLS parameters
+ if (!parameters.dtlsParameters) {
+ parameters.dtlsParameters = SDPUtils.getDtlsParameters(mediaSection, session);
+ }
+
+ // Get SCTP parameters
+ if (!parameters.sctpParameters) {
+ parameters.sctpParameters = SDPUtils.getSctpCapabilities(mediaSection, session);
+ parameters.sctpParameters.port = SDPUtils.getSctpPort(mediaSection, session);
+ }
+ });
+
+ // ICE lite parameter
+ if (!parameters.iceParameters
+ || !parameters.dtlsParameters
+ || !parameters.sctpParameters) {
+ error = 'Could not retrieve required parameters from local description';
+ console.error(error);
+ reject(error);
+ return;
+ }
+ parameters.iceParameters.iceLite =
+ SDPUtils.matchPrefix(session, 'a=ice-lite').length > 0;
+
+ // Get ICE candidates
+ this._waitGatheringComplete.promise.then(() => {
+ // Add ICE candidates
+ for (var sdpCandidate of self.localCandidates) {
+ var candidate = SDPUtils.parseCandidate(sdpCandidate.candidate);
+ parameters.iceCandidates.push(candidate);
+ }
+
+ // Add ICE candidate complete sentinel
+ // parameters.iceCandidates.push({complete: true}); // TODO
+
+ // Done
+ resolve(parameters);
+ });
+ });
+ }
+
+ setRemoteParameters(parameters, type, localMid = null) {
+ return new Promise((resolve, reject) => {
+ if (this.remoteDescription) {
+ resolve(this.remoteDescription);
+ return;
+ }
+
+ if (!this.pc) {
+ console.error('Must create RTCPeerConnection instance');
+ return;
+ }
+
+ if (!localMid) {
+ localMid = this.localMid;
+ }
+ this.remoteParameters = parameters;
+
+ // Translate DTLS role
+ // TODO: This somehow didn't make it into SDPUtils
+ var setupType;
+ switch (parameters.dtlsParameters.role) {
+ case 'client':
+ setupType = 'active';
+ break;
+ case 'server':
+ setupType = 'passive';
+ break;
+ default:
+ // We map 'offer' to 'controlling' and 'answer' to 'controlled',
+ // so rawrtc will take 'server' if offering and 'client' if answering
+ // as specified by the ORTC spec
+ switch (type) {
+ case 'offer':
+ // WebRTC requires actpass in offer
+ setupType = 'actpass';
+ break;
+ case 'answer':
+ setupType = 'active';
+ break;
+ }
+ break;
+ }
+
+ // Write session section
+ var sdp = SDPUtils.writeSessionBoilerplate();
+ sdp += 'a=group:BUNDLE ' + localMid + '\r\n';
+ sdp += 'a=ice-options:trickle\r\n';
+ if (parameters.iceParameters.iceLite) {
+ sdp += 'a=ice-lite\r\n';
+ }
+
+ // Write media section
+ // TODO: Replace
+ // sdp += 'm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\n'; // (03)
+ sdp += 'm=application 9 DTLS/SCTP ' + parameters.sctpParameters.port + '\r\n'; // (01)
+ sdp += 'c=IN IP4 0.0.0.0\r\n';
+ sdp += 'a=mid:' + localMid + '\r\n';
+ sdp += 'a=sendrecv\r\n';
+
+ // SCTP part
+ sdp += SDPUtils.writeSctpCapabilities(parameters.sctpParameters);
+ sdp += SDPUtils.writeSctpPort(parameters.sctpParameters.port);
+ sdp += 'a=sctpmap:' + parameters.sctpParameters.port + ' webrtc-datachannel 65535\r\n'; // (01)
+
+ // DTLS part
+ sdp += SDPUtils.writeDtlsParameters(parameters.dtlsParameters, setupType);
+
+ // ICE part
+ sdp += 'a=connection:new\r\n'; // (03)
+ sdp += SDPUtils.writeIceParameters(parameters.iceParameters);
+
+ // Done
+ console.log('Remote description:\n' + sdp);
+
+ // Set remote description
+ this.pc.setRemoteDescription({type: type, sdp: sdp})
+ .then(() => {
+ console.log('Remote description:\n' + this.pc.remoteDescription.sdp);
+ this.remoteDescription = this.pc.remoteDescription;
+
+ // Add ICE candidates
+ for (var iceCandidate of parameters.iceCandidates) {
+ // Add component which ORTC doesn't have
+ // Note: We choose RTP as it doesn't actually matter for us
+ iceCandidate.component = 1; // RTP
+
+ // Create
+ var candidate = new RTCIceCandidate({
+ candidate: SDPUtils.writeCandidate(iceCandidate),
+ sdpMLineIndex: 0, // TODO: Fix
+ sdpMid: localMid // TODO: Fix
+ });
+
+ // Add
+ console.log(candidate.candidate);
+ this.pc.addIceCandidate(candidate)
+ .then(() => {
+ console.log('Added remote candidate', candidate);
+ });
+ }
+
+ // It's trickle ICE, no need to wait for candidates to be added
+ resolve();
+ })
+ .catch((error) => {
+ reject(error);
+ });
+ });
+ }
+
+ start() {}
+}
+
+class ControllingPeer extends Peer {
+ getLocalParameters() {
+ return new Promise((resolve, reject) => {
+ if (!this.pc) {
+ var error = 'Must create RTCPeerConnection instance';
+ console.error(error);
+ reject(error);
+ return;
+ }
+
+ var getLocalParameters = () => {
+ // Return parameters
+ super.getLocalParameters()
+ .then((parameters) => {
+ this.localParameters = parameters;
+ resolve(parameters);
+ })
+ .catch((error) => {
+ reject(error);
+ });
+ };
+
+ // Create offer
+ if (!this.localDescription) {
+ this.pc.createOffer()
+ .then((description) => {
+ return this.pc.setLocalDescription(description);
+ })
+ .then(() => {
+ console.log('Local description:\n' + this.pc.localDescription.sdp);
+ this.localDescription = this.pc.localDescription;
+ getLocalParameters();
+ })
+ .catch((error) => {
+ reject(error);
+ });
+ } else {
+ getLocalParameters();
+ }
+ });
+ }
+
+ setRemoteParameters(parameters, localMid = null) {
+ return super.setRemoteParameters(parameters, 'answer', localMid);
+ }
+}
+
+class ControlledPeer extends Peer {
+ getLocalParameters() {
+ return new Promise((resolve, reject) => {
+ var error;
+
+ if (!this.pc) {
+ error = 'Must create RTCPeerConnection instance';
+ console.error(error);
+ reject(error);
+ return;
+ }
+ if (!this.remoteDescription) {
+ error = 'Must have remote description';
+ console.error(error);
+ reject(error);
+ return;
+ }
+
+ var getLocalParameters = () => {
+ // Return parameters
+ super.getLocalParameters()
+ .then((parameters) => {
+ resolve(parameters);
+ })
+ .catch((error) => {
+ reject(error);
+ });
+ };
+
+ // Create answer
+ if (!this.localDescription) {
+ this.pc.createAnswer()
+ .then((description) => {
+ return this.pc.setLocalDescription(description);
+ })
+ .then(() => {
+ console.log('Local description:\n' + this.pc.localDescription.sdp);
+ this.localDescription = this.pc.localDescription;
+ getLocalParameters();
+ });
+ } else {
+ getLocalParameters();
+ }
+ });
+ }
+
+ setRemoteParameters(parameters, localMid = null) {
+ return super.setRemoteParameters(parameters, 'offer', localMid);
+ }
+}
diff --git a/htdocs/webrtc/index.html b/htdocs/webrtc/index.html
new file mode 100644
index 0000000..f8b27b9
--- /dev/null
+++ b/htdocs/webrtc/index.html
@@ -0,0 +1,275 @@
+<!DOCTYPE html>
+<html>
+<head lang="en">
+ <meta charset="UTF-8">
+ <title>WebRTC Data Channel Example</title>
+ <style>
+ @keyframes new-fade {
+ 0% {
+ background-color: #fffb85;
+ }
+ 100% {
+ background-color: transparent;
+ }
+ }
+
+ html, body {
+ margin: 0;
+ padding: 0;
+ height: 100%;
+ }
+ body {
+ height: 100vh;
+ display: flex;
+ }
+
+ main {
+ width: 100%;
+ display: flex;
+ flex-grow: 1;
+ flex-wrap: nowrap;
+ }
+
+ section, aside {
+ padding: 5px;
+ flex-grow: 1;
+ flex-basis: 0;
+ }
+ aside {
+ overflow-y: auto;
+ }
+
+ section textarea {
+ width: 100%;
+ }
+
+ #log {
+ margin: 0;
+ padding: 0;
+ flex-grow: 1;
+ flex-basis: 0;
+ }
+ #log > * {
+ margin: 2px 0;
+ padding: 0 6px;
+ animation: new-fade 1.5s ease-out 1;
+ }
+ #log .debug, #log .log {
+ border-left: 2px solid lightgrey;
+ }
+ #log .error {
+ border-left: 2px solid red;
+ }
+ #log .info {
+ border-left: 2px solid cornflowerblue;
+ }
+ #log .warn {
+ border-left: 2px solid orange;
+ }
+ </style>
+</head>
+<body>
+
+<main>
+ <section>
+ <form autocomplete="off">
+ <!-- Role -->
+ <label for="role">Role: Offering</label>
+ <input type="checkbox" id="role" checked>
+ <br>
+
+ <!-- Message size to be used -->
+ <label for="message-size">Message size (bytes):</label>
+ <input id="message-size" type="number" name="message-size" value="16384" step="16384">
+ <br>
+
+ <!-- WebSocket URL & Start button -->
+ <input type="text" id="ws-url" placeholder="Optional Server URL">
+ <button type="button" id="start" disabled>Start</button>
+ <br><hr>
+
+ <!-- Copy local description from this textarea -->
+ Copy local description:<br>
+ <textarea rows="15" id="local-description" disabled readonly></textarea>
+
+ <!-- Paste remote description into this textarea -->
+ Paste remote description:<br>
+ <textarea rows="15" id="remote-description" disabled></textarea>
+ <br>
+ </form>
+ </section>
+ <aside>
+ <pre id="log"></pre>
+ </aside>
+</main>
+
+<!-- Import dependencies -->
+<script src="https://webrtc.github.io/adapter/adapter-6.1.1.js"></script>
+<script src="signaling.js"></script>
+<script src="peerconnection.js"></script>
+
+<!-- UI code -->
+<script>
+'use strict';
+
+// Get elements
+const roleCheckbox = document.getElementById('role');
+const messageSizeInput = document.getElementById('message-size');
+const localDescriptionTextarea = document.getElementById('local-description');
+const remoteDescriptionTextarea = document.getElementById('remote-description');
+const wsUrlInput = document.getElementById('ws-url');
+const startButton = document.getElementById('start');
+const logPre = document.getElementById('log');
+
+// Load & store persistent values
+if (typeof(Storage) !== 'undefined') {
+ const persistentElements = [
+ ['role', 'checked', (value) => value === 'true'],
+ ['message-size', 'value'],
+ ['ws-url', 'value']
+ ];
+ for (const [id, property, transform] of persistentElements) {
+ let value = localStorage.getItem(id);
+ const element = document.getElementById(id);
+ if (transform !== undefined) {
+ value = transform(value);
+ }
+ if (value !== null) {
+ element[property] = value;
+ }
+ element.addEventListener('change', () => {
+ localStorage.setItem(id, element[property]);
+ });
+ }
+}
+
+// Display console logs in the browser as well
+for (const name of ['debug', 'error', 'info', 'log', 'warn']) {
+ const method = window.console[name];
+ window.console[name] = function() {
+ method.apply(null, arguments);
+ const entry = document.createElement('div');
+ entry.classList.add(name);
+ for (let i = 0; i < arguments.length; ++i) {
+ let item = arguments[i];
+ if (typeof arguments[i] === 'object') {
+ entry.innerHTML += JSON.stringify(item, null, 2) + ' ';
+ } else {
+ entry.innerHTML += item + ' ';
+ }
+ }
+ logPre.prepend(entry);
+ };
+}
+
+// Auto-select all text when clicking local description
+localDescriptionTextarea.addEventListener('click', function() {
+ this.select();
+});
+
+// Bind start button & enable
+startButton.addEventListener('click', () => {
+ roleCheckbox.disabled = true;
+ messageSizeInput.disabled = true;
+ wsUrlInput.disabled = true;
+ startButton.disabled = true;
+ localDescriptionTextarea.disabled = false;
+ start(roleCheckbox.checked);
+});
+startButton.disabled = false;
+
+const start = (offering) => {
+ console.info('Starting with role:', offering ? 'Offering' : 'Answering');
+
+ // Create signaling instance
+ const wsUrl = wsUrlInput.value;
+ let signaling;
+ if (wsUrl === '') {
+ signaling = new CopyPasteSignaling();
+ } else {
+ signaling = new WebSocketSignaling(wsUrl + (offering ? '/1' : '/0'));
+ }
+ signaling.onLocalDescriptionUpdate = (description) => {
+ localDescriptionTextarea.value = JSON.stringify(description);
+
+ // Enable remote description once local description has been set
+ remoteDescriptionTextarea.disabled = false;
+ };
+ signaling.onRemoteDescriptionUpdate = (description) => {
+ remoteDescriptionTextarea.value = JSON.stringify(description);
+ };
+
+ // Create peer connection instance
+ const pc = new WebRTCPeerConnection(signaling, offering);
+ window.pc = pc;
+
+ // Apply remote description when pasting
+ const onRemoteDescriptionTextareaChange = () => {
+ // Remove event listener
+ remoteDescriptionTextarea.oninput = null;
+ remoteDescriptionTextarea.onchange = null;
+
+ // Apply remote description once (needs to include candidates)
+ const description = JSON.parse(remoteDescriptionTextarea.value);
+ signaling.handleRemoteDescription(description, true)
+ .catch((error) => console.error(error));
+
+ // Make read-only
+ remoteDescriptionTextarea.readOnly = true;
+ };
+ remoteDescriptionTextarea.oninput = onRemoteDescriptionTextareaChange;
+ remoteDescriptionTextarea.onchange = onRemoteDescriptionTextareaChange;
+
+ // Enable remote description early (if not offering)
+ if (!offering) {
+ remoteDescriptionTextarea.disabled = false;
+ }
+
+ // Get message size
+ const messageSize = parseInt(messageSizeInput.value);
+ if (isNaN(messageSize)) {
+ throw 'Invalid message size value';
+ }
+
+ // Create data channels
+ const createDataChannelWithName = (
+ name, options = null, createOnOpenWithName = null, createOnOpenOptions = null
+ ) => {
+ const dc = pc.createDataChannel(name, options);
+ const defaultOnOpenHandler = dc.onopen;
+ dc.onopen = (event) => {
+ defaultOnOpenHandler(event);
+ if (createOnOpenWithName !== null) {
+ window.setTimeout(() => {
+ createDataChannelWithName(createOnOpenWithName, createOnOpenOptions);
+ }, 1000);
+ }
+ if (messageSize > pc.pc.sctp.maxMessageSize) {
+ console.warn(dc._name, 'message size (' + messageSize + ') > maximum message size' +
+ ' (' + pc.pc.sctp.maxMessageSize + ')');
+ }
+ let data = new Uint8Array(messageSize);
+ console.log(dc._name, 'outgoing message (' + data.byteLength + ' bytes)');
+ try {
+ dc.send(data);
+ } catch (error) {
+ if (error.name === 'TypeError') {
+ console.error(dc._name, 'message too large to send');
+ } else {
+ console.error(dc._name, 'Unknown error:', error.name);
+ }
+ }
+ };
+ };
+ createDataChannelWithName('cat-noises', {
+ negotiated: true,
+ id: 0,
+ }, 'dinosaur-noises');
+};
+
+// Introduction
+console.info("Hello! Press 'Start' when you're ready.");
+</script>
+
+</body>
+</html>
diff --git a/htdocs/webrtc/peerconnection.js b/htdocs/webrtc/peerconnection.js
new file mode 100644
index 0000000..9161966
--- /dev/null
+++ b/htdocs/webrtc/peerconnection.js
@@ -0,0 +1,95 @@
+'use strict';
+
+/**
+ * A WebRTC peer connection helper. Tightly coupled with the signaling
+ * class.
+ */
+class WebRTCPeerConnection {
+ constructor(signaling, offering, configuration = null) {
+ // Set default configuration (if none provided)
+ if (configuration === null) {
+ configuration = {
+ iceServers: [{
+ urls: 'stun:stun.services.mozilla.com',
+ }],
+ };
+ }
+
+ // Create peer connection and bind events
+ const pc = new RTCPeerConnection(configuration);
+ pc._offering = offering; // Meh!
+ signaling.pc = pc;
+ pc.onnegotiationneeded = async () => {
+ console.log('Negotiation needed');
+
+ // Create offer (if required)
+ if (offering) {
+ console.log('Creating offer');
+ const description = await pc.createOffer();
+ await pc.setLocalDescription(description);
+ signaling.handleLocalDescription(description);
+ }
+ };
+ pc.signalingstatechange = () => {
+ console.log('Signaling state:', pc.signalingState);
+ };
+ pc.oniceconnectionstatechange = () => {
+ console.log('ICE connection state:', pc.iceConnectionState);
+ };
+ pc.onicegatheringstatechange = () => {
+ console.log('ICE gathering state:', pc.iceGatheringState);
+ };
+ pc.onconnectionstatechange = () => {
+ console.log('Connection state:', pc.connectionState);
+ };
+ pc.onicecandidate = (event) => {
+ signaling.handleLocalCandidate(event.candidate);
+ };
+ pc.onicecandidateerror = (event) => {
+ console.error('ICE candidate error:', event);
+ };
+ pc.ondatachannel = (event) => {
+ const dc = event.channel;
+ console.log('Incoming data channel:', dc.label);
+
+ // Bind events
+ this.bindDataChannelEvents(dc);
+ };
+
+ // Store configuration & signalling instance
+ this.pc = pc;
+ this.dcs = {};
+ }
+
+ createDataChannel(name, options = null) {
+ const pc = this.pc;
+
+ // Create data channel and bind events
+ const dc = pc.createDataChannel(name, options);
+ this.bindDataChannelEvents(dc);
+
+ // Store data channel and return
+ this.dcs[name] = dc;
+ return dc;
+ }
+
+ bindDataChannelEvents(dc) {
+ dc._name = dc.label; // Meh!
+ dc.onopen = () => {
+ console.log(dc._name, 'open');
+ };
+ dc.onclose = () => {
+ console.log(dc._name, 'closed');
+ };
+ dc.onerror = (event) => {
+ console.log(dc._name, 'error:', event);
+ };
+ dc.onbufferedamountlow = () => {
+ console.log(dc._name, 'buffered amount low:', dc.bufferedAmount);
+ };
+ dc.onmessage = (event) => {
+ const size = event.data.byteLength || event.data.size;
+ console.log(dc._name, 'incoming message (' + size + ' bytes)');
+ };
+ }
+}
diff --git a/htdocs/webrtc/signaling.js b/htdocs/webrtc/signaling.js
new file mode 100644
index 0000000..ff3e3b0
--- /dev/null
+++ b/htdocs/webrtc/signaling.js
@@ -0,0 +1,211 @@
+'use strict';
+
+/**
+ * A copy & paste signalling implementation.
+ *
+ * Tightly coupled with the WebRTC peer connection class.
+ */
+class CopyPasteSignaling {
+ constructor(pc = null) {
+ this._pc = pc;
+ this.pending_inbound_messages = [];
+ this.localIceCandidatesSent = false;
+ this.remoteIceCandidatesReceived = false;
+ this._onLocalDescriptionUpdate = null;
+ this._onRemoteDescriptionUpdate = null;
+ }
+
+ set pc(pc) {
+ this._pc = pc;
+
+ // Process all pending inbound messages
+ for (const message of this.pending_inbound_messages) {
+ this.receiveMessage(message.type, message.value);
+ }
+ }
+
+ set onLocalDescriptionUpdate(callback) {
+ this._onLocalDescriptionUpdate = callback;
+ }
+
+ set onRemoteDescriptionUpdate(callback) {
+ this._onRemoteDescriptionUpdate = callback;
+ }
+
+ handleLocalDescription(description, complete = false) {
+ console.log('Local description:', description);
+
+ // Send local description
+ this.sendMessage('description', description);
+ if (complete) {
+ this.localIceCandidatesSent = true;
+ this.maybeClose();
+ console.info('Local description complete');
+ }
+
+ // Call 'update'
+ if (this._onLocalDescriptionUpdate !== null) {
+ this._onLocalDescriptionUpdate(this._pc.localDescription);
+ }
+ }
+
+ async handleRemoteDescription(description, complete = false) {
+ // Set remote description
+ console.log('Setting remote description');
+ await this._pc.setRemoteDescription(description);
+ console.log('Remote description:', this._pc.remoteDescription);
+ if (complete) {
+ this.remoteIceCandidatesReceived = true;
+ this.maybeClose();
+ console.info('Remote description complete');
+ }
+
+ // Call 'update' (remote description)
+ if (this._onRemoteDescriptionUpdate !== null) {
+ this._onRemoteDescriptionUpdate(this._pc.remoteDescription);
+ }
+
+ // Create answer (if required)
+ if (!this._pc._offering) {
+ console.log(name, 'Creating answer');
+ description = await this._pc.createAnswer();
+
+ // Apply local description
+ await this._pc.setLocalDescription(description);
+ this.handleLocalDescription(description);
+ }
+ }
+
+ handleLocalCandidate(candidate) {
+ console.log('Local ICE candidate:', candidate);
+
+ // Send local candidate
+ this.sendMessage('candidate', candidate);
+
+ // Special handling for last candidate
+ if (candidate === null) {
+ this.localIceCandidatesSent = true;
+ this.maybeClose();
+ console.info('Local description complete');
+ }
+
+ // Call 'update' (local description)
+ if (this._onLocalDescriptionUpdate !== null) {
+ this._onLocalDescriptionUpdate(this._pc.localDescription);
+ }
+ }
+
+ async handleRemoteCandidate(candidate) {
+ console.log('Remote ICE candidate:', candidate);
+ if (candidate !== null) {
+ // Add remote candidate (if any)
+ await this._pc.addIceCandidate(candidate);
+ } else {
+ // Special handling for last candidate
+ this.remoteIceCandidatesReceived = true;
+ this.maybeClose();
+ console.info('Remote description complete');
+ }
+
+ // Call 'update' (remote description)
+ if (this._onRemoteDescriptionUpdate !== null) {
+ this._onRemoteDescriptionUpdate(this._pc.remoteDescription);
+ }
+ }
+
+ sendMessage(type, value) {
+ // Does nothing by default
+ }
+
+ receiveMessage(type, value) {
+ // Hold back messages until peer connection is set
+ if (this._pc === null) {
+ this.pending_inbound_messages.push({type: type, value: value});
+ }
+
+ // Handle message
+ switch (type) {
+ case 'description':
+ this.handleRemoteDescription(value).catch((error) => console.error(error));
+ break;
+ case 'candidate':
+ this.handleRemoteCandidate(value).catch((error) => console.error(error));
+ break;
+ default:
+ console.warn('Unknown message type:', type);
+ break;
+ }
+ }
+
+ maybeClose() {
+ // Close once all messages have been exchanged
+ if (this.localIceCandidatesSent && this.remoteIceCandidatesReceived) {
+ console.log('Closing signalling channel');
+ this.close();
+ }
+ }
+
+ close() {
+ // Does nothing by default
+ }
+}
+
+/**
+ * A signalling implementation intended for this signalling server:
+ * https://github.com/rawrtc/rawrtc-terminal-demo/tree/master/signaling
+ *
+ * Tightly coupled with the WebRTC peer connection class.
+ *
+ * Example: `ws://localhost/meow/0` when offering, and
+ * `ws://localhost/meow/1` when answering.
+ */
+class WebSocketSignaling extends CopyPasteSignaling {
+ constructor(wsUrl, pc = null) {
+ super(pc);
+ this.pending_outbound_messages = [];
+
+ const ws = new WebSocket(wsUrl);
+ ws.onopen = () => {
+ console.log('WS open');
+ for (const message of this.pending_outbound_messages) {
+ this.sendMessage(message.type, message.value);
+ }
+ };
+ ws.onclose = () => {
+ console.log('WS closed');
+ };
+ ws.onerror = (event) => {
+ console.error('WS error:', event);
+ };
+ ws.onmessage = (event) => {
+ const message = JSON.parse(event.data);
+ if (!('type' in message)) {
+ console.warn("Invalid message, did not contain a 'type' field");
+ return;
+ }
+ this.receiveMessage(message.type, message.value || null);
+ };
+
+ // Store web socket instance
+ this.ws = ws;
+ }
+
+ sendMessage(type, value) {
+ // Cache if not open, yet.
+ if (this.ws.readyState !== 1) {
+ this.pending_outbound_messages.push({type: type, value: value});
+ return;
+ }
+
+ // Send
+ this.ws.send(JSON.stringify({
+ type: type,
+ value: value
+ }));
+ }
+
+ close() {
+ super.close();
+ this.ws.close();
+ }
+}