livekit_data_stream/
utils.rs1use livekit_common::EncryptionType;
16use thiserror::Error;
17
18#[derive(Debug, Clone)]
24pub struct SendError;
25
26pub type StreamResult<T> = Result<T, StreamError>;
28
29#[derive(Debug, Error)]
31pub enum StreamError {
32 #[error("stream has already been closed")]
34 AlreadyClosed,
35
36 #[error("stream closed abnormally: {0}")]
37 AbnormalEnd(String),
38
39 #[error("UTF-8 decoding error: {0}")]
40 Utf8(#[from] std::string::FromUtf8Error),
41
42 #[error("incoming header was invalid")]
43 InvalidHeader,
44
45 #[error("expected chunk index to be exactly one more than the previous")]
46 MissedChunk,
47
48 #[error("read length exceeded total length specified in stream header")]
49 LengthExceeded,
50
51 #[error("stream data is incomplete")]
52 Incomplete,
53
54 #[error("unable to send packet")]
55 SendFailed,
56
57 #[error("I/O error: {0}")]
58 Io(#[from] std::io::Error),
59
60 #[error("internal error")]
61 Internal,
62
63 #[error("encryption type mismatch: expected {expected:?}, received {received:?}")]
64 EncryptionTypeMismatch {
65 expected: EncryptionType,
67 received: EncryptionType,
69 },
70
71 #[error("stream header exceeds maximum size")]
72 HeaderTooLarge,
73
74 #[error("stream payload exceeds maximum size")]
75 PayloadTooLarge,
76
77 #[error("decompression failed")]
78 Decompression,
79
80 #[error("file name must be a plain file name without path separators or '..'")]
81 InvalidFileName,
82}
83
84#[derive(Clone, Copy, Default, Debug, Hash, Eq, PartialEq)]
86pub struct StreamProgress {
87 pub(crate) chunk_index: u64,
88 pub(crate) bytes_processed: u64,
90 pub(crate) bytes_total: Option<u64>,
92}
93
94impl StreamProgress {
95 pub fn bytes_processed(&self) -> u64 {
97 self.bytes_processed
98 }
99
100 pub fn bytes_total(&self) -> Option<u64> {
102 self.bytes_total
103 }
104
105 pub fn percentage(&self) -> Option<f32> {
108 self.bytes_total.map(|total| {
109 if total == 0 {
110 1.0
111 } else {
112 self.bytes_processed as f32 / total as f32
113 }
114 })
115 }
116}