PublisherKit

public class PublisherKit implements Observer

A publisher captures an audio-video stream from the sources you specify. You can then publish the audio-video stream to an OpenTok session by calling the Session#publish(PublisherKit publisher) method.

Create a subclass of PublisherKit if you are interested in providing your own video capturing and rendering implementation. Otherwise, use the Publisher class, which includes a pre-built video capturer and renderer.

Nested types: PublisherKit.PublisherKitVideoType, PublisherKit.VideoBitratePreset, PublisherKit.DegradationPreference, PublisherKit.PreferredVideoCodecs, PublisherKit.PublisherListener, PublisherKit.VideoListener, PublisherKit.PublisherRtcStats, PublisherKit.PublisherRtcStatsReportListener, PublisherKit.AudioLevelListener, PublisherKit.VideoLayerStats, PublisherKit.PublisherVideoStats, PublisherKit.PublisherAudioStats, PublisherKit.PublisherMediaLinkStats, PublisherKit.VideoStatsListener, PublisherKit.AudioStatsListener, PublisherKit.MediaLinkStatsListener, PublisherKit.MuteListener, PublisherKit.Builder, PublisherKit.CustomAudioTransformer, PublisherKit.CustomVideoTransformer, PublisherKit.VideoTransformer, PublisherKit.AudioTransformer

Properties

Name Type Description
VIDEO_REASON_NONE String static final Constant value: none
VIDEO_REASON_QUALITY String static final The video event was caused by a change in the network congestion level. The PublisherKit.VideoListener#onVideoDisabled(PublisherKit, String) method is called. When conditions improve, the video stream resumes, and the PublisherKit.VideoListener#onVideoEnabled(PublisherKit, String) method is called. When the video stream is dropped, the subscriber continues to receive the audio stream, if there is one. Constant value: quality
VIDEO_REASON_QUALITY_DEGRADATION_BANDWIDTH String static final Quality degradation due to insufficient network bandwidth. Constant value: bandwidth
VIDEO_REASON_QUALITY_DEGRADATION_CPU String static final Quality degradation caused by CPU limitations on the client device. Constant value: cpu
VIDEO_REASON_QUALITY_DEGRADATION_OTHER String static final Quality degradation caused by other reasons. Constant value: other
VIDEO_REASON_CODEC_CHANGE String static final The video codec for the stream has changed. Constant value: codecChange
VIDEO_REASON_RESOLUTION_CHANGE String static final Change in video resolution or number of layers. This can occur due to bitrate constraints, encoder adaptation, camera source changes (e.g., switching cameras or changing input resolution), or configuration changes. Does not always imply quality degradation. Constant value: resolutionChange
NETWORK_REASON_NETWORK_CONDITION_CHANGE String static final Indicates that the local participant’s network condition has changed. This event is triggered when a significant change is detected in the network condition. Constant value: networkConditionChange
handler Handler protected
publisherListener PublisherKit.PublisherListener protected
audioLevelListener PublisherKit.AudioLevelListener protected
videoStatsListener PublisherKit.VideoStatsListener protected
audioStatsListener PublisherKit.AudioStatsListener protected
mediaLinkStatsListener PublisherKit.MediaLinkStatsListener protected
muteListener PublisherKit.MuteListener protected
rtcStatsReportListener PublisherKit.PublisherRtcStatsReportListener protected
context Context protected
renderer BaseVideoRenderer protected
capturer BaseVideoCapturer protected
videoListener PublisherKit.VideoListener protected

Methods

Name Description
PublisherKit(builder)
destroy() Releases this object, including all hardware and UI resources bound to it, if it cannot be released by the garbage collector.
finalize() Called by the garbage collector when it determines that there are no more references to the PublisherKit object.
getCaptionsInputLanguage() → {String} Gets the configured captions input language for this publisher.
getCapturer() → {BaseVideoCapturer} Returns the video capturer used by this publisher.
getDegradationPreference() → {PublisherKit.DegradationPreference} Gets the preferred policy that the video engine will follow when adjusting frame rate and resolution to limited bandwidth and CPU conditions.
getMaxVideoBitrate() → {int} Gets the maximum video bitrate for the publisher's video stream, in bits per second.
getName() → {String} The stream name.
getPublishAudio() → {boolean} Whether the Publisher is publishing audio (true) or not ( false).
getPublishCaptions() → {boolean} Whether the Publisher is publishing captions (true) or not ( false).
getPublishVideo() → {boolean} Whether the Publisher is publishing video (true) or not ( false).
getPublisherVideoType() → {PublisherKit.PublisherKitVideoType} Returns the type of video for this publisher.
getRenderer() → {BaseVideoRenderer} Returns the video renderer for this publisher.
getRtcStatsReport() Gets the RTC stats report for the publisher.
getSession() → {Session} Returns the Session bound to this Publisher instance.
getStream() → {Stream} Returns the stream object corresponding the the Publisher's stream.
getTargetScalabilityMode() → {String} Gets the target scalability mode that was explicitly set via setTargetScalabilityMode.
getVideoBitratePreset() → {PublisherKit.VideoBitratePreset} Gets the currently active video bitrate preset for the publisher's stream.
getView() → {View} Returns the android.view.View object that contains the Publisher video.
onAudioLevelUpdated(audioLevel) Called periodically to report the audio level of the publisher.
onError(error) Invoked when the publisher fails.
onMuteForced() Invoked when a moderator has forced this publisher to mute audio.
onPause() Do not call directly; call Session#onPause() instead.
onRestart() Restarts publisher resources that were previously released by calling the PublisherKit#onStop() method.
onResume() Do not call directly; call Session#onResume() instead.
onRtcStatsReport(stats) Called when the publisher's RTC stats report is available in response to a call to getRtcStatsReport().
onStop() Releases resources tied to the publisher.
onStreamCreated(stream) Invoked when the publisher's stream is created.
onStreamDestroyed(stream) Invoked when the publisher's stream is destroyed.
setAudioLevelListener(listener) Binds an AudioLevelListener to this PublisherKit instance to monitor audio level updates.
setAudioStatsListener(listener) Sets up a listener for publisher audio statistics.
setAudioTransformers(audioTransformers) Sets a list of audio transformers to this publisher's audio stream.
setCaptionsInputLanguage(language) Sets the preferred captions input language for this publisher.
setCapturer(newCapturer) Sets the video capturer for this publisher.
setDegradationPreference(degradationPreference) Gets the preferred policy that the video engine will follow when adjusting frame rate and resolution to limited bandwidth and CPU conditions.
setMaxVideoBitrate(maxVideoBitrate) Sets the maximum video bitrate for the publisher's video stream, in bits per second.
setMediaLinkStatsListener(listener) Sets up a listener for publisher media link statistics.
setMuteListener(listener) Sets a MuteListener object to monitor when a moderator forces this publisher to mute audio.
setName(name) Set the name for this stream.
setPublishAudio(publishAudio) Toggles publishing of audio on or off, based on the value passed in.
setPublishCaptions(publishCaptions) Toggles publishing of captioning on or off, based on the value passed in.
setPublishVideo(publishVideo) Toggles publishing of video on or off, based on the value passed in.
setPublisherListener(listener) Binds a PublisherListener to this Publisher instance to monitor state changes.
setPublisherVideoType(type) Specifies the type of video for this stream.
setRenderer(renderer) Sets the video renderer for this publisher.
setRtcStatsReportListener(listener) Sets the RTC stats report listener the subscriber.
setStyle(key, value) Sets a style for the video renderer used by this publisher.
setTargetScalabilityMode(scalabilityMode) Sets the target scalability mode (SVC - Scalable Video Coding) for this publisher.
setVideoBitratePreset(videoBitratePreset) Sets a video bitrate preset strategy for the publisher's video stream.
setVideoListener(listener) Sets a VideoListener object to monitor when the publisher starts and stops sending video.
setVideoStatsListener(listener) Sets up a listener for publisher video statistics.
setVideoTransformers(videoTransformers) Sets a list of video transformers to this publisher's video stream.
update(o, arg) This method is called whenever the observed object is changed.

PublisherKit(builder)

Parameters:
Name Type Description
builder PublisherKit.Builder

destroy()

Releases this object, including all hardware and UI resources bound to it, if it cannot be released by the garbage collector. Resources are generally safely released by the garbage collector, and you do not need to call this method. However, this can happen at any time at the VM discretion, which might not be the desired behavior in some cases. This method allows manually releasing resources for scenarios where immediateness is desired:

An app wants to publish again but needs to have access to the camera immediately after unpublishing. An app wants to make sure that the audio device is released immediately after unpublishing.

After you have called this method, you cannot reuse the publisher.

finalize()

Called by the garbage collector when it determines that there are no more references to the PublisherKit object.

See: : - Throws Throwable

getCaptionsInputLanguage() → {String}

Gets the configured captions input language for this publisher.

Returns:

The configured language code, or null if input is not set.

getCapturer() → {BaseVideoCapturer}

Returns the video capturer used by this publisher.

Returns:

BaseVideoCapturer

getDegradationPreference() → {PublisherKit.DegradationPreference}

Gets the preferred policy that the video engine will follow when adjusting frame rate and resolution to limited bandwidth and CPU conditions. See DegradationPreference for a description of the possible values.

Returns:

The current degradation preference.

getMaxVideoBitrate() → {int}

Gets the maximum video bitrate for the publisher's video stream, in bits per second.

Returns the currently configured upper limit. If no manual bitrate has been set, or if a bitrate preset is active, the value is 0.

See: : - setMaxVideoBitrate(int maxVideoBitrate)

Returns:

The maximum video bitrate in bits per second, or 0 if unset or using a preset.

getName() → {String}

The stream name. The name appears at the bottom of Subscriber videos.

Returns:

publisher name

getPublishAudio() → {boolean}

Whether the Publisher is publishing audio (true) or not ( false).

Returns:

boolean

getPublishCaptions() → {boolean}

Whether the Publisher is publishing captions (true) or not ( false).

Returns:

boolean

getPublishVideo() → {boolean}

Whether the Publisher is publishing video (true) or not ( false).

Returns:

boolean

getPublisherVideoType() → {PublisherKit.PublisherKitVideoType}

Returns the type of video for this publisher.

See: : - setPublisherVideoType(PublisherKitVideoType type)

Returns:

PublisherKit.PublisherKitVideoType

getRenderer() → {BaseVideoRenderer}

Returns the video renderer for this publisher.

Returns:

BaseVideoRenderer

getRtcStatsReport()

Gets the RTC stats report for the publisher. This is an asynchronous operation. Call the setRtcStatsReportListener(PublisherKit.PublisherRtcStatsReportListener listener) method, and then implement the PublisherKit.PublisherRtcStatsReportListener#onRtcStatsReport(PublisherKit publisher, PublisherKit.PublisherRtcStats[] stats) method prior to calling this method. When the stats are available, the implementation of the PublisherKit.PublisherRtcStatsReportListener#onRtcStatsReport(PublisherKit publisher, PublisherKit.PublisherRtcStats[] stats) method is called.

Also see setAudioStatsListener(PublisherKit.AudioStatsListener listener), setVideoStatsListener(PublisherKit.VideoStatsListener listener), and SubscriberKit#getRtcStatsReport().

getSession() → {Session}

Returns the Session bound to this Publisher instance.

See: : - Session#publish(com.opentok.android.PublisherKit)

Returns:

The Session bound to this Publisher instance.

getStream() → {Stream}

Returns the stream object corresponding the the Publisher's stream.

Returns:

The stream object.

getTargetScalabilityMode() → {String}

Gets the target scalability mode that was explicitly set via setTargetScalabilityMode. Returns the last value passed to the setter, or null if the setter has never been called. This reflects the user's intent, not the mode actually applied (which may differ due to codec-dependent fallback).

This is a beta feature.

Returns:

The target scalability mode set by the caller, or null if not explicitly set.

getVideoBitratePreset() → {PublisherKit.VideoBitratePreset}

Gets the currently active video bitrate preset for the publisher's stream.

Returns the preset strategy in use. If a custom bitrate has been manually set using setMaxVideoBitrate(int), this method returns VideoBitratePreset#VideoBitratePresetCustom.

See: : - setVideoBitratePreset(VideoBitratePreset videoBitratePreset)

Returns:

The active VideoBitratePreset.

getView() → {View}

Returns the android.view.View object that contains the Publisher video.

Returns:

View

onAudioLevelUpdated(audioLevel)

Called periodically to report the audio level of the publisher.

Parameters:
Name Type Description
audioLevel float The audio level, from 0 to 1.0. Adjust this value logarithmically for use in a user interface visualization (such as a volume meter).

onError(error)

Invoked when the publisher fails.

Most errors are fatal: the publisher is automatically dropped and can no longer be used. Some errors are continuable and leave the publisher usable; when an error is non-fatal it is stated in the error description.

If you extend the PublisherKit class, you can override this method instead of the onError() method of the PublisherKit.PublisherListener interface.

Parameters:
Name Type Description
error OpentokError The error that invoked this callback.

onMuteForced()

Invoked when a moderator has forced this publisher to mute audio.

If you extend the PublisherKit class, you can override this method instead of the onMuteForced() method of the PublisherKit.MuteListener interface.

onPause()

Do not call directly; call Session#onPause() instead.

onRestart()

Restarts publisher resources that were previously released by calling the PublisherKit#onStop() method.

onResume()

Do not call directly; call Session#onResume() instead.

onRtcStatsReport(stats)

Called when the publisher's RTC stats report is available in response to a call to getRtcStatsReport().

If you extend the PublisherKit class, you can override this method instead of implementing the onRtcStatsReport() method of the PublisherKit.PublisherRtcStatsReportListener interface.

Parameters:
Name Type Description
stats PublisherKit.PublisherRtcStats[]

See: : - PublisherRtcStatsReportListener#onRtcStatsReport(PublisherKit publisher, PublisherRtcStats[] stats)

onStop()

Releases resources tied to the publisher. Calling this method causes the publisher to stop capturing, and then it releases the camera. To reuse the publisher after calling this method, call the PublisherKit#onRestart() method.

onStreamCreated(stream)

Invoked when the publisher's stream is created.

If you extend the PublisherKit class, you can override this method instead of the onStreamCreated() method of the PublisherKit.PublisherListener interface.

Parameters:
Name Type Description
stream Stream The stream that that is created.

onStreamDestroyed(stream)

Invoked when the publisher's stream is destroyed.

If you extend the PublisherKit class, you can override this method instead of the onStreamDestroyed() method of the PublisherKit.PublisherListener interface.

Parameters:
Name Type Description
stream Stream The stream that that is destroyed.

setAudioLevelListener(listener)

Binds an AudioLevelListener to this PublisherKit instance to monitor audio level updates.

Parameters:
Name Type Description
listener PublisherKit.AudioLevelListener The AudioLevelListener that will monitor this instance. Set this to null to disconnect a listener and stop monitoring audio levels.

setAudioStatsListener(listener)

Sets up a listener for publisher audio statistics. The listener object implements the PublisherKit.AudioStatsListener#onAudioStats(PublisherKit publisher, PublisherKit.PublisherAudioStats[] stats) method. This method is called periodically to report the following:

  • Total audio packets lost
  • Total audio packets sent
  • Total audio bytes sent
Parameters:
Name Type Description
listener PublisherKit.AudioStatsListener

See: : - setVideoStatsListener(VideoStatsListener listener)

setAudioTransformers(audioTransformers)

Sets a list of audio transformers to this publisher's audio stream. If the list is empty, calling this method resets the transformers. The array can be composed of Vonage audio transformers provided by ml-library or custom transfomers (see this developer guide). Note: Media transformers are not supported on all devices or Android versions. See this documentation. .

Parameters:
Name Type Description
audioTransformers ArrayList<PublisherKit.AudioTransformer> The list of audio transformers, in the order to be applied to the stream.

setCaptionsInputLanguage(language)

Sets the preferred captions input language for this publisher.

Parameters:
Name Type Description
language String A supported BCP-47 language code, or null to reset to the default selection.

setCapturer(newCapturer)

Sets the video capturer for this publisher.

This method is deprecated. Instead, call the PublisherKit.Builder#capturer(BaseVideoCapturer capturer) method when using the builder to create the PublisherKit instance.

Deprecated

Parameters:
Name Type Description
newCapturer BaseVideoCapturer

setDegradationPreference(degradationPreference)

Gets the preferred policy that the video engine will follow when adjusting frame rate and resolution to limited bandwidth and CPU conditions. See DegradationPreference for a description of the possible values.

Parameters:
Name Type Description
degradationPreference PublisherKit.DegradationPreference The degradation preference.

setMaxVideoBitrate(maxVideoBitrate)

Sets the maximum video bitrate for the publisher's video stream, in bits per second.

This method applies an explicit upper limit to the publisher’s video bitrate. Setting the value to 0 clears any manual cap and reverts to the default preset-based adaptive bitrate behavior with no explicit limit.

Parameters:
Name Type Description
maxVideoBitrate int The maximum video bitrate in bits per second, between 5,000 and 10,000,000.

See: : - getMaxVideoBitrate() - setVideoBitratePreset(VideoBitratePreset videoBitratePreset) - Publisher Max Bitrate

setMediaLinkStatsListener(listener)

Sets up a listener for publisher media link statistics. The listener object implements the PublisherKit.MediaLinkStatsListener#onMediaLinkStats(PublisherKit, PublisherMediaLinkStats[]) and PublisherKit.MediaLinkStatsListener#onNetworkConditionChanged(PublisherKit, PublisherMediaLinkStats, String) methods.

Parameters:
Name Type Description
listener PublisherKit.MediaLinkStatsListener The MediaLinkStatsListener instance, or null to remove.

See: : - setVideoStatsListener(VideoStatsListener listener) - setAudioStatsListener(AudioStatsListener listener)

setMuteListener(listener)

Sets a MuteListener object to monitor when a moderator forces this publisher to mute audio.

Parameters:
Name Type Description
listener PublisherKit.MuteListener The MuteListener instance.

setName(name)

Set the name for this stream. The name appears at the bottom of the publisher video and at the bottom of subscriber videos associated with the published stream. You can set this name after initializing the publisher and before the PublisherKit.PublisherListener#onStreamCreated(PublisherKit publisher, Stream stream) method is called. Setting the property after the PublisherKit.PublisherListener#onStreamCreated(PublisherKit publisher, Stream stream) method is called has no effect on the name displayed for the stream.

The default name is "" (an empty string).

Deprecated

Parameters:
Name Type Description
name String

setPublishAudio(publishAudio)

Toggles publishing of audio on or off, based on the value passed in.

Parameters:
Name Type Description
publishAudio boolean Whether to publish audio (true) or not ( false).

setPublishCaptions(publishCaptions)

Toggles publishing of captioning on or off, based on the value passed in.

Parameters:
Name Type Description
publishCaptions boolean Whether to publish captions (true) or not ( false).

setPublishVideo(publishVideo)

Toggles publishing of video on or off, based on the value passed in.

Parameters:
Name Type Description
publishVideo boolean Whether to publish video (true) or not ( false).

setPublisherListener(listener)

Binds a PublisherListener to this Publisher instance to monitor state changes.

Parameters:
Name Type Description
listener PublisherKit.PublisherListener The PublisherListener that will monitor this instance, or null to disconnect a listener.

setPublisherVideoType(type)

Specifies the type of video for this stream. Set this to one of the following values:

  • PublisherKitVideoType.PublisherKitVideoTypeScreen — Optimizes the video encoding for screen sharing. It is recommended to use a low frame rate (5 frames per second or lower) with this video type. See The OpenTok Media Router and media modes.
  • PublisherKitVideoType.PublisherKitVideoTypeCamera — This is the default setting, which you should use when using a camera as the video source.
Parameters:
Name Type Description
type PublisherKit.PublisherKitVideoType

setRenderer(renderer)

Sets the video renderer for this publisher.

This method is deprecated. Instead, call the PublisherKit.Builder#renderer(BaseVideoRenderer renderer) method when using the Builder to create the PublisherKit instance.

Deprecated

Parameters:
Name Type Description
renderer BaseVideoRenderer

setRtcStatsReportListener(listener)

Sets the RTC stats report listener the subscriber. See getRtcStatsReport().

Parameters:

setStyle(key, value)

Sets a style for the video renderer used by this publisher.

By default, there is only one pre-defined style key: BaseVideoRenderer#STYLE_VIDEO_SCALE. You can set a value for this key to determine the scaling of the video. The following values are supported:

Calling this method is equivalent to calling getRenderer().setStyle(). However, if you extend this class, you can override this method to support other styles and behaviors.

Parameters:
Name Type Description
key String The style to set.
value String The value you are setting the style to.

setTargetScalabilityMode(scalabilityMode)

Sets the target scalability mode (SVC - Scalable Video Coding) for this publisher. The scalability mode controls the number of spatial (resolution) and temporal (frame rate) layers that WebRTC encodes. The mode is applied to the Media Router media path.

This method may be called at any time — it does not require the media path negotiation to have completed. The mode will be applied once the Media Router media path is established.

If a non-SVC codec (VP8, H.264) is negotiated on the Media Router media path, the closest applicable mode is used: spatial layers are forced to 1, preserving the temporal dimension (e.g. L2T2 → L1T2 when VP8 is negotiated).

This is a beta feature.

Parameters:
Name Type Description
scalabilityMode String The target scalability mode.

See: : - Throws IllegalArgumentException — if the requested mode is not a valid scalability mode (i.e. not one of L1T1, L1T2, L1T3, L2T1, L2T2, L2T3, L3T1, L3T2, L3T3).

setVideoBitratePreset(videoBitratePreset)

Sets a video bitrate preset strategy for the publisher's video stream.

Preset configurations balance video quality and bandwidth usage with predefined strategies. Preset settings and manual bitrate settings are mutually exclusive. Setting the setMaxVideoBitrate(int) method overrides any preset and automatically changes the active preset to VideoBitratePreset#VideoBitratePresetCustom.

Note: The VideoBitratePreset#VideoBitratePresetCustom value cannot be set directly.

Parameters:
Name Type Description
videoBitratePreset PublisherKit.VideoBitratePreset The VideoBitratePreset to apply.

See: : - getVideoBitratePreset() - setMaxVideoBitrate(int maxVideoBitrate) - Publisher Max Bitrate

setVideoListener(listener)

Sets a VideoListener object to monitor when the publisher starts and stops sending video.

Parameters:
Name Type Description
listener PublisherKit.VideoListener The VideoListener instance.

setVideoStatsListener(listener)

Sets up a listener for publisher video statistics. The listener object implements the PublisherKit.VideoStatsListener#onVideoStats(PublisherKit publisher, PublisherKit.PublisherVideoStats[] stats) method and optionally the PublisherKit.VideoStatsListener#onVideoQualityChanged(PublisherKit, PublisherVideoStats, String) method.

Parameters:
Name Type Description
listener PublisherKit.VideoStatsListener

See: : - setAudioStatsListener(AudioStatsListener listener)

setVideoTransformers(videoTransformers)

Sets a list of video transformers to this publisher's video stream. If the list is empty, calling this method resets the transformers. The array can be composed by Vonage video transformers provided by ml-library or custom transfomers (see this developer guide). Note: Media transformers are not supported on all devices or Android versions. See this documentation.

Parameters:
Name Type Description
videoTransformers ArrayList<PublisherKit.VideoTransformer> The list of video transformers, in the order to be applied to the stream.

update(o, arg)

This method is called whenever the observed object is changed. This class implements the Observer interface, so it observes events, such as the session pause and session resume events.

Deprecated

Parameters:
Name Type Description
o Observable The observable object.
arg Object An argument passed to the notifyObservers method.