icetransportpolicy.go 1.7 KB

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