diff --git a/api.go b/api.go index 47eca770..838dcf53 100644 --- a/api.go +++ b/api.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc // API bundles the global funcions of the WebRTC and ORTC API. diff --git a/certificate.go b/certificate.go index 6897d4d6..b6f0789a 100644 --- a/certificate.go +++ b/certificate.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/configuration.go b/configuration.go index b121bed0..34dcdcb2 100644 --- a/configuration.go +++ b/configuration.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/configuration_js.go b/configuration_js.go new file mode 100644 index 00000000..3417432e --- /dev/null +++ b/configuration_js.go @@ -0,0 +1,53 @@ +// +build js,wasm + +package webrtc + +import ( + "github.com/pions/webrtc/internal/ice" +) + +// Configuration defines a set of parameters to configure how the +// peer-to-peer communication via PeerConnection is established or +// re-established. +type Configuration struct { + // ICEServers defines a slice describing servers available to be used by + // ICE, such as STUN and TURN servers. + ICEServers []ICEServer + + // ICETransportPolicy indicates which candidates the ICEAgent is allowed + // to use. + ICETransportPolicy ICETransportPolicy + + // BundlePolicy indicates which media-bundling policy to use when gathering + // ICE candidates. + BundlePolicy BundlePolicy + + // RTCPMuxPolicy indicates which rtcp-mux policy to use when gathering ICE + // candidates. + RTCPMuxPolicy RTCPMuxPolicy + + // PeerIdentity sets the target peer identity for the PeerConnection. + // The PeerConnection will not establish a connection to a remote peer + // unless it can be successfully authenticated with the provided name. + PeerIdentity string + + // Certificates are not supported in the JavaScript/Wasm bindings. + // Certificates []Certificate + + // ICECandidatePoolSize describes the size of the prefetched ICE pool. + ICECandidatePoolSize uint8 +} + +func (c Configuration) getICEServers() (*[]*ice.URL, error) { + var iceServers []*ice.URL + for _, server := range c.ICEServers { + for _, rawURL := range server.URLs { + url, err := ice.ParseURL(rawURL) + if err != nil { + return nil, err + } + iceServers = append(iceServers, url) + } + } + return &iceServers, nil +} diff --git a/datachannel_js.go b/datachannel_js.go index e6aae3db..8646ab75 100644 --- a/datachannel_js.go +++ b/datachannel_js.go @@ -1,4 +1,4 @@ -// +build js +// +build js,wasm package webrtc diff --git a/examples/data-channels/jsfiddle/main.go b/examples/data-channels/jsfiddle/main.go index 2e558ed1..dfb601cd 100644 --- a/examples/data-channels/jsfiddle/main.go +++ b/examples/data-channels/jsfiddle/main.go @@ -1,4 +1,4 @@ -// +build js +// +build js,wasm package main diff --git a/icegatherer.go b/icegatherer.go index 1531558c..092bed73 100644 --- a/icegatherer.go +++ b/icegatherer.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/iceserver.go b/iceserver.go index 1fb28e93..4c33034d 100644 --- a/iceserver.go +++ b/iceserver.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/iceserver_js.go b/iceserver_js.go new file mode 100644 index 00000000..31255095 --- /dev/null +++ b/iceserver_js.go @@ -0,0 +1,63 @@ +// +build js,wasm + +package webrtc + +import ( + "errors" + + "github.com/pions/webrtc/internal/ice" +) + +// ICEServer describes a single STUN and TURN server that can be used by +// the ICEAgent to establish a connection with a peer. +type ICEServer struct { + URLs []string + Username string + // Note: Credential and CredentialType are not supported. + // Credential interface{} + // CredentialType ICECredentialType +} + +func (s ICEServer) parseURL(i int) (*ice.URL, error) { + return ice.ParseURL(s.URLs[i]) +} + +func (s ICEServer) validate() ([]*ice.URL, error) { + urls := []*ice.URL{} + + for i := range s.URLs { + url, err := s.parseURL(i) + if err != nil { + return nil, err + } + + if url.Scheme == ice.SchemeTypeTURN || url.Scheme == ice.SchemeTypeTURNS { + // // https://www.w3.org/TR/webrtc/#set-the-configuration (step #11.3.2) + // if s.Username == "" || s.Credential == nil { + // return nil, &rtcerr.InvalidAccessError{Err: ErrNoTurnCredencials} + // } + + // switch s.CredentialType { + // 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 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} + // } + + // default: + // return nil, &rtcerr.InvalidAccessError{Err: ErrTurnCredencials} + // } + return nil, errors.New("TURN is not currently supported in the JavaScript/Wasm bindings") + } + + urls = append(urls, url) + } + + return urls, nil +} diff --git a/iceserver_test.go b/iceserver_test.go index 4bb17597..8aeb7b4e 100644 --- a/iceserver_test.go +++ b/iceserver_test.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/js_utils.go b/js_utils.go index 3f82918b..b234d1d7 100644 --- a/js_utils.go +++ b/js_utils.go @@ -1,4 +1,4 @@ -// +build js +// +build js,wasm package webrtc diff --git a/mediaengine.go b/mediaengine.go index c9876707..63d967b8 100644 --- a/mediaengine.go +++ b/mediaengine.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/mediaengine_test.go b/mediaengine_test.go index c220b631..5725d3ff 100644 --- a/mediaengine_test.go +++ b/mediaengine_test.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/peerconnection_go_test.go b/peerconnection_go_test.go index ad007e9c..3ef75b84 100644 --- a/peerconnection_go_test.go +++ b/peerconnection_go_test.go @@ -234,6 +234,24 @@ func TestPeerConnection_SetConfiguration_Go(t *testing.T) { }, wantErr: &rtcerr.InvalidModificationError{Err: ErrModifyingCertificates}, }, + { + name: "update ICEServers, no TURN credentials", + init: func() (*PeerConnection, error) { + return NewPeerConnection(Configuration{}) + }, + config: Configuration{ + ICEServers: []ICEServer{ + { + URLs: []string{ + "stun:stun.l.google.com:19302", + "turns:google.de?transport=tcp", + }, + Username: "unittest", + }, + }, + }, + wantErr: &rtcerr.InvalidAccessError{Err: ErrNoTurnCredencials}, + }, } { pc, err := test.init() if err != nil { diff --git a/peerconnection_js.go b/peerconnection_js.go index 0c4762d3..e6d651e7 100644 --- a/peerconnection_js.go +++ b/peerconnection_js.go @@ -1,4 +1,4 @@ -// +build js +// +build js,wasm // Package webrtc implements the WebRTC 1.0 as defined in W3C WebRTC specification document. package webrtc @@ -48,7 +48,6 @@ func (pc *PeerConnection) OnSignalingStateChange(f func(SignalingState)) { defer oldHandler.Release() } onSignalingStateChangeHandler := js.FuncOf(func(this js.Value, args []js.Value) interface{} { - // TODO(albrow): Protect args access; recover from panics. state := newSignalingState(args[0].String()) go f(state) return js.Undefined() @@ -287,7 +286,6 @@ func (pc *PeerConnection) OnICECandidate(f func(candidate *string)) { defer oldHandler.Release() } onICECandidateHandler := js.FuncOf(func(this js.Value, args []js.Value) interface{} { - // TODO(albrow): Protect args access; recover from panics. candidate := valueToStringPointer(args[0].Get("candidate")) go f(candidate) return js.Undefined() @@ -358,9 +356,7 @@ func (pc *PeerConnection) SetIdentityProvider(provider string) (err error) { return nil } -// Note(albrow) SendRTCP is not included in MDN WebRTC documentation. -// SendRTCP sends a user provided RTCP packet to the connected peer -// If no peer is connected the packet is discarded +// Note: SendRTCP is not supported. // func (pc *PeerConnection) SendRTCP(pkt rtcp.Packet) error { // return errors.New("Not yet implemented") // } @@ -469,9 +465,7 @@ func configurationToValue(configuration Configuration) js.Value { "peerIdentity": stringToValueOrUndefined(configuration.PeerIdentity), "iceCandidatePoolSize": uint8ToValueOrUndefined(configuration.ICECandidatePoolSize), - // TODO(albrow): Docs for RTCCertificate are underspecified. - // https://developer.mozilla.org/en-US/docs/Web/API/RTCCertificate How - // should we handle this? + // Note: Certificates are not currently supported. // "certificates": configuration.Certificates, }) } @@ -491,9 +485,9 @@ func iceServerToValue(server ICEServer) js.Value { return js.ValueOf(map[string]interface{}{ "urls": stringsToValue(server.URLs), // required "username": stringToValueOrUndefined(server.Username), - // TODO(albrow): credential is not currently supported. + // Note: credential and credentialType are not currently supported. // "credential": interfaceToValueOrUndefined(server.Credential), - "credentialType": stringEnumToValueOrUndefined(server.CredentialType.String()), + // "credentialType": stringEnumToValueOrUndefined(server.CredentialType.String()), }) } @@ -509,9 +503,7 @@ func valueToConfiguration(configValue js.Value) Configuration { PeerIdentity: valueToStringOrZero(configValue.Get("peerIdentity")), ICECandidatePoolSize: valueToUint8OrZero(configValue.Get("iceCandidatePoolSize")), - // TODO(albrow): Docs for RTCCertificate are underspecified. - // https://developer.mozilla.org/en-US/docs/Web/API/RTCCertificate How - // should we handle this? + // Note: Certificates are not supported. // Certificates []Certificate } } @@ -531,9 +523,9 @@ func valueToICEServer(iceServerValue js.Value) ICEServer { return ICEServer{ URLs: valueToStrings(iceServerValue.Get("urls")), // required Username: valueToStringOrZero(iceServerValue.Get("username")), - // TODO(albrow): Handle Credential which might have different types. + // Note: Credential and CredentialType are not currently supported. // Credential: iceServerValue.Get("credential"), - CredentialType: newICECredentialType(valueToStringOrZero(iceServerValue.Get("credentialType"))), + // CredentialType: newICECredentialType(valueToStringOrZero(iceServerValue.Get("credentialType"))), } } diff --git a/peerconnection_js_test.go b/peerconnection_js_test.go index 0a36e4e1..da171a5c 100644 --- a/peerconnection_js_test.go +++ b/peerconnection_js_test.go @@ -1,4 +1,4 @@ -// +build js +// +build js,wasm package webrtc diff --git a/peerconnection_test.go b/peerconnection_test.go index 3f1211b3..37c67e34 100644 --- a/peerconnection_test.go +++ b/peerconnection_test.go @@ -157,24 +157,6 @@ func TestPeerConnection_SetConfiguration(t *testing.T) { }, wantErr: &rtcerr.InvalidModificationError{Err: ErrModifyingICECandidatePoolSize}, }, - { - name: "update ICEServers, no TURN credentials", - init: func() (*PeerConnection, error) { - return NewPeerConnection(Configuration{}) - }, - config: Configuration{ - ICEServers: []ICEServer{ - { - URLs: []string{ - "stun:stun.l.google.com:19302", - "turns:google.de?transport=tcp", - }, - Username: "unittest", - }, - }, - }, - wantErr: &rtcerr.InvalidAccessError{Err: ErrNoTurnCredencials}, - }, } { pc, err := test.init() if err != nil { @@ -197,7 +179,6 @@ func TestPeerConnection_GetConfiguration(t *testing.T) { ICETransportPolicy: ICETransportPolicyAll, BundlePolicy: BundlePolicyBalanced, RTCPMuxPolicy: RTCPMuxPolicyRequire, - Certificates: []Certificate{}, ICECandidatePoolSize: 0, } actual := pc.GetConfiguration() diff --git a/settingengine.go b/settingengine.go index 68efcd79..7afd0506 100644 --- a/settingengine.go +++ b/settingengine.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import ( diff --git a/settingengine_test.go b/settingengine_test.go index 3a42f22f..06200ad0 100644 --- a/settingengine_test.go +++ b/settingengine_test.go @@ -1,3 +1,5 @@ +// +build !js + package webrtc import (