1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
package webrtc
import (
"fmt"
"github.com/pion/ice/v2"
)
// ICECandidate represents a ice candidate
type ICECandidate struct {
statsID string
Foundation string `json:"foundation"`
Priority uint32 `json:"priority"`
Address string `json:"address"`
Protocol ICEProtocol `json:"protocol"`
Port uint16 `json:"port"`
Typ ICECandidateType `json:"type"`
Component uint16 `json:"component"`
RelatedAddress string `json:"relatedAddress"`
RelatedPort uint16 `json:"relatedPort"`
TCPType string `json:"tcpType"`
}
// Conversion for package ice
func newICECandidatesFromICE(iceCandidates []ice.Candidate) ([]ICECandidate, error) {
candidates := []ICECandidate{}
for _, i := range iceCandidates {
c, err := newICECandidateFromICE(i)
if err != nil {
return nil, err
}
candidates = append(candidates, c)
}
return candidates, nil
}
func newICECandidateFromICE(i ice.Candidate) (ICECandidate, error) {
typ, err := convertTypeFromICE(i.Type())
if err != nil {
return ICECandidate{}, err
}
protocol, err := NewICEProtocol(i.NetworkType().NetworkShort())
if err != nil {
return ICECandidate{}, err
}
c := ICECandidate{
statsID: i.ID(),
Foundation: i.Foundation(),
Priority: i.Priority(),
Address: i.Address(),
Protocol: protocol,
Port: uint16(i.Port()),
Component: i.Component(),
Typ: typ,
TCPType: i.TCPType().String(),
}
if i.RelatedAddress() != nil {
c.RelatedAddress = i.RelatedAddress().Address
c.RelatedPort = uint16(i.RelatedAddress().Port)
}
return c, nil
}
func (c ICECandidate) toICE() (ice.Candidate, error) {
candidateID := c.statsID
switch c.Typ {
case ICECandidateTypeHost:
config := ice.CandidateHostConfig{
CandidateID: candidateID,
Network: c.Protocol.String(),
Address: c.Address,
Port: int(c.Port),
Component: c.Component,
TCPType: ice.NewTCPType(c.TCPType),
Foundation: c.Foundation,
Priority: c.Priority,
}
return ice.NewCandidateHost(&config)
case ICECandidateTypeSrflx:
config := ice.CandidateServerReflexiveConfig{
CandidateID: candidateID,
Network: c.Protocol.String(),
Address: c.Address,
Port: int(c.Port),
Component: c.Component,
Foundation: c.Foundation,
Priority: c.Priority,
RelAddr: c.RelatedAddress,
RelPort: int(c.RelatedPort),
}
return ice.NewCandidateServerReflexive(&config)
case ICECandidateTypePrflx:
config := ice.CandidatePeerReflexiveConfig{
CandidateID: candidateID,
Network: c.Protocol.String(),
Address: c.Address,
Port: int(c.Port),
Component: c.Component,
Foundation: c.Foundation,
Priority: c.Priority,
RelAddr: c.RelatedAddress,
RelPort: int(c.RelatedPort),
}
return ice.NewCandidatePeerReflexive(&config)
case ICECandidateTypeRelay:
config := ice.CandidateRelayConfig{
CandidateID: candidateID,
Network: c.Protocol.String(),
Address: c.Address,
Port: int(c.Port),
Component: c.Component,
Foundation: c.Foundation,
Priority: c.Priority,
RelAddr: c.RelatedAddress,
RelPort: int(c.RelatedPort),
}
return ice.NewCandidateRelay(&config)
default:
return nil, fmt.Errorf("%w: %s", errICECandidateTypeUnknown, c.Typ)
}
}
func convertTypeFromICE(t ice.CandidateType) (ICECandidateType, error) {
switch t {
case ice.CandidateTypeHost:
return ICECandidateTypeHost, nil
case ice.CandidateTypeServerReflexive:
return ICECandidateTypeSrflx, nil
case ice.CandidateTypePeerReflexive:
return ICECandidateTypePrflx, nil
case ice.CandidateTypeRelay:
return ICECandidateTypeRelay, nil
default:
return ICECandidateType(t), fmt.Errorf("%w: %s", errICECandidateTypeUnknown, t)
}
}
func (c ICECandidate) String() string {
ic, err := c.toICE()
if err != nil {
return fmt.Sprintf("%#v failed to convert to ICE: %s", c, err)
}
return ic.String()
}
// ToJSON returns an ICECandidateInit
// as indicated by the spec https://w3c.github.io/webrtc-pc/#dom-rtcicecandidate-tojson
func (c ICECandidate) ToJSON() ICECandidateInit {
zeroVal := uint16(0)
emptyStr := ""
candidateStr := ""
candidate, err := c.toICE()
if err == nil {
candidateStr = candidate.Marshal()
}
return ICECandidateInit{
Candidate: fmt.Sprintf("candidate:%s", candidateStr),
SDPMid: &emptyStr,
SDPMLineIndex: &zeroVal,
}
}
|