Raw Video Data
Introduction
During the video transmission process, you can pre- and post-process the captured video data to achieve the desired playback effect.
Agora provides the raw data function for you to process the video data per your application scenario. This function enables you to pre-process the captured video frames before sending it to the encoder, or to post-process the decoded video frames.
Sample project
Agora provides an open-source sample project on GitHub. You can view the source code on Github or download the project to try it out.
Implementation
Before using the raw data function, ensure that you have implemented the basic real-time communication functions in your project. See Start a Video Call or Start Interactive Live Video Streaming for details.
IVideoFrameObserver
class to capture and modify raw video data. Therefore, you must use Java to call Agora's C++ interface via the JNI (Java Native Interface). Since the RTC Java SDK encapsulates the RTC C++ SDK, you can include the .h
file in the SDK to directly call the C++ methods.Follow these steps to implement the raw video data function in your project:
- Use the JNI and C++ interface files to generate a shared library in the project, and use Java to call the raw video data interface of the Agora C++ SDK.
- Before joining a channel, call the
registerVideoFrameObserver
method to register a video observer, and implement anIVideoFrameObserver
class in this method. - After you successfully register the observer, the SDK captures every video frame and sends the captured raw video data via the
onCaptureVideoFrame
,onPreEncodeVideoFrame
, oronRenderVideoFrame
callbacks. - Process the captured raw video data according to your needs. Then, you can send it to the SDK via the callbacks mentioned in step 3.
Call the Agora C++ API in a Java project
The following diagram shows the basic flow of calling the Agora C++ API in a Java project:
- The Java project loads the
.so
library built from the C++ interface file (.cpp
file) via the Java interface file. - The Java interface file generates a
.h
file with thejavac -h -jni
command. The C++ interface file should include this file. - The C++ interface file calls C++ methods of the
.so
library in the Agora Android SDK by including the Agora Android SDK header file.
API call sequence
The following diagram shows how to implement the raw video data function in your project:
registerVideoFrameObserver
, onCaptureVideoFrame
, onPreEncodeVideoFrame
, and onRenderVideoFrame
are all C++ methods and callbacks.Sample code
Create a JNI interface
Create a Java interface file and a C++ interface file separately via the JNI interface. Make sure to build the C++ interface file as a .so
library.
- Create a Java interface file to call the C++ API. The interface file should declare the corresponding Java methods for calling C++. Refer to the
MediaPreProcessing.java
file in the sample project for the implementation.
- Run the following command to generate a
.h
file from the Java interface file:
- Create a C++ interface file to include the methods to be called from Java. The C++ interface file exports the corresponding methods from the C++ SDK based on the generated
.h
file. Refer to theio_agora_advancedvideo_rawdata_MediaPreProcessing.cpp
file in the sample project for the implementation.
- Build the C++ interface file via the NDK to generate a
.so
library. Use theSystem.loadLibrary()
method to load the generated.so
library in the Java interface file. See the following CMake example:
Implement the raw video data function in a Java project
- Implement an interface that maps to the C++ methods in a Java interface file.
- Call the
setCallback
method. ThesetCallback
method calls theregisterVideoFrameObserver
C++ method via JNI to register a video frame observer.
- Implement the
onCaptureVideoFrame
,onRenderVideoFrame
, andonPreEncodeVideoFrame
callbacks. Get the video frame from the callbacks, and process the video frame.
API reference
See also
Refer to Raw Audio Data if you want to implement the raw audio data function in your project.