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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = parseSDPFrag(
|
_, _, candidates, err := parseSDPFrag(
|
||||||
http.MaxBytesReader(w, r.Body, sdpLimit),
|
http.MaxBytesReader(w, r.Body, sdpLimit),
|
||||||
c.GotICECandidate,
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("WHIP trickle ICE: %v", err)
|
log.Printf("WHIP trickle ICE: %v", err)
|
||||||
http.Error(w, "bad request", http.StatusBadRequest)
|
http.Error(w, "bad request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
for _, init := range candidates {
|
||||||
|
err := c.GotICECandidate(init)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("WHIP candidate: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RFC 8840
|
// 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)
|
scanner := bufio.NewScanner(r)
|
||||||
mLineIndex := -1
|
mLineIndex := -1
|
||||||
var mid, ufrag []byte
|
var mid, ufrag, pwd []byte
|
||||||
|
var candidates []webrtc.ICECandidateInit
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
l := scanner.Bytes()
|
l := scanner.Bytes()
|
||||||
if bytes.HasPrefix(l, []byte("a=ice-ufrag:")) {
|
if bytes.HasPrefix(l, []byte("a=ice-ufrag:")) {
|
||||||
ufrag = l[len("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=")) {
|
} else if bytes.HasPrefix(l, []byte("m=")) {
|
||||||
mLineIndex++
|
mLineIndex++
|
||||||
mid = nil
|
mid = nil
|
||||||
@@ -388,11 +397,8 @@ func parseSDPFrag(r io.Reader, f func(webrtc.ICECandidateInit) error) error {
|
|||||||
s := string(ufrag)
|
s := string(ufrag)
|
||||||
init.UsernameFragment = &s
|
init.UsernameFragment = &s
|
||||||
}
|
}
|
||||||
err := f(init)
|
candidates = append(candidates, init)
|
||||||
if err != nil {
|
|
||||||
log.Printf("WHIP candidate: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return string(ufrag), string(pwd), candidates, nil
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package webserver
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pion/webrtc/v4"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParseSDPFrag(t *testing.T) {
|
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}
|
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`
|
a=end-of-candidates`
|
||||||
r := strings.NewReader(sdp)
|
r := strings.NewReader(sdp)
|
||||||
candidates := []webrtc.ICECandidateInit(nil)
|
ufrag, pwd, candidates, err := parseSDPFrag(r)
|
||||||
err := parseSDPFrag(r, func(c webrtc.ICECandidateInit) error {
|
|
||||||
candidates = append(candidates, c)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("parseSDPFrag: %v", err)
|
t.Errorf("parseSDPFrag: %v", err)
|
||||||
}
|
}
|
||||||
|
if ufrag != "FZ0m" || pwd != "NRT+gj1EhsEwMm9MA7ljzBRy" {
|
||||||
|
t.Errorf("Ufrag %v, pwd %v", ufrag, pwd)
|
||||||
|
}
|
||||||
if len(candidates) != 1 {
|
if len(candidates) != 1 {
|
||||||
t.Errorf("Expected 1, got %v", candidates)
|
t.Errorf("Expected 1, got %v", candidates)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user