Docs / Android / WebRTC

WebRTC on Android

Beautify a WebRTC camera track with a VideoProcessor on the video source. Camera frames stay on the GPU as OES textures.

Demo app: facevity-android-demo on GitHub

WebRTC’s VideoSource accepts a VideoProcessor that sees every captured frame before the encoder. Camera frames arrive as OES texture buffers on the SurfaceTextureHelper thread, whose EGL context is current, so Facevity renders right there. Other frames go through I420.

1. Requirements

  • Facevity for Android 0.3.3 set up as in the quick start
  • org.webrtc (any recent build with VideoProcessor)
  • Licence feature beauty.basic; buffer.path only for non-texture frames (for example screen sharing)

2. The processor

class WebRtcFacevity(private val fv: Facevity, private val helper: SurfaceTextureHelper) : VideoProcessor {
    private var sink: VideoSink? = null
    private var packed: ByteArray? = null
    private val yuv = YuvConverter()

    override fun setSink(sink: VideoSink?) { this.sink = sink }
    override fun onCapturerStarted(success: Boolean) = Unit
    override fun onCapturerStopped() { helper.handler.post { fv.onGlContextDestroyed() } }

    override fun onFrameCaptured(frame: VideoFrame) {
        val out = sink ?: return
        if (!fv.isActive) { out.onFrame(frame); return }
        val buffer = frame.buffer
        if (buffer is TextureBufferImpl && buffer.type == VideoFrame.TextureBuffer.Type.OES) {
            val m = RendererCommon.convertMatrixFromAndroidGraphicsMatrix(buffer.transformMatrix)
            val tex = fv.processTexture(TextureFrame(buffer.textureId, true, buffer.width, buffer.height,
                                                     frame.rotation, false, frame.timestampNs, m))
            if (tex == buffer.textureId) { out.onFrame(frame); return }
            val processed = TextureBufferImpl(buffer.width, buffer.height, VideoFrame.TextureBuffer.Type.RGB,
                tex, android.graphics.Matrix(), helper.handler, yuv, null)
            out.onFrame(VideoFrame(processed, frame.rotation, frame.timestampNs))
            return
        }
        // CPU frames (e.g. a capturer that delivers I420)
        val i420 = buffer.toI420() ?: run { out.onFrame(frame); return }
        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
            if (fv.processBuffer(BufferFrame(p, PixelFormat.I420, w, h, frame.rotation, false, frame.timestampNs))) {
                val next = JavaI420Buffer.allocate(w, h)
                YuvPlanes.unpackI420(p, next.dataY, next.strideY, next.dataU, next.strideU,
                                     next.dataV, next.strideV, w, h)
                out.onFrame(VideoFrame(next, frame.rotation, frame.timestampNs))
                next.release()
            } else {
                out.onFrame(frame)
            }
        } finally {
            i420.release()
        }
    }
}

3. Attach it to the video source

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

val helper = SurfaceTextureHelper.create("CaptureThread", eglBase.eglBaseContext)
val source = factory.createVideoSource(/* isScreencast = */ false)
source.setVideoProcessor(WebRtcFacevity(fv, helper))
capturer.initialize(helper, context, source.capturerObserver)
capturer.startCapture(1280, 720, 30)

val track = factory.createVideoTrack("camera", source)
peerConnection.addTrack(track)

4. Tear down

capturer.stopCapture()          // onCapturerStopped frees Facevity's GL objects on the helper thread
source.setVideoProcessor(null)
fv.release()

Notes

  • Keep frame.rotation and frame.timestampNs as they are; the renderer and the encoder keep applying the rotation.
  • The texture returned by processTexture is owned by the engine and stays valid until the call after next.
  • The same class works for LiveKit with LiveKit’s repackaged livekit.org.webrtc imports.

Next: all platforms · API reference

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