From 0d393eba5dced01bc78428a92cd49aaf4f03f705 Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Wed, 10 Jan 2024 15:25:07 +0900 Subject: [PATCH] Simplify `publish(data:options:)` method (#297) --- .../LiveKit/Core/DataChannelPairActor.swift | 6 +++--- Sources/LiveKit/Core/Engine.swift | 4 ++-- .../LiveKit/Participant/LocalParticipant.swift | 18 +++++------------- .../Types/Options/DataPublishOptions.swift | 16 ++++++++++++++-- Sources/LiveKit/Types/Other.swift | 13 ------------- 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/Sources/LiveKit/Core/DataChannelPairActor.swift b/Sources/LiveKit/Core/DataChannelPairActor.swift index c41cbf953..4dfce82cf 100644 --- a/Sources/LiveKit/Core/DataChannelPairActor.swift +++ b/Sources/LiveKit/Core/DataChannelPairActor.swift @@ -74,20 +74,20 @@ actor DataChannelPairActor: NSObject, Loggable { openCompleter.reset() } - public func send(userPacket: Livekit_UserPacket, reliability: Reliability) throws { + public func send(userPacket: Livekit_UserPacket, kind: Livekit_DataPacket.Kind) throws { guard isOpen else { throw LiveKitError(.invalidState, message: "Data channel is not open") } let packet = Livekit_DataPacket.with { - $0.kind = reliability.toPBType() + $0.kind = kind $0.user = userPacket } let serializedData = try packet.serializedData() let rtcData = Engine.createDataBuffer(data: serializedData) - let channel = (reliability == .reliable) ? _reliableChannel : _lossyChannel + let channel = (kind == .reliable) ? _reliableChannel : _lossyChannel guard let sendDataResult = channel?.sendData(rtcData), sendDataResult else { throw LiveKitError(.invalidState, message: "sendData failed") } diff --git a/Sources/LiveKit/Core/Engine.swift b/Sources/LiveKit/Core/Engine.swift index 8e9754c30..78eed9679 100644 --- a/Sources/LiveKit/Core/Engine.swift +++ b/Sources/LiveKit/Core/Engine.swift @@ -203,7 +203,7 @@ class Engine: MulticastDelegate { _state.mutate { $0.hasPublished = true } } - func send(userPacket: Livekit_UserPacket, reliability: Reliability = .reliable) async throws { + func send(userPacket: Livekit_UserPacket, kind: Livekit_DataPacket.Kind) async throws { func ensurePublisherConnected() async throws { guard subscriberPrimary else { return } @@ -225,7 +225,7 @@ class Engine: MulticastDelegate { assert(dataChannelIsOpen, "publisher data channel is not .open") // Should return true if successful - try await publisherDataChannel.send(userPacket: userPacket, reliability: reliability) + try await publisherDataChannel.send(userPacket: userPacket, kind: kind) } } diff --git a/Sources/LiveKit/Participant/LocalParticipant.swift b/Sources/LiveKit/Participant/LocalParticipant.swift index 027bf3cf4..10b665601 100644 --- a/Sources/LiveKit/Participant/LocalParticipant.swift +++ b/Sources/LiveKit/Participant/LocalParticipant.swift @@ -309,27 +309,19 @@ public class LocalParticipant: Participant { /// Data is forwarded to each participant in the room. Each payload must not exceed 15k. /// - Parameters: /// - data: Data to send - /// - reliability: Toggle between sending relialble vs lossy delivery. - /// For data that you need delivery guarantee (such as chat messages), use Reliable. - /// For data that should arrive as quickly as possible, but you are ok with dropped packets, use Lossy. - /// - destinations: SIDs of the participants who will receive the message. If empty, deliver to everyone + /// - options: Provide options with a ``DataPublishOptions`` class. @objc - public func publish(data: Data, - reliability: Reliability = .reliable, - destinationIdentities: [Identity]? = nil, - topic: String? = nil, - options: DataPublishOptions? = nil) async throws - { + public func publish(data: Data, options: DataPublishOptions? = nil) async throws { let options = options ?? room._state.options.defaultDataPublishOptions let userPacket = Livekit_UserPacket.with { $0.participantSid = self.sid $0.payload = data - $0.destinationIdentities = destinationIdentities ?? options.destinationIdentities - $0.topic = topic ?? options.topic ?? "" + $0.destinationIdentities = options.destinationIdentities + $0.topic = options.topic ?? "" } - try await room.engine.send(userPacket: userPacket, reliability: reliability) + try await room.engine.send(userPacket: userPacket, kind: options.reliable ? .reliable : .lossy) } /** diff --git a/Sources/LiveKit/Types/Options/DataPublishOptions.swift b/Sources/LiveKit/Types/Options/DataPublishOptions.swift index 7d8a0890c..f7b75a0cb 100644 --- a/Sources/LiveKit/Types/Options/DataPublishOptions.swift +++ b/Sources/LiveKit/Types/Options/DataPublishOptions.swift @@ -21,19 +21,29 @@ public class DataPublishOptions: NSObject, PublishOptions { @objc public let name: String? + /// The identities of participants who will receive the message, will be sent to every one if empty. @objc public let destinationIdentities: [Identity] + /// The topic under which the message gets published. @objc public let topic: String? + /// Whether to send this as reliable or lossy. + /// For data that you need delivery guarantee (such as chat messages) set to true (reliable). + /// For data that should arrive as quickly as possible, but you are ok with dropped packets, set to false (lossy). + @objc + public let reliable: Bool + public init(name: String? = nil, destinationIdentities: [Identity] = [], - topic: String? = nil) + topic: String? = nil, + reliable: Bool = false) { self.name = name self.destinationIdentities = destinationIdentities self.topic = topic + self.reliable = reliable } // MARK: - Equal @@ -42,7 +52,8 @@ public class DataPublishOptions: NSObject, PublishOptions { guard let other = object as? Self else { return false } return name == other.name && destinationIdentities == other.destinationIdentities && - topic == other.topic + topic == other.topic && + reliable == other.reliable } override public var hash: Int { @@ -50,6 +61,7 @@ public class DataPublishOptions: NSObject, PublishOptions { hasher.combine(name) hasher.combine(destinationIdentities) hasher.combine(topic) + hasher.combine(reliable) return hasher.finalize() } } diff --git a/Sources/LiveKit/Types/Other.swift b/Sources/LiveKit/Types/Other.swift index b9ba71c14..4ff8af27a 100644 --- a/Sources/LiveKit/Types/Other.swift +++ b/Sources/LiveKit/Types/Other.swift @@ -21,19 +21,6 @@ import Foundation public typealias Sid = String public typealias Identity = String -@objc -public enum Reliability: Int { - case reliable - case lossy -} - -extension Reliability { - func toPBType() -> Livekit_DataPacket.Kind { - if self == .lossy { return .lossy } - return .reliable - } -} - public enum SimulateScenario { case nodeFailure case migration