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

How to create a mobile streaming app on Android

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 this article, we will take a closer look at how to create your own application for mobile streaming and watching livestreams on Android, as well as how to integrate it with G-Core Labs Streaming Platform.

How to create a mobile streaming app on Android
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, Android devices are equipped with a native MediaPlayer 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 Android devices and even fewer truly functional options. Let’s take a look at some of them.

1. rtmp-rtsp-stream-client-java

Pros:

  • New, constantly updated library
  • Minimum API 16 support
  • Camera and Camera2 API support
  • Camera switching during streaming
  • Adaptive bitrate
  • Audio and video activation/deactivation during streaming
  • Possibility to configure broadcasting parameters
  • Installation of OpenGL filters, images, GIFs, or text in real-time
  • Easy to use: Everything works out of the box, without any additional manipulations
  • Availability of documentation and instructions for the library on GitHub
  • Convenient library import via Gradle

Cons:

  • Pause function for a broadcast stream is not available

2. HaishinKit

Pros:

  • Stable updates
  • Supports RTMP playback
  • Uses the current Camera2 API
  • Supports camera switching during streaming
  • Allows you to configure broadcasting parameters

Cons:

  • Adaptive bitrate option is not available
  • Pause function for a broadcast stream is not available

3. LiveVideoBroadcaster

Pros:

  • Minimum API 28 support
  • Camera switching during streaming
  • Adaptive stream quality (frame rate and bit rate)
  • Audio activation/deactivation during streaming

Cons:

  • Out of date: last commit on July 28, 2020
  • Uses outdated Camera API
  • No library import via Gradle
  • Much more complex than rtmp-rtsp-stream-client-java
  • Frames sent by the library contain errors (DC, AC, MV)

To summarize, rtmp-rtsp-stream-client-java has the most advantages and the fewest disadvantages of all the three options. Therefore, we consider this library to be the most suitable solution.

Streaming implementation via RTMP protocol from an Android smartphone

The rtmp-rtsp-stream-client-java library provides two types of objects for streaming—RtmpCamera1 and RtmpCamera2. The former uses Camera API to capture the stream from your smartphone camera, while the latter uses Camera2 API.

We recommend using RtmpCamera2, as Camera API has been obsolete since Android API level 21. Our example uses the up-to-date RtmpCamera2.

We will now take a step-by-step look at how to use the rtmp-rtsp-stream-client-java library for mobile streaming. But first, let’s briefly review how it works.

  • When the method is called, the camera is captured.
  • The captured camera session is sent to the OpenGlView input, where it is displayed to the user.
  • When the method is called, a connection to the remote server is established.
  • The captured session from the camera is transmitted via the RTMP protocol to the specified rtmpUrl.

Now let’s take a step-by-step look at how to organize online mobile streaming.

1. Init

To use the rtmp-rtsp-stream-client-java library in your project, you need to add dependencies to build.gradle:

Current library version

2. Permissions

Specify the required permissions in the AndroidManifest.xml file:

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. Android uses SurfaceView or TextureView. The used library also has its own OpenGlView that is inherited from SurfaceView.

With RtmpCamera1 you can use any of these Views, while only OpenGlView is available with RtmpCamera2. But among all Views, only OpenGlView allows you to use various filters, images, GIFs, or text during streaming.

Add OpenGlView to Layout Activity or Fragment to see the camera stream:

How to create a mobile streaming app on Android

4. Preparing for streaming

To initialize the RtmpCamera2 object, you need the OpenGlView object and the ConnectCheckerRtmp interface implementation:

To use the adaptive bitrate, make some additions to the implementation of this interface:

Add a callback to the OpenGlView object. The callback methods will be used to start and stop the camera preview:

Create the RtmpCamera2 object that will be used for streaming:

5. Launching and stopping a livestream

Set the video and audio parameters, and launch the livestream.

Livestream with default parameters:

Livestream with custom parameters:

Stopping the stream:

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 G-Core Labs API. We will execute requests using Retrofit together with RxJava. But you can use any other method of sending HTTP requests if you want.

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 Android MediaPlayer. If you want to have more control over the stream and the ability to customize the player, we recommend using ExoPlayer.

On-screen display

To display the video stream on the smartphone screen, VideoView is required. It should be added to the Layout Activity or Fragment where you plan to play the stream:

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:

Once the initialization is complete, start playback by calling the videoView.start() method.

Launching the player:

Summary

Using our examples, organizing a livestream in an Android application is quite simple and does not take much time. All you need is the open-source rtmp-rtsp-stream-client-java library and our 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