Publishing a Stream to the Session
When the app connects to the Vonage Video session, we want it to publish an audio-video stream to the session, using the device's camera and microphone:
- Add a
publisher&pubViewproperties 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)
}()
@Published var pubView: UIView? // this will be needed to display the UI later on
The OTPublisher class is defined in the iOS SDK. It uses the device's camera and microphone, to publish a stream Vonage Video session.
- Modify the implementation of the
sessionDidConnect(_:)method to include code to publish a stream to the session:
func sessionDidConnect(_ session: OTSession) {
print("The client connected to the session.")
var error: OTError?
defer {
if let error {
print(error)
}
}
guard let publisher else {
return
}
session.publish(publisher, error: &error)
if let view = publisher.view {
DispatchQueue.main.async {
self.pubView = view
}
}
}
When the app connects to a session, it initializes an instance of the OTPublisher, defined in the iOS SDK. The constructor takes one parameter: the object that implements the OTPublisherDelegate protocol.
The code then passes the OTPublisher object in as a parameter of the session.publish() method. This method publishes an audio-video stream to the session, using the camera and microphone of the iOS device. (Note that in the Xcode simulator, the iOS SDK uses a test video when publishing a stream.)
The OTPublisher object has a view property, which is a UIView object. This view displays the video captured from the device's camera. In the next steps we will display this view in SwiftUI (in case of UIKit it is simple and can be done with a addSubview(view) function)
- Next we need to create a wrapper around UIView to be able to use it in SwiftUI world:
struct Wrap: UIViewRepresentable {
@State var view: UIView
func makeUIView(context: Context) -> UIView {
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
DispatchQueue.main.async {
self.view = uiView
}
}
}
- Now let's connect
VonageVideoManagerto theContentViewas a property, we'll need it to interact with the data. The solution is flexible and fits any architecture, so if you're using ViewModel, Interactors or anything else in your project - we've got you covered:
struct ContentView: View {
@ObservedObject var videoManager = VonageVideoManager()
}
- It's time to populate the body of the view, we will use our
Wrapview that we've created earlier:
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)
}
.task {
videoManager.setup()
}
}
}
- Now we will implement methods of the
OTPublisherDelegateprotocol. This protocol includes methods for handling events related to the publisher. Add the following code to the end of theVonageVideoManager.swiftfile, after the closing bracket of theOTSessionDelegateextension:
// MARK: - OTPublisherDelegate callbacks
extension VonageVideoManager: OTPublisherDelegate {
func publisher(_ publisher: OTPublisherKit, didFailWithError error: OTError) {
print("The publisher failed: \(error)")
}
}
- If the client fails to publish to the session, an
OTErrorobject is passed into thepublisher(_: didFailWithError:)method.
Debug your application. If the app successfully connects to the session, it will publish a stream to the session, and you will see the publisher's video in the app.
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.