rust 我如何在内存中保存一个cpal流?

nxowjjhe  于 2023-03-02  发布在  其他
关注(0)|答案(1)|浏览(220)

我在rust中的库设计遇到了一些问题。我正在创建一个使用cpal的库。
我希望能够从外部调用start_streamstop_stream,当你调用start_stream时,我创建:

match(device.build_input_stream(... a config, a callback))
{
   Ok(stream_in) => {
      stream_in.play();
      // stream_in goes out of memory instantly and the callback will never be called
   }
   Err(...
}

我基本上认为我可以创建一个全局状态(* 红色标记 *)来保存流,这样我就可以在将来停止它。
我试过这样的方法:

lazy_static! {
    static ref STREAM_IN: Arc<Mutex<Option<Stream>>> = None;
}
// ... later when creating the stream I could save it here.

但这并不符合:

error[E0277]: `*mut ()` cannot be sent between threads safely
  --> src/api.rs:66:1
   |
66 | / lazy_static! {
67 | |     static ref STREAM_IN: Arc<Mutex<Option<Stream>>> = None;
68 | | }
   | |_^ `*mut ()` cannot be sent between threads safely
   |
   = help: within `Option<cpal::Stream>`, the trait `Send` is not implemented for `*mut ()`
   = note: required because it appears within the type `PhantomData<*mut ()>`
   = note: required because it appears within the type `cpal::platform::NotSendSyncAcrossAllPlatforms`
   = note: required because it appears within the type `cpal::Stream`
   = note: required because it appears within the type `Option<cpal::Stream>`
   = note: required for `Mutex<Option<cpal::Stream>>` to implement `std::marker::Sync`
   = note: 1 redundant requirement hidden
   = note: required for `Arc<Mutex<Option<cpal::Stream>>>` to implement `std::marker::Sync`
note: required by a bound in `Lazy`
  --> /Users/user/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-1.4.0/src/inline_lazy.rs:19:20
   |
19 | pub struct Lazy<T: Sync>(Cell<Option<T>>, Once);
   |                    ^^^^ required by this bound in `Lazy`
   = note: this error originates in the macro `__lazy_static_create` which comes from the expansion of the macro `lazy_static` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: `(dyn FnMut() + 'static)` cannot be sent between threads safely
  --> src/api.rs:66:1
   |
66 | / lazy_static! {
67 | |     static ref STREAM_IN: Arc<Mutex<Option<Stream>>> = None;
68 | | }
   | |_^ `(dyn FnMut() + 'static)` cannot be sent between threads safely
   |

谁能告诉我做这件事更好的方法?

gk7wooem

gk7wooem1#

我创建了一个拥有音频流的流。通信是通过通道完成的。
比如:

let (tx, rx): (Sender<CommandRecord>, Receiver<CommandRecord>) = channel();
    let join_handle = thread::spawn(move || {
        match
            device.build_input_stream(
                &config,
                audio_in_callback,
                move |_| {},
                Some(Duration::from_secs(5))
            )
        {
            Ok(stream_in) => {
                stream_in.play().expect("Could not play stream");

                while let Ok(command) = rx.recv() {
                    match command {
                        CommandRecord::Stop => {
                             // do something to stop the stream
                        }
                    }
                }
            }
            Err(_) => {}
        }
    });

相关问题