WebRTC For The Brave

WebRTC for Swift Developers

When looking into implementing video calling, the WebRTC (Web Real-Time Communication) protocol is one of the best options available. While you are most likely better off using an existing implementation of a WebRTC based stack, it is helpful for your understanding of WebRTC to implement a barebones implementation yourself. In this article, we will explore what it takes to render a video chat using WebRTC in Swift. We have written more about the basics of WebRTC in our WebRTC for the Brave series. If you are unfamiliar with the concept, read our introduction to WebRTC first.

WebRTC on iOS

Google maintains the open-source project WebRTC. For iOS, a prebuilt library is available. It allows you to integrate WebRTC with iOS-specific features such as camera, audio, video access network handling, and much more. However, if you look through the guides made available by Google, they rely on CocoaPods, and Google’s version on CocoaPods is deprecated. To make the current WebRTC available as a Swift package, we keep a Swift package definition up to date with the current WebRTC binaries. At Stream, we use this framework daily to build our video service and support our customers. By using our build of WebRTC binaries as a Swift Package, it is easy to integrate WebRTC into your codebase. In Xcode, you need to search for a git package.

<https://github.com/GetStream/stream-video-swift-webrtc.git>

Or when adding to a Package.swift file.

We’ve made a customized version of stasel/WebRTC-iOS available. You can find our version using our WebRTC example in this GitHub repository.

We’ve added StreamWebRTC as a Swift package on this project and, from then on, made a few minor adjustments to compile the code with our WebRTC framework build.

Let’s now take a quick tour of the WebRTC-iOS-Sample. The core component in the entire codebase is the WebRTCClient. You can find the complete 300+ lines of code here.

PeerConnection

When looking at this code, the structure of the WebRTCClient is an implementation of the RTCPeerConnectionDelegate protocol.

The WebRTC framework provides this protocol, and a PeerConnection is one of the core concepts related to connecting with a remote peer. It handles the Session Description Protocol (SDP) and allows you to respond to the changes associated with ICE (Interactive Connectivity Establishment).

Through the WebRTCClient, the SDP offering is initiated and handled. The WebRTCClient uses the SignalingClient to send and receive SDP messages to and from the SignallingServer.

RTCPeerConnectionFactory

The WebRTCClient maintains an instance of a RTCPeerConnectionFactory. It is responsible for creating a PeerConnection and building and keeping track of audio/video tracks and sources. It’s a key component bridging the C++ Native code of the core WebRTC library.

If you look at the creation of the WebRTCClient, you will also notice that the servers used for ICE are Google’s STUN servers.

The Signaling Client

As we described earlier, the WebRTCClient initiates signaling, but the SignallingClient does all the encoding, decoding, and handling of SDP messages.

The SignallingClient needs to connect to a signaling server. And fortunately, there’s a basic implementation of a signaling server in our repository as well. The signaling server is nothing more than a WebSocket server that forwards every message it receives to all other connected clients—a simple but essential task enabling things like NAT traversal.

The Signaling Server

We will use the server described in our lesson about Signaling Servers in our WebRTC tutorials for signaling.

Connecting Audio and Video

After establishing a PeerConnection, it is time to create a few things:

  • A Video Source
  • Video Capturer to obtain audio and video through a Video Source
  • Local and Remote Video Track
  • Local and Remote Data Channel

These components give you enough control over the camera capabilities to select a maximum resolution and control video rendering.

In the background, the WebSocket with the signaling server remains intact. When network conditions require adjusting or changing an ICE candidate, the WebRTCClient gets informed and responds accordingly.

The linked GitHub repository contains all the parts needed to set up a working WebRTC connection.

Run the WebRTC iOS Sample Project.

Conclusion

In this lesson, you've worked with an iOS based implementation of WebRTC. Several of the core concepts were discussed such as peer-to-peer communication, the signaling server, SDP (Session Description Protocol), and ICE (Interactive Connectivity Establishment). You've also learned how to implement real-time video streaming on iOS using Swift. For further exploration, you can access the complete source code on our GitHub repository.