/
DefaultAudioVideoController.swift
204 lines (177 loc) · 9.61 KB
/
DefaultAudioVideoController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// DefaultAudioVideoController.swift
// AmazonChimeSDK
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
import AVFoundation
import Foundation
@objcMembers public class DefaultAudioVideoController: NSObject, AudioVideoControllerFacade {
public let configuration: MeetingSessionConfiguration
public let logger: Logger
private let audioClientController: AudioClientController
private let audioClientObserver: AudioClientObserver
private let clientMetricsCollector: ClientMetricsCollector
private var videoClientController: VideoClientController
private let videoTileController: VideoTileController
private var primaryMeetingPromotionObserver: PrimaryMeetingPromotionObserver?
public init(audioClientController: AudioClientController,
audioClientObserver: AudioClientObserver,
clientMetricsCollector: ClientMetricsCollector,
videoClientController: VideoClientController,
videoTileController: VideoTileController,
configuration: MeetingSessionConfiguration,
logger: Logger) {
self.audioClientController = audioClientController
self.audioClientObserver = audioClientObserver
self.clientMetricsCollector = clientMetricsCollector
self.videoClientController = videoClientController
self.videoTileController = videoTileController
self.configuration = configuration
self.logger = logger
}
public func start() throws {
// By default, start for calls without CallKit integration. Use start(callKitEnabled:)
// to override the default behavior if the call is integrated with CallKit
try self.start(audioVideoConfiguration: AudioVideoConfiguration())
}
public func start(callKitEnabled: Bool) throws {
try self.start(audioVideoConfiguration: AudioVideoConfiguration(callKitEnabled: callKitEnabled))
}
public func start(audioVideoConfiguration: AudioVideoConfiguration) throws {
try audioClientController.start(audioFallbackUrl: configuration.urls.audioFallbackUrl,
audioHostUrl: configuration.urls.audioHostUrl,
meetingId: configuration.meetingId,
attendeeId: configuration.credentials.attendeeId,
joinToken: configuration.credentials.joinToken,
callKitEnabled: audioVideoConfiguration.callKitEnabled,
audioMode: audioVideoConfiguration.audioMode)
videoClientController.subscribeToVideoTileControllerObservers(observer: videoTileController)
videoClientController.start()
}
public func stop() {
audioClientController.stop()
videoClientController.stopAndDestroy()
}
public func addAudioVideoObserver(observer: AudioVideoObserver) {
audioClientObserver.subscribeToAudioClientStateChange(observer: observer)
videoClientController.subscribeToVideoClientStateChange(observer: observer)
}
public func removeAudioVideoObserver(observer: AudioVideoObserver) {
audioClientObserver.unsubscribeFromAudioClientStateChange(observer: observer)
videoClientController.unsubscribeFromVideoClientStateChange(observer: observer)
}
public func addMetricsObserver(observer: MetricsObserver) {
clientMetricsCollector.subscribeToMetrics(observer: observer)
}
public func removeMetricsObserver(observer: MetricsObserver) {
clientMetricsCollector.unsubscribeFromMetrics(observer: observer)
}
public func startLocalVideo() throws {
try videoClientController.startLocalVideo()
}
public func startLocalVideo(config: LocalVideoConfiguration) throws {
try videoClientController.startLocalVideo(config: config)
}
public func startLocalVideo(source: VideoSource) {
videoClientController.startLocalVideo(source: source)
}
public func startLocalVideo(source: VideoSource, config: LocalVideoConfiguration) {
videoClientController.startLocalVideo(source: source, config: config)
}
public func stopLocalVideo() {
videoClientController.stopLocalVideo()
}
public func startRemoteVideo() {
videoClientController.startRemoteVideo()
}
public func stopRemoteVideo() {
videoClientController.stopRemoteVideo()
}
public func updateVideoSourceSubscriptions(addedOrUpdated: Dictionary<RemoteVideoSource, VideoSubscriptionConfiguration>, removed: Array<RemoteVideoSource>) {
videoClientController.updateVideoSourceSubscriptions(addedOrUpdated: addedOrUpdated, removed: removed)
}
public func promoteToPrimaryMeeting(
credentials: MeetingSessionCredentials,
observer: PrimaryMeetingPromotionObserver) {
let group = DispatchGroup()
var videoClientPromotionStatus: MeetingSessionStatus?
let videoClientPromotionCallback = { (status: MeetingSessionStatus) -> Void in
self.logger.info(
msg: "Video primary meeting promotion has completed with status \(status.statusCode.description)")
videoClientPromotionStatus = status
group.leave()
}
let videoClientDemotionCallback = { (status: MeetingSessionStatus) -> Void in
self.logger.info(
msg: "Video primary meeting demotion has completed with status \(status.statusCode.description)")
self.audioClientController.demoteFromPrimaryMeeting()
observer.didDemoteFromPrimaryMeeting(status: status)
}
group.enter()
var audioClientPromotionStatus: MeetingSessionStatus?
let audioClientPromotionCallback = { (status: MeetingSessionStatus) -> Void in
self.logger.info(
msg: "Audio primary meeting promotion has completed with status \(status.statusCode.description)")
audioClientPromotionStatus = status
group.leave()
}
let audioClientDemotionCallback = { (status: MeetingSessionStatus) -> Void in
self.logger.info(
msg: "Audio primary meeting demotion has completed with status \(status.statusCode.description)")
self.videoClientController.demoteFromPrimaryMeeting()
observer.didDemoteFromPrimaryMeeting(status: status)
}
group.enter()
// In these observers we try demoting the other client. Note that the individual controllers
// do not follow the exact same pattern of calling back on observer (with `MeetingSessionStatusCode.OK` in
// the case of explicit demotion request so we don't need to worry about any infinite loops
class PrimaryMeetingPromotionObserverAdapter: PrimaryMeetingPromotionObserver {
var promotionCallback: (MeetingSessionStatus) -> Void
var demotionCallback: (MeetingSessionStatus) -> Void
init(promotionCallback: @escaping (MeetingSessionStatus) -> Void,
demotionCallback: @escaping (MeetingSessionStatus) -> Void) {
self.promotionCallback = promotionCallback
self.demotionCallback = demotionCallback
}
func didPromoteToPrimaryMeeting(status: MeetingSessionStatus) {
self.promotionCallback(status)
}
func didDemoteFromPrimaryMeeting(status: MeetingSessionStatus) {
self.demotionCallback(status)
}
}
let videoClientPromotionObserverAdapter = PrimaryMeetingPromotionObserverAdapter(
promotionCallback: videoClientPromotionCallback,
demotionCallback: videoClientDemotionCallback)
let audioClientPromotionObserverAdapter = PrimaryMeetingPromotionObserverAdapter(
promotionCallback: audioClientPromotionCallback,
demotionCallback: audioClientDemotionCallback)
primaryMeetingPromotionObserver = observer // Store for demotion
videoClientController.promoteToPrimaryMeeting(credentials: credentials,
observer: videoClientPromotionObserverAdapter)
audioClientController.promoteToPrimaryMeeting(credentials: credentials,
observer: audioClientPromotionObserverAdapter)
group.notify(queue: DispatchQueue.main) {
if let videoClientStatus = videoClientPromotionStatus, let audioClientStatus = audioClientPromotionStatus {
// Mux the statuses together, single failure is total failure
if videoClientStatus.statusCode != MeetingSessionStatusCode.ok {
self.audioClientController.demoteFromPrimaryMeeting() // Leave other controller
observer.didPromoteToPrimaryMeeting(status: videoClientStatus)
} else if audioClientStatus.statusCode != MeetingSessionStatusCode.ok {
self.videoClientController.demoteFromPrimaryMeeting() // Leave other controller
observer.didPromoteToPrimaryMeeting(status: audioClientStatus)
} else {
observer.didPromoteToPrimaryMeeting(status: videoClientStatus) // Just use video client status
}
}
}
}
public func demoteFromPrimaryMeeting() {
videoClientController.demoteFromPrimaryMeeting()
audioClientController.demoteFromPrimaryMeeting()
primaryMeetingPromotionObserver?.didDemoteFromPrimaryMeeting(
status: MeetingSessionStatus(statusCode: MeetingSessionStatusCode.ok))
}
}