diff --git a/Example/HMSSDKExample/Login/LoginViewController.swift b/Example/HMSSDKExample/Login/LoginViewController.swift index ff283da..98c90fb 100644 --- a/Example/HMSSDKExample/Login/LoginViewController.swift +++ b/Example/HMSSDKExample/Login/LoginViewController.swift @@ -57,6 +57,10 @@ final class LoginViewController: UIViewController { Utilities.drawCorner(on: joinMeetingButton) } } + + var shouldShowPreview: Bool { + UserDefaults.standard.object(forKey: Constants.showVideoPreview) == nil || UserDefaults.standard.bool(forKey: Constants.showVideoPreview) + } // MARK: - View Lifecycle @@ -170,20 +174,42 @@ final class LoginViewController: UIViewController { return } - guard let name = alertController.textFields?[0].text, !name.isEmpty, - let viewController = self.storyboard?.instantiateViewController(identifier: Constants.previewControllerIdentifier) as? PreJoinPreviewViewController - else { + guard let name = alertController.textFields?[0].text, !name.isEmpty else { dismiss(animated: true) let message = "Enter Name!" showErrorAlert(with: message) return } - - viewController.user = name - viewController.roomName = room - + save(name, room) - + + if shouldShowPreview { + preview(name: name, room: room) + } else { + join(name: name, room: room) + } + } + + private func join(name: String, room: String) { + guard let viewController = UIStoryboard(name: Constants.meeting, bundle: nil) + .instantiateInitialViewController() as? MeetingViewController + else { + return + } + + viewController.interactor = HMSSDKInteractor(for: name, in: room) + + navigationController?.pushViewController(viewController, animated: true) + } + + private func preview(name: String, room: String) { + guard let viewController = self.storyboard?.instantiateViewController(identifier: Constants.previewControllerIdentifier) as? PreJoinPreviewViewController + else { + return + } + + viewController.interactor = HMSSDKInteractor(for: name, in: room) + navigationController?.pushViewController(viewController, animated: true) } diff --git a/Example/HMSSDKExample/Login/PreJoinPreviewViewController.swift b/Example/HMSSDKExample/Login/PreJoinPreviewViewController.swift index 299d64d..8f1ab7a 100644 --- a/Example/HMSSDKExample/Login/PreJoinPreviewViewController.swift +++ b/Example/HMSSDKExample/Login/PreJoinPreviewViewController.swift @@ -9,9 +9,6 @@ import UIKit class PreJoinPreviewViewController: PreviewViewController { - - internal var user: String! - internal var roomName: String! @IBOutlet weak var networkQualityView: NetworkQualityView! @IBOutlet weak var peerListButton: UIButton! { didSet { @@ -32,6 +29,7 @@ class PreJoinPreviewViewController: PreviewViewController { joinButton.isEnabled = false setupInteractor() startObservingNotifications() + interactor.preview() UIApplication.shared.isIdleTimerDisabled = true } @@ -46,7 +44,6 @@ class PreJoinPreviewViewController: PreviewViewController { } private func setupInteractor() { - interactor = HMSSDKInteractor(for: user, in: roomName) {} interactor.onPreview = { [weak self] _, tracks in self?.setupTracks(tracks: tracks) self?.joinButton.isEnabled = true @@ -116,8 +113,6 @@ class PreJoinPreviewViewController: PreviewViewController { return } - viewController.user = user - viewController.roomName = roomName viewController.interactor = interactor navigationController?.pushViewController(viewController, animated: true) @@ -130,7 +125,6 @@ class PreJoinPreviewViewController: PreviewViewController { return } - viewController.roomName = roomName viewController.interactor = interactor present(viewController, animated: true) diff --git a/Example/HMSSDKExample/Meeting/Chat/ChatViewController.swift b/Example/HMSSDKExample/Meeting/Chat/ChatViewController.swift index 0994185..4427e88 100644 --- a/Example/HMSSDKExample/Meeting/Chat/ChatViewController.swift +++ b/Example/HMSSDKExample/Meeting/Chat/ChatViewController.swift @@ -221,6 +221,8 @@ extension ChatViewController { if let peer = message.sender { name = peer.name isLocal = (peer.peerID == interactor?.hmsSDK?.localPeer?.peerID) + } else { + name = "Bot" } guard let name = name else { return } diff --git a/Example/HMSSDKExample/Meeting/HMSSDKInteractor.swift b/Example/HMSSDKExample/Meeting/HMSSDKInteractor.swift index f315124..cf02759 100644 --- a/Example/HMSSDKExample/Meeting/HMSSDKInteractor.swift +++ b/Example/HMSSDKExample/Meeting/HMSSDKInteractor.swift @@ -12,6 +12,8 @@ import HMSSDK final class HMSSDKInteractor: HMSUpdateListener { private(set) var hmsSDK: HMSSDK? + let user: String + let room: String internal var onPreview: ((HMSRoom, [HMSTrack]) -> Void)? internal var onRoleChange: ((HMSRoleChangeRequest) -> Void)? @@ -49,20 +51,27 @@ final class HMSSDKInteractor: HMSUpdateListener { // MARK: - Setup SDK init(for user: String, - in room: String, - _ completion: @escaping () -> Void) { - + in room: String) { + self.user = user + self.room = room setupPlugins() - - RoomService.setup(for: user, room) { [weak self] token in - guard let token = token else { - print(#function, "Error fetching token") - return - } - - self?.setup(for: user, token: token, room) - - completion() + setupSDK() + } + + private func setupSDK() { + hmsSDK = HMSSDK.build { sdk in + sdk.appGroup = "group.live.100ms.videoapp" + + let videoSettings = HMSVideoTrackSettings(codec: .VP8, + resolution: .init(width: 320, height: 180), + maxBitrate: 512, + maxFrameRate: 25, + cameraFacing: .front, + trackDescription: "Just a normal video track", videoPlugins: self.videoPlugins) + + let audioSettings = HMSAudioTrackSettings(maxBitrate: 32, trackDescription: "Just a normal audio track") + sdk.trackSettings = HMSTrackSettings(videoSettings: videoSettings, audioSettings: audioSettings) + sdk.logger = self } } @@ -90,35 +99,47 @@ final class HMSSDKInteractor: HMSUpdateListener { } private func setup(for user: String, token: String, _ room: String) { + - hmsSDK = HMSSDK.build { sdk in - sdk.analyticsLevel = .verbose - sdk.appGroup = "group.live.100ms.videoapp" - - let videoSettings = HMSVideoTrackSettings(codec: .VP8, - resolution: .init(width: 320, height: 180), - maxBitrate: 512, - maxFrameRate: 25, - cameraFacing: .front, - trackDescription: "Just a normal video track", videoPlugins: self.videoPlugins) - - let audioSettings = HMSAudioTrackSettings(maxBitrate: 32, trackDescription: "Just a normal audio track") - sdk.trackSettings = HMSTrackSettings(videoSettings: videoSettings, audioSettings: audioSettings) - sdk.logger = self + + guard let config = config else { return } + hmsSDK?.join(config: config, delegate: self) + } + + func fetchConfig(completion: @escaping ((HMSConfig?) -> Void)) { + guard config == nil else { + completion(config) + return } + + RoomService.fetchToken(for: user, room) { [weak self] token in + guard let token = token, let self = self else { + print(#function, "Error fetching token") + completion(nil) + return + } - config = HMSConfig(userName: user, authToken: token, captureNetworkQualityInPreview: true) + self.config = HMSConfig(userName: self.user, authToken: token, captureNetworkQualityInPreview: true) - guard let config = config else { return } - hmsSDK?.preview(config: config, delegate: self) + completion(self.config) + } + } + + func preview() { + fetchConfig { [weak self] config in + guard let config = config, let self = self else { return } + self.hmsSDK?.preview(config: config, delegate: self) + } } - internal func join() { - guard let config = config else { return } - hmsSDK?.join(config: config, delegate: self) + func join() { + fetchConfig { [weak self] config in + guard let config = config, let self = self else { return } + self.hmsSDK?.join(config: config, delegate: self) + } } - internal func leave() { + func leave() { hmsSDK?.leave() } @@ -170,7 +191,7 @@ final class HMSSDKInteractor: HMSUpdateListener { messages.append(message) NotificationCenter.default.post(name: Constants.messageReceived, object: nil) - Utilities.showToast(message: "💬 \(message.sender!.name) sent you a message") + Utilities.showToast(message: "💬 \(message.sender?.name ?? "Unknown sender") sent you a message") } func on(updated speakers: [HMSSpeaker]) { diff --git a/Example/HMSSDKExample/Meeting/MeetingViewController.swift b/Example/HMSSDKExample/Meeting/MeetingViewController.swift index 0ec51eb..077e184 100644 --- a/Example/HMSSDKExample/Meeting/MeetingViewController.swift +++ b/Example/HMSSDKExample/Meeting/MeetingViewController.swift @@ -14,8 +14,14 @@ final class MeetingViewController: UIViewController { // MARK: - View Properties - internal var user: String! - internal var roomName: String! + var user: String { + interactor.user + } + + var roomName: String { + interactor.room + } + internal var interactor: HMSSDKInteractor! @IBOutlet weak var hlsContainer: UIView! @@ -116,6 +122,8 @@ final class MeetingViewController: UIViewController { viewModel?.showRoleChangePrompt = { [weak self] peer, force in self?.showRoleChangePrompt(for: peer, force: force) } + + interactor.join() } override func viewWillAppear(_ animated: Bool) { @@ -148,11 +156,13 @@ final class MeetingViewController: UIViewController { hlsController?.streamURL = nil hlsController?.stop() collectionView.isHidden = false + interactor.hmsSDK?.resumeAfterExternalAudioPlayback() return } collectionView.isHidden = true hlsContainer.isHidden = false + interactor.hmsSDK?.prepareForExternalAudioPlayback() hlsController?.streamURL = interactor?.hmsSDK?.room?.hlsStreamingState.variants.first?.url hlsController?.play() } diff --git a/Example/HMSSDKExample/Meeting/MeetingViewModel.swift b/Example/HMSSDKExample/Meeting/MeetingViewModel.swift index 0a66349..01ceb3e 100644 --- a/Example/HMSSDKExample/Meeting/MeetingViewModel.swift +++ b/Example/HMSSDKExample/Meeting/MeetingViewModel.swift @@ -98,7 +98,6 @@ final class MeetingViewModel: NSObject, self.interactor = interactor setupDataSource() - interactor.join() setup(collectionView) diff --git a/Example/HMSSDKExample/Meeting/Menus/RTMPSettingsViewController.swift b/Example/HMSSDKExample/Meeting/Menus/RTMPSettingsViewController.swift index 39e8e7e..47f7f5b 100644 --- a/Example/HMSSDKExample/Meeting/Menus/RTMPSettingsViewController.swift +++ b/Example/HMSSDKExample/Meeting/Menus/RTMPSettingsViewController.swift @@ -48,6 +48,13 @@ final class RTMPSettingsViewController: FormViewController { <<< SwitchRow("recordingSwitch") { $0.title = "Record" } + form +++ Section("") + <<< IntRow("rtmpResolutionHeight"){ + $0.title = "Resolution Height" + } + <<< IntRow("rtmpResolutionWidth") { + $0.title = "Resolution Width" + } form +++ Section("") <<< ButtonRow { $0.title = "Start" @@ -75,10 +82,17 @@ final class RTMPSettingsViewController: FormViewController { if let inputURLs = values["rtmpURLs"] as? [Any] { rtmpURLs = inputURLs.compactMap { $0 as? URL } } - + + var resolution: HMSVideoResolution? + if let height = values["rtmpResolutionHeight"] as? Int, + let width = values["rtmpResolutionWidth"] as? Int, + height > 0 && width > 0 { + resolution = HMSVideoResolution(width: width, height: height) + } + let recordingEnabled = (values["recordingSwitch"] as? Bool) ?? false - let config = HMSRTMPConfig(meetingURL: meetingURL, rtmpURLs: rtmpURLs, record: recordingEnabled) + let config = HMSRTMPConfig(meetingURL: meetingURL, rtmpURLs: rtmpURLs, record: recordingEnabled, resolution: resolution) delegate?.rtmpSettingsController(self, didSelect: config) navigationController?.popViewController(animated: true) } diff --git a/Example/HMSSDKExample/Meeting/Peers List/PreviewPeersListViewController.swift b/Example/HMSSDKExample/Meeting/Peers List/PreviewPeersListViewController.swift index f86b14a..7b3c5f5 100644 --- a/Example/HMSSDKExample/Meeting/Peers List/PreviewPeersListViewController.swift +++ b/Example/HMSSDKExample/Meeting/Peers List/PreviewPeersListViewController.swift @@ -15,7 +15,9 @@ final class PreviewPeersListViewController: UIViewController { @IBOutlet private weak var table: UITableView! - internal var roomName: String! + var roomName: String { + interactor.room + } @IBOutlet weak var roomNameButton: UIButton! { didSet { diff --git a/Example/HMSSDKExample/Meeting/RoomService.swift b/Example/HMSSDKExample/Meeting/RoomService.swift index 13a480b..2202b8f 100644 --- a/Example/HMSSDKExample/Meeting/RoomService.swift +++ b/Example/HMSSDKExample/Meeting/RoomService.swift @@ -9,19 +9,9 @@ import Foundation struct RoomService { - - static func setup(for user: String, - _ room: String, - completion: @escaping (String?) -> Void) { - - getToken(for: user, room) { (token) in - completion(token) - } - } - // MARK: - Room Token - private static func getToken(for user: String, + static func fetchToken(for user: String, _ room: String, completion: @escaping (String?) -> Void) { @@ -43,7 +33,7 @@ struct RoomService { } } - private static func requestToken(for user: String, + static func requestToken(for user: String, _ roomID: String, completion: @escaping (String?, Error?) -> Void) { diff --git a/Example/HMSSDKExample/Settings/Base.lproj/Settings.storyboard b/Example/HMSSDKExample/Settings/Base.lproj/Settings.storyboard index 4d5d43f..14a5687 100644 --- a/Example/HMSSDKExample/Settings/Base.lproj/Settings.storyboard +++ b/Example/HMSSDKExample/Settings/Base.lproj/Settings.storyboard @@ -1,9 +1,9 @@ - + - + @@ -190,8 +190,8 @@ -