Subscribing to Other Clients' Streams
Finally, we want clients to be able to subscribe to (or view) other clients' streams in the session:
- Add a
subscriber&subViewproperties to theVonageVideoManagerclass:
final class VonageVideoManager: NSObject, ObservableObject {
private var session: OTSession?
private lazy var publisher: OTPublisher? = {
let settings = OTPublisherSettings()
settings.name = UIDevice.current.name
return OTPublisher(delegate: self, settings: settings)
}()
private var subscriber: OTSubscriber?
@Published var pubView: UIView?
@Published var subView: UIView?
The OTSubscriber class is defined in the iOS SDK. It uses the device's camera and microphone, to subscribe a stream Vonage Video session.
- Modify the implementation of the
session(_: streamCreated)method (one of theOTSessionDelegatecallbacks) to include code to subscribe to other clients' streams the session:
func session(_ session: OTSession, streamCreated stream: OTStream) {
print("Session streamCreated: \(stream.streamId)")
var error: OTError?
defer {
if let error {
print(error)
}
}
let subscriber = OTSubscriber(stream: stream, delegate: self)
self.subscriber = subscriber
session.subscribe(subscriber, error: &error)
}
When another client publishes a stream to a session, this method is called, and an OTStream object is passed in. The OTStream class is defined in the iOS SDK, and it represents an audio-video stream in the session. The code initializes an instance of the OTSubscriber class, defined in the iOS SDK. The OTSubscriber() constructor takes two parameters: The OTStream object (for the stream you want to view) and the object that implements the OTSubscriberDelegate protocol.
- Next we will implement methods of the
OTSubscriberDelegateprotocol. This protocol includes methods for handling events related to the subscriber. Add the following code to the end of theVonageVideoManager.swiftfile, after the closing bracket of theOTPublisherDelegateextension:
// MARK: - OTSubscriberDelegate callbacks
extension VonageVideoManager: OTSubscriberDelegate {
func subscriberDidConnect(toStream subscriberKit: OTSubscriberKit) {
print("The subscriber did connect to the stream.")
if let view = subscriber?.view {
DispatchQueue.main.async {
self.subView = view
}
}
}
func subscriber(_ subscriber: OTSubscriberKit, didFailWithError error: OTError) {
print("Subscriber failed: \(error.localizedDescription)")
}
}
- When the client connects to the session, the
subscriberDidConnect(_:)method is called. - If the client fails to connect to the session, an OTError object is passed into the
subscriber(_: didFailWithError:)method.
- Finally update the UI code to include the subscriber view as well:
struct ContentView: View {
@ObservedObject var videoManager = VonageVideoManager()
var body: some View {
VStack {
videoManager.pubView.flatMap { view in
Wrap(view)
.frame(width: 200, height: 200, alignment: .center)
}.cornerRadius(5.0)
videoManager.subView.flatMap { view in
Wrap(view)
.frame(width: 200, height: 200, alignment: .center)
}.cornerRadius(5.0)
}
.task {
videoManager.setup()
}
}
}
Basic video chat
Learn the basic concepts of the Vonage Video API platform, including how users can communicate through video, voice, and messaging. Explore a basic Vonage Video API flow.