Skip to content

Commit

Permalink
Prepare for v2 release (livekit#265)
Browse files Browse the repository at this point in the history
* data delegates

* remove legacy stats

* remove reportStats option from RoomConnection

* remove deprecated macos screenshare code

* remove deprecated dimensions

* remove deprecated connect related code

* remove deprecated data publish method

* update publish(data:) method

* set(reportStatistics:)
  • Loading branch information
hiroshihorie committed Oct 30, 2023
1 parent 347a27b commit 19af0f8
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 615 deletions.
5 changes: 0 additions & 5 deletions Sources/LiveKit/Core/Engine+TransportDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import Promises

extension Engine: TransportDelegate {

func transport(_ transport: Transport, didGenerate stats: [TrackStats], target: Livekit_SignalTarget) {
// relay to Room
notify { $0.engine(self, didGenerate: stats, target: target) }
}

func transport(_ transport: Transport, didUpdate pcState: RTCPeerConnectionState) {
log("target: \(transport.target), state: \(pcState)")

Expand Down
6 changes: 2 additions & 4 deletions Sources/LiveKit/Core/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,12 @@ internal extension Engine {
let subscriber = try Transport(config: rtcConfiguration,
target: .subscriber,
primary: self.subscriberPrimary,
delegate: self,
reportStats: room._state.options.reportStats)
delegate: self)

let publisher = try Transport(config: rtcConfiguration,
target: .publisher,
primary: !self.subscriberPrimary,
delegate: self,
reportStats: room._state.options.reportStats)
delegate: self)

publisher.onOffer = { offer in
self.log("publisher onOffer \(offer.sdp)")
Expand Down
24 changes: 0 additions & 24 deletions Sources/LiveKit/Core/Room+EngineDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,6 @@ extension Room: EngineDelegate {
}
}

func engine(_ engine: Engine, didGenerate trackStats: [TrackStats], target: Livekit_SignalTarget) {

let allParticipants = ([[localParticipant],
_state.remoteParticipants.map { $0.value }] as [[Participant?]])
.joined()
.compactMap { $0 }

let allTracks = allParticipants.map { $0._state.tracks.values.map { $0.track } }.joined()
.compactMap { $0 }

// this relies on the last stat entry being the latest
for track in allTracks {
if let stats = trackStats.last(where: { $0.trackId == track.mediaTrack.trackId }) {
track.set(stats: stats)
}
}
}

func engine(_ engine: Engine, didUpdate speakers: [Livekit_SpeakerInfo]) {

let activeSpeakers = _state.mutate { state -> [Participant] in
Expand Down Expand Up @@ -187,18 +169,12 @@ extension Room: EngineDelegate {
guard let self = self else { return }

self.delegates.notify(label: { "room.didReceive data: \(userPacket.payload)" }) {
// deprecated
$0.room?(self, participant: participant, didReceive: userPacket.payload)
// new method with topic param
$0.room?(self, participant: participant, didReceiveData: userPacket.payload, topic: userPacket.topic)
}

if let participant = participant {
participant.delegates.notify(label: { "participant.didReceive data: \(userPacket.payload)" }) { [weak participant] (delegate) -> Void in
guard let participant = participant else { return }
// deprecated
delegate.participant?(participant, didReceive: userPacket.payload)
// new method with topic param
delegate.participant?(participant, didReceiveData: userPacket.payload, topic: userPacket.topic)
}
}
Expand Down
63 changes: 1 addition & 62 deletions Sources/LiveKit/Core/Transport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,13 @@ internal class Transport: MulticastDelegate<TransportDelegate> {
private let pc: LKRTCPeerConnection
private var pendingCandidates: [LKRTCIceCandidate] = []

// used for stats timer
private let statsTimer = DispatchQueueTimer(timeInterval: 1, queue: .liveKitWebRTC)
private var stats = [String: TrackStats]()

// keep reference to cancel later
private var debounceWorkItem: DispatchWorkItem?

init(config: LKRTCConfiguration,
target: Livekit_SignalTarget,
primary: Bool,
delegate: TransportDelegate,
reportStats: Bool = false) throws {
delegate: TransportDelegate) throws {

// try create peerConnection
guard let pc = Engine.createPeerConnection(config,
Expand All @@ -96,29 +91,16 @@ internal class Transport: MulticastDelegate<TransportDelegate> {
self.pc = pc

super.init()

log()

DispatchQueue.liveKitWebRTC.sync { pc.delegate = self }
add(delegate: delegate)

statsTimer.handler = { [weak self] in
self?.onStatsTimer()
}

set(reportStats: reportStats)
}

deinit {
statsTimer.suspend()
log()
}

internal func set(reportStats: Bool) {
log("reportStats: \(reportStats)")
reportStats ? statsTimer.resume() : statsTimer.suspend()
}

@discardableResult
func addIceCandidate(_ candidate: LKRTCIceCandidate) -> Promise<Void> {

Expand Down Expand Up @@ -196,7 +178,6 @@ internal class Transport: MulticastDelegate<TransportDelegate> {

// prevent debounced negotiate firing
self.debounceWorkItem?.cancel()
self.statsTimer.suspend()

// can be async
DispatchQueue.liveKitWebRTC.async {
Expand Down Expand Up @@ -224,48 +205,6 @@ extension Transport {
func statistics(for receiver: LKRTCRtpReceiver) async -> LKRTCStatisticsReport {
await pc.statistics(for: receiver)
}

func onStatsTimer() {

statsTimer.suspend()

pc.stats(for: nil, statsOutputLevel: .standard) { [weak self] reports in

guard let self = self else { return }

self.statsTimer.resume()

let tracks = reports
.filter { $0.type == TrackStats.keyTypeSSRC }
.map { entry -> TrackStats? in

let findPrevious = { () -> TrackStats? in
guard let ssrc = entry.values[TrackStats.keyTypeSSRC],
let previous = self.stats[ssrc] else { return nil }
return previous
}

return TrackStats(from: entry.values, previous: findPrevious())
}
.compactMap { $0 }

for track in tracks {
// cache
self.stats[track.ssrc] = track
}

if !tracks.isEmpty {
self.notify { $0.transport(self, didGenerate: tracks, target: self.target) }
}

// clean up
// for key in self.stats.keys {
// if !tracks.contains(where: { $0.ssrc == key }) {
// self.stats.removeValue(forKey: key)
// }
// }
}
}
}

// MARK: - RTCPeerConnectionDelegate
Expand Down
16 changes: 0 additions & 16 deletions Sources/LiveKit/LiveKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,6 @@ public class LiveKit: NSObject {
@objc(sdkVersion)
public static let version = "1.1.3"

@available(*, deprecated, message: "Use Room.connect() instead, protocol v8 and higher do not support this method")
public static func connect(
_ url: String,
_ token: String,
delegate: RoomDelegate? = nil,
connectOptions: ConnectOptions = ConnectOptions(),
roomOptions: RoomOptions = RoomOptions()) -> Promise<Room> {

let room = Room(delegate: delegate,
// Override with protocol v7 or lower when using this deprecated method
connectOptions: connectOptions.protocolVersion >= .v8 ? connectOptions.copyWith(protocolVersion: .v7) : connectOptions,
roomOptions: roomOptions)

return room.connect(url, token)
}

@objc
public static func setLoggerStandardOutput() {
LoggingSystem.bootstrap({
Expand Down
15 changes: 1 addition & 14 deletions Sources/LiveKit/Participant/LocalParticipant+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,6 @@ public extension LocalParticipant {
}
}

func publishData(_ data: Data,
reliability: Reliability = .reliable,
destination: [String] = []) async throws {

try await withCheckedThrowingContinuation { continuation in
publishData(data: data, reliability: reliability, destination: destination).then(on: queue) { result in
continuation.resume(returning: result)
}.catch(on: queue) { error in
continuation.resume(throwing: error)
}
}
}

///
/// Publish data to the other participants in the room
///
Expand All @@ -102,7 +89,7 @@ public extension LocalParticipant {
///
func publish(data: Data,
reliability: Reliability = .reliable,
destinations: [RemoteParticipant]? = nil,
destinations: [Sid]? = nil,
topic: String? = nil,
options: DataPublishOptions? = nil) async throws {

Expand Down
4 changes: 2 additions & 2 deletions Sources/LiveKit/Participant/LocalParticipant+ObjC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ extension LocalParticipant {
@discardableResult
public func publishDataObjC(data: Data,
reliability: Reliability = .reliable,
destination: [String] = []) -> Promise<Void>.ObjCPromise<NSNull> {
destinations: [String] = []) -> Promise<Void>.ObjCPromise<NSNull> {

publishData(data: data, reliability: reliability, destination: destination).asObjCPromise()
publish(data: data, reliability: reliability, destinations: destinations).asObjCPromise()
}

@objc(setTrackSubscriptionPermissionsWithAllParticipantsAllowed:trackPermissions:)
Expand Down
30 changes: 4 additions & 26 deletions Sources/LiveKit/Participant/LocalParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -282,42 +282,20 @@ public class LocalParticipant: Participant {
/// - 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.
/// - destination: SIDs of the participants who will receive the message. If empty, deliver to everyone
///
/// > Notice: Deprecated, use ``publish(data:reliability:destinations:topic:options:)-2581z`` instead.
@available(*, deprecated, renamed: "publish(data:reliability:destinations:topic:options:)")
@discardableResult
public func publishData(data: Data,
reliability: Reliability = .reliable,
destination: [String] = []) -> Promise<Void> {

let userPacket = Livekit_UserPacket.with {
$0.destinationSids = destination
$0.payload = data
$0.participantSid = self.sid
}

return room.engine.send(userPacket: userPacket,
reliability: reliability)
}

///
/// Promise version of ``publish(data:reliability:destinations:topic:options:)-75jme``.
///
/// - destinations: SIDs of the participants who will receive the message. If empty, deliver to everyone
@discardableResult
public func publish(data: Data,
reliability: Reliability = .reliable,
destinations: [RemoteParticipant]? = nil,
destinations: [Sid]? = nil,
topic: String? = nil,
options: DataPublishOptions? = nil) -> Promise<Void> {

let options = options ?? self.room._state.options.defaultDataPublishOptions
let destinations = destinations?.map { $0.sid }

let userPacket = Livekit_UserPacket.with {
$0.destinationSids = destinations ?? options.destinations
$0.payload = data
$0.participantSid = self.sid
$0.payload = data
$0.destinationSids = destinations ?? options.destinations
$0.topic = topic ?? options.topic ?? ""
}

Expand Down
1 change: 0 additions & 1 deletion Sources/LiveKit/Protocols/EngineDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ internal protocol EngineDelegate: AnyObject {
func engine(_ engine: Engine, didAddTrack track: LKRTCMediaStreamTrack, rtpReceiver: LKRTCRtpReceiver, streams: [LKRTCMediaStream])
func engine(_ engine: Engine, didRemove track: LKRTCMediaStreamTrack)
func engine(_ engine: Engine, didReceive userPacket: Livekit_UserPacket)
func engine(_ engine: Engine, didGenerate stats: [TrackStats], target: Livekit_SignalTarget)
}
7 changes: 0 additions & 7 deletions Sources/LiveKit/Protocols/ParticipantDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ public protocol ParticipantDelegate: AnyObject {
func participant(_ participant: RemoteParticipant, didUnsubscribe publication: RemoteTrackPublication, track: Track)

/// Data was received from a ``RemoteParticipant``.
///
/// > Notice: Deprecated, use ``participant(_:didReceiveData:topic:)`` instead.
@objc(participant:didReceiveData:)
@available(*, deprecated, renamed: "participant(_:didReceiveData:topic:)")
optional
func participant(_ participant: RemoteParticipant, didReceive data: Data)

@objc(participant:didReceiveData:topic:) optional
func participant(_ participant: RemoteParticipant, didReceiveData data: Data, topic: String)
}
7 changes: 0 additions & 7 deletions Sources/LiveKit/Protocols/RoomDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,6 @@ public protocol RoomDelegateObjC: AnyObject {

/// Same with ``ParticipantDelegate/participant(_:didReceive:)-2t55a``
/// participant could be nil if data was sent by server api.
///
/// Deprecated, use ``room(_:participant:didReceiveData:topic:)`` instead.
@objc(room:participant:didReceiveData:)
@available(*, deprecated, renamed: "room(_:participant:didReceiveData:topic:)")
optional
func room(_ room: Room, participant: RemoteParticipant?, didReceive data: Data)

@objc(room:participant:didReceiveData:topic:) optional
func room(_ room: Room, participant: RemoteParticipant?, didReceiveData data: Data, topic: String)

Expand Down
5 changes: 0 additions & 5 deletions Sources/LiveKit/Protocols/TrackDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public protocol TrackDelegate: AnyObject {
@objc(track:didUpdateMuted:shouldSendSignal:) optional
func track(_ track: Track, didUpdate muted: Bool, shouldSendSignal: Bool)

/// Statistics for the track has been generated.
@available(*, deprecated, message: "Use track:didUpdateStatistics: instead")
@objc(track:didUpdateStats:) optional
func track(_ track: Track, didUpdate stats: TrackStats)

/// Statistics for the track has been generated (v2).
@objc(track:didUpdateStatistics:) optional
func track(_ track: Track, didUpdateStatistics: TrackStatistics)
Expand Down
1 change: 0 additions & 1 deletion Sources/LiveKit/Protocols/TransportDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ internal protocol TransportDelegate: AnyObject {
func transport(_ transport: Transport, didAddTrack: LKRTCMediaStreamTrack, rtpReceiver: LKRTCRtpReceiver, streams: [LKRTCMediaStream])
func transport(_ transport: Transport, didRemove track: LKRTCMediaStreamTrack)
func transportShouldNegotiate(_ transport: Transport)
func transport(_ transport: Transport, didGenerate stats: [TrackStats], target: Livekit_SignalTarget)
}
16 changes: 8 additions & 8 deletions Sources/LiveKit/SwiftUI/SwiftUIVideoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import SwiftUI
internal class TrackDelegateReceiver: TrackDelegate, Loggable {

@Binding var dimensions: Dimensions?
@Binding var stats: TrackStats?
@Binding var statistics: TrackStatistics?

init(dimensions: Binding<Dimensions?>, stats: Binding<TrackStats?>) {
init(dimensions: Binding<Dimensions?>, statistics: Binding<TrackStatistics?>) {
self._dimensions = dimensions
self._stats = stats
self._statistics = statistics
}

func track(_ track: VideoTrack, didUpdate dimensions: Dimensions?) {
Expand All @@ -34,9 +34,9 @@ internal class TrackDelegateReceiver: TrackDelegate, Loggable {
}
}

func track(_ track: Track, didUpdate stats: TrackStats) {
func track(_ track: Track, didUpdateStatistics statistics: TrackStatistics) {
Task.detached { @MainActor in
self.stats = stats
self.statistics = statistics
}
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ public struct SwiftUIVideoView: NativeViewRepresentable {
debugMode: Bool = false,
isRendering: Binding<Bool> = .constant(false),
dimensions: Binding<Dimensions?> = .constant(nil),
trackStats: Binding<TrackStats?> = .constant(nil)) {
trackStatistics: Binding<TrackStatistics?> = .constant(nil)) {

self.track = track
self.layoutMode = layoutMode
Expand All @@ -95,14 +95,14 @@ public struct SwiftUIVideoView: NativeViewRepresentable {
self._dimensions = dimensions

self.trackDelegateReceiver = TrackDelegateReceiver(dimensions: dimensions,
stats: trackStats)
statistics: trackStatistics)

self.videoViewDelegateReceiver = VideoViewDelegateReceiver(isRendering: isRendering)

// update binding value
Task.detached { @MainActor in
dimensions.wrappedValue = track.dimensions
trackStats.wrappedValue = track.stats
trackStatistics.wrappedValue = track.statistics
}

// listen for TrackDelegate
Expand Down
Loading

0 comments on commit 19af0f8

Please sign in to comment.