Skip to main content

PlaybackStream

Struct PlaybackStream 

Source
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

Source

pub fn decision(&self) -> &PlaybackDecision

Source

pub fn format(&self) -> &StreamFormat

Source

pub fn set_observer( &mut self, observer: Option<Arc<dyn Fn(&[u8]) + Send + Sync>>, )

Source

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.

Source

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?
examples/usbip_lifecycle.rs (line 792)
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    }
Source

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.

Source

pub fn play(&self) -> Result<()>

Examples found in repository?
examples/usbip_lifecycle.rs (line 795)
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    }
Source

pub fn pause(&self) -> Result<()>

Source

pub fn flush(&mut self) -> Result<()>

Source

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?
examples/usbip_lifecycle.rs (line 783)
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    }
Source

pub fn is_drained(&self) -> bool

Source

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?
examples/usbip_lifecycle.rs (line 781)
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    }
Source

pub fn target_stats(&self) -> StreamStats

Raw target transport statistics; not a claim of source fidelity.

Examples found in repository?
examples/usbip_lifecycle.rs (line 800)
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    }
Source

pub fn conversion_stats(&self) -> PlaybackConversionStats

Source

pub fn played_frames(&self) -> u64

Examples found in repository?
examples/usbip_lifecycle.rs (line 781)
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    }
Source

pub fn latency_frames(&self) -> u64

Source

pub fn close(self) -> Result<()>

Examples found in repository?
examples/usbip_lifecycle.rs (line 816)
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    }

Auto Trait Implementations§

§

impl !Freeze for PlaybackStream

§

impl !RefUnwindSafe for PlaybackStream

§

impl !UnwindSafe for PlaybackStream

§

impl Forget for PlaybackStream

§

impl Move for PlaybackStream

§

impl Send for PlaybackStream
where Arc<StreamInner>: Send, RawVecInner: Send, BoxRaw<FloatEngine>: Send, BoxRaw<DsdPcmConverter>: Send, Mutex<RawMutex, Staged>: Send, Arc<dyn Fn(&[u8]) + Sync + Send>: Send, Arc<dyn Fn(ProducerTiming) + Sync + Send>: Send,

§

impl Sync for PlaybackStream
where Arc<StreamInner>: Sync, RawVecInner: Sync, BoxRaw<FloatEngine>: Sync, BoxRaw<DsdPcmConverter>: Sync, Mutex<RawMutex, Staged>: Sync, Arc<dyn Fn(&[u8]) + Sync + Send>: Sync, Arc<dyn Fn(ProducerTiming) + Sync + Send>: Sync,

§

impl Unpin for PlaybackStream
where Arc<StreamInner>: Unpin, *const u8 is !null: Unpin, Box<FloatEngine>: Unpin, Box<DsdPcmConverter>: Unpin, Align1<u8>: Unpin, Arc<dyn Fn(&[u8]) + Sync + Send>: Unpin, Arc<dyn Fn(ProducerTiming) + Sync + Send>: Unpin,

§

impl UnsafeUnpin for PlaybackStream

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.