From ce5b8e89793567ee4576b9ebf77682bbe1182049 Mon Sep 17 00:00:00 2001 From: Tiago Jacobs Date: Sun, 27 Mar 2022 13:54:18 -0300 Subject: [PATCH] Implements the createOffer method --- .../SampleHandler.swift | 2 +- example/ios/Podfile.lock | 4 +- .../Classes/BBBSampleHandler.swift | 21 ++++++++--- .../Classes/ScreenBroadcaster.swift | 32 ++++++++++++++++ ...ile-sdk-broadcast-upload-extension.podspec | 2 +- ios-common/Classes/WebRTCClient.swift | 37 +++++++++---------- .../bigbluebutton-mobile-sdk-common.podspec | 2 +- 7 files changed, 70 insertions(+), 30 deletions(-) create mode 100644 ios-broadcast-upload-extension/Classes/ScreenBroadcaster.swift diff --git a/example/ios/BigBlueButtonMobileSdkBroadcastExample/SampleHandler.swift b/example/ios/BigBlueButtonMobileSdkBroadcastExample/SampleHandler.swift index be77445..a77a3e1 100644 --- a/example/ios/BigBlueButtonMobileSdkBroadcastExample/SampleHandler.swift +++ b/example/ios/BigBlueButtonMobileSdkBroadcastExample/SampleHandler.swift @@ -6,7 +6,7 @@ // import ReplayKit -import bigbluebutton_mobile_sdk_common +import bigbluebutton_mobile_sdk_broadcast_upload_extension class SampleHandler: BBBSampleHandler { override func broadcastStarted(withSetupInfo setupInfo: [String : NSObject]?) { diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index a3206fc..a259975 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -362,8 +362,8 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: bigbluebutton-mobile-sdk: 3b5da479ad35ed28109cc74b1f8bf7c284e61bb0 - bigbluebutton-mobile-sdk-broadcast-upload-extension: ab8f9cebcde6ac34d5643c27da289928f8225b78 - bigbluebutton-mobile-sdk-common: 1da6689f4b52a15f0225f991d9e62158e654b941 + bigbluebutton-mobile-sdk-broadcast-upload-extension: a7db971c25db05172529a414b1faf12e410f732a + bigbluebutton-mobile-sdk-common: a230c42cc0f816b44a060a829255fd3900e08e35 boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c DoubleConversion: cde416483dac037923206447da6e1454df403714 FBLazyVector: 3bb422f41b18121b71783a905c10e58606f7dc3e diff --git a/ios-broadcast-upload-extension/Classes/BBBSampleHandler.swift b/ios-broadcast-upload-extension/Classes/BBBSampleHandler.swift index 6da7126..ea351fe 100644 --- a/ios-broadcast-upload-extension/Classes/BBBSampleHandler.swift +++ b/ios-broadcast-upload-extension/Classes/BBBSampleHandler.swift @@ -13,6 +13,7 @@ open class BBBSampleHandler : RPBroadcastSampleHandler { private var logger = os.Logger(subsystem: "BigBlueButtonMobileSDK", category: "BBBSampleHandler") private var appGroupName:String = ""; private var observer:NSKeyValueObservation?; + private var screenBroadcaster:ScreenBroadcaster?; open func setAppGroupName(appGroupName:String) { logger.info("Received appGroupName: \(appGroupName)") @@ -31,15 +32,25 @@ open class BBBSampleHandler : RPBroadcastSampleHandler { logger.info("ReplayKit2 event - broadcastStarted - persisting information on UserDefaults") userDefaults.set(BBBSharedData.generatePayload(), forKey: BBBSharedData.SharedData.broadcastStarted) + self.screenBroadcaster = ScreenBroadcaster() + // Listen for createOffer requests from the UI APP logger.info("Configuring observer") self.observer = userDefaults.observe(\.createScreenShareOffer, options: [.new]) { (defaults, change) in self.logger.info("Observer detected a createScreenShareOffer request!") - BBBSharedData - .getUserDefaults(appGroupName: self.appGroupName) - .set(BBBSharedData.generatePayload(properties: [ - "sdp": "this is SDP from extension" - ]), forKey: BBBSharedData.SharedData.screenShareOfferCreated) + + Task.init { + let optionalSdp = await self.screenBroadcaster?.createOffer() + if(optionalSdp != nil){ + let sdp = optionalSdp! + self.logger.info("Got SDP back from screenBroadcaster: \(sdp)") + BBBSharedData + .getUserDefaults(appGroupName: self.appGroupName) + .set(BBBSharedData.generatePayload(properties: [ + "sdp": sdp + ]), forKey: BBBSharedData.SharedData.screenShareOfferCreated) + } + } } } diff --git a/ios-broadcast-upload-extension/Classes/ScreenBroadcaster.swift b/ios-broadcast-upload-extension/Classes/ScreenBroadcaster.swift new file mode 100644 index 0000000..689a391 --- /dev/null +++ b/ios-broadcast-upload-extension/Classes/ScreenBroadcaster.swift @@ -0,0 +1,32 @@ +// +// ScreenBroadcaster.swift +// +// Created by Tiago Daniel Jacobs on 27/03/22. +// +import os +import bigbluebutton_mobile_sdk_common + +open class ScreenBroadcaster { + // Logger (these messages are displayed in the console application) + private var logger = os.Logger(subsystem: "BigBlueButtonMobileSDK", category: "ScreenBroadcaster") + private var webRTCClient:WebRTCClient + + init() { + webRTCClient = WebRTCClient(iceServers: ["stun:stun.l.google.com:19302", + "stun:stun1.l.google.com:19302", + "stun:stun2.l.google.com:19302", + "stun:stun3.l.google.com:19302", + "stun:stun4.l.google.com:19302"]) + } + + public func createOffer() async -> String? { + do{ + let rtcSessionDescription = try await self.webRTCClient.offer() + return rtcSessionDescription.sdp + } catch { + logger.error("Error on webRTCClient.offer") + return nil + } + } + +} diff --git a/ios-broadcast-upload-extension/bigbluebutton-mobile-sdk-broadcast-upload-extension.podspec b/ios-broadcast-upload-extension/bigbluebutton-mobile-sdk-broadcast-upload-extension.podspec index ba35402..53e3b04 100644 --- a/ios-broadcast-upload-extension/bigbluebutton-mobile-sdk-broadcast-upload-extension.podspec +++ b/ios-broadcast-upload-extension/bigbluebutton-mobile-sdk-broadcast-upload-extension.podspec @@ -13,7 +13,7 @@ Pod::Spec.new do |s| s.platforms = { :ios => "14.7" } s.source = { :git => "https://github.com/bigbluebutton/bigbluebutton-mobile-sdk.git", :tag => "#{s.version}" } - s.source_files = "ios/**/*.{h,m,mm,swift}" + s.source_files = "Classes/*.{h,m,mm,swift}" s.dependency "WebRTC-lib" s.dependency "bigbluebutton-mobile-sdk-common" diff --git a/ios-common/Classes/WebRTCClient.swift b/ios-common/Classes/WebRTCClient.swift index 58764c5..ec199cf 100644 --- a/ios-common/Classes/WebRTCClient.swift +++ b/ios-common/Classes/WebRTCClient.swift @@ -13,7 +13,7 @@ protocol WebRTCClientDelegate: AnyObject { func webRTCClient(_ client: WebRTCClient, didChangeConnectionState state: RTCIceConnectionState) } -final class WebRTCClient: NSObject { +open class WebRTCClient: NSObject { // The `RTCPeerConnectionFactory` is in charge of creating new RTCPeerConnection instances. // A new RTCPeerConnection should be created every new call, but the factory is shared. @@ -40,7 +40,7 @@ final class WebRTCClient: NSObject { fatalError("WebRTCClient:init is unavailable") } - required init(iceServers: [String]) { + public required init(iceServers: [String]) { let config = RTCConfiguration() config.iceServers = [RTCIceServer(urlStrings: iceServers)] @@ -69,14 +69,11 @@ final class WebRTCClient: NSObject { // MARK: Signaling - func offer(completion: @escaping (_ sdp: RTCSessionDescription) -> Void) { + public func offer() async throws -> RTCSessionDescription { let constrains = RTCMediaConstraints(mandatoryConstraints: self.mediaConstrains, optionalConstraints: nil) - self.peerConnection.offer(for: constrains) { (sdp, error) in - guard let sdp = sdp else { return } - self.peerConnection.setLocalDescription(sdp, completionHandler: { (error) in - completion(sdp) - }) - } + let sdp = try await self.peerConnection.offer(for: constrains) + try await self.peerConnection.setLocalDescription(sdp) + return sdp } func set(remoteSdp: RTCSessionDescription, completion: @escaping (Error?) -> ()) { @@ -140,41 +137,41 @@ final class WebRTCClient: NSObject { extension WebRTCClient: RTCPeerConnectionDelegate { - func peerConnection(_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didChange stateChanged: RTCSignalingState) { debugPrint("peerConnection new signaling state: \(stateChanged)") } - func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didAdd stream: RTCMediaStream) { debugPrint("peerConnection did add stream \(stream)") } - func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didRemove stream: RTCMediaStream) { debugPrint("peerConnection did remove stream \(stream)") } - func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) { + public func peerConnectionShouldNegotiate(_ peerConnection: RTCPeerConnection) { debugPrint("peerConnection should negotiate") } - func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceConnectionState) { debugPrint("peerConnection new connection state: \(newState)") self.delegate?.webRTCClient(self, didChangeConnectionState: newState) } - func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didChange newState: RTCIceGatheringState) { debugPrint("peerConnection new gathering state: \(newState)") } - func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didGenerate candidate: RTCIceCandidate) { debugPrint("peerConnection discovered new candidate") self.delegate?.webRTCClient(self, didDiscoverLocalCandidate: candidate) } - func peerConnection(_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didRemove candidates: [RTCIceCandidate]) { debugPrint("peerConnection did remove candidate(s)") } - func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) { + public func peerConnection(_ peerConnection: RTCPeerConnection, didOpen dataChannel: RTCDataChannel) { debugPrint("peerConnection did open data channel") } } @@ -255,11 +252,11 @@ extension WebRTCClient { } extension WebRTCClient: RTCDataChannelDelegate { - func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) { + public func dataChannelDidChangeState(_ dataChannel: RTCDataChannel) { debugPrint("dataChannel did change state: \(dataChannel.readyState)") } - func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) { + public func dataChannel(_ dataChannel: RTCDataChannel, didReceiveMessageWith buffer: RTCDataBuffer) { debugPrint("dataChannel did receive message with buffer: \(buffer)") } } diff --git a/ios-common/bigbluebutton-mobile-sdk-common.podspec b/ios-common/bigbluebutton-mobile-sdk-common.podspec index 94e9b6b..1c7a708 100644 --- a/ios-common/bigbluebutton-mobile-sdk-common.podspec +++ b/ios-common/bigbluebutton-mobile-sdk-common.podspec @@ -13,7 +13,7 @@ Pod::Spec.new do |s| s.platforms = { :ios => "14.7" } s.source = { :git => "https://github.com/bigbluebutton/bigbluebutton-mobile-sdk.git", :tag => "#{s.version}" } - s.source_files = "ios/**/*.{h,m,mm,swift}" + s.source_files = "Classes/*.{h,m,mm,swift}" s.dependency "WebRTC-lib" end