作者目前工作在音視頻流媒體行業,用了大概一年的業余時間學習Rust,並且實現了一個簡單的音視頻流媒體服務,雖然據說Rust已經連續多年被評為最受程序員喜歡的語言,但是在國內還是比較冷門,作者比較看好Rust的未來,如果你也對Rust感興趣,同時也對音視頻感興趣,推薦你看看這個項目,下面是項目介紹:
項目地址: https://github.com/harlanc/xiu
XIU是用純Rust開發的一款簡單和安全的流媒體服務器,目前支持流行的三大流媒體協議包括RTMP/HLS/HTTPFLV(將來有可能支持其它協議),可以單點部署,也可以用relay功能來部署集群。
功能
- RTMP
- 發布直播流和播放直播流
- 轉發:靜態轉推和靜態回源
- HTTPFLV
- HLS
准備工作
安裝 Rust and Cargo
安裝和運行
有兩種方式來安裝xiu:
- 直接用cargo來安裝
- 源碼編譯安裝
用cargo命令安裝
執行下面的命令來安轉xiu:
cargo install xiu
執行下面的命令來啟動服務:
xiu configuration_file_path/config.toml
源碼編譯安裝
克隆 Xiu
git clone https://github.com/harlanc/xiu.git
Checkout最新發布的版本代碼:
git checkout tags/<tag_name> -b <branch_name>
編譯
cd ./xiu/application/xiu
cargo build --release
運行
cd ./xiu/target/release
./xiu config.toml
配置
RTMP
[rtmp]
enabled = true
port = 1935
# pull streams from other server node.
[rtmp.pull]
enabled = false
address = "192.168.0.1"
port = 1935
# push streams to other server node.
[[rtmp.push]]
enabled = true
address = "localhost"
port = 1936
[[rtmp.push]]
enabled = true
address = "192.168.0.3"
port = 1935
HTTPFLV
[httpflv]
# true or false to enable or disable the feature
enabled = true
# listening port
port = 8081
HLS
[hls]
# true or false to enable or disable the feature
enabled = true
# listening port
port = 8080
Log
[log]
level = "info"
一些配置的例子
有一些現成的配置文件放在下面的目錄:
xiu/application/xiu/src/config
包括4個配置文件:
config_rtmp.toml //只打開rtmp
config_rtmp_hls.toml //打開 rtmp 和 hls
config_rtmp_httpflv.toml //打開 rtmp 和 httpflv
config_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/test
ffplay -i http://localhost:8081/live/test.flv
ffplay -i http://localhost:8080/live/test/test.m3u8
轉發 - 靜態轉推
應用場景為邊緣節點的直播流被轉推到源站,配置如下:
邊緣節點的配置文件config_push.toml:
[rtmp]
enabled = true
port = 1935
[[rtmp.push]]
enabled = true
address = "localhost"
port = 1936
源站節點的配置文件config.toml:
[rtmp]
enabled = true
port = 1936
啟動兩個服務:
./xiu config.toml
./xiu config_push.toml
將一路RTMP直播流推送到邊緣節點,此直播流會被自動轉推到源站,可以同時播放源站或者邊緣節點的直播流:
ffplay -i rtmp://localhost:1935/live/test
ffplay -i rtmp://localhost:1936/live/test
轉發 - 靜態回源
應用場景為播放過程中用戶從邊緣節點拉流,邊緣節點無此流,則回源拉流,配置文件如下:
源站節點的配置文件為 config.toml:
[rtmp]
enabled = true
port = 1935
邊緣節點的配置文件為 config_pull.toml:
[rtmp]
enabled = true
port = 1936
[rtmp.pull]
enabled = false
address = "localhost"
port = 1935
運行兩個服務:
./xiu config.toml
./xiu config_pull.toml
直接將直播流推送到源站,到邊緣節點請求此路直播流,邊緣節點會回源拉流,可以同時播放邊緣和源站節點上的直播流:
ffplay -i rtmp://localhost:1935/live/test
ffplay -i rtmp://localhost:1936/live/test
Star History
鳴謝
其它
有任何問題請在issues提問,歡迎star和提pull request。你的關注可以讓此項目走的更快更遠。