Implement Send for Stream on certain platforms#840
Conversation
|
As a small test, the pub fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error>
where
T: SizedSample + FromSample<f32>,
{
// snip
// Create the stream on the main thread...
let stream = device.build_output_stream(
config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
write_data(data, channels, &mut next_value)
},
err_fn,
None,
)?;
// ...then send it to a new thread and play it...
let stream = std::thread::spawn(move || {
stream.play()?;
std::thread::sleep(std::time::Duration::from_millis(1000));
Ok::<_, anyhow::Error>(stream)
});
// ...get it back and then pause and play it again.
let stream = stream.join().unwrap()?;
stream.pause()?;
std::thread::sleep(std::time::Duration::from_millis(1000));
stream.play()?;
std::thread::sleep(std::time::Duration::from_millis(1000));
Ok(())
} |
|
is there anyway we can get this merged? |
|
This change does seem very helpful - it would indeed help in my current use-case also. I am trying to store the stream in a struct field so that it remains in scope and playing, but when I do this I cannot call this struct from any other threads. I am finding work-arounds though. However, would it be simpler to omit the global If the intent is to allow |
|
android is the only platform that prevents Stream to be Send. (am I wrong?) See #961 |
|
I don't think only changing things for Windows makes sense, it subverts the idea of that type: make us non-Send/Sync across all platforms because some don't implement it. For non-desktop platforms like Android or such I think we can make an exception though. |
Objective
SendforStream#818Solution
Moved
NotSendSyncAcrossAllPlatformsinto its own module,maybe_send(for clarity) and implementedSendfor an instance where (I believe) it is safe to do so. I've deliberately left thecfgstatement controlling this implementation verbose to make it clear how other platforms could be included where it is confirmed to be safe to do so:To add this support to Linux but only on FreeBSD using Jack (as an example) then a single statement can be added without modifying any of the others:
I have deliberately only implemented
Sendon Windows under WASAPI in this pull request as I believe it is known to beSendsafe and should be relatively straight-forward to test. I would encourage others to test on platforms they can also confirm the behaviour on and then make follow-up PRs to increase support.Motivation
I personally ran into this issue myself when working with Bevy and thought it was a little annoying. Not a deal breaker by any stretch of the imagination, the workarounds are well documented and perfectly reasonable. This is more of a quality of life improvement than anything else.