@@ -36,6 +36,7 @@ import {
3636 SDPStreamMetadataPurpose ,
3737 SDPStreamMetadata ,
3838 SDPStreamMetadataKey ,
39+ MCallSDPStreamMetadataChanged ,
3940} from './callEventTypes' ;
4041import { CallFeed } from './callFeed' ;
4142
@@ -353,8 +354,6 @@ export class MatrixCall extends EventEmitter {
353354 this . makingOffer = false ;
354355
355356 this . remoteOnHold = false ;
356- this . micMuted = false ;
357- this . vidMuted = false ;
358357
359358 this . feeds = [ ] ;
360359
@@ -402,6 +401,14 @@ export class MatrixCall extends EventEmitter {
402401 return this . remoteAssertedIdentity ;
403402 }
404403
404+ public get localUsermediaFeed ( ) : CallFeed {
405+ return this . getLocalFeeds ( ) . find ( ( feed ) => feed . purpose === SDPStreamMetadataPurpose . Usermedia ) ;
406+ }
407+
408+ private getFeedByStreamId ( streamId : string ) : CallFeed {
409+ return this . getFeeds ( ) . find ( ( feed ) => feed . stream . id === streamId ) ;
410+ }
411+
405412 /**
406413 * Returns an array of all CallFeeds
407414 * @returns {Array<CallFeed> } CallFeeds
@@ -431,10 +438,12 @@ export class MatrixCall extends EventEmitter {
431438 * @returns {SDPStreamMetadata } localSDPStreamMetadata
432439 */
433440 private getLocalSDPStreamMetadata ( ) : SDPStreamMetadata {
434- const metadata = { } ;
441+ const metadata : SDPStreamMetadata = { } ;
435442 for ( const localFeed of this . getLocalFeeds ( ) ) {
436443 metadata [ localFeed . stream . id ] = {
437444 purpose : localFeed . purpose ,
445+ audio_muted : localFeed . isAudioMuted ( ) ,
446+ video_muted : localFeed . isVideoMuted ( ) ,
438447 } ;
439448 }
440449 logger . debug ( "Got local SDPStreamMetadata" , metadata ) ;
@@ -459,6 +468,8 @@ export class MatrixCall extends EventEmitter {
459468
460469 const userId = this . getOpponentMember ( ) . userId ;
461470 const purpose = this . remoteSDPStreamMetadata [ stream . id ] . purpose ;
471+ const audioMuted = this . remoteSDPStreamMetadata [ stream . id ] . audio_muted ;
472+ const videoMuted = this . remoteSDPStreamMetadata [ stream . id ] . video_muted ;
462473
463474 if ( ! purpose ) {
464475 logger . warn ( `Ignoring stream with id ${ stream . id } because we didn't get any metadata about it` ) ;
@@ -471,7 +482,7 @@ export class MatrixCall extends EventEmitter {
471482 if ( existingFeed ) {
472483 existingFeed . setNewStream ( stream ) ;
473484 } else {
474- this . feeds . push ( new CallFeed ( stream , userId , purpose , this . client , this . roomId ) ) ;
485+ this . feeds . push ( new CallFeed ( stream , userId , purpose , this . client , this . roomId , audioMuted , videoMuted ) ) ;
475486 this . emit ( CallEvent . FeedsChanged , this . feeds ) ;
476487 }
477488
@@ -498,11 +509,11 @@ export class MatrixCall extends EventEmitter {
498509
499510 // Try to find a feed with the same stream id as the new stream,
500511 // if we find it replace the old stream with the new one
501- const feed = this . feeds . find ( ( feed ) => feed . stream . id === stream . id ) ;
512+ const feed = this . getFeedByStreamId ( stream . id ) ;
502513 if ( feed ) {
503514 feed . setNewStream ( stream ) ;
504515 } else {
505- this . feeds . push ( new CallFeed ( stream , userId , purpose , this . client , this . roomId ) ) ;
516+ this . feeds . push ( new CallFeed ( stream , userId , purpose , this . client , this . roomId , false , false ) ) ;
506517 this . emit ( CallEvent . FeedsChanged , this . feeds ) ;
507518 }
508519
@@ -517,7 +528,7 @@ export class MatrixCall extends EventEmitter {
517528 if ( existingFeed ) {
518529 existingFeed . setNewStream ( stream ) ;
519530 } else {
520- this . feeds . push ( new CallFeed ( stream , userId , purpose , this . client , this . roomId ) ) ;
531+ this . feeds . push ( new CallFeed ( stream , userId , purpose , this . client , this . roomId , false , false ) ) ;
521532 this . emit ( CallEvent . FeedsChanged , this . feeds ) ;
522533 }
523534
@@ -555,7 +566,7 @@ export class MatrixCall extends EventEmitter {
555566 private deleteFeedByStream ( stream : MediaStream ) {
556567 logger . debug ( `Removing feed with stream id ${ stream . id } ` ) ;
557568
558- const feed = this . feeds . find ( ( feed ) => feed . stream . id === stream . id ) ;
569+ const feed = this . getFeedByStreamId ( stream . id ) ;
559570 if ( ! feed ) {
560571 logger . warn ( `Didn't find the feed with stream id ${ stream . id } to delete` ) ;
561572 return ;
@@ -605,7 +616,7 @@ export class MatrixCall extends EventEmitter {
605616
606617 const sdpStreamMetadata = invite [ SDPStreamMetadataKey ] ;
607618 if ( sdpStreamMetadata ) {
608- this . remoteSDPStreamMetadata = sdpStreamMetadata ;
619+ this . updateRemoteSDPStreamMetadata ( sdpStreamMetadata ) ;
609620 } else {
610621 logger . debug ( "Did not get any SDPStreamMetadata! Can not send/receive multiple streams" ) ;
611622 }
@@ -891,7 +902,7 @@ export class MatrixCall extends EventEmitter {
891902 * @param {boolean } muted True to mute the outbound video.
892903 */
893904 setLocalVideoMuted ( muted : boolean ) {
894- this . vidMuted = muted ;
905+ this . localUsermediaFeed ?. setVideoMuted ( muted ) ;
895906 this . updateMuteStatus ( ) ;
896907 }
897908
@@ -905,16 +916,15 @@ export class MatrixCall extends EventEmitter {
905916 * (including if the call is not set up yet).
906917 */
907918 isLocalVideoMuted ( ) : boolean {
908- if ( this . type === CallType . Voice ) return true ;
909- return this . vidMuted ;
919+ return this . localUsermediaFeed ?. isVideoMuted ( ) ;
910920 }
911921
912922 /**
913923 * Set whether the microphone should be muted or not.
914924 * @param {boolean } muted True to mute the mic.
915925 */
916926 setMicrophoneMuted ( muted : boolean ) {
917- this . micMuted = muted ;
927+ this . localUsermediaFeed ?. setAudioMuted ( muted ) ;
918928 this . updateMuteStatus ( ) ;
919929 }
920930
@@ -928,7 +938,7 @@ export class MatrixCall extends EventEmitter {
928938 * is not set up yet).
929939 */
930940 isMicrophoneMuted ( ) : boolean {
931- return this . micMuted ;
941+ return this . localUsermediaFeed ?. isAudioMuted ( ) ;
932942 }
933943
934944 /**
@@ -991,14 +1001,14 @@ export class MatrixCall extends EventEmitter {
9911001 }
9921002
9931003 private updateMuteStatus ( ) {
994- if ( ! this . localAVStream ) {
995- return ;
996- }
1004+ this . sendVoipEvent ( EventType . CallSDPStreamMetadataChangedPrefix , {
1005+ [ SDPStreamMetadataKey ] : this . getLocalSDPStreamMetadata ( ) ,
1006+ } ) ;
9971007
998- const micShouldBeMuted = this . micMuted || this . remoteOnHold ;
999- setTracksEnabled ( this . localAVStream . getAudioTracks ( ) , ! micShouldBeMuted ) ;
1008+ const micShouldBeMuted = this . localUsermediaFeed ?. isAudioMuted ( ) || this . remoteOnHold ;
1009+ const vidShouldBeMuted = this . localUsermediaFeed ?. isVideoMuted ( ) || this . remoteOnHold ;
10001010
1001- const vidShouldBeMuted = this . vidMuted || this . remoteOnHold ;
1011+ setTracksEnabled ( this . localAVStream . getAudioTracks ( ) , ! micShouldBeMuted ) ;
10021012 setTracksEnabled ( this . localAVStream . getVideoTracks ( ) , ! vidShouldBeMuted ) ;
10031013 }
10041014
@@ -1214,7 +1224,7 @@ export class MatrixCall extends EventEmitter {
12141224
12151225 const sdpStreamMetadata = event . getContent ( ) [ SDPStreamMetadataKey ] ;
12161226 if ( sdpStreamMetadata ) {
1217- this . remoteSDPStreamMetadata = sdpStreamMetadata ;
1227+ this . updateRemoteSDPStreamMetadata ( sdpStreamMetadata ) ;
12181228 } else {
12191229 logger . warn ( "Did not get any SDPStreamMetadata! Can not send/receive multiple streams" ) ;
12201230 }
@@ -1289,9 +1299,9 @@ export class MatrixCall extends EventEmitter {
12891299
12901300 const prevLocalOnHold = this . isLocalOnHold ( ) ;
12911301
1292- const metadata = event . getContent ( ) [ SDPStreamMetadataKey ] ;
1293- if ( metadata ) {
1294- this . remoteSDPStreamMetadata = metadata ;
1302+ const sdpStreamMetadata = event . getContent ( ) [ SDPStreamMetadataKey ] ;
1303+ if ( sdpStreamMetadata ) {
1304+ this . updateRemoteSDPStreamMetadata ( sdpStreamMetadata ) ;
12951305 } else {
12961306 logger . warn ( "Received negotiation event without SDPStreamMetadata!" ) ;
12971307 }
@@ -1321,6 +1331,22 @@ export class MatrixCall extends EventEmitter {
13211331 }
13221332 }
13231333
1334+ private updateRemoteSDPStreamMetadata ( metadata : SDPStreamMetadata ) : void {
1335+ this . remoteSDPStreamMetadata = utils . recursivelyAssign ( this . remoteSDPStreamMetadata || { } , metadata , true ) ;
1336+ for ( const feed of this . getRemoteFeeds ( ) ) {
1337+ const streamId = feed . stream . id ;
1338+ feed . setAudioMuted ( this . remoteSDPStreamMetadata [ streamId ] ?. audio_muted ) ;
1339+ feed . setVideoMuted ( this . remoteSDPStreamMetadata [ streamId ] ?. video_muted ) ;
1340+ feed . purpose = this . remoteSDPStreamMetadata [ streamId ] ?. purpose ;
1341+ }
1342+ }
1343+
1344+ public onSDPStreamMetadataChangedReceived ( event : MatrixEvent ) : void {
1345+ const content = event . getContent < MCallSDPStreamMetadataChanged > ( ) ;
1346+ const metadata = content [ SDPStreamMetadataKey ] ;
1347+ this . updateRemoteSDPStreamMetadata ( metadata ) ;
1348+ }
1349+
13241350 async onAssertedIdentityReceived ( event : MatrixEvent ) {
13251351 if ( ! event . getContent ( ) . asserted_identity ) return ;
13261352
0 commit comments