PeerConnection

Manages WebRTC connection and SDP information between peers.

Constructor

new PeerConnection()

Example
const peerConnection = new PeerConnection()

Extends

  • EventEmitter

Methods

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

Add remote receiving track.

Parameters:
NameTypeDescription
mediaString

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

streamsArray.<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, modeopt)

Instance new RTCPeerConnection.

Parameters:
NameTypeAttributesDefaultDescription
configRTCConfiguration

Peer configuration.

Properties
NameTypeAttributesDefaultDescription
autoInitStatsBoolean<optional>
true

True to initialize statistics monitoring of the RTCPeerConnection accessed via Logger.get(), false to opt-out.

statsIntervalMsNumber<optional>
1000

The default interval at which the SDK will return WebRTC stats to the consuming application.

modeString<optional>
"Viewer"

Type of connection that is trying to be created, either 'Viewer' or 'Publisher'.

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

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

Parameters:
NameTypeDescription
optionsObject
Properties
NameTypeDescription
stereoBoolean

True to modify SDP for support stereo. Otherwise False.

dtxBoolean

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

mediaStreamMediaStream | 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.

codecVideoCodec

Selected codec for support simulcast.

simulcastBoolean

True to modify SDP for support simulcast. Only available in Chromium based browsers and with H.264 or VP8 video codecs.

scalabilityModeString

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

absCaptureTimeBoolean

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

dependencyDescriptorBoolean

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

disableAudioBoolean

True to not support audio.

disableVideoBoolean

True to not support video.

setSDPToPeerBoolean

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.

Fires:
  • PeerConnection#event:stats
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:
NameTypeDescription
mediaStreamTrackMediaStreamTrack

New audio or video track to replace the current one.

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

Set SDP information to remote peer.

Parameters:
NameTypeDescription
sdpString

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:
NameTypeDescription
sdpString

Remote SDP.

bitrateNumber

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:
NameTypeDefaultDescription
bitrateNumber0

New bitrate value in kbps or 0 unlimited bitrate.

Returns:

Promise object which resolves when bitrate was successfully updated.

Type: 
Promise.<void>

(static) getCapabilities(kind) → {MillicastCapability}

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

Parameters:
NameTypeDescription
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

Events

connectionStateChange

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

Type:
  • RTCPeerConnectionState

track

New track event.

Type:
  • RTCTrackEvent