diff --git a/AUTHORS.md b/AUTHORS.md index 1016145b9..5df4de2c5 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -148,6 +148,7 @@ date of first contribution): * [ldursw](https://github.com/ldursw) * [Ian Wilt](https://github.com/ianwiltdotcom) * [Ben Sycha](https://github.com/Sycha) + * [John Boiles](https://github.com/johnboiles) OctoPrint started off as a fork of [Cura](https://github.com/daid/Cura) by [Daid Braam](https://github.com/daid). Parts of its communication layer and diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index ef6ac98e8..e52a11c3a 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -1543,6 +1543,10 @@ var copyToClipboard = function (text) { var determineWebcamStreamType = function (streamUrl) { if (streamUrl) { + if (streamUrl.startsWith("webrtc://")) { + return "webrtc"; + } + var lastDotPosition = streamUrl.lastIndexOf("."); var firstQuotationSignPosition = streamUrl.indexOf("?"); if ( @@ -1619,3 +1623,67 @@ var deepMerge = function (target, source) { return target; }; + +var negotiateWebRTC = function (streamUrl) { + pc.addTransceiver("video", {direction: "recvonly"}); + pc.addTransceiver("audio", {direction: "recvonly"}); + return pc + .createOffer() + .then(function (offer) { + return pc.setLocalDescription(offer); + }) + .then(function () { + // Wait for ICE gathering to complete + return new Promise(function (resolve) { + if (pc.iceGatheringState === "complete") { + resolve(); + } else { + function checkState() { + if (pc.iceGatheringState === "complete") { + pc.removeEventListener("icegatheringstatechange", checkState); + resolve(); + } + } + pc.addEventListener("icegatheringstatechange", checkState); + } + }); + }) + .then(function () { + var offer = pc.localDescription; + streamUrl = streamUrl.slice("webrtc://".length); + return fetch("http://" + streamUrl, { + body: JSON.stringify({ + sdp: offer.sdp, + type: offer.type + }), + headers: { + "Content-Type": "application/json" + }, + method: "POST" + }); + }) + .then(function (response) { + return response.json(); + }) + .then(function (answer) { + return pc.setRemoteDescription(answer); + }) + .catch(function (e) { + console.error(e); + }); +}; + +var startWebRTC = function (videoElement, streamUrl) { + var config = { + sdpSemantics: "unified-plan", + iceServers: [{urls: ["stun:stun.l.google.com:19302"]}] + }; + pc = new RTCPeerConnection(config); + pc.addEventListener("track", function (evt) { + if (evt.track.kind == "video") { + videoElement.srcObject = evt.streams[0]; + } + }); + + negotiateWebRTC(streamUrl); +}; diff --git a/src/octoprint/static/js/app/viewmodels/control.js b/src/octoprint/static/js/app/viewmodels/control.js index 2fefcef18..a8444ded4 100644 --- a/src/octoprint/static/js/app/viewmodels/control.js +++ b/src/octoprint/static/js/app/viewmodels/control.js @@ -41,6 +41,7 @@ $(function () { self.webcamLoaded = ko.observable(false); self.webcamMjpgEnabled = ko.observable(false); self.webcamHlsEnabled = ko.observable(false); + self.webcamWebRTCEnabled = ko.observable(false); self.webcamError = ko.observable(false); self.keycontrolActive = ko.observable(false); @@ -524,6 +525,8 @@ $(function () { self._switchToMjpgWebcam(); } else if (streamType == "hls") { self._switchToHlsWebcam(); + } else if (streamType == "webrtc") { + self._switchToWebRTCWebcam(); } else { throw "Unknown stream type " + streamType; } @@ -708,6 +711,7 @@ $(function () { self.webcamHlsEnabled(false); self.webcamMjpgEnabled(true); + self.webcamWebRTCEnabled(false); } }; @@ -729,6 +733,17 @@ $(function () { self.webcamMjpgEnabled(false); self.webcamHlsEnabled(true); + self.webcamWebRTCEnabled(false); + }; + + self._switchToWebRTCWebcam = function () { + var video = document.getElementById("webcam_webrtc"); + + startWebRTC(video, self.settings.webcam_streamUrl()); + + self.webcamMjpgEnabled(false); + self.webcamHlsEnabled(false); + self.webcamWebRTCEnabled(true); }; } diff --git a/src/octoprint/static/js/app/viewmodels/settings.js b/src/octoprint/static/js/app/viewmodels/settings.js index ab0dfa877..5c8df6acb 100644 --- a/src/octoprint/static/js/app/viewmodels/settings.js +++ b/src/octoprint/static/js/app/viewmodels/settings.js @@ -453,6 +453,13 @@ $(function () { hls.loadSource(self.webcam_streamUrl()); hls.attachMedia(video_element); } + } else if (streamType == "webrtc") { + webcam_element = $( + '' + ); + video_element = webcam_element[0]; + + startWebRTC(video_element, self.webcam_streamUrl()); } else { throw "Unknown stream type " + streamType; } diff --git a/src/octoprint/templates/tabs/control.jinja2 b/src/octoprint/templates/tabs/control.jinja2 index 7f772187b..445ef6dfa 100644 --- a/src/octoprint/templates/tabs/control.jinja2 +++ b/src/octoprint/templates/tabs/control.jinja2 @@ -1,5 +1,8 @@ {% if enableWebcam %} +