Skip to content

Commit

Permalink
Fix play_from_disk example to work with recent SDK (#529)
Browse files Browse the repository at this point in the history
* fix compilation error

* fix wave header parser to allow other chunks before the data chunk
  • Loading branch information
typester authored Dec 17, 2024
1 parent 3d9168a commit e04a3cc
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 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 Expand Up @@ -123,12 +134,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
.await
.unwrap();
let room = Arc::new(room);
log::info!("Connected to room: {} - {}", room.name(), room.sid());
log::info!("Connected to room: {} - {}", room.name(), room.sid().await);

let source = NativeAudioSource::new(
AudioSourceOptions::default(),
header.sample_rate,
header.num_channels as u32,
1000,
);

let track = LocalAudioTrack::create_audio_track("file", RtcAudioSource::Native(source.clone()));
Expand Down

0 comments on commit e04a3cc

Please sign in to comment.