🎥 Added HLS webcam support.

This commit is contained in:
Timofei Korostelev committed 2020-07-19 16:57:08 -07:00
1 parent fcf60b2a9a
commit 9e36208fed
8 files changed
+21588 -23

No files matched your search

+1
View File
@@ -121,6 +121,7 @@ date of first contribution):
* [Josh Friend](https://github.com/joshfriend)
* [Lachlan Cresswell](https://github.com/lachyc)
* [Khoi Pham](https://github.com/osubuu)
* [Timofei Korostelev](https://github.com/chudsaviet)
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
+1
View File
@@ -12,6 +12,7 @@
* [css-element-queries](https://github.com/marcj/css-element-queries): MIT
* [Detect Mobile Browser](http://detectmobilebrowsers.com/): Public Domain
* [Flot](http://www.flotcharts.org/): MIT
* [HLS.js](https://github.com/video-dev/hls.js): Apache License 2.0
* [JavaScript MD5](https://github.com/blueimp/JavaScript-MD5): MIT
* [JQuery](http://jquery.com/): MIT
* [JQuery Bootstrap Wizard](http://github.com/VinceG/twitter-bootstrap-wizard): MIT, GPL (dual licensed)
+2 -1
View File
@@ -1542,7 +1542,8 @@ class Server(object):
"js/lib/loglevel.min.js",
"js/lib/sockjs.min.js",
"js/lib/ResizeSensor.js",
"js/lib/less.min.js"
"js/lib/less.min.js",
"js/lib/hls.js"
]
js_client = [
"js/app/client/base.js",
+30
View File
@@ -1409,3 +1409,33 @@ var copyToClipboard = function(text) {
document.execCommand("copy");
temp.remove();
};
var determineWebcamStreamType = function(streamUrl) {
if (streamUrl) {
var lastDotPosition = streamUrl.lastIndexOf(".");
var firstQuotationSignPosition = streamUrl.indexOf("?");
if(
lastDotPosition != -1 &&
firstQuotationSignPosition != -1 &&
lastDotPosition >= firstQuotationSignPosition
) {
throw "Malformed URL. Cannot determine stream type."
}
// If we have found a dot, try to extract the extension.
if (lastDotPosition > -1) {
if (firstQuotationSignPosition > -1) {
var extension = streamUrl.slice(lastDotPosition+1, firstQuotationSignPosition-1);
} else {
var extension = streamUrl.slice(lastDotPosition+1);
}
if (extension.toLowerCase() == "m3u8") {
return "hls";
};
};
// By default, 'mjpg' is the stream type.
return "mjpg";
} else {
throw "Empty streamUrl. Cannot determine stream type.";
};
};
@@ -39,6 +39,8 @@ $(function() {
self.webcamDisableTimeout = undefined;
self.webcamLoaded = ko.observable(false);
self.webcamMjpgEnabled = ko.observable(false);
self.webcamHlsEnabled = ko.observable(false);
self.webcamError = ko.observable(false);
self.keycontrolActive = ko.observable(false);
@@ -434,26 +436,15 @@ $(function() {
if (self.webcamDisableTimeout != undefined) {
clearTimeout(self.webcamDisableTimeout);
}
var webcamImage = $("#webcam_image");
var currentSrc = webcamImage.attr("src");
// safari bug doesn't release the mjpeg stream, so we just set it up the once
if (OctoPrint.coreui.browser.safari && currentSrc != undefined) {
return;
}
var newSrc = self.settings.webcam_streamUrl();
if (currentSrc != newSrc) {
if (newSrc.lastIndexOf("?") > -1) {
newSrc += "&";
} else {
newSrc += "?";
}
newSrc += new Date().getTime();
self.webcamLoaded(false);
self.webcamError(false);
webcamImage.attr("src", newSrc);
// Determine stream type and switch to corresponding webcam.
var streamType = determineWebcamStreamType(self.settings.webcam_streamUrl());
if (streamType == "mjpg") {
self._switchToMjpgWebcam();
} else if (streamType == "hls") {
self._switchToHlsWebcam();
} else {
throw "Unknown stream type " + streamType;
}
};
@@ -608,6 +599,48 @@ $(function() {
return distance.toString().replace(".", "");
};
self._switchToMjpgWebcam = function() {
var webcamImage = $("#webcam_image");
var currentSrc = webcamImage.attr("src");
// safari bug doesn't release the mjpeg stream, so we just set it up the once
if (OctoPrint.coreui.browser.safari && currentSrc != undefined) {
return;
}
var newSrc = self.settings.webcam_streamUrl();
if (currentSrc != newSrc) {
if (newSrc.lastIndexOf("?") > -1) {
newSrc += "&";
} else {
newSrc += "?";
}
newSrc += new Date().getTime();
self.webcamLoaded(false);
self.webcamError(false);
webcamImage.attr("src", newSrc);
self.webcamHlsEnabled(false);
self.webcamMjpgEnabled(true);
}
}
self._switchToHlsWebcam = function() {
var video = document.getElementById('webcam_hls');
if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = self.settings.webcam_streamUrl();
}
else if (Hls.isSupported()) {
var hls = new Hls();
hls.loadSource(self.settings.webcam_streamUrl());
hls.attachMedia(video);
}
self.webcamMjpgEnabled(false);
self.webcamHlsEnabled(true);
}
}
OCTOPRINT_VIEWMODELS.push({
@@ -351,10 +351,28 @@ $(function() {
}
var text = gettext("If you see your webcam stream below, the entered stream URL is ok.");
var image = $('<img src="' + self.webcam_streamUrl() + '">');
var streamType = determineWebcamStreamType(self.webcam_streamUrl());
var webcam_element;
if (streamType == "mjpg") {
webcam_element = $('<img src="' + self.webcam_streamUrl() + '">');
} else if (streamType == "hls") {
webcam_element = $('<video id="webcam_hls" muted autoplay style="width: 100%"/>');
video_element = webcam_element[0];
if (video_element.canPlayType('application/vnd.apple.mpegurl')) {
video_element.src = self.webcam_streamUrl();
}
else if (Hls.isSupported()) {
var hls = new Hls();
hls.loadSource(self.webcam_streamUrl());
hls.attachMedia(video_element);
}
} else {
throw "Unknown stream type " + streamType;
}
var message = $("<p></p>")
.append(text)
.append(image);
.append(webcam_element);
self.testWebcamStreamUrlBusy(true);
showMessageDialog({
+21478
View File
File diff suppressed because it is too large. Load diff
+4 -1
View File
@@ -1,5 +1,8 @@
{% if enableWebcam %}
<div id="webcam_container" tabindex="0" data-bind="event: { keydown: onKeyDown, mouseover: onMouseOver, mouseout: onMouseOut, focus: onFocus }">
<div id="webcam_hls_container" tabindex="0" data-bind="visible: webcamHlsEnabled()">
<video id="webcam_hls" muted autoplay style="width: 100%"/>
</div>
<div id="webcam_container" tabindex="0" data-bind="event: { keydown: onKeyDown, mouseover: onMouseOver, mouseout: onMouseOut, focus: onFocus }, visible: webcamMjpgEnabled()">
<div class="nowebcam" data-bind="visible: !webcamLoaded()">
<div class="text webcam_loading" data-bind="visible: !webcamLoaded() && !webcamError()">
<p><strong>{{ _('Webcam stream loading...') }}</strong></p>