Let’s Live Stream your iOS Video Games in Real Time

Live Streaming Video Games

From the past few years, tremendous competition has been seen in the gaming industry. The gaming industry has been exposed to numerous new trends and one of the trending topic in gaming industry would be live streaming video games. The videos games allows users to interact with the game in real time. But, in the mobile devices like iPhone’s due to horrendous complications, this live streaming isn’t easily possible.

But, hey, we haven’t said that it isn’t completely impossible. You can add the feature of live streaming into your mobile device very easily. Because in this tutorial, we are going to share the method to live stream video games. We will use render a SceneKit scene into video buffers, connect to any live stream server, and stream the rendered video buffers to Twitch’s live stream server in real-time.

Okay, it seems bit gibberish, so let us actually start the process to live stream video games in real time.

Let’s get Started

So, first, you have to download the both ARVideoKit and LFLiveKit from GitHub. ARVideoKit is an iOS framework for videos, live photos, and GIF in ARV format. LFLiveKit is a RTMP streaming SDK for iOS. In this tutorial, we will be working in these two iOS libraries.

Adding Frameworks – So, once both the libraries are installed, the next step is add framework to the real project game. To do so you have to follow these steps:

  1. Drag libraries to the project.
  2. Then, add .framework embedded binary library.

Adding User Interface – In this tutorial, we are using programmatically created user interface instead of Interface Builder. To make the process simple we will only add UIButton, a UIAlertController, and a UILabel. To implement this we have to initiate and declare the main class of the game. Next, you have to add button to the subview of the main game. Insert the following lines of code in the viewDidLoad() method:

 

self.view.addSubview(startLive)

self.view.addSubview(status)

self.view.addSubview(startLive)

self.view.addSubview(statusLbl)

In order to handle the button action, we then create a method called startEndLive(_:):

@objc func startEndLive(_ sender: UIButton) {

}

 

@objc func startEndLive(_ sender: UIButton) {

}

Implementing Frameworks – Now, is the time to implement the ARVideoKit and LFLiveKit frameworks into the main game scene. For this, you have to import the both frameworks. Then, add delegates and methods as their extensions. After this, you have create the empty Swift file named TwitchIngest.swift and add enum in it. Now, you have to create the needed object to render the live stream and game scene to the Twitch. In the viewDidLoad() method you have to initialize and configure recorder (a RecordAR variable) and session (a LFLiveSession variable). The

Implementing Start/End Live Stream Function – Now, that the RecordAR and LFLiveSession are ready variable. So, next, you have to work on implementing start and end live streaming buttons. For this, you have to perform some heavy coding work to implement the startEndLive(_:) method. So, we advice to be carefully here and keep on testing your system by far with the testers.

Delegate Method – So, your implementing start and end live stream function is ready, but you can’t use it until the rendered video isn’t pushed from game scene to the server. For this, the delegate method will come useful to render both audio and video. To begin, we will update the textFieldDidEndEditing(_:,_:) delegate method to store the user’s Twitch Stream key locally:

MARK: – UITextField Delegate Method

func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {

   guard let keyField = streamKeyAlert.textFields?.first, let key = keyField.text else { return }

   if reason == .committed && textField == keyField && key.count > 8 {

       userStreamKey = key

       

       // Enable “Start Live Video” button

       startLive.setTitle(“Start Live Video”, for: .normal)

       startLive.setTitleColor(.black, for: .normal)

   }

}

After the user key stock has rendered locally, then you are ready to stream the rendered audio & video buffers of the game to Twitch. So, that’s it you are ready to live stream your video game and for detail explanation of the tutorial, you can visit the Github.

Send a Message