Skip to content

Commit

Permalink
Add temporary logging to webrtc player (#22213)
Browse files Browse the repository at this point in the history
* add temporary logging to webrtc player

* Update ha-web-rtc-player.ts

* Update ha-web-rtc-player.ts

* Update ha-web-rtc-player.ts
  • Loading branch information
bramkragten authored Oct 3, 2024
1 parent 7d97dbe commit 365b712
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions src/components/ha-web-rtc-player.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import {
css,
CSSResultGroup,
Expand All @@ -6,12 +7,13 @@ import {
PropertyValues,
TemplateResult,
} 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 {
fetchWebRtcClientConfiguration,
handleWebRtcOffer,
WebRtcAnswer,
fetchWebRtcClientConfiguration,
} from "../data/camera";
import type { HomeAssistant } from "../types";
import "./ha-alert";
Expand Down Expand Up @@ -39,12 +41,11 @@ class HaWebRtcPlayer extends LitElement {
@property({ type: Boolean, attribute: "playsinline" })
public playsInline = false;

@property() public posterUrl!: string;
@property({ attribute: "poster-url" }) public posterUrl?: string;

@state() private _error?: string;

// don't cache this, as we remove it on disconnects
@query("#remote-stream") private _videoEl!: HTMLVideoElement;
@query("#remote-stream", true) private _videoEl!: HTMLVideoElement;

private _peerConnection?: RTCPeerConnection;

Expand All @@ -61,7 +62,7 @@ class HaWebRtcPlayer extends LitElement {
.muted=${this.muted}
?playsinline=${this.playsInline}
?controls=${this.controls}
.poster=${this.posterUrl}
poster=${ifDefined(this.posterUrl)}
@loadeddata=${this._loadedData}
></video>
`;
Expand All @@ -83,20 +84,23 @@ class HaWebRtcPlayer extends LitElement {
if (!changedProperties.has("entityid")) {
return;
}
if (!this._videoEl) {
return;
}
this._startWebRtc();
}

private async _startWebRtc(): Promise<void> {
console.time("WebRTC");

this._error = undefined;

console.timeLog("WebRTC", "start clientConfig");

const clientConfig = await fetchWebRtcClientConfiguration(
this.hass,
this.entityid
);

console.timeLog("WebRTC", "end clientConfig", clientConfig);

const peerConnection = new RTCPeerConnection(clientConfig.configuration);

if (clientConfig.dataChannel) {
Expand All @@ -111,30 +115,48 @@ class HaWebRtcPlayer extends LitElement {
offerToReceiveAudio: true,
offerToReceiveVideo: true,
};

console.timeLog("WebRTC", "start createOffer", offerOptions);

const offer: RTCSessionDescriptionInit =
await peerConnection.createOffer(offerOptions);

console.timeLog("WebRTC", "end createOffer", offer);

console.timeLog("WebRTC", "start setLocalDescription");

await peerConnection.setLocalDescription(offer);

console.timeLog("WebRTC", "end setLocalDescription");

console.timeLog("WebRTC", "start iceResolver");

let candidates = ""; // Build an Offer SDP string with ice candidates
const iceResolver = new Promise<void>((resolve) => {
peerConnection.addEventListener("icecandidate", async (event) => {
peerConnection.addEventListener("icecandidate", (event) => {
if (!event.candidate?.candidate) {
resolve(); // Gathering complete
return;
}
console.timeLog("WebRTC", "iceResolver candidate", event.candidate);
candidates += `a=${event.candidate.candidate}\r\n`;
});
});
await iceResolver;

console.timeLog("WebRTC", "end iceResolver", candidates);

const offer_sdp = offer.sdp! + candidates;

let webRtcAnswer: WebRtcAnswer;
try {
console.timeLog("WebRTC", "start WebRTCOffer", offer_sdp);
webRtcAnswer = await handleWebRtcOffer(
this.hass,
this.entityid,
offer_sdp
);
console.timeLog("WebRTC", "end webRtcOffer", webRtcAnswer);
} catch (err: any) {
this._error = "Failed to start WebRTC stream: " + err.message;
peerConnection.close();
Expand All @@ -144,6 +166,7 @@ class HaWebRtcPlayer extends LitElement {
// Setup callbacks to render remote stream once media tracks are discovered.
const remoteStream = new MediaStream();
peerConnection.addEventListener("track", (event) => {
console.timeLog("WebRTC", "track", event);
remoteStream.addTrack(event.track);
this._videoEl.srcObject = remoteStream;
});
Expand All @@ -155,7 +178,9 @@ class HaWebRtcPlayer extends LitElement {
sdp: webRtcAnswer.answer,
});
try {
console.timeLog("WebRTC", "start setRemoteDescription", remoteDesc);
await peerConnection.setRemoteDescription(remoteDesc);
console.timeLog("WebRTC", "end setRemoteDescription");
} catch (err: any) {
this._error = "Failed to connect WebRTC stream: " + err.message;
peerConnection.close();
Expand All @@ -182,6 +207,8 @@ class HaWebRtcPlayer extends LitElement {
}

private _loadedData() {
console.timeLog("WebRTC", "loadedData");
console.timeEnd("WebRTC");
// @ts-ignore
fireEvent(this, "load");
}
Expand Down

0 comments on commit 365b712

Please sign in to comment.