use cubeb_core; use ffi; use std::ffi::CString; use std::marker::PhantomData; use std::mem::ManuallyDrop; use std::os::raw::{c_long, c_void}; use std::slice::{from_raw_parts, from_raw_parts_mut}; use std::{ops, panic, ptr}; use {ContextRef, DeviceId, Error, Result, State, StreamParamsRef};
/// User supplied data callback. /// /// - Calling other cubeb functions from this callback is unsafe. /// - The code in the callback should be non-blocking. /// - Returning less than the number of frames this callback asks for or /// provides puts the stream in drain mode. This callback will not be called /// again, and the state callback will be called with CUBEB_STATE_DRAINED when /// all the frames have been output. /// /// # Arguments /// /// - `input_buffer`: A slice containing the input data, zero-len if this is an output-only stream. /// - `output_buffer`: A mutable slice to be filled with audio samples, zero-len if this is an input-only stream. /// /// # Return value /// /// If the stream has output, this is the number of frames written to the output buffer. In this /// case, if this number is less than the length of the output buffer, then the stream will start to /// drain. /// /// If the stream is input only, then returning the length of the input buffer indicates data has /// been read. In this case, a value less than that will result in the stream being stopped. pubtype DataCallback<F> = dyn FnMut(&[F], &mut [F]) -> isize + Send + Sync + 'static;
/// User supplied state callback. /// /// # Arguments /// /// `state`: The new state of the stream pubtype StateCallback = dyn FnMut(State) + Send + Sync + 'static;
/// User supplied callback called when the underlying device changed. pubtype DeviceChangedCallback = dyn FnMut() + Send + Sync + 'static;
/// User supplied data callback, see [`DataCallback`] pubfn data_callback<D>(&mutself, cb: D) -> &mutSelf where
D: FnMut(&[F], &mut [F]) -> isize + Send + Sync + 'static,
{ self.data_cb = Some(Box::new(cb) asBox<DataCallback<F>>); self
}
/// User supplied state callback, see [`StateCallback`] pubfn state_callback<S>(&mutself, cb: S) -> &>mutSelf where
S: FnMut(State) + Send + Sync + 'static,
{ self.state_cb = Some(Box::new(cb) asBox<StateCallback>); self
}
/// A name for this stream. pubfn name<T: Into<Vec<u8>>>(&mutself, name: T) -> &mutSelf { self.name = Some(CString::new(name).unwrap()); self
}
/// Use the default input device with `params` /// /// Optional if the stream is output only pubfn default_input(&mutself, params: &>'a StreamParamsRef) -> &mut Self { self.input = Some((ptr::null(), params)); self
}
/// Use a specific input device with `params` /// /// Optional if the stream is output only pubfn input(&mutself, device: DeviceId, params: &'a StreamParamsRef) -> &mut Self { self.input = Some((device, params)); self
}
/// Use the default output device with `params` /// /// Optional if the stream is input only pubfn default_output(&mutself, params: &'a StreamParamsRef) -> &mut Self { self.output = Some((ptr::null(), params)); self
}
/// Use a specific output device with `params` /// /// Optional if the stream is input only pubfn output(&mutself, device: DeviceId, params: &'a StreamParamsRef) -> &mut Self { self.output = Some((device, params)); self
}
/// Stream latency in frames. /// /// Valid range is [1, 96000]. pubfn latency(&mutself, latency: u32) -> &mutSelf { self.latency = Some(latency); self
}
/// User supplied callback called when the underlying device changed. /// /// See [`StateCallback`] /// /// Optional pubfn device_changed_cb<CB>(&mutself, cb: CB) -> &mutSelf where
CB: FnMut() + Send + Sync + 'static,
{ self.device_changed_cb = Some(Box::new(cb) asBox<DeviceChangedCallback>); self
}
// C callable callbacks unsafeextern"C"fn data_cb_c<F>(
_: *mut ffi::cubeb_stream,
user_ptr: *mut c_void,
input_buffer: *const c_void,
output_buffer: *mut c_void,
nframes: c_long,
) -> c_long { let ok = panic::catch_unwind(|| { let cbs = &mut *(user_ptr as *mut StreamCallbacks<F>); let input: &[F] = if input_buffer.is_null() {
&[]
} else {
from_raw_parts(input_buffer as *const _, nframes as usize)
}; let output: &mut [F] = if output_buffer.is_null() {
&mut []
} else {
from_raw_parts_mut(output_buffer as *mut _, nframes as usize)
};
(cbs.data)(input, output) as c_long
});
ok.unwrap_or(0)
}
unsafeextern"C"fn state_cb_c<F>(
_: *mut ffi::cubeb_stream,
user_ptr: *mut c_void,
state: ffi::cubeb_state,
) { let ok = panic::catch_unwind(|| { let state = State::from(state); let cbs = &mut *(user_ptr as *mut StreamCallbacks<F>);
(cbs.state)(state);
});
ok.expect("State callback panicked");
}
unsafeextern"C"fn device_changed_cb_c<F>(user_ptr: *mut c_void) { let ok = panic::catch_unwind(|| { let cbs = &mut *(user_ptr as *mut StreamCallbacks<F>); iflet Some(refmut device_changed) = cbs.device_changed {
device_changed();
}
});
ok.expect("Device changed callback panicked");
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.27 Sekunden
(vorverarbeitet am 2026-06-17)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.