From 5e847e114653092550930541237d722d57a2be4a Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Thu, 4 Feb 2021 17:04:09 -0500 Subject: [PATCH 01/12] Remove commented out dispatch queue --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 52 ++++++++++------------ 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index a1e42ef..943a8a0 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -226,39 +226,35 @@ public class RTCBandwidth: NSObject { let constraints = RTCMediaConstraints(mandatoryConstraints: mandatoryConstraints, optionalConstraints: nil) peerConnection.offer(for: constraints) { offer, error in -// DispatchQueue.main.async { - if let error = error { - print(error.localizedDescription) - } - - guard let offer = offer else { + if let error = error { + print(error.localizedDescription) + } + + guard let offer = offer else { + return + } + + self.signaling?.offer(endpointId: endpointId, sdp: offer.sdp) { result in + guard let result = result else { return } - - self.signaling?.offer(endpointId: endpointId, sdp: offer.sdp) { result in - guard let result = result else { - return + + peerConnection.setLocalDescription(offer) { error in + if let error = error { + debugPrint(error.localizedDescription) } - - peerConnection.setLocalDescription(offer) { error in -// DispatchQueue.main.async { - if let error = error { - debugPrint(error.localizedDescription) - } - - let sdp = RTCSessionDescription(type: .answer, sdp: result.sdpAnswer) - - peerConnection.setRemoteDescription(sdp) { error in - if let error = error { - debugPrint(error.localizedDescription) - } - - completion() - } -// } + + let sdp = RTCSessionDescription(type: .answer, sdp: result.sdpAnswer) + + peerConnection.setRemoteDescription(sdp) { error in + if let error = error { + debugPrint(error.localizedDescription) + } + + completion() } } -// } + } } } From 0ac22a6b8e98ae67ac4e2be473cbe09ea0a9ced2 Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Thu, 4 Feb 2021 17:10:09 -0500 Subject: [PATCH 02/12] Close peer connection when disconnected or failed --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 943a8a0..6584c0d 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -357,11 +357,14 @@ extension RTCBandwidth: RTCPeerConnectionDelegate { print("peerConnection didChange newState: \(newState)") if [.disconnected, .failed].contains(newState) { - guard let remoteConnection = remoteConnections.first(where: { $0.peerConnection == peerConnection }) else { + guard let index = remoteConnections.firstIndex(where: { $0.peerConnection == peerConnection }) else { return } - - delegate?.bandwidth(self, streamUnavailableAt: remoteConnection.endpointId) + + delegate?.bandwidth(self, streamUnavailableAt: remoteConnections[index].endpointId) + + remoteConnections[index].peerConnection.close() + remoteConnections.remove(at: index) } } From 3b321ed6b9292032a4870163c5418026e20ebf2b Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Thu, 4 Feb 2021 17:15:21 -0500 Subject: [PATCH 03/12] No need for a function with a single line of code --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 6584c0d..b84adb6 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -290,10 +290,6 @@ public class RTCBandwidth: NSObject { connection.peerConnection.add(candidate) } - - private func endpointRemoved(with endpointId: String) { - delegate?.bandwidth(self, streamUnavailableAt: endpointId) - } } extension RTCBandwidth: RTCPeerConnectionDelegate { @@ -387,6 +383,6 @@ extension RTCBandwidth: SignalingDelegate { } func signaling(_ signaling: Signaling, didReceiveEndpointRemoved parameters: EndpointRemovedParameters) { - endpointRemoved(with: parameters.endpointId) + delegate?.bandwidth(self, streamUnavailableAt: parameters.endpointId) } } From 88fa93f0f9ca85d19066e411461fb9f610442dd5 Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 11:44:45 -0500 Subject: [PATCH 04/12] Mark deprecated delegate functions --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index b84adb6..276d6cd 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -316,12 +316,14 @@ extension RTCBandwidth: RTCPeerConnectionDelegate { debugPrint("peerConnection didChange stateChanged: \(stateChanged)") } + @available(*, deprecated) public func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) { - debugPrint("peerConnection didAdd stream: RTCMediaStream") + } + @available(*, deprecated) public func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) { - debugPrint("peerConnection didRemove stream: RTCMediaStream") + } public func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) { From 6fb9b68dea913bdd7fc8770104386867d5da4a45 Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 12:12:02 -0500 Subject: [PATCH 05/12] Build and add an audio stream rather than an audio track --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 276d6cd..4489706 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -41,6 +41,18 @@ public class RTCBandwidth: NSObject { private let audioQueue = DispatchQueue(label: "audio") + private lazy var localAudioTrack: RTCAudioTrack = { + let audioTrackId = "audio_track_0" + return RTCBandwidth.factory.audioTrack(withTrackId: audioTrackId) + }() + + private lazy var localAudioStream: RTCMediaStream = { + let audioStreamId = "audio_stream_0" + let audioStream = RTCBandwidth.factory.mediaStream(withStreamId: audioStreamId) + audioStream.addAudioTrack(localAudioTrack) + return audioStream + }() + private var videoCapturer: RTCVideoCapturer? private var localVideoTrack: RTCVideoTrack? @@ -176,8 +188,7 @@ public class RTCBandwidth: NSObject { // Create an audio track for the peer connection. if audio { - let audioTrack = createAudioTrack() - peerConnection.add(audioTrack, streamIds: [streamId]) + peerConnection.add(localAudioStream) } // Create a video track for the peer connection. From 6a475a287bebc41e9adc2830d28c360d637b62e4 Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 12:21:28 -0500 Subject: [PATCH 06/12] Revert "Build and add an audio stream rather than an audio track" This reverts commit 6fb9b68dea913bdd7fc8770104386867d5da4a45. --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 4489706..276d6cd 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -41,18 +41,6 @@ public class RTCBandwidth: NSObject { private let audioQueue = DispatchQueue(label: "audio") - private lazy var localAudioTrack: RTCAudioTrack = { - let audioTrackId = "audio_track_0" - return RTCBandwidth.factory.audioTrack(withTrackId: audioTrackId) - }() - - private lazy var localAudioStream: RTCMediaStream = { - let audioStreamId = "audio_stream_0" - let audioStream = RTCBandwidth.factory.mediaStream(withStreamId: audioStreamId) - audioStream.addAudioTrack(localAudioTrack) - return audioStream - }() - private var videoCapturer: RTCVideoCapturer? private var localVideoTrack: RTCVideoTrack? @@ -188,7 +176,8 @@ public class RTCBandwidth: NSObject { // Create an audio track for the peer connection. if audio { - peerConnection.add(localAudioStream) + let audioTrack = createAudioTrack() + peerConnection.add(audioTrack, streamIds: [streamId]) } // Create a video track for the peer connection. From 210dfae47764401cab921816dbbe161bb64a57ef Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 13:27:49 -0500 Subject: [PATCH 07/12] Allow setting local audio and video connection state --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 276d6cd..27aab78 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -171,6 +171,41 @@ public class RTCBandwidth: NSObject { } #endif + /// Determine whether the local connection's audio should be in an enabled state. When `endpointId` is nil audio state will be set for all local connections. + /// + /// - Parameter endpointId: The endpoint id for the local connection. + /// - Parameter isEnabled: A Boolean value indicating whether the audio is in the enabled state. + public func setAudio(_ endpointId: String? = nil, isEnabled: Bool) { + setTrack(RTCAudioTrack.self, endpointId: endpointId, isEnabled: isEnabled) + } + + /// Determine whether the local connection's video should be in an enabled state. When `endpointId` is nil video state will be set for all local connections. + /// + /// - Parameter endpointId: The endpoint id for the local connection. + /// - Parameter isEnabled: A Boolean value indicating whether the video is in the enabled state. + public func setVideo(_ endpointId: String? = nil, isEnabled: Bool) { + setTrack(RTCVideoTrack.self, endpointId: endpointId, isEnabled: isEnabled) + } + + private func setTrack(_ type: T.Type, endpointId: String?, isEnabled: Bool) { + if let endpointId = endpointId { + localConnections + .filter { $0.endpointId == endpointId } + .compactMap { $0.peerConnection } + .forEach { setTrack(RTCAudioTrack.self, peerConnection: $0, isEnabled: isEnabled) } + } else { + localConnections + .compactMap { $0.peerConnection } + .forEach { setTrack(RTCAudioTrack.self, peerConnection: $0, isEnabled: isEnabled) } + } + } + + private func setTrack(_ type: T.Type, peerConnection: RTCPeerConnection, isEnabled: Bool) { + peerConnection.transceivers + .compactMap { $0.sender.track as? T } + .forEach { $0.isEnabled = isEnabled } + } + private func createMediaSenders(peerConnection: RTCPeerConnection, audio: Bool, video: Bool) { let streamId = "stream" From 0cb51439f7728d396968201abd22f258906e0e0f Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 13:56:41 -0500 Subject: [PATCH 08/12] Oops, setting track should use the generic --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 27aab78..9d80d1b 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -192,11 +192,11 @@ public class RTCBandwidth: NSObject { localConnections .filter { $0.endpointId == endpointId } .compactMap { $0.peerConnection } - .forEach { setTrack(RTCAudioTrack.self, peerConnection: $0, isEnabled: isEnabled) } + .forEach { setTrack(T.self, peerConnection: $0, isEnabled: isEnabled) } } else { localConnections .compactMap { $0.peerConnection } - .forEach { setTrack(RTCAudioTrack.self, peerConnection: $0, isEnabled: isEnabled) } + .forEach { setTrack(T.self, peerConnection: $0, isEnabled: isEnabled) } } } From b0c470e485c9b6940d2b03c9a4a68f7349afd9b9 Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 14:16:02 -0500 Subject: [PATCH 09/12] Keep parameter naming consistent with setting audio and video --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 9d80d1b..4c24c36 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -155,7 +155,7 @@ public class RTCBandwidth: NSObject { } #if os(iOS) - public func setSpeaker(_ speaker: Bool) { + public func setSpeaker(_ isEnabled: Bool) { audioQueue.async { defer { RTCAudioSession.sharedInstance().unlockForConfiguration() @@ -163,7 +163,7 @@ public class RTCBandwidth: NSObject { RTCAudioSession.sharedInstance().lockForConfiguration() do { - try RTCAudioSession.sharedInstance().overrideOutputAudioPort(speaker ? .speaker : .none) + try RTCAudioSession.sharedInstance().overrideOutputAudioPort(isEnabled ? .speaker : .none) } catch { debugPrint(error.localizedDescription) } From 3253d5c38490c1b1ae38cab2a99db9cd08dca957 Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 14:18:39 -0500 Subject: [PATCH 10/12] Add comment for setting the speaker --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 4c24c36..a6c1f21 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -155,6 +155,9 @@ public class RTCBandwidth: NSObject { } #if os(iOS) + /// Determine whether the device's speaker should be in an enabled state. + /// + /// - Parameter isEnabled: A Boolean value indicating whether the device's speaker is in the enabled state. public func setSpeaker(_ isEnabled: Bool) { audioQueue.async { defer { From bcac04ea944215fbe1091390fabed35669bf222e Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 14:41:29 -0500 Subject: [PATCH 11/12] Remove local connections on disconnect --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index a6c1f21..2bf4b12 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -64,6 +64,7 @@ public class RTCBandwidth: NSObject { public func disconnect() { signaling?.disconnect() + localConnections.removeAll() } public func publish(audio: Bool, video: Bool, alias: String?, completion: @escaping () -> Void) { From 2a803bba80ca2c2180cb1ef4c8b536c6f0ca3345 Mon Sep 17 00:00:00 2001 From: Mike Hamer Date: Fri, 5 Feb 2021 14:50:08 -0500 Subject: [PATCH 12/12] Add comment --- Sources/BandwidthWebRTC/RTCBandwidth.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/BandwidthWebRTC/RTCBandwidth.swift b/Sources/BandwidthWebRTC/RTCBandwidth.swift index 2bf4b12..c8ce801 100644 --- a/Sources/BandwidthWebRTC/RTCBandwidth.swift +++ b/Sources/BandwidthWebRTC/RTCBandwidth.swift @@ -62,6 +62,7 @@ public class RTCBandwidth: NSObject { } } + /// Disconnect from Bandwidth's WebRTC signaling server and remove all local connections. public func disconnect() { signaling?.disconnect() localConnections.removeAll()