Skip to content

Instantly share code, notes, and snippets.

@maxxfrazer
Last active December 17, 2020 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxxfrazer/5d05606893a66c94eff57f4d8efff645 to your computer and use it in GitHub Desktop.
Save maxxfrazer/5d05606893a66c94eff57f4d8efff645 to your computer and use it in GitHub Desktop.
Create the ChannelViewController, for displaying the broadcasting members of the channel
import UIKit
import AgoraRtcKit
class ChannelViewController: UIViewController {
static let channelName: String = <#Static Channel Name#>
static let appId: String = <#Agora App ID#>
/// Using an app without a token requirement in this example.
static var channelToken: String? = nil
/// Setting to zero will tell Agora to assign one for you
lazy var userID: UInt = 0
/// Role of the local user, `.audience` by default here.
var userRole: AgoraClientRole = .audience
/// Creates the AgoraRtcEngineKit, with default role as `.audience` above.
lazy var agkit: AgoraRtcEngineKit = {
let engine = AgoraRtcEngineKit.sharedEngine(
withAppId: ChannelViewController.appId,
delegate: self
)
engine.setChannelProfile(.liveBroadcasting)
engine.setClientRole(self.userRole)
return engine
}()
var hostButton: UIButton?
var closeButton: UIButton?
/// UIView for holding all the camera feeds from broadcasters.
var agoraVideoHolder = UIView()
/// IDs of the broadcaster(s)
var remoteUserIDs: Set<UInt> = []
/// Dictionary to find the Video Canvas for a user by ID.
var userVideoLookup: [UInt: AgoraRtcVideoCanvas] = [:] {
didSet {
reorganiseVideos()
}
}
/// Local video UIView, used only when broadcasting
lazy var localVideoView: UIView = {
let vview = UIView()
return vview
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.agoraVideoHolder)
self.joinChannel()
}
/// Join the pre-configured Agora channel
func joinChannel() {
self.agkit.enableVideo()
self.agkit.joinChannel(
byToken: ChannelViewController.channelToken,
channelId: ChannelViewController.channelName,
info: nil, uid: self.userID
) { [weak self] _, uid, _ in
self?.userID = uid
// Add button to toggle broadcaster/audience
self?.getHostButton().isHidden = false
// Add button to close the view
self?.getCloseButton().isHidden = false
}
}
required init() {
super.init(nibName: nil, bundle: nil)
self.modalPresentationStyle = .fullScreen
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Add Close and Host buttons to the scene
@discardableResult
func getCloseButton() -> UIButton {
if let closeButton = self.closeButton {
return closeButton
}
guard let chevronSymbol = UIImage(systemName: "chevron.left") else {
fatalError("Could not create chevron.left symbol")
}
let button = UIButton.systemButton(with: chevronSymbol, target: self, action: #selector(leaveChannel))
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
[
button.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 20),
button.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20)
].forEach { $0.isActive = true}
self.closeButton = button
return button
}
@discardableResult
func getHostButton() -> UIButton {
if let hostButton = self.hostButton {
return hostButton
}
let button = UIButton(type: .custom)
button.setTitle("Host", for: .normal)
button.setTitleColor(.label, for: .normal)
button.setTitleColor(.secondaryLabel, for: .focused)
button.addTarget(self, action: #selector(toggleBroadcast), for: .touchUpInside)
self.view.addSubview(button)
button.frame = CGRect(
origin: CGPoint(x: self.view.bounds.midX - 75, y: 50),
size: CGSize(width: 150, height: 50)
)
button.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin]
button.backgroundColor = .systemRed
button.layer.cornerRadius = 25
self.hostButton = button
return button
}
/// Toggle between being a host or a member of the audience.
/// On changing to being a broadcaster, the app first checks
/// that it has access to both the microphone and camera on the device.
@objc func toggleBroadcast() {
// Swap the userRole
self.userRole = self.userRole == .audience ? .broadcaster : .audience
self.agkit.setClientRole(self.userRole)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment