Livestreaming from mobile devices allows you to connect with your audience, wherever you are. Services that provide such an opportunity are very popular and used in various fields.
In our previous article, “How to create a mobile streaming app on Android”, we talked about how to set up streaming on Android devices. This time, we will take a closer look at how to create your own application for mobile streaming and watching livestreams on iOS, as well as how to integrate it with G-Core Labs Streaming Platform.
Streaming protocols are used to send video and audio over public networks. One of the most popular streaming protocols is RTMP. It is supported by most streaming platforms. RTMP is reliable and perfect for livestreaming due to its low latency and retransmission of data packets based on the TCP protocol.
To distribute and play content on users’ devices, streaming platforms offer the popular and scalable broadcast HLS and DASH formats. In addition, iOS devices are equipped with a native AVPlayer that supports HLS playback. That is why we will focus on this particular protocol.
There are few open-source solutions for RTMP streaming from iOS devices and even fewer truly functional options. Let’s take a look at the primary solutions:
Given all this, HaishinKit is the most suitable library. It is an up-to-date, functional, and at the same time, free solution with good documentation.
In addition, HaishinKit has many advantages:
However, there are several drawbacks:
The library provides two types of objects for streaming: RTMPStream and RTMPConnection.
Let’s take a step-by-step look at how to set up mobile streaming.
To use the HaishinKit library in your project, you need to add it via SPM by entering:
Specify the required permissions in the project’s Info.plist:
When streaming from a smartphone camera, you need to see what is being broadcast. For this purpose, a corresponding View will display the camera stream on the screen. In iOS, an object of the MTHKView class is used for these purposes, with the RTMPStream object attached to it.
First of all, you need to configure and activate AVAudioSession. This can be done in the AppDelegate application class:
Create the RTMPConnection and RTMPStream objects:
Set the rtmpStream parameters:
Two RTMPStreamDelegate methods are used to implement adaptive video bitrate and resolution:
Examples of implementation:
Apple does not allow video recording in the background, which means that the library cannot send video fragments to the server. On the server side, it causes crashes and stream interruptions.
To fix this, we decided to add more functionality to the library for sending a static image in the background. Our version of the library can be taken from our project on GitHub.
Establish server connection and start streaming:
To pause a stream, use the paused Boolean property with the True value:
To integrate the streaming platform into the project, you need to create a free G-Core Labs account with your email and password.
Activate the service by selecting Free Live or any other suitable plan.
To interact with G-Core Labs Streaming Platform, we will use the G-Core Labs API. Requests will be executed by native code using the methods of the NetworkManager structure which transmits data to the HTTPCommunication class. Data parsing is performed via the CodingKey protocol using the DataParser structure. You can use any other HTTP library if you want.
An example of a method for creating a request:
Log in to start working with API. Use the email and password specified during registration to receive the Access Token, which you will need for further requests.
There are two ways to get the URL for sending the RTMP stream:
Method 1. Send a Get all live streams request to get all livestreams. As a response, we will receive data on all streams created in your account.
An example of sending a request:
Method 2. Send a Get live stream request to get a particular livestream. As a response, we will receive data only on the requested stream, if such stream exists.
An example of sending a request:
From the responses to these requests, we get a push_url and use it as the URL for sending the RTMP stream. Once the stream begins, the broadcast will start automatically. Select the required stream in your personal account. You can use the preview before publishing the stream to your website or player.
With G-Core Labs Streaming Platform, you can broadcast streams on third-party resources in various formats, including HLS.
In our example, we will not consider simultaneous streaming and playback on one device. Instead, streaming should be launched from any other device.
To play the active stream, use the standard AVPlayer.
Before playing, the hls_playlist_url of the active stream should be embedded in the player during its initialization. hls_playlist_url is returned in the response to the Get livestream request mentioned above.
Player initialization:
Using our examples, organizing a livestream in an iOS application is quite simple and does not take much time. All you need is the open-source HaishinKit library and G-Core Labs Streaming Platform.
All the code mentioned in the article can be viewed on GitHub.