crtmpserver系列(二):搭建简易流媒体直播系统

crtmpserver简介

我们在第一章的时候已经简要说明了crtmpserver,crtmpserver是一个由C++语言编写的开源的RTMP流媒体服务器,与其对应的商业产品自然是Adobe公司的FMS。与FMS相比,从功能上来说crtmpserver只能称为FMS的简化版本,其功能并没有FMS那么完善甚至是远远没有达到。其与flash player的兼容性自然也比不上官方的FMS了。但是crtmpserver提供了最常见的RTMP实现。作为开源的高性能RTMP流媒体服务器,不仅可以用在x86平台的linux服务器,windows服务器,还可以被用在arm等嵌入式平台上。crtmpserver的代码结构良好,类的继承体系清楚,代码效率高。是学习RTMP协议和服务器端编程的好例子。

crtmpserver的官方网站是www.rtmpd.com 但是目前该网站好像是出了问题,并不能打开,crtmpserver的google论坛上有网友说crtmpserver的源码暂时移动到github上了,具体地址是https://github.com/shiretu/crtmpserver 如果大家想要下载到最新的crtmpserver源码,可以到该git页面上下载。后续还请关注crtmpserver的官方网站。

本系列文章并不打算使用该git上下载的代码,由于之前工作中使用的crtmpserver代码是基于717的版本,所以该系列也将基于此版本作为讲解。请在这里下载crtmpserver-717.tar.gz 代码包,其关联的openssl代码为 openssl-1.0.1c.tar.gz

linux下编译与运行crtmpserver

本次linux使用的发行版是Ubuntu 10.10,各个编译工具版本如下:
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
cmake version 2.8.7

crtmpserver源码依赖openssl,所以我们需要先编译openssl库,并安装相关的头文件。另外crtmpserver使用的cmake工具进行源代码的编译管理,所以也需要安装cmake。

openssl的编译:本次使用的是openssl-1.0.1c.tar.gz 解压之后在源码根目录直接运行:
$ ./config
$ make
$ make test
$ make install

crtmpserver的编译:解压上面下载到的gz压缩包,运行下面命令:
cd crtmpserver-717/builders/cmake/
cmake .
make

在make的时候报告下面的错误:
1

在文件tinyxmlparser.cpp中有一个变量ptrdiff_t没有定义,我们打开该文件,在第407行将变量ptrdiff_t加上std的前缀即可,改为 std::ptrdiff_t,之后继续make

之后出现下面的错误:
2

说有一个变量objectEncoding定义之后没有使用,而警告被当作错误处理。我们只需要修改一下CMakeFileLists.txt 找到如下这一行,注释掉即可,其实主要是去掉-Werror选项:

3

修改CMakeFileLists.txt之后需要重新cmake 这样会重新生成Makefile文件,然后再make直到完成,最后完成之后的信息是这样的:

4

运行crtmpserver,编译完成之后此时的目录还在crtmpserver-717/builders/cmake/目录下,直接运行下面的命令:
./crtmpserver/crtmpserver ./crtmpserver/crtmpserver.lua

程序成功运行之后的显示如下,表示成功运行:
5

windows下编译与运行crtmpserver

首先在windows下编译crtmpserver也需要windows版本的openssl,所以我们需要先编译windows版本的openssl。

编译windows版本的openssl:将openssl解压之后,有一个INSTALL.W32或者INSTALL.W64,里面有如何编译windows版本的openssl的说明,我们以win32为例子。

编译openssl需要perl的支持,我们先安装windows环境下的perl工具,其下载地址为:http://www.activestate.com/activeperl 我的电脑是64位的,所以我下载的是 ActivePerl-5.24.0.2400-MSWin32-x64-300558.exe 直接安装,在安装的下一步中选择 “Typical”就可以。

安装完成之后,我们就可以开始编译windows版本的openssl了,步骤如下:

<1>. 我用的是VS2010。其他的版本应该差不太多,打开“开始”---“程序”---“Microsoft Visual Studio 2010”---“Visual Studio Tools”---“Visual Studio 命令提示(2010)”,注意这里要从菜单中打开VS 2010的命令行,会自动设置一些环境变量的,如果直接运行“开始”---“运行”--cmd.exe这个命令行的话,可能编译的时候找不到头文件等。
<2>. 在上面打开的VS2010的命令行中,cd到openssl源码根目录,然后运行下面的命令:
perl Configure VC-WIN32 no-asm --prefix=c:/openssl-1.0.1c  
指定配置,并设置openssl的安装目录
<3>. ms\do_ms
<4>. 将nmake.exe所在的路径添加到系统环境变量,例如我找到nmake.exe是在D:\Program Files\Microsoft Visual Studio 10.0\VC\bin下面
<5>. 将D:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE添加到环境变量,因为cl.exe需要的mspdb100.dll在这个目录下,否则会返回0x80错误。同样你也应该在你自己的VS2010的相关安装目录下找。
<6>. nmake -f ms\ntdll.mak
<7>. nmake -f ms\ntdll.mak test 这一步是编译测试,可以不做,仅仅是为了测试上面的是否编译好了
<8>. nmake -f ms\ntdll.mak install  这一步是安装,会安装到我们的第4步指定的 --prefix=c:/openssl-1.0.1c 目录中也就是拷贝相关的库文件到这里

windows下的openssl编译完成的界面如下:
6

安装完成的界面如下:

7

现在开始编译windows版本的crtmpserver,打开crtmpserver的源码目录中的builders\VS2010\VS2010.sln 工程,直接在解决方案上面右键,生成解决方案。会出现错误提示找不到openssl的头文件:

9

在VS2010中crtmpserver的各个模块被单独做为项目工程,很多工程都用到了openssl,所以我们需要设置一个全局的openssl的头文件和库文件的路径。在VS2010中设置全局包含没有VC那么方便,首先打开属性管理器窗口:

9

再打开面板中随便选择一个项目,展开如图的界面,双击如图所示指定的项目文件,并在弹出的界面中指定包含目录路径:

1

用同样的方法指定库文件的路径:

1

继续编译,发现报错如下:

1

将上图中所示的两处地方,0值改成(uint32_t)0; 继续编译,报告一个链接错误,并且还有一些intellisense错误,如下:

1

在crtmpserver官方源码提供的VS2010.sln解决方案中有两个源文件没有添加到工程中来,我们手工添加进来,如下图,打开thelib工程,展开如下图的rtp目录,右键添加现有项目:

 1

选择头文件nattraversalprotocol.h 该文件在源码目录的sources/thelib/include/protocols/rtp/目录下:

1

同样的方法添加cpp文件,cpp文件所在的目录为sources/thelib/src/protocols/rtp/目录:

11

添加完成之后继续生成解决方案,解决方案生成成功,我们需要设置一下运行的命令行参数:--use-implicit-console-appender crtmpserver.lua 表示打印控制台日志,并且使用工作目录下的crtmpserver.lua作为配置文件,将工作目录设置成$(OutDir)该变量对应的目录实际上是crtmpserver-717\builders\VS2010\Debug,我们的程序最终编译之后的执行文件和库文件就放在这个目录下:

1

我们可以打开编译成功之后文件生成的目录中的文件如下,目录为builders/VS2010/Debug/目录:

1

直接点击VS2010的菜单的“开始执行(不调试)”,这样cmd输出窗口最后输出完成之后,会输出一行“请按任意键继续…”否则一闪而过出错也看不清楚。

运行之后干脆报下面的错误:
1

程序直接崩溃,原来openssl的相关dll没有拷贝到我们的程序目录下,我们拷贝过来:

1

继续运行,发现报错如下:

1

我们去掉配置文件中的ssl权限验证相关的内容:

1

注释掉相关节点之后,继续运行,仍然报错:

1

错误显示配置中指定的log的路径设置有问题,继续修改配置如下:

1

再次运行,显示运行成功,界面如下:

1

推流到crtmpserver

编译成功crtmpserver之后,我们试着将直播流推送到服务器上,然后直播。没有摄像头,我们可以使用开源的obs软件推送直播流,下载安装就不说了,相信做过游戏直播的都多少了解。下面是obs打开之后的界面:

1

设置视频捕获源:

1

点击主界面右下角的“设置”按钮进入推流的参数设置,我们主要是设置“串流”选项卡中的内容,crtmpserver的配置文件中默认配置有live这个推流点,所以我们的推流路径是rtmp://127.0.0.1/live/ 随便设置一个直播流名,例如这里设置的是test,设置好之后如下图,点击确定即可。

1

然后点击主界面上的“开始串流”按钮开始 推流,可以看到推流成功之后,在我们的crtmpserver运行的控制台窗口中会有一些日志打印出来:

1

从最后两行的日志中我们可以看到流test已经成功注册。

从crtmpserver播放直播流

如何播放刚才推送的流呢,我们找一个支持rtmp协议的播放器,vlc的最新版本应该是支持rtmp协议的播放的。另外flash播放器也是一个很好的选择,在播放器中输入地址和流名:

1

播放效果如下,截的是电脑桌面:

1

 

到此,我们整个crtmpserver源码的windows平台的编译,linux平台的编译,推流,播放就算完整了。可以看到windows的编译要比linux复杂的多,其实很多开源软件对linux要更加友好一点,包括编译运行的过程都要顺利一点。后面的章节中,我们将开始分析crtmpserver的架构、RTMP协议的细节,MP4文件的格式,最后我们将实现HLS功能(原生的crtmpserver是没有HLS的)。


出处:http://www.cnblogs.com/wangqiguo/p/6014519.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ RTMP Server Instructions how to compile and use C++ RTMP Server (a.k.a crtmpserver) Requirements: * GCC and other C++ tools * SVN * libdl, libssl, libcrypto (make sure you have the "devel" packages of libdl, ssl and crypto installed prior to compiling) In order to get the source code, issue the following command: svn co --username anonymous https://svn.rtmpd.com/crtmpserver/trunk crtmpserver When it asks for password, hit Enter key Compile the package. Do the following steps: cd crtmpserver/builders/cmake cmake . (this step will create all the Makefile's that are required. If some package is missing, the cmake will complain) make The compilation procedure should be finished within few minutes. After you compiled the package, it's time to test it. Run the following command: ./crtmpserver/crtmpserver crtmpserver/crtmpserver.lua If everything goes well, you'll get on your console a table with IP's, ports, protocols, and application names If you see this table, then crtmpserver is working. Lets test it the server. Follow these simple steps: * Download a simple FLV or MP4 file. You can dowload a sample file from here: http://www.mediacollege.com/adobe/flash/video/tutorial/example-flv.html * Place the file you downloaded into the crtmpserver/media folder * Download an FLV player. For this example, we'll use JW Player. Get it here: http://www.longtailvideo.com/players/jw-flv-player * Extract the JW Player to a directory which is accessible through your web server * Go to the extracted directory and create an HTML file which will include the player and play the file. Here's an example: <html> <body> <script type='text/javascript' src='swfobject.js'></script> <div id='mediaspace'>This text will be replaced</div> <script type='text/javascript'> var so = new SWFObject('player.swf','mpl','640','360','9'); so.addParam('allowfullscreen','true'); so.addParam('allowscriptaccess','always'); so.addParam('wmode','opaque'); so.addVariable('file','file-download'); so.addVariable('streamer','rtmp://127.0.0.1/flvplayback/'); so.write('mediaspace'); </script> </body> </html> * Change the 127.0.0.1 to either the IP of your crtmpserver or simply use a hostname of your machine * Replace file-download with the actual filename of your sample you download. Remeber to omit the .flv if it's an FLV file * Open a web browser and point it to to the web server IP/Hostname and the directory you installed the player (example: http://127.0.0.1/player) * You should see a player. Click the play button and the video should be played. If you see the video, then everything works well. Installing crtmpserver: * Go to the directory crtmpserver/cmake * Run the following command: cmake -DCRTMPSERVER_INSTALL_PREFIX=<path> (for example /usr/local/crtmpserver) * After previous command done start build project with command: make * After build comlete run command: make install * After install you has installed crtmpserver in <path>(in our case /usr/local/crtmpserver) * Now you can start crtmpserver with command: sudo <path>/sbin/crtmpserver --uid=<UID> <path>/etc/crtmpserver.lua in our case: sudo /usr/local/crtmpserver/sbin/crtmpserver --uid=<UID> /usr/local/crtmpserver/etc/crtmpserver.lua Also look into builders/packing directory. There are several OS specific builders. * in directory "debian" builder for Debian, also can be used for Ubuntu and other distributions based on debian * in directory "freebsd" builder(port) for FreeBSD crtmpserver settings * All crtmpserver settings are located in a detailed file calle: crtmpserver.lua
编译记录 : ~、 common 工程 缺少 utils/misc/format.cpp utils/logging/formatter.cpp ~、thelib 工程 增加宏定义 “HAS_MEDIA_TS” \crtmpsvr_tr\sources\thelib\src\mediaformats\readers 全部重新组织添加 \crtmpsvr_tr\sources\thelib\src\protocols\ts 去掉 " " 以及相应的头文件。 去掉 protocols 下的 " " 新建筛选器目录“protocols/passthrough”,添加此目录下的两个文件 (可选) \thelib\src\protocols\ts\basetsappprotocolhandler.cpp BaseTSAppProtocolHandler::BaseTSAppProtocolHandler(Variant &configuration) //红标处漏写 -chenlx : BaseAppProtocolHandler(configuration) { \sources\thelib\src\netio\select\tcpcarrier.cpp TCPCarrier::TCPCarrier(int32_t fd) : IOHandler(fd, fd, IOHT_TCP_CARRIER) { //EventLogger::GetDefaultLogger()->LogCarrierCreated(stats); //-chenlx 去掉 netio\select\inboundnamedpipecarrier.cpp .h 添加 \fdstats.cpp JsonVariantProtocol.cpp 新建筛选器目录并添加源文件: thelib\src\recording ~、卸载项目 applestreamingclient,取消crtmpserver对applestreamingclient库的依赖,原因 是trunk中根本就没用到这个项目,对比branches和trunk中的cmake文件,applestreamingclient工程明显被注释。 注意,要先取消crtmpserver对applestreamingclient库的依赖再卸载:在项目crtmpserver上右击属性,在框架和引用上将对applestreamingclient链接库依赖选择false ~、vm \sources\vm\src\basevmlua.cpp 编不过去,提示“luaL_Reg”类型非法 在 \sources\common\include\utils\lua\luautils.h 里引入下列头文件,并重新编译 common.lib extern "C" { #include <lualib.h> #include <lauxlib.h> } ~、 appselector 、 flvplayback :\lab\ztttprj\coder\_svr_voip\streaming_\crtmpsvr_tr\sources\applications\flvplayback\include\tsappprotocolhandler.h(30): error C2504: “BaseTSAppProtocolHandler”: 未定义基类 增加宏定义 “HAS_MEDIA_TS”

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值