harlanc / xiu

A simple,high performance and secure live media server in pure Rust (RTMP[cluster]/RTSP/WebRTC[whip/whep]/HTTP-FLV/HLS).🦀
https://rustxiu.com
MIT License
1.76k stars 187 forks source link

[Documentation] rtmp crate doc out of date #54

Closed Sir-Thom closed 1 year ago

Sir-Thom commented 1 year ago

I was trying to use the recent version the crate rtmp on version 0.4.0 and the cluster and single exemple doesnt work anymore because of probably a refactor thertmp::channels::ChannelsManager do not exist anymore and RtmpServer::new() need a usize.

here cargo compiling error

single:


error[E0432]: unresolved import `rtmp::channels`
--> src/main.rs:1:11
|
1 | use rtmp::channels::ChannelsManager;
|           ^^^^^^^^ could not find `channels` in `rtmp`
error[E0061]: this function takes 3 arguments but 2 arguments were supplied --> src/main.rs:12:27 12 let mut rtmp_server = RtmpServer::new(address, producer); ^^^^^^^^^^^^^^^------------------- an argument of type usize is missing
note: associated function defined here --> /home/thomas/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rtmp-0.4.0/src/rtmp.rs:15:12 15 pub fn new(address: String, event_producer: StreamHubEventSender, gop_num: ... ^^^ help: provide the argument
12 let mut rtmp_server = RtmpServer::new(address, producer, / usize /);
~~~~~~~~

Some errors have detailed explanations: E0061, E0432. For more information about an error, try rustc --explain E0061.


> cluster:

error[E0432]: unresolved import rtmp::channels --> src/main.rs:1:11 | 1 | use rtmp::channels::ChannelsManager; | ^^^^^^^^ could not find channels in rtmp

error[E0061]: this function takes 3 arguments but 2 arguments were supplied --> src/main.rs:45:27 45 let mut rtmp_server = RtmpServer::new(address, producer.clone()); ^^^^^^^^^^^^^^^--------------------------- an argument of type usize is missing
note: associated function defined here --> /home/thomas/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rtmp-0.4.0/src/rtmp.rs:15:12 15 pub fn new(address: String, event_producer: StreamHubEventSender, gop_num: usize) -> Self { ^^^ help: provide the argument
45 let mut rtmp_server = RtmpServer::new(address, producer.clone(), / usize /);
~~~~~~~~


is there other small exemple somewhere ?
thank in advance 
harlanc commented 1 year ago

The updated codes:

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 });
}

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_single_server();
    //start_cluster();
    tokio::signal::ctrl_c().await?;
    Ok(())
}