Skip to content

Commit

Permalink
[Fix]Pass all available parameters to connect (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipavlidakis committed Sep 26, 2024
1 parent 0be2618 commit 6a52f95
Show file tree
Hide file tree
Showing 28 changed files with 492 additions and 253 deletions.
2 changes: 0 additions & 2 deletions Sources/StreamVideo/Call.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ public class Call: @unchecked Sendable, WSEventsSubscriber {
guard let self else { throw ClientError.Unexpected() }
let response = try await callController.joinCall(
create: create,
callType: callType,
callId: callId,
callSettings: callSettings,
options: options,
ring: ring,
Expand Down
111 changes: 35 additions & 76 deletions Sources/StreamVideo/Controllers/CallController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ class CallController: @unchecked Sendable {
callCid: callCid(from: callId, callType: callType),
videoConfig: videoConfig
) {
[weak self, callId] create, ring, migratingFrom in
[weak self, callId] create, ring, migratingFrom, notify, options in
if let self {
return try await authenticateCall(
create: create,
ring: ring,
migratingFrom: migratingFrom
migratingFrom: migratingFrom,
notify: notify,
options: options
)
} else {
throw ClientError("Unable to authenticate callId:\(callId).")
Expand Down Expand Up @@ -112,18 +114,17 @@ class CallController: @unchecked Sendable {
@discardableResult
func joinCall(
create: Bool = true,
callType: String,
callId: String,
callSettings: CallSettings?,
options: CreateCallOptions? = nil,
migratingFrom: String? = nil,
sessionID: String? = nil,
ring: Bool = false,
notify: Bool = false
) async throws -> JoinCallResponse {
try await webRTCCoordinator.connect(
create: create,
callSettings: callSettings,
ring: ring
options: options,
ring: ring,
notify: notify
)
guard
let response = try await joinCallResponseSubject
Expand Down Expand Up @@ -447,42 +448,40 @@ class CallController: @unchecked Sendable {
.store(in: disposableBag)
}

private func joinCall(
private func authenticateCall(
create: Bool,
callType: String,
callId: String,
options: CreateCallOptions? = nil,
migratingFrom: String?,
ring: Bool,
notify: Bool
migratingFrom: String?,
notify: Bool,
options: CreateCallOptions?
) async throws -> JoinCallResponse {
let location = try await getLocation()
let response = try await joinCall(
callId: callId,
type: callType,
location: location,
options: options,
migratingFrom: migratingFrom,
create: create,
ring: ring,
notify: notify
var membersRequest = [MemberRequest]()
options?.memberIds?.forEach {
membersRequest.append(.init(userId: $0))
}
options?.members?.forEach {
membersRequest.append($0)
}
let callRequest = CallRequest(
custom: options?.custom,
members: membersRequest,
settingsOverride: options?.settings,
startsAt: options?.startsAt,
team: options?.team
)
return response
}

private func authenticateCall(
create: Bool,
ring: Bool,
migratingFrom: String?
) async throws -> JoinCallResponse {
let response = try await joinCall(
let joinCall = JoinCallRequest(
create: create,
callType: callType,
callId: callId,
options: nil,
data: callRequest,
location: location,
migratingFrom: migratingFrom,
ring: ring,
notify: false
notify: notify,
ring: ring
)
let response = try await defaultAPI.joinCall(
type: callType,
id: callId,
joinCallRequest: joinCall
)

// We allow the CallController to manage its state.
Expand All @@ -509,46 +508,6 @@ class CallController: @unchecked Sendable {
return try await LocationFetcher.getLocation()
}

private func joinCall(
callId: String,
type: String,
location: String,
options: CreateCallOptions? = nil,
migratingFrom: String?,
create: Bool,
ring: Bool,
notify: Bool
) async throws -> JoinCallResponse {
var membersRequest = [MemberRequest]()
options?.memberIds?.forEach {
membersRequest.append(.init(userId: $0))
}
options?.members?.forEach {
membersRequest.append($0)
}
let callRequest = CallRequest(
custom: options?.custom,
members: membersRequest,
settingsOverride: options?.settings,
startsAt: options?.startsAt,
team: options?.team
)
let joinCall = JoinCallRequest(
create: create,
data: callRequest,
location: location,
migratingFrom: migratingFrom,
notify: notify,
ring: ring
)
let joinCallResponse = try await defaultAPI.joinCall(
type: type,
id: callId,
joinCallRequest: joinCall
)
return joinCallResponse
}

private func didFetch(_ response: JoinCallResponse) async {
let sessionId = await webRTCCoordinator.stateAdapter.sessionID
currentSFU = response.credentials.server.edgeName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ extension WebRTCCoordinator.StateMachine.Stage {
/// - ring: A Boolean indicating whether to ring the other participants.
/// - Returns: A `ConnectingStage` instance representing the connecting
/// state of the WebRTC coordinator.
/// - Important: When transitioning from `.rejoining` values for ``ring``,
/// ``notify`` & ``options`` are nullified as are not relevant to the `rejoining` flow.
static func connecting(
_ context: Context,
ring: Bool
create: Bool,
options: CreateCallOptions?,
ring: Bool,
notify: Bool
) -> WebRTCCoordinator.StateMachine.Stage {
ConnectingStage(
context,
ring: ring
create: create,
options: options,
ring: ring,
notify: notify
)
}
}
Expand All @@ -31,18 +39,27 @@ extension WebRTCCoordinator.StateMachine.Stage {
WebRTCCoordinator.StateMachine.Stage,
@unchecked Sendable
{
let create: Bool
let options: CreateCallOptions?
/// Indicates whether to ring the other participants.
let ring: Bool
let notify: Bool

/// Initializes a new instance of `ConnectingStage`.
/// - Parameters:
/// - context: The context for the connecting stage.
/// - ring: A Boolean indicating whether to ring other participants.
init(
_ context: Context,
ring: Bool
create: Bool,
options: CreateCallOptions?,
ring: Bool,
notify: Bool
) {
self.create = create
self.options = options
self.ring = ring
self.notify = notify
super.init(id: .connecting, context: context)
}

Expand All @@ -52,16 +69,35 @@ extension WebRTCCoordinator.StateMachine.Stage {
/// occurring.
/// - Returns: This `ConnectingStage` instance if the transition is
/// valid, otherwise `nil`.
/// - Important: When transitioning from `.rejoining` values for ``ring``,
/// ``notify`` & ``options`` are nullified as are not relevant to the `rejoining` flow.
/// - Note: Valid transition from: `.idle`, `.rejoining`
override func transition(
from previousStage: WebRTCCoordinator.StateMachine.Stage
) -> Self? {
switch previousStage.id {
case .idle:
execute(create: true, updateSession: false)
execute(
create: create,
ring: ring,
notify: notify,
options: options,
updateSession: false
)
return self
case .rejoining:
execute(create: false, updateSession: true)
if ring || notify || options != nil {
log.assert(ring == false, "Ring cannot be true when rejoining.")
log.assert(notify == false, "Notfiy cannot be true when rejoining.")
log.assert(options == nil, "CreateCallOptions cannot be non-nil when rejoining.")
}
execute(
create: false,
ring: false,
notify: false,
options: nil,
updateSession: true
)
return self
default:
return nil
Expand All @@ -71,9 +107,19 @@ extension WebRTCCoordinator.StateMachine.Stage {
/// Executes the call connecting process.
/// - Parameters:
/// - create: A Boolean indicating whether to create a new session.
/// - ring: A Boolean indicating whether to ring other participants.
/// - notify: A Boolean indicating whether to notify other participants.
/// - options: A `CreateCallOptions` instance to provide additional informations when
/// creating a call.
/// - updateSession: A Boolean indicating whether to update the
/// existing session.
private func execute(create: Bool, updateSession: Bool) {
private func execute(
create: Bool,
ring: Bool,
notify: Bool,
options: CreateCallOptions?,
updateSession: Bool
) {
Task { [weak self] in
guard let self else { return }
do {
Expand All @@ -97,7 +143,9 @@ extension WebRTCCoordinator.StateMachine.Stage {
coordinator: coordinator,
currentSFU: nil,
create: create,
ring: ring
ring: ring,
notify: notify,
options: options
)

/// We provide the ``SFUAdapter`` to the authenticator which will ensure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ extension WebRTCCoordinator.StateMachine.Stage {
coordinator: coordinator,
currentSFU: context.currentSFU,
create: false,
ring: false
ring: false,
notify: false,
options: nil
)

/// We provide the ``SFUAdapter`` to the authenticator which will ensure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ extension WebRTCCoordinator.StateMachine.Stage {
try transition?(
.connecting(
context,
ring: false
create: false,
options: nil,
ring: false,
notify: false
)
)
} catch {
Expand Down
16 changes: 13 additions & 3 deletions Sources/StreamVideo/WebRTC/v2/WebRTCAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ protocol WebRTCAuthenticating {
coordinator: WebRTCCoordinator,
currentSFU: String?,
create: Bool,
ring: Bool
ring: Bool,
notify: Bool,
options: CreateCallOptions?
) async throws -> (sfuAdapter: SFUAdapter, response: JoinCallResponse)

/// Awaits the SFU to allow authentication
Expand Down Expand Up @@ -48,10 +50,18 @@ struct WebRTCAuthenticator: WebRTCAuthenticating {
coordinator: WebRTCCoordinator,
currentSFU: String?,
create: Bool,
ring: Bool
ring: Bool,
notify: Bool,
options: CreateCallOptions?
) async throws -> (sfuAdapter: SFUAdapter, response: JoinCallResponse) {
let response = try await coordinator
.callAuthentication(create, ring, currentSFU)
.callAuthentication(
create,
ring,
currentSFU,
notify,
options
)

await coordinator.stateAdapter.set(
token: response.credentials.token
Expand Down
23 changes: 20 additions & 3 deletions Sources/StreamVideo/WebRTC/v2/WebRTCCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ final class WebRTCCoordinator: @unchecked Sendable {
/// - create: Whether the call should be created on the backend side.
/// - ring: Whether the call is a ringing call.
/// - migratingFrom: If migrating, where are we migrating from.
/// - notify:
/// - options:
/// - Returns: A `JoinCallResponse` wrapped in an async throw.
typealias AuthenticationHandler = (Bool, Bool, String?) async throws -> JoinCallResponse
typealias AuthenticationHandler = (
Bool,
Bool,
String?,
Bool,
CreateCallOptions?
) async throws -> JoinCallResponse

private static let recordingUserId = "recording-egress"
private static let participantsThreshold = 10
Expand Down Expand Up @@ -81,12 +89,21 @@ final class WebRTCCoordinator: @unchecked Sendable {
/// - callSettings: Optional call settings.
/// - ring: Boolean flag indicating if a ring tone should be played.
func connect(
create: Bool = true,
callSettings: CallSettings?,
ring: Bool
options: CreateCallOptions?,
ring: Bool,
notify: Bool
) async throws {
await stateAdapter.set(initialCallSettings: callSettings)
try stateMachine.transition(
.connecting(stateMachine.currentStage.context, ring: ring)
.connecting(
stateMachine.currentStage.context,
create: create,
options: options,
ring: ring,
notify: notify
)
)
}

Expand Down
Loading

0 comments on commit 6a52f95

Please sign in to comment.