telegabber/xmpp/jingle/candidate_pion.go
2026-05-25 06:27:20 -07:00

66 lines
1.5 KiB
Go

package jingle
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/pion/webrtc/v4"
)
// strips the "candidate:" prefix so candidateFromSDP gets the same shape
// it does on SDP import
func pionToCandidate(c *webrtc.ICECandidate) (*Candidate, error) {
if c == nil {
return nil, errors.New("nil ICECandidate")
}
init := c.ToJSON()
body := init.Candidate
body = strings.TrimPrefix(body, "candidate:")
cand, err := candidateFromSDP(body)
if err != nil {
return nil, err
}
if cand == nil {
return nil, fmt.Errorf("unrecognised pion candidate %q", init.Candidate)
}
return cand, nil
}
// fills SDPMid and parsed SDPMLineIndex when mid is numeric
func candidateToPionInit(c *Candidate, mid string) (webrtc.ICECandidateInit, error) {
sdpBody, err := candidateToSDP(c)
if err != nil {
return webrtc.ICECandidateInit{}, err
}
init := webrtc.ICECandidateInit{
Candidate: "candidate:" + sdpBody,
}
if mid != "" {
m := mid
init.SDPMid = &m
}
if n, err := strconv.Atoi(mid); err == nil && n >= 0 && n <= 0xffff {
idx := uint16(n)
init.SDPMLineIndex = &idx
}
return init, nil
}
// initiator/responder stay unset; Session stamps them per role before send
func buildTransportInfo(sid, mid, ufrag, pwd, creator string, cand *Candidate) *JingleIQ {
return &JingleIQ{
Action: ActionTransportInfo,
SID: sid,
Contents: []Content{{
Creator: creator,
Name: mid,
Transport: &Transport{
Ufrag: ufrag,
Pwd: pwd,
Candidates: []Candidate{*cand},
},
}},
}
}