mirror of
https://github.com/haileys/bark
synced 2024-11-10 05:54:15 +00:00
make proper SessionId type
This commit is contained in:
parent
a483f0696c
commit
d76192f774
3 changed files with 24 additions and 11 deletions
|
@ -29,7 +29,7 @@ pub struct AudioPacket {
|
|||
|
||||
// stream id - set to the start time of a stream, used by receivers to
|
||||
// detect new stream starts, used by senders to detect stream takeovers
|
||||
pub sid: TimestampMicros,
|
||||
pub sid: SessionId,
|
||||
|
||||
// packet sequence number - monotonic + gapless, arbitrary start point
|
||||
pub seq: u64,
|
||||
|
@ -50,7 +50,7 @@ pub struct AudioPacket {
|
|||
pub struct TimePacket {
|
||||
pub magic: u32,
|
||||
pub flags: u32,
|
||||
pub sid: TimestampMicros,
|
||||
pub sid: SessionId,
|
||||
|
||||
pub stream_1: TimestampMicros,
|
||||
pub receive_2: TimestampMicros,
|
||||
|
@ -139,3 +139,16 @@ impl TimestampMicros {
|
|||
TimestampMicros(micros)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Zeroable, Pod, PartialEq, PartialOrd)]
|
||||
#[repr(transparent)]
|
||||
pub struct SessionId(i64);
|
||||
|
||||
impl SessionId {
|
||||
pub fn generate() -> Self {
|
||||
let timespec = nix::time::clock_gettime(ClockId::CLOCK_REALTIME)
|
||||
.expect("clock_gettime(CLOCK_REALTIME)");
|
||||
|
||||
SessionId(timespec.num_microseconds())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ use cpal::{SampleRate, OutputCallbackInfo};
|
|||
use cpal::traits::{HostTrait, DeviceTrait};
|
||||
use structopt::StructOpt;
|
||||
|
||||
use crate::protocol::{AudioPacket, self, TimePacket, TimestampMicros, Packet};
|
||||
use crate::protocol::{AudioPacket, self, TimePacket, TimestampMicros, Packet, SessionId};
|
||||
use crate::resample::Resampler;
|
||||
use crate::status::{Status, StreamStatus};
|
||||
use crate::time::{Timestamp, SampleDuration, TimestampDelta, ClockDelta};
|
||||
|
@ -38,7 +38,7 @@ impl QueueEntry {
|
|||
}
|
||||
|
||||
struct Stream {
|
||||
sid: TimestampMicros,
|
||||
sid: SessionId,
|
||||
start_seq: u64,
|
||||
sync: bool,
|
||||
resampler: Resampler,
|
||||
|
@ -97,7 +97,7 @@ impl Receiver {
|
|||
return;
|
||||
};
|
||||
|
||||
if stream.sid.0 != packet.sid.0 {
|
||||
if stream.sid != packet.sid {
|
||||
// not relevant to our stream, ignore
|
||||
return;
|
||||
}
|
||||
|
@ -127,12 +127,12 @@ impl Receiver {
|
|||
|
||||
fn prepare_stream(&mut self, packet: &AudioPacket) -> bool {
|
||||
if let Some(stream) = self.stream.as_mut() {
|
||||
if packet.sid.0 < stream.sid.0 {
|
||||
if packet.sid < stream.sid {
|
||||
// packet belongs to a previous stream, ignore
|
||||
return false;
|
||||
}
|
||||
|
||||
if packet.sid.0 > stream.sid.0 {
|
||||
if packet.sid > stream.sid {
|
||||
// new stream is taking over! switch over to it
|
||||
println!("\nnew stream beginning");
|
||||
self.stream = Some(Stream::start_from_packet(packet));
|
||||
|
|
|
@ -7,7 +7,7 @@ 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};
|
||||
use crate::protocol::{self, Packet, TimestampMicros, AudioPacket, PacketBuffer, TimePacket, MAX_PACKET_SIZE, TimePacketPadding, SessionId};
|
||||
use crate::time::{SampleDuration, Timestamp};
|
||||
use crate::util;
|
||||
use crate::RunError;
|
||||
|
@ -52,7 +52,7 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
|
|||
let delay = Duration::from_millis(opt.delay_ms);
|
||||
let delay = SampleDuration::from_std_duration_lossy(delay);
|
||||
|
||||
let sid = TimestampMicros::now();
|
||||
let sid = SessionId::generate();
|
||||
|
||||
let mut packet = AudioPacket {
|
||||
magic: protocol::MAGIC_AUDIO,
|
||||
|
@ -164,14 +164,14 @@ pub fn run(opt: StreamOpt) -> Result<(), RunError> {
|
|||
Some(Packet::Audio(packet)) => {
|
||||
// we should only ever receive an audio packet if another
|
||||
// stream is present. check if it should take over
|
||||
if packet.sid.0 > sid.0 {
|
||||
if packet.sid > sid {
|
||||
eprintln!("Another stream has taken over from {addr}, exiting");
|
||||
break;
|
||||
}
|
||||
}
|
||||
Some(Packet::Time(packet)) => {
|
||||
// only handle packet if it belongs to our stream:
|
||||
if packet.sid.0 == sid.0 {
|
||||
if packet.sid == sid {
|
||||
packet.stream_3 = TimestampMicros::now();
|
||||
socket.send_to(bytemuck::bytes_of(packet), addr)
|
||||
.expect("socket.send responding to time packet");
|
||||
|
|
Loading…
Reference in a new issue