Merge pull request #1150 from dermotduffy/2-way-audio-is-a-property-of-loaded-media

Only show the microphone button if the media that is loaded supports 2-way audio
This commit is contained in:
Dermot Duffy
2023-05-07 17:03:07 -07:00
committed by GitHub
17 changed files with 118 additions and 44 deletions
+9
View File
@@ -406,6 +406,8 @@ See the [fully expanded menu configuration example](#config-expanded-menu) for h
| `button_size` | 40 | :white_check_mark: | The size of the menu buttons in pixels. Must be >= `20`.| | `button_size` | 40 | :white_check_mark: | The size of the menu buttons in pixels. Must be >= `20`.|
| `buttons` | | :white_check_mark: | Whether to show or hide built-in buttons. See below. | | `buttons` | | :white_check_mark: | Whether to show or hide built-in buttons. See below. |
<a name="menu-buttons"></a>
#### Menu Options: Buttons #### Menu Options: Buttons
All configuration is under: All configuration is under:
@@ -433,6 +435,7 @@ menu:
| `expand` | :white_check_mark: | The `expand` menu button: expand the card into a popup/dialog. | | `expand` | :white_check_mark: | The `expand` menu button: expand the card into a popup/dialog. |
| `timeline` | :white_check_mark: | The `timeline` menu button: show the event timeline. | | `timeline` | :white_check_mark: | The `timeline` menu button: show the event timeline. |
| `media_player` | :white_check_mark: | The `media_player` menu button: sends the visible media to a remote media player. Supports Frigate clips, snapshots and live camera (only for cameras that specify a `camera_entity` and only using the default HA stream (equivalent to the `ha` live provider). `jsmpeg` or `webrtc-card` are not supported, although live can still be played as long as `camera_entity` is specified. In the player list, a `tap` will send the media to the player, a `hold` will stop the media on the player. | | `media_player` | :white_check_mark: | The `media_player` menu button: sends the visible media to a remote media player. Supports Frigate clips, snapshots and live camera (only for cameras that specify a `camera_entity` and only using the default HA stream (equivalent to the `ha` live provider). `jsmpeg` or `webrtc-card` are not supported, although live can still be played as long as `camera_entity` is specified. In the player list, a `tap` will send the media to the player, a `hold` will stop the media on the player. |
| `microphone` | :white_check_mark: | The `microphone` button allows usage of 2-way audio in certain configurations. See [Using 2-way audio](#using-2-way-audio). |
##### Configuration on each button ##### Configuration on each button
@@ -3863,6 +3866,12 @@ with `go2rtc` not with the card. In this case, you could file an issue in [that
repo](https://github.com/AlexxIT/go2rtc/issues) with debugging information as repo](https://github.com/AlexxIT/go2rtc/issues) with debugging information as
appropriate. appropriate.
### Microphone menu button does not appear
The microphone menu button will only appear if both enabled (see [Menu Button Options](#menu-buttons))
and if the media that is currently loaded supports 2-way audio. See
[Using 2-way audio](#using-2-way-audio) for more information about the requirements that must be followed.
### Static image URL with credentials doesn't load ### Static image URL with credentials doesn't load
Your browser will not allow a page/script (like this card) to pass credentials to a cross-origin (different host) image URL for security reasons. There is no way around this unless you could also control the webserver that is serving the image to specifically allow `crossorigin` requests (which is typically not the case for an image served from a camera, for example). The stock Home Assistant Picture Glance card has the same limitation, for the same reasons. Your browser will not allow a page/script (like this card) to pass credentials to a cross-origin (different host) image URL for security reasons. There is no way around this unless you could also control the webserver that is serving the image to specifically allow `crossorigin` requests (which is typically not the case for an image served from a camera, for example). The stock Home Assistant Picture Glance card has the same limitation, for the same reasons.
@@ -1081,7 +1081,6 @@ export class FrigateCameraManagerEngine
supportsSnapshots: !isBirdseye, supportsSnapshots: !isBirdseye,
supportsRecordings: !isBirdseye, supportsRecordings: !isBirdseye,
supportsTimeline: !isBirdseye, supportsTimeline: !isBirdseye,
supports2WayAudio: cameraConfig.live_provider === 'go2rtc',
}; };
} }
@@ -182,7 +182,6 @@ export class GenericCameraManagerEngine implements CameraManagerEngine {
supportsRecordings: false, supportsRecordings: false,
supportsSnapshots: false, supportsSnapshots: false,
supportsTimeline: false, supportsTimeline: false,
supports2WayAudio: false,
}; };
} }
-1
View File
@@ -755,7 +755,6 @@ export class CameraManager {
supportsRecordings: perCameraCapabilities.some((cap) => cap?.supportsRecordings), supportsRecordings: perCameraCapabilities.some((cap) => cap?.supportsRecordings),
supportsSnapshots: perCameraCapabilities.some((cap) => cap?.supportsSnapshots), supportsSnapshots: perCameraCapabilities.some((cap) => cap?.supportsSnapshots),
supportsTimeline: perCameraCapabilities.some((cap) => cap?.supportsTimeline), supportsTimeline: perCameraCapabilities.some((cap) => cap?.supportsTimeline),
supports2WayAudio: perCameraCapabilities.some((cap) => cap?.supports2WayAudio),
}; };
} }
} }
-1
View File
@@ -100,7 +100,6 @@ interface BaseCapabilities {
supportsRecordings: boolean; supportsRecordings: boolean;
supportsSnapshots: boolean; supportsSnapshots: boolean;
supportsTimeline: boolean; supportsTimeline: boolean;
supports2WayAudio: boolean;
} }
export type CameraManagerCapabilities = BaseCapabilities; export type CameraManagerCapabilities = BaseCapabilities;
+4 -1
View File
@@ -619,7 +619,10 @@ class FrigateCard extends LitElement {
}); });
} }
if (this._microphoneController && cameraCapabilities?.supports2WayAudio) { if (
this._microphoneController &&
this._currentMediaLoadedInfo?.capabilities?.supports2WayAudio
) {
const muted = this._microphoneController.isMuted(); const muted = this._microphoneController.isMuted();
const buttonType = this._getConfig().menu.buttons.microphone.type; const buttonType = this._getConfig().menu.buttons.microphone.type;
buttons.push({ buttons.push({
+41 -6
View File
@@ -11,27 +11,32 @@ import {
import { customElement, property } from 'lit/decorators.js'; import { customElement, property } from 'lit/decorators.js';
import { live } from 'lit/directives/live.js'; import { live } from 'lit/directives/live.js';
import { createRef, ref, Ref } from 'lit/directives/ref.js'; import { createRef, ref, Ref } from 'lit/directives/ref.js';
import isEqual from 'lodash-es/isEqual';
import { CachedValueController } from '../cached-value-controller.js'; import { CachedValueController } from '../cached-value-controller.js';
import defaultImage from '../images/frigate-bird-in-sky.jpg'; import defaultImage from '../images/frigate-bird-in-sky.jpg';
import { localize } from '../localize/localize.js'; import { localize } from '../localize/localize.js';
import imageStyle from '../scss/image.scss'; import imageStyle from '../scss/image.scss';
import { CameraConfig, ImageViewConfig, MediaLoadedInfo } from '../types.js'; import {
CameraConfig,
FrigateCardMediaPlayer,
ImageViewConfig,
MediaLoadedInfo,
} from '../types.js';
import { contentsChanged } from '../utils/basic.js';
import { isHassDifferent } from '../utils/ha'; import { isHassDifferent } from '../utils/ha';
import { updateElementStyleFromMediaLayoutConfig } from '../utils/media-layout.js';
import { import {
createMediaLoadedInfo, createMediaLoadedInfo,
dispatchExistingMediaLoadedInfoAsEvent, dispatchExistingMediaLoadedInfoAsEvent,
} from '../utils/media-info.js'; } from '../utils/media-info.js';
import { updateElementStyleFromMediaLayoutConfig } from '../utils/media-layout.js';
import { View } from '../view/view.js'; import { View } from '../view/view.js';
import { dispatchErrorMessageEvent } from './message.js'; import { dispatchErrorMessageEvent } from './message.js';
import { contentsChanged } from '../utils/basic.js';
import isEqual from 'lodash-es/isEqual';
// See TOKEN_CHANGE_INTERVAL in https://github.com/home-assistant/core/blob/dev/homeassistant/components/camera/__init__.py . // See TOKEN_CHANGE_INTERVAL in https://github.com/home-assistant/core/blob/dev/homeassistant/components/camera/__init__.py .
const HASS_REJECTION_CUTOFF_MS = 5 * 60 * 1000; const HASS_REJECTION_CUTOFF_MS = 5 * 60 * 1000;
@customElement('frigate-card-image') @customElement('frigate-card-image')
export class FrigateCardImage extends LitElement { export class FrigateCardImage extends LitElement implements FrigateCardMediaPlayer {
@property({ attribute: false }) @property({ attribute: false })
public hass?: HomeAssistant; public hass?: HomeAssistant;
@@ -54,6 +59,36 @@ export class FrigateCardImage extends LitElement {
protected _mediaLoadedInfo: MediaLoadedInfo | null = null; protected _mediaLoadedInfo: MediaLoadedInfo | null = null;
public async play(): Promise<void> {
this._cachedValueController?.startTimer();
}
public async pause(): Promise<void> {
this._cachedValueController?.stopTimer();
}
public async mute(): Promise<void> {
// Not implemented.
}
public async unmute(): Promise<void> {
// Not implemented.
}
public isMuted(): boolean {
return true;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async seek(_seconds: number): Promise<void> {
// Not implemented.
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async setControls(_controls: boolean): Promise<void> {
// Not implemented.
}
/** /**
* Get the camera entity for the current camera configuration. * Get the camera entity for the current camera configuration.
* @returns The entity or undefined if no camera entity is available. * @returns The entity or undefined if no camera entity is available.
@@ -250,7 +285,7 @@ export class FrigateCardImage extends LitElement {
${ref(this._refImage)} ${ref(this._refImage)}
src=${live(src)} src=${live(src)}
@load=${(ev: Event) => { @load=${(ev: Event) => {
const mediaLoadedInfo = createMediaLoadedInfo(ev); const mediaLoadedInfo = createMediaLoadedInfo(ev, { player: this });
// Avoid the media being reported as repeatedly loading unless the // Avoid the media being reported as repeatedly loading unless the
// media info changes. // media info changes.
if (mediaLoadedInfo && !isEqual(this._mediaLoadedInfo, mediaLoadedInfo)) { if (mediaLoadedInfo && !isEqual(this._mediaLoadedInfo, mediaLoadedInfo)) {
+14 -3
View File
@@ -36,9 +36,11 @@ const GO2RTC_URL_SIGN_EXPIRY_SECONDS = 24 * 60 * 60;
@customElement('frigate-card-live-go2rtc-player') @customElement('frigate-card-live-go2rtc-player')
class FrigateCardGo2RTCPlayer extends VideoRTC { class FrigateCardGo2RTCPlayer extends VideoRTC {
protected _microphoneStream?: MediaStream; protected _microphoneStream?: MediaStream;
protected _containingPlayer?: FrigateCardMediaPlayer;
constructor(microphoneStream?: MediaStream) { constructor(containingPlayer: FrigateCardMediaPlayer, microphoneStream?: MediaStream) {
super(); super();
this._containingPlayer = containingPlayer;
if (microphoneStream) { if (microphoneStream) {
this._microphoneStream = microphoneStream; this._microphoneStream = microphoneStream;
} }
@@ -79,7 +81,16 @@ class FrigateCardGo2RTCPlayer extends VideoRTC {
onloadeddata.call(this.video, e); onloadeddata.call(this.video, e);
} }
hideMediaControlsTemporarily(this.video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS); hideMediaControlsTemporarily(this.video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS);
dispatchMediaLoadedEvent(this, this.video); dispatchMediaLoadedEvent(this, this.video, {
player: this._containingPlayer,
capabilities: {
// 2-way audio is only supported on WebRTC connections. The state of
// `this._microphoneStream` is not taken into account here since
// that can be created after the fact -- this is purely saying that
// were a microphone stream available it could be used usefully.
supports2WayAudio: !!this.pc,
},
});
}; };
// Always started muted. Media may be unmuted in accordance with user // Always started muted. Media may be unmuted in accordance with user
@@ -256,7 +267,7 @@ export class FrigateCardGo2RTC extends LitElement implements FrigateCardMediaPla
return; return;
} }
this._player = new FrigateCardGo2RTCPlayer(this.microphoneStream); this._player = new FrigateCardGo2RTCPlayer(this, this.microphoneStream);
this._player.src = address; this._player.src = address;
this._player.visibilityCheck = false; this._player.visibilityCheck = false;
+16 -20
View File
@@ -1,10 +1,11 @@
import { HomeAssistant } from 'custom-card-helpers'; import { HomeAssistant } from 'custom-card-helpers';
import { CSSResultGroup, html, LitElement, TemplateResult, unsafeCSS } from 'lit'; import { CSSResultGroup, html, LitElement, TemplateResult, unsafeCSS } from 'lit';
import { customElement, property, state } from 'lit/decorators.js'; import { customElement, property } from 'lit/decorators.js';
import { createRef, ref, Ref } from 'lit/directives/ref.js';
import liveImageStyle from '../../scss/live-image.scss'; import liveImageStyle from '../../scss/live-image.scss';
import { CameraConfig, FrigateCardMediaPlayer } from '../../types.js'; import { CameraConfig, FrigateCardMediaPlayer } from '../../types.js';
import { getStateObjOrDispatchError } from './live.js';
import '../image.js'; import '../image.js';
import { getStateObjOrDispatchError } from './live.js';
@customElement('frigate-card-live-image') @customElement('frigate-card-live-image')
export class FrigateCardLiveImage extends LitElement implements FrigateCardMediaPlayer { export class FrigateCardLiveImage extends LitElement implements FrigateCardMediaPlayer {
@@ -14,37 +15,34 @@ export class FrigateCardLiveImage extends LitElement implements FrigateCardMedia
@property({ attribute: false }) @property({ attribute: false })
public cameraConfig?: CameraConfig; public cameraConfig?: CameraConfig;
@state() protected _refImage: Ref<Element & FrigateCardMediaPlayer> = createRef();
protected _playing = true;
public async play(): Promise<void> { public async play(): Promise<void> {
this._playing = true; await this._refImage.value?.play();
} }
public async pause(): Promise<void> { public async pause(): Promise<void> {
this._playing = false; await this._refImage.value?.pause();
} }
public async mute(): Promise<void> { public async mute(): Promise<void> {
// Not implemented. await this._refImage.value?.mute();
} }
public async unmute(): Promise<void> { public async unmute(): Promise<void> {
// Not implemented. await this._refImage.value?.unmute();
} }
public isMuted(): boolean { public isMuted(): boolean {
return true; return !!this._refImage.value?.isMuted();
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars public async seek(seconds: number): Promise<void> {
public async seek(_seconds: number): Promise<void> { await this._refImage.value?.seek(seconds);
// Not implemented.
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars public async setControls(controls: boolean): Promise<void> {
public async setControls(_controls: boolean): Promise<void> { await this._refImage.value?.setControls(controls);
// Not implemented.
} }
protected render(): TemplateResult | void { protected render(): TemplateResult | void {
@@ -55,16 +53,14 @@ export class FrigateCardLiveImage extends LitElement implements FrigateCardMedia
getStateObjOrDispatchError(this, this.hass, this.cameraConfig); getStateObjOrDispatchError(this, this.hass, this.cameraConfig);
return html` <frigate-card-image return html` <frigate-card-image
${ref(this._refImage)}
.imageConfig=${{ .imageConfig=${{
mode: this.cameraConfig.image.url ? ('url' as const) : ('camera' as const), mode: this.cameraConfig.image.url ? ('url' as const) : ('camera' as const),
refresh_seconds: this._playing ? this.cameraConfig.image.refresh_seconds : 0, refresh_seconds: this.cameraConfig.image.refresh_seconds,
url: this.cameraConfig.image.url, url: this.cameraConfig.image.url,
// The live provider will take care of zoom. // The live provider will take care of zoom and layout options.
zoomable: false, zoomable: false,
// Don't need to pass layout options as FrigateCardLiveProvider has
// already taken care of this for us.
}} }}
.hass=${this.hass} .hass=${this.hass}
.cameraConfig=${this.cameraConfig} .cameraConfig=${this.cameraConfig}
+3 -1
View File
@@ -111,7 +111,9 @@ export class FrigateCardLiveJSMPEG extends LitElement implements FrigateCardMedi
// ignore any subsequent calls. // ignore any subsequent calls.
if (!videoDecoded && this._jsmpegCanvasElement) { if (!videoDecoded && this._jsmpegCanvasElement) {
videoDecoded = true; videoDecoded = true;
dispatchMediaLoadedEvent(this, this._jsmpegCanvasElement); dispatchMediaLoadedEvent(this, this._jsmpegCanvasElement, {
player: this,
});
resolve(player); resolve(player);
} }
}, },
+1 -1
View File
@@ -185,7 +185,7 @@ export class FrigateCardLiveWebRTCCard
onloadeddata.call(video, e); onloadeddata.call(video, e);
} }
hideMediaControlsTemporarily(video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS); hideMediaControlsTemporarily(video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS);
dispatchMediaLoadedEvent(this, video); dispatchMediaLoadedEvent(this, video, { player: this });
}; };
} }
}); });
+3 -3
View File
@@ -750,7 +750,7 @@ export class FrigateCardViewerProvider
} }
}} }}
@loadeddata=${(ev: Event) => { @loadeddata=${(ev: Event) => {
dispatchMediaLoadedEvent(this, ev); dispatchMediaLoadedEvent(this, ev, { player: this });
}} }}
> >
<source <source
@@ -768,8 +768,8 @@ export class FrigateCardViewerProvider
this._dispatchRelatedClipView(); this._dispatchRelatedClipView();
} }
}} }}
@load=${(e: Event) => { @load=${(ev: Event) => {
dispatchMediaLoadedEvent(this, e); dispatchMediaLoadedEvent(this, ev, { player: this });
}} }}
/>`} />`}
`); `);
+1 -1
View File
@@ -90,7 +90,7 @@ customElements.whenDefined('ha-camera-stream').then(() => {
return html` return html`
<img <img
@load=${(ev: Event) => { @load=${(ev: Event) => {
dispatchMediaLoadedEvent(this, ev); dispatchMediaLoadedEvent(this, ev, { player: this });
}} }}
.src=${typeof this._connected == 'undefined' || this._connected .src=${typeof this._connected == 'undefined' || this._connected
? computeMJPEGStreamUrl(this.stateObj) ? computeMJPEGStreamUrl(this.stateObj)
+1 -1
View File
@@ -97,7 +97,7 @@ customElements.whenDefined('ha-hls-player').then(() => {
hideMediaControlsTemporarily(this._video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS); hideMediaControlsTemporarily(this._video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS);
}} }}
@loadeddata=${(e) => { @loadeddata=${(e) => {
dispatchMediaLoadedEvent(this, e); dispatchMediaLoadedEvent(this, e, { player: this });
}} }}
></video> ></video>
`; `;
+1 -1
View File
@@ -93,7 +93,7 @@ customElements.whenDefined('ha-web-rtc-player').then(() => {
hideMediaControlsTemporarily(this._video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS); hideMediaControlsTemporarily(this._video, MEDIA_LOAD_CONTROLS_HIDE_SECONDS);
}} }}
@loadeddata=${(e) => { @loadeddata=${(e) => {
dispatchMediaLoadedEvent(this, e); dispatchMediaLoadedEvent(this, e, { player: this });
}} }}
></video> ></video>
`; `;
+6
View File
@@ -1471,9 +1471,15 @@ export interface ExtendedHomeAssistant extends HomeAssistant {
}; };
} }
export interface MediaLoadedCapabilities {
supports2WayAudio?: boolean;
}
export interface MediaLoadedInfo { export interface MediaLoadedInfo {
width: number; width: number;
height: number; height: number;
player?: FrigateCardMediaPlayer;
capabilities?: MediaLoadedCapabilities;
} }
export const MESSAGE_TYPE_PRIORITIES = { export const MESSAGE_TYPE_PRIORITIES = {
+18 -2
View File
@@ -1,4 +1,8 @@
import { MediaLoadedInfo } from '../types.js'; import {
FrigateCardMediaPlayer,
MediaLoadedCapabilities,
MediaLoadedInfo,
} from '../types.js';
import { dispatchFrigateCardEvent } from './basic.js'; import { dispatchFrigateCardEvent } from './basic.js';
const MEDIA_INFO_HEIGHT_CUTOFF = 50; const MEDIA_INFO_HEIGHT_CUTOFF = 50;
@@ -11,6 +15,10 @@ const MEDIA_INFO_WIDTH_CUTOFF = MEDIA_INFO_HEIGHT_CUTOFF;
*/ */
export function createMediaLoadedInfo( export function createMediaLoadedInfo(
source: Event | HTMLElement, source: Event | HTMLElement,
options?: {
player?: FrigateCardMediaPlayer;
capabilities?: MediaLoadedCapabilities;
},
): MediaLoadedInfo | null { ): MediaLoadedInfo | null {
let target: HTMLElement | EventTarget; let target: HTMLElement | EventTarget;
if (source instanceof Event) { if (source instanceof Event) {
@@ -23,16 +31,20 @@ export function createMediaLoadedInfo(
return { return {
width: (target as HTMLImageElement).naturalWidth, width: (target as HTMLImageElement).naturalWidth,
height: (target as HTMLImageElement).naturalHeight, height: (target as HTMLImageElement).naturalHeight,
...options,
}; };
} else if (target instanceof HTMLVideoElement) { } else if (target instanceof HTMLVideoElement) {
return { return {
width: (target as HTMLVideoElement).videoWidth, width: (target as HTMLVideoElement).videoWidth,
height: (target as HTMLVideoElement).videoHeight, height: (target as HTMLVideoElement).videoHeight,
...options,
}; };
} else if (target instanceof HTMLCanvasElement) { } else if (target instanceof HTMLCanvasElement) {
return { return {
width: (target as HTMLCanvasElement).width, width: (target as HTMLCanvasElement).width,
height: (target as HTMLCanvasElement).height, height: (target as HTMLCanvasElement).height,
player: options?.player,
...options,
}; };
} }
return null; return null;
@@ -46,8 +58,12 @@ export function createMediaLoadedInfo(
export function dispatchMediaLoadedEvent( export function dispatchMediaLoadedEvent(
target: HTMLElement, target: HTMLElement,
source: Event | HTMLElement, source: Event | HTMLElement,
options?: {
player?: FrigateCardMediaPlayer;
capabilities?: MediaLoadedCapabilities;
},
): void { ): void {
const mediaLoadedInfo = createMediaLoadedInfo(source); const mediaLoadedInfo = createMediaLoadedInfo(source, options);
if (mediaLoadedInfo) { if (mediaLoadedInfo) {
dispatchExistingMediaLoadedInfoAsEvent(target, mediaLoadedInfo); dispatchExistingMediaLoadedInfoAsEvent(target, mediaLoadedInfo);
} }