Easily get started with Android live broadcast related technologies and build a live broadcast system from scratch

Easily get started with Android live broadcast related technologies and build a live broadcast system from scratch

Build a live broadcast system from scratch

I put it into practice immediately, so first I will share with you the entire construction process:

My operating system is Mac. Students using other systems can install the software by themselves according to the prompts.

A simple live broadcast system can be roughly composed of three parts:

Building an rtmp media server

  • Streaming End
  • Pull flow end

Now the goal is to build it quickly, so of course we use open source projects and some software:

  • rtmp media server: srs is used here
  • Streaming end: OBS is used here
  • Streaming end: Use the player vlc here

Building rtmp media server

SRS is used here, and the link of SRS is:

https://github.com/ossrs/srs

First clone it locally and enter the trunk directory:

  1. git clone https://github.com/ossrs/srs.git  
  2. cd srs/trunk

Then execute:

  1. ./configure --osx  

Note: Centos6.x/Ubuntu12 32/64bits users only need to execute ./configure.

***implement:

  1. make

After successful execution, we can start our service:

  1. ./etc/init.d/srs start

If it is a mac system, it will fail at this time because the max_connections in srs.conf is too large.

The directory is srs/trunk/conf/srs.conf and can be modified to 248 (other operating systems may not have this problem).

Return to the trunk directory again:

  1. ./etc/init.d/srs start

At this point our srs server has been set up.

Note:

For CentOS and Ubuntu, you can refer to the official website for setup, which is relatively simple.

If you encounter other errors during startup, you can view the log information:

  1. srs/trunk/objs/srs.log

Other instructions:

  1. Stop ./etc/init.d/srs stop  
  2. Restart ./etc/init.d/srs restart

Now that we have the server, we are ready to start our streaming end.

If you really can't build it successfully, you can use the IP 116.196.121.20 for testing first. I built it on JD Cloud with a low configuration. It is mainly used for temporary testing. It may be unstable. Just take a look at it. It will be turned off later, so try to build it successfully by yourself.

Use OBS to push the stream

Download address: https://obsproject.com/

Download and install first, it's simple here

First, select Click + Select Source, here I selected Window Capture, and then click Settings on the right:

Select Stream, select Custom for Stream Type, and then enter the URL:

  1. rtmp://your ip/your favorite url

The stream name can be freely entered as above.

Remember our url and stream name:

  1. rtmp://192.168.1.102/zhy/mylive

When finished, click OK.

Then click Start Streaming.

In this way, our OBS streaming is started. You can explore more uses of the software by yourself.

***The only thing left is our streaming.

Use VLC to pull the stream

Download address: http://www.videolan.org/vlc/

Download and install first, this is even easier.

Click Open Network, enter the URL + stream name we just created, and click OK.

Wait a moment, and our streaming content will start playing.

On the far left is VLC, in the middle is OBS, and on the right is my window capture object.

At this point, we have built a live broadcast system. The feeling of successfully building it by ourselves is extremely refreshing, and it can also greatly stimulate our interest in subsequent learning.

Later, we can choose to learn to pull or push streams according to our needs, and gradually replace the software. There are many ways to pull streams, and many open source players support them. Here we consider replacing push streams, hoping to use mobile phones to push streams.

Using a third-party streaming SDK

The simplest way is to use a third-party streaming SDK. In most cases, the complete third-party SDK solution is charged, but their streaming Android SDK can be downloaded and learned without paying.

Here we take Baidu Cloud's live broadcast SDK as an example, download address:

https://cloud.baidu.com/doc/Downloadcenter/Push.html#.E7.89.88.E6.9C.AC.E6.9B.B4.E6.96.B0.E8.AF.B4.E6.98.8E

Just click to download the Android SDK, unzip it after downloading, and then import it into AS (this is actually an Eclipse project...), fortunately AS supports it, after importing:

Just modify mStreamKey to our rtmp address.

Note that you need to add v7 dependencies in build.gradle

  1. compile 'com.android.support:appcompat-v7:23.0.0'  

Then run it, the interface is OK:

Here is a picture of the runtime effect:

You can still use vlc to pull the stream. The whole process is very slow, so you need to wait patiently. The effect is not very good, but it can run smoothly. The main purpose is to learn. Then you can learn from it and try other SDKs.

Of course, many open source projects are actually better than SDK as learning materials, and they all have source code. Here is an open source project as an example.

Use open source projects to push streams

Using an open source project:

https://github.com/begeekmyfriend/yasea

Directly clone and import.

This is relatively smooth. After importing, modify the rtmp link:

Then just run it (if the import fails, find a way to solve it yourself, it’s basic ability~).

Post a picture of the effect:

In the case of hard decoding, the effect is much better than the previous SDK~~

Okay, let's look at another way.

Well, ffmpeg is very popular and ffmpeg is very powerful.

So the first way is to see how to use ffmpeg to push the stream~~

Use ffmpeg to push stream

You can download the source code of ffmepg yourself, and then compile it into so according to the method on the Internet. The simpler and more practical way is to compile so that can execute ffmpeg commands, so that you can do a lot of things.

Here, due to the length of the article, we will directly use the projects compiled by others.

https://github.com/WritingMinds/ffmpeg-android-java

Direct import, the project supports running ffmpeg commands directly.

There are many ffmpeg commands:

For example:

Convert .avi to gif animation (uncompressed)

  1. ffmpeg -i video_origine.avi gif_anime.gif

Combining video and audio

  1. ffmpeg -i son.wav -i video_origine.avi video_finale.mpg

One of the commands supports streaming. Here, zixia.mp4 on the phone is used as input:

  1. ffmpeg -re -i /storage/emulated/0/zixia.mp4
  2. -vcodec libx264
  3. -acodec aac
  4. -f flv
  5. -strict -2 rtmp://192.168.1.102/zhy/mylive=

Then this library supports running ffmpeg commands on mobile phones, which is simple:

Paste the command we need to execute and run it.

Note here that I am pushing a media file on the storage card. Pay attention to adding relevant permissions. The effect is as follows.

Well, now we have roughly learned how to build a small live broadcast system, how to use SDK, open source projects, and simply use ffmpeg to push streams~~

Often, learning a relatively large technical direction is difficult at the beginning and you don’t know where to start. This article should be a very easy-to-understand tutorial. I hope it will be helpful to those who want to learn live broadcast technology. I also hope that this can stimulate everyone’s interest in learning. Of course, live broadcast technology is far more than this. You can continue to study in depth according to your own situation~

<<:  Aiti Tribe Stories (27): The Growth Diary of a Neighboring Operations Engineer

>>:  Just like the leak, let me show you the Apple conference in 3 minutes

Recommend

Wangchuan Education-Interest E-commerce Team Self-broadcasting Growth Camp

Wangchuan Education-Interest E-commerce Team Self...

Zhihu Promotion and Operation | Zhihu under siege!

Since Zhihu was founded in December 2010, it has ...

On Humanity’s Lunar Day, how to protect the lunar heritage?

In order to take a photo of the banner on the lun...

Tablets have no future: iPad replacing PC is a false proposition

Did you notice this detail at the Apple new produ...

Mycorrhizae "weave" networks, forming an "Internet" in the forest

Deep in the dense forest, there is a magical phen...

What did Shenzhou 15 do during its first extravehicular activity?

According to the China Manned Space Engineering O...

How much does it cost to make a tea mini program in Handan?

According to industry insiders, mini programs wil...