✨ HLS & WebRTC webcam rotation and mirroring (#4378)
* Make player fit source aspect ratio when rotated * Fix rotated landscape video in wrong position * Ensure webcam layout updated when rotation changed * Add PiP and sound controls * Add full screen control * Make controls transparent unless hovering video * Fix WebRTC mute/unmute not working * Fix typo Co-authored-by: Gina Häußge <gina@octoprint.org> * Reduce code duplication for HLS and webRTC * Ensure webRTC and HLS are unloaded when switching * Add comments to rotation code * Ensure PiP and fullscreen use correct video * Extract click handler for fullscreen and PiP Co-authored-by: Gina Häußge <gina@octoprint.org>
This commit is contained in:
1 parent
884f2008f4
commit
8d0d3187de
4 files changed
+3129
-9
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -43,6 +43,7 @@ $(function () {
|
||||
self.webcamHlsEnabled = ko.observable(false);
|
||||
self.webcamWebRTCEnabled = ko.observable(false);
|
||||
self.webcamError = ko.observable(false);
|
||||
self.webcamMuted = ko.observable(true);
|
||||
self.webRTCPeerConnection = null;
|
||||
|
||||
self.keycontrolActive = ko.observable(false);
|
||||
@@ -68,6 +69,15 @@ $(function () {
|
||||
}
|
||||
});
|
||||
|
||||
// Subscribe to rotation event to ensure we update calculations.
|
||||
// We need to wait for the CSS to be updated by KO, thus we use a timeout to ensure our
|
||||
// calculations run after the CSS was updated
|
||||
self.settings.webcam_rotate90.subscribe(function () {
|
||||
window.setTimeout(function () {
|
||||
self._updateVideoTagWebcamLayout();
|
||||
}, 1);
|
||||
});
|
||||
|
||||
self.settings.printerProfiles.currentProfileData.subscribe(function () {
|
||||
self._updateExtruderCount();
|
||||
self.settings.printerProfiles
|
||||
@@ -318,6 +328,22 @@ $(function () {
|
||||
}
|
||||
};
|
||||
|
||||
self._getActiveWebcamVideoElement = function () {
|
||||
if (self.webcamWebRTCEnabled()) {
|
||||
return document.getElementById("webcam_webrtc");
|
||||
} else {
|
||||
return document.getElementById("webcam_hls");
|
||||
}
|
||||
};
|
||||
|
||||
self.launchWebcamPictureInPicture = function () {
|
||||
self._getActiveWebcamVideoElement().requestPictureInPicture();
|
||||
};
|
||||
|
||||
self.launchWebcamFullscreen = function () {
|
||||
self._getActiveWebcamVideoElement().requestFullscreen();
|
||||
};
|
||||
|
||||
self.sendJogCommand = function (axis, multiplier, distance) {
|
||||
if (typeof distance === "undefined") distance = self.distance();
|
||||
if (
|
||||
@@ -718,6 +744,13 @@ $(function () {
|
||||
|
||||
self._switchToHlsWebcam = function () {
|
||||
var video = document.getElementById("webcam_hls");
|
||||
video.onresize = self._updateVideoTagWebcamLayout;
|
||||
|
||||
// Ensure WebRTC is unloaded
|
||||
if (self.webRTCPeerConnection != null) {
|
||||
self.webRTCPeerConnection.close();
|
||||
self.webRTCPeerConnection = null;
|
||||
}
|
||||
|
||||
// Check for native playback options: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/canPlayType
|
||||
if (
|
||||
@@ -727,9 +760,9 @@ $(function () {
|
||||
) {
|
||||
video.src = self.settings.webcam_streamUrl();
|
||||
} else if (Hls.isSupported()) {
|
||||
var hls = new Hls();
|
||||
hls.loadSource(self.settings.webcam_streamUrl());
|
||||
hls.attachMedia(video);
|
||||
self.hls = new Hls();
|
||||
self.hls.loadSource(self.settings.webcam_streamUrl());
|
||||
self.hls.attachMedia(video);
|
||||
}
|
||||
|
||||
self.webcamMjpgEnabled(false);
|
||||
@@ -742,6 +775,14 @@ $(function () {
|
||||
return;
|
||||
}
|
||||
var video = document.getElementById("webcam_webrtc");
|
||||
video.onresize = self._updateVideoTagWebcamLayout;
|
||||
|
||||
// Ensure HLS is unloaded
|
||||
if (self.hls != null) {
|
||||
document.getElementById("webcam_hls").src = null;
|
||||
self.hls.destroy();
|
||||
self.hls = null;
|
||||
}
|
||||
|
||||
// Close any existing, disconnected connection
|
||||
if (
|
||||
@@ -765,6 +806,50 @@ $(function () {
|
||||
self.webcamHlsEnabled(false);
|
||||
self.webcamWebRTCEnabled(true);
|
||||
};
|
||||
|
||||
self._updateVideoTagWebcamLayout = function () {
|
||||
// Get all elements we need
|
||||
var player = self._getActiveWebcamVideoElement();
|
||||
var rotationContainer = document.querySelector(
|
||||
"#webcam_video_container .webcam_rotated"
|
||||
);
|
||||
var rotationTarget = document.querySelector(
|
||||
"#webcam_video_container .webcam_rotated .rotation_target"
|
||||
);
|
||||
var unrotationContainer = document.querySelector(
|
||||
"#webcam_video_container .webcam_unrotated"
|
||||
);
|
||||
var unrotationTarget = document.querySelector(
|
||||
"#webcam_video_container .webcam_unrotated .rotation_target"
|
||||
);
|
||||
|
||||
// If we found the rotation container, the view is rotated 90 degrees. This means we
|
||||
// need to manually calculate the player dimensions and apply them to the rotation target
|
||||
// where height = width and width = height (to accomodate the rotation). The target is centered
|
||||
// in the container and rotated around it's center so after we manually resized the container everything will layout nicely.
|
||||
if (rotationContainer && player.videoWidth && player.videoHeight) {
|
||||
// Calcualte the height the video will have in the UI. Based on the video width and the aspect ratio.
|
||||
var aspectRatio = player.videoWidth / player.videoHeight;
|
||||
var height = aspectRatio * rotationContainer.offsetWidth;
|
||||
|
||||
// Enforce the height on the rotation container and the rotation target. Width of the container will be 100%, height a calculated
|
||||
// The size of the rotation target (the element that has the 90 deg transform) is the inverse size of the container (so height -> width and width -> height)
|
||||
rotationContainer.style.height = height + "px";
|
||||
rotationTarget.style.height = rotationContainer.offsetWidth + "px";
|
||||
rotationTarget.style.width = rotationContainer.offsetHeight + "px";
|
||||
|
||||
// Remove the padding we used to give the element an initial height.
|
||||
rotationContainer.style.paddingBottom = 0;
|
||||
}
|
||||
|
||||
// We are not rotated, clean up all changes we might have done before
|
||||
if (unrotationContainer) {
|
||||
unrotationContainer.style.height = null;
|
||||
unrotationContainer.style.paddingBottom = 0;
|
||||
unrotationTarget.style.height = null;
|
||||
unrotationTarget.style.width = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
OCTOPRINT_VIEWMODELS.push({
|
||||
|
||||
@@ -645,6 +645,57 @@ ul.dropdown-menu li a {
|
||||
}
|
||||
|
||||
/** Webcam */
|
||||
#webcam_video_container {
|
||||
width: 100%;
|
||||
position: relative;
|
||||
outline: none;
|
||||
|
||||
.video-controls {
|
||||
position: absolute;
|
||||
bottom: 0px;
|
||||
right: 8px;
|
||||
z-index: 10;
|
||||
opacity: 0.4;
|
||||
}
|
||||
|
||||
&:hover {
|
||||
.video-controls {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
#webcam_hls,
|
||||
#webcam_rtsp {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.webcam_rotated {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
padding-bottom: 56.25%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
margin: auto;
|
||||
|
||||
.rotation_target {
|
||||
position: absolute;
|
||||
transform: rotate(-90deg);
|
||||
width: 100%;
|
||||
|
||||
div {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.webcam_unrotated {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
#webcam_container {
|
||||
width: 100%;
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
{% 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 playsinline controls 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 id="webcam_video_container" tabindex="0" data-bind="visible: webcamWebRTCEnabled() || webcamHlsEnabled(), css: { webcam_rotated: settings.webcam_rotate90(), webcam_unrotated: !settings.webcam_rotate90() }">
|
||||
<div class="video-controls btn-group action-buttons">
|
||||
<div class="btn btn-mini" data-bind="click: function(data, event) { $root.launchWebcamFullscreen() }"><i class="fas fa-expand"></i></div>
|
||||
<div class="btn btn-mini" data-bind="visible: document.pictureInPictureEnabled, click: function(data, event) { $root.launchWebcamPictureInPicture() }"><i class="fas fa-images"></i></div>
|
||||
<div class="btn btn-mini" data-bind="click: function(data, event) { webcamMuted(!webcamMuted()); document.getElementById('webcam_webrtc').muted = webcamMuted(); document.getElementById('webcam_hls').muted = webcamMuted() }">
|
||||
<!-- ko if: webcamMuted() -->
|
||||
<i class="fas fa-volume-mute"></i>
|
||||
<!-- /ko -->
|
||||
<!-- ko ifnot: webcamMuted() -->
|
||||
<i class="fas fa-volume-up"></i>
|
||||
<!-- /ko -->
|
||||
</div>
|
||||
</div>
|
||||
<div data-bind="css: { webcam_rotated: settings.webcam_rotate90(), webcam_unrotated: !settings.webcam_rotate90() }">
|
||||
<div class="rotation_target">
|
||||
<div data-bind="css: { flipH: settings.webcam_flipH(), flipV: settings.webcam_flipV() }">
|
||||
<video id="webcam_webrtc" muted autoplay playsinline style="width: 100%" data-bind="visible: webcamWebRTCEnabled()"></video>
|
||||
<video id="webcam_hls" muted autoplay playsinline style="width: 100%" data-bind="visible: webcamHlsEnabled()"></video>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</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()">
|
||||
|
||||
Reference in new issue
Block a user