Attention
You have Cookie disabled in your browser. Displaying the site will be incorrect!

How to create a mobile streaming app on iOS

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.

How to create a mobile streaming app on iOS
Mobile livestreaming with G-Core Labs Streaming Platform

Streaming protocols

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.

Selecting a library to create an RTMP stream

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:

  • LFLiveKit is outdated—the last commit was on December 21, 2016.
  • HaishinKit is currently supported and free of charge.
  • Larix SDK is a working but paid solution ($300 for providing the source files).
  • KSYLive is outdated—the last commit was on March 22, 2020, and the description is in Chinese.

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:

  • Constantly updated
  • Supports RTMP playback
  • Easy to install in the application
  • Hides the work internally with AVCaptureSession—convenient when the application does not need additional work with the session
  • Supports switching and turning off the camera, as well as disabling the microphone during streaming
  • Can change the resolution and bitrate, and can enable/disable video and audio during streaming
  • Provides the ability to flexibly configure the broadcast parameters
  • Has an option to pause the stream

However, there are several drawbacks:

  • Adaptive bitrate option is not available
  • Hides the work internally with AVCaptureSession, which can cause problems when any actions are required with the incoming audio and video

Streaming implementation via RTMP protocol from an iOS device

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.

1. Init

To use the HaishinKit library in your project, you need to add it via SPM by entering:

Current library version

2. Permissions

Specify the required permissions in the project’s Info.plist:

  • NSMicrophoneUsageDescription (Privacy—Microphone Usage Description)
  • NSCameraUsageDescription (Privacy—Camera Usage Description)

3. Displaying the camera stream

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.

4. Preparing for streaming

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:

HaishinKit settings

5. Adaptive video bitrate and resolution

Two RTMPStreamDelegate methods are used to implement adaptive video bitrate and resolution:

  1. —the method is called every second if there is enough bandwidth (used for increasing bitrate and resolution)
  2. —the method is called in case of insufficient bandwidth (used for decreasing bitrate and resolution)

Examples of implementation:

6. Background streaming

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.

7. Launching and stopping a livestream

Establish server connection and start streaming:

To pause a stream, use the paused Boolean property with the True value:

Integration with G-Core Labs Streaming Platform

Creating a G-Core Labs account

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:

Authorization

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.

Getting PUSH URL

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.

Active stream playback

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.

Starting playback

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:

Summary

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.

G-Core Labs streaming API

All the code mentioned in the article can be viewed on GitHub.

Subscribe to a useful newsletter

Favorable offers and important news once a month. No spam