SendBirdVideoChat 0.9.2

SendBirdVideoChat 0.9.2

Jed Gyeong 维护。



  • Jed Gyeong 和 Young Hwang

SendBird VideoChat for iOS

Platform Languages CocoaPods Commercial License

SendBird VideoChat 是一种附加到您的应用程序的产品,它使得用户能够进行视频和音频通话。SendBird VideoChat 通过 WebRTC 提供。

注意:这是一个测试版,目前不对所有用户开放。如果您想尝试 SendBird VideoChat,请联系我们的 销售支持 获取更多信息。

内容

如何设置和初始化 VideoChat

以下展示了如何通过 CocoaPodsCarthage 安装 SendBird VideoChat 框架,以及如何在您的应用中初始化它。

系统需求

CocoaPods

将以下行添加到您项目目录中的 Podfile 文件。

pod 'SendBirdSDK'
pod 'SendBirdWebRTC', :git => 'https://github.com/sendbird/SendBird-WebRTC-iOS.git', :tag => 'v1.0'
pod 'SendBirdVideoChat'

通过 CocoaPods 安装 SendBirdVideoChat 框架。

pod install

现在您可以通过检查 YOUR_PROJECT.xcworkspace 来查看已安装的 SendBirdVideoChat 框架。

Carthage

将以下行添加到您的 Cartfile

github "sendbird/sendbird-webrtc-ios.git"
github "sendbird/sendbird-ios-framework"
github "sendbird/sendbird-videochat-ios"

通过 Carthage 安装框架。

carthage update

前往 Xcode 项目的“通用”设置。在 Finder 中打开 <YOUR_XCODE_PROJECT_DIRECTORY>/Carthage/Build/iOS,并将 WebRTC.frameworkSendBirdSDK.frameworkSendBirdVideoChat.framework 拖到 Xcode 的“已打包产品”部分的“嵌入二进制文件”中。确保选择了“如果需要则复制”并点击“完成”。

AppDelegate.hAppDelegate.m 在应用中

要初始化 SendBirdVideoChat 框架,将以下行添加到您的 AppDelegate.m 文件。

// AppDelegate.h

#import <SendBirdSDK/SendBirdSDK.h>
#import <SendBirdVideoChat/SendBirdVideoChat.h>
#import <WebRTC/WebRTC.h>

// ...
// AppDelegate.m
#import "AppDelegate.h"

// ...

@interface AppDelegate ()

// ...

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // ...

  [SBVCMain config]

  // ...
}

限制

SendBird VideoChat 测试版有一些限制。

  • 仅在有 普通私有 群组设置下的频道中可用。(非超级和非公开)
  • 仅支持两个用户之间的视频和音频通话。
  • 要开始视频和音频通话,两个用户都必须连接到 SendBird 服务器。
  • 在相同的频道中,您无法启动新的、独立的视频或音频通话。

警告:请不要调用 SendBird iOS SDK 中的 removeAllChannelDelegates。这不仅会移除已经添加的委托,还会移除由 SendBirdVideoChat 管理的委托。

制作视频和音频通话

您可以使用 SendBirdVideoChat 的方法在两个用户之间进行视频和音频通话。

startCallWithAudioOnly:...

startCallWithAudioOnly:options:completionHandler: 会向 SendBird 服务器发送带指定选项的视频或音频通话请求。此函数需要 audioOnlyoptions 参数。

  • audioOnly:确定是否在通话中启用仅音频或视频和音频。
  • options:使用 SBVCCallOptions 实例指定通话设置。有关设置的更多信息,请参阅 SBVCCallOptions
[SBVCMain startCallWithAudioOnly:isAudioOnly options:options completionHandler:^(SBDError * _Nullable error) {

}];

如果服务器接受请求并通知被叫方,调用者将通过 SBVCVideoChatDelegatedidSendStartCall:withMessage: 收到成功回调。被叫方也可以通过 SBVCVideoChatDelegatedidReceiveStartCall: 收到请求的通知。

注册/注销事件委托

与 SendBird SDK 相似,SBVCVideoChatDelegate 有自己的事件委托。该委托支持各种从 SendBird 服务器接收事件回调的方法。您可以在应用中创建、注册并实现该委托方法。然后,您的应用将会被通知视频和音频通话中发生的事件。

// YourViewController.h
#import <SendBirdVideoChat/SendBirdVideoChat.h>

@interface YourViewController : UIViewController<SBVCVideoChatDelegate>

// ...

@end
// YourViewController.m
#import "YourViewController.h"

@implementation YourViewController

// ...

- (void)viewDidLoad {
  // ..

  [SBVCMain addVideoChatDelegate:self identifier:IDENTIFIER];
  
  // ..
}

#pragma mark - SBVCVideoChatDelegate
- (void)didSendStartCall:(SBVCCall *)call withMessage:(SBDUserMessage *)message {

}

- (void)didReceiveStartCall:(SBVCCall *)call {

}

- (void)didSendAcceptCall:(SBVCCall *)call {

}

- (void)didReceiveAcceptCall:(SBVCCall *)call {

}

- (void)didSendEndCall:(SBVCCall *)call message:(SBDUserMessage *)message {

}

- (void)didReceiveEndCall:(nonnull SBVCCall *)call {

}

- (void)didOpenCall:(SBVCCall *)call {

}

- (void)didOpponentAudioStateChange:(nonnull SBVCCall *)call {

}

- (void)didOpponentVideoStateChange:(nonnull SBVCCall *)call {

}

// ...

// If you unregister SBVCVideoChatDelegate, use as follows.  
// [SBVCMain removeVideoChatDelegateWithIdentifier:IDENTIFIER];
// [SBVCMain removeAllVideoChatDelegates];

@end

startCallWithAudioOnly:... 的超时

如果被叫方在 30 秒(默认)内未对调用者的通话请求做出响应,则 SendBirdVideoChat 会自动关闭通话。超时值可设置在 30 到 60 秒之间。

[SBVCMain setStartCallTimeout:40];

获取 SBVCCall 实例

SendBirdVideoChat 提供了一个表示特定的视频或音频通话的 SBVCCall 类,它有自己的属性和方法。有关该对象的信息,请参阅 SBVCCall

  • getActiveCall:检索当前正在进行中的 SBVCCall 实例。
  • getCallWithCallId:通过传递的调用 ID 获取 SBVCCall 实例。
  • buildCallFromNotification:利用推送通知消息中的 SendBird 有效载荷,创建并返回一个新的 'SBVCCall' 实例。
  • buildCallFromUserMessage:使用传递的请求视频或音频通话的 SBDUserMessage 对象,创建并返回一个新的 SBVCCall 实例。
SBVCCall *activeCall = [SBVCMain getActiveCall];
SBVCCall *call = [SBVCMain getCallWithCallId:CALL_ID];
SBVCCall *call = [SBVCMain buildCallFromNotification:payload.dictionaryPayload];
SBVCCall *call = [SBVCMain buildCallFromUserMessage:userMessage];

识别消息类型

使用 getRenderingMessageType:,你可以识别传入消息的类型,并根据消息确定如何根据消息渲染您的聊天视图。该方法返回以下四个值之一:

  • SBVCRenderingMessageTypeChat:如果消息是 SBDUserMessageSBDFileMessageSBDAdminMessage(作为 SendBird SDK 的 SBDBaseMessage)之一,则返回。
  • SBVCRenderingMessageTypeStart:调用请求消息。
  • SBVCRenderingMessageTypeEnd:调用结束消息。
  • SBVCRenderingMessageTypeNotRender:不需要在聊天视图中渲染的消息。
SBVCRenderingMessageType renderingType = [SBVCMain getRenderingMessageType:message];

从 PushKit 通知开始通话

要接收视频聊天的通知,客户端必须实现 PushKit。收到 PushKit 通知后,如果应用程序未运行,系统将自动启动。使用用户通知生成不同的设备令牌。要注册 PushKit 设备令牌,请使用 registerDevicePushKitToken:unique:completionHandler:SBDMain 中。

如果您的应用程序运行在 iOS 10 或更高版本,则可以使用 CallKit,它为您的应用程序提供了系统呼叫 UI 并帮助应用程序集成到系统中。请参阅 CallKit使用 CallKit 在 WWDC 2016 中增强 VoIP 应用程序 了解更多信息。

如果您的应用程序在 iOS 9 或更低版本上运行,您必须使用本地通知来通知用户即将到来的通话。

// AppDelegate.h
#import <PushKit/PushKit.h>
#import <SendBirdVideoChat/SendBirdVideoChat.h>

@interface AppDelegate : UIResponder <PKPushRegistryDelegate, SBVCVideoChatDelegate>

// ...

@end
// AppDelegate.m

@interface AppDelegate ()

@property (strong, nullable) PKPushRegistry *pushRegistry;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // ...

  [SBVCMain addVideoChatDelegate:self identifier:IDENTIFIER];

  // ...

  self.pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
  self.pushRegistry.delegate = self;
  self.pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

  // ...

}

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)pushCredentials 
             forType:(PKPushType)type {
  [SBDMain registerDevicePushKitToken:pushCredentials.token unique:NO completionHandler:^(SBDPushTokenRegistrationStatus status, SBDError * _Nullable error) {
    // ...
  }];
}

// PKPushRegistryDelegate for iOS 10.x or lower.
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
             forType:(PKPushType)type {
  if (@available(iOS 10.0, *)) {
    if (@available(iOS 11.0, *)) {
      // Do nothing.
      // This method is not called in iOS 11 or higher.
    }
    else {
      if ([SBDMain getCurrentUser] == nil) {
        // Keep the payload globally.
        // Start CallKit after connecting to SendBird is completed.
      }
      else {
        if (![SBVCMain handleNotification:payload.dictionaryPayload]) {
          // Do something if the push payload isn't for SendBird VideoChat.
        }
      }
    }
  }
  else {
    // iOS 9.x or lower
    if ([SBVCMain isStartCallNotification:payload.dictionaryPayload]) {
      // Schedule the local notification for incoming call.
    }
    else {
      if ([SBVCMain handleNotification:payload.dictionaryPayload]) {
        if ([SBVCMain isEndCallNotification:payload.dictionaryPayload]) {
          // Schedule the local notification for ending call.
        }
      }
    }
  }
}

// PKPushRegistryDelegate for iOS 11 or higher
- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
             forType:(PKPushType)type withCompletionHandler:(void(^)(void))completion API_AVAILABLE(ios(11.0)) {
  if ([SBDMain getCurrentUser] == nil) {
    // Keep the payload globally.
    // Start CallKit after connecting to SendBird is completed.
  }
  else {
    if (![SBVCMain handleNotification:payload.dictionaryPayload]) {
      // Do something if the push payload isn't for SendBird VideoChat.
    }
  }
}

// SBVCVideoChatDelegate methods.
// This delegate method is called by `handleNotification:`.
- (void)didReceiveStartCall:(SBVCCall *)call {
  if (@available(iOS 10.0, *)) {
    // Display incoming call via CallKit.
  }
}

@end

SBVCVideoChatDelegate

《code>SBVCVideoChatDelegate 支持各种方法,能够接收来自 SendBird 服务器的回调,将这些事件通知到应用。以下是 SBVCVideoChatDelegate 提供的方法:

  1. odidSendStartCall:withMessage
  • 当调用者的视频或音频调用请求由 SendBird 服务器成功接受时调用(在调用者的应用中)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  • message:一个包含与调用请求一起发送的文本的 SBDUserMessage 实例。
  1. didReceiveStartCall
  • 当接收方收到视频或音频调用请求时调用(在接收方应用中)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  1. didSendAcceptCall
  • 当接收方使用 acceptWithOptions:completionHandler: 接受视频或音频调用请求并且 SendBird 服务器确认接受时调用(在接收方应用中)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  1. didReceiveAcceptCall
  • 当 TURN 服务器开始调用者和接收者之间的视频或音频调用传递时调用(在调用者的应用中)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  1. didSendEndCall:message
  • 当使用 endWithCompletionHandler: 将关闭请求发送到 SendBird 服务器并且服务器成功接受请求时调用(在发送关闭请求的应用中)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  • message:一个包含与关闭请求一起发送的文本的 UserMessage 实例。
  1. didReceiveEndCall
  • 当接收方收到关闭请求时调用(在接收关闭请求的应用中)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  1. didOpenCall
  • 当调用者和接收者通过 SendBird 服务器连接并可以相互通信时调用(在两个应用中)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  1. didOpponentAudioStateChange
  • 当调用者或接收者的音频状态发生改变时调用(通知对方应用)。
  • call:一个包含当前调用状态的 SBVCCall 实例。
  1. didOpponentVideoStateChange
  • 当调用者或接收者的视频状态发生改变时调用(通知对方应用)。
  • call:一个包含当前调用状态的 SBVCCall 实例。

SBVCLocalVideoView

《code>SBVCLocalVideoView 支持用于本地视频聊天视图的方法。

SBVCRemoteVideoView

SBVCRemoteVideoView 支持远程视频通话视图的方法。

SBVCCallOptions

SBVCCallOptions 是一个用户类,用于通过 startCallWithAudioOnly:options:completionHandler: 请求通话或通过 acceptWithOptions:completionHandler: 接受通话。 SBVCCallOptions 的项包括:

  • initWithChannelUrl: (必需):指定发送通话请求的频道URL。
  • localVideoView:指定用于渲染当前用户聊天视图的 SBVCLocalVideoView
  • remoteVideoView:指定用于渲染对方聊天视图的 SBVCRemoteVideoView
  • videoEnabled:确定是否在 SBVCCall 中使用视频。此值仅在视频调用中接受。
  • audioEnabled:确定是否在 SBVCCall 中使用音频。
  • resolution:指定视频的宽度和高度。此值仅在视频调用中有效。
  • videoFps:指定视频的帧率。此值仅在视频调用中有效。
  • defaultCameraPosition:指定默认摄像头。如果为 YES,则尝试设置前置摄像头。
SBVCOptions *options = [[SBVCOptions alloc] initWithChannelUrl:CHANNEL_URL];
options.videoEnabled = DEFAULT_VIDEO_ENABLED;
options.audioEnabled = DEFAULT_AUDIO_ENABLED;
options.resolution = CGSizeMake(720, 1280);
options.videoFps = 30;
options.defaultCameraPosition = SBVCCameraPositionFront; // or SBVCCameraPositionBack
options.localVideoView = localVideoView;
options.remoteVideoView = remoteVideoView;

SBVCCall

通过一个 SBVCCall 实例,您可以执行视频或音频呼叫操作。它还包含调用的最新信息。

  • callId:检索区分每个调用的唯一ID。
  • channelUrl:检索视频或音频通话的频道URL。
  • messageId:检索包含关于 SBVCCall 的信息的消息ID。
  • isAudioCall:检查 SBVCCall 是否为音频通话。
  • caller:检索呼叫者 Caller 的信息,即发起调用的人。
  • callee:检索被叫者 Callee 的信息,即接收通话的人。
  • getEnder:检索 CallerCallee 的信息,即结束通话的人。
  • endType:指定 SBVCCall 已结束。此值可以是以下值之一。
    • SBVCEndTypeNone:尚未结束。
    • SBVCEndTypeCancel:调用者 Caller 在接受 SBVCCall 之前通过 endWithCompletionHandler: 调用的。
    • SBVCEndTypeDecline:接受者 Callee 在不接受 SBVCCall 的情况下通过 endWithCompletionHandler: 调用的。
    • SBVCEndTypeEnd:连接后,调用者 Caller 或接受者 Callee 结束了视频或音频通话。
    • SBVCEndTypeTimeout:当呼叫者没有响应呼叫请求时,Call被关闭。
    • SBVCEndTypeUnknown:由于未知原因,Call被关闭。
  • period:检索Call的时间长度,以Unix时间戳表示。
  • myRole:检索在Call上的角色,作为SBVCRoleCallerSBVCRoleCallee
  • acceptWithOptions:completionHandler::被叫方接受SBVCCall
  • endWithCompletionHandler::关闭SBVCCall
  • startVideo:调用你注册用于在SBVCCall上启动视频流量的didOpponentVideoStateChange:代理方法。
  • stopVideo:调用你注册用于在SBVCCall上停止视频流量的didOpponentVideoStateChange:处理程序。
  • muteMicrophone:调用你注册用于在SBVCCall上启动音频流量的didOpponentAudioStateChange:处理程序。
  • unmuteMicrophone:调用你注册用于在SBVCCall上停止音频流量的didOpponentAudioStateChange:处理程序。
- (void)didSendStartCall:(SBVCCall *)call withMessage:(SBDUserMessage *)message {
  // Cancel call
  [call endWithCompletionHandler:^(SBDError * _Nullable error) {

  }
}

- (void)didReceiveStartCall:(SBVCCall *)call {
  // Decline a call
  [call endWithCompletionHandler:^(SBDError * _Nullable error) {

  }
}

- (void)didOpenCall:(SBVCCall *)call {
  // End call
  [call endWithCompletionHandler:^(SBDError * _Nullable error) {

  }
}
SBVCCall *call = [SBVCMain getActiveCall];
[call startVideo];
[call stopVideo];
[call muteMicrophone];
[call unmuteMicrophone];

SBVCCallUser

SBVCCallUser可以识别为CallerCalleeCaller是发起呼叫的一方,而Callee是接收呼叫请求的一方。

  • userId:检索CallerCallee的用户ID。
  • nickname:检索CallerCallee的昵称。
  • profileUrl:检索CallerCallee的个人资料URL。
  • isAudioEnabled:指示CallerCallee是否正在使用音频。
  • isVideoEnabled:指示CallerCallee是否正在使用视频。

SBVCErrorCode

SendBirdVideoChat发生错误时返回SBDError

typedef NS_ENUM(NSUInteger, SBVCErrorCode ) {
   SBVCErrorUnknown = 820000,
   SBVCErrorSendBirdConnectionRequired = 820100,
   SBVCErrorSendBirdRequired = 820130,
   SBVCErrorInvalidAction = 820200,
   SBVCErrorAcceptingFail = 820300,
   SBVCErrorInvalidParameter = 820400,
   SBVCErrorUnsuitableChannel = 820401,
   SBVCErrorAlreadyCameraSwitching = 820510,
   SBVCErrorAlreadyCallGoingOn = 900600,
};