Add audio playback again

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau committed 2021-07-09 16:13:56 +04:00
1 parent fc78781ec2
commit 36c84d517f
3 files changed
+115 -11

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
use std::error::Error;
use std::result::Result;
use std::thread;
use qemu_display_listener::Audio;
#[derive(Debug, Default)]
pub struct Handler {
thread: Option<thread::JoinHandle<()>>,
}
impl Handler {
pub async fn new(conn: &zbus::azync::Connection) -> Result<Self, Box<dyn Error>> {
if !Audio::available(conn).await {
log::debug!("No qemu audio provided on the bus");
return Ok(Self::default());
}
let audio = Audio::new(conn).await?;
let rx = audio.listen_out().await?;
let mut gst = rdw::GstAudio::new()?;
let thread = thread::spawn(move || loop {
match rx.recv() {
Ok(event) => {
use qemu_display_listener::AudioOutEvent::*;
match event {
Init { id, info } => {
if let Err(e) = gst.init_out(id, &info.gst_caps()) {
log::warn!("Failed to initialize audio stream: {}", e);
}
}
Fini { id } => {
gst.fini_out(id);
}
SetEnabled { id, enabled } => {
if let Err(e) = gst.set_enabled_out(id, enabled) {
log::warn!("Failed to set enabled audio stream: {}", e);
}
}
SetVolume { id, volume } => {
if let Err(e) = gst.set_volume_out(
id,
volume.mute,
volume.volume.first().map(|v| *v as f64 / 255f64),
) {
log::warn!("Failed to set volume: {}", e);
}
}
Write { id, data } => {
if let Err(e) = gst.write_out(id, data) {
log::warn!("Failed to output stream: {}", e);
}
}
}
}
Err(e) => log::warn!("Audio thread error: {}", e),
}
});
Ok(Self {
thread: Some(thread),
})
}
}
+10
View File
@@ -1,9 +1,11 @@
use gio::ApplicationFlags;
use glib::{clone, MainContext};
use gtk::{gio, glib, prelude::*};
use once_cell::sync::OnceCell;
use qemu_display_listener::Console;
use zbus::Connection;
mod audio;
mod display_qemu;
fn main() {
@@ -15,6 +17,8 @@ fn main() {
.expect("Failed to connect to DBus")
.into();
let audio = std::sync::Arc::new(OnceCell::new());
app.connect_activate(move |app| {
let window = gtk::ApplicationWindow::new(app);
@@ -22,11 +26,17 @@ fn main() {
window.set_default_size(1024, 768);
let conn = conn.clone();
let audio_clone = audio.clone();
MainContext::default().spawn_local(clone!(@strong window => async move {
let console = Console::new(&conn, 0).await.expect("Failed to get the QEMU console");
let display = display_qemu::DisplayQemu::new(console);
window.set_child(Some(&display));
match audio::Handler::new(&conn).await {
Ok(handler) => audio_clone.set(handler).unwrap(),
Err(e) => log::warn!("Failed to setup audio: {}", e),
}
window.show();
}));
});