Docs / iOS / WebRTC

WebRTC on iOS

Beautify a WebRTC camera track by placing a capturer delegate between RTCCameraVideoCapturer and the video source.

Demo app: facevity-ios-demo on GitHub

RTCCameraVideoCapturer delivers each frame to its delegate, normally the RTCVideoSource. Put a small wrapper in between: it processes the frame’s RTCCVPixelBuffer in place and forwards the frame to the source.

1. Requirements

  • Facevity for iOS set up as in the quick start
  • The WebRTC framework (any recent build with RTCCameraVideoCapturer)
  • Licence feature beauty.basic

2. The capturer delegate

import WebRTC
import Facevity

final class FacevityCapturerDelegate: NSObject, RTCVideoCapturerDelegate {
    let facevity: Facevity
    private weak var downstream: RTCVideoCapturerDelegate?

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

    func capturer(_ capturer: RTCVideoCapturer, didCapture frame: RTCVideoFrame) {
        if facevity.isActive, let cv = frame.buffer as? RTCCVPixelBuffer {
            // RTCVideoFrame.rotation is the clockwise rotation to upright
            _ = facevity.processPixelBuffer(cv.pixelBuffer, rotation: frame.rotation.rawValue,
                                            timestampNs: frame.timeStampNs)
        }
        downstream?.capturer(capturer, didCapture: frame)
    }
}

3. Wire it up

let source = factory.videoSource()
let beauty = try FacevityCapturerDelegate(downstream: source)   // keep a strong reference
let capturer = RTCCameraVideoCapturer(delegate: beauty)
capturer.startCapture(with: frontCamera, format: format, fps: 30)

let track = factory.videoTrack(with: source, trackId: "camera")
peerConnection.add(track, streamIds: ["stream"])

The local renderer and the encoder both read from the source, so both get the processed frame.

4. Tear down

capturer.stopCapture()
beauty.facevity.release()

Next: all platforms · API reference

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