VoIPCallManager.m 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2017-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import <AVFoundation/AVFoundation.h>
  21. #import <AVKit/AVKit.h>
  22. #import <UIKit/UIKit.h>
  23. #import <WebRTC/WebRTC.h>
  24. #import <WebRTC/RTCIceServer.h>
  25. #import "VoIPCallManager.h"
  26. #import "VoIPSender.h"
  27. #include <arpa/inet.h>
  28. #import "VoIPCallIceCandidatesMessage.h"
  29. #import "BundleUtil.h"
  30. #import "CallViewController.h"
  31. #import "CallManager.h"
  32. #import "MainTabBarController.h"
  33. #import "AppDelegate.h"
  34. #import "UserSettings.h"
  35. #import "EntityManager.h"
  36. #import "DateFormatter.h"
  37. #import "NSString+Hex.h"
  38. #import "AppGroup.h"
  39. #import "ServerConnector.h"
  40. #import "DateFormatter.h"
  41. #import "VoIPHelper.h"
  42. #import "NotificationManager.h"
  43. #import "PushSetting.h"
  44. #import "ValidationLogger.h"
  45. #import "Threema-Swift.h"
  46. #define kIncomingCallTimeout 60.0
  47. #define kLogStatsIntervalConnecting 2.0
  48. #define kLogStatsIntervalConnected 30.0
  49. @interface VoIPCallManager () <RTCPeerConnectionDelegate, AVAudioPlayerDelegate, RTCAudioSessionDelegate>
  50. @property (nonatomic, strong) RTCPeerConnectionFactory *factory;
  51. @property (nonatomic, strong) RTCPeerConnection *connection;
  52. @property (nonatomic, strong) Contact *contact;
  53. @property (nonatomic, strong) EntityManager *entityManager;
  54. @property (nonatomic, strong) NSMutableDictionary *bufferReceivedAddIceCandidates;
  55. @property (nonatomic, strong) NSMutableDictionary *bufferReceivedRemoveIceCandidates;
  56. @property (nonatomic, assign) BOOL isMuteEnabled;
  57. @property (nonatomic, strong) RTCAudioTrack *defaultAudioTrack;
  58. @property (nonatomic, strong) NSMutableArray *iceCandidates;
  59. @property (nonatomic, strong) NSMutableArray *tmpIceCandidates;
  60. @property (nonatomic) BOOL isCopyIceCandidates;
  61. @property (nonatomic, strong) NSTimer *incomingCallTimer;
  62. @property (nonatomic, strong) NSTimer *iceCandidatesTimer;
  63. @property (strong, nonatomic) AVAudioPlayer *callPlayer;
  64. @property (strong, nonatomic) AVAudioPlayer *hangupPlayer;
  65. @property (strong, nonatomic) AVAudioPlayer *pickupPlayer;
  66. @property (strong, nonatomic) AVAudioPlayer *ringTonePlayer;
  67. @property (strong, nonatomic) AVAudioPlayer *problemPlayer;
  68. @property (strong, nonatomic) AVAudioPlayer *rejectedPlayer;
  69. @property (strong, nonatomic) NSTimer *durationTimer;
  70. @property (strong, nonatomic) NSTimer *reconnectTimer;
  71. @property (strong, nonatomic) NSTimer *statsTimer;
  72. @property (nonatomic) BOOL isSpeakerActive;
  73. @property (nonatomic) BOOL changedToWebRTCAudio;
  74. @end
  75. @implementation VoIPCallManager
  76. + (VoIPCallManager*)sharedVoIPCallManager {
  77. static VoIPCallManager *instance;
  78. @synchronized (self) {
  79. if (!instance)
  80. instance = [[VoIPCallManager alloc] init];
  81. }
  82. return instance;
  83. }
  84. - (id)init {
  85. self = [super init];
  86. if (self) {
  87. _factory = [RTCPeerConnectionFactory new];
  88. _bufferReceivedAddIceCandidates = [NSMutableDictionary new];
  89. _bufferReceivedRemoveIceCandidates = [NSMutableDictionary new];
  90. _isMuteEnabled = NO;
  91. _state = VoIPCallManagerStateIdle;
  92. _iceCandidates = [NSMutableArray new];
  93. _tmpIceCandidates = [NSMutableArray new];
  94. _isCopyIceCandidates = NO;
  95. _entityManager = [[EntityManager alloc] init];
  96. _isSpeakerActive = NO;
  97. _changedToWebRTCAudio = NO;
  98. _callAlreadyEnded = NO;
  99. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
  100. [[RTCAudioSession sharedInstance] addDelegate:self];
  101. }
  102. return self;
  103. }
  104. #pragma mark - private functions
  105. - (RTCConfiguration *)defaultRTCConfiguration {
  106. RTCConfiguration *configuration = [[RTCConfiguration alloc] init];
  107. RTCIceServer *servers = [[RTCIceServer alloc] initWithURLStrings:@[@"turn:stun-voip.threema.ch:3478", @"turn:stun-voip.threema.ch:443", @"turn:stun-voip.threema.ch:53", @"turn:turn-voip.threema.ch:3478", @"turn:turn-voip.threema.ch:443", @"turn:turn-voip.threema.ch:53"] username:@"threema-voip-ios" credential:@"ZdDbP1PF1vpAnqWgHXNSag" tlsCertPolicy:RTCTlsCertPolicySecure];
  108. configuration.iceServers = @[servers];
  109. if (_contact.verificationLevel == kVerificationLevelUnverified || [UserSettings sharedUserSettings].alwaysRelayCalls) {
  110. configuration.iceTransportPolicy = RTCIceTransportPolicyRelay;
  111. }
  112. configuration.bundlePolicy = RTCBundlePolicyMaxBundle;
  113. configuration.rtcpMuxPolicy = RTCRtcpMuxPolicyRequire;
  114. configuration.tcpCandidatePolicy = RTCTcpCandidatePolicyDisabled;
  115. configuration.continualGatheringPolicy = RTCContinualGatheringPolicyGatherContinually;
  116. return configuration;
  117. }
  118. - (RTCMediaConstraints *)defaultPeerConnectionConstraints {
  119. NSDictionary *optionalConstraints = @{@"DtlsSrtpKeyAgreement": @"true"};
  120. RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:optionalConstraints];
  121. return constraints;
  122. }
  123. - (RTCMediaConstraints *)defaultOfferConstraints {
  124. NSDictionary *mandatoryConstraints = @{@"OfferToReceiveAudio": @"true", @"OfferToReceiveVideo": @"false"};
  125. RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:nil];
  126. return constraints;
  127. }
  128. - (RTCMediaConstraints *)defaultAnswerConstraints {
  129. NSDictionary *mandatoryConstraints = @{@"OfferToReceiveAudio": @"true", @"OfferToReceiveVideo": @"false"};
  130. RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:nil];
  131. return constraints;
  132. }
  133. - (RTCMediaConstraints *)defaultAudioConstraints {
  134. RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:nil];
  135. return constraints;
  136. }
  137. - (RTCMediaStream *)createLocalMediaStreamWithFactory:(RTCPeerConnectionFactory *)factory {
  138. RTCAudioSource *source = [factory audioSourceWithConstraints:[self defaultAudioConstraints]];
  139. RTCMediaStream *localStream = [factory mediaStreamWithStreamId:@"AMACALL"];
  140. [localStream addAudioTrack:[factory audioTrackWithSource:source trackId:@"AMACALLa0"]];
  141. return localStream;
  142. }
  143. + (BOOL)isIPv6Address:(NSString *)ip {
  144. const char *utf8 = [ip UTF8String];
  145. // Check valid IPv4.
  146. struct in_addr dst;
  147. int success = inet_pton(AF_INET, utf8, &(dst.s_addr));
  148. if (success != 1) {
  149. // Check valid IPv6.
  150. struct in6_addr dst6;
  151. return inet_pton(AF_INET6, utf8, &dst6);
  152. }
  153. return NO;
  154. }
  155. - (BOOL)shouldAddCandidate:(RTCIceCandidate *)candidate {
  156. BOOL addCandidate = NO;
  157. if (![UserSettings sharedUserSettings].enableIPv6) {
  158. NSArray *sdpSplit = [candidate.sdp componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
  159. if (sdpSplit.count >= 5) {
  160. if ([sdpSplit[4] rangeOfString:@"."].location == NSNotFound && [sdpSplit[4] rangeOfString:@":"].location == NSNotFound) {
  161. addCandidate = YES;
  162. } else {
  163. if (![VoIPCallManager isIPv6Address:sdpSplit[4]]) {
  164. addCandidate = YES;
  165. }
  166. }
  167. } else {
  168. addCandidate = YES;
  169. }
  170. } else {
  171. addCandidate = YES;
  172. }
  173. return addCandidate;
  174. }
  175. - (void)updateCallDurationTime {
  176. _callDurationTime = _callDurationTime + 1;
  177. if (_state != VoIPCallManagerStateReconnecting) {
  178. _callTimeString = [DateFormatter timeFormatted:_callDurationTime];
  179. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackgroundTimeChanged object:[NSNumber numberWithInt:_callDurationTime]];
  180. } else {
  181. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackgroundTimeChanged object:nil];
  182. }
  183. }
  184. - (void)setupCallTone {
  185. NSString *soundFilePath = [BundleUtil pathForResource:@"ringing-tone-ch-fade" ofType:@"mp3"];
  186. NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
  187. _callPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
  188. _callPlayer.numberOfLoops = -1;
  189. [_callPlayer prepareToPlay];
  190. }
  191. - (void)setupHangupTone {
  192. NSString *soundFilePath = [BundleUtil pathForResource:@"threema_hangup" ofType:@"mp3"];
  193. NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
  194. _hangupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
  195. _hangupPlayer.numberOfLoops = 1;
  196. _hangupPlayer.delegate = self;
  197. [_hangupPlayer prepareToPlay];
  198. }
  199. - (void)setupPickupTone {
  200. NSString *soundFilePath = [BundleUtil pathForResource:@"threema_pickup" ofType:@"mp3"];
  201. NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
  202. _pickupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
  203. _pickupPlayer.numberOfLoops = 1;
  204. [_pickupPlayer prepareToPlay];
  205. }
  206. - (void)setupRingTone {
  207. NSString *voIPSound = [UserSettings sharedUserSettings].voIPSound;
  208. NSString *soundFilePath;
  209. if (![voIPSound isEqualToString:@"default"]) {
  210. soundFilePath = [BundleUtil pathForResource:[UserSettings sharedUserSettings].voIPSound ofType:@"mp3"];
  211. } else {
  212. soundFilePath = [BundleUtil pathForResource:@"threema_best" ofType:@"mp3"];
  213. }
  214. NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
  215. _ringTonePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
  216. _ringTonePlayer.numberOfLoops = -1;
  217. [_ringTonePlayer prepareToPlay];
  218. }
  219. - (void)setupProblemTone {
  220. NSString *soundFilePath = [BundleUtil pathForResource:@"threema_problem" ofType:@"mp3"];
  221. NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
  222. _problemPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
  223. _problemPlayer.numberOfLoops = -1;
  224. [_problemPlayer prepareToPlay];
  225. }
  226. - (void)setupRejectedTone {
  227. NSString *soundFilePath = [BundleUtil pathForResource:@"busy-4x" ofType:@"mp3"];
  228. NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
  229. _rejectedPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
  230. _rejectedPlayer.numberOfLoops = -1;
  231. [_rejectedPlayer prepareToPlay];
  232. }
  233. - (void)playReconnecting:(NSTimer *)timer {
  234. if (_state == VoIPCallManagerStateReconnecting) {
  235. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
  236. [self playTone:VoIPCallManagerToneProblem];
  237. AVAudioSession *session = [AVAudioSession sharedInstance];
  238. NSArray *outputs = [[session currentRoute] outputs];
  239. BOOL isSpeaker = NO;
  240. for (AVAudioSessionPortDescription *desc in outputs) {
  241. if ([desc.portType isEqualToString:AVAudioSessionPortBuiltInSpeaker]) {
  242. isSpeaker = YES;
  243. }
  244. }
  245. [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
  246. isSpeaker ? [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil] : [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
  247. [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
  248. }
  249. }
  250. - (void)schedulePeriodStatsWithOptions:(VoIPStatsOptions *)options period:(NSTimeInterval)period {
  251. // Reset timer
  252. if (self.statsTimer && [self.statsTimer isValid]) {
  253. [self.statsTimer invalidate];
  254. self.statsTimer = nil;
  255. }
  256. // Create new timer with <period> (but immediately log once)
  257. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  258. [dict setObject:self.connection forKey:@"connection"];
  259. [dict setObject:options forKey:@"options"];
  260. self.statsTimer = [NSTimer scheduledTimerWithTimeInterval:period target:self selector:@selector(logDebugStatsFromTimer:) userInfo:dict repeats:YES];
  261. [self logDebugStats:dict];
  262. [[NSRunLoop mainRunLoop] addTimer:self.statsTimer forMode:NSRunLoopCommonModes];
  263. }
  264. - (void)logDebugStatsFromTimer:(NSTimer *)timer {
  265. NSDictionary *dict = [timer userInfo];
  266. [self logDebugStats:dict];
  267. }
  268. - (void)logDebugStats:(NSDictionary *)dict {
  269. RTCPeerConnection *connection = [dict objectForKey:@"connection"];
  270. VoIPStatsOptions *options = [dict objectForKey:@"options"];
  271. [connection statsForTrack:nil statsOutputLevel:RTCStatsOutputLevelDebug completionHandler:^(NSArray<RTCLegacyStatsReport *> * _Nonnull report) {
  272. VoIPStats *stats = [[VoIPStats alloc] initWithReport:report options:options];
  273. NSString *statsStr = [stats getRepresentation];
  274. [[ValidationLogger sharedValidationLogger] logString:[NSString stringWithFormat:@"Call: Stats\n%@", statsStr]];
  275. // Execute callback (if any)
  276. void(^callback)(void) = [dict objectForKey:@"callback"];
  277. if (callback) {
  278. callback();
  279. }
  280. }];
  281. }
  282. - (NSString *)stringForIceConnectionState:(RTCIceConnectionState)state {
  283. switch (state) {
  284. case RTCIceConnectionStateNew:
  285. return @"new";
  286. case RTCIceConnectionStateChecking:
  287. return @"checking";
  288. case RTCIceConnectionStateConnected:
  289. return @"connected";
  290. case RTCIceConnectionStateCompleted:
  291. return @"completed";
  292. case RTCIceConnectionStateFailed:
  293. return @"failed";
  294. case RTCIceConnectionStateDisconnected:
  295. return @"disconnected";
  296. case RTCIceConnectionStateClosed:
  297. return @"closed";
  298. case RTCIceConnectionStateCount:
  299. return @"count";
  300. }
  301. }
  302. #pragma mark - public functions
  303. - (Contact *)getCallContact {
  304. return _contact;
  305. }
  306. - (void)startVoIPCallWithContact:(Contact *)contact {
  307. _state = VoIPCallManagerStateWaitForRinging;
  308. _changedToWebRTCAudio = NO;
  309. [self setupTones];
  310. dispatch_async(dispatch_get_main_queue(),^{
  311. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
  312. AVAudioSession *session = [AVAudioSession sharedInstance];
  313. [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
  314. [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
  315. });
  316. _contact = contact;
  317. RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
  318. RTCConfiguration *configuration = [self defaultRTCConfiguration];
  319. _connection = [_factory peerConnectionWithConfiguration:configuration constraints:constraints delegate:self];
  320. RTCMediaStream *localStream = [self createLocalMediaStreamWithFactory:_factory];
  321. [_connection addStream:localStream];
  322. [_connection offerForConstraints:constraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
  323. [_connection setLocalDescription:sdp completionHandler:^(NSError * _Nullable error) {
  324. if (!error) {
  325. _isCallInitiator = YES;
  326. [VoIPSender startVoIPCallWithContact:contact sessionDescription:sdp];
  327. }
  328. }];
  329. }];
  330. }
  331. - (void)startRinging {
  332. _state = VoIPCallManagerStateRinging;
  333. [[VoIPCallManager sharedVoIPCallManager] playTone:VoIPCallManagerToneCall];
  334. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
  335. }
  336. - (void)startVoIPCallAnswerWithContact:(Contact *)contact {
  337. _state = VoIPCallManagerStateInitializing;
  338. _changedToWebRTCAudio = NO;
  339. [self setupTones];
  340. dispatch_async(dispatch_get_main_queue(),^{
  341. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
  342. AVAudioSession *session = [AVAudioSession sharedInstance];
  343. [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
  344. [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
  345. });
  346. __weak VoIPCallManager *weakSelf = self;
  347. [[CallManager sharedInstance] callPickedUpFromReceiver];
  348. if (!contact) {
  349. contact = _contact;
  350. }
  351. RTCMediaConstraints *answerConstraints = [self defaultAnswerConstraints];
  352. [_connection answerForConstraints:answerConstraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
  353. if (!error) {
  354. [_connection setLocalDescription:sdp completionHandler:^(NSError * _Nullable error) {
  355. if (!error) {
  356. _isCallInitiator = NO;
  357. VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
  358. message.answer = sdp;
  359. message.action = VoIPCallAnswerMessageActionCall;
  360. message.rejectReason = VoIPCallAnswerMessageRejectReasonUnknown;
  361. [VoIPSender startVoIPCallAnswerWithContact:contact message:message];
  362. if (weakSelf.incomingCallTimer && [weakSelf.incomingCallTimer isValid]){
  363. [weakSelf.incomingCallTimer invalidate];
  364. weakSelf.incomingCallTimer = nil;
  365. }
  366. }
  367. }];
  368. }
  369. }];
  370. }
  371. - (void)setOffer:(RTCSessionDescription *)sdp fromContact:(Contact *)contact {
  372. __weak VoIPCallManager *weakSelf = self;
  373. _state = VoIPCallManagerStateWaitForRinging;
  374. dispatch_async(dispatch_get_main_queue(),^{
  375. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
  376. });
  377. _contact = contact;
  378. RTCMediaConstraints *offerConstraints = [self defaultPeerConnectionConstraints];
  379. RTCConfiguration *configuration = [self defaultRTCConfiguration];
  380. _connection = [_factory peerConnectionWithConfiguration:configuration constraints:offerConstraints delegate:self];
  381. RTCMediaStream *localStream = [self createLocalMediaStreamWithFactory:_factory];
  382. [_connection addStream:localStream];
  383. @synchronized (_bufferReceivedAddIceCandidates) {
  384. [_bufferReceivedAddIceCandidates removeAllObjects];
  385. }
  386. @synchronized (_bufferReceivedRemoveIceCandidates) {
  387. [_bufferReceivedRemoveIceCandidates removeAllObjects];
  388. }
  389. [_connection setRemoteDescription:sdp completionHandler:^(NSError * _Nullable error) {
  390. if (!error) {
  391. NSUUID *uuid = [NSUUID new];
  392. _state = VoIPCallManagerStateRinging;
  393. // add already received candidates
  394. @synchronized (weakSelf.bufferReceivedAddIceCandidates) {
  395. NSMutableArray *addCandidates = [weakSelf.bufferReceivedAddIceCandidates valueForKey:contact.identity];
  396. [addCandidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
  397. [weakSelf.connection addIceCandidate:candidate];
  398. }];
  399. [weakSelf.bufferReceivedAddIceCandidates removeAllObjects];
  400. }
  401. @synchronized (weakSelf.bufferReceivedRemoveIceCandidates) {
  402. NSMutableArray *removeCandidates = [weakSelf.bufferReceivedRemoveIceCandidates valueForKey:contact.identity];
  403. if (removeCandidates) {
  404. [weakSelf.connection removeIceCandidates:removeCandidates];
  405. }
  406. [weakSelf.bufferReceivedRemoveIceCandidates removeAllObjects];
  407. }
  408. dispatch_async(dispatch_get_main_queue(),^{
  409. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:weakSelf.state]];
  410. _incomingCallTimer = [NSTimer scheduledTimerWithTimeInterval:kIncomingCallTimeout target:self selector:@selector(timeoutCall) userInfo:nil repeats:NO];
  411. });
  412. _isCallInitiator = NO;
  413. if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0") && [UserSettings sharedUserSettings].enableCallKit && ![[[NSLocale currentLocale] objectForKey: NSLocaleCountryCode] isEqualToString:@"CN"]) {
  414. [[CallManager sharedInstance] reportIncomingCallForUUID:uuid contact:contact];
  415. } else {
  416. dispatch_async(dispatch_get_main_queue(), ^{
  417. UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
  418. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"CallStoryboard" bundle:nil];
  419. CallViewController *callViewController = (CallViewController *)[storyboard instantiateInitialViewController];
  420. callViewController.contact = [weakSelf contact];
  421. callViewController.alreadyAccepted = NO;
  422. callViewController.isCallInitiator = NO;
  423. callViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
  424. [vc presentViewController:callViewController animated:NO completion:nil];
  425. });
  426. dispatch_async(dispatch_get_main_queue(), ^{
  427. if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
  428. /* We're in the background and have received a message. There will be no push, so we need to generate a local notification */
  429. NSString *cmd;
  430. UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];
  431. notification.categoryIdentifier = @"INCOMCALL";
  432. cmd = @"newcall";
  433. NSString *voIPSound = [UserSettings sharedUserSettings].voIPSound;
  434. if (![voIPSound isEqualToString:@"default"]) {
  435. notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.mp3", [UserSettings sharedUserSettings].voIPSound]];
  436. } else {
  437. notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"threema_best.mp3"]];
  438. }
  439. notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObjectsAndKeys: cmd, @"cmd", contact.displayName, @"from", nil], @"threema", nil];
  440. notification.title = contact.displayName;
  441. notification.body = NSLocalizedString(@"call_incoming_ended", @"");
  442. NSString *notificationIdentifier = contact.identity;
  443. UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:notificationIdentifier content:notification trigger:nil];
  444. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  445. [center addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
  446. }];
  447. }
  448. });
  449. }
  450. [VoIPSender sendVoIPCallRingingMessageToContact:weakSelf.contact];
  451. }
  452. }];
  453. }
  454. - (void)setAnswer:(RTCSessionDescription *)sdp {
  455. __weak VoIPCallManager *weakSelf = self;
  456. [_connection setRemoteDescription:sdp completionHandler:^(NSError * _Nullable error) {
  457. if (error) {
  458. [weakSelf callRejected];
  459. }
  460. }];
  461. }
  462. - (void)removeIceCandidates:(NSArray *)candidates fromIdentity:(NSString *)identity {
  463. if (_connection) {
  464. if ([_contact.identity isEqualToString:identity]) {
  465. [_connection removeIceCandidates:candidates];
  466. }
  467. } else {
  468. @synchronized (_bufferReceivedRemoveIceCandidates) {
  469. NSMutableArray *candidatesArray = [_bufferReceivedRemoveIceCandidates valueForKey:identity];
  470. if (!candidatesArray) {
  471. candidatesArray = [NSMutableArray new];
  472. }
  473. [candidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
  474. [candidatesArray addObject:candidate];
  475. }];
  476. [_bufferReceivedRemoveIceCandidates setObject:candidatesArray forKey:identity];
  477. }
  478. }
  479. }
  480. - (void)addIceCandidates:(NSArray *)candidates fromIdentity:(NSString *)identity{
  481. if (_connection) {
  482. if ([_contact.identity isEqualToString:identity]) {
  483. [candidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
  484. if ([self shouldAddCandidate:candidate]) {
  485. [_connection addIceCandidate:candidate];
  486. }
  487. }];
  488. }
  489. } else {
  490. @synchronized (_bufferReceivedAddIceCandidates) {
  491. NSMutableArray *candidatesArray = [_bufferReceivedAddIceCandidates valueForKey:identity];
  492. if (!candidatesArray) {
  493. candidatesArray = [NSMutableArray new];
  494. }
  495. [candidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
  496. if ([self shouldAddCandidate:candidate]) {
  497. [candidatesArray addObject:candidate];
  498. }
  499. }];
  500. [_bufferReceivedAddIceCandidates setObject:candidatesArray forKey:identity];
  501. }
  502. }
  503. }
  504. - (void)hangup {
  505. if (!_callAlreadyEnded) {
  506. _callAlreadyEnded = YES;
  507. [VoIPSender sendVoIPCallHangupToContact:_contact];
  508. if (_state == VoIPCallManagerStateWaitForRinging || _state == VoIPCallManagerStateRinging || _state == VoIPCallManagerStateInitializing || _state == VoIPCallManagerStateCalling || _state == VoIPCallManagerStateReconnecting || _state == VoIPCallManagerStateSystemRejected) {
  509. Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
  510. [_entityManager performSyncBlockAndSafe:^{
  511. SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
  512. systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallEnded];
  513. if (!_callTimeString)
  514. _callTimeString = @"";
  515. NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallTime": _callTimeString, @"CallInitiator": [NSNumber numberWithBool:_isCallInitiator]};
  516. NSError *error;
  517. NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
  518. systemMessage.arg = data;
  519. systemMessage.isOwn = [NSNumber numberWithBool:_isCallInitiator];
  520. systemMessage.conversation = conversation;
  521. conversation.lastMessage = systemMessage;
  522. _callTimeString = @"";
  523. }];
  524. }
  525. if (_incomingCallTimer && [_incomingCallTimer isValid]){
  526. [_incomingCallTimer invalidate];
  527. _incomingCallTimer = nil;
  528. }
  529. [self disconnect: true];
  530. [[CallManager sharedInstance] endCall];
  531. }
  532. }
  533. - (void)hangupOnCompletion:(void(^)(void))onCompletion {
  534. [VoIPSender sendVoIPCallHangupAndWaitToContact:_contact];
  535. if (_state == VoIPCallManagerStateWaitForRinging || _state == VoIPCallManagerStateRinging || _state == VoIPCallManagerStateInitializing || _state == VoIPCallManagerStateCalling || _state == VoIPCallManagerStateReconnecting || _state == VoIPCallManagerStateSystemRejected) {
  536. if (![[VoIPCallManager sharedVoIPCallManager] callAlreadyEnded]) {
  537. [[VoIPCallManager sharedVoIPCallManager] setCallAlreadyEnded:YES];
  538. Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
  539. [_entityManager performSyncBlockAndSafe:^{
  540. SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
  541. systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallEnded];
  542. if (!_callTimeString)
  543. _callTimeString = @"";
  544. NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallTime": _callTimeString, @"CallInitiator": [NSNumber numberWithBool:_isCallInitiator]};
  545. NSError *error;
  546. NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
  547. systemMessage.arg = data;
  548. systemMessage.isOwn = [NSNumber numberWithBool:_isCallInitiator];
  549. systemMessage.conversation = conversation;
  550. conversation.lastMessage = systemMessage;
  551. _callTimeString = @"";
  552. }];
  553. }
  554. }
  555. if (_incomingCallTimer && [_incomingCallTimer isValid]){
  556. [_incomingCallTimer invalidate];
  557. _incomingCallTimer = nil;
  558. }
  559. [self disconnect: true];
  560. [[CallManager sharedInstance] endCall];
  561. onCompletion();
  562. }
  563. - (void)callHangedup {
  564. [self disconnect: true];
  565. [[CallManager sharedInstance] endCall];
  566. }
  567. - (void)callRejected {
  568. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:VoIPCallManagerStateSystemRejected]];
  569. [_reconnectTimer invalidate];
  570. [self setCallTimeString:@""];
  571. [self disconnect: false];
  572. [[CallManager sharedInstance] endCall];
  573. }
  574. - (void)timeoutCall {
  575. __weak VoIPCallManager *weakSelf = self;
  576. RTCMediaConstraints *answerConstraints = [self defaultAnswerConstraints];
  577. [_connection answerForConstraints:answerConstraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
  578. if (!error) {
  579. VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
  580. message.answer = sdp;
  581. message.action = VoIPCallAnswerMessageActionReject;
  582. message.rejectReason = VoIPCallAnswerMessageRejectReasonTimeout;
  583. [VoIPSender startVoIPCallAnswerRejectWithContact:_contact message:message];
  584. }
  585. __block NSString *messageId;
  586. Conversation *conversation = [_entityManager.entityFetcher conversationForIdentity:_contact.identity];
  587. [_entityManager performSyncBlockAndSafe:^{
  588. /* Insert system message to document the missed call */
  589. NSDictionary *argDict;
  590. SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
  591. systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallMissed];
  592. systemMessage.isOwn = [NSNumber numberWithBool:NO];
  593. conversation.unreadMessageCount = [NSNumber numberWithInt:[[conversation unreadMessageCount] intValue] + 1];
  594. argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallInitiator": [NSNumber numberWithBool:[[VoIPCallManager sharedVoIPCallManager] isCallInitiator]]};
  595. NSError *error;
  596. NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
  597. systemMessage.arg = data;
  598. systemMessage.isOwn = [NSNumber numberWithBool:[[VoIPCallManager sharedVoIPCallManager] isCallInitiator]];
  599. systemMessage.conversation = conversation;
  600. conversation.lastMessage = systemMessage;
  601. messageId = [NSString stringWithHexData:systemMessage.id];
  602. }];
  603. dispatch_async(dispatch_get_main_queue(),^{
  604. [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
  605. });
  606. __block UIApplicationState state;
  607. dispatch_async(dispatch_get_main_queue(),^{
  608. state = [UIApplication sharedApplication].applicationState;
  609. });
  610. PushSetting *pushSetting = [PushSetting findPushSettingForConversation:conversation];
  611. BOOL canSendPush = YES;
  612. if (pushSetting != nil) {
  613. canSendPush = [pushSetting canSendPush];
  614. }
  615. if (state == UIApplicationStateBackground && canSendPush) {
  616. UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];
  617. NSString *cmd;
  618. if ([UserSettings sharedUserSettings].pushDecrypt) {
  619. notification.title = _contact.displayName;
  620. notification.body = NSLocalizedString(@"call_missed", nil);
  621. notification.categoryIdentifier = @"CALL";
  622. } else {
  623. notification.body = [NSString stringWithFormat:NSLocalizedString(@"new_message_from_x", nil), _contact.displayName];
  624. notification.categoryIdentifier = @"";
  625. }
  626. cmd = @"newmsg";
  627. if (![[UserSettings sharedUserSettings].pushSound isEqualToString:@"none"]) {
  628. if (pushSetting != nil) {
  629. if (!pushSetting.silent) {
  630. notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.caf", [UserSettings sharedUserSettings].pushSound]];
  631. }
  632. } else {
  633. notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.caf", [UserSettings sharedUserSettings].pushSound]];
  634. }
  635. }
  636. notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObjectsAndKeys: cmd, @"cmd", _contact.displayName, @"from", messageId, @"messageId", nil], @"threema", nil];
  637. NSString *notificationIdentifier = _contact.identity;
  638. UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:notificationIdentifier content:notification trigger:nil];
  639. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  640. [center addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
  641. }];
  642. }
  643. if (weakSelf.incomingCallTimer && [weakSelf.incomingCallTimer isValid]){
  644. [weakSelf.incomingCallTimer invalidate];
  645. weakSelf.incomingCallTimer = nil;
  646. }
  647. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:VoIPCallManagerStateIdle]];
  648. [_reconnectTimer invalidate];
  649. [self setCallTimeString:@""];
  650. [[RTCAudioSession sharedInstance] lockForConfiguration];
  651. NSError *rtcError = nil;
  652. if (![[RTCAudioSession sharedInstance] setActive:NO error:&rtcError]) {
  653. NSLog(@"resume music player failed, error=%@", rtcError);
  654. }
  655. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  656. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  657. if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&rtcError]) {
  658. NSLog(@"resume music player failed, error=%@", rtcError);
  659. }
  660. [self disconnect: true];
  661. [[CallManager sharedInstance] endCall];
  662. }];
  663. }
  664. - (void)rejectCallOnCompletion:(void(^)(void))onCompletion {
  665. __weak VoIPCallManager *weakSelf = self;
  666. RTCMediaConstraints *answerConstraints = [self defaultAnswerConstraints];
  667. [_connection answerForConstraints:answerConstraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
  668. if (!error) {
  669. _isCallInitiator = NO;
  670. VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
  671. message.answer = sdp;
  672. message.action = VoIPCallAnswerMessageActionReject;
  673. message.rejectReason = VoIPCallAnswerMessageRejectReasonReject;
  674. [VoIPSender startVoIPCallAnswerRejectWithContact:_contact message:message];
  675. if (![[VoIPCallManager sharedVoIPCallManager] callAlreadyEnded]) {
  676. [[VoIPCallManager sharedVoIPCallManager] setCallAlreadyEnded:YES];
  677. Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
  678. [_entityManager performSyncBlockAndSafe:^{
  679. SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
  680. systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallRejected];
  681. NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallInitiator": [NSNumber numberWithBool:_isCallInitiator]};
  682. NSError *error;
  683. NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
  684. systemMessage.arg = data;
  685. systemMessage.isOwn = [NSNumber numberWithBool:_isCallInitiator];
  686. systemMessage.read = [NSNumber numberWithBool:YES];
  687. systemMessage.conversation = conversation;
  688. conversation.lastMessage = systemMessage;
  689. }];
  690. }
  691. }
  692. if (weakSelf.incomingCallTimer && [weakSelf.incomingCallTimer isValid]){
  693. [weakSelf.incomingCallTimer invalidate];
  694. weakSelf.incomingCallTimer = nil;
  695. }
  696. [self disconnect: false];
  697. [[CallManager sharedInstance] endCall];
  698. onCompletion();
  699. }];
  700. }
  701. - (void)rejectCallWithBusy:(Contact *)contact onCompletion:(void(^)(void))onCompletion {
  702. VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
  703. message.answer = nil;
  704. message.action = VoIPCallAnswerMessageActionReject;
  705. message.rejectReason = VoIPCallAnswerMessageRejectReasonBusy;
  706. [VoIPSender startVoIPCallAnswerRejectWithContact:contact message:message];
  707. Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
  708. [_entityManager performSyncBlockAndSafe:^{
  709. SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
  710. systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallMissed];
  711. NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallInitiator": [NSNumber numberWithBool:NO]};
  712. NSError *error;
  713. NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
  714. systemMessage.arg = data;
  715. systemMessage.isOwn = [NSNumber numberWithBool:NO];
  716. systemMessage.conversation = conversation;
  717. conversation.lastMessage = systemMessage;
  718. conversation.unreadMessageCount = [NSNumber numberWithInt:[[conversation unreadMessageCount] intValue] + 1];
  719. }];
  720. dispatch_async(dispatch_get_main_queue(),^{
  721. [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
  722. });
  723. onCompletion();
  724. }
  725. - (void)rejectCallWithDisabled:(Contact *)contact onCompletion:(void(^)(void))onCompletion {
  726. _state = VoIPCallManagerStateSystemRejected;
  727. VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
  728. message.answer = nil;
  729. message.action = VoIPCallAnswerMessageActionReject;
  730. message.rejectReason = VoIPCallAnswerMessageRejectReasonDisabled;
  731. [VoIPSender startVoIPCallAnswerRejectWithContact:contact message:message];
  732. dispatch_async(dispatch_get_main_queue(),^{
  733. [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
  734. });
  735. [[CallManager sharedInstance] endCall];
  736. [self disconnect: false];
  737. if (onCompletion != nil) {
  738. onCompletion();
  739. }
  740. }
  741. - (void)disconnect:(BOOL)playSound {
  742. void(^disconnectCallback)(void) = ^{
  743. [_connection close];
  744. _connection = nil;
  745. };
  746. if (playSound) {
  747. [self playTone:VoIPCallManagerToneHangup];
  748. }
  749. [[VoIPHelper shared] setIsCallActiveInBackground:NO];
  750. [[VoIPHelper shared] setContactName:nil];
  751. [[VoIPHelper shared] setLastUpdatedCallDuration:nil];
  752. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackground object:nil];
  753. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackgroundTimeChanged object:nil];
  754. if (_connection) {
  755. dispatch_async(dispatch_get_main_queue(),^{
  756. if (_statsTimer && [_statsTimer isValid]) {
  757. // Invalidate timer
  758. [_statsTimer invalidate];
  759. _statsTimer = nil;
  760. // Hijack the existing dict, override options and set callback
  761. VoIPStatsOptions *options = [[VoIPStatsOptions alloc] init];
  762. options.transport = true;
  763. options.inboundRtp = true;
  764. options.codecs = true;
  765. options.candidatePairsFlag = CandidatePairVariantOVERVIEW_AND_DETAILED;
  766. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  767. self.connection, @"connection",
  768. options, @"options",
  769. disconnectCallback, @"callback",
  770. nil];
  771. // One-shot stats fetch before disconnect
  772. [self logDebugStats:dict];
  773. } else {
  774. disconnectCallback();
  775. }
  776. [_incomingCallTimer invalidate];
  777. _incomingCallTimer = nil;
  778. _contact = nil;
  779. _callAlreadyEnded = NO;
  780. _isMuteEnabled = NO;
  781. [_iceCandidatesTimer invalidate];
  782. _iceCandidatesTimer = nil;
  783. [_callDurationTimer invalidate];
  784. _callDurationTimer = nil;
  785. _callDurationTime = 0;
  786. _callTimeString = @"";
  787. [_iceCandidates removeAllObjects];
  788. _state = VoIPCallManagerStateIdle;
  789. });
  790. }
  791. }
  792. - (void)muteAudioIn {
  793. RTCMediaStream *localStream = _connection.localStreams[0];
  794. self.defaultAudioTrack = localStream.audioTracks[0];
  795. [localStream removeAudioTrack:localStream.audioTracks[0]];
  796. [_connection removeStream:localStream];
  797. [_connection addStream:localStream];
  798. _isMuteEnabled = YES;
  799. }
  800. - (void)unmuteAudioIn {
  801. RTCMediaStream* localStream = _connection.localStreams[0];
  802. [localStream addAudioTrack:self.defaultAudioTrack];
  803. [_connection removeStream:localStream];
  804. [_connection addStream:localStream];
  805. _isMuteEnabled = NO;
  806. }
  807. - (BOOL)isMuteEnabled {
  808. return _isMuteEnabled;
  809. }
  810. - (void)invalidateDurationTimer {
  811. [_durationTimer invalidate];
  812. _durationTimer = nil;
  813. }
  814. - (void)setupTones {
  815. [self setupCallTone];
  816. [self setupHangupTone];
  817. [self setupPickupTone];
  818. [self setupRingTone];
  819. [self setupProblemTone];
  820. [self setupRejectedTone];
  821. }
  822. - (void)playTone:(VoIPCallManagerTone)tone {
  823. switch (tone) {
  824. case VoIPCallManagerToneCall:
  825. [_hangupPlayer stop];
  826. [_pickupPlayer stop];
  827. [_ringTonePlayer stop];
  828. [_problemPlayer stop];
  829. [_rejectedPlayer stop];
  830. [_callPlayer setCurrentTime:0];
  831. [_callPlayer play];
  832. break;
  833. case VoIPCallManagerToneHangup:
  834. [_callPlayer stop];
  835. [_pickupPlayer stop];
  836. [_ringTonePlayer stop];
  837. [_problemPlayer stop];
  838. [_rejectedPlayer stop];
  839. [_hangupPlayer play];
  840. break;
  841. case VoIPCallManagerTonePickup:
  842. [_callPlayer stop];
  843. [_hangupPlayer stop];
  844. [_ringTonePlayer stop];
  845. [_problemPlayer stop];
  846. [_rejectedPlayer stop];
  847. [_pickupPlayer play];
  848. break;
  849. case VoIPCallManagerToneRing:
  850. [_callPlayer stop];
  851. [_hangupPlayer stop];
  852. [_pickupPlayer stop];
  853. [_problemPlayer stop];
  854. [_rejectedPlayer stop];
  855. [_ringTonePlayer setCurrentTime:0];
  856. [_ringTonePlayer play];
  857. break;
  858. case VoIPCallManagerToneProblem:
  859. [_callPlayer stop];
  860. [_hangupPlayer stop];
  861. [_pickupPlayer stop];
  862. [_ringTonePlayer stop];
  863. [_rejectedPlayer stop];
  864. [_problemPlayer setCurrentTime:0];
  865. [_problemPlayer play];
  866. break;
  867. case VoIPCallManagerToneRejected:
  868. [_callPlayer stop];
  869. [_hangupPlayer stop];
  870. [_pickupPlayer stop];
  871. [_ringTonePlayer stop];
  872. [_problemPlayer stop];
  873. [_rejectedPlayer setCurrentTime:0];
  874. [_rejectedPlayer play];
  875. default:
  876. break;
  877. }
  878. }
  879. - (void)stopAllTones {
  880. [_callPlayer stop];
  881. [_hangupPlayer stop];
  882. [_pickupPlayer stop];
  883. [_ringTonePlayer stop];
  884. [_problemPlayer stop];
  885. [_rejectedPlayer stop];
  886. [[RTCAudioSession sharedInstance] lockForConfiguration];
  887. NSError *error = nil;
  888. if (![[RTCAudioSession sharedInstance] setActive:NO error:&error]) {
  889. NSLog(@"resume music player failed, error=%@", error);
  890. }
  891. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  892. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  893. if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]) {
  894. NSLog(@"resume music player failed, error=%@", error);
  895. }
  896. }
  897. - (void)activateRTCAudio {
  898. AVAudioSessionRouteDescription *currentRoute = [[RTCAudioSession sharedInstance] currentRoute];
  899. AVAudioSessionPortDescription *portDesc = [[currentRoute outputs] firstObject];
  900. if ([portDesc.portType isEqualToString:@"Speaker"]) {
  901. NSError *error;
  902. [[RTCAudioSession sharedInstance] lockForConfiguration];
  903. [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
  904. [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
  905. [[RTCAudioSession sharedInstance] setActive:YES error:&error];
  906. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  907. [[RTCAudioSession sharedInstance] lockForConfiguration];
  908. [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
  909. [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
  910. [[RTCAudioSession sharedInstance] setActive:YES error:&error];
  911. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  912. } else {
  913. NSError *error;
  914. [[RTCAudioSession sharedInstance] lockForConfiguration];
  915. [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
  916. [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
  917. [[RTCAudioSession sharedInstance] setActive:YES error:&error];
  918. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  919. [[RTCAudioSession sharedInstance] lockForConfiguration];
  920. [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
  921. [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
  922. [[RTCAudioSession sharedInstance] setActive:YES error:&error];
  923. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  924. }
  925. }
  926. #pragma mark - RTCPeerConnectionDelegate
  927. - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeSignalingState:(RTCSignalingState)stateChanged {
  928. }
  929. /** Called when media is received on a new stream from remote peer. */
  930. - (void)peerConnection:(RTCPeerConnection *)peerConnection didAddStream:(RTCMediaStream *)stream {
  931. // ignore
  932. }
  933. /** Called when a remote peer closes a stream. */
  934. - (void)peerConnection:(RTCPeerConnection *)peerConnection didRemoveStream:(RTCMediaStream *)stream {
  935. // ignore
  936. }
  937. /** Called when negotiation is needed, for example ICE has restarted. */
  938. - (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
  939. // ignore
  940. }
  941. /** Called any time the IceConnectionState changes. */
  942. - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeIceConnectionState:(RTCIceConnectionState)newState {
  943. BOOL wasInitializing = _state == VoIPCallManagerStateInitializing;
  944. NSString *strState = [self stringForIceConnectionState:newState];
  945. [[ValidationLogger sharedValidationLogger] logString:[NSString stringWithFormat: @"Call: ICE connection state -> %@", strState]];
  946. if (newState == RTCIceConnectionStateChecking) {
  947. _state = VoIPCallManagerStateInitializing;
  948. [self playTone:VoIPCallManagerTonePickup];
  949. AVAudioSession *session = [AVAudioSession sharedInstance];
  950. [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
  951. [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
  952. dispatch_async(dispatch_get_main_queue(),^{
  953. // Schedule 'connecting' stats timer
  954. VoIPStatsOptions *options = [[VoIPStatsOptions alloc] init];
  955. options.transport = true;
  956. options.inboundRtp = true;
  957. options.codecs = true;
  958. options.candidatePairsFlag = CandidatePairVariantOVERVIEW_AND_DETAILED;
  959. [self schedulePeriodStatsWithOptions:options period:kLogStatsIntervalConnecting];
  960. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStartDebugMode object:_connection];
  961. });
  962. } else if (newState == RTCIceConnectionStateConnected) {
  963. if (_state != VoIPCallManagerStateReconnecting) {
  964. [_callDurationTimer invalidate];
  965. _callDurationTimer = nil;
  966. _callDurationTime = 0;
  967. dispatch_async(dispatch_get_main_queue(),^{
  968. self.callDurationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCallDurationTime) userInfo:nil repeats:YES];
  969. [[NSRunLoop mainRunLoop] addTimer:self.callDurationTimer forMode:NSRunLoopCommonModes];
  970. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStartDebugMode object:_connection];
  971. });
  972. }
  973. _state = VoIPCallManagerStateCalling;
  974. [_callPlayer stop];
  975. [_hangupPlayer stop];
  976. [_ringTonePlayer stop];
  977. [_problemPlayer stop];
  978. _changedToWebRTCAudio = NO;
  979. } else if (newState == RTCIceConnectionStateCompleted) {
  980. _state = VoIPCallManagerStateCalling;
  981. [_callPlayer stop];
  982. [_hangupPlayer stop];
  983. [_ringTonePlayer stop];
  984. [_problemPlayer stop];
  985. if (_iceCandidatesTimer && [_iceCandidatesTimer isValid]){
  986. [_iceCandidatesTimer invalidate];
  987. _iceCandidatesTimer = nil;
  988. }
  989. } else if (newState == RTCIceConnectionStateFailed) {
  990. [self hangup];
  991. } else if (newState == RTCIceConnectionStateDisconnected) {
  992. _state = VoIPCallManagerStateReconnecting;
  993. dispatch_async(dispatch_get_main_queue(),^{
  994. // wait 2 seconds and play sound if its still on status reconnecting
  995. _reconnectTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(playReconnecting:) userInfo:nil repeats:NO];
  996. });
  997. } else if (newState == RTCIceConnectionStateClosed) {
  998. }
  999. if (newState != RTCIceConnectionStateDisconnected) {
  1000. dispatch_async(dispatch_get_main_queue(),^{
  1001. [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
  1002. });
  1003. }
  1004. if (wasInitializing && (newState == RTCIceConnectionStateConnected || newState == RTCIceConnectionStateCompleted)) {
  1005. dispatch_async(dispatch_get_main_queue(),^{
  1006. // Schedule 'connected' stats timer
  1007. VoIPStatsOptions *options = [[VoIPStatsOptions alloc] init];
  1008. options.transport = true;
  1009. options.selectedCandidatePair = true;
  1010. options.inboundRtp = true;
  1011. options.codecs = true;
  1012. options.candidatePairsFlag = CandidatePairVariantOVERVIEW;
  1013. [self schedulePeriodStatsWithOptions:options period:kLogStatsIntervalConnected];
  1014. });
  1015. }
  1016. }
  1017. /** Called any time the IceGatheringState changes. */
  1018. - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeIceGatheringState:(RTCIceGatheringState)newState {
  1019. // ignore
  1020. }
  1021. /** New ice candidate has been found. */
  1022. - (void)peerConnection:(RTCPeerConnection *)peerConnection didGenerateIceCandidate:(RTCIceCandidate *)candidate {
  1023. if ([self shouldAddCandidate:candidate]) {
  1024. if (_isCopyIceCandidates) {
  1025. [_tmpIceCandidates addObject:candidate];
  1026. } else {
  1027. if (_tmpIceCandidates.count > 0) {
  1028. [_iceCandidates addObjectsFromArray:_tmpIceCandidates];
  1029. [_tmpIceCandidates removeAllObjects];
  1030. }
  1031. [_iceCandidates addObject:candidate];
  1032. }
  1033. dispatch_async(dispatch_get_main_queue(),^{
  1034. if (!_iceCandidatesTimer) {
  1035. _iceCandidatesTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(sendCandidates:) userInfo:nil repeats:YES];
  1036. }
  1037. });
  1038. }
  1039. }
  1040. - (void)sendCandidates:(NSTimer *)timer {
  1041. if (_iceCandidates.count) {
  1042. _isCopyIceCandidates = YES;
  1043. NSMutableArray *candidates = _iceCandidates.copy;
  1044. [_iceCandidates removeAllObjects];
  1045. _isCopyIceCandidates = NO;
  1046. NSMutableArray *sendCandidates = [NSMutableArray new];
  1047. int candidatesCount = 0;
  1048. for (int i = 0; i < candidates.count; i++) {
  1049. candidatesCount++;
  1050. [sendCandidates addObject:candidates[i]];
  1051. if (candidates.count > 0 && (candidatesCount == 5 || i == candidates.count - 1)) {
  1052. VoIPCallIceCandidatesMessage *message = [VoIPCallIceCandidatesMessage new];
  1053. message.removed = NO;
  1054. message.candidates = sendCandidates.copy;
  1055. [VoIPSender sendVoIPCallIceCandidatesMessage:message toContact:_contact];
  1056. [sendCandidates removeAllObjects];
  1057. candidatesCount = 0;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. /** Called when a group of local Ice candidates have been removed. */
  1063. - (void)peerConnection:(RTCPeerConnection *)peerConnection didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates {
  1064. // send it only all 50 milli seconds
  1065. VoIPCallIceCandidatesMessage *message = [VoIPCallIceCandidatesMessage new];
  1066. message.removed = YES;
  1067. message.candidates = candidates;
  1068. [VoIPSender sendVoIPCallIceCandidatesMessage:message toContact:_contact];
  1069. }
  1070. /** New data channel has been opened. */
  1071. - (void)peerConnection:(RTCPeerConnection *)peerConnection didOpenDataChannel:(RTCDataChannel *)dataChannel {
  1072. // ignore
  1073. }
  1074. #pragma mark - AVAudioPlayerDelegate
  1075. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
  1076. [[RTCAudioSession sharedInstance] lockForConfiguration];
  1077. NSError *error = nil;
  1078. if (![[RTCAudioSession sharedInstance] setActive:NO error:&error]) {
  1079. NSLog(@"resume music player failed, error=%@", error);
  1080. }
  1081. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  1082. AVAudioSession *audioSession = [AVAudioSession sharedInstance];
  1083. if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]) {
  1084. NSLog(@"resume music player failed, error=%@", error);
  1085. }
  1086. }
  1087. #pragma mark - RTCAudioSessionDelegate
  1088. - (void)audioSessionDidStartPlayOrRecord:(RTCAudioSession *)session {
  1089. if ([session.currentRoute.outputs[0].portType isEqualToString:@"Speaker"]) {
  1090. _isSpeakerActive = YES;
  1091. } else {
  1092. _isSpeakerActive = NO;
  1093. }
  1094. _changedToWebRTCAudio = YES;
  1095. }
  1096. - (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session {
  1097. _changedToWebRTCAudio = NO;
  1098. }
  1099. #pragma mark - Notifications
  1100. - (void)handleRouteChange:(NSNotification *)notification {
  1101. if (_changedToWebRTCAudio && _isSpeakerActive) {
  1102. NSError *error;
  1103. [[RTCAudioSession sharedInstance] lockForConfiguration];
  1104. [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
  1105. [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
  1106. [[RTCAudioSession sharedInstance] setActive:YES error:&error];
  1107. [[RTCAudioSession sharedInstance] unlockForConfiguration];
  1108. }
  1109. }
  1110. @end