// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-nocheck // ==================================================================== // ** Keep modifications to this file to a minimum ** // // Type checking is disabled since this is a modified copy-and-paste of // underlying render() function, but the rest of the class source it not // available as compilation time. // ==================================================================== import { css, html, nothing, unsafeCSS, type CSSResultGroup, type PropertyValues, } from 'lit'; import { customElement, property } from 'lit/decorators.js'; import { HA_CAMERA_STREAM_MUTE_CHANGE_EVENT } from '../components-lib/live/ha-stream-mute-controller.js'; import { dispatchLiveErrorEvent, type LiveError, } from '../components-lib/live/utils/dispatch-live-error.js'; import { MediaLoadedInfoSourceController } from '../components-lib/media-loaded-info-source-controller.js'; import '../components/image-player.js'; import liveHAComponentsStyle from '../scss/live-ha-components.scss'; import type { MediaLoadedInfo, MediaLoadedInfoEventDetail, MediaPlayer, MediaPlayerController, } from '../types.js'; import { onAbort } from '../utils/abort-signal.js'; import './ha-hls-player.js'; import './ha-web-rtc-player.js'; // A failure reported by one of the inner players. Its existence is the failure; // `error` carries whatever the player knew about it. `dispatched` records // whether it has already been announced, so a stream that fails again after // recovering is announced again. interface StreamError { error: LiveError; dispatched: boolean; } void customElements.whenDefined('ha-camera-stream').then(() => { // ======================================================================================== // From: // - https://github.com/home-assistant/frontend/blob/dev/src/data/camera.ts // - https://github.com/home-assistant/frontend/blob/dev/src/common/entity/compute_state_name.ts // - https://github.com/home-assistant/frontend/blob/dev/src/common/entity/compute_object_id.ts // ======================================================================================== const computeMJPEGStreamUrl = (entity: CameraEntity): string => `/api/camera_proxy_stream/${entity.entity_id}?token=${entity.attributes.access_token}`; const STREAM_TYPE_HLS = 'hls'; const STREAM_TYPE_WEB_RTC = 'web_rtc'; const STREAM_TYPE_MJPEG = 'mjpeg'; type StreamType = STREAM_TYPE_HLS | STREAM_TYPE_WEB_RTC | STREAM_TYPE_MJPEG; @customElement('advanced-camera-card-ha-camera-stream') // eslint-disable-next-line @typescript-eslint/no-unused-vars class AdvancedCameraCardHaCameraStream extends customElements.get('ha-camera-stream') implements MediaPlayer { @property({ attribute: false }) public targetID?: string; // ha-camera-stream renders up to three inner players (MJPEG / HLS / // WebRTC), only one visible. Inner leaves all fire `media:loaded` // independently -- we suppress those at this boundary (`stopPropagation` in // `_captureInnerLoad`), cache the latest per type, and republish the // visible one's info via our own source controller in `updated()`. private _mediaLoadedInfoPerStream: Record = {}; private _mediaLoadedInfoSourceController = new MediaLoadedInfoSourceController( this, { getTargetID: () => this.targetID ?? null, }, ); // An inner player that fails renders its own error, but a hidden one is in // a `display: none` subtree and its failure says nothing about the stream // the user is watching. Errors are therefore captured per type here // (`stopPropagation` in `_captureInnerError`) and only re-dispatched once // the failing type is the visible one, which also covers HA later promoting // a previously-hidden stream. See: // https://github.com/dermotduffy/advanced-camera-card/issues/2583 private _errorPerStream: Partial> = {}; // The currently-visible stream type, refreshed in `updated()`. private _visibleStreamType: StreamType | null = null; // HA chooses between a camera's low-latency and audio-carrying streams from // `muted`, and never re-selects in response to the native