Custom Capturer
LiveKit provides base classes and protocols for implementing custom video capturers. This is useful for advanced scenarios like AR capture, custom video sources, or specialized processing pipelines.BufferCapturer
TheBufferCapturer class allows you to capture video from CMSampleBuffer or CVPixelBuffer sources. This is the simplest way to integrate custom video sources.
Creating a Buffer Track
Capturing Frames
From CMSampleBuffer
From CVPixelBuffer
BufferCaptureOptions
Configure buffer capture behavior:| Property | Type | Default | Description |
|---|---|---|---|
dimensions | Dimensions | .h1080_169 | Target dimensions |
fps | Int | 15 | Expected frame rate |
Supported Pixel Formats
The SDK supports the following pixel formats:kCVPixelFormatType_420YpCbCr8BiPlanarVideoRangekCVPixelFormatType_420YpCbCr8BiPlanarFullRangekCVPixelFormatType_32BGRAkCVPixelFormatType_32ARGB
Creating a Custom VideoCapturer
For more advanced scenarios, you can subclassVideoCapturer to create fully custom capture implementations.
Subclassing VideoCapturer
Creating Custom Options
Factory Method
VideoCapturer Base Class
TheVideoCapturer base class provides:
Properties
dimensions: Dimensions?- Current capture dimensionscaptureState: CapturerState- Current state (.stoppedor.started)processor: VideoProcessor?- Optional video processor for frame processingdelegates: MulticastDelegate<VideoCapturerDelegate>- Delegate collection
Methods
startCapture() async throws -> Bool- Start capturing (callsuper.startCapture()first)stopCapture() async throws -> Bool- Stop capturing (callsuper.stopCapture()first)restartCapture() async throws -> Bool- Restart capturing
Capture Methods
Video Processing
All capturers support optional video processing via theVideoProcessor protocol:
ReplayKit Integration
UseBufferCapturer for ReplayKit screen capture in broadcast extensions:
Timestamp Generation
Create accurate timestamps for captured frames:Best Practices
Dimension Requirements
- Ensure dimensions are even numbers (required for video encoding)
- The SDK validates with
dimensions.isEncodeSafe - Invalid dimensions will be rejected with a warning
Frame Publishing
At least one frame must be captured before publishing the track:Thread Safety
TheVideoCapturer base class is @unchecked Sendable and uses internal synchronization. When capturing frames:
- Capture methods can be called from any thread
- Frame processing happens on a dedicated serial queue
- If processing is busy, frames are dropped with a warning
Resource Management
Always balancestartCapture() and stopCapture() calls:
start calls require matching stop calls.
See Also
- Camera Capturer - Camera video capture
- Screen Capturer - Screen capture
- LocalVideoTrack - Publishing custom video
- VideoProcessor - Video frame processing