logo
Video Call
On this page

Custom Video Preprocessing

2024-07-11

Feature Overview

Video preprocessing is a process between capture and encoding. Developers capture video data themselves. After obtaining the video data captured by the SDK, they can perform video preprocessing through the SDK's built-in basic beauty effects and watermark features. If the SDK cannot meet developers' needs (for example, the beauty effects cannot achieve the expected results), they can also combine with ZEGO Effects SDK to perform special processing on the video, such as beauty effects, adding accessories, etc. This process is custom video preprocessing.

Compared with custom video capture, custom video preprocessing has the advantage that developers do not need to manage device input sources, they only need to operate on the raw data provided by ZEGO Express SDK, and then send it back to ZEGO Express SDK.

Note

For more complex scenarios, such as wanting to perform layer mixing through camera images, it is recommended that developers use the Custom Video Capture feature to implement it, as this method has greater room for performance optimization.

Example Source Code Download

ZEGO provides example source code for implementing beauty effects through the Effects SDK. Please refer to Using Real-Time Video and AI Beauty Effects Together.

Prerequisites

Before performing custom video preprocessing, please ensure:

Usage Steps

The flow and interface calls for custom video preprocessing are as follows:

1

Initialize Express SDK and login to Room.

3

Get raw video data and perform video preprocessing.

1

Call the setCustomVideoProcessHandler interface to register custom video preprocessing callbacks and implement callback methods such as onCapturedUnprocessedTextureData.

2

Start preview and publish stream.

3

Through the onStart callback, notify that custom video preprocessing has started. If developers use third-party beauty libraries or pre-allocate memory, they can initialize tools and allocate memory at this time.

4

Through the onCapturedUnprocessedTextureData callback interface, get raw video data. Developers can perform related processing according to business needs: they can process through Express SDK's built-in basic beauty effects, watermarks and other features; they can also combine with Effects SDK to achieve more processing effects.

5

After video data processing is completed, in the onCapturedUnprocessedTextureData callback, call the sendCustomVideoProcessedTextureData interface to send the processed video frame data to Express SDK.

4

Other clients can play the processed video stream.

5

Stop preview and publishing stream.

6

Logout from Room.

7

Through the onStop callback, notify that custom video preprocessing has ended. If developers use third-party beauty libraries or pre-allocate memory, they can deinitialize tools and release memory at this time.

8

Destroy Engine and release resources.

1 Enable Custom Video Preprocessing

Create a custom video preprocessing ZegoCustomVideoProcessConfig object and set the bufferType property to provide the video frame data type to Express SDK.

Currently Android SDK supports the following 3 types of bufferType data types. Setting other enumeration values will not work properly:

Buffer TypeEnumeration ValueDescription
GLTexture2DZegoVideoBufferType.GL_TEXTURE_2DIndicates raw video data of Texture texture type.
SurfaceTextureZegoVideoBufferType.SURFACE_TEXTUREIndicates raw video data of SurfaceTexture type.
GLTexture2DAndRawDataZegoVideoBufferType.GL_TEXTURE_2D_AND_RAW_DATAIndicates OpenGL Texture 2D type video frame and raw data type video frame.

Before starting preview and starting to publish stream, call the enableCustomVideoProcessing interface to enable the custom video preprocessing feature.

The following takes "ZegoVideoBufferType.GL_TEXTURE_2D" as an example to demonstrate the usage of custom video preprocessing.

ZegoCustomVideoProcessConfig config = new ZegoCustomVideoProcessConfig();
// Select GL_TEXTURE_2D type video frame data
config.bufferType = ZegoVideoBufferType.GL_TEXTURE_2D;

// Enable custom preprocessing
express.enableCustomVideoProcessing(true, config, ZegoPublishChannel.MAIN);

2 Get Raw Video Data and Perform Video Preprocessing

  1. Call the setCustomVideoProcessHandler interface to register custom video preprocessing callbacks and implement callback methods such as onCapturedUnprocessedTextureData.

  2. When the SDK obtains raw video data, it will notify the developer through the onCapturedUnprocessedTextureData method.

  3. Developers can use Express SDK's built-in basic beauty effects, watermarks and other features to process video data. For details, please refer to Publish Stream Video Enhancement; they can also combine with Effects SDK to achieve more processing effects. For details, please refer to Using Real-Time Video and AI Beauty Effects Together.

    Warning

    If developers use third-party beauty libraries or pre-allocate memory, please:

    • Initialize tools and allocate memory when custom video preprocessing starts (onStart).
    • Deinitialize tools and release memory when custom video preprocessing ends (onStop).
  4. After processing is completed, call the sendCustomVideoProcessedTextureData interface to send the processed video frame data to Express SDK, which will be published to the stream, and other clients can play the processed video stream.

// Callback method to get raw data
// Callback processing
express.setCustomVideoProcessHandler(new IZegoCustomVideoProcessHandler() {
    ...

    // Receive Texture from ZegoExpressEngine engine
    @Override
    public void onCapturedUnprocessedTextureData(int textureID, int width, int height, long referenceTimeMillisecond, ZegoPublishChannel channel) {


        ZegoEffectsVideoFrameParam param = new ZegoEffectsVideoFrameParam();
        param.format = ZegoEffectsVideoFrameFormat.BGRA32;
        param.width = width;
        param.height = height;

        // Custom preprocessing: Use Effects SDK for video processing here
        int processedTextureID = effects.processTexture(textureID, param);

        // Send the processed buffer back to Express SDK
        express.sendCustomVideoProcessedTextureData(processedTextureID, width, height, referenceTimeMillisecond);
    }
}

Previous

Custom Video Rendering

Next

Super Resolution

On this page

Back to top