iceprotocol.go 1.0 KB

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