Easy access to Android live broadcast related technologies to build a live broadcast system from 0

This article has been originally published on my public account hongyangAndroid.
Please indicate the source for reprinting:
http://blog.csdn.net/lmj623565791/article/details/77937483
This article is from Zhang Hongyang's blog

This article has been originally published on my public account hongyangAndroid, a collection of articles .

Many followers in the background of the official account have left messages for me, wanting to learn about live broadcast related technologies, but I have no way to start. In fact, I am not a live broadcast professional, but I can provide some entry-level solutions, hoping to achieve a certain guiding role.

First of all, I searched for a wave and found that there is a similar question on Zhihu:

https://www.zhihu.com/question/49160322/answer/114587604

The first answer in the article is to guide how to build a live broadcast system.

Build a live broadcast system from scratch

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

My operating system is mac, students of other systems can install the software by themselves according to the prompts.

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

  • Build a rtmp media server
  • Push end
  • pull end

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

  • rtmp media server: use srs here
  • Push stream end: use obs here
  • Pull the stream end: use the player vlc here

The construction of rtmp media server

srs is used here, the link of srs is:
https://github.com/ossrs/srs

First clone to the local, enter the trunk directory:

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

Then execute:

./configure --osx

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

Finally execute:

make

After the execution is successful, we can start our service:

./etc/init.d/srs start

If it is a mac system, it will fail at this time. The reason is that the max_connections in srs.conf is too large, and the
directory is srs/trunk/conf/srs.conf, which can be modified to 248 (other operating systems may not have this problem).

Go back to the trunk directory again:

./etc/init.d/srs start

At this point our srs server is set up.

Note:
Centos and Ubuntu can refer to the official website to build, which is relatively simple.
If you encounter other errors during startup, you can view the log information:
srs/trunk/objs/srs.log
Other commands:
stop./etc/init.d/srs stop
restart./etc/init.d/srs restart

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

If you really fail to build it, you can test it with the ip 116.196.121.20. 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 and turn it off later. So try to build it yourself as successfully as possible.

Push Streaming Using OBS

Download address: https://obsproject.com/

Download and install first, it's easy here

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

Select Stream, select Custom for Stream Type, then url, and fill in:

rtmp://你的ip/你喜欢的url

The stream name can be freely entered as above.

Remember our url and stream name:

rtmp://192.168.1.102/zhy/mylive

When done, click OK.

Then click to start streaming.

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

In the end, that's all we have left.

Pull stream using VLC

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

Download and install first, this is easier.

Click Open Network, enter our just url + stream name, and click OK.

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

Demonstrate an animation:

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

At this point, even if we build a live broadcast system~~The feeling of building a successful one by oneself is very refreshing, and it can greatly stimulate our interest in follow-up learning.

Later, we can choose to learn to pull stream or push stream according to our own needs, and gradually replace the software. There are many ways to pull streams, which are supported by many open source players. Here we consider replacing the push stream, hoping to use the mobile phone to push the stream.

Use third-party streaming SDK

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

Here is an example of Baidu Cloud's live broadcast SDK, 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. After the download is complete, unzip it, and then import it into AS (this turned out to be an Eclipse project...), fortunately, AS supports it. After importing:

Just modify the mStreamKey to the address of our rtmp.

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

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

Then run, the interface can also:

Paste the renderings at runtime:

You can still pull the stream with vlc, the whole process is very slow, wait patiently, and the effect is not very good, but it can run through, mainly for learning. Then you can infer other SDKs.

Of course, many open source projects are actually better than the SDK as learning materials, and there are more source codes. The following is an example of an open source project.

Use open source projects to push streams

Using an open source project:

https://github.com/begeekmyfriend/yasea

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

Then you can run it (the import is unsuccessful, you can find a way to solve it yourself, the basic ability~).

Paste a rendering:

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

Well, finally we look at another way.

Well, ffmpeg is very popular, ffmpeg is very powerful.
So the last way is to see how to use ffmpeg to push the stream~~

Use ffmpeg thrust

You can download the source code of ffmepg yourself, and then compile the so according to the online method. The simple and more practical is to compile the so that can execute the ffmpeg command, so that you can do a lot of things.

Here, due to the space, we 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)
ffmpeg -i video_origine.avi gif_anime.gif
synthesizes video and audio
ffmpeg -i son.wav -i video_origine.avi video_finale.mpg
There are many more functions, you can refer to:
http: //blog.csdn.net/king1425/article/details/70348374

One of the commands is to support push streaming. Here, the zixia.mp4 on the mobile phone is used as input:

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

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

Paste the command we need to execute and run it.

Note here that I am pushing a media file on the memory card, pay attention to adding relevant permissions, the effect is as follows.

Ok, so 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~~

Many times, learning a relatively large technical direction is difficult at the beginning, and there is no way to start, so this article should be a very easy-to-understand tutorial. I hope it will be helpful to small partners who want to learn live broadcast technology, and I hope this can inspire Everyone has a certain interest in learning. Of course, the live broadcast technology is far more than that. You can continue to study in depth according to your own situation~


支持我的话可以关注下我的公众号,每天都会推送新知识~

欢迎关注我的微信公众号:hongyangAndroid
(可以给我留言你想学习的文章,支持投稿)

参考:

https://github.com/WritingMinds/ffmpeg-android-java
https://github.com/begeekmyfriend/yasea
https://cloud.baidu.com/doc/Downloadcenter/Push.html
https://www.zhihu.com/question/49160322/answer/114587604
https://github.com/ossrs/srs
http://www.jianshu.com/p/dd3f58392aa0#
http://blog.csdn.net/king1425/article/details/70348374

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325764942&siteId=291194637