Docs / Android / Agora

Agora on Android

Beautify the published camera of an Agora RTC 4.x call with an IVideoFrameObserver at the post-capturer position.

Demo app: facevity-android-demo on GitHub

Agora encodes what a video frame observer leaves in the frame, so a read-write observer at the post-capturer position is all it takes: remote users receive the beautified video and the local preview shows the same frame.

1. Requirements

  • Facevity for Android 0.3.3 set up as in the quick start
  • Agora RTC SDK 4.x (io.agora.rtc:full-sdk or the lite variant)
  • Licence features beauty.basic and buffer.path (I420 frames)

2. The observer

Register an IVideoFrameObserver at POSITION_POST_CAPTURER with I420 frames. Pack the planes, process them in place and write them back.

class AgoraFacevity(private val fv: Facevity) : IVideoFrameObserver {
    private var packed: ByteArray? = null

    override fun onCaptureVideoFrame(sourceType: Int, videoFrame: VideoFrame): Boolean {
        if (!fv.isActive) return true                       // nothing to do: skip the conversions
        val i420 = videoFrame.buffer.toI420()
        try {
            val w = i420.width; val h = i420.height
            val p = YuvPlanes.packI420(i420.dataY, i420.strideY, i420.dataU, i420.strideU,
                                       i420.dataV, i420.strideV, w, h, packed)
            packed = p
            val frame = BufferFrame(p, PixelFormat.I420, w, h, videoFrame.rotation,
                                    mirrored = false, timestampNs = videoFrame.timestampNs)
            if (fv.processBuffer(frame)) {                  // false = passed through (busy / budget / off)
                YuvPlanes.unpackI420(p, i420.dataY, i420.strideY, i420.dataU, i420.strideU,
                                     i420.dataV, i420.strideV, w, h)
                videoFrame.replaceBuffer(i420, videoFrame.rotation, videoFrame.timestampNs)
            }
        } finally {
            i420.release()
        }
        return true
    }

    override fun getVideoFrameProcessMode() = IVideoFrameObserver.PROCESS_MODE_READ_WRITE
    override fun getVideoFormatPreference() = IVideoFrameObserver.VIDEO_PIXEL_I420
    override fun getRotationApplied() = false    // rotation travels with the frame
    override fun getMirrorApplied() = false      // never mirror what is published
    override fun getObservedFramePosition() = IVideoFrameObserver.POSITION_POST_CAPTURER
    // onPreEncodeVideoFrame, onRenderVideoFrame, … return true (pass through)
}

3. Register it before joining

val fv = Facevity.initialize(context)
fv.setParams(BeautyParams.NATURAL)

engine.registerVideoFrameObserver(AgoraFacevity(fv))
engine.enableVideo()
engine.startPreview()
engine.joinChannel(token, channelName, 0, ChannelMediaOptions())

Beauty settings (setParams, setLipstick, setProp, …) apply to the call from the next frame.

4. Tear down

engine.registerVideoFrameObserver(null)
engine.leaveChannel()
fv.release()

Notes

  • Rotation. Agora reports the rotation per frame; pass it unchanged. The output keeps the input orientation.
  • Budget. processBuffer waits at most 75% of the frame interval. A frame that misses it is filled with the last retouched frame (up to 250 ms old), so the effect does not flicker in calls; see stats().framesRepeated.
  • Strided planes. YuvPlanes.packI420 and unpackI420 handle row strides, so any I420 buffer works.

Next: ZEGOCLOUD on Android · all platforms

Need help with an integration? Contact the Facevity team. We answer integration questions during trials.