From 94edbbdd04eb69ec3005f10f50ee68b557a478d2 Mon Sep 17 00:00:00 2001 From: Ivan Sein Date: Thu, 14 Nov 2019 10:24:21 +0100 Subject: [PATCH] Create different externalSignalingController for each user. Signed-off-by: Ivan Sein --- VideoCalls/NCCallController.m | 122 +++++++++++---------- VideoCalls/NCConnectionController.m | 11 +- VideoCalls/NCExternalSignalingController.h | 22 ++-- VideoCalls/NCExternalSignalingController.m | 43 ++++---- VideoCalls/NCRoomsManager.m | 39 ++++--- VideoCalls/NCSettingsController.h | 7 +- VideoCalls/NCSettingsController.m | 38 +++++-- 7 files changed, 165 insertions(+), 117 deletions(-) diff --git a/VideoCalls/NCCallController.m b/VideoCalls/NCCallController.m index 92622221..ea9b737d 100644 --- a/VideoCalls/NCCallController.m +++ b/VideoCalls/NCCallController.m @@ -28,7 +28,7 @@ static NSString * const kNCAudioTrackId = @"NCa0"; static NSString * const kNCVideoTrackId = @"NCv0"; static NSString * const kNCVideoTrackKind = @"video"; -@interface NCCallController () +@interface NCCallController () @property (nonatomic, assign) BOOL isAudioOnly; @property (nonatomic, assign) BOOL inCall; @@ -49,6 +49,8 @@ static NSString * const kNCVideoTrackKind = @"video"; @property (nonatomic, strong) RTCVideoTrack *localVideoTrack; @property (nonatomic, strong) RTCPeerConnectionFactory *peerConnectionFactory; @property (nonatomic, strong) NCSignalingController *signalingController; +@property (nonatomic, strong) NCExternalSignalingController *externalSignalingController; +@property (nonatomic, strong) TalkAccount *account; @property (nonatomic, strong) NSURLSessionTask *joinCallTask; @property (nonatomic, strong) NSURLSessionTask *getPeersForCallTask; @@ -74,6 +76,10 @@ static NSString * const kNCVideoTrackKind = @"video"; _signalingController = [[NCSignalingController alloc] initForRoom:room]; _signalingController.observer = self; + _account = [[NCDatabaseManager sharedInstance] activeAccount]; + _externalSignalingController = [[NCSettingsController sharedInstance] externalSignalingControllerForAccount:_account.accountId]; + _externalSignalingController.delegate = self; + if (audioOnly) { [[NCAudioController sharedInstance] setAudioSessionToVoiceChatMode]; } else { @@ -81,11 +87,6 @@ static NSString * const kNCVideoTrackKind = @"video"; } [self initRecorder]; - - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalSignalingMessageReceived:) name:NCESReceivedSignalingMessageNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(externalParticipantListMessageReceived:) name:NCESReceivedParticipantListMessageNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shouldRejoinCall:) name:NCESShouldRejoinCallNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willRejoinCall:) name:NCESWillRejoinCallNotification object:nil]; } return self; @@ -99,14 +100,14 @@ static NSString * const kNCVideoTrackKind = @"video"; - (void)joinCall { - _joinCallTask = [[NCAPIController sharedInstance] joinCall:_room.token forAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSError *error) { + _joinCallTask = [[NCAPIController sharedInstance] joinCall:_room.token forAccount:_account withCompletionBlock:^(NSError *error) { if (!error) { [self.delegate callControllerDidJoinCall:self]; [self getPeersForCall]; [self startMonitoringMicrophoneAudioLevel]; - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - _userSessionId = [[NCExternalSignalingController sharedInstance] sessionId]; - if ([[NCExternalSignalingController sharedInstance] hasMCU]) { + if ([_externalSignalingController isEnabled]) { + _userSessionId = [_externalSignalingController sessionId]; + if ([_externalSignalingController hasMCU]) { [self createOwnPublishPeerConnection]; } if (_pendingUsersInRoom) { @@ -132,14 +133,14 @@ static NSString * const kNCVideoTrackKind = @"video"; }]; } -- (void)shouldRejoinCall:(NSNotification *)notification +- (void)shouldRejoinCall { - _userSessionId = [[NCExternalSignalingController sharedInstance] sessionId]; - _joinCallTask = [[NCAPIController sharedInstance] joinCall:_room.token forAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSError *error) { + _userSessionId = [_externalSignalingController sessionId]; + _joinCallTask = [[NCAPIController sharedInstance] joinCall:_room.token forAccount:_account withCompletionBlock:^(NSError *error) { if (!error) { [self.delegate callControllerDidJoinCall:self]; NSLog(@"Rejoined call"); - if ([[NCExternalSignalingController sharedInstance] hasMCU]) { + if ([_externalSignalingController hasMCU]) { [self createOwnPublishPeerConnection]; } if (_pendingUsersInRoom) { @@ -155,7 +156,7 @@ static NSString * const kNCVideoTrackKind = @"video"; }]; } -- (void)willRejoinCall:(NSNotification *)notification +- (void)willRejoinCall { NSLog(@"willRejoinCall"); [self setInCall:NO]; @@ -170,7 +171,7 @@ static NSString * const kNCVideoTrackKind = @"video"; [self setInCall:NO]; [self cleanCurrentPeerConnections]; [self.delegate callControllerIsReconnectingCall:self]; - [[NCExternalSignalingController sharedInstance] forceReconnect]; + [_externalSignalingController forceReconnect]; } - (void)leaveCall @@ -344,7 +345,7 @@ static NSString * const kNCVideoTrackKind = @"video"; - (void)getPeersForCall { - _getPeersForCallTask = [[NCAPIController sharedInstance] getPeersForCall:_room.token forAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSMutableArray *peers, NSError *error) { + _getPeersForCallTask = [[NCAPIController sharedInstance] getPeersForCall:_room.token forAccount:_account withCompletionBlock:^(NSMutableArray *peers, NSError *error) { if (!error) { _peersInCall = peers; } @@ -405,7 +406,7 @@ static NSString * const kNCVideoTrackKind = @"video"; peerConnectionWrapper.roomType = roomType; peerConnectionWrapper.delegate = self; // TODO: Try to get display name here - if (![[NCExternalSignalingController sharedInstance] hasMCU] || !screensharingPeer) { + if (![_externalSignalingController hasMCU] || !screensharingPeer) { [peerConnectionWrapper.peerConnection addStream:_localStream]; } @@ -434,7 +435,7 @@ static NSString * const kNCVideoTrackKind = @"video"; - (void)sendDataChannelMessageToAllOfType:(NSString *)type withPayload:(id)payload { - if ([[NCExternalSignalingController sharedInstance] hasMCU]) { + if ([_externalSignalingController hasMCU]) { [_ownPeerConnection sendDataChannelMessageOfType:type withPayload:payload]; } else { NSArray *connectionWrappers = [self.connectionsDict allValues]; @@ -459,33 +460,11 @@ static NSString * const kNCVideoTrackKind = @"video"; [_ownPeerConnection sendPublishOfferToMCU]; } -- (void)externalSignalingMessageReceived:(NSNotification *)notification -{ - NSLog(@"External signaling message received: %@", notification); - NCSignalingMessage *signalingMessage = [NCSignalingMessage messageFromExternalSignalingJSONDictionary:notification.userInfo]; - [self checkIfPendingOffer:signalingMessage]; - [self processSignalingMessage:signalingMessage]; -} - -- (void)externalParticipantListMessageReceived:(NSNotification *)notification -{ - NSLog(@"External participants message received: %@", notification); - NSArray *usersInRoom = [notification.userInfo objectForKey:@"users"]; - if (_inCall) { - [self processUsersInRoom:usersInRoom]; - } else { - // Store pending usersInRoom since this websocket message could - // arrive before NCCallController knows that it's in the call. - _pendingUsersInRoom = usersInRoom; - } -} - - (void)sendNick { - TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; NSDictionary *payload = @{ - @"userid":activeAccount.userId, - @"name":activeAccount.userDisplayName + @"userid":_account.userId, + @"name":_account.userDisplayName }; [self sendDataChannelMessageToAllOfType:@"nickChanged" withPayload:payload]; } @@ -509,7 +488,7 @@ static NSString * const kNCVideoTrackKind = @"video"; { NSString *sessionId = [timer.userInfo objectForKey:@"sessionId"]; NSString *roomType = [timer.userInfo objectForKey:@"roomType"]; - [[NCExternalSignalingController sharedInstance] requestOfferForSessionId:sessionId andRoomType:roomType]; + [_externalSignalingController requestOfferForSessionId:sessionId andRoomType:roomType]; } - (void)checkIfPendingOffer:(NCSignalingMessage *)signalingMessage @@ -521,6 +500,39 @@ static NSString * const kNCVideoTrackKind = @"video"; } } +#pragma mark - External Signaling Controller Delegate + +- (void)externalSignalingController:(NCExternalSignalingController *)externalSignalingController didReceivedSignalingMessage:(NSDictionary *)signalingMessageDict +{ + NSLog(@"External signaling message received: %@", signalingMessageDict); + NCSignalingMessage *signalingMessage = [NCSignalingMessage messageFromExternalSignalingJSONDictionary:signalingMessageDict]; + [self checkIfPendingOffer:signalingMessage]; + [self processSignalingMessage:signalingMessage]; +} + +- (void)externalSignalingController:(NCExternalSignalingController *)externalSignalingController didReceivedParticipantListMessage:(NSDictionary *)participantListMessageDict +{ + NSLog(@"External participants message received: %@", participantListMessageDict); + NSArray *usersInRoom = [participantListMessageDict objectForKey:@"users"]; + if (_inCall) { + [self processUsersInRoom:usersInRoom]; + } else { + // Store pending usersInRoom since this websocket message could + // arrive before NCCallController knows that it's in the call. + _pendingUsersInRoom = usersInRoom; + } +} + +- (void)externalSignalingControllerShouldRejoinCall:(NCExternalSignalingController *)externalSignalingController +{ + [self shouldRejoinCall]; +} + +- (void)externalSignalingControllerWillRejoinCall:(NCExternalSignalingController *)externalSignalingController +{ + [self willRejoinCall]; +} + #pragma mark - Signaling Controller Delegate - (void)signalingController:(NCSignalingController *)signalingController didReceiveSignalingMessage:(NSDictionary *)message @@ -604,9 +616,9 @@ static NSString * const kNCVideoTrackKind = @"video"; for (NSString *sessionId in newSessions) { NSString *peerKey = [sessionId stringByAppendingString:kRoomTypeVideo]; if (![_connectionsDict objectForKey:peerKey] && ![_userSessionId isEqualToString:sessionId]) { - if ([[NCExternalSignalingController sharedInstance] hasMCU]) { + if ([_externalSignalingController hasMCU]) { NSLog(@"Requesting offer to the MCU for session: %@", sessionId); - [[NCExternalSignalingController sharedInstance] requestOfferForSessionId:sessionId andRoomType:kRoomTypeVideo]; + [_externalSignalingController requestOfferForSessionId:sessionId andRoomType:kRoomTypeVideo]; } else { NSComparisonResult result = [sessionId compare:_userSessionId]; if (result == NSOrderedAscending) { @@ -642,8 +654,8 @@ static NSString * const kNCVideoTrackKind = @"video"; - (NSString *)getUserIdFromSessionId:(NSString *)sessionId { - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - return [[NCExternalSignalingController sharedInstance] getUserIdFromSessionId:sessionId]; + if ([_externalSignalingController isEnabled]) { + return [_externalSignalingController getUserIdFromSessionId:sessionId]; } NSString *userId = nil; for (NSMutableDictionary *user in _peersInCall) { @@ -657,7 +669,7 @@ static NSString * const kNCVideoTrackKind = @"video"; - (void)getUserIdInServerFromSessionId:(NSString *)sessionId withCompletionBlock:(GetUserIdForSessionIdCompletionBlock)block { - [[NCAPIController sharedInstance] getPeersForCall:_room.token forAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSMutableArray *peers, NSError *error) { + [[NCAPIController sharedInstance] getPeersForCall:_room.token forAccount:_account withCompletionBlock:^(NSMutableArray *peers, NSError *error) { if (!error) { NSString *userId = nil; for (NSMutableDictionary *user in peers) { @@ -698,7 +710,7 @@ static NSString * const kNCVideoTrackKind = @"video"; if (peerConnection.isMCUPublisherPeer) { [self forceReconnect]; // If another peer failed using MCU then request a new offer - } else if ([[NCExternalSignalingController sharedInstance] hasMCU]) { + } else if ([_externalSignalingController hasMCU]) { NSString *sessionId = [peerConnection.peerId copy]; NSString *roomType = [peerConnection.roomType copy]; NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init]; @@ -707,7 +719,7 @@ static NSString * const kNCVideoTrackKind = @"video"; // Close failed peer connection [self cleanPeerConnectionsForSessionId:peerConnection.peerId]; // Request new offer - [[NCExternalSignalingController sharedInstance] requestOfferForSessionId:peerConnection.peerId andRoomType:peerConnection.roomType]; + [_externalSignalingController requestOfferForSessionId:peerConnection.peerId andRoomType:peerConnection.roomType]; // Set timeout to request new offer NSTimer *pendingOfferTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(requestNewOffer:) userInfo:userInfo repeats:YES]; [_pendingOffersDict setObject:pendingOfferTimer forKey:peerConnection.peerId]; @@ -750,8 +762,8 @@ static NSString * const kNCVideoTrackKind = @"video"; sid:nil roomType:peerConnection.roomType]; - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - [[NCExternalSignalingController sharedInstance] sendCallMessage:message]; + if ([_externalSignalingController isEnabled]) { + [_externalSignalingController sendCallMessage:message]; } else { [_signalingController sendSignalingMessage:message]; } @@ -767,8 +779,8 @@ static NSString * const kNCVideoTrackKind = @"video"; roomType:peerConnection.roomType nick:_userDisplayName]; - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - [[NCExternalSignalingController sharedInstance] sendCallMessage:message]; + if ([_externalSignalingController isEnabled]) { + [_externalSignalingController sendCallMessage:message]; } else { [_signalingController sendSignalingMessage:message]; } diff --git a/VideoCalls/NCConnectionController.m b/VideoCalls/NCConnectionController.m index aa519a06..580a676b 100644 --- a/VideoCalls/NCConnectionController.m +++ b/VideoCalls/NCConnectionController.m @@ -12,7 +12,6 @@ #import "NCAPIController.h" #import "NCDatabaseManager.h" #import "NCSettingsController.h" -#import "NCExternalSignalingController.h" #import "NCUserInterfaceController.h" NSString * const NCAppStateHasChangedNotification = @"NCAppStateHasChangedNotification"; @@ -88,9 +87,9 @@ NSString * const NCConnectionStateHasChangedNotification = @"NCConnectionStat - (void)checkAppState { - TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; - ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:activeAccount.accountId]; - NSDictionary *ncSignalingConfig = [NCSettingsController sharedInstance].ncSignalingConfiguration; + TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; + ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:activeAccount.accountId]; + NSDictionary *activeAccountSignalingConfig = [[[NCSettingsController sharedInstance] signalingConfigutations] objectForKey:activeAccount.accountId]; if (!activeAccount.server || !activeAccount.user) { [self setAppState:kAppStateNotServerProvided]; @@ -113,14 +112,14 @@ NSString * const NCConnectionStateHasChangedNotification = @"NCConnectionStat [self checkAppState]; } }]; - } else if (!ncSignalingConfig) { + } else if (!activeAccountSignalingConfig) { [self setAppState:kAppStateMissingSignalingConfiguration]; [[NCSettingsController sharedInstance] getSignalingConfigurationWithCompletionBlock:^(NSError *error) { if (error) { [self notifyAppState]; } else { // SetSignalingConfiguration should be called just once - [[NCSettingsController sharedInstance] setSignalingConfiguration]; + [[NCSettingsController sharedInstance] setSignalingConfigurationForAccount:activeAccount.accountId]; [self checkAppState]; } }]; diff --git a/VideoCalls/NCExternalSignalingController.h b/VideoCalls/NCExternalSignalingController.h index ffc7b11f..64c0a77b 100644 --- a/VideoCalls/NCExternalSignalingController.h +++ b/VideoCalls/NCExternalSignalingController.h @@ -10,20 +10,28 @@ #import "NCSignalingMessage.h" -extern NSString * const NCESReceivedSignalingMessageNotification; -extern NSString * const NCESReceivedParticipantListMessageNotification; -extern NSString * const NCESShouldRejoinCallNotification; -extern NSString * const NCESWillRejoinCallNotification; +@class NCExternalSignalingController; +@class TalkAccount; + +@protocol NCExternalSignalingControllerDelegate + +- (void)externalSignalingController:(NCExternalSignalingController *)externalSignalingController didReceivedSignalingMessage:(NSDictionary *)signalingMessageDict; +- (void)externalSignalingController:(NCExternalSignalingController *)externalSignalingController didReceivedParticipantListMessage:(NSDictionary *)participantListMessageDict; +- (void)externalSignalingControllerShouldRejoinCall:(NCExternalSignalingController *)externalSignalingController; +- (void)externalSignalingControllerWillRejoinCall:(NCExternalSignalingController *)externalSignalingController; + +@end @interface NCExternalSignalingController : NSObject -@property (nonatomic, strong) NSString* currentRoom; +@property (nonatomic, strong) NSString *currentRoom; +@property (nonatomic, strong) TalkAccount *account; +@property (nonatomic, weak) id delegate; -+ (instancetype)sharedInstance; +- (instancetype)initWithAccount:(TalkAccount *)account server:(NSString *)serverUrl andTicket:(NSString *)ticket; - (BOOL)isEnabled; - (BOOL)hasMCU; - (NSString *)sessionId; -- (void)setServer:(NSString *)serverUrl andTicket:(NSString *)ticket; - (void)joinRoom:(NSString *)roomId withSessionId:(NSString *)sessionId; - (void)leaveRoom:(NSString *)roomId; - (void)sendCallMessage:(NCSignalingMessage *)message; diff --git a/VideoCalls/NCExternalSignalingController.m b/VideoCalls/NCExternalSignalingController.m index d47b68e3..76e7b7da 100644 --- a/VideoCalls/NCExternalSignalingController.m +++ b/VideoCalls/NCExternalSignalingController.m @@ -25,6 +25,8 @@ static NSTimeInterval kMaxReconnectInterval = 16; @property (nonatomic, strong) NSString* ticket; @property (nonatomic, strong) NSString* resumeId; @property (nonatomic, strong) NSString* sessionId; +@property (nonatomic, strong) NSString* userId; +@property (nonatomic, strong) NSString* authenticationBackendUrl; @property (nonatomic, assign) BOOL connected; @property (nonatomic, assign) BOOL mcuSupport; @property (nonatomic, strong) NSMutableDictionary* participantsMap; @@ -38,11 +40,6 @@ static NSTimeInterval kMaxReconnectInterval = 16; @implementation NCExternalSignalingController -NSString * const NCESReceivedSignalingMessageNotification = @"NCESReceivedSignalingMessageNotification"; -NSString * const NCESReceivedParticipantListMessageNotification = @"NCESReceivedParticipantListMessageNotification"; -NSString * const NCESShouldRejoinCallNotification = @"NCESShouldRejoinCallNotification"; -NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotification"; - + (NCExternalSignalingController *)sharedInstance { static dispatch_once_t once; @@ -53,6 +50,18 @@ NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotificati return sharedInstance; } +- (instancetype)initWithAccount:(TalkAccount *)account server:(NSString *)serverUrl andTicket:(NSString *)ticket +{ + self = [super init]; + if (self) { + _account = account; + _userId = _account.userId; + _authenticationBackendUrl = [[NCAPIController sharedInstance] authenticationBackendUrlForAccount:_account]; + [self setServer:serverUrl andTicket:ticket]; + } + return self; +} + - (BOOL)isEnabled { return (_serverUrl) ? YES : NO; @@ -181,15 +190,14 @@ NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotificati - (void)sendHello { - TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; NSDictionary *helloDict = @{ @"type": @"hello", @"hello": @{ @"version": @"1.0", @"auth": @{ - @"url": [[NCAPIController sharedInstance] authenticationBackendUrlForAccount:activeAccount], + @"url": _authenticationBackendUrl, @"params": @{ - @"userid": activeAccount.userId, + @"userid": _userId, @"ticket": _ticket } } @@ -231,9 +239,7 @@ NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotificati // Re-join if user was in a room if (_currentRoom && _sessionChanged) { - [[NSNotificationCenter defaultCenter] postNotificationName:NCESWillRejoinCallNotification - object:self - userInfo:nil]; + [self.delegate externalSignalingControllerWillRejoinCall:self]; [[NCRoomsManager sharedInstance] rejoinRoom:_currentRoom]; } } @@ -311,9 +317,7 @@ NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotificati // Notify that session has change to rejoin the call if currently in a call if (_sessionChanged) { _sessionChanged = NO; - [[NSNotificationCenter defaultCenter] postNotificationName:NCESShouldRejoinCallNotification - object:self - userInfo:nil]; + [self.delegate externalSignalingControllerShouldRejoinCall:self]; } } @@ -341,8 +345,7 @@ NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotificati if (!participantId || [participantId isEqualToString:@""]) { NSLog(@"Guest joined room."); } else { - TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; - if ([participantId isEqualToString:activeAccount.userId]) { + if ([participantId isEqualToString:_userId]) { NSLog(@"App user joined room."); } else { [_participantsMap setObject:participant forKey:[participant objectForKey:@"sessionid"]]; @@ -379,9 +382,7 @@ NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotificati NSString *eventType = [eventDict objectForKey:@"type"]; if ([eventType isEqualToString:@"update"]) { NSLog(@"Participant list changed: %@", [eventDict objectForKey:@"update"]); - [[NSNotificationCenter defaultCenter] postNotificationName:NCESReceivedParticipantListMessageNotification - object:self - userInfo:[eventDict objectForKey:@"update"]]; + [self.delegate externalSignalingController:self didReceivedParticipantListMessage:[eventDict objectForKey:@"update"]]; } else { NSLog(@"Unknown room event: %@", eventDict); } @@ -390,9 +391,7 @@ NSString * const NCESWillRejoinCallNotification = @"NCESWillRejoinCallNotificati - (void)messageReceived:(NSDictionary *)messageDict { NSLog(@"Message received"); - [[NSNotificationCenter defaultCenter] postNotificationName:NCESReceivedSignalingMessageNotification - object:self - userInfo:messageDict]; + [self.delegate externalSignalingController:self didReceivedSignalingMessage:messageDict]; } #pragma mark - SRWebSocketDelegate diff --git a/VideoCalls/NCRoomsManager.m b/VideoCalls/NCRoomsManager.m index e8ccafc5..1c5d7d9e 100644 --- a/VideoCalls/NCRoomsManager.m +++ b/VideoCalls/NCRoomsManager.m @@ -84,9 +84,10 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess { NSMutableDictionary *userInfo = [NSMutableDictionary new]; NCRoomController *roomController = [_activeRooms objectForKey:token]; + TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; if (!roomController) { _joiningRoom = token; - _joinRoomTask = [[NCAPIController sharedInstance] joinRoom:token forAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSString *sessionId, NSError *error, NSInteger statusCode) { + _joinRoomTask = [[NCAPIController sharedInstance] joinRoom:token forAccount:activeAccount withCompletionBlock:^(NSString *sessionId, NSError *error, NSInteger statusCode) { if (!_joiningRoom) { NSLog(@"Not joining the room any more. Ignore response."); return; @@ -98,8 +99,9 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess [_activeRooms setObject:controller forKey:token]; [_joinRoomAttempts removeObjectForKey:token]; [userInfo setObject:controller forKey:@"roomController"]; - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - [[NCExternalSignalingController sharedInstance] joinRoom:token withSessionId:sessionId]; + NCExternalSignalingController *extSignalingController = [[NCSettingsController sharedInstance] externalSignalingControllerForAccount:activeAccount.accountId]; + if ([extSignalingController isEnabled]) { + [extSignalingController joinRoom:token withSessionId:sessionId]; } } else { NSInteger joinAttempts = [[_joinRoomAttempts objectForKey:token] integerValue]; @@ -142,14 +144,16 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess - (void)rejoinRoom:(NSString *)token { NCRoomController *roomController = [_activeRooms objectForKey:token]; + TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; if (roomController) { _joiningRoom = [token copy]; - _joinRoomTask = [[NCAPIController sharedInstance] joinRoom:token forAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSString *sessionId, NSError *error, NSInteger statusCode) { + _joinRoomTask = [[NCAPIController sharedInstance] joinRoom:token forAccount:activeAccount withCompletionBlock:^(NSString *sessionId, NSError *error, NSInteger statusCode) { if (!error) { roomController.userSessionId = sessionId; roomController.inChat = YES; - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - [[NCExternalSignalingController sharedInstance] joinRoom:token withSessionId:sessionId]; + NCExternalSignalingController *extSignalingController = [[NCSettingsController sharedInstance] externalSignalingControllerForAccount:activeAccount.accountId]; + if ([extSignalingController isEnabled]) { + [extSignalingController joinRoom:token withSessionId:sessionId]; } } else { NSLog(@"Could not re-join room. Status code: %ld. Error: %@", (long)statusCode, error.description); @@ -172,11 +176,13 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess [roomController stopRoomController]; [_activeRooms removeObjectForKey:token]; - [[NCAPIController sharedInstance] exitRoom:token forAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSError *error) { + TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; + [[NCAPIController sharedInstance] exitRoom:token forAccount:activeAccount withCompletionBlock:^(NSError *error) { NSMutableDictionary *userInfo = [NSMutableDictionary new]; if (!error) { - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - [[NCExternalSignalingController sharedInstance] leaveRoom:token]; + NCExternalSignalingController *extSignalingController = [[NCSettingsController sharedInstance] externalSignalingControllerForAccount:activeAccount.accountId]; + if ([extSignalingController isEnabled]) { + [extSignalingController leaveRoom:token]; } } else { [userInfo setObject:error forKey:@"error"]; @@ -256,10 +262,12 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess NCRoomController *roomController = [_activeRooms objectForKey:room.token]; if (!roomController) { // Workaround until external signaling supports multi-room - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - NSString *currentRoom = [NCExternalSignalingController sharedInstance].currentRoom; + TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; + NCExternalSignalingController *extSignalingController = [[NCSettingsController sharedInstance] externalSignalingControllerForAccount:activeAccount.accountId]; + if ([extSignalingController isEnabled]) { + NSString *currentRoom = extSignalingController.currentRoom; if (![currentRoom isEqualToString:room.token]) { - [NCExternalSignalingController sharedInstance].currentRoom = nil; + extSignalingController.currentRoom = nil; } } } @@ -339,14 +347,15 @@ NSString * const NCRoomsManagerDidReceiveChatMessagesNotification = @"ChatMess [_callViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; _callViewController.delegate = self; // Workaround until external signaling supports multi-room - if ([[NCExternalSignalingController sharedInstance] isEnabled]) { - NSString *currentRoom = [NCExternalSignalingController sharedInstance].currentRoom; + NCExternalSignalingController *extSignalingController = [[NCSettingsController sharedInstance] externalSignalingControllerForAccount:activeAccount.accountId]; + if ([extSignalingController isEnabled]) { + NSString *currentRoom = extSignalingController.currentRoom; if (![currentRoom isEqualToString:room.token]) { [[NCUserInterfaceController sharedInstance] presentConversationsList]; if (currentRoom) { [self leaveChatInRoom:currentRoom]; } - [NCExternalSignalingController sharedInstance].currentRoom = nil; + extSignalingController.currentRoom = nil; } } [[NCUserInterfaceController sharedInstance] presentCallViewController:_callViewController]; diff --git a/VideoCalls/NCSettingsController.h b/VideoCalls/NCSettingsController.h index fac7536c..fadd36c6 100644 --- a/VideoCalls/NCSettingsController.h +++ b/VideoCalls/NCSettingsController.h @@ -58,6 +58,7 @@ typedef enum NCPreferredFileSorting { NCModificationDateSorting } NCPreferredFileSorting; +@class NCExternalSignalingController; @interface NCSettingsController : NSObject @@ -76,8 +77,9 @@ typedef enum NCPreferredFileSorting { @property (nonatomic, copy) NSString *ncUserPublicKey; @property (nonatomic, copy) NSString *defaultBrowser; @property (nonatomic, copy) NSMutableArray *supportedBrowsers; -@property (nonatomic, copy) NSDictionary *ncSignalingConfiguration; @property (nonatomic, copy) ARDSettingsModel *videoSettingsModel; +@property (nonatomic, copy) NSMutableDictionary *signalingConfigutations; // accountId -> signalingConfigutation +@property (nonatomic, copy) NSMutableDictionary *externalSignalingControllers; // accountId -> externalSignalingController + (instancetype)sharedInstance; - (void)setToken:(NSString *)token forAccount:(NSString *)account; @@ -93,7 +95,8 @@ typedef enum NCPreferredFileSorting { - (void)logoutWithCompletionBlock:(LogoutCompletionBlock)block; - (void)getCapabilitiesWithCompletionBlock:(GetCapabilitiesCompletionBlock)block; - (void)getSignalingConfigurationWithCompletionBlock:(GetSignalingConfigCompletionBlock)block; -- (void)setSignalingConfiguration; +- (void)setSignalingConfigurationForAccount:(NSString *)accountId; +- (NCExternalSignalingController *)externalSignalingControllerForAccount:(NSString *)accountId; - (void)subscribeForPushNotificationsForAccount:(NSString *)account; - (BOOL)serverHasTalkCapability:(NSString *)capability; - (NSInteger)chatMaxLengthConfigCapability; diff --git a/VideoCalls/NCSettingsController.m b/VideoCalls/NCSettingsController.m index ca9aec05..67699c1e 100644 --- a/VideoCalls/NCSettingsController.m +++ b/VideoCalls/NCSettingsController.m @@ -87,6 +87,10 @@ NSString * const NCUserProfileImageUpdatedNotification = @"NCUserProfileImageUpd _videoSettingsModel = [[ARDSettingsModel alloc] init]; _keychain = [UICKeyChainStore keyChainStoreWithService:@"com.nextcloud.Talk" accessGroup:@"group.com.nextcloud.Talk"]; + + _signalingConfigutations = [NSMutableDictionary new]; + _externalSignalingControllers = [NSMutableDictionary new]; + [self readValuesFromKeyChain]; [self configureDatabase]; [self configureDefaultBrowser]; @@ -225,8 +229,6 @@ NSString * const NCUserProfileImageUpdatedNotification = @"NCUserProfileImageUpd _ncDeviceSignature = nil; _defaultBrowser = @"Safari"; _pushNotificationSubscribed = nil; - // Also remove values that are not stored in the keychain - _ncSignalingConfiguration = nil; [_keychain removeItemForKey:kNCServerKey]; [_keychain removeItemForKey:kNCUserKey]; @@ -285,7 +287,8 @@ NSString * const NCUserProfileImageUpdatedNotification = @"NCUserProfileImageUpd } }]; } - [[NCExternalSignalingController sharedInstance] disconnect]; + NCExternalSignalingController *extSignalingController = [self externalSignalingControllerForAccount:removingAccount.accountId]; + [extSignalingController disconnect]; [[NCSettingsController sharedInstance] cleanUserAndServerStoredValues]; [[NCAPIController sharedInstance] removeProfileImageForAccount:removingAccount]; [[NCDatabaseManager sharedInstance] removeAccount:removingAccount.accountId]; @@ -326,9 +329,11 @@ NSString * const NCUserProfileImageUpdatedNotification = @"NCUserProfileImageUpd - (void)getSignalingConfigurationWithCompletionBlock:(GetSignalingConfigCompletionBlock)block { - [[NCAPIController sharedInstance] getSignalingSettingsForAccount:[[NCDatabaseManager sharedInstance] activeAccount] withCompletionBlock:^(NSDictionary *settings, NSError *error) { - if (!error) { - _ncSignalingConfiguration = [[settings objectForKey:@"ocs"] objectForKey:@"data"]; + TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount]; + [[NCAPIController sharedInstance] getSignalingSettingsForAccount:activeAccount withCompletionBlock:^(NSDictionary *settings, NSError *error) { + if (!error && !activeAccount.invalidated) { + NSDictionary *signalingConfiguration = [[settings objectForKey:@"ocs"] objectForKey:@"data"]; + [_signalingConfigutations setObject:signalingConfiguration forKey:activeAccount.accountId]; if (block) block(nil); } else { NSLog(@"Error while getting signaling configuration"); @@ -338,19 +343,32 @@ NSString * const NCUserProfileImageUpdatedNotification = @"NCUserProfileImageUpd } // SetSignalingConfiguration should be called just once -- (void)setSignalingConfiguration +- (void)setSignalingConfigurationForAccount:(NSString *)accountId { + NSDictionary *signalingConfiguration = [_signalingConfigutations objectForKey:accountId]; NSString *externalSignalingServer = nil; - id server = [_ncSignalingConfiguration objectForKey:@"server"]; + id server = [signalingConfiguration objectForKey:@"server"]; if ([server isKindOfClass:[NSString class]]) { externalSignalingServer = server; } - NSString *externalSignalingTicket = [_ncSignalingConfiguration objectForKey:@"ticket"]; + NSString *externalSignalingTicket = [signalingConfiguration objectForKey:@"ticket"]; if (externalSignalingServer && externalSignalingTicket) { - [[NCExternalSignalingController sharedInstance] setServer:externalSignalingServer andTicket:externalSignalingTicket]; + dispatch_async(dispatch_get_main_queue(), ^{ + NCExternalSignalingController *extSignalingController = [_externalSignalingControllers objectForKey:accountId]; + if (!extSignalingController) { + TalkAccount *account = [[NCDatabaseManager sharedInstance] talkAccountForAccountId:accountId]; + extSignalingController = [[NCExternalSignalingController alloc] initWithAccount:account server:externalSignalingServer andTicket:externalSignalingTicket]; + [_externalSignalingControllers setObject:extSignalingController forKey:accountId]; + } + }); } } +- (NCExternalSignalingController *)externalSignalingControllerForAccount:(NSString *)accountId +{ + return [_externalSignalingControllers objectForKey:accountId]; +} + #pragma mark - Server Capabilities - (void)getCapabilitiesWithCompletionBlock:(GetCapabilitiesCompletionBlock)block;