use std::time::Duration;
use rtmp_runtime::amf0::{Amf0Limits, Amf0Value};
use rtmp_runtime::chunk::ChunkLimits;
use rtmp_runtime::client::{ClientEvent, ClientLimits, ClientSession, RtmpClientConfig};
use rtmp_runtime::ids::Timestamp;
use rtmp_runtime::server::{RtmpServerConfig, ServerEvent, ServerLimits, ServerSession};
const CHUNK: ChunkLimits = ChunkLimits {
max_chunk_size: 1 << 20,
max_message_len: 1 << 20,
max_csids: 64,
max_in_progress_bytes: 1 << 22,
};
const AMF0: Amf0Limits = Amf0Limits {
max_depth: 16,
max_values: 1024,
};
const VIDEO_TAG_BODY: [u8; 10] = [0x17, 0x01, 0, 0, 0, 0, 0, 0, 1, 0x65];
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut config = RtmpClientConfig::new(
ClientLimits {
chunk: CHUNK,
amf0: AMF0,
},
4096,
2_500_000,
Duration::from_secs(3),
);
config.app = "live".into();
config.stream_key = "camera-1".into();
let mut client = ClientSession::new(config);
let mut server = ServerSession::new(RtmpServerConfig::new(
ServerLimits {
chunk: CHUNK,
amf0: AMF0,
max_command_len: 16 * 1024,
max_failed_publish_attempts: 3,
},
4096,
2_500_000,
2_500_000,
));
let mut to_server = client.start();
let mut media = Vec::new();
for _ in 0..32 {
let (to_client, events) = server.handle_data(&to_server)?;
for event in events {
match event {
ServerEvent::Connected { app } => println!("server: connected to app {app:?}"),
ServerEvent::Publish { stream_key, .. } => {
println!("server: publish {stream_key:?}");
}
ServerEvent::Media { flv } => media.push(flv),
_ => {}
}
}
let (next, events) = client.handle_data(&to_client)?;
to_server = next;
if events.iter().any(|e| matches!(e, ClientEvent::Publishing)) {
println!("client: publishing");
}
if client.is_publishing() && to_server.is_empty() {
break;
}
}
if !client.is_publishing() {
return Err("the publish flow did not complete".into());
}
let mut frames = client.send_metadata(&[
("width".to_owned(), Amf0Value::Number(1280.0)),
("height".to_owned(), Amf0Value::Number(720.0)),
])?;
frames.extend(client.send_video(
Timestamp::try_new(Duration::from_millis(40))?,
&VIDEO_TAG_BODY,
)?);
let (_, events) = server.handle_data(&frames)?;
for event in events {
if let ServerEvent::Media { flv } = event {
media.push(flv);
}
}
let flv: Vec<u8> = media.concat();
println!(
"server produced {} FLV bytes in {} pieces",
flv.len(),
media.len()
);
if !flv.starts_with(b"FLV") || media.len() < 2 {
return Err("expected an FLV header, the metadata tag and the video tag".into());
}
Ok(())
}