Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom video frame processing #530

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Sources/LiveKit/Protocols/VideoProcessor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

@objc
public protocol VideoProcessor {
func process(frame: VideoFrame) -> VideoFrame?
}
36 changes: 28 additions & 8 deletions Sources/LiveKit/Track/Capturers/VideoCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,22 @@ public class VideoCapturer: NSObject, Loggable, VideoCapturerProtocol {

let dimensionsCompleter = AsyncCompleter<Dimensions>(label: "Dimensions", defaultTimeout: .defaultCaptureStart)

struct State: Equatable {
struct State {
// Counts calls to start/stopCapturer so multiple Tracks can use the same VideoCapturer.
var startStopCounter: Int = 0
var dimensions: Dimensions? = nil
weak var processor: VideoProcessor? = nil
}

var _state = StateSync(State())

public var dimensions: Dimensions? { _state.dimensions }

public weak var processor: VideoProcessor? {
get { _state.processor }
set { _state.mutate { $0.processor = newValue } }
}

func set(dimensions newValue: Dimensions?) {
let didUpdate = _state.mutate {
let oldDimensions = $0.dimensions
Expand Down Expand Up @@ -223,17 +229,31 @@ extension VideoCapturer {
device: AVCaptureDevice?,
options: VideoCaptureOptions)
{
var rtcFrame: LKRTCVideoFrame = frame
guard var lkFrame: VideoFrame = frame.toLKType() else {
log("Failed to convert a RTCVideoFrame to VideoFrame.", .error)
return
}

// Apply processing if we have a processor attached.
if let processor = _state.processor {
guard let processedFrame = processor.process(frame: lkFrame) else {
log("VideoProcessor didn't return a frame, skipping frame.", .warning)
return
}
lkFrame = processedFrame
rtcFrame = processedFrame.toRTCType()
}

// Resolve real dimensions (apply frame rotation)
set(dimensions: Dimensions(width: frame.width, height: frame.height).apply(rotation: frame.rotation))
set(dimensions: Dimensions(width: rtcFrame.width, height: rtcFrame.height).apply(rotation: rtcFrame.rotation))

delegate?.capturer(capturer, didCapture: frame)
delegate?.capturer(capturer, didCapture: rtcFrame)

if rendererDelegates.isDelegatesNotEmpty {
if let lkVideoFrame = frame.toLKType() {
rendererDelegates.notify { renderer in
renderer.render?(frame: lkVideoFrame)
renderer.render?(frame: lkVideoFrame, captureDevice: device, captureOptions: options)
}
rendererDelegates.notify { renderer in
renderer.render?(frame: lkFrame)
renderer.render?(frame: lkFrame, captureDevice: device, captureOptions: options)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/LiveKit/Types/VideoFrame.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class CVPixelVideoBuffer: VideoBuffer, RTCCompatibleVideoBuffer {
_rtcType.pixelBuffer
}

public init(pixelBuffer: CVPixelBuffer) {
_rtcType = LKRTCCVPixelBuffer(pixelBuffer: pixelBuffer)
}

// Internal only.
init(rtcCVPixelBuffer: LKRTCCVPixelBuffer) {
_rtcType = rtcCVPixelBuffer
}
Expand Down
Loading