Я только что скачал Многопартийный пример и установил на 3 разных устройствах один и тот же токен сеанса. ( Media Mode : Routed )
Также я создал 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.
Чего мне не хватает, мне нужны видео подписчика и счет ??