1use std::io;
12use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU64, Ordering};
13use std::sync::{Arc, Mutex, PoisonError};
14use std::thread::JoinHandle;
15use std::time::Duration;
16
17use atdecc::stream_format::StreamFormat;
18use avb_net::MacAddress;
19use avb_net::clock::{monotonic_now, precise_sleeps};
20use avb_net::stream::FrameSender;
21
22use crate::MediaClock;
23use crate::audio::{Input, Source};
24use crate::media::{Media, Packetizer};
25use crate::meter::Peaks;
26use crate::recovered::{Fit, RecoveredClock, div_ceil};
27
28const ETHERTYPE_VLAN: u16 = 0x8100;
30const ETHERTYPE_AVTP: u16 = 0x22f0;
31const ETHERNET_LEN: usize = 18;
33const CROSSING: i64 = 500_000;
37const LONGEST_SLEEP: Duration = Duration::from_millis(20);
40const ALIGN: u64 = 48;
44const NANOS: u128 = 1_000_000_000;
45
46#[derive(Debug, Clone, PartialEq)]
48pub struct TalkerConfig {
49 pub interface: String,
50 pub mac: MacAddress,
52 pub stream_id: u64,
53 pub destinations: Vec<MacAddress>,
57 pub vlan_id: u16,
58 pub priority: u8,
59 pub format: StreamFormat,
60 pub max_transit_time: Duration,
62 pub source: Source,
63}
64
65#[derive(Debug, Clone, PartialEq)]
67pub struct GroupConfig {
68 pub interface: String,
69 pub mac: MacAddress,
71 pub rate: u32,
72}
73
74#[derive(Debug, Clone, PartialEq)]
76pub struct GroupStream {
77 pub stream_id: u64,
78 pub destinations: Vec<MacAddress>,
81 pub vlan_id: u16,
82 pub priority: u8,
83 pub format: StreamFormat,
85 pub max_transit_time: Duration,
87 pub source: Source,
89 pub channels: u16,
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
96pub struct TalkerStats {
97 pub frames_sent: u64,
98 pub media_resets: u64,
100 pub late_resets: u64,
103 pub clock_resets: u64,
106 pub adjustments: u64,
109 pub underruns: u64,
110 pub send_errors: u64,
112 pub realtime: bool,
114 pub drift_ppm: i32,
117}
118
119#[derive(Default)]
121struct StreamCounters {
122 frames_sent: AtomicU64,
123 send_errors: AtomicU64,
124 peaks: Peaks,
125}
126
127#[derive(Default)]
129struct GroupCounters {
130 media_resets: AtomicU64,
131 late_resets: AtomicU64,
132 clock_resets: AtomicU64,
133 adjustments: AtomicU64,
134 underruns: AtomicU64,
135 realtime: AtomicBool,
136 drift_ppm: AtomicI32,
137}
138
139struct Shared {
141 streams: Mutex<Vec<Option<GroupStream>>>,
144 changes: AtomicU64,
145 inputs: Mutex<Vec<Option<Input>>>,
148 follow: Mutex<Option<RecoveredClock>>,
150 counters: Vec<StreamCounters>,
151 group: GroupCounters,
152}
153
154impl Shared {
155 fn lock(&self) -> std::sync::MutexGuard<'_, Vec<Option<GroupStream>>> {
156 self.streams.lock().unwrap_or_else(PoisonError::into_inner)
157 }
158}
159
160pub struct TalkerGroup {
162 shared: Arc<Shared>,
163 stop: Arc<AtomicBool>,
164 handle: Option<JoinHandle<()>>,
165 rate: u32,
166}
167
168impl TalkerGroup {
169 pub fn start(config: GroupConfig, places: usize, clock: Arc<MediaClock>) -> io::Result<Self> {
172 let sender = FrameSender::open(&config.interface)?;
173 let stop = Arc::new(AtomicBool::new(false));
174 let shared = Arc::new(Shared {
175 streams: Mutex::new(vec![None; places]),
176 changes: AtomicU64::new(0),
177 inputs: Mutex::new((0..places).map(|_| None).collect()),
178 follow: Mutex::new(None),
179 counters: (0..places).map(|_| StreamCounters::default()).collect(),
180 group: GroupCounters::default(),
181 });
182 let rate = config.rate;
183 let handle = {
184 let stop = stop.clone();
185 let shared = shared.clone();
186 std::thread::Builder::new()
187 .name("talker".into())
188 .spawn(move || run(&config, &shared, &sender, &clock, &stop))?
189 };
190 Ok(TalkerGroup {
191 shared,
192 stop,
193 handle: Some(handle),
194 rate,
195 })
196 }
197
198 pub fn rate(&self) -> u32 {
200 self.rate
201 }
202
203 pub fn set_stream(&self, place: usize, stream: Option<GroupStream>) -> io::Result<()> {
206 let input = match &stream {
207 Some(stream) => {
208 let media = Media::of(stream.format).ok_or_else(|| {
209 io::Error::new(
210 io::ErrorKind::Unsupported,
211 "not an AAF, AM824 or CRF format this talker sends",
212 )
213 })?;
214 if media.frame_samples_at(self.rate).is_none() {
215 return Err(io::Error::new(
216 io::ErrorKind::InvalidInput,
217 "a stream at another rate than its group's",
218 ));
219 }
220 if media.is_clock() {
221 None
222 } else {
223 Some(Input::open(&stream.source, stream.channels, self.rate)?)
224 }
225 }
226 None => None,
227 };
228 let mut streams = self.shared.lock();
229 let Some(slot) = streams.get_mut(place) else {
230 return Err(io::Error::new(io::ErrorKind::InvalidInput, "no such place"));
231 };
232 if let Some(waiting) = self
233 .shared
234 .inputs
235 .lock()
236 .unwrap_or_else(PoisonError::into_inner)
237 .get_mut(place)
238 {
239 *waiting = input;
240 }
241 *slot = stream;
242 self.shared.changes.fetch_add(1, Ordering::Relaxed);
243 Ok(())
244 }
245
246 pub fn set_destinations(&self, place: usize, destinations: Vec<MacAddress>) {
249 let mut streams = self.shared.lock();
250 if let Some(Some(stream)) = streams.get_mut(place) {
251 stream.destinations = destinations;
252 self.shared.changes.fetch_add(1, Ordering::Relaxed);
253 }
254 }
255
256 pub fn follow(&self, clock: Option<RecoveredClock>) {
261 let mut follow = self
262 .shared
263 .follow
264 .lock()
265 .unwrap_or_else(PoisonError::into_inner);
266 if *follow != clock {
267 *follow = clock;
268 self.shared.changes.fetch_add(1, Ordering::Relaxed);
269 }
270 }
271
272 pub fn sending(&self) -> bool {
274 self.shared.lock().iter().any(Option::is_some)
275 }
276
277 pub fn sends(&self, place: usize) -> bool {
279 self.shared.lock().get(place).is_some_and(Option::is_some)
280 }
281
282 pub fn stats(&self, place: usize) -> TalkerStats {
284 let read = |counter: &AtomicU64| counter.load(Ordering::Relaxed);
285 let group = &self.shared.group;
286 let (frames_sent, send_errors) =
287 self.shared.counters.get(place).map_or((0, 0), |counters| {
288 (read(&counters.frames_sent), read(&counters.send_errors))
289 });
290 TalkerStats {
291 frames_sent,
292 media_resets: read(&group.media_resets),
293 late_resets: read(&group.late_resets),
294 clock_resets: read(&group.clock_resets),
295 adjustments: read(&group.adjustments),
296 underruns: read(&group.underruns),
297 send_errors,
298 realtime: group.realtime.load(Ordering::Relaxed),
299 drift_ppm: group.drift_ppm.load(Ordering::Relaxed),
300 }
301 }
302
303 pub fn take_peaks(&self, place: usize) -> [f32; 2] {
307 self.shared
308 .counters
309 .get(place)
310 .map_or([0.0; 2], |counters| counters.peaks.take())
311 }
312}
313
314impl Drop for TalkerGroup {
315 fn drop(&mut self) {
316 self.stop.store(true, Ordering::Relaxed);
317 if let Some(handle) = self.handle.take() {
318 let _ = handle.join();
319 }
320 }
321}
322
323pub struct GroupTalker {
326 group: Arc<TalkerGroup>,
327 place: usize,
328}
329
330impl GroupTalker {
331 pub fn start(group: &Arc<TalkerGroup>, place: usize, stream: GroupStream) -> io::Result<Self> {
333 group.set_stream(place, Some(stream))?;
334 Ok(GroupTalker {
335 group: group.clone(),
336 place,
337 })
338 }
339
340 pub fn set_destinations(&self, destinations: Vec<MacAddress>) {
343 self.group.set_destinations(self.place, destinations);
344 }
345
346 pub fn stats(&self) -> TalkerStats {
347 self.group.stats(self.place)
348 }
349
350 pub fn take_peaks(&self) -> [f32; 2] {
353 self.group.take_peaks(self.place)
354 }
355}
356
357impl Drop for GroupTalker {
358 fn drop(&mut self) {
359 let _ = self.group.set_stream(self.place, None);
360 }
361}
362
363pub struct Talker {
365 group: TalkerGroup,
366}
367
368impl Talker {
369 pub fn start(config: TalkerConfig, clock: Arc<MediaClock>) -> io::Result<Self> {
371 let media = Media::of(config.format).ok_or_else(|| {
372 io::Error::new(
373 io::ErrorKind::Unsupported,
374 "not an AAF, AM824 or CRF format this talker sends",
375 )
376 })?;
377 let group = TalkerGroup::start(
378 GroupConfig {
379 interface: config.interface,
380 mac: config.mac,
381 rate: media.sample_rate(),
382 },
383 1,
384 clock,
385 )?;
386 group.set_stream(
387 0,
388 Some(GroupStream {
389 stream_id: config.stream_id,
390 destinations: config.destinations,
391 vlan_id: config.vlan_id,
392 priority: config.priority,
393 format: config.format,
394 max_transit_time: config.max_transit_time,
395 source: config.source,
396 channels: media.channels(),
397 }),
398 )?;
399 Ok(Talker { group })
400 }
401
402 pub fn set_destinations(&self, destinations: Vec<MacAddress>) {
405 self.group.set_destinations(0, destinations);
406 }
407
408 pub fn stats(&self) -> TalkerStats {
409 self.group.stats(0)
410 }
411}
412
413fn ethernet_header(mac: MacAddress, stream: &GroupStream) -> [u8; ETHERNET_LEN] {
415 let mut header = [0; ETHERNET_LEN];
416 header[..6].copy_from_slice(&stream.destinations.first().map_or([0; 6], |mac| mac.0));
417 header[6..12].copy_from_slice(&mac.0);
418 header[12..14].copy_from_slice(ÐERTYPE_VLAN.to_be_bytes());
419 let tag = (u16::from(stream.priority & 0x7) << 13) | (stream.vlan_id & 0x0fff);
420 header[14..16].copy_from_slice(&tag.to_be_bytes());
421 header[16..18].copy_from_slice(ÐERTYPE_AVTP.to_be_bytes());
422 header
423}
424
425struct Running {
427 stream: GroupStream,
428 input: Option<Input>,
430 packetizer: Packetizer,
431 frame: Vec<u8>,
432 samples: Vec<f32>,
433 channels: usize,
435 frame_samples: u64,
437 next: u64,
439 media_reset: MediaReset,
441}
442
443#[derive(Debug, Default)]
448struct MediaReset {
449 bit: bool,
450 carried: bool,
452}
453
454impl MediaReset {
455 fn restart(&mut self) {
457 if self.carried {
458 self.bit = !self.bit;
459 self.carried = false;
460 }
461 }
462
463 fn sent(&mut self) {
465 self.carried = true;
466 }
467}
468
469impl Running {
470 fn new(
471 mac: MacAddress,
472 stream: GroupStream,
473 media: Media,
474 input: Option<Input>,
475 frame_samples: u64,
476 ) -> Self {
477 let mut frame = vec![0u8; ETHERNET_LEN + media.pdu_length()];
478 frame[..ETHERNET_LEN].copy_from_slice(ðernet_header(mac, &stream));
479 let samples =
480 vec![0.0f32; usize::from(media.channels()) * usize::from(media.samples_per_frame())];
481 Running {
482 packetizer: Packetizer::new(media, stream.stream_id),
483 stream,
484 input,
485 frame,
486 samples,
487 channels: usize::from(media.channels()),
488 frame_samples: frame_samples.max(1),
489 next: 0,
490 media_reset: MediaReset::default(),
491 }
492 }
493
494 fn frame_samples(&self) -> u64 {
495 self.frame_samples
496 }
497
498 fn first_from(&self, from: u64) -> u64 {
500 from.div_ceil(self.frame_samples()) * self.frame_samples()
501 }
502}
503
504struct Timeline {
507 rate: u64,
508 origin: i64,
509 follow: Option<Following>,
511 wanted: Option<Fit>,
514}
515
516#[derive(Debug, Clone, Copy)]
519struct Following {
520 fit: Fit,
521 sample: u64,
522 sub: i128,
523 base: i128,
524}
525
526impl Timeline {
527 fn at(&self, sample: u64) -> i64 {
528 match &self.follow {
529 Some(following) => {
530 let samples = i128::from(sample) - i128::from(following.sample);
531 following
532 .fit
533 .time_at(following.sub + samples * following.base, self.rate)
534 }
535 None => self.origin + (u128::from(sample) * NANOS / u128::from(self.rate)) as i64,
536 }
537 }
538
539 fn sample_at(&self, time: i64) -> u64 {
541 match &self.follow {
542 Some(following) => {
543 let sub = following.fit.sub_at(time, self.rate);
544 let samples = div_ceil(sub - following.sub, following.base);
545 (i128::from(following.sample) + samples).max(0) as u64
546 }
547 None => {
548 let since = (time - self.origin).max(0);
549 (since as u128 * u128::from(self.rate)).div_ceil(NANOS) as u64
550 }
551 }
552 }
553
554 fn take(&mut self, fit: Option<Fit>) -> bool {
558 self.wanted = fit;
559 match (fit, self.follow.as_mut()) {
560 (Some(fit), Some(following)) if following.fit.generation == fit.generation => {
561 following.fit = fit;
562 false
563 }
564 (None, None) => false,
565 _ => true,
566 }
567 }
568
569 fn restart(&mut self, from: u64, now: i64) -> u64 {
574 let first = from.div_ceil(ALIGN) * ALIGN;
575 let boundary = (now / 1_000_000 + 2) * 1_000_000;
576 match self.wanted {
577 Some(fit) => {
578 let rate = i128::from(self.rate);
579 let edge = div_ceil(fit.sub_at(boundary, self.rate), rate) * rate;
580 self.follow = Some(Following {
581 fit,
582 sample: first,
583 sub: edge,
584 base: i128::from(fit.base_frequency),
585 });
586 }
587 None => {
588 self.follow = None;
589 self.origin = boundary - (u128::from(first) * NANOS / u128::from(self.rate)) as i64;
590 }
591 }
592 first
593 }
594}
595
596fn take_changes(
600 config: &GroupConfig,
601 shared: &Shared,
602 running: &mut [Option<Running>],
603 timeline: &mut Timeline,
604 clock: &MediaClock,
605) {
606 let wanted = shared.lock().clone();
607 let mut inputs = shared.inputs.lock().unwrap_or_else(PoisonError::into_inner);
608 let idle = running.iter().all(Option::is_none);
609 let mut from = None;
610 for (place, wanted) in wanted.into_iter().enumerate() {
611 let Some(slot) = running.get_mut(place) else {
612 continue;
613 };
614 match (wanted, slot.as_mut()) {
615 (None, _) => *slot = None,
616 (Some(stream), Some(now)) if same_but_destinations(&stream, &now.stream) => {
617 if stream.destinations != now.stream.destinations {
618 now.frame[..ETHERNET_LEN]
619 .copy_from_slice(ðernet_header(config.mac, &stream));
620 now.stream.destinations = stream.destinations;
621 }
622 }
623 (Some(stream), _) => {
624 let input = inputs.get_mut(place).and_then(Option::take);
625 let Some((media, frame_samples)) = Media::of(stream.format)
626 .and_then(|media| Some((media, media.frame_samples_at(config.rate)?)))
627 else {
628 *slot = None;
629 continue;
630 };
631 if input.is_none() && !media.is_clock() {
633 *slot = None;
634 continue;
635 }
636 let start = *from.get_or_insert_with(|| {
638 let now = clock.now();
639 if idle {
640 timeline.restart(timeline.sample_at(now), now)
641 } else {
642 timeline.sample_at(now + 1_000_000)
643 }
644 });
645 let mut made = Running::new(config.mac, stream, media, input, frame_samples);
646 made.next = made.first_from(start);
647 *slot = Some(made);
648 }
649 }
650 }
651}
652
653fn same_but_destinations(one: &GroupStream, other: &GroupStream) -> bool {
655 GroupStream {
656 destinations: Vec::new(),
657 ..one.clone()
658 } == GroupStream {
659 destinations: Vec::new(),
660 ..other.clone()
661 }
662}
663
664fn run(
665 config: &GroupConfig,
666 shared: &Shared,
667 sender: &FrameSender,
668 clock: &MediaClock,
669 stop: &AtomicBool,
670) {
671 precise_sleeps();
672 shared
673 .group
674 .realtime
675 .store(crate::realtime::raise(), Ordering::Relaxed);
676 let mut running: Vec<Option<Running>> = (0..shared.counters.len()).map(|_| None).collect();
677 let mut timeline = Timeline {
678 rate: u64::from(config.rate.max(1)),
679 origin: 0,
680 follow: None,
681 wanted: None,
682 };
683 let mut seen = u64::MAX;
684 let mut frames: u64 = 0;
685 let mut follow: Option<RecoveredClock> = None;
686 let mut switched = false;
687 let mut generation = clock.generation();
688 while !stop.load(Ordering::Relaxed) {
689 let changes = shared.changes.load(Ordering::Relaxed);
690 if changes != seen {
691 seen = changes;
692 let wanted = shared
693 .follow
694 .lock()
695 .unwrap_or_else(PoisonError::into_inner)
696 .clone();
697 if wanted != follow {
700 follow = wanted;
701 switched = timeline.follow.take().is_some();
702 }
703 timeline.take(follow.as_ref().and_then(RecoveredClock::fit));
704 take_changes(config, shared, &mut running, &mut timeline, clock);
705 }
706 let retime = timeline.take(follow.as_ref().and_then(RecoveredClock::fit));
710 let jumped = clock.generation() != generation;
713 generation = clock.generation();
714 if (retime || jumped || std::mem::take(&mut switched))
715 && running.iter().any(Option::is_some)
716 {
717 restart(&mut running, &mut timeline, clock);
718 shared.group.media_resets.fetch_add(1, Ordering::Relaxed);
719 if jumped {
720 shared.group.clock_resets.fetch_add(1, Ordering::Relaxed);
721 }
722 }
723 let Some((place, due)) = running
725 .iter()
726 .enumerate()
727 .filter_map(|(place, stream)| {
728 stream
729 .as_ref()
730 .map(|stream| (place, timeline.at(stream.next)))
731 })
732 .min_by_key(|(_, due)| *due)
733 else {
734 std::thread::sleep(LONGEST_SLEEP);
735 continue;
736 };
737 let wake = clock.monotonic_at(due);
738 let now = monotonic_now();
739 if wake > now {
740 let wait = wake - now;
741 std::thread::sleep(wait.min(LONGEST_SLEEP));
742 if wait > LONGEST_SLEEP {
743 if due - clock.now() > 2 * LONGEST_SLEEP.as_nanos() as i64 {
746 restart(&mut running, &mut timeline, clock);
747 shared.group.media_resets.fetch_add(1, Ordering::Relaxed);
748 shared.group.clock_resets.fetch_add(1, Ordering::Relaxed);
749 }
750 continue;
751 }
752 }
753 let Some(stream) = running[place].as_mut() else {
754 continue;
755 };
756 let transit = stream.stream.max_transit_time.as_nanos() as i64;
759 let late = clock.now() - due;
760 if late > (transit - CROSSING).max(transit / 2) {
761 restart(&mut running, &mut timeline, clock);
764 shared.group.media_resets.fetch_add(1, Ordering::Relaxed);
765 shared.group.late_resets.fetch_add(1, Ordering::Relaxed);
766 continue;
767 }
768 if let Some(input) = &mut stream.input {
769 input.read_as(&mut stream.samples, stream.channels);
770 }
771 let counters = &shared.counters[place];
772 counters.peaks.raise(&stream.samples, stream.channels);
773 stream.packetizer.write(
774 due + transit,
775 stream.media_reset.bit,
776 &stream.samples,
777 &mut stream.frame[ETHERNET_LEN..],
778 );
779 for destination in &stream.stream.destinations {
780 stream.frame[..6].copy_from_slice(&destination.0);
781 match sender.send(&stream.frame) {
782 Ok(()) => {
783 counters.frames_sent.fetch_add(1, Ordering::Relaxed);
784 stream.media_reset.sent();
785 }
786 Err(_) => {
787 counters.send_errors.fetch_add(1, Ordering::Relaxed);
788 }
789 }
790 }
791 stream.next += stream.frame_samples();
792 frames += 1;
793 if frames.is_multiple_of(800) {
794 let (mut made, mut empty, mut drift) = (0, 0, 0);
797 for input in running
798 .iter()
799 .flatten()
800 .filter_map(|stream| stream.input.as_ref())
801 {
802 let (adjusted, short) = input.counts();
803 made += adjusted;
804 empty += short;
805 if drift == 0 {
806 drift = input.drift_ppm();
807 }
808 }
809 shared.group.adjustments.store(made, Ordering::Relaxed);
810 shared.group.drift_ppm.store(drift, Ordering::Relaxed);
811 shared.group.underruns.store(empty, Ordering::Relaxed);
812 }
813 }
814}
815
816fn restart(running: &mut [Option<Running>], timeline: &mut Timeline, clock: &MediaClock) {
819 let now = clock.now();
820 let first = timeline.restart(timeline.sample_at(now), now);
821 for stream in running.iter_mut().flatten() {
822 stream.next = stream.first_from(first);
823 stream.media_reset.restart();
824 }
825}
826
827#[cfg(test)]
828mod tests {
829 use super::*;
830
831 #[test]
834 fn the_timeline_counts_samples_on_gptp_time() {
835 let mut timeline = Timeline {
836 rate: 48_000,
837 origin: 0,
838 follow: None,
839 wanted: None,
840 };
841 let first = timeline.restart(5, 7_400_000);
842 assert_eq!(first, 48);
843 assert_eq!(timeline.at(first), 9_000_000);
844 assert_eq!(timeline.at(first + 6) - timeline.at(first), 125_000);
845 assert_eq!(timeline.at(first + 12) - timeline.at(first), 250_000);
846 assert_eq!(timeline.sample_at(timeline.at(first + 6)), first + 6);
847 assert_eq!(timeline.sample_at(timeline.at(first) + 1), first + 1);
849 }
850
851 #[test]
856 fn the_timeline_follows_a_media_clock() {
857 let period = (1e9 / 48_000.0 / (1.0 + 50e-6) * 4_294_967_296.0) as u64;
859 let fit = Fit {
860 generation: 3,
861 event: 960_000,
862 time: 21_000_000_000,
863 period,
864 base_frequency: 48_000,
865 };
866 let mut timeline = Timeline {
867 rate: 192_000,
868 origin: 0,
869 follow: None,
870 wanted: None,
871 };
872 assert!(timeline.take(Some(fit)), "a clock to follow starts again");
873 let first = timeline.restart(1000, 21_000_000_000);
874 assert_eq!(first, 1008);
875 let start = timeline.at(first);
876 assert!(start >= 21_002_000_000, "{start}");
877 let events = fit.sub_at(start, 1) - i128::from(fit.event);
879 assert_eq!(fit.time_at(i128::from(fit.event) + events, 1), start);
880 let second = timeline.at(first + 192_000) - start;
882 assert!((second - 999_950_002).abs() <= 2, "{second}");
883 assert_eq!(
884 timeline.at(first + 4) - start,
885 fit.time_at(i128::from(fit.event) + events + 1, 1) - start
886 );
887 assert_eq!(timeline.sample_at(timeline.at(first + 7)), first + 7);
888 let later = Fit {
890 event: fit.event + 96,
891 time: fit.time_at(i128::from(fit.event) + 96, 1),
892 ..fit
893 };
894 assert!(!timeline.take(Some(later)));
895 assert_eq!(timeline.at(first + 192_000) - start, second);
896 assert!(timeline.take(Some(Fit {
898 generation: 4,
899 ..later
900 })));
901 assert!(timeline.take(None));
902 timeline.restart(first + 192_000, 22_500_000_000);
903 assert!(timeline.follow.is_none());
904 assert!(!timeline.take(None));
905 }
906
907 #[test]
912 fn the_mr_bit_tells_each_start() {
913 let mut media_reset = MediaReset::default();
914 media_reset.restart();
915 assert!(!media_reset.bit, "nothing to tell before the first frame");
916 media_reset.sent();
917 media_reset.restart();
918 media_reset.restart();
919 assert!(media_reset.bit, "two starts change the bit once");
920 media_reset.restart();
922 assert!(media_reset.bit);
923 media_reset.sent();
924 media_reset.restart();
925 assert!(!media_reset.bit);
926 }
927}