use std::fmt;
use crate::codec::CodecMedia;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SdpDirection {
Local,
LocalRing,
Remote,
Unknown,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SipInviteDirection {
Receiving,
Sending,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DtmfSource {
Rtp,
Channel,
SipInfo,
}
impl fmt::Display for DtmfSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DtmfSource::Rtp => f.pad("rtp"),
DtmfSource::Channel => f.pad("channel"),
DtmfSource::SipInfo => f.pad("sip-info"),
}
}
}
impl fmt::Display for SdpDirection {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SdpDirection::Local => f.pad("local"),
SdpDirection::LocalRing => f.pad("local-ring"),
SdpDirection::Remote => f.pad("remote"),
SdpDirection::Unknown => f.pad("unknown"),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MessageKind {
Execute {
depth: u32,
channel: String,
application: String,
arguments: String,
},
Dialplan { channel: String, detail: String },
ChannelData,
ChannelField { name: String, value: String },
Variable { name: String, value: String },
SdpMarker { direction: SdpDirection },
StateChange { detail: String },
CodecNegotiation { media: CodecMedia },
Media { detail: String },
ChannelLifecycle { detail: String },
SipInvite {
direction: SipInviteDirection,
profile: String,
call_id: Option<String>,
},
EventSocket { detail: String },
Dtmf {
source: DtmfSource,
digit: char,
duration_ms: Option<u32>,
},
General,
FileChange,
DateChange,
}
impl MessageKind {
pub const ALL_LABELS: &[&str] = &[
"execute",
"dialplan",
"channel-data",
"channel-field",
"variable",
"sdp-marker",
"state-change",
"codec-negotiation",
"media",
"channel-lifecycle",
"sip-invite",
"event-socket",
"dtmf",
"general",
"file-change",
"date-change",
];
pub fn label(&self) -> &'static str {
match self {
MessageKind::Execute { .. } => "execute",
MessageKind::Dialplan { .. } => "dialplan",
MessageKind::ChannelData => "channel-data",
MessageKind::ChannelField { .. } => "channel-field",
MessageKind::Variable { .. } => "variable",
MessageKind::SdpMarker { .. } => "sdp-marker",
MessageKind::StateChange { .. } => "state-change",
MessageKind::CodecNegotiation { .. } => "codec-negotiation",
MessageKind::Media { .. } => "media",
MessageKind::ChannelLifecycle { .. } => "channel-lifecycle",
MessageKind::SipInvite { .. } => "sip-invite",
MessageKind::EventSocket { .. } => "event-socket",
MessageKind::Dtmf { .. } => "dtmf",
MessageKind::General => "general",
MessageKind::FileChange => "file-change",
MessageKind::DateChange => "date-change",
}
}
}
impl fmt::Display for MessageKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MessageKind::Execute { application, .. } => write!(f, "execute({})", application),
MessageKind::Dialplan { .. } => f.pad("dialplan"),
MessageKind::ChannelData => f.pad("channel-data"),
MessageKind::ChannelField { name, .. } => write!(f, "field({})", name),
MessageKind::Variable { name, .. } => write!(f, "var({})", name),
MessageKind::SdpMarker { direction } => write!(f, "sdp({})", direction),
MessageKind::StateChange { .. } => f.pad("state-change"),
MessageKind::CodecNegotiation { media } => {
f.pad(&format!("codec-negotiation({media})"))
}
MessageKind::Media { .. } => f.pad("media"),
MessageKind::ChannelLifecycle { .. } => f.pad("channel-lifecycle"),
MessageKind::SipInvite { .. } => f.pad("sip-invite"),
MessageKind::EventSocket { .. } => f.pad("event-socket"),
MessageKind::Dtmf {
source,
digit,
duration_ms,
} => match duration_ms {
Some(ms) => write!(f, "dtmf({source}:{digit}:{ms}ms)"),
None => write!(f, "dtmf({source}:{digit})"),
},
MessageKind::General => f.pad("general"),
MessageKind::FileChange => f.pad("file-change"),
MessageKind::DateChange => f.pad("date-change"),
}
}
}