
作者目前工作在音视频流媒体行业,用了大概一年的业余时间学习Rust,并且实现了一个简单的音视频流媒体服务,虽然据说Rust已经连续多年被评为最受程序员喜欢的语言,但是在国内还是比较冷门,作者比较看好Rust的未来,如果你也对Rust感兴趣,同时也对音视频感兴趣,推荐你看看这个项目,下面是项目介绍:
XIU是用纯Rust开发的一款简单和安全的流媒体服务器,目前支持流行的三大流媒体协议包括RTMP/HLS/HTTPFLV(将来有可能支持其它协议),可以单点部署,也可以用relay功能来部署集群。
Document
有两种方式来安装xiu:
执行下面的命令来安转xiu:
cargo install xiu执行下面的命令来启动服务:
xiu configuration_file_path/config.tomlgit clone https://github.com/harlanc/xiu.gitCheckout最新发布的版本代码:
git checkout tags/<tag_name> -b <branch_name>cd ./xiu/application/xiucargo build --releasecd ./xiu/target/release./xiu config.toml[rtmp]enabled = trueport = 1935# pull streams from other server node.[rtmp.pull]enabled = falseaddress = "192.168.0.1"port = 1935# push streams to other server node.[[rtmp.push]]enabled = trueaddress = "localhost"port = 1936[[rtmp.push]]enabled = trueaddress = "192.168.0.3"port = 1935[httpflv]# true or false to enable or disable the featureenabled = true# listening portport = 8081[hls]# true or false to enable or disable the featureenabled = true# listening portport = 8080[log]level = "info"有一些现成的配置文件放在下面的目录:
xiu/application/xiu/src/config包括4个配置文件:
config_rtmp.toml //只打开rtmpconfig_rtmp_hls.toml //打开 rtmp 和 hlsconfig_rtmp_httpflv.toml //打开 rtmp 和 httpflvconfig_rtmp_httpflv_hls.toml //打开所有的 3 个协议可以用任何推流软件或者命令工具来推RTMP流,比如使用OBS或者用ffmpeg命令行:
ffmpeg -re -stream_loop -1 -i test.mp4 -c:a copy -c:v copy -f flv -flvflags no_duration_filesize rtmp://127.0.0.1:1935/live/test使用ffplay来播放 rtmp/httpflv/hls协议的直播流:
ffplay -i rtmp://localhost:1935/live/testffplay -i http://localhost:8081/live/test.flvffplay -i http://localhost:8080/live/test/test.m3u8应用场景为边缘节点的直播流被转推到源站,配置如下:
边缘节点的配置文件config_push.toml:
[rtmp]enabled = trueport = 1935[[rtmp.push]]enabled = trueaddress = "localhost"port = 1936源站节点的配置文件config.toml:
[rtmp]enabled = trueport = 1936启动两个服务:
./xiu config.toml./xiu config_push.toml将一路RTMP直播流推送到边缘节点,此直播流会被自动转推到源站,可以同时播放源站或者边缘节点的直播流:
ffplay -i rtmp://localhost:1935/live/testffplay -i rtmp://localhost:1936/live/test应用场景为播放过程中用户从边缘节点拉流,边缘节点无此流,则回源拉流,配置文件如下:
源站节点的配置文件为 config.toml:
[rtmp]enabled = trueport = 1935边缘节点的配置文件为 config_pull.toml:
[rtmp]enabled = trueport = 1936[rtmp.pull]enabled = falseaddress = "localhost"port = 1935运行两个服务:
./xiu config.toml./xiu config_pull.toml直接将直播流推送到源站,到边缘节点请求此路直播流,边缘节点会回源拉流,可以同时播放边缘和源站节点上的直播流:
ffplay -i rtmp://localhost:1935/live/testffplay -i rtmp://localhost:1936/live/testlink
有任何问题请在issues提问,欢迎star和提pull request。你的关注可以让此项目走的更快更远。
作者:HarlanC
博客地址:http://www.cnblogs.com/harlanc/
个人博客: http://www.harlancn.me/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接