icetransportpolicy.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. import (
  5. "encoding/json"
  6. )
  7. // ICETransportPolicy defines the ICE candidate policy surface the
  8. // permitted candidates. Only these candidates are used for connectivity checks.
  9. type ICETransportPolicy int
  10. // ICEGatherPolicy is the ORTC equivalent of ICETransportPolicy
  11. type ICEGatherPolicy = ICETransportPolicy
  12. const (
  13. // ICETransportPolicyAll indicates any type of candidate is used.
  14. ICETransportPolicyAll ICETransportPolicy = iota
  15. // ICETransportPolicyRelay indicates only media relay candidates such
  16. // as candidates passing through a TURN server are used.
  17. ICETransportPolicyRelay
  18. )
  19. // This is done this way because of a linter.
  20. const (
  21. iceTransportPolicyRelayStr = "relay"
  22. iceTransportPolicyAllStr = "all"
  23. )
  24. // NewICETransportPolicy takes a string and converts it to ICETransportPolicy
  25. func NewICETransportPolicy(raw string) ICETransportPolicy {
  26. switch raw {
  27. case iceTransportPolicyRelayStr:
  28. return ICETransportPolicyRelay
  29. case iceTransportPolicyAllStr:
  30. return ICETransportPolicyAll
  31. default:
  32. return ICETransportPolicy(Unknown)
  33. }
  34. }
  35. func (t ICETransportPolicy) String() string {
  36. switch t {
  37. case ICETransportPolicyRelay:
  38. return iceTransportPolicyRelayStr
  39. case ICETransportPolicyAll:
  40. return iceTransportPolicyAllStr
  41. default:
  42. return ErrUnknownType.Error()
  43. }
  44. }
  45. // UnmarshalJSON parses the JSON-encoded data and stores the result
  46. func (t *ICETransportPolicy) UnmarshalJSON(b []byte) error {
  47. var val string
  48. if err := json.Unmarshal(b, &val); err != nil {
  49. return err
  50. }
  51. *t = NewICETransportPolicy(val)
  52. return nil
  53. }
  54. // MarshalJSON returns the JSON encoding
  55. func (t ICETransportPolicy) MarshalJSON() ([]byte, error) {
  56. return json.Marshal(t.String())
  57. }