Publish

Manages connection with a secure WebSocket path to signal the Millicast server and establishes a WebRTC connection to broadcast a MediaStream.

Before you can broadcast, you will need:

  • MediaStream which has at most one audio track and at most one video track. This will be used for stream the contained tracks.

  • A connection path that you can get from Director module or from your own implementation.

Constructor

new Publish(streamName, tokenGenerator, autoReconnectopt)

Parameters:
NameTypeAttributesDefaultDescription
streamNameString

Millicast existing stream name.

tokenGeneratortokenGeneratorCallback

Callback function executed when a new token is needed.

autoReconnectBoolean<optional>
true

Enable auto reconnect to stream.

Extends

Methods

(async) connect(options) → {Promise.<void>}

Starts broadcast to an existing stream name.

In the example, getYourMediaStream and getYourPublisherConnection is your own implementation.

Parameters:
NameTypeDescription
optionsObject

General broadcast options.

Properties
NameTypeAttributesDefaultDescription
sourceIdString

Source unique id. Only avialable if stream is multisource.

stereoBoolean<optional>
false

True to modify SDP for support stereo. Otherwise False.

dtxBoolean<optional>
false

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

absCaptureTimeBoolean<optional>
false

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

dependencyDescriptorBoolean<optional>
false

True to modify SDP for supporting aom dependency descriptor header extension. 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.

bandwidthNumber<optional>
0

Broadcast bandwidth. 0 for unlimited.

disableVideoBoolean<optional>
false

Disable the opportunity to send video stream.

disableAudioBoolean<optional>
false

Disable the opportunity to send audio stream.

codecVideoCodec<optional>
'h264'

Codec for publish stream.

simulcastBoolean<optional>
false

Enable simulcast. Only available in Google Chrome and with H.264 or VP8 video codecs.

scalabilityModeString<optional>
null

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

peerConfigRTCConfiguration<optional>
null

Options to configure the new RTCPeerConnection.

recordBoolean<optional>
false

Enable stream recording. If record is not provided, use default Token configuration. Only available in Tokens with recording enabled.

eventsArray.<String><optional>
null

Specify which events will be delivered by the server (any of "active" | "inactive" | "viewercount").*

priorityNumber<optional>
null

When multiple ingest streams are provided by the customer, add the ability to specify a priority between all ingest streams. Decimal integer between the range [-2^31, +2^31 - 1]. For more information, visit our documentation.

Returns:

Promise object which resolves when the broadcast started successfully.

Type: 
Promise.<void>
Examples
await publish.connect(options)
import Publish from '@millicast/sdk'

//Define callback for generate new token
const tokenGenerator = () => getYourPublisherConnection(token, streamName)

//Create a new instance
const streamName = "My Millicast Stream Name"
const millicastPublish = new Publish(streamName, tokenGenerator)

//Get MediaStream
const mediaStream = getYourMediaStream()

//Options
const broadcastOptions = {
   mediaStream: mediaStream
 }

//Start broadcast
try {
 await millicastPublish.connect(broadcastOptions)
} catch (e) {
 console.log('Connection failed, handle error', e)
}

getRTCPeerConnection() → {RTCPeerConnection}

Get current RTC peer connection.

Returns:

Object which represents the RTCPeerConnection.

Type: 
RTCPeerConnection

isActive() → {Boolean}

Get if the current connection is active.

Inherited From
Returns:
  • True if connected, false if not.
Type: 
Boolean

(async) reconnect(dataopt)

Reconnects to last broadcast.

Parameters:
NameTypeAttributesDescription
dataObject<optional>

This object contains the error property. It may be expanded to contain more information in the future.

Properties
NameTypeDescription
errorString

The value sent in the first reconnect event within the error key of the payload

(async) record()

Initialize recording in an active stream and change the current record option.

setReconnect()

Sets reconnection if autoReconnect is enabled.

stop()

Stops connection.

Inherited From

(async) unrecord()

Finalize recording in an active stream and change the current record option.

Events

reconnect

Emits with every reconnection attempt made when an active stream stopped unexpectedly.

Type:
  • Object
Properties
NameTypeDescription
timeoutNumber

Next retry interval in milliseconds.

errorError

Error object with cause of failure. Possible errors are:

  • Signaling error: wsConnectionError if there was an error in the Websocket connection.
  • Connection state change: RTCPeerConnectionState disconnected if there was an error in the RTCPeerConnection.
  • Attempting to reconnect if the reconnect was trigered externally.
  • Or any internal error thrown by either Publish.connect or View.connect methods