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