12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259 |
- // _____ _
- // |_ _| |_ _ _ ___ ___ _ __ __ _
- // | | | ' \| '_/ -_) -_) ' \/ _` |_
- // |_| |_||_|_| \___\___|_|_|_\__,_(_)
- //
- // Threema iOS Client
- // Copyright (c) 2017-2020 Threema GmbH
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License, version 3,
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- #import <AVFoundation/AVFoundation.h>
- #import <AVKit/AVKit.h>
- #import <UIKit/UIKit.h>
- #import <WebRTC/WebRTC.h>
- #import <WebRTC/RTCIceServer.h>
- #import "VoIPCallManager.h"
- #import "VoIPSender.h"
- #include <arpa/inet.h>
- #import "VoIPCallIceCandidatesMessage.h"
- #import "BundleUtil.h"
- #import "CallViewController.h"
- #import "CallManager.h"
- #import "MainTabBarController.h"
- #import "AppDelegate.h"
- #import "UserSettings.h"
- #import "EntityManager.h"
- #import "DateFormatter.h"
- #import "NSString+Hex.h"
- #import "AppGroup.h"
- #import "ServerConnector.h"
- #import "DateFormatter.h"
- #import "VoIPHelper.h"
- #import "NotificationManager.h"
- #import "PushSetting.h"
- #import "ValidationLogger.h"
- #import "Threema-Swift.h"
- #define kIncomingCallTimeout 60.0
- #define kLogStatsIntervalConnecting 2.0
- #define kLogStatsIntervalConnected 30.0
- @interface VoIPCallManager () <RTCPeerConnectionDelegate, AVAudioPlayerDelegate, RTCAudioSessionDelegate>
- @property (nonatomic, strong) RTCPeerConnectionFactory *factory;
- @property (nonatomic, strong) RTCPeerConnection *connection;
- @property (nonatomic, strong) Contact *contact;
- @property (nonatomic, strong) EntityManager *entityManager;
- @property (nonatomic, strong) NSMutableDictionary *bufferReceivedAddIceCandidates;
- @property (nonatomic, strong) NSMutableDictionary *bufferReceivedRemoveIceCandidates;
- @property (nonatomic, assign) BOOL isMuteEnabled;
- @property (nonatomic, strong) RTCAudioTrack *defaultAudioTrack;
- @property (nonatomic, strong) NSMutableArray *iceCandidates;
- @property (nonatomic, strong) NSMutableArray *tmpIceCandidates;
- @property (nonatomic) BOOL isCopyIceCandidates;
- @property (nonatomic, strong) NSTimer *incomingCallTimer;
- @property (nonatomic, strong) NSTimer *iceCandidatesTimer;
- @property (strong, nonatomic) AVAudioPlayer *callPlayer;
- @property (strong, nonatomic) AVAudioPlayer *hangupPlayer;
- @property (strong, nonatomic) AVAudioPlayer *pickupPlayer;
- @property (strong, nonatomic) AVAudioPlayer *ringTonePlayer;
- @property (strong, nonatomic) AVAudioPlayer *problemPlayer;
- @property (strong, nonatomic) AVAudioPlayer *rejectedPlayer;
- @property (strong, nonatomic) NSTimer *durationTimer;
- @property (strong, nonatomic) NSTimer *reconnectTimer;
- @property (strong, nonatomic) NSTimer *statsTimer;
- @property (nonatomic) BOOL isSpeakerActive;
- @property (nonatomic) BOOL changedToWebRTCAudio;
- @end
- @implementation VoIPCallManager
- + (VoIPCallManager*)sharedVoIPCallManager {
- static VoIPCallManager *instance;
-
- @synchronized (self) {
- if (!instance)
- instance = [[VoIPCallManager alloc] init];
- }
-
- return instance;
- }
- - (id)init {
- self = [super init];
- if (self) {
- _factory = [RTCPeerConnectionFactory new];
- _bufferReceivedAddIceCandidates = [NSMutableDictionary new];
- _bufferReceivedRemoveIceCandidates = [NSMutableDictionary new];
- _isMuteEnabled = NO;
- _state = VoIPCallManagerStateIdle;
- _iceCandidates = [NSMutableArray new];
- _tmpIceCandidates = [NSMutableArray new];
- _isCopyIceCandidates = NO;
- _entityManager = [[EntityManager alloc] init];
- _isSpeakerActive = NO;
- _changedToWebRTCAudio = NO;
- _callAlreadyEnded = NO;
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
- [[RTCAudioSession sharedInstance] addDelegate:self];
- }
- return self;
- }
- #pragma mark - private functions
- - (RTCConfiguration *)defaultRTCConfiguration {
- RTCConfiguration *configuration = [[RTCConfiguration alloc] init];
- 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];
- configuration.iceServers = @[servers];
-
- if (_contact.verificationLevel == kVerificationLevelUnverified || [UserSettings sharedUserSettings].alwaysRelayCalls) {
- configuration.iceTransportPolicy = RTCIceTransportPolicyRelay;
- }
-
- configuration.bundlePolicy = RTCBundlePolicyMaxBundle;
- configuration.rtcpMuxPolicy = RTCRtcpMuxPolicyRequire;
- configuration.tcpCandidatePolicy = RTCTcpCandidatePolicyDisabled;
- configuration.continualGatheringPolicy = RTCContinualGatheringPolicyGatherContinually;
-
- return configuration;
- }
- - (RTCMediaConstraints *)defaultPeerConnectionConstraints {
- NSDictionary *optionalConstraints = @{@"DtlsSrtpKeyAgreement": @"true"};
- RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:optionalConstraints];
- return constraints;
- }
- - (RTCMediaConstraints *)defaultOfferConstraints {
- NSDictionary *mandatoryConstraints = @{@"OfferToReceiveAudio": @"true", @"OfferToReceiveVideo": @"false"};
- RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:nil];
- return constraints;
- }
- - (RTCMediaConstraints *)defaultAnswerConstraints {
- NSDictionary *mandatoryConstraints = @{@"OfferToReceiveAudio": @"true", @"OfferToReceiveVideo": @"false"};
- RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:mandatoryConstraints optionalConstraints:nil];
- return constraints;
- }
- - (RTCMediaConstraints *)defaultAudioConstraints {
- RTCMediaConstraints* constraints = [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil optionalConstraints:nil];
- return constraints;
- }
- - (RTCMediaStream *)createLocalMediaStreamWithFactory:(RTCPeerConnectionFactory *)factory {
- RTCAudioSource *source = [factory audioSourceWithConstraints:[self defaultAudioConstraints]];
- RTCMediaStream *localStream = [factory mediaStreamWithStreamId:@"AMACALL"];
- [localStream addAudioTrack:[factory audioTrackWithSource:source trackId:@"AMACALLa0"]];
- return localStream;
- }
- + (BOOL)isIPv6Address:(NSString *)ip {
- const char *utf8 = [ip UTF8String];
-
- // Check valid IPv4.
- struct in_addr dst;
- int success = inet_pton(AF_INET, utf8, &(dst.s_addr));
- if (success != 1) {
- // Check valid IPv6.
- struct in6_addr dst6;
- return inet_pton(AF_INET6, utf8, &dst6);
- }
- return NO;
- }
- - (BOOL)shouldAddCandidate:(RTCIceCandidate *)candidate {
- BOOL addCandidate = NO;
- if (![UserSettings sharedUserSettings].enableIPv6) {
- NSArray *sdpSplit = [candidate.sdp componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" "]];
-
- if (sdpSplit.count >= 5) {
- if ([sdpSplit[4] rangeOfString:@"."].location == NSNotFound && [sdpSplit[4] rangeOfString:@":"].location == NSNotFound) {
- addCandidate = YES;
- } else {
- if (![VoIPCallManager isIPv6Address:sdpSplit[4]]) {
- addCandidate = YES;
- }
- }
- } else {
- addCandidate = YES;
- }
- } else {
- addCandidate = YES;
- }
-
- return addCandidate;
- }
- - (void)updateCallDurationTime {
- _callDurationTime = _callDurationTime + 1;
- if (_state != VoIPCallManagerStateReconnecting) {
- _callTimeString = [DateFormatter timeFormatted:_callDurationTime];
- [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackgroundTimeChanged object:[NSNumber numberWithInt:_callDurationTime]];
- } else {
- [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackgroundTimeChanged object:nil];
- }
- }
- - (void)setupCallTone {
- NSString *soundFilePath = [BundleUtil pathForResource:@"ringing-tone-ch-fade" ofType:@"mp3"];
- NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
- _callPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
- _callPlayer.numberOfLoops = -1;
- [_callPlayer prepareToPlay];
- }
- - (void)setupHangupTone {
- NSString *soundFilePath = [BundleUtil pathForResource:@"threema_hangup" ofType:@"mp3"];
- NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
- _hangupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
- _hangupPlayer.numberOfLoops = 1;
- _hangupPlayer.delegate = self;
- [_hangupPlayer prepareToPlay];
- }
- - (void)setupPickupTone {
- NSString *soundFilePath = [BundleUtil pathForResource:@"threema_pickup" ofType:@"mp3"];
- NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
- _pickupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
- _pickupPlayer.numberOfLoops = 1;
- [_pickupPlayer prepareToPlay];
- }
- - (void)setupRingTone {
- NSString *voIPSound = [UserSettings sharedUserSettings].voIPSound;
- NSString *soundFilePath;
- if (![voIPSound isEqualToString:@"default"]) {
- soundFilePath = [BundleUtil pathForResource:[UserSettings sharedUserSettings].voIPSound ofType:@"mp3"];
- } else {
- soundFilePath = [BundleUtil pathForResource:@"threema_best" ofType:@"mp3"];
- }
- NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
- _ringTonePlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
- _ringTonePlayer.numberOfLoops = -1;
- [_ringTonePlayer prepareToPlay];
- }
- - (void)setupProblemTone {
- NSString *soundFilePath = [BundleUtil pathForResource:@"threema_problem" ofType:@"mp3"];
- NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
- _problemPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
- _problemPlayer.numberOfLoops = -1;
- [_problemPlayer prepareToPlay];
- }
- - (void)setupRejectedTone {
- NSString *soundFilePath = [BundleUtil pathForResource:@"busy-4x" ofType:@"mp3"];
- NSURL *filePath = [NSURL fileURLWithPath:soundFilePath];
- _rejectedPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
- _rejectedPlayer.numberOfLoops = -1;
- [_rejectedPlayer prepareToPlay];
- }
- - (void)playReconnecting:(NSTimer *)timer {
- if (_state == VoIPCallManagerStateReconnecting) {
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
- [self playTone:VoIPCallManagerToneProblem];
- AVAudioSession *session = [AVAudioSession sharedInstance];
- NSArray *outputs = [[session currentRoute] outputs];
- BOOL isSpeaker = NO;
- for (AVAudioSessionPortDescription *desc in outputs) {
- if ([desc.portType isEqualToString:AVAudioSessionPortBuiltInSpeaker]) {
- isSpeaker = YES;
- }
- }
- [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
- isSpeaker ? [session overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil] : [session overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
- [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
- }
- }
- - (void)schedulePeriodStatsWithOptions:(VoIPStatsOptions *)options period:(NSTimeInterval)period {
- // Reset timer
- if (self.statsTimer && [self.statsTimer isValid]) {
- [self.statsTimer invalidate];
- self.statsTimer = nil;
- }
-
- // Create new timer with <period> (but immediately log once)
- NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
- [dict setObject:self.connection forKey:@"connection"];
- [dict setObject:options forKey:@"options"];
- self.statsTimer = [NSTimer scheduledTimerWithTimeInterval:period target:self selector:@selector(logDebugStatsFromTimer:) userInfo:dict repeats:YES];
- [self logDebugStats:dict];
- [[NSRunLoop mainRunLoop] addTimer:self.statsTimer forMode:NSRunLoopCommonModes];
- }
- - (void)logDebugStatsFromTimer:(NSTimer *)timer {
- NSDictionary *dict = [timer userInfo];
- [self logDebugStats:dict];
- }
- - (void)logDebugStats:(NSDictionary *)dict {
- RTCPeerConnection *connection = [dict objectForKey:@"connection"];
- VoIPStatsOptions *options = [dict objectForKey:@"options"];
- [connection statsForTrack:nil statsOutputLevel:RTCStatsOutputLevelDebug completionHandler:^(NSArray<RTCLegacyStatsReport *> * _Nonnull report) {
- VoIPStats *stats = [[VoIPStats alloc] initWithReport:report options:options];
- NSString *statsStr = [stats getRepresentation];
- [[ValidationLogger sharedValidationLogger] logString:[NSString stringWithFormat:@"Call: Stats\n%@", statsStr]];
-
- // Execute callback (if any)
- void(^callback)(void) = [dict objectForKey:@"callback"];
- if (callback) {
- callback();
- }
- }];
- }
- - (NSString *)stringForIceConnectionState:(RTCIceConnectionState)state {
- switch (state) {
- case RTCIceConnectionStateNew:
- return @"new";
- case RTCIceConnectionStateChecking:
- return @"checking";
- case RTCIceConnectionStateConnected:
- return @"connected";
- case RTCIceConnectionStateCompleted:
- return @"completed";
- case RTCIceConnectionStateFailed:
- return @"failed";
- case RTCIceConnectionStateDisconnected:
- return @"disconnected";
- case RTCIceConnectionStateClosed:
- return @"closed";
- case RTCIceConnectionStateCount:
- return @"count";
- }
- }
- #pragma mark - public functions
- - (Contact *)getCallContact {
- return _contact;
- }
- - (void)startVoIPCallWithContact:(Contact *)contact {
- _state = VoIPCallManagerStateWaitForRinging;
- _changedToWebRTCAudio = NO;
- [self setupTones];
- dispatch_async(dispatch_get_main_queue(),^{
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
- [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
- });
-
- _contact = contact;
- RTCMediaConstraints *constraints = [self defaultPeerConnectionConstraints];
- RTCConfiguration *configuration = [self defaultRTCConfiguration];
- _connection = [_factory peerConnectionWithConfiguration:configuration constraints:constraints delegate:self];
- RTCMediaStream *localStream = [self createLocalMediaStreamWithFactory:_factory];
- [_connection addStream:localStream];
- [_connection offerForConstraints:constraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
- [_connection setLocalDescription:sdp completionHandler:^(NSError * _Nullable error) {
- if (!error) {
- _isCallInitiator = YES;
- [VoIPSender startVoIPCallWithContact:contact sessionDescription:sdp];
- }
- }];
- }];
- }
- - (void)startRinging {
- _state = VoIPCallManagerStateRinging;
- [[VoIPCallManager sharedVoIPCallManager] playTone:VoIPCallManagerToneCall];
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
- }
- - (void)startVoIPCallAnswerWithContact:(Contact *)contact {
- _state = VoIPCallManagerStateInitializing;
- _changedToWebRTCAudio = NO;
- [self setupTones];
- dispatch_async(dispatch_get_main_queue(),^{
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
- [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
- });
-
- __weak VoIPCallManager *weakSelf = self;
-
- [[CallManager sharedInstance] callPickedUpFromReceiver];
-
- if (!contact) {
- contact = _contact;
- }
-
- RTCMediaConstraints *answerConstraints = [self defaultAnswerConstraints];
- [_connection answerForConstraints:answerConstraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
- if (!error) {
- [_connection setLocalDescription:sdp completionHandler:^(NSError * _Nullable error) {
- if (!error) {
- _isCallInitiator = NO;
- VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
- message.answer = sdp;
- message.action = VoIPCallAnswerMessageActionCall;
- message.rejectReason = VoIPCallAnswerMessageRejectReasonUnknown;
-
- [VoIPSender startVoIPCallAnswerWithContact:contact message:message];
-
- if (weakSelf.incomingCallTimer && [weakSelf.incomingCallTimer isValid]){
- [weakSelf.incomingCallTimer invalidate];
- weakSelf.incomingCallTimer = nil;
- }
- }
- }];
- }
- }];
- }
- - (void)setOffer:(RTCSessionDescription *)sdp fromContact:(Contact *)contact {
- __weak VoIPCallManager *weakSelf = self;
- _state = VoIPCallManagerStateWaitForRinging;
- dispatch_async(dispatch_get_main_queue(),^{
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
- });
- _contact = contact;
- RTCMediaConstraints *offerConstraints = [self defaultPeerConnectionConstraints];
- RTCConfiguration *configuration = [self defaultRTCConfiguration];
- _connection = [_factory peerConnectionWithConfiguration:configuration constraints:offerConstraints delegate:self];
- RTCMediaStream *localStream = [self createLocalMediaStreamWithFactory:_factory];
- [_connection addStream:localStream];
-
- @synchronized (_bufferReceivedAddIceCandidates) {
- [_bufferReceivedAddIceCandidates removeAllObjects];
- }
- @synchronized (_bufferReceivedRemoveIceCandidates) {
- [_bufferReceivedRemoveIceCandidates removeAllObjects];
- }
-
- [_connection setRemoteDescription:sdp completionHandler:^(NSError * _Nullable error) {
- if (!error) {
- NSUUID *uuid = [NSUUID new];
- _state = VoIPCallManagerStateRinging;
-
- // add already received candidates
- @synchronized (weakSelf.bufferReceivedAddIceCandidates) {
- NSMutableArray *addCandidates = [weakSelf.bufferReceivedAddIceCandidates valueForKey:contact.identity];
- [addCandidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
- [weakSelf.connection addIceCandidate:candidate];
- }];
- [weakSelf.bufferReceivedAddIceCandidates removeAllObjects];
- }
-
- @synchronized (weakSelf.bufferReceivedRemoveIceCandidates) {
- NSMutableArray *removeCandidates = [weakSelf.bufferReceivedRemoveIceCandidates valueForKey:contact.identity];
- if (removeCandidates) {
- [weakSelf.connection removeIceCandidates:removeCandidates];
- }
- [weakSelf.bufferReceivedRemoveIceCandidates removeAllObjects];
- }
-
- dispatch_async(dispatch_get_main_queue(),^{
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:weakSelf.state]];
-
- _incomingCallTimer = [NSTimer scheduledTimerWithTimeInterval:kIncomingCallTimeout target:self selector:@selector(timeoutCall) userInfo:nil repeats:NO];
- });
- _isCallInitiator = NO;
- if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0") && [UserSettings sharedUserSettings].enableCallKit && ![[[NSLocale currentLocale] objectForKey: NSLocaleCountryCode] isEqualToString:@"CN"]) {
- [[CallManager sharedInstance] reportIncomingCallForUUID:uuid contact:contact];
- } else {
- dispatch_async(dispatch_get_main_queue(), ^{
- UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
- UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"CallStoryboard" bundle:nil];
- CallViewController *callViewController = (CallViewController *)[storyboard instantiateInitialViewController];
- callViewController.contact = [weakSelf contact];
- callViewController.alreadyAccepted = NO;
- callViewController.isCallInitiator = NO;
- callViewController.modalPresentationStyle = UIModalPresentationOverFullScreen;
- [vc presentViewController:callViewController animated:NO completion:nil];
- });
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
- /* We're in the background and have received a message. There will be no push, so we need to generate a local notification */
- NSString *cmd;
- UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];
- notification.categoryIdentifier = @"INCOMCALL";
- cmd = @"newcall";
-
- NSString *voIPSound = [UserSettings sharedUserSettings].voIPSound;
- if (![voIPSound isEqualToString:@"default"]) {
- notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.mp3", [UserSettings sharedUserSettings].voIPSound]];
- } else {
- notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"threema_best.mp3"]];
- }
-
- notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObjectsAndKeys: cmd, @"cmd", contact.displayName, @"from", nil], @"threema", nil];
- notification.title = contact.displayName;
- notification.body = NSLocalizedString(@"call_incoming_ended", @"");
-
- NSString *notificationIdentifier = contact.identity;
- UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:notificationIdentifier content:notification trigger:nil];
-
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
- [center addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
- }];
- }
- });
- }
-
- [VoIPSender sendVoIPCallRingingMessageToContact:weakSelf.contact];
- }
- }];
- }
- - (void)setAnswer:(RTCSessionDescription *)sdp {
- __weak VoIPCallManager *weakSelf = self;
- [_connection setRemoteDescription:sdp completionHandler:^(NSError * _Nullable error) {
- if (error) {
- [weakSelf callRejected];
- }
- }];
- }
- - (void)removeIceCandidates:(NSArray *)candidates fromIdentity:(NSString *)identity {
- if (_connection) {
- if ([_contact.identity isEqualToString:identity]) {
- [_connection removeIceCandidates:candidates];
- }
- } else {
- @synchronized (_bufferReceivedRemoveIceCandidates) {
- NSMutableArray *candidatesArray = [_bufferReceivedRemoveIceCandidates valueForKey:identity];
- if (!candidatesArray) {
- candidatesArray = [NSMutableArray new];
- }
- [candidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
- [candidatesArray addObject:candidate];
- }];
- [_bufferReceivedRemoveIceCandidates setObject:candidatesArray forKey:identity];
- }
- }
- }
- - (void)addIceCandidates:(NSArray *)candidates fromIdentity:(NSString *)identity{
- if (_connection) {
- if ([_contact.identity isEqualToString:identity]) {
- [candidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
- if ([self shouldAddCandidate:candidate]) {
- [_connection addIceCandidate:candidate];
- }
- }];
- }
- } else {
- @synchronized (_bufferReceivedAddIceCandidates) {
- NSMutableArray *candidatesArray = [_bufferReceivedAddIceCandidates valueForKey:identity];
-
- if (!candidatesArray) {
- candidatesArray = [NSMutableArray new];
- }
- [candidates enumerateObjectsUsingBlock:^(RTCIceCandidate *candidate, NSUInteger idx, BOOL * _Nonnull stop) {
- if ([self shouldAddCandidate:candidate]) {
- [candidatesArray addObject:candidate];
- }
- }];
-
- [_bufferReceivedAddIceCandidates setObject:candidatesArray forKey:identity];
- }
- }
- }
- - (void)hangup {
- if (!_callAlreadyEnded) {
- _callAlreadyEnded = YES;
- [VoIPSender sendVoIPCallHangupToContact:_contact];
- if (_state == VoIPCallManagerStateWaitForRinging || _state == VoIPCallManagerStateRinging || _state == VoIPCallManagerStateInitializing || _state == VoIPCallManagerStateCalling || _state == VoIPCallManagerStateReconnecting || _state == VoIPCallManagerStateSystemRejected) {
- Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
- [_entityManager performSyncBlockAndSafe:^{
- SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
- systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallEnded];
- if (!_callTimeString)
- _callTimeString = @"";
- NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallTime": _callTimeString, @"CallInitiator": [NSNumber numberWithBool:_isCallInitiator]};
- NSError *error;
- NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
- systemMessage.arg = data;
- systemMessage.isOwn = [NSNumber numberWithBool:_isCallInitiator];
- systemMessage.conversation = conversation;
- conversation.lastMessage = systemMessage;
- _callTimeString = @"";
- }];
- }
- if (_incomingCallTimer && [_incomingCallTimer isValid]){
- [_incomingCallTimer invalidate];
- _incomingCallTimer = nil;
- }
- [self disconnect: true];
- [[CallManager sharedInstance] endCall];
- }
- }
- - (void)hangupOnCompletion:(void(^)(void))onCompletion {
- [VoIPSender sendVoIPCallHangupAndWaitToContact:_contact];
- if (_state == VoIPCallManagerStateWaitForRinging || _state == VoIPCallManagerStateRinging || _state == VoIPCallManagerStateInitializing || _state == VoIPCallManagerStateCalling || _state == VoIPCallManagerStateReconnecting || _state == VoIPCallManagerStateSystemRejected) {
- if (![[VoIPCallManager sharedVoIPCallManager] callAlreadyEnded]) {
- [[VoIPCallManager sharedVoIPCallManager] setCallAlreadyEnded:YES];
- Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
- [_entityManager performSyncBlockAndSafe:^{
- SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
- systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallEnded];
- if (!_callTimeString)
- _callTimeString = @"";
- NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallTime": _callTimeString, @"CallInitiator": [NSNumber numberWithBool:_isCallInitiator]};
- NSError *error;
- NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
- systemMessage.arg = data;
- systemMessage.isOwn = [NSNumber numberWithBool:_isCallInitiator];
- systemMessage.conversation = conversation;
- conversation.lastMessage = systemMessage;
- _callTimeString = @"";
- }];
- }
- }
- if (_incomingCallTimer && [_incomingCallTimer isValid]){
- [_incomingCallTimer invalidate];
- _incomingCallTimer = nil;
- }
- [self disconnect: true];
- [[CallManager sharedInstance] endCall];
- onCompletion();
- }
- - (void)callHangedup {
- [self disconnect: true];
- [[CallManager sharedInstance] endCall];
- }
- - (void)callRejected {
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:VoIPCallManagerStateSystemRejected]];
- [_reconnectTimer invalidate];
- [self setCallTimeString:@""];
-
- [self disconnect: false];
- [[CallManager sharedInstance] endCall];
- }
- - (void)timeoutCall {
- __weak VoIPCallManager *weakSelf = self;
- RTCMediaConstraints *answerConstraints = [self defaultAnswerConstraints];
- [_connection answerForConstraints:answerConstraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
- if (!error) {
- VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
- message.answer = sdp;
- message.action = VoIPCallAnswerMessageActionReject;
- message.rejectReason = VoIPCallAnswerMessageRejectReasonTimeout;
- [VoIPSender startVoIPCallAnswerRejectWithContact:_contact message:message];
- }
- __block NSString *messageId;
- Conversation *conversation = [_entityManager.entityFetcher conversationForIdentity:_contact.identity];
- [_entityManager performSyncBlockAndSafe:^{
- /* Insert system message to document the missed call */
- NSDictionary *argDict;
- SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
- systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallMissed];
- systemMessage.isOwn = [NSNumber numberWithBool:NO];
- conversation.unreadMessageCount = [NSNumber numberWithInt:[[conversation unreadMessageCount] intValue] + 1];
- argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallInitiator": [NSNumber numberWithBool:[[VoIPCallManager sharedVoIPCallManager] isCallInitiator]]};
- NSError *error;
- NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
- systemMessage.arg = data;
- systemMessage.isOwn = [NSNumber numberWithBool:[[VoIPCallManager sharedVoIPCallManager] isCallInitiator]];
- systemMessage.conversation = conversation;
- conversation.lastMessage = systemMessage;
- messageId = [NSString stringWithHexData:systemMessage.id];
- }];
- dispatch_async(dispatch_get_main_queue(),^{
- [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
- });
-
- __block UIApplicationState state;
- dispatch_async(dispatch_get_main_queue(),^{
- state = [UIApplication sharedApplication].applicationState;
- });
- PushSetting *pushSetting = [PushSetting findPushSettingForConversation:conversation];
- BOOL canSendPush = YES;
- if (pushSetting != nil) {
- canSendPush = [pushSetting canSendPush];
- }
- if (state == UIApplicationStateBackground && canSendPush) {
- UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];
-
- NSString *cmd;
- if ([UserSettings sharedUserSettings].pushDecrypt) {
- notification.title = _contact.displayName;
- notification.body = NSLocalizedString(@"call_missed", nil);
- notification.categoryIdentifier = @"CALL";
- } else {
- notification.body = [NSString stringWithFormat:NSLocalizedString(@"new_message_from_x", nil), _contact.displayName];
- notification.categoryIdentifier = @"";
- }
-
- cmd = @"newmsg";
-
- if (![[UserSettings sharedUserSettings].pushSound isEqualToString:@"none"]) {
- if (pushSetting != nil) {
- if (!pushSetting.silent) {
- notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.caf", [UserSettings sharedUserSettings].pushSound]];
- }
- } else {
- notification.sound = [UNNotificationSound soundNamed:[NSString stringWithFormat:@"%@.caf", [UserSettings sharedUserSettings].pushSound]];
- }
- }
-
- notification.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:[NSDictionary dictionaryWithObjectsAndKeys: cmd, @"cmd", _contact.displayName, @"from", messageId, @"messageId", nil], @"threema", nil];
- NSString *notificationIdentifier = _contact.identity;
- UNNotificationRequest *notificationRequest = [UNNotificationRequest requestWithIdentifier:notificationIdentifier content:notification trigger:nil];
-
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
- [center addNotificationRequest:notificationRequest withCompletionHandler:^(NSError * _Nullable error) {
- }];
- }
-
- if (weakSelf.incomingCallTimer && [weakSelf.incomingCallTimer isValid]){
- [weakSelf.incomingCallTimer invalidate];
- weakSelf.incomingCallTimer = nil;
- }
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:VoIPCallManagerStateIdle]];
-
- [_reconnectTimer invalidate];
- [self setCallTimeString:@""];
-
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- NSError *rtcError = nil;
- if (![[RTCAudioSession sharedInstance] setActive:NO error:&rtcError]) {
- NSLog(@"resume music player failed, error=%@", rtcError);
- }
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&rtcError]) {
- NSLog(@"resume music player failed, error=%@", rtcError);
- }
-
- [self disconnect: true];
- [[CallManager sharedInstance] endCall];
- }];
- }
- - (void)rejectCallOnCompletion:(void(^)(void))onCompletion {
- __weak VoIPCallManager *weakSelf = self;
- RTCMediaConstraints *answerConstraints = [self defaultAnswerConstraints];
- [_connection answerForConstraints:answerConstraints completionHandler:^(RTCSessionDescription * _Nullable sdp, NSError * _Nullable error) {
- if (!error) {
- _isCallInitiator = NO;
- VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
- message.answer = sdp;
- message.action = VoIPCallAnswerMessageActionReject;
- message.rejectReason = VoIPCallAnswerMessageRejectReasonReject;
- [VoIPSender startVoIPCallAnswerRejectWithContact:_contact message:message];
- if (![[VoIPCallManager sharedVoIPCallManager] callAlreadyEnded]) {
- [[VoIPCallManager sharedVoIPCallManager] setCallAlreadyEnded:YES];
- Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
- [_entityManager performSyncBlockAndSafe:^{
- SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
- systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallRejected];
- NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallInitiator": [NSNumber numberWithBool:_isCallInitiator]};
- NSError *error;
- NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
- systemMessage.arg = data;
- systemMessage.isOwn = [NSNumber numberWithBool:_isCallInitiator];
- systemMessage.read = [NSNumber numberWithBool:YES];
- systemMessage.conversation = conversation;
- conversation.lastMessage = systemMessage;
- }];
- }
- }
- if (weakSelf.incomingCallTimer && [weakSelf.incomingCallTimer isValid]){
- [weakSelf.incomingCallTimer invalidate];
- weakSelf.incomingCallTimer = nil;
- }
- [self disconnect: false];
- [[CallManager sharedInstance] endCall];
- onCompletion();
- }];
- }
- - (void)rejectCallWithBusy:(Contact *)contact onCompletion:(void(^)(void))onCompletion {
- VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
- message.answer = nil;
- message.action = VoIPCallAnswerMessageActionReject;
- message.rejectReason = VoIPCallAnswerMessageRejectReasonBusy;
- [VoIPSender startVoIPCallAnswerRejectWithContact:contact message:message];
- Conversation *conversation = [_entityManager conversationForContact:_contact createIfNotExisting:YES];
- [_entityManager performSyncBlockAndSafe:^{
- SystemMessage *systemMessage = [_entityManager.entityCreator systemMessageForConversation:conversation];
- systemMessage.type = [NSNumber numberWithInteger:kSystemMessageCallMissed];
- NSDictionary *argDict = @{@"DateString": [DateFormatter shortStyleTimeNoDate:[NSDate date]], @"CallInitiator": [NSNumber numberWithBool:NO]};
- NSError *error;
- NSData *data = [NSJSONSerialization dataWithJSONObject:argDict options:NSJSONWritingPrettyPrinted error:&error];
- systemMessage.arg = data;
- systemMessage.isOwn = [NSNumber numberWithBool:NO];
- systemMessage.conversation = conversation;
- conversation.lastMessage = systemMessage;
- conversation.unreadMessageCount = [NSNumber numberWithInt:[[conversation unreadMessageCount] intValue] + 1];
- }];
- dispatch_async(dispatch_get_main_queue(),^{
- [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
- });
- onCompletion();
- }
- - (void)rejectCallWithDisabled:(Contact *)contact onCompletion:(void(^)(void))onCompletion {
- _state = VoIPCallManagerStateSystemRejected;
- VoIPCallAnswerMessage *message = [VoIPCallAnswerMessage new];
- message.answer = nil;
- message.action = VoIPCallAnswerMessageActionReject;
- message.rejectReason = VoIPCallAnswerMessageRejectReasonDisabled;
- [VoIPSender startVoIPCallAnswerRejectWithContact:contact message:message];
- dispatch_async(dispatch_get_main_queue(),^{
- [[NotificationManager sharedInstance] updateUnreadMessagesCount:NO];
- });
- [[CallManager sharedInstance] endCall];
- [self disconnect: false];
- if (onCompletion != nil) {
- onCompletion();
- }
- }
- - (void)disconnect:(BOOL)playSound {
- void(^disconnectCallback)(void) = ^{
- [_connection close];
- _connection = nil;
- };
-
- if (playSound) {
- [self playTone:VoIPCallManagerToneHangup];
- }
- [[VoIPHelper shared] setIsCallActiveInBackground:NO];
- [[VoIPHelper shared] setContactName:nil];
- [[VoIPHelper shared] setLastUpdatedCallDuration:nil];
- [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackground object:nil];
- [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationCallInBackgroundTimeChanged object:nil];
-
- if (_connection) {
- dispatch_async(dispatch_get_main_queue(),^{
- if (_statsTimer && [_statsTimer isValid]) {
- // Invalidate timer
- [_statsTimer invalidate];
- _statsTimer = nil;
-
- // Hijack the existing dict, override options and set callback
- VoIPStatsOptions *options = [[VoIPStatsOptions alloc] init];
- options.transport = true;
- options.inboundRtp = true;
- options.codecs = true;
- options.candidatePairsFlag = CandidatePairVariantOVERVIEW_AND_DETAILED;
- NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
- self.connection, @"connection",
- options, @"options",
- disconnectCallback, @"callback",
- nil];
-
- // One-shot stats fetch before disconnect
- [self logDebugStats:dict];
- } else {
- disconnectCallback();
- }
- [_incomingCallTimer invalidate];
- _incomingCallTimer = nil;
- _contact = nil;
- _callAlreadyEnded = NO;
- _isMuteEnabled = NO;
- [_iceCandidatesTimer invalidate];
- _iceCandidatesTimer = nil;
- [_callDurationTimer invalidate];
- _callDurationTimer = nil;
- _callDurationTime = 0;
- _callTimeString = @"";
- [_iceCandidates removeAllObjects];
- _state = VoIPCallManagerStateIdle;
- });
- }
- }
- - (void)muteAudioIn {
- RTCMediaStream *localStream = _connection.localStreams[0];
- self.defaultAudioTrack = localStream.audioTracks[0];
- [localStream removeAudioTrack:localStream.audioTracks[0]];
- [_connection removeStream:localStream];
- [_connection addStream:localStream];
- _isMuteEnabled = YES;
- }
- - (void)unmuteAudioIn {
- RTCMediaStream* localStream = _connection.localStreams[0];
- [localStream addAudioTrack:self.defaultAudioTrack];
- [_connection removeStream:localStream];
- [_connection addStream:localStream];
- _isMuteEnabled = NO;
- }
- - (BOOL)isMuteEnabled {
- return _isMuteEnabled;
- }
- - (void)invalidateDurationTimer {
- [_durationTimer invalidate];
- _durationTimer = nil;
- }
- - (void)setupTones {
- [self setupCallTone];
- [self setupHangupTone];
- [self setupPickupTone];
- [self setupRingTone];
- [self setupProblemTone];
- [self setupRejectedTone];
- }
- - (void)playTone:(VoIPCallManagerTone)tone {
- switch (tone) {
- case VoIPCallManagerToneCall:
- [_hangupPlayer stop];
- [_pickupPlayer stop];
- [_ringTonePlayer stop];
- [_problemPlayer stop];
- [_rejectedPlayer stop];
-
- [_callPlayer setCurrentTime:0];
- [_callPlayer play];
- break;
- case VoIPCallManagerToneHangup:
- [_callPlayer stop];
- [_pickupPlayer stop];
- [_ringTonePlayer stop];
- [_problemPlayer stop];
- [_rejectedPlayer stop];
-
- [_hangupPlayer play];
- break;
- case VoIPCallManagerTonePickup:
- [_callPlayer stop];
- [_hangupPlayer stop];
- [_ringTonePlayer stop];
- [_problemPlayer stop];
- [_rejectedPlayer stop];
-
- [_pickupPlayer play];
- break;
- case VoIPCallManagerToneRing:
- [_callPlayer stop];
- [_hangupPlayer stop];
- [_pickupPlayer stop];
- [_problemPlayer stop];
- [_rejectedPlayer stop];
-
- [_ringTonePlayer setCurrentTime:0];
- [_ringTonePlayer play];
- break;
- case VoIPCallManagerToneProblem:
- [_callPlayer stop];
- [_hangupPlayer stop];
- [_pickupPlayer stop];
- [_ringTonePlayer stop];
- [_rejectedPlayer stop];
-
- [_problemPlayer setCurrentTime:0];
- [_problemPlayer play];
- break;
- case VoIPCallManagerToneRejected:
- [_callPlayer stop];
- [_hangupPlayer stop];
- [_pickupPlayer stop];
- [_ringTonePlayer stop];
- [_problemPlayer stop];
-
- [_rejectedPlayer setCurrentTime:0];
- [_rejectedPlayer play];
- default:
- break;
- }
- }
- - (void)stopAllTones {
- [_callPlayer stop];
- [_hangupPlayer stop];
- [_pickupPlayer stop];
- [_ringTonePlayer stop];
- [_problemPlayer stop];
- [_rejectedPlayer stop];
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- NSError *error = nil;
- if (![[RTCAudioSession sharedInstance] setActive:NO error:&error]) {
- NSLog(@"resume music player failed, error=%@", error);
- }
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]) {
- NSLog(@"resume music player failed, error=%@", error);
- }
- }
- - (void)activateRTCAudio {
- AVAudioSessionRouteDescription *currentRoute = [[RTCAudioSession sharedInstance] currentRoute];
- AVAudioSessionPortDescription *portDesc = [[currentRoute outputs] firstObject];
- if ([portDesc.portType isEqualToString:@"Speaker"]) {
- NSError *error;
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
- [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
- [[RTCAudioSession sharedInstance] setActive:YES error:&error];
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
-
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
- [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
- [[RTCAudioSession sharedInstance] setActive:YES error:&error];
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
- } else {
- NSError *error;
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
- [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
- [[RTCAudioSession sharedInstance] setActive:YES error:&error];
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
-
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
- [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&error];
- [[RTCAudioSession sharedInstance] setActive:YES error:&error];
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
- }
- }
- #pragma mark - RTCPeerConnectionDelegate
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeSignalingState:(RTCSignalingState)stateChanged {
-
- }
- /** Called when media is received on a new stream from remote peer. */
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didAddStream:(RTCMediaStream *)stream {
- // ignore
- }
- /** Called when a remote peer closes a stream. */
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didRemoveStream:(RTCMediaStream *)stream {
- // ignore
- }
- /** Called when negotiation is needed, for example ICE has restarted. */
- - (void)peerConnectionShouldNegotiate:(RTCPeerConnection *)peerConnection {
- // ignore
- }
- /** Called any time the IceConnectionState changes. */
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeIceConnectionState:(RTCIceConnectionState)newState {
- BOOL wasInitializing = _state == VoIPCallManagerStateInitializing;
- NSString *strState = [self stringForIceConnectionState:newState];
- [[ValidationLogger sharedValidationLogger] logString:[NSString stringWithFormat: @"Call: ICE connection state -> %@", strState]];
- if (newState == RTCIceConnectionStateChecking) {
- _state = VoIPCallManagerStateInitializing;
-
- [self playTone:VoIPCallManagerTonePickup];
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setCategory:AVAudioSessionCategoryPlayAndRecord mode:AVAudioSessionModeVoiceChat options:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth|AVAudioSessionCategoryOptionAllowBluetoothA2DP error:nil];
- [session setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
-
- dispatch_async(dispatch_get_main_queue(),^{
- // Schedule 'connecting' stats timer
- VoIPStatsOptions *options = [[VoIPStatsOptions alloc] init];
- options.transport = true;
- options.inboundRtp = true;
- options.codecs = true;
- options.candidatePairsFlag = CandidatePairVariantOVERVIEW_AND_DETAILED;
- [self schedulePeriodStatsWithOptions:options period:kLogStatsIntervalConnecting];
-
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStartDebugMode object:_connection];
- });
- } else if (newState == RTCIceConnectionStateConnected) {
- if (_state != VoIPCallManagerStateReconnecting) {
- [_callDurationTimer invalidate];
- _callDurationTimer = nil;
- _callDurationTime = 0;
-
- dispatch_async(dispatch_get_main_queue(),^{
- self.callDurationTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCallDurationTime) userInfo:nil repeats:YES];
- [[NSRunLoop mainRunLoop] addTimer:self.callDurationTimer forMode:NSRunLoopCommonModes];
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStartDebugMode object:_connection];
- });
- }
- _state = VoIPCallManagerStateCalling;
- [_callPlayer stop];
- [_hangupPlayer stop];
- [_ringTonePlayer stop];
- [_problemPlayer stop];
- _changedToWebRTCAudio = NO;
- } else if (newState == RTCIceConnectionStateCompleted) {
- _state = VoIPCallManagerStateCalling;
- [_callPlayer stop];
- [_hangupPlayer stop];
- [_ringTonePlayer stop];
- [_problemPlayer stop];
-
- if (_iceCandidatesTimer && [_iceCandidatesTimer isValid]){
- [_iceCandidatesTimer invalidate];
- _iceCandidatesTimer = nil;
- }
- } else if (newState == RTCIceConnectionStateFailed) {
- [self hangup];
- } else if (newState == RTCIceConnectionStateDisconnected) {
- _state = VoIPCallManagerStateReconnecting;
- dispatch_async(dispatch_get_main_queue(),^{
- // wait 2 seconds and play sound if its still on status reconnecting
- _reconnectTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(playReconnecting:) userInfo:nil repeats:NO];
- });
- } else if (newState == RTCIceConnectionStateClosed) {
- }
-
- if (newState != RTCIceConnectionStateDisconnected) {
- dispatch_async(dispatch_get_main_queue(),^{
- [[NSNotificationCenter defaultCenter] postNotificationName:kVoIPCallStatusChanged object:[NSNumber numberWithInt:_state]];
- });
- }
-
- if (wasInitializing && (newState == RTCIceConnectionStateConnected || newState == RTCIceConnectionStateCompleted)) {
- dispatch_async(dispatch_get_main_queue(),^{
- // Schedule 'connected' stats timer
- VoIPStatsOptions *options = [[VoIPStatsOptions alloc] init];
- options.transport = true;
- options.selectedCandidatePair = true;
- options.inboundRtp = true;
- options.codecs = true;
- options.candidatePairsFlag = CandidatePairVariantOVERVIEW;
- [self schedulePeriodStatsWithOptions:options period:kLogStatsIntervalConnected];
- });
- }
- }
- /** Called any time the IceGatheringState changes. */
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didChangeIceGatheringState:(RTCIceGatheringState)newState {
- // ignore
- }
- /** New ice candidate has been found. */
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didGenerateIceCandidate:(RTCIceCandidate *)candidate {
- if ([self shouldAddCandidate:candidate]) {
- if (_isCopyIceCandidates) {
- [_tmpIceCandidates addObject:candidate];
- } else {
- if (_tmpIceCandidates.count > 0) {
- [_iceCandidates addObjectsFromArray:_tmpIceCandidates];
- [_tmpIceCandidates removeAllObjects];
- }
- [_iceCandidates addObject:candidate];
- }
-
- dispatch_async(dispatch_get_main_queue(),^{
- if (!_iceCandidatesTimer) {
- _iceCandidatesTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(sendCandidates:) userInfo:nil repeats:YES];
- }
- });
- }
- }
- - (void)sendCandidates:(NSTimer *)timer {
- if (_iceCandidates.count) {
- _isCopyIceCandidates = YES;
- NSMutableArray *candidates = _iceCandidates.copy;
- [_iceCandidates removeAllObjects];
- _isCopyIceCandidates = NO;
-
- NSMutableArray *sendCandidates = [NSMutableArray new];
- int candidatesCount = 0;
- for (int i = 0; i < candidates.count; i++) {
- candidatesCount++;
- [sendCandidates addObject:candidates[i]];
-
- if (candidates.count > 0 && (candidatesCount == 5 || i == candidates.count - 1)) {
- VoIPCallIceCandidatesMessage *message = [VoIPCallIceCandidatesMessage new];
- message.removed = NO;
- message.candidates = sendCandidates.copy;
- [VoIPSender sendVoIPCallIceCandidatesMessage:message toContact:_contact];
- [sendCandidates removeAllObjects];
- candidatesCount = 0;
- }
- }
- }
- }
- /** Called when a group of local Ice candidates have been removed. */
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didRemoveIceCandidates:(NSArray<RTCIceCandidate *> *)candidates {
- // send it only all 50 milli seconds
- VoIPCallIceCandidatesMessage *message = [VoIPCallIceCandidatesMessage new];
- message.removed = YES;
- message.candidates = candidates;
- [VoIPSender sendVoIPCallIceCandidatesMessage:message toContact:_contact];
- }
- /** New data channel has been opened. */
- - (void)peerConnection:(RTCPeerConnection *)peerConnection didOpenDataChannel:(RTCDataChannel *)dataChannel {
- // ignore
- }
- #pragma mark - AVAudioPlayerDelegate
- - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- NSError *error = nil;
- if (![[RTCAudioSession sharedInstance] setActive:NO error:&error]) {
- NSLog(@"resume music player failed, error=%@", error);
- }
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- if (![audioSession setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]) {
- NSLog(@"resume music player failed, error=%@", error);
- }
- }
- #pragma mark - RTCAudioSessionDelegate
- - (void)audioSessionDidStartPlayOrRecord:(RTCAudioSession *)session {
- if ([session.currentRoute.outputs[0].portType isEqualToString:@"Speaker"]) {
- _isSpeakerActive = YES;
- } else {
- _isSpeakerActive = NO;
- }
- _changedToWebRTCAudio = YES;
- }
- - (void)audioSessionDidStopPlayOrRecord:(RTCAudioSession *)session {
- _changedToWebRTCAudio = NO;
- }
- #pragma mark - Notifications
- - (void)handleRouteChange:(NSNotification *)notification {
- if (_changedToWebRTCAudio && _isSpeakerActive) {
- NSError *error;
- [[RTCAudioSession sharedInstance] lockForConfiguration];
- [[RTCAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionMixWithOthers|AVAudioSessionCategoryOptionAllowBluetooth error:nil];
- [[RTCAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error];
- [[RTCAudioSession sharedInstance] setActive:YES error:&error];
- [[RTCAudioSession sharedInstance] unlockForConfiguration];
- }
- }
- @end
|