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

5
ws.go
View File

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