@@ -9,7 +9,7 @@ import Foundation
99import WebRTC
1010
1111public protocol RTCBandwidthDelegate {
12- func bandwidth( _ bandwidth: RTCBandwidth , streamAvailableAt connection : RTCBandwidthConnection , stream : RTCMediaStream ? )
12+ func bandwidth( _ bandwidth: RTCBandwidth , streamAvailableAt endpointId : String , participantId : String , alias : String ? , mediaTypes : [ MediaType ] , mediaStream : RTCMediaStream ? )
1313 func bandwidth( _ bandwidth: RTCBandwidth , streamUnavailableAt endpointId: String )
1414}
1515
@@ -32,8 +32,8 @@ public class RTCBandwidth: NSObject {
3232
3333 private let mediaConstraints = RTCMediaConstraints ( mandatoryConstraints: nil , optionalConstraints: [ " DtlsSrtpKeyAgreement " : kRTCMediaConstraintsValueTrue] )
3434
35- private var localConnections = [ RTCBandwidthConnection ] ( )
36- private var remoteConnections = [ RTCBandwidthConnection ] ( )
35+ private var localConnections = [ Connection ] ( )
36+ private var remoteConnections = [ Connection ] ( )
3737
3838 #if os(iOS)
3939 private let audioSession = RTCAudioSession . sharedInstance ( )
@@ -62,6 +62,10 @@ public class RTCBandwidth: NSObject {
6262 }
6363 }
6464
65+ public func disconnect( ) {
66+ signaling? . disconnect ( )
67+ }
68+
6569 public func publish( audio: Bool , video: Bool , alias: String ? , completion: @escaping ( ) -> Void ) {
6670 var mediaTypes = [ MediaType] ( )
6771
@@ -84,18 +88,27 @@ public class RTCBandwidth: NSObject {
8488
8589 self . createMediaSenders ( peerConnection: peerConnection, audio: audio, video: video)
8690
87- let localConnection = RTCBandwidthConnection ( endpointId: result. endpointId, peerConnection : peerConnection , alias : alias , participantId : nil )
91+ let localConnection = Connection ( peerConnection : peerConnection , endpointId: result. endpointId, participantId : result . participantId , mediaTypes : mediaTypes , alias : alias )
8892 self . localConnections. append ( localConnection)
8993
9094 self . negotiateSDP ( endpointId: result. endpointId, direction: result. direction, mediaTypes: result. mediaTypes, for: peerConnection) {
91- DispatchQueue . main. async {
92- completion ( )
93- }
95+ completion ( )
9496 }
9597 }
9698 }
9799 }
98100
101+ public func unpublish( endpointId: String ) {
102+ signaling? . unpublish ( endpointId: endpointId) { result in
103+
104+ }
105+
106+ if let index = localConnections. firstIndex ( where: { $0. endpointId == endpointId } ) {
107+ localConnections [ index] . peerConnection. close ( )
108+ localConnections. remove ( at: index)
109+ }
110+ }
111+
99112 // MARK: Media
100113
101114 public func captureLocalVideo( renderer: RTCVideoRenderer ) {
@@ -197,7 +210,7 @@ public class RTCBandwidth: NSObject {
197210 return videoTrack
198211 }
199212
200- private func negotiateSDP( endpointId: String , direction: String , mediaTypes: [ String ] , for peerConnection: RTCPeerConnection , completion: @escaping ( ) -> Void ) {
213+ private func negotiateSDP( endpointId: String , direction: String , mediaTypes: [ MediaType ] , for peerConnection: RTCPeerConnection , completion: @escaping ( ) -> Void ) {
201214 debugPrint ( direction)
202215
203216 var mandatoryConstraints = [
@@ -206,14 +219,14 @@ public class RTCBandwidth: NSObject {
206219 ]
207220
208221 if direction. contains ( " recv " ) {
209- mandatoryConstraints [ kRTCMediaConstraintsOfferToReceiveAudio] = mediaTypes. contains ( " AUDIO " ) ? kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse
210- mandatoryConstraints [ kRTCMediaConstraintsOfferToReceiveVideo] = mediaTypes. contains ( " VIDEO " ) ? kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse
222+ mandatoryConstraints [ kRTCMediaConstraintsOfferToReceiveAudio] = mediaTypes. contains ( . audio ) ? kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse
223+ mandatoryConstraints [ kRTCMediaConstraintsOfferToReceiveVideo] = mediaTypes. contains ( . video ) ? kRTCMediaConstraintsValueTrue : kRTCMediaConstraintsValueFalse
211224 }
212225
213226 let constraints = RTCMediaConstraints ( mandatoryConstraints: mandatoryConstraints, optionalConstraints: nil )
214227
215228 peerConnection. offer ( for: constraints) { offer, error in
216- DispatchQueue . main. async {
229+ // DispatchQueue.main.async {
217230 if let error = error {
218231 print ( error. localizedDescription)
219232 }
@@ -228,7 +241,7 @@ public class RTCBandwidth: NSObject {
228241 }
229242
230243 peerConnection. setLocalDescription ( offer) { error in
231- DispatchQueue . main. async {
244+ // DispatchQueue.main.async {
232245 if let error = error {
233246 debugPrint ( error. localizedDescription)
234247 }
@@ -242,21 +255,28 @@ public class RTCBandwidth: NSObject {
242255
243256 completion ( )
244257 }
245- }
258+ // }
246259 }
247260 }
248- }
261+ // }
249262 }
250263 }
251264
252265 private func handleSDPNeededEvent( parameters: SDPNeededParameters ) {
253- let peerConnection = RTCBandwidth . factory. peerConnection ( with: configuration, constraints: mediaConstraints, delegate: self )
254- let remoteConnection = RTCBandwidthConnection ( endpointId: parameters. endpointId, peerConnection: peerConnection, alias: parameters. alias, participantId: parameters. participantId)
266+ let remotePeerConnection = RTCBandwidth . factory. peerConnection ( with: configuration, constraints: mediaConstraints, delegate: self )
267+
268+ let remoteConnection = Connection (
269+ peerConnection: remotePeerConnection,
270+ endpointId: parameters. endpointId,
271+ participantId: parameters. participantId,
272+ mediaTypes: parameters. mediaTypes,
273+ alias: parameters. alias
274+ )
255275
256276 remoteConnections. append ( remoteConnection)
257277
258- negotiateSDP ( endpointId: parameters. endpointId, direction: parameters. direction, mediaTypes: parameters. mediaTypes, for: peerConnection ) {
259-
278+ negotiateSDP ( endpointId: parameters. endpointId, direction: parameters. direction, mediaTypes: parameters. mediaTypes, for: remotePeerConnection ) {
279+
260280 }
261281 }
262282
@@ -288,11 +308,16 @@ extension RTCBandwidth: RTCPeerConnectionDelegate {
288308 public func peerConnection( _ peerConnection: RTCPeerConnection , didAdd rtpReceiver: RTCRtpReceiver , streams mediaStreams: [ RTCMediaStream ] ) {
289309 debugPrint ( " peerConnection didAdd rtpReceiver: streams media Streams: " )
290310
291- guard let connection = remoteConnections. first ( where: { $0. peerConnection == peerConnection } ) else { return }
292-
293- DispatchQueue . main. async {
294- self . delegate? . bandwidth ( self , streamAvailableAt: connection, stream: mediaStreams. first)
295- }
311+ guard let remoteConnection = remoteConnections. first ( where: { $0. peerConnection == peerConnection } ) else { return }
312+
313+ self . delegate? . bandwidth (
314+ self ,
315+ streamAvailableAt: remoteConnection. endpointId,
316+ participantId: remoteConnection. participantId,
317+ alias: remoteConnection. alias,
318+ mediaTypes: remoteConnection. mediaTypes,
319+ mediaStream: mediaStreams. first
320+ )
296321 }
297322
298323 public func peerConnection( _ peerConnection: RTCPeerConnection , didChange stateChanged: RTCSignalingState ) {
@@ -318,11 +343,30 @@ extension RTCBandwidth: RTCPeerConnectionDelegate {
318343 public func peerConnection( _ peerConnection: RTCPeerConnection , didGenerate candidate: RTCIceCandidate ) {
319344 debugPrint ( " peerConnection didGenerate candidate: RTCIceCandidate " )
320345
321- guard let endpointId = remoteConnections. first ( where: { $0. peerConnection == peerConnection } ) ? . endpointId else {
346+ guard let remoteConnection = remoteConnections. first ( where: { $0. peerConnection == peerConnection } ) else {
322347 return
323348 }
324349
325- signaling? . sendIceCandidate ( endpointId: endpointId, candidate: candidate)
350+ signaling? . sendIceCandidate (
351+ endpointId: remoteConnection. endpointId,
352+ sdp: candidate. sdp,
353+ sdpMLineIndex: Int ( candidate. sdpMLineIndex) ,
354+ sdpMid: candidate. sdpMid ?? " "
355+ ) { _ in
356+
357+ }
358+ }
359+
360+ public func peerConnection( _ peerConnection: RTCPeerConnection , didChange newState: RTCPeerConnectionState ) {
361+ print ( " peerConnection didChange newState: \( newState) " )
362+
363+ if [ . disconnected, . failed] . contains ( newState) {
364+ guard let remoteConnection = remoteConnections. first ( where: { $0. peerConnection == peerConnection } ) else {
365+ return
366+ }
367+
368+ delegate? . bandwidth ( self , streamUnavailableAt: remoteConnection. endpointId)
369+ }
326370 }
327371
328372 public func peerConnection( _ peerConnection: RTCPeerConnection , didRemove candidates: [ RTCIceCandidate ] ) {
0 commit comments