1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use crate::{error::*, ffi, shared::*};
wrap!(
/// Context for an Audio FIFO Buffer.
///
/// - Operates at the sample level rather than the byte level.
/// - Supports multiple channels with either planar or packed sample format.
/// - Automatic reallocation when writing to a full buffer.
AVAudioFifo: ffi::AVAudioFifo
);
impl AVAudioFifo {
/// Allocate an AVAudioFifo.
pub fn new(sample_fmt: ffi::AVSampleFormat, channels: i32, nb_samples: i32) -> Self {
let fifo = unsafe { ffi::av_audio_fifo_alloc(sample_fmt, channels, nb_samples) }
.upgrade()
.unwrap();
unsafe { Self::from_raw(fifo) }
}
/// Reallocate an AVAudioFifo.
pub fn realloc(&mut self, nb_samples: i32) {
// Almost only panic on no memory, in other cases panic on invalid
// parameters which is not possible with current good API.
unsafe { ffi::av_audio_fifo_realloc(self.as_mut_ptr(), nb_samples) }
.upgrade()
.unwrap();
}
/// Write data to an AVAudioFifo.
///
/// The AVAudioFifo will be reallocated automatically if the available space
/// is less than nb_samples.
///
/// # Safety
/// Function is safe when the `data` points to valid samples.
pub unsafe fn write(&mut self, data: *const *mut u8, nb_samples: i32) -> Result<()> {
let ret = unsafe { ffi::av_audio_fifo_write(self.as_mut_ptr(), data as _, nb_samples) }
.upgrade()?;
debug_assert_eq!(ret, nb_samples);
Ok(())
}
/// Peek data from an AVAudioFifo.
///
/// # Safety
/// Function is safe when the `data` points to valid sample buffer.
pub unsafe fn peek(&mut self, data: *const *mut u8, nb_samples: i32) -> Result<i32> {
let ret = unsafe { ffi::av_audio_fifo_peek(self.as_mut_ptr(), data as _, nb_samples) }
.upgrade()?;
Ok(ret)
}
/// Peek data from an AVAudioFifo.
///
/// # Safety
/// Function is safe when the `data` points to valid sample buffer.
pub unsafe fn peek_at(
&mut self,
data: *const *mut u8,
nb_samples: i32,
offset: i32,
) -> Result<i32> {
let ret =
unsafe { ffi::av_audio_fifo_peek_at(self.as_mut_ptr(), data as _, nb_samples, offset) }
.upgrade()?;
Ok(ret)
}
/// Read data from an AVAudioFifo.
///
/// This function returns actually read size if success.
///
/// # Safety
/// Function is safe when the `data` points to valid array such as AVFrame::data.
pub unsafe fn read(&mut self, data: *const *mut u8, nb_samples: i32) -> Result<i32> {
let ret = unsafe { ffi::av_audio_fifo_read(self.as_mut_ptr(), data as _, nb_samples) }
.upgrade()?;
Ok(ret)
}
/// Drain data from an AVAudioFifo.
///
/// Removes the data without reading it.
pub fn drain(&mut self, nb_samples: i32) {
// FFI function only error when the nb_samples is negative.
unsafe { ffi::av_audio_fifo_drain(self.as_mut_ptr(), nb_samples) }
.upgrade()
.unwrap();
}
/// Reset the AVAudioFifo buffer.
///
/// This empties all data in the buffer.
pub fn reset(&mut self) {
unsafe { ffi::av_audio_fifo_reset(self.as_mut_ptr()) }
}
/// Get the current number of samples in the [`AVAudioFifo`] available for
/// reading.
pub fn size(&self) -> i32 {
unsafe {
// function doesn't modify self, casting safe
ffi::av_audio_fifo_size(self.as_ptr() as *mut _)
}
}
/// Get the current number of samples in the [`AVAudioFifo`] available for
/// writing.
pub fn space(&self) -> i32 {
unsafe {
// function doesn't modify self, casting safe
ffi::av_audio_fifo_space(self.as_ptr() as *mut _)
}
}
}
impl Drop for AVAudioFifo {
fn drop(&mut self) {
unsafe { ffi::av_audio_fifo_free(self.as_mut_ptr()) }
}
}