grafton_ndi/advanced/kvm_receiver.rs
1//! Shared receiver operations compatible with a live KVM session.
2
3use std::time::Duration;
4
5use super::{PacketAttached, PacketItem, PacketItemRef, PacketReceiver};
6
7use crate::{
8 AudioMode, ConnectionStats, EventMode, MetadataMode, MetadataSource, Ptz, RawCapture,
9 ReceiveDelay, Receiver, ReceiverEvent, ReceiverItem, ReceiverItemRef, ReceiverMode, Result,
10 SettingChange, Source, SourceNameChange, Tally, VideoMode,
11};
12
13/// Restricted receiver access lent by [`super::KvmSession::receiver`].
14///
15/// Capture and event polling follow separate sealed mode bounds. FrameSync and
16/// AV sync views permit metadata capture but no event polling. Shared controls delegate
17/// to the same receiver, but this view cannot connect, disconnect, reconnect,
18/// or recover unrestricted receiver access. To switch sources, successfully
19/// release all input, end the session, change the owner's connection and start
20/// a new session. See [`super::KvmSession`] for best-effort release limitations.
21///
22/// This reference-sized view allocates nothing and makes no SDK calls or lock
23/// acquisitions when constructed. Capture handles, borrowed frames and PTZ
24/// controls retain the original receiver borrow, so they can coexist with
25/// mutable session input calls. Packet sessions also capture through
26/// `session.receiver()`, so a worker can capture while its session sends input.
27/// Like the receiver, the view is `Send` and `Sync`.
28pub struct KvmReceiver<'a, M: ReceiverMode = RawCapture> {
29 receiver: &'a Receiver<M>,
30}
31
32impl<M: ReceiverMode> std::fmt::Debug for KvmReceiver<'_, M> {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 f.debug_struct("KvmReceiver")
35 .field("receiver", &self.receiver)
36 .finish_non_exhaustive()
37 }
38}
39
40impl<'a, M: ReceiverMode> KvmReceiver<'a, M> {
41 pub(super) fn new(receiver: &'a Receiver<M>) -> Self {
42 Self { receiver }
43 }
44
45 /// Set video buffering; see [`Receiver::set_video_delay`].
46 ///
47 /// # Errors
48 ///
49 /// As for [`Receiver::set_video_delay`].
50 #[doc(alias = "NDIlib_recv_set_video_delay")]
51 pub fn set_video_delay(&self, delay: ReceiveDelay) -> Result<SettingChange> {
52 self.receiver.set_video_delay(delay)
53 }
54
55 /// Query video buffering; see [`Receiver::video_delay`].
56 ///
57 /// # Errors
58 ///
59 /// As for [`Receiver::video_delay`].
60 #[doc(alias = "NDIlib_recv_get_video_delay")]
61 pub fn video_delay(&self) -> Result<ReceiveDelay> {
62 self.receiver.video_delay()
63 }
64
65 /// Set audio buffering; see [`Receiver::set_audio_delay`].
66 ///
67 /// # Errors
68 ///
69 /// As for [`Receiver::set_audio_delay`].
70 #[doc(alias = "NDIlib_recv_set_audio_delay")]
71 pub fn set_audio_delay(&self, delay: ReceiveDelay) -> Result<SettingChange> {
72 self.receiver.set_audio_delay(delay)
73 }
74
75 /// Query audio buffering; see [`Receiver::audio_delay`].
76 ///
77 /// # Errors
78 ///
79 /// As for [`Receiver::audio_delay`].
80 #[doc(alias = "NDIlib_recv_get_audio_delay")]
81 pub fn audio_delay(&self) -> Result<ReceiveDelay> {
82 self.receiver.audio_delay()
83 }
84
85 /// Register connection metadata; see [`Receiver::add_connection_metadata`].
86 ///
87 /// # Errors
88 ///
89 /// As for [`Receiver::add_connection_metadata`].
90 #[doc(alias = "NDIlib_recv_add_connection_metadata")]
91 pub fn add_connection_metadata<S: MetadataSource + ?Sized>(&self, metadata: &S) -> Result<()> {
92 self.receiver.add_connection_metadata(metadata)
93 }
94
95 /// Clear registered metadata; see [`Receiver::clear_connection_metadata`].
96 ///
97 /// # Errors
98 ///
99 /// As for [`Receiver::clear_connection_metadata`].
100 #[doc(alias = "NDIlib_recv_clear_connection_metadata")]
101 pub fn clear_connection_metadata(&self) -> Result<()> {
102 self.receiver.clear_connection_metadata()
103 }
104
105 /// Copy the device web-control URL; see [`Receiver::web_control_url`].
106 ///
107 /// # Errors
108 ///
109 /// As for [`Receiver::web_control_url`].
110 #[doc(alias = "NDIlib_recv_get_web_control")]
111 pub fn web_control_url(&self) -> Result<Option<String>> {
112 self.receiver.web_control_url()
113 }
114
115 /// Query source-name changes; see [`Receiver::wait_for_source_name_change`].
116 ///
117 /// # Errors
118 ///
119 /// As for [`Receiver::wait_for_source_name_change`].
120 #[doc(alias = "NDIlib_recv_get_source_name")]
121 pub fn wait_for_source_name_change(
122 &self,
123 timeout: Duration,
124 ) -> Result<Option<SourceNameChange>> {
125 self.receiver.wait_for_source_name_change(timeout)
126 }
127
128 /// Set upstream tally; see [`Receiver::set_tally`].
129 #[doc(alias = "NDIlib_recv_set_tally")]
130 pub fn set_tally(&self, tally: Tally) {
131 self.receiver.set_tally(tally)
132 }
133
134 /// Send upstream metadata; see [`Receiver::send_metadata`].
135 ///
136 /// # Errors
137 ///
138 /// As for [`Receiver::send_metadata`].
139 #[doc(alias = "NDIlib_recv_send_metadata")]
140 pub fn send_metadata<S: MetadataSource + ?Sized>(&self, metadata: &S) -> Result<()> {
141 self.receiver.send_metadata(metadata)
142 }
143
144 /// Borrow PTZ controls; see [`Receiver::ptz`].
145 #[must_use = "the Ptz handle does nothing until a command is called"]
146 pub fn ptz(&self) -> Ptz<'a> {
147 self.receiver.ptz()
148 }
149
150 /// Query connection status; see [`Receiver::is_connected`].
151 #[doc(alias = "NDIlib_recv_get_no_connections")]
152 pub fn is_connected(&self) -> bool {
153 self.receiver.is_connected()
154 }
155
156 /// Copy the last requested target, if any; see [`Receiver::connection_target`].
157 pub fn connection_target(&self) -> Option<Source> {
158 self.receiver.connection_target()
159 }
160
161 /// Query reception statistics; see [`Receiver::connection_stats`].
162 #[doc(alias = "NDIlib_recv_get_no_connections")]
163 #[doc(alias = "NDIlib_recv_get_performance")]
164 #[doc(alias = "NDIlib_recv_get_queue")]
165 pub fn connection_stats(&self) -> Result<ConnectionStats> {
166 self.receiver.connection_stats()
167 }
168
169 /// Request a keyframe; see [`Receiver::request_keyframe`].
170 ///
171 /// # Errors
172 ///
173 /// As for [`Receiver::request_keyframe`].
174 #[doc(alias = "NDIlib_recv_request_keyframe")]
175 pub fn request_keyframe(&self) -> Result<()> {
176 self.receiver.request_keyframe()
177 }
178}
179
180macro_rules! kvm_capture_methods {
181 ($(($mode:ident, $kind:ident, $owned:ident, $borrowed:ident,
182 $capture:ident, $capture_ref:ident, $capture_with:ident, $label:literal)),* $(,)?) => {
183 $(
184 impl<'a, M: $mode> KvmReceiver<'a, M> {
185 #[doc = concat!("Capture owned ", $label, "; see [`Receiver::", stringify!($capture), "`].")]
186 #[doc(alias = "NDIlib_recv_capture_v3")]
187 pub fn $capture(&self, timeout: Duration) -> Result<Option<crate::$owned>> {
188 self.receiver.$capture(timeout)
189 }
190
191 #[doc = concat!("Borrow ", $label, "; see [`Receiver::", stringify!($capture_ref), "`].")]
192 /// The frame retains the original receiver borrow and may coexist
193 /// with later mutable KVM session input calls.
194 #[doc(alias = "NDIlib_recv_capture_v3")]
195 pub fn $capture_ref(&self, timeout: Duration) -> Result<Option<crate::$borrowed<'a>>> {
196 let receiver: &'a Receiver<M> = self.receiver;
197 receiver.$capture_ref(timeout)
198 }
199 }
200 )*
201 };
202}
203crate::capture::capture_method_kinds!(kvm_capture_methods);
204
205impl<'a, M: EventMode> KvmReceiver<'a, M> {
206 /// Poll receiver events; see [`Receiver::next_event`].
207 ///
208 /// # Errors
209 ///
210 /// As for [`Receiver::next_event`].
211 #[doc(alias = "NDIlib_recv_capture_v3")]
212 pub fn next_event(&self, timeout: Duration) -> Result<Option<ReceiverEvent>> {
213 self.receiver.next_event(timeout)
214 }
215}
216
217impl<'a> KvmReceiver<'a> {
218 /// Borrow mixed media or events; see [`Receiver::capture_any_ref`].
219 ///
220 /// # Errors
221 ///
222 /// As for [`Receiver::capture_any_ref`], including [`crate::Error::UnexpectedSdkResult`]
223 /// once per failed status publication, whose event remains pending.
224 #[doc(alias = "NDIlib_recv_capture_v3")]
225 pub fn capture_any_ref(&self, timeout: Duration) -> Result<Option<ReceiverItemRef<'a>>> {
226 self.receiver.capture_any_ref(timeout)
227 }
228
229 /// Copy mixed media or events; see [`Receiver::capture_any`].
230 ///
231 /// # Errors
232 ///
233 /// As for [`Receiver::capture_any`], including [`crate::Error::UnexpectedSdkResult`]
234 /// once per failed status publication, whose event remains pending.
235 #[doc(alias = "NDIlib_recv_capture_v3")]
236 pub fn capture_any(&self, timeout: Duration) -> Result<Option<ReceiverItem>> {
237 self.receiver.capture_any(timeout)
238 }
239}
240
241impl<'a> KvmReceiver<'a, PacketAttached> {
242 /// Wait up to `timeout` for the next frame or event of a
243 /// [`PacketReceiver`], exactly as [`PacketReceiver::capture_any`] does, while
244 /// the session holds its inputs.
245 ///
246 /// Every result owns its data, so nothing it returns keeps the session
247 /// borrowed. Both packet capture methods use a shared borrow of the
248 /// receiver, as the packet receiver's own capture methods do.
249 ///
250 /// # Errors
251 ///
252 /// As for [`PacketReceiver::capture_any`], including [`crate::Error::UnexpectedSdkResult`]
253 /// once per failed status publication, whose event remains pending.
254 #[doc(alias = "NDIlib_recv_capture_v3")]
255 pub fn capture_any(&self, timeout: Duration) -> Result<Option<PacketItem>> {
256 PacketReceiver::capture_from(self.receiver, timeout)
257 }
258
259 /// Borrow a packet for the underlying receiver's lifetime, so inputs can
260 /// still be released while the packet is held. See [`PacketReceiver::capture_any_ref`].
261 ///
262 /// # Errors
263 ///
264 /// As for [`PacketReceiver::capture_any_ref`], including [`crate::Error::UnexpectedSdkResult`]
265 /// once per failed status publication, whose event remains pending.
266 #[doc(alias = "NDIlib_recv_capture_v3")]
267 pub fn capture_any_ref(&self, timeout: Duration) -> Result<Option<PacketItemRef<'a>>> {
268 PacketReceiver::capture_ref_from(self.receiver, timeout)
269 }
270}