|
| 1 | +package conference |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/matrix-org/waterfall/pkg/peer" |
| 7 | + "github.com/pion/webrtc/v3" |
| 8 | + "maunium.net/go/mautrix/event" |
| 9 | +) |
| 10 | + |
| 11 | +func (c *Conference) processJoinedTheCallMessage(participant *Participant, message peer.JoinedTheCall) { |
| 12 | + participant.logger.Info("Joined the call") |
| 13 | +} |
| 14 | + |
| 15 | +func (c *Conference) processLeftTheCallMessage(participant *Participant, msg peer.LeftTheCall) { |
| 16 | + participant.logger.Info("Left the call: %s", msg.Reason) |
| 17 | + c.removeParticipant(participant.id) |
| 18 | + c.signaling.SendHangup(participant.asMatrixRecipient(), msg.Reason) |
| 19 | +} |
| 20 | + |
| 21 | +func (c *Conference) processNewTrackPublishedMessage(participant *Participant, msg peer.NewTrackPublished) { |
| 22 | + participant.logger.Infof("Published new track: %s", msg.Track.ID()) |
| 23 | + key := event.SFUTrackDescription{ |
| 24 | + StreamID: msg.Track.StreamID(), |
| 25 | + TrackID: msg.Track.ID(), |
| 26 | + } |
| 27 | + |
| 28 | + if _, ok := participant.publishedTracks[key]; ok { |
| 29 | + c.logger.Errorf("Track already published: %v", key) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + participant.publishedTracks[key] = PublishedTrack{Track: msg.Track} |
| 34 | + c.resendMetadataToAllExcept(participant.id) |
| 35 | +} |
| 36 | + |
| 37 | +func (c *Conference) processPublishedTrackFailedMessage(participant *Participant, msg peer.PublishedTrackFailed) { |
| 38 | + participant.logger.Infof("Failed published track: %s", msg.Track.ID()) |
| 39 | + delete(participant.publishedTracks, event.SFUTrackDescription{ |
| 40 | + StreamID: msg.Track.StreamID(), |
| 41 | + TrackID: msg.Track.ID(), |
| 42 | + }) |
| 43 | + |
| 44 | + for _, otherParticipant := range c.participants { |
| 45 | + if otherParticipant.id == participant.id { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + otherParticipant.peer.UnsubscribeFrom([]*webrtc.TrackLocalStaticRTP{msg.Track}) |
| 50 | + } |
| 51 | + |
| 52 | + c.resendMetadataToAllExcept(participant.id) |
| 53 | +} |
| 54 | + |
| 55 | +func (c *Conference) processNewICECandidateMessage(participant *Participant, msg peer.NewICECandidate) { |
| 56 | + participant.logger.Debug("Received a new local ICE candidate") |
| 57 | + |
| 58 | + // Convert WebRTC ICE candidate to Matrix ICE candidate. |
| 59 | + jsonCandidate := msg.Candidate.ToJSON() |
| 60 | + candidates := []event.CallCandidate{{ |
| 61 | + Candidate: jsonCandidate.Candidate, |
| 62 | + SDPMLineIndex: int(*jsonCandidate.SDPMLineIndex), |
| 63 | + SDPMID: *jsonCandidate.SDPMid, |
| 64 | + }} |
| 65 | + c.signaling.SendICECandidates(participant.asMatrixRecipient(), candidates) |
| 66 | +} |
| 67 | + |
| 68 | +func (c *Conference) processICEGatheringCompleteMessage(participant *Participant, msg peer.ICEGatheringComplete) { |
| 69 | + participant.logger.Info("Completed local ICE gathering") |
| 70 | + |
| 71 | + // Send an empty array of candidates to indicate that ICE gathering is complete. |
| 72 | + c.signaling.SendCandidatesGatheringFinished(participant.asMatrixRecipient()) |
| 73 | +} |
| 74 | + |
| 75 | +func (c *Conference) processRenegotiationRequiredMessage(participant *Participant, msg peer.RenegotiationRequired) { |
| 76 | + participant.logger.Info("Started renegotiation") |
| 77 | + participant.sendDataChannelMessage(event.SFUMessage{ |
| 78 | + Op: event.SFUOperationOffer, |
| 79 | + SDP: msg.Offer.SDP, |
| 80 | + Metadata: c.getAvailableStreamsFor(participant.id), |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +func (c *Conference) processDataChannelMessage(participant *Participant, msg peer.DataChannelMessage) { |
| 85 | + participant.logger.Debug("Received data channel message") |
| 86 | + var sfuMessage event.SFUMessage |
| 87 | + if err := json.Unmarshal([]byte(msg.Message), &sfuMessage); err != nil { |
| 88 | + c.logger.Errorf("Failed to unmarshal SFU message: %v", err) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + switch sfuMessage.Op { |
| 93 | + case event.SFUOperationSelect: |
| 94 | + c.processSelectDCMessage(participant, sfuMessage) |
| 95 | + case event.SFUOperationAnswer: |
| 96 | + c.processAnswerDCMessage(participant, sfuMessage) |
| 97 | + case event.SFUOperationPublish: |
| 98 | + c.processPublishDCMessage(participant, sfuMessage) |
| 99 | + case event.SFUOperationUnpublish: |
| 100 | + c.processUnpublishDCMessage(participant) |
| 101 | + case event.SFUOperationAlive: |
| 102 | + c.processAliveDCMessage(participant) |
| 103 | + case event.SFUOperationMetadata: |
| 104 | + c.processMetadataDCMessage(participant, sfuMessage) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func (c *Conference) processDataChannelAvailableMessage(participant *Participant, msg peer.DataChannelAvailable) { |
| 109 | + participant.logger.Info("Connected data channel") |
| 110 | + participant.sendDataChannelMessage(event.SFUMessage{ |
| 111 | + Op: event.SFUOperationMetadata, |
| 112 | + Metadata: c.getAvailableStreamsFor(participant.id), |
| 113 | + }) |
| 114 | +} |
| 115 | + |
| 116 | +func (c *Conference) processForwardRTCPMessage(msg peer.ForwardRTCP) { |
| 117 | + for _, participant := range c.participants { |
| 118 | + for _, publishedTrack := range participant.publishedTracks { |
| 119 | + if publishedTrack.Track.StreamID() == msg.StreamID && publishedTrack.Track.ID() == msg.TrackID { |
| 120 | + participant.peer.WriteRTCP(msg.Packets, msg.StreamID, msg.TrackID, publishedTrack.LastPLITimestamp.Load()) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func (c *Conference) processPLISentMessage(msg peer.PLISent) { |
| 127 | + for _, participant := range c.participants { |
| 128 | + for _, publishedTrack := range participant.publishedTracks { |
| 129 | + if publishedTrack.Track.StreamID() == msg.StreamID && publishedTrack.Track.ID() == msg.TrackID { |
| 130 | + publishedTrack.LastPLITimestamp.Store(msg.Timestamp) |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments