Skip to content

Commit

Permalink
fix wave header parser to allow other chunks before the data chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
typester committed Dec 17, 2024
1 parent c8189e6 commit 91f1a8d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions examples/play_from_disk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use livekit::{
},
Room, RoomOptions,
};
use std::{env, mem::size_of, sync::Arc, time::Duration};
use std::{env, io::SeekFrom, mem::size_of, sync::Arc, time::Duration};
use std::{error::Error, io};
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncReadExt, BufReader};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncSeek, AsyncSeekExt, BufReader};

#[derive(Debug, Error)]
pub enum WavError {
Expand All @@ -20,7 +20,7 @@ pub enum WavError {
Io(#[from] io::Error),
}

pub struct WavReader<R: AsyncRead + Unpin> {
pub struct WavReader<R: AsyncRead + AsyncSeek + Unpin> {
reader: R,
}

Expand All @@ -39,7 +39,7 @@ pub struct WavHeader {
bits_per_sample: u16,
}

impl<R: AsyncRead + Unpin> WavReader<R> {
impl<R: AsyncRead + AsyncSeek + Unpin> WavReader<R> {
pub fn new(reader: R) -> Self {
Self { reader }
}
Expand Down Expand Up @@ -76,8 +76,19 @@ impl<R: AsyncRead + Unpin> WavReader<R> {
let byte_rate = self.reader.read_u32_le().await?;
let block_align = self.reader.read_u16_le().await?;
let bits_per_sample = self.reader.read_u16_le().await?;
self.reader.read_exact(&mut data_chunk).await?;
let data_size = self.reader.read_u32_le().await?;

let mut data_size = 0;
loop {
self.reader.read_exact(&mut data_chunk).await?;
data_size = self.reader.read_u32_le().await?;

if &data_chunk == b"data" {
break;
} else {
// skip non data chunks
self.reader.seek(SeekFrom::Current(data_size.into())).await?;
}
}

if &data_chunk != b"data" {
return Err(WavError::InvalidHeader("Invalid data chunk"));
Expand Down

0 comments on commit 91f1a8d

Please sign in to comment.