github.com/pion/webrtc/v4@v4.0.1/iceprotocol.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  	"strings"
     9  )
    10  
    11  // ICEProtocol indicates the transport protocol type that is used in the
    12  // ice.URL structure.
    13  type ICEProtocol int
    14  
    15  const (
    16  	// ICEProtocolUnknown is the enum's zero-value
    17  	ICEProtocolUnknown ICEProtocol = iota
    18  
    19  	// ICEProtocolUDP indicates the URL uses a UDP transport.
    20  	ICEProtocolUDP
    21  
    22  	// ICEProtocolTCP indicates the URL uses a TCP transport.
    23  	ICEProtocolTCP
    24  )
    25  
    26  // This is done this way because of a linter.
    27  const (
    28  	iceProtocolUDPStr = "udp"
    29  	iceProtocolTCPStr = "tcp"
    30  )
    31  
    32  // NewICEProtocol takes a string and converts it to ICEProtocol
    33  func NewICEProtocol(raw string) (ICEProtocol, error) {
    34  	switch {
    35  	case strings.EqualFold(iceProtocolUDPStr, raw):
    36  		return ICEProtocolUDP, nil
    37  	case strings.EqualFold(iceProtocolTCPStr, raw):
    38  		return ICEProtocolTCP, nil
    39  	default:
    40  		return ICEProtocolUnknown, fmt.Errorf("%w: %s", errICEProtocolUnknown, raw)
    41  	}
    42  }
    43  
    44  func (t ICEProtocol) String() string {
    45  	switch t {
    46  	case ICEProtocolUDP:
    47  		return iceProtocolUDPStr
    48  	case ICEProtocolTCP:
    49  		return iceProtocolTCPStr
    50  	default:
    51  		return ErrUnknownType.Error()
    52  	}
    53  }