github.com/pion/webrtc/v4@v4.0.1/icetransportstate.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package webrtc
     5  
     6  import "github.com/pion/ice/v4"
     7  
     8  // ICETransportState represents the current state of the ICE transport.
     9  type ICETransportState int
    10  
    11  const (
    12  	// ICETransportStateUnknown is the enum's zero-value
    13  	ICETransportStateUnknown ICETransportState = iota
    14  
    15  	// ICETransportStateNew indicates the ICETransport is waiting
    16  	// for remote candidates to be supplied.
    17  	ICETransportStateNew
    18  
    19  	// ICETransportStateChecking indicates the ICETransport has
    20  	// received at least one remote candidate, and a local and remote
    21  	// ICECandidateComplete dictionary was not added as the last candidate.
    22  	ICETransportStateChecking
    23  
    24  	// ICETransportStateConnected indicates the ICETransport has
    25  	// received a response to an outgoing connectivity check, or has
    26  	// received incoming DTLS/media after a successful response to an
    27  	// incoming connectivity check, but is still checking other candidate
    28  	// pairs to see if there is a better connection.
    29  	ICETransportStateConnected
    30  
    31  	// ICETransportStateCompleted indicates the ICETransport tested
    32  	// all appropriate candidate pairs and at least one functioning
    33  	// candidate pair has been found.
    34  	ICETransportStateCompleted
    35  
    36  	// ICETransportStateFailed indicates the ICETransport the last
    37  	// candidate was added and all appropriate candidate pairs have either
    38  	// failed connectivity checks or have lost consent.
    39  	ICETransportStateFailed
    40  
    41  	// ICETransportStateDisconnected indicates the ICETransport has received
    42  	// at least one local and remote candidate, but the final candidate was
    43  	// received yet and all appropriate candidate pairs thus far have been
    44  	// tested and failed.
    45  	ICETransportStateDisconnected
    46  
    47  	// ICETransportStateClosed indicates the ICETransport has shut down
    48  	// and is no longer responding to STUN requests.
    49  	ICETransportStateClosed
    50  )
    51  
    52  const (
    53  	iceTransportStateNewStr          = "new"
    54  	iceTransportStateCheckingStr     = "checking"
    55  	iceTransportStateConnectedStr    = "connected"
    56  	iceTransportStateCompletedStr    = "completed"
    57  	iceTransportStateFailedStr       = "failed"
    58  	iceTransportStateDisconnectedStr = "disconnected"
    59  	iceTransportStateClosedStr       = "closed"
    60  )
    61  
    62  func newICETransportState(raw string) ICETransportState {
    63  	switch raw {
    64  	case iceTransportStateNewStr:
    65  		return ICETransportStateNew
    66  	case iceTransportStateCheckingStr:
    67  		return ICETransportStateChecking
    68  	case iceTransportStateConnectedStr:
    69  		return ICETransportStateConnected
    70  	case iceTransportStateCompletedStr:
    71  		return ICETransportStateCompleted
    72  	case iceTransportStateFailedStr:
    73  		return ICETransportStateFailed
    74  	case iceTransportStateDisconnectedStr:
    75  		return ICETransportStateDisconnected
    76  	case iceTransportStateClosedStr:
    77  		return ICETransportStateClosed
    78  	default:
    79  		return ICETransportStateUnknown
    80  	}
    81  }
    82  
    83  func (c ICETransportState) String() string {
    84  	switch c {
    85  	case ICETransportStateNew:
    86  		return iceTransportStateNewStr
    87  	case ICETransportStateChecking:
    88  		return iceTransportStateCheckingStr
    89  	case ICETransportStateConnected:
    90  		return iceTransportStateConnectedStr
    91  	case ICETransportStateCompleted:
    92  		return iceTransportStateCompletedStr
    93  	case ICETransportStateFailed:
    94  		return iceTransportStateFailedStr
    95  	case ICETransportStateDisconnected:
    96  		return iceTransportStateDisconnectedStr
    97  	case ICETransportStateClosed:
    98  		return iceTransportStateClosedStr
    99  	default:
   100  		return ErrUnknownType.Error()
   101  	}
   102  }
   103  
   104  func newICETransportStateFromICE(i ice.ConnectionState) ICETransportState {
   105  	switch i {
   106  	case ice.ConnectionStateNew:
   107  		return ICETransportStateNew
   108  	case ice.ConnectionStateChecking:
   109  		return ICETransportStateChecking
   110  	case ice.ConnectionStateConnected:
   111  		return ICETransportStateConnected
   112  	case ice.ConnectionStateCompleted:
   113  		return ICETransportStateCompleted
   114  	case ice.ConnectionStateFailed:
   115  		return ICETransportStateFailed
   116  	case ice.ConnectionStateDisconnected:
   117  		return ICETransportStateDisconnected
   118  	case ice.ConnectionStateClosed:
   119  		return ICETransportStateClosed
   120  	default:
   121  		return ICETransportStateUnknown
   122  	}
   123  }
   124  
   125  func (c ICETransportState) toICE() ice.ConnectionState {
   126  	switch c {
   127  	case ICETransportStateNew:
   128  		return ice.ConnectionStateNew
   129  	case ICETransportStateChecking:
   130  		return ice.ConnectionStateChecking
   131  	case ICETransportStateConnected:
   132  		return ice.ConnectionStateConnected
   133  	case ICETransportStateCompleted:
   134  		return ice.ConnectionStateCompleted
   135  	case ICETransportStateFailed:
   136  		return ice.ConnectionStateFailed
   137  	case ICETransportStateDisconnected:
   138  		return ice.ConnectionStateDisconnected
   139  	case ICETransportStateClosed:
   140  		return ice.ConnectionStateClosed
   141  	default:
   142  		return ice.ConnectionStateUnknown
   143  	}
   144  }
   145  
   146  // MarshalText implements encoding.TextMarshaler
   147  func (c ICETransportState) MarshalText() ([]byte, error) {
   148  	return []byte(c.String()), nil
   149  }
   150  
   151  // UnmarshalText implements encoding.TextUnmarshaler
   152  func (c *ICETransportState) UnmarshalText(b []byte) error {
   153  	*c = newICETransportState(string(b))
   154  	return nil
   155  }