Skip to content

Commit

Permalink
Merge pull request #7 from 374139544/main
Browse files Browse the repository at this point in the history
修复bug
  • Loading branch information
lixm1988 authored Jul 4, 2022
2 parents 5428920 + afe421a commit c464fb0
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 95 deletions.
26 changes: 26 additions & 0 deletions Classes/Process/AgoraChatCallKitModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// AgoraChatCallKitModel.h
// AgoraChatCallKit
//
// Created by 冯钊 on 2022/6/22.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSUInteger, AgoraChatCallKitModelHandleType) {
AgoraChatCallKitModelHandleTypeUnhandle,
AgoraChatCallKitModelHandleTypeAccept,
AgoraChatCallKitModelHandleTypeRefuse,
};

@interface AgoraChatCallKitModel : NSObject

@property (nonatomic, strong) NSString *unhandleCallId;
@property (nonatomic, assign) AgoraChatCallKitModelHandleType handleType;
@property (nonatomic, strong) dispatch_block_t timeoutBlock;

@end

NS_ASSUME_NONNULL_END
12 changes: 12 additions & 0 deletions Classes/Process/AgoraChatCallKitModel.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// AgoraChatCallKitModel.m
// AgoraChatCallKit
//
// Created by 冯钊 on 2022/6/22.
//

#import "AgoraChatCallKitModel.h"

@implementation AgoraChatCallKitModel

@end
9 changes: 8 additions & 1 deletion Classes/Process/AgoraChatCallManager+CallKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
//

#import "AgoraChatCallManager.h"
#import "AgoraChatCallKitModel.h"

@import PushKit;
@class AgoraChatCall;

NS_ASSUME_NONNULL_BEGIN

@interface AgoraChatCallManager (CallKit) <CXProviderDelegate>
@interface AgoraChatCallManager (CallKit) <CXProviderDelegate, PKPushRegistryDelegate>

- (void)initCallKit;
- (void)requestPushKitToken;
- (void)didRecvCancelMessage:(NSString *)callId;

- (void)reportNewIncomingCall:(AgoraChatCall *)call;
- (void)reportCallEnd:(AgoraChatCall *)call reason:(AgoarChatCallEndReason)reason;

- (AgoraChatCallKitModel *)getUnhandleCall;
- (void)clearUnhandleCall;

@end

NS_ASSUME_NONNULL_END
133 changes: 117 additions & 16 deletions Classes/Process/AgoraChatCallManager+CallKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#import "UIImage+Ext.h"

@import CallKit;
@import AgoraChat;

static NSUUID *callKitCurrentCallUUID;
static NSString *pushKitRecvCallId;
static AgoraChatCallKitModel *callKitModel;

@implementation AgoraChatCallManager (CallKit)

Expand All @@ -33,12 +38,26 @@ - (void)initCallKit
self.callController = [[CXCallController alloc] initWithQueue:dispatch_get_main_queue()];
}

- (void)requestPushKitToken
{
PKPushRegistry *pushKit = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
pushKit.delegate = self;
pushKit.desiredPushTypes = [NSSet setWithObjects:PKPushTypeVoIP, nil];
}

- (void)didRecvCancelMessage:(NSString *)callId
{
if ([callId isEqualToString:pushKitRecvCallId]) {
[self reportCallEndWithReason:CXCallEndedReasonUnanswered];
}
}

- (void)reportNewIncomingCall:(AgoraChatCall *)call
{
if (![self getAgoraChatCallConfig].enableIosCallKit) {
return;
}
if (self.callKitCurrentCallReportNewIncoming) {
if (pushKitRecvCallId) {
return;
}
CXHandle *handle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:call.remoteUserAccount];
Expand All @@ -50,12 +69,12 @@ - (void)reportNewIncomingCall:(AgoraChatCall *)call
update.supportsDTMF = NO;
update.hasVideo = NO;
update.localizedCallerName = call.remoteUserAccount;
if (self.callKitCurrentCallUUID) {
[self.provider reportCallWithUUID:self.callKitCurrentCallUUID endedAtDate:nil reason:CXCallEndedReasonUnanswered];
if (callKitCurrentCallUUID) {
[self.provider reportCallWithUUID:callKitCurrentCallUUID endedAtDate:nil reason:CXCallEndedReasonUnanswered];
}

self.callKitCurrentCallUUID = [NSUUID UUID];
[self.provider reportNewIncomingCallWithUUID:self.callKitCurrentCallUUID update:update completion:^(NSError * _Nullable error) {
callKitCurrentCallUUID = [NSUUID UUID];
[self.provider reportNewIncomingCallWithUUID:callKitCurrentCallUUID update:update completion:^(NSError * _Nullable error) {
NSLog(@"%@", error);
}];
}
Expand All @@ -65,7 +84,7 @@ - (void)reportCallEnd:(AgoraChatCall *)call reason:(AgoarChatCallEndReason)reaso
if (![self getAgoraChatCallConfig].enableIosCallKit) {
return;
}
if (!self.callKitCurrentCallUUID) {
if (!callKitCurrentCallUUID) {
return;
}
CXCallEndedReason callKitReason;
Expand All @@ -83,10 +102,21 @@ - (void)reportCallEnd:(AgoraChatCall *)call reason:(AgoarChatCallEndReason)reaso
callKitReason = CXCallEndedReasonUnanswered;
break;
}
[self reportCallEndWithReason:callKitReason];
}

- (void)reportCallEndWithReason:(CXCallEndedReason)reason
{
if (![self getAgoraChatCallConfig].enableIosCallKit) {
return;
}
if (!callKitCurrentCallUUID) {
return;
}

[self.provider reportCallWithUUID:self.callKitCurrentCallUUID endedAtDate:nil reason:callKitReason];
self.callKitCurrentCallUUID = nil;
self.callKitCurrentCallReportNewIncoming = NO;
[self.provider reportCallWithUUID:callKitCurrentCallUUID endedAtDate:nil reason:reason];
callKitCurrentCallUUID = nil;
pushKitRecvCallId = nil;
}

- (void)providerDidReset:(CXProvider *)provider
Expand All @@ -96,18 +126,24 @@ - (void)providerDidReset:(CXProvider *)provider

- (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAction *)action
{
if ([self.callKitCurrentCallUUID isEqual:action.callUUID]) {
[self acceptAction];
if ([callKitCurrentCallUUID isEqual:action.callUUID]) {
if (callKitModel && ![self checkCallIdCanHandle:callKitModel.unhandleCallId]) {
callKitModel.handleType = AgoraChatCallKitModelHandleTypeAccept;
} else {
[self acceptAction];
}
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[action fulfill];
});
[action fulfill];
}

- (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)action
{
if ([self.callKitCurrentCallUUID isEqual:action.callUUID]) {
[self hangupAction];
if ([callKitCurrentCallUUID isEqual:action.callUUID]) {
if (callKitModel && ![self checkCallIdCanHandle:callKitModel.unhandleCallId]) {
callKitModel.handleType = AgoraChatCallKitModelHandleTypeRefuse;
} else {
[self hangupAction];
}
}
[action fulfill];
}
Expand All @@ -125,4 +161,69 @@ - (void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCal
// [action fulfill];
//}

#pragma mark - PKPushRegistryDelegate
- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)pushCredentials forType:(PKPushType)type
{
[AgoraChatClient.sharedClient registerPushKitToken:pushCredentials.token completion:^(AgoraChatError *aError) {
if (aError) {
NSLog(@"AgoraChatClient registerPushKitToken error: %@", aError.description);
}
}];
}

- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(PKPushType)type
{
NSLog(@"PushKit %s type=%d", __FUNCTION__, type);
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion
{
NSString *from = payload.dictionaryPayload[@"f"];
NSDictionary *custom = payload.dictionaryPayload[@"e"];
NSString *callId = custom[@"callId"];
CXHandle *handle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:from];
CXCallUpdate *update = [[CXCallUpdate alloc] init];
update.remoteHandle = handle;
update.supportsHolding = NO;
update.supportsGrouping = NO;
update.supportsUngrouping = NO;
update.supportsDTMF = NO;
update.hasVideo = NO;
update.localizedCallerName = from;

pushKitRecvCallId = callId;
callKitModel = [[AgoraChatCallKitModel alloc] init];
callKitModel.unhandleCallId = callId;
callKitModel.handleType = AgoraChatCallKitModelHandleTypeUnhandle;
__weak typeof(self)weakSelf = self;
callKitModel.timeoutBlock = dispatch_block_create(0, ^{
[weakSelf reportCallEndWithReason:CXCallEndedReasonUnanswered];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)), dispatch_get_main_queue(), callKitModel.timeoutBlock);

callKitCurrentCallUUID = NSUUID.UUID;
[self.provider reportNewIncomingCallWithUUID:callKitCurrentCallUUID update:update completion:^(NSError * _Nullable error) {
NSLog(@"%@", error);
}];
completion();
}

- (AgoraChatCallKitModel *)getUnhandleCall
{
if (callKitModel && callKitModel.handleType != AgoraChatCallKitModelHandleTypeUnhandle) {
return callKitModel;
}
[self clearUnhandleCall];
return nil;
}

- (void)clearUnhandleCall
{
if (callKitModel.timeoutBlock) {
dispatch_block_cancel(callKitModel.timeoutBlock);
callKitModel.timeoutBlock = nil;
}
callKitModel = nil;
}

@end
2 changes: 2 additions & 0 deletions Classes/Process/AgoraChatCallManager+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- (void)inviteAction;
- (void)muteAudio:(BOOL)muted;
- (void)speakeOut:(BOOL)enable;
- (BOOL)speakeOut;
- (NSString *)getNicknameByUserName:(NSString *)aUserName;
- (NSURL *)getHeadImageByUserName:(NSString *)aUserName;
- (NSString *)getUserNameByUid:(NSNumber *)uId;
Expand All @@ -25,5 +26,6 @@
- (void)setupRemoteVideoView:(NSUInteger)uid withDisplayView:(UIView *)view;
- (void)joinChannel;
- (void)switchToVoice;
- (BOOL)checkCallIdCanHandle:(NSString *)callId;

@end /* AgoraChatCallManager_Private_h */
2 changes: 0 additions & 2 deletions Classes/Process/AgoraChatCallManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ extern NSNotificationName const AGORA_CHAT_CALL_KIT_COMMMUNICATE_RECORD;

@property (nonatomic, strong, nullable) CXProvider *provider;
@property (nonatomic, strong, nullable) CXCallController *callController;
@property (nonatomic, strong) NSUUID *callKitCurrentCallUUID;
@property (nonatomic, assign) BOOL callKitCurrentCallReportNewIncoming;

/**
* \~chinese
Expand Down
Loading

0 comments on commit c464fb0

Please sign in to comment.