diff --git a/.travis.yml b/.travis.yml index b3ae371e9f7..e6d9ed5ed8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -39,8 +39,10 @@ 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="./test-wasm/go_js_wasm_exec" -v . + - 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 - 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 af602afb233..548d6d93b40 100644 --- a/errors.go +++ b/errors.go @@ -19,14 +19,6 @@ 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 new file mode 100644 index 00000000000..8def4680cf4 --- /dev/null +++ b/ice.go @@ -0,0 +1,289 @@ +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.ICETransportPolicyRelay + + // ICETransportPolicyAll indicates any type of candidate is used. + ICETransportPolicyAll = ice.ICETransportPolicyAll + + // ICETransportStateNew indicates the Transport is waiting + // for remote candidates to be supplied. + ICETransportStateNew = ice.ICETransportStateNew + + // 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.ICETransportStateChecking + + // 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.ICETransportStateConnected + + // ICETransportStateCompleted indicates the Transport tested + // all appropriate candidate pairs and at least one functioning + // candidate pair has been found. + ICETransportStateCompleted = ice.ICETransportStateCompleted + + // 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.ICETransportStateFailed + + // 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.ICETransportStateDisconnected + + // ICETransportStateClosed indicates the Transport has shut down + // and is no longer responding to STUN requests. + ICETransportStateClosed = ice.ICETransportStateClosed + + // 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.ICEConnectionStateNew + + // 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.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 = ice.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 = ice.ICEConnectionStateCompleted + + // ICEConnectionStateDisconnected indicates that any of the + // ICETransports are in the "disconnected" state and none of them are + // in the "failed" state. + ICEConnectionStateDisconnected = ice.ICEConnectionStateDisconnected + + // ICEConnectionStateFailed indicates that any of the ICETransports + // are in the "failed" state. + ICEConnectionStateFailed = ice.ICEConnectionStateFailed + + // ICEConnectionStateClosed indicates that the PeerConnection's + // isClosed is true. + ICEConnectionStateClosed = ice.ICEConnectionStateClosed + + // 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.ICEGatheringStateNew + + // ICEGatheringStateGathering indicates that any of the ICETransports + // are in the "gathering" state. + ICEGatheringStateGathering = ice.ICEGatheringStateGathering + + // ICEGatheringStateComplete indicates that at least one Transport + // exists, and all ICETransports are in the "completed" gathering state. + ICEGatheringStateComplete = ice.ICEGatheringStateComplete + + // ICERoleControlled indicates that an ICE agent that waits for the + // controlling agent to select the final choice of candidate pairs. + ICERoleControlled = ice.ICERoleControlled + + // 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.ICERoleControlling + + // ICECredentialTypePassword describes username and pasword based + // credentials as described in https://tools.ietf.org/html/rfc5389. + ICECredentialTypePassword = ice.ICECredentialTypePassword + + // ICECredentialTypeOauth describes token based credential as described + // in https://tools.ietf.org/html/rfc7635. + ICECredentialTypeOauth = ice.ICECredentialTypeOauth + + // 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.ICECandidateTypeHost + + // 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.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 = ice.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 = ice.ICECandidateTypeRelay + + // 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.ICEProtocolUDP + + // ICEProtocolTCP indicates the URL uses a TCP transport. + ICEProtocolTCP = ice.ICEProtocolTCP + + // 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.ICEComponentRTP + + // 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.ICEComponentRTCP + + // ICEGathererStateNew indicates object has been created but + // gather() has not been called. + ICEGathererStateNew = ice.ICEGathererStateNew + + // ICEGathererStateGathering indicates gather() has been called, + // and the Gatherer is in the process of gathering candidates. + ICEGathererStateGathering = ice.ICEGathererStateGathering + + // ICEGathererStateComplete indicates the Gatherer has completed gathering. + ICEGathererStateComplete = ice.ICEGathererStateComplete + + // ICEGathererStateClosed indicates the closed state can only be entered + // when the Gatherer has been closed intentionally by calling close(). + ICEGathererStateClosed = ice.ICEGathererStateClosed +) + +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.ICEServer + + // ICETransportPolicy defines the ICE candidate policy surface the + // permitted candidates. Only these candidates are used for connectivity checks. + ICETransportPolicy = ice.ICETransportPolicy + + // ICEGatherPolicy is the ORTC equivalent of TransportPolicy + ICEGatherPolicy = ice.ICEGatherPolicy + + // ICEGatheringState describes the state of the candidate gathering process. + ICEGatheringState = ice.ICEGatheringState + + // ICEConnectionState indicates signaling state of the ICE Connection. + ICEConnectionState = ice.ICEConnectionState + + // ICECandidate represents a ice candidate + ICECandidate = ice.ICECandidate + + // ICEGatherOptions provides options relating to the gathering of ICE candidates. + ICEGatherOptions = ice.ICEGatherOptions + + // ICETransportState represents the current state of the ICE transport. + ICETransportState = ice.ICETransportState + + // ICERole indicates the current role of the ICE transport. + ICERole = ice.ICERole + + // ICEParameters includes the ICE username fragment + // and password and other ICE-related parameters. + ICEParameters = ice.ICEParameters + + // ICECandidateInit is used to serialize ice candidates + ICECandidateInit = ice.ICECandidateInit + + // ICECandidateType represents the type of the ICE candidate used. + ICECandidateType = ice.ICECandidateType + + // ICECredentialType indicates the type of credentials used to connect to + // an ICE server. + ICECredentialType = ice.ICECredentialType + + // ICEProtocol indicates the transport protocol type that is used in the + // ice.ICEURL structure. + ICEProtocol = ice.ICEProtocol + + // ICECandidatePair represents an ICE Candidate pair + ICECandidatePair = ice.ICECandidatePair + + // ICEComponent ICEComponent + // State TransportState + // gatheringState GathererState + ICEComponent = ice.ICEComponent + + // ICEGathererState represents the current state of the ICE gatherer. + ICEGathererState = ice.ICEGathererState + + // 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.NewICECandidatePair + + // NewICECandidateType takes a string and converts it into CandidateType + NewICECandidateType = ice.NewICECandidateType + + // NewICEProtocol takes a string and converts it to Protocol + NewICEProtocol = ice.NewICEProtocol + + // NewICEConnectionState takes a string and converts it to ConnectionState + NewICEConnectionState = ice.NewICEConnectionState + + // NewICEGatheringState takes a string and converts it to GatheringState + NewICEGatheringState = ice.NewICEGatheringState + + // NewICETransportPolicy takes a string and converts it to TransportPolicy + NewICETransportPolicy = ice.NewICETransportPolicy + + // 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 3390ef93329..20515eb96f5 100644 --- a/ice_go.go +++ b/ice_go.go @@ -2,7 +2,33 @@ package webrtc -import "github.com/pion/sdp/v2" +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.ICETransport + + // 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.ICEGatherer +) + +var ( + // NewICEGatherer creates a new NewICEGatherer. + NewICEGatherer = ice.NewICEGatherer + + // NewICETransport creates a new NewICETransport. + NewICETransport = ice.NewICETransport +) // NewICEGatherer creates a new NewICEGatherer. // This constructor is part of the ORTC API. It is not diff --git a/icecandidate.go b/internal/ice/candidate.go similarity index 99% rename from icecandidate.go rename to internal/ice/candidate.go index df33d050990..996854236af 100644 --- a/icecandidate.go +++ b/internal/ice/candidate.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "errors" diff --git a/icecandidate_test.go b/internal/ice/candidate_test.go similarity index 99% rename from icecandidate_test.go rename to internal/ice/candidate_test.go index f2a5403ad5e..322c2084d2a 100644 --- a/icecandidate_test.go +++ b/internal/ice/candidate_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "net" diff --git a/icecandidateinit.go b/internal/ice/candidateinit.go similarity index 95% rename from icecandidateinit.go rename to internal/ice/candidateinit.go index 64cfa89dba0..aab91b289f8 100644 --- a/icecandidateinit.go +++ b/internal/ice/candidateinit.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICECandidateInit is used to serialize ice candidates type ICECandidateInit struct { diff --git a/icecandidateinit_test.go b/internal/ice/candidateinit_test.go similarity index 98% rename from icecandidateinit_test.go rename to internal/ice/candidateinit_test.go index c9c98b603a5..ddeb53a3054 100644 --- a/icecandidateinit_test.go +++ b/internal/ice/candidateinit_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "encoding/json" diff --git a/icecandidatepair.go b/internal/ice/candidatepair.go similarity index 97% rename from icecandidatepair.go rename to internal/ice/candidatepair.go index 417b6d774a7..afb8e174405 100644 --- a/icecandidatepair.go +++ b/internal/ice/candidatepair.go @@ -1,4 +1,4 @@ -package webrtc +package ice import "fmt" diff --git a/icecandidatetype.go b/internal/ice/candidatetype.go similarity index 99% rename from icecandidatetype.go rename to internal/ice/candidatetype.go index d1381f0cd1e..7e0c07f80f9 100644 --- a/icecandidatetype.go +++ b/internal/ice/candidatetype.go @@ -1,4 +1,4 @@ -package webrtc +package ice import "fmt" diff --git a/icecandidatetype_test.go b/internal/ice/candidatetype_test.go similarity index 98% rename from icecandidatetype_test.go rename to internal/ice/candidatetype_test.go index fa433429421..e54ce7c23db 100644 --- a/icecandidatetype_test.go +++ b/internal/ice/candidatetype_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/icecomponent.go b/internal/ice/component.go similarity index 98% rename from icecomponent.go rename to internal/ice/component.go index 1f03ec5b062..f133000f492 100644 --- a/icecomponent.go +++ b/internal/ice/component.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICEComponent describes if the ice transport is used for RTP // (or RTCP multiplexing). diff --git a/icecomponent_test.go b/internal/ice/component_test.go similarity index 98% rename from icecomponent_test.go rename to internal/ice/component_test.go index 399cd81fd8e..e692d67a35a 100644 --- a/icecomponent_test.go +++ b/internal/ice/component_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/iceconnectionstate.go b/internal/ice/connectionstate.go similarity index 99% rename from iceconnectionstate.go rename to internal/ice/connectionstate.go index 22fd26975a8..578dba521a5 100644 --- a/iceconnectionstate.go +++ b/internal/ice/connectionstate.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICEConnectionState indicates signaling state of the ICE Connection. type ICEConnectionState int diff --git a/iceconnectionstate_test.go b/internal/ice/connectionstate_test.go similarity index 98% rename from iceconnectionstate_test.go rename to internal/ice/connectionstate_test.go index bae46f2e32a..76a470fa358 100644 --- a/iceconnectionstate_test.go +++ b/internal/ice/connectionstate_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/internal/ice/constants.go b/internal/ice/constants.go new file mode 100644 index 00000000000..d06a92f280e --- /dev/null +++ b/internal/ice/constants.go @@ -0,0 +1,10 @@ +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/icecredentialtype.go b/internal/ice/credentialtype.go similarity index 98% rename from icecredentialtype.go rename to internal/ice/credentialtype.go index d1340ac01bb..3816258205a 100644 --- a/icecredentialtype.go +++ b/internal/ice/credentialtype.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICECredentialType indicates the type of credentials used to connect to // an ICE server. diff --git a/icecredentialtype_test.go b/internal/ice/credentialtype_test.go similarity index 98% rename from icecredentialtype_test.go rename to internal/ice/credentialtype_test.go index fd10c19a2d4..20a49be4c3e 100644 --- a/icecredentialtype_test.go +++ b/internal/ice/credentialtype_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/internal/ice/errors.go b/internal/ice/errors.go new file mode 100644 index 00000000000..71453a7eb21 --- /dev/null +++ b/internal/ice/errors.go @@ -0,0 +1,16 @@ +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/icegatherer_test.go b/internal/ice/gatherer_test.go similarity index 98% rename from icegatherer_test.go rename to internal/ice/gatherer_test.go index c6b38141372..f17141ec7dc 100644 --- a/icegatherer_test.go +++ b/internal/ice/gatherer_test.go @@ -1,6 +1,6 @@ // +build !js -package webrtc +package ice import ( "testing" diff --git a/icegathererstate.go b/internal/ice/gathererstate.go similarity index 98% rename from icegathererstate.go rename to internal/ice/gathererstate.go index 56133d38102..980058ca8e5 100644 --- a/icegathererstate.go +++ b/internal/ice/gathererstate.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICEGathererState represents the current state of the ICE gatherer. type ICEGathererState byte diff --git a/icegathererstate_test.go b/internal/ice/gathererstate_test.go similarity index 97% rename from icegathererstate_test.go rename to internal/ice/gathererstate_test.go index 7dd2eb80454..6a0b278c5bc 100644 --- a/icegathererstate_test.go +++ b/internal/ice/gathererstate_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/icegatheringstate.go b/internal/ice/gatheringstate.go similarity index 99% rename from icegatheringstate.go rename to internal/ice/gatheringstate.go index 21361f912e0..c65d09142da 100644 --- a/icegatheringstate.go +++ b/internal/ice/gatheringstate.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICEGatheringState describes the state of the candidate gathering process. type ICEGatheringState int diff --git a/icegatheringstate_test.go b/internal/ice/gatheringstate_test.go similarity index 98% rename from icegatheringstate_test.go rename to internal/ice/gatheringstate_test.go index bc5a3330151..219046038eb 100644 --- a/icegatheringstate_test.go +++ b/internal/ice/gatheringstate_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/icegatheroptions.go b/internal/ice/gatheroptions.go similarity index 92% rename from icegatheroptions.go rename to internal/ice/gatheroptions.go index 88421c74e5f..548657a41a7 100644 --- a/icegatheroptions.go +++ b/internal/ice/gatheroptions.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICEGatherOptions provides options relating to the gathering of ICE candidates. type ICEGatherOptions struct { diff --git a/icegatherer.go b/internal/ice/icegatherer.go similarity index 98% rename from icegatherer.go rename to internal/ice/icegatherer.go index 767d006a21b..3697eb595b2 100644 --- a/icegatherer.go +++ b/internal/ice/icegatherer.go @@ -1,6 +1,6 @@ // +build !js -package webrtc +package ice import ( "errors" @@ -241,3 +241,7 @@ func (g *ICEGatherer) getAgent() *ice.Agent { defer g.lock.RUnlock() return g.agent } + +func (g *ICEGatherer) AgentIsTrickle() bool { + return g.agentIsTrickle +} diff --git a/networktype.go b/internal/ice/networktype.go similarity index 98% rename from networktype.go rename to internal/ice/networktype.go index c90726b3f83..c78b02fa93c 100644 --- a/networktype.go +++ b/internal/ice/networktype.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "fmt" diff --git a/networktype_test.go b/internal/ice/networktype_test.go similarity index 98% rename from networktype_test.go rename to internal/ice/networktype_test.go index 56879598e24..ba91de0f7d9 100644 --- a/networktype_test.go +++ b/internal/ice/networktype_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/oauthcredential.go b/internal/ice/oauthcredential.go similarity index 97% rename from oauthcredential.go rename to internal/ice/oauthcredential.go index 46170c7a24b..4db1b8f5819 100644 --- a/oauthcredential.go +++ b/internal/ice/oauthcredential.go @@ -1,4 +1,4 @@ -package webrtc +package ice // OAuthCredential represents OAuth credential information which is used by // the STUN/TURN client to connect to an ICE server as defined in diff --git a/iceparameters.go b/internal/ice/parameters.go similarity index 94% rename from iceparameters.go rename to internal/ice/parameters.go index 0c03a88bf2f..993d865e9aa 100644 --- a/iceparameters.go +++ b/internal/ice/parameters.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICEParameters includes the ICE username fragment // and password and other ICE-related parameters. diff --git a/iceprotocol.go b/internal/ice/protocol.go similarity index 98% rename from iceprotocol.go rename to internal/ice/protocol.go index 41c7811a4c4..353b205946c 100644 --- a/iceprotocol.go +++ b/internal/ice/protocol.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "fmt" diff --git a/iceprotocol_test.go b/internal/ice/protocol_test.go similarity index 98% rename from iceprotocol_test.go rename to internal/ice/protocol_test.go index ea7e59382df..b0a1755d233 100644 --- a/iceprotocol_test.go +++ b/internal/ice/protocol_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/icerole.go b/internal/ice/role.go similarity index 98% rename from icerole.go rename to internal/ice/role.go index 11187863b16..21e0b5957cd 100644 --- a/icerole.go +++ b/internal/ice/role.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICERole describes the role ice.Agent is playing in selecting the // preferred the candidate pair. diff --git a/icerole_test.go b/internal/ice/role_test.go similarity index 98% rename from icerole_test.go rename to internal/ice/role_test.go index 4e9f9c484d1..cce96d028a2 100644 --- a/icerole_test.go +++ b/internal/ice/role_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/iceserver.go b/internal/ice/server.go similarity index 99% rename from iceserver.go rename to internal/ice/server.go index 437f7c57ff7..ca00e6e0dc0 100644 --- a/iceserver.go +++ b/internal/ice/server.go @@ -1,6 +1,6 @@ // +build !js -package webrtc +package ice import ( "github.com/pion/ice" diff --git a/iceserver_js.go b/internal/ice/server_js.go similarity index 89% rename from iceserver_js.go rename to internal/ice/server_js.go index 227014acbfa..79f8891a333 100644 --- a/iceserver_js.go +++ b/internal/ice/server_js.go @@ -1,6 +1,6 @@ // +build js,wasm -package webrtc +package ice import ( "errors" @@ -22,7 +22,13 @@ func (s ICEServer) parseURL(i int) (*ice.URL, error) { return ice.ParseURL(s.URLs[i]) } -func (s ICEServer) validate() ([]*ice.URL, error) { +// Validate checks if the ICEServer struct is valid +func (s ICEServer) Validate() error { + _, err := s.urls() + return err +} + +func (s ICEServer) urls() ([]*ice.URL, error) { urls := []*ice.URL{} for i := range s.URLs { diff --git a/iceserver_test.go b/internal/ice/server_test.go similarity index 99% rename from iceserver_test.go rename to internal/ice/server_test.go index cd819c3ca11..8be753512b3 100644 --- a/iceserver_test.go +++ b/internal/ice/server_test.go @@ -1,6 +1,6 @@ // +build !js -package webrtc +package ice import ( "testing" diff --git a/icetransport.go b/internal/ice/transport.go similarity index 99% rename from icetransport.go rename to internal/ice/transport.go index f4118ba6753..1cae8d212e1 100644 --- a/icetransport.go +++ b/internal/ice/transport.go @@ -1,6 +1,6 @@ // +build !js -package webrtc +package ice import ( "context" diff --git a/icetransportpolicy.go b/internal/ice/transportpolicy.go similarity index 98% rename from icetransportpolicy.go rename to internal/ice/transportpolicy.go index 679de978bee..dc6fc068e25 100644 --- a/icetransportpolicy.go +++ b/internal/ice/transportpolicy.go @@ -1,4 +1,4 @@ -package webrtc +package ice // ICETransportPolicy defines the ICE candidate policy surface the // permitted candidates. Only these candidates are used for connectivity checks. diff --git a/icetransportpolicy_test.go b/internal/ice/transportpolicy_test.go similarity index 98% rename from icetransportpolicy_test.go rename to internal/ice/transportpolicy_test.go index 897f1754cd8..fa1046e1df3 100644 --- a/icetransportpolicy_test.go +++ b/internal/ice/transportpolicy_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/icetransportstate.go b/internal/ice/transportstate.go similarity index 99% rename from icetransportstate.go rename to internal/ice/transportstate.go index 5aac3c4cc9d..d20688f0b73 100644 --- a/icetransportstate.go +++ b/internal/ice/transportstate.go @@ -1,4 +1,4 @@ -package webrtc +package ice import "github.com/pion/ice" diff --git a/icetransportstate_test.go b/internal/ice/transportstate_test.go similarity index 99% rename from icetransportstate_test.go rename to internal/ice/transportstate_test.go index 62c5dfa6252..7beab08fa0c 100644 --- a/icetransportstate_test.go +++ b/internal/ice/transportstate_test.go @@ -1,4 +1,4 @@ -package webrtc +package ice import ( "testing" diff --git a/internal/tools/gen/genaliasdocs.go b/internal/tools/gen/genaliasdocs.go new file mode 100644 index 00000000000..dd31651c292 --- /dev/null +++ b/internal/tools/gen/genaliasdocs.go @@ -0,0 +1,261 @@ +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/peerconnection.go b/peerconnection.go index 1375826c495..e01037e8835 100644 --- a/peerconnection.go +++ b/peerconnection.go @@ -410,7 +410,7 @@ func (pc *PeerConnection) CreateOffer(options *OfferOptions) (SessionDescription return SessionDescription{}, err } - if !pc.iceGatherer.agentIsTrickle { + if !pc.iceGatherer.AgentIsTrickle() { if err = pc.iceGatherer.Gather(); err != nil { return SessionDescription{}, err } @@ -589,7 +589,7 @@ func (pc *PeerConnection) addAnswerMediaTransceivers(d *sdp.SessionDescription) return nil, err } - if !pc.iceGatherer.agentIsTrickle { + if !pc.iceGatherer.AgentIsTrickle() { if err = pc.iceGatherer.Gather(); err != nil { return nil, err } @@ -834,7 +834,7 @@ func (pc *PeerConnection) SetLocalDescription(desc SessionDescription) error { return err } - if !pc.iceGatherer.agentIsTrickle { + if !pc.iceGatherer.AgentIsTrickle() { return nil } return pc.iceGatherer.Gather() @@ -861,7 +861,7 @@ func (pc *PeerConnection) SetRemoteDescription(desc SessionDescription) error { return &rtcerr.InvalidStateError{Err: ErrConnectionClosed} } - if !pc.iceGatherer.agentIsTrickle { + if !pc.iceGatherer.AgentIsTrickle() { if err := pc.iceGatherer.Gather(); err != nil { return err } diff --git a/peerconnection_js.go b/peerconnection_js.go index 4ee1e385846..03f0c313ef3 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 } }