Add support for the new HA native WebRTC.

This commit is contained in:
Dermot Duffy
2022-02-06 21:30:27 -08:00
parent 474ccec22e
commit 1ee4f07714
3 changed files with 121 additions and 30 deletions
+1
View File
@@ -65,6 +65,7 @@ import './components/viewer.js';
import './components/thumbnail-carousel.js'; import './components/thumbnail-carousel.js';
import './patches/ha-camera-stream.js'; import './patches/ha-camera-stream.js';
import './patches/ha-hls-player.js'; import './patches/ha-hls-player.js';
import './patches/ha-web-rtc-player.ts';
import cardStyle from './scss/card.scss'; import cardStyle from './scss/card.scss';
import { ResolvedMediaCache } from './resolved-media.js'; import { ResolvedMediaCache } from './resolved-media.js';
+23 -11
View File
@@ -32,6 +32,9 @@ customElements.whenDefined('ha-camera-stream').then(() => {
? computeObjectId(stateObj.entity_id).replace(/_/g, ' ') ? computeObjectId(stateObj.entity_id).replace(/_/g, ' ')
: stateObj.attributes.friendly_name || ''; : stateObj.attributes.friendly_name || '';
const STREAM_TYPE_HLS = 'hls';
const STREAM_TYPE_WEB_RTC = 'web_rtc';
@customElement('frigate-card-ha-camera-stream') @customElement('frigate-card-ha-camera-stream')
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
class FrigateCardHaCameraStream extends customElements.get('ha-camera-stream') { class FrigateCardHaCameraStream extends customElements.get('ha-camera-stream') {
@@ -79,9 +82,8 @@ customElements.whenDefined('ha-camera-stream').then(() => {
return html``; return html``;
} }
if (this._shouldRenderMJPEG) {
return html` return html`
${this._shouldRenderMJPEG
? html`
<img <img
@load=${(e) => { @load=${(e) => {
dispatchMediaShowEvent(this, e); dispatchMediaShowEvent(this, e);
@@ -91,10 +93,11 @@ customElements.whenDefined('ha-camera-stream').then(() => {
: ''} : ''}
.alt=${`Preview of the ${computeStateName(this.stateObj)} camera.`} .alt=${`Preview of the ${computeStateName(this.stateObj)} camera.`}
/> />
` `;
: this._url }
? html` if (this.stateObj.attributes.frontend_stream_type === STREAM_TYPE_HLS) {
<frigate-card-ha-hls-player return this._url
? html` <frigate-card-ha-hls-player
${ref(this._playerRef)} ${ref(this._playerRef)}
playsinline playsinline
.allowExoPlayer=${this.allowExoPlayer} .allowExoPlayer=${this.allowExoPlayer}
@@ -102,10 +105,18 @@ customElements.whenDefined('ha-camera-stream').then(() => {
.controls=${this.controls} .controls=${this.controls}
.hass=${this.hass} .hass=${this.hass}
.url=${this._url} .url=${this._url}
></frigate-card-ha-hls-player> ></frigate-card-ha-hls-player>`
` : html``;
: ''} }
`; if (this.stateObj.attributes.frontend_stream_type === STREAM_TYPE_WEB_RTC) {
return html`<frigate-card-ha-web-rtc-player
playsinline
.muted=${this.muted}
.controls=${this.controls}
.hass=${this.hass}
.entityid=${this.stateObj.entity_id}
></frigate-card-ha-web-rtc-player>`;
}
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {
@@ -115,7 +126,8 @@ customElements.whenDefined('ha-camera-stream').then(() => {
:host { :host {
width: 100%; width: 100%;
height: 100%; height: 100%;
}` }
`,
]; ];
} }
} }
+78
View File
@@ -0,0 +1,78 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
// ====================================================================
// ** Keep modifications to this file to a minimum **
//
// Type checking is disabled since this is a modified copy-and-paste of
// underlying render() function, but the rest of the class source it not
// available as compilation time.
// ====================================================================
import { Ref, createRef, ref } from 'lit/directives/ref';
import { TemplateResult, html } from 'lit';
import { customElement } from 'lit/decorators.js';
import { dispatchErrorMessageEvent, dispatchMediaShowEvent } from '../common.js';
customElements.whenDefined('ha-web-rtc-player').then(() => {
@customElement('frigate-card-ha-web-rtc-player')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class FrigateCardHaWebRtcPlayer extends customElements.get('ha-web-rtc-player') {
protected _videoRef: Ref<HTMLVideoElement> = createRef();
/**
* Play the video.
*/
public play(): void {
this._videoRef.value?.play();
}
/**
* Pause the video.
*/
public pause(): void {
this._videoRef.value?.pause();
}
/**
* Mute the video.
*/
public mute(): void {
this.muted = true;
}
/**
* Unmute the video.
*/
public unmute(): void {
this.muted = false;
}
// =====================================================================================
// Minor modifications from:
// - https://github.com/home-assistant/frontend/blob/dev/src/components/ha-web-rtc-player.ts
// =====================================================================================
protected render(): TemplateResult | void {
if (this._error) {
// Use native Frigate card error handling, and attach the entityid to
// clarify which camera the error refers to.
return dispatchErrorMessageEvent(
this,
`${this._error} (${this.entityid})`);
}
return html`
<video
${ref(this._videoRef)}
id="remote-stream"
?autoplay=${this.autoPlay}
.muted=${this.muted}
?playsinline=${this.playsInline}
?controls=${this.controls}
@loadeddata=${(e) => {
dispatchMediaShowEvent(this, e);
}}
></video>
`;
}
}
});