Skip to main content
Version: iOS 1.0.12

Host a Low Latency Camera Stream

This guide shows you how to use our low latency live streaming framework to broadcast (1:n) a camera stream to multiple devices.

Setup#

Make sure you followed the guide to use our live framework in your project.

Import our Live Framework in your ViewController.

import VideoKitLive

Now, in our viewDidLoad

  • We setup the live preview view, which will give us a preview of what we are going to stream
  • Prepare the stream,
  • Set our desired parameters,
  • And start hosting the broadcast

Stream Using Back Camera#

override func viewDidLoad() {
let streamView = UIView(frame: .zero)
view.addSubview(streamView)
streamView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
streamView.topAnchor.constraint(equalTo: view.topAnchor),
streamView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
streamView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
streamView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
])
let vkLive = VKLiveStream()
// Set camera to back camera (default is back)
vkLive.streamer.cameraPosition = .front
// We want to stream video and audio (other options for instance are cameraVideoOnly or audioOnly)
vkLive.streamer.streamingMode = .cameraVideoAndAudio
// Start hosting inside your view streamView
// This will initialize the stream on our backend, prepare your camera and preview view
// and give you a VKStreamerProtocol instance (streamer) to start broadcasting
vkLive.host(type: .lowLatency, inView: streamView) { (streamer, stream, error) in
guard let stream = stream else {
print("There was an error starting your stream.")
return
}
// Start broadcasting your stream
streamer.start()
print("Stream started: \(stream.id)")
print("Playback url: " + stream.getPlaybackUrl()?.absoluteString ?? "Error retrieving stream URL")
}
}

Stop Streaming#

vkLive.stopHosting(stream: stream) { (success) in
print("Stream ended.")
}
Last updated on by Nikita Gaer