Decoded frames interception and hangling with OpenCV¶
Overview¶
The decoded frames interception and handling feature allows to use a powerful images handling library OpenCV. The feature may be useful, for example, in case of AR implementatioin at server side and other similar cases, and is available since WCS build 5.2.1914.
OpenCV library should be built before implementing a custom Java class to integrate it.
Building OpenCV¶
Let's describe OpenCV 4.9.0 building on Centos 7, to provide glibc 2.17 compatibility.
-
Install JDK 8 and ANT
-
Detect JDK installation folder path and set the environment variable
JAVA_HOME
-
Install CMake 3.6.2 or newer
-
Install GCC 11 and switch to it to build OpenCV
-
Download and unpack OpenCV source code
-
Set up the build
-
Build OpenCV
-
Copy native and Java modules to WCS installation folder
Interceptor implementation using OpenCV¶
A Java class implementing Java IDecodedFrameInterceptorinterface should be developed. The function frameDecoded() of this class will receive decoded frames in YUV I420 format, create OpenCV Matrix for each YUV420 data array and apply blur effect
Previous transcoding generation implementation example¶
TestInterceptor.java
// Package name should be strictly defined as com.flashphoner.frameInterceptor
package com.flashphoner.frameInterceptor;
import com.flashphoner.sdk.media.IDecodedFrameInterceptor;
import com.flashphoner.sdk.media.YUVFrame;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.nio.ByteBuffer;
public class OpenCvInterceptor implements IDecodedFrameInterceptor {
// Load OpenCv java wrapper
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
@Override
public void frameDecoded(String streamName, YUVFrame frame) {
ByteBuffer[] data = frame.getData();
// WCS handles YUV pointers separately. Create Matrix for each YUV420 plane
Mat yMatrix = new Mat(frame.getHeight(), frame.getStrideY(), CvType.CV_8UC1, data[YUVFrame.Y_POINTER_IDX]);
Mat uMatrix = new Mat(frame.getChromaHeight(), frame.getStrideChroma(), CvType.CV_8UC1, data[YUVFrame.U_POINTER_IDX]);
Mat vMatrix = new Mat(frame.getChromaHeight(), frame.getStrideChroma(), CvType.CV_8UC1, data[YUVFrame.V_POINTER_IDX]);
Size gaussianKernelSize = new Size(33, 33);
// Apply Gaussian Blue. Passed kernel size should be odd
Imgproc.GaussianBlur(yMatrix, yMatrix, gaussianKernelSize, 0);
Imgproc.GaussianBlur(uMatrix, uMatrix, gaussianKernelSize, 0);
Imgproc.GaussianBlur(vMatrix, vMatrix, gaussianKernelSize, 0);
}
}
Transcoding generation 3 implementation example¶
TestInterceptor.java
// Package name should be strictly defined as com.flashphoner.frameInterceptor
package com.flashphoner.frameInterceptor;
import com.flashphoner.sdk.media.IDecodedFrameInterceptor;
import com.flashphoner.sdk.media.YUVFrame;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import java.nio.ByteBuffer;
public class OpenCvInterceptor implements IDecodedFrameInterceptor {
// Load OpenCv java wrapper
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
@Override
public void frameDecoded(String streamName, YUVFrame frame) {
if (!frame.isNewGen()) {
// skip only GEN3 supported
return;
}
ByteBuffer[] data = frame.getData();
// WCS handles YUV pointers separately. Create Matrix for each YUV420 plane
Mat yMatrix = new Mat(frame.getHeight(), frame.getStrideY(), CvType.CV_8UC1, data[YUVFrame.Y_POINTER_IDX]);
Mat uMatrix = new Mat(frame.getChromaHeight(), frame.getStrideChroma(), CvType.CV_8UC1, data[YUVFrame.U_POINTER_IDX]);
Mat vMatrix = new Mat(frame.getChromaHeight(), frame.getStrideChroma(), CvType.CV_8UC1, data[YUVFrame.V_POINTER_IDX]);
Size gaussianKernelSize = new Size(33, 33);
// Apply Gaussian Blue. Passed kernel size should be odd
Imgproc.GaussianBlur(yMatrix, yMatrix, gaussianKernelSize, 0);
Imgproc.GaussianBlur(uMatrix, uMatrix, gaussianKernelSize, 0);
Imgproc.GaussianBlur(vMatrix, vMatrix, gaussianKernelSize, 0);
}
}
Building and using the example¶
Then the class should be complied into byte code. To do this, create folder tree accordind to TestInterceptor class package name
and execute the command
javac -cp /usr/local/FlashphonerWebCallServer/lib/wcs-core.jar:/usr/local/FlashphonerWebCallServer/lib/custom/opencv-490.jar ./com/flashphoner/frameInterceptor/TestInterceptor.java
Now, pack the code compiled to jar file
and copy this file to WCS custom libraries folder
To use custom frames interceptor class, set its package name to the following parameter in flashphoner.properties
and restart WCS.
Testing¶
-
Publish a test stream in Two Way Streaming example

-
Play the stream in Player example with explicit resolution setting to enable transcoding, for example
https://test1.flashphoner.com:8444/client2/examples/demo/streaming/player/player.html?resolution=320x240,
wheretest1.flashphoner.comis WCS server address

The picture will be blurred.