From 605910c68cb4996569219cd79ba7819ea18e31b9 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Tue, 29 Oct 2024 13:30:47 +0100 Subject: [PATCH] move support checks --- src/components/ha-camera-stream.ts | 34 ++++++++++++++--------------- src/components/ha-hls-player.ts | 7 ++++++ src/components/ha-web-rtc-player.ts | 7 ++++++ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/components/ha-camera-stream.ts b/src/components/ha-camera-stream.ts index 652416ad00b5..dcb6412e3514 100644 --- a/src/components/ha-camera-stream.ts +++ b/src/components/ha-camera-stream.ts @@ -7,7 +7,6 @@ import { PropertyValues, } from "lit"; import { customElement, property, state } from "lit/decorators"; -import { isComponentLoaded } from "../common/config/is_component_loaded"; import { computeStateName } from "../common/entity/compute_state_name"; import { supportsFeature } from "../common/entity/supports-feature"; import { @@ -131,6 +130,9 @@ export class HaCameraStream extends LitElement { } private async _getCapabilities() { + this._capabilities = undefined; + this._hlsStreams = undefined; + this._webRtcStreams = undefined; this._capabilities = await fetchCameraCapabilities( this.hass!, this.stateObj!.entity_id @@ -142,26 +144,24 @@ export class HaCameraStream extends LitElement { private get _shouldRenderMJPEG() { if (this._forceMJPEG === this.stateObj!.entity_id) { - // Fallback when unable to fetch stream url - return true; - } - if (!supportsFeature(this.stateObj!, CAMERA_SUPPORT_STREAM)) { - // Steaming is not supported by the camera so fallback to MJPEG stream + // Fallback when unable to stream return true; } if ( - this._capabilities?.frontend_stream_types.length === 1 && - this._capabilities?.frontend_stream_types.includes(STREAM_TYPE_WEB_RTC) + this._capabilities && + (!this._capabilities.frontend_stream_types.includes(STREAM_TYPE_HLS) || + this._hlsStreams?.hasVideo === false) && + (!this._capabilities.frontend_stream_types.includes( + STREAM_TYPE_WEB_RTC + ) || + this._webRtcStreams?.hasVideo === false) ) { - // Browser support required for WebRTC - return typeof RTCPeerConnection === "undefined"; + // No video in HLS stream and no video in WebRTC stream + return true; } - if ( - this._capabilities?.frontend_stream_types.length === 1 && - this._capabilities?.frontend_stream_types.includes(STREAM_TYPE_HLS) - ) { - // Server side stream component required for HLS - return !isComponentLoaded(this.hass!, "stream"); + if (!supportsFeature(this.stateObj!, CAMERA_SUPPORT_STREAM)) { + // Steaming is not supported by the camera so fallback to MJPEG stream + return true; } return false; } @@ -202,8 +202,6 @@ export class HaCameraStream extends LitElement { this._streamType = STREAM_TYPE_HLS; } else if (this._webRtcStreams.hasVideo) { this._streamType = STREAM_TYPE_WEB_RTC; - } else { - this._forceMJPEG = this.stateObj!.entity_id; } } diff --git a/src/components/ha-hls-player.ts b/src/components/ha-hls-player.ts index b33a461a4c43..056e038892cb 100644 --- a/src/components/ha-hls-player.ts +++ b/src/components/ha-hls-player.ts @@ -13,6 +13,7 @@ import { nextRender } from "../common/util/render-status"; import type { HomeAssistant } from "../types"; import "./ha-alert"; import { fetchStreamUrl } from "../data/camera"; +import { isComponentLoaded } from "../common/config/is_component_loaded"; type HlsLite = Omit< HlsType, @@ -109,6 +110,12 @@ class HaHLSPlayer extends LitElement { private async _getStreamUrl(): Promise { this._cleanUp(); this._resetError(); + + if (!isComponentLoaded(this.hass!, "stream")) { + this._setFatalError("Streaming component is not loaded."); + return; + } + if (!this.entityid) { return; } diff --git a/src/components/ha-web-rtc-player.ts b/src/components/ha-web-rtc-player.ts index a1510acdae0b..562bb382ff5b 100644 --- a/src/components/ha-web-rtc-player.ts +++ b/src/components/ha-web-rtc-player.ts @@ -103,6 +103,13 @@ class HaWebRtcPlayer extends LitElement { private async _startWebRtc(): Promise { this._cleanUp(); + // Browser support required for WebRTC + if (typeof RTCPeerConnection === "undefined") { + this._error = "WebRTC is not supported in this browser"; + fireEvent(this, "streams", { hasAudio: false, hasVideo: false }); + return; + } + if (!this.hass || !this.entityid) { return; }