WebRTC support
This commit is contained in:
1 parent
4c1d0338f4
commit
0f398328ad
5 files changed
+94
No files matched your search
@@ -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
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -453,6 +453,13 @@ $(function () {
|
||||
hls.loadSource(self.webcam_streamUrl());
|
||||
hls.attachMedia(video_element);
|
||||
}
|
||||
} else if (streamType == "webrtc") {
|
||||
webcam_element = $(
|
||||
'<video id="webcam_webrtc" muted autoplay style="width: 100%"/>'
|
||||
);
|
||||
video_element = webcam_element[0];
|
||||
|
||||
startWebRTC(video_element, self.webcam_streamUrl());
|
||||
} else {
|
||||
throw "Unknown stream type " + streamType;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
{% if enableWebcam %}
|
||||
<!-- ko if: loginState.hasPermissionKo(access.permissions.WEBCAM) -->
|
||||
<div id="webcam_webrtc_container" tabindex="0" data-bind="visible: webcamWebRTCEnabled()">
|
||||
<video id="webcam_webrtc" muted autoplay style="width: 100%"></video>
|
||||
</div>
|
||||
<div id="webcam_hls_container" tabindex="0" data-bind="visible: webcamHlsEnabled()">
|
||||
<video id="webcam_hls" muted autoplay style="width: 100%"></video>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user