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

Revamp broadcast IPC #565

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
eed85f9
Create value type wrapper around `CFHTTPMessage`
ladvoc Jan 22, 2025
fd592db
Create `BroadcastImageCodec` to isolate encoding functionality
ladvoc Jan 22, 2025
ebaee5c
Create `BroadcastSampleEncoder`
ladvoc Jan 22, 2025
e223f5d
Decouple encoding from transport on broadcast extension side
ladvoc Jan 22, 2025
85f1b3d
Add support for decoding in `BroadcastImageCodec`
ladvoc Jan 22, 2025
68f3d45
Move single constant into type definition
ladvoc Jan 22, 2025
002f835
Define `BroadcastSample` enum and implement its decoder
ladvoc Jan 23, 2025
d8c3f9a
Implement `HTTPMessageReader`
ladvoc Jan 23, 2025
fd4d65d
Integrate new types
ladvoc Jan 23, 2025
e2beb05
Rename file
ladvoc Jan 23, 2025
8deb4df
Fully decouple networking component
ladvoc Jan 24, 2025
7b25d67
Rename methods
ladvoc Jan 24, 2025
eb1fc39
Create and integrate `BroadcastSampleReceiver`
ladvoc Jan 24, 2025
fddf1ed
Create `SocketPath` struct
ladvoc Jan 28, 2025
caa6923
Create `IPCChannel` abstraction
ladvoc Jan 29, 2025
c8ac5ba
Overhaul broadcast IPC
ladvoc Jan 29, 2025
b093eea
Relocate file
ladvoc Jan 29, 2025
e02d487
Merge remote-tracking branch 'upstream/main' into broadcast-ipc
ladvoc Jan 29, 2025
1b88945
Remove print
ladvoc Jan 29, 2025
e8a1036
Fix closure bugs
ladvoc Jan 29, 2025
e32ae5d
Refactor test
ladvoc Jan 29, 2025
2d7a8b8
Only compile broadcast components for iOS
ladvoc Jan 31, 2025
179cf31
Fix compilation errors with older Swift versions
ladvoc Jan 31, 2025
39224a3
Implement rate control
ladvoc Feb 3, 2025
a779853
Merge remote-tracking branch 'upstream/main' into broadcast-ipc
ladvoc Feb 3, 2025
7c4f8ea
Run SwiftFormat
ladvoc Feb 3, 2025
3205da2
Merge remote-tracking branch 'upstream/main' into broadcast-ipc
ladvoc Feb 3, 2025
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
7 changes: 4 additions & 3 deletions Sources/LiveKit/Broadcast/BroadcastBundleInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class BroadcastBundleInfo {
static var screenSharingExtension: String?

/// Path to the socket file used for interprocess communication.
static var socketPath: String? {
static var socketPath: SocketPath? {
guard let groupIdentifier else { return nil }
return Self.socketPath(for: groupIdentifier)
}
Expand All @@ -41,11 +41,12 @@ final class BroadcastBundleInfo {

private static let socketFileDescriptor = "rtc_SSFD"

private static func socketPath(for groupIdentifier: String) -> String? {
private static func socketPath(for groupIdentifier: String) -> SocketPath? {
guard let sharedContainer = FileManager.default
.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)
else { return nil }
return sharedContainer.appendingPathComponent(Self.socketFileDescriptor).path
let path = sharedContainer.appendingPathComponent(Self.socketFileDescriptor).path
return SocketPath(path)
}
}

Expand Down
45 changes: 24 additions & 21 deletions Sources/LiveKit/Broadcast/BroadcastScreenCapturer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,13 @@ internal import LiveKitWebRTC
#endif

class BroadcastScreenCapturer: BufferCapturer {
var frameReader: SocketConnectionFrameReader?
private var receiver: BroadcastReceiver?

override func startCapture() async throws -> Bool {
let didStart = try await super.startCapture()

guard didStart else { return false }

guard let socketPath = BroadcastBundleInfo.socketPath else {
logger.error("Bundle settings improperly configured for screen capture")
return false
}

let bounds = await UIScreen.main.bounds
let width = bounds.size.width
let height = bounds.size.height
Expand All @@ -53,22 +48,32 @@ class BroadcastScreenCapturer: BufferCapturer {
.toEncodeSafeDimensions()

set(dimensions: targetDimensions)
return createReceiver()
}

let frameReader = SocketConnectionFrameReader()
guard let socketConnection = BroadcastServerSocketConnection(filePath: socketPath, streamDelegate: frameReader)
else { return false }
frameReader.didCapture = { pixelBuffer, rotation in
self.capture(pixelBuffer, rotation: rotation.toLKType())
private func createReceiver() -> Bool {
guard let socketPath = BroadcastBundleInfo.socketPath else {
logger.error("Bundle settings improperly configured for screen capture")
return false
}
frameReader.didEnd = { [weak self] in
guard let self else { return }
Task {
try await self.stopCapture()
Task { [weak self] in
do {
let receiver = try await BroadcastReceiver(socketPath: socketPath)
logger.debug("Broadcast receiver connected")
self?.receiver = receiver

for try await sample in receiver.incomingSamples {
switch sample {
case let .image(imageBuffer, rotation):
self?.capture(imageBuffer, rotation: rotation)
}
}
logger.debug("Broadcast receiver closed")
} catch {
logger.error("Broadcast receiver error: \(error)")
}
_ = try? await self?.stopCapture()
}
frameReader.startCapture(with: socketConnection)
self.frameReader = frameReader

return true
}

Expand All @@ -77,9 +82,7 @@ class BroadcastScreenCapturer: BufferCapturer {

// Already stopped
guard didStop else { return false }

frameReader?.stopCapture()
frameReader = nil
receiver?.close()
return true
}
}
Expand Down
200 changes: 0 additions & 200 deletions Sources/LiveKit/Broadcast/BroadcastServerSocketConnection.swift

This file was deleted.

25 changes: 25 additions & 0 deletions Sources/LiveKit/Broadcast/IPC/BroadcastIPCHeader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2025 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.
*/

#if os(iOS)

/// Message header for communication between uploader and receiver.
enum BroadcastIPCHeader: Codable {
/// Image sample sent by uploader.
case image(BroadcastImageCodec.Metadata, VideoRotation)
}

#endif
Loading
Loading