2
0

WFCCallKitManager.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // WFCCallKitManager.m
  3. // WildFireChat
  4. //
  5. // Created by Rain on 2022/4/25.
  6. // Copyright © 2022 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCCallKitManager.h"
  9. #if WFCU_SUPPORT_VOIP
  10. #import <CallKit/CallKit.h>
  11. #import <UIKit/UIKit.h>
  12. #import <WFChatUIKit/WFChatUIKit.h>
  13. @interface WFCCallKitManager ()
  14. @property(nonatomic, strong) CXProvider *provider;
  15. @property(nonatomic, strong) CXCallController *callController;
  16. @property(nonatomic, strong) NSMutableDictionary<NSString *, NSUUID *> *callUUIDDict;
  17. @end
  18. @implementation WFCCallKitManager
  19. - (instancetype)init {
  20. self = [super init];
  21. if(self) {
  22. #if USE_CALL_KIT
  23. self.callUUIDDict = [[NSMutableDictionary alloc] init];
  24. static CXProviderConfiguration* configInternal = nil;
  25. configInternal = [[CXProviderConfiguration alloc] initWithLocalizedName:@"野火"];
  26. configInternal.supportsVideo = true;
  27. configInternal.maximumCallsPerCallGroup = 1;
  28. configInternal.maximumCallGroups = 1;
  29. configInternal.supportedHandleTypes = [[NSSet alloc] initWithObjects:[NSNumber numberWithInt:CXHandleTypeGeneric],[NSNumber numberWithInt:CXHandleTypePhoneNumber], nil];
  30. UIImage* iconMaskImage = [UIImage imageNamed:@"file_icon"];
  31. configInternal.iconTemplateImageData = UIImagePNGRepresentation(iconMaskImage);
  32. self.provider = [[CXProvider alloc] initWithConfiguration: configInternal];
  33. [self.provider setDelegate:self queue:nil];
  34. self.callController = [[CXCallController alloc] initWithQueue:dispatch_get_main_queue()];
  35. #endif
  36. }
  37. return self;
  38. }
  39. - (void)didReceiveCall:(WFAVCallSession *)session {
  40. if(self.callUUIDDict[session.callId]) {
  41. session.callUUID = self.callUUIDDict[session.callId];
  42. } else {
  43. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:session.initiator refresh:NO];
  44. NSUUID *currentCall = [NSUUID UUID];
  45. session.callUUID = currentCall;
  46. [self reportIncomingCallWithTitle:userInfo.displayName Sid:session.initiator audioOnly:session.audioOnly callId:session.callId uuid:currentCall];
  47. }
  48. }
  49. - (void)didCallEnded:(WFAVCallEndReason)reason duration:(int)callDuration {
  50. if([WFAVEngineKit sharedEngineKit].currentSession.callUUID) {
  51. CXEndCallAction *endAction = [[CXEndCallAction alloc] initWithCallUUID:[WFAVEngineKit sharedEngineKit].currentSession.callUUID];
  52. CXTransaction *transaction = [[CXTransaction alloc] initWithAction:endAction];
  53. [self.callController requestTransaction:transaction completion:^(NSError * _Nullable error) {
  54. NSLog(@"end call");
  55. }];
  56. }
  57. }
  58. - (void)didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
  59. forType:(NSString *)type {
  60. NSLog(@"didReceiveIncomingPushWithPayload");
  61. NSDictionary *wfc = payload.dictionaryPayload[@"wfc"];
  62. if(wfc) {
  63. NSString *sender = wfc[@"sender"];
  64. NSString *pushData = wfc[@"pushData"];
  65. NSDictionary *pd = [NSJSONSerialization JSONObjectWithData:[pushData dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
  66. BOOL audioOnly = [pd[@"audioOnly"] boolValue];
  67. NSString *callId = pd[@"callId"];
  68. WFCCUserInfo *userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:sender refresh:NO];
  69. [self reportIncomingCallWithTitle:userInfo.displayName Sid:sender audioOnly:audioOnly callId:callId];
  70. }
  71. }
  72. - (void)reportIncomingCallWithTitle:(NSString *)title Sid:(NSString *)sid audioOnly:(BOOL)audioOnly callId:(NSString *)callId {
  73. NSUUID *currentCall = [NSUUID UUID];
  74. [self reportIncomingCallWithTitle:title Sid:sid audioOnly:audioOnly callId:callId uuid:currentCall];
  75. }
  76. - (void)reportIncomingCallWithTitle:(NSString *)title Sid:(NSString *)sid audioOnly:(BOOL)audioOnly callId:(NSString *)callId uuid:(NSUUID *)uuid {
  77. CXCallUpdate* update = [[CXCallUpdate alloc] init];
  78. update.supportsDTMF = false;
  79. update.supportsHolding = false;
  80. update.supportsGrouping = false;
  81. update.supportsUngrouping = false;
  82. update.hasVideo = !audioOnly;
  83. update.remoteHandle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:sid];
  84. update.localizedCallerName = title;
  85. self.callUUIDDict[callId] = uuid;
  86. [[WFAVEngineKit sharedEngineKit] registerCall:callId uuid:uuid];
  87. [self.provider reportNewIncomingCallWithUUID:uuid update:update completion:^(NSError * _Nullable error) {
  88. if(error) {
  89. NSLog(@"error:%@", error);
  90. }
  91. }];
  92. }
  93. #pragma - mark CXProviderDelegate
  94. - (void)providerDidReset:(CXProvider *)provider {
  95. NSLog(@"providerDidReset");
  96. }
  97. - (void)provider:(CXProvider *)provider performStartCallAction:(CXStartCallAction *)action {
  98. NSLog(@"performStartCallAction");
  99. }
  100. - (void)provider:(CXProvider *)provider performAnswerCallAction:(CXAnswerCallAction *)action {
  101. NSLog(@"performAnswerCallAction");
  102. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
  103. [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
  104. [action fulfill];
  105. }
  106. - (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)action {
  107. NSLog(@"performEndCallAction");
  108. if([action.callUUID isEqual:[WFAVEngineKit sharedEngineKit].currentSession.callUUID]) {
  109. if ([WFAVEngineKit sharedEngineKit].currentSession.state != kWFAVEngineStateIdle) {
  110. [[WFAVEngineKit sharedEngineKit].currentSession endCall];
  111. }
  112. }
  113. [action fulfill];
  114. }
  115. - (void)provider:(CXProvider *)provider performSetMutedCallAction:(CXSetMutedCallAction *)action {
  116. if([action.callUUID isEqual:[WFAVEngineKit sharedEngineKit].currentSession.callUUID]) {
  117. if ([WFAVEngineKit sharedEngineKit].currentSession.state != kWFAVEngineStateIdle) {
  118. if (action.isMuted) {
  119. [[WFAVEngineKit sharedEngineKit].currentSession muteAudio:YES];
  120. } else {
  121. [[WFAVEngineKit sharedEngineKit].currentSession muteAudio:NO];
  122. }
  123. }
  124. }
  125. [action fulfill];
  126. }
  127. - (void)answerCall {
  128. [[WFAVEngineKit sharedEngineKit].currentSession answerCall:false callExtra:nil];
  129. UIViewController *videoVC;
  130. if ([WFAVEngineKit sharedEngineKit].currentSession.conversation.type == Group_Type && [WFAVEngineKit sharedEngineKit].supportMultiCall) {
  131. videoVC = [[WFCUMultiVideoViewController alloc] initWithSession:[WFAVEngineKit sharedEngineKit].currentSession];
  132. } else {
  133. videoVC = [[WFCUVideoViewController alloc] initWithSession:[WFAVEngineKit sharedEngineKit].currentSession];
  134. }
  135. if([[WFAVEngineKit sharedEngineKit].currentSession isAudioOnly]) {
  136. [[WFAVEngineKit sharedEngineKit].currentSession enableSpeaker:NO];
  137. } else {
  138. [[WFAVEngineKit sharedEngineKit].currentSession enableSpeaker:YES];
  139. }
  140. [[WFAVEngineKit sharedEngineKit] presentViewController:videoVC];
  141. }
  142. -(void)provider:(CXProvider *)provider didActivateAudioSession:(AVAudioSession *)audioSession {
  143. if ([WFAVEngineKit sharedEngineKit].currentSession.state == kWFAVEngineStateIncomming) {
  144. [self answerCall];
  145. } else {
  146. //有可能用户点击接听以后,IM服务消息还没有同步完成,来电消息还没有被处理,需要等待有来电session再接听.
  147. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  148. int count = 60;
  149. while (count--) {
  150. [NSThread sleepForTimeInterval:0.05];
  151. if ([WFAVEngineKit sharedEngineKit].currentSession.state == kWFAVEngineStateIncomming) {
  152. dispatch_async(dispatch_get_main_queue(), ^{
  153. [self answerCall];
  154. });
  155. break;
  156. }
  157. }
  158. //3秒内没有接听成功,这里设置为失败
  159. [self.provider invalidate];
  160. });
  161. }
  162. }
  163. @end
  164. #endif