github.com/pion/webrtc/v4@v4.0.1/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  	default:
    38  		return ICETransportPolicyAll
    39  	}
    40  }
    41  
    42  func (t ICETransportPolicy) String() string {
    43  	switch t {
    44  	case ICETransportPolicyRelay:
    45  		return iceTransportPolicyRelayStr
    46  	case ICETransportPolicyAll:
    47  		return iceTransportPolicyAllStr
    48  	default:
    49  		return ErrUnknownType.Error()
    50  	}
    51  }
    52  
    53  // UnmarshalJSON parses the JSON-encoded data and stores the result
    54  func (t *ICETransportPolicy) UnmarshalJSON(b []byte) error {
    55  	var val string
    56  	if err := json.Unmarshal(b, &val); err != nil {
    57  		return err
    58  	}
    59  	*t = NewICETransportPolicy(val)
    60  	return nil
    61  }
    62  
    63  // MarshalJSON returns the JSON encoding
    64  func (t ICETransportPolicy) MarshalJSON() ([]byte, error) {
    65  	return json.Marshal(t.String())
    66  }