From 91f1a8d3da3480362c931a65a409684f66073c3d Mon Sep 17 00:00:00 2001 From: Daisuke Murase Date: Mon, 16 Dec 2024 16:32:08 -0800 Subject: [PATCH] fix wave header parser to allow other chunks before the data chunk --- examples/play_from_disk/src/main.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/examples/play_from_disk/src/main.rs b/examples/play_from_disk/src/main.rs index 6aed66fbd..21f0de17a 100644 --- a/examples/play_from_disk/src/main.rs +++ b/examples/play_from_disk/src/main.rs @@ -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 { @@ -20,7 +20,7 @@ pub enum WavError { Io(#[from] io::Error), } -pub struct WavReader { +pub struct WavReader { reader: R, } @@ -39,7 +39,7 @@ pub struct WavHeader { bits_per_sample: u16, } -impl WavReader { +impl WavReader { pub fn new(reader: R) -> Self { Self { reader } } @@ -76,8 +76,19 @@ impl WavReader { 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"));