mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 03:06:41 +00:00
Use camera ws endpoint to get WebRTC config (#22009)
This commit is contained in:
parent
67217b9dd0
commit
bc51b53b4a
@ -7,10 +7,12 @@ import {
|
|||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state, query } from "lit/decorators";
|
import { customElement, property, state, query } from "lit/decorators";
|
||||||
import { isComponentLoaded } from "../common/config/is_component_loaded";
|
|
||||||
import { fireEvent } from "../common/dom/fire_event";
|
import { fireEvent } from "../common/dom/fire_event";
|
||||||
import { handleWebRtcOffer, WebRtcAnswer } from "../data/camera";
|
import {
|
||||||
import { fetchWebRtcSettings } from "../data/rtsp_to_webrtc";
|
handleWebRtcOffer,
|
||||||
|
WebRtcAnswer,
|
||||||
|
fetchWebRtcClientConfiguration,
|
||||||
|
} from "../data/camera";
|
||||||
import type { HomeAssistant } from "../types";
|
import type { HomeAssistant } from "../types";
|
||||||
import "./ha-alert";
|
import "./ha-alert";
|
||||||
|
|
||||||
@ -90,11 +92,18 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
private async _startWebRtc(): Promise<void> {
|
private async _startWebRtc(): Promise<void> {
|
||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
|
|
||||||
const configuration = await this._fetchPeerConfiguration();
|
const clientConfig = await fetchWebRtcClientConfiguration(
|
||||||
const peerConnection = new RTCPeerConnection(configuration);
|
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
|
// Some cameras (such as nest) require a data channel to establish a stream
|
||||||
// however, not used by any integrations.
|
// however, not used by any integrations.
|
||||||
peerConnection.createDataChannel("dataSendChannel");
|
peerConnection.createDataChannel(clientConfig.dataChannel);
|
||||||
|
}
|
||||||
peerConnection.addTransceiver("audio", { direction: "recvonly" });
|
peerConnection.addTransceiver("audio", { direction: "recvonly" });
|
||||||
peerConnection.addTransceiver("video", { direction: "recvonly" });
|
peerConnection.addTransceiver("video", { direction: "recvonly" });
|
||||||
|
|
||||||
@ -155,23 +164,6 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
this._peerConnection = peerConnection;
|
this._peerConnection = peerConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _fetchPeerConfiguration(): Promise<RTCConfiguration> {
|
|
||||||
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() {
|
private _cleanUp() {
|
||||||
if (this._remoteStream) {
|
if (this._remoteStream) {
|
||||||
this._remoteStream.getTracks().forEach((track) => {
|
this._remoteStream.getTracks().forEach((track) => {
|
||||||
|
@ -133,3 +133,17 @@ export const isCameraMediaSource = (mediaContentId: string) =>
|
|||||||
|
|
||||||
export const getEntityIdFromCameraMediaSource = (mediaContentId: string) =>
|
export const getEntityIdFromCameraMediaSource = (mediaContentId: string) =>
|
||||||
mediaContentId.substring(CAMERA_MEDIA_SOURCE_PREFIX.length);
|
mediaContentId.substring(CAMERA_MEDIA_SOURCE_PREFIX.length);
|
||||||
|
|
||||||
|
export interface WebRTCClientConfiguration {
|
||||||
|
configuration: RTCConfiguration;
|
||||||
|
dataChannel?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const fetchWebRtcClientConfiguration = async (
|
||||||
|
hass: HomeAssistant,
|
||||||
|
entityId: string
|
||||||
|
) =>
|
||||||
|
hass.callWS<WebRTCClientConfiguration>({
|
||||||
|
type: "camera/webrtc/get_client_config",
|
||||||
|
entity_id: entityId,
|
||||||
|
});
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
import { HomeAssistant } from "../types";
|
|
||||||
|
|
||||||
export interface WebRtcSettings {
|
|
||||||
stun_server?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const fetchWebRtcSettings = async (hass: HomeAssistant) =>
|
|
||||||
hass.callWS<WebRtcSettings>({
|
|
||||||
type: "rtsp_to_webrtc/get_settings",
|
|
||||||
});
|
|
Loading…
x
Reference in New Issue
Block a user