Skip to content

Commit

Permalink
Fix audio; fix Buffer docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zmerp committed Aug 16, 2023
1 parent d9fbff2 commit 795921d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion alvr/audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ pub fn record_audio_blocking(
};

if is_streaming.value() {
let buffer = sender.get_buffer(&()).unwrap();
let mut buffer = sender.get_buffer(&()).unwrap();
buffer.get_range_mut(0, data.len()).copy_from_slice(&data);
sender.send(buffer).ok();
} else {
*state.lock() = AudioRecordState::ShouldStop;
Expand Down
7 changes: 5 additions & 2 deletions alvr/client_core/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ impl AudioInputCallback for RecorderCallback {
_: &mut dyn AudioInputStreamSafe,
frames: &[i16],
) -> DataCallbackResult {
let mut sample_buffer = Vec::<u8>::with_capacity(frames.len() * mem::size_of::<i16>());
let mut sample_buffer = Vec::with_capacity(frames.len() * mem::size_of::<i16>());

for frame in frames {
sample_buffer.extend(&frame.to_ne_bytes());
}

if self.running.value() {
let buffer = self.sender.get_buffer(&()).unwrap();
let mut buffer = self.sender.get_buffer(&()).unwrap();
buffer
.get_range_mut(0, sample_buffer.len())
.copy_from_slice(&sample_buffer);
self.sender.send(buffer).ok();

DataCallbackResult::Continue
Expand Down
16 changes: 8 additions & 8 deletions alvr/sockets/src/stream_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const SHARD_PREFIX_SIZE: usize = mem::size_of::<u32>() // packet length - field
+ mem::size_of::<u32>() // shards count
+ mem::size_of::<u32>(); // shards index

// Memory buffer that contains a hidden prefix
/// Memory buffer that contains a hidden prefix
#[derive(Default)]
pub struct Buffer<H = ()> {
inner: Vec<u8>,
Expand All @@ -48,7 +48,7 @@ pub struct Buffer<H = ()> {
}

impl<H> Buffer<H> {
// Length of payload (without prefix)
/// Length of payload (without prefix)
#[must_use]
pub fn len(&self) -> usize {
self.length
Expand All @@ -59,13 +59,13 @@ impl<H> Buffer<H> {
self.len() == 0
}

// Get the whole payload of the buffer
/// Get the whole payload of the buffer
pub fn get(&self) -> &[u8] {
&self.inner[self.hidden_offset..][..self.length]
}

// Note: If the range is outside the valid range, new space will be allocated
// NB: the offset parameter is applied on top of the internal offset of the buffer
/// If the range is outside the valid range, new space will be allocated
/// NB: the offset parameter is applied on top of the internal offset of the buffer
pub fn get_range_mut(&mut self, offset: usize, size: usize) -> &mut [u8] {
let required_size = self.hidden_offset + offset + size;
if required_size > self.inner.len() {
Expand All @@ -77,7 +77,7 @@ impl<H> Buffer<H> {
&mut self.inner[self.hidden_offset + offset..][..size]
}

// if length > current length, allocate more space
/// If length > current length, allocate more space
pub fn set_len(&mut self, length: usize) {
self.inner.resize(self.hidden_offset + length, 0);
self.length = length;
Expand All @@ -96,8 +96,8 @@ pub struct StreamSender<H> {
}

impl<H> StreamSender<H> {
// Shard and send a buffer with zero copies and zero allocations.
// The prefix of each shard is written over the previously sent shard to avoid reallocations.
/// Shard and send a buffer with zero copies and zero allocations.
/// The prefix of each shard is written over the previously sent shard to avoid reallocations.
pub fn send(&mut self, mut buffer: Buffer<H>) -> Result<()> {
let max_shard_data_size = self.max_packet_size - SHARD_PREFIX_SIZE;
let actual_buffer_size = buffer.hidden_offset + buffer.length;
Expand Down

0 comments on commit 795921d

Please sign in to comment.