@@ -59,8 +59,7 @@ import {MatrixClientPeg} from './MatrixClientPeg';
5959import PlatformPeg from './PlatformPeg' ;
6060import Modal from './Modal' ;
6161import { _t } from './languageHandler' ;
62- // @ts -ignore - XXX: tsc doesn't like this: our js-sdk imports are complex so this isn't surprising
63- import Matrix from 'matrix-js-sdk' ;
62+ import { createNewMatrixCall } from 'matrix-js-sdk/src/webrtc/call' ;
6463import dis from './dispatcher/dispatcher' ;
6564import WidgetUtils from './utils/WidgetUtils' ;
6665import WidgetEchoStore from './stores/WidgetEchoStore' ;
@@ -77,7 +76,7 @@ import ErrorDialog from "./components/views/dialogs/ErrorDialog";
7776import WidgetStore from "./stores/WidgetStore" ;
7877import { WidgetMessagingStore } from "./stores/widgets/WidgetMessagingStore" ;
7978import { ElementWidgetActions } from "./stores/widgets/ElementWidgetActions" ;
80- import { MatrixCall , CallErrorCode , CallState , CallEvent , CallParty , CallType } from "matrix-js-sdk/lib /webrtc/call" ;
79+ import { MatrixCall , CallErrorCode , CallState , CallEvent , CallParty , CallType } from "matrix-js-sdk/src /webrtc/call" ;
8180import Analytics from './Analytics' ;
8281import CountlyAnalytics from "./CountlyAnalytics" ;
8382
@@ -98,6 +97,21 @@ export enum PlaceCallType {
9897 ScreenSharing = 'screensharing' ,
9998}
10099
100+ function getRemoteAudioElement ( ) : HTMLAudioElement {
101+ // this needs to be somewhere at the top of the DOM which
102+ // always exists to avoid audio interruptions.
103+ // Might as well just use DOM.
104+ const remoteAudioElement = document . getElementById ( "remoteAudio" ) as HTMLAudioElement ;
105+ if ( ! remoteAudioElement ) {
106+ console . error (
107+ "Failed to find remoteAudio element - cannot play audio!" +
108+ "You need to add an <audio/> to the DOM." ,
109+ ) ;
110+ return null ;
111+ }
112+ return remoteAudioElement ;
113+ }
114+
101115export default class CallHandler {
102116 private calls = new Map < string , MatrixCall > ( ) ;
103117 private audioPromises = new Map < AudioID , Promise < void > > ( ) ;
@@ -291,6 +305,11 @@ export default class CallHandler {
291305 } ) ;
292306 }
293307
308+ private setCallAudioElement ( call : MatrixCall ) {
309+ const audioElement = getRemoteAudioElement ( ) ;
310+ if ( audioElement ) call . setRemoteAudioElement ( audioElement ) ;
311+ }
312+
294313 private setCallState ( call : MatrixCall , status : CallState ) {
295314 console . log (
296315 `Call state in ${ call . roomId } changed to ${ status } ` ,
@@ -343,9 +362,11 @@ export default class CallHandler {
343362 ) {
344363 Analytics . trackEvent ( 'voip' , 'placeCall' , 'type' , type ) ;
345364 CountlyAnalytics . instance . trackStartCall ( roomId , type === PlaceCallType . Video , false ) ;
346- const call = Matrix . createNewMatrixCall ( MatrixClientPeg . get ( ) , roomId ) ;
365+ const call = createNewMatrixCall ( MatrixClientPeg . get ( ) , roomId ) ;
347366 this . calls . set ( roomId , call ) ;
348367 this . setCallListeners ( call ) ;
368+ this . setCallAudioElement ( call ) ;
369+
349370 if ( type === PlaceCallType . Voice ) {
350371 call . placeVoiceCall ( ) ;
351372 } else if ( type === 'video' ) {
@@ -451,6 +472,7 @@ export default class CallHandler {
451472 Analytics . trackEvent ( 'voip' , 'receiveCall' , 'type' , call . type ) ;
452473 this . calls . set ( call . roomId , call )
453474 this . setCallListeners ( call ) ;
475+ this . setCallAudioElement ( call ) ;
454476 }
455477 break ;
456478 case 'hangup' :
0 commit comments