Skip to content

Commit

Permalink
Make media_recorder::Blob more spec compliant (to denote mimetype)
Browse files Browse the repository at this point in the history
  • Loading branch information
orottier committed Jul 24, 2024
1 parent cd21ecb commit 1b53a06
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions examples/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ fn main() {
eprintln!(
"timecode {:.6}, data size {}",
event.timecode,
event.blob.len()
event.blob.size()
);
std::io::stdout().write_all(&event.blob).unwrap();
std::io::stdout().write_all(&event.blob.data).unwrap();
});
recorder.start();

Expand Down
35 changes: 29 additions & 6 deletions src/media_recorder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ impl MediaRecorderInner {
.duration_since(recorded_data.start_timecode)
.as_secs_f64();

let send = std::mem::replace(&mut recorded_data.blob, Vec::with_capacity(128 * 1024));
let data = std::mem::replace(&mut recorded_data.blob, Vec::with_capacity(128 * 1024));
if let Some(f) = self.data_available_callback.lock().unwrap().as_mut() {
let blob = Blob {
data,
type_: "audio/wav",
};
let event = BlobEvent {
blob: send,
blob,
timecode,
event: Event { type_: "BlobEvent" },
};
Expand Down Expand Up @@ -147,7 +151,7 @@ pub struct MediaRecorderOptions {
/// let options = MediaRecorderOptions::default(); // default to audio/wav
/// let recorder = MediaRecorder::new(output.stream(), options);
/// recorder.set_ondataavailable(|event| {
/// println!("Received {} bytes of data", event.blob.len());
/// println!("Received {} bytes of data", event.blob.size());
/// });
/// recorder.start();
/// ```
Expand Down Expand Up @@ -302,15 +306,34 @@ impl MediaRecorder {
#[non_exhaustive]
#[derive(Debug)]
pub struct BlobEvent {
/// The encoded data
pub blob: Vec<u8>,
/// The encoded Blob whose type attribute indicates the encoding of the blob data.
pub blob: Blob,
/// The difference between the timestamp of the first chunk in data and the timestamp of the
/// first chunk in the first BlobEvent produced by this recorder
pub timecode: f64,
/// Inherits from this base Event
pub event: Event,
}

#[derive(Debug)]
pub struct Blob {
/// Byte sequence of this blob
pub data: Vec<u8>,
type_: &'static str,
}

impl Blob {
/// Returns the size of the byte sequence in number of bytes
pub fn size(&self) -> usize {
self.data.len()
}

/// The ASCII-encoded string in lower case representing the media type
pub fn type_(&self) -> &str {
self.type_
}
}

#[cfg(test)]
mod tests {
use crate::context::{BaseAudioContext, OfflineAudioContext};
Expand Down Expand Up @@ -400,7 +423,7 @@ mod tests {
{
let samples = Arc::clone(&samples);
recorder.set_ondataavailable(move |e| {
samples.lock().unwrap().extend_from_slice(&e.blob);
samples.lock().unwrap().extend_from_slice(&e.blob.data);
});
}

Expand Down

0 comments on commit 1b53a06

Please sign in to comment.