From 3804653512e08e0061f77c42714d178f40f8ed15 Mon Sep 17 00:00:00 2001 From: Ivan Bushchik Date: Tue, 27 Aug 2024 12:38:50 +0300 Subject: [PATCH] Encode PCM the same way as any other encoder Signed-off-by: Ivan Bushchik --- lonelyradio_types/src/lib.rs | 2 +- src/main.rs | 40 ++++++------------------------------ 2 files changed, 7 insertions(+), 35 deletions(-) diff --git a/lonelyradio_types/src/lib.rs b/lonelyradio_types/src/lib.rs index 8e40613..d226476 100644 --- a/lonelyradio_types/src/lib.rs +++ b/lonelyradio_types/src/lib.rs @@ -103,7 +103,7 @@ pub enum Encoder { #[derive(Deserialize, Serialize, Clone, Debug, PartialEq)] pub struct FragmentMetadata { - // In bytes or samples, depends on encoder: Pcm* - samples, any compressed - bytes + // In bytes #[serde(rename = "le")] pub length: u64, diff --git a/src/main.rs b/src/main.rs index f383b90..a1c7970 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,46 +102,18 @@ async fn stream_track( let mut _samples = _samples.concat(); match md.encoder { - Encoder::Pcm16 => { - let _md = PlayMessage::F(FragmentMetadata { - length: _samples.len() as u64 * 2, - magic_cookie: None, - }); - if s.write_all(rmp_serde::to_vec(&_md).unwrap().as_slice()).is_err() { - return true; - } - if s.write_all( - &encode(Encoder::Pcm16, _samples, md.sample_rate, md.channels).unwrap().0, - ) - .is_err() - { - return true; - } - } - Encoder::PcmFloat => { - let _md = PlayMessage::F(FragmentMetadata { - length: _samples.len() as u64 * 4, - magic_cookie: None, - }); - if s.write_all(rmp_serde::to_vec(&_md).unwrap().as_slice()).is_err() { - return true; - } - if s.write_all( - &encode(Encoder::PcmFloat, _samples, md.sample_rate, md.channels).unwrap().0, - ) - .is_err() - { - return true; - } - } - Encoder::Flac | Encoder::Alac | Encoder::Vorbis => { + Encoder::Pcm16 + | Encoder::PcmFloat + | Encoder::Flac + | Encoder::Alac + | Encoder::Vorbis => { let (encoded, magic_cookie) = encode(md.encoder, _samples, md.sample_rate, md.channels).unwrap(); let _md = PlayMessage::F(FragmentMetadata { length: encoded.as_slice().len() as u64, magic_cookie, }); - if s.write_all(rmp_serde::to_vec(&_md).unwrap().as_slice()).is_err() { + if s.write_all(rmp_serde::to_vec_named(&_md).unwrap().as_slice()).is_err() { return true; } if s.write_all(encoded.as_slice()).is_err() {