1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-11-22 16:45:58 +01:00

Cache early ICE candidates.

This commit is contained in:
Juliusz Chroboczek 2020-05-22 23:07:38 +02:00
parent 57163c70a3
commit 4699c338e1
2 changed files with 65 additions and 9 deletions

View file

@ -654,7 +654,7 @@ func getDownConn(c *client, id string) *downConnection {
return conn return conn
} }
func getConn(c *client, id string) connection { func getConn(c *client, id string) iceConnection {
up := getUpConn(c, id) up := getUpConn(c, id)
if up != nil { if up != nil {
return up return up
@ -1053,6 +1053,11 @@ func gotOffer(c *client, id string, offer webrtc.SessionDescription, labels map[
up.labels = labels up.labels = labels
err = up.flushICECandidates()
if err != nil {
log.Printf("ICE: %v", err)
}
return c.write(clientMessage{ return c.write(clientMessage{
Type: "answer", Type: "answer",
Id: id, Id: id,
@ -1061,17 +1066,22 @@ func gotOffer(c *client, id string, offer webrtc.SessionDescription, labels map[
} }
func gotAnswer(c *client, id string, answer webrtc.SessionDescription) error { func gotAnswer(c *client, id string, answer webrtc.SessionDescription) error {
conn := getDownConn(c, id) down := getDownConn(c, id)
if conn == nil { if down == nil {
return protocolError("unknown id in answer") return protocolError("unknown id in answer")
} }
err := conn.pc.SetRemoteDescription(answer) err := down.pc.SetRemoteDescription(answer)
if err != nil { if err != nil {
return err return err
} }
for _, t := range conn.tracks { err = down.flushICECandidates()
activateDownTrack(conn, t) if err != nil {
log.Printf("ICE: %v", err)
}
for _, t := range down.tracks {
activateDownTrack(down, t)
} }
return nil return nil
} }
@ -1081,7 +1091,7 @@ func gotICE(c *client, candidate *webrtc.ICECandidateInit, id string) error {
if conn == nil { if conn == nil {
return errors.New("unknown id in ICE") return errors.New("unknown id in ICE")
} }
return conn.getPC().AddICECandidate(*candidate) return conn.addICECandidate(candidate)
} }
func (c *client) setRequested(requested map[string]uint32) error { func (c *client) setRequested(requested map[string]uint32) error {

50
conn.go
View file

@ -6,6 +6,7 @@
package main package main
import ( import (
"errors"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -16,8 +17,9 @@ import (
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v2"
) )
type connection interface { type iceConnection interface {
getPC() *webrtc.PeerConnection addICECandidate(candidate *webrtc.ICECandidateInit) error
flushICECandidates() error
} }
type upTrack struct { type upTrack struct {
@ -30,6 +32,7 @@ type upTrack struct {
lastPLI uint64 lastPLI uint64
lastSenderReport uint32 lastSenderReport uint32
lastSenderReportTime uint32 lastSenderReportTime uint32
iceCandidates []*webrtc.ICECandidateInit
localCh chan struct{} // signals that local has changed localCh chan struct{} // signals that local has changed
writerDone chan struct{} // closed when the loop dies writerDone chan struct{} // closed when the loop dies
@ -103,6 +106,35 @@ func (up *upConnection) getPC() *webrtc.PeerConnection {
return up.pc return up.pc
} }
func (up *upConnection) addICECandidate(candidate *webrtc.ICECandidateInit) error {
if up.pc.RemoteDescription() != nil {
return up.pc.AddICECandidate(*candidate)
}
up.iceCandidates = append(up.iceCandidates, candidate)
return nil
}
func flushICECandidates(pc *webrtc.PeerConnection, candidates []*webrtc.ICECandidateInit) error {
if pc.RemoteDescription() == nil {
return errors.New("flushICECandidates called in bad state")
}
var err error
for _, candidate := range candidates {
err2 := pc.AddICECandidate(*candidate)
if err == nil {
err = err2
}
}
return err
}
func (up *upConnection) flushICECandidates() error {
err := flushICECandidates(up.pc, up.iceCandidates)
up.iceCandidates = nil
return err
}
func getUpMid(pc *webrtc.PeerConnection, track *webrtc.Track) string { func getUpMid(pc *webrtc.PeerConnection, track *webrtc.Track) string {
for _, t := range pc.GetTransceivers() { for _, t := range pc.GetTransceivers() {
if t.Receiver() != nil && t.Receiver().Track() == track { if t.Receiver() != nil && t.Receiver().Track() == track {
@ -201,3 +233,17 @@ type downConnection struct {
func (down *downConnection) getPC() *webrtc.PeerConnection { func (down *downConnection) getPC() *webrtc.PeerConnection {
return down.pc return down.pc
} }
func (down *downConnection) addICECandidate(candidate *webrtc.ICECandidateInit) error {
if down.pc.RemoteDescription() != nil {
return down.pc.AddICECandidate(*candidate)
}
down.iceCandidates = append(down.iceCandidates, candidate)
return nil
}
func (down *downConnection) flushICECandidates() error {
err := flushICECandidates(down.pc, down.iceCandidates)
down.iceCandidates = nil
return err
}