Suggestions

close search

Back to Custom Camera Video Capturing Overview

Custom Camera Video Capturing Step 1: Implementation

  1. 1
    Custom Camera Video Capturing Step 2:
    Implementation
  2. 2
    Custom Camera Video Capturing Step 2:
    Configuration

To see the code for this sample, switch to the basic-capturer branch of the learning-opentok-android repo:

git checkout basic-capturer

This page shows the difference between the basic-capturer branch and the basics.step-6 branch, which this branch builds from.

This branch shows you how to make minor modifications to the video capturer used by the Publisher class.

In this example, the app uses a custom video capturer to publish random pixels (white noise). This is done simply to illustrate the basic principals of setting up a custom video capturer. (For a more practical example, see the Camera Video Capturer and Screen Video Capturer examples, described in the sections that follow.)

In the initializePublisher() method ChatActivity class, after creating a Publisher object, the code calls the setCapturer(capturer) method of the Publisher object, passing in a NoiseVideoCapturer object as a parameter:

mPublisher.setCapturer(new NoiseVideoCapturer(320, 240));

NoiseVideoCapturer is a custom class that extends the BaseVideoCapturer class (defined in the OpenTok iOS SDK). This class lets you define a custom video capturer to be used by an OpenTok publisher.

The BaseVideoCapturer.init() method initializes capture settings to be used by the custom video capturer. In this sample's custom implementation of BaseVideoCapturer (NoiseVideoCapturer), the initCapture() method sets properties of a mCapturerSettings property:

@Override
public void init() {
    mCapturerHasStarted = false;
    mCapturerIsPaused = false;

    mCapturerSettings = new CaptureSettings();
    mCapturerSettings.height = mHeight;
    mCapturerSettings.width = mWidth;
    mCapturerSettings.format = BaseVideoCapturer.ARGB;
    mCapturerSettings.fps = FPS;
    mCapturerSettings.expectedDelay = 0;
}

The BaseVideoCapturer.CaptureSettings class (which defines the mCapturerSettings property) is defined by the OpenTok Android SDK. In this sample code, the format of the video capturer is set to use ARGB 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. In the NoiseVideoCapturer implementation of this method, the run() mFrameProducer thread is started:

@Override
public int startCapture() {
    mCapturerHasStarted = true;
    mFrameProducer.run();
    return 0;
}

The [self produceFrame] method creates a buffer of bytes and fills it with random noise. (Note that each frame is four bytes in the buffer, defining the constituent ARGB values for the frame.) It then passes the buffer into the provideByteArrayFrame(data, format, width, height, rotation, mirrorX) method, defined by the BaseVideoCapturer class (in the OpenTok Android SDK):

Runnable mFrameProducer = new Runnable() {
    @Override
    public void run() {
        Random random = new Random();
        byte[] buffer = new byte[mWidth * mHeight * 4];
        byte[] randoms = new byte[4];
        for (int i = 0; i < mWidth * mHeight * 4; i += 4) {
            random.nextBytes(randoms);
            buffer[i] = randoms[0];
            buffer[i + 1] = randoms[1];
            buffer[i + 2] = randoms[2];
            buffer[i + 3] = randoms[3];
        }

        provideByteArrayFrame(buffer, BaseVideoCapturer.ARGB,
                mWidth, mHeight, Surface.ROTATION_0, false);

        if (mCapturerHasStarted && !mCapturerIsPaused) {
            mFrameProducerHandler.postDelayed(mFrameProducer, mFrameProducerIntervalMillis);
        }
    }
};

This causes the publisher to send the frame of data to the video stream in the session. If the session is still publishing data, the mFrameProducer thread is run again after a specified delay (mFrameProducerIntervalMillis), causing another frame of video to be captured and published.

  1. 1
    Custom Camera Video Capturing Step 2:
    Implementation
  2. 2
    Custom Camera Video Capturing Step 2:
    Configuration