PeerConnection

PeerConnection

Manages WebRTC connection and SDP information between peers.

Constructor

# new PeerConnection()

Example
const peerConnection = new PeerConnection()

Extends

  • EventEmitter

Methods

# (static) getCapabilities(kind) → {MillicastCapability}

Gets user's browser media capabilities compared with Millicast Media Server support.

Parameters:
Name Type Description
kind "audio" | "video"

Type of media for which you wish to get sender capabilities.

Returns:

Object with all capabilities supported by user's browser and Millicast Media Server.

Type
MillicastCapability

# (async) addRemoteTrack(media, streams) → {Promise.<RTCRtpTransceiver>}

Add remote receving track.

Parameters:
Name Type Description
media String

Media kind ('audio' | 'video').

streams Array.<MediaStream>

Streams the track will belong to.

Returns:

Promise that will be resolved when the RTCRtpTransceiver is assigned an mid value.

Type
Promise.<RTCRtpTransceiver>

# (async) closeRTCPeer()

Close RTC peer connection.

# (async) createRTCPeer(config)

Instance new RTCPeerConnection.

Parameters:
Name Type Default Description
config RTCConfiguration null

Peer configuration.

# (async) getRTCLocalSDP(options) → {Promise.<String>}

Get the SDP modified depending the options. Optionally set the SDP information to local peer.

Parameters:
Name Type Description
options Object
Properties
Name Type Description
stereo Boolean

True to modify SDP for support stereo. Otherwise False.

dtx Boolean

True to modify SDP for supporting dtx in opus. Otherwise False.*

mediaStream MediaStream | Array.<MediaStreamTrack>

MediaStream to offer in a stream. This object must have 1 audio track and 1 video track, or at least one of them. Alternative you can provide both tracks in an array.

codec VideoCodec

Selected codec for support simulcast.

simulcast Boolean

True to modify SDP for support simulcast. Only available in Google Chrome and with H.264 or VP8 video codecs.

scalabilityMode String

Selected scalability mode. You can get the available capabilities using PeerConnection.getCapabilities method. Only available in Google Chrome.

absCaptureTime Boolean

True to modify SDP for supporting absolute capture time header extension. Otherwise False.

dependencyDescriptor Boolean

True to modify SDP for supporting aom dependency descriptor header extension. Otherwise False.

disableAudio Boolean

True to not support audio.

disableVideo Boolean

True to not support video.

setSDPToPeer Boolean

True to set the SDP to local peer.

Returns:

Promise object which represents the SDP information of the created offer.

Type
Promise.<String>

# getRTCPeer() → {RTCPeerConnection}

Get current RTC peer connection.

Returns:

Object which represents the RTCPeerConnection.

Type
RTCPeerConnection

# getRTCPeerStatus() → (nullable) {RTCPeerConnectionState}

Get peer connection state.

Returns:

Promise object which represents the peer connection state.

Type
RTCPeerConnectionState

# getTracks() → {Array.<MediaStreamTrack>}

Get sender tracks

Returns:

An array with all tracks in sender peer.

Type
Array.<MediaStreamTrack>

# initStats()

Initialize the statistics monitoring of the RTCPeerConnection.

It will be emitted every second.

Examples
peerConnection.initStats()
import Publish from '@millicast/sdk'

//Initialize and connect your Publisher
const millicastPublish = new Publish(streamName, tokenGenerator)
await millicastPublish.connect(options)

//Initialize get stats
millicastPublish.webRTCPeer.initStats()

//Capture new stats from event every second
millicastPublish.webRTCPeer.on('stats', (stats) => {
  console.log('Stats from event: ', stats)
})
import View from '@millicast/sdk'

//Initialize and connect your Viewer
const millicastView = new View(streamName, tokenGenerator)
await millicastView.connect()

//Initialize get stats
millicastView.webRTCPeer.initStats()

//Capture new stats from event every second
millicastView.webRTCPeer.on('stats', (stats) => {
  console.log('Stats from event: ', stats)
})

# replaceTrack(mediaStreamTrack)

Replace current audio or video track that is being broadcasted.

Parameters:
Name Type Description
mediaStreamTrack MediaStreamTrack

New audio or video track to replace the current one.

# (async) setRTCRemoteSDP(sdp) → {Promise.<void>}

Set SDP information to remote peer.

Parameters:
Name Type Description
sdp String

New SDP to be set in the remote peer.

Returns:

Promise object which resolves when SDP information was successfully set.

Type
Promise.<void>

# stopStats()

Stops the monitoring of RTCPeerConnection statistics.

Example
peerConnection.stopStats()

# updateBandwidthRestriction(sdp, bitrate) → {String}

Update remote SDP information to restrict bandwidth.

Parameters:
Name Type Description
sdp String

Remote SDP.

bitrate Number

New bitrate value in kbps or 0 unlimited bitrate.

Returns:

Updated SDP information with new bandwidth restriction.

Type
String

# (async) updateBitrate(bitrate) → {Promise.<void>}

Set SDP information to remote peer with bandwidth restriction.

Parameters:
Name Type Default Description
bitrate Number 0

New bitrate value in kbps or 0 unlimited bitrate.

Returns:

Promise object which resolves when bitrate was successfully updated.

Type
Promise.<void>

Events

# connectionStateChange

Peer connection state change. Could be new, connecting, connected, disconnected, failed or closed.

Type:
  • RTCPeerConnectionState

# stats

Peer connection incoming stats.

# track

New track event.

Type:
  • RTCTrackEvent