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