PeerConnection
Extends: EventEmitter
Source: PeerConnection.js:38
Manages WebRTC connection and SDP information between peers.
Example
const peerConnection = new PeerConnection()Constructor
new PeerConnection(): PeerConnectionInstance Methods
createRTCPeer(config: RTCConfiguration, mode?: String)
Instance new RTCPeerConnection.
Parameters
config(RTCConfiguration) — Peer configuration.config.autoInitStats(Boolean, optional, default: true) — True to initialize statistics monitoring of the RTCPeerConnection accessed via Logger.get(), false to opt-out.config.statsIntervalMs(Number, optional, default: 1000) — The default interval at which the SDK will return WebRTC stats to the consuming application.
mode(String, optional, default: "\"Viewer\"") — Type of connection that is trying to be created, either 'Viewer' or 'Publisher'.
getRTCPeer(): RTCPeerConnection
Get current RTC peer connection.
Returns
RTCPeerConnection— Object which represents the RTCPeerConnection.
closeRTCPeer()
Close RTC peer connection.
Fires
PeerConnection#event:connectionStateChange
setRTCRemoteSDP(sdp: String): Promise.<void>
Set SDP information to remote peer.
Parameters
sdp(String) — New SDP to be set in the remote peer.
Returns
Promise.<void>— Promise object which resolves when SDP information was successfully set.
getRTCLocalSDP(options: Object): Promise.<String>
Get the SDP modified depending the options. Optionally set the SDP information to local peer.
Parameters
options(Object)options.stereo(Boolean) — True to modify SDP for support stereo. Otherwise False.options.dtx(Boolean) — True to modify SDP for supporting dtx in opus. Otherwise False.*options.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.options.codec(VideoCodec) — Selected codec for support simulcast.options.simulcast(Boolean) — True to modify SDP for support simulcast. Only available in Chromium based browsers and with H.264 or VP8 video codecs.options.scalabilityMode(String) — Selected scalability mode. You can get the available capabilities using PeerConnection.getCapabilities method. Only available in Google Chrome.options.absCaptureTime(Boolean) — True to modify SDP for supporting absolute capture time header extension. Otherwise False.options.dependencyDescriptor(Boolean) — True to modify SDP for supporting aom dependency descriptor header extension. Otherwise False.options.disableAudio(Boolean) — True to not support audio.options.disableVideo(Boolean) — True to not support video.options.setSDPToPeer(Boolean) — True to set the SDP to local peer.
Returns
Promise.<String>— Promise object which represents the SDP information of the created offer.
setSimulcastScaling(): Promise.<void>
Scale the simulcast layers of the video sender to 1/4, 1/2 and full resolution. Must be called after the local description with simulcast has been set. Since Chromium 154, layers added through SDP munging are no longer scaled by default and would all be encoded at full resolution.
Returns
Promise.<void>— Promise object which resolves when the scaling has been applied.
addRemoteTrack(
media: String,
streams: Array.<MediaStream>,
): Promise.<RTCRtpTransceiver>
Add remote receiving track.
Parameters
media(String) — Media kind ('audio' | 'video').streams(Array.<MediaStream>) — Streams the track will belong to.
Returns
Promise.<RTCRtpTransceiver>— Promise that will be resolved when the RTCRtpTransceiver is assigned an mid value.
updateBandwidthRestriction(sdp: String, bitrate: Number): String
Update remote SDP information to restrict bandwidth.
Parameters
sdp(String) — Remote SDP.bitrate(Number) — New bitrate value in kbps or 0 unlimited bitrate.
Returns
String— Updated SDP information with new bandwidth restriction.
updateBitrate(bitrate: Number): Promise.<void>
Set SDP information to remote peer with bandwidth restriction.
Parameters
bitrate(Number, default: 0) — New bitrate value in kbps or 0 unlimited bitrate.
Returns
Promise.<void>— Promise object which resolves when bitrate was successfully updated.
getRTCPeerStatus(): RTCPeerConnectionState
Get peer connection state.
Returns
RTCPeerConnectionState— Promise object which represents the peer connection state.
replaceTrack(mediaStreamTrack: MediaStreamTrack)
Replace current audio or video track that is being broadcasted.
Parameters
mediaStreamTrack(MediaStreamTrack) — New audio or video track to replace the current one.
getTracks(): Array.<MediaStreamTrack>
Get sender tracks
Returns
Array.<MediaStreamTrack>— An array with all tracks in sender peer.
initStats()
Initialize the statistics monitoring of the RTCPeerConnection.
It will be emitted every second.
Fires
PeerConnection#event:stats
Example
peerConnection.initStats()import Publish from '@millicast/sdk'
//Initialize and connect your Publisher
const millicastPublish = new Publish(undefined, 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(undefined, 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)
})stopStats()
Stops the monitoring of RTCPeerConnection statistics.
Example
peerConnection.stopStats()Static Methods
getCapabilities(kind: 'audio' | 'video'): MillicastCapability
Gets user's browser media capabilities compared with Millicast Media Server support.
Parameters
kind("audio" | "video") — Type of media for which you wish to get sender capabilities.
Returns
MillicastCapability— Object with all capabilities supported by user's browser and Millicast Media Server.
Events
track: RTCTrackEvent
New track event.
connectionStateChange: RTCPeerConnectionState
Peer connection state change. Could be new, connecting, connected, disconnected, failed or closed.