move protocol to protocol::types

This commit is contained in:
Hailey Somerville 2023-08-22 15:36:36 +10:00
parent 5cd67b0b51
commit 91936e99f7
7 changed files with 31 additions and 24 deletions

11
src/protocol/mod.rs Normal file
View file

@ -0,0 +1,11 @@
pub mod types;
pub use cpal::{SampleFormat, SampleRate, ChannelCount};
pub const SAMPLE_FORMAT: SampleFormat = SampleFormat::F32;
pub const SAMPLE_RATE: SampleRate = SampleRate(48000);
pub const CHANNELS: ChannelCount = 2;
pub const FRAMES_PER_PACKET: usize = 160;
pub const SAMPLES_PER_PACKET: usize = CHANNELS as usize * FRAMES_PER_PACKET;
pub const MAX_PACKET_SIZE: usize = ::std::mem::size_of::<types::PacketUnion>();

View file

@ -1,15 +1,9 @@
use bytemuck::{Pod, Zeroable};
use cpal::{SampleFormat, SampleRate, ChannelCount};
use nix::time::ClockId;
use nix::sys::time::TimeValLike;
use crate::stats;
pub const SAMPLE_FORMAT: SampleFormat = SampleFormat::F32;
pub const SAMPLE_RATE: SampleRate = SampleRate(48000);
pub const CHANNELS: ChannelCount = 2;
pub const FRAMES_PER_PACKET: usize = 160;
pub const SAMPLES_PER_PACKET: usize = CHANNELS as usize * FRAMES_PER_PACKET;
use crate::protocol;
pub const MAGIC_AUDIO: u32 = 0x00a79ae2;
pub const MAGIC_TIME: u32 = 0x01a79ae2;
@ -132,7 +126,7 @@ bitflags::bitflags! {
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct PacketBuffer(pub [f32; SAMPLES_PER_PACKET]);
pub struct PacketBuffer(pub [f32; protocol::SAMPLES_PER_PACKET]);
/// SAFETY: Pod is impl'd for f32, and [T: Pod; N: usize]
/// but for some reason doesn't like N == SAMPLES_PER_PACKET?
@ -142,7 +136,7 @@ unsafe impl Pod for PacketBuffer {}
/// but for some reason doesn't like N == SAMPLES_PER_PACKET?
unsafe impl Zeroable for PacketBuffer {
fn zeroed() -> Self {
PacketBuffer([0f32; SAMPLES_PER_PACKET])
PacketBuffer([0f32; protocol::SAMPLES_PER_PACKET])
}
}
@ -163,10 +157,8 @@ unsafe impl Zeroable for TimePacketPadding {
// TimePacket::_pad field
static_assertions::assert_eq_size!(AudioPacket, TimePacket);
pub const MAX_PACKET_SIZE: usize = ::std::mem::size_of::<PacketUnion>();
#[repr(C)]
union PacketUnion {
pub union PacketUnion {
_1: AudioPacket,
_2: TimePacket,
_3: StatsRequestPacket,

View file

@ -8,7 +8,8 @@ use cpal::{SampleRate, OutputCallbackInfo};
use cpal::traits::{HostTrait, DeviceTrait};
use structopt::StructOpt;
use crate::protocol::{AudioPacket, self, TimePacket, TimestampMicros, Packet, SessionId, ReceiverId, TimePhase, StatsReplyPacket, StatsReplyFlags};
use crate::protocol;
use crate::protocol::types::{AudioPacket, TimePacket, TimestampMicros, Packet, SessionId, ReceiverId, TimePhase, StatsReplyPacket, StatsReplyFlags};
use crate::resample::Resampler;
use crate::socket::{Socket, SocketOpt};
use crate::stats::node::NodeStats;
@ -577,7 +578,7 @@ pub fn run(opt: ReceiveOpt) -> Result<(), RunError> {
drop(state);
let reply = StatsReplyPacket {
magic: protocol::MAGIC_STATS_REPLY,
magic: protocol::types::MAGIC_STATS_REPLY,
flags: StatsReplyFlags::IS_RECEIVER,
sid: sid.unwrap_or(SessionId::zeroed()),
receiver: stats,

View file

@ -13,7 +13,8 @@ use bytemuck::Zeroable;
use structopt::StructOpt;
use termcolor::BufferedStandardStream;
use crate::protocol::{StatsRequestPacket, self, StatsReplyPacket, StatsReplyFlags};
use crate::protocol;
use crate::protocol::types::{StatsRequestPacket, StatsReplyPacket, StatsReplyFlags};
use crate::socket::{Socket, SocketOpt};
use crate::RunError;
@ -37,7 +38,7 @@ pub fn run(opt: StatsOpt) -> Result<(), RunError> {
move || {
loop {
let packet = StatsRequestPacket {
magic: protocol::MAGIC_STATS_REQ,
magic: protocol::types::MAGIC_STATS_REQ,
flags: 0,
};
@ -59,7 +60,7 @@ pub fn run(opt: StatsOpt) -> Result<(), RunError> {
continue;
}
if reply.magic != protocol::MAGIC_STATS_REPLY {
if reply.magic != protocol::types::MAGIC_STATS_REPLY {
continue;
}

View file

@ -2,9 +2,9 @@ use std::net::SocketAddrV4;
use termcolor::{WriteColor, ColorSpec, Color};
use crate::protocol::types::{StatsReplyPacket, StatsReplyFlags};
use crate::stats::receiver::{ReceiverStats, StreamStatus};
use crate::stats::node::NodeStats;
use crate::protocol::{StatsReplyPacket, StatsReplyFlags};
#[derive(Default)]
pub struct Padding {

View file

@ -6,7 +6,8 @@ use cpal::traits::{HostTrait, DeviceTrait, StreamTrait};
use cpal::InputCallbackInfo;
use structopt::StructOpt;
use crate::protocol::{self, Packet, TimestampMicros, AudioPacket, PacketBuffer, TimePacket, MAX_PACKET_SIZE, TimePacketPadding, SessionId, ReceiverId, TimePhase, StatsReplyPacket, StatsReplyFlags};
use crate::protocol;
use crate::protocol::types::{Packet, TimestampMicros, AudioPacket, PacketBuffer, TimePacket, TimePacketPadding, SessionId, ReceiverId, TimePhase, StatsReplyPacket, StatsReplyFlags};
use crate::socket::{Socket, SocketOpt};
use crate::stats::node::NodeStats;
use crate::stats::receiver::ReceiverStats;
@ -57,7 +58,7 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
let node = NodeStats::get();
let mut packet = AudioPacket {
magic: protocol::MAGIC_AUDIO,
magic: protocol::types::MAGIC_AUDIO,
flags: 0,
sid,
seq: 1,
@ -140,7 +141,7 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
let now = TimestampMicros::now();
let packet = TimePacket {
magic: protocol::MAGIC_TIME,
magic: protocol::types::MAGIC_TIME,
flags: 0,
sid,
rid: ReceiverId::broadcast(),
@ -164,7 +165,7 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
crate::thread::set_realtime_priority();
loop {
let mut packet_raw = [0u8; MAX_PACKET_SIZE];
let mut packet_raw = [0u8; protocol::MAX_PACKET_SIZE];
let (nbytes, addr) = socket.recv_from(&mut packet_raw)
.expect("socket.recv_from");
@ -200,7 +201,7 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
}
Some(Packet::StatsRequest(_)) => {
let reply = StatsReplyPacket {
magic: protocol::MAGIC_STATS_REPLY,
magic: protocol::types::MAGIC_STATS_REPLY,
flags: StatsReplyFlags::IS_STREAM,
sid: sid,
receiver: ReceiverStats::zeroed(),

View file

@ -1,4 +1,5 @@
use crate::protocol::{self, TimestampMicros, TimePacket};
use crate::protocol;
use crate::protocol::types::{TimestampMicros, TimePacket};
/// A timestamp with implicit denominator SAMPLE_RATE
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]