mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-19 15:26:36 +00:00
parent
7306b8c102
commit
4db908171f
@ -1,4 +1,3 @@
|
|||||||
/* eslint-disable no-console */
|
|
||||||
import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
|
import type { CSSResultGroup, PropertyValues, TemplateResult } from "lit";
|
||||||
import type { UnsubscribeFunc } from "home-assistant-js-websocket";
|
import type { UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||||
import { css, html, LitElement } from "lit";
|
import { css, html, LitElement } from "lit";
|
||||||
@ -108,18 +107,18 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.time("WebRTC");
|
|
||||||
|
|
||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
|
|
||||||
console.timeLog("WebRTC", "start clientConfig");
|
this._startTimer();
|
||||||
|
|
||||||
|
this._logEvent("start clientConfig");
|
||||||
|
|
||||||
this._clientConfig = await fetchWebRtcClientConfiguration(
|
this._clientConfig = await fetchWebRtcClientConfiguration(
|
||||||
this.hass,
|
this.hass,
|
||||||
this.entityid
|
this.entityid
|
||||||
);
|
);
|
||||||
|
|
||||||
console.timeLog("WebRTC", "end clientConfig", this._clientConfig);
|
this._logEvent("end clientConfig", this._clientConfig);
|
||||||
|
|
||||||
this._peerConnection = new RTCPeerConnection(
|
this._peerConnection = new RTCPeerConnection(
|
||||||
this._clientConfig.configuration
|
this._clientConfig.configuration
|
||||||
@ -141,11 +140,10 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
this._peerConnection.onsignalingstatechange = (ev) => {
|
this._peerConnection.onsignalingstatechange = (ev) => {
|
||||||
switch ((ev.target as RTCPeerConnection).signalingState) {
|
switch ((ev.target as RTCPeerConnection).signalingState) {
|
||||||
case "stable":
|
case "stable":
|
||||||
console.timeLog("WebRTC", "ICE negotiation complete");
|
this._logEvent("ICE negotiation complete");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.timeLog(
|
this._logEvent(
|
||||||
"WebRTC",
|
|
||||||
"Signaling state changed",
|
"Signaling state changed",
|
||||||
(ev.target as RTCPeerConnection).signalingState
|
(ev.target as RTCPeerConnection).signalingState
|
||||||
);
|
);
|
||||||
@ -170,7 +168,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
offerToReceiveVideo: true,
|
offerToReceiveVideo: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
console.timeLog("WebRTC", "start createOffer", offerOptions);
|
this._logEvent("start createOffer", offerOptions);
|
||||||
|
|
||||||
const offer: RTCSessionDescriptionInit =
|
const offer: RTCSessionDescriptionInit =
|
||||||
await this._peerConnection.createOffer(offerOptions);
|
await this._peerConnection.createOffer(offerOptions);
|
||||||
@ -179,9 +177,9 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.timeLog("WebRTC", "end createOffer", offer);
|
this._logEvent("end createOffer", offer);
|
||||||
|
|
||||||
console.timeLog("WebRTC", "start setLocalDescription");
|
this._logEvent("start setLocalDescription");
|
||||||
|
|
||||||
await this._peerConnection.setLocalDescription(offer);
|
await this._peerConnection.setLocalDescription(offer);
|
||||||
|
|
||||||
@ -189,7 +187,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.timeLog("WebRTC", "end setLocalDescription");
|
this._logEvent("end setLocalDescription");
|
||||||
|
|
||||||
let candidates = "";
|
let candidates = "";
|
||||||
|
|
||||||
@ -203,11 +201,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
|
|
||||||
console.timeLog(
|
this._logEvent("Ice gathering state changed", iceGatheringState);
|
||||||
"WebRTC",
|
|
||||||
"Ice gathering state changed",
|
|
||||||
iceGatheringState
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -225,7 +219,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
|
|
||||||
const offer_sdp = offer.sdp! + candidates;
|
const offer_sdp = offer.sdp! + candidates;
|
||||||
|
|
||||||
console.timeLog("WebRTC", "start webRtcOffer", offer_sdp);
|
this._logEvent("start webRtcOffer", offer_sdp);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this._unsub = webRtcOffer(this.hass, this.entityid, offer_sdp, (event) =>
|
this._unsub = webRtcOffer(this.hass, this.entityid, offer_sdp, (event) =>
|
||||||
@ -238,8 +232,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private _iceConnectionStateChanged = () => {
|
private _iceConnectionStateChanged = () => {
|
||||||
console.timeLog(
|
this._logEvent(
|
||||||
"WebRTC",
|
|
||||||
"ice connection state change",
|
"ice connection state change",
|
||||||
this._peerConnection?.iceConnectionState
|
this._peerConnection?.iceConnectionState
|
||||||
);
|
);
|
||||||
@ -265,18 +258,19 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
this._candidatesList = [];
|
this._candidatesList = [];
|
||||||
}
|
}
|
||||||
if (event.type === "answer") {
|
if (event.type === "answer") {
|
||||||
console.timeLog("WebRTC", "answer", event.answer);
|
this._logEvent("answer", event.answer);
|
||||||
|
|
||||||
this._handleAnswer(event);
|
this._handleAnswer(event);
|
||||||
}
|
}
|
||||||
if (event.type === "candidate") {
|
if (event.type === "candidate") {
|
||||||
console.timeLog("WebRTC", "remote ice candidate", event.candidate);
|
this._logEvent("remote ice candidate", event.candidate);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this._peerConnection?.addIceCandidate(
|
await this._peerConnection?.addIceCandidate(
|
||||||
new RTCIceCandidate({ candidate: event.candidate, sdpMid: "0" })
|
new RTCIceCandidate({ candidate: event.candidate, sdpMid: "0" })
|
||||||
);
|
);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
console.error(err);
|
console.error(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -291,11 +285,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.timeLog(
|
this._logEvent("local ice candidate", event.candidate?.candidate);
|
||||||
"WebRTC",
|
|
||||||
"local ice candidate",
|
|
||||||
event.candidate?.candidate
|
|
||||||
);
|
|
||||||
|
|
||||||
if (this._sessionId) {
|
if (this._sessionId) {
|
||||||
addWebRtcCandidate(
|
addWebRtcCandidate(
|
||||||
@ -334,19 +324,16 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
sdp: event.answer,
|
sdp: event.answer,
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
console.timeLog("WebRTC", "start setRemoteDescription", remoteDesc);
|
this._logEvent("start setRemoteDescription", remoteDesc);
|
||||||
await this._peerConnection.setRemoteDescription(remoteDesc);
|
await this._peerConnection.setRemoteDescription(remoteDesc);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
this._error = "Failed to connect WebRTC stream: " + err.message;
|
this._error = "Failed to connect WebRTC stream: " + err.message;
|
||||||
this._cleanUp();
|
this._cleanUp();
|
||||||
}
|
}
|
||||||
console.timeLog("WebRTC", "end setRemoteDescription");
|
this._logEvent("end setRemoteDescription");
|
||||||
}
|
}
|
||||||
|
|
||||||
private _cleanUp() {
|
private _cleanUp() {
|
||||||
console.timeLog("WebRTC", "stopped");
|
|
||||||
console.timeEnd("WebRTC");
|
|
||||||
|
|
||||||
if (this._remoteStream) {
|
if (this._remoteStream) {
|
||||||
this._remoteStream.getTracks().forEach((track) => {
|
this._remoteStream.getTracks().forEach((track) => {
|
||||||
track.stop();
|
track.stop();
|
||||||
@ -372,6 +359,9 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
this._peerConnection.onsignalingstatechange = null;
|
this._peerConnection.onsignalingstatechange = null;
|
||||||
|
|
||||||
this._peerConnection = undefined;
|
this._peerConnection = undefined;
|
||||||
|
|
||||||
|
this._logEvent("stopped");
|
||||||
|
this._stopTimer();
|
||||||
}
|
}
|
||||||
this._unsub?.then((unsub) => unsub());
|
this._unsub?.then((unsub) => unsub());
|
||||||
this._unsub = undefined;
|
this._unsub = undefined;
|
||||||
@ -380,17 +370,43 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _loadedData() {
|
private _loadedData() {
|
||||||
console.timeLog("WebRTC", "loadedData");
|
|
||||||
console.timeEnd("WebRTC");
|
|
||||||
|
|
||||||
const video = this._videoEl;
|
const video = this._videoEl;
|
||||||
const stream = video.srcObject as MediaStream;
|
const stream = video.srcObject as MediaStream;
|
||||||
|
|
||||||
fireEvent(this, "load");
|
const data = {
|
||||||
fireEvent(this, "streams", {
|
|
||||||
hasAudio: Boolean(stream?.getAudioTracks().length),
|
hasAudio: Boolean(stream?.getAudioTracks().length),
|
||||||
hasVideo: Boolean(stream?.getVideoTracks().length),
|
hasVideo: Boolean(stream?.getVideoTracks().length),
|
||||||
});
|
};
|
||||||
|
|
||||||
|
fireEvent(this, "load");
|
||||||
|
fireEvent(this, "streams", data);
|
||||||
|
|
||||||
|
this._logEvent("loadedData", data);
|
||||||
|
this._stopTimer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _startTimer() {
|
||||||
|
if (!__DEV__) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.time("WebRTC");
|
||||||
|
}
|
||||||
|
|
||||||
|
private _stopTimer() {
|
||||||
|
if (!__DEV__) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.timeEnd("WebRTC");
|
||||||
|
}
|
||||||
|
|
||||||
|
private _logEvent(msg: string, ...args: unknown[]) {
|
||||||
|
if (!__DEV__) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.timeLog("WebRTC", msg, ...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user