27个版本

1.0.0 2023年1月21日
0.6.5 2024年8月11日
0.6.4 2024年5月17日
0.6.3 2024年3月15日
0.0.5 2021年6月24日

#296 in 视频

Download history 6/week @ 2024-04-27 39/week @ 2024-05-11 184/week @ 2024-05-18 36/week @ 2024-05-25 38/week @ 2024-06-01 18/week @ 2024-06-08 26/week @ 2024-06-15 22/week @ 2024-06-22 136/week @ 2024-06-29 29/week @ 2024-07-06 21/week @ 2024-07-13 13/week @ 2024-07-20 124/week @ 2024-07-27 10/week @ 2024-08-03 155/week @ 2024-08-10

每月306次下载
用于 2 个库

MIT 许可证

505KB
12K SLoC

简介

这是一个简单的rtmp库,易于使用和阅读,您可以构建自己的单个rtmp服务器或集群。

示例

客户端

您可以使用此库推送或拉取RTMP流,请参考xiu/application/pprtmp中的pprtmp示例。

单个服务器

use rtmp::{
    relay::{pull_client::PullClient, push_client::PushClient},
    rtmp::RtmpServer,
};

 use {anyhow::Result, streamhub::StreamsHub};

fn start_single_server() {
    let mut stream_hub = StreamsHub::new(None);
    let sender = stream_hub.get_hub_event_sender();

    let listen_port = 1935;
    let address = format!("0.0.0.0:{port}", port = listen_port);

    let mut rtmp_server = RtmpServer::new(address, sender, 1);
    tokio::spawn(async move {
        if let Err(err) = rtmp_server.run().await {
            log::error!("rtmp server error: {}\n", err);
        }
    });

    tokio::spawn(async move { stream_hub.run().await });
}

#[tokio::main]

async fn main() -> Result<()> {
    start_single_server();
    //start_cluster();
    tokio::signal::ctrl_c().await?;
    Ok(())
}

集群

use rtmp::{
    relay::{pull_client::PullClient, push_client::PushClient},
    rtmp::RtmpServer,
};

 use {anyhow::Result, streamhub::StreamsHub};

fn start_cluster() {
    let mut stream_hub = StreamsHub::new(None);
    let sender = stream_hub.get_hub_event_sender();

    // push the rtmp stream from local to 192.168.0.2:1935
    let address = format!("{ip}:{port}", ip = "192.168.0.2", port = 1935);

    let mut push_client = PushClient::new(
        address,
        stream_hub.get_client_event_consumer(),
        sender.clone(),
    );
    tokio::spawn(async move {
        if let Err(err) = push_client.run().await {
            log::error!("push client error {}\n", err);
        }
    });
    stream_hub.set_rtmp_push_enabled(true);

    //pull the rtmp stream from 192.168.0.3:1935 to local
    let address = format!("{ip}:{port}", ip = "192.168.0.3", port = "1935");
    log::info!("start rtmp pull client from address: {}", address);
    let mut pull_client = PullClient::new(
        address,
        stream_hub.get_client_event_consumer(),
        sender.clone(),
    );

    tokio::spawn(async move {
        if let Err(err) = pull_client.run().await {
            log::error!("pull client error {}\n", err);
        }
    });
    stream_hub.set_rtmp_pull_enabled(true);

    // the local rtmp server
    let listen_port = 1935;
    let address = format!("0.0.0.0:{port}", port = listen_port);

    let mut rtmp_server = RtmpServer::new(address, sender.clone(), 1);
    tokio::spawn(async move {
        if let Err(err) = rtmp_server.run().await {
            log::error!("rtmp server error: {}\n", err);
        }
    });

    tokio::spawn(async move { stream_hub.run().await });
}

#[tokio::main]

async fn main() -> Result<()> {
    start_cluster();
    tokio::signal::ctrl_c().await?;
    Ok(())
}

有关更详细的实现,请参考xiu服务器

依赖项

~13–30MB
~488K SLoC