From f107d63fab3fb1e3bfd60b4caf4d0005fce7c8fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Fri, 27 Aug 2021 15:06:55 +0200 Subject: [PATCH] Give `MatrixCall` the capability to emit `LengthChanged` events MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- src/webrtc/call.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/webrtc/call.ts b/src/webrtc/call.ts index cfc19b2fe..4098377d3 100644 --- a/src/webrtc/call.ts +++ b/src/webrtc/call.ts @@ -120,6 +120,8 @@ export enum CallEvent { FeedsChanged = 'feeds_changed', AssertedIdentityChanged = 'asserted_identity_changed', + + LengthChanged = 'length_changed' } export enum CallErrorCode { @@ -313,6 +315,9 @@ export class MatrixCall extends EventEmitter { private remoteSDPStreamMetadata: SDPStreamMetadata; + private callLengthInterval: number; + private callLength = 0; + constructor(opts: CallOpts) { super(); this.roomId = opts.roomId; @@ -1485,6 +1490,10 @@ export class MatrixCall extends EventEmitter { // chrome doesn't implement any of the 'onstarted' events yet if (this.peerConn.iceConnectionState == 'connected') { this.setState(CallState.Connected); + this.callLengthInterval = setInterval(() => { + this.callLength++; + this.emit(CallEvent.LengthChanged, this.callLength); + }, 1000); } else if (this.peerConn.iceConnectionState == 'failed') { this.hangup(CallErrorCode.IceFailed, false); } @@ -1735,6 +1744,10 @@ export class MatrixCall extends EventEmitter { clearTimeout(this.inviteTimeout); this.inviteTimeout = null; } + if (this.callLengthInterval) { + clearInterval(this.callLengthInterval); + this.callLengthInterval = null; + } // Order is important here: first we stopAllMedia() and only then we can deleteAllFeeds() // We don't stop media if the call was replaced as we want to re-use streams in the successor