mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Add temporary logging to webrtc player (#22213)
* add temporary logging to webrtc player * Update ha-web-rtc-player.ts * Update ha-web-rtc-player.ts * Update ha-web-rtc-player.ts
This commit is contained in:
parent
7d97dbe15b
commit
365b712976
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
import {
|
import {
|
||||||
css,
|
css,
|
||||||
CSSResultGroup,
|
CSSResultGroup,
|
||||||
@ -6,12 +7,13 @@ import {
|
|||||||
PropertyValues,
|
PropertyValues,
|
||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state, query } from "lit/decorators";
|
import { customElement, property, query, state } from "lit/decorators";
|
||||||
|
import { ifDefined } from "lit/directives/if-defined";
|
||||||
import { fireEvent } from "../common/dom/fire_event";
|
import { fireEvent } from "../common/dom/fire_event";
|
||||||
import {
|
import {
|
||||||
|
fetchWebRtcClientConfiguration,
|
||||||
handleWebRtcOffer,
|
handleWebRtcOffer,
|
||||||
WebRtcAnswer,
|
WebRtcAnswer,
|
||||||
fetchWebRtcClientConfiguration,
|
|
||||||
} from "../data/camera";
|
} from "../data/camera";
|
||||||
import type { HomeAssistant } from "../types";
|
import type { HomeAssistant } from "../types";
|
||||||
import "./ha-alert";
|
import "./ha-alert";
|
||||||
@ -39,12 +41,11 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
@property({ type: Boolean, attribute: "playsinline" })
|
@property({ type: Boolean, attribute: "playsinline" })
|
||||||
public playsInline = false;
|
public playsInline = false;
|
||||||
|
|
||||||
@property() public posterUrl!: string;
|
@property({ attribute: "poster-url" }) public posterUrl?: string;
|
||||||
|
|
||||||
@state() private _error?: string;
|
@state() private _error?: string;
|
||||||
|
|
||||||
// don't cache this, as we remove it on disconnects
|
@query("#remote-stream", true) private _videoEl!: HTMLVideoElement;
|
||||||
@query("#remote-stream") private _videoEl!: HTMLVideoElement;
|
|
||||||
|
|
||||||
private _peerConnection?: RTCPeerConnection;
|
private _peerConnection?: RTCPeerConnection;
|
||||||
|
|
||||||
@ -61,7 +62,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
.muted=${this.muted}
|
.muted=${this.muted}
|
||||||
?playsinline=${this.playsInline}
|
?playsinline=${this.playsInline}
|
||||||
?controls=${this.controls}
|
?controls=${this.controls}
|
||||||
.poster=${this.posterUrl}
|
poster=${ifDefined(this.posterUrl)}
|
||||||
@loadeddata=${this._loadedData}
|
@loadeddata=${this._loadedData}
|
||||||
></video>
|
></video>
|
||||||
`;
|
`;
|
||||||
@ -83,20 +84,23 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
if (!changedProperties.has("entityid")) {
|
if (!changedProperties.has("entityid")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!this._videoEl) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this._startWebRtc();
|
this._startWebRtc();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _startWebRtc(): Promise<void> {
|
private async _startWebRtc(): Promise<void> {
|
||||||
|
console.time("WebRTC");
|
||||||
|
|
||||||
this._error = undefined;
|
this._error = undefined;
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "start clientConfig");
|
||||||
|
|
||||||
const clientConfig = await fetchWebRtcClientConfiguration(
|
const clientConfig = await fetchWebRtcClientConfiguration(
|
||||||
this.hass,
|
this.hass,
|
||||||
this.entityid
|
this.entityid
|
||||||
);
|
);
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "end clientConfig", clientConfig);
|
||||||
|
|
||||||
const peerConnection = new RTCPeerConnection(clientConfig.configuration);
|
const peerConnection = new RTCPeerConnection(clientConfig.configuration);
|
||||||
|
|
||||||
if (clientConfig.dataChannel) {
|
if (clientConfig.dataChannel) {
|
||||||
@ -111,30 +115,48 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
offerToReceiveAudio: true,
|
offerToReceiveAudio: true,
|
||||||
offerToReceiveVideo: true,
|
offerToReceiveVideo: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "start createOffer", offerOptions);
|
||||||
|
|
||||||
const offer: RTCSessionDescriptionInit =
|
const offer: RTCSessionDescriptionInit =
|
||||||
await peerConnection.createOffer(offerOptions);
|
await peerConnection.createOffer(offerOptions);
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "end createOffer", offer);
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "start setLocalDescription");
|
||||||
|
|
||||||
await peerConnection.setLocalDescription(offer);
|
await peerConnection.setLocalDescription(offer);
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "end setLocalDescription");
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "start iceResolver");
|
||||||
|
|
||||||
let candidates = ""; // Build an Offer SDP string with ice candidates
|
let candidates = ""; // Build an Offer SDP string with ice candidates
|
||||||
const iceResolver = new Promise<void>((resolve) => {
|
const iceResolver = new Promise<void>((resolve) => {
|
||||||
peerConnection.addEventListener("icecandidate", async (event) => {
|
peerConnection.addEventListener("icecandidate", (event) => {
|
||||||
if (!event.candidate?.candidate) {
|
if (!event.candidate?.candidate) {
|
||||||
resolve(); // Gathering complete
|
resolve(); // Gathering complete
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
console.timeLog("WebRTC", "iceResolver candidate", event.candidate);
|
||||||
candidates += `a=${event.candidate.candidate}\r\n`;
|
candidates += `a=${event.candidate.candidate}\r\n`;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await iceResolver;
|
await iceResolver;
|
||||||
|
|
||||||
|
console.timeLog("WebRTC", "end iceResolver", candidates);
|
||||||
|
|
||||||
const offer_sdp = offer.sdp! + candidates;
|
const offer_sdp = offer.sdp! + candidates;
|
||||||
|
|
||||||
let webRtcAnswer: WebRtcAnswer;
|
let webRtcAnswer: WebRtcAnswer;
|
||||||
try {
|
try {
|
||||||
|
console.timeLog("WebRTC", "start WebRTCOffer", offer_sdp);
|
||||||
webRtcAnswer = await handleWebRtcOffer(
|
webRtcAnswer = await handleWebRtcOffer(
|
||||||
this.hass,
|
this.hass,
|
||||||
this.entityid,
|
this.entityid,
|
||||||
offer_sdp
|
offer_sdp
|
||||||
);
|
);
|
||||||
|
console.timeLog("WebRTC", "end webRtcOffer", webRtcAnswer);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
this._error = "Failed to start WebRTC stream: " + err.message;
|
this._error = "Failed to start WebRTC stream: " + err.message;
|
||||||
peerConnection.close();
|
peerConnection.close();
|
||||||
@ -144,6 +166,7 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
// Setup callbacks to render remote stream once media tracks are discovered.
|
// Setup callbacks to render remote stream once media tracks are discovered.
|
||||||
const remoteStream = new MediaStream();
|
const remoteStream = new MediaStream();
|
||||||
peerConnection.addEventListener("track", (event) => {
|
peerConnection.addEventListener("track", (event) => {
|
||||||
|
console.timeLog("WebRTC", "track", event);
|
||||||
remoteStream.addTrack(event.track);
|
remoteStream.addTrack(event.track);
|
||||||
this._videoEl.srcObject = remoteStream;
|
this._videoEl.srcObject = remoteStream;
|
||||||
});
|
});
|
||||||
@ -155,7 +178,9 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
sdp: webRtcAnswer.answer,
|
sdp: webRtcAnswer.answer,
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
|
console.timeLog("WebRTC", "start setRemoteDescription", remoteDesc);
|
||||||
await peerConnection.setRemoteDescription(remoteDesc);
|
await peerConnection.setRemoteDescription(remoteDesc);
|
||||||
|
console.timeLog("WebRTC", "end setRemoteDescription");
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
this._error = "Failed to connect WebRTC stream: " + err.message;
|
this._error = "Failed to connect WebRTC stream: " + err.message;
|
||||||
peerConnection.close();
|
peerConnection.close();
|
||||||
@ -182,6 +207,8 @@ class HaWebRtcPlayer extends LitElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private _loadedData() {
|
private _loadedData() {
|
||||||
|
console.timeLog("WebRTC", "loadedData");
|
||||||
|
console.timeEnd("WebRTC");
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
fireEvent(this, "load");
|
fireEvent(this, "load");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user