Live phone suddenly fire up when I was in 15 years, as the Chinese prickly ash, reflected the guest, such as live once appeared in tuyere, companies in view of the fighting started quickly broadcast, combat process is brief, as many companies exit and die, mobile broadcast industry tends to be stable, live the service time is occupied by a traditional CDN manufacturer firmly, behind you and put effort into When it comes to interactive live broadcasting, I lost confidence in mobile phone live broadcasting. Now with the arrival of THE 5G era and the rise of VR technology, livestream may have a second life, just like short video 15 years ago and 17 years later. Take advantage of some time to pick up things in the field of live broadcasting, from protocol implementation to coding to collection, gradually in-depth and share.

If you want to do a good job, you need to do a good job first. Today, we will first build a complete service system, from collecting and pushing stream to service processing and broadcasting end.

Let’s set up the server first

RTMP server setup

Commonly used RTMP server: AMS (FMS), wowza, Red5, SRS, crtmpserver, nginx.

  • RTMP, short for Real Time Message Protocol, is an application layer Protocol proposed by Adobe to solve the Multiplexing and packetizing problems of multimedia data transfer streams. Flash Media Server (FMS), later renamed Adobe Flash Media Server (AMS). It is an early (2002) streaming media server product, the original company is Macromedia, later acquired by Adobe, it is streaming video and real-time communication industry leading solution, this product can quickly set up a set of streaming media live, on-demand server. AMS is arguably the official implementation service for RTMP. Adobe Media Server Family, but for a fee. Yes, you can use AMS to build the service at the beginning, you can refer to the FMS tutorial to try to build.
  • Wowza Streaming Engine wowza Streaming Engine wowza is a wowza Streaming Engine. It is also an older sub-product besides the FMS product, which was released in 2007 and is very good at HLS(M3U8) distribution. From wowza2.0 to now wowza4.5, I have paid attention to and used their products, generally speaking, the price is quite high. Like AMS, it is a paid product. Wowza Live Stream Software, also refer to RED5 tutorials to try to build their own
  • Red5: The biggest advantage is that it is free and open source, so there are many users using their products, but the disadvantage is that the server resources are high. Red5 Media Server
  • Evostream Media Server (www.evostream.com) CRTMPD is a community version of Evostream Media Server (www.evostream.com). It is a high-performance RTMP streaming Media Server developed by C/C++ and licensed by GPLV3. It can realize multi-terminal support function of live broadcast and on-demand function, and is a good substitute for FMS in certain cases. A bunch of protocols that support RTMP (RTMP,RTMPE, RTMPS, RTMPT, RTMPTE), which is also a good choice if you don’t want to use RED5. Making address: github.com/j0sh/crtmps… There has been no maintenance for eight or nine years
  • Nginx-rtmp: is deployed as an Nginx module. The official website address is nginx-rtmp, and the github address is nginx-rtmp-module
  • SRS(Simple Rtmp Server) is positioned as an operation-grade Internet live broadcast Server cluster, pursuing better conceptual integrity and the simplest implementation code. This is the official introduction of the product itself. It is worth mentioning that the first few products are foreign, while the author is domestic, and the author is currently in Ali.

Based on the above description,SRS and nginx-rtmp-Module are the best options for our selection. As for these two open source implementations,SRS author established a fork comparison:

SRS has slight advantages. For performance comparison, you can refer to SRS and Nginx-rTMP performance comparison. At present, the author is still iterating and upgrading, and there are thousands of people reception problems (if you are interested, you can add my wechat kouwei_qing to pull you into the group). Here we choose SRS as the RTMP service. The author provides docker environment, and we set up our SRS service on MAC according to the official website:

Step1: get SRS:

git clone https://github.com/ossrs/srs &&
cd srs/trunk
Copy the code

Download too slow can choose domestic mirror: git clone https://gitee.com/winlinvip/srs.oschina.git

Step 2: Configure the Docker environment

Docker compilation mode is selected, the author provides docker, can be directly used:

docker pull ossrs/srs:dev
Copy the code

Start docker in SRS directory:

docker run -it -v `pwd`:/tmp/srs -w /tmp/srs/trunk -p 1935:1935 \
  -p 1985:1985 -p 8080:8080 -p 8085:8085 ossrs/srs:dev bash
Copy the code

Note: Recommend to use AliyunCR registry.cn-hangzhou.aliyuncs.com/ossrs/srs:dev, because it’s much faster.

You can enter the Docker container in a new window as follows:

dockerID=`docker ps --format "{{.ID}} {{.Image}}" |grep 'ossrs/srs:dev' |awk '{print $1}'` &&
docker exec -it $dockerID bash
Copy the code

Step 3: Compile SRS

Compile in Docker

./configure && make
Copy the code

Step 4: Run SRS

./objs/srs -c conf/srs.conf
Copy the code

Step 5: Docker enable GDB debugging

To run docker with –privileged for GDB, or it fail for error Cannot create process: Operation not permitted.

Push the flow end

With the server set up, the next step is to push the audio and video streams to the server. On PC we use ffMPEG and OBS most frequently:

Ffmpeg command mode push stream

Brew install ffmpeg can be used to install ffmpeg on a MAC. After installing ffmpeg, prepare a video to be streamed by executing the following command:

ffmpeg -re -i ./kobe.mp4 -vcodec copy -acodec copy -fFLV - y RTMP: / / 127.0.0.1:1935 / live/livestreamCopy the code

Among them:

  • -re Sends data at the frame rate. If not, the manual says send as fast as possible
  • -i./kobe. Mp4 pushes the kobe. Mp4 file in the current directory
  • – Vcodec Copy Video encoding to keep the original Kobe. Mp4 video encoding, I this file is H264
  • To keep the original Kobe. Mp4 audio encoding, I made this file aAC
  • -f FLV Indicates FLV
  • – y RTMP: / / 127.0.0.1:1935 / live/livestream write directly to the output file, there is the RTMP service address

Execution effect:

obs

After obS installation:

other

There are some other streams implemented on the mobile end, but none of them have the two aspects above the PC. Later we will implement an Android/iOS stream application.

player

The most commonly used RTMP player on PC is FFPlay and VLC. Although they have the disadvantages of high latency, they do not interfere with our usual development and debugging:

ffplay

After installing ffmpeg bring ffplay, direct ffplay RTMP: / / 127.0.0.1:1935 / live/livestream can slice the official, direct effect on:

vlc

VLC is easier, open VLC, press CMD + N, select Network and enter our live broadcast address to start:

other

On the mobile end of the most process is ijkPlayer,github address :github.com/bilibili/ij… Ijkplayer is based on FFMPEG,Features: Common

  • remove rarely used ffmpeg components to reduce binary size config/module-lite.sh

  • workaround for some buggy online video. Android

  • platform: API 9~23

  • cpu: ARMv7a, ARM64v8a, x86 (ARMv5 is not tested on real devices)

  • api: MediaPlayer-like

  • Video-output: NativeWindow, OpenGL ES 2.0

  • audio-output: AudioTrack, OpenSL ES

  • Hw-decoder: MediaCodec (API 16+, Android 4.1+)

  • alternative-backend: android.media.MediaPlayer, ExoPlayer iOS

  • Platform: iOS 7.0 ~ 10.2 x

  • cpu: armv7, arm64, i386, x86_64, (armv7s is obselete)

  • api: MediaPlayer.framework-like

  • Video – the output: OpenGL ES 2.0

  • audio-output: AudioQueue, AudioUnit

  • hw-decoder: VideoToolbox (iOS 8+)

  • alternative-backend: AVFoundation.Framework.AVPlayer, MediaPlayer.Framework.MPMoviePlayerControlelr (obselete since iOS 8)

Considering the complexity and large volume, ffmpeg developed oarplayer myself, making the address https://github.com/qingkouwei/oarplayer, not using ffmpeg, only use a mobile phone system hardware encoder, the subsequent also in unceasing optimization and iteration , welcome to experience the use, to participate in the maintenance.