diff --git a/src/components/ha-web-rtc-player.ts b/src/components/ha-web-rtc-player.ts index aca8ff001d..5573bc599c 100644 --- a/src/components/ha-web-rtc-player.ts +++ b/src/components/ha-web-rtc-player.ts @@ -7,10 +7,12 @@ import { TemplateResult, } from "lit"; import { customElement, property, state, query } from "lit/decorators"; -import { isComponentLoaded } from "../common/config/is_component_loaded"; import { fireEvent } from "../common/dom/fire_event"; -import { handleWebRtcOffer, WebRtcAnswer } from "../data/camera"; -import { fetchWebRtcSettings } from "../data/rtsp_to_webrtc"; +import { + handleWebRtcOffer, + WebRtcAnswer, + fetchWebRtcClientConfiguration, +} from "../data/camera"; import type { HomeAssistant } from "../types"; import "./ha-alert"; @@ -90,11 +92,18 @@ class HaWebRtcPlayer extends LitElement { private async _startWebRtc(): Promise { this._error = undefined; - const configuration = await this._fetchPeerConfiguration(); - const peerConnection = new RTCPeerConnection(configuration); - // Some cameras (such as nest) require a data channel to establish a stream - // however, not used by any integrations. - peerConnection.createDataChannel("dataSendChannel"); + const clientConfig = await fetchWebRtcClientConfiguration( + this.hass, + this.entityid + ); + + const peerConnection = new RTCPeerConnection(clientConfig.configuration); + + if (clientConfig.dataChannel) { + // Some cameras (such as nest) require a data channel to establish a stream + // however, not used by any integrations. + peerConnection.createDataChannel(clientConfig.dataChannel); + } peerConnection.addTransceiver("audio", { direction: "recvonly" }); peerConnection.addTransceiver("video", { direction: "recvonly" }); @@ -155,23 +164,6 @@ class HaWebRtcPlayer extends LitElement { this._peerConnection = peerConnection; } - private async _fetchPeerConfiguration(): Promise { - if (!isComponentLoaded(this.hass!, "rtsp_to_webrtc")) { - return {}; - } - const settings = await fetchWebRtcSettings(this.hass!); - if (!settings || !settings.stun_server) { - return {}; - } - return { - iceServers: [ - { - urls: [`stun:${settings.stun_server!}`], - }, - ], - }; - } - private _cleanUp() { if (this._remoteStream) { this._remoteStream.getTracks().forEach((track) => { diff --git a/src/data/camera.ts b/src/data/camera.ts index 26d928f754..0bf40f9cb5 100644 --- a/src/data/camera.ts +++ b/src/data/camera.ts @@ -133,3 +133,17 @@ export const isCameraMediaSource = (mediaContentId: string) => export const getEntityIdFromCameraMediaSource = (mediaContentId: string) => mediaContentId.substring(CAMERA_MEDIA_SOURCE_PREFIX.length); + +export interface WebRTCClientConfiguration { + configuration: RTCConfiguration; + dataChannel?: string; +} + +export const fetchWebRtcClientConfiguration = async ( + hass: HomeAssistant, + entityId: string +) => + hass.callWS({ + type: "camera/webrtc/get_client_config", + entity_id: entityId, + }); diff --git a/src/data/rtsp_to_webrtc.ts b/src/data/rtsp_to_webrtc.ts deleted file mode 100644 index 777d7147e4..0000000000 --- a/src/data/rtsp_to_webrtc.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { HomeAssistant } from "../types"; - -export interface WebRtcSettings { - stun_server?: string; -} - -export const fetchWebRtcSettings = async (hass: HomeAssistant) => - hass.callWS({ - type: "rtsp_to_webrtc/get_settings", - });