Suggestions

close search

Back to Basic Video Chat Overview

Basic Video Chat Step 6: Subscribing to another client's audio-video stream

  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams

After completing the last step, you should understand how your app is publishing to a session. In this step we'll understand how to subscribe to other users that are streaming in the session.

The onStreamReceived(Session session, Stream stream) method (defined in the Session.SessionListener interface) is called when a new stream is created in the session. The app implements this method with the following:

@Override
public void onStreamReceived(Session session, Stream stream) {
    Log.i(LOG_TAG, "Stream Received");

    if (mSubscriber == null) {
        mSubscriber = new Subscriber.Builder(this, stream).build();
        mSubscriber.setSubscriberListener(this);
        mSubscriber.getRenderer().setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE,
                BaseVideoRenderer.STYLE_VIDEO_FILL);
        mSession.subscribe(mSubscriber);
    }
}

The method is passed a session and Stream object, which are both defined by the OpenTok Android SDK. The Stream object represents the stream that another client is publishing. Although this app assumes that only one other client is connecting to the session and publishing, the method checks to see if the app is already subscribing to a stream (if the mSubscriber property is null). If not, the method initializes a Subscriber object (mSubscriber) using the Subscriber.Builder. This is used to subscribe to the stream, passing in the OTStream object to the constructor function. It also sets the ChatActivity object as the implementor of the SubscriberKit.SubscriberListener interface. Similar to the session and Publisher listener patterns, this interface defines methods that handle events related to the subscriber.

The Subscriber class is also defined in the OpenTok Android SDK. It is a subclass of SubscriberKit, which lets you define a custom video renderer. The Subscriber object implements a built-in video renderer.

The following code sets the Subscriber to scale the video to fill the entire area of the renderer, with cropping as needed:

  mSubscriber.getRenderer().setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE,
          BaseVideoRenderer.STYLE_VIDEO_FILL)

The app then calls the subscribe(SubscriberKit) method of the Session object to have the app subscribe to the stream.

When the app starts receiving the subscribed stream, the implementation of the onConnected(subscriberKit) method (defined by the SubscriberKit.SubscriberListener interface) is called:

@Override
public void onConnected(SubscriberKit subscriberKit) {
    Log.i(LOG_TAG, "Subscriber Connected");

    mSubscriberViewContainer.addView(mSubscriber.getView());
}

It adds view of the subscriber stream (returned by the getView() method of the Subscriber object) as a subview of the mSubscriberViewContainer View object.

If the subscriber's stream is dropped from the session (perhaps the client chose to stop publishing or to disconnect from the session), the implementation of the Session.SessionListener.onStreamDropped(session, stream) method is called:

@Override
public void onStreamDropped(Session session, Stream stream) {
    Log.i(LOG_TAG, "Stream Dropped");

    if (mSubscriber != null) {
        mSubscriber = null;
        mSubscriberViewContainer.removeAllViews();
    }
}

That's it! Give yourself a pat on the back!

  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams