Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hardware and software Environment
- Ubuntu 16.04
- Android Studio 2.1.3
- OTT BOx with android 5.1.1
- Nginx 1.11.3
- nginx-rtmp-module
- vitamio
preface
At present, live broadcasting has become a hot word on the Internet. It not only refers to the real-time broadcasting of traditional radio and TV, but also the extension of the more extensive real-time sharing of audio and video. In the past, live broadcast data sources could only come from TV stations and program production centers. But now, with the rapid development of computer technology, anyone can produce content by himself and share it with terminal devices around him. You are not only the audience, but also the protagonist. It can be said that the popular “Internet celebrity” culture, live broadcast technology has made great contributions. This paper aims to build a simple live video system, including server side and Android client, using Nginx, Nginx-Rtmp, VITAMio and FFMPEG.
RTMP protocol
RTMP, short for Real Time Messaging Protocol, is a network Protocol designed for real-time data communication. It is a protocol family, including RTMPE, RTMPT, RTMPS and so on, and is a common network protocol in live broadcast technology
Server Configuration
Nginx adds RTMP support
Download version 1.11.3 from http://nginx.org/en/download.html, and then download nginx RTMP patch from https://github.com/arut/nginx-rtmp-module, The downloaded files are placed in /home/djstava (please modify them according to your actual situation) and nginx is compiled from source code
Tar XVF nginx-1.11.3.tar.gz CD nginx-1.11.3 mkdir build./configure --prefix=/home/djstava/nginx-1.11.3/build --add-module=/home/djstava/nginx-rtmp-module make -f objs/Makefile make installCopy the code
If a fallthrough error occurs
The objs/Makefile needs to be modified and added in CFLAGS
-Wno-implicit-fallthrough
Copy the code
Example Modify the configuration file nginx.conf
Edit/home/djstava/nginx – 1.11.3 / build/conf/nginx. Conf, add the following at the end of the file
rtmp { server { listen 1935; ping 30s; notify_method get; application myapp { live on; # sample play/publish handlers #on_play http://localhost:8080/on_play; #on_publish http://localhost:8080/on_publish; # sample recorder #recorder rec1 { # record all; # record_interval 30s; # record_path /tmp; # record_unique on; #} # sample HLS #hls on; #hls_path /tmp/hls; #hls_sync 100ms; } # Video on demand #application vod { # play /var/Videos; #} # Video on demand over HTTP #application vod_http { # play http://localhost:8080/vod/; #}}}Copy the code
Start the nginx service
Execute the command
/ home/djstava/nginx - 1.11.3 / build/sbin/nginxCopy the code
Ffmpeg push RTMP
Find a local video file, use FFmpeg to push, command is
Ffmpeg-re-i The Moonlight Box of A Journey to the West. Qing BD1280 super cantonese bilingual in English and double word. Mp4 - vcodec libx264 - acodec aac -f FLV RTMP: / / localhost/myapp/mystreamCopy the code
If the server is not local, replace localhost with the corresponding IP address
Ffplay play test
Once the service is up, you can test it. You can also use VLC if you don’t have FFPlay installed
ffplay rtmp://localhost/myapp/mystream
Copy the code
Afterword.
The previous steps are all done on the machine. In practice, however, the situation is much more complicated, nginx may be one server, ffmPEG may be another server, in which case, localhost can be replaced with the corresponding IP address. If the data source comes from the camera, it can also be pushed through ffmpeg. The command is as follows
ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv RTMP: / / 10.10.10.84 / myapp/mystream1Copy the code
Android client play
I’ve written about a vitamio based video player at github.com/xugaoxiang/… “, we will modify it to find mainactivity.java
private String[] files = {"rtmp demo","apple demo"};
Copy the code
Send an intent containing the address to play when the item of the ListView is clicked
Intent intent = new Intent(MainActivity.this, VitamioVideoViewActivity.class); Intent. PutExtra (movieUrl ", "" RTMP: / / 10.10.10.84 / myapp/mystream"); startActivity(intent);Copy the code
reference
- Ffmpeg.org/download.ht…
- Github.com/yixia/Vitam…
- Github.com/xugaoxiang/…
- nginx-rtmp.blogspot.com