Оптимизация кода и выход пира из комнаты
This commit is contained in:
@@ -7,9 +7,6 @@ import (
|
|||||||
"github.com/pion/webrtc/v4"
|
"github.com/pion/webrtc/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SFU -> Java signaling: отправить server-offer для конкретного peer
|
|
||||||
var OnServerOffer func(roomID string, peerID string, offer webrtc.SessionDescription)
|
|
||||||
|
|
||||||
// Вызывается при JoinWithOffer для ретрансляции RTP пакетов от издателя к другим участникам комнаты
|
// Вызывается при JoinWithOffer для ретрансляции RTP пакетов от издателя к другим участникам комнаты
|
||||||
func SetupForwardingForPeer(roomID string, publisherPeerID string, publisherPC *webrtc.PeerConnection) {
|
func SetupForwardingForPeer(roomID string, publisherPeerID string, publisherPC *webrtc.PeerConnection) {
|
||||||
publisherPC.OnTrack(func(remote *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
|
publisherPC.OnTrack(func(remote *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
|
||||||
@@ -91,8 +88,24 @@ func renegotiatePeer(roomID, peerID string, pc *webrtc.PeerConnection) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Java -> SFU: answer клиента на server-offer
|
// Добавляет ICE-кандидата к пиру
|
||||||
func HandleClientAnswer(roomID, peerID string, answer webrtc.SessionDescription) error {
|
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()
|
roomsMu.RLock()
|
||||||
room, ok := rooms[roomID]
|
room, ok := rooms[roomID]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
12
sfu/rooms.go
12
sfu/rooms.go
@@ -110,18 +110,20 @@ func JoinWithOffer(roomID string, peerID string, offer webrtc.SessionDescription
|
|||||||
return peerConnection.LocalDescription(), nil
|
return peerConnection.LocalDescription(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddICECandidate добавляет ICE-кандидата к пиру
|
// LeaveRoom позволяет пиру покинуть комнату
|
||||||
func AddICECandidate(roomID string, peerID string, candidate webrtc.ICECandidateInit) error {
|
func LeaveRoom(roomID string, peerID string) error {
|
||||||
room, exists := GetRoom(roomID)
|
room, exists := GetRoom(roomID)
|
||||||
if !exists {
|
if !exists {
|
||||||
return ErrRoomNotFound
|
return ErrRoomNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, peer := range room.Peers {
|
for i, peer := range room.Peers {
|
||||||
if peer.PeerID == peerID {
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ var (
|
|||||||
pcConfig webrtc.Configuration
|
pcConfig webrtc.Configuration
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Коллбек для обработки новых SDP офферов от сервера для конкретного пира (при renegotiation)
|
||||||
|
var OnServerOffer func(roomID string, peerID string, offer webrtc.SessionDescription)
|
||||||
|
|
||||||
// Коллбек для обработки новых ICE кандидатов
|
// Коллбек для обработки новых ICE кандидатов
|
||||||
var OnLocalICECandidate func(roomID, peerID string, candidate webrtc.ICECandidateInit)
|
var OnLocalICECandidate func(roomID, peerID string, candidate webrtc.ICECandidateInit)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user