Respond to offers

This commit is contained in:
Pierre HUBERT 2020-04-11 18:20:46 +02:00
parent d2d2dedf92
commit ea73efb0b0
2 changed files with 43 additions and 17 deletions

View File

@ -22,6 +22,9 @@ const (
// CANDIDATE This is a candidate // CANDIDATE This is a candidate
CANDIDATE = iota CANDIDATE = iota
// RequestOffer for a broadcast receiver
RequestOffer = iota
// CloseConnection Requests the connection to be closed // CloseConnection Requests the connection to be closed
CloseConnection = iota CloseConnection = iota
) )
@ -30,7 +33,7 @@ type receivedSignal struct {
peerID string peerID string
callHash string callHash string
sigType uint sigType uint
offer webrtc.SessionDescription sdp webrtc.SessionDescription
candidate webrtc.ICECandidateInit candidate webrtc.ICECandidateInit
} }
@ -70,7 +73,7 @@ func onSignal(callHash, peerID string, data map[string]interface{}) {
return return
} }
err = json.Unmarshal(enc, &newSignal.offer) err = json.Unmarshal(enc, &newSignal.sdp)
if err != nil { if err != nil {
log.Printf("Discarding invalid candidate: %s", err) log.Printf("Discarding invalid candidate: %s", err)
return return
@ -93,6 +96,11 @@ func onSignal(callHash, peerID string, data map[string]interface{}) {
return return
} }
} else if data["type"] == "REQUEST_OFFER" {
// Request an offer
newSignal.sigType = RequestOffer
} else if data["type"] == "CLOSE_CONN" { } else if data["type"] == "CLOSE_CONN" {
// Close connection // Close connection
@ -132,6 +140,13 @@ func onSignal(callHash, peerID string, data map[string]interface{}) {
} }
} }
/// Request an offer for a client of a broadcast
func onRequestOffer(callHash, peerID string) {
onSignal(callHash, peerID, map[string]interface{}{
"type": "REQUEST_OFFER",
})
}
/// Request connections to be closed /// Request connections to be closed
func onCloseConnection(callHash, peerID string) { func onCloseConnection(callHash, peerID string) {
onSignal(callHash, peerID, map[string]interface{}{ onSignal(callHash, peerID, map[string]interface{}{
@ -173,7 +188,7 @@ func newCall(mainOffer receivedSignal, r activeRelay) {
// Since we are answering use PayloadTypes declared by offerer // Since we are answering use PayloadTypes declared by offerer
mediaEngine := webrtc.MediaEngine{} mediaEngine := webrtc.MediaEngine{}
err := mediaEngine.PopulateFromSDP(mainOffer.offer) err := mediaEngine.PopulateFromSDP(mainOffer.sdp)
if err != nil { if err != nil {
log.Println("Error: invalid data in offer!", err) log.Println("Error: invalid data in offer!", err)
askForClose(r) askForClose(r)
@ -271,7 +286,7 @@ func newCall(mainOffer receivedSignal, r activeRelay) {
}) })
// Set the remote SessionDescription // Set the remote SessionDescription
err = mainPeerConnection.SetRemoteDescription(mainOffer.offer) err = mainPeerConnection.SetRemoteDescription(mainOffer.sdp)
if err != nil { if err != nil {
log.Println("Set remote description error!", err) log.Println("Set remote description error!", err)
askForClose(r) askForClose(r)
@ -339,7 +354,7 @@ func newCall(mainOffer receivedSignal, r activeRelay) {
} }
// Check if we are creating a new connection // Check if we are creating a new connection
if newMessage.sigType == SDP { if newMessage.sigType == RequestOffer {
// Close any previous connection of this client // Close any previous connection of this client
if val, ok := clients[newMessage.peerID]; ok { if val, ok := clients[newMessage.peerID]; ok {
@ -363,21 +378,14 @@ func newCall(mainOffer receivedSignal, r activeRelay) {
} }
} }
// Set remote description // Create the offer
err = newPeerConnection.SetRemoteDescription(newMessage.offer) offer, err := newPeerConnection.CreateOffer(nil)
if err != nil {
log.Printf("Could not set remote description (remote peer): %s", err)
continue
}
// Create the answer
answer, err := newPeerConnection.CreateAnswer(nil)
if err != nil { if err != nil {
log.Printf("Could not create answer: %s!", err) log.Printf("Could not create answer: %s!", err)
continue continue
} }
err = newPeerConnection.SetLocalDescription(answer) err = newPeerConnection.SetLocalDescription(offer)
if err != nil { if err != nil {
log.Printf("Could not set local description: %s!", err) log.Printf("Could not set local description: %s!", err)
continue continue
@ -390,8 +398,21 @@ func newCall(mainOffer receivedSignal, r activeRelay) {
} }
}) })
// Send answer // Send offer
sendSignal(r.callHash, newMessage.peerID, answer) sendSignal(r.callHash, newMessage.peerID, offer)
} else if newMessage.sigType == SDP {
// Got an answer from the client
if val, ok := clients[newMessage.peerID]; ok {
// Set remote description
if err := val.SetRemoteDescription(newMessage.sdp); err != nil {
log.Printf("Could not set remote description! %s", err)
}
} else {
log.Printf("Dropped un-usable answer: callHash %s / peer ID %s", newMessage.callHash, newMessage.peerID)
}
} else if newMessage.sigType == CANDIDATE { } else if newMessage.sigType == CANDIDATE {

5
ws.go
View File

@ -82,6 +82,11 @@ func openWs(conf *Config) {
onSignal(msg.CallHash, msg.PeerID, msg.Data.(map[string]interface{})) onSignal(msg.CallHash, msg.PeerID, msg.Data.(map[string]interface{}))
break break
// Request an offer
case "request_offer":
onRequestOffer(msg.CallHash, msg.PeerID)
break
// Close a connection // Close a connection
case "close_conn": case "close_conn":
onCloseConnection(msg.CallHash, msg.PeerID) onCloseConnection(msg.CallHash, msg.PeerID)