RTMP Protocol: Enable Instant Video Streaming for Android Apps
Live video streaming is a really big thing. What can be easier than instantly running a live streaming app on your smartphone? It will directly broadcast you or an event taking place around you… And while you’re streaming a video, your friends are watching it from their mobile devices. And all you need to make it happen is the camera and a built-in microphone on your smartphone… Isn’t this fun?
Live streaming has introduced a different manner for users to connect with their friends and for brands to engage and reach out to their customers.
But how can your business or startup easily implement live streaming for Androidwithout dedicating lots of resources and hours of developers’ work to do it? What technology stands behind live broadcast and makes it real?
Let’s dig into the topic to find comprehensive answers to these questions and also learn more about ready-to-use solutions that facilitate and speed up the process of adding a live video streaming feature into your app.
What are characteristics of live streaming
Unlike the format of video on demand which presumes that a user can select and watch videos at any time, live broadcast videos are tied to a specific moment in time and are displayed while being recorded.
And here’s how live streaming process looks like. Video and audio are both transferred through messages that the video player and the media server exchange. The video player connects to the media server first. The server transfers a portion of the video to the player. This video fragment corresponds to the current playing time. A buffer of several seconds ensures video playback without delays or interruptions. This process is constantly repeated the whole time the web broadcast lasts.
But live streaming never transfers media files as a whole (thus saving bandwidth). Instead, it’s enabled through media servers responsible for transferring and streaming digital video, audio and data. Live streaming provides protection to source video files because they can’t get copied to a viewer’s computer. With the help of additional software, live streaming also supports live chatting and Q&A.
There is no just one single format that exists to transmit media data. Instead, you can choose the one that suits your technical needs best.
What video streaming technologies exist?
There are multiple ways of enabling live broadcast in your app. We’ll briefly overview the existing technologies below. This way you’ll be able to differentiate their key features and type of content they’re best made to deliver:
- RTMP Protocol
Real-Time Messaging Protocol (or just RTMP) was developed for high-performance transfer of the video and audio streams and real-time data messages over the web. Real-time streaming is enabled by establishing a two-way connection between the Flash server and Flash player.
Adobe Flash Player is the most common RTMP client that supports video and audio playback. It is currently available as an open-source specification for development of products and technology that delivers video streams in the formats of AMF, SWF, FLV and F4V (compatible with Adobe Flash Player formats) and audio in AAC and MP3.
- HTTP-based streaming protocols (HLS and HDS)
HLS format is based on MPEG2-TS and pertains to streaming to iOS devices. It isn’t part of HTML5. HLS combines multiple streams with the metadata information and easily switches between different media data.
HDS uses Adobe’s format for videos and delivers fragments of MPEG-4 files. MPEG format allows storing metadata and audio and video streams in different locations. HTTP Dynamic Streaming is a convenient technology for large media libraries. It helps reduce bandwidth, saves battery life and provides a better user experience compared to HLS.
- Silverlight streaming protocol
This video streaming format is used for rich Internet apps such as Adobe Flash and for running movies. Despite Microsoft Silverlight no longer works in Chrome browser, it is available in Internet Explorer and Mozilla Firefox.
MPEG-DASH provides high-quality live and on-demand video streaming for media coming from HTTP servers. The format allows adding ads in between video segments for both types of streaming. Media content is encrypted once and delivered to a client computer with the help of different DRM (Digital Rights Management) schemes.
- Progressive download streaming
With progressive download, media content is transferred from a server to a client computer through HTTP protocol. It doesn’t require any streaming server for file hosting or downloading a media file to watch it. Progressive download enables video playback before a complete video download.
The rise of 4K video, high user expectations of quality standards and smooth experience, development of new effective platforms and faster Internet connection make demands to video streaming formats stricter. Irrespective of variety of technologies available, we’ll focus on RTMP as one of the most widespread and favorite streaming protocols among developers.
So, what is RTMP protocol and why it’s so good
Created by Macromedia (currently Adobe), Real-Time Messaging Protocol ensures adaptive bitrate streaming (ABS). When the bandwidth changes, the video quality automatically tunes up to it. RTMP is based on Transmission Control Protocol (TCP) which prioritizes maintenance of stable connection and low latency. Regarding the type of content, RTMP allows transmitting both prerecorded and live media data and text information.
Moreover, RTMP can be used not only for mobile streaming. RTMP server can send media data both to web applications as well as to mobile devices.
It is most often praised for the following advantages it provides:
- It is specifically optimized for live streaming
The greatest benefit that RTMP provides is optimization of video and audio data transmission. For streaming, it uses the exclusive 1935 port that is suitable for low-latency content transmission which is vital for live streaming and requires no buffering.
- It is a mature technology
RTMP has been used for live streaming for more than 10 years. For a developer, setting up RTMP Server is an easy task. For a user, video streaming is enabled in different formats at the same time. Security features such as Player verification are applied too.
- You can watch any part of the video being broadcasted
All RTMP servers allow recording incoming media streams and provide the ability to display videos from the beginning. A user can skip parts of the video till the current moment in the broadcast. This feature is particularly useful for long videos (more than 10 minutes), allowing to easily switch to any part of it.
- It is the most popular real-time streaming protocol
RTMP is a platform-independent protocol that requires no hardware dependencies and is compatible with Windows, Mac, Linux, Android, iOS and Windows mobile. It is also widely utilized in gaming devices, such as PlayStation, Xbox One, and PS Vita.
How RTMP works
In simple words, RTMP streaming consists of the following three entities:
- A client that creates a media stream and broadcasts by RTMP protocol to a server. As client “publishers”, you can use a lot of apps on different platforms, such as FFMPEG based players, VLC Player, YouTube clients, and AVLib based solution.
- A server that handles an incoming stream and performs processing and recording as necessary. The server also provides broadcasts to other clients. Among servers, the best options would be Wowza Media Server, Flussonic Media Server, and Simple RTMP Server.
- Viewers represent any client that can watch published streams. For displaying such media streams, you could use any player that supports RTMP protocol. The good news is that almost all currently used players for all existing platforms support RTMP streams. You can select Flash Player, VLC, or Media Player Classic.
The most complex and resource-intensive are broadcaster apps. ‘Broadcasting flow’ includes media processing, mixing, encoding and further streaming over RTMP protocol to a server. It depends significantly on a platform and hardware and requires a lot of supporting resources.
To transmit as much data as possible with RTMP protocol, video and audio are split into fragments of different sizes. The default sizes are 64 bytes for audio and 128 bytes for video. Video chunk sizes are determined by the interaction format between a user and a server.
Fragments of media are then interleaved and multiplexed over a single connection while correlating with a channel’s bandwidth and latency. The stream coming from the broadcaster to the server consists of small data blocks that include different types of data (i.e., video, audio service, etc.). RTMP streaming can concurrently transfer several different data blocks, thus encapsulating both video and audio and enabling remote procedure calls.
When the client opens the stream in RTMP player, a request is sent to RTMP Server. A session is initialized and service data exchange occurs. Once the client RTMP player receives the necessary portion of the service data packages about the ongoing stream, it starts decoding, buffering and displaying RTMP media stream.
With RTMP, media is not required to be stored in any temporary storage. It continues to stream directly between clients and servers. If the bandwidth capacity allows, the same stream is directed to numerous clients that send requests. The streaming capacity improves if several media servers are put into a network. The streaming process continues until either the server or the player finishes the session.
How can you use RTMP to stream video in your Android app?
You can watch RTMP stream on any platform with the help of any player that supports RTMP protocol. For Android, the most popular third-party players are MX, VLC, and BS Players as well as any other player that supports FFMPEG. For watching RTMP from inside the application, a developer needs to integrate any player library that can read RTMP streams. These are ijkPlayer and VLC SDK.
Onix team have made their technological contribution to finding an easy and beautiful solution for video streaming and have created AVLib, Onix own external library for Android RTMP streaming.
Let’s see how it works with the live broadcast for Android apps and how the idea originated.
How AVLib solves the basic problem for Android RTMP streaming
Our Android team of developers have come to the idea of AVLib almost accidentally, but with a great outcome and effect. While working on the task of adding a feature to broadcast video and audio for Android devices to RTMP servers, they encountered a serious problem. The customer’s requirements stated that the app should be able to stream video and audio data at its maximum capacity through an Android camera. It was also necessary to enable support of Android 4.0 as the minimum version, for users with older mobile devices could use it, and possess a number of convenient usability settings for a user.
Onix team have conducted a deep research and comparison of existing both commercial and open-source streaming libraries. But with no satisfying result. It turned out there was no similar solution in the market that could meet our technical needs in a complete manner. The team members quickly came to a decision of creating their own external library that could support RTMP streaming for Android. And because there is a vast number of Android devices and OS versions being currently in use, this library was built to support streaming needs of all current Android users.
But it never makes sense to spend time on creating a one-time tool that solves one particular task strictly to the required extent. What if a similar situation is repeated? Why not use the best of what technology has to offer and build a well-tested software product?
We intended to develop an elegant, flexible and simple solution, so any other third-party Android team or a developer who was looking for an RTMP library could easily integrate it into an app without having to reinvent the wheel. And we did it! AVLib combines all features we expected and comprehensively solves the basic task of the video broadcast.
How AVLib was developed and why it’s a reliable RTMP library
AVLib is an RTMP library that we’re truly proud of. It is a software product that solves a customer’s requirements for live streaming of any complexity level. It is a ready-to-use tool for Android Studio that is integrated into a mobile app straight away.
AVLib represents a convenient and stable solution that is based on the following principles:
- An easy integration with the app
For an Android developer, it would take about an hour to complete the process of integration of AVLib and add a live streaming feature into an app.
- Maximum capacity
To implement the RTMP library and empower it with a high capacity potential, our team chose the C programming language. Every single function included into AVLib, starting from the conveyor logic, coding, work with RTMP and up to processing, were built using pure C.
- UI custom-built library
AVLib is flexible and can be easily adjusted to customer needs in UI design (i.e., buttons, text) and video settings (i.e., frame resolution, audio, codec quality, bitrates).
- Support of all existing types of Android devices
This RTMP library comes as one .aar file with no dependencies. AVLib was tested to make sure it is compatible with various Android devices and supports Android OS version 4.0 and higher.
You can find more details about AVLib’s features here.
The development process of AVLib was really full of challenges and required thought and effort from our Android team.
- Streaming from action cameras? Yes!
One of the peculiar features of AVLib is its ability to stream video and audio from action cameras on the fly. To enable this feature, you need to work simultaneously with two network interfaces on the side of the module in C. The task was completed successfully but demanded thorough research of the source code of Android OS and continuous debugging.
- Building the broadcast life cycle
Another significant challenge that we encountered during the development phase was enabling smooth work with the camera and processing of the life cycle on different devices. That was rather time-consuming and required lots of scrutinizing work.
How you can try AVLib to see if this solution works for you
You can give AVLib a try by requesting a demo app that demonstrates the basic capacity of the RTMP library. With this demo app, you’ll be able to:
- stream live video from your smartphone to your YouTube account,
- evaluate the basic functionality of AVLib and
- see how easy and fast it is.
With the demo app, you’ll also receive a detailed instruction and access to the app’s source code on GitHub. The instruction contains examples of how AVLib works with action cameras.