Skip to content
Open
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
59 changes: 45 additions & 14 deletions Swift/KVSiOSApp/ChannelConfigurationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
var isMaster: Bool = false
var signalingConnected: Bool = false
var selectedResolution: VideoResolution = .resolution720p
var useDualStackKvsEndpoint: Bool = false

// clients for WEBRTC Connection
var signalingClient: SignalingClient?
Expand All @@ -53,6 +54,7 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
@IBOutlet var channelName: UITextField!
@IBOutlet var clientID: UITextField!
@IBOutlet var regionName: UITextField!
@IBOutlet var useDualStackKvsEndpointSwitch: UISwitch!
@IBOutlet var isAudioEnabled: UISwitch!
@IBOutlet var isVideoEnabled: UISwitch!
@IBOutlet var resolutionButton: UIButton!
Expand Down Expand Up @@ -80,6 +82,33 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
return AWSMobileClient.default()
}

// Helper function to get KVS endpoint override if specified.
private func getKvsEndpointOverride()-> String? {

// Environment variable override takes precedence.
if let envVarOverride = kvsControlPlaneOverride, !envVarOverride.isEmpty {
return envVarOverride
}

// If using dual-stack endpoint format, construct the appropriate endpoint using the region name.
if self.useDualStackKvsEndpoint {

guard let regionText = self.regionName.text?.trim(),
!regionText.isEmpty else {
return nil
}

if regionText.hasPrefix("cn-") {
return String(format: PROD_CONTROL_PLANE_ENDPOINT_FORMAT_DUAL_STACK_CN, regionText)
}

return String(format: PROD_CONTROL_PLANE_ENDPOINT_FORMAT_DUAL_STACK, regionText)
}

return nil
}


override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
self.signalingConnected = false
Expand Down Expand Up @@ -113,20 +142,16 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
navigationController?.setToolbarHidden(false, animated: true)
}

@IBAction func kvsEndpointStateChanged(sender: UISwitch!) {
self.useDualStackKvsEndpoint = sender.isOn
}

@IBAction func audioStateChanged(sender: UISwitch!) {
if sender.isOn {
self.sendAudioEnabled = true
} else {
self.sendAudioEnabled = false
}
self.sendAudioEnabled = sender.isOn
}

@IBAction func videoStateChanged(sender: UISwitch!) {
if sender.isOn {
self.sendVideoEnabled = true
} else {
self.sendVideoEnabled = false
}
self.sendVideoEnabled = sender.isOn
}

@IBAction func resolutionButtonTapped(_ sender: UIButton) {
Expand Down Expand Up @@ -221,7 +246,7 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
}
// Kinesis Video Client Configuration
let configuration = AWSServiceConfiguration(region: awsRegionType,
endpoint: kvsControlPlaneOverride.map { AWSEndpoint(urlString: $0) },
endpoint: getKvsEndpointOverride().map { AWSEndpoint(urlString: $0) },
credentialsProvider: getCredentialsProvider())
AWSKinesisVideo.register(with: configuration!, forKey: awsKinesisVideoKey)

Expand Down Expand Up @@ -252,7 +277,7 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
}

// get signalling channel endpoints
let endpoints = getSignallingEndpoints(channelARN: channelARN!, region: awsRegionValue, isMaster: self.isMaster, useStorageSession: useStorageSession)
let endpoints = getSignallingEndpoints(channelARN: channelARN!, awsRegionValue: awsRegionValue, isMaster: self.isMaster, useStorageSession: useStorageSession)
//// Ensure that the WebSocket (WSS) endpoint is available; WebRTC requires a valid signaling endpoint.
if endpoints["WSS"] == nil {
popUpError(title: "Invalid SignallingEndpoints", message: "SignallingEndpoints is required for WebRTC connection")
Expand Down Expand Up @@ -385,7 +410,13 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
func getIceCandidates(channelARN: String, endpoint: AWSEndpoint, regionType: AWSRegionType, clientId: String) -> [RTCIceServer] {
var RTCIceServersList = [RTCIceServer]()
// TODO: don't use the self.regionName.text!
let kvsStunUrlStrings = ["stun:stun.kinesisvideo." + self.regionName.text! + ".amazonaws.com:443"]

let kvsStunUrlStrings: [String]
if self.useDualStackKvsEndpoint {
kvsStunUrlStrings = ["stun:stun.kinesisvideo." + self.regionName.text! + ".api.aws:443"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also CN variant as well

} else {
kvsStunUrlStrings = ["stun:stun.kinesisvideo." + self.regionName.text! + ".amazonaws.com:443"]
}
/*
equivalent AWS CLI command:
aws kinesis-video-signaling get-ice-server-config --channel-arn channelARN --client-id clientId --region cognitoIdentityUserPoolRegion
Expand Down Expand Up @@ -418,7 +449,7 @@ class ChannelConfigurationViewController: UIViewController, UITextFieldDelegate
}

// Get signalling endpoints for the given signalling channel ARN
func getSignallingEndpoints(channelARN: String, region: String, isMaster: Bool, useStorageSession: Bool) -> Dictionary<String, String?> {
func getSignallingEndpoints(channelARN: String, awsRegionValue: String, isMaster: Bool, useStorageSession: Bool) -> Dictionary<String, String?> {

var endpoints = Dictionary <String, String?>()
/*
Expand Down
4 changes: 4 additions & 0 deletions Swift/KVSiOSApp/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ let awsAccessKey: String? = ProcessInfo.processInfo.environment["AWS_ACCESS_KEY_
let awsSecretKey: String? = ProcessInfo.processInfo.environment["AWS_SECRET_ACCESS_KEY"]
let awsSessionToken: String? = ProcessInfo.processInfo.environment["AWS_SESSION_TOKEN"]
let kvsControlPlaneOverride: String? = ProcessInfo.processInfo.environment["CONTROL_PLANE_URI"]

// Dual-stack endpoint formats.
let PROD_CONTROL_PLANE_ENDPOINT_FORMAT_DUAL_STACK = "https://kinesisvideo.%@.api.aws"
let PROD_CONTROL_PLANE_ENDPOINT_FORMAT_DUAL_STACK_CN = "https://kinesisvideo.%@.api.amazonwebservices.com.cn"
Loading
Loading