diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index afb0a71..9fa9bc8 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -64,9 +64,24 @@ public class RTCBandwidth: NSObject { } } + /// Connect to the signaling server to start publishing media. + /// - Parameters: + /// - url: Complete URL containing everything required to access WebRTC. + /// - completion: The completion handler to call when the connect request is complete. + /// - Throws: Throw when a connection to the signaling server experiences an error. + public func connect(to url: URL, completion: @escaping () -> Void) throws { + signaling = Signaling() + signaling?.delegate = self + + try signaling?.connect(to: url) { + completion() + } + } + /// Disconnect from Bandwidth's WebRTC signaling server and remove all local connections. public func disconnect() { signaling?.disconnect() + localConnections.forEach { $0.peerConnection.senders.forEach { $0.track?.isEnabled = false } } localConnections.removeAll() } @@ -172,7 +187,7 @@ public class RTCBandwidth: NSObject { } } #endif - + private func negotiateSDP(endpointId: String, direction: String, mediaTypes: [MediaType], for peerConnection: RTCPeerConnection, completion: @escaping () -> Void) { debugPrint(direction) diff --git a/Sources/BandwidthWebRTC/Signaling/Signaling.swift b/Sources/BandwidthWebRTC/Signaling/Signaling.swift index 98f27d3..3672a90 100644 --- a/Sources/BandwidthWebRTC/Signaling/Signaling.swift +++ b/Sources/BandwidthWebRTC/Signaling/Signaling.swift @@ -44,6 +44,12 @@ class Signaling { throw SignalingError.invalidWebSocketURL } + try connect(to: url) { + completion() + } + } + + func connect(to url: URL, completion: @escaping () -> Void) throws { try client.subscribe(to: SignalingMethod.endpointRemoved.rawValue, type: EndpointRemovedParameters.self) client.on(method: SignalingMethod.endpointRemoved.rawValue, type: EndpointRemovedParameters.self) { parameters in self.delegate?.signaling(self, didReceiveEndpointRemoved: parameters)