Suggestions

close search

Back to Basic Video Chat Overview

Basic Video Chat Step 4: Initializing the session

  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

In the previous two sections you set up a web service that provides session information. Now we're going to look at how the app uses that web service, connects to the session, and starts publishing. Before we dig into the code, though, let's try running the app. This will help you see what we're working towards.

  1. Find the test.html file in the root of the project. You will use the test.html file to connect to the OpenTok session and view the audio-video stream published by the Android app:

    • Edit the test.html file and set the sampleServerBaseUrl variable to match the CHAT_SERVER_URL property used in the Android app.

    • Add the test.html file to a web server. (You cannot run WebRTC videos in web pages loaded from the desktop.)

    • In a browser, load the test.html file from the web server.

  2. Run the Android app. The Android app publishes an audio-video stream to the session and the the web client app subscribes to the stream.

Now lets look at our code to see how it works.

WebServiceCoordinator.java

  1. Open the WebServiceCoordinator.java file. This is in the com.tokbox.android.demo.learningopentok package.

  2. Edit the CHAT_SERVER_URL and SESSION_INFO_ENDPOINT values to match the URL and end-point of the web service:

     private static final String CHAT_SERVER_URL = "https://yourservice.herokuapp.com";
     private static final String SESSION_INFO_ENDPOINT = CHAT_SERVER_URL + "/session";
    

WebServiceCoordinator.java calls the web service you created. This is just like what you did when you went to the URL with your browser, except it is done programatically:

Following the Listener pattern, the onCreate() method of the main ChatActivity object instantiates a WebServiceCoordinator object, sets itself as a listener by implementing WebServiceCoordinator.Listener, and calls its fetchSessionConnectionData() method. This method makes an API call to the /session endpoint of the web service to obtain the OpenTok API key, session ID, and a token to connect to the session.

public class ChatActivity extends ActionBarActivity implements WebServiceCoordinator.Listener,
    Session.SessionListener, PublisherKit.PublisherListener, SubscriberKit.SubscriberListener {

    ...
    ...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat);

    // initialize view objects from your layout
    mPublisherViewContainer = (FrameLayout)findViewById(R.id.publisher_container);
    mSubscriberViewContainer = (FrameLayout)findViewById(R.id.subscriber_container);

    // initialize WebServiceCoordinator and kick off request for necessary data
    mWebServiceCoordinator = new WebServiceCoordinator(this, this);
    mWebServiceCoordinator.fetchSessionConnectionData();
}

Once the session ID is obtained, WebServiceCoordinator fires the onSessionConnectionDataReady() method that is implemented in ChatActivity class as part of the Listener pattern. This sets the OpenTok API key, session ID, and token and then calls the initializeSession() and initializePublisher() methods.

public void onSessionConnectionDataReady(String apiKey, String sessionId, String token) {
    mApiKey = apiKey;
    mSessionId = sessionId;
    mToken = token;

    initializeSession();
    initializePublisher();
}

initializeSession()

In initializeSession(), we first initialize a new Session Object using Session.Builder. In the next line, we set this insance of the ChatActivity as the listener for session events defined by the Session.SessionListener interface. This allows our client to be notified of important session events. Finally, we connect to the session with mSession's connect method, passing in our Token.

private void initializeSession() {
  mSession = new Session.Builder(this, mApiKey, mSessionId).build();
  mSession.setSessionListener(this);
  mSession.connect(mToken);
}

The ChatActivity class must still implement the Session.SessionListener interface to listen to session events. It does this overriding methods including onConnected(Session session). This method runs once ChatActivity has connected to the session. In it we log that the event fired, and after checking if the Publisher exist, start publishing.

@Override
public void onConnected(Session session) {
    Log.i(LOG_TAG, "Session Connected");

    if (mPublisher != null) {
        mSession.publish(mPublisher);
    }
}

The Session.SessionListener interface also defines methods for handling other session-related events, which we will look at in the following section.

  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