Skip to main content

adk_audio/pipeline/
handle.rs

1//! Pipeline handle for interacting with a running pipeline.
2
3use std::sync::Arc;
4
5use tokio::sync::{RwLock, mpsc, oneshot};
6
7use crate::pipeline::types::{PipelineInput, PipelineMetrics, PipelineOutput};
8
9/// Handle to a running audio pipeline.
10///
11/// Provides channels for sending input, receiving output, reading metrics,
12/// and shutting down the pipeline.
13pub struct PipelineHandle {
14    /// Send audio, text, or control messages into the pipeline.
15    pub input_tx: mpsc::Sender<PipelineInput>,
16    /// Receive audio, transcript, or metrics output from the pipeline.
17    pub output_rx: mpsc::Receiver<PipelineOutput>,
18    /// Real-time pipeline metrics (updated after each stage).
19    pub metrics: Arc<RwLock<PipelineMetrics>>,
20    /// One-shot channel to signal graceful shutdown.
21    shutdown_tx: Option<oneshot::Sender<()>>,
22}
23
24impl PipelineHandle {
25    /// Create a new `PipelineHandle`.
26    pub(crate) fn new(
27        input_tx: mpsc::Sender<PipelineInput>,
28        output_rx: mpsc::Receiver<PipelineOutput>,
29        metrics: Arc<RwLock<PipelineMetrics>>,
30        shutdown_tx: oneshot::Sender<()>,
31    ) -> Self {
32        Self { input_tx, output_rx, metrics, shutdown_tx: Some(shutdown_tx) }
33    }
34
35    /// Signal the pipeline to shut down gracefully.
36    pub fn shutdown(&mut self) {
37        if let Some(tx) = self.shutdown_tx.take() {
38            let _ = tx.send(());
39        }
40    }
41}