Skip to main content

str0m/
error.rs

1//! Various error types.
2
3use std::error::Error;
4use std::fmt;
5
6// Re-export all error types for convenience
7pub use crate::crypto::DtlsError;
8pub use crate::io::NetError;
9pub use crate::packet::{PacketError, Vp8DescriptorError, Vp8PatchError};
10pub use crate::rtp_::RtpError;
11pub use crate::sctp::ProtoError;
12pub use crate::sctp::SctpError;
13pub use crate::sdp::SdpError;
14pub use is::IceError;
15
16use crate::{Direction, KeyframeRequestKind, Mid, Pt, Rid};
17
18/// Errors for the whole Rtc engine.
19#[derive(Debug)]
20#[non_exhaustive]
21pub enum RtcError {
22    /// Some problem with the remote SDP.
23    RemoteSdp(String),
24
25    /// SDP errors.
26    Sdp(SdpError),
27
28    /// RTP errors.
29    Rtp(RtpError),
30
31    /// Other IO errors.
32    Io(std::io::Error),
33
34    /// DTLS errors
35    Dtls(DtlsError),
36
37    /// RTP packetization, depacketization, or payload validation error.
38    Packet(Mid, Pt, PacketError),
39
40    /// The PT attempted to write to is not known.
41    UnknownPt(Pt),
42
43    /// The Rid attempted to write is not known.
44    UnknownRid(Rid),
45
46    /// If MediaWriter.write fails because we can't find an SSRC to use.
47    NoSenderSource,
48
49    /// Using `write_rtp` for a stream with RTX without providing a rtx_pt.
50    ResendRequiresRtxPt,
51
52    /// Direction does not allow sending of Media data.
53    NotSendingDirection(Direction),
54
55    /// Direction does not allow receiving media data.
56    NotReceivingDirection,
57
58    /// If MediaWriter.request_keyframe fails because we can't find an SSRC to use.
59    NoReceiverSource(Option<Rid>),
60
61    /// The keyframe request failed because the kind of request is not enabled
62    /// in the media.
63    FeedbackNotEnabled(KeyframeRequestKind),
64
65    /// Parser errors from network packet parsing.
66    Net(NetError),
67
68    /// ICE agent errors.
69    Ice(IceError),
70
71    /// SCTP (data channel engine) errors.
72    Sctp(SctpError),
73
74    /// [`crate::change::SdpApi`] was not done in a correct order.
75    ///
76    /// For [`crate::change::SdpApi`]:
77    ///
78    /// 1. We created an [`crate::change::SdpOffer`].
79    /// 2. The remote side created an [`crate::change::SdpOffer`] at the same time.
80    /// 3. We applied the remote side [`crate::change::SdpApi::accept_offer`].
81    /// 4. The we used the [`crate::change::SdpPendingOffer`] created in step 1.
82    ChangesOutOfOrder,
83
84    /// The [`crate::media::Writer`] was used twice without doing [`crate::Rtc::poll_output`] in between. This
85    /// is an incorrect usage pattern of the str0m API.
86    WriteWithoutPoll,
87}
88
89impl fmt::Display for RtcError {
90    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91        match self {
92            RtcError::RemoteSdp(msg) => write!(f, "remote sdp: {}", msg),
93            RtcError::Sdp(err) => write!(f, "{}", err),
94            RtcError::Rtp(err) => write!(f, "{}", err),
95            RtcError::Io(err) => write!(f, "{}", err),
96            RtcError::Dtls(err) => write!(f, "{}", err),
97            RtcError::Packet(mid, pt, err) => write!(f, "{} {} {}", mid, pt, err),
98            RtcError::UnknownPt(pt) => write!(f, "PT is unknown {}", pt),
99            RtcError::UnknownRid(rid) => write!(f, "RID is unknown {}", rid),
100            RtcError::NoSenderSource => write!(f, "No sender source"),
101            RtcError::ResendRequiresRtxPt => write!(
102                f,
103                "When outgoing stream has RTX, write_rtp must be called with rtp_pt set"
104            ),
105            RtcError::NotSendingDirection(dir) => {
106                write!(f, "Direction does not allow sending: {}", dir)
107            }
108            RtcError::NotReceivingDirection => write!(f, "Direction does not allow receiving"),
109            RtcError::NoReceiverSource(rid) => write!(f, "No receiver source (rid: {:?})", rid),
110            RtcError::FeedbackNotEnabled(kind) => {
111                write!(f, "Requested feedback is not enabled: {:?}", kind)
112            }
113            RtcError::Net(err) => write!(f, "{}", err),
114            RtcError::Ice(err) => write!(f, "{}", err),
115            RtcError::Sctp(err) => write!(f, "{}", err),
116            RtcError::ChangesOutOfOrder => write!(f, "Changes made out of order"),
117            RtcError::WriteWithoutPoll => write!(
118                f,
119                "Consecutive calls to write() without poll_output() in between"
120            ),
121        }
122    }
123}
124
125impl Error for RtcError {
126    fn source(&self) -> Option<&(dyn Error + 'static)> {
127        match self {
128            RtcError::Sdp(err) => Some(err),
129            RtcError::Rtp(err) => Some(err),
130            RtcError::Io(err) => Some(err),
131            RtcError::Dtls(err) => Some(err),
132            RtcError::Packet(_, _, err) => Some(err),
133            RtcError::Net(err) => Some(err),
134            RtcError::Ice(err) => Some(err),
135            RtcError::Sctp(err) => Some(err),
136            _ => None,
137        }
138    }
139}
140
141// From implementations for error conversions
142impl From<SdpError> for RtcError {
143    fn from(err: SdpError) -> Self {
144        RtcError::Sdp(err)
145    }
146}
147
148impl From<RtpError> for RtcError {
149    fn from(err: RtpError) -> Self {
150        RtcError::Rtp(err)
151    }
152}
153
154impl From<std::io::Error> for RtcError {
155    fn from(err: std::io::Error) -> Self {
156        RtcError::Io(err)
157    }
158}
159
160impl From<DtlsError> for RtcError {
161    fn from(err: DtlsError) -> Self {
162        RtcError::Dtls(err)
163    }
164}
165
166impl From<NetError> for RtcError {
167    fn from(err: NetError) -> Self {
168        RtcError::Net(err)
169    }
170}
171
172impl From<IceError> for RtcError {
173    fn from(err: IceError) -> Self {
174        RtcError::Ice(err)
175    }
176}
177
178impl From<SctpError> for RtcError {
179    fn from(err: SctpError) -> Self {
180        RtcError::Sctp(err)
181    }
182}