从零搭建流媒体服务器+obs推流直播

2019/05/14 16:15
阅读数 256

<div id="content_views" class="markdown_views"> <!-- flowchart 箭头图标 勿删 --> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path></svg> <h2 id="背景介绍"><a name="t0"></a>背景介绍</h2>

<blockquote> <p>本文使用的流媒体服务器的搭建是基于rtmp(Real Time Message Protocol)协议的,rtmp协议是应用层的协议,要依靠底层的传输层协议,比如tcp协议来保证信息传输的可靠性。最后提供了一个不错的测试方案。</p>

<p>流媒体服务器依赖的服务,1.nginx 服务器;2.nginx服务器安装需要依赖的服务 OpenSSL、pcre、zlib、 c++、gcc等。</p>

<p>服务器环境是Centos 7.3 64 位,本地操作系统 win7 旗舰版</p> </blockquote>

<h2 id="注意事项"><a name="t1"></a>注意事项</h2>

<blockquote> <p>需要资源:1.一台云服务器;2.一台能上网的电脑 <br> 需要注意的是本机搭建使用的是Centos 7.X的版本,iptables命令等使用方式有变动,如需修改参照<a href="http://blog.csdn.net/qzcsu/article/details/72547861#t1" rel="nofollow" target="_blank">3.I 中修改防火墙操作方式</a> <br> 由于本机上先安装的Apache,而nginx和Apache都是默认的80端口,所以防止冲突本文修改了nginx的默认端口</p> </blockquote>

<h2 id="安装nginx"><a name="t2"></a>安装nginx</h2>

<blockquote> <p>首先需要注意的是虽然nginx可以使用yum安装,但是yum安装使用的都是编译后的文件进行安装,并且后面所需要的rtmp模块一般在yum源里面是找不到的,所以要想将rtmp也安装进来,我们选择源码安装,经过步骤配置,编译,安装。 <br> 本文是使用nginx源码,自行编译安装。首先是下载源码,由于nginx的代码是开源托管在github上,我们先用yum 将git下载下来,然后使用git将源码从github上clone下来</p> </blockquote>

<pre class="prettyprint" name="code"><code class="hljs bash has-numbering" onclick="mdcp.signin(event)"><span class="hljs-built_in">cd</span> mkdir <span class="hljs-built_in">source</span> <span class="hljs-comment">#创建源码目录 后面的源码都放在这个目录</span> <span class="hljs-built_in">cd</span> <span class="hljs-built_in">source</span> yum -y install git <span class="hljs-comment">#安装git</span> git clone https://github.com/nginx/nginx.git <span class="hljs-comment">#从github服务器上将nginx的源代码下载下来</span> git clone https://github.com/arut/nginx-rtmp-module.git <span class="hljs-comment">#将rtmp模块的源码下载下来</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>下载依赖模块源码pcre、OpenSSL、zlib 如果机器上已经安装了这些模块就不需要了</p>

<pre class="prettyprint" name="code"><code class="hljs perl has-numbering" onclick="mdcp.signin(event)">rpm -qa|<span class="hljs-keyword">grep</span> 模块名字 <span class="hljs-comment">#查询安装的模块的包信息</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<pre class="prettyprint" name="code"><code class="hljs avrasm has-numbering" onclick="mdcp.signin(event)">wget https://www<span class="hljs-preprocessor">.openssl</span><span class="hljs-preprocessor">.org</span>/source/openssl-<span class="hljs-number">1.1</span><span class="hljs-number">.0</span><span class="hljs-preprocessor">.tar</span><span class="hljs-preprocessor">.gz</span> <span class="hljs-preprocessor">#下载OpenSSL源码包</span> wget https://ftp<span class="hljs-preprocessor">.pcre</span><span class="hljs-preprocessor">.org</span>/pub/pcre/pcre-<span class="hljs-number">8.39</span><span class="hljs-preprocessor">.tar</span><span class="hljs-preprocessor">.gz</span> <span class="hljs-preprocessor">#下载pcre源码包</span> wget http://www<span class="hljs-preprocessor">.zlib</span><span class="hljs-preprocessor">.net</span>/zlib-<span class="hljs-number">1.2</span><span class="hljs-number">.11</span><span class="hljs-preprocessor">.tar</span><span class="hljs-preprocessor">.gz</span> <span class="hljs-preprocessor">#下载zlib包源码</span><div class="hljs-button signin" data-title="登录后复制"></div></code>/pre> <pre class="prettyprint" name="code"><code class="hljs bash has-numbering" onclick="mdcp.signin(event)">tar -zxvf 包名 <span class="hljs-comment">#解压各个包源码</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>附一张目录结构图 <br> <img src="https://img-blog.csdn.net/20170527122342724?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="目录结构" title=""></p>

<p>找到nginx源码包中 configure 所在的目录,以下脚本就是基于configure来实现的,在本机上configure 命令就在auto 目录下,当前目录 /root/source/nginx 就在这个目录下编写脚本vi config.sh 加入以下内容</p>

<pre class="prettyprint" name="code"><code class="hljs lasso has-numbering" onclick="mdcp.signin(event)"><span class="hljs-built_in">.</span>/auto/configure <span class="hljs-subst">--</span>prefix<span class="hljs-subst">=</span>/usr/<span class="hljs-built_in">local</span>/nginx <span class="hljs-subst">\</span> <span class="hljs-subst">--</span><span class="hljs-keyword">with</span><span class="hljs-attribute">-pcre</span><span class="hljs-subst">=</span><span class="hljs-built_in">..</span>/pcre<span class="hljs-subst">-</span><span class="hljs-number">8.39</span> <span class="hljs-subst">\</span> <span class="hljs-subst">--</span><span class="hljs-keyword">with</span><span class="hljs-attribute">-openssl</span><span class="hljs-subst">=</span><span class="hljs-built_in">..</span>/openssl<span class="hljs-subst">-</span><span class="hljs-number">1.1</span><span class="hljs-number">.0</span> <span class="hljs-subst">\</span> <span class="hljs-subst">--</span><span class="hljs-keyword">with</span><span class="hljs-attribute">-zlib</span><span class="hljs-subst">=</span><span class="hljs-built_in">..</span>/zlib<span class="hljs-subst">-</span><span class="hljs-number">1.2</span><span class="hljs-number">.11</span> <span class="hljs-subst">\</span> <span class="hljs-subst">--</span><span class="hljs-keyword">with</span><span class="hljs-attribute">-http_v2_module</span> <span class="hljs-subst">\</span> <span class="hljs-subst">--</span><span class="hljs-keyword">with</span><span class="hljs-attribute">-http_flv_module</span> <span class="hljs-subst">\</span> <span class="hljs-subst">--</span><span class="hljs-keyword">with</span><span class="hljs-attribute">-http_mp4_module</span> <span class="hljs-subst">\</span> <span class="hljs-subst">--</span>add<span class="hljs-attribute">-module</span><span class="hljs-subst">=</span><span class="hljs-built_in">..</span>/nginx<span class="hljs-attribute">-rtmp</span><span class="hljs-attribute">-module</span><span class="hljs-subst">/</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>保存后给文件赋予操作权限,再执行</p>

<pre class="prettyprint" name="code"><code class="hljs avrasm has-numbering" onclick="mdcp.signin(event)">chmod <span class="hljs-number">777</span> config<span class="hljs-preprocessor">.sh</span> <span class="hljs-preprocessor">#赋予权限</span> ./config<span class="hljs-preprocessor">.sh</span> <span class="hljs-preprocessor">#执行脚本</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>检查配置没有问题 <br> <img src="https://img-blog.csdn.net/20170527152759817?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="配置检查通过" title=""></p>

<pre class="prettyprint" name="code"><code class="hljs cmake has-numbering" onclick="mdcp.signin(event)">yum -y <span class="hljs-keyword">install</span> gcc <span class="hljs-comment">#确保依赖的gcc安装</span> yum -y <span class="hljs-keyword">install</span> gcc-c++ <span class="hljs-comment">#确保依赖的c++已经安装</span> make <span class="hljs-comment">#编译</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p>显示如下则编译成功 <br> <img src="https://img-blog.csdn.net/20170527153253829?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="编译成功" title=""></p>

<pre class="prettyprint" name="code"><code class="hljs cmake has-numbering" onclick="mdcp.signin(event)">make <span class="hljs-keyword">install</span> <span class="hljs-comment">#安装</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<blockquote> <p>安装成功 <br> <img src="https://img-blog.csdn.net/20170527153345674?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="安装成功" title=""></p>

<p>同样需要 1.云服务器控制台对相应的端口开放;2.并且Linux机器对默认端口80开放(如果想修改默认端口自行百度),由于本机80端口被Apache 服务器占用了,所以将nginx默认端口改为8084 </p> </blockquote>

<pre class="prettyprint" name="code"><code class="hljs perl has-numbering" onclick="mdcp.signin(event)">iptables -I INPUT -p tcp -<span class="hljs-keyword">m</span> <span class="hljs-keyword">state</span> --<span class="hljs-keyword">state</span> NEW --dport <span class="hljs-number">8084</span> -j ACCEPT <span class="hljs-comment">#放行8084端口的 NEW状态请求(本机的RELATED,ESTABLISHED状态是默认放行的)</span> service iptables save <span class="hljs-comment">#保存规则</span> service iptables restart <span class="hljs-comment">#重启防火墙保证新的规则加载进来</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p><img src="https://img-blog.csdn.net/20170527144413013?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="防火墙规则链" title=""></p>

<blockquote> <p>测试nginx是否安装成功 ip:port 如果出现以下界面说明安装成功 <br> <img src="https://img-blog.csdn.net/20170527154230295?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="nginx配置成功" title=""></p> </blockquote>

<h2 id="配置rtmp"><a name="t3"></a>配置rtmp</h2>

<blockquote> <p>在nginx配置文件中配置rtmp服务,记住rtmp服务是和http服务是平级的,所以我们需要在和http配置平级的位置另起rtmp服务</p> </blockquote>

<pre class="prettyprint" name="code"><code class="hljs livecodeserver has-numbering" onclick="mdcp.signin(event)">vi /usr/<span class="hljs-built_in">local</span>/nginx/conf/nginx.conf <span class="hljs-comment">#修改配置文件</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<blockquote> <p>在配置文件末尾加入以下内容(括号嵌套切记不要弄混)</p> </blockquote>

<pre class="prettyprint" name="code"><code class="hljs coffeescript has-numbering" onclick="mdcp.signin(event)"><span class="hljs-comment"># rtmp config</span> rtmp { server { listen <span class="hljs-number">1935</span>; chunk_size <span class="hljs-number">4096</span>; application live { live <span class="hljs-literal">on</span>; record <span class="hljs-literal">off</span>; } application live2 { live <span class="hljs-literal">on</span>; record <span class="hljs-literal">off</span>; } application vod { play /<span class="hljs-reserved">var</span>/flvs; } application vod_http { play <span class="hljs-attribute">http</span>:<span class="hljs-regexp">//</span>服务器的ip/vod; } application hls { live <span class="hljs-literal">on</span>; hls <span class="hljs-literal">on</span>; hls_path /tmp/hls; } } }<div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<pre class="prettyprint" name="code"><code class="hljs bash has-numbering" onclick="mdcp.signin(event)">/usr/local/nginx/sbin/nginx <span class="hljs-operator">-s</span> reload <span class="hljs-comment">#修改配置文件重启nginx服务</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<h3 id="测试rtmp端口是否通"><a name="t4"></a>测试rtmp端口是否通</h3>

<blockquote> <p>端口放行需要有三个条件,1.云服务的安全组放行;2.防火墙iptables放行;3.服务本身放行</p> </blockquote>

<p><strong><em>安全组</em></strong> </p>

<blockquote> <p>登录云服务器控制台进行设置 <br> <img src="https://img-blog.csdn.net/20170527160627321?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="云服务器放行的端口" title=""></p> </blockquote>

<p><strong><em>防火墙</em></strong></p>

<pre class="prettyprint" name="code"><code class="hljs perl has-numbering" onclick="mdcp.signin(event)">iptables -I INPUT -p tcp -<span class="hljs-keyword">m</span> <span class="hljs-keyword">state</span> --<span class="hljs-keyword">state</span> NEW --dport <span class="hljs-number">1935</span> -j ACCEPT <span class="hljs-comment">#放行1935端口的 NEW状态请求</span> service iptables save <span class="hljs-comment">#保存规则</span> service iptables restart <span class="hljs-comment">#重启防火墙保证新的规则加载进来</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p><img src="https://img-blog.csdn.net/20170527160936526?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="新加入的规则" title=""></p>

<p><strong><em>服务本身</em></strong></p>

<blockquote> <p>由于nginx服务我们已经配置过1935端口,所以是放行状态的,我们可以在windows本地使用telnet 命令测试端口是否通</p> </blockquote>

<p><img src="https://img-blog.csdn.net/20170527161232667?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="telnet测试端口" title=""></p>

<blockquote> <p>如果出现一下界面说明端口已经通了 <br> <img src="https://img-blog.csdn.net/20170527161607638?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" title=""></p> </blockquote>

<hr>

<h2 id="使obs推流"><a name="t5"></a>使OBS推流</h2>

<blockquote> <p>OBS(Open Broadcaster Software) 是以互联网流媒体直播内容为目的免费和开放源码软件。需要下载这个软件,借助这个软件进行推流(电脑没有摄像头的貌似安装不了。。。)</p>

<p>就长这个样子 <br> <img src="https://img-blog.csdn.net/20170527162301459?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" title=""></p>

<p>打开后我们需要有一个场景,并且在这个场景下有一个流的来源(可以是窗口,如果选的是视频则会自动识别摄像头),接下来就是设置了</p> </blockquote>

<p><img src="https://img-blog.csdn.net/20170527162713622?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" title=""></p>

<blockquote> <p>在配置中最需要关注的就是流的配置了,由于是自建的流媒体服务器所以我们照这样配置</p> </blockquote>

<pre class="prettyprint" name="code"><code class="hljs ruby has-numbering" onclick="mdcp.signin(event)"><span class="hljs-symbol">rtmp:</span>/<span class="hljs-regexp">/你的服务器ip:端口(1935)/live</span> <span class="hljs-comment">#URL填写流的地址</span><div class="hljs-button signin" data-title="登录后复制"></div></code></pre>

<p><img src="https://img-blog.csdn.net/20170527163036334?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" title=""></p>

<blockquote> <p>设置完成我们就可以 开始推流了</p> </blockquote>

<h3 id="拉流测试地址"><a name="t6"></a>拉流测试地址</h3>

<blockquote> <p>推荐一个拉流的测试地址,里面针对各种协议都能测试<a href="https://www.wowza.com/testplayers" rel="nofollow" target="_blank">拉流测试</a>,需要注意图中几个地方,由于我们使用的rtmp协议,我们选择这一栏,底下填写我们推流的地址和我们在上面obs的设置里面配置的流的名称,start, ok搞定!!!!</p> </blockquote>

<p><img src="https://img-blog.csdn.net/20170527163539696?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXpjc3U=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" title=""></p>

<hr>

<h2 id="总结"><a name="t7"></a>总结</h2>

<blockquote> <p>本文用nginx作为服务器,使用rtmp协议简述了的搭建了一个流媒体服务器的过程,并且描述了一个简单的测试流程。其中调试端口的可用性,安装过程服务的依赖处理,修改配置需要重启服务,是常见的问题,需要特别注意。</p>

<p>笔者也是第一次搭建,不足之处,欢迎留言,一起交流!</p> </blockquote> </div>

展开阅读全文
打赏
1
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
1
分享
返回顶部
顶部