Suggestions

close search

Add Messaging, Voice, and Authentication to your apps with Vonage Communications APIs

Visit the Vonage API Developer Portal

Back to Tutorials

Custom Video Capturing (Android)

Overview

This tutorial walks through the steps required to make modifications to the video capturer in your OpenTok Android application.

Setting up your project

The code for this section is available in the Basic-Video-Capturer-Camera-2-Java project of the opentok-android-sdk-samples repo. If you haven't already, you'll need to clone the repo into a local directory. On the command line, run:

git clone git@github.com:opentok/opentok-android-sdk-samples.git

Open the Basic-Video-Capturer-Camera-2-Java project in Android Studio to follow along.

Exploring the code

In this example, the app uses a custom video capturer to mirror a video image. This is done simply to illustrate the basic principals of setting up a custom video capturer.

MirrorVideoCapturer is a custom class that extends the BaseVideoCapturer class (defined in the OpenTok Android SDK). The BaseVideoCapturer class lets you define a custom video capturer to be used by an OpenTok publisher:

publisher = new Publisher.Builder(MainActivity.this)
    .capturer(new MirrorVideoCapturer(MainActivity.this))
    .build();

The getCaptureSettings() method provides settings used by the custom video capturer:

@Override
public synchronized CaptureSettings getCaptureSettings() {
    CaptureSettings captureSettings = new CaptureSettings();
    captureSettings.fps = desiredFps;
    captureSettings.width = (null != cameraFrame) ? cameraFrame.getWidth() : 0;
    captureSettings.height = (null != cameraFrame) ? cameraFrame.getHeight() : 0;
    captureSettings.format = BaseVideoCapturer.NV21;
    captureSettings.expectedDelay = 0;
    return captureSettings;
}

The BaseVideoCapturer.CaptureSetting class (which defines the capturerSettings property) is defined by the OpenTok Android SDK. In this sample code, the format of the video capturer is set to use NV21 as the pixel format, with a specific number of frames per second, a specific height, and a specific width.

The BaseVideoCapturer startCapture() method is called when a publisher starts capturing video to be sent as a stream to the OpenTok session. This will occur after the Session.publish(publisher) method is called:

@Override
public synchronized int startCapture() {
    Log.d(TAG,"startCapture enter (cameraState: "+ cameraState +")");

    if (null != camera && CameraState.OPEN == cameraState) {
        return startCameraCapture();
    } else if (CameraState.SETUP == cameraState) {
        Log.d(TAG,"camera not yet ready, queuing the start until camera is opened");
        executeAfterCameraOpened = () -> startCameraCapture();
    } else {
        throw new Camera2Exception("Start Capture called before init successfully completed");
    }

    Log.d(TAG,"startCapture exit");

    return 0;
}

Congratulations! You've finished the Custom Video Capturer Tutorial for Android.
You can continue to play with and adjust the code you've developed here, or check out the Next Steps below.