Handle visibility changes in camera players (#26235)

* Handle visibility changes in webrtc player

* Implement visibility handling for hls

* Remove console logs
This commit is contained in:
Abílio Costa 2025-07-23 18:31:39 +01:00 committed by GitHub
parent aae1a3604c
commit 3e2f5b0dd3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 0 deletions

View File

@ -59,6 +59,15 @@ class HaHLSPlayer extends LitElement {
private static streamCount = 0; private static streamCount = 0;
private _handleVisibilityChange = () => {
if (document.hidden) {
this._cleanUp();
} else {
this._resetError();
this._startHls();
}
};
public connectedCallback() { public connectedCallback() {
super.connectedCallback(); super.connectedCallback();
HaHLSPlayer.streamCount += 1; HaHLSPlayer.streamCount += 1;
@ -66,10 +75,15 @@ class HaHLSPlayer extends LitElement {
this._resetError(); this._resetError();
this._startHls(); this._startHls();
} }
document.addEventListener("visibilitychange", this._handleVisibilityChange);
} }
public disconnectedCallback() { public disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
document.removeEventListener(
"visibilitychange",
this._handleVisibilityChange
);
HaHLSPlayer.streamCount -= 1; HaHLSPlayer.streamCount -= 1;
this._cleanUp(); this._cleanUp();
} }

View File

@ -61,6 +61,14 @@ class HaWebRtcPlayer extends LitElement {
private _candidatesList: RTCIceCandidate[] = []; private _candidatesList: RTCIceCandidate[] = [];
private _handleVisibilityChange = () => {
if (document.hidden) {
this._cleanUp();
} else {
this._startWebRtc();
}
};
protected override render(): TemplateResult { protected override render(): TemplateResult {
if (this._error) { if (this._error) {
return html`<ha-alert alert-type="error">${this._error}</ha-alert>`; return html`<ha-alert alert-type="error">${this._error}</ha-alert>`;
@ -88,10 +96,15 @@ class HaWebRtcPlayer extends LitElement {
if (this.hasUpdated && this.entityid) { if (this.hasUpdated && this.entityid) {
this._startWebRtc(); this._startWebRtc();
} }
document.addEventListener("visibilitychange", this._handleVisibilityChange);
} }
public override disconnectedCallback() { public override disconnectedCallback() {
super.disconnectedCallback(); super.disconnectedCallback();
document.removeEventListener(
"visibilitychange",
this._handleVisibilityChange
);
this._cleanUp(); this._cleanUp();
} }