Simplify and extend parseSDPFrag.
We now return a list of candidates instead of using a callback. Also return the ICE ufrag and pwd.
This commit is contained in:
+15
-9
@@ -346,27 +346,36 @@ func whipResourceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = parseSDPFrag(
|
||||
_, _, candidates, err := parseSDPFrag(
|
||||
http.MaxBytesReader(w, r.Body, sdpLimit),
|
||||
c.GotICECandidate,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("WHIP trickle ICE: %v", err)
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
for _, init := range candidates {
|
||||
err := c.GotICECandidate(init)
|
||||
if err != nil {
|
||||
log.Printf("WHIP candidate: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// RFC 8840
|
||||
func parseSDPFrag(r io.Reader, f func(webrtc.ICECandidateInit) error) error {
|
||||
func parseSDPFrag(r io.Reader) (string, string, []webrtc.ICECandidateInit, error) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
mLineIndex := -1
|
||||
var mid, ufrag []byte
|
||||
var mid, ufrag, pwd []byte
|
||||
var candidates []webrtc.ICECandidateInit
|
||||
for scanner.Scan() {
|
||||
l := scanner.Bytes()
|
||||
if bytes.HasPrefix(l, []byte("a=ice-ufrag:")) {
|
||||
ufrag = l[len("a=ice-ufrag:"):]
|
||||
} else if bytes.HasPrefix(l, []byte("a=ice-pwd:")) {
|
||||
pwd = l[len("a=ice-pwd:"):]
|
||||
} else if bytes.HasPrefix(l, []byte("m=")) {
|
||||
mLineIndex++
|
||||
mid = nil
|
||||
@@ -388,11 +397,8 @@ func parseSDPFrag(r io.Reader, f func(webrtc.ICECandidateInit) error) error {
|
||||
s := string(ufrag)
|
||||
init.UsernameFragment = &s
|
||||
}
|
||||
err := f(init)
|
||||
if err != nil {
|
||||
log.Printf("WHIP candidate: %v", err)
|
||||
}
|
||||
candidates = append(candidates, init)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return string(ufrag), string(pwd), candidates, nil
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package webserver
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
func TestParseSDPFrag(t *testing.T) {
|
||||
@@ -16,14 +14,13 @@ a=candidate:2930517337 1 udp 2113937151 1eaafdf1-4127-499f-90d4-8c35ea49d5e6.loc
|
||||
2024/09/30 00:07:41 {candidate:2930517337 1 udp 2113937151 1eaafdf1-4127-499f-90d4-8c35ea49d5e6.local 44360 typ host generation 0 ufrag FZ0m network-cost 999 0xc00062a580 0xc000620288 0xc00062a590}
|
||||
a=end-of-candidates`
|
||||
r := strings.NewReader(sdp)
|
||||
candidates := []webrtc.ICECandidateInit(nil)
|
||||
err := parseSDPFrag(r, func(c webrtc.ICECandidateInit) error {
|
||||
candidates = append(candidates, c)
|
||||
return nil
|
||||
})
|
||||
ufrag, pwd, candidates, err := parseSDPFrag(r)
|
||||
if err != nil {
|
||||
t.Errorf("parseSDPFrag: %v", err)
|
||||
}
|
||||
if ufrag != "FZ0m" || pwd != "NRT+gj1EhsEwMm9MA7ljzBRy" {
|
||||
t.Errorf("Ufrag %v, pwd %v", ufrag, pwd)
|
||||
}
|
||||
if len(candidates) != 1 {
|
||||
t.Errorf("Expected 1, got %v", candidates)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user