RustAudio / rodio

Rust audio playback library
Apache License 2.0
1.78k stars 232 forks source link

Panic when sink ends #496

Open Cynosure-null opened 1 year ago

Cynosure-null commented 1 year ago

Behavior: When an audio sink ends, the thread it is running off panics and terminates Code Used: ```rust pub fn play(name: &String) { let (_stream, stream_handle) = OutputStream::try_default().unwrap(); let sink = Sink::try_new(&stream_handle).unwrap();

let file = BufReader::new(File::open(name).unwrap());
// Add a dummy source of the sake of the example.
let source = Decoder::new(file).unwrap();
sink.append(source);

// The sound plays in a separate thread. This call will block the current thread until the sink
// has finished playing all its queued sounds.
// The point of the loop is to play till the thread is terminated by the OS, restarting the song if needed.
loop {
    while !sink.empty() {
        sink.play();
    }
    sink.append(Decoder::new(BufReader::new(File::open(name).unwrap())).unwrap());
    println!("here");
}

}

Note that it never reaches here. The same error has occurred while using `sink.sleep_until_end();` instead of the loop.
Error Message: 

thread 'cpal_alsa_out' panicked at 'index out of bounds: the len is 0 but the index is 0', /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/decoder/symphonia.rs:175:22 stack backtrace: 0: rust_begin_unwind at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/panicking.rs:575:5 1: core::panicking::panic_fmt at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/core/src/panicking.rs:64:14 2: core::panicking::panic_bounds_check at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/core/src/panicking.rs:159:5 3: ::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/decoder/symphonia.rs:175:22 4: <rodio::decoder::Decoder as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/decoder/mod.rs:274:47 5: <rodio::source::speed::Speed as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/speed.rs:56:9 6: <rodio::source::pausable::Pausable as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/pausable.rs:85:9 7: <rodio::source::amplify::Amplify as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/amplify.rs:56:9 8: <rodio::source::skippable::Skippable as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/skippable.rs:58:13 9: <rodio::source::stoppable::Stoppable as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/stoppable.rs:57:13 10: <rodio::source::periodic::PeriodicAccess<I,F> as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/periodic.rs:86:9 11: <rodio::source::samples_converter::SamplesConverter<I,D> as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/samples_converter.rs:56:9 12: <rodio::source::done::Done as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/done.rs:53:20 13: <alloc::boxed::Box<I,A> as core::iter::traits::iterator::Iterator>::next at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:1922:9 14: <rodio::queue::SourcesQueueOutput as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/queue.rs:178:35 15: <rodio::source::uniform::Take as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/uniform.rs:159:17 16: <rodio::conversions::sample_rate::SampleRateConverter as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/conversions/sample_rate.rs:131:20 17: <rodio::conversions::channels::ChannelCountConverter as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/conversions/channels.rs:62:13 18: <rodio::conversions::sample::DataConverter<I,O> as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/conversions/sample.rs:38:9 19: <rodio::source::uniform::UniformSourceIterator<I,D> as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/source/uniform.rs:88:30 20: <alloc::boxed::Box<I,A> as core::iter::traits::iterator::Iterator>::next at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:1922:9 21: rodio::dynamic_mixer::DynamicMixer::sum_current_sources at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/dynamic_mixer.rs:170:34 22: <rodio::dynamic_mixer::DynamicMixer as core::iter::traits::iterator::Iterator>::next at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/dynamic_mixer.rs:125:19 23: ::new_output_stream_with_format::{{closure}}::{{closure}} at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/stream.rs:219:44 24: <core::slice::iter::IterMut as core::iter::traits::iterator::Iterator>::for_each at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/core/src/slice/iter/macros.rs:201:21 25: ::new_output_stream_with_format::{{closure}} at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/stream.rs:218:21 26: cpal::traits::DeviceTrait::build_output_stream::{{closure}} at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.15.2/src/traits.rs:169:17 27: cpal::host::alsa::process_output at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.15.2/src/host/alsa/mod.rs:836:9 28: cpal::host::alsa::output_stream_worker at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.15.2/src/host/alsa/mod.rs:671:35 29: cpal::host::alsa::Stream::new_output::{{closure}} at /home/laptop-main/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.15.2/src/host/alsa/mod.rs:965:17 note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace.

schneiderfelipe commented 1 year ago

Same thing here:

❯ cargo run -- take-on-me.mp3
   Compiling sinewaves v0.1.0 (/home/schneider/Dropbox/sinewaves)
    Finished dev [unoptimized + debuginfo] target(s) in 46.50s
     Running `target/debug/sinewaves take-on-me.mp3`
Cli {
    path: "take-on-me.mp3",
}
thread 'cpal_alsa_out' panicked at 'index out of bounds: the len is 0 but the index is 0', /home/schneider/.cargo/registry/src/github.com-1ecc6299db9ec823/rodio-0.17.1/src/decoder/symphonia.rs:175:22
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `-1`,
 right: `8`', /home/schneider/.cargo/registry/src/github.com-1ecc6299db9ec823/cpal-0.15.2/src/host/alsa/mod.rs:145:9
[package]
name = "sinewaves"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.2.7", features = ["derive"] }
rodio = "0.17.1"
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;

use clap::Parser;
use rodio::Decoder;
use rodio::OutputStream;
use rodio::Sink;

#[derive(Debug, Parser)]
struct Cli {
    /// Path to audio file.
    path: PathBuf,
}

fn main() -> Result<(), Box<dyn Error>> {
    let cli = Cli::parse();
    println!("{cli:#?}");

    let file = BufReader::new(File::open(cli.path)?);
    let source = Decoder::new(file)?;

    let (_stream, stream_handle) = OutputStream::try_default()?;
    let sink = Sink::try_new(&stream_handle)?;

    sink.append(source);
    sink.sleep_until_end();

    Ok(())
}
❯ neofetch
            .-/+oossssoo+/-.               schneider@apollo14
        `:+ssssssssssssssssss+:`           ------------------
      -+ssssssssssssssssssyyssss+-         OS: Ubuntu 23.04 x86_64
    .ossssssssssssssssssdMMMNysssso.       Kernel: 6.2.0-20-generic
   /ssssssssssshdmmNNmmyNMMMMhssssss/      Uptime: 19 mins
  +ssssssssshmydMMMMMMMNddddyssssssss+     Packages: 3449 (dpkg), 15 (snap)
 /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Shell: zsh 5.9
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Resolution: 1920x1080
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   DE: GNOME
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM: Mutter
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM Theme: Adwaita
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Theme: Yaru-dark [GTK2/3]
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Icons: Yaru-dark [GTK2/3]
 /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/    Terminal: tmux
  +sssssssssdmydMMMMMMMMddddyssssssss+     CPU: AMD Ryzen 5 1600 (12) @ 3.200GHz
   /ssssssssssshdmNNNNmyNMMMMhssssss/      GPU: AMD ATI Radeon RX 470/480/570/570X/580/580X/5
    .ossssssssssssssssssdMMMNysssso.       Memory: 2865MiB / 15905MiB
      -+sssssssssssssssssyyyssss+-
        `:+ssssssssssssssssss+:`
            .-/+oossssoo+/-.

❯ rustc --version
rustc 1.69.0 (84c898d65 2023-04-16)

❯ cargo --version
cargo 1.69.0 (6e9a83356 2023-04-12)
schneiderfelipe commented 1 year ago

I just found out that this does not happen with version 0.16.x

shreyas-shriyan commented 1 year ago

I got same error on 0.17.1 . but working on 0.16.0 as @schneiderfelipe mentioned

TBS1996 commented 1 year ago

Same error, I think this PR fixes it: https://github.com/RustAudio/rodio/commit/5e034a290838f608fbd026dde2bcd80a6747b787

but It's not yet released