Skip to content

Commit

Permalink
Simplify publish(data:options:) method (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie authored Jan 10, 2024
1 parent 7ae1aa5 commit 0d393eb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 33 deletions.
6 changes: 3 additions & 3 deletions Sources/LiveKit/Core/DataChannelPairActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/LiveKit/Core/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Engine: MulticastDelegate<EngineDelegate> {
_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 }

Expand All @@ -225,7 +225,7 @@ class Engine: MulticastDelegate<EngineDelegate> {
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)
}
}

Expand Down
18 changes: 5 additions & 13 deletions Sources/LiveKit/Participant/LocalParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
16 changes: 14 additions & 2 deletions Sources/LiveKit/Types/Options/DataPublishOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,14 +52,16 @@ 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 {
var hasher = Hasher()
hasher.combine(name)
hasher.combine(destinationIdentities)
hasher.combine(topic)
hasher.combine(reliable)
return hasher.finalize()
}
}
13 changes: 0 additions & 13 deletions Sources/LiveKit/Types/Other.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0d393eb

Please sign in to comment.