Skip to content

Commit

Permalink
IPCCapturer
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Nov 23, 2021
1 parent 76567ea commit 6ac4a7a
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Sources/LiveKit/track/capturers/IPCCapturer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import Foundation
import WebRTC
import Promises

/// Experimental capturer that uses inter-process-communication to receive
/// video buffers
class IPCCapturer: VideoCapturer {

private let capturer = RTCVideoCapturer()
private let ipcServer = IPCServer()
private let ipcName: String

init(delegate: RTCVideoCapturerDelegate, ipcName: String) {
self.ipcName = ipcName
super.init(delegate: delegate)

ipcServer.onReceivedData = { _, messageId, data in

guard let message = try? IPCMessage(serializedData: data) else {
logger.warning("Failed to decode ipc message")
return
}

if case let .buffer(bufferMessage) = message.type,
case let .video(videoMessage) = bufferMessage.type {

// restore pixel buffer from data
let pixelBuffer = CVPixelBuffer.from(bufferMessage.buffer,
width: Int(videoMessage.width),
height: Int(videoMessage.height),
pixelFormat: videoMessage.format)

// TODO: handle rotation

delegate.capturer(self.capturer,
didCapture: pixelBuffer,
timeStampNs: bufferMessage.timestampNs,
rotation: ._0)
}
}
}

override func startCapture() -> Promise<Void> {
super.startCapture().then {
// start listening for ipc messages
self.ipcServer.listen(self.ipcName)
}
}

override func stopCapture() -> Promise<Void> {
super.stopCapture().then {
// stop listening for ipc messages
self.ipcServer.close()
}
}
}

extension LocalVideoTrack {

public static func createIPCTrack(name: String = Track.screenShareName,
ipcName: String,
source: VideoTrack.Source = .screenShareVideo) -> LocalVideoTrack {
let videoSource = Engine.factory.videoSource()
let capturer = IPCCapturer(delegate: videoSource, ipcName: ipcName)
return LocalVideoTrack(
capturer: capturer,
videoSource: videoSource,
name: name,
source: source
)
}
}

0 comments on commit 6ac4a7a

Please sign in to comment.