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.
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.
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.
Pros:
Cons:
Pros:
Cons:
Pros:
Cons:
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.
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.
Now let’s take a step-by-step look at how to organize online mobile streaming.
To use the rtmp-rtsp-stream-client-java library in your project, you need to add dependencies to build.gradle:
Specify the required permissions in the AndroidManifest.xml file:
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:
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:
Set the video and audio parameters, and launch the livestream.
Livestream with default parameters:
Livestream with custom parameters:
Stopping the stream:
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.
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 Android MediaPlayer. If you want to have more control over the stream and the ability to customize the player, we recommend using ExoPlayer.
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:
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:
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.
All the code mentioned in the article can be viewed on GitHub.