? ? 這幾天看到實驗室里面的幾個師兄個個都在搞這個,本著好奇地心里去看看,并自己動手簡單的搭一下
這個簡單的流媒體服務器來實現(xiàn)實時直播功能,過程還算流暢。
首先,我們需要清楚,流媒體服務器的實現(xiàn)流程是咋樣的:
分三部分來理解,推流端,拉流端,服務器端。其中涉及的編碼以及解碼,我就不詳細介紹了如圖。

推流端:這個的選擇可以有很多,比如
1.使用OBS推流 (下載地址:https://obsproject.com/)
2.使用第三方推流SDK,這個是百度云的一個SDK,無需收費,是一個Eclipse項目,不過AS也是支持的,導入便可(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)
3.使用開源項目推流(https://github.com/begeekmyfriend/yasea)導入后,修改下rtmp鏈接。
4.利用ffmpeg推流,這個很火對直播而言(https://github.com/WritingMinds/ffmpeg-android-java)
以上四種方法,都可以用來進行推流。其中最簡淡的是第一種直接用市面上已經有的軟件推流,只需要會用就好。
如果只是想我一樣只是單純想去了解一下,那么使用這個會快一點,有時間最好全部都試試。OBS的具體操作的話,我就不多說了

剛開始可以選擇窗口捕獲。

我的服務器一般是開著的,所以,如果哪位哥們想測試,可以使用上面的rtmp地址,不過,不允許哪位黑客大兄弟黑了哈,
這服務器啥都沒有,黑的話沒意思!

在這里選擇開始或者結束,現(xiàn)在這張圖里他是處于推流狀態(tài)的。
下面這個是我用上面介紹的一個集成項目(移動端的)


依次的功能是,開始推流,攝像頭選擇(前后),錄屏,軟硬編碼器的選擇(這里推薦使用硬編碼器),。。。上面還有美顏的一些效果
拉流端:拉流端也可以有多個選擇,這里推薦使用VCL
(不為啥,因為我用的就是它,你也可以使用其他的)
(下載地址:http://www.videolan.org/vlc/)

它的rtmp拉流地址是和推流端的一樣的,輸入點擊播放即可。
服務器端(重點,這里只是講了nginx+rtmp,還有其他方式來實現(xiàn),其實,服務器端的最重要的一點就是編碼和解碼,
你也可以使用第三方的一些API來進行,這個無所謂,這里主要講的是如何在啊里云的服務器上搭建這個,注意一下我的
服務器部署的系統(tǒng)是win7 32位的,這個其實不影響,我在nginx官網哪里看到過,現(xiàn)在的nginx是沒有64位的,都是
32位的通用版,意思是,無論你的系統(tǒng)是32位還是64位并不影響)
在這之前你需要有一個自己的服務器,阿里云上或者其他地方,租也行買也行。如果你還是一個學生,可以租一下啊里云的。
具體怎么去租我就不一一介紹了.
1.下載nginx開源包和插件
下載鏈接: http://nginx-win.ecsds.eu/download/nginx 1.7.11.3 Gryphon.zip
下載鏈接: https://github.com/arut/nginx-rtmp-module/releases 下載v1.2.0版zip格式
下載下來后,將nginx-1.7.11.3-Gryphon解壓后的nginx-1.7.11.3-Gryphon
(這里我是放到C:\nginx-1.7.11.3-Gryphon的),
nginx-rtmp-module文件解壓后放到C:\nginx-1.7.11.3-Gryphon\nginx-rtmp-module,注意里面需要有


這個文件必須要有。
這個文件是下面這個服務器使用監(jiān)聽情況的主要文件

這個時候搭建工作基本已經完成了,剩下的就是修改配置了,但是在此之前我們還需要下載msvcr100.dll這個文件,因為在運行的時候會出現(xiàn)提示缺少這個文件無法運行的提示。
只要下載好這個文件,x64版本的放到C:\WINDOWS\SYSTEM32下,x86版本放到C:\WINDOWS\SYSWOW64下,接著重啟系統(tǒng)。
下面來修改conf配置文件(配置文件位于C:\nginx-1.7.11.3-Gryphon\conf下)
復制nginx-win.conf文件,修改名稱為nginx.conf,將下面代碼復制進去后保存。
#user? nobody;
# multiple workers works !
worker_processes? 2;
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid? ? ? ? logs/nginx.pid;
events {
? ? worker_connections? 8192;
? ? # max value 32768, nginx recycling connections+registry optimization =
? ? #? this.value * 20 = max concurrent connections currently tested with one worker
? ? #? C1000K should be possible depending there is enough ram/cpu power
? ? # multi_accept on;
}
rtmp {
? ? server {
? ? ? ? listen 1935;
? ? ? ? chunk_size 4000;
? ? ? ? application live {
? ? ? ? ? ? live on;
? ? ? ? }
? ? }
}
http {
? ? #include? ? ? /nginx/conf/naxsi_core.rules;
? ? include? ? ? mime.types;
? ? default_type? application/octet-stream;
? ? #log_format? main? '$remote_addr:$remote_port - $remote_user [$time_local] "$request" '
? ? #? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? #? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
? ? #access_log? logs/access.log? main;
#? ? # loadbalancing PHP
#? ? upstream myLoadBalancer {
#? ? ? ? server 127.0.0.1:9001 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9002 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9003 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9004 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9005 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9006 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9007 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9008 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9009 weight=1 fail_timeout=5;
#? ? ? ? server 127.0.0.1:9010 weight=1 fail_timeout=5;
#? ? ? ? least_conn;
#? ? }
? ? sendfile? ? ? ? off;
? ? #tcp_nopush? ? on;
? ? server_names_hash_bucket_size 128;
## Start: Timeouts ##
? ? client_body_timeout? 10;
? ? client_header_timeout 10;
? ? keepalive_timeout? ? 30;
? ? send_timeout? ? ? ? ? 10;
? ? keepalive_requests? ? 10;
## End: Timeouts ##
? ? #gzip? on;
? ? server {
? ? ? ? listen? ? ? 80;
? ? ? ? server_name? localhost;
? ? ? ? location /stat {
? ? ? ? ? ? rtmp_stat all;
? ? ? ? ? ? rtmp_stat_stylesheet stat.xsl;
? ? ? ? }
? ? ? ? location /stat.xsl {
? ? ? ? ? ? root nginx-rtmp-module/;
? ? ? ? }
? ? ? ? location /control {
? ? ? ? ? ? rtmp_control all;
? ? ? ? }
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? ## Caching Static Files, put before first location
? ? ? ? #location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
? ? ? ? #? ? expires 14d;
? ? ? ? #? ? add_header Vary Accept-Encoding;
? ? ? ? #}
# For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode
? ? ? ? location / {
? ? ? ? ? ? #include? ? /nginx/conf/mysite.rules; # see also http block naxsi include line
? ? ? ? ? ? ##SecRulesEnabled;
? ? ? ? ##DeniedUrl "/RequestDenied";
? ? ? ? ##CheckRule "$SQL >= 8" BLOCK;
? ? ? ? ##CheckRule "$RFI >= 8" BLOCK;
? ? ? ? ##CheckRule "$TRAVERSAL >= 4" BLOCK;
? ? ? ? ##CheckRule "$XSS >= 8" BLOCK;
? ? ? ? ? ? root? html;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
# For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi
? ? ? ? ##location /RequestDenied {
? ? ? ? ##? ? return 412;
? ? ? ? ##}
## Lua examples !
#? ? ? ? location /robots.txt {
#? ? ? ? ? rewrite_by_lua '
#? ? ? ? ? ? if ngx.var.http_host ~= "localhost" then
#? ? ? ? ? ? ? return ngx.exec("/robots_disallow.txt");
#? ? ? ? ? ? end
#? ? ? ? ? ';
#? ? ? ? }
? ? ? ? #error_page? 404? ? ? ? ? ? ? /404.html;
? ? ? ? # redirect server error pages to the static page /50x.html
? ? ? ? #
? ? ? ? error_page? 500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? html;
? ? ? ? }
? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? proxy_pass? http://127.0.0.1;
? ? ? ? #}
? ? ? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? root? ? ? ? ? html;
? ? ? ? #? ? fastcgi_pass? 127.0.0.1:9000; # single backend process
? ? ? ? #? ? fastcgi_pass? myLoadBalancer; # or multiple, see example above
? ? ? ? #? ? fastcgi_index? index.php;
? ? ? ? #? ? fastcgi_param? SCRIPT_FILENAME? $document_root$fastcgi_script_name;
? ? ? ? #? ? include? ? ? ? fastcgi_params;
? ? ? ? #}
? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? # concurs with nginx's one
? ? ? ? #
? ? ? ? #location ~ /\.ht {
? ? ? ? #? ? deny? all;
? ? ? ? #}
? ? }
? ? # another virtual host using mix of IP-, name-, and port-based configuration
? ? #
? ? #server {
? ? #? ? listen? ? ? 8000;
? ? #? ? listen? ? ? somename:8080;
? ? #? ? server_name? somename? alias? another.alias;
? ? #? ? location / {
? ? #? ? ? ? root? html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
? ? # HTTPS server
? ? #
? ? #server {
? ? #? ? listen? ? ? 443 ssl spdy;
? ? #? ? server_name? localhost;
? ? #? ? ssl? ? ? ? ? ? ? ? ? on;
? ? #? ? ssl_certificate? ? ? cert.pem;
? ? #? ? ssl_certificate_key? cert.key;
? ? #? ? ssl_session_timeout? 5m;
? ? #? ? ssl_prefer_server_ciphers On;
? ? #? ? ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
? ? #? ? ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:ECDH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS:!EXP:!ADH:!LOW:!MEDIUM;
? ? #? ? location / {
? ? #? ? ? ? root? html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
}

到這里,就基本上可以了,然后就是啟動它
這里簡單的介紹一下,在cmd里面一些常用操作nginx的命令
(1)啟動:
start nginx 或 nginx.exe(使用這個會把cmd給卡死,而沒辦法輸入其他命令)
注:建議使用第一種,第二種會使你的cmd窗口一直處于執(zhí)行中,不能進行其他命令操作。
(2)停止:
nginx.exe -s stop
stop是快速停止nginx,可能并不保存相關信息;
nginx.exe -s quit
quit是完整有序的停止nginx,并保存相關信息。
(3)重新載入Nginx:
nginx.exe -s reload
當配置信息修改,需要重新載入這些配置時使用此命令。
(4)重新打開日志文件:
nginx.exe -s reopen
(5)查看Nginx版本:
nginx -v

(最好別用雙擊去啟動這個應用,不然會報錯,操作它都是用命令行的,這里需要注意一下自己的斷口沒有被占用,特別是80端口,
如果有被占用的情況,那就去任務管理器里面kill它,而且需要停掉本身自帶的IIS服務器一般,這個會占用80端口)
這時,打開服務器上的瀏覽器輸入 http://127.0.0.1/,如果你看到這個說明你的nginx服務器已經搭建好了

整個服務器端我們需要注意的,就是我們剛剛寫進去的nginx.conf
文件,而這個文件里面需要注意的就是我們加入的rtmp模塊(俄羅斯人民發(fā)明的nginx,里面本身是沒有rtmp,如果你需要
,就加進去,當然還有其他的模塊,nginx功能還是很強的,具體的話,同志就慢慢探索了)
rtmp {
? ? server {
? ? ? ? listen 1935;
? ? ? ? chunk_size 4000;
? ? ? ? application live {
? ? ? ? ? ? live on;
? ? ? ? }
? ? }
}
Nginx的推流地址格式為:rtmp://云服務器公網IP地址/live/xxxxx(這里的xxxxx為任意字符)
對比我們的rtmp地址rtmp://123.56.9.157:1935/live,這里的1935是監(jiān)聽端口(如果你用的是阿里云的服務器,記住這里是需要自己去開啟1935這個端口的,而且,服務器的防火墻也需要關閉) 其中要重點說一下live,這里的live就是conf配置文件中的application后面的字符,可以任意修改,這里的意思相當于live為直播應用,而xxxxx為直播應用下的房間,可以有很多個。
ok,講到這里就全部結束了,如果在這個過程中入到問題,慢慢的排查,是在沒辦法可以給我留言,我看見就回。
最后附上兩張領袖的照片!哈哈

