Skip to content

Commit

Permalink
iOS:1v1实时通话接口封装完成
Browse files Browse the repository at this point in the history
  • Loading branch information
FuDongHai authored and bingo committed Jan 18, 2020
1 parent b57e2ca commit 84a6498
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 26 deletions.
12 changes: 6 additions & 6 deletions example/lib/pages/chat_page.dart
Expand Up @@ -482,7 +482,7 @@ class _ChatPageState extends State<ChatPage> implements EMMessageListener,ChatIt
@override
void onTapItemPhone() {
// TODO: implement onTapItemPhone
EMClient.getInstance().callManager().startCall(EMCallType.Voice, toChatUsername, false, false, "123",
EMClient.getInstance().callManager().startCall(EMCallType.Voice, toChatUsername, true, false, "123",
onSuccess:(){
print('拨打通话成功 --- ');
} ,
Expand Down Expand Up @@ -577,11 +577,11 @@ class _ChatPageState extends State<ChatPage> implements EMMessageListener,ChatIt
@override
void onDisconnected(CallReason reason) async{
// TODO: implement onDisconnected
Future.delayed(Duration(milliseconds: 500), () {
_onConversationInit();
});
var getServerRecordId = await EMClient.getInstance().callManager().getServerRecordId();
print('-----------getServerRecordId----------> '+ getServerRecordId);
// Future.delayed(Duration(milliseconds: 500), () {
// _onConversationInit();
// });
// var getServerRecordId = await EMClient.getInstance().callManager().getServerRecordId();
// print('-----------getServerRecordId----------> '+ getServerRecordId);
print('-----------EMCallStateChangeListener---------->'+ ': onDisconnected' + reason.toString());
}

Expand Down
9 changes: 5 additions & 4 deletions ios/Classes/Call/1v1/DemoCallManager.h
Expand Up @@ -22,9 +22,10 @@

- (void)_makeCallWithUsername:(NSString *)aUsername
type:(EMCallType)aType
record:(BOOL)isRecord
mergeStream:(BOOL)isMerge
ext:(NSString *)ext
isCustomVideoData:(BOOL)aIsCustomVideo;
record:(BOOL)isRecord
mergeStream:(BOOL)isMerge
ext:(NSString *)ext
isCustomVideoData:(BOOL)aIsCustomVideo
completion:(void(^)(EMCallSession *aCallSession, EMError *aError))aCompletion;

@end
8 changes: 5 additions & 3 deletions ios/Classes/Call/1v1/DemoCallManager.m
Expand Up @@ -442,6 +442,7 @@ - (void)_makeCallWithUsername:(NSString *)aUsername
mergeStream:(BOOL)isMerge
ext:(NSString *)ext
isCustomVideoData:(BOOL)aIsCustomVideo
completion:(void(^)(EMCallSession *aCallSession, EMError *aError))aCompletion
{
if ([aUsername length] == 0) {
return;
Expand Down Expand Up @@ -486,18 +487,19 @@ - (void)_makeCallWithUsername:(NSString *)aUsername
gIsCalling = NO;
[[EMClient sharedClient].callManager endCall:aCallSession.callId reason:EMCallEndReasonNoResponse];
}

};

gIsCalling = YES;
EMCallOptions *options = [[EMClient sharedClient].callManager getCallOptions];
options.enableCustomizeVideoData = aIsCustomVideo;
options.isSendPushIfOffline = YES;
[[EMClient sharedClient].callManager startCall:aType remoteName:aUsername
record:isRecord
mergeStream:isMerge
ext:ext completion:^(EMCallSession *aCallSession, EMError *aError) {
completionBlock(aCallSession, aError);
}];
completionBlock(aCallSession, aError);
aCompletion(aCallSession, aError);
}];
}

#pragma mark - public
Expand Down
238 changes: 226 additions & 12 deletions ios/Classes/EMCallManagerWrapper.m
Expand Up @@ -9,12 +9,18 @@
#import "EMSDKMethod.h"
#import "EM1v1CallViewController.h"
#import "DemoCallManager.h"
#import "EMHelper.h"

@interface EMCallManagerWrapper () <EMCallManagerDelegate>
@property (nonatomic, strong) EMCallSession *callSession;
@end

@implementation EMCallManagerWrapper
- (instancetype)initWithChannelName:(NSString *)aChannelName
registrar:(NSObject<FlutterPluginRegistrar>*)registrar {
if(self = [super initWithChannelName:aChannelName
registrar:registrar]) {
[EMClient.sharedClient.callManager addDelegate:self delegateQueue:nil];
}
return self;
}
Expand All @@ -25,31 +31,239 @@ - (void)handleMethodCall:(FlutterMethodCall*)call
result:(FlutterResult)result {
if ([EMMethodKeyStartCall isEqualToString:call.method]) {
[self startCall:call.arguments result:result];
} else if ([EMMethodKeySetCallOptions isEqualToString:call.method]) {
[self setCallOptions:call.arguments result:result];
} else if ([EMMethodKeyRegisterCallReceiver isEqualToString:call.method]) {
[self registerCallReceiver:call.arguments result:result];
} else if ([EMMethodKeyGetCallId isEqualToString:call.method]) {
[self getCallId:call.arguments result:result];
} else if ([EMMethodKeyGetConnectType isEqualToString:call.method]) {
[self getConnectType:call.arguments result:result];
} else if ([EMMethodKeyGetExt isEqualToString:call.method]) {
[self getExt:call.arguments result:result];
} else if ([EMMethodKeyGetLocalName isEqualToString:call.method]) {
[self getLocalName:call.arguments result:result];
} else if ([EMMethodKeyGetRemoteName isEqualToString:call.method]) {
[self getRemoteName:call.arguments result:result];
} else if ([EMMethodKeyGetServerRecordId isEqualToString:call.method]) {
[self getServerRecordId:call.arguments result:result];
} else if ([EMMethodKeyGetCallType isEqualToString:call.method]) {
[self getCallType:call.arguments result:result];
} else if ([EMMethodKeyIsRecordOnServer isEqualToString:call.method]) {
[self isRecordOnServer:call.arguments result:result];
} else {
[super handleMethodCall:call result:result];
}
}

#pragma mark - Actions

- (void)startCall:(NSDictionary *)param result:(FlutterResult)result {
EMCallType callType = [param[@"callType"] intValue];
NSString *remoteName = param[@"remoteName"];
BOOL isRecord = [param[@"record"] boolValue];
BOOL isMerge = [param[@"mergeStream"] boolValue];
NSString *ext = param[@"ext"];

__weak typeof(self) weakSelf = self;
[[DemoCallManager sharedManager] _makeCallWithUsername:remoteName type:callType record:isRecord mergeStream:isMerge ext:ext isCustomVideoData:NO completion:^(EMCallSession *aCallSession, EMError *aError) {
weakSelf.callSession = aCallSession;
}];
}

- (void)setCallOptions:(NSDictionary *)param result:(FlutterResult)result {
[[EMClient sharedClient].callManager setCallOptions:[EMHelper callOptionsDictionaryToEMCallOptions:param]];
}

- (void)registerCallReceiver:(NSDictionary *)param result:(FlutterResult)result {

[[DemoCallManager sharedManager] _makeCallWithUsername:remoteName type:callType record:isRecord mergeStream:isMerge ext:ext isCustomVideoData:NO];
}

- (void)getCallId:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
NSString *callId = self.callSession.callId;
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":callId}];
}
}

- (void)getConnectType:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
EMCallConnectType connectType = self.callSession.connectType;
int type;
if (connectType == EMCallConnectTypeNone) {
type = 0;
} else if (connectType == EMCallConnectTypeDirect) {
type = 1;
} else {
type = 2;
}
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":[NSNumber numberWithInt:type]}];
}
}

- (void)getExt:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
NSString *ext = self.callSession.ext;
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":ext}];
}
}

- (void)getLocalName:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
NSString *localName = self.callSession.localName;
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":localName}];
}
}

- (void)getRemoteName:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
NSString *remoteName = self.callSession.remoteName;
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":remoteName}];
}
}

- (void)getServerRecordId:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
NSString *serverVideoId = self.callSession.serverVideoId;
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":serverVideoId}];
}
}

- (void)getCallType:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
EMCallType callType = self.callSession.type;
int type;
if (callType == EMCallTypeVoice) {
type = 0;
} else {
type = 1;
}
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":[NSNumber numberWithInt:type]}];
}
}

- (void)isRecordOnServer:(NSDictionary *)param result:(FlutterResult)result {
if (self.callSession) {
BOOL willRecord = self.callSession.willRecord;
[self wrapperCallBack:result
error:nil
userInfo:@{@"value":[NSNumber numberWithBool:willRecord]}];
}
}

#pragma mark - EMCallManagerDelegate

- (void)callDidReceive:(EMCallSession *)aSession {
NSDictionary *map = @{
@"type":@"connecting"
};
[self.channel invokeMethod:EMMethodKeyOnCallChanged
arguments:map];
}

- (void)callDidConnect:(EMCallSession *)aSession {
NSDictionary *map = @{
@"type":@"connected"
};
[self.channel invokeMethod:EMMethodKeyOnCallChanged
arguments:map];
}

- (void)callDidAccept:(EMCallSession *)aSession {
NSDictionary *map = @{
@"type":@"accepted"
};
[self.channel invokeMethod:EMMethodKeyOnCallChanged
arguments:map];
}

- (void)callDidEnd:(EMCallSession *)aSession
reason:(EMCallEndReason)aReason
error:(EMError *)aError {
NSDictionary *map = @{
@"type":@"disconnected",
@"reason":[NSNumber numberWithInt:[self callEndReasonToInt:aReason]],
};
[self.channel invokeMethod:EMMethodKeyOnCallChanged
arguments:map];
}

- (void)callStateDidChange:(EMCallSession *)aSession
type:(EMCallStreamingStatus)aType {
NSString *type;
if (aType == EMCallStreamStatusVoicePause) {
type = @"netVoicePause";
} else if (aType == EMCallStreamStatusVoiceResume) {
type = @"netVoiceResume";
} else if (aType == EMCallStreamStatusVideoPause) {
type = @"netVideoPause";
} else {
type = @"netVideoResume";
}
NSDictionary *map = @{
@"type":type
};
[self.channel invokeMethod:EMMethodKeyOnCallChanged
arguments:map];
}

- (void)callNetworkDidChange:(EMCallSession *)aSession
status:(EMCallNetworkStatus)aStatus {
NSString *type;
if (aStatus == EMCallNetworkStatusNormal) {
type = @"netWorkNormal";
} else if (aStatus == EMCallNetworkStatusUnstable) {
type = @"networkUnstable";
} else {
type = @"netWorkDisconnected";
}

// [[EMClient sharedClient].callManager startCall:callType
// remoteName:remoteName
// record:isRecord
// mergeStream:isMerge
// ext:ext
// completion:^(EMCallSession *aCallSession, EMError *aError) {
//
// [self wrapperCallBack:result
// error:aError
// userInfo:nil];
// }];
NSDictionary *map = @{
@"type":type
};
[self.channel invokeMethod:EMMethodKeyOnCallChanged
arguments:map];
}

// 通话结束原因
- (int)callEndReasonToInt:(EMCallEndReason)aReason {
int reason;
if (aReason == EMCallEndReasonHangup) {
reason = 0;
} else if (aReason == EMCallEndReasonNoResponse) {
reason = 1;
} else if (aReason == EMCallEndReasonDecline) {
reason = 2;
} else if (aReason == EMCallEndReasonBusy) {
reason = 3;
} else if (aReason == EMCallEndReasonFailed) {
reason = 4;
} else if (aReason == EMCallEndReasonRemoteOffline) {
reason = 5;
} else if (aReason == EMCallEndReasonNotEnable) {
reason = 101;
} else if (aReason == EMCallEndReasonServiceArrearages) {
reason = 102;
} else if (aReason == EMCallEndReasonServiceForbidden) {
reason = 103;
} else {
reason = -1;
}
return reason;
}

@end
5 changes: 5 additions & 0 deletions ios/Classes/EMHelper.h
Expand Up @@ -47,6 +47,11 @@ NS_ASSUME_NONNULL_BEGIN

+ (NSArray *)chatRoomsToDictionaries:(NSArray *)chatRooms;

#pragma mark - ChatRoom

+ (EMCallOptions *)callOptionsDictionaryToEMCallOptions:(NSDictionary *)options;


#pragma mark - Others

+ (NSDictionary *)pageReslutToDictionary:(EMPageResult *)pageResult;
Expand Down
22 changes: 21 additions & 1 deletion ios/Classes/EMHelper.m
Expand Up @@ -489,7 +489,6 @@ + (NSDictionary *)chatRoomToDictionary:(EMChatroom *)aChatRoom {
chatRoomDitc[@"blacklist"] = aChatRoom.blacklist;
}
if (aChatRoom.muteList) {
NSMutableArray *muteList = [NSMutableArray array];
chatRoomDitc[@"muteList"] = aChatRoom.muteList;
}
if (aChatRoom.maxOccupantsCount) {
Expand All @@ -510,6 +509,27 @@ + (NSArray *)chatRoomsToDictionaries:(NSArray *)chatRooms {
}
return ret;
}

#pragma mark - ChatRoom
+ (EMCallOptions *)callOptionsDictionaryToEMCallOptions:(NSDictionary *)optionsDict
{
EMCallOptions *options = [[EMClient sharedClient].callManager getCallOptions];
options.isSendPushIfOffline = [optionsDict[@"isSendPushIfOffline"] boolValue];

if ([optionsDict[@"userSetAutoResizing"] boolValue]) {
options.videoResolution = EMCallVideoResolutionDefault;
} else {
options.videoResolution = EMCallVideoResolution1280_720;
}
options.pingInterval = [optionsDict[@"pingInterval"] intValue];
options.maxVideoKbps = [optionsDict[@"maxVideoKbps"] intValue];
options.minVideoKbps = [optionsDict[@"minVideoKbps"] intValue];
options.maxVideoFrameRate = [optionsDict[@"maxVideoFrameRate"] intValue];
options.maxAudioKbps = [optionsDict[@"maxAudioKbps"] intValue];

return options;
}

#pragma mark - Others

+ (NSDictionary *)pageReslutToDictionary:(EMPageResult *)aPageResult {
Expand Down

0 comments on commit 84a6498

Please sign in to comment.