mirror of
https://github.com/home-assistant/frontend.git
synced 2026-11-24 13:49:44 +00:00
Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f847eac4e | ||
|
|
30e4fe3838 | ||
|
|
d634893527 | ||
|
|
002ed8030b | ||
|
|
13f891e54a |
No files matched your search
@@ -0,0 +1,8 @@
|
||||
/** Loads an image, resolves with undefined when it can't be loaded. */
|
||||
export const loadImage = (url: string): Promise<HTMLImageElement | undefined> =>
|
||||
new Promise((resolve) => {
|
||||
const image = new Image();
|
||||
image.onload = () => resolve(image);
|
||||
image.onerror = () => resolve(undefined);
|
||||
image.src = url;
|
||||
});
|
||||
@@ -14,6 +14,7 @@ import { customElement, property, query, state } from "lit/decorators";
|
||||
import { classMap } from "lit/directives/class-map";
|
||||
import { styleMap } from "lit/directives/style-map";
|
||||
import { consumeLocalize } from "../../common/decorators/consume-context-entry";
|
||||
import { storage } from "../../common/decorators/storage";
|
||||
import { deepActiveElement } from "../../common/dom/deep-active-element";
|
||||
import type { HASSDomEvent } from "../../common/dom/fire_event";
|
||||
import { ctrlOrCmdLabel } from "../../common/keyboard/ctrl-or-cmd";
|
||||
@@ -29,13 +30,16 @@ import "../ha-spinner";
|
||||
import "../ha-svg-icon";
|
||||
import "./ha-camera-stream-badge";
|
||||
import {
|
||||
clampZoomPan,
|
||||
fitPicture,
|
||||
isZoomed,
|
||||
panBy,
|
||||
refitZoomPan,
|
||||
visiblePart,
|
||||
ZOOM_PAN_IDENTITY,
|
||||
zoomAtPoint,
|
||||
type ZoomPan,
|
||||
type ZoomPanSize,
|
||||
type ZoomPanViewport,
|
||||
} from "./zoom-pan";
|
||||
|
||||
const CONTROLS_TIMEOUT = 3000;
|
||||
@@ -53,6 +57,9 @@ const WHEEL_ZOOM_SPEED = 0.0025;
|
||||
// The longer side of the minimap of a zoomed in picture, in pixels
|
||||
const MINIMAP_SIZE = 64;
|
||||
|
||||
// Most cameras, for the room the player keeps until it knows the picture
|
||||
const DEFAULT_ASPECT_RATIO = 16 / 9;
|
||||
|
||||
interface PointerInfo {
|
||||
x: number;
|
||||
y: number;
|
||||
@@ -105,6 +112,15 @@ export class HaCameraPlayer extends LitElement {
|
||||
|
||||
@state() private _zoom: ZoomPan = ZOOM_PAN_IDENTITY;
|
||||
|
||||
@state() private _pictureSize?: ZoomPanSize;
|
||||
|
||||
// Known before the stream's picture, so the player can make room for it
|
||||
@state() private _posterSize?: ZoomPanSize;
|
||||
|
||||
// Of each camera's picture last time, so the player opens at its size
|
||||
@storage({ key: "cameraAspectRatios", state: true, subscribe: false })
|
||||
private _storedAspectRatios?: Record<string, number>;
|
||||
|
||||
@state() private _gesture = false;
|
||||
|
||||
@state() private _fullscreen = false;
|
||||
@@ -130,13 +146,13 @@ export class HaCameraPlayer extends LitElement {
|
||||
// More than one finger touched since all were lifted, so it is no tap
|
||||
private _multiPointer = false;
|
||||
|
||||
// The size of the viewport the zoom was last made for
|
||||
private _zoomSize: ZoomPanSize = { width: 0, height: 0 };
|
||||
// The viewport and picture the zoom was last made for
|
||||
private _zoomViewport: ZoomPanViewport = fitPicture({ width: 0, height: 0 });
|
||||
|
||||
private _viewportResize = new ResizeController(this, {
|
||||
target: null,
|
||||
skipInitial: true,
|
||||
callback: () => this._viewportResized(),
|
||||
callback: () => this._refitZoom(),
|
||||
});
|
||||
|
||||
private _controlsTimeout?: number;
|
||||
@@ -201,6 +217,8 @@ export class HaCameraPlayer extends LitElement {
|
||||
const oldStateObj = changedProps.get("stateObj");
|
||||
if (oldStateObj && oldStateObj.entity_id !== this.stateObj?.entity_id) {
|
||||
this._zoom = ZOOM_PAN_IDENTITY;
|
||||
this._pictureSize = undefined;
|
||||
this._posterSize = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,6 +276,11 @@ export class HaCameraPlayer extends LitElement {
|
||||
"camera-player-zoomed": zoomed,
|
||||
gesture: this._gesture,
|
||||
})}
|
||||
style=${styleMap({
|
||||
aspectRatio: this._fullscreen
|
||||
? undefined
|
||||
: String(this._aspectRatio()),
|
||||
})}
|
||||
@pointerdown=${this._pointerDown}
|
||||
@pointerup=${this._pointerUp}
|
||||
@pointercancel=${this._pointerCancel}
|
||||
@@ -271,6 +294,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
.allowExoPlayer=${this.allowExoPlayer}
|
||||
.muted=${this._muted}
|
||||
mjpeg-video
|
||||
measure-poster
|
||||
style=${styleMap({
|
||||
transform: zoomed
|
||||
? `translate(${this._zoom.x}px, ${this._zoom.y}px) scale(${this._zoom.scale})`
|
||||
@@ -279,6 +303,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
@camera-stream-status-changed=${this._statusChanged}
|
||||
@load=${this._updateVideo}
|
||||
@camera-capabilities-changed=${this._capabilitiesChanged}
|
||||
@camera-poster-size-changed=${this._posterSizeChanged}
|
||||
></ha-camera-stream>
|
||||
</div>
|
||||
${inApp ? nothing : this._renderControls(status, zoomed, badgeVisible)}
|
||||
@@ -400,12 +425,12 @@ export class HaCameraPlayer extends LitElement {
|
||||
* which isn't mirrored in right to left languages.
|
||||
*/
|
||||
private _renderMinimap() {
|
||||
const { scale, x, y } = this._zoom;
|
||||
const { width, height } = this._zoomSize;
|
||||
const { width, height } = this._zoomViewport.picture;
|
||||
if (!width || !height) {
|
||||
return nothing;
|
||||
}
|
||||
const longSide = Math.max(width, height);
|
||||
const area = visiblePart(this._zoom, this._zoomViewport);
|
||||
return html`<div
|
||||
class=${classMap({ minimap: true, gesture: this._gesture })}
|
||||
style=${styleMap({
|
||||
@@ -417,10 +442,10 @@ export class HaCameraPlayer extends LitElement {
|
||||
<div
|
||||
class="area"
|
||||
style=${styleMap({
|
||||
left: `${(-x / (width * scale)) * 100}%`,
|
||||
top: `${(-y / (height * scale)) * 100}%`,
|
||||
width: `${100 / scale}%`,
|
||||
height: `${100 / scale}%`,
|
||||
left: `${area.x * 100}%`,
|
||||
top: `${area.y * 100}%`,
|
||||
width: `${area.width * 100}%`,
|
||||
height: `${area.height * 100}%`,
|
||||
})}
|
||||
></div>
|
||||
</div>`;
|
||||
@@ -523,6 +548,30 @@ export class HaCameraPlayer extends LitElement {
|
||||
this._capabilities = ev.detail.capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Of the stream's picture, until then of the camera's picture last time,
|
||||
* its poster or most cameras'.
|
||||
*/
|
||||
private _aspectRatio(): number {
|
||||
if (this._pictureSize) {
|
||||
return this._pictureSize.width / this._pictureSize.height;
|
||||
}
|
||||
const stored =
|
||||
this.stateObj && this._storedAspectRatios?.[this.stateObj.entity_id];
|
||||
if (stored) {
|
||||
return stored;
|
||||
}
|
||||
return this._posterSize
|
||||
? this._posterSize.width / this._posterSize.height
|
||||
: DEFAULT_ASPECT_RATIO;
|
||||
}
|
||||
|
||||
private _posterSizeChanged(
|
||||
ev: HASSDomEvent<HASSDomEvents["camera-poster-size-changed"]>
|
||||
) {
|
||||
this._posterSize = ev.detail.size;
|
||||
}
|
||||
|
||||
private _showStreamInfo() {
|
||||
showCameraStreamInfoDialog(this, {
|
||||
status: this._status,
|
||||
@@ -538,6 +587,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
return;
|
||||
}
|
||||
this._video = this._stream?.videoElement;
|
||||
this._updatePictureSize();
|
||||
// Binds the video first, which resets its paused state
|
||||
await this.updateComplete;
|
||||
if (!this._status.connecting) {
|
||||
@@ -577,6 +627,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
for (const type of ["play", "pause", "emptied"]) {
|
||||
video.addEventListener(type, this._updatePaused);
|
||||
}
|
||||
video.addEventListener("resize", this._updatePictureSize);
|
||||
// Only a pause from outside shows the play button, not the moment before
|
||||
// a new video starts playing
|
||||
this._paused = false;
|
||||
@@ -595,6 +646,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
for (const type of ["play", "pause", "emptied"]) {
|
||||
video?.removeEventListener(type, this._updatePaused);
|
||||
}
|
||||
video?.removeEventListener("resize", this._updatePictureSize);
|
||||
video?.removeEventListener(
|
||||
"enterpictureinpicture",
|
||||
this._pictureInPictureChanged
|
||||
@@ -703,35 +755,56 @@ export class HaCameraPlayer extends LitElement {
|
||||
}
|
||||
}
|
||||
|
||||
private _viewportSize() {
|
||||
private _updatePictureSize = () => {
|
||||
const size = this._stream?.pictureSize;
|
||||
if (
|
||||
size &&
|
||||
(size.width !== this._pictureSize?.width ||
|
||||
size.height !== this._pictureSize.height)
|
||||
) {
|
||||
this._pictureSize = size;
|
||||
this._storeAspectRatio(size.width / size.height);
|
||||
}
|
||||
// The new picture can have another size
|
||||
this._refitZoom();
|
||||
};
|
||||
|
||||
private _storeAspectRatio(aspectRatio: number) {
|
||||
const entityId = this.stateObj?.entity_id;
|
||||
if (!entityId || this._storedAspectRatios?.[entityId] === aspectRatio) {
|
||||
return;
|
||||
}
|
||||
this._storedAspectRatios = {
|
||||
...this._storedAspectRatios,
|
||||
[entityId]: aspectRatio,
|
||||
};
|
||||
}
|
||||
|
||||
private _measureViewport() {
|
||||
const rect = this._viewport!.getBoundingClientRect();
|
||||
// Kept for the minimap, every zoom and pan measures it first
|
||||
this._zoomSize = { width: rect.width, height: rect.height };
|
||||
return { rect, size: this._zoomSize };
|
||||
this._zoomViewport = fitPicture(
|
||||
{ width: rect.width, height: rect.height },
|
||||
this._stream?.pictureSize ?? this._pictureSize ?? this._posterSize
|
||||
);
|
||||
return { rect, viewport: this._zoomViewport };
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the same part of the picture in view when the size changes, like
|
||||
* when turning a phone.
|
||||
* Keeps the same part of the picture in view when the viewport or the
|
||||
* picture changes size.
|
||||
*/
|
||||
private _viewportResized() {
|
||||
const oldSize = this._zoomSize;
|
||||
if (!isZoomed(this._zoom) || !this._viewport || !oldSize.width) {
|
||||
private _refitZoom = () => {
|
||||
const oldViewport = this._zoomViewport;
|
||||
if (!isZoomed(this._zoom) || !this._viewport || !oldViewport.width) {
|
||||
return;
|
||||
}
|
||||
const { size } = this._viewportSize();
|
||||
if (!size.width || !size.height) {
|
||||
const { viewport } = this._measureViewport();
|
||||
if (!viewport.width || !viewport.height) {
|
||||
return;
|
||||
}
|
||||
this._zoom = clampZoomPan(
|
||||
{
|
||||
scale: this._zoom.scale,
|
||||
x: (this._zoom.x * size.width) / oldSize.width,
|
||||
y: (this._zoom.y * size.height) / oldSize.height,
|
||||
},
|
||||
size
|
||||
);
|
||||
}
|
||||
this._zoom = refitZoomPan(this._zoom, oldViewport, viewport);
|
||||
};
|
||||
|
||||
private _resetZoom() {
|
||||
this._zoom = ZOOM_PAN_IDENTITY;
|
||||
@@ -749,7 +822,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
// Also keeps the browser from zooming the whole page
|
||||
ev.preventDefault();
|
||||
this._hideWheelHint();
|
||||
const { rect, size } = this._viewportSize();
|
||||
const { rect, viewport } = this._measureViewport();
|
||||
// Lines instead of pixels, like in Firefox with a mouse wheel
|
||||
const delta =
|
||||
ev.deltaMode === WheelEvent.DOM_DELTA_LINE ? ev.deltaY * 33 : ev.deltaY;
|
||||
@@ -758,7 +831,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
this._zoom,
|
||||
this._zoom.scale * Math.exp(-clampedDelta * WHEEL_ZOOM_SPEED),
|
||||
{ x: ev.clientX - rect.left, y: ev.clientY - rect.top },
|
||||
size
|
||||
viewport
|
||||
);
|
||||
}
|
||||
|
||||
@@ -789,12 +862,12 @@ export class HaCameraPlayer extends LitElement {
|
||||
this._resetZoom();
|
||||
return;
|
||||
}
|
||||
const { rect, size } = this._viewportSize();
|
||||
const { rect, viewport } = this._measureViewport();
|
||||
this._zoom = zoomAtPoint(
|
||||
this._zoom,
|
||||
2,
|
||||
{ x: ev.clientX - rect.left, y: ev.clientY - rect.top },
|
||||
size
|
||||
viewport
|
||||
);
|
||||
}
|
||||
|
||||
@@ -828,7 +901,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
if (!pointer || !this._viewport) {
|
||||
return;
|
||||
}
|
||||
const { rect, size } = this._viewportSize();
|
||||
const { rect, viewport } = this._measureViewport();
|
||||
|
||||
if (this._pointers.size === 2) {
|
||||
const [a, b] = [...this._pointers.values()];
|
||||
@@ -846,13 +919,13 @@ export class HaCameraPlayer extends LitElement {
|
||||
this._zoom,
|
||||
(this._zoom.scale * newDistance) / oldDistance,
|
||||
{ x: oldCenter.x - rect.left, y: oldCenter.y - rect.top },
|
||||
size
|
||||
viewport
|
||||
);
|
||||
this._zoom = panBy(
|
||||
zoomed,
|
||||
newCenter.x - oldCenter.x,
|
||||
newCenter.y - oldCenter.y,
|
||||
size
|
||||
viewport
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -862,7 +935,7 @@ export class HaCameraPlayer extends LitElement {
|
||||
pointer.x = ev.clientX;
|
||||
pointer.y = ev.clientY;
|
||||
if (this._pointers.size === 1 && isZoomed(this._zoom)) {
|
||||
this._zoom = panBy(this._zoom, dx, dy, size);
|
||||
this._zoom = panBy(this._zoom, dx, dy, viewport);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -953,12 +1026,18 @@ export class HaCameraPlayer extends LitElement {
|
||||
.viewport {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
max-height: var(--video-max-height, calc(100vh - 97px));
|
||||
/* As wide as the player also when its height is limited, which the
|
||||
aspect ratio would otherwise limit the width with, so the zoomed in
|
||||
picture and gestures reach into the bars next to it */
|
||||
width: 100%;
|
||||
/* Scrolls the page until zoomed in, pinches are handled here */
|
||||
touch-action: pan-x pan-y;
|
||||
/* From the poster's or a default size to the stream's */
|
||||
transition: aspect-ratio var(--ha-animation-duration-normal) ease-out;
|
||||
}
|
||||
|
||||
.player.fullscreen .viewport,
|
||||
.player.fullscreen ha-camera-stream {
|
||||
.player.fullscreen .viewport {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
@@ -972,7 +1051,11 @@ export class HaCameraPlayer extends LitElement {
|
||||
cursor: grabbing;
|
||||
}
|
||||
|
||||
/* Sized by the viewport, a video without a picture yet would be only
|
||||
as high as an empty video */
|
||||
ha-camera-stream {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
transform-origin: 0 0;
|
||||
transition: transform var(--ha-animation-duration-fast) ease-out;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* Zoom and pan of content that fills a viewport, applied as
|
||||
* `translate(x, y) scale(scale)` with the transform origin at the top left.
|
||||
* Zoom and pan of a picture in a viewport, applied as
|
||||
* `translate(x, y) scale(scale)` with the transform origin at the top left
|
||||
* of the viewport.
|
||||
*/
|
||||
export interface ZoomPan {
|
||||
scale: number;
|
||||
@@ -13,28 +14,87 @@ export interface ZoomPanSize {
|
||||
height: number;
|
||||
}
|
||||
|
||||
export interface ZoomPanRect extends ZoomPanSize {
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The viewport and where the picture shows in it before zooming, with bars
|
||||
* next to it when their aspect ratios differ.
|
||||
*/
|
||||
export interface ZoomPanViewport extends ZoomPanSize {
|
||||
picture: ZoomPanRect;
|
||||
}
|
||||
|
||||
export const ZOOM_PAN_IDENTITY: ZoomPan = { scale: 1, x: 0, y: 0 };
|
||||
|
||||
export const MAX_ZOOM = 5;
|
||||
|
||||
export const isZoomed = (zoomPan: ZoomPan): boolean => zoomPan.scale > 1;
|
||||
|
||||
/** Keeps the scale in range and the content covering the whole viewport. */
|
||||
/**
|
||||
* The viewport with the picture fit in its middle, like `object-fit:
|
||||
* contain`. Without the size of the picture, it fills the viewport.
|
||||
*/
|
||||
export const fitPicture = (
|
||||
{ width, height }: ZoomPanSize,
|
||||
pictureSize?: ZoomPanSize
|
||||
): ZoomPanViewport => {
|
||||
if (!pictureSize?.width || !pictureSize.height || !width || !height) {
|
||||
return { width, height, picture: { x: 0, y: 0, width, height } };
|
||||
}
|
||||
const fit = Math.min(width / pictureSize.width, height / pictureSize.height);
|
||||
const pictureWidth = pictureSize.width * fit;
|
||||
const pictureHeight = pictureSize.height * fit;
|
||||
return {
|
||||
width,
|
||||
height,
|
||||
picture: {
|
||||
x: (width - pictureWidth) / 2,
|
||||
y: (height - pictureHeight) / 2,
|
||||
width: pictureWidth,
|
||||
height: pictureHeight,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* The offset on one axis that keeps the zoomed picture covering the viewport,
|
||||
* or in its middle while it is still smaller than the viewport.
|
||||
*/
|
||||
const clampOffset = (
|
||||
offset: number,
|
||||
scale: number,
|
||||
viewportLength: number,
|
||||
pictureStart: number,
|
||||
pictureLength: number
|
||||
): number => {
|
||||
const scaledLength = pictureLength * scale;
|
||||
if (scaledLength <= viewportLength) {
|
||||
return (viewportLength - scaledLength) / 2 - pictureStart * scale;
|
||||
}
|
||||
const min = viewportLength - (pictureStart + pictureLength) * scale;
|
||||
// Not -0 when the picture starts at the edge of the viewport
|
||||
const max = 0 - pictureStart * scale;
|
||||
return Math.min(max, Math.max(min, offset));
|
||||
};
|
||||
|
||||
/** Keeps the scale in range and the bars next to the picture out of view. */
|
||||
export const clampZoomPan = (
|
||||
{ scale, x, y }: ZoomPan,
|
||||
{ width, height }: ZoomPanSize,
|
||||
viewport: ZoomPanViewport,
|
||||
maxScale = MAX_ZOOM
|
||||
): ZoomPan => {
|
||||
const clampedScale = Math.min(Math.max(scale, 1), maxScale);
|
||||
if (clampedScale === 1) {
|
||||
return ZOOM_PAN_IDENTITY;
|
||||
}
|
||||
const minX = width - width * clampedScale;
|
||||
const minY = height - height * clampedScale;
|
||||
const { picture } = viewport;
|
||||
return {
|
||||
scale: clampedScale,
|
||||
x: Math.min(0, Math.max(minX, x)),
|
||||
y: Math.min(0, Math.max(minY, y)),
|
||||
x: clampOffset(x, clampedScale, viewport.width, picture.x, picture.width),
|
||||
y: clampOffset(y, clampedScale, viewport.height, picture.y, picture.height),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -46,7 +106,7 @@ export const zoomAtPoint = (
|
||||
zoomPan: ZoomPan,
|
||||
scale: number,
|
||||
point: { x: number; y: number },
|
||||
size: ZoomPanSize,
|
||||
viewport: ZoomPanViewport,
|
||||
maxScale = MAX_ZOOM
|
||||
): ZoomPan => {
|
||||
const clampedScale = Math.min(Math.max(scale, 1), maxScale);
|
||||
@@ -57,7 +117,7 @@ export const zoomAtPoint = (
|
||||
x: point.x - (point.x - zoomPan.x) * ratio,
|
||||
y: point.y - (point.y - zoomPan.y) * ratio,
|
||||
},
|
||||
size,
|
||||
viewport,
|
||||
maxScale
|
||||
);
|
||||
};
|
||||
@@ -66,11 +126,69 @@ export const panBy = (
|
||||
zoomPan: ZoomPan,
|
||||
dx: number,
|
||||
dy: number,
|
||||
size: ZoomPanSize,
|
||||
viewport: ZoomPanViewport,
|
||||
maxScale = MAX_ZOOM
|
||||
): ZoomPan =>
|
||||
clampZoomPan(
|
||||
{ ...zoomPan, x: zoomPan.x + dx, y: zoomPan.y + dy },
|
||||
size,
|
||||
viewport,
|
||||
maxScale
|
||||
);
|
||||
|
||||
/**
|
||||
* Keeps the part of the picture in the middle of the viewport there when the
|
||||
* viewport or the picture changes size, like when turning a phone.
|
||||
*/
|
||||
export const refitZoomPan = (
|
||||
zoomPan: ZoomPan,
|
||||
oldViewport: ZoomPanViewport,
|
||||
newViewport: ZoomPanViewport,
|
||||
maxScale = MAX_ZOOM
|
||||
): ZoomPan => {
|
||||
const { scale } = zoomPan;
|
||||
const oldPicture = oldViewport.picture;
|
||||
const newPicture = newViewport.picture;
|
||||
if (!isZoomed(zoomPan) || !oldPicture.width || !oldPicture.height) {
|
||||
return clampZoomPan(zoomPan, newViewport, maxScale);
|
||||
}
|
||||
const centerX =
|
||||
((oldViewport.width / 2 - zoomPan.x) / scale - oldPicture.x) /
|
||||
oldPicture.width;
|
||||
const centerY =
|
||||
((oldViewport.height / 2 - zoomPan.y) / scale - oldPicture.y) /
|
||||
oldPicture.height;
|
||||
return clampZoomPan(
|
||||
{
|
||||
scale,
|
||||
x:
|
||||
newViewport.width / 2 -
|
||||
(newPicture.x + centerX * newPicture.width) * scale,
|
||||
y:
|
||||
newViewport.height / 2 -
|
||||
(newPicture.y + centerY * newPicture.height) * scale,
|
||||
},
|
||||
newViewport,
|
||||
maxScale
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* The part of the picture that shows, as fractions of the picture between 0
|
||||
* and 1.
|
||||
*/
|
||||
export const visiblePart = (
|
||||
{ scale, x, y }: ZoomPan,
|
||||
{ width, height, picture }: ZoomPanViewport
|
||||
): ZoomPanRect => {
|
||||
if (!picture.width || !picture.height) {
|
||||
return { x: 0, y: 0, width: 1, height: 1 };
|
||||
}
|
||||
const left = Math.max(0, (-x / scale - picture.x) / picture.width);
|
||||
const right = Math.min(1, ((width - x) / scale - picture.x) / picture.width);
|
||||
const top = Math.max(0, (-y / scale - picture.y) / picture.height);
|
||||
const bottom = Math.min(
|
||||
1,
|
||||
((height - y) / scale - picture.y) / picture.height
|
||||
);
|
||||
return { x: left, y: top, width: right - left, height: bottom - top };
|
||||
};
|
||||
@@ -7,6 +7,7 @@ import { styleMap } from "lit/directives/style-map";
|
||||
import memoizeOne from "memoize-one";
|
||||
import { consume } from "../common/decorators/consume";
|
||||
import { fireEvent, type HASSDomEvent } from "../common/dom/fire_event";
|
||||
import { loadImage } from "../common/dom/load-image";
|
||||
import { supportsFeature } from "../common/entity/supports-feature";
|
||||
import {
|
||||
CameraEntityFeature,
|
||||
@@ -85,6 +86,14 @@ export class HaCameraStream extends LitElement {
|
||||
@property({ type: Boolean, attribute: "mjpeg-video" })
|
||||
public mjpegVideo = false;
|
||||
|
||||
/**
|
||||
* Loads the poster first to tell its size with
|
||||
* `camera-poster-size-changed`, so a player can make room for it before
|
||||
* the stream loads.
|
||||
*/
|
||||
@property({ type: Boolean, attribute: "measure-poster" })
|
||||
public measurePoster = false;
|
||||
|
||||
// Video background image before its loaded
|
||||
@state() private _posterUrl?: string;
|
||||
|
||||
@@ -171,6 +180,21 @@ export class HaCameraStream extends LitElement {
|
||||
return this._visiblePlayer?.videoElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* The size of the shown stream's picture, before it is fit into the
|
||||
* player. Undefined until it is known.
|
||||
*/
|
||||
public get pictureSize(): { width: number; height: number } | undefined {
|
||||
const player = this._visiblePlayer;
|
||||
if (player && player === this._mjpegPlayer) {
|
||||
return this._mjpegPlayer.pictureSize;
|
||||
}
|
||||
const video = player?.videoElement;
|
||||
return video?.videoWidth
|
||||
? { width: video.videoWidth, height: video.videoHeight }
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/** Opens the video of the shown stream picture in picture. */
|
||||
public async requestPictureInPicture() {
|
||||
const player = this._visiblePlayer;
|
||||
@@ -377,17 +401,41 @@ export class HaCameraStream extends LitElement {
|
||||
}
|
||||
|
||||
private async _getPosterUrl(): Promise<void> {
|
||||
const entityId = this.stateObj!.entity_id;
|
||||
let posterUrl: string | undefined;
|
||||
try {
|
||||
this._posterUrl = await fetchThumbnailUrlWithCache(
|
||||
posterUrl = await fetchThumbnailUrlWithCache(
|
||||
this._thumbnailApi(this._api, this._connection),
|
||||
this.stateObj!.entity_id,
|
||||
entityId,
|
||||
this.clientWidth,
|
||||
this.clientHeight
|
||||
);
|
||||
} catch (_err: any) {
|
||||
// poster url is optional
|
||||
this._posterUrl = undefined;
|
||||
}
|
||||
if (!this.measurePoster) {
|
||||
if (this.stateObj?.entity_id === entityId) {
|
||||
this._posterUrl = posterUrl;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Loaded here first for its size, the poster then uses the same image
|
||||
// instead of asking the camera again
|
||||
const poster = posterUrl ? await loadImage(posterUrl) : undefined;
|
||||
if (this.stateObj?.entity_id !== entityId) {
|
||||
return;
|
||||
}
|
||||
this._posterUrl = posterUrl;
|
||||
fireEvent(
|
||||
this,
|
||||
"camera-poster-size-changed",
|
||||
{
|
||||
size: poster?.naturalWidth
|
||||
? { width: poster.naturalWidth, height: poster.naturalHeight }
|
||||
: undefined,
|
||||
},
|
||||
{ bubbles: false, composed: false }
|
||||
);
|
||||
}
|
||||
|
||||
private _handleImageLoad() {
|
||||
@@ -491,6 +539,7 @@ declare global {
|
||||
"stream-type-changed": { type?: CameraStreamType };
|
||||
"camera-capabilities-changed": { capabilities?: CameraCapabilities };
|
||||
"camera-stream-status-changed": CameraStreamStatus;
|
||||
"camera-poster-size-changed": { size?: { width: number; height: number } };
|
||||
streams: {
|
||||
hasAudio: boolean;
|
||||
hasVideo: boolean;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { ContextType } from "@lit/context";
|
||||
import { ResizeController } from "@lit-labs/observers/resize-controller";
|
||||
import type HlsType from "hls.js";
|
||||
import type { PropertyValues, TemplateResult } from "lit";
|
||||
import { css, html, LitElement } from "lit";
|
||||
@@ -78,6 +79,14 @@ export class HaHLSPlayer extends LitElement {
|
||||
|
||||
private _exoPlayer = false;
|
||||
|
||||
// The app's player only follows the video when told, also when the video
|
||||
// changes size without the window, like in a dialog that grows
|
||||
private _exoPlayerResize = new ResizeController(this, {
|
||||
target: null,
|
||||
skipInitial: true,
|
||||
callback: () => this._resizeExoPlayer(),
|
||||
});
|
||||
|
||||
private static streamCount = 0;
|
||||
|
||||
private _hiddenCleanupTimeout?: number;
|
||||
@@ -316,6 +325,7 @@ export class HaHLSPlayer extends LitElement {
|
||||
this._exoPlayer = true;
|
||||
fireEvent(this, "exoplayer-changed", { active: true });
|
||||
window.addEventListener("resize", this._resizeExoPlayer);
|
||||
this._exoPlayerResize.observe(this._videoEl);
|
||||
this.updateComplete.then(() => nextRender()).then(this._resizeExoPlayer);
|
||||
this._videoEl.style.visibility = "hidden";
|
||||
await this._config.auth.external!.fireMessage({
|
||||
@@ -448,6 +458,7 @@ export class HaHLSPlayer extends LitElement {
|
||||
}
|
||||
if (this._exoPlayer) {
|
||||
window.removeEventListener("resize", this._resizeExoPlayer);
|
||||
this._exoPlayerResize.unobserve(this._videoEl);
|
||||
this._config.auth.external!.fireMessage({ type: "exoplayer/stop" });
|
||||
this._exoPlayer = false;
|
||||
fireEvent(this, "exoplayer-changed", { active: false });
|
||||
@@ -500,8 +511,12 @@ export class HaHLSPlayer extends LitElement {
|
||||
max-height: var(--video-max-height, calc(100vh - 97px));
|
||||
}
|
||||
|
||||
/* Scrolls in a player that is only as high as its picture */
|
||||
.fatal {
|
||||
display: block;
|
||||
box-sizing: border-box;
|
||||
max-height: 100%;
|
||||
overflow: auto;
|
||||
padding: 100px 16px;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,14 @@ export class HaMjpegPlayer extends LitElement {
|
||||
return this._canCopy ? this._videoEl : undefined;
|
||||
}
|
||||
|
||||
/** The size of the stream's frames, undefined until the first one. */
|
||||
public get pictureSize(): { width: number; height: number } | undefined {
|
||||
const image = this._imageEl;
|
||||
return image?.naturalWidth
|
||||
? { width: image.naturalWidth, height: image.naturalHeight }
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts copying the image into the video and opens it picture in
|
||||
* picture. Stops copying again when it can't be opened.
|
||||
|
||||
@@ -852,6 +852,13 @@ export class HaWebRtcPlayer extends LitElement {
|
||||
max-height: var(--video-max-height, calc(100vh - 97px));
|
||||
}
|
||||
|
||||
/* Scrolls in a player that is only as high as its picture */
|
||||
ha-alert {
|
||||
display: block;
|
||||
max-height: 100%;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
.reconnecting {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
|
||||
@@ -1,12 +1,21 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
clampZoomPan,
|
||||
fitPicture,
|
||||
panBy,
|
||||
refitZoomPan,
|
||||
visiblePart,
|
||||
ZOOM_PAN_IDENTITY,
|
||||
zoomAtPoint,
|
||||
} from "../../../src/components/camera/zoom-pan";
|
||||
|
||||
const size = { width: 400, height: 300 };
|
||||
const size = fitPicture({ width: 400, height: 300 });
|
||||
|
||||
// A 4:3 picture in a 16:9 viewport, with 120px bars left and right
|
||||
const pillarboxed = fitPicture(
|
||||
{ width: 640, height: 360 },
|
||||
{ width: 1600, height: 1200 }
|
||||
);
|
||||
|
||||
// A wrong transform shows black bars next to the picture, or zooms away from
|
||||
// what the user pointed at
|
||||
@@ -71,3 +80,64 @@ describe("clampZoomPan", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("fitPicture", () => {
|
||||
it("fits the picture in the middle of the viewport", () => {
|
||||
expect(pillarboxed.picture).toEqual({
|
||||
x: 80,
|
||||
y: 0,
|
||||
width: 480,
|
||||
height: 360,
|
||||
});
|
||||
expect(fitPicture({ width: 400, height: 300 }).picture).toEqual({
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 400,
|
||||
height: 300,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Panning must not show the bars next to a picture of another aspect ratio
|
||||
describe("bars next to the picture", () => {
|
||||
it("keeps the picture in the middle while it is narrower than the viewport", () => {
|
||||
// 480 * 1.2 = 576 is still narrower than 640
|
||||
expect(
|
||||
zoomAtPoint(ZOOM_PAN_IDENTITY, 1.2, { x: 0, y: 0 }, pillarboxed)
|
||||
).toEqual({ scale: 1.2, x: (640 - 576) / 2 - 80 * 1.2, y: 0 });
|
||||
});
|
||||
|
||||
it("stops at the edges of the picture, not of the viewport", () => {
|
||||
// The picture spans 160 to 1120 at 2x, so it can move 320px each way
|
||||
expect(panBy({ scale: 2, x: -480, y: 0 }, 1000, 0, pillarboxed).x).toBe(
|
||||
-160
|
||||
);
|
||||
expect(panBy({ scale: 2, x: -480, y: 0 }, -1000, 0, pillarboxed).x).toBe(
|
||||
640 - 1120
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("visiblePart", () => {
|
||||
it("is the part of the picture in view", () => {
|
||||
// At 2x, the left half of the visible area is the picture's left edge
|
||||
expect(visiblePart({ scale: 2, x: -160, y: 0 }, pillarboxed)).toEqual({
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 640 / 2 / 480,
|
||||
height: 0.5,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("refitZoomPan", () => {
|
||||
it("keeps the middle of the view when the viewport changes", () => {
|
||||
const zoomed = { scale: 2, x: -400, y: -300 };
|
||||
const larger = fitPicture({ width: 800, height: 600 });
|
||||
expect(refitZoomPan(zoomed, size, larger)).toEqual({
|
||||
scale: 2,
|
||||
x: -800,
|
||||
y: -600,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user