Clean up todos and minor audio backend changes

main
DaXcess 2022-11-08 18:19:07 +01:00
parent 8e19a9c4d4
commit 8f97a13bb6
4 changed files with 37 additions and 12 deletions

View File

@ -1,32 +1,52 @@
use librespot::playback::audio_backend::{Sink, SinkAsBytes, SinkResult}; use librespot::playback::audio_backend::{Sink, SinkAsBytes, SinkError, SinkResult};
use librespot::playback::convert::Converter; use librespot::playback::convert::Converter;
use librespot::playback::decoder::AudioPacket; use librespot::playback::decoder::AudioPacket;
use std::io::Write; use log::error;
use std::io::{Stdout, Write};
use crate::ipc; use crate::ipc;
use crate::ipc::packet::IpcPacket; use crate::ipc::packet::IpcPacket;
pub struct StdoutSink { pub struct StdoutSink {
client: ipc::Client, client: ipc::Client,
output: Option<Box<Stdout>>,
} }
impl StdoutSink { impl StdoutSink {
pub fn new(client: ipc::Client) -> Self { pub fn new(client: ipc::Client) -> Self {
StdoutSink { client } StdoutSink {
client,
output: None,
}
} }
} }
impl Sink for StdoutSink { impl Sink for StdoutSink {
fn start(&mut self) -> SinkResult<()> { fn start(&mut self) -> SinkResult<()> {
// TODO: Handle error if let Err(why) = self.client.send(IpcPacket::StartPlayback) {
self.client.send(IpcPacket::StartPlayback).unwrap(); error!("Failed to send start playback packet: {}", why);
return Err(SinkError::ConnectionRefused(why.to_string()));
}
self.output.get_or_insert(Box::new(std::io::stdout()));
Ok(()) Ok(())
} }
fn stop(&mut self) -> SinkResult<()> { fn stop(&mut self) -> SinkResult<()> {
// Stop songbird's playback if let Err(why) = self.client.send(IpcPacket::StopPlayback) {
self.client.send(IpcPacket::StopPlayback).unwrap(); error!("Failed to send stop playback packet: {}", why);
return Err(SinkError::ConnectionRefused(why.to_string()));
}
self
.output
.take()
.ok_or(SinkError::NotConnected(
"StdoutSink is not connected".to_string(),
))?
.flush()
.map_err(|why| SinkError::OnWrite(why.to_string()))?;
Ok(()) Ok(())
} }
@ -58,7 +78,14 @@ impl Sink for StdoutSink {
impl SinkAsBytes for StdoutSink { impl SinkAsBytes for StdoutSink {
fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> { fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> {
std::io::stdout().write_all(data).unwrap(); self
.output
.as_deref_mut()
.ok_or(SinkError::NotConnected(
"StdoutSink is not connected".to_string(),
))?
.write_all(data)
.map_err(|why| SinkError::OnWrite(why.to_string()))?;
Ok(()) Ok(())
} }

View File

@ -118,7 +118,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
Some(user) => user, Some(user) => user,
None => { None => {
// This shouldn't happen // This shouldn't happen
// TODO: Test if this can no longer happen
error!("Could not find user with id {}", owner); error!("Could not find user with id {}", owner);

View File

@ -1,3 +1,2 @@
// TODO: Check all image urls in embed responses
pub mod commands; pub mod commands;
pub mod events; pub mod events;

View File

@ -246,8 +246,8 @@ impl SpoticordSession {
let mut instance = ipc_instance.clone(); let mut instance = ipc_instance.clone();
let context = ipc_context.clone(); let context = ipc_context.clone();
// TODO: Check if this is actually needed // Fetch track info
// The new backend may have fixed the need for this // This is done in a separate task to avoid blocking the IPC handler
tokio::spawn(async move { tokio::spawn(async move {
if let Err(why) = instance.update_track(&context, &owner_id, track_id).await { if let Err(why) = instance.update_track(&context, &owner_id, track_id).await {
error!("Failed to update track: {:?}", why); error!("Failed to update track: {:?}", why);