From 3d05b80ce16484399fa0612fe631e7b14baca41c Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Tue, 5 Nov 2024 09:28:06 +0000 Subject: [PATCH 1/4] Add sdp m line index to ICE Candidates --- src/components/ha-web-rtc-player.ts | 21 +++++++++++++++------ src/data/camera.ts | 7 +++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/components/ha-web-rtc-player.ts b/src/components/ha-web-rtc-player.ts index b2467e28045b..809867738311 100644 --- a/src/components/ha-web-rtc-player.ts +++ b/src/components/ha-web-rtc-player.ts @@ -55,7 +55,7 @@ class HaWebRtcPlayer extends LitElement { private _sessionId?: string; - private _candidatesList: string[] = []; + private _candidatesList: RTCIceCandidate[] = []; protected override render(): TemplateResult { if (this._error) { @@ -255,11 +255,13 @@ class HaWebRtcPlayer extends LitElement { if (event.type === "session") { this._sessionId = event.session_id; this._candidatesList.forEach((candidate) => + // sdpMLineIndex is always populated addWebRtcCandidate( this.hass, this.entityid!, event.session_id, - candidate + candidate.candidate, + candidate?.sdpMLineIndex || 0 ) ); this._candidatesList = []; @@ -274,7 +276,10 @@ class HaWebRtcPlayer extends LitElement { try { await this._peerConnection?.addIceCandidate( - new RTCIceCandidate({ candidate: event.candidate, sdpMid: "0" }) + new RTCIceCandidate({ + candidate: event.candidate, + sdpMLineIndex: event.sdp_m_line_index, + }) ); } catch (err: any) { console.error(err); @@ -294,18 +299,22 @@ class HaWebRtcPlayer extends LitElement { console.timeLog( "WebRTC", "local ice candidate", - event.candidate?.candidate + event.candidate?.candidate, + event.candidate?.sdpMLineIndex ); if (this._sessionId) { + // sdpMLineIndex is always populated + const sdpMLineIndex = event.candidate?.sdpMLineIndex || 0; addWebRtcCandidate( this.hass, this.entityid, this._sessionId, - event.candidate?.candidate + event.candidate?.candidate, + sdpMLineIndex ); } else { - this._candidatesList.push(event.candidate?.candidate); + this._candidatesList.push(event.candidate); } }; diff --git a/src/data/camera.ts b/src/data/camera.ts index 8c3bfe537f1d..29cb6f4f6415 100644 --- a/src/data/camera.ts +++ b/src/data/camera.ts @@ -60,6 +60,7 @@ export interface WebRtcAnswer { export interface WebRtcCandidate { type: "candidate"; candidate: string; + sdp_m_line_index: number; } export interface WebRtcError { @@ -139,13 +140,15 @@ export const addWebRtcCandidate = ( hass: HomeAssistant, entity_id: string, session_id: string, - candidate: string + candidate: string, + sdpMLineIndex: number ) => hass.callWS({ type: "camera/webrtc/candidate", entity_id, session_id, - candidate, + candidate: candidate, + sdp_m_line_index: sdpMLineIndex, }); export const fetchCameraPrefs = (hass: HomeAssistant, entityId: string) => From 9b69d2e9c314b40f4ed4dc3d702212a5ef33ef8d Mon Sep 17 00:00:00 2001 From: "Steven B." <51370195+sdb9696@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:26:38 +0000 Subject: [PATCH 2/4] Remove ? from candidate Co-authored-by: Bram Kragten --- src/components/ha-web-rtc-player.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ha-web-rtc-player.ts b/src/components/ha-web-rtc-player.ts index da6205a5761b..17a4f0bc9426 100644 --- a/src/components/ha-web-rtc-player.ts +++ b/src/components/ha-web-rtc-player.ts @@ -254,7 +254,7 @@ class HaWebRtcPlayer extends LitElement { this.entityid!, event.session_id, candidate.candidate, - candidate?.sdpMLineIndex || 0 + candidate.sdpMLineIndex || 0 ) ); this._candidatesList = []; From bb727bd6677727a5ca6487412e6ddf1e6af4b1b1 Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Wed, 6 Nov 2024 13:08:44 +0000 Subject: [PATCH 3/4] Send full candidate in message --- .vscode/launch.json | 6 +++--- src/components/ha-web-rtc-player.ts | 14 +++----------- src/data/camera.ts | 7 ++----- 3 files changed, 8 insertions(+), 19 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9b3f7b48adf7..105d62bf4cfc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,10 +6,10 @@ "request": "launch", "type": "pwa-chrome", "url": "http://localhost:8123/", - "webRoot": "${workspaceFolder}/hass_frontend", + //"webRoot": "${workspaceFolder}/hass_frontend", "disableNetworkCache": true, - "preLaunchTask": "Develop Frontend", - "outFiles": ["${workspaceFolder}/hass_frontend/frontend_latest/*.js"] + "preLaunchTask": "Develop Frontend" + //"outFiles": ["${workspaceFolder}/hass_frontend/frontend_latest/*.js"] }, { "name": "Debug Gallery", diff --git a/src/components/ha-web-rtc-player.ts b/src/components/ha-web-rtc-player.ts index 17a4f0bc9426..a15673ccea09 100644 --- a/src/components/ha-web-rtc-player.ts +++ b/src/components/ha-web-rtc-player.ts @@ -248,13 +248,11 @@ class HaWebRtcPlayer extends LitElement { if (event.type === "session") { this._sessionId = event.session_id; this._candidatesList.forEach((candidate) => - // sdpMLineIndex is always populated addWebRtcCandidate( this.hass, this.entityid!, event.session_id, - candidate.candidate, - candidate.sdpMLineIndex || 0 + candidate ) ); this._candidatesList = []; @@ -269,10 +267,7 @@ class HaWebRtcPlayer extends LitElement { try { await this._peerConnection?.addIceCandidate( - new RTCIceCandidate({ - candidate: event.candidate, - sdpMLineIndex: event.sdp_m_line_index, - }) + new RTCIceCandidate(event.candidate) ); } catch (err: any) { // eslint-disable-next-line no-console @@ -297,14 +292,11 @@ class HaWebRtcPlayer extends LitElement { ); if (this._sessionId) { - // sdpMLineIndex is always populated - const sdpMLineIndex = event.candidate?.sdpMLineIndex || 0; addWebRtcCandidate( this.hass, this.entityid, this._sessionId, - event.candidate?.candidate, - sdpMLineIndex + event.candidate ); } else { this._candidatesList.push(event.candidate); diff --git a/src/data/camera.ts b/src/data/camera.ts index 29cb6f4f6415..6420293bd899 100644 --- a/src/data/camera.ts +++ b/src/data/camera.ts @@ -59,8 +59,7 @@ export interface WebRtcAnswer { export interface WebRtcCandidate { type: "candidate"; - candidate: string; - sdp_m_line_index: number; + candidate: RTCIceCandidate; } export interface WebRtcError { @@ -140,15 +139,13 @@ export const addWebRtcCandidate = ( hass: HomeAssistant, entity_id: string, session_id: string, - candidate: string, - sdpMLineIndex: number + candidate: RTCIceCandidate ) => hass.callWS({ type: "camera/webrtc/candidate", entity_id, session_id, candidate: candidate, - sdp_m_line_index: sdpMLineIndex, }); export const fetchCameraPrefs = (hass: HomeAssistant, entityId: string) => From 2692edcdf86ecf107e581d2cc19e578042046c60 Mon Sep 17 00:00:00 2001 From: Steven B <51370195+sdb9696@users.noreply.github.com> Date: Wed, 6 Nov 2024 13:19:01 +0000 Subject: [PATCH 4/4] Revert launch.json change --- .vscode/launch.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 105d62bf4cfc..9b3f7b48adf7 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,10 +6,10 @@ "request": "launch", "type": "pwa-chrome", "url": "http://localhost:8123/", - //"webRoot": "${workspaceFolder}/hass_frontend", + "webRoot": "${workspaceFolder}/hass_frontend", "disableNetworkCache": true, - "preLaunchTask": "Develop Frontend" - //"outFiles": ["${workspaceFolder}/hass_frontend/frontend_latest/*.js"] + "preLaunchTask": "Develop Frontend", + "outFiles": ["${workspaceFolder}/hass_frontend/frontend_latest/*.js"] }, { "name": "Debug Gallery",