| 
 | 1 | +package conference  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"github.com/pion/webrtc/v3"  | 
 | 5 | +	"golang.org/x/exp/slices"  | 
 | 6 | +	"maunium.net/go/mautrix/event"  | 
 | 7 | +)  | 
 | 8 | + | 
 | 9 | +// Handle the `SFUMessage` event from the DataChannel message.  | 
 | 10 | +func (c *Conference) processSelectDCMessage(participant *Participant, msg event.SFUMessage) {  | 
 | 11 | +	participant.logger.Info("Received select request over DC")  | 
 | 12 | + | 
 | 13 | +	// Find tracks based on what we were asked for.  | 
 | 14 | +	tracks := c.getTracks(msg.Start)  | 
 | 15 | + | 
 | 16 | +	// Let's check if we have all the tracks that we were asked for are there.  | 
 | 17 | +	// If not, we will list which are not available (later on we must inform participant  | 
 | 18 | +	// about it unless the participant retries it).  | 
 | 19 | +	if len(tracks) != len(msg.Start) {  | 
 | 20 | +		for _, expected := range msg.Start {  | 
 | 21 | +			found := slices.IndexFunc(tracks, func(track *webrtc.TrackLocalStaticRTP) bool {  | 
 | 22 | +				return track.StreamID() == expected.StreamID && track.ID() == expected.TrackID  | 
 | 23 | +			})  | 
 | 24 | + | 
 | 25 | +			if found == -1 {  | 
 | 26 | +				c.logger.Warnf("Track not found: %s", expected.TrackID)  | 
 | 27 | +			}  | 
 | 28 | +		}  | 
 | 29 | +	}  | 
 | 30 | + | 
 | 31 | +	// Subscribe to the found tracks.  | 
 | 32 | +	for _, track := range tracks {  | 
 | 33 | +		if err := participant.peer.SubscribeTo(track); err != nil {  | 
 | 34 | +			participant.logger.Errorf("Failed to subscribe to track: %v", err)  | 
 | 35 | +			return  | 
 | 36 | +		}  | 
 | 37 | +	}  | 
 | 38 | +}  | 
 | 39 | + | 
 | 40 | +func (c *Conference) processAnswerDCMessage(participant *Participant, msg event.SFUMessage) {  | 
 | 41 | +	participant.logger.Info("Received SDP answer over DC")  | 
 | 42 | + | 
 | 43 | +	if err := participant.peer.ProcessSDPAnswer(msg.SDP); err != nil {  | 
 | 44 | +		participant.logger.Errorf("Failed to set SDP answer: %v", err)  | 
 | 45 | +		return  | 
 | 46 | +	}  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +func (c *Conference) processPublishDCMessage(participant *Participant, msg event.SFUMessage) {  | 
 | 50 | +	participant.logger.Info("Received SDP offer over DC")  | 
 | 51 | + | 
 | 52 | +	answer, err := participant.peer.ProcessSDPOffer(msg.SDP)  | 
 | 53 | +	if err != nil {  | 
 | 54 | +		participant.logger.Errorf("Failed to set SDP offer: %v", err)  | 
 | 55 | +		return  | 
 | 56 | +	}  | 
 | 57 | + | 
 | 58 | +	participant.streamMetadata = msg.Metadata  | 
 | 59 | + | 
 | 60 | +	participant.sendDataChannelMessage(event.SFUMessage{  | 
 | 61 | +		Op:       event.SFUOperationAnswer,  | 
 | 62 | +		SDP:      answer.SDP,  | 
 | 63 | +		Metadata: c.getAvailableStreamsFor(participant.id),  | 
 | 64 | +	})  | 
 | 65 | +}  | 
 | 66 | + | 
 | 67 | +func (c *Conference) processUnpublishDCMessage(participant *Participant) {  | 
 | 68 | +	participant.logger.Info("Received unpublish over DC")  | 
 | 69 | +}  | 
 | 70 | + | 
 | 71 | +func (c *Conference) processAliveDCMessage(participant *Participant) {  | 
 | 72 | +	participant.peer.ProcessHeartbeat()  | 
 | 73 | +}  | 
 | 74 | + | 
 | 75 | +func (c *Conference) processMetadataDCMessage(participant *Participant, msg event.SFUMessage) {  | 
 | 76 | +	participant.streamMetadata = msg.Metadata  | 
 | 77 | +	c.resendMetadataToAllExcept(participant.id)  | 
 | 78 | +}  | 
0 commit comments