Skip to content

Commit d845e7b

Browse files
conference: add call_id handling
The `call_id` does seem to be after all not the same as `conf_id`!
1 parent 0569f85 commit d845e7b

File tree

6 files changed

+54
-30
lines changed

6 files changed

+54
-30
lines changed

pkg/conference/matrix.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@ import (
66
"github.com/pion/webrtc/v3"
77
"github.com/sirupsen/logrus"
88
"maunium.net/go/mautrix/event"
9-
"maunium.net/go/mautrix/id"
109
)
1110

1211
type MessageContent interface{}
1312

1413
type MatrixMessage struct {
15-
UserID id.UserID
16-
Content MessageContent
14+
Sender ParticipantID
15+
Content MessageContent
16+
RawEvent *event.Event
1717
}
1818

1919
// New participant tries to join the conference.
2020
func (c *Conference) onNewParticipant(participantID ParticipantID, inviteEvent *event.CallInviteEventContent) error {
2121
logger := c.logger.WithFields(logrus.Fields{
2222
"user_id": participantID.UserID,
2323
"device_id": participantID.DeviceID,
24+
"call_id": participantID.CallID,
2425
})
2526

2627
logger.Info("Incoming call invite")

pkg/conference/participant.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
type ParticipantID struct {
1717
UserID id.UserID
1818
DeviceID id.DeviceID
19+
CallID string
1920
}
2021

2122
// Participant represents a participant in the conference.
@@ -32,6 +33,7 @@ func (p *Participant) asMatrixRecipient() signaling.MatrixRecipient {
3233
return signaling.MatrixRecipient{
3334
UserID: p.id.UserID,
3435
DeviceID: p.id.DeviceID,
36+
CallID: p.id.CallID,
3537
RemoteSessionID: p.remoteSessionID,
3638
}
3739
}

pkg/conference/processor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ func (c *Conference) handleDataChannelMessage(participant *Participant, sfuMessa
196196
func (c *Conference) processMatrixMessage(msg MatrixMessage) {
197197
switch ev := msg.Content.(type) {
198198
case *event.CallInviteEventContent:
199-
c.onNewParticipant(ParticipantID{UserID: msg.UserID, DeviceID: ev.DeviceID}, ev)
199+
c.onNewParticipant(msg.Sender, ev)
200200
case *event.CallCandidatesEventContent:
201-
c.onCandidates(ParticipantID{UserID: msg.UserID, DeviceID: ev.DeviceID}, ev)
201+
c.onCandidates(msg.Sender, ev)
202202
case *event.CallSelectAnswerEventContent:
203-
c.onSelectAnswer(ParticipantID{UserID: msg.UserID, DeviceID: ev.DeviceID}, ev)
203+
c.onSelectAnswer(msg.Sender, ev)
204204
case *event.CallHangupEventContent:
205-
c.onHangup(ParticipantID{UserID: msg.UserID, DeviceID: ev.DeviceID}, ev)
205+
c.onHangup(msg.Sender, ev)
206206
default:
207207
c.logger.Errorf("Unexpected event type: %T", ev)
208208
}

pkg/conference/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func StartConference(
4747
logger: logrus.WithFields(logrus.Fields{"conf_id": confID}),
4848
}
4949

50-
participantID := ParticipantID{UserID: UserID, DeviceID: inviteEvent.DeviceID}
50+
participantID := ParticipantID{UserID: UserID, DeviceID: inviteEvent.DeviceID, CallID: inviteEvent.CallID}
5151
if err := conference.onNewParticipant(participantID, inviteEvent); err != nil {
5252
return nil, err
5353
}

pkg/router.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/matrix-org/waterfall/pkg/signaling"
2323
"github.com/sirupsen/logrus"
2424
"maunium.net/go/mautrix/event"
25+
"maunium.net/go/mautrix/id"
2526
)
2627

2728
type Conference = common.Sender[conf.MatrixMessage]
@@ -58,10 +59,11 @@ func newRouter(matrix *signaling.MatrixClient, config conf.Config) chan<- Router
5859
case ConferenceEndedMessage:
5960
// Remove the conference that ended from the list.
6061
delete(router.conferenceSinks, msg.conferenceID)
62+
6163
// Process the message that was not read by the conference.
62-
if len(msg.unread) > 0 {
63-
// FIXME: We must handle these messages!
64-
logrus.Warnf("Unread messages: %v", len(msg.unread))
64+
for _, msg := range msg.unread {
65+
// TODO: We actually already know the type, so we can do this better.
66+
router.handleMatrixEvent(msg.RawEvent)
6567
}
6668
}
6769
}
@@ -72,22 +74,36 @@ func newRouter(matrix *signaling.MatrixClient, config conf.Config) chan<- Router
7274

7375
// Handles incoming To-Device events that the SFU receives from clients.
7476
func (r *Router) handleMatrixEvent(evt *event.Event) {
75-
// Check if `conf_id` is present in the message (right messages do have it).
76-
rawConferenceID, ok := evt.Content.Raw["conf_id"]
77-
if !ok {
78-
return
79-
}
77+
var (
78+
conferenceID string
79+
callID string
80+
deviceID string
81+
userID = evt.Sender
82+
)
8083

81-
// Try to parse the conference ID without parsing the whole event.
82-
conferenceID, ok := rawConferenceID.(string)
83-
if !ok {
84-
return
84+
// Check if `conf_id` is present in the message (right messages do have it).
85+
rawConferenceID, okConferenceId := evt.Content.Raw["conf_id"]
86+
rawCallID, okCallId := evt.Content.Raw["call_id"]
87+
rawDeviceID, okDeviceID := evt.Content.Raw["device_id"]
88+
89+
if okConferenceId && okCallId && okDeviceID {
90+
// Extract the conference ID from the message.
91+
conferenceID, okConferenceId = rawConferenceID.(string)
92+
callID, okCallId = rawCallID.(string)
93+
deviceID, okDeviceID = rawDeviceID.(string)
94+
95+
if !okConferenceId || !okCallId || !okDeviceID {
96+
logrus.Warn("Ignoring invalid message without IDs")
97+
return
98+
}
8599
}
86100

87101
logger := logrus.WithFields(logrus.Fields{
88-
"type": evt.Type.Type,
89-
"user_id": evt.Sender.String(),
90-
"conf_id": conferenceID,
102+
"type": evt.Type.Type,
103+
"user_id": userID,
104+
"conf_id": conferenceID,
105+
"call_id": callID,
106+
"device_id": deviceID,
91107
})
92108

93109
conference := r.conferenceSinks[conferenceID]
@@ -101,7 +117,7 @@ func (r *Router) handleMatrixEvent(evt *event.Event) {
101117
r.config,
102118
r.matrix.CreateForConference(conferenceID),
103119
createConferenceEndNotifier(conferenceID, r.channel),
104-
evt.Sender,
120+
userID,
105121
evt.Content.AsCallInvite(),
106122
)
107123
if err != nil {
@@ -122,9 +138,10 @@ func (r *Router) handleMatrixEvent(evt *event.Event) {
122138
// A helper function to deal with messages that can't be sent due to the conference closed.
123139
// Not a function due to the need to capture local environment.
124140
sendToConference := func(eventContent conf.MessageContent) {
141+
sender := conf.ParticipantID{userID, id.DeviceID(deviceID), callID}
125142
// At this point the conference is not nil.
126143
// Let's check if the channel is still available.
127-
if conference.Send(conf.MatrixMessage{UserID: evt.Sender, Content: eventContent}) != nil {
144+
if conference.Send(conf.MatrixMessage{Content: eventContent, RawEvent: evt, Sender: sender}) != nil {
128145
// If sending failed, then the conference is over.
129146
delete(r.conferenceSinks, conferenceID)
130147
// Since we were not able to send the message, let's re-process it now.

pkg/signaling/matrix.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type MatrixRecipient struct {
4646
UserID id.UserID
4747
DeviceID id.DeviceID
4848
RemoteSessionID id.SessionID
49+
CallID string
4950
}
5051

5152
// Interface that abstracts sending Send-to-device messages for the conference.
@@ -63,7 +64,7 @@ func (m *MatrixForConference) SendSDPAnswer(
6364
) {
6465
eventContent := &event.Content{
6566
Parsed: event.CallAnswerEventContent{
66-
BaseCallEventContent: m.createBaseEventContent(recipient.RemoteSessionID),
67+
BaseCallEventContent: m.createBaseEventContent(recipient.CallID, recipient.RemoteSessionID),
6768
Answer: event.CallData{
6869
Type: "answer",
6970
SDP: sdp,
@@ -78,7 +79,7 @@ func (m *MatrixForConference) SendSDPAnswer(
7879
func (m *MatrixForConference) SendICECandidates(recipient MatrixRecipient, candidates []event.CallCandidate) {
7980
eventContent := &event.Content{
8081
Parsed: event.CallCandidatesEventContent{
81-
BaseCallEventContent: m.createBaseEventContent(recipient.RemoteSessionID),
82+
BaseCallEventContent: m.createBaseEventContent(recipient.CallID, recipient.RemoteSessionID),
8283
Candidates: candidates,
8384
},
8485
}
@@ -89,7 +90,7 @@ func (m *MatrixForConference) SendICECandidates(recipient MatrixRecipient, candi
8990
func (m *MatrixForConference) SendCandidatesGatheringFinished(recipient MatrixRecipient) {
9091
eventContent := &event.Content{
9192
Parsed: event.CallCandidatesEventContent{
92-
BaseCallEventContent: m.createBaseEventContent(recipient.RemoteSessionID),
93+
BaseCallEventContent: m.createBaseEventContent(recipient.CallID, recipient.RemoteSessionID),
9394
Candidates: []event.CallCandidate{{Candidate: ""}},
9495
},
9596
}
@@ -100,15 +101,18 @@ func (m *MatrixForConference) SendCandidatesGatheringFinished(recipient MatrixRe
100101
func (m *MatrixForConference) SendHangup(recipient MatrixRecipient, reason event.CallHangupReason) {
101102
eventContent := &event.Content{
102103
Parsed: event.CallHangupEventContent{
103-
BaseCallEventContent: m.createBaseEventContent(recipient.RemoteSessionID),
104+
BaseCallEventContent: m.createBaseEventContent(recipient.CallID, recipient.RemoteSessionID),
104105
Reason: reason,
105106
},
106107
}
107108

108109
m.sendToDevice(recipient, event.CallHangup, eventContent)
109110
}
110111

111-
func (m *MatrixForConference) createBaseEventContent(destSessionID id.SessionID) event.BaseCallEventContent {
112+
func (m *MatrixForConference) createBaseEventContent(
113+
callID string,
114+
destSessionID id.SessionID,
115+
) event.BaseCallEventContent {
112116
return event.BaseCallEventContent{
113117
CallID: m.conferenceID,
114118
ConfID: m.conferenceID,

0 commit comments

Comments
 (0)