pub struct PlaybackStream { /* private fields */ }Expand description
One source-domain stream. Conversion and USB writes are nonblocking; a short input count means the caller must retry the remainder. Observers see bytes accepted by the core stream, before any native DSD wire packing/DoP marker rewrite.
Implementations§
Source§impl PlaybackStream
impl PlaybackStream
pub fn decision(&self) -> &PlaybackDecision
pub fn format(&self) -> &StreamFormat
pub fn set_observer( &mut self, observer: Option<Arc<dyn Fn(&[u8]) + Send + Sync>>, )
Sourcepub fn set_timing_observer(&mut self, observer: Option<ProducerTimingObserver>)
pub fn set_timing_observer(&mut self, observer: Option<ProducerTimingObserver>)
Install an optional synchronous, nonblocking instrumentation callback.
started uses the process monotonic clock and can be aligned with the
transport feedback trace’s started_at. Callbacks must not block.
Sourcepub fn write(&mut self, data: &[u8]) -> Result<usize>
pub fn write(&mut self, data: &[u8]) -> Result<usize>
Accepts whole source frames and returns bytes accepted, never target bytes.
Takes as much as the ring can hold (up to its capacity, like
DacStream::write); converted streams work through it in internal steps
of WRITE_CHUNK_FRAMES source frames. An error met after input was
accepted is returned by the next write or drain, so an accepted
count is never lost.
Examples found in repository?
694 fn dop_resolution(dac: Dac, fd: i32, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
695 use usbdac_core::playback::{PlaybackFidelity, PlaybackPolicy, PlaybackTransform};
696 use usbdac_core::Encoding;
697 let confirmed = PlaybackPolicy {
698 dop_confirmed: true,
699 ..PlaybackPolicy::default()
700 };
701 let caps = dac.capabilities()?;
702 assert!(
703 caps.exclusion_snapshot_complete,
704 "the fixture descriptor snapshot must be complete"
705 );
706 let source = StreamRequest::dop(2_822_400, if mode == "dop-stereo24" { 1 } else { 2 });
707 if mode == "dop-carrier32" {
708 // H2: 24-bit DoP on a 32-bit-only DAC reaches the 32-bit carrier.
709 let widened = dac.resolve_playback(&source, &confirmed)?;
710 println!(
711 "DOP_DECISION mode={mode} case=widen transform={:?} fidelity={:?} bits={} preflight={:?}",
712 widened.transform, widened.fidelity, widened.format.valid_bits, widened.source_preflight_refusal
713 );
714 assert!(widened.source_preflight_refusal.is_some());
715 assert_eq!(widened.format.encoding, Encoding::DoP);
716 assert_eq!(widened.format.valid_bits, 32);
717 assert_eq!(
718 widened.transform,
719 PlaybackTransform::DopRepack { target_channels: 2 }
720 );
721 }
722 let policy = if mode == "dop-carrier32" {
723 // H1: the review's smallest case.
724 PlaybackPolicy {
725 allowed_pcm_depths: Some(vec![32]),
726 ..confirmed.clone()
727 }
728 } else {
729 confirmed.clone()
730 };
731 out_of_band("reset")?;
732 let decision = dac.resolve_playback(&source, &policy)?;
733 println!(
734 "DOP_DECISION mode={mode} transform={:?} fidelity={:?} format={:?}/{}ch/{}bit preflight={:?} attempted={}",
735 decision.transform,
736 decision.fidelity,
737 decision.format.encoding,
738 decision.format.channels,
739 decision.format.valid_bits,
740 decision.source_preflight_refusal,
741 decision.attempted_candidates
742 );
743 if mode == "dop-no-carrier" {
744 // H2: confirmed DoP on a DAC that cannot carry it falls back like unconfirmed.
745 assert!(decision.source_preflight_refusal.is_some());
746 assert_eq!(decision.format.encoding, Encoding::Pcm);
747 assert!(matches!(
748 decision.transform,
749 PlaybackTransform::DsdToPcm {
750 dop_input: true,
751 ..
752 }
753 ));
754 let unconfirmed = dac.resolve_playback(&source, &PlaybackPolicy::default())?;
755 assert_eq!(unconfirmed.format, decision.format);
756 } else {
757 assert_eq!(decision.format.encoding, Encoding::DoP);
758 assert_eq!(decision.format.channels, 2);
759 assert_eq!(
760 decision.transform,
761 PlaybackTransform::DopRepack { target_channels: 2 }
762 );
763 assert_eq!(
764 decision.fidelity,
765 if source.channels == 2 {
766 PlaybackFidelity::BitExact
767 } else {
768 PlaybackFidelity::Lossless
769 }
770 );
771 assert_eq!(decision.attempted_candidates, 1);
772 }
773 let mut stream = dac.open_playback(&decision, Arc::new(|_| {}))?;
774 let channels = usize::from(source.channels);
775 let frame = decision.source_frame_bytes;
776 let mut next = 0usize;
777 let mut chunk = Vec::new();
778 let start = Instant::now();
779 let mut started = false;
780 while start.elapsed() < Duration::from_secs(3)
781 && (stream.played_frames() == 0 || stream.stats().completed_transfers == 0)
782 {
783 let frames = stream.writable_bytes() / frame;
784 if frames > 0 {
785 chunk.clear();
786 for i in next..next + frames {
787 let marker = if i % 2 == 0 { 0x05 } else { 0xfa };
788 for _ in 0..channels {
789 chunk.extend_from_slice(&[0x69, 0x69, marker]);
790 }
791 }
792 next += stream.write(&chunk)? / frame;
793 }
794 if !started && next > 0 {
795 stream.play()?;
796 started = true;
797 }
798 thread::sleep(Duration::from_millis(2));
799 }
800 let stats = stream.target_stats();
801 let snapshot = out_of_band("snapshot")?;
802 println!("DOP_STATS mode={mode} source_frames={next} {stats:?} {snapshot}");
803 assert!(
804 stats.played_frames > 0 && stats.completed_transfers > 0,
805 "no DoP-resolved completions"
806 );
807 let rates = snapshot.split(';').next().ok_or("missing rate snapshot")?;
808 let carrier_set = rates
809 .split(|c: char| !c.is_ascii_digit())
810 .any(|v| v == "176400");
811 // The carrier DACs clock 176.4 kHz; the exact carrier is never set on a DAC without one.
812 assert_eq!(carrier_set, mode != "dop-no-carrier", "{snapshot}");
813 if mode != "dop-no-carrier" {
814 assert!(stats.payload_verified, "the DoP payload must audit clean");
815 }
816 stream.close()?;
817 assert_eq!(alt(fd)?, 0, "stream teardown must restore alternate zero");
818 let retirement = dac.retirement();
819 dac.close()?;
820 assert!(retirement.wait(Duration::from_secs(3)));
821 assert_eq!(alt(fd)?, 0);
822 for interface in [1, 0] {
823 claim(fd, interface, false)?;
824 }
825 println!("USBIP_LIFECYCLE_PASS mode={mode} alt=0 retired=true");
826 Ok(())
827 }Sourcepub fn drain(&mut self) -> Result<bool>
pub fn drain(&mut self) -> Result<bool>
Begin or progress drain. Returns false when the caller must retry after
the USB ring makes space; is_drained reports actual USB completion.
A final partial native DSD word or DoP pair is completed with DSD idle
(0x69), so every accepted DSD byte is played.
Sourcepub fn play(&self) -> Result<()>
pub fn play(&self) -> Result<()>
Examples found in repository?
694 fn dop_resolution(dac: Dac, fd: i32, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
695 use usbdac_core::playback::{PlaybackFidelity, PlaybackPolicy, PlaybackTransform};
696 use usbdac_core::Encoding;
697 let confirmed = PlaybackPolicy {
698 dop_confirmed: true,
699 ..PlaybackPolicy::default()
700 };
701 let caps = dac.capabilities()?;
702 assert!(
703 caps.exclusion_snapshot_complete,
704 "the fixture descriptor snapshot must be complete"
705 );
706 let source = StreamRequest::dop(2_822_400, if mode == "dop-stereo24" { 1 } else { 2 });
707 if mode == "dop-carrier32" {
708 // H2: 24-bit DoP on a 32-bit-only DAC reaches the 32-bit carrier.
709 let widened = dac.resolve_playback(&source, &confirmed)?;
710 println!(
711 "DOP_DECISION mode={mode} case=widen transform={:?} fidelity={:?} bits={} preflight={:?}",
712 widened.transform, widened.fidelity, widened.format.valid_bits, widened.source_preflight_refusal
713 );
714 assert!(widened.source_preflight_refusal.is_some());
715 assert_eq!(widened.format.encoding, Encoding::DoP);
716 assert_eq!(widened.format.valid_bits, 32);
717 assert_eq!(
718 widened.transform,
719 PlaybackTransform::DopRepack { target_channels: 2 }
720 );
721 }
722 let policy = if mode == "dop-carrier32" {
723 // H1: the review's smallest case.
724 PlaybackPolicy {
725 allowed_pcm_depths: Some(vec![32]),
726 ..confirmed.clone()
727 }
728 } else {
729 confirmed.clone()
730 };
731 out_of_band("reset")?;
732 let decision = dac.resolve_playback(&source, &policy)?;
733 println!(
734 "DOP_DECISION mode={mode} transform={:?} fidelity={:?} format={:?}/{}ch/{}bit preflight={:?} attempted={}",
735 decision.transform,
736 decision.fidelity,
737 decision.format.encoding,
738 decision.format.channels,
739 decision.format.valid_bits,
740 decision.source_preflight_refusal,
741 decision.attempted_candidates
742 );
743 if mode == "dop-no-carrier" {
744 // H2: confirmed DoP on a DAC that cannot carry it falls back like unconfirmed.
745 assert!(decision.source_preflight_refusal.is_some());
746 assert_eq!(decision.format.encoding, Encoding::Pcm);
747 assert!(matches!(
748 decision.transform,
749 PlaybackTransform::DsdToPcm {
750 dop_input: true,
751 ..
752 }
753 ));
754 let unconfirmed = dac.resolve_playback(&source, &PlaybackPolicy::default())?;
755 assert_eq!(unconfirmed.format, decision.format);
756 } else {
757 assert_eq!(decision.format.encoding, Encoding::DoP);
758 assert_eq!(decision.format.channels, 2);
759 assert_eq!(
760 decision.transform,
761 PlaybackTransform::DopRepack { target_channels: 2 }
762 );
763 assert_eq!(
764 decision.fidelity,
765 if source.channels == 2 {
766 PlaybackFidelity::BitExact
767 } else {
768 PlaybackFidelity::Lossless
769 }
770 );
771 assert_eq!(decision.attempted_candidates, 1);
772 }
773 let mut stream = dac.open_playback(&decision, Arc::new(|_| {}))?;
774 let channels = usize::from(source.channels);
775 let frame = decision.source_frame_bytes;
776 let mut next = 0usize;
777 let mut chunk = Vec::new();
778 let start = Instant::now();
779 let mut started = false;
780 while start.elapsed() < Duration::from_secs(3)
781 && (stream.played_frames() == 0 || stream.stats().completed_transfers == 0)
782 {
783 let frames = stream.writable_bytes() / frame;
784 if frames > 0 {
785 chunk.clear();
786 for i in next..next + frames {
787 let marker = if i % 2 == 0 { 0x05 } else { 0xfa };
788 for _ in 0..channels {
789 chunk.extend_from_slice(&[0x69, 0x69, marker]);
790 }
791 }
792 next += stream.write(&chunk)? / frame;
793 }
794 if !started && next > 0 {
795 stream.play()?;
796 started = true;
797 }
798 thread::sleep(Duration::from_millis(2));
799 }
800 let stats = stream.target_stats();
801 let snapshot = out_of_band("snapshot")?;
802 println!("DOP_STATS mode={mode} source_frames={next} {stats:?} {snapshot}");
803 assert!(
804 stats.played_frames > 0 && stats.completed_transfers > 0,
805 "no DoP-resolved completions"
806 );
807 let rates = snapshot.split(';').next().ok_or("missing rate snapshot")?;
808 let carrier_set = rates
809 .split(|c: char| !c.is_ascii_digit())
810 .any(|v| v == "176400");
811 // The carrier DACs clock 176.4 kHz; the exact carrier is never set on a DAC without one.
812 assert_eq!(carrier_set, mode != "dop-no-carrier", "{snapshot}");
813 if mode != "dop-no-carrier" {
814 assert!(stats.payload_verified, "the DoP payload must audit clean");
815 }
816 stream.close()?;
817 assert_eq!(alt(fd)?, 0, "stream teardown must restore alternate zero");
818 let retirement = dac.retirement();
819 dac.close()?;
820 assert!(retirement.wait(Duration::from_secs(3)));
821 assert_eq!(alt(fd)?, 0);
822 for interface in [1, 0] {
823 claim(fd, interface, false)?;
824 }
825 println!("USBIP_LIFECYCLE_PASS mode={mode} alt=0 retired=true");
826 Ok(())
827 }pub fn pause(&self) -> Result<()>
pub fn flush(&mut self) -> Result<()>
Sourcepub fn writable_bytes(&self) -> usize
pub fn writable_bytes(&self) -> usize
Source bytes to write now to bring the ring to its active target, in
whole source frames, mirroring DacStream::writable_bytes: the ring’s
room converted to source frames through the source/wire rate ratio,
minus source frames still waiting in the converter. Staged wire output
is first moved into that room (observers see it here), so it never pins
the capacity. 0 after drain or once the stream has failed; while an
error is pending it is one source frame, so the next write reports it.
write still accepts more, up to the ring’s capacity.
Examples found in repository?
694 fn dop_resolution(dac: Dac, fd: i32, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
695 use usbdac_core::playback::{PlaybackFidelity, PlaybackPolicy, PlaybackTransform};
696 use usbdac_core::Encoding;
697 let confirmed = PlaybackPolicy {
698 dop_confirmed: true,
699 ..PlaybackPolicy::default()
700 };
701 let caps = dac.capabilities()?;
702 assert!(
703 caps.exclusion_snapshot_complete,
704 "the fixture descriptor snapshot must be complete"
705 );
706 let source = StreamRequest::dop(2_822_400, if mode == "dop-stereo24" { 1 } else { 2 });
707 if mode == "dop-carrier32" {
708 // H2: 24-bit DoP on a 32-bit-only DAC reaches the 32-bit carrier.
709 let widened = dac.resolve_playback(&source, &confirmed)?;
710 println!(
711 "DOP_DECISION mode={mode} case=widen transform={:?} fidelity={:?} bits={} preflight={:?}",
712 widened.transform, widened.fidelity, widened.format.valid_bits, widened.source_preflight_refusal
713 );
714 assert!(widened.source_preflight_refusal.is_some());
715 assert_eq!(widened.format.encoding, Encoding::DoP);
716 assert_eq!(widened.format.valid_bits, 32);
717 assert_eq!(
718 widened.transform,
719 PlaybackTransform::DopRepack { target_channels: 2 }
720 );
721 }
722 let policy = if mode == "dop-carrier32" {
723 // H1: the review's smallest case.
724 PlaybackPolicy {
725 allowed_pcm_depths: Some(vec![32]),
726 ..confirmed.clone()
727 }
728 } else {
729 confirmed.clone()
730 };
731 out_of_band("reset")?;
732 let decision = dac.resolve_playback(&source, &policy)?;
733 println!(
734 "DOP_DECISION mode={mode} transform={:?} fidelity={:?} format={:?}/{}ch/{}bit preflight={:?} attempted={}",
735 decision.transform,
736 decision.fidelity,
737 decision.format.encoding,
738 decision.format.channels,
739 decision.format.valid_bits,
740 decision.source_preflight_refusal,
741 decision.attempted_candidates
742 );
743 if mode == "dop-no-carrier" {
744 // H2: confirmed DoP on a DAC that cannot carry it falls back like unconfirmed.
745 assert!(decision.source_preflight_refusal.is_some());
746 assert_eq!(decision.format.encoding, Encoding::Pcm);
747 assert!(matches!(
748 decision.transform,
749 PlaybackTransform::DsdToPcm {
750 dop_input: true,
751 ..
752 }
753 ));
754 let unconfirmed = dac.resolve_playback(&source, &PlaybackPolicy::default())?;
755 assert_eq!(unconfirmed.format, decision.format);
756 } else {
757 assert_eq!(decision.format.encoding, Encoding::DoP);
758 assert_eq!(decision.format.channels, 2);
759 assert_eq!(
760 decision.transform,
761 PlaybackTransform::DopRepack { target_channels: 2 }
762 );
763 assert_eq!(
764 decision.fidelity,
765 if source.channels == 2 {
766 PlaybackFidelity::BitExact
767 } else {
768 PlaybackFidelity::Lossless
769 }
770 );
771 assert_eq!(decision.attempted_candidates, 1);
772 }
773 let mut stream = dac.open_playback(&decision, Arc::new(|_| {}))?;
774 let channels = usize::from(source.channels);
775 let frame = decision.source_frame_bytes;
776 let mut next = 0usize;
777 let mut chunk = Vec::new();
778 let start = Instant::now();
779 let mut started = false;
780 while start.elapsed() < Duration::from_secs(3)
781 && (stream.played_frames() == 0 || stream.stats().completed_transfers == 0)
782 {
783 let frames = stream.writable_bytes() / frame;
784 if frames > 0 {
785 chunk.clear();
786 for i in next..next + frames {
787 let marker = if i % 2 == 0 { 0x05 } else { 0xfa };
788 for _ in 0..channels {
789 chunk.extend_from_slice(&[0x69, 0x69, marker]);
790 }
791 }
792 next += stream.write(&chunk)? / frame;
793 }
794 if !started && next > 0 {
795 stream.play()?;
796 started = true;
797 }
798 thread::sleep(Duration::from_millis(2));
799 }
800 let stats = stream.target_stats();
801 let snapshot = out_of_band("snapshot")?;
802 println!("DOP_STATS mode={mode} source_frames={next} {stats:?} {snapshot}");
803 assert!(
804 stats.played_frames > 0 && stats.completed_transfers > 0,
805 "no DoP-resolved completions"
806 );
807 let rates = snapshot.split(';').next().ok_or("missing rate snapshot")?;
808 let carrier_set = rates
809 .split(|c: char| !c.is_ascii_digit())
810 .any(|v| v == "176400");
811 // The carrier DACs clock 176.4 kHz; the exact carrier is never set on a DAC without one.
812 assert_eq!(carrier_set, mode != "dop-no-carrier", "{snapshot}");
813 if mode != "dop-no-carrier" {
814 assert!(stats.payload_verified, "the DoP payload must audit clean");
815 }
816 stream.close()?;
817 assert_eq!(alt(fd)?, 0, "stream teardown must restore alternate zero");
818 let retirement = dac.retirement();
819 dac.close()?;
820 assert!(retirement.wait(Duration::from_secs(3)));
821 assert_eq!(alt(fd)?, 0);
822 for interface in [1, 0] {
823 claim(fd, interface, false)?;
824 }
825 println!("USBIP_LIFECYCLE_PASS mode={mode} alt=0 retired=true");
826 Ok(())
827 }pub fn is_drained(&self) -> bool
Sourcepub fn stats(&self) -> StreamStats
pub fn stats(&self) -> StreamStats
Source-relative statistics. A lossy or channel-expanding transform can never report source bit-perfect even if its target USB payload is intact.
Examples found in repository?
694 fn dop_resolution(dac: Dac, fd: i32, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
695 use usbdac_core::playback::{PlaybackFidelity, PlaybackPolicy, PlaybackTransform};
696 use usbdac_core::Encoding;
697 let confirmed = PlaybackPolicy {
698 dop_confirmed: true,
699 ..PlaybackPolicy::default()
700 };
701 let caps = dac.capabilities()?;
702 assert!(
703 caps.exclusion_snapshot_complete,
704 "the fixture descriptor snapshot must be complete"
705 );
706 let source = StreamRequest::dop(2_822_400, if mode == "dop-stereo24" { 1 } else { 2 });
707 if mode == "dop-carrier32" {
708 // H2: 24-bit DoP on a 32-bit-only DAC reaches the 32-bit carrier.
709 let widened = dac.resolve_playback(&source, &confirmed)?;
710 println!(
711 "DOP_DECISION mode={mode} case=widen transform={:?} fidelity={:?} bits={} preflight={:?}",
712 widened.transform, widened.fidelity, widened.format.valid_bits, widened.source_preflight_refusal
713 );
714 assert!(widened.source_preflight_refusal.is_some());
715 assert_eq!(widened.format.encoding, Encoding::DoP);
716 assert_eq!(widened.format.valid_bits, 32);
717 assert_eq!(
718 widened.transform,
719 PlaybackTransform::DopRepack { target_channels: 2 }
720 );
721 }
722 let policy = if mode == "dop-carrier32" {
723 // H1: the review's smallest case.
724 PlaybackPolicy {
725 allowed_pcm_depths: Some(vec![32]),
726 ..confirmed.clone()
727 }
728 } else {
729 confirmed.clone()
730 };
731 out_of_band("reset")?;
732 let decision = dac.resolve_playback(&source, &policy)?;
733 println!(
734 "DOP_DECISION mode={mode} transform={:?} fidelity={:?} format={:?}/{}ch/{}bit preflight={:?} attempted={}",
735 decision.transform,
736 decision.fidelity,
737 decision.format.encoding,
738 decision.format.channels,
739 decision.format.valid_bits,
740 decision.source_preflight_refusal,
741 decision.attempted_candidates
742 );
743 if mode == "dop-no-carrier" {
744 // H2: confirmed DoP on a DAC that cannot carry it falls back like unconfirmed.
745 assert!(decision.source_preflight_refusal.is_some());
746 assert_eq!(decision.format.encoding, Encoding::Pcm);
747 assert!(matches!(
748 decision.transform,
749 PlaybackTransform::DsdToPcm {
750 dop_input: true,
751 ..
752 }
753 ));
754 let unconfirmed = dac.resolve_playback(&source, &PlaybackPolicy::default())?;
755 assert_eq!(unconfirmed.format, decision.format);
756 } else {
757 assert_eq!(decision.format.encoding, Encoding::DoP);
758 assert_eq!(decision.format.channels, 2);
759 assert_eq!(
760 decision.transform,
761 PlaybackTransform::DopRepack { target_channels: 2 }
762 );
763 assert_eq!(
764 decision.fidelity,
765 if source.channels == 2 {
766 PlaybackFidelity::BitExact
767 } else {
768 PlaybackFidelity::Lossless
769 }
770 );
771 assert_eq!(decision.attempted_candidates, 1);
772 }
773 let mut stream = dac.open_playback(&decision, Arc::new(|_| {}))?;
774 let channels = usize::from(source.channels);
775 let frame = decision.source_frame_bytes;
776 let mut next = 0usize;
777 let mut chunk = Vec::new();
778 let start = Instant::now();
779 let mut started = false;
780 while start.elapsed() < Duration::from_secs(3)
781 && (stream.played_frames() == 0 || stream.stats().completed_transfers == 0)
782 {
783 let frames = stream.writable_bytes() / frame;
784 if frames > 0 {
785 chunk.clear();
786 for i in next..next + frames {
787 let marker = if i % 2 == 0 { 0x05 } else { 0xfa };
788 for _ in 0..channels {
789 chunk.extend_from_slice(&[0x69, 0x69, marker]);
790 }
791 }
792 next += stream.write(&chunk)? / frame;
793 }
794 if !started && next > 0 {
795 stream.play()?;
796 started = true;
797 }
798 thread::sleep(Duration::from_millis(2));
799 }
800 let stats = stream.target_stats();
801 let snapshot = out_of_band("snapshot")?;
802 println!("DOP_STATS mode={mode} source_frames={next} {stats:?} {snapshot}");
803 assert!(
804 stats.played_frames > 0 && stats.completed_transfers > 0,
805 "no DoP-resolved completions"
806 );
807 let rates = snapshot.split(';').next().ok_or("missing rate snapshot")?;
808 let carrier_set = rates
809 .split(|c: char| !c.is_ascii_digit())
810 .any(|v| v == "176400");
811 // The carrier DACs clock 176.4 kHz; the exact carrier is never set on a DAC without one.
812 assert_eq!(carrier_set, mode != "dop-no-carrier", "{snapshot}");
813 if mode != "dop-no-carrier" {
814 assert!(stats.payload_verified, "the DoP payload must audit clean");
815 }
816 stream.close()?;
817 assert_eq!(alt(fd)?, 0, "stream teardown must restore alternate zero");
818 let retirement = dac.retirement();
819 dac.close()?;
820 assert!(retirement.wait(Duration::from_secs(3)));
821 assert_eq!(alt(fd)?, 0);
822 for interface in [1, 0] {
823 claim(fd, interface, false)?;
824 }
825 println!("USBIP_LIFECYCLE_PASS mode={mode} alt=0 retired=true");
826 Ok(())
827 }Sourcepub fn target_stats(&self) -> StreamStats
pub fn target_stats(&self) -> StreamStats
Raw target transport statistics; not a claim of source fidelity.
Examples found in repository?
694 fn dop_resolution(dac: Dac, fd: i32, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
695 use usbdac_core::playback::{PlaybackFidelity, PlaybackPolicy, PlaybackTransform};
696 use usbdac_core::Encoding;
697 let confirmed = PlaybackPolicy {
698 dop_confirmed: true,
699 ..PlaybackPolicy::default()
700 };
701 let caps = dac.capabilities()?;
702 assert!(
703 caps.exclusion_snapshot_complete,
704 "the fixture descriptor snapshot must be complete"
705 );
706 let source = StreamRequest::dop(2_822_400, if mode == "dop-stereo24" { 1 } else { 2 });
707 if mode == "dop-carrier32" {
708 // H2: 24-bit DoP on a 32-bit-only DAC reaches the 32-bit carrier.
709 let widened = dac.resolve_playback(&source, &confirmed)?;
710 println!(
711 "DOP_DECISION mode={mode} case=widen transform={:?} fidelity={:?} bits={} preflight={:?}",
712 widened.transform, widened.fidelity, widened.format.valid_bits, widened.source_preflight_refusal
713 );
714 assert!(widened.source_preflight_refusal.is_some());
715 assert_eq!(widened.format.encoding, Encoding::DoP);
716 assert_eq!(widened.format.valid_bits, 32);
717 assert_eq!(
718 widened.transform,
719 PlaybackTransform::DopRepack { target_channels: 2 }
720 );
721 }
722 let policy = if mode == "dop-carrier32" {
723 // H1: the review's smallest case.
724 PlaybackPolicy {
725 allowed_pcm_depths: Some(vec![32]),
726 ..confirmed.clone()
727 }
728 } else {
729 confirmed.clone()
730 };
731 out_of_band("reset")?;
732 let decision = dac.resolve_playback(&source, &policy)?;
733 println!(
734 "DOP_DECISION mode={mode} transform={:?} fidelity={:?} format={:?}/{}ch/{}bit preflight={:?} attempted={}",
735 decision.transform,
736 decision.fidelity,
737 decision.format.encoding,
738 decision.format.channels,
739 decision.format.valid_bits,
740 decision.source_preflight_refusal,
741 decision.attempted_candidates
742 );
743 if mode == "dop-no-carrier" {
744 // H2: confirmed DoP on a DAC that cannot carry it falls back like unconfirmed.
745 assert!(decision.source_preflight_refusal.is_some());
746 assert_eq!(decision.format.encoding, Encoding::Pcm);
747 assert!(matches!(
748 decision.transform,
749 PlaybackTransform::DsdToPcm {
750 dop_input: true,
751 ..
752 }
753 ));
754 let unconfirmed = dac.resolve_playback(&source, &PlaybackPolicy::default())?;
755 assert_eq!(unconfirmed.format, decision.format);
756 } else {
757 assert_eq!(decision.format.encoding, Encoding::DoP);
758 assert_eq!(decision.format.channels, 2);
759 assert_eq!(
760 decision.transform,
761 PlaybackTransform::DopRepack { target_channels: 2 }
762 );
763 assert_eq!(
764 decision.fidelity,
765 if source.channels == 2 {
766 PlaybackFidelity::BitExact
767 } else {
768 PlaybackFidelity::Lossless
769 }
770 );
771 assert_eq!(decision.attempted_candidates, 1);
772 }
773 let mut stream = dac.open_playback(&decision, Arc::new(|_| {}))?;
774 let channels = usize::from(source.channels);
775 let frame = decision.source_frame_bytes;
776 let mut next = 0usize;
777 let mut chunk = Vec::new();
778 let start = Instant::now();
779 let mut started = false;
780 while start.elapsed() < Duration::from_secs(3)
781 && (stream.played_frames() == 0 || stream.stats().completed_transfers == 0)
782 {
783 let frames = stream.writable_bytes() / frame;
784 if frames > 0 {
785 chunk.clear();
786 for i in next..next + frames {
787 let marker = if i % 2 == 0 { 0x05 } else { 0xfa };
788 for _ in 0..channels {
789 chunk.extend_from_slice(&[0x69, 0x69, marker]);
790 }
791 }
792 next += stream.write(&chunk)? / frame;
793 }
794 if !started && next > 0 {
795 stream.play()?;
796 started = true;
797 }
798 thread::sleep(Duration::from_millis(2));
799 }
800 let stats = stream.target_stats();
801 let snapshot = out_of_band("snapshot")?;
802 println!("DOP_STATS mode={mode} source_frames={next} {stats:?} {snapshot}");
803 assert!(
804 stats.played_frames > 0 && stats.completed_transfers > 0,
805 "no DoP-resolved completions"
806 );
807 let rates = snapshot.split(';').next().ok_or("missing rate snapshot")?;
808 let carrier_set = rates
809 .split(|c: char| !c.is_ascii_digit())
810 .any(|v| v == "176400");
811 // The carrier DACs clock 176.4 kHz; the exact carrier is never set on a DAC without one.
812 assert_eq!(carrier_set, mode != "dop-no-carrier", "{snapshot}");
813 if mode != "dop-no-carrier" {
814 assert!(stats.payload_verified, "the DoP payload must audit clean");
815 }
816 stream.close()?;
817 assert_eq!(alt(fd)?, 0, "stream teardown must restore alternate zero");
818 let retirement = dac.retirement();
819 dac.close()?;
820 assert!(retirement.wait(Duration::from_secs(3)));
821 assert_eq!(alt(fd)?, 0);
822 for interface in [1, 0] {
823 claim(fd, interface, false)?;
824 }
825 println!("USBIP_LIFECYCLE_PASS mode={mode} alt=0 retired=true");
826 Ok(())
827 }pub fn conversion_stats(&self) -> PlaybackConversionStats
Sourcepub fn played_frames(&self) -> u64
pub fn played_frames(&self) -> u64
Examples found in repository?
694 fn dop_resolution(dac: Dac, fd: i32, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
695 use usbdac_core::playback::{PlaybackFidelity, PlaybackPolicy, PlaybackTransform};
696 use usbdac_core::Encoding;
697 let confirmed = PlaybackPolicy {
698 dop_confirmed: true,
699 ..PlaybackPolicy::default()
700 };
701 let caps = dac.capabilities()?;
702 assert!(
703 caps.exclusion_snapshot_complete,
704 "the fixture descriptor snapshot must be complete"
705 );
706 let source = StreamRequest::dop(2_822_400, if mode == "dop-stereo24" { 1 } else { 2 });
707 if mode == "dop-carrier32" {
708 // H2: 24-bit DoP on a 32-bit-only DAC reaches the 32-bit carrier.
709 let widened = dac.resolve_playback(&source, &confirmed)?;
710 println!(
711 "DOP_DECISION mode={mode} case=widen transform={:?} fidelity={:?} bits={} preflight={:?}",
712 widened.transform, widened.fidelity, widened.format.valid_bits, widened.source_preflight_refusal
713 );
714 assert!(widened.source_preflight_refusal.is_some());
715 assert_eq!(widened.format.encoding, Encoding::DoP);
716 assert_eq!(widened.format.valid_bits, 32);
717 assert_eq!(
718 widened.transform,
719 PlaybackTransform::DopRepack { target_channels: 2 }
720 );
721 }
722 let policy = if mode == "dop-carrier32" {
723 // H1: the review's smallest case.
724 PlaybackPolicy {
725 allowed_pcm_depths: Some(vec![32]),
726 ..confirmed.clone()
727 }
728 } else {
729 confirmed.clone()
730 };
731 out_of_band("reset")?;
732 let decision = dac.resolve_playback(&source, &policy)?;
733 println!(
734 "DOP_DECISION mode={mode} transform={:?} fidelity={:?} format={:?}/{}ch/{}bit preflight={:?} attempted={}",
735 decision.transform,
736 decision.fidelity,
737 decision.format.encoding,
738 decision.format.channels,
739 decision.format.valid_bits,
740 decision.source_preflight_refusal,
741 decision.attempted_candidates
742 );
743 if mode == "dop-no-carrier" {
744 // H2: confirmed DoP on a DAC that cannot carry it falls back like unconfirmed.
745 assert!(decision.source_preflight_refusal.is_some());
746 assert_eq!(decision.format.encoding, Encoding::Pcm);
747 assert!(matches!(
748 decision.transform,
749 PlaybackTransform::DsdToPcm {
750 dop_input: true,
751 ..
752 }
753 ));
754 let unconfirmed = dac.resolve_playback(&source, &PlaybackPolicy::default())?;
755 assert_eq!(unconfirmed.format, decision.format);
756 } else {
757 assert_eq!(decision.format.encoding, Encoding::DoP);
758 assert_eq!(decision.format.channels, 2);
759 assert_eq!(
760 decision.transform,
761 PlaybackTransform::DopRepack { target_channels: 2 }
762 );
763 assert_eq!(
764 decision.fidelity,
765 if source.channels == 2 {
766 PlaybackFidelity::BitExact
767 } else {
768 PlaybackFidelity::Lossless
769 }
770 );
771 assert_eq!(decision.attempted_candidates, 1);
772 }
773 let mut stream = dac.open_playback(&decision, Arc::new(|_| {}))?;
774 let channels = usize::from(source.channels);
775 let frame = decision.source_frame_bytes;
776 let mut next = 0usize;
777 let mut chunk = Vec::new();
778 let start = Instant::now();
779 let mut started = false;
780 while start.elapsed() < Duration::from_secs(3)
781 && (stream.played_frames() == 0 || stream.stats().completed_transfers == 0)
782 {
783 let frames = stream.writable_bytes() / frame;
784 if frames > 0 {
785 chunk.clear();
786 for i in next..next + frames {
787 let marker = if i % 2 == 0 { 0x05 } else { 0xfa };
788 for _ in 0..channels {
789 chunk.extend_from_slice(&[0x69, 0x69, marker]);
790 }
791 }
792 next += stream.write(&chunk)? / frame;
793 }
794 if !started && next > 0 {
795 stream.play()?;
796 started = true;
797 }
798 thread::sleep(Duration::from_millis(2));
799 }
800 let stats = stream.target_stats();
801 let snapshot = out_of_band("snapshot")?;
802 println!("DOP_STATS mode={mode} source_frames={next} {stats:?} {snapshot}");
803 assert!(
804 stats.played_frames > 0 && stats.completed_transfers > 0,
805 "no DoP-resolved completions"
806 );
807 let rates = snapshot.split(';').next().ok_or("missing rate snapshot")?;
808 let carrier_set = rates
809 .split(|c: char| !c.is_ascii_digit())
810 .any(|v| v == "176400");
811 // The carrier DACs clock 176.4 kHz; the exact carrier is never set on a DAC without one.
812 assert_eq!(carrier_set, mode != "dop-no-carrier", "{snapshot}");
813 if mode != "dop-no-carrier" {
814 assert!(stats.payload_verified, "the DoP payload must audit clean");
815 }
816 stream.close()?;
817 assert_eq!(alt(fd)?, 0, "stream teardown must restore alternate zero");
818 let retirement = dac.retirement();
819 dac.close()?;
820 assert!(retirement.wait(Duration::from_secs(3)));
821 assert_eq!(alt(fd)?, 0);
822 for interface in [1, 0] {
823 claim(fd, interface, false)?;
824 }
825 println!("USBIP_LIFECYCLE_PASS mode={mode} alt=0 retired=true");
826 Ok(())
827 }pub fn latency_frames(&self) -> u64
Sourcepub fn close(self) -> Result<()>
pub fn close(self) -> Result<()>
Examples found in repository?
694 fn dop_resolution(dac: Dac, fd: i32, mode: &str) -> Result<(), Box<dyn std::error::Error>> {
695 use usbdac_core::playback::{PlaybackFidelity, PlaybackPolicy, PlaybackTransform};
696 use usbdac_core::Encoding;
697 let confirmed = PlaybackPolicy {
698 dop_confirmed: true,
699 ..PlaybackPolicy::default()
700 };
701 let caps = dac.capabilities()?;
702 assert!(
703 caps.exclusion_snapshot_complete,
704 "the fixture descriptor snapshot must be complete"
705 );
706 let source = StreamRequest::dop(2_822_400, if mode == "dop-stereo24" { 1 } else { 2 });
707 if mode == "dop-carrier32" {
708 // H2: 24-bit DoP on a 32-bit-only DAC reaches the 32-bit carrier.
709 let widened = dac.resolve_playback(&source, &confirmed)?;
710 println!(
711 "DOP_DECISION mode={mode} case=widen transform={:?} fidelity={:?} bits={} preflight={:?}",
712 widened.transform, widened.fidelity, widened.format.valid_bits, widened.source_preflight_refusal
713 );
714 assert!(widened.source_preflight_refusal.is_some());
715 assert_eq!(widened.format.encoding, Encoding::DoP);
716 assert_eq!(widened.format.valid_bits, 32);
717 assert_eq!(
718 widened.transform,
719 PlaybackTransform::DopRepack { target_channels: 2 }
720 );
721 }
722 let policy = if mode == "dop-carrier32" {
723 // H1: the review's smallest case.
724 PlaybackPolicy {
725 allowed_pcm_depths: Some(vec![32]),
726 ..confirmed.clone()
727 }
728 } else {
729 confirmed.clone()
730 };
731 out_of_band("reset")?;
732 let decision = dac.resolve_playback(&source, &policy)?;
733 println!(
734 "DOP_DECISION mode={mode} transform={:?} fidelity={:?} format={:?}/{}ch/{}bit preflight={:?} attempted={}",
735 decision.transform,
736 decision.fidelity,
737 decision.format.encoding,
738 decision.format.channels,
739 decision.format.valid_bits,
740 decision.source_preflight_refusal,
741 decision.attempted_candidates
742 );
743 if mode == "dop-no-carrier" {
744 // H2: confirmed DoP on a DAC that cannot carry it falls back like unconfirmed.
745 assert!(decision.source_preflight_refusal.is_some());
746 assert_eq!(decision.format.encoding, Encoding::Pcm);
747 assert!(matches!(
748 decision.transform,
749 PlaybackTransform::DsdToPcm {
750 dop_input: true,
751 ..
752 }
753 ));
754 let unconfirmed = dac.resolve_playback(&source, &PlaybackPolicy::default())?;
755 assert_eq!(unconfirmed.format, decision.format);
756 } else {
757 assert_eq!(decision.format.encoding, Encoding::DoP);
758 assert_eq!(decision.format.channels, 2);
759 assert_eq!(
760 decision.transform,
761 PlaybackTransform::DopRepack { target_channels: 2 }
762 );
763 assert_eq!(
764 decision.fidelity,
765 if source.channels == 2 {
766 PlaybackFidelity::BitExact
767 } else {
768 PlaybackFidelity::Lossless
769 }
770 );
771 assert_eq!(decision.attempted_candidates, 1);
772 }
773 let mut stream = dac.open_playback(&decision, Arc::new(|_| {}))?;
774 let channels = usize::from(source.channels);
775 let frame = decision.source_frame_bytes;
776 let mut next = 0usize;
777 let mut chunk = Vec::new();
778 let start = Instant::now();
779 let mut started = false;
780 while start.elapsed() < Duration::from_secs(3)
781 && (stream.played_frames() == 0 || stream.stats().completed_transfers == 0)
782 {
783 let frames = stream.writable_bytes() / frame;
784 if frames > 0 {
785 chunk.clear();
786 for i in next..next + frames {
787 let marker = if i % 2 == 0 { 0x05 } else { 0xfa };
788 for _ in 0..channels {
789 chunk.extend_from_slice(&[0x69, 0x69, marker]);
790 }
791 }
792 next += stream.write(&chunk)? / frame;
793 }
794 if !started && next > 0 {
795 stream.play()?;
796 started = true;
797 }
798 thread::sleep(Duration::from_millis(2));
799 }
800 let stats = stream.target_stats();
801 let snapshot = out_of_band("snapshot")?;
802 println!("DOP_STATS mode={mode} source_frames={next} {stats:?} {snapshot}");
803 assert!(
804 stats.played_frames > 0 && stats.completed_transfers > 0,
805 "no DoP-resolved completions"
806 );
807 let rates = snapshot.split(';').next().ok_or("missing rate snapshot")?;
808 let carrier_set = rates
809 .split(|c: char| !c.is_ascii_digit())
810 .any(|v| v == "176400");
811 // The carrier DACs clock 176.4 kHz; the exact carrier is never set on a DAC without one.
812 assert_eq!(carrier_set, mode != "dop-no-carrier", "{snapshot}");
813 if mode != "dop-no-carrier" {
814 assert!(stats.payload_verified, "the DoP payload must audit clean");
815 }
816 stream.close()?;
817 assert_eq!(alt(fd)?, 0, "stream teardown must restore alternate zero");
818 let retirement = dac.retirement();
819 dac.close()?;
820 assert!(retirement.wait(Duration::from_secs(3)));
821 assert_eq!(alt(fd)?, 0);
822 for interface in [1, 0] {
823 claim(fd, interface, false)?;
824 }
825 println!("USBIP_LIFECYCLE_PASS mode={mode} alt=0 retired=true");
826 Ok(())
827 }