use super::*;
pub const DEFAULT_SERVER_PORT: u16 = 1935;
pub const DEFAULT_READ_TIMEOUT: Duration = Duration::from_secs(30);
pub const DEFAULT_WRITE_TIMEOUT: Duration = Duration::from_secs(30);
pub const DEFAULT_CHUNK_SIZE: u32 = 4096;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PublishType {
Live,
Record,
Append,
}
impl PublishType {
#[must_use]
pub fn from_str(s: &str) -> Option<Self> {
match s {
"live" => Some(Self::Live),
"record" => Some(Self::Record),
"append" => Some(Self::Append),
_ => None,
}
}
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::Live => "live",
Self::Record => "record",
Self::Append => "append",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AuthResult {
Success,
Failed(String),
}
#[async_trait]
pub trait AuthHandler: Send + Sync {
async fn authenticate_connect(
&self,
app: &str,
tc_url: &str,
params: &HashMap<String, String>,
) -> AuthResult;
async fn authenticate_publish(
&self,
app: &str,
stream_key: &str,
publish_type: PublishType,
) -> AuthResult;
async fn authenticate_play(&self, app: &str, stream_key: &str) -> AuthResult;
}
#[derive(Debug, Clone, Copy)]
pub struct AllowAllAuth;
#[async_trait]
impl AuthHandler for AllowAllAuth {
async fn authenticate_connect(
&self,
_app: &str,
_tc_url: &str,
_params: &HashMap<String, String>,
) -> AuthResult {
AuthResult::Success
}
async fn authenticate_publish(
&self,
_app: &str,
_stream_key: &str,
_publish_type: PublishType,
) -> AuthResult {
AuthResult::Success
}
async fn authenticate_play(&self, _app: &str, _stream_key: &str) -> AuthResult {
AuthResult::Success
}
}
#[derive(Debug, Clone)]
pub struct MediaPacket {
pub packet_type: MediaPacketType,
pub timestamp: u32,
pub stream_id: u32,
pub data: Bytes,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MediaPacketType {
Audio,
Video,
Data,
}
#[derive(Debug, Clone)]
pub struct StreamMetadata {
pub stream_key: String,
pub app_name: String,
pub width: Option<u32>,
pub height: Option<u32>,
pub frame_rate: Option<f64>,
pub video_codec: Option<String>,
pub audio_codec: Option<String>,
pub metadata: HashMap<String, AmfValue>,
}
impl StreamMetadata {
#[must_use]
pub fn new(stream_key: impl Into<String>, app_name: impl Into<String>) -> Self {
Self {
stream_key: stream_key.into(),
app_name: app_name.into(),
width: None,
height: None,
frame_rate: None,
video_codec: None,
audio_codec: None,
metadata: HashMap::new(),
}
}
pub fn update_from_amf(&mut self, metadata: &AmfValue) {
if let Some(obj) = metadata.as_object() {
if let Some(AmfValue::Number(w)) = obj.get("width") {
self.width = Some(*w as u32);
}
if let Some(AmfValue::Number(h)) = obj.get("height") {
self.height = Some(*h as u32);
}
if let Some(AmfValue::Number(fps)) = obj.get("framerate") {
self.frame_rate = Some(*fps);
}
if let Some(codec) = obj.get("videocodecid").and_then(AmfValue::as_str) {
self.video_codec = Some(codec.to_string());
}
if let Some(codec) = obj.get("audiocodecid").and_then(AmfValue::as_str) {
self.audio_codec = Some(codec.to_string());
}
}
}
}