Use LiveKit's Rust SDK on Linux while continue using Swift SDK on Mac (#21550)
Similar to #20826 but keeps the Swift implementation. There were quite a few changes in the `call` crate, and so that code now has two variants. Closes #13714 Release Notes: - Added preliminary Linux support for voice chat and viewing screenshares. --------- Co-authored-by: Kirill Bulatov <mail4score@gmail.com> Co-authored-by: Kirill Bulatov <kirill@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
parent
0511768b22
commit
6a4cd53fd8
91 changed files with 7187 additions and 1028 deletions
2
crates/livekit_client_macos/.cargo/config.toml
Normal file
2
crates/livekit_client_macos/.cargo/config.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
[livekit_client_test]
|
||||
rustflags = ["-C", "link-args=-ObjC"]
|
67
crates/livekit_client_macos/Cargo.toml
Normal file
67
crates/livekit_client_macos/Cargo.toml
Normal file
|
@ -0,0 +1,67 @@
|
|||
[package]
|
||||
name = "livekit_client_macos"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "Bindings to LiveKit Swift client SDK"
|
||||
publish = false
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[lib]
|
||||
path = "src/livekit_client.rs"
|
||||
doctest = false
|
||||
|
||||
[[example]]
|
||||
name = "test_app"
|
||||
|
||||
[features]
|
||||
no-webrtc = []
|
||||
test-support = [
|
||||
"async-trait",
|
||||
"collections/test-support",
|
||||
"gpui/test-support",
|
||||
"livekit_server",
|
||||
"nanoid",
|
||||
]
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
async-broadcast = "0.7"
|
||||
async-trait = { workspace = true, optional = true }
|
||||
collections = { workspace = true, optional = true }
|
||||
futures.workspace = true
|
||||
gpui = { workspace = true, optional = true }
|
||||
livekit_server = { workspace = true, optional = true }
|
||||
log.workspace = true
|
||||
media.workspace = true
|
||||
nanoid = { workspace = true, optional = true}
|
||||
parking_lot.workspace = true
|
||||
postage.workspace = true
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
core-foundation.workspace = true
|
||||
|
||||
[target.'cfg(all(not(target_os = "macos")))'.dependencies]
|
||||
async-trait = { workspace = true }
|
||||
collections = { workspace = true }
|
||||
gpui = { workspace = true }
|
||||
livekit_server.workspace = true
|
||||
nanoid.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
async-trait.workspace = true
|
||||
collections = { workspace = true, features = ["test-support"] }
|
||||
gpui = { workspace = true, features = ["test-support"] }
|
||||
livekit_server.workspace = true
|
||||
nanoid.workspace = true
|
||||
sha2.workspace = true
|
||||
simplelog.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
[package.metadata.cargo-machete]
|
||||
ignored = ["serde_json"]
|
1
crates/livekit_client_macos/LICENSE-GPL
Symbolic link
1
crates/livekit_client_macos/LICENSE-GPL
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../LICENSE-GPL
|
52
crates/livekit_client_macos/LiveKitBridge/Package.resolved
Normal file
52
crates/livekit_client_macos/LiveKitBridge/Package.resolved
Normal file
|
@ -0,0 +1,52 @@
|
|||
{
|
||||
"object": {
|
||||
"pins": [
|
||||
{
|
||||
"package": "LiveKit",
|
||||
"repositoryURL": "https://github.com/livekit/client-sdk-swift.git",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "7331b813a5ab8a95cfb81fb2b4ed10519428b9ff",
|
||||
"version": "1.0.12"
|
||||
}
|
||||
},
|
||||
{
|
||||
"package": "Promises",
|
||||
"repositoryURL": "https://github.com/google/promises.git",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "ec957ccddbcc710ccc64c9dcbd4c7006fcf8b73a",
|
||||
"version": "2.2.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"package": "WebRTC",
|
||||
"repositoryURL": "https://github.com/webrtc-sdk/Specs.git",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "2f6bab30c8df0fe59ab3e58bc99097f757f85f65",
|
||||
"version": "104.5112.17"
|
||||
}
|
||||
},
|
||||
{
|
||||
"package": "swift-log",
|
||||
"repositoryURL": "https://github.com/apple/swift-log.git",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "32e8d724467f8fe623624570367e3d50c5638e46",
|
||||
"version": "1.5.2"
|
||||
}
|
||||
},
|
||||
{
|
||||
"package": "SwiftProtobuf",
|
||||
"repositoryURL": "https://github.com/apple/swift-protobuf.git",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "ce20dc083ee485524b802669890291c0d8090170",
|
||||
"version": "1.22.1"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"version": 1
|
||||
}
|
27
crates/livekit_client_macos/LiveKitBridge/Package.swift
Normal file
27
crates/livekit_client_macos/LiveKitBridge/Package.swift
Normal file
|
@ -0,0 +1,27 @@
|
|||
// swift-tools-version: 5.5
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "LiveKitBridge",
|
||||
platforms: [
|
||||
.macOS(.v10_15)
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
||||
.library(
|
||||
name: "LiveKitBridge",
|
||||
type: .static,
|
||||
targets: ["LiveKitBridge"]),
|
||||
],
|
||||
dependencies: [
|
||||
.package(url: "https://github.com/livekit/client-sdk-swift.git", .exact("1.0.12")),
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
||||
.target(
|
||||
name: "LiveKitBridge",
|
||||
dependencies: [.product(name: "LiveKit", package: "client-sdk-swift")]),
|
||||
]
|
||||
)
|
3
crates/livekit_client_macos/LiveKitBridge/README.md
Normal file
3
crates/livekit_client_macos/LiveKitBridge/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# LiveKitBridge
|
||||
|
||||
A description of this package.
|
|
@ -0,0 +1,383 @@
|
|||
import Foundation
|
||||
import LiveKit
|
||||
import WebRTC
|
||||
import ScreenCaptureKit
|
||||
|
||||
class LKRoomDelegate: RoomDelegate {
|
||||
var data: UnsafeRawPointer
|
||||
var onDidDisconnect: @convention(c) (UnsafeRawPointer) -> Void
|
||||
var onDidSubscribeToRemoteAudioTrack: @convention(c) (UnsafeRawPointer, CFString, CFString, UnsafeRawPointer, UnsafeRawPointer) -> Void
|
||||
var onDidUnsubscribeFromRemoteAudioTrack: @convention(c) (UnsafeRawPointer, CFString, CFString) -> Void
|
||||
var onMuteChangedFromRemoteAudioTrack: @convention(c) (UnsafeRawPointer, CFString, Bool) -> Void
|
||||
var onActiveSpeakersChanged: @convention(c) (UnsafeRawPointer, CFArray) -> Void
|
||||
var onDidSubscribeToRemoteVideoTrack: @convention(c) (UnsafeRawPointer, CFString, CFString, UnsafeRawPointer) -> Void
|
||||
var onDidUnsubscribeFromRemoteVideoTrack: @convention(c) (UnsafeRawPointer, CFString, CFString) -> Void
|
||||
var onDidPublishOrUnpublishLocalAudioTrack: @convention(c) (UnsafeRawPointer, UnsafeRawPointer, Bool) -> Void
|
||||
var onDidPublishOrUnpublishLocalVideoTrack: @convention(c) (UnsafeRawPointer, UnsafeRawPointer, Bool) -> Void
|
||||
|
||||
init(
|
||||
data: UnsafeRawPointer,
|
||||
onDidDisconnect: @escaping @convention(c) (UnsafeRawPointer) -> Void,
|
||||
onDidSubscribeToRemoteAudioTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString, UnsafeRawPointer, UnsafeRawPointer) -> Void,
|
||||
onDidUnsubscribeFromRemoteAudioTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString) -> Void,
|
||||
onMuteChangedFromRemoteAudioTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, Bool) -> Void,
|
||||
onActiveSpeakersChanged: @convention(c) (UnsafeRawPointer, CFArray) -> Void,
|
||||
onDidSubscribeToRemoteVideoTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString, UnsafeRawPointer) -> Void,
|
||||
onDidUnsubscribeFromRemoteVideoTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString) -> Void,
|
||||
onDidPublishOrUnpublishLocalAudioTrack: @escaping @convention(c) (UnsafeRawPointer, UnsafeRawPointer, Bool) -> Void,
|
||||
onDidPublishOrUnpublishLocalVideoTrack: @escaping @convention(c) (UnsafeRawPointer, UnsafeRawPointer, Bool) -> Void
|
||||
)
|
||||
{
|
||||
self.data = data
|
||||
self.onDidDisconnect = onDidDisconnect
|
||||
self.onDidSubscribeToRemoteAudioTrack = onDidSubscribeToRemoteAudioTrack
|
||||
self.onDidUnsubscribeFromRemoteAudioTrack = onDidUnsubscribeFromRemoteAudioTrack
|
||||
self.onDidSubscribeToRemoteVideoTrack = onDidSubscribeToRemoteVideoTrack
|
||||
self.onDidUnsubscribeFromRemoteVideoTrack = onDidUnsubscribeFromRemoteVideoTrack
|
||||
self.onMuteChangedFromRemoteAudioTrack = onMuteChangedFromRemoteAudioTrack
|
||||
self.onActiveSpeakersChanged = onActiveSpeakersChanged
|
||||
self.onDidPublishOrUnpublishLocalAudioTrack = onDidPublishOrUnpublishLocalAudioTrack
|
||||
self.onDidPublishOrUnpublishLocalVideoTrack = onDidPublishOrUnpublishLocalVideoTrack
|
||||
}
|
||||
|
||||
func room(_ room: Room, didUpdate connectionState: ConnectionState, oldValue: ConnectionState) {
|
||||
if connectionState.isDisconnected {
|
||||
self.onDidDisconnect(self.data)
|
||||
}
|
||||
}
|
||||
|
||||
func room(_ room: Room, participant: RemoteParticipant, didSubscribe publication: RemoteTrackPublication, track: Track) {
|
||||
if track.kind == .video {
|
||||
self.onDidSubscribeToRemoteVideoTrack(self.data, participant.identity as CFString, track.sid! as CFString, Unmanaged.passUnretained(track).toOpaque())
|
||||
} else if track.kind == .audio {
|
||||
self.onDidSubscribeToRemoteAudioTrack(self.data, participant.identity as CFString, track.sid! as CFString, Unmanaged.passUnretained(track).toOpaque(), Unmanaged.passUnretained(publication).toOpaque())
|
||||
}
|
||||
}
|
||||
|
||||
func room(_ room: Room, participant: Participant, didUpdate publication: TrackPublication, muted: Bool) {
|
||||
if publication.kind == .audio {
|
||||
self.onMuteChangedFromRemoteAudioTrack(self.data, publication.sid as CFString, muted)
|
||||
}
|
||||
}
|
||||
|
||||
func room(_ room: Room, didUpdate speakers: [Participant]) {
|
||||
guard let speaker_ids = speakers.compactMap({ $0.identity as CFString }) as CFArray? else { return }
|
||||
self.onActiveSpeakersChanged(self.data, speaker_ids)
|
||||
}
|
||||
|
||||
func room(_ room: Room, participant: RemoteParticipant, didUnsubscribe publication: RemoteTrackPublication, track: Track) {
|
||||
if track.kind == .video {
|
||||
self.onDidUnsubscribeFromRemoteVideoTrack(self.data, participant.identity as CFString, track.sid! as CFString)
|
||||
} else if track.kind == .audio {
|
||||
self.onDidUnsubscribeFromRemoteAudioTrack(self.data, participant.identity as CFString, track.sid! as CFString)
|
||||
}
|
||||
}
|
||||
|
||||
func room(_ room: Room, localParticipant: LocalParticipant, didPublish publication: LocalTrackPublication) {
|
||||
if publication.kind == .video {
|
||||
self.onDidPublishOrUnpublishLocalVideoTrack(self.data, Unmanaged.passUnretained(publication).toOpaque(), true)
|
||||
} else if publication.kind == .audio {
|
||||
self.onDidPublishOrUnpublishLocalAudioTrack(self.data, Unmanaged.passUnretained(publication).toOpaque(), true)
|
||||
}
|
||||
}
|
||||
|
||||
func room(_ room: Room, localParticipant: LocalParticipant, didUnpublish publication: LocalTrackPublication) {
|
||||
if publication.kind == .video {
|
||||
self.onDidPublishOrUnpublishLocalVideoTrack(self.data, Unmanaged.passUnretained(publication).toOpaque(), false)
|
||||
} else if publication.kind == .audio {
|
||||
self.onDidPublishOrUnpublishLocalAudioTrack(self.data, Unmanaged.passUnretained(publication).toOpaque(), false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LKVideoRenderer: NSObject, VideoRenderer {
|
||||
var data: UnsafeRawPointer
|
||||
var onFrame: @convention(c) (UnsafeRawPointer, CVPixelBuffer) -> Bool
|
||||
var onDrop: @convention(c) (UnsafeRawPointer) -> Void
|
||||
var adaptiveStreamIsEnabled: Bool = false
|
||||
var adaptiveStreamSize: CGSize = .zero
|
||||
weak var track: VideoTrack?
|
||||
|
||||
init(data: UnsafeRawPointer, onFrame: @escaping @convention(c) (UnsafeRawPointer, CVPixelBuffer) -> Bool, onDrop: @escaping @convention(c) (UnsafeRawPointer) -> Void) {
|
||||
self.data = data
|
||||
self.onFrame = onFrame
|
||||
self.onDrop = onDrop
|
||||
}
|
||||
|
||||
deinit {
|
||||
self.onDrop(self.data)
|
||||
}
|
||||
|
||||
func setSize(_ size: CGSize) {
|
||||
}
|
||||
|
||||
func renderFrame(_ frame: RTCVideoFrame?) {
|
||||
let buffer = frame?.buffer as? RTCCVPixelBuffer
|
||||
if let pixelBuffer = buffer?.pixelBuffer {
|
||||
if !self.onFrame(self.data, pixelBuffer) {
|
||||
DispatchQueue.main.async {
|
||||
self.track?.remove(videoRenderer: self)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomDelegateCreate")
|
||||
public func LKRoomDelegateCreate(
|
||||
data: UnsafeRawPointer,
|
||||
onDidDisconnect: @escaping @convention(c) (UnsafeRawPointer) -> Void,
|
||||
onDidSubscribeToRemoteAudioTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString, UnsafeRawPointer, UnsafeRawPointer) -> Void,
|
||||
onDidUnsubscribeFromRemoteAudioTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString) -> Void,
|
||||
onMuteChangedFromRemoteAudioTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, Bool) -> Void,
|
||||
onActiveSpeakerChanged: @escaping @convention(c) (UnsafeRawPointer, CFArray) -> Void,
|
||||
onDidSubscribeToRemoteVideoTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString, UnsafeRawPointer) -> Void,
|
||||
onDidUnsubscribeFromRemoteVideoTrack: @escaping @convention(c) (UnsafeRawPointer, CFString, CFString) -> Void,
|
||||
onDidPublishOrUnpublishLocalAudioTrack: @escaping @convention(c) (UnsafeRawPointer, UnsafeRawPointer, Bool) -> Void,
|
||||
onDidPublishOrUnpublishLocalVideoTrack: @escaping @convention(c) (UnsafeRawPointer, UnsafeRawPointer, Bool) -> Void
|
||||
) -> UnsafeMutableRawPointer {
|
||||
let delegate = LKRoomDelegate(
|
||||
data: data,
|
||||
onDidDisconnect: onDidDisconnect,
|
||||
onDidSubscribeToRemoteAudioTrack: onDidSubscribeToRemoteAudioTrack,
|
||||
onDidUnsubscribeFromRemoteAudioTrack: onDidUnsubscribeFromRemoteAudioTrack,
|
||||
onMuteChangedFromRemoteAudioTrack: onMuteChangedFromRemoteAudioTrack,
|
||||
onActiveSpeakersChanged: onActiveSpeakerChanged,
|
||||
onDidSubscribeToRemoteVideoTrack: onDidSubscribeToRemoteVideoTrack,
|
||||
onDidUnsubscribeFromRemoteVideoTrack: onDidUnsubscribeFromRemoteVideoTrack,
|
||||
onDidPublishOrUnpublishLocalAudioTrack: onDidPublishOrUnpublishLocalAudioTrack,
|
||||
onDidPublishOrUnpublishLocalVideoTrack: onDidPublishOrUnpublishLocalVideoTrack
|
||||
)
|
||||
return Unmanaged.passRetained(delegate).toOpaque()
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomCreate")
|
||||
public func LKRoomCreate(delegate: UnsafeRawPointer) -> UnsafeMutableRawPointer {
|
||||
let delegate = Unmanaged<LKRoomDelegate>.fromOpaque(delegate).takeUnretainedValue()
|
||||
return Unmanaged.passRetained(Room(delegate: delegate)).toOpaque()
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomConnect")
|
||||
public func LKRoomConnect(room: UnsafeRawPointer, url: CFString, token: CFString, callback: @escaping @convention(c) (UnsafeRawPointer, CFString?) -> Void, callback_data: UnsafeRawPointer) {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
|
||||
room.connect(url as String, token as String).then { _ in
|
||||
callback(callback_data, UnsafeRawPointer(nil) as! CFString?)
|
||||
}.catch { error in
|
||||
callback(callback_data, error.localizedDescription as CFString)
|
||||
}
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomDisconnect")
|
||||
public func LKRoomDisconnect(room: UnsafeRawPointer) {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
room.disconnect()
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomPublishVideoTrack")
|
||||
public func LKRoomPublishVideoTrack(room: UnsafeRawPointer, track: UnsafeRawPointer, callback: @escaping @convention(c) (UnsafeRawPointer, UnsafeMutableRawPointer?, CFString?) -> Void, callback_data: UnsafeRawPointer) {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
let track = Unmanaged<LocalVideoTrack>.fromOpaque(track).takeUnretainedValue()
|
||||
room.localParticipant?.publishVideoTrack(track: track).then { publication in
|
||||
callback(callback_data, Unmanaged.passRetained(publication).toOpaque(), nil)
|
||||
}.catch { error in
|
||||
callback(callback_data, nil, error.localizedDescription as CFString)
|
||||
}
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomPublishAudioTrack")
|
||||
public func LKRoomPublishAudioTrack(room: UnsafeRawPointer, track: UnsafeRawPointer, callback: @escaping @convention(c) (UnsafeRawPointer, UnsafeMutableRawPointer?, CFString?) -> Void, callback_data: UnsafeRawPointer) {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
let track = Unmanaged<LocalAudioTrack>.fromOpaque(track).takeUnretainedValue()
|
||||
room.localParticipant?.publishAudioTrack(track: track).then { publication in
|
||||
callback(callback_data, Unmanaged.passRetained(publication).toOpaque(), nil)
|
||||
}.catch { error in
|
||||
callback(callback_data, nil, error.localizedDescription as CFString)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@_cdecl("LKRoomUnpublishTrack")
|
||||
public func LKRoomUnpublishTrack(room: UnsafeRawPointer, publication: UnsafeRawPointer) {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
let publication = Unmanaged<LocalTrackPublication>.fromOpaque(publication).takeUnretainedValue()
|
||||
let _ = room.localParticipant?.unpublish(publication: publication)
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomAudioTracksForRemoteParticipant")
|
||||
public func LKRoomAudioTracksForRemoteParticipant(room: UnsafeRawPointer, participantId: CFString) -> CFArray? {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
|
||||
for (_, participant) in room.remoteParticipants {
|
||||
if participant.identity == participantId as String {
|
||||
return participant.audioTracks.compactMap { $0.track as? RemoteAudioTrack } as CFArray?
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomAudioTrackPublicationsForRemoteParticipant")
|
||||
public func LKRoomAudioTrackPublicationsForRemoteParticipant(room: UnsafeRawPointer, participantId: CFString) -> CFArray? {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
|
||||
for (_, participant) in room.remoteParticipants {
|
||||
if participant.identity == participantId as String {
|
||||
return participant.audioTracks.compactMap { $0 as? RemoteTrackPublication } as CFArray?
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@_cdecl("LKRoomVideoTracksForRemoteParticipant")
|
||||
public func LKRoomVideoTracksForRemoteParticipant(room: UnsafeRawPointer, participantId: CFString) -> CFArray? {
|
||||
let room = Unmanaged<Room>.fromOpaque(room).takeUnretainedValue()
|
||||
|
||||
for (_, participant) in room.remoteParticipants {
|
||||
if participant.identity == participantId as String {
|
||||
return participant.videoTracks.compactMap { $0.track as? RemoteVideoTrack } as CFArray?
|
||||
}
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
@_cdecl("LKLocalAudioTrackCreateTrack")
|
||||
public func LKLocalAudioTrackCreateTrack() -> UnsafeMutableRawPointer {
|
||||
let track = LocalAudioTrack.createTrack(options: AudioCaptureOptions(
|
||||
echoCancellation: true,
|
||||
noiseSuppression: true
|
||||
))
|
||||
|
||||
return Unmanaged.passRetained(track).toOpaque()
|
||||
}
|
||||
|
||||
|
||||
@_cdecl("LKCreateScreenShareTrackForDisplay")
|
||||
public func LKCreateScreenShareTrackForDisplay(display: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
|
||||
let display = Unmanaged<MacOSDisplay>.fromOpaque(display).takeUnretainedValue()
|
||||
let track = LocalVideoTrack.createMacOSScreenShareTrack(source: display, preferredMethod: .legacy)
|
||||
return Unmanaged.passRetained(track).toOpaque()
|
||||
}
|
||||
|
||||
@_cdecl("LKVideoRendererCreate")
|
||||
public func LKVideoRendererCreate(data: UnsafeRawPointer, onFrame: @escaping @convention(c) (UnsafeRawPointer, CVPixelBuffer) -> Bool, onDrop: @escaping @convention(c) (UnsafeRawPointer) -> Void) -> UnsafeMutableRawPointer {
|
||||
Unmanaged.passRetained(LKVideoRenderer(data: data, onFrame: onFrame, onDrop: onDrop)).toOpaque()
|
||||
}
|
||||
|
||||
@_cdecl("LKVideoTrackAddRenderer")
|
||||
public func LKVideoTrackAddRenderer(track: UnsafeRawPointer, renderer: UnsafeRawPointer) {
|
||||
let track = Unmanaged<Track>.fromOpaque(track).takeUnretainedValue() as! VideoTrack
|
||||
let renderer = Unmanaged<LKVideoRenderer>.fromOpaque(renderer).takeRetainedValue()
|
||||
renderer.track = track
|
||||
track.add(videoRenderer: renderer)
|
||||
}
|
||||
|
||||
@_cdecl("LKRemoteVideoTrackGetSid")
|
||||
public func LKRemoteVideoTrackGetSid(track: UnsafeRawPointer) -> CFString {
|
||||
let track = Unmanaged<RemoteVideoTrack>.fromOpaque(track).takeUnretainedValue()
|
||||
return track.sid! as CFString
|
||||
}
|
||||
|
||||
@_cdecl("LKRemoteAudioTrackGetSid")
|
||||
public func LKRemoteAudioTrackGetSid(track: UnsafeRawPointer) -> CFString {
|
||||
let track = Unmanaged<RemoteAudioTrack>.fromOpaque(track).takeUnretainedValue()
|
||||
return track.sid! as CFString
|
||||
}
|
||||
|
||||
@_cdecl("LKRemoteAudioTrackStart")
|
||||
public func LKRemoteAudioTrackStart(track: UnsafeRawPointer) {
|
||||
let track = Unmanaged<RemoteAudioTrack>.fromOpaque(track).takeUnretainedValue()
|
||||
track.start()
|
||||
}
|
||||
|
||||
@_cdecl("LKRemoteAudioTrackStop")
|
||||
public func LKRemoteAudioTrackStop(track: UnsafeRawPointer) {
|
||||
let track = Unmanaged<RemoteAudioTrack>.fromOpaque(track).takeUnretainedValue()
|
||||
track.stop()
|
||||
}
|
||||
|
||||
@_cdecl("LKDisplaySources")
|
||||
public func LKDisplaySources(data: UnsafeRawPointer, callback: @escaping @convention(c) (UnsafeRawPointer, CFArray?, CFString?) -> Void) {
|
||||
MacOSScreenCapturer.sources(for: .display, includeCurrentApplication: false, preferredMethod: .legacy).then { displaySources in
|
||||
callback(data, displaySources as CFArray, nil)
|
||||
}.catch { error in
|
||||
callback(data, nil, error.localizedDescription as CFString)
|
||||
}
|
||||
}
|
||||
|
||||
@_cdecl("LKLocalTrackPublicationSetMute")
|
||||
public func LKLocalTrackPublicationSetMute(
|
||||
publication: UnsafeRawPointer,
|
||||
muted: Bool,
|
||||
on_complete: @escaping @convention(c) (UnsafeRawPointer, CFString?) -> Void,
|
||||
callback_data: UnsafeRawPointer
|
||||
) {
|
||||
let publication = Unmanaged<LocalTrackPublication>.fromOpaque(publication).takeUnretainedValue()
|
||||
|
||||
if muted {
|
||||
publication.mute().then {
|
||||
on_complete(callback_data, nil)
|
||||
}.catch { error in
|
||||
on_complete(callback_data, error.localizedDescription as CFString)
|
||||
}
|
||||
} else {
|
||||
publication.unmute().then {
|
||||
on_complete(callback_data, nil)
|
||||
}.catch { error in
|
||||
on_complete(callback_data, error.localizedDescription as CFString)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@_cdecl("LKLocalTrackPublicationIsMuted")
|
||||
public func LKLocalTrackPublicationIsMuted(
|
||||
publication: UnsafeRawPointer
|
||||
) -> Bool {
|
||||
let publication = Unmanaged<LocalTrackPublication>.fromOpaque(publication).takeUnretainedValue()
|
||||
return publication.muted
|
||||
}
|
||||
|
||||
@_cdecl("LKRemoteTrackPublicationSetEnabled")
|
||||
public func LKRemoteTrackPublicationSetEnabled(
|
||||
publication: UnsafeRawPointer,
|
||||
enabled: Bool,
|
||||
on_complete: @escaping @convention(c) (UnsafeRawPointer, CFString?) -> Void,
|
||||
callback_data: UnsafeRawPointer
|
||||
) {
|
||||
let publication = Unmanaged<RemoteTrackPublication>.fromOpaque(publication).takeUnretainedValue()
|
||||
|
||||
publication.set(enabled: enabled).then {
|
||||
on_complete(callback_data, nil)
|
||||
}.catch { error in
|
||||
on_complete(callback_data, error.localizedDescription as CFString)
|
||||
}
|
||||
}
|
||||
|
||||
@_cdecl("LKRemoteTrackPublicationIsMuted")
|
||||
public func LKRemoteTrackPublicationIsMuted(
|
||||
publication: UnsafeRawPointer
|
||||
) -> Bool {
|
||||
let publication = Unmanaged<RemoteTrackPublication>.fromOpaque(publication).takeUnretainedValue()
|
||||
|
||||
return publication.muted
|
||||
}
|
||||
|
||||
@_cdecl("LKRemoteTrackPublicationGetSid")
|
||||
public func LKRemoteTrackPublicationGetSid(
|
||||
publication: UnsafeRawPointer
|
||||
) -> CFString {
|
||||
let publication = Unmanaged<RemoteTrackPublication>.fromOpaque(publication).takeUnretainedValue()
|
||||
|
||||
return publication.sid as CFString
|
||||
}
|
||||
|
||||
@_cdecl("LKLocalTrackPublicationGetSid")
|
||||
public func LKLocalTrackPublicationGetSid(
|
||||
publication: UnsafeRawPointer
|
||||
) -> CFString {
|
||||
let publication = Unmanaged<LocalTrackPublication>.fromOpaque(publication).takeUnretainedValue()
|
||||
|
||||
return publication.sid as CFString
|
||||
}
|
185
crates/livekit_client_macos/build.rs
Normal file
185
crates/livekit_client_macos/build.rs
Normal file
|
@ -0,0 +1,185 @@
|
|||
use serde::Deserialize;
|
||||
use std::{
|
||||
env,
|
||||
path::{Path, PathBuf},
|
||||
process::Command,
|
||||
};
|
||||
|
||||
const SWIFT_PACKAGE_NAME: &str = "LiveKitBridge";
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SwiftTargetInfo {
|
||||
pub triple: String,
|
||||
pub unversioned_triple: String,
|
||||
pub module_triple: String,
|
||||
pub swift_runtime_compatibility_version: String,
|
||||
#[serde(rename = "librariesRequireRPath")]
|
||||
pub libraries_require_rpath: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SwiftPaths {
|
||||
pub runtime_library_paths: Vec<String>,
|
||||
pub runtime_library_import_paths: Vec<String>,
|
||||
pub runtime_resource_path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct SwiftTarget {
|
||||
pub target: SwiftTargetInfo,
|
||||
pub paths: SwiftPaths,
|
||||
}
|
||||
|
||||
const MACOS_TARGET_VERSION: &str = "10.15.7";
|
||||
|
||||
fn main() {
|
||||
if cfg!(all(
|
||||
target_os = "macos",
|
||||
not(any(test, feature = "test-support", feature = "no-webrtc")),
|
||||
)) {
|
||||
let swift_target = get_swift_target();
|
||||
|
||||
build_bridge(&swift_target);
|
||||
link_swift_stdlib(&swift_target);
|
||||
link_webrtc_framework(&swift_target);
|
||||
|
||||
// Register exported Objective-C selectors, protocols, etc when building example binaries.
|
||||
println!("cargo:rustc-link-arg=-Wl,-ObjC");
|
||||
}
|
||||
}
|
||||
|
||||
fn build_bridge(swift_target: &SwiftTarget) {
|
||||
println!("cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET");
|
||||
println!("cargo:rerun-if-changed={}/Sources", SWIFT_PACKAGE_NAME);
|
||||
println!(
|
||||
"cargo:rerun-if-changed={}/Package.swift",
|
||||
SWIFT_PACKAGE_NAME
|
||||
);
|
||||
println!(
|
||||
"cargo:rerun-if-changed={}/Package.resolved",
|
||||
SWIFT_PACKAGE_NAME
|
||||
);
|
||||
|
||||
let swift_package_root = swift_package_root();
|
||||
let swift_target_folder = swift_target_folder();
|
||||
let swift_cache_folder = swift_cache_folder();
|
||||
if !Command::new("swift")
|
||||
.arg("build")
|
||||
.arg("--disable-automatic-resolution")
|
||||
.args(["--configuration", &env::var("PROFILE").unwrap()])
|
||||
.args(["--triple", &swift_target.target.triple])
|
||||
.args(["--build-path".into(), swift_target_folder])
|
||||
.args(["--cache-path".into(), swift_cache_folder])
|
||||
.current_dir(&swift_package_root)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success()
|
||||
{
|
||||
panic!(
|
||||
"Failed to compile swift package in {}",
|
||||
swift_package_root.display()
|
||||
);
|
||||
}
|
||||
|
||||
println!(
|
||||
"cargo:rustc-link-search=native={}",
|
||||
swift_target.out_dir_path().display()
|
||||
);
|
||||
println!("cargo:rustc-link-lib=static={}", SWIFT_PACKAGE_NAME);
|
||||
}
|
||||
|
||||
fn link_swift_stdlib(swift_target: &SwiftTarget) {
|
||||
for path in &swift_target.paths.runtime_library_paths {
|
||||
println!("cargo:rustc-link-search=native={}", path);
|
||||
}
|
||||
}
|
||||
|
||||
fn link_webrtc_framework(swift_target: &SwiftTarget) {
|
||||
let swift_out_dir_path = swift_target.out_dir_path();
|
||||
println!("cargo:rustc-link-lib=framework=WebRTC");
|
||||
println!(
|
||||
"cargo:rustc-link-search=framework={}",
|
||||
swift_out_dir_path.display()
|
||||
);
|
||||
// Find WebRTC.framework as a sibling of the executable when running tests.
|
||||
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path");
|
||||
// Find WebRTC.framework in parent directory of the executable when running examples.
|
||||
println!("cargo:rustc-link-arg=-Wl,-rpath,@executable_path/..");
|
||||
|
||||
let source_path = swift_out_dir_path.join("WebRTC.framework");
|
||||
let deps_dir_path =
|
||||
PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../deps/WebRTC.framework");
|
||||
let target_dir_path =
|
||||
PathBuf::from(env::var("OUT_DIR").unwrap()).join("../../../WebRTC.framework");
|
||||
copy_dir(&source_path, &deps_dir_path);
|
||||
copy_dir(&source_path, &target_dir_path);
|
||||
}
|
||||
|
||||
fn get_swift_target() -> SwiftTarget {
|
||||
let mut arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
|
||||
if arch == "aarch64" {
|
||||
arch = "arm64".into();
|
||||
}
|
||||
let target = format!("{}-apple-macosx{}", arch, MACOS_TARGET_VERSION);
|
||||
|
||||
let swift_target_info_str = Command::new("swift")
|
||||
.args(["-target", &target, "-print-target-info"])
|
||||
.output()
|
||||
.unwrap()
|
||||
.stdout;
|
||||
|
||||
serde_json::from_slice(&swift_target_info_str).unwrap()
|
||||
}
|
||||
|
||||
fn swift_package_root() -> PathBuf {
|
||||
env::current_dir().unwrap().join(SWIFT_PACKAGE_NAME)
|
||||
}
|
||||
|
||||
fn swift_target_folder() -> PathBuf {
|
||||
let target = env::var("TARGET").unwrap();
|
||||
env::current_dir()
|
||||
.unwrap()
|
||||
.join(format!("../../target/{target}/{SWIFT_PACKAGE_NAME}_target"))
|
||||
}
|
||||
|
||||
fn swift_cache_folder() -> PathBuf {
|
||||
let target = env::var("TARGET").unwrap();
|
||||
env::current_dir()
|
||||
.unwrap()
|
||||
.join(format!("../../target/{target}/{SWIFT_PACKAGE_NAME}_cache"))
|
||||
}
|
||||
|
||||
fn copy_dir(source: &Path, destination: &Path) {
|
||||
assert!(
|
||||
Command::new("rm")
|
||||
.arg("-rf")
|
||||
.arg(destination)
|
||||
.status()
|
||||
.unwrap()
|
||||
.success(),
|
||||
"could not remove {:?} before copying",
|
||||
destination
|
||||
);
|
||||
|
||||
assert!(
|
||||
Command::new("cp")
|
||||
.arg("-R")
|
||||
.args([source, destination])
|
||||
.status()
|
||||
.unwrap()
|
||||
.success(),
|
||||
"could not copy {:?} to {:?}",
|
||||
source,
|
||||
destination
|
||||
);
|
||||
}
|
||||
|
||||
impl SwiftTarget {
|
||||
fn out_dir_path(&self) -> PathBuf {
|
||||
swift_target_folder()
|
||||
.join(&self.target.unversioned_triple)
|
||||
.join(env::var("PROFILE").unwrap())
|
||||
}
|
||||
}
|
172
crates/livekit_client_macos/examples/test_app.rs
Normal file
172
crates/livekit_client_macos/examples/test_app.rs
Normal file
|
@ -0,0 +1,172 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use futures::StreamExt;
|
||||
use gpui::{actions, KeyBinding, Menu, MenuItem};
|
||||
use livekit_client_macos::{LocalAudioTrack, LocalVideoTrack, Room, RoomUpdate};
|
||||
use livekit_server::token::{self, VideoGrant};
|
||||
use log::LevelFilter;
|
||||
use simplelog::SimpleLogger;
|
||||
|
||||
actions!(livekit_client_macos, [Quit]);
|
||||
|
||||
fn main() {
|
||||
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
|
||||
|
||||
gpui::App::new().run(|cx| {
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
println!("USING TEST LIVEKIT");
|
||||
|
||||
#[cfg(not(any(test, feature = "test-support")))]
|
||||
println!("USING REAL LIVEKIT");
|
||||
|
||||
cx.activate(true);
|
||||
|
||||
cx.on_action(quit);
|
||||
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
|
||||
|
||||
cx.set_menus(vec![Menu {
|
||||
name: "Zed".into(),
|
||||
items: vec![MenuItem::Action {
|
||||
name: "Quit".into(),
|
||||
action: Box::new(Quit),
|
||||
os_action: None,
|
||||
}],
|
||||
}]);
|
||||
|
||||
let live_kit_url = std::env::var("LIVE_KIT_URL").unwrap_or("http://localhost:7880".into());
|
||||
let live_kit_key = std::env::var("LIVE_KIT_KEY").unwrap_or("devkey".into());
|
||||
let live_kit_secret = std::env::var("LIVE_KIT_SECRET").unwrap_or("secret".into());
|
||||
|
||||
cx.spawn(|cx| async move {
|
||||
let user_a_token = token::create(
|
||||
&live_kit_key,
|
||||
&live_kit_secret,
|
||||
Some("test-participant-1"),
|
||||
VideoGrant::to_join("test-room"),
|
||||
)
|
||||
.unwrap();
|
||||
let room_a = Room::new();
|
||||
room_a.connect(&live_kit_url, &user_a_token).await.unwrap();
|
||||
|
||||
let user2_token = token::create(
|
||||
&live_kit_key,
|
||||
&live_kit_secret,
|
||||
Some("test-participant-2"),
|
||||
VideoGrant::to_join("test-room"),
|
||||
)
|
||||
.unwrap();
|
||||
let room_b = Room::new();
|
||||
room_b.connect(&live_kit_url, &user2_token).await.unwrap();
|
||||
|
||||
let mut room_updates = room_b.updates();
|
||||
let audio_track = LocalAudioTrack::create();
|
||||
let audio_track_publication = room_a.publish_audio_track(audio_track).await.unwrap();
|
||||
|
||||
if let RoomUpdate::SubscribedToRemoteAudioTrack(track, _) =
|
||||
room_updates.next().await.unwrap()
|
||||
{
|
||||
let remote_tracks = room_b.remote_audio_tracks("test-participant-1");
|
||||
assert_eq!(remote_tracks.len(), 1);
|
||||
assert_eq!(remote_tracks[0].publisher_id(), "test-participant-1");
|
||||
assert_eq!(track.publisher_id(), "test-participant-1");
|
||||
} else {
|
||||
panic!("unexpected message");
|
||||
}
|
||||
|
||||
audio_track_publication.set_mute(true).await.unwrap();
|
||||
|
||||
println!("waiting for mute changed!");
|
||||
if let RoomUpdate::RemoteAudioTrackMuteChanged { track_id, muted } =
|
||||
room_updates.next().await.unwrap()
|
||||
{
|
||||
let remote_tracks = room_b.remote_audio_tracks("test-participant-1");
|
||||
assert_eq!(remote_tracks[0].sid(), track_id);
|
||||
assert!(muted);
|
||||
} else {
|
||||
panic!("unexpected message");
|
||||
}
|
||||
|
||||
audio_track_publication.set_mute(false).await.unwrap();
|
||||
|
||||
if let RoomUpdate::RemoteAudioTrackMuteChanged { track_id, muted } =
|
||||
room_updates.next().await.unwrap()
|
||||
{
|
||||
let remote_tracks = room_b.remote_audio_tracks("test-participant-1");
|
||||
assert_eq!(remote_tracks[0].sid(), track_id);
|
||||
assert!(!muted);
|
||||
} else {
|
||||
panic!("unexpected message");
|
||||
}
|
||||
|
||||
println!("Pausing for 5 seconds to test audio, make some noise!");
|
||||
let timer = cx.background_executor().timer(Duration::from_secs(5));
|
||||
timer.await;
|
||||
let remote_audio_track = room_b
|
||||
.remote_audio_tracks("test-participant-1")
|
||||
.pop()
|
||||
.unwrap();
|
||||
room_a.unpublish_track(audio_track_publication);
|
||||
|
||||
// Clear out any active speakers changed messages
|
||||
let mut next = room_updates.next().await.unwrap();
|
||||
while let RoomUpdate::ActiveSpeakersChanged { speakers } = next {
|
||||
println!("Speakers changed: {:?}", speakers);
|
||||
next = room_updates.next().await.unwrap();
|
||||
}
|
||||
|
||||
if let RoomUpdate::UnsubscribedFromRemoteAudioTrack {
|
||||
publisher_id,
|
||||
track_id,
|
||||
} = next
|
||||
{
|
||||
assert_eq!(publisher_id, "test-participant-1");
|
||||
assert_eq!(remote_audio_track.sid(), track_id);
|
||||
assert_eq!(room_b.remote_audio_tracks("test-participant-1").len(), 0);
|
||||
} else {
|
||||
panic!("unexpected message");
|
||||
}
|
||||
|
||||
let displays = room_a.display_sources().await.unwrap();
|
||||
let display = displays.into_iter().next().unwrap();
|
||||
|
||||
let local_video_track = LocalVideoTrack::screen_share_for_display(&display);
|
||||
let local_video_track_publication =
|
||||
room_a.publish_video_track(local_video_track).await.unwrap();
|
||||
|
||||
if let RoomUpdate::SubscribedToRemoteVideoTrack(track) =
|
||||
room_updates.next().await.unwrap()
|
||||
{
|
||||
let remote_video_tracks = room_b.remote_video_tracks("test-participant-1");
|
||||
assert_eq!(remote_video_tracks.len(), 1);
|
||||
assert_eq!(remote_video_tracks[0].publisher_id(), "test-participant-1");
|
||||
assert_eq!(track.publisher_id(), "test-participant-1");
|
||||
} else {
|
||||
panic!("unexpected message");
|
||||
}
|
||||
|
||||
let remote_video_track = room_b
|
||||
.remote_video_tracks("test-participant-1")
|
||||
.pop()
|
||||
.unwrap();
|
||||
room_a.unpublish_track(local_video_track_publication);
|
||||
if let RoomUpdate::UnsubscribedFromRemoteVideoTrack {
|
||||
publisher_id,
|
||||
track_id,
|
||||
} = room_updates.next().await.unwrap()
|
||||
{
|
||||
assert_eq!(publisher_id, "test-participant-1");
|
||||
assert_eq!(remote_video_track.sid(), track_id);
|
||||
assert_eq!(room_b.remote_video_tracks("test-participant-1").len(), 0);
|
||||
} else {
|
||||
panic!("unexpected message");
|
||||
}
|
||||
|
||||
cx.update(|cx| cx.shutdown()).ok();
|
||||
})
|
||||
.detach();
|
||||
});
|
||||
}
|
||||
|
||||
fn quit(_: &Quit, cx: &mut gpui::AppContext) {
|
||||
cx.quit();
|
||||
}
|
37
crates/livekit_client_macos/src/livekit_client.rs
Normal file
37
crates/livekit_client_macos/src/livekit_client.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
#![allow(clippy::arc_with_non_send_sync)]
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
#[cfg(all(target_os = "macos", not(any(test, feature = "test-support"))))]
|
||||
pub mod prod;
|
||||
|
||||
#[cfg(all(target_os = "macos", not(any(test, feature = "test-support"))))]
|
||||
pub use prod::*;
|
||||
|
||||
#[cfg(any(test, feature = "test-support", not(target_os = "macos")))]
|
||||
pub mod test;
|
||||
|
||||
#[cfg(any(test, feature = "test-support", not(target_os = "macos")))]
|
||||
pub use test::*;
|
||||
|
||||
pub type Sid = String;
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub enum ConnectionState {
|
||||
Disconnected,
|
||||
Connected { url: String, token: String },
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum RoomUpdate {
|
||||
ActiveSpeakersChanged { speakers: Vec<Sid> },
|
||||
RemoteAudioTrackMuteChanged { track_id: Sid, muted: bool },
|
||||
SubscribedToRemoteVideoTrack(Arc<RemoteVideoTrack>),
|
||||
SubscribedToRemoteAudioTrack(Arc<RemoteAudioTrack>, Arc<RemoteTrackPublication>),
|
||||
UnsubscribedFromRemoteVideoTrack { publisher_id: Sid, track_id: Sid },
|
||||
UnsubscribedFromRemoteAudioTrack { publisher_id: Sid, track_id: Sid },
|
||||
LocalAudioTrackPublished { publication: LocalTrackPublication },
|
||||
LocalAudioTrackUnpublished { publication: LocalTrackPublication },
|
||||
LocalVideoTrackPublished { publication: LocalTrackPublication },
|
||||
LocalVideoTrackUnpublished { publication: LocalTrackPublication },
|
||||
}
|
981
crates/livekit_client_macos/src/prod.rs
Normal file
981
crates/livekit_client_macos/src/prod.rs
Normal file
|
@ -0,0 +1,981 @@
|
|||
use crate::{ConnectionState, RoomUpdate, Sid};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use core_foundation::{
|
||||
array::{CFArray, CFArrayRef},
|
||||
base::{CFRelease, CFRetain, TCFType},
|
||||
string::{CFString, CFStringRef},
|
||||
};
|
||||
use futures::{
|
||||
channel::{mpsc, oneshot},
|
||||
Future,
|
||||
};
|
||||
pub use media::core_video::CVImageBuffer;
|
||||
use media::core_video::CVImageBufferRef;
|
||||
use parking_lot::Mutex;
|
||||
use postage::watch;
|
||||
use std::{
|
||||
ffi::c_void,
|
||||
sync::{Arc, Weak},
|
||||
};
|
||||
|
||||
macro_rules! pointer_type {
|
||||
($pointer_name:ident) => {
|
||||
#[repr(transparent)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct $pointer_name(pub *const std::ffi::c_void);
|
||||
unsafe impl Send for $pointer_name {}
|
||||
};
|
||||
}
|
||||
|
||||
mod swift {
|
||||
pointer_type!(Room);
|
||||
pointer_type!(LocalAudioTrack);
|
||||
pointer_type!(RemoteAudioTrack);
|
||||
pointer_type!(LocalVideoTrack);
|
||||
pointer_type!(RemoteVideoTrack);
|
||||
pointer_type!(LocalTrackPublication);
|
||||
pointer_type!(RemoteTrackPublication);
|
||||
pointer_type!(MacOSDisplay);
|
||||
pointer_type!(RoomDelegate);
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn LKRoomDelegateCreate(
|
||||
callback_data: *mut c_void,
|
||||
on_did_disconnect: extern "C" fn(callback_data: *mut c_void),
|
||||
on_did_subscribe_to_remote_audio_track: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
remote_track: swift::RemoteAudioTrack,
|
||||
remote_publication: swift::RemoteTrackPublication,
|
||||
),
|
||||
on_did_unsubscribe_from_remote_audio_track: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
),
|
||||
on_mute_changed_from_remote_audio_track: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
track_id: CFStringRef,
|
||||
muted: bool,
|
||||
),
|
||||
on_active_speakers_changed: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
participants: CFArrayRef,
|
||||
),
|
||||
on_did_subscribe_to_remote_video_track: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
remote_track: swift::RemoteVideoTrack,
|
||||
),
|
||||
on_did_unsubscribe_from_remote_video_track: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
),
|
||||
on_did_publish_or_unpublish_local_audio_track: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
publication: swift::LocalTrackPublication,
|
||||
is_published: bool,
|
||||
),
|
||||
on_did_publish_or_unpublish_local_video_track: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
publication: swift::LocalTrackPublication,
|
||||
is_published: bool,
|
||||
),
|
||||
) -> swift::RoomDelegate;
|
||||
|
||||
fn LKRoomCreate(delegate: swift::RoomDelegate) -> swift::Room;
|
||||
fn LKRoomConnect(
|
||||
room: swift::Room,
|
||||
url: CFStringRef,
|
||||
token: CFStringRef,
|
||||
callback: extern "C" fn(*mut c_void, CFStringRef),
|
||||
callback_data: *mut c_void,
|
||||
);
|
||||
fn LKRoomDisconnect(room: swift::Room);
|
||||
fn LKRoomPublishVideoTrack(
|
||||
room: swift::Room,
|
||||
track: swift::LocalVideoTrack,
|
||||
callback: extern "C" fn(*mut c_void, swift::LocalTrackPublication, CFStringRef),
|
||||
callback_data: *mut c_void,
|
||||
);
|
||||
fn LKRoomPublishAudioTrack(
|
||||
room: swift::Room,
|
||||
track: swift::LocalAudioTrack,
|
||||
callback: extern "C" fn(*mut c_void, swift::LocalTrackPublication, CFStringRef),
|
||||
callback_data: *mut c_void,
|
||||
);
|
||||
fn LKRoomUnpublishTrack(room: swift::Room, publication: swift::LocalTrackPublication);
|
||||
|
||||
fn LKRoomAudioTracksForRemoteParticipant(
|
||||
room: swift::Room,
|
||||
participant_id: CFStringRef,
|
||||
) -> CFArrayRef;
|
||||
|
||||
fn LKRoomAudioTrackPublicationsForRemoteParticipant(
|
||||
room: swift::Room,
|
||||
participant_id: CFStringRef,
|
||||
) -> CFArrayRef;
|
||||
|
||||
fn LKRoomVideoTracksForRemoteParticipant(
|
||||
room: swift::Room,
|
||||
participant_id: CFStringRef,
|
||||
) -> CFArrayRef;
|
||||
|
||||
fn LKVideoRendererCreate(
|
||||
callback_data: *mut c_void,
|
||||
on_frame: extern "C" fn(callback_data: *mut c_void, frame: CVImageBufferRef) -> bool,
|
||||
on_drop: extern "C" fn(callback_data: *mut c_void),
|
||||
) -> *const c_void;
|
||||
|
||||
fn LKRemoteAudioTrackGetSid(track: swift::RemoteAudioTrack) -> CFStringRef;
|
||||
fn LKRemoteVideoTrackGetSid(track: swift::RemoteVideoTrack) -> CFStringRef;
|
||||
fn LKRemoteAudioTrackStart(track: swift::RemoteAudioTrack);
|
||||
fn LKRemoteAudioTrackStop(track: swift::RemoteAudioTrack);
|
||||
fn LKVideoTrackAddRenderer(track: swift::RemoteVideoTrack, renderer: *const c_void);
|
||||
|
||||
fn LKDisplaySources(
|
||||
callback_data: *mut c_void,
|
||||
callback: extern "C" fn(
|
||||
callback_data: *mut c_void,
|
||||
sources: CFArrayRef,
|
||||
error: CFStringRef,
|
||||
),
|
||||
);
|
||||
fn LKCreateScreenShareTrackForDisplay(display: swift::MacOSDisplay) -> swift::LocalVideoTrack;
|
||||
fn LKLocalAudioTrackCreateTrack() -> swift::LocalAudioTrack;
|
||||
|
||||
fn LKLocalTrackPublicationSetMute(
|
||||
publication: swift::LocalTrackPublication,
|
||||
muted: bool,
|
||||
on_complete: extern "C" fn(callback_data: *mut c_void, error: CFStringRef),
|
||||
callback_data: *mut c_void,
|
||||
);
|
||||
|
||||
fn LKRemoteTrackPublicationSetEnabled(
|
||||
publication: swift::RemoteTrackPublication,
|
||||
enabled: bool,
|
||||
on_complete: extern "C" fn(callback_data: *mut c_void, error: CFStringRef),
|
||||
callback_data: *mut c_void,
|
||||
);
|
||||
|
||||
fn LKLocalTrackPublicationIsMuted(publication: swift::LocalTrackPublication) -> bool;
|
||||
fn LKRemoteTrackPublicationIsMuted(publication: swift::RemoteTrackPublication) -> bool;
|
||||
fn LKLocalTrackPublicationGetSid(publication: swift::LocalTrackPublication) -> CFStringRef;
|
||||
fn LKRemoteTrackPublicationGetSid(publication: swift::RemoteTrackPublication) -> CFStringRef;
|
||||
}
|
||||
|
||||
pub struct Room {
|
||||
native_room: swift::Room,
|
||||
connection: Mutex<(
|
||||
watch::Sender<ConnectionState>,
|
||||
watch::Receiver<ConnectionState>,
|
||||
)>,
|
||||
update_subscribers: Mutex<Vec<mpsc::UnboundedSender<RoomUpdate>>>,
|
||||
_delegate: RoomDelegate,
|
||||
}
|
||||
|
||||
impl Room {
|
||||
pub fn new() -> Arc<Self> {
|
||||
Arc::new_cyclic(|weak_room| {
|
||||
let delegate = RoomDelegate::new(weak_room.clone());
|
||||
Self {
|
||||
native_room: unsafe { LKRoomCreate(delegate.native_delegate) },
|
||||
connection: Mutex::new(watch::channel_with(ConnectionState::Disconnected)),
|
||||
update_subscribers: Default::default(),
|
||||
_delegate: delegate,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn status(&self) -> watch::Receiver<ConnectionState> {
|
||||
self.connection.lock().1.clone()
|
||||
}
|
||||
|
||||
pub fn connect(self: &Arc<Self>, url: &str, token: &str) -> impl Future<Output = Result<()>> {
|
||||
let url = CFString::new(url);
|
||||
let token = CFString::new(token);
|
||||
let (did_connect, tx, rx) = Self::build_done_callback();
|
||||
unsafe {
|
||||
LKRoomConnect(
|
||||
self.native_room,
|
||||
url.as_concrete_TypeRef(),
|
||||
token.as_concrete_TypeRef(),
|
||||
did_connect,
|
||||
tx,
|
||||
)
|
||||
}
|
||||
|
||||
let this = self.clone();
|
||||
let url = url.to_string();
|
||||
let token = token.to_string();
|
||||
async move {
|
||||
rx.await.unwrap().context("error connecting to room")?;
|
||||
*this.connection.lock().0.borrow_mut() = ConnectionState::Connected { url, token };
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn did_disconnect(&self) {
|
||||
*self.connection.lock().0.borrow_mut() = ConnectionState::Disconnected;
|
||||
}
|
||||
|
||||
pub fn display_sources(self: &Arc<Self>) -> impl Future<Output = Result<Vec<MacOSDisplay>>> {
|
||||
extern "C" fn callback(tx: *mut c_void, sources: CFArrayRef, error: CFStringRef) {
|
||||
unsafe {
|
||||
let tx = Box::from_raw(tx as *mut oneshot::Sender<Result<Vec<MacOSDisplay>>>);
|
||||
|
||||
if sources.is_null() {
|
||||
let _ = tx.send(Err(anyhow!("{}", CFString::wrap_under_get_rule(error))));
|
||||
} else {
|
||||
let sources = CFArray::wrap_under_get_rule(sources)
|
||||
.into_iter()
|
||||
.map(|source| MacOSDisplay::new(swift::MacOSDisplay(*source)))
|
||||
.collect();
|
||||
|
||||
let _ = tx.send(Ok(sources));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
unsafe {
|
||||
LKDisplaySources(Box::into_raw(Box::new(tx)) as *mut _, callback);
|
||||
}
|
||||
|
||||
async move { rx.await.unwrap() }
|
||||
}
|
||||
|
||||
pub fn publish_video_track(
|
||||
self: &Arc<Self>,
|
||||
track: LocalVideoTrack,
|
||||
) -> impl Future<Output = Result<LocalTrackPublication>> {
|
||||
let (tx, rx) = oneshot::channel::<Result<LocalTrackPublication>>();
|
||||
extern "C" fn callback(
|
||||
tx: *mut c_void,
|
||||
publication: swift::LocalTrackPublication,
|
||||
error: CFStringRef,
|
||||
) {
|
||||
let tx =
|
||||
unsafe { Box::from_raw(tx as *mut oneshot::Sender<Result<LocalTrackPublication>>) };
|
||||
if error.is_null() {
|
||||
let _ = tx.send(Ok(LocalTrackPublication::new(publication)));
|
||||
} else {
|
||||
let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
|
||||
let _ = tx.send(Err(anyhow!(error)));
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
LKRoomPublishVideoTrack(
|
||||
self.native_room,
|
||||
track.0,
|
||||
callback,
|
||||
Box::into_raw(Box::new(tx)) as *mut c_void,
|
||||
);
|
||||
}
|
||||
async { rx.await.unwrap().context("error publishing video track") }
|
||||
}
|
||||
|
||||
pub fn publish_audio_track(
|
||||
self: &Arc<Self>,
|
||||
track: LocalAudioTrack,
|
||||
) -> impl Future<Output = Result<LocalTrackPublication>> {
|
||||
let (tx, rx) = oneshot::channel::<Result<LocalTrackPublication>>();
|
||||
extern "C" fn callback(
|
||||
tx: *mut c_void,
|
||||
publication: swift::LocalTrackPublication,
|
||||
error: CFStringRef,
|
||||
) {
|
||||
let tx =
|
||||
unsafe { Box::from_raw(tx as *mut oneshot::Sender<Result<LocalTrackPublication>>) };
|
||||
if error.is_null() {
|
||||
let _ = tx.send(Ok(LocalTrackPublication::new(publication)));
|
||||
} else {
|
||||
let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
|
||||
let _ = tx.send(Err(anyhow!(error)));
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
LKRoomPublishAudioTrack(
|
||||
self.native_room,
|
||||
track.0,
|
||||
callback,
|
||||
Box::into_raw(Box::new(tx)) as *mut c_void,
|
||||
);
|
||||
}
|
||||
async { rx.await.unwrap().context("error publishing audio track") }
|
||||
}
|
||||
|
||||
pub fn unpublish_track(&self, publication: LocalTrackPublication) {
|
||||
unsafe {
|
||||
LKRoomUnpublishTrack(self.native_room, publication.0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remote_video_tracks(&self, participant_id: &str) -> Vec<Arc<RemoteVideoTrack>> {
|
||||
unsafe {
|
||||
let tracks = LKRoomVideoTracksForRemoteParticipant(
|
||||
self.native_room,
|
||||
CFString::new(participant_id).as_concrete_TypeRef(),
|
||||
);
|
||||
|
||||
if tracks.is_null() {
|
||||
Vec::new()
|
||||
} else {
|
||||
let tracks = CFArray::wrap_under_get_rule(tracks);
|
||||
tracks
|
||||
.into_iter()
|
||||
.map(|native_track| {
|
||||
let native_track = swift::RemoteVideoTrack(*native_track);
|
||||
let id =
|
||||
CFString::wrap_under_get_rule(LKRemoteVideoTrackGetSid(native_track))
|
||||
.to_string();
|
||||
Arc::new(RemoteVideoTrack::new(
|
||||
native_track,
|
||||
id,
|
||||
participant_id.into(),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remote_audio_tracks(&self, participant_id: &str) -> Vec<Arc<RemoteAudioTrack>> {
|
||||
unsafe {
|
||||
let tracks = LKRoomAudioTracksForRemoteParticipant(
|
||||
self.native_room,
|
||||
CFString::new(participant_id).as_concrete_TypeRef(),
|
||||
);
|
||||
|
||||
if tracks.is_null() {
|
||||
Vec::new()
|
||||
} else {
|
||||
let tracks = CFArray::wrap_under_get_rule(tracks);
|
||||
tracks
|
||||
.into_iter()
|
||||
.map(|native_track| {
|
||||
let native_track = swift::RemoteAudioTrack(*native_track);
|
||||
let id =
|
||||
CFString::wrap_under_get_rule(LKRemoteAudioTrackGetSid(native_track))
|
||||
.to_string();
|
||||
Arc::new(RemoteAudioTrack::new(
|
||||
native_track,
|
||||
id,
|
||||
participant_id.into(),
|
||||
))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remote_audio_track_publications(
|
||||
&self,
|
||||
participant_id: &str,
|
||||
) -> Vec<Arc<RemoteTrackPublication>> {
|
||||
unsafe {
|
||||
let tracks = LKRoomAudioTrackPublicationsForRemoteParticipant(
|
||||
self.native_room,
|
||||
CFString::new(participant_id).as_concrete_TypeRef(),
|
||||
);
|
||||
|
||||
if tracks.is_null() {
|
||||
Vec::new()
|
||||
} else {
|
||||
let tracks = CFArray::wrap_under_get_rule(tracks);
|
||||
tracks
|
||||
.into_iter()
|
||||
.map(|native_track_publication| {
|
||||
let native_track_publication =
|
||||
swift::RemoteTrackPublication(*native_track_publication);
|
||||
Arc::new(RemoteTrackPublication::new(native_track_publication))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn updates(&self) -> mpsc::UnboundedReceiver<RoomUpdate> {
|
||||
let (tx, rx) = mpsc::unbounded();
|
||||
self.update_subscribers.lock().push(tx);
|
||||
rx
|
||||
}
|
||||
|
||||
fn did_subscribe_to_remote_audio_track(
|
||||
&self,
|
||||
track: RemoteAudioTrack,
|
||||
publication: RemoteTrackPublication,
|
||||
) {
|
||||
let track = Arc::new(track);
|
||||
let publication = Arc::new(publication);
|
||||
self.update_subscribers.lock().retain(|tx| {
|
||||
tx.unbounded_send(RoomUpdate::SubscribedToRemoteAudioTrack(
|
||||
track.clone(),
|
||||
publication.clone(),
|
||||
))
|
||||
.is_ok()
|
||||
});
|
||||
}
|
||||
|
||||
fn did_unsubscribe_from_remote_audio_track(&self, publisher_id: String, track_id: String) {
|
||||
self.update_subscribers.lock().retain(|tx| {
|
||||
tx.unbounded_send(RoomUpdate::UnsubscribedFromRemoteAudioTrack {
|
||||
publisher_id: publisher_id.clone(),
|
||||
track_id: track_id.clone(),
|
||||
})
|
||||
.is_ok()
|
||||
});
|
||||
}
|
||||
|
||||
fn mute_changed_from_remote_audio_track(&self, track_id: String, muted: bool) {
|
||||
self.update_subscribers.lock().retain(|tx| {
|
||||
tx.unbounded_send(RoomUpdate::RemoteAudioTrackMuteChanged {
|
||||
track_id: track_id.clone(),
|
||||
muted,
|
||||
})
|
||||
.is_ok()
|
||||
});
|
||||
}
|
||||
|
||||
fn active_speakers_changed(&self, speakers: Vec<String>) {
|
||||
self.update_subscribers.lock().retain(move |tx| {
|
||||
tx.unbounded_send(RoomUpdate::ActiveSpeakersChanged {
|
||||
speakers: speakers.clone(),
|
||||
})
|
||||
.is_ok()
|
||||
});
|
||||
}
|
||||
|
||||
fn did_subscribe_to_remote_video_track(&self, track: RemoteVideoTrack) {
|
||||
let track = Arc::new(track);
|
||||
self.update_subscribers.lock().retain(|tx| {
|
||||
tx.unbounded_send(RoomUpdate::SubscribedToRemoteVideoTrack(track.clone()))
|
||||
.is_ok()
|
||||
});
|
||||
}
|
||||
|
||||
fn did_unsubscribe_from_remote_video_track(&self, publisher_id: String, track_id: String) {
|
||||
self.update_subscribers.lock().retain(|tx| {
|
||||
tx.unbounded_send(RoomUpdate::UnsubscribedFromRemoteVideoTrack {
|
||||
publisher_id: publisher_id.clone(),
|
||||
track_id: track_id.clone(),
|
||||
})
|
||||
.is_ok()
|
||||
});
|
||||
}
|
||||
|
||||
fn build_done_callback() -> (
|
||||
extern "C" fn(*mut c_void, CFStringRef),
|
||||
*mut c_void,
|
||||
oneshot::Receiver<Result<()>>,
|
||||
) {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
extern "C" fn done_callback(tx: *mut c_void, error: CFStringRef) {
|
||||
let tx = unsafe { Box::from_raw(tx as *mut oneshot::Sender<Result<()>>) };
|
||||
if error.is_null() {
|
||||
let _ = tx.send(Ok(()));
|
||||
} else {
|
||||
let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
|
||||
let _ = tx.send(Err(anyhow!(error)));
|
||||
}
|
||||
}
|
||||
(
|
||||
done_callback,
|
||||
Box::into_raw(Box::new(tx)) as *mut c_void,
|
||||
rx,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn set_display_sources(&self, _: Vec<MacOSDisplay>) {
|
||||
unreachable!("This is a test-only function")
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Room {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
LKRoomDisconnect(self.native_room);
|
||||
CFRelease(self.native_room.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RoomDelegate {
|
||||
native_delegate: swift::RoomDelegate,
|
||||
weak_room: *mut c_void,
|
||||
}
|
||||
|
||||
impl RoomDelegate {
|
||||
fn new(weak_room: Weak<Room>) -> Self {
|
||||
let weak_room = weak_room.into_raw() as *mut c_void;
|
||||
let native_delegate = unsafe {
|
||||
LKRoomDelegateCreate(
|
||||
weak_room,
|
||||
Self::on_did_disconnect,
|
||||
Self::on_did_subscribe_to_remote_audio_track,
|
||||
Self::on_did_unsubscribe_from_remote_audio_track,
|
||||
Self::on_mute_change_from_remote_audio_track,
|
||||
Self::on_active_speakers_changed,
|
||||
Self::on_did_subscribe_to_remote_video_track,
|
||||
Self::on_did_unsubscribe_from_remote_video_track,
|
||||
Self::on_did_publish_or_unpublish_local_audio_track,
|
||||
Self::on_did_publish_or_unpublish_local_video_track,
|
||||
)
|
||||
};
|
||||
Self {
|
||||
native_delegate,
|
||||
weak_room,
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn on_did_disconnect(room: *mut c_void) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.did_disconnect();
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_did_subscribe_to_remote_audio_track(
|
||||
room: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
track: swift::RemoteAudioTrack,
|
||||
publication: swift::RemoteTrackPublication,
|
||||
) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
|
||||
let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
|
||||
let track = RemoteAudioTrack::new(track, track_id, publisher_id);
|
||||
let publication = RemoteTrackPublication::new(publication);
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.did_subscribe_to_remote_audio_track(track, publication);
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_did_unsubscribe_from_remote_audio_track(
|
||||
room: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
|
||||
let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.did_unsubscribe_from_remote_audio_track(publisher_id, track_id);
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_mute_change_from_remote_audio_track(
|
||||
room: *mut c_void,
|
||||
track_id: CFStringRef,
|
||||
muted: bool,
|
||||
) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.mute_changed_from_remote_audio_track(track_id, muted);
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_active_speakers_changed(room: *mut c_void, participants: CFArrayRef) {
|
||||
if participants.is_null() {
|
||||
return;
|
||||
}
|
||||
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
let speakers = unsafe {
|
||||
CFArray::wrap_under_get_rule(participants)
|
||||
.into_iter()
|
||||
.map(
|
||||
|speaker: core_foundation::base::ItemRef<'_, *const c_void>| {
|
||||
CFString::wrap_under_get_rule(*speaker as CFStringRef).to_string()
|
||||
},
|
||||
)
|
||||
.collect()
|
||||
};
|
||||
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.active_speakers_changed(speakers);
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_did_subscribe_to_remote_video_track(
|
||||
room: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
track: swift::RemoteVideoTrack,
|
||||
) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
|
||||
let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
|
||||
let track = RemoteVideoTrack::new(track, track_id, publisher_id);
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.did_subscribe_to_remote_video_track(track);
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_did_unsubscribe_from_remote_video_track(
|
||||
room: *mut c_void,
|
||||
publisher_id: CFStringRef,
|
||||
track_id: CFStringRef,
|
||||
) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
let publisher_id = unsafe { CFString::wrap_under_get_rule(publisher_id).to_string() };
|
||||
let track_id = unsafe { CFString::wrap_under_get_rule(track_id).to_string() };
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.did_unsubscribe_from_remote_video_track(publisher_id, track_id);
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_did_publish_or_unpublish_local_audio_track(
|
||||
room: *mut c_void,
|
||||
publication: swift::LocalTrackPublication,
|
||||
is_published: bool,
|
||||
) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
if let Some(room) = room.upgrade() {
|
||||
let publication = LocalTrackPublication::new(publication);
|
||||
let update = if is_published {
|
||||
RoomUpdate::LocalAudioTrackPublished { publication }
|
||||
} else {
|
||||
RoomUpdate::LocalAudioTrackUnpublished { publication }
|
||||
};
|
||||
room.update_subscribers
|
||||
.lock()
|
||||
.retain(|tx| tx.unbounded_send(update.clone()).is_ok());
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
|
||||
extern "C" fn on_did_publish_or_unpublish_local_video_track(
|
||||
room: *mut c_void,
|
||||
publication: swift::LocalTrackPublication,
|
||||
is_published: bool,
|
||||
) {
|
||||
let room = unsafe { Weak::from_raw(room as *mut Room) };
|
||||
if let Some(room) = room.upgrade() {
|
||||
let publication = LocalTrackPublication::new(publication);
|
||||
let update = if is_published {
|
||||
RoomUpdate::LocalVideoTrackPublished { publication }
|
||||
} else {
|
||||
RoomUpdate::LocalVideoTrackUnpublished { publication }
|
||||
};
|
||||
room.update_subscribers
|
||||
.lock()
|
||||
.retain(|tx| tx.unbounded_send(update.clone()).is_ok());
|
||||
}
|
||||
let _ = Weak::into_raw(room);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RoomDelegate {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
CFRelease(self.native_delegate.0);
|
||||
let _ = Weak::from_raw(self.weak_room as *mut Room);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LocalAudioTrack(swift::LocalAudioTrack);
|
||||
|
||||
impl LocalAudioTrack {
|
||||
pub fn create() -> Self {
|
||||
Self(unsafe { LKLocalAudioTrackCreateTrack() })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LocalAudioTrack {
|
||||
fn drop(&mut self) {
|
||||
unsafe { CFRelease(self.0 .0) }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LocalVideoTrack(swift::LocalVideoTrack);
|
||||
|
||||
impl LocalVideoTrack {
|
||||
pub fn screen_share_for_display(display: &MacOSDisplay) -> Self {
|
||||
Self(unsafe { LKCreateScreenShareTrackForDisplay(display.0) })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LocalVideoTrack {
|
||||
fn drop(&mut self) {
|
||||
unsafe { CFRelease(self.0 .0) }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LocalTrackPublication(swift::LocalTrackPublication);
|
||||
|
||||
impl LocalTrackPublication {
|
||||
pub fn new(native_track_publication: swift::LocalTrackPublication) -> Self {
|
||||
unsafe {
|
||||
CFRetain(native_track_publication.0);
|
||||
}
|
||||
Self(native_track_publication)
|
||||
}
|
||||
|
||||
pub fn sid(&self) -> String {
|
||||
unsafe { CFString::wrap_under_get_rule(LKLocalTrackPublicationGetSid(self.0)).to_string() }
|
||||
}
|
||||
|
||||
pub fn set_mute(&self, muted: bool) -> impl Future<Output = Result<()>> {
|
||||
let (tx, rx) = futures::channel::oneshot::channel();
|
||||
|
||||
extern "C" fn complete_callback(callback_data: *mut c_void, error: CFStringRef) {
|
||||
let tx = unsafe { Box::from_raw(callback_data as *mut oneshot::Sender<Result<()>>) };
|
||||
if error.is_null() {
|
||||
tx.send(Ok(())).ok();
|
||||
} else {
|
||||
let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
|
||||
tx.send(Err(anyhow!(error))).ok();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
LKLocalTrackPublicationSetMute(
|
||||
self.0,
|
||||
muted,
|
||||
complete_callback,
|
||||
Box::into_raw(Box::new(tx)) as *mut c_void,
|
||||
)
|
||||
}
|
||||
|
||||
async move { rx.await.unwrap() }
|
||||
}
|
||||
|
||||
pub fn is_muted(&self) -> bool {
|
||||
unsafe { LKLocalTrackPublicationIsMuted(self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for LocalTrackPublication {
|
||||
fn clone(&self) -> Self {
|
||||
unsafe {
|
||||
CFRetain(self.0 .0);
|
||||
}
|
||||
Self(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for LocalTrackPublication {
|
||||
fn drop(&mut self) {
|
||||
unsafe { CFRelease(self.0 .0) }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RemoteTrackPublication(swift::RemoteTrackPublication);
|
||||
|
||||
impl RemoteTrackPublication {
|
||||
pub fn new(native_track_publication: swift::RemoteTrackPublication) -> Self {
|
||||
unsafe {
|
||||
CFRetain(native_track_publication.0);
|
||||
}
|
||||
Self(native_track_publication)
|
||||
}
|
||||
|
||||
pub fn sid(&self) -> String {
|
||||
unsafe { CFString::wrap_under_get_rule(LKRemoteTrackPublicationGetSid(self.0)).to_string() }
|
||||
}
|
||||
|
||||
pub fn is_muted(&self) -> bool {
|
||||
unsafe { LKRemoteTrackPublicationIsMuted(self.0) }
|
||||
}
|
||||
|
||||
pub fn set_enabled(&self, enabled: bool) -> impl Future<Output = Result<()>> {
|
||||
let (tx, rx) = futures::channel::oneshot::channel();
|
||||
|
||||
extern "C" fn complete_callback(callback_data: *mut c_void, error: CFStringRef) {
|
||||
let tx = unsafe { Box::from_raw(callback_data as *mut oneshot::Sender<Result<()>>) };
|
||||
if error.is_null() {
|
||||
tx.send(Ok(())).ok();
|
||||
} else {
|
||||
let error = unsafe { CFString::wrap_under_get_rule(error).to_string() };
|
||||
tx.send(Err(anyhow!(error))).ok();
|
||||
}
|
||||
}
|
||||
|
||||
unsafe {
|
||||
LKRemoteTrackPublicationSetEnabled(
|
||||
self.0,
|
||||
enabled,
|
||||
complete_callback,
|
||||
Box::into_raw(Box::new(tx)) as *mut c_void,
|
||||
)
|
||||
}
|
||||
|
||||
async move { rx.await.unwrap() }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RemoteTrackPublication {
|
||||
fn drop(&mut self) {
|
||||
unsafe { CFRelease(self.0 .0) }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RemoteAudioTrack {
|
||||
native_track: swift::RemoteAudioTrack,
|
||||
sid: Sid,
|
||||
publisher_id: String,
|
||||
}
|
||||
|
||||
impl RemoteAudioTrack {
|
||||
fn new(native_track: swift::RemoteAudioTrack, sid: Sid, publisher_id: String) -> Self {
|
||||
unsafe {
|
||||
CFRetain(native_track.0);
|
||||
}
|
||||
Self {
|
||||
native_track,
|
||||
sid,
|
||||
publisher_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sid(&self) -> &str {
|
||||
&self.sid
|
||||
}
|
||||
|
||||
pub fn publisher_id(&self) -> &str {
|
||||
&self.publisher_id
|
||||
}
|
||||
|
||||
pub fn start(&self) {
|
||||
unsafe { LKRemoteAudioTrackStart(self.native_track) }
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
unsafe { LKRemoteAudioTrackStop(self.native_track) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RemoteAudioTrack {
|
||||
fn drop(&mut self) {
|
||||
// todo: uncomment this `CFRelease`, unless we find that it was causing
|
||||
// the crash in the `livekit.multicast` thread.
|
||||
//
|
||||
// unsafe { CFRelease(self.native_track.0) }
|
||||
let _ = self.native_track;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RemoteVideoTrack {
|
||||
native_track: swift::RemoteVideoTrack,
|
||||
sid: Sid,
|
||||
publisher_id: String,
|
||||
}
|
||||
|
||||
impl RemoteVideoTrack {
|
||||
fn new(native_track: swift::RemoteVideoTrack, sid: Sid, publisher_id: String) -> Self {
|
||||
unsafe {
|
||||
CFRetain(native_track.0);
|
||||
}
|
||||
Self {
|
||||
native_track,
|
||||
sid,
|
||||
publisher_id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sid(&self) -> &str {
|
||||
&self.sid
|
||||
}
|
||||
|
||||
pub fn publisher_id(&self) -> &str {
|
||||
&self.publisher_id
|
||||
}
|
||||
|
||||
pub fn frames(&self) -> async_broadcast::Receiver<Frame> {
|
||||
extern "C" fn on_frame(callback_data: *mut c_void, frame: CVImageBufferRef) -> bool {
|
||||
unsafe {
|
||||
let tx = Box::from_raw(callback_data as *mut async_broadcast::Sender<Frame>);
|
||||
let buffer = CVImageBuffer::wrap_under_get_rule(frame);
|
||||
let result = tx.try_broadcast(Frame(buffer));
|
||||
let _ = Box::into_raw(tx);
|
||||
match result {
|
||||
Ok(_) => true,
|
||||
Err(async_broadcast::TrySendError::Closed(_))
|
||||
| Err(async_broadcast::TrySendError::Inactive(_)) => {
|
||||
log::warn!("no active receiver for frame");
|
||||
false
|
||||
}
|
||||
Err(async_broadcast::TrySendError::Full(_)) => {
|
||||
log::warn!("skipping frame as receiver is not keeping up");
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn on_drop(callback_data: *mut c_void) {
|
||||
unsafe {
|
||||
let _ = Box::from_raw(callback_data as *mut async_broadcast::Sender<Frame>);
|
||||
}
|
||||
}
|
||||
|
||||
let (tx, rx) = async_broadcast::broadcast(64);
|
||||
unsafe {
|
||||
let renderer = LKVideoRendererCreate(
|
||||
Box::into_raw(Box::new(tx)) as *mut c_void,
|
||||
on_frame,
|
||||
on_drop,
|
||||
);
|
||||
LKVideoTrackAddRenderer(self.native_track, renderer);
|
||||
rx
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RemoteVideoTrack {
|
||||
fn drop(&mut self) {
|
||||
unsafe { CFRelease(self.native_track.0) }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MacOSDisplay(swift::MacOSDisplay);
|
||||
|
||||
impl MacOSDisplay {
|
||||
fn new(ptr: swift::MacOSDisplay) -> Self {
|
||||
unsafe {
|
||||
CFRetain(ptr.0);
|
||||
}
|
||||
Self(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for MacOSDisplay {
|
||||
fn drop(&mut self) {
|
||||
unsafe { CFRelease(self.0 .0) }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Frame(CVImageBuffer);
|
||||
|
||||
impl Frame {
|
||||
pub fn width(&self) -> usize {
|
||||
self.0.width()
|
||||
}
|
||||
|
||||
pub fn height(&self) -> usize {
|
||||
self.0.height()
|
||||
}
|
||||
|
||||
pub fn image(&self) -> CVImageBuffer {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
882
crates/livekit_client_macos/src/test.rs
Normal file
882
crates/livekit_client_macos/src/test.rs
Normal file
|
@ -0,0 +1,882 @@
|
|||
use crate::{ConnectionState, RoomUpdate, Sid};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use async_trait::async_trait;
|
||||
use collections::{btree_map::Entry as BTreeEntry, hash_map::Entry, BTreeMap, HashMap, HashSet};
|
||||
use futures::Stream;
|
||||
use gpui::{BackgroundExecutor, SurfaceSource};
|
||||
use livekit_server::{proto, token};
|
||||
|
||||
use parking_lot::Mutex;
|
||||
use postage::watch;
|
||||
use std::{
|
||||
future::Future,
|
||||
mem,
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering::SeqCst},
|
||||
Arc, Weak,
|
||||
},
|
||||
};
|
||||
|
||||
static SERVERS: Mutex<BTreeMap<String, Arc<TestServer>>> = Mutex::new(BTreeMap::new());
|
||||
|
||||
pub struct TestServer {
|
||||
pub url: String,
|
||||
pub api_key: String,
|
||||
pub secret_key: String,
|
||||
rooms: Mutex<HashMap<String, TestServerRoom>>,
|
||||
executor: BackgroundExecutor,
|
||||
}
|
||||
|
||||
impl TestServer {
|
||||
pub fn create(
|
||||
url: String,
|
||||
api_key: String,
|
||||
secret_key: String,
|
||||
executor: BackgroundExecutor,
|
||||
) -> Result<Arc<TestServer>> {
|
||||
let mut servers = SERVERS.lock();
|
||||
if let BTreeEntry::Vacant(e) = servers.entry(url.clone()) {
|
||||
let server = Arc::new(TestServer {
|
||||
url,
|
||||
api_key,
|
||||
secret_key,
|
||||
rooms: Default::default(),
|
||||
executor,
|
||||
});
|
||||
e.insert(server.clone());
|
||||
Ok(server)
|
||||
} else {
|
||||
Err(anyhow!("a server with url {:?} already exists", url))
|
||||
}
|
||||
}
|
||||
|
||||
fn get(url: &str) -> Result<Arc<TestServer>> {
|
||||
Ok(SERVERS
|
||||
.lock()
|
||||
.get(url)
|
||||
.ok_or_else(|| anyhow!("no server found for url"))?
|
||||
.clone())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) -> Result<()> {
|
||||
SERVERS
|
||||
.lock()
|
||||
.remove(&self.url)
|
||||
.ok_or_else(|| anyhow!("server with url {:?} does not exist", self.url))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn create_api_client(&self) -> TestApiClient {
|
||||
TestApiClient {
|
||||
url: self.url.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_room(&self, room: String) -> Result<()> {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
if let Entry::Vacant(e) = server_rooms.entry(room.clone()) {
|
||||
e.insert(Default::default());
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!("room {:?} already exists", room))
|
||||
}
|
||||
}
|
||||
|
||||
async fn delete_room(&self, room: String) -> Result<()> {
|
||||
// TODO: clear state associated with all `Room`s.
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
server_rooms
|
||||
.remove(&room)
|
||||
.ok_or_else(|| anyhow!("room {:?} does not exist", room))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn join_room(&self, token: String, client_room: Arc<Room>) -> Result<()> {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
|
||||
let claims = livekit_server::token::validate(&token, &self.secret_key)?;
|
||||
let identity = claims.sub.unwrap().to_string();
|
||||
let room_name = claims.video.room.unwrap();
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = (*server_rooms).entry(room_name.to_string()).or_default();
|
||||
|
||||
if let Entry::Vacant(e) = room.client_rooms.entry(identity.clone()) {
|
||||
for track in &room.video_tracks {
|
||||
client_room
|
||||
.0
|
||||
.lock()
|
||||
.updates_tx
|
||||
.try_broadcast(RoomUpdate::SubscribedToRemoteVideoTrack(Arc::new(
|
||||
RemoteVideoTrack {
|
||||
server_track: track.clone(),
|
||||
},
|
||||
)))
|
||||
.unwrap();
|
||||
}
|
||||
for track in &room.audio_tracks {
|
||||
client_room
|
||||
.0
|
||||
.lock()
|
||||
.updates_tx
|
||||
.try_broadcast(RoomUpdate::SubscribedToRemoteAudioTrack(
|
||||
Arc::new(RemoteAudioTrack {
|
||||
server_track: track.clone(),
|
||||
room: Arc::downgrade(&client_room),
|
||||
}),
|
||||
Arc::new(RemoteTrackPublication),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
e.insert(client_room);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"{:?} attempted to join room {:?} twice",
|
||||
identity,
|
||||
room_name
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
async fn leave_room(&self, token: String) -> Result<()> {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
let claims = livekit_server::token::validate(&token, &self.secret_key)?;
|
||||
let identity = claims.sub.unwrap().to_string();
|
||||
let room_name = claims.video.room.unwrap();
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
room.client_rooms.remove(&identity).ok_or_else(|| {
|
||||
anyhow!(
|
||||
"{:?} attempted to leave room {:?} before joining it",
|
||||
identity,
|
||||
room_name
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_participant(&self, room_name: String, identity: String) -> Result<()> {
|
||||
// TODO: clear state associated with the `Room`.
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
room.client_rooms.remove(&identity).ok_or_else(|| {
|
||||
anyhow!(
|
||||
"participant {:?} did not join room {:?}",
|
||||
identity,
|
||||
room_name
|
||||
)
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_participant(
|
||||
&self,
|
||||
room_name: String,
|
||||
identity: String,
|
||||
permission: proto::ParticipantPermission,
|
||||
) -> Result<()> {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
room.participant_permissions.insert(identity, permission);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn disconnect_client(&self, client_identity: String) {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
for room in server_rooms.values_mut() {
|
||||
if let Some(room) = room.client_rooms.remove(&client_identity) {
|
||||
*room.0.lock().connection.0.borrow_mut() = ConnectionState::Disconnected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn publish_video_track(
|
||||
&self,
|
||||
token: String,
|
||||
local_track: LocalVideoTrack,
|
||||
) -> Result<Sid> {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
let claims = livekit_server::token::validate(&token, &self.secret_key)?;
|
||||
let identity = claims.sub.unwrap().to_string();
|
||||
let room_name = claims.video.room.unwrap();
|
||||
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
|
||||
let can_publish = room
|
||||
.participant_permissions
|
||||
.get(&identity)
|
||||
.map(|permission| permission.can_publish)
|
||||
.or(claims.video.can_publish)
|
||||
.unwrap_or(true);
|
||||
|
||||
if !can_publish {
|
||||
return Err(anyhow!("user is not allowed to publish"));
|
||||
}
|
||||
|
||||
let sid = nanoid::nanoid!(17);
|
||||
let track = Arc::new(TestServerVideoTrack {
|
||||
sid: sid.clone(),
|
||||
publisher_id: identity.clone(),
|
||||
frames_rx: local_track.frames_rx.clone(),
|
||||
});
|
||||
|
||||
room.video_tracks.push(track.clone());
|
||||
|
||||
for (id, client_room) in &room.client_rooms {
|
||||
if *id != identity {
|
||||
let _ = client_room
|
||||
.0
|
||||
.lock()
|
||||
.updates_tx
|
||||
.try_broadcast(RoomUpdate::SubscribedToRemoteVideoTrack(Arc::new(
|
||||
RemoteVideoTrack {
|
||||
server_track: track.clone(),
|
||||
},
|
||||
)))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(sid)
|
||||
}
|
||||
|
||||
async fn publish_audio_track(
|
||||
&self,
|
||||
token: String,
|
||||
_local_track: &LocalAudioTrack,
|
||||
) -> Result<Sid> {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
self.executor.simulate_random_delay().await;
|
||||
|
||||
let claims = livekit_server::token::validate(&token, &self.secret_key)?;
|
||||
let identity = claims.sub.unwrap().to_string();
|
||||
let room_name = claims.video.room.unwrap();
|
||||
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
|
||||
let can_publish = room
|
||||
.participant_permissions
|
||||
.get(&identity)
|
||||
.map(|permission| permission.can_publish)
|
||||
.or(claims.video.can_publish)
|
||||
.unwrap_or(true);
|
||||
|
||||
if !can_publish {
|
||||
return Err(anyhow!("user is not allowed to publish"));
|
||||
}
|
||||
|
||||
let sid = nanoid::nanoid!(17);
|
||||
let track = Arc::new(TestServerAudioTrack {
|
||||
sid: sid.clone(),
|
||||
publisher_id: identity.clone(),
|
||||
muted: AtomicBool::new(false),
|
||||
});
|
||||
|
||||
let publication = Arc::new(RemoteTrackPublication);
|
||||
|
||||
room.audio_tracks.push(track.clone());
|
||||
|
||||
for (id, client_room) in &room.client_rooms {
|
||||
if *id != identity {
|
||||
let _ = client_room
|
||||
.0
|
||||
.lock()
|
||||
.updates_tx
|
||||
.try_broadcast(RoomUpdate::SubscribedToRemoteAudioTrack(
|
||||
Arc::new(RemoteAudioTrack {
|
||||
server_track: track.clone(),
|
||||
room: Arc::downgrade(client_room),
|
||||
}),
|
||||
publication.clone(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(sid)
|
||||
}
|
||||
|
||||
fn set_track_muted(&self, token: &str, track_sid: &str, muted: bool) -> Result<()> {
|
||||
let claims = livekit_server::token::validate(token, &self.secret_key)?;
|
||||
let room_name = claims.video.room.unwrap();
|
||||
let identity = claims.sub.unwrap();
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
if let Some(track) = room
|
||||
.audio_tracks
|
||||
.iter_mut()
|
||||
.find(|track| track.sid == track_sid)
|
||||
{
|
||||
track.muted.store(muted, SeqCst);
|
||||
for (id, client_room) in room.client_rooms.iter() {
|
||||
if *id != identity {
|
||||
client_room
|
||||
.0
|
||||
.lock()
|
||||
.updates_tx
|
||||
.try_broadcast(RoomUpdate::RemoteAudioTrackMuteChanged {
|
||||
track_id: track_sid.to_string(),
|
||||
muted,
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_track_muted(&self, token: &str, track_sid: &str) -> Option<bool> {
|
||||
let claims = livekit_server::token::validate(token, &self.secret_key).ok()?;
|
||||
let room_name = claims.video.room.unwrap();
|
||||
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms.get_mut(&*room_name)?;
|
||||
room.audio_tracks.iter().find_map(|track| {
|
||||
if track.sid == track_sid {
|
||||
Some(track.muted.load(SeqCst))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn video_tracks(&self, token: String) -> Result<Vec<Arc<RemoteVideoTrack>>> {
|
||||
let claims = livekit_server::token::validate(&token, &self.secret_key)?;
|
||||
let room_name = claims.video.room.unwrap();
|
||||
let identity = claims.sub.unwrap();
|
||||
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
room.client_rooms
|
||||
.get(identity.as_ref())
|
||||
.ok_or_else(|| anyhow!("not a participant in room"))?;
|
||||
Ok(room
|
||||
.video_tracks
|
||||
.iter()
|
||||
.map(|track| {
|
||||
Arc::new(RemoteVideoTrack {
|
||||
server_track: track.clone(),
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn audio_tracks(&self, token: String) -> Result<Vec<Arc<RemoteAudioTrack>>> {
|
||||
let claims = livekit_server::token::validate(&token, &self.secret_key)?;
|
||||
let room_name = claims.video.room.unwrap();
|
||||
let identity = claims.sub.unwrap();
|
||||
|
||||
let mut server_rooms = self.rooms.lock();
|
||||
let room = server_rooms
|
||||
.get_mut(&*room_name)
|
||||
.ok_or_else(|| anyhow!("room {} does not exist", room_name))?;
|
||||
let client_room = room
|
||||
.client_rooms
|
||||
.get(identity.as_ref())
|
||||
.ok_or_else(|| anyhow!("not a participant in room"))?;
|
||||
Ok(room
|
||||
.audio_tracks
|
||||
.iter()
|
||||
.map(|track| {
|
||||
Arc::new(RemoteAudioTrack {
|
||||
server_track: track.clone(),
|
||||
room: Arc::downgrade(client_room),
|
||||
})
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct TestServerRoom {
|
||||
client_rooms: HashMap<Sid, Arc<Room>>,
|
||||
video_tracks: Vec<Arc<TestServerVideoTrack>>,
|
||||
audio_tracks: Vec<Arc<TestServerAudioTrack>>,
|
||||
participant_permissions: HashMap<Sid, proto::ParticipantPermission>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct TestServerVideoTrack {
|
||||
sid: Sid,
|
||||
publisher_id: Sid,
|
||||
frames_rx: async_broadcast::Receiver<Frame>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct TestServerAudioTrack {
|
||||
sid: Sid,
|
||||
publisher_id: Sid,
|
||||
muted: AtomicBool,
|
||||
}
|
||||
|
||||
impl TestServerRoom {}
|
||||
|
||||
pub struct TestApiClient {
|
||||
url: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl livekit_server::api::Client for TestApiClient {
|
||||
fn url(&self) -> &str {
|
||||
&self.url
|
||||
}
|
||||
|
||||
async fn create_room(&self, name: String) -> Result<()> {
|
||||
let server = TestServer::get(&self.url)?;
|
||||
server.create_room(name).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn delete_room(&self, name: String) -> Result<()> {
|
||||
let server = TestServer::get(&self.url)?;
|
||||
server.delete_room(name).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn remove_participant(&self, room: String, identity: String) -> Result<()> {
|
||||
let server = TestServer::get(&self.url)?;
|
||||
server.remove_participant(room, identity).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_participant(
|
||||
&self,
|
||||
room: String,
|
||||
identity: String,
|
||||
permission: livekit_server::proto::ParticipantPermission,
|
||||
) -> Result<()> {
|
||||
let server = TestServer::get(&self.url)?;
|
||||
server
|
||||
.update_participant(room, identity, permission)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn room_token(&self, room: &str, identity: &str) -> Result<String> {
|
||||
let server = TestServer::get(&self.url)?;
|
||||
token::create(
|
||||
&server.api_key,
|
||||
&server.secret_key,
|
||||
Some(identity),
|
||||
token::VideoGrant::to_join(room),
|
||||
)
|
||||
}
|
||||
|
||||
fn guest_token(&self, room: &str, identity: &str) -> Result<String> {
|
||||
let server = TestServer::get(&self.url)?;
|
||||
token::create(
|
||||
&server.api_key,
|
||||
&server.secret_key,
|
||||
Some(identity),
|
||||
token::VideoGrant::for_guest(room),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct RoomState {
|
||||
connection: (
|
||||
watch::Sender<ConnectionState>,
|
||||
watch::Receiver<ConnectionState>,
|
||||
),
|
||||
display_sources: Vec<MacOSDisplay>,
|
||||
paused_audio_tracks: HashSet<Sid>,
|
||||
updates_tx: async_broadcast::Sender<RoomUpdate>,
|
||||
updates_rx: async_broadcast::Receiver<RoomUpdate>,
|
||||
}
|
||||
|
||||
pub struct Room(Mutex<RoomState>);
|
||||
|
||||
impl Room {
|
||||
pub fn new() -> Arc<Self> {
|
||||
let (updates_tx, updates_rx) = async_broadcast::broadcast(128);
|
||||
Arc::new(Self(Mutex::new(RoomState {
|
||||
connection: watch::channel_with(ConnectionState::Disconnected),
|
||||
display_sources: Default::default(),
|
||||
paused_audio_tracks: Default::default(),
|
||||
updates_tx,
|
||||
updates_rx,
|
||||
})))
|
||||
}
|
||||
|
||||
pub fn status(&self) -> watch::Receiver<ConnectionState> {
|
||||
self.0.lock().connection.1.clone()
|
||||
}
|
||||
|
||||
pub fn connect(self: &Arc<Self>, url: &str, token: &str) -> impl Future<Output = Result<()>> {
|
||||
let this = self.clone();
|
||||
let url = url.to_string();
|
||||
let token = token.to_string();
|
||||
async move {
|
||||
let server = TestServer::get(&url)?;
|
||||
server
|
||||
.join_room(token.clone(), this.clone())
|
||||
.await
|
||||
.context("room join")?;
|
||||
*this.0.lock().connection.0.borrow_mut() = ConnectionState::Connected { url, token };
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn display_sources(self: &Arc<Self>) -> impl Future<Output = Result<Vec<MacOSDisplay>>> {
|
||||
let this = self.clone();
|
||||
async move {
|
||||
// todo(linux): Remove this once the cross-platform LiveKit implementation is merged
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
{
|
||||
let server = this.test_server();
|
||||
server.executor.simulate_random_delay().await;
|
||||
}
|
||||
|
||||
Ok(this.0.lock().display_sources.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn publish_video_track(
|
||||
self: &Arc<Self>,
|
||||
track: LocalVideoTrack,
|
||||
) -> impl Future<Output = Result<LocalTrackPublication>> {
|
||||
let this = self.clone();
|
||||
let track = track.clone();
|
||||
async move {
|
||||
let sid = this
|
||||
.test_server()
|
||||
.publish_video_track(this.token(), track)
|
||||
.await?;
|
||||
Ok(LocalTrackPublication {
|
||||
room: Arc::downgrade(&this),
|
||||
sid,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn publish_audio_track(
|
||||
self: &Arc<Self>,
|
||||
track: LocalAudioTrack,
|
||||
) -> impl Future<Output = Result<LocalTrackPublication>> {
|
||||
let this = self.clone();
|
||||
let track = track.clone();
|
||||
async move {
|
||||
let sid = this
|
||||
.test_server()
|
||||
.publish_audio_track(this.token(), &track)
|
||||
.await?;
|
||||
Ok(LocalTrackPublication {
|
||||
room: Arc::downgrade(&this),
|
||||
sid,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unpublish_track(&self, _publication: LocalTrackPublication) {}
|
||||
|
||||
pub fn remote_audio_tracks(&self, publisher_id: &str) -> Vec<Arc<RemoteAudioTrack>> {
|
||||
if !self.is_connected() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
self.test_server()
|
||||
.audio_tracks(self.token())
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|track| track.publisher_id() == publisher_id)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn remote_audio_track_publications(
|
||||
&self,
|
||||
publisher_id: &str,
|
||||
) -> Vec<Arc<RemoteTrackPublication>> {
|
||||
if !self.is_connected() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
self.test_server()
|
||||
.audio_tracks(self.token())
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|track| track.publisher_id() == publisher_id)
|
||||
.map(|_track| Arc::new(RemoteTrackPublication {}))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn remote_video_tracks(&self, publisher_id: &str) -> Vec<Arc<RemoteVideoTrack>> {
|
||||
if !self.is_connected() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
self.test_server()
|
||||
.video_tracks(self.token())
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.filter(|track| track.publisher_id() == publisher_id)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn updates(&self) -> impl Stream<Item = RoomUpdate> {
|
||||
self.0.lock().updates_rx.clone()
|
||||
}
|
||||
|
||||
pub fn set_display_sources(&self, sources: Vec<MacOSDisplay>) {
|
||||
self.0.lock().display_sources = sources;
|
||||
}
|
||||
|
||||
fn test_server(&self) -> Arc<TestServer> {
|
||||
match self.0.lock().connection.1.borrow().clone() {
|
||||
ConnectionState::Disconnected => panic!("must be connected to call this method"),
|
||||
ConnectionState::Connected { url, .. } => TestServer::get(&url).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
fn token(&self) -> String {
|
||||
match self.0.lock().connection.1.borrow().clone() {
|
||||
ConnectionState::Disconnected => panic!("must be connected to call this method"),
|
||||
ConnectionState::Connected { token, .. } => token,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_connected(&self) -> bool {
|
||||
match *self.0.lock().connection.1.borrow() {
|
||||
ConnectionState::Disconnected => false,
|
||||
ConnectionState::Connected { .. } => true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Room {
|
||||
fn drop(&mut self) {
|
||||
if let ConnectionState::Connected { token, .. } = mem::replace(
|
||||
&mut *self.0.lock().connection.0.borrow_mut(),
|
||||
ConnectionState::Disconnected,
|
||||
) {
|
||||
if let Ok(server) = TestServer::get(&token) {
|
||||
let executor = server.executor.clone();
|
||||
executor
|
||||
.spawn(async move { server.leave_room(token).await.unwrap() })
|
||||
.detach();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LocalTrackPublication {
|
||||
sid: String,
|
||||
room: Weak<Room>,
|
||||
}
|
||||
|
||||
impl LocalTrackPublication {
|
||||
pub fn set_mute(&self, mute: bool) -> impl Future<Output = Result<()>> {
|
||||
let sid = self.sid.clone();
|
||||
let room = self.room.clone();
|
||||
async move {
|
||||
if let Some(room) = room.upgrade() {
|
||||
room.test_server()
|
||||
.set_track_muted(&room.token(), &sid, mute)
|
||||
} else {
|
||||
Err(anyhow!("no such room"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_muted(&self) -> bool {
|
||||
if let Some(room) = self.room.upgrade() {
|
||||
room.test_server()
|
||||
.is_track_muted(&room.token(), &self.sid)
|
||||
.unwrap_or(false)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sid(&self) -> String {
|
||||
self.sid.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct RemoteTrackPublication;
|
||||
|
||||
impl RemoteTrackPublication {
|
||||
pub fn set_enabled(&self, _enabled: bool) -> impl Future<Output = Result<()>> {
|
||||
async { Ok(()) }
|
||||
}
|
||||
|
||||
pub fn is_muted(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
pub fn sid(&self) -> String {
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LocalVideoTrack {
|
||||
frames_rx: async_broadcast::Receiver<Frame>,
|
||||
}
|
||||
|
||||
impl LocalVideoTrack {
|
||||
pub fn screen_share_for_display(display: &MacOSDisplay) -> Self {
|
||||
Self {
|
||||
frames_rx: display.frames.1.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LocalAudioTrack;
|
||||
|
||||
impl LocalAudioTrack {
|
||||
pub fn create() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RemoteVideoTrack {
|
||||
server_track: Arc<TestServerVideoTrack>,
|
||||
}
|
||||
|
||||
impl RemoteVideoTrack {
|
||||
pub fn sid(&self) -> &str {
|
||||
&self.server_track.sid
|
||||
}
|
||||
|
||||
pub fn publisher_id(&self) -> &str {
|
||||
&self.server_track.publisher_id
|
||||
}
|
||||
|
||||
pub fn frames(&self) -> async_broadcast::Receiver<Frame> {
|
||||
self.server_track.frames_rx.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RemoteAudioTrack {
|
||||
server_track: Arc<TestServerAudioTrack>,
|
||||
room: Weak<Room>,
|
||||
}
|
||||
|
||||
impl RemoteAudioTrack {
|
||||
pub fn sid(&self) -> &str {
|
||||
&self.server_track.sid
|
||||
}
|
||||
|
||||
pub fn publisher_id(&self) -> &str {
|
||||
&self.server_track.publisher_id
|
||||
}
|
||||
|
||||
pub fn start(&self) {
|
||||
if let Some(room) = self.room.upgrade() {
|
||||
room.0
|
||||
.lock()
|
||||
.paused_audio_tracks
|
||||
.remove(&self.server_track.sid);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn stop(&self) {
|
||||
if let Some(room) = self.room.upgrade() {
|
||||
room.0
|
||||
.lock()
|
||||
.paused_audio_tracks
|
||||
.insert(self.server_track.sid.clone());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_playing(&self) -> bool {
|
||||
!self
|
||||
.room
|
||||
.upgrade()
|
||||
.unwrap()
|
||||
.0
|
||||
.lock()
|
||||
.paused_audio_tracks
|
||||
.contains(&self.server_track.sid)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MacOSDisplay {
|
||||
frames: (
|
||||
async_broadcast::Sender<Frame>,
|
||||
async_broadcast::Receiver<Frame>,
|
||||
),
|
||||
}
|
||||
|
||||
impl Default for MacOSDisplay {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl MacOSDisplay {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
frames: async_broadcast::broadcast(128),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_frame(&self, frame: Frame) {
|
||||
self.frames.0.try_broadcast(frame).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Frame {
|
||||
pub label: String,
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
}
|
||||
|
||||
impl Frame {
|
||||
pub fn width(&self) -> usize {
|
||||
self.width
|
||||
}
|
||||
|
||||
pub fn height(&self) -> usize {
|
||||
self.height
|
||||
}
|
||||
|
||||
pub fn image(&self) -> SurfaceSource {
|
||||
unimplemented!("you can't call this in test mode")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue