@@ -18,46 +18,33 @@ package conference
1818
1919import  (
2020	"github.com/matrix-org/waterfall/src/peer" 
21+ 	"github.com/matrix-org/waterfall/src/signaling" 
2122	"github.com/pion/webrtc/v3" 
2223	"github.com/sirupsen/logrus" 
2324	"maunium.net/go/mautrix/event" 
24- 	"maunium.net/go/mautrix/id" 
2525)
2626
27- // Configuration for the group conferences (calls). 
28- type  CallConfig  struct  {
29- 	// Keep-alive timeout for WebRTC connections. If no keep-alive has been received 
30- 	// from the client for this duration, the connection is considered dead. 
31- 	KeepAliveTimeout  int 
32- }
33- 
34- type  Participant  struct  {
35- 	Peer  * peer.Peer 
36- 	Data  * ParticipantData 
37- }
38- 
39- type  ParticipantData  struct  {
40- 	RemoteSessionID  id.SessionID 
41- 	StreamMetadata   event.CallSDPStreamMetadata 
42- }
43- 
4427type  Conference  struct  {
45- 	conferenceID         string 
46- 	config               * CallConfig 
47- 	participants         map [peer.ID ]* Participant 
48- 	participantsChannel  peer.MessageChannel 
49- 	logger               * logrus.Entry 
28+ 	id                string 
29+ 	config            Config 
30+ 	signaling         signaling.MatrixSignaling 
31+ 	participants      map [peer.ID ]* Participant 
32+ 	peerEventsStream  chan  peer.Message 
33+ 	logger            * logrus.Entry 
5034}
5135
52- func  NewConference (confID  string , config  * CallConfig ) * Conference  {
53- 	conference  :=  new (Conference )
54- 	conference .config  =  config 
55- 	conference .conferenceID  =  confID 
56- 	conference .participants  =  make (map [peer.ID ]* Participant )
57- 	conference .participantsChannel  =  make (peer.MessageChannel )
58- 	conference .logger  =  logrus .WithFields (logrus.Fields {
59- 		"conf_id" : confID ,
60- 	})
36+ func  NewConference (confID  string , config  Config , signaling  signaling.MatrixSignaling ) * Conference  {
37+ 	conference  :=  & Conference {
38+ 		id :               confID ,
39+ 		config :           config ,
40+ 		signaling :        signaling ,
41+ 		participants :     make (map [peer.ID ]* Participant ),
42+ 		peerEventsStream : make (chan  peer.Message ),
43+ 		logger :           logrus .WithFields (logrus.Fields {"conf_id" : confID }),
44+ 	}
45+ 
46+ 	// Start conference "main loop". 
47+ 	go  conference .processMessages ()
6148	return  conference 
6249}
6350
@@ -66,41 +53,43 @@ func (c *Conference) OnNewParticipant(participantID peer.ID, inviteEvent *event.
6653	// As per MSC3401, when the `session_id` field changes from an incoming `m.call.member` event, 
6754	// any existing calls from this device in this call should be terminated. 
6855	// TODO: Implement this. 
69- 	/* 
70- 		for _, participant := range c.participants { 
71- 			if participant.data.DeviceID == inviteEvent.DeviceID { 
72- 				if participant.data.RemoteSessionID == inviteEvent.SenderSessionID { 
73- 					c.logger.WithFields(logrus.Fields{ 
74- 						"device_id":  inviteEvent.DeviceID, 
75- 						"session_id": inviteEvent.SenderSessionID, 
76- 					}).Errorf("Found existing participant with equal DeviceID and SessionID") 
77- 					return 
78- 				} else { 
79- 					participant.Terminate() 
80- 					delete(c.participants, participant.data.UserID) 
81- 				} 
56+ 	for  id , participant  :=  range  c .participants  {
57+ 		if  id .DeviceID  ==  inviteEvent .DeviceID  {
58+ 			if  participant .remoteSessionID  ==  inviteEvent .SenderSessionID  {
59+ 				c .logger .WithFields (logrus.Fields {
60+ 					"device_id" :  inviteEvent .DeviceID ,
61+ 					"session_id" : inviteEvent .SenderSessionID ,
62+ 				}).Errorf ("Found existing participant with equal DeviceID and SessionID" )
63+ 				return 
64+ 			} else  {
65+ 				participant .peer .Terminate ()
8266			}
8367		}
84- 	*/  
68+ 	} 
8569
86- 	peer , _ , err  :=  peer .NewPeer (participantID , c .conferenceID , inviteEvent .Offer .SDP , c .participantsChannel )
70+ 	peer , sdpOffer , err  :=  peer .NewPeer (participantID , c .id , inviteEvent .Offer .SDP , c .peerEventsStream )
8771	if  err  !=  nil  {
8872		c .logger .WithError (err ).Errorf ("Failed to create new peer" )
8973		return 
9074	}
9175
92- 	participantData  :=  & ParticipantData {
93- 		RemoteSessionID : inviteEvent .SenderSessionID ,
94- 		StreamMetadata :  inviteEvent .SDPStreamMetadata ,
76+ 	participant  :=  & Participant {
77+ 		id :              participantID ,
78+ 		peer :            peer ,
79+ 		remoteSessionID : inviteEvent .SenderSessionID ,
80+ 		streamMetadata :  inviteEvent .SDPStreamMetadata ,
81+ 		publishedTracks : make (map [event.SFUTrackDescription ]* webrtc.TrackLocalStaticRTP ),
9582	}
9683
97- 	c .participants [participantID ] =  & Participant { Peer :  peer ,  Data :  participantData } 
84+ 	c .participants [participantID ] =  participant 
9885
99- 	// TODO: Send the SDP answer back to the participant's device. 
86+ 	recipient  :=  participant .asMatrixRecipient ()
87+ 	streamMetadata  :=  c .getStreamsMetadata (participantID )
88+ 	c .signaling .SendSDPAnswer (recipient , streamMetadata , sdpOffer .SDP )
10089}
10190
10291func  (c  * Conference ) OnCandidates (peerID  peer.ID , candidatesEvent  * event.CallCandidatesEventContent ) {
103- 	if  participant  :=  c .getParticipant (peerID ); participant  !=  nil  {
92+ 	if  participant  :=  c .getParticipant (peerID ,  nil ); participant  !=  nil  {
10493		// Convert the candidates to the WebRTC format. 
10594		candidates  :=  make ([]webrtc.ICECandidateInit , len (candidatesEvent .Candidates ))
10695		for  i , candidate  :=  range  candidatesEvent .Candidates  {
@@ -112,36 +101,23 @@ func (c *Conference) OnCandidates(peerID peer.ID, candidatesEvent *event.CallCan
112101			}
113102		}
114103
115- 		participant .Peer .AddICECandidates (candidates )
104+ 		participant .peer .AddICECandidates (candidates )
116105	}
117106}
118107
119108func  (c  * Conference ) OnSelectAnswer (peerID  peer.ID , selectAnswerEvent  * event.CallSelectAnswerEventContent ) {
120- 	if  participant  :=  c .getParticipant (peerID ); participant  !=  nil  {
109+ 	if  participant  :=  c .getParticipant (peerID ,  nil ); participant  !=  nil  {
121110		if  selectAnswerEvent .SelectedPartyID  !=  peerID .DeviceID .String () {
122111			c .logger .WithFields (logrus.Fields {
123112				"device_id" : selectAnswerEvent .SelectedPartyID ,
124113			}).Errorf ("Call was answered on a different device, kicking this peer" )
125- 			participant .Peer .Terminate ()
114+ 			participant .peer .Terminate ()
126115		}
127116	}
128117}
129118
130119func  (c  * Conference ) OnHangup (peerID  peer.ID , hangupEvent  * event.CallHangupEventContent ) {
131- 	if  participant  :=  c .getParticipant (peerID ); participant  !=  nil  {
132- 		participant .Peer .Terminate ()
133- 	}
134- }
135- 
136- func  (c  * Conference ) getParticipant (peerID  peer.ID ) * Participant  {
137- 	participant , ok  :=  c .participants [peerID ]
138- 	if  ! ok  {
139- 		c .logger .WithFields (logrus.Fields {
140- 			"user_id" :   peerID .UserID ,
141- 			"device_id" : peerID .DeviceID ,
142- 		}).Errorf ("Failed to find participant" )
143- 		return  nil 
120+ 	if  participant  :=  c .getParticipant (peerID , nil ); participant  !=  nil  {
121+ 		participant .peer .Terminate ()
144122	}
145- 
146- 	return  participant 
147123}
0 commit comments