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::decoder::AudioPacket;
use std::io::Write;
use log::error;
use std::io::{Stdout, Write};
use crate::ipc;
use crate::ipc::packet::IpcPacket;
pub struct StdoutSink {
client: ipc::Client,
output: Option<Box<Stdout>>,
}
impl StdoutSink {
pub fn new(client: ipc::Client) -> Self {
StdoutSink { client }
StdoutSink {
client,
output: None,
}
}
}
impl Sink for StdoutSink {
fn start(&mut self) -> SinkResult<()> {
// TODO: Handle error
self.client.send(IpcPacket::StartPlayback).unwrap();
if let Err(why) = self.client.send(IpcPacket::StartPlayback) {
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(())
}
fn stop(&mut self) -> SinkResult<()> {
// Stop songbird's playback
self.client.send(IpcPacket::StopPlayback).unwrap();
if let Err(why) = self.client.send(IpcPacket::StopPlayback) {
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(())
}
@ -58,7 +78,14 @@ impl Sink for StdoutSink {
impl SinkAsBytes for StdoutSink {
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(())
}

View File

@ -118,7 +118,6 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
Some(user) => user,
None => {
// This shouldn't happen
// TODO: Test if this can no longer happen
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 events;

View File

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