diff --git a/cras/server/processor/processor.h b/cras/server/processor/processor.h index bcd62e0a..06e2a107 100644 --- a/cras/server/processor/processor.h +++ b/cras/server/processor/processor.h @@ -27,11 +27,18 @@ enum CrasProcessorWrapMode { */ WrapModeDedicatedThread, /** - * Run the processor pipeline with a ChunkWrapper. + * Run the processor pipeline with a ChunkWrapper with the inner block + * size set to [`CrasProcessorConfig::block_size`]. * In this mode, the caller is allowed to run the pipeline with a block - * size that is different from `CrasProcessorConfig::block_size` + * size that is different from [`CrasProcessorConfig::block_size`]. */ WrapModeChunk, + /** + * Like `WrapModeChunk` but the pipeline is run inside a peer processor (sandbox). + * [`CrasProcessorConfig::max_block_size`] must be set in this mode. + * WAVE dump is not supported in this mode. + */ + WrapModePeerChunk, }; struct CrasProcessorCreateResult { @@ -53,6 +60,11 @@ struct CrasProcessorConfig { enum CrasProcessorEffect effect; enum CrasProcessorWrapMode wrap_mode; bool wav_dump; + /** + * The max block size when wrap_mode is WrapModePeerChunk. + * Used to determine buffer size to allocate for peer IPC. + */ + size_t max_block_size; }; /** diff --git a/cras/server/processor/src/lib.rs b/cras/server/processor/src/lib.rs index 2fff751a..4c7bfec4 100644 --- a/cras/server/processor/src/lib.rs +++ b/cras/server/processor/src/lib.rs @@ -38,10 +38,15 @@ pub enum CrasProcessorWrapMode { WrapModeNone, /// Run the processor pipeline in a separate, dedicated thread. WrapModeDedicatedThread, - /// Run the processor pipeline with a ChunkWrapper. + /// Run the processor pipeline with a ChunkWrapper with the inner block + /// size set to [`CrasProcessorConfig::block_size`]. /// In this mode, the caller is allowed to run the pipeline with a block - /// size that is different from `CrasProcessorConfig::block_size` + /// size that is different from [`CrasProcessorConfig::block_size`]. WrapModeChunk, + /// Like `WrapModeChunk` but the pipeline is run inside a peer processor (sandbox). + /// [`CrasProcessorConfig::max_block_size`] must be set in this mode. + /// WAVE dump is not supported in this mode. + WrapModePeerChunk, } #[repr(C)] @@ -58,6 +63,10 @@ pub struct CrasProcessorConfig { // Enable processing dumps as WAVE files. wav_dump: bool, + + /// The max block size when wrap_mode is WrapModePeerChunk. + /// Used to determine buffer size to allocate for peer IPC. + max_block_size: usize, } impl CrasProcessorConfig { @@ -275,22 +284,37 @@ impl CrasProcessor { }); } - let pipeline = if matches!(config.wrap_mode, CrasProcessorWrapMode::WrapModeChunk) { - Processor::WrapChunk { - inner: Box::new(Processor::Pipeline { processors: decl }), + let mut pipeline = Processor::Pipeline { processors: decl }; + if matches!( + config.wrap_mode, + CrasProcessorWrapMode::WrapModeChunk | CrasProcessorWrapMode::WrapModePeerChunk + ) { + pipeline = Processor::WrapChunk { + inner: Box::new(pipeline), inner_block_size: config.block_size, disallow_hoisting: true, + }; + if matches!(config.wrap_mode, CrasProcessorWrapMode::WrapModePeerChunk) { + pipeline = Processor::Peer { + processor: Box::new(pipeline), + }; } - } else { - Processor::Pipeline { processors: decl } - }; + } let decl_debug = format!("{pipeline:?}"); - let pipeline = PipelineBuilder::new(config.format()) - // TODO(b/349784210): Use a hardened worker factory. - .with_worker_factory(AudioWorkerSubprocessFactory::default().with_set_thread_priority()) - .build(pipeline) - .context("failed to build pipeline")?; + let pipeline = PipelineBuilder::new(Format { + block_size: if matches!(config.wrap_mode, CrasProcessorWrapMode::WrapModePeerChunk) { + assert_ne!(config.max_block_size, 0); + config.max_block_size // Used for allocation. + } else { + config.block_size + }, + ..config.format() + }) + // TODO(b/349784210): Use a hardened worker factory. + .with_worker_factory(AudioWorkerSubprocessFactory::default().with_set_thread_priority()) + .build(pipeline) + .context("failed to build pipeline")?; log::info!("CrasProcessor #{id} created with: {config:?}"); log::info!("CrasProcessor #{id} pipeline: {decl_debug}"); diff --git a/cras/src/server/cras_dsp_mod_builtin.c b/cras/src/server/cras_dsp_mod_builtin.c index 8437e31a..363893e6 100644 --- a/cras/src/server/cras_dsp_mod_builtin.c +++ b/cras/src/server/cras_dsp_mod_builtin.c @@ -822,10 +822,11 @@ static int cras_processor_instantiate(struct dsp_module* module, struct CrasProcessorConfig cfg = { .channels = data->channels, .block_size = 256, - .wrap_mode = WrapModeChunk, + .max_block_size = DSP_BUFFER_SIZE, + .wrap_mode = WrapModePeerChunk, .frame_rate = sample_rate, .effect = data->effect, - .wav_dump = cras_feature_enabled(CrOSLateBootCrasProcessorWavDump), + .wav_dump = false, // Not supported for WrapModePeerChunk. }; struct CrasProcessorCreateResult result = cras_processor_create(&cfg, NULL);