iceprotocol.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. import (
  5. "fmt"
  6. "strings"
  7. )
  8. // ICEProtocol indicates the transport protocol type that is used in the
  9. // ice.URL structure.
  10. type ICEProtocol int
  11. const (
  12. // ICEProtocolUDP indicates the URL uses a UDP transport.
  13. ICEProtocolUDP ICEProtocol = iota + 1
  14. // ICEProtocolTCP indicates the URL uses a TCP transport.
  15. ICEProtocolTCP
  16. )
  17. // This is done this way because of a linter.
  18. const (
  19. iceProtocolUDPStr = "udp"
  20. iceProtocolTCPStr = "tcp"
  21. )
  22. // NewICEProtocol takes a string and converts it to ICEProtocol
  23. func NewICEProtocol(raw string) (ICEProtocol, error) {
  24. switch {
  25. case strings.EqualFold(iceProtocolUDPStr, raw):
  26. return ICEProtocolUDP, nil
  27. case strings.EqualFold(iceProtocolTCPStr, raw):
  28. return ICEProtocolTCP, nil
  29. default:
  30. return ICEProtocol(Unknown), fmt.Errorf("%w: %s", errICEProtocolUnknown, raw)
  31. }
  32. }
  33. func (t ICEProtocol) String() string {
  34. switch t {
  35. case ICEProtocolUDP:
  36. return iceProtocolUDPStr
  37. case ICEProtocolTCP:
  38. return iceProtocolTCPStr
  39. default:
  40. return ErrUnknownType.Error()
  41. }
  42. }