Kotlin

Publishing a stream to the session

When the app connects to the Vonage Video session, we want it to publish an audio-video stream to the session, using the camera and microphone:

  1. Add a publisher property to the MainActivity class (after the declaration of the session property):
private var publisher: Publisher? = null

The Publisher class is defined in the Android SDK.

  1. Modify the onConnected() callback in your SessionListener to set up and publish the stream:
override fun onConnected(session: Session) {
    Log.d(TAG, "onConnected: Connected to session: ${session.sessionId}")

    publisher = Publisher.Builder(this@MainActivity).build().apply {
        setPublisherListener(publisherListener)
        renderer.setStyle(
            BaseVideoRenderer.STYLE_VIDEO_SCALE,
            BaseVideoRenderer.STYLE_VIDEO_FILL
        )
    }

    // Add the publisher view to the UI
    publisherView = publisher?.view

    // Publish the stream to the session
    session.publish(publisher)
}

The code above uses Publisher.Builder() to instantiate a Publisher object. The constructor takes one parameter: the context associated with this process (although it may be different in a production app).

The Publisher.setPublisherListener() method sets the object that will implement the PublisherListener interface. This interface includes callback methods are called in response to publisher-related events.

Important: The context you used depends on the specific use case. However usually it is desired for the session to live outside of the Activity (for example, between activities). For production applications, it's convenient to use application context instead of activity context.

The code then passes the Publisher object in as a parameter of the Session.publish() method. This method publishes an audio-video stream to the session, using the camera and microphone of the Android device. (Note that in an Android virtual device, the Android SDK uses a test video when publishing a stream).

The Publisher object has a getView() returns, which returns an Android View object. This view displays the video captured from the device’s camera. The code adds this view as a subview of the publisherView object.

  1. To log events, add a publisherListener property to the sessionListener of the MainActivity:
private val publisherListener = object : PublisherKit.PublisherListener {
    override fun onStreamCreated(publisherKit: PublisherKit, stream: Stream) {
        Log.d(TAG, "onStreamCreated: Publisher Stream Created. Stream ID: ${stream.streamId}")
    }

    override fun onStreamDestroyed(publisherKit: PublisherKit, stream: Stream) {
        Log.d(TAG, "onStreamDestroyed: Publisher Stream Destroyed. Stream ID: ${stream.streamId}")
    }

    override fun onError(publisherKit: PublisherKit, opentokError: OpentokError) {
        Log.e(TAG, "PublisherKit onError: ${opentokError.message}")
    }
}

Note that the Publisher class extends the PublisherKit class (also defined by the Android SDK). The PublisherKit class is a base class you can use to create advanced publishers (such as publishers that use custom video capturers or renderers).

This implements the PublisherListener methods:

  • onStreamCreated(publisherKit, stream) — Called when the publisher starts streaming to the session.
  • onStreamDestroyed(publisherKit, stream) — Called when the publisher stops streaming to the session.
  • onError(error) — Called when the client fails in publishing to the session. An OpentokError object is passed into the method.