From 2838b1a836499ddbc8b2aae23ddb0fa1801207a5 Mon Sep 17 00:00:00 2001 From: Sean DuBois Date: Sat, 15 Jun 2019 00:02:49 -0700 Subject: [PATCH] Move ICE code out of internal Users are unable to use the callbacks inside `internal/ice`. Even though we alias things like OnSelectedCandidatePairChange are unusable since in the package they use `ice.Candidate` instead of `ICECandidate` --- .travis.yml | 4 +- errors.go | 8 + ice.go | 289 ------------------ ice_go.go | 30 +- internal/ice/candidate.go => icecandidate.go | 64 ++-- .../candidate_test.go => icecandidate_test.go | 36 +-- .../candidateinit.go => icecandidateinit.go | 6 +- ...teinit_test.go => icecandidateinit_test.go | 12 +- icecandidatepair.go | 24 ++ icecandidatetype.go | 73 +++++ icecandidatetype_test.go | 54 ++++ icecomponent.go | 47 +++ .../component_test.go => icecomponent_test.go | 24 +- iceconnectionstate.go | 94 ++++++ iceconnectionstate_test.go | 55 ++++ icecredentialtype.go | 43 +++ ...ltype_test.go => icecredentialtype_test.go | 20 +- internal/ice/gatherer.go => icegatherer.go | 75 +++-- .../gatherer_test.go => icegatherer_test.go | 12 +- icegathererstate.go | 36 +++ icegathererstate_test.go | 28 ++ icegatheringstate.go | 53 ++++ icegatheringstate_test.go | 47 +++ icegatheroptions.go | 7 + .../ice/parameters.go => iceparameters.go | 6 +- iceprotocol.go | 47 +++ .../protocol_test.go => iceprotocol_test.go | 28 +- icerole.go | 45 +++ internal/ice/role_test.go => icerole_test.go | 24 +- internal/ice/server.go => iceserver.go | 19 +- internal/ice/server_js.go => iceserver_js.go | 22 +- .../ice/server_test.go => iceserver_test.go | 36 +-- internal/ice/transport.go => icetransport.go | 85 +++--- icetransportpolicy.go | 46 +++ ...licy_test.go => icetransportpolicy_test.go | 20 +- .../transportstate.go => icetransportstate.go | 84 ++--- icetransportstate_test.go | 61 ++++ internal/ice/candidatepair.go | 24 -- internal/ice/candidatetype.go | 73 ----- internal/ice/candidatetype_test.go | 54 ---- internal/ice/component.go | 47 --- internal/ice/connectionstate.go | 94 ------ internal/ice/connectionstate_test.go | 55 ---- internal/ice/constants.go | 10 - internal/ice/credentialtype.go | 43 --- internal/ice/errors.go | 16 - internal/ice/gathererstate.go | 36 --- internal/ice/gathererstate_test.go | 28 -- internal/ice/gatheringstate.go | 53 ---- internal/ice/gatheringstate_test.go | 47 --- internal/ice/gatheroptions.go | 7 - internal/ice/protocol.go | 47 --- internal/ice/role.go | 45 --- internal/ice/transportpolicy.go | 46 --- internal/ice/transportstate_test.go | 61 ---- internal/tools/gen/genaliasdocs.go | 261 ---------------- internal/ice/networktype.go => networktype.go | 2 +- ...networktype_test.go => networktype_test.go | 2 +- .../oauthcredential.go => oauthcredential.go | 4 +- peerconnection.go | 8 +- peerconnection_js.go | 2 +- 61 files changed, 1059 insertions(+), 1670 deletions(-) delete mode 100644 ice.go rename internal/ice/candidate.go => icecandidate.go (53%) rename internal/ice/candidate_test.go => icecandidate_test.go (82%) rename internal/ice/candidateinit.go => icecandidateinit.go (67%) rename internal/ice/candidateinit_test.go => icecandidateinit_test.go (85%) create mode 100644 icecandidatepair.go create mode 100644 icecandidatetype.go create mode 100644 icecandidatetype_test.go create mode 100644 icecomponent.go rename internal/ice/component_test.go => icecomponent_test.go (53%) create mode 100644 iceconnectionstate.go create mode 100644 iceconnectionstate_test.go create mode 100644 icecredentialtype.go rename internal/ice/credentialtype_test.go => icecredentialtype_test.go (54%) rename internal/ice/gatherer.go => icegatherer.go (78%) rename internal/ice/gatherer_test.go => icegatherer_test.go (70%) create mode 100644 icegathererstate.go create mode 100644 icegathererstate_test.go create mode 100644 icegatheringstate.go create mode 100644 icegatheringstate_test.go create mode 100644 icegatheroptions.go rename internal/ice/parameters.go => iceparameters.go (66%) create mode 100644 iceprotocol.go rename internal/ice/protocol_test.go => iceprotocol_test.go (53%) create mode 100644 icerole.go rename internal/ice/role_test.go => icerole_test.go (53%) rename internal/ice/server.go => iceserver.go (77%) rename internal/ice/server_js.go => iceserver_js.go (76%) rename internal/ice/server_test.go => iceserver_test.go (78%) rename internal/ice/transport.go => icetransport.go (64%) create mode 100644 icetransportpolicy.go rename internal/ice/transportpolicy_test.go => icetransportpolicy_test.go (55%) rename internal/ice/transportstate.go => icetransportstate.go (50%) create mode 100644 icetransportstate_test.go delete mode 100644 internal/ice/candidatepair.go delete mode 100644 internal/ice/candidatetype.go delete mode 100644 internal/ice/candidatetype_test.go delete mode 100644 internal/ice/component.go delete mode 100644 internal/ice/connectionstate.go delete mode 100644 internal/ice/connectionstate_test.go delete mode 100644 internal/ice/constants.go delete mode 100644 internal/ice/credentialtype.go delete mode 100644 internal/ice/errors.go delete mode 100644 internal/ice/gathererstate.go delete mode 100644 internal/ice/gathererstate_test.go delete mode 100644 internal/ice/gatheringstate.go delete mode 100644 internal/ice/gatheringstate_test.go delete mode 100644 internal/ice/gatheroptions.go delete mode 100644 internal/ice/protocol.go delete mode 100644 internal/ice/role.go delete mode 100644 internal/ice/transportpolicy.go delete mode 100644 internal/ice/transportstate_test.go delete mode 100644 internal/tools/gen/genaliasdocs.go rename internal/ice/networktype.go => networktype.go (98%) rename internal/ice/networktype_test.go => networktype_test.go (98%) rename internal/ice/oauthcredential.go => oauthcredential.go (86%) diff --git a/.travis.yml b/.travis.yml index e6d9ed5ed8c..b3ae371e9f7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,10 +39,8 @@ script: - cd examples && golangci-lint run --build-tags quic ./... -D gochecknoinits - cd .. - rm -rf examples # Remove examples, no test coverage for them - - rm -rf internal/tools # Remove tools, no test coverage for them - go test -tags quic -coverpkg=$(go list ./... | tr '\n' ',') -coverprofile=cover.out -v -race -covermode=atomic ./... - - GOOS=js GOARCH=wasm go test -exec="${PWD}/test-wasm/go_js_wasm_exec" -v . - - GOOS=js GOARCH=wasm go test -exec="${PWD}/test-wasm/go_js_wasm_exec" -v ./internal/ice + - GOOS=js GOARCH=wasm go test -exec="./test-wasm/go_js_wasm_exec" -v . - bash <(curl -s https://codecov.io/bash) - bash .github/assert-contributors.sh - bash .github/lint-disallowed-functions-in-library.sh diff --git a/errors.go b/errors.go index 548d6d93b40..af602afb233 100644 --- a/errors.go +++ b/errors.go @@ -19,6 +19,14 @@ var ( // ErrCertificateExpired indicates that an x509 certificate has expired. ErrCertificateExpired = errors.New("x509Cert expired") + // ErrNoTurnCredencials indicates that a TURN server URL was provided + // without required credentials. + ErrNoTurnCredencials = errors.New("turn server credentials required") + + // ErrTurnCredencials indicates that provided TURN credentials are partial + // or malformed. + ErrTurnCredencials = errors.New("invalid turn server credentials") + // ErrExistingTrack indicates that a track already exists. ErrExistingTrack = errors.New("track already exists") diff --git a/ice.go b/ice.go deleted file mode 100644 index 52047cb1590..00000000000 --- a/ice.go +++ /dev/null @@ -1,289 +0,0 @@ -package webrtc - -import ( - "github.com/pion/webrtc/v2/internal/ice" -) - -//go:generate go run internal/tools/gen/genaliasdocs.go -pkg "./internal/ice" $GOFILE - -const ( - - // ICETransportPolicyRelay indicates only media relay candidates such - // as candidates passing through a TURN server are used. - ICETransportPolicyRelay = ice.TransportPolicyRelay - - // ICETransportPolicyAll indicates any type of candidate is used. - ICETransportPolicyAll = ice.TransportPolicyAll - - // ICETransportStateNew indicates the Transport is waiting - // for remote candidates to be supplied. - ICETransportStateNew = ice.TransportStateNew - - // ICETransportStateChecking indicates the Transport has - // received at least one remote candidate, and a local and remote - // ICECandidateComplete dictionary was not added as the last candidate. - ICETransportStateChecking = ice.TransportStateChecking - - // ICETransportStateConnected indicates the Transport has - // received a response to an outgoing connectivity check, or has - // received incoming DTLS/media after a successful response to an - // incoming connectivity check, but is still checking other candidate - // pairs to see if there is a better connection. - ICETransportStateConnected = ice.TransportStateConnected - - // ICETransportStateCompleted indicates the Transport tested - // all appropriate candidate pairs and at least one functioning - // candidate pair has been found. - ICETransportStateCompleted = ice.TransportStateCompleted - - // ICETransportStateFailed indicates the Transport the last - // candidate was added and all appropriate candidate pairs have either - // failed connectivity checks or have lost consent. - ICETransportStateFailed = ice.TransportStateFailed - - // ICETransportStateDisconnected indicates the Transport has received - // at least one local and remote candidate, but the final candidate was - // received yet and all appropriate candidate pairs thus far have been - // tested and failed. - ICETransportStateDisconnected = ice.TransportStateDisconnected - - // ICETransportStateClosed indicates the Transport has shut down - // and is no longer responding to STUN requests. - ICETransportStateClosed = ice.TransportStateClosed - - // ICEConnectionStateNew indicates that any of the ICETransports are - // in the "new" state and none of them are in the "checking", "disconnected" - // or "failed" state, or all ICETransports are in the "closed" state, or - // there are no transports. - ICEConnectionStateNew = ice.ConnectionStateNew - - // ICEConnectionStateChecking indicates that any of the ICETransports - // are in the "checking" state and none of them are in the "disconnected" - // or "failed" state. - ICEConnectionStateChecking = ice.ConnectionStateChecking - - // ICEConnectionStateConnected indicates that all ICETransports are - // in the "connected", "completed" or "closed" state and at least one of - // them is in the "connected" state. - ICEConnectionStateConnected = ice.ConnectionStateConnected - - // ICEConnectionStateCompleted indicates that all ICETransports are - // in the "completed" or "closed" state and at least one of them is in the - // "completed" state. - ICEConnectionStateCompleted = ice.ConnectionStateCompleted - - // ICEConnectionStateDisconnected indicates that any of the - // ICETransports are in the "disconnected" state and none of them are - // in the "failed" state. - ICEConnectionStateDisconnected = ice.ConnectionStateDisconnected - - // ICEConnectionStateFailed indicates that any of the ICETransports - // are in the "failed" state. - ICEConnectionStateFailed = ice.ConnectionStateFailed - - // ICEConnectionStateClosed indicates that the PeerConnection's - // isClosed is true. - ICEConnectionStateClosed = ice.ConnectionStateClosed - - // ICEGatheringStateNew indicates that any of the ICETransports are - // in the "new" gathering state and none of the transports are in the - // "gathering" state, or there are no transports. - ICEGatheringStateNew = ice.GatheringStateNew - - // ICEGatheringStateGathering indicates that any of the ICETransports - // are in the "gathering" state. - ICEGatheringStateGathering = ice.GatheringStateGathering - - // ICEGatheringStateComplete indicates that at least one Transport - // exists, and all ICETransports are in the "completed" gathering state. - ICEGatheringStateComplete = ice.GatheringStateComplete - - // ICERoleControlled indicates that an ICE agent that waits for the - // controlling agent to select the final choice of candidate pairs. - ICERoleControlled = ice.RoleControlled - - // ICERoleControlling indicates that the ICE agent that is responsible - // for selecting the final choice of candidate pairs and signaling them - // through STUN and an updated offer, if needed. In any session, one agent - // is always controlling. The other is the controlled agent. - ICERoleControlling = ice.RoleControlling - - // ICECredentialTypePassword describes username and pasword based - // credentials as described in https://tools.ietf.org/html/rfc5389. - ICECredentialTypePassword = ice.CredentialTypePassword - - // ICECredentialTypeOauth describes token based credential as described - // in https://tools.ietf.org/html/rfc7635. - ICECredentialTypeOauth = ice.CredentialTypeOauth - - // ICECandidateTypeHost indicates that the candidate is of Host type as - // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.1. A - // candidate obtained by binding to a specific port from an IP address on - // the host. This includes IP addresses on physical interfaces and logical - // ones, such as ones obtained through VPNs. - ICECandidateTypeHost = ice.CandidateTypeHost - - // ICECandidateTypeSrflx indicates the the candidate is of Server - // Reflexive type as described - // https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A candidate type - // whose IP address and port are a binding allocated by a NAT for an ICE - // agent after it sends a packet through the NAT to a server, such as a - // STUN server. - ICECandidateTypeSrflx = ice.CandidateTypeSrflx - - // ICECandidateTypePrflx indicates that the candidate is of Peer - // Reflexive type. A candidate type whose IP address and port are a binding - // allocated by a NAT for an ICE agent after it sends a packet through the - // NAT to its peer. - ICECandidateTypePrflx = ice.CandidateTypePrflx - - // ICECandidateTypeRelay indicates the the candidate is of Relay type as - // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A - // candidate type obtained from a relay server, such as a TURN server. - ICECandidateTypeRelay = ice.CandidateTypeRelay - - // NetworkTypeUDP4 indicates UDP over IPv4. - NetworkTypeUDP4 = ice.NetworkTypeUDP4 - - // NetworkTypeUDP6 indicates UDP over IPv6. - NetworkTypeUDP6 = ice.NetworkTypeUDP6 - - // NetworkTypeTCP4 indicates TCP over IPv4. - NetworkTypeTCP4 = ice.NetworkTypeTCP4 - - // NetworkTypeTCP6 indicates TCP over IPv6. - NetworkTypeTCP6 = ice.NetworkTypeTCP6 - - // ICEProtocolUDP indicates the URL uses a UDP transport. - ICEProtocolUDP = ice.ProtocolUDP - - // ICEProtocolTCP indicates the URL uses a TCP transport. - ICEProtocolTCP = ice.ProtocolTCP - - // ICEComponentRTP indicates that the ICE Transport is used for RTP (or - // RTCP multiplexing), as defined in - // https://tools.ietf.org/html/rfc5245#section-4.1.1.1. Protocols - // multiplexed with RTP (e.g. data channel) share its component ID. This - // represents the component-id value 1 when encoded in candidate-attribute. - ICEComponentRTP = ice.ComponentRTP - - // ICEComponentRTCP indicates that the ICE Transport is used for RTCP as - // defined by https://tools.ietf.org/html/rfc5245#section-4.1.1.1. This - // represents the component-id value 2 when encoded in candidate-attribute. - ICEComponentRTCP = ice.ComponentRTCP - - // ICEGathererStateNew indicates object has been created but - // gather() has not been called. - ICEGathererStateNew = ice.GathererStateNew - - // ICEGathererStateGathering indicates gather() has been called, - // and the Gatherer is in the process of gathering candidates. - ICEGathererStateGathering = ice.GathererStateGathering - - // ICEGathererStateComplete indicates the Gatherer has completed gathering. - ICEGathererStateComplete = ice.GathererStateComplete - - // ICEGathererStateClosed indicates the closed state can only be entered - // when the Gatherer has been closed intentionally by calling close(). - ICEGathererStateClosed = ice.GathererStateClosed -) - -type ( - - // ICEServer describes a single STUN and TURN server that can be used by - // the ICEAgent to establish a connection with a peer. - ICEServer = ice.Server - - // ICETransportPolicy defines the ICE candidate policy surface the - // permitted candidates. Only these candidates are used for connectivity checks. - ICETransportPolicy = ice.TransportPolicy - - // ICEGatherPolicy is the ORTC equivalent of TransportPolicy - ICEGatherPolicy = ice.GatherPolicy - - // ICEGatheringState describes the state of the candidate gathering process. - ICEGatheringState = ice.GatheringState - - // ICEConnectionState indicates signaling state of the ICE Connection. - ICEConnectionState = ice.ConnectionState - - // ICECandidate represents a ice candidate - ICECandidate = ice.Candidate - - // ICEGatherOptions provides options relating to the gathering of ICE candidates. - ICEGatherOptions = ice.GatherOptions - - // ICETransportState represents the current state of the ICE transport. - ICETransportState = ice.TransportState - - // ICERole indicates the current role of the ICE transport. - ICERole = ice.Role - - // ICEParameters includes the ICE username fragment - // and password and other ICE-related parameters. - ICEParameters = ice.Parameters - - // ICECandidateInit is used to serialize ice candidates - ICECandidateInit = ice.CandidateInit - - // ICECandidateType represents the type of the ICE candidate used. - ICECandidateType = ice.CandidateType - - // ICECredentialType indicates the type of credentials used to connect to - // an ICE server. - ICECredentialType = ice.CredentialType - - // ICEProtocol indicates the transport protocol type that is used in the - // ice.URL structure. - ICEProtocol = ice.Protocol - - // ICECandidatePair represents an ICE Candidate pair - ICECandidatePair = ice.CandidatePair - - // ICEComponent ICEComponent - // State TransportState - // gatheringState GathererState - ICEComponent = ice.Component - - // ICEGathererState represents the current state of the ICE gatherer. - ICEGathererState = ice.GathererState - - // NetworkType represents the type of network - NetworkType = ice.NetworkType - - // OAuthCredential represents OAuth credential information which is used by - // the STUN/TURN client to connect to an ICE server as defined in - // https://tools.ietf.org/html/rfc7635. Note that the kid parameter is not - // located in OAuthCredential, but in Server's username member. - OAuthCredential = ice.OAuthCredential -) - -var ( - - // NewICECandidatePair returns an initialized *CandidatePair - // for the given pair of Candidate instances - NewICECandidatePair = ice.NewCandidatePair - - // NewICECandidateType takes a string and converts it into CandidateType - NewICECandidateType = ice.NewCandidateType - - // NewICEProtocol takes a string and converts it to Protocol - NewICEProtocol = ice.NewProtocol - - // NewICEConnectionState takes a string and converts it to ConnectionState - NewICEConnectionState = ice.NewConnectionState - - // NewICEGatheringState takes a string and converts it to GatheringState - NewICEGatheringState = ice.NewGatheringState - - // NewICETransportPolicy takes a string and converts it to TransportPolicy - NewICETransportPolicy = ice.NewTransportPolicy - - // ErrNoTurnCredencials indicates that a TURN server URL was provided - // without required credentials. - ErrNoTurnCredencials = ice.ErrNoTurnCredencials - - // ErrTurnCredencials indicates that provided TURN credentials are partial - // or malformed. - ErrTurnCredencials = ice.ErrTurnCredencials -) diff --git a/ice_go.go b/ice_go.go index 1e2f8d6d6f8..eb90c9f8e2b 100644 --- a/ice_go.go +++ b/ice_go.go @@ -2,35 +2,7 @@ package webrtc -import ( - "github.com/pion/sdp/v2" - - "github.com/pion/webrtc/v2/internal/ice" -) - -//go:generate go run internal/tools/gen/genaliasdocs.go -pkg "./internal/ice" -build-tags "!js" $GOFILE - -type ( - - // ICETransport allows an application access to information about the ICE - // transport over which packets are sent and received. - ICETransport = ice.Transport - - // ICEGatherer gathers local host, server reflexive and relay - // candidates, as well as enabling the retrieval of local Interactive - // Connectivity Establishment (ICE) parameters which can be - // exchanged in signaling. - ICEGatherer = ice.Gatherer -) - -var ( - - // NewICEGatherer creates a new NewICEGatherer. - NewICEGatherer = ice.NewGatherer - - // NewICETransport creates a new NewICETransport. - NewICETransport = ice.NewTransport -) +import "github.com/pion/sdp/v2" // NewICEGatherer creates a new NewICEGatherer. // This constructor is part of the ORTC API. It is not diff --git a/internal/ice/candidate.go b/icecandidate.go similarity index 53% rename from internal/ice/candidate.go rename to icecandidate.go index e6ba1e99013..df33d050990 100644 --- a/internal/ice/candidate.go +++ b/icecandidate.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "errors" @@ -8,26 +8,26 @@ import ( "github.com/pion/ice" ) -// Candidate represents a ice candidate -type Candidate struct { - Foundation string `json:"foundation"` - Priority uint32 `json:"priority"` - IP string `json:"ip"` - Protocol Protocol `json:"protocol"` - Port uint16 `json:"port"` - Typ CandidateType `json:"type"` - Component uint16 `json:"component"` - RelatedAddress string `json:"relatedAddress"` - RelatedPort uint16 `json:"relatedPort"` +// ICECandidate represents a ice candidate +type ICECandidate struct { + Foundation string `json:"foundation"` + Priority uint32 `json:"priority"` + IP string `json:"ip"` + Protocol ICEProtocol `json:"protocol"` + Port uint16 `json:"port"` + Typ ICECandidateType `json:"type"` + Component uint16 `json:"component"` + RelatedAddress string `json:"relatedAddress"` + RelatedPort uint16 `json:"relatedPort"` } // Conversion for package ice -func newCandidatesFromICE(iceCandidates []ice.Candidate) ([]Candidate, error) { - candidates := []Candidate{} +func newICECandidatesFromICE(iceCandidates []ice.Candidate) ([]ICECandidate, error) { + candidates := []ICECandidate{} for _, i := range iceCandidates { - c, err := newCandidateFromICE(i) + c, err := newICECandidateFromICE(i) if err != nil { return nil, err } @@ -37,17 +37,17 @@ func newCandidatesFromICE(iceCandidates []ice.Candidate) ([]Candidate, error) { return candidates, nil } -func newCandidateFromICE(i ice.Candidate) (Candidate, error) { +func newICECandidateFromICE(i ice.Candidate) (ICECandidate, error) { typ, err := convertTypeFromICE(i.Type()) if err != nil { - return Candidate{}, err + return ICECandidate{}, err } - protocol, err := NewProtocol(i.NetworkType().NetworkShort()) + protocol, err := NewICEProtocol(i.NetworkType().NetworkShort()) if err != nil { - return Candidate{}, err + return ICECandidate{}, err } - c := Candidate{ + c := ICECandidate{ Foundation: "foundation", Priority: i.Priority(), IP: i.IP().String(), @@ -65,22 +65,22 @@ func newCandidateFromICE(i ice.Candidate) (Candidate, error) { return c, nil } -func (c Candidate) toICE() (ice.Candidate, error) { +func (c ICECandidate) toICE() (ice.Candidate, error) { ip := net.ParseIP(c.IP) if ip == nil { return nil, errors.New("failed to parse IP address") } switch c.Typ { - case CandidateTypeHost: + case ICECandidateTypeHost: return ice.NewCandidateHost(c.Protocol.String(), ip, int(c.Port), c.Component) - case CandidateTypeSrflx: + case ICECandidateTypeSrflx: return ice.NewCandidateServerReflexive(c.Protocol.String(), ip, int(c.Port), c.Component, c.RelatedAddress, int(c.RelatedPort)) - case CandidateTypePrflx: + case ICECandidateTypePrflx: return ice.NewCandidatePeerReflexive(c.Protocol.String(), ip, int(c.Port), c.Component, c.RelatedAddress, int(c.RelatedPort)) - case CandidateTypeRelay: + case ICECandidateTypeRelay: return ice.NewCandidateRelay(c.Protocol.String(), ip, int(c.Port), c.Component, c.RelatedAddress, int(c.RelatedPort)) default: @@ -88,22 +88,22 @@ func (c Candidate) toICE() (ice.Candidate, error) { } } -func convertTypeFromICE(t ice.CandidateType) (CandidateType, error) { +func convertTypeFromICE(t ice.CandidateType) (ICECandidateType, error) { switch t { case ice.CandidateTypeHost: - return CandidateTypeHost, nil + return ICECandidateTypeHost, nil case ice.CandidateTypeServerReflexive: - return CandidateTypeSrflx, nil + return ICECandidateTypeSrflx, nil case ice.CandidateTypePeerReflexive: - return CandidateTypePrflx, nil + return ICECandidateTypePrflx, nil case ice.CandidateTypeRelay: - return CandidateTypeRelay, nil + return ICECandidateTypeRelay, nil default: - return CandidateType(t), fmt.Errorf("unknown ICE candidate type: %s", t) + return ICECandidateType(t), fmt.Errorf("unknown ICE candidate type: %s", t) } } -func (c Candidate) String() string { +func (c ICECandidate) String() string { ic, err := c.toICE() if err != nil { return fmt.Sprintf("%#v failed to convert to ICE: %s", c, err) diff --git a/internal/ice/candidate_test.go b/icecandidate_test.go similarity index 82% rename from internal/ice/candidate_test.go rename to icecandidate_test.go index bf2335fdd13..f2a5403ad5e 100644 --- a/internal/ice/candidate_test.go +++ b/icecandidate_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "net" @@ -8,9 +8,9 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCandidate_Convert(t *testing.T) { +func TestICECandidate_Convert(t *testing.T) { testCases := []struct { - native Candidate + native ICECandidate expectedType ice.CandidateType expectedNetwork string @@ -20,13 +20,13 @@ func TestCandidate_Convert(t *testing.T) { expectedRelatedAddress *ice.CandidateRelatedAddress }{ { - Candidate{ + ICECandidate{ Foundation: "foundation", Priority: 128, IP: "1.0.0.1", - Protocol: ProtocolUDP, + Protocol: ICEProtocolUDP, Port: 1234, - Typ: CandidateTypeHost, + Typ: ICECandidateTypeHost, Component: 1, }, @@ -38,13 +38,13 @@ func TestCandidate_Convert(t *testing.T) { nil, }, { - Candidate{ + ICECandidate{ Foundation: "foundation", Priority: 128, IP: "::1", - Protocol: ProtocolUDP, + Protocol: ICEProtocolUDP, Port: 1234, - Typ: CandidateTypeSrflx, + Typ: ICECandidateTypeSrflx, Component: 1, RelatedAddress: "1.0.0.1", RelatedPort: 4321, @@ -61,13 +61,13 @@ func TestCandidate_Convert(t *testing.T) { }, }, { - Candidate{ + ICECandidate{ Foundation: "foundation", Priority: 128, IP: "::1", - Protocol: ProtocolUDP, + Protocol: ICEProtocolUDP, Port: 1234, - Typ: CandidateTypePrflx, + Typ: ICECandidateTypePrflx, Component: 1, RelatedAddress: "1.0.0.1", RelatedPort: 4321, @@ -113,8 +113,8 @@ func TestConvertTypeFromICE(t *testing.T) { if err != nil { t.Fatal("failed coverting ice.CandidateTypeHost") } - if ct != CandidateTypeHost { - t.Fatal("should be coverted to CandidateTypeHost") + if ct != ICECandidateTypeHost { + t.Fatal("should be coverted to ICECandidateTypeHost") } }) t.Run("srflx", func(t *testing.T) { @@ -122,8 +122,8 @@ func TestConvertTypeFromICE(t *testing.T) { if err != nil { t.Fatal("failed coverting ice.CandidateTypeServerReflexive") } - if ct != CandidateTypeSrflx { - t.Fatal("should be coverted to CandidateTypeSrflx") + if ct != ICECandidateTypeSrflx { + t.Fatal("should be coverted to ICECandidateTypeSrflx") } }) t.Run("prflx", func(t *testing.T) { @@ -131,8 +131,8 @@ func TestConvertTypeFromICE(t *testing.T) { if err != nil { t.Fatal("failed coverting ice.CandidateTypePeerReflexive") } - if ct != CandidateTypePrflx { - t.Fatal("should be coverted to CandidateTypePrflx") + if ct != ICECandidateTypePrflx { + t.Fatal("should be coverted to ICECandidateTypePrflx") } }) } diff --git a/internal/ice/candidateinit.go b/icecandidateinit.go similarity index 67% rename from internal/ice/candidateinit.go rename to icecandidateinit.go index 1cdbf5e10ec..64cfa89dba0 100644 --- a/internal/ice/candidateinit.go +++ b/icecandidateinit.go @@ -1,7 +1,7 @@ -package ice +package webrtc -// CandidateInit is used to serialize ice candidates -type CandidateInit struct { +// ICECandidateInit is used to serialize ice candidates +type ICECandidateInit struct { Candidate string `json:"candidate"` SDPMid *string `json:"sdpMid,omitempty"` SDPMLineIndex *uint16 `json:"sdpMLineIndex,omitempty"` diff --git a/internal/ice/candidateinit_test.go b/icecandidateinit_test.go similarity index 85% rename from internal/ice/candidateinit_test.go rename to icecandidateinit_test.go index 3377139666f..c9c98b603a5 100644 --- a/internal/ice/candidateinit_test.go +++ b/icecandidateinit_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "encoding/json" @@ -7,18 +7,18 @@ import ( "github.com/stretchr/testify/assert" ) -func TestCandidateInit_Serialization(t *testing.T) { +func TestICECandidateInit_Serialization(t *testing.T) { tt := []struct { - candidate CandidateInit + candidate ICECandidateInit serialized string }{ - {CandidateInit{ + {ICECandidateInit{ Candidate: "candidate:abc123", SDPMid: refString("0"), SDPMLineIndex: refUint16(0), UsernameFragment: "def", }, `{"candidate":"candidate:abc123","sdpMid":"0","sdpMLineIndex":0,"usernameFragment":"def"}`}, - {CandidateInit{ + {ICECandidateInit{ Candidate: "candidate:abc123", UsernameFragment: "def", }, `{"candidate":"candidate:abc123","usernameFragment":"def"}`}, @@ -34,7 +34,7 @@ func TestCandidateInit_Serialization(t *testing.T) { t.Errorf("%d expected %s got %s", i, tc.serialized, actualSerialized) } - var actual CandidateInit + var actual ICECandidateInit err = json.Unmarshal(b, &actual) if err != nil { t.Errorf("Failed to unmarshal %d: %v", i, err) diff --git a/icecandidatepair.go b/icecandidatepair.go new file mode 100644 index 00000000000..417b6d774a7 --- /dev/null +++ b/icecandidatepair.go @@ -0,0 +1,24 @@ +package webrtc + +import "fmt" + +type ( + // ICECandidatePair represents an ICE Candidate pair + ICECandidatePair struct { + Local *ICECandidate + Remote *ICECandidate + } +) + +func (p *ICECandidatePair) String() string { + return fmt.Sprintf("(local) %s <-> (remote) %s", p.Local, p.Remote) +} + +// NewICECandidatePair returns an initialized *ICECandidatePair +// for the given pair of ICECandidate instances +func NewICECandidatePair(local, remote *ICECandidate) *ICECandidatePair { + return &ICECandidatePair{ + Local: local, + Remote: remote, + } +} diff --git a/icecandidatetype.go b/icecandidatetype.go new file mode 100644 index 00000000000..d1381f0cd1e --- /dev/null +++ b/icecandidatetype.go @@ -0,0 +1,73 @@ +package webrtc + +import "fmt" + +// ICECandidateType represents the type of the ICE candidate used. +type ICECandidateType int + +const ( + // ICECandidateTypeHost indicates that the candidate is of Host type as + // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.1. A + // candidate obtained by binding to a specific port from an IP address on + // the host. This includes IP addresses on physical interfaces and logical + // ones, such as ones obtained through VPNs. + ICECandidateTypeHost ICECandidateType = iota + 1 + + // ICECandidateTypeSrflx indicates the the candidate is of Server + // Reflexive type as described + // https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A candidate type + // whose IP address and port are a binding allocated by a NAT for an ICE + // agent after it sends a packet through the NAT to a server, such as a + // STUN server. + ICECandidateTypeSrflx + + // ICECandidateTypePrflx indicates that the candidate is of Peer + // Reflexive type. A candidate type whose IP address and port are a binding + // allocated by a NAT for an ICE agent after it sends a packet through the + // NAT to its peer. + ICECandidateTypePrflx + + // ICECandidateTypeRelay indicates the the candidate is of Relay type as + // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A + // candidate type obtained from a relay server, such as a TURN server. + ICECandidateTypeRelay +) + +// This is done this way because of a linter. +const ( + iceCandidateTypeHostStr = "host" + iceCandidateTypeSrflxStr = "srflx" + iceCandidateTypePrflxStr = "prflx" + iceCandidateTypeRelayStr = "relay" +) + +// NewICECandidateType takes a string and converts it into ICECandidateType +func NewICECandidateType(raw string) (ICECandidateType, error) { + switch raw { + case iceCandidateTypeHostStr: + return ICECandidateTypeHost, nil + case iceCandidateTypeSrflxStr: + return ICECandidateTypeSrflx, nil + case iceCandidateTypePrflxStr: + return ICECandidateTypePrflx, nil + case iceCandidateTypeRelayStr: + return ICECandidateTypeRelay, nil + default: + return ICECandidateType(Unknown), fmt.Errorf("unknown ICE candidate type: %s", raw) + } +} + +func (t ICECandidateType) String() string { + switch t { + case ICECandidateTypeHost: + return iceCandidateTypeHostStr + case ICECandidateTypeSrflx: + return iceCandidateTypeSrflxStr + case ICECandidateTypePrflx: + return iceCandidateTypePrflxStr + case ICECandidateTypeRelay: + return iceCandidateTypeRelayStr + default: + return ErrUnknownType.Error() + } +} diff --git a/icecandidatetype_test.go b/icecandidatetype_test.go new file mode 100644 index 00000000000..fa433429421 --- /dev/null +++ b/icecandidatetype_test.go @@ -0,0 +1,54 @@ +package webrtc + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestICECandidateType(t *testing.T) { + testCases := []struct { + typeString string + shouldFail bool + expectedType ICECandidateType + }{ + {unknownStr, true, ICECandidateType(Unknown)}, + {"host", false, ICECandidateTypeHost}, + {"srflx", false, ICECandidateTypeSrflx}, + {"prflx", false, ICECandidateTypePrflx}, + {"relay", false, ICECandidateTypeRelay}, + } + + for i, testCase := range testCases { + actual, err := NewICECandidateType(testCase.typeString) + if (err != nil) != testCase.shouldFail { + t.Error(err) + } + assert.Equal(t, + testCase.expectedType, + actual, + "testCase: %d %v", i, testCase, + ) + } +} + +func TestICECandidateType_String(t *testing.T) { + testCases := []struct { + cType ICECandidateType + expectedString string + }{ + {ICECandidateType(Unknown), unknownStr}, + {ICECandidateTypeHost, "host"}, + {ICECandidateTypeSrflx, "srflx"}, + {ICECandidateTypePrflx, "prflx"}, + {ICECandidateTypeRelay, "relay"}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.expectedString, + testCase.cType.String(), + "testCase: %d %v", i, testCase, + ) + } +} diff --git a/icecomponent.go b/icecomponent.go new file mode 100644 index 00000000000..1f03ec5b062 --- /dev/null +++ b/icecomponent.go @@ -0,0 +1,47 @@ +package webrtc + +// ICEComponent describes if the ice transport is used for RTP +// (or RTCP multiplexing). +type ICEComponent int + +const ( + // ICEComponentRTP indicates that the ICE Transport is used for RTP (or + // RTCP multiplexing), as defined in + // https://tools.ietf.org/html/rfc5245#section-4.1.1.1. Protocols + // multiplexed with RTP (e.g. data channel) share its component ID. This + // represents the component-id value 1 when encoded in candidate-attribute. + ICEComponentRTP ICEComponent = iota + 1 + + // ICEComponentRTCP indicates that the ICE Transport is used for RTCP as + // defined by https://tools.ietf.org/html/rfc5245#section-4.1.1.1. This + // represents the component-id value 2 when encoded in candidate-attribute. + ICEComponentRTCP +) + +// This is done this way because of a linter. +const ( + iceComponentRTPStr = "rtp" + iceComponentRTCPStr = "rtcp" +) + +func newICEComponent(raw string) ICEComponent { + switch raw { + case iceComponentRTPStr: + return ICEComponentRTP + case iceComponentRTCPStr: + return ICEComponentRTCP + default: + return ICEComponent(Unknown) + } +} + +func (t ICEComponent) String() string { + switch t { + case ICEComponentRTP: + return iceComponentRTPStr + case ICEComponentRTCP: + return iceComponentRTCPStr + default: + return ErrUnknownType.Error() + } +} diff --git a/internal/ice/component_test.go b/icecomponent_test.go similarity index 53% rename from internal/ice/component_test.go rename to icecomponent_test.go index d904d611dad..399cd81fd8e 100644 --- a/internal/ice/component_test.go +++ b/icecomponent_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "testing" @@ -6,33 +6,33 @@ import ( "github.com/stretchr/testify/assert" ) -func TestComponent(t *testing.T) { +func TestICEComponent(t *testing.T) { testCases := []struct { componentString string - expectedComponent Component + expectedComponent ICEComponent }{ - {unknownStr, Component(Unknown)}, - {"rtp", ComponentRTP}, - {"rtcp", ComponentRTCP}, + {unknownStr, ICEComponent(Unknown)}, + {"rtp", ICEComponentRTP}, + {"rtcp", ICEComponentRTCP}, } for i, testCase := range testCases { assert.Equal(t, - newComponent(testCase.componentString), + newICEComponent(testCase.componentString), testCase.expectedComponent, "testCase: %d %v", i, testCase, ) } } -func TestComponent_String(t *testing.T) { +func TestICEComponent_String(t *testing.T) { testCases := []struct { - state Component + state ICEComponent expectedString string }{ - {Component(Unknown), unknownStr}, - {ComponentRTP, "rtp"}, - {ComponentRTCP, "rtcp"}, + {ICEComponent(Unknown), unknownStr}, + {ICEComponentRTP, "rtp"}, + {ICEComponentRTCP, "rtcp"}, } for i, testCase := range testCases { diff --git a/iceconnectionstate.go b/iceconnectionstate.go new file mode 100644 index 00000000000..22fd26975a8 --- /dev/null +++ b/iceconnectionstate.go @@ -0,0 +1,94 @@ +package webrtc + +// ICEConnectionState indicates signaling state of the ICE Connection. +type ICEConnectionState int + +const ( + // ICEConnectionStateNew indicates that any of the ICETransports are + // in the "new" state and none of them are in the "checking", "disconnected" + // or "failed" state, or all ICETransports are in the "closed" state, or + // there are no transports. + ICEConnectionStateNew ICEConnectionState = iota + 1 + + // ICEConnectionStateChecking indicates that any of the ICETransports + // are in the "checking" state and none of them are in the "disconnected" + // or "failed" state. + ICEConnectionStateChecking + + // ICEConnectionStateConnected indicates that all ICETransports are + // in the "connected", "completed" or "closed" state and at least one of + // them is in the "connected" state. + ICEConnectionStateConnected + + // ICEConnectionStateCompleted indicates that all ICETransports are + // in the "completed" or "closed" state and at least one of them is in the + // "completed" state. + ICEConnectionStateCompleted + + // ICEConnectionStateDisconnected indicates that any of the + // ICETransports are in the "disconnected" state and none of them are + // in the "failed" state. + ICEConnectionStateDisconnected + + // ICEConnectionStateFailed indicates that any of the ICETransports + // are in the "failed" state. + ICEConnectionStateFailed + + // ICEConnectionStateClosed indicates that the PeerConnection's + // isClosed is true. + ICEConnectionStateClosed +) + +// This is done this way because of a linter. +const ( + iceConnectionStateNewStr = "new" + iceConnectionStateCheckingStr = "checking" + iceConnectionStateConnectedStr = "connected" + iceConnectionStateCompletedStr = "completed" + iceConnectionStateDisconnectedStr = "disconnected" + iceConnectionStateFailedStr = "failed" + iceConnectionStateClosedStr = "closed" +) + +// NewICEConnectionState takes a string and converts it to ICEConnectionState +func NewICEConnectionState(raw string) ICEConnectionState { + switch raw { + case iceConnectionStateNewStr: + return ICEConnectionStateNew + case iceConnectionStateCheckingStr: + return ICEConnectionStateChecking + case iceConnectionStateConnectedStr: + return ICEConnectionStateConnected + case iceConnectionStateCompletedStr: + return ICEConnectionStateCompleted + case iceConnectionStateDisconnectedStr: + return ICEConnectionStateDisconnected + case iceConnectionStateFailedStr: + return ICEConnectionStateFailed + case iceConnectionStateClosedStr: + return ICEConnectionStateClosed + default: + return ICEConnectionState(Unknown) + } +} + +func (c ICEConnectionState) String() string { + switch c { + case ICEConnectionStateNew: + return iceConnectionStateNewStr + case ICEConnectionStateChecking: + return iceConnectionStateCheckingStr + case ICEConnectionStateConnected: + return iceConnectionStateConnectedStr + case ICEConnectionStateCompleted: + return iceConnectionStateCompletedStr + case ICEConnectionStateDisconnected: + return iceConnectionStateDisconnectedStr + case ICEConnectionStateFailed: + return iceConnectionStateFailedStr + case ICEConnectionStateClosed: + return iceConnectionStateClosedStr + default: + return ErrUnknownType.Error() + } +} diff --git a/iceconnectionstate_test.go b/iceconnectionstate_test.go new file mode 100644 index 00000000000..bae46f2e32a --- /dev/null +++ b/iceconnectionstate_test.go @@ -0,0 +1,55 @@ +package webrtc + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewICEConnectionState(t *testing.T) { + testCases := []struct { + stateString string + expectedState ICEConnectionState + }{ + {unknownStr, ICEConnectionState(Unknown)}, + {"new", ICEConnectionStateNew}, + {"checking", ICEConnectionStateChecking}, + {"connected", ICEConnectionStateConnected}, + {"completed", ICEConnectionStateCompleted}, + {"disconnected", ICEConnectionStateDisconnected}, + {"failed", ICEConnectionStateFailed}, + {"closed", ICEConnectionStateClosed}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.expectedState, + NewICEConnectionState(testCase.stateString), + "testCase: %d %v", i, testCase, + ) + } +} + +func TestICEConnectionState_String(t *testing.T) { + testCases := []struct { + state ICEConnectionState + expectedString string + }{ + {ICEConnectionState(Unknown), unknownStr}, + {ICEConnectionStateNew, "new"}, + {ICEConnectionStateChecking, "checking"}, + {ICEConnectionStateConnected, "connected"}, + {ICEConnectionStateCompleted, "completed"}, + {ICEConnectionStateDisconnected, "disconnected"}, + {ICEConnectionStateFailed, "failed"}, + {ICEConnectionStateClosed, "closed"}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.expectedString, + testCase.state.String(), + "testCase: %d %v", i, testCase, + ) + } +} diff --git a/icecredentialtype.go b/icecredentialtype.go new file mode 100644 index 00000000000..d1340ac01bb --- /dev/null +++ b/icecredentialtype.go @@ -0,0 +1,43 @@ +package webrtc + +// ICECredentialType indicates the type of credentials used to connect to +// an ICE server. +type ICECredentialType int + +const ( + // ICECredentialTypePassword describes username and pasword based + // credentials as described in https://tools.ietf.org/html/rfc5389. + ICECredentialTypePassword ICECredentialType = iota + + // ICECredentialTypeOauth describes token based credential as described + // in https://tools.ietf.org/html/rfc7635. + ICECredentialTypeOauth +) + +// This is done this way because of a linter. +const ( + iceCredentialTypePasswordStr = "password" + iceCredentialTypeOauthStr = "oauth" +) + +func newICECredentialType(raw string) ICECredentialType { + switch raw { + case iceCredentialTypePasswordStr: + return ICECredentialTypePassword + case iceCredentialTypeOauthStr: + return ICECredentialTypeOauth + default: + return ICECredentialType(Unknown) + } +} + +func (t ICECredentialType) String() string { + switch t { + case ICECredentialTypePassword: + return iceCredentialTypePasswordStr + case ICECredentialTypeOauth: + return iceCredentialTypeOauthStr + default: + return ErrUnknownType.Error() + } +} diff --git a/internal/ice/credentialtype_test.go b/icecredentialtype_test.go similarity index 54% rename from internal/ice/credentialtype_test.go rename to icecredentialtype_test.go index fbcfec67950..fd10c19a2d4 100644 --- a/internal/ice/credentialtype_test.go +++ b/icecredentialtype_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "testing" @@ -6,31 +6,31 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewCredentialType(t *testing.T) { +func TestNewICECredentialType(t *testing.T) { testCases := []struct { credentialTypeString string - expectedCredentialType CredentialType + expectedCredentialType ICECredentialType }{ - {"password", CredentialTypePassword}, - {"oauth", CredentialTypeOauth}, + {"password", ICECredentialTypePassword}, + {"oauth", ICECredentialTypeOauth}, } for i, testCase := range testCases { assert.Equal(t, testCase.expectedCredentialType, - newCredentialType(testCase.credentialTypeString), + newICECredentialType(testCase.credentialTypeString), "testCase: %d %v", i, testCase, ) } } -func TestCredentialType_String(t *testing.T) { +func TestICECredentialType_String(t *testing.T) { testCases := []struct { - credentialType CredentialType + credentialType ICECredentialType expectedString string }{ - {CredentialTypePassword, "password"}, - {CredentialTypeOauth, "oauth"}, + {ICECredentialTypePassword, "password"}, + {ICECredentialTypeOauth, "oauth"}, } for i, testCase := range testCases { diff --git a/internal/ice/gatherer.go b/icegatherer.go similarity index 78% rename from internal/ice/gatherer.go rename to icegatherer.go index d17d64030c9..b5188973a59 100644 --- a/internal/ice/gatherer.go +++ b/icegatherer.go @@ -1,6 +1,6 @@ // +build !js -package ice +package webrtc import ( "sync" @@ -10,13 +10,13 @@ import ( "github.com/pion/logging" ) -// Gatherer gathers local host, server reflexive and relay +// ICEGatherer gathers local host, server reflexive and relay // candidates, as well as enabling the retrieval of local Interactive // Connectivity Establishment (ICE) parameters which can be // exchanged in signaling. -type Gatherer struct { +type ICEGatherer struct { lock sync.RWMutex - state GathererState + state ICEGathererState validatedServers []*ice.URL @@ -37,12 +37,12 @@ type Gatherer struct { log logging.LeveledLogger networkTypes []NetworkType - onLocalCandidateHdlr func(candidate *Candidate) - onStateChangeHdlr func(state GathererState) + onLocalCandidateHdlr func(candidate *ICECandidate) + onStateChangeHdlr func(state ICEGathererState) } -// NewGatherer creates a new Gatherer. -func NewGatherer( +// NewICEGatherer creates a new NewICEGatherer. +func NewICEGatherer( portMin uint16, portMax uint16, connectionTimeout, @@ -55,8 +55,8 @@ func NewGatherer( loggerFactory logging.LoggerFactory, agentIsTrickle bool, networkTypes []NetworkType, - opts GatherOptions, -) (*Gatherer, error) { + opts ICEGatherOptions, +) (*ICEGatherer, error) { var validatedServers []*ice.URL if len(opts.ICEServers) > 0 { for _, server := range opts.ICEServers { @@ -68,13 +68,13 @@ func NewGatherer( } } - candidateTypes := make([]ice.CandidateType, 0) - if opts.ICEGatherPolicy == TransportPolicyRelay { + candidateTypes := []ice.CandidateType{} + if opts.ICEGatherPolicy == ICETransportPolicyRelay { candidateTypes = append(candidateTypes, ice.CandidateTypeRelay) } - return &Gatherer{ - state: GathererStateNew, + return &ICEGatherer{ + state: ICEGathererStateNew, validatedServers: validatedServers, portMin: portMin, portMax: portMax, @@ -93,7 +93,7 @@ func NewGatherer( }, nil } -func (g *Gatherer) createAgent() error { +func (g *ICEGatherer) createAgent() error { g.lock.Lock() defer g.lock.Unlock() @@ -133,14 +133,14 @@ func (g *Gatherer) createAgent() error { g.agent = agent if !g.agentIsTrickle { - g.state = GathererStateComplete + g.state = ICEGathererStateComplete } return nil } // Gather ICE candidates. -func (g *Gatherer) Gather() error { +func (g *ICEGatherer) Gather() error { if err := g.createAgent(); err != nil { return err } @@ -155,17 +155,17 @@ func (g *Gatherer) Gather() error { return nil } - g.setState(GathererStateGathering) + g.setState(ICEGathererStateGathering) if err := agent.OnCandidate(func(candidate ice.Candidate) { if candidate != nil { - c, err := newCandidateFromICE(candidate) + c, err := newICECandidateFromICE(candidate) if err != nil { g.log.Warnf("Failed to convert ice.Candidate: %s", err) return } onLocalCandidateHdlr(&c) } else { - g.setState(GathererStateComplete) + g.setState(ICEGathererStateComplete) onLocalCandidateHdlr(nil) } }); err != nil { @@ -175,7 +175,7 @@ func (g *Gatherer) Gather() error { } // Close prunes all local candidates, and closes the ports. -func (g *Gatherer) Close() error { +func (g *ICEGatherer) Close() error { g.lock.Lock() defer g.lock.Unlock() @@ -192,22 +192,22 @@ func (g *Gatherer) Close() error { return nil } -// GetLocalParameters returns the ICE parameters of the Gatherer. -func (g *Gatherer) GetLocalParameters() (Parameters, error) { +// GetLocalParameters returns the ICE parameters of the ICEGatherer. +func (g *ICEGatherer) GetLocalParameters() (ICEParameters, error) { if err := g.createAgent(); err != nil { - return Parameters{}, err + return ICEParameters{}, err } frag, pwd := g.agent.GetLocalUserCredentials() - return Parameters{ + return ICEParameters{ UsernameFragment: frag, Password: pwd, ICELite: false, }, nil } -// GetLocalCandidates returns the sequence of valid local candidates associated with the Gatherer. -func (g *Gatherer) GetLocalCandidates() ([]Candidate, error) { +// GetLocalCandidates returns the sequence of valid local candidates associated with the ICEGatherer. +func (g *ICEGatherer) GetLocalCandidates() ([]ICECandidate, error) { if err := g.createAgent(); err != nil { return nil, err } @@ -216,31 +216,31 @@ func (g *Gatherer) GetLocalCandidates() ([]Candidate, error) { return nil, err } - return newCandidatesFromICE(iceCandidates) + return newICECandidatesFromICE(iceCandidates) } // OnLocalCandidate sets an event handler which fires when a new local ICE candidate is available -func (g *Gatherer) OnLocalCandidate(f func(*Candidate)) { +func (g *ICEGatherer) OnLocalCandidate(f func(*ICECandidate)) { g.lock.Lock() defer g.lock.Unlock() g.onLocalCandidateHdlr = f } -// OnStateChange fires any time the Gatherer changes -func (g *Gatherer) OnStateChange(f func(GathererState)) { +// OnStateChange fires any time the ICEGatherer changes +func (g *ICEGatherer) OnStateChange(f func(ICEGathererState)) { g.lock.Lock() defer g.lock.Unlock() g.onStateChangeHdlr = f } // State indicates the current state of the ICE gatherer. -func (g *Gatherer) State() GathererState { +func (g *ICEGatherer) State() ICEGathererState { g.lock.RLock() defer g.lock.RUnlock() return g.state } -func (g *Gatherer) setState(s GathererState) { +func (g *ICEGatherer) setState(s ICEGathererState) { g.lock.Lock() g.state = s hdlr := g.onStateChangeHdlr @@ -251,7 +251,7 @@ func (g *Gatherer) setState(s GathererState) { } } -func (g *Gatherer) getAgent() *ice.Agent { +func (g *ICEGatherer) getAgent() *ice.Agent { g.lock.RLock() defer g.lock.RUnlock() return g.agent @@ -259,7 +259,7 @@ func (g *Gatherer) getAgent() *ice.Agent { // SignalCandidates imitates gathering process to backward support old tricle // false behavior. -func (g *Gatherer) SignalCandidates() error { +func (g *ICEGatherer) SignalCandidates() error { candidates, err := g.GetLocalCandidates() if err != nil { return err @@ -279,8 +279,3 @@ func (g *Gatherer) SignalCandidates() error { } return nil } - -// AgentIsTrickle returns true if agent is in trickle mode. -func (g *Gatherer) AgentIsTrickle() bool { - return g.agentIsTrickle -} diff --git a/internal/ice/gatherer_test.go b/icegatherer_test.go similarity index 70% rename from internal/ice/gatherer_test.go rename to icegatherer_test.go index 3b41b23aec9..b80261d8f8b 100644 --- a/internal/ice/gatherer_test.go +++ b/icegatherer_test.go @@ -1,6 +1,6 @@ // +build !js -package ice +package webrtc import ( "testing" @@ -10,7 +10,7 @@ import ( "github.com/pion/transport/test" ) -func TestGatherer_Success(t *testing.T) { +func TestNewICEGatherer_Success(t *testing.T) { // Limit runtime in case of deadlocks lim := test.TimeOut(time.Second * 20) defer lim.Stop() @@ -18,16 +18,16 @@ func TestGatherer_Success(t *testing.T) { report := test.CheckRoutines(t) defer report() - opts := GatherOptions{ - ICEServers: []Server{{URLs: []string{"stun:stun.l.google.com:19302"}}}, + opts := ICEGatherOptions{ + ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}, } - gatherer, err := NewGatherer(0, 0, nil, nil, nil, nil, nil, nil, nil, logging.NewDefaultLoggerFactory(), false, nil, opts) + gatherer, err := NewICEGatherer(0, 0, nil, nil, nil, nil, nil, nil, nil, logging.NewDefaultLoggerFactory(), false, nil, opts) if err != nil { t.Error(err) } - if gatherer.State() != GathererStateNew { + if gatherer.State() != ICEGathererStateNew { t.Fatalf("Expected gathering state new") } diff --git a/icegathererstate.go b/icegathererstate.go new file mode 100644 index 00000000000..56133d38102 --- /dev/null +++ b/icegathererstate.go @@ -0,0 +1,36 @@ +package webrtc + +// ICEGathererState represents the current state of the ICE gatherer. +type ICEGathererState byte + +const ( + // ICEGathererStateNew indicates object has been created but + // gather() has not been called. + ICEGathererStateNew ICEGathererState = iota + 1 + + // ICEGathererStateGathering indicates gather() has been called, + // and the ICEGatherer is in the process of gathering candidates. + ICEGathererStateGathering + + // ICEGathererStateComplete indicates the ICEGatherer has completed gathering. + ICEGathererStateComplete + + // ICEGathererStateClosed indicates the closed state can only be entered + // when the ICEGatherer has been closed intentionally by calling close(). + ICEGathererStateClosed +) + +func (s ICEGathererState) String() string { + switch s { + case ICEGathererStateNew: + return "new" + case ICEGathererStateGathering: + return "gathering" + case ICEGathererStateComplete: + return "complete" + case ICEGathererStateClosed: + return "closed" + default: + return unknownStr + } +} diff --git a/icegathererstate_test.go b/icegathererstate_test.go new file mode 100644 index 00000000000..7dd2eb80454 --- /dev/null +++ b/icegathererstate_test.go @@ -0,0 +1,28 @@ +package webrtc + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestICEGathererState_String(t *testing.T) { + testCases := []struct { + state ICEGathererState + expectedString string + }{ + {ICEGathererState(Unknown), unknownStr}, + {ICEGathererStateNew, "new"}, + {ICEGathererStateGathering, "gathering"}, + {ICEGathererStateComplete, "complete"}, + {ICEGathererStateClosed, "closed"}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.expectedString, + testCase.state.String(), + "testCase: %d %v", i, testCase, + ) + } +} diff --git a/icegatheringstate.go b/icegatheringstate.go new file mode 100644 index 00000000000..21361f912e0 --- /dev/null +++ b/icegatheringstate.go @@ -0,0 +1,53 @@ +package webrtc + +// ICEGatheringState describes the state of the candidate gathering process. +type ICEGatheringState int + +const ( + // ICEGatheringStateNew indicates that any of the ICETransports are + // in the "new" gathering state and none of the transports are in the + // "gathering" state, or there are no transports. + ICEGatheringStateNew ICEGatheringState = iota + 1 + + // ICEGatheringStateGathering indicates that any of the ICETransports + // are in the "gathering" state. + ICEGatheringStateGathering + + // ICEGatheringStateComplete indicates that at least one ICETransport + // exists, and all ICETransports are in the "completed" gathering state. + ICEGatheringStateComplete +) + +// This is done this way because of a linter. +const ( + iceGatheringStateNewStr = "new" + iceGatheringStateGatheringStr = "gathering" + iceGatheringStateCompleteStr = "complete" +) + +// NewICEGatheringState takes a string and converts it to ICEGatheringState +func NewICEGatheringState(raw string) ICEGatheringState { + switch raw { + case iceGatheringStateNewStr: + return ICEGatheringStateNew + case iceGatheringStateGatheringStr: + return ICEGatheringStateGathering + case iceGatheringStateCompleteStr: + return ICEGatheringStateComplete + default: + return ICEGatheringState(Unknown) + } +} + +func (t ICEGatheringState) String() string { + switch t { + case ICEGatheringStateNew: + return iceGatheringStateNewStr + case ICEGatheringStateGathering: + return iceGatheringStateGatheringStr + case ICEGatheringStateComplete: + return iceGatheringStateCompleteStr + default: + return ErrUnknownType.Error() + } +} diff --git a/icegatheringstate_test.go b/icegatheringstate_test.go new file mode 100644 index 00000000000..bc5a3330151 --- /dev/null +++ b/icegatheringstate_test.go @@ -0,0 +1,47 @@ +package webrtc + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewICEGatheringState(t *testing.T) { + testCases := []struct { + stateString string + expectedState ICEGatheringState + }{ + {unknownStr, ICEGatheringState(Unknown)}, + {"new", ICEGatheringStateNew}, + {"gathering", ICEGatheringStateGathering}, + {"complete", ICEGatheringStateComplete}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.expectedState, + NewICEGatheringState(testCase.stateString), + "testCase: %d %v", i, testCase, + ) + } +} + +func TestICEGatheringState_String(t *testing.T) { + testCases := []struct { + state ICEGatheringState + expectedString string + }{ + {ICEGatheringState(Unknown), unknownStr}, + {ICEGatheringStateNew, "new"}, + {ICEGatheringStateGathering, "gathering"}, + {ICEGatheringStateComplete, "complete"}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.expectedString, + testCase.state.String(), + "testCase: %d %v", i, testCase, + ) + } +} diff --git a/icegatheroptions.go b/icegatheroptions.go new file mode 100644 index 00000000000..88421c74e5f --- /dev/null +++ b/icegatheroptions.go @@ -0,0 +1,7 @@ +package webrtc + +// ICEGatherOptions provides options relating to the gathering of ICE candidates. +type ICEGatherOptions struct { + ICEServers []ICEServer + ICEGatherPolicy ICETransportPolicy +} diff --git a/internal/ice/parameters.go b/iceparameters.go similarity index 66% rename from internal/ice/parameters.go rename to iceparameters.go index 82d340738d3..0c03a88bf2f 100644 --- a/internal/ice/parameters.go +++ b/iceparameters.go @@ -1,8 +1,8 @@ -package ice +package webrtc -// Parameters includes the ICE username fragment +// ICEParameters includes the ICE username fragment // and password and other ICE-related parameters. -type Parameters struct { +type ICEParameters struct { UsernameFragment string `json:"usernameFragment"` Password string `json:"password"` ICELite bool `json:"iceLite"` diff --git a/iceprotocol.go b/iceprotocol.go new file mode 100644 index 00000000000..41c7811a4c4 --- /dev/null +++ b/iceprotocol.go @@ -0,0 +1,47 @@ +package webrtc + +import ( + "fmt" + "strings" +) + +// ICEProtocol indicates the transport protocol type that is used in the +// ice.URL structure. +type ICEProtocol int + +const ( + // ICEProtocolUDP indicates the URL uses a UDP transport. + ICEProtocolUDP ICEProtocol = iota + 1 + + // ICEProtocolTCP indicates the URL uses a TCP transport. + ICEProtocolTCP +) + +// This is done this way because of a linter. +const ( + iceProtocolUDPStr = "udp" + iceProtocolTCPStr = "tcp" +) + +// NewICEProtocol takes a string and converts it to ICEProtocol +func NewICEProtocol(raw string) (ICEProtocol, error) { + switch { + case strings.EqualFold(iceProtocolUDPStr, raw): + return ICEProtocolUDP, nil + case strings.EqualFold(iceProtocolTCPStr, raw): + return ICEProtocolTCP, nil + default: + return ICEProtocol(Unknown), fmt.Errorf("unknown protocol: %s", raw) + } +} + +func (t ICEProtocol) String() string { + switch t { + case ICEProtocolUDP: + return iceProtocolUDPStr + case ICEProtocolTCP: + return iceProtocolTCPStr + default: + return ErrUnknownType.Error() + } +} diff --git a/internal/ice/protocol_test.go b/iceprotocol_test.go similarity index 53% rename from internal/ice/protocol_test.go rename to iceprotocol_test.go index aaf96181ed7..ea7e59382df 100644 --- a/internal/ice/protocol_test.go +++ b/iceprotocol_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "testing" @@ -6,21 +6,21 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewProtocol(t *testing.T) { +func TestNewICEProtocol(t *testing.T) { testCases := []struct { protoString string shouldFail bool - expectedProto Protocol + expectedProto ICEProtocol }{ - {unknownStr, true, Protocol(Unknown)}, - {"udp", false, ProtocolUDP}, - {"tcp", false, ProtocolTCP}, - {"UDP", false, ProtocolUDP}, - {"TCP", false, ProtocolTCP}, + {unknownStr, true, ICEProtocol(Unknown)}, + {"udp", false, ICEProtocolUDP}, + {"tcp", false, ICEProtocolTCP}, + {"UDP", false, ICEProtocolUDP}, + {"TCP", false, ICEProtocolTCP}, } for i, testCase := range testCases { - actual, err := NewProtocol(testCase.protoString) + actual, err := NewICEProtocol(testCase.protoString) if (err != nil) != testCase.shouldFail { t.Error(err) } @@ -32,14 +32,14 @@ func TestNewProtocol(t *testing.T) { } } -func TestProtocol_String(t *testing.T) { +func TestICEProtocol_String(t *testing.T) { testCases := []struct { - proto Protocol + proto ICEProtocol expectedString string }{ - {Protocol(Unknown), unknownStr}, - {ProtocolUDP, "udp"}, - {ProtocolTCP, "tcp"}, + {ICEProtocol(Unknown), unknownStr}, + {ICEProtocolUDP, "udp"}, + {ICEProtocolTCP, "tcp"}, } for i, testCase := range testCases { diff --git a/icerole.go b/icerole.go new file mode 100644 index 00000000000..11187863b16 --- /dev/null +++ b/icerole.go @@ -0,0 +1,45 @@ +package webrtc + +// ICERole describes the role ice.Agent is playing in selecting the +// preferred the candidate pair. +type ICERole int + +const ( + // ICERoleControlling indicates that the ICE agent that is responsible + // for selecting the final choice of candidate pairs and signaling them + // through STUN and an updated offer, if needed. In any session, one agent + // is always controlling. The other is the controlled agent. + ICERoleControlling ICERole = iota + 1 + + // ICERoleControlled indicates that an ICE agent that waits for the + // controlling agent to select the final choice of candidate pairs. + ICERoleControlled +) + +// This is done this way because of a linter. +const ( + iceRoleControllingStr = "controlling" + iceRoleControlledStr = "controlled" +) + +func newICERole(raw string) ICERole { + switch raw { + case iceRoleControllingStr: + return ICERoleControlling + case iceRoleControlledStr: + return ICERoleControlled + default: + return ICERole(Unknown) + } +} + +func (t ICERole) String() string { + switch t { + case ICERoleControlling: + return iceRoleControllingStr + case ICERoleControlled: + return iceRoleControlledStr + default: + return ErrUnknownType.Error() + } +} diff --git a/internal/ice/role_test.go b/icerole_test.go similarity index 53% rename from internal/ice/role_test.go rename to icerole_test.go index 21fbc7e1d91..4e9f9c484d1 100644 --- a/internal/ice/role_test.go +++ b/icerole_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "testing" @@ -6,33 +6,33 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewRole(t *testing.T) { +func TestNewICERole(t *testing.T) { testCases := []struct { roleString string - expectedRole Role + expectedRole ICERole }{ - {unknownStr, Role(Unknown)}, - {"controlling", RoleControlling}, - {"controlled", RoleControlled}, + {unknownStr, ICERole(Unknown)}, + {"controlling", ICERoleControlling}, + {"controlled", ICERoleControlled}, } for i, testCase := range testCases { assert.Equal(t, testCase.expectedRole, - newRole(testCase.roleString), + newICERole(testCase.roleString), "testCase: %d %v", i, testCase, ) } } -func TestRole_String(t *testing.T) { +func TestICERole_String(t *testing.T) { testCases := []struct { - proto Role + proto ICERole expectedString string }{ - {Role(Unknown), unknownStr}, - {RoleControlling, "controlling"}, - {RoleControlled, "controlled"}, + {ICERole(Unknown), unknownStr}, + {ICERoleControlling, "controlling"}, + {ICERoleControlled, "controlled"}, } for i, testCase := range testCases { diff --git a/internal/ice/server.go b/iceserver.go similarity index 77% rename from internal/ice/server.go rename to iceserver.go index e442f067934..e9f3b5ae885 100644 --- a/internal/ice/server.go +++ b/iceserver.go @@ -1,32 +1,31 @@ // +build !js -package ice +package webrtc import ( "github.com/pion/ice" "github.com/pion/webrtc/v2/pkg/rtcerr" ) -// Server describes a single STUN and TURN server that can be used by +// ICEServer describes a single STUN and TURN server that can be used by // the ICEAgent to establish a connection with a peer. -type Server struct { +type ICEServer struct { URLs []string Username string Credential interface{} - CredentialType CredentialType + CredentialType ICECredentialType } -func (s Server) parseURL(i int) (*ice.URL, error) { +func (s ICEServer) parseURL(i int) (*ice.URL, error) { return ice.ParseURL(s.URLs[i]) } -// Validate checks if the Server struct is valid -func (s Server) Validate() error { +func (s ICEServer) validate() error { _, err := s.urls() return err } -func (s Server) urls() ([]*ice.URL, error) { +func (s ICEServer) urls() ([]*ice.URL, error) { urls := []*ice.URL{} for i := range s.URLs { @@ -43,7 +42,7 @@ func (s Server) urls() ([]*ice.URL, error) { url.Username = s.Username switch s.CredentialType { - case CredentialTypePassword: + case ICECredentialTypePassword: // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.3) password, ok := s.Credential.(string) if !ok { @@ -51,7 +50,7 @@ func (s Server) urls() ([]*ice.URL, error) { } url.Password = password - case CredentialTypeOauth: + case ICECredentialTypeOauth: // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.4) if _, ok := s.Credential.(OAuthCredential); !ok { return nil, &rtcerr.InvalidAccessError{Err: ErrTurnCredencials} diff --git a/internal/ice/server_js.go b/iceserver_js.go similarity index 76% rename from internal/ice/server_js.go rename to iceserver_js.go index 57a4f774e91..227014acbfa 100644 --- a/internal/ice/server_js.go +++ b/iceserver_js.go @@ -1,6 +1,6 @@ // +build js,wasm -package ice +package webrtc import ( "errors" @@ -8,27 +8,21 @@ import ( "github.com/pion/ice" ) -// Server describes a single STUN and TURN server that can be used by +// ICEServer describes a single STUN and TURN server that can be used by // the ICEAgent to establish a connection with a peer. -type Server struct { +type ICEServer struct { URLs []string Username string // Note: Credential and CredentialType are not supported. // Credential interface{} - // CredentialType CredentialType + // CredentialType ICECredentialType } -func (s Server) parseURL(i int) (*ice.URL, error) { +func (s ICEServer) parseURL(i int) (*ice.URL, error) { return ice.ParseURL(s.URLs[i]) } -// Validate checks if the Server struct is valid -func (s Server) Validate() error { - _, err := s.urls() - return err -} - -func (s Server) urls() ([]*ice.URL, error) { +func (s ICEServer) validate() ([]*ice.URL, error) { urls := []*ice.URL{} for i := range s.URLs { @@ -44,13 +38,13 @@ func (s Server) urls() ([]*ice.URL, error) { // } // switch s.CredentialType { - // case CredentialTypePassword: + // case ICECredentialTypePassword: // // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.3) // if _, ok := s.Credential.(string); !ok { // return nil, &rtcerr.InvalidAccessError{Err: ErrTurnCredencials} // } - // case CredentialTypeOauth: + // case ICECredentialTypeOauth: // // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.4) // if _, ok := s.Credential.(OAuthCredential); !ok { // return nil, &rtcerr.InvalidAccessError{Err: ErrTurnCredencials} diff --git a/internal/ice/server_test.go b/iceserver_test.go similarity index 78% rename from internal/ice/server_test.go rename to iceserver_test.go index 5ec0766a81d..cd819c3ca11 100644 --- a/internal/ice/server_test.go +++ b/iceserver_test.go @@ -1,6 +1,6 @@ // +build !js -package ice +package webrtc import ( "testing" @@ -10,70 +10,70 @@ import ( "github.com/stretchr/testify/assert" ) -func TestServer_validate(t *testing.T) { +func TestICEServer_validate(t *testing.T) { t.Run("Success", func(t *testing.T) { testCases := []struct { - server Server + iceServer ICEServer expectedValidate bool }{ - {Server{ + {ICEServer{ URLs: []string{"turn:192.158.29.39?transport=udp"}, Username: "unittest", Credential: "placeholder", - CredentialType: CredentialTypePassword, + CredentialType: ICECredentialTypePassword, }, true}, - {Server{ + {ICEServer{ URLs: []string{"turn:192.158.29.39?transport=udp"}, Username: "unittest", Credential: OAuthCredential{ MACKey: "WmtzanB3ZW9peFhtdm42NzUzNG0=", AccessToken: "AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ5VhNDgeMR3+ZlZ35byg972fW8QjpEl7bx91YLBPFsIhsxloWcXPhA==", }, - CredentialType: CredentialTypeOauth, + CredentialType: ICECredentialTypeOauth, }, true}, } for i, testCase := range testCases { - _, err := testCase.server.urls() + _, err := testCase.iceServer.urls() assert.Nil(t, err, "testCase: %d %v", i, testCase) } }) t.Run("Failure", func(t *testing.T) { testCases := []struct { - server Server + iceServer ICEServer expectedErr error }{ - {Server{ + {ICEServer{ URLs: []string{"turn:192.158.29.39?transport=udp"}, }, &rtcerr.InvalidAccessError{Err: ErrNoTurnCredencials}}, - {Server{ + {ICEServer{ URLs: []string{"turn:192.158.29.39?transport=udp"}, Username: "unittest", Credential: false, - CredentialType: CredentialTypePassword, + CredentialType: ICECredentialTypePassword, }, &rtcerr.InvalidAccessError{Err: ErrTurnCredencials}}, - {Server{ + {ICEServer{ URLs: []string{"turn:192.158.29.39?transport=udp"}, Username: "unittest", Credential: false, - CredentialType: CredentialTypeOauth, + CredentialType: ICECredentialTypeOauth, }, &rtcerr.InvalidAccessError{Err: ErrTurnCredencials}}, - {Server{ + {ICEServer{ URLs: []string{"turn:192.158.29.39?transport=udp"}, Username: "unittest", Credential: false, CredentialType: Unknown, }, &rtcerr.InvalidAccessError{Err: ErrTurnCredencials}}, - {Server{ + {ICEServer{ URLs: []string{"stun:google.de?transport=udp"}, Username: "unittest", Credential: false, - CredentialType: CredentialTypeOauth, + CredentialType: ICECredentialTypeOauth, }, ice.ErrSTUNQuery}, } for i, testCase := range testCases { - _, err := testCase.server.urls() + _, err := testCase.iceServer.urls() assert.EqualError(t, err, testCase.expectedErr.Error(), diff --git a/internal/ice/transport.go b/icetransport.go similarity index 64% rename from internal/ice/transport.go rename to icetransport.go index afcad4cb903..9bdfae2c909 100644 --- a/internal/ice/transport.go +++ b/icetransport.go @@ -1,6 +1,6 @@ // +build !js -package ice +package webrtc import ( "context" @@ -9,26 +9,25 @@ import ( "github.com/pion/ice" "github.com/pion/logging" - "github.com/pion/webrtc/v2/internal/mux" ) -// Transport allows an application access to information about the ICE +// ICETransport allows an application access to information about the ICE // transport over which packets are sent and received. -type Transport struct { +type ICETransport struct { lock sync.RWMutex - role Role - // Component Component - // State TransportState - // gatheringState GathererState + role ICERole + // Component ICEComponent + // State ICETransportState + // gatheringState ICEGathererState - onConnectionStateChangeHdlr func(TransportState) - onSelectedCandidatePairChangeHdlr func(*CandidatePair) + onConnectionStateChangeHdlr func(ICETransportState) + onSelectedCandidatePairChangeHdlr func(*ICECandidatePair) - state TransportState + state ICETransportState - gatherer *Gatherer + gatherer *ICEGatherer conn *ice.Conn mux *mux.Mux @@ -37,38 +36,38 @@ type Transport struct { log logging.LeveledLogger } -// func (t *Transport) GetLocalCandidates() []Candidate { +// func (t *ICETransport) GetLocalCandidates() []ICECandidate { // // } // -// func (t *Transport) GetRemoteCandidates() []Candidate { +// func (t *ICETransport) GetRemoteCandidates() []ICECandidate { // // } // -// func (t *Transport) GetSelectedCandidatePair() CandidatePair { +// func (t *ICETransport) GetSelectedCandidatePair() ICECandidatePair { // // } // -// func (t *Transport) GetLocalParameters() Parameters { +// func (t *ICETransport) GetLocalParameters() ICEParameters { // // } // -// func (t *Transport) GetRemoteParameters() Parameters { +// func (t *ICETransport) GetRemoteParameters() ICEParameters { // // } -// NewTransport creates a new NewTransport. -func NewTransport(gatherer *Gatherer, loggerFactory logging.LoggerFactory) *Transport { - return &Transport{ +// NewICETransport creates a new NewICETransport. +func NewICETransport(gatherer *ICEGatherer, loggerFactory logging.LoggerFactory) *ICETransport { + return &ICETransport{ gatherer: gatherer, loggerFactory: loggerFactory, log: loggerFactory.NewLogger("ortc"), - state: TransportStateNew, + state: ICETransportStateNew, } } // Start incoming connectivity checks based on its configured role. -func (t *Transport) Start(gatherer *Gatherer, params Parameters, role *Role) error { +func (t *ICETransport) Start(gatherer *ICEGatherer, params ICEParameters, role *ICERole) error { t.lock.Lock() defer t.lock.Unlock() @@ -82,7 +81,7 @@ func (t *Transport) Start(gatherer *Gatherer, params Parameters, role *Role) err agent := t.gatherer.agent if err := agent.OnConnectionStateChange(func(iceState ice.ConnectionState) { - state := newTransportStateFromICE(iceState) + state := newICETransportStateFromICE(iceState) t.lock.Lock() t.state = state t.lock.Unlock() @@ -92,18 +91,18 @@ func (t *Transport) Start(gatherer *Gatherer, params Parameters, role *Role) err return err } if err := agent.OnSelectedCandidatePairChange(func(local, remote ice.Candidate) { - candidates, err := newCandidatesFromICE([]ice.Candidate{local, remote}) + candidates, err := newICECandidatesFromICE([]ice.Candidate{local, remote}) if err != nil { t.log.Warnf("Unable to convert ICE candidates to ICECandidates: %s", err) return } - t.onSelectedCandidatePairChange(NewCandidatePair(&candidates[0], &candidates[1])) + t.onSelectedCandidatePairChange(NewICECandidatePair(&candidates[0], &candidates[1])) }); err != nil { return err } if role == nil { - controlled := RoleControlled + controlled := ICERoleControlled role = &controlled } t.role = *role @@ -115,12 +114,12 @@ func (t *Transport) Start(gatherer *Gatherer, params Parameters, role *Role) err var iceConn *ice.Conn var err error switch *role { - case RoleControlling: + case ICERoleControlling: iceConn, err = agent.Dial(context.TODO(), params.UsernameFragment, params.Password) - case RoleControlled: + case ICERoleControlled: iceConn, err = agent.Accept(context.TODO(), params.UsernameFragment, params.Password) @@ -147,8 +146,8 @@ func (t *Transport) Start(gatherer *Gatherer, params Parameters, role *Role) err return nil } -// Stop irreversibly stops the Transport. -func (t *Transport) Stop() error { +// Stop irreversibly stops the ICETransport. +func (t *ICETransport) Stop() error { t.lock.Lock() defer t.lock.Unlock() @@ -162,13 +161,13 @@ func (t *Transport) Stop() error { // OnSelectedCandidatePairChange sets a handler that is invoked when a new // ICE candidate pair is selected -func (t *Transport) OnSelectedCandidatePairChange(f func(*CandidatePair)) { +func (t *ICETransport) OnSelectedCandidatePairChange(f func(*ICECandidatePair)) { t.lock.Lock() defer t.lock.Unlock() t.onSelectedCandidatePairChangeHdlr = f } -func (t *Transport) onSelectedCandidatePairChange(pair *CandidatePair) { +func (t *ICETransport) onSelectedCandidatePairChange(pair *ICECandidatePair) { t.lock.RLock() hdlr := t.onSelectedCandidatePairChangeHdlr t.lock.RUnlock() @@ -179,13 +178,13 @@ func (t *Transport) onSelectedCandidatePairChange(pair *CandidatePair) { // OnConnectionStateChange sets a handler that is fired when the ICE // connection state changes. -func (t *Transport) OnConnectionStateChange(f func(TransportState)) { +func (t *ICETransport) OnConnectionStateChange(f func(ICETransportState)) { t.lock.Lock() defer t.lock.Unlock() t.onConnectionStateChangeHdlr = f } -func (t *Transport) onConnectionStateChange(state TransportState) { +func (t *ICETransport) onConnectionStateChange(state ICETransportState) { t.lock.RLock() hdlr := t.onConnectionStateChangeHdlr t.lock.RUnlock() @@ -195,15 +194,15 @@ func (t *Transport) onConnectionStateChange(state TransportState) { } // Role indicates the current role of the ICE transport. -func (t *Transport) Role() Role { +func (t *ICETransport) Role() ICERole { t.lock.RLock() defer t.lock.RUnlock() return t.role } -// SetRemoteCandidates sets the sequence of candidates associated with the remote Transport. -func (t *Transport) SetRemoteCandidates(remoteCandidates []Candidate) error { +// SetRemoteCandidates sets the sequence of candidates associated with the remote ICETransport. +func (t *ICETransport) SetRemoteCandidates(remoteCandidates []ICECandidate) error { t.lock.RLock() defer t.lock.RUnlock() @@ -225,8 +224,8 @@ func (t *Transport) SetRemoteCandidates(remoteCandidates []Candidate) error { return nil } -// AddRemoteCandidate adds a candidate associated with the remote Transport. -func (t *Transport) AddRemoteCandidate(remoteCandidate Candidate) error { +// AddRemoteCandidate adds a candidate associated with the remote ICETransport. +func (t *ICETransport) AddRemoteCandidate(remoteCandidate ICECandidate) error { t.lock.RLock() defer t.lock.RUnlock() @@ -247,23 +246,23 @@ func (t *Transport) AddRemoteCandidate(remoteCandidate Candidate) error { } // State returns the current ice transport state. -func (t *Transport) State() TransportState { +func (t *ICETransport) State() ICETransportState { t.lock.RLock() defer t.lock.RUnlock() return t.state } // NewEndpoint registers a new endpoint on the underlying mux. -func (t *Transport) NewEndpoint(f mux.MatchFunc) *mux.Endpoint { +func (t *ICETransport) NewEndpoint(f mux.MatchFunc) *mux.Endpoint { t.lock.Lock() defer t.lock.Unlock() return t.mux.NewEndpoint(f) } -func (t *Transport) ensureGatherer() error { +func (t *ICETransport) ensureGatherer() error { if t.gatherer == nil { return errors.New("gatherer not started") - } else if t.gatherer.getAgent() == nil && t.gatherer.AgentIsTrickle() { + } else if t.gatherer.getAgent() == nil && t.gatherer.agentIsTrickle { // Special case for trickle=true. (issue-707) if err := t.gatherer.createAgent(); err != nil { return err diff --git a/icetransportpolicy.go b/icetransportpolicy.go new file mode 100644 index 00000000000..679de978bee --- /dev/null +++ b/icetransportpolicy.go @@ -0,0 +1,46 @@ +package webrtc + +// ICETransportPolicy defines the ICE candidate policy surface the +// permitted candidates. Only these candidates are used for connectivity checks. +type ICETransportPolicy int + +// ICEGatherPolicy is the ORTC equivalent of ICETransportPolicy +type ICEGatherPolicy = ICETransportPolicy + +const ( + // ICETransportPolicyAll indicates any type of candidate is used. + ICETransportPolicyAll ICETransportPolicy = iota + + // ICETransportPolicyRelay indicates only media relay candidates such + // as candidates passing through a TURN server are used. + ICETransportPolicyRelay +) + +// This is done this way because of a linter. +const ( + iceTransportPolicyRelayStr = "relay" + iceTransportPolicyAllStr = "all" +) + +// NewICETransportPolicy takes a string and converts it to ICETransportPolicy +func NewICETransportPolicy(raw string) ICETransportPolicy { + switch raw { + case iceTransportPolicyRelayStr: + return ICETransportPolicyRelay + case iceTransportPolicyAllStr: + return ICETransportPolicyAll + default: + return ICETransportPolicy(Unknown) + } +} + +func (t ICETransportPolicy) String() string { + switch t { + case ICETransportPolicyRelay: + return iceTransportPolicyRelayStr + case ICETransportPolicyAll: + return iceTransportPolicyAllStr + default: + return ErrUnknownType.Error() + } +} diff --git a/internal/ice/transportpolicy_test.go b/icetransportpolicy_test.go similarity index 55% rename from internal/ice/transportpolicy_test.go rename to icetransportpolicy_test.go index 1e896bc9864..897f1754cd8 100644 --- a/internal/ice/transportpolicy_test.go +++ b/icetransportpolicy_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "testing" @@ -6,31 +6,31 @@ import ( "github.com/stretchr/testify/assert" ) -func TestNewTransportPolicy(t *testing.T) { +func TestNewICETransportPolicy(t *testing.T) { testCases := []struct { policyString string - expectedPolicy TransportPolicy + expectedPolicy ICETransportPolicy }{ - {"relay", TransportPolicyRelay}, - {"all", TransportPolicyAll}, + {"relay", ICETransportPolicyRelay}, + {"all", ICETransportPolicyAll}, } for i, testCase := range testCases { assert.Equal(t, testCase.expectedPolicy, - NewTransportPolicy(testCase.policyString), + NewICETransportPolicy(testCase.policyString), "testCase: %d %v", i, testCase, ) } } -func TestTransportPolicy_String(t *testing.T) { +func TestICETransportPolicy_String(t *testing.T) { testCases := []struct { - policy TransportPolicy + policy ICETransportPolicy expectedString string }{ - {TransportPolicyRelay, "relay"}, - {TransportPolicyAll, "all"}, + {ICETransportPolicyRelay, "relay"}, + {ICETransportPolicyAll, "all"}, } for i, testCase := range testCases { diff --git a/internal/ice/transportstate.go b/icetransportstate.go similarity index 50% rename from internal/ice/transportstate.go rename to icetransportstate.go index cebff780c7d..5aac3c4cc9d 100644 --- a/internal/ice/transportstate.go +++ b/icetransportstate.go @@ -1,105 +1,105 @@ -package ice +package webrtc import "github.com/pion/ice" -// TransportState represents the current state of the ICE transport. -type TransportState int +// ICETransportState represents the current state of the ICE transport. +type ICETransportState int const ( - // TransportStateNew indicates the Transport is waiting + // ICETransportStateNew indicates the ICETransport is waiting // for remote candidates to be supplied. - TransportStateNew = iota + 1 + ICETransportStateNew = iota + 1 - // TransportStateChecking indicates the Transport has + // ICETransportStateChecking indicates the ICETransport has // received at least one remote candidate, and a local and remote // ICECandidateComplete dictionary was not added as the last candidate. - TransportStateChecking + ICETransportStateChecking - // TransportStateConnected indicates the Transport has + // ICETransportStateConnected indicates the ICETransport has // received a response to an outgoing connectivity check, or has // received incoming DTLS/media after a successful response to an // incoming connectivity check, but is still checking other candidate // pairs to see if there is a better connection. - TransportStateConnected + ICETransportStateConnected - // TransportStateCompleted indicates the Transport tested + // ICETransportStateCompleted indicates the ICETransport tested // all appropriate candidate pairs and at least one functioning // candidate pair has been found. - TransportStateCompleted + ICETransportStateCompleted - // TransportStateFailed indicates the Transport the last + // ICETransportStateFailed indicates the ICETransport the last // candidate was added and all appropriate candidate pairs have either // failed connectivity checks or have lost consent. - TransportStateFailed + ICETransportStateFailed - // TransportStateDisconnected indicates the Transport has received + // ICETransportStateDisconnected indicates the ICETransport has received // at least one local and remote candidate, but the final candidate was // received yet and all appropriate candidate pairs thus far have been // tested and failed. - TransportStateDisconnected + ICETransportStateDisconnected - // TransportStateClosed indicates the Transport has shut down + // ICETransportStateClosed indicates the ICETransport has shut down // and is no longer responding to STUN requests. - TransportStateClosed + ICETransportStateClosed ) -func (c TransportState) String() string { +func (c ICETransportState) String() string { switch c { - case TransportStateNew: + case ICETransportStateNew: return "new" - case TransportStateChecking: + case ICETransportStateChecking: return "checking" - case TransportStateConnected: + case ICETransportStateConnected: return "connected" - case TransportStateCompleted: + case ICETransportStateCompleted: return "completed" - case TransportStateFailed: + case ICETransportStateFailed: return "failed" - case TransportStateDisconnected: + case ICETransportStateDisconnected: return "disconnected" - case TransportStateClosed: + case ICETransportStateClosed: return "closed" default: return unknownStr } } -func newTransportStateFromICE(i ice.ConnectionState) TransportState { +func newICETransportStateFromICE(i ice.ConnectionState) ICETransportState { switch i { case ice.ConnectionStateNew: - return TransportStateNew + return ICETransportStateNew case ice.ConnectionStateChecking: - return TransportStateChecking + return ICETransportStateChecking case ice.ConnectionStateConnected: - return TransportStateConnected + return ICETransportStateConnected case ice.ConnectionStateCompleted: - return TransportStateCompleted + return ICETransportStateCompleted case ice.ConnectionStateFailed: - return TransportStateFailed + return ICETransportStateFailed case ice.ConnectionStateDisconnected: - return TransportStateDisconnected + return ICETransportStateDisconnected case ice.ConnectionStateClosed: - return TransportStateClosed + return ICETransportStateClosed default: - return TransportState(Unknown) + return ICETransportState(Unknown) } } -func (c TransportState) toICE() ice.ConnectionState { +func (c ICETransportState) toICE() ice.ConnectionState { switch c { - case TransportStateNew: + case ICETransportStateNew: return ice.ConnectionStateNew - case TransportStateChecking: + case ICETransportStateChecking: return ice.ConnectionStateChecking - case TransportStateConnected: + case ICETransportStateConnected: return ice.ConnectionStateConnected - case TransportStateCompleted: + case ICETransportStateCompleted: return ice.ConnectionStateCompleted - case TransportStateFailed: + case ICETransportStateFailed: return ice.ConnectionStateFailed - case TransportStateDisconnected: + case ICETransportStateDisconnected: return ice.ConnectionStateDisconnected - case TransportStateClosed: + case ICETransportStateClosed: return ice.ConnectionStateClosed default: return ice.ConnectionState(Unknown) diff --git a/icetransportstate_test.go b/icetransportstate_test.go new file mode 100644 index 00000000000..62c5dfa6252 --- /dev/null +++ b/icetransportstate_test.go @@ -0,0 +1,61 @@ +package webrtc + +import ( + "testing" + + "github.com/pion/ice" + "github.com/stretchr/testify/assert" +) + +func TestICETransportState_String(t *testing.T) { + testCases := []struct { + state ICETransportState + expectedString string + }{ + {ICETransportState(Unknown), unknownStr}, + {ICETransportStateNew, "new"}, + {ICETransportStateChecking, "checking"}, + {ICETransportStateConnected, "connected"}, + {ICETransportStateCompleted, "completed"}, + {ICETransportStateFailed, "failed"}, + {ICETransportStateDisconnected, "disconnected"}, + {ICETransportStateClosed, "closed"}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.expectedString, + testCase.state.String(), + "testCase: %d %v", i, testCase, + ) + } +} + +func TestICETransportState_Convert(t *testing.T) { + testCases := []struct { + native ICETransportState + ice ice.ConnectionState + }{ + {ICETransportState(Unknown), ice.ConnectionState(Unknown)}, + {ICETransportStateNew, ice.ConnectionStateNew}, + {ICETransportStateChecking, ice.ConnectionStateChecking}, + {ICETransportStateConnected, ice.ConnectionStateConnected}, + {ICETransportStateCompleted, ice.ConnectionStateCompleted}, + {ICETransportStateFailed, ice.ConnectionStateFailed}, + {ICETransportStateDisconnected, ice.ConnectionStateDisconnected}, + {ICETransportStateClosed, ice.ConnectionStateClosed}, + } + + for i, testCase := range testCases { + assert.Equal(t, + testCase.native.toICE(), + testCase.ice, + "testCase: %d %v", i, testCase, + ) + assert.Equal(t, + testCase.native, + newICETransportStateFromICE(testCase.ice), + "testCase: %d %v", i, testCase, + ) + } +} diff --git a/internal/ice/candidatepair.go b/internal/ice/candidatepair.go deleted file mode 100644 index bdb248f1f34..00000000000 --- a/internal/ice/candidatepair.go +++ /dev/null @@ -1,24 +0,0 @@ -package ice - -import "fmt" - -type ( - // CandidatePair represents an ICE Candidate pair - CandidatePair struct { - Local *Candidate - Remote *Candidate - } -) - -func (p *CandidatePair) String() string { - return fmt.Sprintf("(local) %s <-> (remote) %s", p.Local, p.Remote) -} - -// NewCandidatePair returns an initialized *CandidatePair -// for the given pair of Candidate instances -func NewCandidatePair(local, remote *Candidate) *CandidatePair { - return &CandidatePair{ - Local: local, - Remote: remote, - } -} diff --git a/internal/ice/candidatetype.go b/internal/ice/candidatetype.go deleted file mode 100644 index 0d037fd0985..00000000000 --- a/internal/ice/candidatetype.go +++ /dev/null @@ -1,73 +0,0 @@ -package ice - -import "fmt" - -// CandidateType represents the type of the ICE candidate used. -type CandidateType int - -const ( - // CandidateTypeHost indicates that the candidate is of Host type as - // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.1. A - // candidate obtained by binding to a specific port from an IP address on - // the host. This includes IP addresses on physical interfaces and logical - // ones, such as ones obtained through VPNs. - CandidateTypeHost CandidateType = iota + 1 - - // CandidateTypeSrflx indicates the the candidate is of Server - // Reflexive type as described - // https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A candidate type - // whose IP address and port are a binding allocated by a NAT for an ICE - // agent after it sends a packet through the NAT to a server, such as a - // STUN server. - CandidateTypeSrflx - - // CandidateTypePrflx indicates that the candidate is of Peer - // Reflexive type. A candidate type whose IP address and port are a binding - // allocated by a NAT for an ICE agent after it sends a packet through the - // NAT to its peer. - CandidateTypePrflx - - // CandidateTypeRelay indicates the the candidate is of Relay type as - // described in https://tools.ietf.org/html/rfc8445#section-5.1.1.2. A - // candidate type obtained from a relay server, such as a TURN server. - CandidateTypeRelay -) - -// This is done this way because of a linter. -const ( - candidateTypeHostStr = "host" - candidateTypeSrflxStr = "srflx" - candidateTypePrflxStr = "prflx" - candidateTypeRelayStr = "relay" -) - -// NewCandidateType takes a string and converts it into CandidateType -func NewCandidateType(raw string) (CandidateType, error) { - switch raw { - case candidateTypeHostStr: - return CandidateTypeHost, nil - case candidateTypeSrflxStr: - return CandidateTypeSrflx, nil - case candidateTypePrflxStr: - return CandidateTypePrflx, nil - case candidateTypeRelayStr: - return CandidateTypeRelay, nil - default: - return CandidateType(Unknown), fmt.Errorf("unknown ICE candidate type: %s", raw) - } -} - -func (t CandidateType) String() string { - switch t { - case CandidateTypeHost: - return candidateTypeHostStr - case CandidateTypeSrflx: - return candidateTypeSrflxStr - case CandidateTypePrflx: - return candidateTypePrflxStr - case CandidateTypeRelay: - return candidateTypeRelayStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/candidatetype_test.go b/internal/ice/candidatetype_test.go deleted file mode 100644 index 9327dc3fa58..00000000000 --- a/internal/ice/candidatetype_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package ice - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestCandidateType(t *testing.T) { - testCases := []struct { - typeString string - shouldFail bool - expectedType CandidateType - }{ - {unknownStr, true, CandidateType(Unknown)}, - {"host", false, CandidateTypeHost}, - {"srflx", false, CandidateTypeSrflx}, - {"prflx", false, CandidateTypePrflx}, - {"relay", false, CandidateTypeRelay}, - } - - for i, testCase := range testCases { - actual, err := NewCandidateType(testCase.typeString) - if (err != nil) != testCase.shouldFail { - t.Error(err) - } - assert.Equal(t, - testCase.expectedType, - actual, - "testCase: %d %v", i, testCase, - ) - } -} - -func TestCandidateType_String(t *testing.T) { - testCases := []struct { - cType CandidateType - expectedString string - }{ - {CandidateType(Unknown), unknownStr}, - {CandidateTypeHost, "host"}, - {CandidateTypeSrflx, "srflx"}, - {CandidateTypePrflx, "prflx"}, - {CandidateTypeRelay, "relay"}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.expectedString, - testCase.cType.String(), - "testCase: %d %v", i, testCase, - ) - } -} diff --git a/internal/ice/component.go b/internal/ice/component.go deleted file mode 100644 index d1c3fbe1e04..00000000000 --- a/internal/ice/component.go +++ /dev/null @@ -1,47 +0,0 @@ -package ice - -// Component describes if the ice transport is used for RTP -// (or RTCP multiplexing). -type Component int - -const ( - // ComponentRTP indicates that the ICE Transport is used for RTP (or - // RTCP multiplexing), as defined in - // https://tools.ietf.org/html/rfc5245#section-4.1.1.1. Protocols - // multiplexed with RTP (e.g. data channel) share its component ID. This - // represents the component-id value 1 when encoded in candidate-attribute. - ComponentRTP Component = iota + 1 - - // ComponentRTCP indicates that the ICE Transport is used for RTCP as - // defined by https://tools.ietf.org/html/rfc5245#section-4.1.1.1. This - // represents the component-id value 2 when encoded in candidate-attribute. - ComponentRTCP -) - -// This is done this way because of a linter. -const ( - componentRTPStr = "rtp" - componentRTCPStr = "rtcp" -) - -func newComponent(raw string) Component { - switch raw { - case componentRTPStr: - return ComponentRTP - case componentRTCPStr: - return ComponentRTCP - default: - return Component(Unknown) - } -} - -func (t Component) String() string { - switch t { - case ComponentRTP: - return componentRTPStr - case ComponentRTCP: - return componentRTCPStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/connectionstate.go b/internal/ice/connectionstate.go deleted file mode 100644 index d2f96740d5f..00000000000 --- a/internal/ice/connectionstate.go +++ /dev/null @@ -1,94 +0,0 @@ -package ice - -// ConnectionState indicates signaling state of the ICE Connection. -type ConnectionState int - -const ( - // ConnectionStateNew indicates that any of the ICETransports are - // in the "new" state and none of them are in the "checking", "disconnected" - // or "failed" state, or all ICETransports are in the "closed" state, or - // there are no transports. - ConnectionStateNew ConnectionState = iota + 1 - - // ConnectionStateChecking indicates that any of the ICETransports - // are in the "checking" state and none of them are in the "disconnected" - // or "failed" state. - ConnectionStateChecking - - // ConnectionStateConnected indicates that all ICETransports are - // in the "connected", "completed" or "closed" state and at least one of - // them is in the "connected" state. - ConnectionStateConnected - - // ConnectionStateCompleted indicates that all ICETransports are - // in the "completed" or "closed" state and at least one of them is in the - // "completed" state. - ConnectionStateCompleted - - // ConnectionStateDisconnected indicates that any of the - // ICETransports are in the "disconnected" state and none of them are - // in the "failed" state. - ConnectionStateDisconnected - - // ConnectionStateFailed indicates that any of the ICETransports - // are in the "failed" state. - ConnectionStateFailed - - // ConnectionStateClosed indicates that the PeerConnection's - // isClosed is true. - ConnectionStateClosed -) - -// This is done this way because of a linter. -const ( - connectionStateNewStr = "new" - connectionStateCheckingStr = "checking" - connectionStateConnectedStr = "connected" - connectionStateCompletedStr = "completed" - connectionStateDisconnectedStr = "disconnected" - connectionStateFailedStr = "failed" - connectionStateClosedStr = "closed" -) - -// NewConnectionState takes a string and converts it to ConnectionState -func NewConnectionState(raw string) ConnectionState { - switch raw { - case connectionStateNewStr: - return ConnectionStateNew - case connectionStateCheckingStr: - return ConnectionStateChecking - case connectionStateConnectedStr: - return ConnectionStateConnected - case connectionStateCompletedStr: - return ConnectionStateCompleted - case connectionStateDisconnectedStr: - return ConnectionStateDisconnected - case connectionStateFailedStr: - return ConnectionStateFailed - case connectionStateClosedStr: - return ConnectionStateClosed - default: - return ConnectionState(Unknown) - } -} - -func (c ConnectionState) String() string { - switch c { - case ConnectionStateNew: - return connectionStateNewStr - case ConnectionStateChecking: - return connectionStateCheckingStr - case ConnectionStateConnected: - return connectionStateConnectedStr - case ConnectionStateCompleted: - return connectionStateCompletedStr - case ConnectionStateDisconnected: - return connectionStateDisconnectedStr - case ConnectionStateFailed: - return connectionStateFailedStr - case ConnectionStateClosed: - return connectionStateClosedStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/connectionstate_test.go b/internal/ice/connectionstate_test.go deleted file mode 100644 index 8456ad628ee..00000000000 --- a/internal/ice/connectionstate_test.go +++ /dev/null @@ -1,55 +0,0 @@ -package ice - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewConnectionState(t *testing.T) { - testCases := []struct { - stateString string - expectedState ConnectionState - }{ - {unknownStr, ConnectionState(Unknown)}, - {"new", ConnectionStateNew}, - {"checking", ConnectionStateChecking}, - {"connected", ConnectionStateConnected}, - {"completed", ConnectionStateCompleted}, - {"disconnected", ConnectionStateDisconnected}, - {"failed", ConnectionStateFailed}, - {"closed", ConnectionStateClosed}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.expectedState, - NewConnectionState(testCase.stateString), - "testCase: %d %v", i, testCase, - ) - } -} - -func TestConnectionState_String(t *testing.T) { - testCases := []struct { - state ConnectionState - expectedString string - }{ - {ConnectionState(Unknown), unknownStr}, - {ConnectionStateNew, "new"}, - {ConnectionStateChecking, "checking"}, - {ConnectionStateConnected, "connected"}, - {ConnectionStateCompleted, "completed"}, - {ConnectionStateDisconnected, "disconnected"}, - {ConnectionStateFailed, "failed"}, - {ConnectionStateClosed, "closed"}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.expectedString, - testCase.state.String(), - "testCase: %d %v", i, testCase, - ) - } -} diff --git a/internal/ice/constants.go b/internal/ice/constants.go deleted file mode 100644 index d06a92f280e..00000000000 --- a/internal/ice/constants.go +++ /dev/null @@ -1,10 +0,0 @@ -package ice - -const ( - // Unknown defines default public constant to use for "enum" like struct - // comparisons when no value was defined. - Unknown = iota - unknownStr = "unknown" - - receiveMTU = 8192 -) diff --git a/internal/ice/credentialtype.go b/internal/ice/credentialtype.go deleted file mode 100644 index b89d1b9ae5d..00000000000 --- a/internal/ice/credentialtype.go +++ /dev/null @@ -1,43 +0,0 @@ -package ice - -// CredentialType indicates the type of credentials used to connect to -// an ICE server. -type CredentialType int - -const ( - // CredentialTypePassword describes username and pasword based - // credentials as described in https://tools.ietf.org/html/rfc5389. - CredentialTypePassword CredentialType = iota - - // CredentialTypeOauth describes token based credential as described - // in https://tools.ietf.org/html/rfc7635. - CredentialTypeOauth -) - -// This is done this way because of a linter. -const ( - credentialTypePasswordStr = "password" - credentialTypeOauthStr = "oauth" -) - -func newCredentialType(raw string) CredentialType { - switch raw { - case credentialTypePasswordStr: - return CredentialTypePassword - case credentialTypeOauthStr: - return CredentialTypeOauth - default: - return CredentialType(Unknown) - } -} - -func (t CredentialType) String() string { - switch t { - case CredentialTypePassword: - return credentialTypePasswordStr - case CredentialTypeOauth: - return credentialTypeOauthStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/errors.go b/internal/ice/errors.go deleted file mode 100644 index 71453a7eb21..00000000000 --- a/internal/ice/errors.go +++ /dev/null @@ -1,16 +0,0 @@ -package ice - -import "errors" - -var ( - // ErrUnknownType indicates an error with Unknown info. - ErrUnknownType = errors.New("unknown") - - // ErrNoTurnCredencials indicates that a TURN server URL was provided - // without required credentials. - ErrNoTurnCredencials = errors.New("turn server credentials required") - - // ErrTurnCredencials indicates that provided TURN credentials are partial - // or malformed. - ErrTurnCredencials = errors.New("invalid turn server credentials") -) diff --git a/internal/ice/gathererstate.go b/internal/ice/gathererstate.go deleted file mode 100644 index 0fc7bf26d45..00000000000 --- a/internal/ice/gathererstate.go +++ /dev/null @@ -1,36 +0,0 @@ -package ice - -// GathererState represents the current state of the ICE gatherer. -type GathererState byte - -const ( - // GathererStateNew indicates object has been created but - // gather() has not been called. - GathererStateNew GathererState = iota + 1 - - // GathererStateGathering indicates gather() has been called, - // and the Gatherer is in the process of gathering candidates. - GathererStateGathering - - // GathererStateComplete indicates the Gatherer has completed gathering. - GathererStateComplete - - // GathererStateClosed indicates the closed state can only be entered - // when the Gatherer has been closed intentionally by calling close(). - GathererStateClosed -) - -func (s GathererState) String() string { - switch s { - case GathererStateNew: - return "new" - case GathererStateGathering: - return "gathering" - case GathererStateComplete: - return "complete" - case GathererStateClosed: - return "closed" - default: - return unknownStr - } -} diff --git a/internal/ice/gathererstate_test.go b/internal/ice/gathererstate_test.go deleted file mode 100644 index 7d7dcfeae4f..00000000000 --- a/internal/ice/gathererstate_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package ice - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestGathererState_String(t *testing.T) { - testCases := []struct { - state GathererState - expectedString string - }{ - {GathererState(Unknown), unknownStr}, - {GathererStateNew, "new"}, - {GathererStateGathering, "gathering"}, - {GathererStateComplete, "complete"}, - {GathererStateClosed, "closed"}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.expectedString, - testCase.state.String(), - "testCase: %d %v", i, testCase, - ) - } -} diff --git a/internal/ice/gatheringstate.go b/internal/ice/gatheringstate.go deleted file mode 100644 index 01d2f310e5e..00000000000 --- a/internal/ice/gatheringstate.go +++ /dev/null @@ -1,53 +0,0 @@ -package ice - -// GatheringState describes the state of the candidate gathering process. -type GatheringState int - -const ( - // GatheringStateNew indicates that any of the ICETransports are - // in the "new" gathering state and none of the transports are in the - // "gathering" state, or there are no transports. - GatheringStateNew GatheringState = iota + 1 - - // GatheringStateGathering indicates that any of the ICETransports - // are in the "gathering" state. - GatheringStateGathering - - // GatheringStateComplete indicates that at least one Transport - // exists, and all ICETransports are in the "completed" gathering state. - GatheringStateComplete -) - -// This is done this way because of a linter. -const ( - gatheringStateNewStr = "new" - gatheringStateGatheringStr = "gathering" - gatheringStateCompleteStr = "complete" -) - -// NewGatheringState takes a string and converts it to GatheringState -func NewGatheringState(raw string) GatheringState { - switch raw { - case gatheringStateNewStr: - return GatheringStateNew - case gatheringStateGatheringStr: - return GatheringStateGathering - case gatheringStateCompleteStr: - return GatheringStateComplete - default: - return GatheringState(Unknown) - } -} - -func (t GatheringState) String() string { - switch t { - case GatheringStateNew: - return gatheringStateNewStr - case GatheringStateGathering: - return gatheringStateGatheringStr - case GatheringStateComplete: - return gatheringStateCompleteStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/gatheringstate_test.go b/internal/ice/gatheringstate_test.go deleted file mode 100644 index ca301a8ed95..00000000000 --- a/internal/ice/gatheringstate_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package ice - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNewGatheringState(t *testing.T) { - testCases := []struct { - stateString string - expectedState GatheringState - }{ - {unknownStr, GatheringState(Unknown)}, - {"new", GatheringStateNew}, - {"gathering", GatheringStateGathering}, - {"complete", GatheringStateComplete}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.expectedState, - NewGatheringState(testCase.stateString), - "testCase: %d %v", i, testCase, - ) - } -} - -func TestGatheringState_String(t *testing.T) { - testCases := []struct { - state GatheringState - expectedString string - }{ - {GatheringState(Unknown), unknownStr}, - {GatheringStateNew, "new"}, - {GatheringStateGathering, "gathering"}, - {GatheringStateComplete, "complete"}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.expectedString, - testCase.state.String(), - "testCase: %d %v", i, testCase, - ) - } -} diff --git a/internal/ice/gatheroptions.go b/internal/ice/gatheroptions.go deleted file mode 100644 index a390c055159..00000000000 --- a/internal/ice/gatheroptions.go +++ /dev/null @@ -1,7 +0,0 @@ -package ice - -// GatherOptions provides options relating to the gathering of ICE candidates. -type GatherOptions struct { - ICEServers []Server - ICEGatherPolicy TransportPolicy -} diff --git a/internal/ice/protocol.go b/internal/ice/protocol.go deleted file mode 100644 index 9167624522a..00000000000 --- a/internal/ice/protocol.go +++ /dev/null @@ -1,47 +0,0 @@ -package ice - -import ( - "fmt" - "strings" -) - -// Protocol indicates the transport protocol type that is used in the -// ice.URL structure. -type Protocol int - -const ( - // ProtocolUDP indicates the URL uses a UDP transport. - ProtocolUDP Protocol = iota + 1 - - // ProtocolTCP indicates the URL uses a TCP transport. - ProtocolTCP -) - -// This is done this way because of a linter. -const ( - protocolUDPStr = "udp" - protocolTCPStr = "tcp" -) - -// NewProtocol takes a string and converts it to Protocol -func NewProtocol(raw string) (Protocol, error) { - switch { - case strings.EqualFold(protocolUDPStr, raw): - return ProtocolUDP, nil - case strings.EqualFold(protocolTCPStr, raw): - return ProtocolTCP, nil - default: - return Protocol(Unknown), fmt.Errorf("unknown protocol: %s", raw) - } -} - -func (t Protocol) String() string { - switch t { - case ProtocolUDP: - return protocolUDPStr - case ProtocolTCP: - return protocolTCPStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/role.go b/internal/ice/role.go deleted file mode 100644 index d88b3a5cd0c..00000000000 --- a/internal/ice/role.go +++ /dev/null @@ -1,45 +0,0 @@ -package ice - -// Role describes the role ice.Agent is playing in selecting the -// preferred the candidate pair. -type Role int - -const ( - // RoleControlling indicates that the ICE agent that is responsible - // for selecting the final choice of candidate pairs and signaling them - // through STUN and an updated offer, if needed. In any session, one agent - // is always controlling. The other is the controlled agent. - RoleControlling Role = iota + 1 - - // RoleControlled indicates that an ICE agent that waits for the - // controlling agent to select the final choice of candidate pairs. - RoleControlled -) - -// This is done this way because of a linter. -const ( - roleControllingStr = "controlling" - roleControlledStr = "controlled" -) - -func newRole(raw string) Role { - switch raw { - case roleControllingStr: - return RoleControlling - case roleControlledStr: - return RoleControlled - default: - return Role(Unknown) - } -} - -func (t Role) String() string { - switch t { - case RoleControlling: - return roleControllingStr - case RoleControlled: - return roleControlledStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/transportpolicy.go b/internal/ice/transportpolicy.go deleted file mode 100644 index 37e93862873..00000000000 --- a/internal/ice/transportpolicy.go +++ /dev/null @@ -1,46 +0,0 @@ -package ice - -// TransportPolicy defines the ICE candidate policy surface the -// permitted candidates. Only these candidates are used for connectivity checks. -type TransportPolicy int - -// GatherPolicy is the ORTC equivalent of TransportPolicy -type GatherPolicy = TransportPolicy - -const ( - // TransportPolicyAll indicates any type of candidate is used. - TransportPolicyAll TransportPolicy = iota - - // TransportPolicyRelay indicates only media relay candidates such - // as candidates passing through a TURN server are used. - TransportPolicyRelay -) - -// This is done this way because of a linter. -const ( - transportPolicyRelayStr = "relay" - transportPolicyAllStr = "all" -) - -// NewTransportPolicy takes a string and converts it to TransportPolicy -func NewTransportPolicy(raw string) TransportPolicy { - switch raw { - case transportPolicyRelayStr: - return TransportPolicyRelay - case transportPolicyAllStr: - return TransportPolicyAll - default: - return TransportPolicy(Unknown) - } -} - -func (t TransportPolicy) String() string { - switch t { - case TransportPolicyRelay: - return transportPolicyRelayStr - case TransportPolicyAll: - return transportPolicyAllStr - default: - return ErrUnknownType.Error() - } -} diff --git a/internal/ice/transportstate_test.go b/internal/ice/transportstate_test.go deleted file mode 100644 index 73becc3703a..00000000000 --- a/internal/ice/transportstate_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package ice - -import ( - "testing" - - "github.com/pion/ice" - "github.com/stretchr/testify/assert" -) - -func TestTransportState_String(t *testing.T) { - testCases := []struct { - state TransportState - expectedString string - }{ - {TransportState(Unknown), unknownStr}, - {TransportStateNew, "new"}, - {TransportStateChecking, "checking"}, - {TransportStateConnected, "connected"}, - {TransportStateCompleted, "completed"}, - {TransportStateFailed, "failed"}, - {TransportStateDisconnected, "disconnected"}, - {TransportStateClosed, "closed"}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.expectedString, - testCase.state.String(), - "testCase: %d %v", i, testCase, - ) - } -} - -func TestTransportState_Convert(t *testing.T) { - testCases := []struct { - native TransportState - ice ice.ConnectionState - }{ - {TransportState(Unknown), ice.ConnectionState(Unknown)}, - {TransportStateNew, ice.ConnectionStateNew}, - {TransportStateChecking, ice.ConnectionStateChecking}, - {TransportStateConnected, ice.ConnectionStateConnected}, - {TransportStateCompleted, ice.ConnectionStateCompleted}, - {TransportStateFailed, ice.ConnectionStateFailed}, - {TransportStateDisconnected, ice.ConnectionStateDisconnected}, - {TransportStateClosed, ice.ConnectionStateClosed}, - } - - for i, testCase := range testCases { - assert.Equal(t, - testCase.native.toICE(), - testCase.ice, - "testCase: %d %v", i, testCase, - ) - assert.Equal(t, - testCase.native, - newTransportStateFromICE(testCase.ice), - "testCase: %d %v", i, testCase, - ) - } -} diff --git a/internal/tools/gen/genaliasdocs.go b/internal/tools/gen/genaliasdocs.go deleted file mode 100644 index dd31651c292..00000000000 --- a/internal/tools/gen/genaliasdocs.go +++ /dev/null @@ -1,261 +0,0 @@ -package main - -import ( - "bufio" - "flag" - "fmt" - "go/ast" - "go/build" - "go/format" - "go/parser" - "go/token" - "os" - "path" - "path/filepath" - "sort" - "strings" - "unicode" -) - -const cmdUsage = ` -Usage : gnaliasdocs [options] -Options: - -pkg (mandatory) the location of the aliased package - -GOOS (optional) GOOS used to filter which files are considered when collection comments - -GOARCH (optional) GOARCH used to filter which files are considered when collection comments - -build-tags (optional) additional build tags used to filter which files are considered when collection comments` - -func main() { - var ( - pkg string - goos string - goarch string - buildTagsStr string - ) - - flag.StringVar(&pkg, "pkg", "", "the location of the aliased package") - flag.StringVar(&goos, "GOOS", "", "GOOS used to filter which files are considered when collection comments") - flag.StringVar(&goarch, "GOARCH", "", "GOARCH used to filter which files are considered when collection comments") - flag.StringVar(&buildTagsStr, "build-tags", "", "space separated build tags") - flag.Parse() - - if pkg == "" { - fmt.Println(cmdUsage) - return - } - var buildTags []string - if buildTagsStr != "" { - buildTags = strings.Split(buildTagsStr, " ") - } - - args := flag.Args() - if len(args) != 1 { - fmt.Println(cmdUsage) - return - } - - f := args[0] - if !path.IsAbs(pkg) { - pkg = path.Join(path.Dir(f), pkg) - } - aliasedComments, err := getAliasedComments(pkg, goos, goarch, buildTags) - if err != nil { - fmt.Printf("Error collecting aliased comments: %v", err) - } - - for _, arg := range args { - files, err := filepath.Glob(arg) - if err != nil { - fmt.Println(err) - os.Exit(1) - return - } - for _, f := range files { - generate(f, aliasedComments) - } - } -} - -func generate(fileName string, aliasedComments map[string]string) { - fset := token.NewFileSet() - node, err := parser.ParseFile(fset, fileName, nil, parser.ParseComments) - if err != nil { - fmt.Printf("Error parsing file: %v", err) - return - } - - // need this because Inspect will skip comments in the root node (eg: the generate comment) - origignalComments := make(map[token.Pos]*ast.CommentGroup, len(node.Comments)) - for _, c := range node.Comments { - origignalComments[c.Pos()] = c - } - - comments := make([]*ast.CommentGroup, 0) - - ast.Inspect(node, func(n ast.Node) bool { - switch t := n.(type) { - case *ast.CommentGroup: - delete(origignalComments, t.Pos()) - comments = append(comments, t) - - case *ast.ValueSpec: - if len(t.Values) != len(t.Names) { - return true - } - - cg := getCommentGroup(t.Names, t.Values, t.Pos(), aliasedComments) - if cg != nil { - if t.Doc != nil { - for _, l := range t.Doc.List { - delete(origignalComments, l.Slash) - } - } - t.Doc = cg - } - - case *ast.TypeSpec: - cg := getCommentGroup([]*ast.Ident{t.Name}, []ast.Expr{t.Type}, t.Pos(), aliasedComments) - if cg != nil { - if t.Doc != nil { - for _, l := range t.Doc.List { - delete(origignalComments, l.Slash) - } - } - t.Doc = cg - } - default: - } - - return true - }) - for _, c := range origignalComments { - comments = append(comments, c) - sort.Slice(comments, func(i, j int) bool { - return comments[i].Pos() < comments[j].Pos() - }) - } - - node.Comments = comments - - // overwrite the file with modified version of ast. - write, err := os.Create(fileName) - if err != nil { - fmt.Printf("Error opening file %v", err) - return - } - defer func() { - err = write.Close() - if err != nil { - fmt.Println(err) - } - }() - w := bufio.NewWriter(write) - err = format.Node(w, fset, node) - if err != nil { - fmt.Printf("Error formating file %s", err) - return - } - err = w.Flush() - if err != nil { - fmt.Printf("Error writing file %s", err) - return - } -} - -func getCommentGroup(names []*ast.Ident, values []ast.Expr, pos token.Pos, aliasedComments map[string]string) *ast.CommentGroup { - if len(names) != len(values) { - return nil - } - - cg := &ast.CommentGroup{ - List: make([]*ast.Comment, 0), - } - for i, name := range names { - v, ok := values[i].(*ast.SelectorExpr) - if !ok { - continue - } - - aliasedName := v.Sel.Name - c, ok := aliasedComments[aliasedName] - if !ok || c == "" { - c = fmt.Sprintf("%s TODO: missing comment", aliasedName) - } - - c = strings.TrimSpace(c) - for _, l := range strings.Split(c, "\n") { - l = strings.TrimSpace(l) - - cg.List = append(cg.List, &ast.Comment{ - Slash: pos - 1, - Text: "// " + strings.Replace(l, aliasedName, name.Name, -1), - }) - } - } - - if len(cg.List) == 0 { - return nil - } - - return cg -} - -func getAliasedComments(dir string, goos string, goarch string, buildTags []string) (map[string]string, error) { - files, err := filepath.Glob(dir + "/*.go") - if err != nil { - return nil, err - } - - comments := make(map[string]string) - - buildCtx := build.Default - if goos != "" { - buildCtx.GOOS = goos - } - if goarch != "" { - buildCtx.GOARCH = goarch - } - if len(buildTags) > 0 { - buildCtx.BuildTags = buildTags - } - - for _, f := range files { - if strings.HasSuffix(f, "_test.go") { - continue - } - - match, err := buildCtx.MatchFile(path.Dir(f), path.Base(f)) - if err != nil { - return nil, fmt.Errorf("error parsing file %v", err.Error()) - } - - if !match { - continue - } - - fset := token.NewFileSet() - // Parse the file given in arguments - node, err := parser.ParseFile(fset, f, nil, parser.ParseComments) - if err != nil { - return nil, fmt.Errorf("error parsing file %v", err.Error()) - } - - for _, c := range node.Comments { - name := strings.SplitN(c.Text(), " ", 2)[0] - exported := false - for _, first := range name { - if unicode.IsUpper(first) { - exported = true - } - break - } - - if exported { - comments[name] = c.Text() - } - } - - } - - return comments, nil -} diff --git a/internal/ice/networktype.go b/networktype.go similarity index 98% rename from internal/ice/networktype.go rename to networktype.go index c78b02fa93c..c90726b3f83 100644 --- a/internal/ice/networktype.go +++ b/networktype.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "fmt" diff --git a/internal/ice/networktype_test.go b/networktype_test.go similarity index 98% rename from internal/ice/networktype_test.go rename to networktype_test.go index ba91de0f7d9..56879598e24 100644 --- a/internal/ice/networktype_test.go +++ b/networktype_test.go @@ -1,4 +1,4 @@ -package ice +package webrtc import ( "testing" diff --git a/internal/ice/oauthcredential.go b/oauthcredential.go similarity index 86% rename from internal/ice/oauthcredential.go rename to oauthcredential.go index 090d19e93b5..46170c7a24b 100644 --- a/internal/ice/oauthcredential.go +++ b/oauthcredential.go @@ -1,9 +1,9 @@ -package ice +package webrtc // OAuthCredential represents OAuth credential information which is used by // the STUN/TURN client to connect to an ICE server as defined in // https://tools.ietf.org/html/rfc7635. Note that the kid parameter is not -// located in OAuthCredential, but in Server's username member. +// located in OAuthCredential, but in ICEServer's username member. type OAuthCredential struct { // MACKey is a base64-url encoded format. It is used in STUN message // integrity hash calculation. diff --git a/peerconnection.go b/peerconnection.go index 6ef14faaf31..7a2554199c7 100644 --- a/peerconnection.go +++ b/peerconnection.go @@ -124,7 +124,7 @@ func (api *API) NewPeerConnection(configuration Configuration) (*PeerConnection, return nil, err } - if !pc.iceGatherer.AgentIsTrickle() { + if !pc.iceGatherer.agentIsTrickle { if err = pc.iceGatherer.Gather(); err != nil { return nil, err } @@ -197,7 +197,7 @@ func (pc *PeerConnection) initConfiguration(configuration Configuration) error { if len(configuration.ICEServers) > 0 { for _, server := range configuration.ICEServers { - if err := server.Validate(); err != nil { + if err := server.validate(); err != nil { return err } } @@ -374,7 +374,7 @@ func (pc *PeerConnection) SetConfiguration(configuration Configuration) error { if len(configuration.ICEServers) > 0 { // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3) for _, server := range configuration.ICEServers { - if err := server.Validate(); err != nil { + if err := server.validate(); err != nil { return err } } @@ -826,7 +826,7 @@ func (pc *PeerConnection) SetLocalDescription(desc SessionDescription) error { return err } - if !pc.iceGatherer.AgentIsTrickle() { + if !pc.iceGatherer.agentIsTrickle { // To support all unittests which are following the future trickle=true // setup while also support the old trickle=false synchronous gathering // process this is necessary to avoid calling Garther() in multiple diff --git a/peerconnection_js.go b/peerconnection_js.go index 03f0c313ef3..4ee1e385846 100644 --- a/peerconnection_js.go +++ b/peerconnection_js.go @@ -165,7 +165,7 @@ func (pc *PeerConnection) checkConfiguration(configuration Configuration) error if len(configuration.ICEServers) > 0 { // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3) for _, server := range configuration.ICEServers { - if err := server.Validate(); err != nil { + if _, err := server.validate(); err != nil { return err } }