Skip to main content

active_call/media/
cache.rs

1use anyhow::{Result, anyhow};
2use bytes::BytesMut;
3#[cfg(not(test))]
4use once_cell::sync::Lazy;
5use sha2::{Digest, Sha256};
6#[cfg(not(test))]
7use std::sync::RwLock;
8use std::{
9    io::{IoSlice, SeekFrom},
10    path::PathBuf,
11};
12use tokio::io::{AsyncReadExt, AsyncSeekExt};
13use tokio::{fs::create_dir_all, io::AsyncWriteExt};
14use tracing::{debug, info};
15
16// Default cache directory
17static DEFAULT_CACHE_DIR: &str = "/tmp/mediacache";
18
19// Global cache configuration
20#[cfg(not(test))]
21static CACHE_CONFIG: Lazy<RwLock<CacheConfig>> = Lazy::new(|| {
22    RwLock::new(CacheConfig {
23        cache_dir: PathBuf::from(DEFAULT_CACHE_DIR),
24    })
25});
26
27/// Unit tests run in one process and in parallel, so a process-global cache
28/// directory makes them race: one test would retarget the cache while another
29/// was mid-store, and the file would be gone (or in the wrong directory) by
30/// the next assertion. Keep the test cache directory per thread instead. This
31/// only affects `cargo test`; the published library uses the global above.
32#[cfg(test)]
33thread_local! {
34    static TEST_CACHE_DIR: std::cell::RefCell<Option<PathBuf>> =
35        const { std::cell::RefCell::new(None) };
36}
37
38#[derive(Debug, Clone)]
39pub struct CacheConfig {
40    pub cache_dir: PathBuf,
41}
42
43/// Set the cache directory for the media cache
44pub fn set_cache_dir(path: &str) -> Result<()> {
45    #[cfg(test)]
46    {
47        TEST_CACHE_DIR.with(|dir| *dir.borrow_mut() = Some(PathBuf::from(path)));
48        Ok(())
49    }
50
51    #[cfg(not(test))]
52    {
53        let path = PathBuf::from(path);
54        let mut config = CACHE_CONFIG
55            .write()
56            .map_err(|_| anyhow!("Failed to acquire write lock"))?;
57        config.cache_dir = path;
58        Ok(())
59    }
60}
61
62/// Get the current cache directory
63pub fn get_cache_dir() -> Result<PathBuf> {
64    #[cfg(test)]
65    {
66        Ok(TEST_CACHE_DIR
67            .with(|dir| dir.borrow().clone())
68            .unwrap_or_else(|| PathBuf::from(DEFAULT_CACHE_DIR)))
69    }
70
71    #[cfg(not(test))]
72    {
73        let config = CACHE_CONFIG
74            .read()
75            .map_err(|_| anyhow!("Failed to acquire read lock"))?;
76        Ok(config.cache_dir.clone())
77    }
78}
79
80/// Ensure the cache directory exists
81pub async fn ensure_cache_dir() -> Result<()> {
82    let cache_dir = get_cache_dir()?;
83
84    if !cache_dir.exists() {
85        debug!("Creating cache directory: {:?}", cache_dir);
86        create_dir_all(&cache_dir).await?;
87    }
88
89    Ok(())
90}
91
92/// Generate a cache key from text or URL
93pub fn generate_cache_key(
94    input: &str,
95    sample_rate: u32,
96    speaker: Option<&String>,
97    speed: Option<f32>,
98) -> String {
99    let mut hasher = Sha256::new();
100    hasher.update(input.as_bytes());
101    let result = hasher.finalize();
102    match speaker {
103        Some(speaker) => format!(
104            "{}_{}_{}_{}",
105            hex::encode(result),
106            sample_rate,
107            speaker,
108            speed.unwrap_or(1.0)
109        ),
110        None => format!(
111            "{}_{}_{}",
112            hex::encode(result),
113            sample_rate,
114            speed.unwrap_or(1.0)
115        ),
116    }
117}
118
119/// Get the full path for a cached file
120pub fn get_cache_path(key: &str) -> Result<PathBuf> {
121    let cache_dir = get_cache_dir()?;
122    Ok(cache_dir.join(key).with_extension("pcm"))
123}
124
125/// Check if a file exists in the cache
126pub async fn is_cached(key: &str) -> Result<bool> {
127    let path = get_cache_path(key)?;
128    Ok(tokio::fs::try_exists(&path).await?)
129}
130
131/// Store data in the cache
132pub async fn store_in_cache(key: &str, data: &Vec<u8>) -> Result<()> {
133    ensure_cache_dir().await?;
134    let path = get_cache_path(key)?;
135    tokio::fs::write(&path.with_extension(".tmp"), data).await?;
136    tokio::fs::rename(&path.with_extension(".tmp"), &path).await?;
137    info!("cache: Stored {} -> {} bytes", key, data.len());
138    Ok(())
139}
140
141// Store datas in the cache
142pub async fn store_in_cache_vectored(key: &str, data: &[impl AsRef<[u8]>]) -> Result<()> {
143    ensure_cache_dir().await?;
144    let path = get_cache_path(key)?;
145    let tmp_path = path.with_extension(".tmp");
146    let mut file = tokio::fs::File::create(tmp_path.clone()).await?;
147    let io_slices = data
148        .iter()
149        .map(|d| IoSlice::new(d.as_ref()))
150        .collect::<Vec<_>>();
151    let n = file.write_vectored(&io_slices).await?;
152    tokio::fs::rename(&tmp_path, &path).await?;
153    info!("cache: Stored {} -> {} bytes", key, n);
154    Ok(())
155}
156
157/// Store decoded PCM samples (i16) in the cache as raw little-endian bytes
158pub async fn store_pcm_in_cache(key: &str, samples: &[i16]) -> Result<()> {
159    let mut bytes = Vec::with_capacity(samples.len() * 2);
160    for sample in samples {
161        bytes.extend_from_slice(&sample.to_le_bytes());
162    }
163    store_in_cache(key, &bytes).await
164}
165
166/// Retrieve decoded PCM samples (i16) from the cache stored as raw little-endian bytes
167pub async fn retrieve_pcm_from_cache(key: &str) -> Result<Vec<i16>> {
168    retrieve_pcm_from_cache_at(key, 0).await
169}
170
171/// Retrieve decoded PCM samples (i16) from the cache starting at `sample_offset`
172/// samples in, seeking past the skipped bytes instead of reading them.
173pub async fn retrieve_pcm_from_cache_at(key: &str, sample_offset: usize) -> Result<Vec<i16>> {
174    let path = get_cache_path(key)?;
175    let mut file = tokio::fs::File::open(&path).await?;
176    let file_size = file.metadata().await?.len();
177    let byte_offset = ((sample_offset as u64) * 2).min(file_size);
178    if byte_offset > 0 {
179        file.seek(SeekFrom::Start(byte_offset)).await?;
180    }
181
182    let remaining = (file_size - byte_offset) as usize;
183    let mut bytes = Vec::with_capacity(remaining);
184    file.read_to_end(&mut bytes).await?;
185
186    if bytes.len() % 2 != 0 {
187        return Err(anyhow!(
188            "cache: pcm data length {} is not aligned to i16 for key: {}",
189            bytes.len(),
190            key
191        ));
192    }
193    let samples = bytes
194        .chunks_exact(2)
195        .map(|b| i16::from_le_bytes([b[0], b[1]]))
196        .collect();
197    Ok(samples)
198}
199
200/// Retrieve data from the cache
201pub async fn retrieve_from_cache(key: &str) -> Result<Vec<u8>> {
202    let path = get_cache_path(key)?;
203
204    if !tokio::fs::try_exists(&path).await? {
205        return Err(anyhow!("Cache file not found for key: {}", key));
206    }
207
208    let data = tokio::fs::read(&path).await?;
209    debug!(key, size = data.len(), "retrieved file from cache");
210    Ok(data)
211}
212
213// Retrieve data from the cache with a buffer
214pub async fn retrieve_from_cache_with_buffer(key: &str, buffer: &mut BytesMut) -> Result<()> {
215    let path = get_cache_path(key)?;
216    let mut file = tokio::fs::File::open(path).await?;
217    let metadata = file.metadata().await?;
218    let file_size = metadata.len() as usize;
219    buffer.reserve(file_size);
220
221    while file.read_buf(buffer).await? > 0 {}
222    Ok(())
223}
224
225/// Delete a specific file from the cache
226pub async fn delete_from_cache(key: &str) -> Result<()> {
227    let path = get_cache_path(key)?;
228
229    if tokio::fs::try_exists(&path).await? {
230        tokio::fs::remove_file(path).await?;
231        debug!("Deleted file from cache with key: {}", key);
232    }
233
234    Ok(())
235}
236
237#[cfg(test)]
238mod tests {
239    use super::*;
240
241    /// Give this test its own cache directory so it cannot observe another
242    /// test retargeting or deleting the cache (see `TEST_CACHE_DIR`).
243    fn test_cache_dir() -> Result<tempfile::TempDir> {
244        let dir = tempfile::tempdir()?;
245        set_cache_dir(dir.path().to_str().unwrap())?;
246        Ok(dir)
247    }
248
249    #[tokio::test]
250    async fn test_cache_operations() -> Result<()> {
251        let _cache_dir = test_cache_dir()?;
252        ensure_cache_dir().await?;
253
254        // Generate a cache key
255        let key = generate_cache_key("test_data", 8000, None, None);
256
257        // Test storing data in cache
258        let test_data = b"TEST DATA".to_vec();
259        store_in_cache(&key, &test_data).await?;
260
261        // Test if data is cached
262        assert!(is_cached(&key).await?);
263
264        // Test retrieving data from cache
265        let retrieved_data = retrieve_from_cache(&key).await?;
266        assert_eq!(retrieved_data, test_data);
267
268        // Test deleting data from cache
269        delete_from_cache(&key).await?;
270        assert!(!is_cached(&key).await?);
271
272        // Test clean cache
273        let key2 = generate_cache_key("test_data2", 16000, None, None);
274        store_in_cache(&key2, &test_data).await?;
275        Ok(())
276    }
277
278    #[tokio::test]
279    async fn test_pcm_cache_roundtrip_and_offset() -> Result<()> {
280        let _cache_dir = test_cache_dir()?;
281        let key = generate_cache_key("test_pcm_offset", 16000, None, None);
282        delete_from_cache(&key).await.ok();
283
284        let samples: Vec<i16> = (0..1000i16).collect();
285        store_pcm_in_cache(&key, &samples).await?;
286
287        // Full retrieval matches what we stored.
288        let full = retrieve_pcm_from_cache(&key).await?;
289        assert_eq!(full, samples);
290
291        // Offset retrieval matches slicing the full buffer.
292        let offset = 250;
293        let tail = retrieve_pcm_from_cache_at(&key, offset).await?;
294        assert_eq!(tail, samples[offset..]);
295
296        // Offset past the end yields an empty buffer rather than erroring.
297        let empty = retrieve_pcm_from_cache_at(&key, samples.len() + 100).await?;
298        assert!(empty.is_empty());
299
300        delete_from_cache(&key).await?;
301        Ok(())
302    }
303
304    #[test]
305    fn test_generate_cache_key() {
306        let key1 = generate_cache_key("hello", 16000, None, None);
307        let key2 = generate_cache_key("hello", 8000, None, None);
308        let key3 = generate_cache_key("world", 16000, None, None);
309
310        // Same input with different sample rates should produce different keys
311        assert_ne!(key1, key2);
312
313        // Different inputs with same sample rate should produce different keys
314        assert_ne!(key1, key3);
315    }
316
317    #[tokio::test]
318    async fn test_store_in_cache_v() -> Result<()> {
319        let _cache_dir = test_cache_dir()?;
320        // Test data as multiple slices
321        let data1 = b"Hello, ";
322        let data2 = b"world!";
323        let data3 = b" This is a test.";
324        let data_slices = [data1.as_slice(), data2.as_slice(), data3.as_slice()];
325
326        let key = generate_cache_key("test_vectored_store", 16000, None, None);
327
328        // Ensure the key doesn't exist initially
329        delete_from_cache(&key).await.ok();
330        assert!(!is_cached(&key).await?);
331
332        // Store using vectored write
333        store_in_cache_vectored(&key, &data_slices).await?;
334
335        // Verify it was stored
336        assert!(is_cached(&key).await?);
337
338        // Retrieve and verify content
339        let retrieved = retrieve_from_cache(&key).await?;
340        let expected = [data1.as_slice(), data2.as_slice(), data3.as_slice()].concat();
341        assert_eq!(retrieved, expected);
342
343        // Clean up
344        delete_from_cache(&key).await?;
345        Ok(())
346    }
347
348    #[tokio::test]
349    async fn test_store_in_cache_v_empty_slices() -> Result<()> {
350        let _cache_dir = test_cache_dir()?;
351        let empty_data: &[&[u8]] = &[];
352        let key = generate_cache_key("test_empty_vectored", 16000, None, None);
353
354        // Clean up first
355        delete_from_cache(&key).await.ok();
356
357        // Store empty data
358        store_in_cache_vectored(&key, empty_data).await?;
359
360        // Verify it was stored as empty file
361        assert!(is_cached(&key).await?);
362        let retrieved = retrieve_from_cache(&key).await?;
363        assert_eq!(retrieved.len(), 0);
364
365        // Clean up
366        delete_from_cache(&key).await?;
367        Ok(())
368    }
369
370    #[tokio::test]
371    async fn test_retrieve_from_cache_with_buffer() -> Result<()> {
372        let _cache_dir = test_cache_dir()?;
373        // Test data
374        let test_data = b"This is test data for buffer retrieval testing.";
375        let key = generate_cache_key("test_buffer_retrieve", 16000, None, None);
376
377        // Clean up first
378        delete_from_cache(&key).await.ok();
379
380        // Store test data using regular store
381        store_in_cache(&key, &test_data.to_vec()).await?;
382
383        // Retrieve using buffer method
384        let mut buffer = BytesMut::new();
385        retrieve_from_cache_with_buffer(&key, &mut buffer).await?;
386
387        // Verify content
388        assert_eq!(buffer.as_ref(), test_data);
389        assert_eq!(buffer.len(), test_data.len());
390
391        // Clean up
392        delete_from_cache(&key).await?;
393        Ok(())
394    }
395
396    #[tokio::test]
397    async fn test_retrieve_from_cache_with_buffer_large_file() -> Result<()> {
398        let _cache_dir = test_cache_dir()?;
399        // Create larger test data (1MB)
400        let large_data: Vec<u8> = vec![7; 1024 * 1024];
401        let key = generate_cache_key("test_large_buffer", 16000, None, None);
402
403        // Clean up first
404        delete_from_cache(&key).await.ok();
405
406        // Store large data
407        store_in_cache(&key, &large_data).await?;
408
409        // Retrieve using buffer method
410        let mut buffer = BytesMut::new();
411        retrieve_from_cache_with_buffer(&key, &mut buffer).await?;
412
413        // Verify content
414        assert_eq!(buffer.len(), large_data.len());
415        assert_eq!(buffer.as_ref(), large_data.as_slice());
416
417        // Clean up
418        delete_from_cache(&key).await?;
419        Ok(())
420    }
421
422    #[tokio::test]
423    async fn test_retrieve_from_cache_with_buffer_nonexistent() -> Result<()> {
424        let _cache_dir = test_cache_dir()?;
425        let nonexistent_key = generate_cache_key("nonexistent_file", 16000, None, None);
426        let mut buffer = BytesMut::new();
427
428        // Should fail for nonexistent file
429        let result = retrieve_from_cache_with_buffer(&nonexistent_key, &mut buffer).await;
430        assert!(result.is_err());
431
432        Ok(())
433    }
434
435    #[tokio::test]
436    async fn test_store_v_and_retrieve_buffer_integration() -> Result<()> {
437        let _cache_dir = test_cache_dir()?;
438        // Test integration between vectored store and buffer retrieve
439        let data_parts = [
440            b"Part 1: Hello".as_slice(),
441            b", Part 2: World".as_slice(),
442            b", Part 3: Integration Test!".as_slice(),
443        ];
444        let key = generate_cache_key("test_integration", 16000, None, None);
445
446        // Clean up first
447        delete_from_cache(&key).await.ok();
448
449        // Store using vectored write
450        store_in_cache_vectored(&key, &data_parts).await?;
451
452        // Retrieve using buffer method
453        let mut buffer = BytesMut::new();
454        retrieve_from_cache_with_buffer(&key, &mut buffer).await?;
455
456        // Verify the data was correctly concatenated
457        let expected = data_parts.concat();
458        assert_eq!(buffer.as_ref(), expected.as_slice());
459        assert_eq!(buffer.len(), expected.len());
460
461        // Also verify with regular retrieve for double-check
462        let regular_retrieve = retrieve_from_cache(&key).await?;
463        assert_eq!(regular_retrieve, expected);
464        assert_eq!(buffer.as_ref(), regular_retrieve.as_slice());
465
466        // Clean up
467        delete_from_cache(&key).await?;
468        Ok(())
469    }
470}