ios - Vonage OpenTok iOS Swift:在发布者的情况下订阅者计数为零
问题描述
我刚刚下载了多方示例并使用相同的会话令牌安装在 3 个不同的设备中。(媒体模式:路由)
另外,我创建了 1 个发布者和 2 个订阅者生成令牌并添加到代码中。
我在所有订阅者设备屏幕上都获得了发布者的流/视频。
但是在发布者中,我没有收到订阅者的流/视频和音频?
class ViewController: UIViewController {
@IBOutlet var endCallButton: UIButton!
@IBOutlet var swapCameraButton: UIButton!
@IBOutlet var muteMicButton: UIButton!
@IBOutlet var userName: UILabel!
@IBOutlet var collectionView: UICollectionView!
var subscribers: [IndexPath: OTSubscriber] = [:]
lazy var session: OTSession = {
return OTSession(
apiKey: kApiKey,
sessionId: kSessionId,
delegate: self
)!
}()
lazy var publisher: OTPublisher = {
let settings = OTPublisherSettings()
settings.name = UIDevice.current.name
return OTPublisher(delegate: self, settings: settings)!
}()
var error: OTError?
override func viewDidLoad() {
super.viewDidLoad()
self.session.connect(withToken: kToken, error: &error)
self.userName.text = UIDevice.current.name
}
override func viewDidAppear(_ animated: Bool) {
guard let layout = self.collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
return
}
layout.itemSize = CGSize(width: self.collectionView.bounds.size.width / 2,
height: self.collectionView.bounds.size.height / 2)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func swapCameraAction(_ sender: AnyObject) {
let previousPosition = self.publisher.cameraPosition
self.publisher.cameraPosition = previousPosition == .front ? .back : .front
}
@IBAction func muteMicAction(_ sender: AnyObject) {
self.publisher.publishAudio = !publisher.publishAudio
let buttonImage: UIImage = {
if !self.publisher.publishAudio {
return #imageLiteral(resourceName: "mic_muted-24")
} else {
return #imageLiteral(resourceName: "mic-24")
}
}()
self.muteMicButton.setImage(buttonImage, for: .normal)
}
@IBAction func endCallAction(_ sender: AnyObject) {
self.session.disconnect(&error)
}
}
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
func reloadCollectionView() {
print("self.subscribers.count: \(self.subscribers.count)")
self.collectionView.isHidden = self.subscribers.count == 0
self.collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return self.subscribers.count
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: "subscriberCell",
for: indexPath
) as! SubscriberCollectionCell
cell.subscriber = self.subscribers[indexPath]
return cell
}
}
// MARK: - OpenTok Methods
extension ViewController {
func doPublish() {
self.swapCameraButton.isEnabled = true
self.muteMicButton.isEnabled = true
self.endCallButton.isEnabled = true
if let pubView = self.publisher.view {
let publisherDimensions = CGSize(width: view.bounds.size.width / 4,
height: view.bounds.size.height / 6)
pubView.frame = CGRect(origin: CGPoint(x:collectionView.bounds.size.width - publisherDimensions.width,
y:collectionView.bounds.size.height - publisherDimensions.height + collectionView.frame.origin.y),
size: publisherDimensions)
view.addSubview(pubView)
}
self.session.publish(self.publisher, error: &error)
}
func doSubscribe(to stream: OTStream) {
if let subscriber = OTSubscriber(stream: stream, delegate: self) {
let indexPath = IndexPath(item: self.subscribers.count, section: 0)
self.subscribers[indexPath] = subscriber
self.session.subscribe(subscriber, error: &error)
self.reloadCollectionView()
}
}
func findSubscriber(byStreamId id: String) -> (IndexPath, OTSubscriber)? {
for (_, entry) in self.subscribers.enumerated() {
if let stream = entry.value.stream, stream.streamId == id {
return (entry.key, entry.value)
}
}
return nil
}
func findSubscriberCell(byStreamId id: String) -> SubscriberCollectionCell? {
for cell in collectionView.visibleCells {
if let subscriberCell = cell as? SubscriberCollectionCell,
let subscriberOfCell = subscriberCell.subscriber,
(subscriberOfCell.stream?.streamId ?? "") == id {
return subscriberCell
}
}
return nil
}
}
// MARK: - OTSession delegate callbacks
extension ViewController: OTSessionDelegate {
func sessionDidConnect(_ session: OTSession) {
print("Session connected")
doPublish()
}
func sessionDidDisconnect(_ session: OTSession) {
print("Session disconnected")
self.subscribers.removeAll()
reloadCollectionView()
}
func session(_ session: OTSession, streamCreated stream: OTStream) {
print("Session streamCreated: \(stream.streamId)")
if self.subscribers.count == 4 {
print("Sorry this sample only supports up to 4 self.subscribers :)")
return
}
self.doSubscribe(to: stream)
}
func session(_ session: OTSession, streamDestroyed stream: OTStream) {
print("Session streamDestroyed: \(stream.streamId)")
guard let (index, subscriber) = self.findSubscriber(byStreamId: stream.streamId) else {
return
}
subscriber.view?.removeFromSuperview()
self.subscribers.removeValue(forKey: index)
self.reloadCollectionView()
}
func session(_ session: OTSession, didFailWithError error: OTError) {
print("session Failed to connect: \(error.localizedDescription)")
}
}
// MARK: - OTPublisher delegate callbacks
extension ViewController: OTPublisherDelegate {
func publisher(_ publisher: OTPublisherKit, streamCreated stream: OTStream) {
print("Publisher streamCreated")
//TESTING
self.reloadCollectionView()
}
func publisher(_ publisher: OTPublisherKit, streamDestroyed stream: OTStream) {
print("Publisher streamDestroyed")
}
func publisher(_ publisher: OTPublisherKit, didFailWithError error: OTError) {
print("Publisher failed: \(error.localizedDescription)")
}
}
// MARK: - OTSubscriber delegate callbacks
extension ViewController: OTSubscriberDelegate {
func subscriberDidConnect(toStream subscriberKit: OTSubscriberKit) {
print("Subscriber connected")
self.reloadCollectionView()
}
func subscriber(_ subscriber: OTSubscriberKit, didFailWithError error: OTError) {
print("Subscriber failed: \(error.localizedDescription)")
}
func subscriberVideoDataReceived(_ subscriber: OTSubscriber) {
print("Subscriber subscriberVideoDataReceived")
}
}
即使我试图控制发布者中的日志订阅者数量,我也得到了 0。
我缺少什么,我需要订阅者视频和计数?
解决方案
推荐阅读
- java - 根据jframe中的条件在特定区域上绘制颜色
- python - 在 Python 上使用 plotly 方法:离线散点法在同一个图中同时比较两个因素
- python - 使用池的 TensorFlow 错误:无法腌制 _thread.RLock 对象
- .net - 如何使用计数在四个表之间获取 Linq 中的数据到 EF 连接?
- javascript - 显示和映射多个键及其对象数据
- actions-on-google - 如何为 Google Home Action 开启 alpha/beta 测试?
- connection - 树莓派 3B+ WLAN 问题
- php - PHP7.2 缺少国际扩展
- php - 在 Wordpress 中将我的自定义 PHP 文件保存在哪里
- python - 除了表单在 django 中同时发布和重定向之外,还有其他方法吗?