Docs / iOS / ZEGOCLOUD

ZEGOCLOUD on iOS

Beautify the published camera of a ZEGOCLOUD Express 3.x call with custom video processing on CVPixelBuffer frames.

Demo app: facevity-ios-demo on GitHub

With custom video processing, ZEGO hands you each captured CVPixelBuffer and waits for the processed buffer back. Facevity processes it in place and you return the same buffer.

1. Requirements

  • Facevity for iOS set up as in the quick start
  • ZEGOCLOUD Express SDK 3.x (pod 'ZegoExpressEngine')
  • Licence feature beauty.basic

2. The handler

import CoreMedia
import ZegoExpressEngine
import Facevity

final class FacevityZegoVideoProcessor: NSObject, ZegoCustomVideoProcessHandler {
    let facevity: Facevity
    /// Raw camera buffers arrive in the capture orientation ZEGO configured; 0 when they are upright.
    var rotation = 0

    init(options: FacevityOptions = FacevityOptions()) throws {
        facevity = try Facevity.initialize(options)
        facevity.setParams(.NATURAL)
        super.init()
    }

    func attach(to engine: ZegoExpressEngine, channel: ZegoPublishChannel = .main) {
        let config = ZegoCustomVideoProcessConfig()
        config.bufferType = .cvPixelBuffer
        engine.enableCustomVideoProcessing(true, config: config, channel: channel)
        engine.setCustomVideoProcessHandler(self)
    }

    func onCapturedUnprocessedCVPixelBuffer(_ buffer: CVPixelBuffer, timestamp: CMTime, channel: ZegoPublishChannel) {
        if facevity.isActive {
            _ = facevity.processPixelBuffer(buffer, rotation: rotation,
                                            timestampNs: Int64(CMTimeGetSeconds(timestamp) * 1e9))
        }
        ZegoExpressEngine.shared().sendCustomVideoProcessedCVPixelBuffer(buffer, timestamp: timestamp, channel: channel)
    }
}

3. Attach it before preview and publishing

let profile = ZegoEngineProfile()
profile.appID = appID
profile.appSign = appSign
profile.scenario = .default
ZegoExpressEngine.createEngine(with: profile, eventHandler: self)

let beauty = try FacevityZegoVideoProcessor()
beauty.attach(to: ZegoExpressEngine.shared())            // BEFORE startPreview / startPublishingStream

ZegoExpressEngine.shared().startPreview(ZegoCanvas(view: previewView))
ZegoExpressEngine.shared().startPublishingStream(streamID)

Preview and publish both use the processed buffer.

4. Tear down

ZegoExpressEngine.shared().stopPublishingStream()
ZegoExpressEngine.shared().stopPreview()
ZegoExpressEngine.shared().enableCustomVideoProcessing(false, config: nil, channel: .main)
beauty.facevity.release()

Next: LiveKit on iOS · all platforms

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