MultipeerLiveKit 1.1.0

MultipeerLiveKit 1.1.0

hayao11 维护。



  • 作者
  • Takashi Miyazaki

MultipeerLiveKit

Carthage compatible

概述

Multipeer Connectivity 包装器。

描述

该库提供直播摄像头和类似的文本聊天功能。

演示

要求

  • Swift 4.2+
  • iOS 10.0+

用法

在使用此库时,您需要定义MCSessionManager和LivePresenter。

#MCSessionManager#

MCSessionManager是一个用于建立连接的类。

import MultipeerLiveKit

let displayName = UIDevice.current.name
let serviceType = "YourServiceType"
self.mcSessionManager = MCSessionManager.init(displayName: displayName, serviceType: serviceType)

需要决定数据的传输/接收协议,默认指定为".textAndVideo"。如果只想选择视频数据,请指定".videoOnly"。此时,将在服务类型末尾添加"-V"。

self.mcSessionManager = MCSessionManager.init(displayName: displayName, serviceType: serviceType, serviceProtocol: .videoOnly)

当您想设置true以启动。设置false以停止

mcSessionManager.needsToRunSession = true
mcSessionManager.needsAdvertising  = true
mcSessionManager.needsBrowsing     = true

获取目标MCPeerID和相对于连接的状态

mcSessionManager.onStateChanaged(connecting: { (targetPeerID, state) in

   //Called when connection state changes
   switch state {
   case .tryConnecting:break
   case .connected:break
   case .connectionFail:break
   }
   
}) {(foundPeerIDs) in

   //It is called when browsing is started or when an MCPeerID of the same service type is found.
   foundPeerIDs.forEach {
      self.mcSessionManager.inviteTo(peerID: $0, timeout: 10)
   }
   
}

获取与连接相关的状态

mcSessionManager.onRunStateChange { (rollType, isRun) in

  switch rollType {
  case .advertising:break
  case .browsing:break
  case .connectionRunning:break
  }
  
}

设置接受邀请的条件

mcSessionManager.onInvited { (fromPeerID, acceptAnswer) in
   acceptAnswer(true)
}

发送邀请。

mcSesionManager.inviteTo(peerID:targetPeerID , timeout: 10)

请求取消与PeerID的连接

只有建立了直接连接的目标才有效。

mcSessionManager.canselConectRequestTo(peerID: id)     

#LivePresenter#

LivePresenter是一个主要提供相机功能和数据传输/接收处理的类。考虑到设备负载,建议将sendVideoInterval设置为0.1或以上,并将sessionPreset设置为中等。

  let sendInterval:TimeInterval = 0.1
  let videoCompressionQuality:CGFloat = 0.8
  let sessionPreset:AVCaptureSession.Preset = .medium
  
do{

  try livePresenter = LivePresenter.init(mcSessionManager: mcSessionManager,
                                         sendVideoInterval: sendVideoInterval,
                                         videoCompressionQuality: videoCompressionQuality,
                                         targetPeerID: targetPeerID,
                                         sessionPreset: sessionPreset)
}catch let error{
  print(error)
}

处理接收到的数据

livePresenter.bindReceivedCallbacks(gotImage: { (image, fromPeerID) in
  // for image
}, gotAudioData: { (audioData, fromPeerID) in
  // for audio
}, gotTextMessage: {(msg, fromPeerID) in
  // for text
})

启动服务时,务必指定已连接的MCPeerID

livePresenter.updateTargetPeerID(peerID)

发送相机和麦克风数据

设置为true以启动。设置为false以停止。

livePresenter.needsVideoRun = true

切换前后摄像头

do{
  try livePresenter.toggleCamera()
}catch let error{
  print(error)
}

播放音频

do{
  try livePresenter.playSound(audioData: audioData)
}catch let error{
  print(error)
}

其他说明

  • 请打开wifi。
  • 即使连接失败,多次连接后,最好先断开一次连接。
  • 目前,PCMSampleBuffer的音频处理不太好,图片通过MCSession的send方法发送,音频通过stream方法发送。发送文本也通过send方法发送。
  • 我只用SnapKit的Demo。

开始使用

Carthage

github "hayao11/MultipeerLiveKit"

CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
 pod 'MultipeerLiveKit'
end

许可证

MIT 协议

作者

Takashi Miyazaki