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