Skip to content

Commit

Permalink
move support checks
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten committed Oct 29, 2024
1 parent c1100cd commit 605910c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/components/ha-camera-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/components/ha-hls-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -109,6 +110,12 @@ class HaHLSPlayer extends LitElement {
private async _getStreamUrl(): Promise<void> {
this._cleanUp();
this._resetError();

if (!isComponentLoaded(this.hass!, "stream")) {
this._setFatalError("Streaming component is not loaded.");
return;
}

if (!this.entityid) {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions src/components/ha-web-rtc-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ class HaWebRtcPlayer extends LitElement {
private async _startWebRtc(): Promise<void> {
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;
}
Expand Down

0 comments on commit 605910c

Please sign in to comment.