Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Commit

Permalink
Feature/code cleanup (#142)
Browse files Browse the repository at this point in the history
* Cleanup of deprecated code and memory management of VSLCall and VSLRingback
  • Loading branch information
Redmer Loen authored Jun 14, 2018
1 parent 00dfdbc commit 8feba25
Show file tree
Hide file tree
Showing 13 changed files with 164 additions and 105 deletions.
14 changes: 7 additions & 7 deletions Pod/Classes/CallKitProviderDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ - (void)reportIncomingCall:(VSLCall *)call {
* "native" CallKit interface.
*/
- (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAction *)action NS_AVAILABLE_IOS(10.0) {
VSLCall *call = [self.callManager callWithUUID:action.callUUID];
__weak VSLCall *call = [self.callManager callWithUUID:action.callUUID];
if (call) {
[self.callManager.audioController configureAudioSession];

Expand Down Expand Up @@ -127,7 +127,7 @@ - (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAct
*/
- (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)action NS_AVAILABLE_IOS(10.0){
// Find call.
VSLCall *call = [self.callManager callWithUUID:action.callUUID];
__weak VSLCall *call = [self.callManager callWithUUID:action.callUUID];
if (!call) {
VSLLogInfo(@"Error hanging up call(%@). No call found", action.callUUID.UUIDString);
[action fulfill];
Expand Down Expand Up @@ -158,7 +158,7 @@ - (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)
* Delegate method called when CallKit approves the apps request to start an outbound call.
*/
- (void)provider:(CXProvider *)provider performStartCallAction:(CXStartCallAction *)action NS_AVAILABLE_IOS(10.0) {
VSLCall *call = [self.callManager callWithUUID:action.callUUID];
__weak VSLCall *call = [self.callManager callWithUUID:action.callUUID];
[self.callManager.audioController configureAudioSession];

[call startWithCompletion:^(NSError *error) {
Expand All @@ -179,7 +179,7 @@ - (void)provider:(CXProvider *)provider performStartCallAction:(CXStartCallActio
}

- (void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCallAction *)action NS_AVAILABLE_IOS(10.0) {
VSLCall *call = [self.callManager callWithUUID:action.callUUID];
__weak VSLCall *call = [self.callManager callWithUUID:action.callUUID];
if (!call) {
[action fail];
return;
Expand All @@ -196,7 +196,7 @@ - (void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCal
}

- (void)provider:(CXProvider *)provider performSetHeldCallAction:(CXSetHeldCallAction *)action NS_AVAILABLE_IOS(10.0) {
VSLCall *call = [self.callManager callWithUUID:action.callUUID];
__weak VSLCall *call = [self.callManager callWithUUID:action.callUUID];
if (!call) {
[action fail];
return;
Expand All @@ -213,7 +213,7 @@ - (void)provider:(CXProvider *)provider performSetHeldCallAction:(CXSetHeldCallA
}

- (void)provider:(CXProvider *)provider performPlayDTMFCallAction:(CXPlayDTMFCallAction *)action NS_AVAILABLE_IOS(10.0) {
VSLCall *call = [self.callManager callWithUUID:action.callUUID];
__weak VSLCall *call = [self.callManager callWithUUID:action.callUUID];
if (!call) {
[action fail];
return;
Expand Down Expand Up @@ -242,7 +242,7 @@ - (void)providerDidReset:(CXProvider *)provider NS_AVAILABLE_IOS(10.0) {
}

- (void)callStateChanged:(NSNotification *)notification {
VSLCall *call = [[notification userInfo] objectForKey:VSLNotificationUserInfoCallKey];
__weak VSLCall *call = [[notification userInfo] objectForKey:VSLNotificationUserInfoCallKey];
switch (call.callState) {
case VSLCallStateNull:
break;
Expand Down
4 changes: 2 additions & 2 deletions Pod/Classes/Configurations/VSLCodecConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

#import <Foundation/Foundation.h>

@class VSLAudioCodecs;
@class VSLVideoCodecs;
#import "VSLAudioCodecs.h"
#import "VSLVideoCodecs.h"

@interface VSLCodecConfiguration : NSObject

Expand Down
2 changes: 2 additions & 0 deletions Pod/Classes/VSLAccount.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ typedef void (^RegistrationCompletionBlock)(BOOL success, NSError * _Nullable er
*/
@property (readonly, nonatomic) VSLAccountConfiguration * _Nonnull accountConfiguration;

@property (readwrite, nonatomic) BOOL forceRegistration;

/**
* This init is not available.
*/
Expand Down
7 changes: 3 additions & 4 deletions Pod/Classes/VSLAccount.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ - (BOOL)isRegistered {
}

- (BOOL)configureWithAccountConfiguration:(VSLAccountConfiguration * _Nonnull)accountConfiguration error:(NSError **)error {

// If the endpoint has a tcp connection create a variable with the needed information.
NSString *transportString = @"";
if ([[VSLEndpoint sharedEndpoint].endpointConfiguration hasTCPConfiguration]) {
Expand Down Expand Up @@ -217,7 +216,7 @@ - (void)removeAccount {

- (void)registerAccountWithCompletion:(RegistrationCompletionBlock)completion {
VSLLogDebug(@"Account valid: %@", self.isAccountValid ? @"YES": @"NO");

VSLLogDebug(@"Should force registration: %@", self.forceRegistration ? @"YES" : @"NO");
pjsua_acc_info info;
pjsua_acc_get_info((pjsua_acc_id)self.accountId, &info);

Expand All @@ -227,7 +226,7 @@ - (void)registerAccountWithCompletion:(RegistrationCompletionBlock)completion {
// If pjsua_acc_info.expires == -1 the account has a registration but, as it turns out,
// this is not a valid check whether there is a registration in progress or not, at least,
// not wit a connection loss. So, to track a registration in progress, an ivar is used.
if (!self.registrationInProgress && info.expires == -1) {
if (self.forceRegistration || (!self.registrationInProgress && info.expires == -1)) {
self.registrationInProgress = YES;
VSLLogVerbose(@"Sending registration for account: %@", [NSNumber numberWithInteger:self.accountId]);

Expand Down Expand Up @@ -294,7 +293,7 @@ - (BOOL)unregisterAccount:(NSError * _Nullable __autoreleasing *)error {
}

- (void)reregisterAccount {
if ([[self.callManager callsForAccount:self] count] > 0) {
if ([self.callManager callsForAccount:self].count > 0) {
self.shouldReregister = YES;
[self unregisterAccount:nil];
}
Expand Down
7 changes: 3 additions & 4 deletions Pod/Classes/VSLAudioController.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,17 @@ - (void)checkCurrentThreadIsRegisteredWithPJSUA {
static pj_thread_desc a_thread_desc;
static pj_thread_t *a_thread;
if (!pj_thread_is_registered()) {
pj_thread_register(NULL, a_thread_desc, &a_thread);
pj_thread_register("VialerPJSIP", a_thread_desc, &a_thread);
}
}

- (void)activateAudioSession {
VSLLogDebug(@"Activating audiosession");
[self checkCurrentThreadIsRegisteredWithPJSUA];

pjsua_set_no_snd_dev();

pjsua_snd_dev_param snd_dev_param;
pjsua_snd_dev_param_default(&snd_dev_param);
pj_status_t status = pjsua_set_snd_dev2(&snd_dev_param);
pj_status_t status = pjsua_set_snd_dev(PJMEDIA_AUD_DEFAULT_CAPTURE_DEV, PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV);

if (status != PJ_SUCCESS) {
VSLLogWarning(@"Failure in enabling sound device");
Expand Down
96 changes: 59 additions & 37 deletions Pod/Classes/VSLCall.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ - (void)dealloc {
[[NSNotificationCenter defaultCenter] postNotificationName:VSLCallDeallocNotification
object:nil
userInfo:nil];
VSLLogVerbose(@"Dealloc Call uuid:%@ id:%ld", self.uuid.UUIDString, (long)self.callId);
VSLLogVerbose(@"Dealloc call with uuid:%@ callId:%ld", self.uuid.UUIDString, (long)self.callId);
}

#pragma mark - Properties
Expand Down Expand Up @@ -142,20 +142,26 @@ - (void)setCallState:(VSLCallState)callState {

case VSLCallStateConfirmed: {
self.connected = YES;
[self.ringback stop];
if (!self.incoming) {
// Stop ringback for outgoing calls.
[self.ringback stop];
self.ringback = nil;
}
// Register for the audio interruption notification to be able to restore the sip audio session after an interruption (incoming call/alarm....).
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruption:) name:VSLAudioControllerAudioInterrupted object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruption:) name:VSLAudioControllerAudioResumed object:nil];
// [[NSNotificationCenter defaultCenter] postNotificationName:VSLCallConnectedNotification object:nil];
} break;

case VSLCallStateDisconnected: {
[self calculateStats];
[self.ringback stop];
if (!self.incoming) {
// Stop ringback for outgoing calls.
[self.ringback stop];
self.ringback = nil;
}

[[NSNotificationCenter defaultCenter] removeObserver:self name:VSLAudioControllerAudioResumed object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:VSLAudioControllerAudioInterrupted object:nil];
// [[NSNotificationCenter defaultCenter] postNotificationName:VSLCallDisconnectedNotification object:nil];

if (self.connected && !self.userDidHangUp) {
[self.disconnectedSoundPlayer play];
Expand All @@ -164,7 +170,10 @@ - (void)setCallState:(VSLCallState)callState {
}
[self didChangeValueForKey:stringFromCallStateProperty];

NSDictionary *notificationUserInfo = @{VSLNotificationUserInfoCallKey : self};
NSDictionary *notificationUserInfo = @{
VSLNotificationUserInfoCallKey : self,
VSLNotificationUserInfoCallStateKey: [NSNumber numberWithInt:callState]
};
[[NSNotificationCenter defaultCenter] postNotificationName:VSLCallStateChangedNotification
object:nil
userInfo:notificationUserInfo];
Expand Down Expand Up @@ -367,12 +376,15 @@ - (void)updateCallInfo:(pjsua_call_info)callInfo {
self.callStateText = [NSString stringWithPJString:callInfo.state_text];
self.lastStatus = callInfo.last_status;
self.lastStatusText = [NSString stringWithPJString:callInfo.last_status_text];
self.localURI = [NSString stringWithPJString:callInfo.local_info];
self.remoteURI = [NSString stringWithPJString:callInfo.remote_info];
if (self.remoteURI) {
NSDictionary *callerInfo = [self getCallerInfoFromRemoteUri:self.remoteURI];
self.callerName = callerInfo[@"caller_name"];
self.callerNumber = callerInfo[@"caller_number"];

if (self.callState != VSLCallStateDisconnected) {
self.localURI = [NSString stringWithPJString:callInfo.local_info];
self.remoteURI = [NSString stringWithPJString:callInfo.remote_info];
if (self.remoteURI) {
NSDictionary *callerInfo = [self getCallerInfoFromRemoteUri:self.remoteURI];
self.callerName = callerInfo[@"caller_name"];
self.callerNumber = callerInfo[@"caller_number"];
}
}
}

Expand All @@ -386,7 +398,10 @@ - (void)mediaStateChanged:(pjsua_call_info)callInfo {
self.mediaState = (VSLMediaState)mediaState;

if (self.mediaState == VSLMediaStateActive || self.mediaState == VSLMediaStateRemoteHold) {
[self.ringback stop];
if (!self.incoming) {
// Stop the ringback for outgoing calls.
[self.ringback stop];
}
pjsua_conf_connect(callInfo.conf_slot, 0);
pjsua_conf_connect(0, callInfo.conf_slot);
}
Expand All @@ -405,23 +420,31 @@ - (void)checkIfAudioPresent {
pjsua_call_info callInfo;
pjsua_call_get_info((pjsua_call_id)self.callId, &callInfo);

if (self.audioCheckTimerFired > 5) {
VSLLogInfo(@"There was audio in the last 10 seconds");
[self.audioCheckTimer invalidate];
self.audioCheckTimerFired = 0;
}

if (callInfo.media_status != PJSUA_CALL_MEDIA_ACTIVE) {
VSLLogDebug(@"Unable to check if audio present no active stream!");
self.audioCheckTimerFired++;
return;
}

pj_status_t status;
pjsua_stream_stat stream_stat;
status = pjsua_call_get_stream_stat((pjsua_call_id)self.callId, callInfo.media[0].index, &stream_stat);

int rxPkt = stream_stat.rtcp.rx.pkt;
int txPkt = stream_stat.rtcp.tx.pkt;

if (rxPkt == 0 || txPkt == 0) {
VSLLogWarning(@"There is NO audio %f: sec into the call. Trying a reinvite", self.lastSeenConnectDuration);
[[NSNotificationCenter defaultCenter] postNotificationName:VSLCallNoAudioForCallNotification object:nil];
[self.audioCheckTimer invalidate];
}
if (status == PJ_SUCCESS) {
int rxPkt = stream_stat.rtcp.rx.pkt;
int txPkt = stream_stat.rtcp.tx.pkt;

if (self.audioCheckTimerFired > 5) {
VSLLogInfo(@"There is audio in the last 10 seconds rxPkt: %d and txPkt: %d", rxPkt, txPkt);
[self.audioCheckTimer invalidate];
self.audioCheckTimerFired = 0;
if (rxPkt == 0 || txPkt == 0) {
VSLLogInfo(@"There is NO audio %f: sec into the call. Trying a reinvite", self.lastSeenConnectDuration);
[[NSNotificationCenter defaultCenter] postNotificationName:VSLCallNoAudioForCallNotification object:nil];
[self.audioCheckTimer invalidate];
}
}

self.audioCheckTimerFired++;
Expand Down Expand Up @@ -762,18 +785,17 @@ - (void)calculateStats {
pjsua_call_get_info((pjsua_call_id)self.callId, &callInfo);

if (callInfo.media_status != PJSUA_CALL_MEDIA_ACTIVE) {
VSLLogError(@"Stream is not active!");
}

VSLCallStats *callStats = [[VSLCallStats alloc] initWithCall: self];
NSDictionary *stats = [callStats generate];
if ([stats count] > 0) {

self.activeCodec = stats[VSLCallStatsActiveCodec];
self.MOS = [[stats objectForKey:VSLCallStatsMOS] floatValue];
self.totalMBsUsed = [stats[VSLCallStatsTotalMBsUsed] floatValue];

VSLLogDebug(@"activeCodec: %@ with MOS score: %f and MBs used: %f", self.activeCodec, self.MOS, self.totalMBsUsed);
VSLLogDebug(@"Stream is not active!");
} else {
VSLCallStats *callStats = [[VSLCallStats alloc] initWithCall: self];
NSDictionary *stats = [callStats generate];
if ([stats count] > 0) {
self.activeCodec = stats[VSLCallStatsActiveCodec];
self.MOS = [[stats objectForKey:VSLCallStatsMOS] floatValue];
self.totalMBsUsed = [stats[VSLCallStatsTotalMBsUsed] floatValue];

VSLLogDebug(@"activeCodec: %@ with MOS score: %f and MBs used: %f", self.activeCodec, self.MOS, self.totalMBsUsed);
}
}
[self.audioCheckTimer invalidate];
}
Expand Down
27 changes: 22 additions & 5 deletions Pod/Classes/VSLCallManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ - (void)startCallToNumber:(NSString *)number forAccount:(VSLAccount *)account co
if (account.accountState != VSLAccountStateConnected) {
[account registerAccountWithCompletion:nil];
}

VSLCall *call = [[VSLCall alloc] initOutboundCallWithNumberToCall:number account:account];
[self addCall:call];

if (@available(iOS 10.0, *)) {
CXHandle *numberHandle = [[CXHandle alloc] initWithType:CXHandleTypePhoneNumber value:call.numberToCall];
CXAction *startCallAction = [[CXStartCallAction alloc] initWithCallUUID:call.uuid handle:numberHandle];

[self requestCallKitAction:startCallAction completion:^(NSError *error) {
if (error) {
VSLLogError(@"Error requesting \"Start Call Transaction\" error: %@", error);
Expand Down Expand Up @@ -203,15 +203,23 @@ - (void)requestCallKitAction:(CXAction *)action completion:(void (^)(NSError *er
- (void)addCall:(VSLCall *)call {
[self.calls addObject:call];
VSLLogVerbose(@"Call(%@) added. Calls count:%ld",call.uuid.UUIDString, (long)[self.calls count]);

}

- (void)removeCall:(VSLCall *)call {
[self.calls removeObject:call];

if ([self.calls count] == 0) {
self.calls = nil;
self.audioController = nil;
}
VSLLogVerbose(@"Call(%@) removed. Calls count: %ld",call.uuid.UUIDString, (long)[self.calls count]);
}

- (void)endAllCalls {
if ([self.calls count] == 0) {
return;
}

for (VSLCall *call in self.calls) {
VSLLogVerbose(@"Ending call: %@", call.uuid.UUIDString);
NSError *hangupError;
Expand All @@ -221,6 +229,7 @@ - (void)endAllCalls {
} else {
[self.audioController deactivateAudioSession];
}
[self removeCall:call];
}
}

Expand Down Expand Up @@ -271,6 +280,10 @@ - (VSLCall *)callWithCallId:(NSInteger)callId {
}

- (NSArray *)callsForAccount:(VSLAccount *)account {
if ([self.calls count] == 0) {
return nil;
}

NSMutableArray *callsForAccount = [[NSMutableArray alloc] init];
for (VSLCall *call in self.calls) {
if ([call.account isEqual:account]) {
Expand Down Expand Up @@ -304,6 +317,10 @@ - (VSLCall *)firstActiveCallForAccount:(VSLAccount *)account {
}

- (NSArray <VSLCall *> *)activeCallsForAccount:(VSLAccount *)account {
if ([self.calls count] == 0) {

}

NSMutableArray *activeCallsForAccount = [[NSMutableArray alloc] init];
for (VSLCall *call in self.calls) {
if (call.callState > VSLCallStateNull && call.callState < VSLCallStateDisconnected) {
Expand Down Expand Up @@ -335,7 +352,7 @@ - (void)updateActiveCallsForAccount:(VSLAccount *)account {
}

- (void)callStateChanged:(NSNotification *)notification {
VSLCall *call = [[notification userInfo] objectForKey:VSLNotificationUserInfoCallKey];
__weak VSLCall *call = [[notification userInfo] objectForKey:VSLNotificationUserInfoCallKey];
if (call.callState == VSLCallStateDisconnected) {
[self removeCall:call];
}
Expand Down
4 changes: 1 addition & 3 deletions Pod/Classes/VSLCallStats.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,13 @@ - (NSDictionary *)generate{
pjsua_call_get_info((pjsua_call_id)self.call.callId, &callInfo);

if (callInfo.media_status != PJSUA_CALL_MEDIA_ACTIVE) {
VSLLogError(@"Stream is not active!");
VSLLogDebug(@"Stream is not active!");
return stats;
}


pj_status_t status;
pjsua_stream_info stream_info;
status = pjsua_call_get_stream_info((pjsua_call_id)self.call.callId, callInfo.media[0].index, &stream_info);


if (status == PJ_SUCCESS) {
self.streamInfo = stream_info;
Expand Down
Loading

0 comments on commit 8feba25

Please sign in to comment.