diff --git a/boot/boot.go b/boot/boot.go index 726b046..aac3aba 100644 --- a/boot/boot.go +++ b/boot/boot.go @@ -27,10 +27,12 @@ func Bootstrap() { port = "1001" } sfu.OnLocalICECandidate = OnLocalICECandidate + sfu.OnServerOffer = OnServerOffer logger.LogInfoMessage("server started at x.x.x.x:" + port) http.ListenAndServe(":"+port, nil) } +// Коллбек для обработки новых ICE кандидатов от сервера к пиру func OnLocalICECandidate(roomID string, peerID string, candidate webrtc.ICECandidateInit) { room, exists := sfu.GetRoom(roomID) if !exists { @@ -52,6 +54,24 @@ func OnLocalICECandidate(roomID string, peerID string, candidate webrtc.ICECandi room.Server.Socket.WriteMessage(websocket.BinaryMessage, buffer.Bytes()) } +// Обработка нового оффера от сервера для конкретного пира (при renegotiation) func OnServerOffer(roomID string, peerID string, offer webrtc.SessionDescription) { - logger.LogInfoMessage("new server offer for peer " + peerID + " in room " + roomID) + room, exists := sfu.GetRoom(roomID) + if !exists { + logger.LogWarnMessage("tried to send server offer to non existing room " + roomID) + return + } + jsonOffer, _ := json.Marshal(offer) + buffer := bytebuffer.Allocate( + 1 + 4 + len([]byte(roomID)) + 4 + len([]byte(peerID)) + 4 + len(jsonOffer), + ) + buffer.Put(0x08) + buffer.PutUint32(uint32(len([]byte(roomID)))) + buffer.PutBytes([]byte(roomID)) + buffer.PutUint32(uint32(len([]byte(peerID)))) + buffer.PutBytes([]byte(peerID)) + buffer.PutUint32(uint32(len([]byte(jsonOffer)))) + buffer.PutBytes([]byte(jsonOffer)) + buffer.Flip() + room.Server.Socket.WriteMessage(websocket.BinaryMessage, buffer.Bytes()) } diff --git a/socket/socket.go b/socket/socket.go index 9bdc459..a132348 100644 --- a/socket/socket.go +++ b/socket/socket.go @@ -157,5 +157,51 @@ func processData(data <-chan []byte, connection *connection.Connection) { connection.Socket.WriteMessage(websocket.BinaryMessage, response.Bytes()) continue } + + //ICE-candidate пришел от участника комнаты + if packetId == 0x06 { + roomIdLen, _ := buffer.GetUint32() + roomIDBytes, _ := buffer.GetBytes(int(roomIdLen)) + roomID := string(roomIDBytes) + peerIdLen, _ := buffer.GetUint32() + peerIDBytes, _ := buffer.GetBytes(int(peerIdLen)) + peerID := string(peerIDBytes) + candidateLength, _ := buffer.GetUint32() + candidateBytes, _ := buffer.GetBytes(int(candidateLength)) + var candidate webrtc.ICECandidateInit + err := json.Unmarshal(candidateBytes, &candidate) + if err != nil { + logger.LogWarnMessage("failed to unmarshal ICE candidate from peer " + peerID + " in room " + roomID + ": " + err.Error()) + continue + } + err = sfu.AddICECandidate(roomID, peerID, candidate) + if err != nil { + logger.LogWarnMessage("failed to add ICE candidate from peer " + peerID + " in room " + roomID + ": " + err.Error()) + continue + } + } + + // SDP ANSWER от клиента при renegotiation + if packetId == 0x07 { + roomIdLen, _ := buffer.GetUint32() + roomIDBytes, _ := buffer.GetBytes(int(roomIdLen)) + roomID := string(roomIDBytes) + peerIdLen, _ := buffer.GetUint32() + peerIDBytes, _ := buffer.GetBytes(int(peerIdLen)) + peerID := string(peerIDBytes) + answerLength, _ := buffer.GetUint32() + answerBytes, _ := buffer.GetBytes(int(answerLength)) + var answer webrtc.SessionDescription + err := json.Unmarshal(answerBytes, &answer) + if err != nil { + logger.LogWarnMessage("failed to unmarshal answer from peer " + peerID + " in room " + roomID + ": " + err.Error()) + continue + } + err = sfu.HandleClientAnswer(roomID, peerID, answer) + if err != nil { + logger.LogWarnMessage("failed to handle client answer from peer " + peerID + " in room " + roomID + ": " + err.Error()) + continue + } + } } }