This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QIVoxClientManager.m
159 lines (128 loc) · 4.82 KB
/
QIVoxClientManager.m
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
/*
* Copyright (c) 2011-2019, Zingaya, Inc. All rights reserved.
*/
#import "QIVoxClientManager.h"
@interface QIVoxClientManager () <VIClientSessionDelegate, VIClientCallManagerDelegate>
@property (strong, nonatomic) VIClient *client;
@property (strong, nonatomic) NSHashTable *listeners;
@property (copy, nonatomic) NSString *username;
@property (copy, nonatomic) NSString *password;
@property (strong, nonatomic, readwrite) VICall *currentCall;
@end
@implementation QIVoxClientManager
- (instancetype)initWithClient:(VIClient *)client {
self = [super init];
if (self) {
_client = client;
_client.sessionDelegate = self;
_client.callManagerDelegate = self;
_listeners = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory];
}
return self;
}
- (void)addListener:(id<QIVoxClientManagerListener>)listener {
[_listeners addObject:listener];
}
- (void)removeListener:(id<QIVoxClientManagerListener>)listener {
[_listeners removeObject:listener];
}
- (VILoginSuccess)loginSuccess {
__weak QIVoxClientManager *weakSelf = self;
return ^(NSString *_Nonnull displayName, VIAuthParams *_Nullable authParams) {
__strong QIVoxClientManager *strongSelf = weakSelf;
for (id<QIVoxClientManagerListener> listener in strongSelf.listeners) {
if ([listener respondsToSelector:@selector(loginDidSucceedWithName:)]) {
[listener loginDidSucceedWithName:displayName];
}
}
};
}
- (VILoginFailure)loginFailure {
__weak QIVoxClientManager *weakSelf = self;
return ^(NSError * _Nonnull error) {
__strong QIVoxClientManager *strongSelf = weakSelf;
for (id<QIVoxClientManagerListener> listener in strongSelf.listeners) {
if ([listener respondsToSelector:@selector(loginDidFailWithError:)]) {
[listener loginDidFailWithError:error];
}
}
};
}
- (void)loginWithUsername:(NSString *)username andPassword:(NSString *)password {
_username = username;
_password = password;
if (_client) {
if (_client.clientState == VIClientStateDisconnected) {
[_client connect];
} else {
[_client loginWithUser:_username
password:_password
success:self.loginSuccess
failure:self.loginFailure];
}
}
}
- (void)logout {
if (_client && _client.clientState != VIClientStateDisconnected) {
[_client disconnect];
}
}
- (void)client:(nonnull VIClient *)client sessionDidFailConnectWithError:(nonnull NSError *)error {
for (id<QIVoxClientManagerListener> listener in _listeners) {
if ([listener respondsToSelector:@selector(connectionDidClose:)]) {
[listener connectionDidClose:error];
}
}
}
- (void)clientSessionDidConnect:(nonnull VIClient *)client {
if ([_client isEqual:client]) {
[_client loginWithUser:_username
password:_password
success:self.loginSuccess
failure:self.loginFailure];
}
}
- (void)clientSessionDidDisconnect:(nonnull VIClient *)client {
for (id<QIVoxClientManagerListener> listener in _listeners) {
if ([listener respondsToSelector:@selector(connectionDidClose:)]) {
[listener connectionDidClose:nil];
}
}
}
- (void)client:(nonnull VIClient *)client didReceiveIncomingCall:(nonnull VICall *)call withIncomingVideo:(BOOL)video headers:(nullable NSDictionary *)headers {
if (_currentCall) {
[call rejectWithMode:VIRejectModeBusy headers:nil];
return;
}
_currentCall = call;
VIVideoFlags *videoFlags = [VIVideoFlags videoFlagsWithReceiveVideo:video sendVideo:video];
for (id<QIVoxClientManagerListener> listener in _listeners) {
if ([listener respondsToSelector:@selector(incomingCallReceived:withVideoFlags:)]) {
[listener incomingCallReceived:_currentCall withVideoFlags:videoFlags];
}
}
}
- (VICall *)createCall:(NSString *)user withVideoFlags:(VIVideoFlags *)videoFlags conference:(BOOL)isConference {
if (_currentCall) {
return nil;
}
VICallSettings *callSettings = [VICallSettings new];
callSettings.videoFlags = videoFlags;
if (isConference) {
_currentCall = [_client callConference:user settings:callSettings];
} else {
_currentCall = [_client call:user settings:callSettings];
}
return _currentCall;
}
- (void)call:(VICall *)call didDisconnectWithHeaders:(NSDictionary *)headers answeredElsewhere:(NSNumber *)answeredElsewhere {
if (call == _currentCall) {
_currentCall = nil;
}
}
- (void)call:(VICall *)call didFailWithError:(NSError *)error headers:(NSDictionary *)headers {
if (call == _currentCall) {
_currentCall = nil;
}
}
@end