diff --git a/sfu/forwarding.go b/sfu/forwarding.go index 2256317..8a0679f 100644 --- a/sfu/forwarding.go +++ b/sfu/forwarding.go @@ -7,9 +7,6 @@ import ( "github.com/pion/webrtc/v4" ) -// SFU -> Java signaling: отправить server-offer для конкретного peer -var OnServerOffer func(roomID string, peerID string, offer webrtc.SessionDescription) - // Вызывается при JoinWithOffer для ретрансляции RTP пакетов от издателя к другим участникам комнаты func SetupForwardingForPeer(roomID string, publisherPeerID string, publisherPC *webrtc.PeerConnection) { publisherPC.OnTrack(func(remote *webrtc.TrackRemote, _ *webrtc.RTPReceiver) { @@ -91,8 +88,24 @@ func renegotiatePeer(roomID, peerID string, pc *webrtc.PeerConnection) error { return nil } -// Java -> SFU: answer клиента на server-offer -func HandleClientAnswer(roomID, peerID string, answer webrtc.SessionDescription) error { +// Добавляет ICE-кандидата к пиру +func AddICECandidate(roomID string, peerID string, candidate webrtc.ICECandidateInit) error { + room, exists := GetRoom(roomID) + if !exists { + return ErrRoomNotFound + } + + for _, peer := range room.Peers { + if peer.PeerID == peerID { + return peer.PeerConnection.AddICECandidate(candidate) + } + } + + return ErrPeerNotFound +} + +// Обрабатывает SDP ответ от клиента при renegotiation +func HandleClientAnswer(roomID string, peerID string, answer webrtc.SessionDescription) error { roomsMu.RLock() room, ok := rooms[roomID] if !ok { diff --git a/sfu/rooms.go b/sfu/rooms.go index 71cd525..5efd5fe 100644 --- a/sfu/rooms.go +++ b/sfu/rooms.go @@ -110,18 +110,20 @@ func JoinWithOffer(roomID string, peerID string, offer webrtc.SessionDescription return peerConnection.LocalDescription(), nil } -// AddICECandidate добавляет ICE-кандидата к пиру -func AddICECandidate(roomID string, peerID string, candidate webrtc.ICECandidateInit) error { +// LeaveRoom позволяет пиру покинуть комнату +func LeaveRoom(roomID string, peerID string) error { room, exists := GetRoom(roomID) if !exists { return ErrRoomNotFound } - for _, peer := range room.Peers { + for i, peer := range room.Peers { if peer.PeerID == peerID { - return peer.PeerConnection.AddICECandidate(candidate) + peer.PeerConnection.Close() + room.Peers = append(room.Peers[:i], room.Peers[i+1:]...) + break } } - return ErrPeerNotFound + return nil } diff --git a/sfu/sfu.go b/sfu/sfu.go index 1d85bfd..15e1785 100644 --- a/sfu/sfu.go +++ b/sfu/sfu.go @@ -14,6 +14,9 @@ var ( pcConfig webrtc.Configuration ) +// Коллбек для обработки новых SDP офферов от сервера для конкретного пира (при renegotiation) +var OnServerOffer func(roomID string, peerID string, offer webrtc.SessionDescription) + // Коллбек для обработки новых ICE кандидатов var OnLocalICECandidate func(roomID, peerID string, candidate webrtc.ICECandidateInit)