add format field to AudioPacketHeader

This commit is contained in:
Hailey Somerville 2023-12-27 13:19:53 +11:00
parent a6b7766d54
commit 49e60e8d37
2 changed files with 20 additions and 6 deletions

View file

@ -47,6 +47,19 @@ pub struct AudioPacketHeader {
// data timestamp - the stream's clock when packet is sent
pub dts: TimestampMicros,
pub format: AudioPacketFormat,
}
/// This, regrettably, has to be a u64 to fill out `AudioPacketHeader` with
/// no hidden padding. TODO this whole protocol tier needs a big rethink
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
#[repr(transparent)]
pub struct AudioPacketFormat(u64);
impl AudioPacketFormat {
pub const F32LE: Self = Self(1);
pub const S16LE: Self = Self(2);
}
pub type AudioPacketBuffer = [f32; SAMPLES_PER_PACKET];

View file

@ -5,7 +5,7 @@ use structopt::StructOpt;
use bark_protocol::time::SampleDuration;
use bark_protocol::packet::{self, Audio, StatsReply, PacketKind};
use bark_protocol::types::{TimestampMicros, AudioPacketHeader, SessionId, ReceiverId, TimePhase};
use bark_protocol::types::{TimestampMicros, AudioPacketHeader, SessionId, ReceiverId, TimePhase, AudioPacketFormat};
use crate::audio::config::{DeviceOpt, DEFAULT_PERIOD, DEFAULT_BUFFER};
use crate::audio::input::Input;
@ -65,6 +65,7 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
seq: 1,
pts: TimestampMicros(0),
dts: TimestampMicros(0),
format: AudioPacketFormat::F32LE,
};
std::thread::spawn({
@ -89,11 +90,11 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
let pts = timestamp.add(delay);
// write packet header
let header = audio.header_mut();
header.sid = audio_header.sid;
header.seq = audio_header.seq;
header.pts = pts.to_micros_lossy();
header.dts = time::now();
*audio.header_mut() = AudioPacketHeader {
pts: pts.to_micros_lossy(),
dts: time::now(),
..audio_header
};
// send it
protocol.broadcast(audio.as_packet()).expect("broadcast");