Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Join Methods #17

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example-App/Docs-Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/AgoraIO/AgoraRtcEngine_iOS";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 4.2.0;
kind = exactVersion;
version = 4.2.2;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
"virtual-background-title" = "Virtual background";
"permissions-error" = "Camera and microphone permissions were not granted.\nCheck your security settings and try again.";
"params-continue-button" = "Continue";
"invalid-permissions" = "Camera and microphone permissions were not granted.\nCheck your security settings and try again.";

99 changes: 92 additions & 7 deletions agora-manager/AgoraManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ open class AgoraManager: NSObject, ObservableObject {
_ channel: String, token: String? = nil, uid: UInt = 0, info: String? = nil
) async -> Int32 {
if await !AgoraManager.checkForPermissions() {
DispatchQueue.main.async {
self.label = """
Camera and microphone permissions were not granted.
Check your security settings and try again.
"""
}
await self.updateLabel(key: "invalid-permissions")
return -3
}

Expand All @@ -80,7 +75,86 @@ open class AgoraManager: NSObject, ObservableObject {
)
}

/// Joins a video call, establishing a connection for video communication.
/// - Parameters:
/// - channel: Name of the channel to join.
/// - token: Token to join the channel, this can be nil for a weak security testing session.
/// - uid: User ID of the local user. This can be 0 to allow the engine to automatically assign an ID.
/// - Returns: Error code, 0 = success, < 0 = failure.
@discardableResult
func joinVideoCall(
_ channel: String, token: String? = nil, uid: UInt = 0
) async -> Int32 {
/// See ``AgoraManager/checkForPermissions()``, or Apple's docs for details of this method.
if await !AgoraManager.checkForPermissions() {
await self.updateLabel(key: "invalid-permissions")
return -3
}

let opt = AgoraRtcChannelMediaOptions()
opt.channelProfile = .communication

return self.agoraEngine.joinChannel(
byToken: token, channelId: channel,
uid: uid, mediaOptions: opt
)
}

/// Joins a voice call, establishing a connection for audio communication.
/// - Parameters:
/// - channel: Name of the channel to join.
/// - token: Token to join the channel, this can be nil for a weak security testing session.
/// - uid: User ID of the local user. This can be 0 to allow the engine to automatically assign an ID.
/// - Returns: Error code, 0 = success, < 0 = failure.
@discardableResult
func joinVoiceCall(
_ channel: String, token: String? = nil, uid: UInt = 0
) async -> Int32 {
/// See ``AgoraManager/checkForPermissions()``, or Apple's docs for details of this method.
if await !AgoraManager.checkForPermissions() {
await self.updateLabel(key: "invalid-permissions")
return -3
}

let opt = AgoraRtcChannelMediaOptions()
opt.channelProfile = .communication

return self.agoraEngine.joinChannel(
byToken: token, channelId: channel,
uid: uid, mediaOptions: opt
)
}

/// Joins a broadcast stream, enabling broadcasting or audience mode.
/// - Parameters:
/// - channel: Name of the channel to join.
/// - token: Token to join the channel, this can be nil for a weak security testing session.
/// - uid: User ID of the local user. This can be 0 to allow the engine to automatically assign an ID.
/// - isBroadcaster: Flag to indicate if the user is joining as a broadcaster (true) or audience (false).
/// Defaults to true.
/// - Returns: Error code, 0 = success, < 0 = failure.
@discardableResult
func joinBroadcastStream(
_ channel: String, token: String? = nil,
uid: UInt = 0, isBroadcaster: Bool = true
) async -> Int32 {
/// See ``AgoraManager/checkForPermissions()``, or Apple's docs for details of this method.
if isBroadcaster, await !AgoraManager.checkForPermissions() {
await self.updateLabel(key: "invalid-permissions")
return -3
}

let opt = AgoraRtcChannelMediaOptions()
opt.channelProfile = .liveBroadcasting
opt.clientRoleType = isBroadcaster ? .broadcaster : .audience
opt.audienceLatencyLevel = isBroadcaster ? .ultraLowLatency : .lowLatency

return self.agoraEngine.joinChannel(
byToken: token, channelId: channel,
uid: uid, mediaOptions: opt
)
}

/// This method is used by this app specifically. If there is a tokenURL,
/// it will attempt to retrieve a token from there.
/// Otherwise it will simply apply the provided token in config.json or nil.
Expand All @@ -89,6 +163,7 @@ open class AgoraManager: NSObject, ObservableObject {
/// - channel: Name of the channel to join.
/// - uid: User ID of the local user. This can be 0 to allow the engine to automatically assign an ID.
/// - Returns: Error code, 0 = success, < 0 = failure.
@discardableResult
internal func joinChannel(_ channel: String, uid: UInt? = nil) async -> Int32 {
let userId = uid ?? DocsAppConfig.shared.uid
var token = DocsAppConfig.shared.rtcToken
Expand All @@ -99,7 +174,7 @@ open class AgoraManager: NSObject, ObservableObject {
role: self.role, userId: userId
)
} catch {
print("token server fetch failed: \(error.localizedDescription)")
await self.updateLabel(to: "token server fetch failed: \(error.localizedDescription)")
}
}
return await self.joinChannel(channel, token: token, uid: userId, info: nil)
Expand Down Expand Up @@ -135,6 +210,16 @@ open class AgoraManager: NSObject, ObservableObject {
self.appId = appId
self.role = role
}

@MainActor
func updateLabel(to message: String) {
self.label = message
}

@MainActor
func updateLabel(key: String) {
self.label = NSLocalizedString(key, comment: "")
}
}

// MARK: - Delegate Methods
Expand Down
19 changes: 14 additions & 5 deletions get-started-sdk/GettingStartedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ public struct GettingStartedView: View {
}
ToastView(message: $agoraManager.label)
}.onAppear {
await agoraManager.joinChannel(
DocsAppConfig.shared.channel,
token: DocsAppConfig.shared.rtcToken,
uid: DocsAppConfig.shared.uid
)
let channel = DocsAppConfig.shared.channel
let token = DocsAppConfig.shared.rtcToken
let uid = DocsAppConfig.shared.uid
switch DocsAppConfig.shared.product {
case .rtc:
await agoraManager.joinVideoCall(channel, token: token, uid: uid)
case .ils:
await agoraManager.joinBroadcastStream(
channel, token: token, uid: uid,
isBroadcaster: true
)
case .voice:
await agoraManager.joinVoiceCall(channel, token: token, uid: uid)
}
}.onDisappear {
agoraManager.leaveChannel()
}
Expand Down