Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable two way audio with webrtc #22239

Draft
wants to merge 7 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 88 additions & 14 deletions src/components/ha-web-rtc-player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import {
css,
CSSResultGroup,
html,
nothing,
LitElement,
PropertyValues,
TemplateResult,
} from "lit";
import { customElement, property, query, state } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import { mdiMicrophone, mdiMicrophoneOff } from "@mdi/js";
import { fireEvent } from "../common/dom/fire_event";
import {
fetchWebRtcClientConfiguration,
Expand Down Expand Up @@ -51,21 +52,48 @@ class HaWebRtcPlayer extends LitElement {

private _remoteStream?: MediaStream;

private _localReturnAudioTrack?: MediaStreamTrack;

public toggleMic() {
this._localReturnAudioTrack!.enabled =
!this._localReturnAudioTrack!.enabled;
this.requestUpdate();
}

protected override render(): TemplateResult {
if (this._error) {
return html`<ha-alert alert-type="error">${this._error}</ha-alert>`;
}
return html`
<video
id="remote-stream"
?autoplay=${this.autoPlay}
.muted=${this.muted}
?playsinline=${this.playsInline}
?controls=${this.controls}
poster=${ifDefined(this.posterUrl)}
@loadeddata=${this._loadedData}
></video>
`;
const controls =
this._localReturnAudioTrack === undefined ? this.controls : false;
// const controls = true;
sdb9696 marked this conversation as resolved.
Show resolved Hide resolved

const video_html = html` <video
id="remote-stream"
?autoplay=${this.autoPlay}
.muted=${this.muted}
?playsinline=${this.playsInline}
?controls=${controls}
.poster=${this.posterUrl}
@loadeddata=${this._loadedData}
></video>`;
const mic_html = !this._localReturnAudioTrack
? nothing
: html`
<div class="video-controls">
<mwc-button @click=${this.toggleMic} halign id="toggle_mic">
<ha-svg-icon
.path=${this._localReturnAudioTrack!.enabled
? mdiMicrophone
: mdiMicrophoneOff}
></ha-svg-icon>
${this._localReturnAudioTrack!.enabled
? "Turn off mic"
: "Turn on mic"}
</mwc-button>
</div>
`;
return html` <div class="video-container">${video_html}${mic_html}</div> `;
}

public override connectedCallback() {
Expand Down Expand Up @@ -108,7 +136,24 @@ class HaWebRtcPlayer extends LitElement {
// however, not used by any integrations.
peerConnection.createDataChannel(clientConfig.dataChannel);
}
peerConnection.addTransceiver("audio", { direction: "recvonly" });
// If the stream supports two way audio and the client is configured to allow it
// N.B. connections must be secure for microphone access.
if (clientConfig.audio_direction === "sendrecv" && navigator.mediaDevices) {
const tracks = await this.getMediaTracks("user", {
video: false,
audio: true,
});
if (tracks && tracks.length > 0) {
this._localReturnAudioTrack = tracks[0];
// Start with mic off
this._localReturnAudioTrack.enabled = false;
}
peerConnection.addTransceiver(this._localReturnAudioTrack!, {
direction: "sendrecv",
});
} else {
peerConnection.addTransceiver("audio", { direction: "recvonly" });
}
peerConnection.addTransceiver("video", { direction: "recvonly" });

const offerOptions: RTCOfferOptions = {
Expand Down Expand Up @@ -189,13 +234,30 @@ class HaWebRtcPlayer extends LitElement {
this._peerConnection = peerConnection;
}

private _cleanUp() {
private async getMediaTracks(media, constraints) {
try {
const stream =
media === "user"
? await navigator.mediaDevices.getUserMedia(constraints)
: await navigator.mediaDevices.getDisplayMedia(constraints);
return stream.getTracks();
} catch (err: any) {
// eslint-disable-next-line no-console
sdb9696 marked this conversation as resolved.
Show resolved Hide resolved
this._error = "Failed to get media tracks: " + err.message;
return [];
}
}

private async _cleanUp() {
if (this._remoteStream) {
this._remoteStream.getTracks().forEach((track) => {
track.stop();
});
this._remoteStream = undefined;
}
if (this._localReturnAudioTrack) {
this._localReturnAudioTrack.stop();
}
if (this._videoEl) {
this._videoEl.removeAttribute("src");
this._videoEl.load();
Expand Down Expand Up @@ -224,6 +286,18 @@ class HaWebRtcPlayer extends LitElement {
width: 100%;
max-height: var(--video-max-height, calc(100vh - 97px));
}

.video-controls {
width: 100%;
position: absolute;
bottom: 0;
left: 0;
z-index: 10;
}

mwc-button {
--mdc-theme-primary: white;
}
`;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/data/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export const getEntityIdFromCameraMediaSource = (mediaContentId: string) =>
export interface WebRTCClientConfiguration {
configuration: RTCConfiguration;
dataChannel?: string;
audio_direction: string;
sdb9696 marked this conversation as resolved.
Show resolved Hide resolved
}

export const fetchWebRtcClientConfiguration = async (
Expand Down
Loading