Commit 49bcc342 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Queue negotiation if not in stable state.

If we send two offers back to back, the second answer will arive in
stable state, which will confuse us.  Instead, queue the second offer.
parent cf6c1203
......@@ -129,13 +129,20 @@ func (down *rtpDownTrack) SetCname(cname string) {
down.cname.Store(cname)
}
const (
negotiationUnneeded = iota
negotiationNeeded
negotiationRestartIce
)
type rtpDownConnection struct {
id string
pc *webrtc.PeerConnection
remote conn.Up
tracks []*rtpDownTrack
maxREMBBitrate *bitrate
iceCandidates []*webrtc.ICECandidateInit
id string
pc *webrtc.PeerConnection
remote conn.Up
tracks []*rtpDownTrack
maxREMBBitrate *bitrate
iceCandidates []*webrtc.ICECandidateInit
negotiationNeeded int
}
func newDownConn(c group.Client, id string, remote conn.Up) (*rtpDownConnection, error) {
......
......@@ -423,6 +423,18 @@ func addDownTrack(c *webClient, conn *rtpDownConnection, remoteTrack conn.UpTrac
}
func negotiate(c *webClient, down *rtpDownConnection, renegotiate, restartIce bool) error {
if down.pc.SignalingState() == webrtc.SignalingStateHaveLocalOffer {
// avoid sending multiple offers back-to-back
if restartIce {
down.negotiationNeeded = negotiationRestartIce
} else if down.negotiationNeeded == negotiationUnneeded {
down.negotiationNeeded = negotiationNeeded
}
return nil
}
down.negotiationNeeded = negotiationUnneeded
options := webrtc.OfferOptions{ICERestart: restartIce}
offer, err := down.pc.CreateOffer(&options)
if err != nil {
......@@ -808,6 +820,9 @@ func clientLoop(c *webClient, ws *websocket.Conn) error {
if down != nil {
err = negotiate(c, down, false, false)
if err != nil {
log.Printf(
"Negotiation failed: %v",
err)
delDownConn(c, down.id)
c.error(group.UserError(
"Negotiation failed",
......@@ -1154,13 +1169,26 @@ func handleClientMessage(c *webClient, m clientMessage) error {
}
return failDownConnection(c, m.Id, message)
}
down := getDownConn(c, m.Id)
if down.negotiationNeeded > negotiationUnneeded {
err := negotiate(
c, down, true,
down.negotiationNeeded == negotiationRestartIce,
)
if err != nil {
return failDownConnection(
c, m.Id, "negotiation failed",
)
}
}
case "renegotiate":
down := getDownConn(c, m.Id)
if down != nil {
err := negotiate(c, down, true, true)
if err != nil {
return failDownConnection(c, m.Id,
"renegotiation failed")
return failDownConnection(
c, m.Id, "renegotiation failed",
)
}
} else {
log.Printf("Trying to renegotiate unknown connection")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment