mirror of
https://github.com/home-assistant/frontend.git
synced 2026-11-17 02:12:47 +00:00
Compare commits
38
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e5b019a2f4 | ||
|
|
48444308d8 | ||
|
|
0453233f8b | ||
|
|
e694d61a57 | ||
|
|
f02b21de53 | ||
|
|
26b342bab6 | ||
|
|
c3be1d58ad | ||
|
|
b0e6e401d7 | ||
|
|
d0ee2205df | ||
|
|
ad2a268478 | ||
|
|
5ee9647b22 | ||
|
|
8b55e7b573 | ||
|
|
1a1b97197c | ||
|
|
f048309c73 | ||
|
|
2aa9b43143 | ||
|
|
c65013f95b | ||
|
|
0a6b7f8a3a | ||
|
|
e1da3f61a5 | ||
|
|
97916c62a0 | ||
|
|
816cb162dd | ||
|
|
9a0290b73e | ||
|
|
9a14b096d5 | ||
|
|
70aaf036c3 | ||
|
|
049eb72773 | ||
|
|
359ec4e798 | ||
|
|
128f073eb0 | ||
|
|
fa468ebbea | ||
|
|
6a635de03e | ||
|
|
f7746679a8 | ||
|
|
7938fed22e | ||
|
|
9aae292bbe | ||
|
|
4d2d43ed47 | ||
|
|
bf9b8f771d | ||
|
|
484885ed93 | ||
|
|
fd30120f59 | ||
|
|
f021757910 | ||
|
|
7356a44b47 | ||
|
|
d3468efbb6 |
No files matched your search
@@ -11,7 +11,12 @@ const SWIPE_LOCKED_COMPONENTS = new Set([
|
||||
"ha-filter-chip",
|
||||
]);
|
||||
|
||||
const SWIPE_LOCKED_CLASSES = new Set(["volume-slider-container", "forecast"]);
|
||||
const SWIPE_LOCKED_CLASSES = new Set([
|
||||
"volume-slider-container",
|
||||
"forecast",
|
||||
// A zoomed in camera picture pans on touch
|
||||
"camera-player-zoomed",
|
||||
]);
|
||||
|
||||
/**
|
||||
* Whether a touch inside a bottom sheet belongs to its content rather than to
|
||||
|
||||
@@ -0,0 +1,928 @@
|
||||
import {
|
||||
mdiFullscreen,
|
||||
mdiFullscreenExit,
|
||||
mdiMagnifyMinusOutline,
|
||||
mdiPictureInPictureBottomRight,
|
||||
mdiPictureInPictureBottomRightOutline,
|
||||
mdiVolumeHigh,
|
||||
mdiVolumeOff,
|
||||
} from "@mdi/js";
|
||||
import { css, html, LitElement, nothing, type PropertyValues } from "lit";
|
||||
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 type { HASSDomEvent } from "../../common/dom/fire_event";
|
||||
import { ctrlOrCmdLabel } from "../../common/keyboard/ctrl-or-cmd";
|
||||
import type { LocalizeFunc } from "../../common/translations/localize";
|
||||
import { nextRender } from "../../common/util/render-status";
|
||||
import type { CameraCapabilities, CameraEntity } from "../../data/camera";
|
||||
import type { CameraStreamStatus } from "../../data/camera-streams";
|
||||
import { showCameraStreamInfoDialog } from "../../dialogs/camera/show-dialog-camera-stream-info";
|
||||
import "../ha-camera-stream";
|
||||
import type { HaCameraStream } from "../ha-camera-stream";
|
||||
import "../ha-icon-button";
|
||||
import "../ha-spinner";
|
||||
import "../ha-svg-icon";
|
||||
import "./ha-camera-stream-badge";
|
||||
import {
|
||||
isZoomed,
|
||||
panBy,
|
||||
ZOOM_PAN_IDENTITY,
|
||||
zoomAtPoint,
|
||||
type ZoomPan,
|
||||
} from "./zoom-pan";
|
||||
|
||||
// How long the controls stay after the last interaction
|
||||
const CONTROLS_TIMEOUT = 3000;
|
||||
|
||||
// How long the hint to hold the modifier key to zoom stays
|
||||
const WHEEL_HINT_TIMEOUT = 1500;
|
||||
|
||||
// Movement in pixels up to which a press is a tap rather than a drag
|
||||
const TAP_SLOP = 10;
|
||||
|
||||
const TAP_MAX_DURATION = 300;
|
||||
|
||||
// Zoom per pixel scrolled, a mouse wheel notch scrolls about 100 pixels
|
||||
const WHEEL_ZOOM_SPEED = 0.0025;
|
||||
|
||||
interface PointerInfo {
|
||||
x: number;
|
||||
y: number;
|
||||
startX: number;
|
||||
startY: number;
|
||||
startTime: number;
|
||||
type: string;
|
||||
}
|
||||
|
||||
// Only Safari on iPhone can't show elements full screen, only the video
|
||||
interface WebKitVideoElement extends HTMLVideoElement {
|
||||
webkitEnterFullscreen?: () => void;
|
||||
webkitSupportsFullscreen?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a live camera stream with its own controls, which look the same in
|
||||
* every browser: the connection of the stream, sound, picture in picture and
|
||||
* full screen. It zooms with ctrl or cmd and the scroll wheel, a pinch or a
|
||||
* double click, and pans by dragging while zoomed in. The scroll wheel alone
|
||||
* keeps scrolling the page around it.
|
||||
*/
|
||||
@customElement("ha-camera-player")
|
||||
export class HaCameraPlayer extends LitElement {
|
||||
@state()
|
||||
@consumeLocalize()
|
||||
private _localize!: LocalizeFunc;
|
||||
|
||||
@property({ attribute: false }) public stateObj?: CameraEntity;
|
||||
|
||||
@property({ type: Boolean, attribute: "allow-exoplayer" })
|
||||
public allowExoPlayer = false;
|
||||
|
||||
/** Keeps the screen on while the player shows, like in the more info. */
|
||||
@property({ type: Boolean, attribute: "keep-awake" })
|
||||
public keepAwake = false;
|
||||
|
||||
@state() private _status: CameraStreamStatus = {
|
||||
connecting: true,
|
||||
hasAudio: false,
|
||||
};
|
||||
|
||||
@state() private _capabilities?: CameraCapabilities;
|
||||
|
||||
@state() private _video?: HTMLVideoElement;
|
||||
|
||||
@state() private _muted = false;
|
||||
|
||||
@state() private _controlsVisible = true;
|
||||
|
||||
@state() private _zoom: ZoomPan = ZOOM_PAN_IDENTITY;
|
||||
|
||||
@state() private _gesture = false;
|
||||
|
||||
@state() private _fullscreen = false;
|
||||
|
||||
@state() private _pictureInPicture = false;
|
||||
|
||||
@state() private _wheelHint = false;
|
||||
|
||||
@query("ha-camera-stream") private _stream?: HaCameraStream;
|
||||
|
||||
@query(".player") private _player?: HTMLElement;
|
||||
|
||||
@query(".viewport") private _viewport?: HTMLElement;
|
||||
|
||||
@query(".overlay") private _overlay?: HTMLElement;
|
||||
|
||||
private _pointers = new Map<number, PointerInfo>();
|
||||
|
||||
private _controlsTimeout?: number;
|
||||
|
||||
private _wheelHintTimeout?: number;
|
||||
|
||||
private _videoRequest = 0;
|
||||
|
||||
private _wakeLock?: WakeLockSentinel;
|
||||
|
||||
// Increased on every request and release, so older requests are dropped
|
||||
private _wakeLockRequest = 0;
|
||||
|
||||
// Not passive, so zooming can keep the page from scrolling
|
||||
private _wheelListener = {
|
||||
handleEvent: (ev: WheelEvent) => this._wheel(ev),
|
||||
passive: false,
|
||||
};
|
||||
|
||||
/** The microphone track sent to the camera, only on WebRTC streams. */
|
||||
public get microphoneTrack(): MediaStreamTrack | undefined {
|
||||
return this._stream?.microphoneTrack;
|
||||
}
|
||||
|
||||
/** Toggles sending the microphone to the camera, only on WebRTC streams. */
|
||||
public async toggleMicrophone() {
|
||||
await this._stream?.toggleMicrophone();
|
||||
}
|
||||
|
||||
public connectedCallback() {
|
||||
super.connectedCallback();
|
||||
document.addEventListener("visibilitychange", this._requestWakeLock);
|
||||
// The first update requests it, once keep-awake is set
|
||||
if (this.hasUpdated) {
|
||||
this._requestWakeLock();
|
||||
}
|
||||
document.addEventListener("fullscreenchange", this._fullscreenChanged);
|
||||
document.addEventListener(
|
||||
"webkitfullscreenchange",
|
||||
this._fullscreenChanged
|
||||
);
|
||||
}
|
||||
|
||||
public disconnectedCallback() {
|
||||
super.disconnectedCallback();
|
||||
document.removeEventListener("visibilitychange", this._requestWakeLock);
|
||||
this._releaseWakeLock();
|
||||
document.removeEventListener("fullscreenchange", this._fullscreenChanged);
|
||||
document.removeEventListener(
|
||||
"webkitfullscreenchange",
|
||||
this._fullscreenChanged
|
||||
);
|
||||
clearTimeout(this._controlsTimeout);
|
||||
clearTimeout(this._wheelHintTimeout);
|
||||
this._unbindVideo(this._video);
|
||||
this._video = undefined;
|
||||
this._pointers.clear();
|
||||
}
|
||||
|
||||
protected willUpdate(changedProps: PropertyValues<this>) {
|
||||
super.willUpdate(changedProps);
|
||||
const oldStateObj = changedProps.get("stateObj");
|
||||
if (oldStateObj && oldStateObj.entity_id !== this.stateObj?.entity_id) {
|
||||
this._zoom = ZOOM_PAN_IDENTITY;
|
||||
}
|
||||
}
|
||||
|
||||
protected updated(changedProps: PropertyValues) {
|
||||
super.updated(changedProps);
|
||||
if (changedProps.has("keepAwake")) {
|
||||
if (this.keepAwake) {
|
||||
this._requestWakeLock();
|
||||
} else {
|
||||
this._releaseWakeLock();
|
||||
}
|
||||
}
|
||||
if (changedProps.has("_video")) {
|
||||
this._unbindVideo(changedProps.get("_video") as HTMLVideoElement);
|
||||
this._bindVideo(this._video);
|
||||
}
|
||||
}
|
||||
|
||||
protected render() {
|
||||
if (!this.stateObj) {
|
||||
return nothing;
|
||||
}
|
||||
const zoomed = isZoomed(this._zoom);
|
||||
const status = this._status;
|
||||
// Stays while the connection changes, so the user sees what happens
|
||||
const badgeVisible =
|
||||
this._controlsVisible ||
|
||||
status.connecting ||
|
||||
status.upgrading !== undefined;
|
||||
|
||||
return html`
|
||||
<div
|
||||
class=${classMap({
|
||||
player: true,
|
||||
fullscreen: this._fullscreen,
|
||||
idle: !this._controlsVisible,
|
||||
})}
|
||||
@pointermove=${this._pointerMove}
|
||||
@pointerleave=${this._pointerLeave}
|
||||
@focusin=${this._showControls}
|
||||
>
|
||||
<div
|
||||
class=${classMap({
|
||||
viewport: true,
|
||||
zoomed,
|
||||
"camera-player-zoomed": zoomed,
|
||||
gesture: this._gesture,
|
||||
})}
|
||||
@pointerdown=${this._pointerDown}
|
||||
@pointerup=${this._pointerUp}
|
||||
@pointercancel=${this._pointerCancel}
|
||||
@dblclick=${this._doubleClick}
|
||||
@dragstart=${this._preventDrag}
|
||||
@wheel=${this._wheelListener}
|
||||
>
|
||||
<ha-camera-stream
|
||||
.stateObj=${this.stateObj}
|
||||
.allowExoPlayer=${this.allowExoPlayer}
|
||||
.muted=${this._muted}
|
||||
mjpeg-video
|
||||
style=${styleMap({
|
||||
transform: zoomed
|
||||
? `translate(${this._zoom.x}px, ${this._zoom.y}px) scale(${this._zoom.scale})`
|
||||
: undefined,
|
||||
})}
|
||||
@camera-stream-status-changed=${this._statusChanged}
|
||||
@load=${this._updateVideo}
|
||||
@camera-capabilities-changed=${this._capabilitiesChanged}
|
||||
></ha-camera-stream>
|
||||
</div>
|
||||
<div
|
||||
class=${classMap({ overlay: true, visible: this._controlsVisible })}
|
||||
>
|
||||
<div class="row">
|
||||
<ha-camera-stream-badge
|
||||
class=${classMap({ visible: badgeVisible })}
|
||||
.status=${status}
|
||||
@click=${this._showStreamInfo}
|
||||
></ha-camera-stream-badge>
|
||||
<div class="group">
|
||||
${
|
||||
this._wheelHint
|
||||
? html`<span class="wheel-hint" aria-hidden="true">
|
||||
${this._localize(
|
||||
"ui.components.camera-player.zoom_hint",
|
||||
{ modifier: ctrlOrCmdLabel(this._localize) }
|
||||
)}
|
||||
</span>`
|
||||
: nothing
|
||||
}
|
||||
${
|
||||
zoomed
|
||||
? html`<ha-icon-button
|
||||
class="control"
|
||||
.label=${this._localize(
|
||||
"ui.components.camera-player.reset_zoom"
|
||||
)}
|
||||
.path=${mdiMagnifyMinusOutline}
|
||||
@click=${this._resetZoom}
|
||||
></ha-icon-button>`
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="group">
|
||||
${
|
||||
status.hasAudio
|
||||
? html`<ha-icon-button
|
||||
class=${classMap({
|
||||
control: true,
|
||||
// Stays while muted, so it is clear there is sound
|
||||
pinned: this._muted,
|
||||
})}
|
||||
.label=${this._localize(
|
||||
this._muted
|
||||
? "ui.components.camera-player.unmute"
|
||||
: "ui.components.camera-player.mute"
|
||||
)}
|
||||
.path=${this._muted ? mdiVolumeOff : mdiVolumeHigh}
|
||||
@click=${this._toggleMute}
|
||||
></ha-icon-button>`
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
<div class="group">
|
||||
${
|
||||
this._supportsPictureInPicture()
|
||||
? html`<ha-icon-button
|
||||
class="control"
|
||||
.label=${this._localize(
|
||||
this._pictureInPicture
|
||||
? "ui.components.camera-player.exit_picture_in_picture"
|
||||
: "ui.components.camera-player.picture_in_picture"
|
||||
)}
|
||||
.path=${
|
||||
this._pictureInPicture
|
||||
? mdiPictureInPictureBottomRightOutline
|
||||
: mdiPictureInPictureBottomRight
|
||||
}
|
||||
@click=${this._togglePictureInPicture}
|
||||
></ha-icon-button>`
|
||||
: nothing
|
||||
}
|
||||
${
|
||||
this._supportsFullscreen()
|
||||
? html`<ha-icon-button
|
||||
class="control"
|
||||
.label=${this._localize(
|
||||
this._fullscreen
|
||||
? "ui.components.camera-player.exit_fullscreen"
|
||||
: "ui.components.camera-player.fullscreen"
|
||||
)}
|
||||
.path=${this._fullscreen ? mdiFullscreenExit : mdiFullscreen}
|
||||
@click=${this._toggleFullscreen}
|
||||
></ha-icon-button>`
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
// Screen
|
||||
|
||||
/**
|
||||
* Browsers release the lock when the page is hidden, so it is requested
|
||||
* again whenever the page shows.
|
||||
*/
|
||||
private _requestWakeLock = async () => {
|
||||
if (
|
||||
!this.keepAwake ||
|
||||
!this.isConnected ||
|
||||
this._wakeLock ||
|
||||
document.visibilityState !== "visible" ||
|
||||
!("wakeLock" in navigator)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const request = ++this._wakeLockRequest;
|
||||
try {
|
||||
const wakeLock = await navigator.wakeLock.request("screen");
|
||||
if (request !== this._wakeLockRequest) {
|
||||
wakeLock.release();
|
||||
return;
|
||||
}
|
||||
this._wakeLock = wakeLock;
|
||||
wakeLock.addEventListener("release", () => {
|
||||
if (this._wakeLock === wakeLock) {
|
||||
this._wakeLock = undefined;
|
||||
}
|
||||
});
|
||||
} catch (_err: unknown) {
|
||||
// Not allowed, like without HTTPS or with the battery saver on
|
||||
}
|
||||
};
|
||||
|
||||
private _releaseWakeLock() {
|
||||
this._wakeLockRequest++;
|
||||
this._wakeLock?.release();
|
||||
this._wakeLock = undefined;
|
||||
}
|
||||
|
||||
// Controls
|
||||
|
||||
private _showControls = () => {
|
||||
this._controlsVisible = true;
|
||||
this._scheduleHideControls();
|
||||
};
|
||||
|
||||
private _scheduleHideControls() {
|
||||
clearTimeout(this._controlsTimeout);
|
||||
this._controlsTimeout = window.setTimeout(() => {
|
||||
this._controlsTimeout = undefined;
|
||||
// Stay while the mouse is on a control
|
||||
if (
|
||||
this._overlay?.querySelector(
|
||||
".control:hover, ha-camera-stream-badge:hover"
|
||||
)
|
||||
) {
|
||||
this._scheduleHideControls();
|
||||
return;
|
||||
}
|
||||
this._controlsVisible = false;
|
||||
}, CONTROLS_TIMEOUT);
|
||||
}
|
||||
|
||||
private _hideControls() {
|
||||
clearTimeout(this._controlsTimeout);
|
||||
this._controlsTimeout = undefined;
|
||||
this._controlsVisible = false;
|
||||
}
|
||||
|
||||
private _statusChanged(
|
||||
ev: HASSDomEvent<HASSDomEvents["camera-stream-status-changed"]>
|
||||
) {
|
||||
const oldStatus = this._status;
|
||||
this._status = ev.detail;
|
||||
if (
|
||||
oldStatus.connecting !== ev.detail.connecting ||
|
||||
oldStatus.type !== ev.detail.type
|
||||
) {
|
||||
// Shows the new connection for a moment
|
||||
this._showControls();
|
||||
this._updateVideo();
|
||||
}
|
||||
}
|
||||
|
||||
private _capabilitiesChanged(
|
||||
ev: HASSDomEvent<HASSDomEvents["camera-capabilities-changed"]>
|
||||
) {
|
||||
this._capabilities = ev.detail.capabilities;
|
||||
}
|
||||
|
||||
private _showStreamInfo() {
|
||||
showCameraStreamInfoDialog(this, {
|
||||
status: this._status,
|
||||
supportedTypes: this._capabilities?.frontend_stream_types,
|
||||
});
|
||||
}
|
||||
|
||||
// Video
|
||||
|
||||
private async _updateVideo() {
|
||||
const request = ++this._videoRequest;
|
||||
// The players render their video after the stream renders them
|
||||
await nextRender();
|
||||
if (request !== this._videoRequest) {
|
||||
return;
|
||||
}
|
||||
this._video = this._stream?.videoElement;
|
||||
if (!this._status.connecting) {
|
||||
this._ensurePlaying();
|
||||
}
|
||||
}
|
||||
|
||||
/** Browsers can block playing with sound, then it plays muted instead. */
|
||||
private async _ensurePlaying() {
|
||||
const video = this._video;
|
||||
// An MJPEG stream's video only plays for picture in picture
|
||||
if (!video || !video.paused || (!video.srcObject && !video.currentSrc)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await video.play();
|
||||
} catch (err: unknown) {
|
||||
if (err instanceof DOMException && err.name === "NotAllowedError") {
|
||||
this._muted = true;
|
||||
video.muted = true;
|
||||
await video.play().catch(() => undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _bindVideo(video?: HTMLVideoElement) {
|
||||
if (!video) {
|
||||
this._pictureInPicture = false;
|
||||
return;
|
||||
}
|
||||
video.addEventListener(
|
||||
"enterpictureinpicture",
|
||||
this._pictureInPictureChanged
|
||||
);
|
||||
video.addEventListener(
|
||||
"leavepictureinpicture",
|
||||
this._pictureInPictureChanged
|
||||
);
|
||||
this._pictureInPicture = false;
|
||||
}
|
||||
|
||||
private _unbindVideo(video?: HTMLVideoElement) {
|
||||
video?.removeEventListener(
|
||||
"enterpictureinpicture",
|
||||
this._pictureInPictureChanged
|
||||
);
|
||||
video?.removeEventListener(
|
||||
"leavepictureinpicture",
|
||||
this._pictureInPictureChanged
|
||||
);
|
||||
}
|
||||
|
||||
private _toggleMute() {
|
||||
this._muted = !this._muted;
|
||||
if (!this._muted) {
|
||||
this._ensurePlaying();
|
||||
}
|
||||
}
|
||||
|
||||
private _supportsPictureInPicture(): boolean {
|
||||
return Boolean(
|
||||
this._video &&
|
||||
document.pictureInPictureEnabled &&
|
||||
!this._video.disablePictureInPicture
|
||||
);
|
||||
}
|
||||
|
||||
// document.pictureInPictureElement is only the outermost shadow host, so
|
||||
// the events of the video tell which one it is
|
||||
private _pictureInPictureChanged = (ev: Event) => {
|
||||
this._pictureInPicture = ev.type === "enterpictureinpicture";
|
||||
};
|
||||
|
||||
private async _togglePictureInPicture() {
|
||||
try {
|
||||
if (this._pictureInPicture) {
|
||||
await document.exitPictureInPicture();
|
||||
} else {
|
||||
// An MJPEG stream only plays its video for picture in picture
|
||||
await this._stream?.preparePictureInPicture();
|
||||
await this._video?.requestPictureInPicture();
|
||||
}
|
||||
} catch (_err: unknown) {
|
||||
// Not possible right now, like before the video has loaded
|
||||
}
|
||||
}
|
||||
|
||||
private _supportsFullscreen(): boolean {
|
||||
const video = this._video as WebKitVideoElement | undefined;
|
||||
return (
|
||||
document.fullscreenEnabled ||
|
||||
("webkitFullscreenEnabled" in document &&
|
||||
Boolean(document.webkitFullscreenEnabled)) ||
|
||||
Boolean(video?.webkitSupportsFullscreen)
|
||||
);
|
||||
}
|
||||
|
||||
private _fullscreenChanged = () => {
|
||||
const fullscreenElement =
|
||||
this.shadowRoot?.fullscreenElement ??
|
||||
("webkitFullscreenElement" in document
|
||||
? (document.webkitFullscreenElement as Element | null)
|
||||
: null);
|
||||
const fullscreen = Boolean(
|
||||
this._player && fullscreenElement === this._player
|
||||
);
|
||||
if (fullscreen !== this._fullscreen) {
|
||||
this._fullscreen = fullscreen;
|
||||
// The size changes, so the zoomed in area would be off
|
||||
this._zoom = ZOOM_PAN_IDENTITY;
|
||||
}
|
||||
};
|
||||
|
||||
private async _toggleFullscreen() {
|
||||
const player = this._player;
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (this._fullscreen) {
|
||||
if (document.exitFullscreen) {
|
||||
await document.exitFullscreen();
|
||||
} else if ("webkitExitFullscreen" in document) {
|
||||
(document.webkitExitFullscreen as () => void)();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (player.requestFullscreen) {
|
||||
await player.requestFullscreen();
|
||||
} else if ("webkitRequestFullscreen" in player) {
|
||||
(player.webkitRequestFullscreen as () => void)();
|
||||
} else {
|
||||
// Safari on iPhone only shows the video itself full screen, with its
|
||||
// own controls
|
||||
(
|
||||
this._video as WebKitVideoElement | undefined
|
||||
)?.webkitEnterFullscreen?.();
|
||||
}
|
||||
} catch (_err: unknown) {
|
||||
// Denied by the browser
|
||||
}
|
||||
}
|
||||
|
||||
// Zoom and pan
|
||||
|
||||
private _viewportSize() {
|
||||
const rect = this._viewport!.getBoundingClientRect();
|
||||
return { rect, size: { width: rect.width, height: rect.height } };
|
||||
}
|
||||
|
||||
private _resetZoom() {
|
||||
this._zoom = ZOOM_PAN_IDENTITY;
|
||||
}
|
||||
|
||||
private _wheel(ev: WheelEvent) {
|
||||
if (!this._viewport) {
|
||||
return;
|
||||
}
|
||||
// A pinch on a touchpad scrolls with ctrl held
|
||||
if (!ev.ctrlKey && !ev.metaKey) {
|
||||
this._showWheelHint();
|
||||
return;
|
||||
}
|
||||
// Also keeps the browser from zooming the whole page
|
||||
ev.preventDefault();
|
||||
this._hideWheelHint();
|
||||
const { rect, size } = this._viewportSize();
|
||||
// Lines instead of pixels, like in Firefox with a mouse wheel
|
||||
const delta =
|
||||
ev.deltaMode === WheelEvent.DOM_DELTA_LINE ? ev.deltaY * 33 : ev.deltaY;
|
||||
const clampedDelta = Math.max(-100, Math.min(100, delta));
|
||||
this._zoom = zoomAtPoint(
|
||||
this._zoom,
|
||||
this._zoom.scale * Math.exp(-clampedDelta * WHEEL_ZOOM_SPEED),
|
||||
{ x: ev.clientX - rect.left, y: ev.clientY - rect.top },
|
||||
size
|
||||
);
|
||||
}
|
||||
|
||||
private _showWheelHint() {
|
||||
this._wheelHint = true;
|
||||
clearTimeout(this._wheelHintTimeout);
|
||||
this._wheelHintTimeout = window.setTimeout(() => {
|
||||
this._wheelHint = false;
|
||||
}, WHEEL_HINT_TIMEOUT);
|
||||
}
|
||||
|
||||
private _hideWheelHint() {
|
||||
clearTimeout(this._wheelHintTimeout);
|
||||
this._wheelHint = false;
|
||||
}
|
||||
|
||||
private _preventDrag(ev: DragEvent) {
|
||||
// Dragging an image, like an MJPEG stream, would drag it out instead of
|
||||
// panning
|
||||
ev.preventDefault();
|
||||
}
|
||||
|
||||
private _doubleClick(ev: MouseEvent) {
|
||||
if (!this._viewport) {
|
||||
return;
|
||||
}
|
||||
if (isZoomed(this._zoom)) {
|
||||
this._resetZoom();
|
||||
return;
|
||||
}
|
||||
const { rect, size } = this._viewportSize();
|
||||
this._zoom = zoomAtPoint(
|
||||
this._zoom,
|
||||
2,
|
||||
{ x: ev.clientX - rect.left, y: ev.clientY - rect.top },
|
||||
size
|
||||
);
|
||||
}
|
||||
|
||||
private _pointerDown(ev: PointerEvent) {
|
||||
if (ev.pointerType === "mouse" && ev.button !== 0) {
|
||||
return;
|
||||
}
|
||||
this._pointers.set(ev.pointerId, {
|
||||
x: ev.clientX,
|
||||
y: ev.clientY,
|
||||
startX: ev.clientX,
|
||||
startY: ev.clientY,
|
||||
startTime: ev.timeStamp,
|
||||
type: ev.pointerType,
|
||||
});
|
||||
if (this._pointers.size > 1 || isZoomed(this._zoom)) {
|
||||
this._gesture = true;
|
||||
(ev.currentTarget as HTMLElement).setPointerCapture(ev.pointerId);
|
||||
}
|
||||
}
|
||||
|
||||
private _pointerMove(ev: PointerEvent) {
|
||||
if (ev.pointerType === "mouse" && !this._pointers.size) {
|
||||
this._showControls();
|
||||
}
|
||||
const pointer = this._pointers.get(ev.pointerId);
|
||||
if (!pointer || !this._viewport) {
|
||||
return;
|
||||
}
|
||||
const { rect, size } = this._viewportSize();
|
||||
|
||||
if (this._pointers.size === 2) {
|
||||
const [a, b] = [...this._pointers.values()];
|
||||
const oldDistance = Math.hypot(a.x - b.x, a.y - b.y);
|
||||
const oldCenter = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
||||
pointer.x = ev.clientX;
|
||||
pointer.y = ev.clientY;
|
||||
const newDistance = Math.hypot(a.x - b.x, a.y - b.y);
|
||||
const newCenter = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
||||
if (!oldDistance) {
|
||||
return;
|
||||
}
|
||||
const zoomed = zoomAtPoint(
|
||||
this._zoom,
|
||||
(this._zoom.scale * newDistance) / oldDistance,
|
||||
{ x: newCenter.x - rect.left, y: newCenter.y - rect.top },
|
||||
size
|
||||
);
|
||||
this._zoom = panBy(
|
||||
zoomed,
|
||||
newCenter.x - oldCenter.x,
|
||||
newCenter.y - oldCenter.y,
|
||||
size
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const dx = ev.clientX - pointer.x;
|
||||
const dy = ev.clientY - pointer.y;
|
||||
pointer.x = ev.clientX;
|
||||
pointer.y = ev.clientY;
|
||||
if (this._pointers.size === 1 && isZoomed(this._zoom)) {
|
||||
this._zoom = panBy(this._zoom, dx, dy, size);
|
||||
}
|
||||
}
|
||||
|
||||
private _pointerUp(ev: PointerEvent) {
|
||||
const pointer = this._pointers.get(ev.pointerId);
|
||||
const wasSingle = this._pointers.size === 1;
|
||||
this._pointers.delete(ev.pointerId);
|
||||
if (!this._pointers.size) {
|
||||
this._gesture = false;
|
||||
}
|
||||
if (!pointer || !wasSingle) {
|
||||
return;
|
||||
}
|
||||
const isTap =
|
||||
Math.hypot(ev.clientX - pointer.startX, ev.clientY - pointer.startY) <
|
||||
TAP_SLOP && ev.timeStamp - pointer.startTime < TAP_MAX_DURATION;
|
||||
if (!isTap) {
|
||||
return;
|
||||
}
|
||||
// A tap toggles the controls, a mouse already shows them by moving
|
||||
if (pointer.type !== "mouse" && this._controlsVisible) {
|
||||
this._hideControls();
|
||||
} else {
|
||||
this._showControls();
|
||||
}
|
||||
}
|
||||
|
||||
private _pointerCancel(ev: PointerEvent) {
|
||||
this._pointers.delete(ev.pointerId);
|
||||
if (!this._pointers.size) {
|
||||
this._gesture = false;
|
||||
}
|
||||
}
|
||||
|
||||
private _pointerLeave(ev: PointerEvent) {
|
||||
if (ev.pointerType === "mouse" && !this._pointers.size) {
|
||||
this._hideControls();
|
||||
}
|
||||
}
|
||||
|
||||
static styles = css`
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.player {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
/* Clear of notches and rounded corners, the picture in the middle */
|
||||
.player.fullscreen {
|
||||
--safe-top: env(safe-area-inset-top, 0px);
|
||||
--safe-right: env(safe-area-inset-right, 0px);
|
||||
--safe-bottom: env(safe-area-inset-bottom, 0px);
|
||||
--safe-left: env(safe-area-inset-left, 0px);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
padding: var(--safe-top) var(--safe-right) var(--safe-bottom)
|
||||
var(--safe-left);
|
||||
--video-max-height: calc(100dvh - var(--safe-top) - var(--safe-bottom));
|
||||
}
|
||||
|
||||
.player.fullscreen ha-camera-stream {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.player.fullscreen .overlay {
|
||||
padding: calc(var(--ha-space-2) + var(--safe-top))
|
||||
calc(var(--ha-space-2) + var(--safe-right))
|
||||
calc(var(--ha-space-2) + var(--safe-bottom))
|
||||
calc(var(--ha-space-2) + var(--safe-left));
|
||||
}
|
||||
|
||||
.player.fullscreen.idle {
|
||||
cursor: none;
|
||||
}
|
||||
|
||||
.viewport {
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
/* Scrolls the page until zoomed in, pinches are handled here */
|
||||
touch-action: pan-x pan-y;
|
||||
}
|
||||
|
||||
.player.fullscreen .viewport,
|
||||
.player.fullscreen ha-camera-stream {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.viewport.zoomed {
|
||||
touch-action: none;
|
||||
cursor: grab;
|
||||
}
|
||||
|
||||
.viewport.zoomed.gesture {
|
||||
cursor: grabbing;
|
||||
}
|
||||
|
||||
ha-camera-stream {
|
||||
transform-origin: 0 0;
|
||||
transition: transform var(--ha-animation-duration-fast, 150ms) ease-out;
|
||||
}
|
||||
|
||||
.viewport.gesture ha-camera-stream {
|
||||
transition: none;
|
||||
will-change: transform;
|
||||
}
|
||||
|
||||
.overlay {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
padding: var(--ha-space-2);
|
||||
/* Taps between the controls reach the video below */
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.row,
|
||||
.group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: var(--ha-space-2);
|
||||
}
|
||||
|
||||
.control,
|
||||
ha-camera-stream-badge {
|
||||
opacity: 0;
|
||||
transition: opacity var(--ha-animation-duration-normal, 250ms) ease-in-out;
|
||||
}
|
||||
|
||||
.overlay.visible .control,
|
||||
.control.pinned,
|
||||
ha-camera-stream-badge.visible {
|
||||
opacity: 1;
|
||||
pointer-events: auto;
|
||||
}
|
||||
|
||||
/* Readable on a dark and on a bright picture */
|
||||
.control {
|
||||
--ha-icon-button-size: 40px;
|
||||
--mdc-icon-size: 22px;
|
||||
color: white;
|
||||
background-color: rgba(0, 0, 0, 0.45);
|
||||
border: 1px solid rgba(255, 255, 255, 0.15);
|
||||
border-radius: var(--ha-border-radius-circle);
|
||||
backdrop-filter: blur(8px);
|
||||
-webkit-backdrop-filter: blur(8px);
|
||||
}
|
||||
|
||||
/* Where the reset zoom button comes once zoomed in */
|
||||
.wheel-hint {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
height: 28px;
|
||||
padding-inline: var(--ha-space-3);
|
||||
border: 1px solid rgba(255, 255, 255, 0.15);
|
||||
border-radius: var(--ha-border-radius-pill);
|
||||
background-color: rgba(0, 0, 0, 0.45);
|
||||
backdrop-filter: blur(8px);
|
||||
-webkit-backdrop-filter: blur(8px);
|
||||
color: white;
|
||||
font-size: var(--ha-font-size-s);
|
||||
font-weight: var(--ha-font-weight-medium);
|
||||
white-space: nowrap;
|
||||
animation: fade-in var(--ha-animation-duration-fast, 150ms) ease-out;
|
||||
}
|
||||
|
||||
@keyframes fade-in {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
ha-camera-stream {
|
||||
transition: none;
|
||||
}
|
||||
.wheel-hint {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"ha-camera-player": HaCameraPlayer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import { consumeLocalize } from "../../common/decorators/consume-context-entry";
|
||||
import type { LocalizeFunc } from "../../common/translations/localize";
|
||||
import type {
|
||||
CameraStreamStatus,
|
||||
CameraStreamType,
|
||||
} from "../../data/camera-streams";
|
||||
import "../ha-spinner";
|
||||
|
||||
/** Names of the protocols, the same in every language. */
|
||||
export const CAMERA_STREAM_TYPE_NAMES: Record<CameraStreamType, string> = {
|
||||
web_rtc: "WebRTC",
|
||||
hls: "HLS",
|
||||
mjpeg: "MJPEG",
|
||||
};
|
||||
|
||||
/**
|
||||
* Shows over a camera stream how it is connected. A faster stream that still
|
||||
* connects in the background is shown next to the current one.
|
||||
*/
|
||||
@customElement("ha-camera-stream-badge")
|
||||
export class HaCameraStreamBadge extends LitElement {
|
||||
@state()
|
||||
@consumeLocalize()
|
||||
private _localize!: LocalizeFunc;
|
||||
|
||||
@property({ attribute: false }) public status?: CameraStreamStatus;
|
||||
|
||||
protected render() {
|
||||
const status = this.status;
|
||||
if (!status) {
|
||||
return nothing;
|
||||
}
|
||||
const connecting = status.connecting || !status.type;
|
||||
const label = connecting
|
||||
? this._localize("ui.components.camera-player.connecting")
|
||||
: CAMERA_STREAM_TYPE_NAMES[status.type!];
|
||||
|
||||
return html`
|
||||
<button
|
||||
type="button"
|
||||
aria-label=${this._ariaLabel(status)}
|
||||
class=${connecting ? "connecting" : "live"}
|
||||
>
|
||||
<span class="dot"></span>
|
||||
<span class="label">${label}</span>
|
||||
${
|
||||
status.upgrading
|
||||
? html`<span class="upgrading" aria-hidden="true">
|
||||
<ha-spinner></ha-spinner>
|
||||
${CAMERA_STREAM_TYPE_NAMES[status.upgrading]}
|
||||
</span>`
|
||||
: nothing
|
||||
}
|
||||
</button>
|
||||
`;
|
||||
}
|
||||
|
||||
private _ariaLabel(status: CameraStreamStatus): string {
|
||||
if (status.connecting || !status.type) {
|
||||
return this._localize(
|
||||
"ui.components.camera-player.stream_info_connecting"
|
||||
);
|
||||
}
|
||||
if (status.upgrading) {
|
||||
return this._localize(
|
||||
"ui.components.camera-player.stream_info_upgrading",
|
||||
{
|
||||
type: CAMERA_STREAM_TYPE_NAMES[status.type],
|
||||
upgrading: CAMERA_STREAM_TYPE_NAMES[status.upgrading],
|
||||
}
|
||||
);
|
||||
}
|
||||
return this._localize("ui.components.camera-player.stream_info", {
|
||||
type: CAMERA_STREAM_TYPE_NAMES[status.type],
|
||||
});
|
||||
}
|
||||
|
||||
static styles = css`
|
||||
:host {
|
||||
display: inline-flex;
|
||||
}
|
||||
|
||||
/* Readable on a dark and on a bright picture */
|
||||
button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--ha-space-1);
|
||||
height: 28px;
|
||||
padding-inline: var(--ha-space-2);
|
||||
border: 1px solid rgba(255, 255, 255, 0.15);
|
||||
border-radius: var(--ha-border-radius-pill);
|
||||
background-color: rgba(0, 0, 0, 0.45);
|
||||
backdrop-filter: blur(8px);
|
||||
-webkit-backdrop-filter: blur(8px);
|
||||
color: white;
|
||||
font-family: inherit;
|
||||
font-size: var(--ha-font-size-s);
|
||||
font-weight: var(--ha-font-weight-medium);
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:focus-visible {
|
||||
outline: 2px solid white;
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
.dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: var(--ha-border-radius-circle);
|
||||
background-color: var(--ha-color-green-60, #3fb950);
|
||||
}
|
||||
|
||||
.connecting .dot {
|
||||
background-color: rgba(255, 255, 255, 0.8);
|
||||
animation: pulse 1.2s ease-in-out infinite;
|
||||
}
|
||||
|
||||
.upgrading {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--ha-space-1);
|
||||
padding-inline-start: var(--ha-space-1);
|
||||
border-inline-start: 1px solid rgba(255, 255, 255, 0.3);
|
||||
opacity: 0.7;
|
||||
--ha-spinner-indicator-color: white;
|
||||
--ha-spinner-size: 10px;
|
||||
--ha-spinner-divider-color: rgba(255, 255, 255, 0.3);
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
50% {
|
||||
opacity: 0.3;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.connecting .dot {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"ha-camera-stream-badge": HaCameraStreamBadge;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Zoom and pan of content that fills a viewport, applied as
|
||||
* `translate(x, y) scale(scale)` with the transform origin at the top left.
|
||||
*/
|
||||
export interface ZoomPan {
|
||||
scale: number;
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
||||
export interface ZoomPanSize {
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
|
||||
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. */
|
||||
export const clampZoomPan = (
|
||||
{ scale, x, y }: ZoomPan,
|
||||
{ width, height }: ZoomPanSize,
|
||||
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;
|
||||
return {
|
||||
scale: clampedScale,
|
||||
x: Math.min(0, Math.max(minX, x)),
|
||||
y: Math.min(0, Math.max(minY, y)),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Zooms to a new scale while the point under the cursor or between the
|
||||
* fingers stays where it is. The point is relative to the viewport.
|
||||
*/
|
||||
export const zoomAtPoint = (
|
||||
zoomPan: ZoomPan,
|
||||
scale: number,
|
||||
point: { x: number; y: number },
|
||||
size: ZoomPanSize,
|
||||
maxScale = MAX_ZOOM
|
||||
): ZoomPan => {
|
||||
const clampedScale = Math.min(Math.max(scale, 1), maxScale);
|
||||
const ratio = clampedScale / zoomPan.scale;
|
||||
return clampZoomPan(
|
||||
{
|
||||
scale: clampedScale,
|
||||
x: point.x - (point.x - zoomPan.x) * ratio,
|
||||
y: point.y - (point.y - zoomPan.y) * ratio,
|
||||
},
|
||||
size,
|
||||
maxScale
|
||||
);
|
||||
};
|
||||
|
||||
export const panBy = (
|
||||
zoomPan: ZoomPan,
|
||||
dx: number,
|
||||
dy: number,
|
||||
size: ZoomPanSize,
|
||||
maxScale = MAX_ZOOM
|
||||
): ZoomPan =>
|
||||
clampZoomPan(
|
||||
{ ...zoomPan, x: zoomPan.x + dx, y: zoomPan.y + dy },
|
||||
size,
|
||||
maxScale
|
||||
);
|
||||
@@ -239,6 +239,10 @@ export class HaBottomSheet extends ScrollableFadeMixin(LitElement) {
|
||||
}
|
||||
|
||||
private _handleTouchStart = (ev: TouchEvent) => {
|
||||
if (ev.touches.length > 1) {
|
||||
this._cancelResizing();
|
||||
return;
|
||||
}
|
||||
if (this.preventScrimClose) {
|
||||
return;
|
||||
}
|
||||
@@ -265,6 +269,10 @@ export class HaBottomSheet extends ScrollableFadeMixin(LitElement) {
|
||||
}
|
||||
|
||||
private _handleTouchMove = (ev: TouchEvent) => {
|
||||
if (ev.touches.length > 1) {
|
||||
this._cancelResizing();
|
||||
return;
|
||||
}
|
||||
const currentY = ev.touches[0].clientY;
|
||||
const delta = this._gestureRecognizer.move(currentY);
|
||||
|
||||
@@ -337,6 +345,18 @@ export class HaBottomSheet extends ScrollableFadeMixin(LitElement) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* A second finger makes it a pinch, like zooming a camera picture, which
|
||||
* never moves the sheet. Puts the sheet back where it was.
|
||||
*/
|
||||
private _cancelResizing() {
|
||||
this._unregisterResizeHandlers();
|
||||
if (this._isDragging) {
|
||||
this._isDragging = false;
|
||||
this._animateSnapBack();
|
||||
}
|
||||
}
|
||||
|
||||
private _unregisterResizeHandlers = () => {
|
||||
document.removeEventListener("touchmove", this._handleTouchMove);
|
||||
document.removeEventListener("touchend", this._handleTouchEnd);
|
||||
|
||||
@@ -19,9 +19,11 @@ import {
|
||||
type CameraEntity,
|
||||
} from "../data/camera";
|
||||
import {
|
||||
computeCameraStreamStatus,
|
||||
MJPEG_STREAM,
|
||||
selectCameraStreams,
|
||||
type CameraStream,
|
||||
type CameraStreamStatus,
|
||||
type CameraStreamTracks,
|
||||
type CameraStreamType,
|
||||
} from "../data/camera-streams";
|
||||
@@ -32,10 +34,16 @@ import {
|
||||
formattersContext,
|
||||
} from "../data/context";
|
||||
import "./ha-hls-player";
|
||||
import type { HaHLSPlayer } from "./ha-hls-player";
|
||||
import "./ha-mjpeg-player";
|
||||
import type { HaMjpegPlayer } from "./ha-mjpeg-player";
|
||||
import "./ha-web-rtc-player";
|
||||
import type { HaWebRtcPlayer } from "./ha-web-rtc-player";
|
||||
|
||||
export type { CameraStreamType } from "../data/camera-streams";
|
||||
export type {
|
||||
CameraStreamStatus,
|
||||
CameraStreamType,
|
||||
} from "../data/camera-streams";
|
||||
|
||||
@customElement("ha-camera-stream")
|
||||
export class HaCameraStream extends LitElement {
|
||||
@@ -70,6 +78,13 @@ export class HaCameraStream extends LitElement {
|
||||
@property({ type: Boolean, attribute: "allow-exoplayer" })
|
||||
public allowExoPlayer = false;
|
||||
|
||||
/**
|
||||
* Shows an MJPEG stream in a player that can also open it picture in
|
||||
* picture, which copies its frames into a video while open.
|
||||
*/
|
||||
@property({ type: Boolean, attribute: "mjpeg-video" })
|
||||
public mjpegVideo = false;
|
||||
|
||||
// Video background image before its loaded
|
||||
@state() private _posterUrl?: string;
|
||||
|
||||
@@ -81,10 +96,20 @@ export class HaCameraStream extends LitElement {
|
||||
|
||||
@state() private _webRtcStreams?: CameraStreamTracks;
|
||||
|
||||
// Streams that played their first frame
|
||||
@state() private _loadedStreams: ReadonlySet<CameraStreamType> = new Set();
|
||||
|
||||
@query("ha-web-rtc-player") private _webRtcPlayer?: HaWebRtcPlayer;
|
||||
|
||||
@query(
|
||||
"ha-hls-player:not(.hidden), ha-web-rtc-player:not(.hidden), ha-mjpeg-player"
|
||||
)
|
||||
private _visiblePlayer?: HaHLSPlayer | HaWebRtcPlayer | HaMjpegPlayer;
|
||||
|
||||
private _visibleStreamType?: CameraStreamType;
|
||||
|
||||
private _status?: CameraStreamStatus;
|
||||
|
||||
private _capabilitiesRequest = 0;
|
||||
|
||||
private _thumbnailApi = memoizeOne(
|
||||
@@ -125,6 +150,19 @@ export class HaCameraStream extends LitElement {
|
||||
return this._webRtcPlayer?.microphoneTrack;
|
||||
}
|
||||
|
||||
/** The video of the shown stream, for picture in picture and the like. */
|
||||
public get videoElement(): HTMLVideoElement | undefined {
|
||||
return this._visiblePlayer?.videoElement;
|
||||
}
|
||||
|
||||
/** Gets the video of the shown stream ready for picture in picture. */
|
||||
public async preparePictureInPicture() {
|
||||
const player = this._visiblePlayer;
|
||||
if (player && "preparePictureInPicture" in player) {
|
||||
await player.preparePictureInPicture();
|
||||
}
|
||||
}
|
||||
|
||||
/** Toggles sending the microphone to the camera, only on WebRTC streams. */
|
||||
public async toggleMicrophone() {
|
||||
await this._webRtcPlayer?.toggleMicrophone();
|
||||
@@ -157,6 +195,22 @@ export class HaCameraStream extends LitElement {
|
||||
this._visibleStreamType = visibleStreamType;
|
||||
fireEvent(this, "stream-type-changed", { type: visibleStreamType });
|
||||
}
|
||||
const status = computeCameraStreamStatus(
|
||||
streams,
|
||||
this._loadedStreams,
|
||||
this._hlsStreams,
|
||||
this._webRtcStreams
|
||||
);
|
||||
if (
|
||||
!this._status ||
|
||||
status.type !== this._status.type ||
|
||||
status.connecting !== this._status.connecting ||
|
||||
status.upgrading !== this._status.upgrading ||
|
||||
status.hasAudio !== this._status.hasAudio
|
||||
) {
|
||||
this._status = status;
|
||||
fireEvent(this, "camera-stream-status-changed", status);
|
||||
}
|
||||
}
|
||||
|
||||
protected render() {
|
||||
@@ -176,14 +230,25 @@ export class HaCameraStream extends LitElement {
|
||||
return nothing;
|
||||
}
|
||||
if (stream.type === MJPEG_STREAM) {
|
||||
const mjpegPath = computeMJPEGStreamUrl(this.stateObj);
|
||||
const streamUrl = __DEMO__
|
||||
? this.stateObj.attributes.entity_picture
|
||||
: this._connected
|
||||
? computeMJPEGStreamUrl(this.stateObj)
|
||||
? // Like the other streams, as the frontend can be served elsewhere
|
||||
mjpegPath && this._connection.hassUrl(mjpegPath)
|
||||
: this._posterUrl;
|
||||
if (!streamUrl) {
|
||||
return nothing;
|
||||
}
|
||||
if (this.mjpegVideo) {
|
||||
return html`<ha-mjpeg-player
|
||||
.src=${streamUrl}
|
||||
.alt=${`Preview of the ${this._formatters?.formatEntityName(this.stateObj) ?? ""} camera.`}
|
||||
.aspectRatio=${this.aspectRatio}
|
||||
.fitMode=${this.fitMode}
|
||||
@load=${this._handleMjpegLoad}
|
||||
></ha-mjpeg-player>`;
|
||||
}
|
||||
return html`<img
|
||||
.src=${streamUrl}
|
||||
style=${styleMap({
|
||||
@@ -205,6 +270,7 @@ export class HaCameraStream extends LitElement {
|
||||
.entityid=${this.stateObj.entity_id}
|
||||
.posterUrl=${this._posterUrl}
|
||||
@streams=${this._handleHlsStreams}
|
||||
@load=${this._handleHlsLoad}
|
||||
class=${stream.visible ? "" : "hidden"}
|
||||
.aspectRatio=${this.aspectRatio}
|
||||
.fitMode=${this.fitMode}
|
||||
@@ -220,6 +286,7 @@ export class HaCameraStream extends LitElement {
|
||||
.entityid=${this.stateObj.entity_id}
|
||||
.posterUrl=${this._posterUrl}
|
||||
@streams=${this._handleWebRtcStreams}
|
||||
@load=${this._handleWebRtcLoad}
|
||||
class=${stream.visible ? "" : "hidden"}
|
||||
.aspectRatio=${this.aspectRatio}
|
||||
.fitMode=${this.fitMode}
|
||||
@@ -261,6 +328,20 @@ export class HaCameraStream extends LitElement {
|
||||
private _resetStreams() {
|
||||
this._hlsStreams = undefined;
|
||||
this._webRtcStreams = undefined;
|
||||
this._loadedStreams = new Set();
|
||||
}
|
||||
|
||||
private _setLoaded(type: CameraStreamType, loaded: boolean) {
|
||||
if (this._loadedStreams.has(type) === loaded) {
|
||||
return;
|
||||
}
|
||||
const loadedStreams = new Set(this._loadedStreams);
|
||||
if (loaded) {
|
||||
loadedStreams.add(type);
|
||||
} else {
|
||||
loadedStreams.delete(type);
|
||||
}
|
||||
this._loadedStreams = loadedStreams;
|
||||
}
|
||||
|
||||
private async _getPosterUrl(): Promise<void> {
|
||||
@@ -278,11 +359,27 @@ export class HaCameraStream extends LitElement {
|
||||
}
|
||||
|
||||
private _handleImageLoad() {
|
||||
this._setLoaded(MJPEG_STREAM, true);
|
||||
fireEvent(this, "load");
|
||||
}
|
||||
|
||||
private _handleMjpegLoad() {
|
||||
this._setLoaded(MJPEG_STREAM, true);
|
||||
}
|
||||
|
||||
private _handleHlsLoad() {
|
||||
this._setLoaded(STREAM_TYPE_HLS, true);
|
||||
}
|
||||
|
||||
private _handleWebRtcLoad() {
|
||||
this._setLoaded(STREAM_TYPE_WEB_RTC, true);
|
||||
}
|
||||
|
||||
private _handleHlsStreams(ev: HASSDomEvent<HASSDomEvents["streams"]>) {
|
||||
this._hlsStreams = ev.detail;
|
||||
if (!ev.detail.hasVideo) {
|
||||
this._setLoaded(STREAM_TYPE_HLS, false);
|
||||
}
|
||||
}
|
||||
|
||||
private _handleWebRtcStreams(ev: HASSDomEvent<HASSDomEvents["streams"]>) {
|
||||
@@ -294,9 +391,13 @@ export class HaCameraStream extends LitElement {
|
||||
) {
|
||||
// Without another stream type, keep the player to show why it failed.
|
||||
// Only a browser without WebRTC falls back to MJPEG.
|
||||
this._setLoaded(STREAM_TYPE_WEB_RTC, false);
|
||||
return;
|
||||
}
|
||||
this._webRtcStreams = ev.detail;
|
||||
if (!ev.detail.hasVideo) {
|
||||
this._setLoaded(STREAM_TYPE_WEB_RTC, false);
|
||||
}
|
||||
}
|
||||
|
||||
private _streams = memoizeOne(
|
||||
@@ -318,7 +419,8 @@ export class HaCameraStream extends LitElement {
|
||||
|
||||
static styles = css`
|
||||
:host,
|
||||
img {
|
||||
img,
|
||||
ha-mjpeg-player {
|
||||
display: block;
|
||||
}
|
||||
|
||||
@@ -350,6 +452,7 @@ declare global {
|
||||
load: undefined;
|
||||
"stream-type-changed": { type?: CameraStreamType };
|
||||
"camera-capabilities-changed": { capabilities?: CameraCapabilities };
|
||||
"camera-stream-status-changed": CameraStreamStatus;
|
||||
streams: {
|
||||
hasAudio: boolean;
|
||||
hasVideo: boolean;
|
||||
|
||||
@@ -22,7 +22,7 @@ type HlsLite = Omit<
|
||||
const HIDDEN_CLEANUP_DELAY = 60000;
|
||||
|
||||
@customElement("ha-hls-player")
|
||||
class HaHLSPlayer extends LitElement {
|
||||
export class HaHLSPlayer extends LitElement {
|
||||
@state()
|
||||
@consumeLocalize()
|
||||
private _localize!: LocalizeFunc;
|
||||
@@ -101,6 +101,11 @@ class HaHLSPlayer extends LitElement {
|
||||
}
|
||||
};
|
||||
|
||||
/** The video that plays the stream, undefined while the app plays it. */
|
||||
public get videoElement(): HTMLVideoElement | undefined {
|
||||
return this._exoPlayer ? undefined : (this._videoEl ?? undefined);
|
||||
}
|
||||
|
||||
public connectedCallback() {
|
||||
super.connectedCallback();
|
||||
HaHLSPlayer.streamCount += 1;
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
import { customElement, property, query, state } from "lit/decorators";
|
||||
import { styleMap } from "lit/directives/style-map";
|
||||
import { fireEvent } from "../common/dom/fire_event";
|
||||
|
||||
// How often the image is checked for its first frame
|
||||
const LOAD_CHECK_INTERVAL = 250;
|
||||
|
||||
// MJPEG streams rarely send more frames than this
|
||||
const PICTURE_IN_PICTURE_FRAME_RATE = 15;
|
||||
|
||||
/**
|
||||
* Shows an MJPEG stream as an image, like any browser can. Picture in
|
||||
* picture only works with a video, so while it is open, the image is copied
|
||||
* into a canvas that a hidden video plays. The video only plays then, so
|
||||
* browsers don't offer picture in picture on their own.
|
||||
*/
|
||||
@customElement("ha-mjpeg-player")
|
||||
export class HaMjpegPlayer extends LitElement {
|
||||
@property() public src?: string;
|
||||
|
||||
@property() public alt?: string;
|
||||
|
||||
@property({ attribute: false }) public aspectRatio?: number;
|
||||
|
||||
@property({ attribute: false }) public fitMode?: "cover" | "contain" | "fill";
|
||||
|
||||
@state() private _loaded = false;
|
||||
|
||||
// The image can be copied, it is not from another origin
|
||||
@state() private _canCopy = false;
|
||||
|
||||
@query("#stream") private _imageEl?: HTMLImageElement;
|
||||
|
||||
@query("video") private _videoEl?: HTMLVideoElement;
|
||||
|
||||
private _canvas?: HTMLCanvasElement;
|
||||
|
||||
private _stream?: MediaStream;
|
||||
|
||||
private _loadCheckInterval?: number;
|
||||
|
||||
private _drawInterval?: number;
|
||||
|
||||
/** The video for picture in picture, undefined when it can't be shown. */
|
||||
public get videoElement(): HTMLVideoElement | undefined {
|
||||
return this._canCopy ? this._videoEl : undefined;
|
||||
}
|
||||
|
||||
/** Starts playing the video, which picture in picture needs first. */
|
||||
public async preparePictureInPicture(): Promise<void> {
|
||||
const video = this._videoEl;
|
||||
if (!this._canCopy || !video || this._stream) {
|
||||
return;
|
||||
}
|
||||
this._drawFrame();
|
||||
// Captures a frame whenever the canvas is drawn
|
||||
this._stream = this._canvas!.captureStream();
|
||||
video.srcObject = this._stream;
|
||||
this._drawInterval = window.setInterval(
|
||||
() => this._drawFrame(),
|
||||
1000 / PICTURE_IN_PICTURE_FRAME_RATE
|
||||
);
|
||||
await video.play();
|
||||
}
|
||||
|
||||
public connectedCallback() {
|
||||
super.connectedCallback();
|
||||
if (this.hasUpdated && !this._loaded) {
|
||||
this._startLoadCheck();
|
||||
}
|
||||
}
|
||||
|
||||
public disconnectedCallback() {
|
||||
super.disconnectedCallback();
|
||||
this._stopLoadCheck();
|
||||
this._stopVideo();
|
||||
}
|
||||
|
||||
protected firstUpdated() {
|
||||
this._startLoadCheck();
|
||||
}
|
||||
|
||||
protected render() {
|
||||
if (!this.src) {
|
||||
return nothing;
|
||||
}
|
||||
const style = styleMap({
|
||||
aspectRatio: this.aspectRatio,
|
||||
objectFit: this.fitMode,
|
||||
});
|
||||
return html`
|
||||
${this._loaded ? nothing : html`<div class="placeholder"></div>`}
|
||||
<img
|
||||
id="stream"
|
||||
class=${this._loaded ? "" : "loading"}
|
||||
.src=${this.src}
|
||||
alt=${this.alt ?? ""}
|
||||
style=${style}
|
||||
draggable="false"
|
||||
@load=${this._checkLoaded}
|
||||
@error=${this._stopLoadCheck}
|
||||
/>
|
||||
<video
|
||||
muted
|
||||
playsinline
|
||||
disableremoteplayback
|
||||
aria-hidden="true"
|
||||
@leavepictureinpicture=${this._stopVideo}
|
||||
></video>
|
||||
`;
|
||||
}
|
||||
|
||||
private _startLoadCheck() {
|
||||
this._stopLoadCheck();
|
||||
this._loadCheckInterval = window.setInterval(
|
||||
() => this._checkLoaded(),
|
||||
LOAD_CHECK_INTERVAL
|
||||
);
|
||||
}
|
||||
|
||||
private _stopLoadCheck() {
|
||||
clearInterval(this._loadCheckInterval);
|
||||
this._loadCheckInterval = undefined;
|
||||
}
|
||||
|
||||
// An MJPEG stream never completes loading, so only wait for a first frame
|
||||
private _checkLoaded() {
|
||||
if (!this._imageEl?.naturalWidth) {
|
||||
return;
|
||||
}
|
||||
this._canCopy = this._testCopy();
|
||||
this._setLoaded();
|
||||
}
|
||||
|
||||
private _setLoaded() {
|
||||
this._stopLoadCheck();
|
||||
if (!this._loaded) {
|
||||
this._loaded = true;
|
||||
fireEvent(this, "load");
|
||||
}
|
||||
}
|
||||
|
||||
/** An image from another origin can't be copied out of a canvas. */
|
||||
private _testCopy(): boolean {
|
||||
try {
|
||||
this._drawFrame();
|
||||
this._canvas?.getContext("2d")?.getImageData(0, 0, 1, 1);
|
||||
return Boolean(this._canvas);
|
||||
} catch (_err: unknown) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private _drawFrame() {
|
||||
const image = this._imageEl;
|
||||
if (!image?.naturalWidth) {
|
||||
return;
|
||||
}
|
||||
if (!this._canvas) {
|
||||
this._canvas = document.createElement("canvas");
|
||||
}
|
||||
const canvas = this._canvas;
|
||||
if (
|
||||
canvas.width !== image.naturalWidth ||
|
||||
canvas.height !== image.naturalHeight
|
||||
) {
|
||||
canvas.width = image.naturalWidth;
|
||||
canvas.height = image.naturalHeight;
|
||||
}
|
||||
canvas.getContext("2d")?.drawImage(image, 0, 0);
|
||||
}
|
||||
|
||||
private _stopVideo = () => {
|
||||
clearInterval(this._drawInterval);
|
||||
this._drawInterval = undefined;
|
||||
this._stream?.getTracks().forEach((track) => track.stop());
|
||||
this._stream = undefined;
|
||||
if (this._videoEl) {
|
||||
this._videoEl.srcObject = null;
|
||||
}
|
||||
};
|
||||
|
||||
static styles = css`
|
||||
:host {
|
||||
display: block;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
img {
|
||||
display: block;
|
||||
width: 100%;
|
||||
max-height: var(--video-max-height, calc(100vh - 97px));
|
||||
object-fit: contain;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
}
|
||||
|
||||
/* Keeps the room of a picture until the first frame, so nothing jumps */
|
||||
.placeholder {
|
||||
aspect-ratio: 16 / 9;
|
||||
max-height: var(--video-max-height, calc(100vh - 97px));
|
||||
}
|
||||
|
||||
/* Shows over the placeholder as soon as the browser draws a frame, some
|
||||
browsers don't load a stream they don't show */
|
||||
img.loading {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
/* Only plays for picture in picture, kept rendered so it can play */
|
||||
video {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"ha-mjpeg-player": HaMjpegPlayer;
|
||||
}
|
||||
}
|
||||
@@ -120,6 +120,11 @@ export class HaWebRtcPlayer extends LitElement {
|
||||
return this._localReturnAudioTrack ? "on" : "off";
|
||||
}
|
||||
|
||||
/** The video that plays the stream, undefined while showing an error. */
|
||||
public get videoElement(): HTMLVideoElement | undefined {
|
||||
return this._videoEl ?? undefined;
|
||||
}
|
||||
|
||||
public get microphoneTrack(): MediaStreamTrack | undefined {
|
||||
return this._localReturnAudioTrack;
|
||||
}
|
||||
@@ -624,10 +629,7 @@ export class HaWebRtcPlayer extends LitElement {
|
||||
if (!this._remoteStream) {
|
||||
return;
|
||||
}
|
||||
// If the track is audio and the player is muted, we do not add it to the stream.
|
||||
if (event.track.kind === "audio" && this.muted) {
|
||||
return;
|
||||
}
|
||||
// Audio is always added, even when muted, so it can be unmuted later
|
||||
this._remoteStream.addTrack(event.track);
|
||||
if (!this.hasUpdated) {
|
||||
await this.updateComplete;
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
import { computeDomain } from "../common/entity/compute_domain";
|
||||
import type { EntityRegistryEntry } from "./entity/entity_registry";
|
||||
|
||||
export type PtzDirection = "up" | "right" | "down" | "left";
|
||||
|
||||
export const PTZ_DIRECTIONS: PtzDirection[] = ["up", "right", "down", "left"];
|
||||
|
||||
/** The entities that move a camera, all on the device of the camera. */
|
||||
export interface CameraPtzEntities {
|
||||
/** Buttons that start moving. Only set together with stop. */
|
||||
move?: Record<PtzDirection, string>;
|
||||
/** Button that stops moving and zooming */
|
||||
stop?: string;
|
||||
/** Number entity with the zoom position */
|
||||
zoom?: string;
|
||||
/** Buttons that start zooming in and out until stopped */
|
||||
zoomIn?: string;
|
||||
zoomOut?: string;
|
||||
/** Select entity with the saved positions */
|
||||
preset?: string;
|
||||
/** Entities of these controls that are disabled */
|
||||
disabled: string[];
|
||||
}
|
||||
|
||||
type PtzKey =
|
||||
PtzDirection | Exclude<keyof CameraPtzEntities, "move" | "disabled">;
|
||||
|
||||
// Reolink adds PTZ buttons, which keep moving the camera until stopped
|
||||
const REOLINK_KEYS: Record<string, { domain: string; key: PtzKey }> = {
|
||||
ptz_up: { domain: "button", key: "up" },
|
||||
ptz_right: { domain: "button", key: "right" },
|
||||
ptz_down: { domain: "button", key: "down" },
|
||||
ptz_left: { domain: "button", key: "left" },
|
||||
ptz_stop: { domain: "button", key: "stop" },
|
||||
ptz_zoom_in: { domain: "button", key: "zoomIn" },
|
||||
ptz_zoom_out: { domain: "button", key: "zoomOut" },
|
||||
zoom: { domain: "number", key: "zoom" },
|
||||
ptz_preset: { domain: "select", key: "preset" },
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds the entities that pan, tilt and zoom a camera. Only Reolink cameras
|
||||
* are known so far.
|
||||
*
|
||||
* Moving only works with all four directions and stop, as a press starts
|
||||
* moving and only the stop button ends it. Disabled entities are still
|
||||
* returned, so the user can be told to enable them.
|
||||
*/
|
||||
export const findCameraPtzEntities = (
|
||||
cameraEntityId: string,
|
||||
entities: EntityRegistryEntry[]
|
||||
): CameraPtzEntities | undefined => {
|
||||
const camera = entities.find((entity) => entity.entity_id === cameraEntityId);
|
||||
if (!camera?.device_id || camera.platform !== "reolink") {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const found: Partial<Record<PtzKey, EntityRegistryEntry>> = {};
|
||||
for (const entity of entities) {
|
||||
if (
|
||||
entity.device_id !== camera.device_id ||
|
||||
entity.platform !== "reolink" ||
|
||||
!entity.translation_key
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const match = REOLINK_KEYS[entity.translation_key];
|
||||
if (match && computeDomain(entity.entity_id) === match.domain) {
|
||||
found[match.key] = entity;
|
||||
}
|
||||
}
|
||||
|
||||
const result: CameraPtzEntities = { disabled: [] };
|
||||
const use = (entity: EntityRegistryEntry) => {
|
||||
if (entity.disabled_by) {
|
||||
result.disabled.push(entity.entity_id);
|
||||
}
|
||||
return entity.entity_id;
|
||||
};
|
||||
|
||||
const { up, right, down, left, stop } = found;
|
||||
if (up && right && down && left && stop) {
|
||||
result.move = {
|
||||
up: use(up),
|
||||
right: use(right),
|
||||
down: use(down),
|
||||
left: use(left),
|
||||
};
|
||||
result.stop = use(stop);
|
||||
}
|
||||
if (found.zoom) {
|
||||
result.zoom = use(found.zoom);
|
||||
} else if (
|
||||
found.zoomIn &&
|
||||
found.zoomOut &&
|
||||
stop &&
|
||||
!found.zoomIn.disabled_by &&
|
||||
!found.zoomOut.disabled_by &&
|
||||
!stop.disabled_by
|
||||
) {
|
||||
// Disabled by default, only used when the user enabled them
|
||||
result.zoomIn = found.zoomIn.entity_id;
|
||||
result.zoomOut = found.zoomOut.entity_id;
|
||||
result.stop = stop.entity_id;
|
||||
}
|
||||
if (found.preset) {
|
||||
result.preset = use(found.preset);
|
||||
}
|
||||
|
||||
if (!result.move && !result.zoom && !result.zoomIn && !result.preset) {
|
||||
return undefined;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
@@ -78,3 +78,43 @@ export const selectCameraStreams = ({
|
||||
{ type: STREAM_TYPE_WEB_RTC, visible: false },
|
||||
];
|
||||
};
|
||||
|
||||
/** What the player tells the user about the connection of its stream. */
|
||||
export interface CameraStreamStatus {
|
||||
/** The stream that is shown, undefined until the camera's types are known */
|
||||
type?: CameraStreamType;
|
||||
/** The shown stream has not played its first frame yet */
|
||||
connecting: boolean;
|
||||
/** A faster stream that connects in the background to replace the shown one */
|
||||
upgrading?: CameraStreamType;
|
||||
/** The shown stream has sound */
|
||||
hasAudio: boolean;
|
||||
}
|
||||
|
||||
export const computeCameraStreamStatus = (
|
||||
streams: CameraStream[],
|
||||
loaded: ReadonlySet<CameraStreamType>,
|
||||
hlsStreams?: CameraStreamTracks,
|
||||
webRtcStreams?: CameraStreamTracks
|
||||
): CameraStreamStatus => {
|
||||
const visible = streams.find((stream) => stream.visible);
|
||||
if (!visible) {
|
||||
return { connecting: true, hasAudio: false };
|
||||
}
|
||||
const hidden = streams.find((stream) => !stream.visible);
|
||||
const tracks =
|
||||
visible.type === STREAM_TYPE_HLS
|
||||
? hlsStreams
|
||||
: visible.type === STREAM_TYPE_WEB_RTC
|
||||
? webRtcStreams
|
||||
: undefined;
|
||||
const connecting = !loaded.has(visible.type);
|
||||
return {
|
||||
type: visible.type,
|
||||
connecting,
|
||||
// Only worth mentioning once the shown stream plays, before that the
|
||||
// whole player is connecting
|
||||
upgrading: hidden && !connecting ? hidden.type : undefined,
|
||||
hasAudio: !connecting && (tracks?.hasAudio ?? false),
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,223 @@
|
||||
import {
|
||||
mdiFilmstrip,
|
||||
mdiImageMultipleOutline,
|
||||
mdiLightningBolt,
|
||||
} from "@mdi/js";
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
import { customElement, state } from "lit/decorators";
|
||||
import { consumeLocalize } from "../../common/decorators/consume-context-entry";
|
||||
import type { LocalizeFunc } from "../../common/translations/localize";
|
||||
import { CAMERA_STREAM_TYPE_NAMES } from "../../components/camera/ha-camera-stream-badge";
|
||||
import "../../components/ha-adaptive-dialog";
|
||||
import "../../components/ha-alert";
|
||||
import "../../components/ha-expansion-panel";
|
||||
import "../../components/ha-svg-icon";
|
||||
import {
|
||||
MJPEG_STREAM,
|
||||
STREAM_TYPE_HLS,
|
||||
STREAM_TYPE_WEB_RTC,
|
||||
type CameraStreamType,
|
||||
type StreamType,
|
||||
} from "../../data/camera-streams";
|
||||
import { haStyleDialog, haStyleDialogFixedTop } from "../../resources/styles";
|
||||
import { DialogMixin } from "../dialog-mixin";
|
||||
import type { CameraStreamInfoDialogParams } from "./show-dialog-camera-stream-info";
|
||||
|
||||
// Ordered from the fastest to the most compatible
|
||||
const STREAM_TYPES: { type: CameraStreamType; icon: string }[] = [
|
||||
{ type: STREAM_TYPE_WEB_RTC, icon: mdiLightningBolt },
|
||||
{ type: STREAM_TYPE_HLS, icon: mdiFilmstrip },
|
||||
{ type: MJPEG_STREAM, icon: mdiImageMultipleOutline },
|
||||
];
|
||||
|
||||
@customElement("dialog-camera-stream-info")
|
||||
class DialogCameraStreamInfo extends DialogMixin<CameraStreamInfoDialogParams>(
|
||||
LitElement
|
||||
) {
|
||||
@state()
|
||||
@consumeLocalize()
|
||||
private _localize!: LocalizeFunc;
|
||||
|
||||
protected render() {
|
||||
if (!this.params) {
|
||||
return nothing;
|
||||
}
|
||||
const { status, supportedTypes } = this.params;
|
||||
// Without a video stream, still images are all the camera has
|
||||
const stillImagesOnly = supportedTypes?.length === 0;
|
||||
// MJPEG is only the fallback, only worth explaining while it shows
|
||||
const mjpegInUse = status.type === MJPEG_STREAM;
|
||||
const videoTypes = STREAM_TYPES.filter(({ type }) => type !== MJPEG_STREAM);
|
||||
const isSupported = (type: CameraStreamType) =>
|
||||
!supportedTypes || supportedTypes.includes(type as StreamType);
|
||||
const supported = stillImagesOnly
|
||||
? []
|
||||
: videoTypes.filter(({ type }) => isSupported(type));
|
||||
const unsupported = stillImagesOnly
|
||||
? []
|
||||
: videoTypes.filter(({ type }) => !isSupported(type));
|
||||
const mjpeg = STREAM_TYPES.find(({ type }) => type === MJPEG_STREAM)!;
|
||||
|
||||
return html`
|
||||
<ha-adaptive-dialog
|
||||
open
|
||||
width="medium"
|
||||
.headerTitle=${this._localize("ui.dialogs.camera_stream_info.title")}
|
||||
>
|
||||
<p>
|
||||
${this._localize(
|
||||
stillImagesOnly
|
||||
? "ui.dialogs.camera_stream_info.still_images_only"
|
||||
: "ui.dialogs.camera_stream_info.intro"
|
||||
)}
|
||||
</p>
|
||||
${
|
||||
mjpegInUse && !stillImagesOnly
|
||||
? html`<ha-alert alert-type="info">
|
||||
${this._localize("ui.dialogs.camera_stream_info.fallback_in_use")}
|
||||
</ha-alert>`
|
||||
: nothing
|
||||
}
|
||||
<ul>
|
||||
${mjpegInUse ? this._renderType(mjpeg) : nothing}
|
||||
${supported.map((item) => this._renderType(item))}
|
||||
</ul>
|
||||
${
|
||||
unsupported.length
|
||||
? html`
|
||||
<ha-expansion-panel
|
||||
outlined
|
||||
.header=${this._localize(
|
||||
"ui.dialogs.camera_stream_info.not_supported_count",
|
||||
{ count: unsupported.length }
|
||||
)}
|
||||
>
|
||||
<ul class="unsupported">
|
||||
${unsupported.map((item) => this._renderType(item))}
|
||||
</ul>
|
||||
</ha-expansion-panel>
|
||||
`
|
||||
: nothing
|
||||
}
|
||||
</ha-adaptive-dialog>
|
||||
`;
|
||||
}
|
||||
|
||||
private _renderType({
|
||||
type,
|
||||
icon,
|
||||
}: {
|
||||
type: CameraStreamType;
|
||||
icon: string;
|
||||
}) {
|
||||
const { status } = this.params!;
|
||||
const current = !status.connecting && status.type === type;
|
||||
const upgrading = status.upgrading === type;
|
||||
return html`
|
||||
<li>
|
||||
<ha-svg-icon .path=${icon}></ha-svg-icon>
|
||||
<div>
|
||||
<div class="name">
|
||||
${CAMERA_STREAM_TYPE_NAMES[type]}
|
||||
${
|
||||
current
|
||||
? html`<span class="tag current"
|
||||
>${this._localize("ui.dialogs.camera_stream_info.in_use")}</span
|
||||
>`
|
||||
: upgrading
|
||||
? html`<span class="tag"
|
||||
>${this._localize(
|
||||
"ui.dialogs.camera_stream_info.connecting"
|
||||
)}</span
|
||||
>`
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
<p>
|
||||
${this._localize(`ui.dialogs.camera_stream_info.description.${type}`)}
|
||||
</p>
|
||||
</div>
|
||||
</li>
|
||||
`;
|
||||
}
|
||||
|
||||
static styles = [
|
||||
haStyleDialog,
|
||||
// At the top like the more info dialog it opens from
|
||||
haStyleDialogFixedTop,
|
||||
css`
|
||||
p {
|
||||
margin: 0;
|
||||
color: var(--secondary-text-color);
|
||||
}
|
||||
|
||||
ha-alert {
|
||||
display: block;
|
||||
margin-top: var(--ha-space-4);
|
||||
}
|
||||
|
||||
ul {
|
||||
list-style: none;
|
||||
margin: var(--ha-space-4) 0 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--ha-space-4);
|
||||
}
|
||||
|
||||
li {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: var(--ha-space-4);
|
||||
}
|
||||
|
||||
ha-expansion-panel {
|
||||
display: block;
|
||||
margin-top: var(--ha-space-6);
|
||||
--expansion-panel-summary-padding: 0 var(--ha-space-4);
|
||||
--expansion-panel-content-padding: 0 var(--ha-space-4);
|
||||
}
|
||||
|
||||
ul.unsupported {
|
||||
margin: var(--ha-space-2) 0 var(--ha-space-4);
|
||||
}
|
||||
|
||||
li ha-svg-icon {
|
||||
flex: none;
|
||||
padding: var(--ha-space-2);
|
||||
border-radius: var(--ha-border-radius-circle);
|
||||
background-color: var(--ha-color-fill-neutral-quiet-resting);
|
||||
color: var(--primary-text-color);
|
||||
}
|
||||
|
||||
.name {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--ha-space-2);
|
||||
font-weight: var(--ha-font-weight-medium);
|
||||
margin-bottom: var(--ha-space-1);
|
||||
}
|
||||
|
||||
.tag {
|
||||
font-size: var(--ha-font-size-xs);
|
||||
font-weight: var(--ha-font-weight-medium);
|
||||
padding: 2px var(--ha-space-2);
|
||||
border-radius: var(--ha-border-radius-pill);
|
||||
background-color: var(--ha-color-fill-neutral-quiet-resting);
|
||||
color: var(--secondary-text-color);
|
||||
}
|
||||
|
||||
.tag.current {
|
||||
background-color: var(--ha-color-fill-success-quiet-resting);
|
||||
color: var(--ha-color-on-success-quiet);
|
||||
}
|
||||
`,
|
||||
];
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"dialog-camera-stream-info": DialogCameraStreamInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { fireEvent } from "../../common/dom/fire_event";
|
||||
import type { CameraStreamStatus, StreamType } from "../../data/camera-streams";
|
||||
|
||||
export interface CameraStreamInfoDialogParams {
|
||||
status: CameraStreamStatus;
|
||||
/** The stream types the camera supports, undefined while unknown */
|
||||
supportedTypes?: StreamType[];
|
||||
}
|
||||
|
||||
export const showCameraStreamInfoDialog = (
|
||||
element: HTMLElement,
|
||||
params: CameraStreamInfoDialogParams
|
||||
) =>
|
||||
fireEvent(element, "show-dialog", {
|
||||
dialogTag: "dialog-camera-stream-info",
|
||||
dialogImport: () => import("./dialog-camera-stream-info"),
|
||||
dialogParams: params,
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import { css } from "lit";
|
||||
|
||||
/** A quiet note under a camera control, like why it can't be used. */
|
||||
export const cameraControlHintStyle = css`
|
||||
.hint {
|
||||
box-sizing: border-box;
|
||||
width: 100%;
|
||||
max-width: 360px;
|
||||
margin: 0 auto;
|
||||
padding: var(--ha-space-2) var(--ha-space-3);
|
||||
border-radius: var(--ha-border-radius-md);
|
||||
background-color: var(--ha-color-fill-neutral-quiet-resting);
|
||||
color: var(--secondary-text-color);
|
||||
font-size: var(--ha-font-size-s);
|
||||
line-height: var(--ha-line-height-condensed);
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.hint a {
|
||||
color: var(--primary-color);
|
||||
}
|
||||
`;
|
||||
@@ -0,0 +1,584 @@
|
||||
import type { ContextType } from "@lit/context";
|
||||
import {
|
||||
mdiChevronDown,
|
||||
mdiChevronLeft,
|
||||
mdiChevronRight,
|
||||
mdiChevronUp,
|
||||
mdiMinus,
|
||||
mdiPlus,
|
||||
mdiStarFourPointsOutline,
|
||||
} from "@mdi/js";
|
||||
import type { HassEntity } from "home-assistant-js-websocket";
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import { classMap } from "lit/directives/class-map";
|
||||
import { consume } from "../../../../common/decorators/consume";
|
||||
import { consumeLocalize } from "../../../../common/decorators/consume-context-entry";
|
||||
import type {
|
||||
HASSDomCurrentTargetEvent,
|
||||
HASSDomEvent,
|
||||
} from "../../../../common/dom/fire_event";
|
||||
import type { LocalizeFunc } from "../../../../common/translations/localize";
|
||||
import "../../../../components/ha-control-select-menu";
|
||||
import "../../../../components/ha-control-slider";
|
||||
import type { HaDropdownSelectEvent } from "../../../../components/ha-dropdown";
|
||||
import "../../../../components/ha-icon-button";
|
||||
import "../../../../components/ha-svg-icon";
|
||||
import {
|
||||
PTZ_DIRECTIONS,
|
||||
type CameraPtzEntities,
|
||||
type PtzDirection,
|
||||
} from "../../../../data/camera-controls";
|
||||
import {
|
||||
apiContext,
|
||||
formattersContext,
|
||||
statesContext,
|
||||
} from "../../../../data/context";
|
||||
import { UNAVAILABLE } from "../../../../data/entity/entity";
|
||||
import { forwardHaptic } from "../../../../data/haptics";
|
||||
import { setSelectOption } from "../../../../data/select";
|
||||
import { cameraControlHintStyle } from "./camera-control-styles";
|
||||
|
||||
const DIRECTION_ICONS: Record<PtzDirection, string> = {
|
||||
up: mdiChevronUp,
|
||||
right: mdiChevronRight,
|
||||
down: mdiChevronDown,
|
||||
left: mdiChevronLeft,
|
||||
};
|
||||
|
||||
type PtzPress = PtzDirection | "zoom_in" | "zoom_out";
|
||||
|
||||
/**
|
||||
* Pans, tilts and zooms a camera with its PTZ entities. Moving starts while a
|
||||
* direction is pressed and stops when it is released.
|
||||
*/
|
||||
@customElement("ha-more-info-camera-ptz")
|
||||
export class HaMoreInfoCameraPtz extends LitElement {
|
||||
@state()
|
||||
@consumeLocalize()
|
||||
private _localize!: LocalizeFunc;
|
||||
|
||||
@state()
|
||||
@consume({ context: statesContext, subscribe: true })
|
||||
private _states!: ContextType<typeof statesContext>;
|
||||
|
||||
@state()
|
||||
@consume({ context: formattersContext, subscribe: true })
|
||||
private _formatters!: ContextType<typeof formattersContext>;
|
||||
|
||||
@consume({ context: apiContext, subscribe: true })
|
||||
private _api!: ContextType<typeof apiContext>;
|
||||
|
||||
@property({ attribute: false }) public ptz?: CameraPtzEntities;
|
||||
|
||||
@property({ attribute: false }) public deviceId?: string;
|
||||
|
||||
@state() private _pressed?: PtzPress;
|
||||
|
||||
public disconnectedCallback() {
|
||||
super.disconnectedCallback();
|
||||
// Never leave the camera moving
|
||||
this._release();
|
||||
}
|
||||
|
||||
protected render() {
|
||||
const ptz = this.ptz;
|
||||
if (!ptz) {
|
||||
return nothing;
|
||||
}
|
||||
const zoom = ptz.zoom ? this._states[ptz.zoom] : undefined;
|
||||
const preset = ptz.preset ? this._states[ptz.preset] : undefined;
|
||||
const hint = this._hint();
|
||||
|
||||
return html`
|
||||
<div class="ptz">
|
||||
<div class="pad">
|
||||
${ptz.move ? this._renderMove() : nothing}
|
||||
${
|
||||
ptz.zoom
|
||||
? this._renderZoomSlider(zoom)
|
||||
: ptz.zoomIn
|
||||
? this._renderZoomButtons()
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
${hint ? html`<div class="hint">${hint}</div>` : nothing}
|
||||
${
|
||||
ptz.preset
|
||||
? html`
|
||||
<ha-control-select-menu
|
||||
.label=${this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.preset"
|
||||
)}
|
||||
.value=${preset?.state}
|
||||
.disabled=${!preset || preset.state === UNAVAILABLE}
|
||||
.options=${this._presetOptions(preset)}
|
||||
@wa-select=${this._presetSelected}
|
||||
>
|
||||
<ha-svg-icon
|
||||
slot="icon"
|
||||
.path=${mdiStarFourPointsOutline}
|
||||
></ha-svg-icon>
|
||||
</ha-control-select-menu>
|
||||
`
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
private _presetOptions(preset?: HassEntity) {
|
||||
const options = (preset?.attributes.options as string[] | undefined) ?? [];
|
||||
return options.map((option) => ({
|
||||
value: option,
|
||||
label: this._formatters.formatEntityState(preset!, option),
|
||||
}));
|
||||
}
|
||||
|
||||
private _isUsable(entityId?: string): boolean {
|
||||
const stateObj = entityId ? this._states[entityId] : undefined;
|
||||
return Boolean(stateObj && stateObj.state !== UNAVAILABLE);
|
||||
}
|
||||
|
||||
private _moveUsable(): boolean {
|
||||
const ptz = this.ptz!;
|
||||
return (
|
||||
this._isUsable(ptz.stop) &&
|
||||
PTZ_DIRECTIONS.every((direction) => this._isUsable(ptz.move?.[direction]))
|
||||
);
|
||||
}
|
||||
|
||||
private _hint() {
|
||||
const ptz = this.ptz!;
|
||||
if (ptz.disabled.length) {
|
||||
return this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.disabled_entities",
|
||||
{
|
||||
device_settings: this.deviceId
|
||||
? html`<a href=${`/config/devices/device/${this.deviceId}`}
|
||||
>${this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.device_settings"
|
||||
)}</a
|
||||
>`
|
||||
: this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.device_settings"
|
||||
),
|
||||
}
|
||||
);
|
||||
}
|
||||
if (ptz.move && !this._moveUsable()) {
|
||||
return this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.unavailable"
|
||||
);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private _renderMove() {
|
||||
const disabled = !this._moveUsable();
|
||||
return html`
|
||||
<div
|
||||
class=${classMap({ dpad: true, disabled })}
|
||||
role="group"
|
||||
aria-label=${this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.move"
|
||||
)}
|
||||
>
|
||||
${PTZ_DIRECTIONS.map(
|
||||
(direction) => html`
|
||||
<button
|
||||
type="button"
|
||||
class=${classMap({
|
||||
sector: true,
|
||||
[direction]: true,
|
||||
pressed: this._pressed === direction,
|
||||
})}
|
||||
data-press=${direction}
|
||||
aria-label=${this._localize(
|
||||
`ui.dialogs.more_info_control.camera.ptz.${direction}`
|
||||
)}
|
||||
.disabled=${disabled}
|
||||
@pointerdown=${this._pointerDown}
|
||||
@pointerup=${this._release}
|
||||
@pointercancel=${this._release}
|
||||
@lostpointercapture=${this._release}
|
||||
@keydown=${this._keyDown}
|
||||
@keyup=${this._keyUp}
|
||||
@blur=${this._release}
|
||||
@contextmenu=${this._preventContextMenu}
|
||||
>
|
||||
<ha-svg-icon .path=${DIRECTION_ICONS[direction]}></ha-svg-icon>
|
||||
</button>
|
||||
`
|
||||
)}
|
||||
<div class="center"></div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
private _renderZoomSlider(zoom?: HassEntity) {
|
||||
const disabled = !zoom || zoom.state === UNAVAILABLE;
|
||||
const min = Number(zoom?.attributes.min ?? 0);
|
||||
const max = Number(zoom?.attributes.max ?? 100);
|
||||
const step = Number(zoom?.attributes.step ?? 1);
|
||||
const value = Number(zoom?.state);
|
||||
const label = this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.zoom"
|
||||
);
|
||||
return html`
|
||||
<div class="zoom">
|
||||
<ha-icon-button
|
||||
.label=${this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.zoom_in"
|
||||
)}
|
||||
.path=${mdiPlus}
|
||||
.disabled=${disabled || value >= max}
|
||||
data-direction="1"
|
||||
@click=${this._stepZoom}
|
||||
></ha-icon-button>
|
||||
<ha-control-slider
|
||||
vertical
|
||||
show-handle
|
||||
.label=${label}
|
||||
.disabled=${disabled}
|
||||
.min=${min}
|
||||
.max=${max}
|
||||
.step=${step}
|
||||
.value=${Number.isNaN(value) ? undefined : value}
|
||||
@value-changed=${this._zoomChanged}
|
||||
></ha-control-slider>
|
||||
<ha-icon-button
|
||||
.label=${this._localize(
|
||||
"ui.dialogs.more_info_control.camera.ptz.zoom_out"
|
||||
)}
|
||||
.path=${mdiMinus}
|
||||
.disabled=${disabled || value <= min}
|
||||
data-direction="-1"
|
||||
@click=${this._stepZoom}
|
||||
></ha-icon-button>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
private _renderZoomButtons() {
|
||||
const ptz = this.ptz!;
|
||||
const disabled =
|
||||
!this._isUsable(ptz.zoomIn) ||
|
||||
!this._isUsable(ptz.zoomOut) ||
|
||||
!this._isUsable(ptz.stop);
|
||||
return html`
|
||||
<div class="zoom">
|
||||
${(["zoom_in", "zoom_out"] as const).map(
|
||||
(press) => html`
|
||||
<button
|
||||
type="button"
|
||||
class=${classMap({
|
||||
"zoom-button": true,
|
||||
pressed: this._pressed === press,
|
||||
})}
|
||||
data-press=${press}
|
||||
aria-label=${this._localize(
|
||||
`ui.dialogs.more_info_control.camera.ptz.${press}`
|
||||
)}
|
||||
.disabled=${disabled}
|
||||
@pointerdown=${this._pointerDown}
|
||||
@pointerup=${this._release}
|
||||
@pointercancel=${this._release}
|
||||
@lostpointercapture=${this._release}
|
||||
@keydown=${this._keyDown}
|
||||
@keyup=${this._keyUp}
|
||||
@blur=${this._release}
|
||||
@contextmenu=${this._preventContextMenu}
|
||||
>
|
||||
<ha-svg-icon
|
||||
.path=${press === "zoom_in" ? mdiPlus : mdiMinus}
|
||||
></ha-svg-icon>
|
||||
</button>
|
||||
`
|
||||
)}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
private _entityForPress(press: PtzPress): string | undefined {
|
||||
const ptz = this.ptz!;
|
||||
if (press === "zoom_in") {
|
||||
return ptz.zoomIn;
|
||||
}
|
||||
if (press === "zoom_out") {
|
||||
return ptz.zoomOut;
|
||||
}
|
||||
return ptz.move?.[press];
|
||||
}
|
||||
|
||||
private _pressButton(entityId: string) {
|
||||
this._api.callService("button", "press", { entity_id: entityId });
|
||||
}
|
||||
|
||||
private _press(press: PtzPress) {
|
||||
const entityId = this._entityForPress(press);
|
||||
if (!entityId || this._pressed) {
|
||||
return;
|
||||
}
|
||||
this._pressed = press;
|
||||
forwardHaptic(this, "light");
|
||||
this._pressButton(entityId);
|
||||
}
|
||||
|
||||
private _release = () => {
|
||||
if (!this._pressed) {
|
||||
return;
|
||||
}
|
||||
this._pressed = undefined;
|
||||
if (this.ptz?.stop) {
|
||||
this._pressButton(this.ptz.stop);
|
||||
}
|
||||
};
|
||||
|
||||
private _pointerDown(
|
||||
ev: PointerEvent & HASSDomCurrentTargetEvent<HTMLButtonElement>
|
||||
) {
|
||||
if (ev.pointerType === "mouse" && ev.button !== 0) {
|
||||
return;
|
||||
}
|
||||
// Keeps the release on this button, also when the pointer leaves it
|
||||
ev.currentTarget.setPointerCapture(ev.pointerId);
|
||||
this._press(ev.currentTarget.dataset.press as PtzPress);
|
||||
}
|
||||
|
||||
private _keyDown(
|
||||
ev: KeyboardEvent & HASSDomCurrentTargetEvent<HTMLButtonElement>
|
||||
) {
|
||||
if ((ev.key === "Enter" || ev.key === " ") && !ev.repeat) {
|
||||
ev.preventDefault();
|
||||
this._press(ev.currentTarget.dataset.press as PtzPress);
|
||||
}
|
||||
}
|
||||
|
||||
private _keyUp(ev: KeyboardEvent) {
|
||||
if (ev.key === "Enter" || ev.key === " ") {
|
||||
ev.preventDefault();
|
||||
this._release();
|
||||
}
|
||||
}
|
||||
|
||||
private _preventContextMenu(ev: Event) {
|
||||
// A long press on touch screens would open the context menu
|
||||
ev.preventDefault();
|
||||
}
|
||||
|
||||
private _setZoom(value: number) {
|
||||
this._api.callService("number", "set_value", {
|
||||
entity_id: this.ptz!.zoom!,
|
||||
value,
|
||||
});
|
||||
}
|
||||
|
||||
private _zoomChanged(ev: HASSDomEvent<{ value?: number }>) {
|
||||
if (ev.detail.value !== undefined) {
|
||||
this._setZoom(ev.detail.value);
|
||||
}
|
||||
}
|
||||
|
||||
private _stepZoom(ev: HASSDomCurrentTargetEvent<HTMLElement>) {
|
||||
const zoom = this._states[this.ptz!.zoom!];
|
||||
if (!zoom) {
|
||||
return;
|
||||
}
|
||||
const min = Number(zoom.attributes.min ?? 0);
|
||||
const max = Number(zoom.attributes.max ?? 100);
|
||||
const step = Number(zoom.attributes.step ?? 1);
|
||||
// A tenth of the range per press, on the steps of the entity
|
||||
const increment = Math.max(
|
||||
step,
|
||||
Math.round((max - min) / 10 / step) * step
|
||||
);
|
||||
const direction = Number(ev.currentTarget.dataset.direction);
|
||||
const value = Number(zoom.state) + direction * increment;
|
||||
this._setZoom(Math.min(max, Math.max(min, value)));
|
||||
}
|
||||
|
||||
private _presetSelected(ev: HaDropdownSelectEvent) {
|
||||
const option = ev.detail.item.value;
|
||||
const preset = this._states[this.ptz!.preset!];
|
||||
if (!option || !preset || option === preset.state) {
|
||||
return;
|
||||
}
|
||||
setSelectOption(this._api.callService, preset.entity_id, option);
|
||||
}
|
||||
|
||||
static styles = [
|
||||
cameraControlHintStyle,
|
||||
css`
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* As wide as the pad, the note and presets below line up with it */
|
||||
.ptz {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--ha-space-6);
|
||||
width: fit-content;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.ptz > .hint,
|
||||
ha-control-select-menu {
|
||||
display: block;
|
||||
box-sizing: border-box;
|
||||
width: 0;
|
||||
min-width: 100%;
|
||||
max-width: none;
|
||||
}
|
||||
|
||||
.pad {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: var(--ha-space-8);
|
||||
}
|
||||
|
||||
.dpad {
|
||||
--dpad-size: 200px;
|
||||
position: relative;
|
||||
width: var(--dpad-size);
|
||||
height: var(--dpad-size);
|
||||
border-radius: var(--ha-border-radius-circle);
|
||||
overflow: hidden;
|
||||
background-color: var(--ha-color-fill-neutral-quiet-resting);
|
||||
touch-action: none;
|
||||
}
|
||||
|
||||
/* Each direction fills the quarter of the circle towards its side */
|
||||
.sector {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
padding: var(--ha-space-4);
|
||||
margin: 0;
|
||||
border: none;
|
||||
background-color: transparent;
|
||||
color: var(--primary-text-color);
|
||||
cursor: pointer;
|
||||
transition: background-color var(--ha-animation-duration-fast, 150ms)
|
||||
ease-in-out;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
}
|
||||
|
||||
.sector:focus {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.sector.up {
|
||||
clip-path: polygon(0 0, 100% 0, 50% 50%);
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.sector.right {
|
||||
clip-path: polygon(100% 0, 100% 100%, 50% 50%);
|
||||
align-items: center;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.sector.down {
|
||||
clip-path: polygon(0 100%, 100% 100%, 50% 50%);
|
||||
align-items: flex-end;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.sector.left {
|
||||
clip-path: polygon(0 0, 0 100%, 50% 50%);
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
@media (hover: hover) {
|
||||
.sector:hover:not(:disabled) {
|
||||
background-color: var(--ha-color-fill-neutral-quiet-hover);
|
||||
}
|
||||
}
|
||||
|
||||
.sector:focus-visible {
|
||||
background-color: var(--ha-color-fill-neutral-normal-resting);
|
||||
}
|
||||
|
||||
.sector.pressed {
|
||||
background-color: var(--ha-color-fill-primary-normal-active);
|
||||
color: var(--ha-color-on-primary-normal);
|
||||
}
|
||||
|
||||
.sector:disabled,
|
||||
.zoom-button:disabled {
|
||||
cursor: not-allowed;
|
||||
color: var(--disabled-text-color);
|
||||
}
|
||||
|
||||
.center {
|
||||
position: absolute;
|
||||
inset: 30%;
|
||||
border-radius: var(--ha-border-radius-circle);
|
||||
background-color: var(--card-background-color);
|
||||
box-shadow: 0 0 0 1px var(--divider-color);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.zoom {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: var(--ha-space-1);
|
||||
}
|
||||
|
||||
ha-control-slider {
|
||||
height: 120px;
|
||||
--control-slider-thickness: 40px;
|
||||
--control-slider-border-radius: var(--ha-border-radius-pill);
|
||||
--control-slider-color: var(--primary-color);
|
||||
}
|
||||
|
||||
.zoom-button {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
padding: 0;
|
||||
border: none;
|
||||
border-radius: var(--ha-border-radius-circle);
|
||||
background-color: var(--ha-color-fill-neutral-quiet-resting);
|
||||
color: var(--primary-text-color);
|
||||
cursor: pointer;
|
||||
touch-action: none;
|
||||
user-select: none;
|
||||
-webkit-user-select: none;
|
||||
-webkit-tap-highlight-color: transparent;
|
||||
}
|
||||
|
||||
.zoom-button.pressed {
|
||||
background-color: var(--ha-color-fill-primary-normal-active);
|
||||
color: var(--ha-color-on-primary-normal);
|
||||
}
|
||||
|
||||
.zoom-button:focus-visible {
|
||||
outline: 2px solid var(--primary-color);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.sector {
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
`,
|
||||
];
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface HTMLElementTagNameMap {
|
||||
"ha-more-info-camera-ptz": HaMoreInfoCameraPtz;
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,45 @@
|
||||
import type { ContextType } from "@lit/context";
|
||||
import { mdiMicrophone, mdiMicrophoneOff } from "@mdi/js";
|
||||
import {
|
||||
mdiArrowAll,
|
||||
mdiMicrophone,
|
||||
mdiMicrophoneOff,
|
||||
mdiPhone,
|
||||
} from "@mdi/js";
|
||||
import { css, html, LitElement, nothing, type PropertyValues } from "lit";
|
||||
import { customElement, property, query, state } from "lit/decorators";
|
||||
import { ifDefined } from "lit/directives/if-defined";
|
||||
import { classMap } from "lit/directives/class-map";
|
||||
import memoizeOne from "memoize-one";
|
||||
import { watchAudioLevel } from "../../../common/audio/watch-audio-level";
|
||||
import { consume } from "../../../common/decorators/consume";
|
||||
import { consumeLocalize } from "../../../common/decorators/consume-context-entry";
|
||||
import type { HASSDomEvent } from "../../../common/dom/fire_event";
|
||||
import type { LocalizeFunc } from "../../../common/translations/localize";
|
||||
import "../../../components/ha-button";
|
||||
import "../../../components/ha-camera-stream";
|
||||
import {
|
||||
consumeEntityRegistryEntry,
|
||||
consumeLocalize,
|
||||
} from "../../../common/decorators/consume-context-entry";
|
||||
import type {
|
||||
HASSDomCurrentTargetEvent,
|
||||
HASSDomEvent,
|
||||
} from "../../../common/dom/fire_event";
|
||||
import type { LocalizeFunc } from "../../../common/translations/localize";
|
||||
import "../../../components/camera/ha-camera-player";
|
||||
import type { HaCameraPlayer } from "../../../components/camera/ha-camera-player";
|
||||
import type {
|
||||
CameraStreamStatus,
|
||||
CameraStreamType,
|
||||
HaCameraStream,
|
||||
} from "../../../components/ha-camera-stream";
|
||||
import "../../../components/ha-control-button";
|
||||
import "../../../components/ha-icon-button-group";
|
||||
import "../../../components/ha-icon-button-toggle";
|
||||
import "../../../components/ha-spinner";
|
||||
import "../../../components/ha-svg-icon";
|
||||
import "../../../components/ha-tooltip";
|
||||
import type { WebRtcMicrophoneState } from "../../../components/ha-web-rtc-player";
|
||||
import { STREAM_TYPE_WEB_RTC, type CameraEntity } from "../../../data/camera";
|
||||
import { configContext } from "../../../data/context";
|
||||
import { findCameraPtzEntities } from "../../../data/camera-controls";
|
||||
import { configContext, fullEntitiesContext } from "../../../data/context";
|
||||
import { UNAVAILABLE } from "../../../data/entity/entity";
|
||||
import type {
|
||||
EntityRegistryDisplayEntry,
|
||||
EntityRegistryEntry,
|
||||
} from "../../../data/entity/entity_registry";
|
||||
import {
|
||||
canAppStreamMicrophone,
|
||||
isWebRtcStreamErrorCode,
|
||||
@@ -32,7 +51,10 @@ import type {
|
||||
EMError,
|
||||
WebRtcStreamStopped,
|
||||
} from "../../../external_app/external_messaging";
|
||||
import { showToast } from "../../../util/toast";
|
||||
import { cameraControlHintStyle } from "../components/camera/camera-control-styles";
|
||||
import "../components/camera/ha-more-info-camera-ptz";
|
||||
|
||||
type CameraControlTab = "call" | "ptz";
|
||||
|
||||
@customElement("more-info-camera")
|
||||
class MoreInfoCamera extends LitElement {
|
||||
@@ -44,8 +66,16 @@ class MoreInfoCamera extends LitElement {
|
||||
@consume({ context: configContext, subscribe: true })
|
||||
private _config!: ContextType<typeof configContext>;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
private _fullEntities?: EntityRegistryEntry[];
|
||||
|
||||
@property({ attribute: false }) public stateObj?: CameraEntity;
|
||||
|
||||
@state()
|
||||
@consumeEntityRegistryEntry({ entityIdPath: ["stateObj", "entity_id"] })
|
||||
private _entry?: EntityRegistryDisplayEntry;
|
||||
|
||||
@state() private _attached = false;
|
||||
|
||||
@state() private _microphoneState: WebRtcMicrophoneState = "off";
|
||||
@@ -54,9 +84,21 @@ class MoreInfoCamera extends LitElement {
|
||||
|
||||
@state() private _supportsTwoWayAudio = false;
|
||||
|
||||
@query("ha-camera-stream") private _cameraStream?: HaCameraStream;
|
||||
@state() private _streamStatus?: CameraStreamStatus;
|
||||
|
||||
@query(".microphone-meter") private _microphoneMeter?: HTMLElement;
|
||||
@state() private _tab?: CameraControlTab;
|
||||
|
||||
// Why the app could not start or keep the microphone
|
||||
@state() private _microphoneError?: string;
|
||||
|
||||
@query("ha-camera-player") private _cameraStream?: HaCameraPlayer;
|
||||
|
||||
@query(".microphone") private _microphoneMeter?: HTMLElement;
|
||||
|
||||
private _ptzEntities = memoizeOne(
|
||||
(entityId: string, entities?: EntityRegistryEntry[]) =>
|
||||
entities ? findCameraPtzEntities(entityId, entities) : undefined
|
||||
);
|
||||
|
||||
private _stopAudioLevel?: () => void;
|
||||
|
||||
@@ -84,6 +126,7 @@ class MoreInfoCamera extends LitElement {
|
||||
const oldStateObj = changedProps.get("stateObj");
|
||||
if (oldStateObj && oldStateObj.entity_id !== this.stateObj?.entity_id) {
|
||||
this._resetMicrophone();
|
||||
this._tab = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +134,7 @@ class MoreInfoCamera extends LitElement {
|
||||
private _resetMicrophone() {
|
||||
this._stopAppMicrophone();
|
||||
this._microphoneState = "off";
|
||||
this._microphoneError = undefined;
|
||||
}
|
||||
|
||||
protected updated(changedProps: PropertyValues) {
|
||||
@@ -116,24 +160,86 @@ class MoreInfoCamera extends LitElement {
|
||||
this._microphoneMeter?.style.removeProperty("--microphone-level");
|
||||
}
|
||||
|
||||
private _tabs(): CameraControlTab[] {
|
||||
const tabs: CameraControlTab[] = [];
|
||||
if (this._supportsTwoWayAudio) {
|
||||
tabs.push("call");
|
||||
}
|
||||
if (this._ptzEntities(this.stateObj!.entity_id, this._fullEntities)) {
|
||||
tabs.push("ptz");
|
||||
}
|
||||
return tabs;
|
||||
}
|
||||
|
||||
protected render() {
|
||||
if (!this._attached || !this.stateObj) {
|
||||
return nothing;
|
||||
}
|
||||
|
||||
const tabs = this._tabs();
|
||||
const tab = this._tab && tabs.includes(this._tab) ? this._tab : tabs[0];
|
||||
|
||||
return html`
|
||||
<ha-camera-stream
|
||||
.stateObj=${this.stateObj}
|
||||
allow-exoplayer
|
||||
controls
|
||||
@microphone-changed=${this._microphoneChanged}
|
||||
@stream-type-changed=${this._streamTypeChanged}
|
||||
@camera-capabilities-changed=${this._capabilitiesChanged}
|
||||
></ha-camera-stream>
|
||||
${this._supportsTwoWayAudio ? this._renderMicrophoneButton() : nothing}
|
||||
<div class="layout">
|
||||
<ha-camera-player
|
||||
.stateObj=${this.stateObj}
|
||||
allow-exoplayer
|
||||
keep-awake
|
||||
@microphone-changed=${this._microphoneChanged}
|
||||
@stream-type-changed=${this._streamTypeChanged}
|
||||
@camera-stream-status-changed=${this._streamStatusChanged}
|
||||
@camera-capabilities-changed=${this._capabilitiesChanged}
|
||||
></ha-camera-player>
|
||||
${tabs.length ? this._renderControls(tabs, tab) : nothing}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
private _renderControls(tabs: CameraControlTab[], tab?: CameraControlTab) {
|
||||
return html`
|
||||
<div class="controls">
|
||||
${
|
||||
tabs.length > 1
|
||||
? html`
|
||||
<ha-icon-button-group class="tabs">
|
||||
${tabs.map(
|
||||
(item) => html`
|
||||
<ha-icon-button-toggle
|
||||
.label=${this._localize(
|
||||
`ui.dialogs.more_info_control.camera.tabs.${item}`
|
||||
)}
|
||||
.selected=${tab === item}
|
||||
.path=${item === "call" ? mdiPhone : mdiArrowAll}
|
||||
data-tab=${item}
|
||||
@click=${this._selectTab}
|
||||
></ha-icon-button-toggle>
|
||||
`
|
||||
)}
|
||||
</ha-icon-button-group>
|
||||
`
|
||||
: nothing
|
||||
}
|
||||
${
|
||||
tab === "call"
|
||||
? this._renderCall()
|
||||
: tab === "ptz"
|
||||
? html`<ha-more-info-camera-ptz
|
||||
.ptz=${this._ptzEntities(
|
||||
this.stateObj!.entity_id,
|
||||
this._fullEntities
|
||||
)}
|
||||
.deviceId=${this._entry?.device_id}
|
||||
></ha-more-info-camera-ptz>`
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
private _selectTab(ev: HASSDomCurrentTargetEvent<HTMLElement>) {
|
||||
this._tab = ev.currentTarget.dataset.tab as CameraControlTab;
|
||||
}
|
||||
|
||||
/**
|
||||
* Browsers only allow microphone access in a secure context (HTTPS). Without
|
||||
* it, an app can send the microphone itself in a separate stream, while the
|
||||
@@ -147,6 +253,11 @@ class MoreInfoCamera extends LitElement {
|
||||
}
|
||||
|
||||
private _microphoneDisabledReason(): string | undefined {
|
||||
if (this.stateObj!.state === UNAVAILABLE) {
|
||||
return this._localize(
|
||||
"ui.dialogs.more_info_control.camera.microphone_unavailable"
|
||||
);
|
||||
}
|
||||
if (!window.isSecureContext && !this._useAppMicrophone) {
|
||||
return this._localize(
|
||||
"ui.dialogs.more_info_control.camera.microphone_not_secure"
|
||||
@@ -154,7 +265,9 @@ class MoreInfoCamera extends LitElement {
|
||||
}
|
||||
if (this._streamType !== STREAM_TYPE_WEB_RTC) {
|
||||
return this._localize(
|
||||
"ui.dialogs.more_info_control.camera.microphone_webrtc_only"
|
||||
this._streamStatus?.upgrading === STREAM_TYPE_WEB_RTC
|
||||
? "ui.dialogs.more_info_control.camera.microphone_webrtc_connecting"
|
||||
: "ui.dialogs.more_info_control.camera.microphone_webrtc_only"
|
||||
);
|
||||
}
|
||||
if (this._microphoneState === "denied") {
|
||||
@@ -165,61 +278,60 @@ class MoreInfoCamera extends LitElement {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private _renderMicrophoneButton() {
|
||||
private _renderCall() {
|
||||
const disabledReason = this._microphoneDisabledReason();
|
||||
|
||||
const microphoneOn =
|
||||
this._microphoneState === "on" || this._microphoneState === "connecting";
|
||||
|
||||
// Stopping is always possible, so the microphone is never stuck on
|
||||
const disabled =
|
||||
!microphoneOn &&
|
||||
(disabledReason !== undefined || this.stateObj!.state === UNAVAILABLE);
|
||||
const disabled = !microphoneOn && disabledReason !== undefined;
|
||||
|
||||
const button = html`
|
||||
<ha-button
|
||||
.appearance=${microphoneOn ? "accent" : "filled"}
|
||||
.disabled=${disabled}
|
||||
@click=${this._toggleMicrophone}
|
||||
>
|
||||
${
|
||||
this._microphoneState === "connecting"
|
||||
? html`<ha-spinner slot="start" size="tiny"></ha-spinner>`
|
||||
: html`<ha-svg-icon
|
||||
slot="start"
|
||||
.path=${microphoneOn ? mdiMicrophone : mdiMicrophoneOff}
|
||||
></ha-svg-icon>`
|
||||
}
|
||||
${this._localize(`ui.dialogs.more_info_control.camera.${microphoneOn ? "stop_talking" : "start_talking"}`)}
|
||||
</ha-button>
|
||||
`;
|
||||
const hint = disabled ? disabledReason : this._microphoneError;
|
||||
|
||||
// Only for screen readers, the icon says enough
|
||||
const label = this._localize(
|
||||
`ui.dialogs.more_info_control.camera.${
|
||||
this._microphoneState === "connecting"
|
||||
? "connecting_microphone"
|
||||
: microphoneOn
|
||||
? "stop_talking"
|
||||
: "start_talking"
|
||||
}`
|
||||
);
|
||||
|
||||
return html`
|
||||
<div class="buttons">
|
||||
<div class="call">
|
||||
<div
|
||||
id="microphone-button"
|
||||
class="microphone-meter"
|
||||
tabindex=${ifDefined(disabled && disabledReason ? "0" : undefined)}
|
||||
class=${classMap({
|
||||
microphone: true,
|
||||
on: this._microphoneState === "on",
|
||||
})}
|
||||
>
|
||||
${
|
||||
this._microphoneState === "on"
|
||||
? html`<span class="microphone-waves"></span>`
|
||||
: nothing
|
||||
}
|
||||
${button}
|
||||
<span class="waves"></span>
|
||||
<span class="level"></span>
|
||||
<ha-control-button
|
||||
.label=${label}
|
||||
.disabled=${disabled}
|
||||
class=${classMap({ active: microphoneOn })}
|
||||
@click=${this._toggleMicrophone}
|
||||
>
|
||||
${
|
||||
this._microphoneState === "connecting"
|
||||
? html`<ha-spinner size="small"></ha-spinner>`
|
||||
: html`<ha-svg-icon
|
||||
.path=${microphoneOn ? mdiMicrophone : mdiMicrophoneOff}
|
||||
></ha-svg-icon>`
|
||||
}
|
||||
</ha-control-button>
|
||||
</div>
|
||||
${
|
||||
disabled && disabledReason
|
||||
? html`<ha-tooltip for="microphone-button"
|
||||
>${disabledReason}</ha-tooltip
|
||||
>`
|
||||
: nothing
|
||||
}
|
||||
${hint ? html`<div class="hint" role="status">${hint}</div>` : nothing}
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
private _toggleMicrophone() {
|
||||
this._microphoneError = undefined;
|
||||
if (this._useAppMicrophone) {
|
||||
this._toggleAppMicrophone();
|
||||
return;
|
||||
@@ -256,7 +368,7 @@ class MoreInfoCamera extends LitElement {
|
||||
return;
|
||||
}
|
||||
this._microphoneState = "off";
|
||||
showToast(this, { message: this._appErrorMessage(err as EMError) });
|
||||
this._microphoneError = this._appErrorMessage(err as EMError);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,7 +413,7 @@ class MoreInfoCamera extends LitElement {
|
||||
if (stopped.code === "interrupted") {
|
||||
return;
|
||||
}
|
||||
showToast(this, { message: this._appErrorMessage(stopped) });
|
||||
this._microphoneError = this._appErrorMessage(stopped);
|
||||
};
|
||||
|
||||
private _streamTypeChanged(
|
||||
@@ -310,6 +422,12 @@ class MoreInfoCamera extends LitElement {
|
||||
this._streamType = ev.detail.type;
|
||||
}
|
||||
|
||||
private _streamStatusChanged(
|
||||
ev: HASSDomEvent<HASSDomEvents["camera-stream-status-changed"]>
|
||||
) {
|
||||
this._streamStatus = ev.detail;
|
||||
}
|
||||
|
||||
private _capabilitiesChanged(
|
||||
ev: HASSDomEvent<HASSDomEvents["camera-capabilities-changed"]>
|
||||
) {
|
||||
@@ -325,67 +443,163 @@ class MoreInfoCamera extends LitElement {
|
||||
this._microphoneState = ev.detail.state;
|
||||
}
|
||||
|
||||
static styles = css`
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.buttons {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
padding: var(--ha-space-4);
|
||||
}
|
||||
|
||||
.microphone-meter {
|
||||
position: relative;
|
||||
isolation: isolate;
|
||||
}
|
||||
|
||||
ha-spinner {
|
||||
--ha-spinner-indicator-color: currentColor;
|
||||
}
|
||||
|
||||
.microphone-waves {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
z-index: -1;
|
||||
opacity: var(--microphone-level, 0);
|
||||
transition: opacity 150ms linear;
|
||||
}
|
||||
|
||||
.microphone-waves::before,
|
||||
.microphone-waves::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
border-radius: var(--ha-border-radius-pill);
|
||||
background-color: var(--ha-color-fill-primary-loud-resting);
|
||||
animation: microphone-wave 1.4s ease-out infinite;
|
||||
}
|
||||
|
||||
.microphone-waves::after {
|
||||
animation-delay: 0.7s;
|
||||
}
|
||||
|
||||
@keyframes microphone-wave {
|
||||
from {
|
||||
transform: scale(1);
|
||||
opacity: 0.4;
|
||||
static styles = [
|
||||
cameraControlHintStyle,
|
||||
css`
|
||||
:host {
|
||||
display: block;
|
||||
container-type: inline-size;
|
||||
}
|
||||
to {
|
||||
transform: scale(1.15, 1.6);
|
||||
|
||||
.controls {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.tabs {
|
||||
margin-top: var(--ha-space-4);
|
||||
}
|
||||
|
||||
/* Wide enough, like on a desktop: the controls go next to the picture */
|
||||
@container (min-width: 760px) {
|
||||
.layout {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
/* Fills the height down to the dialog's corner, the picture centered */
|
||||
ha-camera-player {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
/* The tabs stay at the top, so they don't move between tabs */
|
||||
.controls {
|
||||
flex: none;
|
||||
width: 340px;
|
||||
padding: var(--ha-space-2) 0;
|
||||
}
|
||||
|
||||
ha-more-info-camera-ptz,
|
||||
.call {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
}
|
||||
}
|
||||
|
||||
ha-more-info-camera-ptz,
|
||||
.call {
|
||||
padding: 0 var(--ha-space-4);
|
||||
margin: var(--ha-space-6) 0;
|
||||
}
|
||||
|
||||
.call {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: var(--ha-space-4);
|
||||
align-self: stretch;
|
||||
}
|
||||
|
||||
.microphone {
|
||||
--microphone-size: 88px;
|
||||
position: relative;
|
||||
width: var(--microphone-size);
|
||||
height: var(--microphone-size);
|
||||
margin: var(--ha-space-4);
|
||||
isolation: isolate;
|
||||
}
|
||||
|
||||
ha-control-button {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
--control-button-border-radius: var(--ha-border-radius-circle);
|
||||
--mdc-icon-size: 36px;
|
||||
transition: transform var(--ha-animation-duration-fast, 150ms) ease-out;
|
||||
}
|
||||
|
||||
ha-control-button.active {
|
||||
--control-button-background-color: var(--primary-color);
|
||||
--control-button-background-opacity: 1;
|
||||
--control-button-icon-color: var(--text-primary-color, white);
|
||||
}
|
||||
|
||||
ha-control-button:active:not([disabled]) {
|
||||
transform: scale(0.95);
|
||||
}
|
||||
|
||||
ha-spinner {
|
||||
--ha-spinner-indicator-color: currentColor;
|
||||
}
|
||||
|
||||
/* Rings that keep moving out while the microphone is on */
|
||||
.waves,
|
||||
.level {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
z-index: -1;
|
||||
border-radius: var(--ha-border-radius-circle);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.waves::before,
|
||||
.waves::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
border-radius: inherit;
|
||||
border: 2px solid var(--primary-color);
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.microphone-waves::before,
|
||||
.microphone-waves::after {
|
||||
animation: none;
|
||||
transform: scale(1.08, 1.3);
|
||||
.microphone.on .waves::before,
|
||||
.microphone.on .waves::after {
|
||||
animation: microphone-wave 2s ease-out infinite;
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
.microphone.on .waves::after {
|
||||
animation-delay: 1s;
|
||||
}
|
||||
|
||||
/* Grows with the voice going into the microphone */
|
||||
.level {
|
||||
background-color: var(--primary-color);
|
||||
opacity: calc(var(--microphone-level, 0) * 0.35);
|
||||
transform: scale(calc(1 + var(--microphone-level, 0) * 0.45));
|
||||
transition:
|
||||
transform 100ms linear,
|
||||
opacity 100ms linear;
|
||||
}
|
||||
|
||||
@keyframes microphone-wave {
|
||||
from {
|
||||
transform: scale(1);
|
||||
opacity: 0.6;
|
||||
}
|
||||
to {
|
||||
transform: scale(1.7);
|
||||
opacity: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.microphone.on .waves::before,
|
||||
.microphone.on .waves::after {
|
||||
animation: none;
|
||||
}
|
||||
.level,
|
||||
ha-control-button {
|
||||
transition: none;
|
||||
}
|
||||
}
|
||||
`,
|
||||
];
|
||||
}
|
||||
|
||||
declare global {
|
||||
|
||||
@@ -1369,6 +1369,20 @@
|
||||
"load_error": "Failed to load data",
|
||||
"retry": "[%key:ui::panel::app::retry%]"
|
||||
},
|
||||
"camera-player": {
|
||||
"mute": "Mute",
|
||||
"unmute": "Unmute",
|
||||
"picture_in_picture": "Picture in picture",
|
||||
"exit_picture_in_picture": "Exit picture in picture",
|
||||
"fullscreen": "Full screen",
|
||||
"exit_fullscreen": "Exit full screen",
|
||||
"reset_zoom": "Reset zoom",
|
||||
"zoom_hint": "Use {modifier} + scroll to zoom",
|
||||
"connecting": "Connecting",
|
||||
"stream_info": "Streaming with {type}. Show connection details",
|
||||
"stream_info_connecting": "Connecting to the camera. Show connection details",
|
||||
"stream_info_upgrading": "Streaming with {type}, connecting with {upgrading} in the background. Show connection details"
|
||||
},
|
||||
"web-rtc-player": {
|
||||
"not_supported": "WebRTC is not supported in this browser.",
|
||||
"start_failed": "Failed to start the WebRTC stream: {message}",
|
||||
@@ -2183,6 +2197,27 @@
|
||||
"failed_to_download": "Failed to download snapshot. Please check the logs for more information.",
|
||||
"start_talking": "Start talking",
|
||||
"stop_talking": "Stop talking",
|
||||
"connecting_microphone": "Connecting microphone",
|
||||
"microphone_unavailable": "The camera is unavailable.",
|
||||
"microphone_webrtc_connecting": "You can talk to this camera as soon as the WebRTC stream is connected.",
|
||||
"tabs": {
|
||||
"call": "Talk",
|
||||
"ptz": "Move camera"
|
||||
},
|
||||
"ptz": {
|
||||
"move": "Move camera",
|
||||
"up": "Tilt up",
|
||||
"down": "Tilt down",
|
||||
"left": "Pan left",
|
||||
"right": "Pan right",
|
||||
"zoom": "Zoom",
|
||||
"zoom_in": "Zoom in",
|
||||
"zoom_out": "Zoom out",
|
||||
"preset": "Preset position",
|
||||
"unavailable": "The camera controls are unavailable.",
|
||||
"disabled_entities": "Some camera controls are disabled. Enable them in the {device_settings} to use them.",
|
||||
"device_settings": "device settings"
|
||||
},
|
||||
"microphone_not_secure": "[%key:ui::components::secure_connection_required::microphone%]",
|
||||
"microphone_webrtc_only": "Talking to this camera is only possible when the stream uses WebRTC.",
|
||||
"microphone_denied": "Microphone access was denied. Allow it in your browser settings to talk to this camera.",
|
||||
@@ -2794,6 +2829,20 @@
|
||||
"skip": "Skip and finish"
|
||||
}
|
||||
},
|
||||
"camera_stream_info": {
|
||||
"title": "Stream connection",
|
||||
"intro": "Home Assistant starts with the connection that works most reliably and switches to a faster one as soon as it is ready.",
|
||||
"still_images_only": "This camera has no video stream, so it shows a series of still images.",
|
||||
"fallback_in_use": "The video streams of this camera could not be started, so still images are shown instead.",
|
||||
"in_use": "In use",
|
||||
"connecting": "Connecting",
|
||||
"not_supported_count": "Not supported by this camera ({count})",
|
||||
"description": {
|
||||
"web_rtc": "Real-time video with the lowest delay, usually below a second. The video takes the most direct way from the camera to your device, which can need extra network setup when you are away from home. Needed to talk to the camera.",
|
||||
"hls": "The video is sent in short pieces through Home Assistant. It works on almost every network and device, but is a few seconds behind. Used while WebRTC connects, or when WebRTC is not available.",
|
||||
"mjpeg": "A series of still images, used when the camera has no video stream or the other connections failed. It has no sound."
|
||||
}
|
||||
},
|
||||
"shortcuts": {
|
||||
"title": "Shortcuts",
|
||||
"enable_shortcuts_hint": "For keyboard shortcuts to work, make sure you have them enabled in your {user_profile}.",
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
clampZoomPan,
|
||||
panBy,
|
||||
ZOOM_PAN_IDENTITY,
|
||||
zoomAtPoint,
|
||||
} from "../../../src/components/camera/zoom-pan";
|
||||
|
||||
const size = { width: 400, height: 300 };
|
||||
|
||||
// A wrong transform shows black bars next to the picture, or zooms away from
|
||||
// what the user pointed at
|
||||
describe("zoomAtPoint", () => {
|
||||
it("keeps the point under the cursor in place", () => {
|
||||
const zoomed = zoomAtPoint(ZOOM_PAN_IDENTITY, 2, { x: 100, y: 75 }, size);
|
||||
expect(zoomed).toEqual({ scale: 2, x: -100, y: -75 });
|
||||
// The content point under the cursor stays at the same screen position
|
||||
expect((100 - zoomed.x) / zoomed.scale).toBe(100 / 1);
|
||||
});
|
||||
|
||||
it("does not zoom out further than the whole picture", () => {
|
||||
const zoomed = zoomAtPoint(
|
||||
{ scale: 2, x: -100, y: -75 },
|
||||
0.5,
|
||||
{ x: 0, y: 0 },
|
||||
size
|
||||
);
|
||||
expect(zoomed).toEqual(ZOOM_PAN_IDENTITY);
|
||||
});
|
||||
|
||||
it("stops at the maximum zoom", () => {
|
||||
expect(
|
||||
zoomAtPoint(ZOOM_PAN_IDENTITY, 50, { x: 0, y: 0 }, size, 4).scale
|
||||
).toBe(4);
|
||||
});
|
||||
|
||||
it("keeps the picture covering the viewport at the edges", () => {
|
||||
// Zooming at the bottom right corner can't move the picture past it
|
||||
expect(zoomAtPoint(ZOOM_PAN_IDENTITY, 2, { x: 400, y: 300 }, size)).toEqual(
|
||||
{ scale: 2, x: -400, y: -300 }
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("panBy", () => {
|
||||
it("pans within the zoomed in picture", () => {
|
||||
expect(panBy({ scale: 2, x: -100, y: -100 }, 50, -20, size)).toEqual({
|
||||
scale: 2,
|
||||
x: -50,
|
||||
y: -120,
|
||||
});
|
||||
});
|
||||
|
||||
it("stops at the edges", () => {
|
||||
expect(panBy({ scale: 2, x: -100, y: -100 }, 500, -500, size)).toEqual({
|
||||
scale: 2,
|
||||
x: 0,
|
||||
y: -300,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not pan when not zoomed in", () => {
|
||||
expect(panBy(ZOOM_PAN_IDENTITY, 50, 50, size)).toEqual(ZOOM_PAN_IDENTITY);
|
||||
});
|
||||
});
|
||||
|
||||
describe("clampZoomPan", () => {
|
||||
it("resets below the original size", () => {
|
||||
expect(clampZoomPan({ scale: 0.8, x: 10, y: 10 }, size)).toEqual(
|
||||
ZOOM_PAN_IDENTITY
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,120 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { findCameraPtzEntities } from "../../src/data/camera-controls";
|
||||
import type { EntityRegistryEntry } from "../../src/data/entity/entity_registry";
|
||||
|
||||
const entry = (
|
||||
entity_id: string,
|
||||
translation_key?: string,
|
||||
extra: Partial<EntityRegistryEntry> = {}
|
||||
) =>
|
||||
({
|
||||
entity_id,
|
||||
translation_key,
|
||||
platform: "reolink",
|
||||
device_id: "cam",
|
||||
disabled_by: null,
|
||||
...extra,
|
||||
}) as EntityRegistryEntry;
|
||||
|
||||
const camera = entry("camera.garden_fluent");
|
||||
const moveButtons = [
|
||||
entry("button.garden_ptz_up", "ptz_up"),
|
||||
entry("button.garden_ptz_right", "ptz_right"),
|
||||
entry("button.garden_ptz_down", "ptz_down"),
|
||||
entry("button.garden_ptz_left", "ptz_left"),
|
||||
entry("button.garden_ptz_stop", "ptz_stop"),
|
||||
];
|
||||
|
||||
// A press starts moving and only stop ends it: offering moves without a stop
|
||||
// button would leave the camera turning
|
||||
describe("findCameraPtzEntities", () => {
|
||||
it("finds the move buttons, zoom and presets of the camera's device", () => {
|
||||
expect(
|
||||
findCameraPtzEntities("camera.garden_fluent", [
|
||||
camera,
|
||||
...moveButtons,
|
||||
entry("number.garden_zoom", "zoom"),
|
||||
entry("select.garden_ptz_preset", "ptz_preset"),
|
||||
// Other devices and platforms are ignored
|
||||
entry("button.porch_ptz_up", "ptz_up", { device_id: "porch" }),
|
||||
entry("number.other_zoom", "zoom", { platform: "other" }),
|
||||
])
|
||||
).toEqual({
|
||||
move: {
|
||||
up: "button.garden_ptz_up",
|
||||
right: "button.garden_ptz_right",
|
||||
down: "button.garden_ptz_down",
|
||||
left: "button.garden_ptz_left",
|
||||
},
|
||||
stop: "button.garden_ptz_stop",
|
||||
zoom: "number.garden_zoom",
|
||||
preset: "select.garden_ptz_preset",
|
||||
disabled: [],
|
||||
});
|
||||
});
|
||||
|
||||
it("only moves with all directions and stop", () => {
|
||||
expect(
|
||||
findCameraPtzEntities("camera.garden_fluent", [
|
||||
camera,
|
||||
...moveButtons.filter((e) => e.translation_key !== "ptz_stop"),
|
||||
])
|
||||
).toBeUndefined();
|
||||
expect(
|
||||
findCameraPtzEntities("camera.garden_fluent", [
|
||||
camera,
|
||||
...moveButtons.slice(1),
|
||||
entry("number.garden_zoom", "zoom"),
|
||||
])
|
||||
).toEqual({ zoom: "number.garden_zoom", disabled: [] });
|
||||
});
|
||||
|
||||
it("reports disabled entities, so the user can enable them", () => {
|
||||
const result = findCameraPtzEntities("camera.garden_fluent", [
|
||||
camera,
|
||||
...moveButtons.map((e) =>
|
||||
e.translation_key === "ptz_stop"
|
||||
? { ...e, disabled_by: "integration" as const }
|
||||
: e
|
||||
),
|
||||
]);
|
||||
expect(result?.move).toBeDefined();
|
||||
expect(result?.disabled).toEqual(["button.garden_ptz_stop"]);
|
||||
});
|
||||
|
||||
it("only uses the zoom buttons when enabled and without a zoom number", () => {
|
||||
const zoomButtons = [
|
||||
entry("button.garden_ptz_zoom_in", "ptz_zoom_in"),
|
||||
entry("button.garden_ptz_zoom_out", "ptz_zoom_out"),
|
||||
];
|
||||
expect(
|
||||
findCameraPtzEntities("camera.garden_fluent", [
|
||||
camera,
|
||||
...moveButtons,
|
||||
...zoomButtons,
|
||||
])
|
||||
).toMatchObject({
|
||||
zoomIn: "button.garden_ptz_zoom_in",
|
||||
zoomOut: "button.garden_ptz_zoom_out",
|
||||
});
|
||||
expect(
|
||||
findCameraPtzEntities("camera.garden_fluent", [
|
||||
camera,
|
||||
...moveButtons,
|
||||
...zoomButtons.map((e) => ({
|
||||
...e,
|
||||
disabled_by: "integration" as const,
|
||||
})),
|
||||
])?.zoomIn
|
||||
).toBeUndefined();
|
||||
});
|
||||
|
||||
it("ignores cameras of other integrations", () => {
|
||||
expect(
|
||||
findCameraPtzEntities("camera.garden_fluent", [
|
||||
{ ...camera, platform: "onvif" },
|
||||
...moveButtons,
|
||||
])
|
||||
).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
computeCameraStreamStatus,
|
||||
selectCameraStreams,
|
||||
type CameraStreamSelectionOptions,
|
||||
} from "../../src/data/camera-streams";
|
||||
@@ -43,3 +44,57 @@ describe("selectCameraStreams", () => {
|
||||
expect(select({ webRtcStreams: failed, hlsStreams: failed })).toBe("mjpeg");
|
||||
});
|
||||
});
|
||||
|
||||
// The badge over the player tells the user how the stream is connected
|
||||
describe("computeCameraStreamStatus", () => {
|
||||
const hlsLoading = [
|
||||
{ type: "hls" as const, visible: true },
|
||||
{ type: "web_rtc" as const, visible: false },
|
||||
];
|
||||
|
||||
it("connects until the shown stream plays", () => {
|
||||
expect(computeCameraStreamStatus([], new Set())).toEqual({
|
||||
connecting: true,
|
||||
hasAudio: false,
|
||||
});
|
||||
expect(computeCameraStreamStatus(hlsLoading, new Set(), ok)).toEqual({
|
||||
type: "hls",
|
||||
connecting: true,
|
||||
upgrading: undefined,
|
||||
hasAudio: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("shows WebRTC connecting in the background while HLS plays", () => {
|
||||
expect(computeCameraStreamStatus(hlsLoading, new Set(["hls"]), ok)).toEqual(
|
||||
{
|
||||
type: "hls",
|
||||
connecting: false,
|
||||
upgrading: "web_rtc",
|
||||
hasAudio: true,
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it("takes the sound of the shown stream", () => {
|
||||
expect(
|
||||
computeCameraStreamStatus(
|
||||
[{ type: "web_rtc", visible: true }],
|
||||
new Set(["web_rtc"]),
|
||||
ok,
|
||||
silent
|
||||
)
|
||||
).toEqual({
|
||||
type: "web_rtc",
|
||||
connecting: false,
|
||||
upgrading: undefined,
|
||||
hasAudio: false,
|
||||
});
|
||||
expect(
|
||||
computeCameraStreamStatus(
|
||||
[{ type: "mjpeg", visible: true }],
|
||||
new Set(["mjpeg"])
|
||||
).hasAudio
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user