This application, built on top of Basic Video Chat, showcases how to retriebe all statistics from the entire system, being they from session, publisher and/or subscriber level.
The user can retrieve video stats, conatining information such as videoPacketsLost and videoPacketsReceived by implementing listener SubscriberKit.VideoStatsListener.
It is also possible to access estimated sender stats (currentBitrate and maxBitrate). For this it is necessary making sure publisher enables sender statistics track. By default it is not enabled.
publisher = new Publisher.Builder(MainActivity.this).senderStatsTrack(true).build();
session.publish(publisher);
...
subscriber = new Subscriber.Builder(MainActivity.this, stream).build();
subscriber.getRenderer().setStyle(BaseVideoRenderer.STYLE_VIDEO_SCALE, BaseVideoRenderer.STYLE_VIDEO_FILL);
subscriber.setSubscriberListener(subscriberListener);
subscriber.setVideoStatsListener(videoStatsListener);
session.subscribe(subscriber);
...
SubscriberKit.VideoStatsListener videoStatsListener = new SubscriberKit.VideoStatsListener() {
@Override
public void onVideoStats(SubscriberKit subscriber, SubscriberKit.SubscriberVideoStats stats) {
Log.d(TAG, "onVideoStats: Data received");
Log.d(TAG, "onVideoStats: Sender Stats connectionEstimatedBandwidth" + (stats.senderStats != null ? stats.senderStats.connectionEstimatedBandwidth : "NULL"));
Log.d(TAG, "onVideoStats: Sender Stats connectionMaxAllocatedBitrate" + (stats.senderStats != null ? stats.senderStats.connectionMaxAllocatedBitrate : "NULL"));
Log.d(TAG, "onVideoStats: videoBytesReceived" + stats.videoBytesReceived);
Log.d(TAG, "onVideoStats: timeStamp" + stats.timeStamp);
Log.d(TAG, "onVideoStats: videoPacketsLost" + stats.videoPacketsLost);
Log.d(TAG, "onVideoStats: videoPacketsReceived" + stats.videoPacketsReceived);
}
};Open the OpenTokConfig file and configure the API_KEY, SESSION_ID, and TOKEN variables. You can obtain these values from your TokBox account.
For a production application, the SESSION_ID and TOKEN values must be generated by your app server application and passed to the client, because:
- credentials would expire after a certain amount of time
- credentials are lined to given session (all users would be connected to the same room)
To quickly deploy a pre-built server click at one of the Heroku buttons below. You'll be sent to Heroku's website and prompted for your OpenTok API Key and API Secret — you can obtain these values on your project page in your TokBox account. If you don't have a Heroku account, you'll need to sign up (it's free).
| PHP server | Node.js server |
|---|---|
![]() |
![]() |
| Repository | Repository |
Note: You can also build your server from scratch using one of the server SDKs.
After deploying the server open the ServerConfig file in this project and configure the CHAT_SERVER_URL with your domain to fetch credentials from the server:
public static final String CHAT_SERVER_URL = "https://YOURAPPNAME.herokuapp.com";Note that this application will ignore credentials in the
OpenTokConfigfile whenCHAT_SERVER_URLcontains a valid URL.
This is the code responsible for retrieving the credentials from web server:
private void getSession() {
Log.i(TAG, "getSession");
Call<GetSessionResponse> call = apiService.getSession();
call.enqueue(new Callback<GetSessionResponse>() {
@Override
public void onResponse(Call<GetSessionResponse> call, Response<GetSessionResponse> response) {
GetSessionResponse body = response.body();
initializeSession(body.apiKey, body.sessionId, body.token);
}
@Override
public void onFailure(Call<GetSessionResponse> call, Throwable t) {
throw new RuntimeException(t.getMessage());
}
});
}- Review other sample projects
- Read more about OpenTok Android SDK
