CallViewController.m 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "CallViewController.h"
  6. #import <AVKit/AVKit.h>
  7. #import <ReplayKit/ReplayKit.h>
  8. #import <WebRTC/RTCCameraVideoCapturer.h>
  9. #import <WebRTC/RTCMediaStream.h>
  10. #import <WebRTC/RTCMTLVideoView.h>
  11. #import <WebRTC/RTCVideoTrack.h>
  12. #import "JDStatusBarNotification.h"
  13. #import "CallKitManager.h"
  14. #import "CallParticipantViewCell.h"
  15. #import "NCAPIController.h"
  16. #import "NCAppBranding.h"
  17. #import "NCAudioController.h"
  18. #import "NCCallController.h"
  19. #import "NCDatabaseManager.h"
  20. #import "NCRoomsManager.h"
  21. #import "NCSettingsController.h"
  22. #import "NCSignalingMessage.h"
  23. #import "RoomInfoTableViewController.h"
  24. #import "NCScreensharingController.h"
  25. #import "NextcloudTalk-Swift.h"
  26. typedef NS_ENUM(NSInteger, CallState) {
  27. CallStateJoining,
  28. CallStateWaitingParticipants,
  29. CallStateReconnecting,
  30. CallStateInCall,
  31. CallStateSwitchingToAnotherRoom
  32. };
  33. CGFloat const kSidebarWidth = 350;
  34. CGFloat const kReactionViewAnimationDuration = 2.0;
  35. CGFloat const kReactionViewHidingDuration = 1.0;
  36. CGFloat const kMaxReactionsInScreen = 5.0;
  37. typedef void (^UpdateCallParticipantViewCellBlock)(CallParticipantViewCell *cell);
  38. @interface PendingCellUpdate : NSObject
  39. @property (nonatomic, strong) NCPeerConnection *peer;
  40. @property (nonatomic, strong) UpdateCallParticipantViewCellBlock block;
  41. @end
  42. @implementation PendingCellUpdate
  43. @end
  44. @interface CallViewController () <NCCallControllerDelegate, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, RTCVideoViewDelegate, CallParticipantViewCellDelegate, UIGestureRecognizerDelegate, NCChatTitleViewDelegate>
  45. {
  46. CallState _callState;
  47. NSMutableArray *_peersInCall;
  48. NSMutableArray *_screenPeersInCall;
  49. NSMutableDictionary *_videoRenderersDict; // peerIdentifier -> renderer
  50. NSMutableDictionary *_screenRenderersDict; // peerId -> renderer
  51. NSString *_presentedScreenPeerId;
  52. NCCallController *_callController;
  53. ChatViewController *_chatViewController;
  54. UINavigationController *_chatNavigationController;
  55. CGSize _screensharingSize;
  56. UITapGestureRecognizer *_tapGestureForDetailedView;
  57. NSTimer *_detailedViewTimer;
  58. NSTimer *_proximityTimer;
  59. NSString *_displayName;
  60. BOOL _isAudioOnly;
  61. BOOL _isDetailedViewVisible;
  62. BOOL _userDisabledVideo;
  63. BOOL _userDisabledSpeaker;
  64. BOOL _videoCallUpgrade;
  65. BOOL _hangingUp;
  66. BOOL _pushToTalkActive;
  67. BOOL _isHandRaised;
  68. BOOL _proximityState;
  69. BOOL _showChatAfterRoomSwitch;
  70. BOOL _connectingSoundAlreadyPlayed;
  71. UIImpactFeedbackGenerator *_buttonFeedbackGenerator;
  72. CGPoint _localVideoDragStartingPosition;
  73. CGPoint _localVideoOriginPosition;
  74. AVRoutePickerView *_airplayView;
  75. NSMutableArray *_pendingPeerInserts;
  76. NSMutableArray *_pendingPeerDeletions;
  77. NSMutableArray *_pendingPeerUpdates;
  78. NSTimer *_batchUpdateTimer;
  79. UIImageSymbolConfiguration *_barButtonsConfiguration;
  80. CGFloat _lastScheduledReaction;
  81. NSTimer *_callDurationTimer;
  82. AVAudioPlayer *_soundsPlayer;
  83. }
  84. @property (nonatomic, strong) IBOutlet UIButton *audioMuteButton;
  85. @property (nonatomic, strong) IBOutlet UIButton *speakerButton;
  86. @property (nonatomic, strong) IBOutlet UIButton *videoDisableButton;
  87. @property (nonatomic, strong) IBOutlet UIButton *switchCameraButton;
  88. @property (nonatomic, strong) IBOutlet UIButton *hangUpButton;
  89. @property (nonatomic, strong) IBOutlet UIButton *videoCallButton;
  90. @property (nonatomic, strong) IBOutlet UIButton *recordingButton;
  91. @property (nonatomic, strong) IBOutlet UIButton *lowerHandButton;
  92. @property (nonatomic, strong) IBOutlet UIButton *moreMenuButton;
  93. @property (nonatomic, strong) IBOutlet UICollectionView *collectionView;
  94. @property (nonatomic, strong) IBOutlet UIView *topBarView;
  95. @property (nonatomic, strong) IBOutlet UIStackView *topBarButtonStackView;
  96. @property (nonatomic, strong) IBOutlet UIView *sideBarView;
  97. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *collectionViewLeftConstraint;
  98. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *collectionViewBottomConstraint;
  99. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *collectionViewRightConstraint;
  100. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *topBarViewRightContraint;
  101. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *screenshareViewRightContraint;
  102. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *sideBarViewRightConstraint;
  103. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *sideBarViewBottomConstraint;
  104. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *sideBarWidthConstraint;
  105. @property (nonatomic, strong) IBOutlet NSLayoutConstraint *stackViewToTitleViewConstraint;
  106. @end
  107. @implementation CallViewController
  108. @synthesize delegate = _delegate;
  109. - (instancetype)initCallInRoom:(NCRoom *)room asUser:(NSString *)displayName audioOnly:(BOOL)audioOnly
  110. {
  111. self = [super init];
  112. if (!self) {
  113. return nil;
  114. }
  115. self.modalPresentationStyle = UIModalPresentationFullScreen;
  116. _room = room;
  117. _displayName = displayName;
  118. _isAudioOnly = audioOnly;
  119. _peersInCall = [[NSMutableArray alloc] init];
  120. _screenPeersInCall = [[NSMutableArray alloc] init];
  121. _videoRenderersDict = [[NSMutableDictionary alloc] init];
  122. _screenRenderersDict = [[NSMutableDictionary alloc] init];
  123. _buttonFeedbackGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:(UIImpactFeedbackStyleLight)];
  124. _pendingPeerInserts = [[NSMutableArray alloc] init];
  125. _pendingPeerDeletions = [[NSMutableArray alloc] init];
  126. _pendingPeerUpdates = [[NSMutableArray alloc] init];
  127. _lastScheduledReaction = 0.0;
  128. _barButtonsConfiguration = [UIImageSymbolConfiguration configurationWithPointSize:20];
  129. // Use image downloader without cache so I can get 200 or 201 from the avatar requests.
  130. [AvatarBackgroundImageView setSharedImageDownloader:[[NCAPIController sharedInstance] imageDownloaderNoCache]];
  131. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didJoinRoom:) name:NCRoomsManagerDidJoinRoomNotification object:nil];
  132. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(providerDidEndCall:) name:CallKitManagerDidEndCallNotification object:nil];
  133. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(providerDidChangeAudioMute:) name:CallKitManagerDidChangeAudioMuteNotification object:nil];
  134. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(providerWantsToUpgradeToVideoCall:) name:CallKitManagerWantsToUpgradeToVideoCallNotification object:nil];
  135. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidChangeRoute:) name:AudioSessionDidChangeRouteNotification object:nil];
  136. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidActivate:) name:AudioSessionWasActivatedByProviderNotification object:nil];
  137. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionDidChangeRoutingInformation:) name:AudioSessionDidChangeRoutingInformationNotification object:nil];
  138. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
  139. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
  140. [[AllocationTracker shared] addAllocation:@"CallViewController"];
  141. return self;
  142. }
  143. - (void)startCallWithSessionId:(NSString *)sessionId
  144. {
  145. _callController = [[NCCallController alloc] initWithDelegate:self inRoom:_room forAudioOnlyCall:_isAudioOnly withSessionId:sessionId andVoiceChatMode:_voiceChatModeAtStart];
  146. _callController.userDisplayName = _displayName;
  147. _callController.disableAudioAtStart = _audioDisabledAtStart;
  148. _callController.disableVideoAtStart = _videoDisabledAtStart;
  149. _callController.silentCall = _silentCall;
  150. _callController.recordingConsent = _recordingConsent;
  151. [_callController startCall];
  152. }
  153. - (void)viewDidLoad
  154. {
  155. [super viewDidLoad];
  156. [self setCallState:CallStateJoining];
  157. _tapGestureForDetailedView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showDetailedViewWithTimer)];
  158. [_tapGestureForDetailedView setNumberOfTapsRequired:1];
  159. UILongPressGestureRecognizer *pushToTalkRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlePushToTalk:)];
  160. [self.audioMuteButton addGestureRecognizer:pushToTalkRecognizer];
  161. [_participantsLabelContainer setHidden:YES];
  162. [_screensharingView setHidden:YES];
  163. [_screensharingView setClipsToBounds:YES];
  164. [self.hangUpButton.layer setCornerRadius:self.hangUpButton.frame.size.height / 2];
  165. [self.closeScreensharingButton.layer setCornerRadius:16.0f];
  166. [self.collectionView.layer setCornerRadius:22.0f];
  167. [self.collectionView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentAlways];
  168. [self.sideBarView setClipsToBounds:YES];
  169. [self.sideBarView.layer setCornerRadius:22.0f];
  170. _airplayView = [[AVRoutePickerView alloc] initWithFrame:CGRectMake(0, 0, 48, 56)];
  171. _airplayView.tintColor = [UIColor whiteColor];
  172. _airplayView.activeTintColor = [UIColor whiteColor];
  173. self.audioMuteButton.accessibilityLabel = NSLocalizedString(@"Microphone", nil);
  174. self.audioMuteButton.accessibilityValue = NSLocalizedString(@"Microphone enabled", nil);
  175. self.audioMuteButton.accessibilityHint = NSLocalizedString(@"Double tap to enable or disable the microphone", nil);
  176. self.speakerButton.accessibilityLabel = NSLocalizedString(@"Speaker", nil);
  177. self.speakerButton.accessibilityValue = NSLocalizedString(@"Speaker disabled", nil);
  178. self.speakerButton.accessibilityHint = NSLocalizedString(@"Double tap to enable or disable the speaker", nil);
  179. self.videoDisableButton.accessibilityLabel = NSLocalizedString(@"Camera", nil);
  180. self.videoDisableButton.accessibilityValue = NSLocalizedString(@"Camera enabled", nil);
  181. self.videoDisableButton.accessibilityHint = NSLocalizedString(@"Double tap to enable or disable the camera", nil);
  182. self.hangUpButton.accessibilityLabel = NSLocalizedString(@"Hang up", nil);
  183. self.hangUpButton.accessibilityHint = NSLocalizedString(@"Double tap to hang up the call", nil);
  184. self.videoCallButton.accessibilityLabel = NSLocalizedString(@"Camera", nil);
  185. self.videoCallButton.accessibilityHint = NSLocalizedString(@"Double tap to upgrade this voice call to a video call", nil);
  186. self.toggleChatButton.accessibilityLabel = NSLocalizedString(@"Chat", nil);
  187. self.toggleChatButton.accessibilityHint = NSLocalizedString(@"Double tap to show or hide chat view", nil);
  188. self.recordingButton.accessibilityLabel = NSLocalizedString(@"Recording", nil);
  189. self.recordingButton.accessibilityHint = NSLocalizedString(@"Double tap to stop recording", nil);
  190. self.lowerHandButton.accessibilityLabel = NSLocalizedString(@"Lower hand", nil);
  191. self.lowerHandButton.accessibilityHint = NSLocalizedString(@"Double tap to lower hand", nil);
  192. self.moreMenuButton.accessibilityLabel = NSLocalizedString(@"More actions", nil);
  193. self.moreMenuButton.accessibilityHint = NSLocalizedString(@"Double tap to show more actions", nil);
  194. self.moreMenuButton.showsMenuAsPrimaryAction = YES;
  195. // Text color should be always white in the call view
  196. [self.titleView setTitleTextColor:UIColor.whiteColor];
  197. [self.titleView updateForRoom:_room];
  198. // The titleView uses the themeColor as a background for the userStatusImage
  199. // As we always have a black background, we need to change that
  200. [self.titleView setUserStatusBackgroundColor:UIColor.blackColor];
  201. self.titleView.delegate = self;
  202. self.collectionView.delegate = self;
  203. [self createWaitingScreen];
  204. // We hide localVideoView until we receive it from cameraController
  205. [self setLocalVideoViewHidden:YES];
  206. // We disableLocalVideo here even if the call controller has not been created just to show the video button as disabled
  207. // also we set _userDisabledVideo = YES so the proximity sensor doesn't enable it.
  208. if (_videoDisabledAtStart) {
  209. _userDisabledVideo = YES;
  210. [self disableLocalVideo];
  211. }
  212. if (_voiceChatModeAtStart) {
  213. _userDisabledSpeaker = YES;
  214. }
  215. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  216. // 'conversation-permissions' capability was not added in Talk 13 release, so we check for 'direct-mention-flag' capability
  217. // as a workaround.
  218. BOOL serverSupportsConversationPermissions =
  219. [[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityConversationPermissions forAccountId:activeAccount.accountId] ||
  220. [[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityDirectMentionFlag forAccountId:activeAccount.accountId];
  221. if (serverSupportsConversationPermissions) {
  222. [self setAudioMuteButtonEnabled:(_room.permissions & NCPermissionCanPublishAudio)];
  223. [self setVideoDisableButtonEnabled:(_room.permissions & NCPermissionCanPublishVideo)];
  224. }
  225. [self.collectionView registerNib:[UINib nibWithNibName:kCallParticipantCellNibName bundle:nil] forCellWithReuseIdentifier:kCallParticipantCellIdentifier];
  226. [self.collectionView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
  227. UIPanGestureRecognizer *localVideoDragGesturure = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(localVideoDragged:)];
  228. [self.localVideoView addGestureRecognizer:localVideoDragGesturure];
  229. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:)
  230. name:UIDeviceProximityStateDidChangeNotification object:nil];
  231. // callStartTime is only available if we have the "recording-v1" capability
  232. if ([[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityRecordingV1]) {
  233. _callDurationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(callDurationTimerUpdate) userInfo:nil repeats:YES];
  234. }
  235. }
  236. - (void)viewDidLayoutSubviews
  237. {
  238. [super viewDidLayoutSubviews];
  239. [self.screenshareLabelContainer.layer setCornerRadius:self.screenshareLabelContainer.frame.size.height / 2];
  240. [self.participantsLabelContainer.layer setCornerRadius:self.participantsLabelContainer.frame.size.height / 2];
  241. }
  242. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
  243. {
  244. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  245. [self adjustConstraints];
  246. [self.collectionView.collectionViewLayout invalidateLayout];
  247. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  248. [self setLocalVideoRect];
  249. [self->_screensharingView resizeContentView];
  250. [self adjustTopBar];
  251. } completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  252. }];
  253. }
  254. - (void)viewSafeAreaInsetsDidChange
  255. {
  256. [super viewSafeAreaInsetsDidChange];
  257. [self adjustConstraints];
  258. [self setLocalVideoRect];
  259. [self adjustTopBar];
  260. }
  261. - (void)viewWillAppear:(BOOL)animated
  262. {
  263. [super viewWillAppear:animated];
  264. [self setSideBarVisible:NO animated:NO withCompletion:nil];
  265. [self adjustConstraints];
  266. [self setLocalVideoRect];
  267. [self adjustSpeakerButton];
  268. [self adjustTopBar];
  269. }
  270. - (void)viewWillDisappear:(BOOL)animated
  271. {
  272. [super viewWillDisappear:animated];
  273. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
  274. [UIApplication sharedApplication].idleTimerDisabled = NO;
  275. }
  276. - (void)viewDidAppear:(BOOL)animated
  277. {
  278. [super viewDidAppear:animated];
  279. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
  280. [UIApplication sharedApplication].idleTimerDisabled = YES;
  281. }
  282. - (UIStatusBarStyle)preferredStatusBarStyle
  283. {
  284. return UIStatusBarStyleLightContent;
  285. }
  286. - (void)dealloc
  287. {
  288. NSLog(@"CallViewController dealloc");
  289. [[AllocationTracker shared] removeAllocation:@"CallViewController"];
  290. [[NSNotificationCenter defaultCenter] removeObserver:self];
  291. }
  292. - (void)didReceiveMemoryWarning {
  293. [super didReceiveMemoryWarning];
  294. // Dispose of any resources that can be recreated.
  295. }
  296. - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
  297. {
  298. // No push-to-talk while in chat
  299. if (!_chatNavigationController) {
  300. for (UIPress* press in presses) {
  301. if (press.key.keyCode == UIKeyboardHIDUsageKeyboardSpacebar) {
  302. [self pushToTalkStart];
  303. return;
  304. }
  305. }
  306. }
  307. [super pressesBegan:presses withEvent:event];
  308. }
  309. - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
  310. {
  311. // No push-to-talk while in chat
  312. if (!_chatNavigationController) {
  313. for (UIPress* press in presses) {
  314. if (press.key.keyCode == UIKeyboardHIDUsageKeyboardSpacebar) {
  315. [self pushToTalkEnd];
  316. return;
  317. }
  318. }
  319. }
  320. [super pressesEnded:presses withEvent:event];
  321. }
  322. #pragma mark - App lifecycle notifications
  323. -(void)appDidBecomeActive:(NSNotification*)notification
  324. {
  325. if (!_isAudioOnly && _callController && !_userDisabledVideo) {
  326. // Only enable video if it was not disabled by the user.
  327. [self enableLocalVideo];
  328. }
  329. }
  330. -(void)appWillResignActive:(NSNotification*)notification
  331. {
  332. if (!_isAudioOnly && _callController) {
  333. [_callController getVideoEnabledStateWithCompletionBlock:^(BOOL isEnabled) {
  334. if (isEnabled) {
  335. // Disable video when the app moves to the background as we can't access the camera anymore.
  336. [self disableLocalVideo];
  337. }
  338. }];
  339. }
  340. }
  341. #pragma mark - Rooms manager notifications
  342. - (void)didJoinRoom:(NSNotification *)notification
  343. {
  344. NSString *token = [notification.userInfo objectForKey:@"token"];
  345. if (![token isEqualToString:_room.token]) {
  346. return;
  347. }
  348. NSError *error = [notification.userInfo objectForKey:@"error"];
  349. if (error) {
  350. [self presentJoinError:[notification.userInfo objectForKey:@"errorReason"]];
  351. return;
  352. }
  353. NCRoomController *roomController = [notification.userInfo objectForKey:@"roomController"];
  354. if (!_callController) {
  355. [self startCallWithSessionId:roomController.userSessionId];
  356. }
  357. [self.titleView updateForRoom:_room];
  358. }
  359. - (void)providerDidChangeAudioMute:(NSNotification *)notification
  360. {
  361. NSString *roomToken = [notification.userInfo objectForKey:@"roomToken"];
  362. if (![roomToken isEqualToString:_room.token]) {
  363. return;
  364. }
  365. BOOL isMuted = [[notification.userInfo objectForKey:@"isMuted"] boolValue];
  366. [self setAudioMuted:isMuted];
  367. }
  368. - (void)providerDidEndCall:(NSNotification *)notification
  369. {
  370. NSString *roomToken = [notification.userInfo objectForKey:@"roomToken"];
  371. if (![roomToken isEqualToString:_room.token]) {
  372. return;
  373. }
  374. [self hangupForAll:NO];
  375. }
  376. - (void)providerWantsToUpgradeToVideoCall:(NSNotification *)notification
  377. {
  378. NSString *roomToken = [notification.userInfo objectForKey:@"roomToken"];
  379. if (![roomToken isEqualToString:_room.token]) {
  380. return;
  381. }
  382. if (_isAudioOnly) {
  383. [self showUpgradeToVideoCallDialog];
  384. }
  385. }
  386. #pragma mark - Audio controller notifications
  387. - (void)audioSessionDidChangeRoute:(NSNotification *)notification
  388. {
  389. [self adjustSpeakerButton];
  390. }
  391. - (void)audioSessionDidActivate:(NSNotification *)notification
  392. {
  393. [self adjustSpeakerButton];
  394. }
  395. - (void)audioSessionDidChangeRoutingInformation:(NSNotification *)notification
  396. {
  397. [self adjustSpeakerButton];
  398. dispatch_async(dispatch_get_main_queue(), ^{
  399. [self adjustMoreButtonMenu];
  400. });
  401. }
  402. #pragma mark - Local video
  403. - (void)setLocalVideoRect
  404. {
  405. CGSize localVideoSize;
  406. CGFloat width = [UIScreen mainScreen].bounds.size.width / 6;
  407. CGFloat height = [UIScreen mainScreen].bounds.size.height / 6;
  408. NSString *videoResolution = [[[NCSettingsController sharedInstance] videoSettingsModel] currentVideoResolutionSettingFromStore];
  409. NSString *localVideoRes = [[[NCSettingsController sharedInstance] videoSettingsModel] readableResolution:videoResolution];
  410. // When running on MacOS the camera will always be in portrait mode
  411. if ([localVideoRes isEqualToString:@"Low"] || [localVideoRes isEqualToString:@"Normal"]) {
  412. if (width < height || [NCUtils isiOSAppOnMac]) {
  413. localVideoSize = CGSizeMake(height * 3/4, height);
  414. } else {
  415. localVideoSize = CGSizeMake(width, width * 3/4);
  416. }
  417. } else {
  418. if (width < height || [NCUtils isiOSAppOnMac]) {
  419. localVideoSize = CGSizeMake(height * 9/16, height);
  420. } else {
  421. localVideoSize = CGSizeMake(width, width * 9/16);
  422. }
  423. }
  424. UIEdgeInsets safeAreaInsets = self.view.safeAreaInsets;
  425. CGSize viewSize = self.view.frame.size;
  426. CGFloat defaultPadding = 16;
  427. CGFloat extraPadding = 60; // Padding to not cover participant name or mute indicator when there is only one other participant in the call
  428. _localVideoOriginPosition = CGPointMake(viewSize.width - localVideoSize.width - _collectionViewRightConstraint.constant - safeAreaInsets.right - defaultPadding,
  429. viewSize.height - localVideoSize.height - _collectionViewBottomConstraint.constant - safeAreaInsets.bottom - extraPadding);
  430. CGRect localVideoRect = CGRectMake(_localVideoOriginPosition.x, _localVideoOriginPosition.y, localVideoSize.width, localVideoSize.height);
  431. dispatch_async(dispatch_get_main_queue(), ^{
  432. self->_localVideoView.frame = localVideoRect;
  433. self->_localVideoView.layer.cornerRadius = 15.0f;
  434. self->_localVideoView.layer.masksToBounds = YES;
  435. });
  436. }
  437. #pragma mark - Proximity sensor
  438. - (void)sensorStateChange:(NSNotificationCenter *)notification
  439. {
  440. dispatch_async(dispatch_get_main_queue(), ^{
  441. [self->_proximityTimer invalidate];
  442. self->_proximityTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(adjustProximityState) userInfo:nil repeats:NO];
  443. });
  444. }
  445. - (void)adjustProximityState
  446. {
  447. BOOL currentProximityState = [[UIDevice currentDevice] proximityState];
  448. if (currentProximityState == _proximityState) {
  449. return;
  450. }
  451. _proximityState = currentProximityState;
  452. if (!_isAudioOnly) {
  453. if (_proximityState == YES) {
  454. [self disableLocalVideo];
  455. [self disableSpeaker];
  456. } else {
  457. // Only enable video if it was not disabled by the user.
  458. if (!_userDisabledVideo) {
  459. [self enableLocalVideo];
  460. }
  461. if (!_userDisabledSpeaker) {
  462. [self enableSpeaker];
  463. }
  464. }
  465. }
  466. [self pushToTalkEnd];
  467. }
  468. #pragma mark - User Interface
  469. - (void)setCallState:(CallState)state
  470. {
  471. _callState = state;
  472. switch (state) {
  473. case CallStateJoining:
  474. case CallStateWaitingParticipants:
  475. case CallStateReconnecting:
  476. {
  477. [self startPlayingConnectingSound];
  478. [self showWaitingScreen];
  479. [self invalidateDetailedViewTimer];
  480. [self showDetailedView];
  481. [self removeTapGestureForDetailedView];
  482. }
  483. break;
  484. case CallStateInCall:
  485. {
  486. [self stopPlayingConnectingSound];
  487. [self hideWaitingScreen];
  488. if (!_isAudioOnly) {
  489. [self addTapGestureForDetailedView];
  490. [self showDetailedViewWithTimer];
  491. }
  492. }
  493. break;
  494. case CallStateSwitchingToAnotherRoom:
  495. {
  496. [self showWaitingScreen];
  497. [self invalidateDetailedViewTimer];
  498. [self showDetailedView];
  499. [self removeTapGestureForDetailedView];
  500. }
  501. break;
  502. default:
  503. break;
  504. }
  505. }
  506. - (void)setCallStateForPeersInCall
  507. {
  508. if ([_peersInCall count] > 0) {
  509. if (_callState != CallStateInCall) {
  510. [self setCallState:CallStateInCall];
  511. }
  512. } else {
  513. if (_callState == CallStateInCall) {
  514. [self setCallState:CallStateWaitingParticipants];
  515. }
  516. }
  517. if (_room.type != kNCRoomTypeOneToOne) {
  518. dispatch_async(dispatch_get_main_queue(), ^{
  519. NSTextAttachment *participantsAttachment = [[NSTextAttachment alloc] init];
  520. participantsAttachment.image = [[UIImage systemImageNamed:@"person.2"] imageWithTintColor:self.participantsLabel.textColor];
  521. NSMutableAttributedString *resultString = [[NSMutableAttributedString alloc] initWithAttributedString:[NSAttributedString attributedStringWithAttachment:participantsAttachment]];
  522. [resultString appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@" %ld", [self->_peersInCall count] + 1]]];
  523. NSRange range = NSMakeRange(0, [resultString length]);
  524. [resultString addAttribute:NSFontAttributeName value:self.participantsLabel.font range:range];
  525. self.participantsLabel.attributedText = resultString;
  526. [self.participantsLabelContainer setHidden:NO];
  527. });
  528. }
  529. }
  530. - (void)createWaitingScreen
  531. {
  532. self.avatarBackgroundImageView.backgroundColor = [NCAppBranding themeColor];
  533. if (_room.type == kNCRoomTypeOneToOne) {
  534. UIColor *bgColor = [[ColorGenerator shared] usernameToColor:self.room.displayName];
  535. [self.avatarBackgroundImageView setBackgroundColor:bgColor];
  536. self.avatarBackgroundImageView.backgroundColor = [self.avatarBackgroundImageView.backgroundColor colorWithAlphaComponent:0.8];
  537. }
  538. [self setWaitingScreenText];
  539. }
  540. - (void)setWaitingScreenText
  541. {
  542. NSString *waitingMessage = NSLocalizedString(@"Waiting for others to join call …", nil);
  543. if (_room.type == kNCRoomTypeOneToOne) {
  544. waitingMessage = [NSString stringWithFormat:NSLocalizedString(@"Waiting for %@ to join call …", nil), _room.displayName];
  545. }
  546. if (_callState == CallStateReconnecting) {
  547. waitingMessage = NSLocalizedString(@"Connecting to the call …", nil);
  548. }
  549. if (_callState == CallStateSwitchingToAnotherRoom) {
  550. waitingMessage = NSLocalizedString(@"Switching to another conversation …", nil);
  551. }
  552. dispatch_async(dispatch_get_main_queue(), ^{
  553. self.waitingLabel.text = waitingMessage;
  554. });
  555. }
  556. - (void)showWaitingScreen
  557. {
  558. [self setWaitingScreenText];
  559. dispatch_async(dispatch_get_main_queue(), ^{
  560. self.collectionView.backgroundView = self.waitingView;
  561. });
  562. }
  563. - (void)hideWaitingScreen
  564. {
  565. dispatch_async(dispatch_get_main_queue(), ^{
  566. self.collectionView.backgroundView = nil;
  567. });
  568. }
  569. - (void)startPlayingConnectingSound
  570. {
  571. if (!_initiator || _connectingSoundAlreadyPlayed) {
  572. return;
  573. }
  574. NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"connecting" ofType:@"mp3"];
  575. NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
  576. _soundsPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
  577. _soundsPlayer.numberOfLoops = -1;
  578. [_soundsPlayer play];
  579. _connectingSoundAlreadyPlayed = YES;
  580. }
  581. - (void)stopPlayingConnectingSound
  582. {
  583. [_soundsPlayer stop];
  584. }
  585. - (void)addTapGestureForDetailedView
  586. {
  587. dispatch_async(dispatch_get_main_queue(), ^{
  588. [self.view addGestureRecognizer:self->_tapGestureForDetailedView];
  589. });
  590. }
  591. - (void)removeTapGestureForDetailedView
  592. {
  593. dispatch_async(dispatch_get_main_queue(), ^{
  594. [self.view removeGestureRecognizer:self->_tapGestureForDetailedView];
  595. });
  596. }
  597. - (void)showDetailedView
  598. {
  599. _isDetailedViewVisible = YES;
  600. [self showPeersInfo];
  601. }
  602. - (void)showDetailedViewWithTimer
  603. {
  604. if (_isDetailedViewVisible) {
  605. [self hideDetailedView];
  606. } else {
  607. [self showDetailedView];
  608. [self setDetailedViewTimer];
  609. }
  610. }
  611. - (void)hideDetailedView
  612. {
  613. // Keep detailed view visible while push to talk is active
  614. if (_pushToTalkActive) {
  615. [self setDetailedViewTimer];
  616. return;
  617. }
  618. _isDetailedViewVisible = NO;
  619. [self hidePeersInfo];
  620. [self invalidateDetailedViewTimer];
  621. }
  622. - (void)setAudioMuteButtonActive:(BOOL)active
  623. {
  624. dispatch_async(dispatch_get_main_queue(), ^{
  625. NSString *micStatusString = nil;
  626. if (active) {
  627. micStatusString = NSLocalizedString(@"Microphone enabled", nil);
  628. [self->_audioMuteButton setImage:[UIImage systemImageNamed:@"mic.fill" withConfiguration:self->_barButtonsConfiguration] forState:UIControlStateNormal];
  629. } else {
  630. micStatusString = NSLocalizedString(@"Microphone disabled", nil);
  631. [self->_audioMuteButton setImage:[UIImage systemImageNamed:@"mic.slash.fill" withConfiguration:self->_barButtonsConfiguration] forState:UIControlStateNormal];
  632. }
  633. self->_audioMuteButton.accessibilityValue = micStatusString;
  634. });
  635. }
  636. - (void)setAudioMuteButtonEnabled:(BOOL)enabled
  637. {
  638. dispatch_async(dispatch_get_main_queue(), ^{
  639. self->_audioMuteButton.enabled = enabled;
  640. });
  641. }
  642. - (void)setVideoDisableButtonActive:(BOOL)active
  643. {
  644. dispatch_async(dispatch_get_main_queue(), ^{
  645. NSString *cameraStatusString = nil;
  646. if (active) {
  647. cameraStatusString = NSLocalizedString(@"Camera enabled", nil);
  648. [self->_videoDisableButton setImage:[UIImage systemImageNamed:@"video.fill" withConfiguration:self->_barButtonsConfiguration] forState:UIControlStateNormal];
  649. } else {
  650. cameraStatusString = NSLocalizedString(@"Camera disabled", nil);
  651. [self->_videoDisableButton setImage:[UIImage systemImageNamed:@"video.slash.fill" withConfiguration:self->_barButtonsConfiguration] forState:UIControlStateNormal];
  652. }
  653. self->_videoDisableButton.accessibilityValue = cameraStatusString;
  654. });
  655. }
  656. - (void)setVideoDisableButtonEnabled:(BOOL)enabled
  657. {
  658. dispatch_async(dispatch_get_main_queue(), ^{
  659. self->_videoDisableButton.enabled = enabled;
  660. });
  661. }
  662. - (void)setLocalVideoViewHidden:(BOOL)hidden
  663. {
  664. dispatch_async(dispatch_get_main_queue(), ^{
  665. [self->_localVideoView setHidden:hidden];
  666. });
  667. }
  668. - (void)adjustTopBar
  669. {
  670. dispatch_async(dispatch_get_main_queue(), ^{
  671. // Enable/Disable video buttons
  672. self->_videoDisableButton.hidden = self->_isAudioOnly;
  673. self->_switchCameraButton.hidden = self->_isAudioOnly;
  674. self->_videoCallButton.hidden = !self->_isAudioOnly;
  675. self->_lowerHandButton.hidden = !self->_isHandRaised;
  676. // Only when the server supports recording-v1 we have access to callStartTime, otherwise hide the label
  677. self->_callTimeLabel.hidden = ![[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityRecordingV1];
  678. NCAudioController *audioController = [NCAudioController sharedInstance];
  679. self->_speakerButton.hidden = ![audioController isAudioRouteChangeable];
  680. BOOL hideRecordingButton = ![self->_room callRecordingIsInActiveState];
  681. self->_recordingButton.hidden = hideRecordingButton;
  682. // Differ between starting a call recording and an actual running call recording
  683. if (self->_room.callRecording == NCCallRecordingStateVideoStarting || self->_room.callRecording == NCCallRecordingStateAudioStarting) {
  684. self->_recordingButton.tintColor = UIColor.systemGrayColor;
  685. } else {
  686. self->_recordingButton.tintColor = UIColor.systemRedColor;
  687. }
  688. // When the horizontal size is compact (e.g. iPhone portrait) we don't show the 'End call' text on the button
  689. // Don't make assumptions about the device here, because with split screen even an iPad can have a compact width
  690. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  691. [self setHangUpButtonWithTitle:NO];
  692. } else {
  693. [self setHangUpButtonWithTitle:YES];
  694. }
  695. // Make sure we get the correct frame for the stack view, after changing the visibility of buttons
  696. [self->_topBarView setNeedsLayout];
  697. [self->_topBarView layoutIfNeeded];
  698. // Hide titleView if we don't have enough space
  699. // Don't do it in one go, as then we will have some jumping
  700. if (self->_topBarButtonStackView.frame.origin.x < 200) {
  701. [self setHangUpButtonWithTitle:NO];
  702. [self->_titleView setHidden:YES];
  703. [self->_stackViewToTitleViewConstraint setActive:NO];
  704. } else {
  705. [self->_titleView setHidden:NO];
  706. [self->_stackViewToTitleViewConstraint setActive:YES];
  707. }
  708. // Need to update the layout again, if we changed it here
  709. [self->_topBarView setNeedsLayout];
  710. [self->_topBarView layoutIfNeeded];
  711. // Hide the speaker button to make some more room for higher priority buttons
  712. // This should only be the case for iPhone SE (1st Gen) when recording is active and/or hand is raised
  713. if (self->_topBarButtonStackView.frame.origin.x < 0) {
  714. self->_speakerButton.hidden = YES;
  715. }
  716. [self->_topBarView setNeedsLayout];
  717. [self->_topBarView layoutIfNeeded];
  718. if (self->_topBarButtonStackView.frame.origin.x < 0) {
  719. self->_callTimeLabel.hidden = YES;
  720. }
  721. [self adjustMoreButtonMenu];
  722. if (([self->_room canModerate] || self->_room.type == kNCRoomTypeOneToOne) &&
  723. [[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityPublishingPermissions]) {
  724. __weak typeof(self) weakSelf = self;
  725. UIAction *hangupForAllAction = [UIAction actionWithTitle:NSLocalizedString(@"End call for everyone", @"") image:[UIImage systemImageNamed:@"phone.down.fill"] identifier:nil handler:^(UIAction *action) {
  726. [weakSelf hangupForAll:YES];
  727. }];
  728. hangupForAllAction.attributes = UIMenuElementAttributesDestructive;
  729. self.hangUpButton.menu = [UIMenu menuWithTitle:@"" children:@[hangupForAllAction]];
  730. }
  731. });
  732. }
  733. - (void)setHangUpButtonWithTitle:(BOOL)title
  734. {
  735. if (title) {
  736. [_hangUpButton setTitle:NSLocalizedString(@"End call", nil) forState:UIControlStateNormal];
  737. [_hangUpButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
  738. [_hangUpButton setContentEdgeInsets:UIEdgeInsetsMake(0, 16, 0, 24)];
  739. [_hangUpButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 8, 0, -8)];
  740. } else {
  741. [_hangUpButton setTitle:@"" forState:UIControlStateNormal];
  742. [_hangUpButton setContentEdgeInsets:UIEdgeInsetsZero];
  743. [_hangUpButton setTitleEdgeInsets:UIEdgeInsetsZero];
  744. }
  745. }
  746. - (void)adjustConstraints
  747. {
  748. CGFloat rightConstraintConstant = [self getRightSideConstraintConstant];
  749. [self->_collectionViewRightConstraint setConstant:rightConstraintConstant];
  750. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  751. [self->_collectionViewLeftConstraint setConstant:0.0f];
  752. } else {
  753. [self->_collectionViewLeftConstraint setConstant:8.0f];
  754. }
  755. if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
  756. [self->_collectionViewBottomConstraint setConstant:0.0f];
  757. [self->_sideBarViewBottomConstraint setConstant:0.0f];
  758. } else {
  759. [self->_collectionViewBottomConstraint setConstant:8.0f];
  760. [self->_sideBarViewBottomConstraint setConstant:8.0f];
  761. }
  762. }
  763. - (void)showScreensharingPicker
  764. {
  765. RPSystemBroadcastPickerView *broadcastPicker = [[RPSystemBroadcastPickerView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
  766. broadcastPicker.preferredExtension = [NSString stringWithFormat:@"%@.BroadcastUploadExtension", NSBundle.mainBundle.bundleIdentifier];
  767. broadcastPicker.showsMicrophoneButton = NO;
  768. UIButton *btn = nil;
  769. for (UIView *subview in broadcastPicker.subviews) {
  770. if ([subview isKindOfClass:[UIButton class]]) {
  771. btn = (UIButton *)subview;
  772. }
  773. }
  774. if (btn != nil) {
  775. [btn sendActionsForControlEvents:UIControlEventTouchUpInside];
  776. } else {
  777. NSLog(@"RPSystemBroadcastPickerView button not found");
  778. }
  779. }
  780. - (void)adjustMoreButtonMenu
  781. {
  782. // When we target iOS 15, we might want to use an uncached UIDeferredMenuElement
  783. NSMutableArray *items = [[NSMutableArray alloc] init];
  784. __weak typeof(self) weakSelf = self;
  785. // Add speaker button to menu if it was hidden from topbar
  786. NCAudioController *audioController = [NCAudioController sharedInstance];
  787. if ([self.speakerButton isHidden] && [audioController isAudioRouteChangeable]) {
  788. UIImage *speakerImage = [UIImage systemImageNamed:@"speaker.slash.fill"];
  789. NSString *speakerActionTitle = NSLocalizedString(@"Disable speaker", nil);
  790. if (![NCAudioController sharedInstance].isSpeakerActive) {
  791. speakerImage = [UIImage systemImageNamed:@"speaker.wave.3.fill"];
  792. speakerActionTitle = NSLocalizedString(@"Enable speaker", nil);
  793. }
  794. BOOL shouldShowAirPlayButton = audioController.numberOfAvailableInputs > 1;
  795. if (shouldShowAirPlayButton) {
  796. speakerImage = [UIImage systemImageNamed:@"airplayaudio"];
  797. speakerActionTitle = NSLocalizedString(@"Audio options", nil);
  798. }
  799. void (^speakerBlock)(UIAction *action) = ^void(UIAction *action) {
  800. [weakSelf speakerButtonPressed:nil];
  801. };
  802. void (^airplayBlock)(UIAction *action) = ^void(UIAction *action) {
  803. __strong typeof(self) strongSelf = weakSelf;
  804. for (id subview in strongSelf->_airplayView.subviews) {
  805. if ([subview isKindOfClass:[UIButton class]]) {
  806. [subview sendActionsForControlEvents:UIControlEventTouchUpInside];
  807. }
  808. }
  809. };
  810. UIAction *speakerAction = [UIAction actionWithTitle:speakerActionTitle image:speakerImage identifier:nil handler:shouldShowAirPlayButton ? airplayBlock : speakerBlock];
  811. [items addObject:speakerAction];
  812. }
  813. // Raise hand
  814. if ([[NCDatabaseManager sharedInstance] serverHasTalkCapability:kCapabilityRaiseHand]) {
  815. NSString *raiseHandTitel = NSLocalizedString(@"Raise hand", nil);
  816. if (_isHandRaised) {
  817. raiseHandTitel = NSLocalizedString(@"Lower hand", nil);
  818. }
  819. UIAction *raiseHandAction = [UIAction actionWithTitle:raiseHandTitel image:[UIImage systemImageNamed:@"hand.raised.fill"] identifier:nil handler:^(UIAction *action) {
  820. __strong typeof(self) strongSelf = weakSelf;
  821. [strongSelf->_callController raiseHand:!strongSelf->_isHandRaised];
  822. strongSelf->_isHandRaised = !strongSelf->_isHandRaised;
  823. [strongSelf adjustTopBar];
  824. }];
  825. [items addObject:raiseHandAction];
  826. }
  827. // Send a reaction
  828. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  829. ServerCapabilities *serverCapabilities = [[NCDatabaseManager sharedInstance] serverCapabilitiesForAccountId:activeAccount.accountId];
  830. if (serverCapabilities.callReactions.count > 0) {
  831. NSMutableArray *reactionItems = [[NSMutableArray alloc] init];
  832. for (NSString *reaction in serverCapabilities.callReactions) {
  833. UIAction *reactionAction = [UIAction actionWithTitle:reaction image:nil identifier:nil handler:^(UIAction *action) {
  834. __strong typeof(self) strongSelf = weakSelf;
  835. [strongSelf->_callController sendReaction:reaction];
  836. [strongSelf addReaction:reaction fromUser:activeAccount.userDisplayName];
  837. }];
  838. [reactionItems addObject:reactionAction];
  839. }
  840. UIMenu *reactionMenu;
  841. if (@available(iOS 16.0, *)) {
  842. NSInteger currentItemsCount = 0;
  843. NSMutableArray *temporaryReactionItems = [[NSMutableArray alloc] init];
  844. NSMutableArray *temporaryReactionMenus = [[NSMutableArray alloc] init];
  845. for (UIAction *reactionAction in reactionItems) {
  846. currentItemsCount += 1;
  847. [temporaryReactionItems addObject:reactionAction];
  848. if (currentItemsCount >= 2) {
  849. UIMenu *inlineReactionMenu = [UIMenu menuWithTitle:@""
  850. image:nil
  851. identifier:nil
  852. options:UIMenuOptionsDisplayInline
  853. children:temporaryReactionItems];
  854. inlineReactionMenu.preferredElementSize = UIMenuElementSizeSmall;
  855. [temporaryReactionMenus addObject:inlineReactionMenu];
  856. temporaryReactionItems = [[NSMutableArray alloc] init];
  857. currentItemsCount = 0;
  858. }
  859. }
  860. if (currentItemsCount > 0) {
  861. UIMenu *inlineReactionMenu = [UIMenu menuWithTitle:@""
  862. image:nil
  863. identifier:nil
  864. options:UIMenuOptionsDisplayInline
  865. children:temporaryReactionItems];
  866. inlineReactionMenu.preferredElementSize = UIMenuElementSizeSmall;
  867. [temporaryReactionMenus addObject:inlineReactionMenu];
  868. }
  869. reactionMenu = [UIMenu menuWithTitle:NSLocalizedString(@"Send a reaction", nil)
  870. image:[UIImage systemImageNamed:@"face.smiling"]
  871. identifier:nil
  872. options:0
  873. children:temporaryReactionMenus];
  874. } else {
  875. // Show the menu as one long list on devices < iOS 16
  876. reactionMenu = [UIMenu menuWithTitle:NSLocalizedString(@"Send a reaction", nil)
  877. image:[UIImage systemImageNamed:@"face.smiling"]
  878. identifier:nil
  879. options:0
  880. children:reactionItems];
  881. }
  882. [items addObject:reactionMenu];
  883. }
  884. // Start/Stop recording
  885. if ([self->_room isUserOwnerOrModerator] && [[NCSettingsController sharedInstance] isRecordingEnabled]) {
  886. UIImage *recordingImage = [UIImage systemImageNamed:@"record.circle.fill"];
  887. NSString *recordingActionTitle = NSLocalizedString(@"Start recording", nil);
  888. if ([self->_room callRecordingIsInActiveState]) {
  889. recordingImage = [UIImage systemImageNamed:@"stop.circle.fill"];
  890. recordingActionTitle = NSLocalizedString(@"Stop recording", nil);
  891. }
  892. UIAction *recordingAction = [UIAction actionWithTitle:recordingActionTitle image:recordingImage identifier:nil handler:^(UIAction *action) {
  893. __strong typeof(self) strongSelf = weakSelf;
  894. if ([strongSelf->_room callRecordingIsInActiveState]) {
  895. [strongSelf showStopRecordingConfirmationDialog];
  896. } else {
  897. [strongSelf->_callController startRecording];
  898. }
  899. }];
  900. [items addObject:recordingAction];
  901. }
  902. // Background blur
  903. if (!_isAudioOnly) {
  904. UIImage *blurActionImage = [UIImage systemImageNamed:@"person.crop.rectangle.fill"];
  905. NSString *blurActionTitle = NSLocalizedString(@"Enable blur", nil);
  906. if (@available(iOS 16.0, *)) {
  907. blurActionImage = [UIImage systemImageNamed:@"person.and.background.dotted"];
  908. }
  909. if ([self->_callController isBackgroundBlurEnabled]) {
  910. blurActionImage = [UIImage systemImageNamed:@"person.crop.rectangle"];
  911. blurActionTitle = NSLocalizedString(@"Disable blur", nil);
  912. }
  913. UIAction *toggleBackgroundBlur = [UIAction actionWithTitle:blurActionTitle image:blurActionImage identifier:nil handler:^(UIAction *action) {
  914. __strong typeof(self) strongSelf = weakSelf;
  915. [strongSelf->_callController enableBackgroundBlur:![strongSelf->_callController isBackgroundBlurEnabled]];
  916. [strongSelf adjustTopBar];
  917. }];
  918. [items addObject:toggleBackgroundBlur];
  919. }
  920. // Screensharing
  921. UIImage *screensharingImage = [UIImage systemImageNamed:@"rectangle.inset.filled.on.rectangle"];
  922. NSString *screensharingActionTitle = NSLocalizedString(@"Enable screensharing", nil);
  923. if ([self->_callController screensharingActive]) {
  924. screensharingImage = [UIImage systemImageNamed:@"rectangle.on.rectangle.slash"];
  925. screensharingActionTitle = NSLocalizedString(@"Stop screensharing", nil);
  926. }
  927. UIAction *screenshareAction = [UIAction actionWithTitle:screensharingActionTitle image:screensharingImage identifier:nil handler:^(UIAction *action) {
  928. [weakSelf showScreensharingPicker];
  929. }];
  930. [items addObject:screenshareAction];
  931. self.moreMenuButton.menu = [UIMenu menuWithTitle:@"" children:items];
  932. }
  933. - (void)adjustSpeakerButton
  934. {
  935. dispatch_async(dispatch_get_main_queue(), ^{
  936. NCAudioController *audioController = [NCAudioController sharedInstance];
  937. [self setSpeakerButtonActive:audioController.isSpeakerActive];
  938. // If the visibility of the speaker button does not reflect the route changeability
  939. // we need to try and adjust the top bar
  940. if (self->_speakerButton.isHidden == [audioController isAudioRouteChangeable]) {
  941. [self adjustTopBar];
  942. }
  943. // Show AirPlay button if there are more audio routes available
  944. if (audioController.numberOfAvailableInputs > 1) {
  945. [self setSpeakerButtonWithAirplayButton];
  946. } else {
  947. [self->_airplayView removeFromSuperview];
  948. }
  949. });
  950. }
  951. - (void)setDetailedViewTimer
  952. {
  953. [self invalidateDetailedViewTimer];
  954. _detailedViewTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(hideDetailedView) userInfo:nil repeats:NO];
  955. }
  956. - (void)invalidateDetailedViewTimer
  957. {
  958. [_detailedViewTimer invalidate];
  959. _detailedViewTimer = nil;
  960. }
  961. - (void)presentJoinError:(NSString *)alertMessage
  962. {
  963. NSString *alertTitle = [NSString stringWithFormat:NSLocalizedString(@"Could not join %@ call", nil), _room.displayName];
  964. if (_room.type == kNCRoomTypeOneToOne) {
  965. alertTitle = [NSString stringWithFormat:NSLocalizedString(@"Could not join call with %@", nil), _room.displayName];
  966. }
  967. UIAlertController * alert = [UIAlertController alertControllerWithTitle:alertTitle
  968. message:alertMessage
  969. preferredStyle:UIAlertControllerStyleAlert];
  970. UIAlertAction* okButton = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  971. style:UIAlertActionStyleDefault
  972. handler:^(UIAlertAction * _Nonnull action) {
  973. [self hangupForAll:NO];
  974. }];
  975. [alert addAction:okButton];
  976. dispatch_async(dispatch_get_main_queue(), ^{
  977. [self presentViewController:alert animated:YES completion:nil];
  978. });
  979. }
  980. - (void)adjustLocalVideoPositionFromOriginPosition:(CGPoint)position
  981. {
  982. UIEdgeInsets safeAreaInsets = _localVideoView.superview.safeAreaInsets;
  983. CGFloat edgeInsetTop = 16 + _topBarView.frame.origin.y + _topBarView.frame.size.height;
  984. CGFloat edgeInsetLeft = 16 + safeAreaInsets.left + _collectionViewLeftConstraint.constant;
  985. CGFloat edgeInsetBottom = 16 + safeAreaInsets.bottom + _collectionViewBottomConstraint.constant;
  986. CGFloat edgeInsetRight = 16 + safeAreaInsets.right + _collectionViewRightConstraint.constant;
  987. UIEdgeInsets edgeInsets = UIEdgeInsetsMake(edgeInsetTop, edgeInsetLeft, edgeInsetBottom, edgeInsetRight);
  988. CGSize parentSize = _localVideoView.superview.bounds.size;
  989. CGSize viewSize = _localVideoView.bounds.size;
  990. // Adjust left
  991. if (position.x < edgeInsets.left) {
  992. position = CGPointMake(edgeInsets.left, position.y);
  993. }
  994. // Adjust top
  995. if (position.y < edgeInsets.top) {
  996. position = CGPointMake(position.x, edgeInsets.top);
  997. }
  998. // Adjust right
  999. if (position.x > parentSize.width - viewSize.width - edgeInsets.right) {
  1000. position = CGPointMake(parentSize.width - viewSize.width - edgeInsets.right, position.y);
  1001. }
  1002. // Adjust bottom
  1003. if (position.y > parentSize.height - viewSize.height - edgeInsets.bottom) {
  1004. position = CGPointMake(position.x, parentSize.height - viewSize.height - edgeInsets.bottom);
  1005. }
  1006. CGRect frame = _localVideoView.frame;
  1007. frame.origin.x = position.x;
  1008. frame.origin.y = position.y;
  1009. [UIView animateWithDuration:0.3 animations:^{
  1010. self->_localVideoView.frame = frame;
  1011. }];
  1012. }
  1013. - (void)localVideoDragged:(UIPanGestureRecognizer *)gesture
  1014. {
  1015. if (gesture.view == _localVideoView) {
  1016. if (gesture.state == UIGestureRecognizerStateBegan) {
  1017. _localVideoDragStartingPosition = gesture.view.center;
  1018. } else if (gesture.state == UIGestureRecognizerStateChanged) {
  1019. CGPoint translation = [gesture translationInView:gesture.view];
  1020. _localVideoView.center = CGPointMake(_localVideoDragStartingPosition.x + translation.x, _localVideoDragStartingPosition.y + translation.y);
  1021. } else if (gesture.state == UIGestureRecognizerStateEnded) {
  1022. _localVideoOriginPosition = gesture.view.frame.origin;
  1023. [self adjustLocalVideoPositionFromOriginPosition:_localVideoOriginPosition];
  1024. }
  1025. }
  1026. }
  1027. - (void)callDurationTimerUpdate
  1028. {
  1029. NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
  1030. // In case we are the ones who start the call, we don't have the server-side callStartTime, so we set it locally
  1031. if (self.room.callStartTime == 0) {
  1032. self.room.callStartTime = currentTimestamp;
  1033. }
  1034. // Make sure that the remote callStartTime is not in the future
  1035. NSInteger callStartTime = MIN(self.room.callStartTime, currentTimestamp);
  1036. long callDuration = currentTimestamp - callStartTime;
  1037. long oneHourInSeconds = 60 * 60;
  1038. long hours = callDuration / 3600;
  1039. long minutes = (callDuration / 60) % 60;
  1040. long seconds = callDuration % 60;
  1041. if (hours > 0) {
  1042. [self.callTimeLabel setText:[NSString stringWithFormat:@"%lu:%02lu:%02lu", hours, minutes, seconds]];
  1043. } else {
  1044. [self.callTimeLabel setText:[NSString stringWithFormat:@"%02lu:%02lu", minutes, seconds]];
  1045. }
  1046. if (self->_topBarButtonStackView.frame.origin.x < 0) {
  1047. [self adjustTopBar];
  1048. }
  1049. if (callDuration == oneHourInSeconds) {
  1050. NSString *callRunningFor1h = NSLocalizedString(@"The call has been running for one hour", nil);
  1051. [[JDStatusBarNotificationPresenter sharedPresenter] presentWithText:callRunningFor1h dismissAfterDelay:7.0 includedStyle:JDStatusBarNotificationIncludedStyleDark];
  1052. }
  1053. }
  1054. #pragma mark - Call actions
  1055. -(void)handlePushToTalk:(UILongPressGestureRecognizer *)gestureRecognizer
  1056. {
  1057. if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
  1058. [self pushToTalkStart];
  1059. } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
  1060. [self pushToTalkEnd];
  1061. }
  1062. }
  1063. - (void)pushToTalkStart
  1064. {
  1065. if (!_callController) {
  1066. return;
  1067. }
  1068. [_callController getAudioEnabledStateWithCompletionBlock:^(BOOL isEnabled) {
  1069. if (!isEnabled) {
  1070. [self setAudioMuted:NO];
  1071. dispatch_async(dispatch_get_main_queue(), ^{
  1072. [self->_buttonFeedbackGenerator impactOccurred];
  1073. self->_pushToTalkActive = YES;
  1074. });
  1075. }
  1076. }];
  1077. }
  1078. - (void)pushToTalkEnd
  1079. {
  1080. if (_pushToTalkActive) {
  1081. [self setAudioMuted:YES];
  1082. _pushToTalkActive = NO;
  1083. }
  1084. }
  1085. - (IBAction)audioButtonPressed:(id)sender
  1086. {
  1087. if (!_callController) {
  1088. return;
  1089. }
  1090. [_callController getAudioEnabledStateWithCompletionBlock:^(BOOL isEnabled) {
  1091. if ([CallKitManager isCallKitAvailable]) {
  1092. dispatch_async(dispatch_get_main_queue(), ^{
  1093. [[CallKitManager sharedInstance] changeAudioMuted:isEnabled forCall:self->_room.token];
  1094. });
  1095. } else {
  1096. [self setAudioMuted:isEnabled];
  1097. }
  1098. }];
  1099. }
  1100. - (void)forceMuteAudio
  1101. {
  1102. if (!_callController) {
  1103. return;
  1104. }
  1105. [_callController getAudioEnabledStateWithCompletionBlock:^(BOOL isEnabled) {
  1106. if (!isEnabled) {
  1107. // We are already muted, no need to mute again
  1108. return;
  1109. }
  1110. [self setAudioMuted:YES];
  1111. NSString *micDisabledString = NSLocalizedString(@"Microphone disabled", nil);
  1112. NSString *forceMutedString = NSLocalizedString(@"You have been muted by a moderator", nil);
  1113. dispatch_async(dispatch_get_main_queue(), ^{
  1114. [[JDStatusBarNotificationPresenter sharedPresenter] presentWithTitle:micDisabledString subtitle:forceMutedString includedStyle:JDStatusBarNotificationIncludedStyleDark completion:nil];
  1115. [[JDStatusBarNotificationPresenter sharedPresenter] dismissAfterDelay:7.0];
  1116. });
  1117. }];
  1118. }
  1119. - (void)setAudioMuted:(BOOL)isMuted
  1120. {
  1121. [_callController enableAudio:!isMuted];
  1122. [self setAudioMuteButtonActive:!isMuted];
  1123. }
  1124. - (IBAction)videoButtonPressed:(id)sender
  1125. {
  1126. if (!_callController) {
  1127. return;
  1128. }
  1129. [_callController getVideoEnabledStateWithCompletionBlock:^(BOOL isEnabled) {
  1130. [self setLocalVideoEnabled:!isEnabled];
  1131. self->_userDisabledVideo = isEnabled;
  1132. }];
  1133. }
  1134. - (void)disableLocalVideo
  1135. {
  1136. [self setLocalVideoEnabled:NO];
  1137. }
  1138. - (void)enableLocalVideo
  1139. {
  1140. [self setLocalVideoEnabled:YES];
  1141. }
  1142. - (void)setLocalVideoEnabled:(BOOL)enabled
  1143. {
  1144. [_callController enableVideo:enabled];
  1145. [self setLocalVideoViewHidden:!enabled];
  1146. [self setVideoDisableButtonActive:enabled];
  1147. }
  1148. - (IBAction)switchCameraButtonPressed:(id)sender
  1149. {
  1150. [_callController switchCamera];
  1151. [self flipLocalVideoView];
  1152. }
  1153. - (void)flipLocalVideoView
  1154. {
  1155. CATransition *animation = [CATransition animation];
  1156. animation.duration = .5f;
  1157. animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  1158. animation.type = @"oglFlip";
  1159. animation.subtype = kCATransitionFromRight;
  1160. [self.localVideoView.layer addAnimation:animation forKey:nil];
  1161. }
  1162. - (IBAction)speakerButtonPressed:(id)sender
  1163. {
  1164. if ([NCAudioController sharedInstance].isSpeakerActive) {
  1165. [self disableSpeaker];
  1166. _userDisabledSpeaker = YES;
  1167. } else {
  1168. [self enableSpeaker];
  1169. _userDisabledSpeaker = NO;
  1170. }
  1171. [self adjustMoreButtonMenu];
  1172. }
  1173. - (void)disableSpeaker
  1174. {
  1175. [self setSpeakerButtonActive:NO];
  1176. [[WebRTCCommon shared] dispatch:^{
  1177. [[NCAudioController sharedInstance] setAudioSessionToVoiceChatMode];
  1178. }];
  1179. }
  1180. - (void)enableSpeaker
  1181. {
  1182. [self setSpeakerButtonActive:YES];
  1183. [[WebRTCCommon shared] dispatch:^{
  1184. [[NCAudioController sharedInstance] setAudioSessionToVideoChatMode];
  1185. }];
  1186. }
  1187. - (void)setSpeakerButtonActive:(BOOL)active
  1188. {
  1189. dispatch_async(dispatch_get_main_queue(), ^{
  1190. NSString *speakerStatusString = nil;
  1191. if (active) {
  1192. speakerStatusString = NSLocalizedString(@"Speaker enabled", nil);
  1193. [self.speakerButton setImage:[UIImage systemImageNamed:@"speaker.wave.3.fill" withConfiguration:self->_barButtonsConfiguration] forState:UIControlStateNormal];
  1194. } else {
  1195. speakerStatusString = NSLocalizedString(@"Speaker disabled", nil);
  1196. [self.speakerButton setImage:[UIImage systemImageNamed:@"speaker.slash.fill" withConfiguration:self->_barButtonsConfiguration] forState:UIControlStateNormal];
  1197. }
  1198. self.speakerButton.accessibilityValue = speakerStatusString;
  1199. self.speakerButton.accessibilityHint = NSLocalizedString(@"Double tap to enable or disable the speaker", nil);
  1200. });
  1201. }
  1202. - (void)setSpeakerButtonWithAirplayButton
  1203. {
  1204. dispatch_async(dispatch_get_main_queue(), ^{
  1205. [self.speakerButton setImage:nil forState:UIControlStateNormal];
  1206. self.speakerButton.accessibilityValue = NSLocalizedString(@"AirPlay button", nil);
  1207. self.speakerButton.accessibilityHint = NSLocalizedString(@"Double tap to select different audio routes", nil);
  1208. [self.speakerButton addSubview:self->_airplayView];
  1209. });
  1210. }
  1211. - (IBAction)hangupButtonPressed:(id)sender
  1212. {
  1213. [self hangupForAll:NO];
  1214. }
  1215. - (void)hangupForAll:(BOOL)allParticipants
  1216. {
  1217. if (!_hangingUp) {
  1218. _hangingUp = YES;
  1219. // Dismiss possible notifications
  1220. [[NSNotificationCenter defaultCenter] removeObserver:self];
  1221. // Make sure we don't try to receive messages while hanging up
  1222. if (_chatViewController) {
  1223. [_chatViewController leaveChat];
  1224. _chatViewController = nil;
  1225. }
  1226. // Make sure there's no menu interfering with our dismissal
  1227. [self.moreMenuButton.contextMenuInteraction dismissMenu];
  1228. [self.hangUpButton.contextMenuInteraction dismissMenu];
  1229. [self.delegate callViewControllerWantsToBeDismissed:self];
  1230. [_callController stopCapturing];
  1231. [_localVideoView setHidden:YES];
  1232. dispatch_async(dispatch_get_main_queue(), ^{
  1233. for (NCPeerConnection *peerConnection in self->_peersInCall) {
  1234. // Video renderers
  1235. RTCMTLVideoView *videoRenderer = [self->_videoRenderersDict objectForKey:peerConnection.peerIdentifier];
  1236. [self->_videoRenderersDict removeObjectForKey:peerConnection.peerIdentifier];
  1237. [[WebRTCCommon shared] dispatch:^{
  1238. [[[peerConnection getRemoteStream].videoTracks firstObject] removeRenderer:videoRenderer];
  1239. }];
  1240. }
  1241. for (NCPeerConnection *peerConnection in self->_screenPeersInCall) {
  1242. // Screen renderers
  1243. RTCMTLVideoView *screenRenderer = [self->_screenRenderersDict objectForKey:peerConnection.peerId];
  1244. [self->_screenRenderersDict removeObjectForKey:peerConnection.peerId];
  1245. [[WebRTCCommon shared] dispatch:^{
  1246. [[[peerConnection getRemoteStream].videoTracks firstObject] removeRenderer:screenRenderer];
  1247. }];
  1248. }
  1249. [self->_callDurationTimer invalidate];
  1250. });
  1251. if (_callController) {
  1252. [_callController leaveCallForAll:allParticipants];
  1253. } else {
  1254. [self finishCall];
  1255. }
  1256. }
  1257. }
  1258. - (IBAction)videoCallButtonPressed:(id)sender
  1259. {
  1260. [self showUpgradeToVideoCallDialog];
  1261. }
  1262. - (void)showUpgradeToVideoCallDialog
  1263. {
  1264. UIAlertController *confirmDialog =
  1265. [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Do you want to enable your camera?", nil)
  1266. message:NSLocalizedString(@"If you enable your camera, this call will be interrupted for a few seconds.", nil)
  1267. preferredStyle:UIAlertControllerStyleAlert];
  1268. UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Enable", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  1269. [self upgradeToVideoCall];
  1270. }];
  1271. [confirmDialog addAction:confirmAction];
  1272. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
  1273. [confirmDialog addAction:cancelAction];
  1274. [self presentViewController:confirmDialog animated:YES completion:nil];
  1275. }
  1276. - (void)upgradeToVideoCall
  1277. {
  1278. _videoCallUpgrade = YES;
  1279. [self hangupForAll:NO];
  1280. }
  1281. - (IBAction)toggleChatButtonPressed:(id)sender
  1282. {
  1283. [self toggleChatView];
  1284. }
  1285. - (CGFloat)getRightSideConstraintConstant
  1286. {
  1287. CGFloat constant = 0;
  1288. if (self.sideBarWidthConstraint.constant > 0) {
  1289. // Take sidebar width into account
  1290. constant += self.sideBarWidthConstraint.constant;
  1291. // Add padding between the element and the sidebar
  1292. constant += 8;
  1293. }
  1294. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular) {
  1295. // On regular size classes, we also have a padding of 8 to the safe area
  1296. constant += 8;
  1297. }
  1298. return constant;
  1299. }
  1300. - (void)setSideBarVisible:(BOOL)visible animated:(BOOL)animated withCompletion:(void (^ __nullable)(void))block
  1301. {
  1302. [self.view layoutIfNeeded];
  1303. if (visible) {
  1304. [self.sideBarView setHidden:NO];
  1305. [self.sideBarWidthConstraint setConstant:kSidebarWidth];
  1306. } else {
  1307. [self.sideBarWidthConstraint setConstant:0];
  1308. }
  1309. CGFloat rightConstraintConstant = [self getRightSideConstraintConstant];
  1310. [self.topBarViewRightContraint setConstant:rightConstraintConstant];
  1311. [self.screenshareViewRightContraint setConstant:rightConstraintConstant];
  1312. [self.collectionViewRightConstraint setConstant:rightConstraintConstant];
  1313. [self adjustTopBar];
  1314. CGPoint localVideoViewOrigin = self.localVideoView.frame.origin;
  1315. // Check if localVideoView needs to be moved to the right when sidebar is being closed
  1316. if (!visible) {
  1317. CGFloat sideBarWidthGap = self.collectionView.frame.size.width - kSidebarWidth;
  1318. if (localVideoViewOrigin.x > sideBarWidthGap) {
  1319. localVideoViewOrigin.x = self.localVideoView.superview.frame.size.width;
  1320. }
  1321. }
  1322. void (^animations)(void) = ^void() {
  1323. [self.titleView layoutIfNeeded];
  1324. [self.view layoutIfNeeded];
  1325. [self adjustLocalVideoPositionFromOriginPosition:localVideoViewOrigin];
  1326. };
  1327. void (^afterAnimations)(void) = ^void() {
  1328. if (!visible) {
  1329. [self.sideBarView setHidden:YES];
  1330. }
  1331. if (block) {
  1332. block();
  1333. }
  1334. };
  1335. if (animated) {
  1336. [UIView animateWithDuration:0.3f animations:^{
  1337. animations();
  1338. } completion:^(BOOL finished) {
  1339. afterAnimations();
  1340. }];
  1341. } else {
  1342. animations();
  1343. afterAnimations();
  1344. }
  1345. }
  1346. - (void)adjustChatLocation
  1347. {
  1348. if (!_chatNavigationController) {
  1349. return;
  1350. }
  1351. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact && [_chatNavigationController.view isDescendantOfView:_sideBarView]) {
  1352. // Chat is displayed in the sidebar, but needs to move to full screen
  1353. // Remove chat from the sidebar and add to call view
  1354. [_chatNavigationController.view removeFromSuperview];
  1355. [self.view addSubview:_chatNavigationController.view];
  1356. // Show the navigationbar in case of fullscreen and adjust the frame
  1357. [_chatNavigationController setNavigationBarHidden:NO];
  1358. _chatNavigationController.view.frame = self.view.bounds;
  1359. // Finally hide the sidebar
  1360. [self setSideBarVisible:NO animated:NO withCompletion:nil];
  1361. } else if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular && [_chatNavigationController.view isDescendantOfView:self.view]) {
  1362. // Chat is fullscreen, but should move to the sidebar
  1363. // Remove chat from the call view and move it to the sidebar
  1364. [_chatNavigationController.view removeFromSuperview];
  1365. [self.sideBarView addSubview:_chatNavigationController.view];
  1366. // Show the sidebar to have the correct bounds
  1367. [self setSideBarVisible:YES animated:NO withCompletion:nil];
  1368. CGRect sideBarViewBounds = self.sideBarView.bounds;
  1369. _chatNavigationController.view.frame = CGRectMake(sideBarViewBounds.origin.x, sideBarViewBounds.origin.y, kSidebarWidth, sideBarViewBounds.size.height);
  1370. // Don't show the navigation bar when we show the chat in the sidebar
  1371. [_chatNavigationController setNavigationBarHidden:YES];
  1372. }
  1373. }
  1374. - (void)showChat
  1375. {
  1376. if (!_chatNavigationController) {
  1377. // Create new chat controller
  1378. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  1379. NCRoom *room = [[NCDatabaseManager sharedInstance] roomWithToken:_room.token forAccountId:activeAccount.accountId];
  1380. _chatViewController = [[ChatViewController alloc] initFor:room];
  1381. _chatViewController.presentedInCall = YES;
  1382. _chatNavigationController = [[UINavigationController alloc] initWithRootViewController:_chatViewController];
  1383. }
  1384. [self addChildViewController:_chatNavigationController];
  1385. if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
  1386. // Show chat fullscreen
  1387. [self.view addSubview:_chatNavigationController.view];
  1388. _chatNavigationController.view.frame = self.view.bounds;
  1389. _chatNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  1390. } else {
  1391. // Show chat in sidebar
  1392. [self.sideBarView addSubview:_chatNavigationController.view];
  1393. CGRect sideBarViewBounds = self.sideBarView.bounds;
  1394. _chatNavigationController.view.frame = CGRectMake(sideBarViewBounds.origin.x, sideBarViewBounds.origin.y, kSidebarWidth, sideBarViewBounds.size.height);
  1395. // Make sure the width does not change when collapsing the side bar (weird animation)
  1396. _chatNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  1397. [_chatNavigationController setNavigationBarHidden:YES];
  1398. __weak typeof(self) weakSelf = self;
  1399. [self setSideBarVisible:YES animated:YES withCompletion:^{
  1400. __strong typeof(self) strongSelf = weakSelf;
  1401. strongSelf->_chatNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  1402. }];
  1403. }
  1404. [_chatNavigationController didMoveToParentViewController:self];
  1405. }
  1406. - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection
  1407. {
  1408. if (!_chatNavigationController) {
  1409. return;
  1410. }
  1411. if (previousTraitCollection.horizontalSizeClass != self.traitCollection.horizontalSizeClass) {
  1412. // Need to adjust the position of the chat, either sidebar -> fullscreen or fullscreen -> sidebar
  1413. [self adjustChatLocation];
  1414. }
  1415. }
  1416. - (void)toggleChatView
  1417. {
  1418. if (!_chatNavigationController) {
  1419. [self showChat];
  1420. if (!_isAudioOnly) {
  1421. [self.view bringSubviewToFront:_localVideoView];
  1422. }
  1423. [self removeTapGestureForDetailedView];
  1424. } else {
  1425. [self.view layoutIfNeeded];
  1426. // Make sure we have a nice animation when closing the side bar and the chat is not squished
  1427. _chatNavigationController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  1428. __weak typeof(self) weakSelf = self;
  1429. [self setSideBarVisible:NO animated:YES withCompletion:^{
  1430. __strong typeof(self) strongSelf = weakSelf;
  1431. [strongSelf->_chatViewController leaveChat];
  1432. strongSelf->_chatViewController = nil;
  1433. [strongSelf->_chatNavigationController willMoveToParentViewController:nil];
  1434. [strongSelf->_chatNavigationController.view removeFromSuperview];
  1435. [strongSelf->_chatNavigationController removeFromParentViewController];
  1436. strongSelf->_chatNavigationController = nil;
  1437. if (!strongSelf->_isAudioOnly && strongSelf->_callState == CallStateInCall) {
  1438. [strongSelf addTapGestureForDetailedView];
  1439. [strongSelf showDetailedViewWithTimer];
  1440. }
  1441. }];
  1442. }
  1443. }
  1444. - (void)finishCall
  1445. {
  1446. _callController = nil;
  1447. if (_videoCallUpgrade) {
  1448. _videoCallUpgrade = NO;
  1449. [self.delegate callViewControllerWantsVideoCallUpgrade:self];
  1450. } else {
  1451. [self.delegate callViewControllerDidFinish:self];
  1452. }
  1453. }
  1454. - (IBAction)lowerHandButtonPressed:(id)sender
  1455. {
  1456. self->_isHandRaised = NO;
  1457. [self->_callController raiseHand:NO];
  1458. [self adjustTopBar];
  1459. }
  1460. - (IBAction)videoRecordingButtonPressed:(id)sender
  1461. {
  1462. if (![_room canModerate]) {
  1463. NSString *notificationText = NSLocalizedString(@"This call is being recorded", nil);
  1464. [[JDStatusBarNotificationPresenter sharedPresenter] presentWithText:notificationText dismissAfterDelay:7.0 includedStyle:JDStatusBarNotificationIncludedStyleDark];
  1465. return;
  1466. }
  1467. [self showStopRecordingConfirmationDialog];
  1468. }
  1469. - (void)showStopRecordingConfirmationDialog
  1470. {
  1471. UIAlertController *confirmDialog =
  1472. [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Stop recording", nil)
  1473. message:NSLocalizedString(@"Do you want to stop the recording?", nil)
  1474. preferredStyle:UIAlertControllerStyleAlert];
  1475. UIAlertAction *stopAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Stop", @"Action to 'Stop' a recording") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  1476. [self->_callController stopRecording];
  1477. }];
  1478. [confirmDialog addAction:stopAction];
  1479. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:nil];
  1480. [confirmDialog addAction:cancelAction];
  1481. [self presentViewController:confirmDialog animated:YES completion:nil];
  1482. }
  1483. #pragma mark - Call Reactions
  1484. - (void)addReaction:(NSString *)reaction fromUser:(NSString *)user
  1485. {
  1486. CallReactionView *callReactionView = [[CallReactionView alloc] initWithFrame:CGRectZero];
  1487. [callReactionView setReactionWithReaction:reaction actor:user];
  1488. // Schedule when to show reaction
  1489. CGFloat delayBetweenReactions = kReactionViewAnimationDuration / kMaxReactionsInScreen;
  1490. CGFloat now = [[NSDate date] timeIntervalSince1970];
  1491. if (_lastScheduledReaction < now) {
  1492. delayBetweenReactions = (now - _lastScheduledReaction > delayBetweenReactions) ? 0 : delayBetweenReactions;
  1493. _lastScheduledReaction = now;
  1494. }
  1495. _lastScheduledReaction += delayBetweenReactions;
  1496. __weak typeof(self) weakSelf = self;
  1497. CGFloat delay = _lastScheduledReaction - now;
  1498. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){
  1499. [weakSelf showReaction:callReactionView];
  1500. });
  1501. }
  1502. - (void)showReaction:(CallReactionView *)callReactionView
  1503. {
  1504. CGSize callViewSize = self.view.bounds.size;
  1505. CGSize callReactionSize = [callReactionView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
  1506. CGFloat minLeftPosition = callViewSize.width * 0.05;
  1507. CGFloat maxLeftPosition = callViewSize.width * 0.2;
  1508. CGFloat randomLeftPosition = minLeftPosition + arc4random_uniform(maxLeftPosition - minLeftPosition + 1);
  1509. CGFloat startPosition = callViewSize.height - self.view.safeAreaInsets.bottom - callReactionSize.height;
  1510. CGFloat minTopPosition = startPosition / 2;
  1511. CGFloat maxTopPosition = minTopPosition * 1.2;
  1512. CGFloat randomTopPosition = minTopPosition + arc4random_uniform(maxTopPosition - minTopPosition + 1);
  1513. if (callViewSize.width - callReactionSize.width < 0) {
  1514. randomLeftPosition = minLeftPosition;
  1515. }
  1516. CGRect reactionInitialPosition = CGRectMake(randomLeftPosition,
  1517. startPosition,
  1518. callReactionSize.width,
  1519. callReactionSize.height);
  1520. callReactionView.frame = reactionInitialPosition;
  1521. [self.view addSubview:callReactionView];
  1522. [self.view bringSubviewToFront:callReactionView];
  1523. [UIView animateWithDuration:2.0 animations:^{
  1524. callReactionView.frame = CGRectMake(reactionInitialPosition.origin.x,
  1525. randomTopPosition,
  1526. reactionInitialPosition.size.width,
  1527. reactionInitialPosition.size.height);
  1528. }];
  1529. [UIView animateWithDuration:1.0 delay:1.0 options:0 animations:^{
  1530. callReactionView.alpha = 0;
  1531. } completion:^(BOOL finished) {
  1532. [callReactionView removeFromSuperview];
  1533. }];
  1534. }
  1535. #pragma mark - CallParticipantViewCell delegate
  1536. - (void)cellWantsToPresentScreenSharing:(CallParticipantViewCell *)participantCell
  1537. {
  1538. NCPeerConnection *peerConnection = [self peerConnectionForPeerIdentifier:participantCell.peerIdentifier];
  1539. [self showScreenOfPeer:peerConnection];
  1540. }
  1541. - (void)cellWantsToChangeZoom:(CallParticipantViewCell *)participantCell showOriginalSize:(BOOL)showOriginalSize
  1542. {
  1543. NCPeerConnection *peer = [self peerConnectionForPeerIdentifier:participantCell.peerIdentifier];
  1544. if (peer) {
  1545. [peer setShowRemoteVideoInOriginalSize:showOriginalSize];
  1546. }
  1547. }
  1548. #pragma mark - UICollectionView Datasource
  1549. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  1550. {
  1551. [self setCallStateForPeersInCall];
  1552. return [_peersInCall count];
  1553. }
  1554. - (void)updateParticipantCell:(CallParticipantViewCell *)cell withPeerConnection:(NCPeerConnection *)peerConnection
  1555. {
  1556. BOOL isVideoDisabled = peerConnection.isRemoteVideoDisabled;
  1557. if (_isAudioOnly || ![peerConnection hasRemoteStream]) {
  1558. isVideoDisabled = YES;
  1559. }
  1560. RTCMTLVideoView *videoView = [_videoRenderersDict objectForKey:peerConnection.peerIdentifier];
  1561. [cell setVideoView:videoView];
  1562. // It is possible that we receive a `didChangeVideoSize` call, while the participant cell was not yet shown,
  1563. // therefore the remote video size will never be set. In case we have a videoView here, use the frame size
  1564. if (videoView) {
  1565. CGSize videoSize = videoView.frame.size;
  1566. CGSize currentSize = [cell getRemoteVideoSize];
  1567. // Only set it, when there's no size set yet
  1568. if (CGSizeEqualToSize(CGSizeZero, currentSize) && !CGSizeEqualToSize(CGSizeZero, videoSize)) {
  1569. [cell setRemoteVideoSize:videoView.frame.size];
  1570. }
  1571. }
  1572. [cell setDisplayName:peerConnection.peerName];
  1573. [cell setAudioDisabled:peerConnection.isRemoteAudioDisabled];
  1574. [cell setScreenShared:[_screenRenderersDict objectForKey:peerConnection.peerId]];
  1575. [cell setVideoDisabled: isVideoDisabled];
  1576. [cell setShowOriginalSize:peerConnection.showRemoteVideoInOriginalSize];
  1577. [cell setRaiseHand:peerConnection.isHandRaised];
  1578. [cell.peerNameLabel setAlpha:_isDetailedViewVisible ? 1.0 : 0.0];
  1579. [cell.audioOffIndicator setAlpha:_isDetailedViewVisible ? 1.0 : 0.0];
  1580. [[WebRTCCommon shared] dispatch:^{
  1581. TalkActor *actor = [self->_callController getActorFromSessionId:peerConnection.peerId];
  1582. if ([actor.rawDisplayName isEqualToString:@""] && peerConnection.peerName && ![peerConnection.peerName isEqualToString:@""]) {
  1583. actor.rawDisplayName = peerConnection.peerName;
  1584. }
  1585. RTCIceConnectionState connectionState = peerConnection.isDummyPeer ?
  1586. RTCIceConnectionStateConnected : [peerConnection getPeerConnection].iceConnectionState;
  1587. dispatch_async(dispatch_get_main_queue(), ^{
  1588. [cell setAvatarForActor:actor];
  1589. [cell setConnectionState:connectionState];
  1590. });
  1591. }];
  1592. }
  1593. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  1594. {
  1595. CallParticipantViewCell *cell = (CallParticipantViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCallParticipantCellIdentifier forIndexPath:indexPath];
  1596. NCPeerConnection *peerConnection = [_peersInCall objectAtIndex:indexPath.row];
  1597. cell.peerIdentifier = peerConnection.peerIdentifier;
  1598. cell.actionsDelegate = self;
  1599. return cell;
  1600. }
  1601. -(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
  1602. {
  1603. CallParticipantViewCell *participantCell = (CallParticipantViewCell *)cell;
  1604. NCPeerConnection *peerConnection = [_peersInCall objectAtIndex:indexPath.row];
  1605. [self updateParticipantCell:participantCell withPeerConnection:peerConnection];
  1606. }
  1607. #pragma mark - Call Controller delegate
  1608. - (void)callControllerDidJoinCall:(NCCallController *)callController
  1609. {
  1610. [self setCallStateForPeersInCall];
  1611. // Show chat if it was visible before room switch
  1612. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){
  1613. if (self->_showChatAfterRoomSwitch && !self->_chatViewController) {
  1614. self->_showChatAfterRoomSwitch = NO;
  1615. [self toggleChatView];
  1616. }
  1617. });
  1618. }
  1619. - (void)callControllerDidFailedJoiningCall:(NCCallController *)callController statusCode:(NSInteger)statusCode errorReason:(NSString *) errorReason
  1620. {
  1621. dispatch_async(dispatch_get_main_queue(), ^{
  1622. BOOL isAppActive = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
  1623. if (isAppActive) {
  1624. [self presentJoinError:errorReason];
  1625. } else {
  1626. [[CallKitManager sharedInstance] endCall:self->_room.token withStatusCode:statusCode];
  1627. }
  1628. });
  1629. }
  1630. - (void)callControllerDidEndCall:(NCCallController *)callController
  1631. {
  1632. [self finishCall];
  1633. }
  1634. - (void)callController:(NCCallController *)callController peerJoined:(NCPeerConnection *)peer
  1635. {
  1636. // Always add a joined peer, even if the peer doesn't publish any streams (yet)
  1637. [self addPeer:peer];
  1638. }
  1639. - (void)callController:(NCCallController *)callController peerLeft:(NCPeerConnection *)peer
  1640. {
  1641. [self removePeer:peer];
  1642. }
  1643. - (void)callController:(NCCallController *)callController didCreateCameraController:(NCCameraController *)cameraController
  1644. {
  1645. dispatch_async(dispatch_get_main_queue(), ^{
  1646. cameraController.localView = self->_localVideoView;
  1647. });
  1648. }
  1649. - (void)callControllerDidDrawFirstLocalFrame:(NCCallController *)callController
  1650. {
  1651. [_callController getVideoEnabledStateWithCompletionBlock:^(BOOL isEnabled) {
  1652. dispatch_async(dispatch_get_main_queue(), ^{
  1653. [self setLocalVideoViewHidden:!isEnabled];
  1654. });
  1655. }];
  1656. }
  1657. - (void)callController:(NCCallController *)callController userPermissionsChanged:(NSInteger)permissions
  1658. {
  1659. [self setAudioMuteButtonEnabled:(permissions & NCPermissionCanPublishAudio) && [callController isMicrophoneAccessAvailable]];
  1660. [self setVideoDisableButtonEnabled:((permissions & NCPermissionCanPublishVideo) && [callController isCameraAccessAvailable])];
  1661. }
  1662. - (void)callController:(NCCallController *)callController didCreateLocalAudioTrack:(RTCAudioTrack *)audioTrack
  1663. {
  1664. if (!audioTrack) {
  1665. // No audio track was created, probably because there are no publishing rights or microphone access was denied
  1666. [self setAudioMuteButtonEnabled:NO];
  1667. [self setAudioMuteButtonActive:NO];
  1668. return;
  1669. }
  1670. [self setAudioMuteButtonActive:audioTrack.isEnabled];
  1671. }
  1672. - (void)callController:(NCCallController *)callController didCreateLocalVideoTrack:(RTCVideoTrack *)videoTrack
  1673. {
  1674. if (!videoTrack && !self->_isAudioOnly) {
  1675. // No video track was created, probably because there are no publishing rights or camera access was denied
  1676. [self setVideoDisableButtonEnabled:NO];
  1677. [self setVideoDisableButtonActive:NO];
  1678. _userDisabledVideo = YES;
  1679. return;
  1680. }
  1681. [self setVideoDisableButtonActive:videoTrack.isEnabled];
  1682. // We set _userDisabledVideo = YES so the proximity sensor doesn't enable it.
  1683. if (!videoTrack.isEnabled) {
  1684. _userDisabledVideo = YES;
  1685. }
  1686. }
  1687. - (void)callController:(NCCallController *)callController didAddStream:(RTCMediaStream *)remoteStream ofPeer:(NCPeerConnection *)remotePeer
  1688. {
  1689. [[WebRTCCommon shared] assertQueue];
  1690. dispatch_async(dispatch_get_main_queue(), ^{
  1691. RTCMTLVideoView *renderView = [[RTCMTLVideoView alloc] initWithFrame:CGRectZero];
  1692. [[WebRTCCommon shared] dispatch:^{
  1693. RTCVideoTrack *remoteVideoTrack = [[remotePeer getRemoteStream].videoTracks firstObject];
  1694. renderView.delegate = self;
  1695. [remoteVideoTrack addRenderer:renderView];
  1696. }];
  1697. if ([remotePeer.roomType isEqualToString:kRoomTypeVideo]) {
  1698. [self->_videoRenderersDict setObject:renderView forKey:remotePeer.peerIdentifier];
  1699. NSIndexPath *indexPath = [self indexPathForPeerIdentifier:remotePeer.peerIdentifier];
  1700. if (!indexPath) {
  1701. // This is a new peer, add it
  1702. [self addPeer:remotePeer];
  1703. } else {
  1704. // This peer already exists in the collection view, so we can just update its cell
  1705. BOOL isVideoDisabled = (self->_isAudioOnly || remotePeer.isRemoteVideoDisabled);
  1706. [self updatePeer:remotePeer block:^(CallParticipantViewCell *cell) {
  1707. [cell setVideoView:renderView];
  1708. [cell setVideoDisabled:isVideoDisabled];
  1709. }];
  1710. }
  1711. } else if ([remotePeer.roomType isEqualToString:kRoomTypeScreen]) {
  1712. [self->_screenRenderersDict setObject:renderView forKey:remotePeer.peerId];
  1713. [self->_screenPeersInCall addObject:remotePeer];
  1714. [self showScreenOfPeer:remotePeer];
  1715. [self updatePeer:remotePeer block:^(CallParticipantViewCell *cell) {
  1716. [cell setScreenShared:YES];
  1717. }];
  1718. }
  1719. });
  1720. }
  1721. - (void)callController:(NCCallController *)callController didRemoveStream:(RTCMediaStream *)remoteStream ofPeer:(NCPeerConnection *)remotePeer
  1722. {
  1723. }
  1724. - (void)callController:(NCCallController *)callController iceStatusChanged:(RTCIceConnectionState)state ofPeer:(NCPeerConnection *)peer
  1725. {
  1726. if (state == RTCIceConnectionStateClosed) {
  1727. dispatch_async(dispatch_get_main_queue(), ^{
  1728. if ([peer.roomType isEqualToString:kRoomTypeVideo]) {
  1729. [self removePeer:peer];
  1730. } else if ([peer.roomType isEqualToString:kRoomTypeScreen]) {
  1731. [self removeScreensharingOfPeer:peer];
  1732. }
  1733. });
  1734. } else if ([peer.roomType isEqualToString:kRoomTypeVideo]) {
  1735. [self updatePeer:peer block:^(CallParticipantViewCell *cell) {
  1736. [cell setConnectionState:state];
  1737. }];
  1738. }
  1739. }
  1740. - (void)callController:(NCCallController *)callController didAddDataChannel:(RTCDataChannel *)dataChannel
  1741. {
  1742. }
  1743. - (void)callController:(NCCallController *)callController didReceiveDataChannelMessage:(NSString *)message fromPeer:(NCPeerConnection *)peer
  1744. {
  1745. if ([message isEqualToString:@"audioOn"] || [message isEqualToString:@"audioOff"]) {
  1746. [self updatePeer:peer block:^(CallParticipantViewCell *cell) {
  1747. [cell setAudioDisabled:peer.isRemoteAudioDisabled];
  1748. }];
  1749. } else if ([message isEqualToString:@"videoOn"] || [message isEqualToString:@"videoOff"]) {
  1750. if (!_isAudioOnly) {
  1751. [self updatePeer:peer block:^(CallParticipantViewCell *cell) {
  1752. [cell setVideoDisabled:peer.isRemoteVideoDisabled];
  1753. }];
  1754. }
  1755. } else if ([message isEqualToString:@"speaking"] || [message isEqualToString:@"stoppedSpeaking"]) {
  1756. if ([_peersInCall count] > 1) {
  1757. [self updatePeer:peer block:^(CallParticipantViewCell *cell) {
  1758. [cell setSpeaking:peer.isPeerSpeaking];
  1759. }];
  1760. }
  1761. } else if ([message isEqualToString:@"raiseHand"]) {
  1762. [self updatePeer:peer block:^(CallParticipantViewCell *cell) {
  1763. [cell setRaiseHand:peer.isHandRaised];
  1764. }];
  1765. }
  1766. }
  1767. - (void)callController:(NCCallController *)callController didReceiveNick:(NSString *)nick fromPeer:(NCPeerConnection *)peer
  1768. {
  1769. [self updatePeer:peer block:^(CallParticipantViewCell *cell) {
  1770. [cell setDisplayName:nick];
  1771. }];
  1772. if ([peer.peerId isEqualToString:_presentedScreenPeerId]) {
  1773. dispatch_async(dispatch_get_main_queue(), ^{
  1774. [self->_screenshareLabel setText:nick];
  1775. });
  1776. }
  1777. }
  1778. - (void)callController:(NCCallController *)callController didReceiveUnshareScreenFromPeer:(NCPeerConnection *)peer
  1779. {
  1780. [self removeScreensharingOfPeer:peer];
  1781. }
  1782. - (void)callController:(NCCallController *)callController didReceiveForceMuteActionForPeerId:(NSString *)peerId
  1783. {
  1784. if ([peerId isEqualToString:callController.signalingSessionId]) {
  1785. [self forceMuteAudio];
  1786. } else {
  1787. NSLog(@"Peer was force muted: %@", peerId);
  1788. }
  1789. }
  1790. - (void)callController:(NCCallController *)callController didReceiveReaction:(NSString *)reaction fromPeer:(NCPeerConnection *)peer
  1791. {
  1792. dispatch_async(dispatch_get_main_queue(), ^{
  1793. if (reaction.length == 0) {
  1794. return;
  1795. }
  1796. NSString *user = peer.peerName;
  1797. if (user.length == 0) {
  1798. user = NSLocalizedString(@"Guest", nil);
  1799. }
  1800. [self addReaction:reaction fromUser:user];
  1801. });
  1802. }
  1803. - (void)callControllerIsReconnectingCall:(NCCallController *)callController
  1804. {
  1805. dispatch_async(dispatch_get_main_queue(), ^{
  1806. // Cancel any pending operations
  1807. _pendingPeerInserts = [[NSMutableArray alloc] init];
  1808. _pendingPeerDeletions = [[NSMutableArray alloc] init];
  1809. _pendingPeerUpdates = [[NSMutableArray alloc] init];
  1810. _peersInCall = [[NSMutableArray alloc] init];
  1811. // Reset a potential queued batch update
  1812. [self->_batchUpdateTimer invalidate];
  1813. self->_batchUpdateTimer = nil;
  1814. // Force the collectionView to reload all data
  1815. [self.collectionView reloadData];
  1816. [self.collectionView.collectionViewLayout invalidateLayout];
  1817. [self.collectionView layoutSubviews];
  1818. [self setCallState:CallStateReconnecting];
  1819. });
  1820. }
  1821. - (void)callControllerWantsToHangUpCall:(NCCallController *)callController
  1822. {
  1823. dispatch_async(dispatch_get_main_queue(), ^{
  1824. [self hangupForAll:NO];
  1825. });
  1826. }
  1827. - (void)callControllerDidChangeRecording:(NCCallController *)callController
  1828. {
  1829. [self adjustTopBar];
  1830. dispatch_async(dispatch_get_main_queue(), ^{
  1831. NSString *notificationText = NSLocalizedString(@"Call recording stopped", nil);
  1832. if (self->_room.callRecording == NCCallRecordingStateVideoStarting || self->_room.callRecording == NCCallRecordingStateAudioStarting) {
  1833. notificationText = NSLocalizedString(@"Call recording is starting", nil);
  1834. } else if (self->_room.callRecording == NCCallRecordingStateVideoRunning || self->_room.callRecording == NCCallRecordingStateAudioRunning) {
  1835. notificationText = NSLocalizedString(@"Call recording started", nil);
  1836. } else if (self->_room.callRecording == NCCallRecordingStateFailed && self->_room.isUserOwnerOrModerator) {
  1837. notificationText = NSLocalizedString(@"Call recording failed. Please contact your administrator", nil);
  1838. }
  1839. [[JDStatusBarNotificationPresenter sharedPresenter] presentWithText:notificationText dismissAfterDelay:7.0 includedStyle:JDStatusBarNotificationIncludedStyleDark];
  1840. });
  1841. }
  1842. - (void)callControllerDidChangeScreenrecording:(NCCallController *)callController
  1843. {
  1844. [self adjustTopBar];
  1845. }
  1846. - (void)callController:(NCCallController *)callController isSwitchingToCall:(NSString *)token withAudioEnabled:(BOOL)audioEnabled andVideoEnabled:(BOOL)videoEnabled
  1847. {
  1848. [self setCallState:CallStateSwitchingToAnotherRoom];
  1849. // Close chat before switching to another room
  1850. if (_chatViewController) {
  1851. _showChatAfterRoomSwitch = YES;
  1852. [self toggleChatView];
  1853. }
  1854. // Connect to new call
  1855. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  1856. [[NCRoomsManager sharedInstance] updateRoom:token withCompletionBlock:^(NSDictionary *roomDict, NSError *error) {
  1857. if (error) {
  1858. NSLog(@"Error getting room to switch");
  1859. return;
  1860. }
  1861. // Prepare rooms manager to switch to another room
  1862. [[NCRoomsManager sharedInstance] prepareSwitchToAnotherRoomFromRoom:self->_room.token withCompletionBlock:^(NSError *error) {
  1863. // Notify callkit about room switch
  1864. [self.delegate callViewController:self wantsToSwitchCallFromCall:self->_room.token toRoom:token];
  1865. // Assign new room as current room
  1866. self->_room = [NCRoom roomWithDictionary:roomDict andAccountId:activeAccount.accountId];
  1867. // Save current audio and video state
  1868. self->_audioDisabledAtStart = !audioEnabled;
  1869. self->_videoDisabledAtStart = !videoEnabled;
  1870. // Forget current call controller
  1871. self->_callController = nil;
  1872. // Join new room
  1873. [[NCRoomsManager sharedInstance] joinRoom:token forCall:YES];
  1874. }];
  1875. }];
  1876. }
  1877. #pragma mark - Screensharing
  1878. - (void)showScreenOfPeer:(NCPeerConnection *)peer
  1879. {
  1880. dispatch_async(dispatch_get_main_queue(), ^{
  1881. RTCMTLVideoView *renderView = [self->_screenRenderersDict objectForKey:peer.peerId];
  1882. [self->_screensharingView replaceContentView:renderView];
  1883. [self->_screensharingView bringSubviewToFront:self->_closeScreensharingButton];
  1884. // The screenPeer does not have a name associated to it, try to get the nonScreenPeer
  1885. NCPeerConnection *nonScreenPeer = [self peerConnectionForPeerId:peer.peerId];
  1886. NSString *peerDisplayName = nonScreenPeer.peerName;
  1887. if (!peerDisplayName || [peerDisplayName isKindOfClass:[NSNull class]] || [peerDisplayName isEqualToString:@""]) {
  1888. peerDisplayName = NSLocalizedString(@"Guest", nil);
  1889. }
  1890. self->_presentedScreenPeerId = peer.peerId;
  1891. [self->_screenshareLabel setText:peerDisplayName];
  1892. [self->_screensharingView bringSubviewToFront:self->_screenshareLabelContainer];
  1893. [UIView transitionWithView:self->_screensharingView duration:0.4
  1894. options:UIViewAnimationOptionTransitionCrossDissolve
  1895. animations:^{self->_screensharingView.hidden = NO;}
  1896. completion:nil];
  1897. });
  1898. // Enable/Disable detailed view with tap gesture
  1899. // in voice only call when screensharing is enabled
  1900. if (_isAudioOnly) {
  1901. [self addTapGestureForDetailedView];
  1902. [self showDetailedViewWithTimer];
  1903. }
  1904. }
  1905. - (void)removeScreensharingOfPeer:(NCPeerConnection *)peer
  1906. {
  1907. dispatch_async(dispatch_get_main_queue(), ^{
  1908. RTCMTLVideoView *screenRenderer = [self->_screenRenderersDict objectForKey:peer.peerId];
  1909. NCPeerConnection *screenPeerConnection = [self screenPeerConnectionForPeerId:peer.peerId];
  1910. self->_presentedScreenPeerId = nil;
  1911. [self->_screenRenderersDict removeObjectForKey:peer.peerId];
  1912. [self updatePeer:screenPeerConnection block:^(CallParticipantViewCell *cell) {
  1913. [cell setScreenShared:NO];
  1914. }];
  1915. if (self->_screensharingView.contentView == screenRenderer) {
  1916. [self closeScreensharingButtonPressed:self];
  1917. }
  1918. [[WebRTCCommon shared] dispatch:^{
  1919. [[[screenPeerConnection getRemoteStream].videoTracks firstObject] removeRenderer:screenRenderer];
  1920. }];
  1921. [_screenPeersInCall removeObject:screenPeerConnection];
  1922. });
  1923. }
  1924. - (IBAction)closeScreensharingButtonPressed:(id)sender
  1925. {
  1926. dispatch_async(dispatch_get_main_queue(), ^{
  1927. [UIView transitionWithView:self->_screensharingView duration:0.4
  1928. options:UIViewAnimationOptionTransitionCrossDissolve
  1929. animations:^{self->_screensharingView.hidden = YES;}
  1930. completion:nil];
  1931. });
  1932. // Back to normal voice only UI
  1933. if (_isAudioOnly) {
  1934. [self invalidateDetailedViewTimer];
  1935. [self showDetailedView];
  1936. [self removeTapGestureForDetailedView];
  1937. }
  1938. }
  1939. #pragma mark - GestureDelegate
  1940. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  1941. {
  1942. return YES;
  1943. }
  1944. #pragma mark - RTCVideoViewDelegate
  1945. - (void)videoView:(RTCMTLVideoView*)videoView didChangeVideoSize:(CGSize)size
  1946. {
  1947. dispatch_async(dispatch_get_main_queue(), ^{
  1948. for (RTCMTLVideoView *rendererView in [self->_videoRenderersDict allValues]) {
  1949. if ([videoView isEqual:rendererView]) {
  1950. rendererView.frame = CGRectMake(0, 0, size.width, size.height);
  1951. NSArray *keys = [self->_videoRenderersDict allKeysForObject:videoView];
  1952. if (keys.count) {
  1953. NSIndexPath *indexPath = [self indexPathForPeerIdentifier:keys[0]];
  1954. if (indexPath) {
  1955. CallParticipantViewCell *participantCell = (CallParticipantViewCell *) [self.collectionView cellForItemAtIndexPath:indexPath];
  1956. [participantCell setRemoteVideoSize:size];
  1957. }
  1958. }
  1959. }
  1960. }
  1961. for (RTCMTLVideoView *rendererView in [self->_screenRenderersDict allValues]) {
  1962. if ([videoView isEqual:rendererView]) {
  1963. rendererView.frame = CGRectMake(0, 0, size.width, size.height);
  1964. if ([self.screensharingView.contentView isEqual:rendererView]) {
  1965. self->_screensharingSize = rendererView.frame.size;
  1966. [self->_screensharingView setContentViewSize:rendererView.frame.size];
  1967. [self->_screensharingView resizeContentView];
  1968. }
  1969. }
  1970. }
  1971. });
  1972. }
  1973. #pragma mark - Cell updates
  1974. - (NSIndexPath *)indexPathForPeerIdentifier:(NSString *)peerIdentifier
  1975. {
  1976. NSIndexPath *indexPath = nil;
  1977. for (int i = 0; i < _peersInCall.count; i ++) {
  1978. NCPeerConnection *peer = [_peersInCall objectAtIndex:i];
  1979. if ([peer.peerIdentifier isEqualToString:peerIdentifier]) {
  1980. indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  1981. }
  1982. }
  1983. return indexPath;
  1984. }
  1985. - (NSIndexPath *)indexPathForPeerId:(NSString *)peerId
  1986. {
  1987. NSIndexPath *indexPath = nil;
  1988. for (int i = 0; i < _peersInCall.count; i ++) {
  1989. NCPeerConnection *peer = [_peersInCall objectAtIndex:i];
  1990. if ([peer.peerId isEqualToString:peerId]) {
  1991. indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  1992. }
  1993. }
  1994. return indexPath;
  1995. }
  1996. - (void)updatePeer:(NCPeerConnection *)peer block:(UpdateCallParticipantViewCellBlock)block
  1997. {
  1998. dispatch_async(dispatch_get_main_queue(), ^{
  1999. NSIndexPath *indexPath = [self indexPathForPeerId:peer.peerId];
  2000. if (indexPath) {
  2001. CallParticipantViewCell *cell = (id)[self.collectionView cellForItemAtIndexPath:indexPath];
  2002. block(cell);
  2003. } else {
  2004. // The participant might not be added at this point -> delay the update
  2005. PendingCellUpdate *pendingUpdate = [[PendingCellUpdate alloc] init];
  2006. pendingUpdate.peer = peer;
  2007. pendingUpdate.block = block;
  2008. [self->_pendingPeerUpdates addObject:pendingUpdate];
  2009. }
  2010. });
  2011. }
  2012. - (NCPeerConnection *)peerConnectionForPeerIdentifier:(NSString *)peerIdentifier {
  2013. for (NCPeerConnection *peerConnection in self->_peersInCall) {
  2014. if ([peerConnection.peerIdentifier isEqualToString:peerIdentifier]) {
  2015. return peerConnection;
  2016. }
  2017. }
  2018. for (NCPeerConnection *peerConnection in self->_screenPeersInCall) {
  2019. if ([peerConnection.peerIdentifier isEqualToString:peerIdentifier]) {
  2020. return peerConnection;
  2021. }
  2022. }
  2023. return nil;
  2024. }
  2025. - (NCPeerConnection *)peerConnectionForPeerId:(NSString *)peerId {
  2026. for (NCPeerConnection *peerConnection in self->_peersInCall) {
  2027. if ([peerConnection.peerId isEqualToString:peerId]) {
  2028. return peerConnection;
  2029. }
  2030. }
  2031. return nil;
  2032. }
  2033. - (NCPeerConnection *)screenPeerConnectionForPeerId:(NSString *)peerId {
  2034. for (NCPeerConnection *peerConnection in self->_screenPeersInCall) {
  2035. if ([peerConnection.peerId isEqualToString:peerId]) {
  2036. return peerConnection;
  2037. }
  2038. }
  2039. return nil;
  2040. }
  2041. - (void)showPeersInfo
  2042. {
  2043. dispatch_async(dispatch_get_main_queue(), ^{
  2044. NSArray *visibleCells = [self->_collectionView visibleCells];
  2045. for (CallParticipantViewCell *cell in visibleCells) {
  2046. [UIView animateWithDuration:0.3f animations:^{
  2047. [cell.peerNameLabel setAlpha:1.0f];
  2048. [cell.audioOffIndicator setAlpha:1.0f];
  2049. [cell layoutIfNeeded];
  2050. }];
  2051. }
  2052. });
  2053. }
  2054. - (void)hidePeersInfo
  2055. {
  2056. dispatch_async(dispatch_get_main_queue(), ^{
  2057. NSArray *visibleCells = [self->_collectionView visibleCells];
  2058. for (CallParticipantViewCell *cell in visibleCells) {
  2059. [UIView animateWithDuration:0.3f animations:^{
  2060. // Don't hide raise hand indicator, that should always be visible
  2061. [cell.peerNameLabel setAlpha:0.0f];
  2062. [cell.audioOffIndicator setAlpha:0.0f];
  2063. [cell layoutIfNeeded];
  2064. }];
  2065. }
  2066. });
  2067. }
  2068. - (void)addPeer:(NCPeerConnection *)peer
  2069. {
  2070. dispatch_async(dispatch_get_main_queue(), ^{
  2071. if (self->_peersInCall.count == 0) {
  2072. // Don't delay adding the first peer
  2073. [self->_peersInCall addObject:peer];
  2074. NSIndexPath *insertionIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  2075. [self.collectionView insertItemsAtIndexPaths:@[insertionIndexPath]];
  2076. } else {
  2077. // Delay updating the collection view a bit to allow batch updating
  2078. [self->_pendingPeerInserts addObject:peer];
  2079. [self scheduleBatchCollectionViewUpdate];
  2080. }
  2081. });
  2082. }
  2083. - (void)removePeer:(NCPeerConnection *)peer
  2084. {
  2085. dispatch_async(dispatch_get_main_queue(), ^{
  2086. if ([self->_pendingPeerInserts containsObject:peer]) {
  2087. // The peer is a pending insert, but was removed before the batch update
  2088. // In this case we can just remove the pending insert
  2089. [self->_pendingPeerInserts removeObject:peer];
  2090. } else {
  2091. [self->_pendingPeerDeletions addObject:peer];
  2092. [self scheduleBatchCollectionViewUpdate];
  2093. }
  2094. });
  2095. }
  2096. - (void)scheduleBatchCollectionViewUpdate
  2097. {
  2098. // Make sure to call this only from the main queue
  2099. if (self->_batchUpdateTimer == nil) {
  2100. self->_batchUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(batchCollectionViewUpdate) userInfo:nil repeats:NO];
  2101. }
  2102. }
  2103. - (void)batchCollectionViewUpdate
  2104. {
  2105. self->_batchUpdateTimer = nil;
  2106. if (_pendingPeerInserts.count == 0 && _pendingPeerDeletions.count == 0) {
  2107. return;
  2108. }
  2109. [_collectionView performBatchUpdates:^{
  2110. // Perform deletes before inserts according to apples docs
  2111. NSMutableArray *indexPathsToDelete = [[NSMutableArray alloc] init];
  2112. // Determine all indexPaths we want to delete and remove the renderers
  2113. for (NCPeerConnection *peer in _pendingPeerDeletions) {
  2114. // Video renderers
  2115. RTCMTLVideoView *videoRenderer = [self->_videoRenderersDict objectForKey:peer.peerIdentifier];
  2116. [self->_videoRenderersDict removeObjectForKey:peer.peerIdentifier];
  2117. [[WebRTCCommon shared] dispatch:^{
  2118. [[[peer getRemoteStream].videoTracks firstObject] removeRenderer:videoRenderer];
  2119. }];
  2120. NSIndexPath *indexPath = [self indexPathForPeerIdentifier:peer.peerIdentifier];
  2121. // Make sure we remove every index path only once
  2122. if (indexPath && ![indexPathsToDelete containsObject:indexPath]) {
  2123. [indexPathsToDelete addObject:indexPath];
  2124. }
  2125. }
  2126. // Deletes should be done in descending order
  2127. NSSortDescriptor *rowSortDescending = [[NSSortDescriptor alloc] initWithKey:@"row" ascending:NO];
  2128. NSArray *indexPathsToDeleteSorted = [indexPathsToDelete sortedArrayUsingDescriptors:@[rowSortDescending]];
  2129. for (NSIndexPath *indexPath in indexPathsToDeleteSorted) {
  2130. [self->_peersInCall removeObjectAtIndex:indexPath.row];
  2131. [_collectionView deleteItemsAtIndexPaths:@[indexPath]];
  2132. }
  2133. // Add all new peers
  2134. for (NCPeerConnection *peer in _pendingPeerInserts) {
  2135. NSIndexPath *indexPath = [self indexPathForPeerIdentifier:peer.peerIdentifier];
  2136. if (!indexPath) {
  2137. [self->_peersInCall addObject:peer];
  2138. NSIndexPath *insertionIndexPath = [NSIndexPath indexPathForRow:self->_peersInCall.count - 1 inSection:0];
  2139. [self.collectionView insertItemsAtIndexPaths:@[insertionIndexPath]];
  2140. }
  2141. }
  2142. // Process pending updates
  2143. for (PendingCellUpdate *pendingUpdate in _pendingPeerUpdates) {
  2144. [self updatePeer:pendingUpdate.peer block:pendingUpdate.block];
  2145. }
  2146. _pendingPeerInserts = [[NSMutableArray alloc] init];
  2147. _pendingPeerDeletions = [[NSMutableArray alloc] init];
  2148. _pendingPeerUpdates = [[NSMutableArray alloc] init];
  2149. } completion:^(BOOL finished) {
  2150. }];
  2151. }
  2152. #pragma mark - NCChatTitleViewDelegate
  2153. - (void)chatTitleViewTapped:(NCChatTitleView *)titleView
  2154. {
  2155. RoomInfoTableViewController *roomInfoVC = [[RoomInfoTableViewController alloc] initForRoom:_room];
  2156. roomInfoVC.hideDestructiveActions = YES;
  2157. roomInfoVC.modalPresentationStyle = UIModalPresentationPageSheet;
  2158. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:roomInfoVC];
  2159. [self presentViewController:navController animated:YES completion:nil];
  2160. }
  2161. @end