github.com/pion/webrtc/v3@v3.2.24/icecandidatetype.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package webrtc 5 6 import ( 7 "fmt" 8 9 "github.com/pion/ice/v2" 10 ) 11 12 // ICECandidateType represents the type of the ICE candidate used. 13 type ICECandidateType int 14 15 const ( 16 // ICECandidateTypeHost indicates that the candidate is of Host type as 17 // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.1. A 18 // candidate obtained by binding to a specific port from an IP address on 19 // the host. This includes IP addresses on physical interfaces and logical 20 // ones, such as ones obtained through VPNs. 21 ICECandidateTypeHost ICECandidateType = iota + 1 22 23 // ICECandidateTypeSrflx indicates the the candidate is of Server 24 // Reflexive type as described 25 // https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A candidate type 26 // whose IP address and port are a binding allocated by a NAT for an ICE 27 // agent after it sends a packet through the NAT to a server, such as a 28 // STUN server. 29 ICECandidateTypeSrflx 30 31 // ICECandidateTypePrflx indicates that the candidate is of Peer 32 // Reflexive type. A candidate type whose IP address and port are a binding 33 // allocated by a NAT for an ICE agent after it sends a packet through the 34 // NAT to its peer. 35 ICECandidateTypePrflx 36 37 // ICECandidateTypeRelay indicates the the candidate is of Relay type as 38 // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A 39 // candidate type obtained from a relay server, such as a TURN server. 40 ICECandidateTypeRelay 41 ) 42 43 // This is done this way because of a linter. 44 const ( 45 iceCandidateTypeHostStr = "host" 46 iceCandidateTypeSrflxStr = "srflx" 47 iceCandidateTypePrflxStr = "prflx" 48 iceCandidateTypeRelayStr = "relay" 49 ) 50 51 // NewICECandidateType takes a string and converts it into ICECandidateType 52 func NewICECandidateType(raw string) (ICECandidateType, error) { 53 switch raw { 54 case iceCandidateTypeHostStr: 55 return ICECandidateTypeHost, nil 56 case iceCandidateTypeSrflxStr: 57 return ICECandidateTypeSrflx, nil 58 case iceCandidateTypePrflxStr: 59 return ICECandidateTypePrflx, nil 60 case iceCandidateTypeRelayStr: 61 return ICECandidateTypeRelay, nil 62 default: 63 return ICECandidateType(Unknown), fmt.Errorf("%w: %s", errICECandidateTypeUnknown, raw) 64 } 65 } 66 67 func (t ICECandidateType) String() string { 68 switch t { 69 case ICECandidateTypeHost: 70 return iceCandidateTypeHostStr 71 case ICECandidateTypeSrflx: 72 return iceCandidateTypeSrflxStr 73 case ICECandidateTypePrflx: 74 return iceCandidateTypePrflxStr 75 case ICECandidateTypeRelay: 76 return iceCandidateTypeRelayStr 77 default: 78 return ErrUnknownType.Error() 79 } 80 } 81 82 func getCandidateType(candidateType ice.CandidateType) (ICECandidateType, error) { 83 switch candidateType { 84 case ice.CandidateTypeHost: 85 return ICECandidateTypeHost, nil 86 case ice.CandidateTypeServerReflexive: 87 return ICECandidateTypeSrflx, nil 88 case ice.CandidateTypePeerReflexive: 89 return ICECandidateTypePrflx, nil 90 case ice.CandidateTypeRelay: 91 return ICECandidateTypeRelay, nil 92 default: 93 // NOTE: this should never happen[tm] 94 err := fmt.Errorf("%w: %s", errICEInvalidConvertCandidateType, candidateType.String()) 95 return ICECandidateType(Unknown), err 96 } 97 } 98 99 // MarshalText implements the encoding.TextMarshaler interface. 100 func (t ICECandidateType) MarshalText() ([]byte, error) { 101 return []byte(t.String()), nil 102 } 103 104 // UnmarshalText implements the encoding.TextUnmarshaler interface. 105 func (t *ICECandidateType) UnmarshalText(b []byte) error { 106 var err error 107 *t, err = NewICECandidateType(string(b)) 108 return err 109 }