go-webrtc: github.com/keroserene/go-webrtc Index | Files | Directories

package webrtc

import "github.com/keroserene/go-webrtc"

Package webrtc/data contains the go wrapper for the Peer-to-Peer Data API portion of WebRTC spec.

See: https://w3c.github.io/webrtc-pc/#idl-def-RTCDataChannel

Package webrtc is a golang wrapper on native code WebRTC.

For consistency with the browser-based WebRTCs, the interface here is based loosely on: w3c.github.io/webrtc-pc

The main goal of this project is to present a golang WebRTC package in the most idiomatic and simple-to-use way.

However, to provide a better experience for users of this package, there are differences inherent in the interface written here and the original native code WebRTC - from the golang requirement of Capitalized identifiers for public interfaces, to the replacement of certain callbacks with goroutines.

Note that building the necessary libwebrtc static library is excessively complicated, which is why the necessary platform-specific archives will be provided in lib/. This also mitigates the possibility that future commits on native libwebrtc will break go-webrtc, because the interface with the native code, through the intermediate CGO layer, is relatively fragile.

Due to other external goals of the developers, this package will only be focused on DataChannels. However, extending this package to allow video/audio media streams and related functionality, to be a "complete" WebRTC suite, is entirely possible and will likely happen in the long term. (Issue #7) This will however have implications for the archives that need to be built and linked.

Please share any improvements or concerns as issues or pull requests on github.


Package Files

configuration.go datachannel.go ice.go logging.go peerconnection.go sdp.go utils.go


var (
    INFO  *log.Logger
    WARN  *log.Logger
    ERROR *log.Logger
    TRACE *log.Logger
var DCMap = NewCGOMap()
var PCMap = NewCGOMap()
var SdpTypes = []string{"offer", "pranswer", "answer", "rollback"}

TODO: Turn into Enum.

func CgoSdpToGoString Uses

func CgoSdpToGoString(sdp C.CGO_sdp) string

func EnumToStringSafe Uses

func EnumToStringSafe(value int, valueStrings []string) string

Return a string value for an integer enum from a mapping array or the integer string if the integer it outside the expected range.

func MaxPacketLifeTime Uses

func MaxPacketLifeTime(maxPacketLifeTime int) func(*DataChannelInit)

MaxPacketLifeTime configures a DataChannels 'maxRetransmitTime' option.

func MaxRetransmits Uses

func MaxRetransmits(maxRetransmits int) func(*DataChannelInit)

MaxRetransmits configures a DataChannels 'maxRetransmits' option.

func Negotiated Uses

func Negotiated(negotiated bool) func(*DataChannelInit)

Negotiated configures a DataChannels 'negotiated' option.

func Ordered Uses

func Ordered(ordered bool) func(*DataChannelInit)

Ordered configures a DataChannels 'ordered' option.

func SetLoggingVerbosity Uses

func SetLoggingVerbosity(level int)

Logging verbosity level, from 0 (nothing) upwards.

type BundlePolicy Uses

type BundlePolicy int
const (
    BundlePolicyBalanced BundlePolicy = iota

These "Enum" consts must match order in: peerconnectioninterface.h There doesn't seem to be a way to have a named container for enums in go, and the idiomatic way seems to be just prefixes.

func (BundlePolicy) String Uses

func (p BundlePolicy) String() string

type CGOMap Uses

type CGOMap struct {
    // contains filtered or unexported fields

func NewCGOMap Uses

func NewCGOMap() CGOMap

func (*CGOMap) Delete Uses

func (m *CGOMap) Delete(index int)

func (*CGOMap) Get Uses

func (m *CGOMap) Get(index int) interface{}

func (*CGOMap) Set Uses

func (m *CGOMap) Set(p interface{}) int

type Configuration Uses

type Configuration struct {
    IceServers []IceServer
    // [ED] RtcpMuxPolicy        RtcpMuxPolicy
    PeerIdentity string // Target peer identity


func NewConfiguration Uses

func NewConfiguration(options ...ConfigurationOption) *Configuration

Create a new Configuration with default values according to spec. Accepts any number of |IceServer|s. Returns nil if there's an error.

func (*Configuration) AddIceServer Uses

func (config *Configuration) AddIceServer(params ...string) error

type ConfigurationOption Uses

type ConfigurationOption func(c *Configuration) error

Used in Configuration's variadic functional constructor

func OptionBundlePolicy Uses

func OptionBundlePolicy(policy BundlePolicy) ConfigurationOption

func OptionIceServer Uses

func OptionIceServer(params ...string) ConfigurationOption

func OptionIceTransportPolicy Uses

func OptionIceTransportPolicy(policy IceTransportPolicy) ConfigurationOption

type DataChannel Uses

type DataChannel struct {
    BufferedAmountLowThreshold int
    BinaryType                 string

    // Event Handlers
    OnOpen              func()
    OnClose             func()
    OnMessage           func([]byte) // byte slice.
    OnBufferedAmountLow func()
    // contains filtered or unexported fields

OnError - is not implemented because the underlying Send always returns true as specified for SCTP, there is no reasonable exposure of other specific errors from the native code, and OnClose already covers the bases.

func NewDataChannel Uses

func NewDataChannel(o unsafe.Pointer) *DataChannel

Create a Go Channel struct, and prepare internal CGO references / observers. The most reasonable place for this to be created is from PeerConnection, which is not available in the subpackage.

func (*DataChannel) BufferedAmount Uses

func (c *DataChannel) BufferedAmount() int

func (*DataChannel) Close Uses

func (c *DataChannel) Close() error

func (*DataChannel) ID Uses

func (c *DataChannel) ID() int

func (*DataChannel) Label Uses

func (c *DataChannel) Label() string

func (*DataChannel) MaxPacketLifeTime Uses

func (c *DataChannel) MaxPacketLifeTime() uint

func (*DataChannel) MaxRetransmits Uses

func (c *DataChannel) MaxRetransmits() uint

func (*DataChannel) Negotiated Uses

func (c *DataChannel) Negotiated() bool

func (*DataChannel) Ordered Uses

func (c *DataChannel) Ordered() bool

func (*DataChannel) Protocol Uses

func (c *DataChannel) Protocol() string

func (*DataChannel) ReadyState Uses

func (c *DataChannel) ReadyState() DataState

func (*DataChannel) Send Uses

func (c *DataChannel) Send(data []byte)

Send a message over a DataChannel in binary mode.

func (*DataChannel) SendText Uses

func (c *DataChannel) SendText(text string)

SendText sends a message over the DataChannel in text mode.

type DataChannelInit Uses

type DataChannelInit struct {
    Ordered           bool
    MaxPacketLifeTime int
    MaxRetransmits    int
    Protocol          string
    Negotiated        bool
    ID                int

type DataState Uses

type DataState int
const (
    DataStateConnecting DataState = iota

func (DataState) String Uses

func (s DataState) String() string

type IceCandidate Uses

type IceCandidate struct {
    Candidate     string `json:"candidate"`
    SdpMid        string `json:"sdpMid"`
    SdpMLineIndex int    `json:"sdpMLineIndex"`

func DeserializeIceCandidate Uses

func DeserializeIceCandidate(msg string) *IceCandidate

Deserialize a received json string into an IceCandidate, if possible.

func (*IceCandidate) Serialize Uses

func (candidate *IceCandidate) Serialize() string

Serialize an IceCandidate into a JSON string.

type IceCandidateType Uses

type IceCandidateType int
const (
    IceCandidateTypeHost IceCandidateType = iota

func (IceCandidateType) String Uses

func (t IceCandidateType) String() string

type IceConnectionState Uses

type IceConnectionState int
const (
    IceConnectionStateNew IceConnectionState = iota

func (IceConnectionState) String Uses

func (s IceConnectionState) String() string

type IceCredentialType Uses

type IceCredentialType int

type IceGatheringState Uses

type IceGatheringState int
const (
    IceGatheringStateNew IceGatheringState = iota

func (IceGatheringState) String Uses

func (s IceGatheringState) String() string

type IceProtocol Uses

type IceProtocol int
const (
    IceProtocolUPD IceProtocol = iota

func (IceProtocol) String Uses

func (p IceProtocol) String() string

type IceServer Uses

type IceServer struct {
    Urls       []string // The only "required" element.
    Username   string
    Credential string

func NewIceServer Uses

func NewIceServer(params ...string) (*IceServer, error)

Create a new IceServer object. Expects anywhere from one to three strings, in this order: - comma-separated list of urls. - username - credential TODO: For the ED version, may need to support CredentialType.

type IceTcpCandidateType Uses

type IceTcpCandidateType int
const (
    IceTcpCandidateTypeActive IceTcpCandidateType = iota

func (IceTcpCandidateType) String Uses

func (t IceTcpCandidateType) String() string

type IceTransportPolicy Uses

type IceTransportPolicy int
const (
    IceTransportPolicyNone IceTransportPolicy = iota


func (IceTransportPolicy) String Uses

func (p IceTransportPolicy) String() string

type PeerConnection Uses

type PeerConnection struct {

    // Event handlers
    OnNegotiationNeeded        func()
    OnIceCandidate             func(IceCandidate)
    OnIceCandidateError        func()
    OnSignalingStateChange     func(SignalingState)
    OnIceConnectionStateChange func(IceConnectionState)
    OnIceGatheringStateChange  func(IceGatheringState)
    OnConnectionStateChange    func(PeerConnectionState)
    OnDataChannel              func(*DataChannel)
    // contains filtered or unexported fields
WebRTC PeerConnection

This is the main container of WebRTC functionality - from handling the ICE negotiation to setting up Data Channels.

See: https://w3c.github.io/webrtc-pc/#idl-def-RTCPeerConnection

func NewPeerConnection Uses

func NewPeerConnection(config *Configuration) (*PeerConnection, error)
Construct a WebRTC PeerConnection.

For a successful connection, provide at least one ICE server (stun or turn) in the |Configuration| struct.

func (*PeerConnection) AddIceCandidate Uses

func (pc *PeerConnection) AddIceCandidate(ic IceCandidate) error

func (*PeerConnection) Close Uses

func (pc *PeerConnection) Close() error

func (*PeerConnection) ConnectionState Uses

func (pc *PeerConnection) ConnectionState() PeerConnectionState

readonly connectionState

func (*PeerConnection) CreateAnswer Uses

func (pc *PeerConnection) CreateAnswer() (*SessionDescription, error)

CreateAnswer prepares an SDP "answer" message. This should only happen in response to an offer received and set as the remote description. Once generated, this answer should then be set as the local description and sent back over the signaling channel to the remote peer.

This method is blocking, and should occur within a separate goroutine.

func (*PeerConnection) CreateDataChannel Uses

func (pc *PeerConnection) CreateDataChannel(label string, options ...func(*DataChannelInit)) (
    *DataChannel, error)

func (*PeerConnection) CreateOffer Uses

func (pc *PeerConnection) CreateOffer() (*SessionDescription, error)

CreateOffer prepares an SDP "offer" message, which should be set as the local description, then sent to the remote peer over a signalling channel. This should only be called by the peer initiating the connection.

This method is blocking, and should occur within a separate goroutine.

func (*PeerConnection) DeleteDataChannel Uses

func (pc *PeerConnection) DeleteDataChannel(dc *DataChannel)

func (*PeerConnection) Destroy Uses

func (pc *PeerConnection) Destroy() error

func (*PeerConnection) GetConfiguration Uses

func (pc *PeerConnection) GetConfiguration() Configuration

func (*PeerConnection) IceConnectionState Uses

func (pc *PeerConnection) IceConnectionState() IceConnectionState

readonly iceconnectionState

func (*PeerConnection) IceGatheringState Uses

func (pc *PeerConnection) IceGatheringState() IceGatheringState

readonly icegatheringstatee

func (*PeerConnection) LocalDescription Uses

func (pc *PeerConnection) LocalDescription() (sdp *SessionDescription)

readonly localDescription

func (*PeerConnection) RemoteDescription Uses

func (pc *PeerConnection) RemoteDescription() (sdp *SessionDescription)

readonly remoteDescription

func (*PeerConnection) SetConfiguration Uses

func (pc *PeerConnection) SetConfiguration(config Configuration) error

func (*PeerConnection) SetLocalDescription Uses

func (pc *PeerConnection) SetLocalDescription(sdp *SessionDescription) error

Set a |SessionDescription| as the local description. The description should be generated from the local peer's CreateOffer or CreateAnswer, and not be a description received over the signaling channel.

func (*PeerConnection) SetRemoteDescription Uses

func (pc *PeerConnection) SetRemoteDescription(sdp *SessionDescription) error

Set a |SessionDescription| as the remote description. This description should be one generated by the remote peer's CreateOffer or CreateAnswer, received over the signaling channel, and not a description created locally.

If the local peer is the answerer, this must be called before CreateAnswer.

func (*PeerConnection) SignalingState Uses

func (pc *PeerConnection) SignalingState() SignalingState

readonly signalingState

type PeerConnectionState Uses

type PeerConnectionState int
const (
    PeerConnectionStateNew PeerConnectionState = iota

func (PeerConnectionState) String Uses

func (s PeerConnectionState) String() string

type RtcpMuxPolicy Uses

type RtcpMuxPolicy int

type SessionDescription Uses

type SessionDescription struct {
    Type string `json:"type"`
    Sdp  string `json:"sdp"`
WebRTC SessionDescription

See: https://w3c.github.io/webrtc-pc/#idl-def-RTCSessionDescription

func DeserializeSessionDescription Uses

func DeserializeSessionDescription(msg string) *SessionDescription

Deserialize a received json string into a SessionDescription, if possible.

func NewSessionDescription Uses

func NewSessionDescription(sdpType string, serializedSDP C.CGO_sdpString) *SessionDescription

Construct a SessionDescription object from a valid msg.

func (*SessionDescription) GoStringToCgoSdp Uses

func (desc *SessionDescription) GoStringToCgoSdp() C.CGO_sdp

func (*SessionDescription) Serialize Uses

func (desc *SessionDescription) Serialize() string

Serialize a SessionDescription into a JSON string.

type SignalingState Uses

type SignalingState int
const (
    SignalingStateStable SignalingState = iota

func (SignalingState) String Uses

func (s SignalingState) String() string



Package webrtc imports 11 packages (graph) and is imported by 19 packages. Updated 2020-03-12. Refresh now. Tools for package owners.