2023-10-07 11:45:05 +00:00
|
|
|
use std::io::{BufReader, Read, Seek};
|
2023-10-04 14:49:35 +00:00
|
|
|
use std::path::Path;
|
2023-10-07 11:45:05 +00:00
|
|
|
use std::sync::Once;
|
2023-10-08 12:05:34 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2023-10-04 14:49:35 +00:00
|
|
|
|
2023-10-07 11:45:05 +00:00
|
|
|
use rodio::{Decoder, OutputStream, OutputStreamHandle, Sink, Source};
|
2023-10-04 14:49:35 +00:00
|
|
|
|
2023-10-07 11:45:05 +00:00
|
|
|
static mut STREAM: Option<OutputStream> = None;
|
|
|
|
static mut STREAM_HANDLE: Option<OutputStreamHandle> = None;
|
|
|
|
static INIT: Once = Once::new();
|
2023-10-04 14:49:35 +00:00
|
|
|
|
2023-10-07 11:45:05 +00:00
|
|
|
fn global_stream_handle() -> &'static OutputStreamHandle {
|
|
|
|
// mutable global access is guarded by Once therefore
|
|
|
|
// can only happen Once and will not race
|
|
|
|
unsafe {
|
|
|
|
INIT.call_once(|| {
|
|
|
|
let (stream, handle) = rodio::OutputStream::try_default().unwrap();
|
|
|
|
STREAM = Some(stream);
|
|
|
|
STREAM_HANDLE = Some(handle);
|
|
|
|
});
|
|
|
|
STREAM_HANDLE.as_ref().unwrap()
|
|
|
|
}
|
2023-10-04 14:49:35 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 11:45:05 +00:00
|
|
|
fn sink_and_decoder(format: &str) -> (Sink, Decoder<impl Read + Seek>) {
|
|
|
|
let sink = rodio::Sink::try_new(global_stream_handle()).unwrap();
|
|
|
|
let asset = Path::new("assets/music").with_extension(format);
|
|
|
|
let file = std::fs::File::open(asset).unwrap();
|
|
|
|
let decoder = rodio::Decoder::new(BufReader::new(file)).unwrap();
|
|
|
|
(sink, decoder)
|
|
|
|
}
|
|
|
|
|
2023-10-10 00:25:34 +00:00
|
|
|
// run the following to get the other configuration:
|
|
|
|
// cargo test --no-default-features
|
|
|
|
// --features symphonia-wav --features symphonia-vorbis
|
|
|
|
// --features symphonia-flac --features symphonia-isomp4 --features minimp3
|
2023-10-07 11:45:05 +00:00
|
|
|
fn format_decoder_info() -> &'static [(&'static str, bool, &'static str)] {
|
|
|
|
&[
|
2023-10-05 11:28:59 +00:00
|
|
|
#[cfg(feature = "minimp3")]
|
|
|
|
("mp3", true, "minimp3"),
|
|
|
|
#[cfg(feature = "symphonia-mp3")]
|
|
|
|
("mp3", true, "symphonia"),
|
|
|
|
#[cfg(feature = "hound")]
|
2023-10-06 11:30:32 +00:00
|
|
|
("wav", true, "hound"),
|
2023-10-05 11:28:59 +00:00
|
|
|
#[cfg(feature = "symphonia-wav")]
|
|
|
|
("wav", true, "symphonia"),
|
|
|
|
#[cfg(feature = "lewton")]
|
2023-10-05 11:44:10 +00:00
|
|
|
("ogg", true, "lewton"),
|
2023-10-10 00:25:34 +00:00
|
|
|
// note: disabled, symphonia returns error unsupported format
|
|
|
|
// #[cfg(feature = "symphonia-vorbis")]
|
|
|
|
// ("ogg", true, "symphonia"),
|
2023-10-05 11:28:59 +00:00
|
|
|
#[cfg(feature = "claxon")]
|
|
|
|
("flac", false, "claxon"),
|
|
|
|
#[cfg(feature = "symphonia-flac")]
|
|
|
|
("flac", true, "symphonia"),
|
2023-10-10 00:25:34 +00:00
|
|
|
// note: disabled, symphonia returns error unsupported format
|
|
|
|
// #[cfg(feature = "symphonia-isomp4")]
|
|
|
|
// ("m4a", true, "symphonia"),
|
2023-10-07 11:45:05 +00:00
|
|
|
]
|
|
|
|
}
|
2023-10-05 11:28:59 +00:00
|
|
|
|
2023-10-07 11:45:05 +00:00
|
|
|
#[test]
|
|
|
|
fn seek_returns_err_if_unsupported() {
|
|
|
|
for (format, supported, decoder) in format_decoder_info().iter().cloned() {
|
|
|
|
println!("trying: {format},\t\tby: {decoder},\t\tshould support seek: {supported}");
|
|
|
|
let (sink, decoder) = sink_and_decoder(format);
|
|
|
|
sink.append(decoder);
|
|
|
|
let res = sink.try_seek(Duration::from_secs(2));
|
2023-10-04 14:49:35 +00:00
|
|
|
assert_eq!(res.is_ok(), supported);
|
|
|
|
}
|
|
|
|
}
|
2023-10-07 11:45:05 +00:00
|
|
|
|
2023-10-10 00:25:34 +00:00
|
|
|
// #[ignore]
|
|
|
|
#[test] // in the future use PR #510 (playback position) to speed this up
|
|
|
|
fn seek_beyond_end_saturates() {
|
2023-10-08 12:05:34 +00:00
|
|
|
for (format, _, decoder_name) in format_decoder_info()
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.filter(|(_, supported, _)| *supported)
|
|
|
|
{
|
2023-10-07 11:45:05 +00:00
|
|
|
let (sink, decoder) = sink_and_decoder(format);
|
|
|
|
sink.append(decoder);
|
2023-10-10 00:25:34 +00:00
|
|
|
|
|
|
|
println!("seeking beyond end for: {format}\t decoded by: {decoder_name}");
|
|
|
|
let res = sink.try_seek(Duration::from_secs(999));
|
|
|
|
assert!(res.is_ok());
|
|
|
|
|
|
|
|
let now = Instant::now();
|
|
|
|
sink.sleep_until_end();
|
|
|
|
let elapsed = now.elapsed();
|
|
|
|
assert!(elapsed.as_secs() < 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn total_duration(format: &'static str) -> Duration {
|
|
|
|
let (sink, decoder) = sink_and_decoder(format);
|
|
|
|
match decoder.total_duration() {
|
|
|
|
Some(d) => d,
|
|
|
|
None => {
|
|
|
|
let now = Instant::now();
|
|
|
|
sink.append(decoder);
|
|
|
|
sink.sleep_until_end();
|
|
|
|
now.elapsed()
|
|
|
|
}
|
2023-10-07 11:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-08 12:05:34 +00:00
|
|
|
|
|
|
|
#[ignore]
|
|
|
|
#[test] // in the future use PR #510 (playback position) to speed this up
|
|
|
|
fn seek_results_in_correct_remaining_playtime() {
|
|
|
|
for (format, _, decoder_name) in format_decoder_info()
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.filter(|(_, supported, _)| *supported)
|
|
|
|
{
|
2023-10-10 00:25:34 +00:00
|
|
|
println!("checking seek duration for: {format}\t decoded by: {decoder_name}");
|
2023-10-08 12:05:34 +00:00
|
|
|
|
|
|
|
let (sink, decoder) = sink_and_decoder(format);
|
|
|
|
sink.append(decoder);
|
|
|
|
|
|
|
|
const SEEK_BEFORE_END: Duration = Duration::from_secs(5);
|
2023-10-10 00:25:34 +00:00
|
|
|
sink.try_seek(total_duration(format) - SEEK_BEFORE_END)
|
|
|
|
.unwrap();
|
2023-10-08 12:05:34 +00:00
|
|
|
|
|
|
|
let now = Instant::now();
|
|
|
|
sink.sleep_until_end();
|
|
|
|
let elapsed = now.elapsed();
|
|
|
|
let expected = SEEK_BEFORE_END;
|
|
|
|
|
|
|
|
if elapsed.as_millis().abs_diff(expected.as_millis()) > 250 {
|
2023-10-10 00:25:34 +00:00
|
|
|
panic!(
|
|
|
|
"Seek did not result in expected leftover playtime
|
2023-10-08 12:05:34 +00:00
|
|
|
leftover time: {elapsed:?}
|
2023-10-10 00:25:34 +00:00
|
|
|
expected time left in source: {SEEK_BEFORE_END:?}"
|
|
|
|
);
|
2023-10-08 12:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|