1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
pub use cameleon_device::PixelFormat;
use std::time;
use async_std::channel::{Receiver, Sender};
use super::{StreamError, StreamResult};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PayloadType {
Image,
ImageExtendedChunk,
Chunk,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageInfo {
pub width: usize,
pub height: usize,
pub x_offset: usize,
pub y_offset: usize,
pub pixel_format: PixelFormat,
pub image_size: usize,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Payload {
pub(crate) id: u64,
pub(crate) payload_type: PayloadType,
pub(crate) image_info: Option<ImageInfo>,
pub(crate) payload: Vec<u8>,
pub(crate) valid_payload_size: usize,
pub(crate) timestamp: time::Duration,
}
impl Payload {
pub fn payload_type(&self) -> PayloadType {
self.payload_type
}
pub fn image_info(&self) -> Option<&ImageInfo> {
self.image_info.as_ref()
}
pub fn image(&self) -> Option<&[u8]> {
let image_info = self.image_info()?;
Some(&self.payload[..image_info.image_size])
}
pub fn payload(&self) -> &[u8] {
&self.payload[..self.valid_payload_size]
}
pub fn id(&self) -> u64 {
self.id
}
pub fn timestamp(&self) -> time::Duration {
self.timestamp
}
pub fn into_vec(mut self) -> Vec<u8> {
self.payload.resize(self.valid_payload_size, 0);
self.payload
}
}
#[derive(Debug, Clone)]
pub struct PayloadReceiver {
tx: Sender<Payload>,
rx: Receiver<StreamResult<Payload>>,
}
impl PayloadReceiver {
pub async fn recv(&self) -> StreamResult<Payload> {
self.rx.recv().await?
}
pub fn try_recv(&self) -> StreamResult<Payload> {
self.rx.try_recv()?
}
pub fn send_back(&self, payload: Payload) {
self.tx.try_send(payload).ok();
}
}
#[derive(Debug, Clone)]
pub struct PayloadSender {
tx: Sender<StreamResult<Payload>>,
rx: Receiver<Payload>,
}
impl PayloadSender {
pub async fn send(&self, payload: StreamResult<Payload>) -> StreamResult<()> {
Ok(self.tx.send(payload).await?)
}
pub fn try_send(&self, payload: StreamResult<Payload>) -> StreamResult<()> {
Ok(self.tx.try_send(payload)?)
}
pub fn try_recv(&self) -> StreamResult<Payload> {
Ok(self.rx.try_recv()?)
}
}
pub fn channel(payload_cap: usize, buffer_cap: usize) -> (PayloadSender, PayloadReceiver) {
let (device_tx, host_rx) = async_std::channel::bounded(payload_cap);
let (host_tx, device_rx) = async_std::channel::bounded(buffer_cap);
(
PayloadSender {
tx: device_tx,
rx: device_rx,
},
PayloadReceiver {
tx: host_tx,
rx: host_rx,
},
)
}
impl From<async_std::channel::RecvError> for StreamError {
fn from(err: async_std::channel::RecvError) -> Self {
StreamError::ReceiveError(err.to_string().into())
}
}
impl From<async_std::channel::TryRecvError> for StreamError {
fn from(err: async_std::channel::TryRecvError) -> Self {
StreamError::ReceiveError(err.to_string().into())
}
}
impl<T> From<async_std::channel::SendError<T>> for StreamError {
fn from(err: async_std::channel::SendError<T>) -> Self {
StreamError::ReceiveError(err.to_string().into())
}
}
impl<T> From<async_std::channel::TrySendError<T>> for StreamError {
fn from(err: async_std::channel::TrySendError<T>) -> Self {
StreamError::ReceiveError(err.to_string().into())
}
}