Skip to content

Commit

Permalink
avoid copying data
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba-- committed Jun 6, 2024
1 parent a99494a commit 41385da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
34 changes: 9 additions & 25 deletions examples/playogg_from_disk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ use livekit::{
options::TrackPublishOptions,
track::{LocalAudioTrack, LocalTrack, TrackSource},
webrtc::{
audio_source::native::NativeAudioSource,
audio_source::native::EncodedAudioSource,
audio_source::native::NativeAudioSource,
prelude::{AudioFrame_u8, AudioSourceOptions, RtcAudioSource},
},
Room, RoomOptions,
};
use std::{env, sync::Arc, time::Duration, error::Error};
use std::fs::File;
use std::io::{BufReader};
use webrtc_media::io::ogg_reader::{OggReader};

use std::io::BufReader;
use std::{env, error::Error, sync::Arc, time::Duration};
use webrtc_media::io::ogg_reader::OggReader;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -40,9 +39,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
header.version,
);

let (room, mut rx) = Room::connect(&url, &token, RoomOptions::default())
.await
.unwrap();
let (room, mut rx) = Room::connect(&url, &token, RoomOptions::default()).await.unwrap();
let room = Arc::new(room);
log::info!("Connected to room: {} - {}", room.name(), room.sid());

Expand All @@ -63,12 +60,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
room.local_participant()
.publish_track(
LocalTrack::Audio(track),
TrackPublishOptions {
source: TrackSource::Microphone,
..Default::default()
},
).await?;

TrackPublishOptions { source: TrackSource::Microphone, ..Default::default() },
)
.await?;

// Play the wav file and disconnect
tokio::spawn({
Expand All @@ -78,24 +72,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
log::info!("num_channels: {}", header.channels);

while let Ok((page_data, page_header)) = reader.parse_next_page() {

let frame_size = page_data.len();
let mut audio_frame = AudioFrame_u8 {
data: vec![0u8; 0].into(),
data: page_data.freeze().into(),
num_channels: header.channels as u32,
sample_rate: header.sample_rate,
samples_per_channel: (frame_size / header.channels as usize) as u32,
};

let mut bytes = 0;
for d in page_data.iter() {
bytes += 1;
audio_frame.data.push(*d);
}

log::info!("samples_per_channel: {}, bytes: {}, frame_size: {}",
audio_frame.samples_per_channel, bytes, frame_size);

source.capture_frame(&audio_frame).await.unwrap();
}

Expand Down
4 changes: 2 additions & 2 deletions webrtc-sys/src/audio_track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void NativeAudioSink::OnData(const void* audio_data,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames) {
RTC_CHECK_EQ(16, bits_per_sample);
// RTC_CHECK_EQ(16, bits_per_sample);
rust::Slice<const int16_t> data(static_cast<const int16_t*>(audio_data),
number_of_channels * number_of_frames);
observer_->on_data(data, sample_rate, number_of_channels, number_of_frames);
Expand Down Expand Up @@ -150,7 +150,7 @@ void AudioTrackSource::InternalSource::on_captured_frame_u8(
size_t number_of_frames) {
webrtc::MutexLock lock(&mutex_);
for (auto sink : sinks_) {
sink->OnData(data.data(), 8, 2 * sample_rate, number_of_channels,
sink->OnData(data.data(), 16, sample_rate, number_of_channels,
number_of_frames);
}
}
Expand Down

0 comments on commit 41385da

Please sign in to comment.