diff --git a/CHANGELOG.md b/CHANGELOG.md index ceb63541..c5757aaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,51 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) since v0.2.0. ## [Unreleased] + +### Changed +- [main] Enforce reasonable ranges for option values (breaking). +- [main] Don't evaluate options that would otherwise have no effect. +- [playback] `alsa`: Improve `--device ?` functionality for the alsa backend. +- [contrib] Hardened security of the systemd service units +- [main] Verbose logging mode (`-v`, `--verbose`) now logs all parsed environment variables and command line arguments (credentials are redacted). + +### Added +- [cache] Add `disable-credential-cache` flag (breaking). +- [main] Use different option descriptions and error messages based on what backends are enabled at build time. +- [main] Add a `-q`, `--quiet` option that changes the logging level to warn. +- [main] Add a short name for every flag and option. +- [main] Add the ability to parse environment variables. + +### Fixed +- [main] Prevent hang when discovery is disabled and there are no credentials or when bad credentials are given. +- [main] Don't panic when parsing options. Instead list valid values and exit. + +### Removed +- [playback] `alsamixer`: previously deprecated option `mixer-card` has been removed. +- [playback] `alsamixer`: previously deprecated option `mixer-name` has been removed. +- [playback] `alsamixer`: previously deprecated option `mixer-index` has been removed. + +## [0.3.1] - 2021-10-24 + +### Changed +- Include build profile in the displayed version information +- [playback] Improve dithering CPU usage by about 33% + +### Fixed +- [connect] Partly fix behavior after last track of an album/playlist + +## [0.3.0] - 2021-10-13 + ### Added - [discovery] The crate `librespot-discovery` for discovery in LAN was created. Its functionality was previously part of `librespot-connect`. - [playback] Add support for dithering with `--dither` for lower requantization error (breaking) - [playback] Add `--volume-range` option to set dB range and control `log` and `cubic` volume control curves - [playback] `alsamixer`: support for querying dB range from Alsa softvol - [playback] Add `--format F64` (supported by Alsa and GStreamer only) +- [playback] Add `--normalisation-gain-type auto` that switches between album and track automatically ### Changed -- [audio, playback] Moved `VorbisDecoder`, `VorbisError`, `AudioPacket`, `PassthroughDecoder`, `PassthroughError`, `AudioError`, `AudioDecoder` and the `convert` module from `librespot-audio` to `librespot-playback`. The underlying crates `vorbis`, `librespot-tremor`, `lewton` and `ogg` should be used directly. (breaking) +- [audio, playback] Moved `VorbisDecoder`, `VorbisError`, `AudioPacket`, `PassthroughDecoder`, `PassthroughError`, `DecoderError`, `AudioDecoder` and the `convert` module from `librespot-audio` to `librespot-playback`. The underlying crates `vorbis`, `librespot-tremor`, `lewton` and `ogg` should be used directly. (breaking) - [audio, playback] Use `Duration` for time constants and functions (breaking) - [connect, playback] Moved volume controls from `librespot-connect` to `librespot-playback` crate - [connect] Synchronize player volume with mixer volume on playback @@ -22,20 +58,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [playback] Make cubic volume control available to all mixers with `--volume-ctrl cubic` - [playback] Normalize volumes to `[0.0..1.0]` instead of `[0..65535]` for greater precision and performance (breaking) - [playback] `alsamixer`: complete rewrite (breaking) -- [playback] `alsamixer`: query card dB range for the `log` volume control unless specified otherwise +- [playback] `alsamixer`: query card dB range for the volume control unless specified otherwise - [playback] `alsamixer`: use `--device` name for `--mixer-card` unless specified otherwise - [playback] `player`: consider errors in `sink.start`, `sink.stop` and `sink.write` fatal and `exit(1)` (breaking) +- [playback] `player`: make `convert` and `decoder` public so you can implement your own `Sink` +- [playback] `player`: update default normalisation threshold to -2 dBFS +- [playback] `player`: default normalisation type is now `auto` ### Deprecated - [connect] The `discovery` module was deprecated in favor of the `librespot-discovery` crate +- [playback] `alsamixer`: renamed `mixer-card` to `alsa-mixer-device` +- [playback] `alsamixer`: renamed `mixer-name` to `alsa-mixer-control` +- [playback] `alsamixer`: renamed `mixer-index` to `alsa-mixer-index` ### Removed - [connect] Removed no-op mixer started/stopped logic (breaking) - [playback] Removed `with-vorbis` and `with-tremor` features -- [playback] `alsamixer`: removed `--mixer-linear-volume` option; use `--volume-ctrl linear` instead +- [playback] `alsamixer`: removed `--mixer-linear-volume` option, now that `--volume-ctrl {linear|log}` work as expected on Alsa ### Fixed - [connect] Fix step size on volume up/down events +- [connect] Fix looping back to the first track after the last track of an album or playlist - [playback] Incorrect `PlayerConfig::default().normalisation_threshold` caused distortion when using dynamic volume normalisation downstream - [playback] Fix `log` and `cubic` volume controls to be mute at zero volume - [playback] Fix `S24_3` format on big-endian systems @@ -43,7 +86,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [playback] `alsamixer`: make `--volume-ctrl {linear|log}` work as expected - [playback] `alsa`, `gstreamer`, `pulseaudio`: always output in native endianness - [playback] `alsa`: revert buffer size to ~500 ms -- [playback] `alsa`, `pipe`: better error handling +- [playback] `alsa`, `pipe`, `pulseaudio`: better error handling +- [metadata] Skip tracks whose Spotify ID's can't be found (e.g. local files, which aren't supported) ## [0.2.0] - 2021-05-04 @@ -59,7 +103,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.1.0] - 2019-11-06 -[unreleased]: https://github.com/librespot-org/librespot/compare/v0.2.0..HEAD +[unreleased]: https://github.com/librespot-org/librespot/compare/v0.3.1..HEAD +[0.3.1]: https://github.com/librespot-org/librespot/compare/v0.3.0..v0.3.1 +[0.3.0]: https://github.com/librespot-org/librespot/compare/v0.2.0..v0.3.0 [0.2.0]: https://github.com/librespot-org/librespot/compare/v0.1.6..v0.2.0 [0.1.6]: https://github.com/librespot-org/librespot/compare/v0.1.5..v0.1.6 [0.1.5]: https://github.com/librespot-org/librespot/compare/v0.1.3..v0.1.5 diff --git a/COMPILING.md b/COMPILING.md index 8748cd0c..39ae20cc 100644 --- a/COMPILING.md +++ b/COMPILING.md @@ -5,20 +5,15 @@ In order to compile librespot, you will first need to set up a suitable Rust build environment, with the necessary dependencies installed. You will need to have a C compiler, Rust, and the development libraries for the audio backend(s) you want installed. These instructions will walk you through setting up a simple build environment. ### Install Rust -The easiest, and recommended way to get Rust is to use [rustup](https://rustup.rs). On Unix/MacOS You can install `rustup` with this command: - -```bash -curl https://sh.rustup.rs -sSf | sh -``` - -Follow any prompts it gives you to install Rust. Once that’s done, Rust's standard tools should be setup and ready to use. +The easiest, and recommended way to get Rust is to use [rustup](https://rustup.rs). Once that’s installed, Rust's standard tools should be set up and ready to use. *Note: The current minimum required Rust version at the time of writing is 1.48, you can find the current minimum version specified in the `.github/workflow/test.yml` file.* #### Additional Rust tools - `rustfmt` -To ensure a consistent codebase, we utilise [`rustfmt`](https://github.com/rust-lang/rustfmt), which is installed by default with `rustup` these days, else it can be installed manually with: +To ensure a consistent codebase, we utilise [`rustfmt`](https://github.com/rust-lang/rustfmt) and [`clippy`](https://github.com/rust-lang/rust-clippy), which are installed by default with `rustup` these days, else they can be installed manually with: ```bash rustup component add rustfmt +rustup component add clippy ``` Using `rustfmt` is not optional, as our CI checks against this repo's rules. @@ -43,12 +38,13 @@ Depending on the chosen backend, specific development libraries are required. |--------------------|------------------------------|-----------------------------------|-------------| |Rodio (default) | `libasound2-dev` | `alsa-lib-devel` | | |ALSA | `libasound2-dev, pkg-config` | `alsa-lib-devel` | | +|GStreamer | `gstreamer1.0-plugins-base libgstreamer-plugins-base1.0-dev gstreamer1.0-plugins-good libgstreamer-plugins-good1.0-dev` | `gstreamer1 gstreamer1-devel gstreamer1-plugins-base-devel gstreamer1-plugins-good` | `gstreamer gst-devtools gst-plugins-base gst-plugins-good` | |PortAudio | `portaudio19-dev` | `portaudio-devel` | `portaudio` | |PulseAudio | `libpulse-dev` | `pulseaudio-libs-devel` | | -|JACK | `libjack-dev` | `jack-audio-connection-kit-devel` | | -|JACK over Rodio | `libjack-dev` | `jack-audio-connection-kit-devel` | - | -|SDL | `libsdl2-dev` | `SDL2-devel` | | -|Pipe | - | - | - | +|JACK | `libjack-dev` | `jack-audio-connection-kit-devel` | `jack` | +|JACK over Rodio | `libjack-dev` | `jack-audio-connection-kit-devel` | `jack` | +|SDL | `libsdl2-dev` | `SDL2-devel` | `sdl2` | +|Pipe & subprocess | - | - | - | ###### For example, to build an ALSA based backend, you would need to run the following to install the required dependencies: @@ -68,7 +64,6 @@ The recommended method is to first fork the repo, so that you have a copy that y ```bash git clone git@github.com:YOURUSERNAME/librespot.git -cd librespot ``` ## Compiling & Running @@ -109,7 +104,9 @@ cargo build --no-default-features --features "alsa-backend" Assuming you just compiled a ```debug``` build, you can run librespot with the following command: ```bash -./target/debug/librespot -n Librespot +./target/debug/librespot ``` There are various runtime options, documented in the wiki, and visible by running librespot with the ```-h``` argument. + +Note that debug builds may cause buffer underruns and choppy audio when dithering is enabled (which it is by default). You can disable dithering with ```--dither none```. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3395529c..1ba24393 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,10 +8,12 @@ If you have encountered a bug, please report it, as we rely on user reports to f Please also make sure that your issues are helpful. To ensure that your issue is helpful, please read over this brief checklist to avoid the more common pitfalls: - - Please take a moment to search/read previous similar issues to ensure you aren’t posting a duplicate. Duplicates will be closed immediately. - - Please include a clear description of what the issue is. Issues with descriptions such as ‘It hangs after 40 minutes’ will be closed immediately. - - Please include, where possible, steps to reproduce the bug, along with any other material that is related to the bug. For example, if librespot consistently crashes when you try to play a song, please include the Spotify URI of that song. This can be immensely helpful in quickly pinpointing and resolving issues. - - Lastly, and perhaps most importantly, please include a backtrace where possible. Recent versions of librespot should produce these automatically when it crashes, and print them to the console, but in some cases, you may need to run ‘export RUST_BACKTRACE=full’ before running librespot to enable backtraces. +- Please take a moment to search/read previous similar issues to ensure you aren’t posting a duplicate. Duplicates will be closed immediately. +- Please include a clear description of what the issue is. Issues with descriptions such as ‘It hangs after 40 minutes’ will be closed immediately. +- Please include, where possible, steps to reproduce the bug, along with any other material that is related to the bug. For example, if librespot consistently crashes when you try to play a song, please include the Spotify URI of that song. This can be immensely helpful in quickly pinpointing and resolving issues. +- Please be alert and respond to questions asked by any project members. Stale issues will be closed. +- When your issue concerns audio playback, please first make sure that your audio system is set up correctly and can play audio from other applications. This project aims to provide correct audio backends, not to provide Linux support to end users. +- Lastly, and perhaps most importantly, please include a backtrace where possible. Recent versions of librespot should produce these automatically when it crashes, and print them to the console, but in some cases, you may need to run ‘export RUST_BACKTRACE=full’ before running librespot to enable backtraces. ## Contributing Code @@ -29,20 +31,25 @@ In order to prepare for a PR, you will need to do a couple of things first: Make any changes that you are going to make to the code, but do not commit yet. -Unless your changes are negligible, please add an entry in the "Unreleased" section of `CHANGELOG.md`. Refer to [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) for instructions on how this entry should look like. +Unless your changes are negligible, please add an entry in the "Unreleased" section of `CHANGELOG.md`. Refer to [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) for instructions on how this entry should look like. If your changes break the API such that downstream packages that depend on librespot need to update their source to still compile, you should mark your changes as `(breaking)`. Make sure that the code is correctly formatted by running: ```bash -cargo +stable fmt --all +cargo fmt --all ``` -This command runs the previously installed stable version of ```rustfmt```, a code formatting tool that will automatically correct any formatting that you have used that does not conform with the librespot code style. Once that command has run, you will need to rebuild the project: +This command runs ```rustfmt```, a code formatting tool that will automatically correct any formatting that you have used that does not conform with the librespot code style. Once that command has run, you will need to rebuild the project: ```bash cargo build ``` -Once it has built, and you have confirmed there are no warnings or errors, you should commit your changes. +Once it has built, check for common code mistakes by running: +```bash +cargo clippy +``` + +Once you have confirmed there are no warnings or errors, you should commit your changes. ```bash git commit -a -m "My fancy fix" diff --git a/Cargo.lock b/Cargo.lock index 1b537099..daf7ce62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -78,9 +78,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.40" +version = "1.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" +checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203" [[package]] name = "async-trait" @@ -137,9 +137,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "block-buffer" @@ -152,9 +152,9 @@ dependencies = [ [[package]] name = "bumpalo" -version = "3.6.1" +version = "3.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63396b8a4b9de3f4fdfb320ab6080762242f66a8ef174c49d8e19b674db4cdbe" +checksum = "8f1e260c3a9040a7c19a12468758f4c16f31a81a1fe087482be9570ec864bb6c" [[package]] name = "byteorder" @@ -170,9 +170,9 @@ checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" [[package]] name = "cc" -version = "1.0.68" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" +checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" dependencies = [ "jobserver", ] @@ -213,7 +213,7 @@ dependencies = [ "libc", "num-integer", "num-traits", - "time", + "time 0.1.43", "winapi", ] @@ -228,13 +228,13 @@ dependencies = [ [[package]] name = "clang-sys" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "853eda514c284c2287f4bf20ae614f8781f40a81d32ecda6e91449304dfe077c" +checksum = "fa66045b9cb23c2e9c1520732030608b02ee07e5cfaa5a521ec15ded7fa24c90" dependencies = [ "glob", "libc", - "libloading 0.7.0", + "libloading 0.7.2", ] [[package]] @@ -250,9 +250,9 @@ dependencies = [ [[package]] name = "combine" -version = "4.5.2" +version = "4.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc4369b5e4c0cddf64ad8981c0111e7df4f7078f4d6ba98fb31f2e17c4c57b7e" +checksum = "b2b2f5d0ee456f3928812dfc8c6d9a1d592b98678f6d56db9b0cd2b7bc6c8db5" dependencies = [ "bytes", "memchr", @@ -264,16 +264,10 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6888e10551bb93e424d8df1d07f1a8b4fceb0001a3a4b048bfc47554946f47b3" dependencies = [ - "core-foundation-sys 0.8.3", + "core-foundation-sys", "libc", ] -[[package]] -name = "core-foundation-sys" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b" - [[package]] name = "core-foundation-sys" version = "0.8.3" @@ -301,21 +295,21 @@ dependencies = [ [[package]] name = "cpal" -version = "0.13.3" +version = "0.13.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8351ddf2aaa3c583fa388029f8b3d26f3c7035a20911fdd5f2e2ed7ab57dad25" +checksum = "98f45f0a21f617cd2c788889ef710b63f075c949259593ea09c826f1e47a2418" dependencies = [ "alsa", - "core-foundation-sys 0.6.2", + "core-foundation-sys", "coreaudio-rs", - "jack 0.6.6", + "jack", "jni", "js-sys", "lazy_static", "libc", "mach", - "ndk", - "ndk-glue", + "ndk 0.3.0", + "ndk-glue 0.3.0", "nix", "oboe", "parking_lot", @@ -327,18 +321,18 @@ dependencies = [ [[package]] name = "cpufeatures" -version = "0.1.4" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ed00c67cb5d0a7d64a44f6ad2668db7e7530311dd53ea79bcd4fb022c64911c8" +checksum = "95059428f66df56b63431fdb4e1947ed2190586af5c5a8a8b71122bdf5a7f469" dependencies = [ "libc", ] [[package]] name = "crypto-mac" -version = "0.11.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25fab6889090c8133f3deb8f73ba3c65a7f456f66436fc012a1b1e272b1e103e" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" dependencies = [ "generic-array", "subtle", @@ -435,9 +429,9 @@ checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] name = "env_logger" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" dependencies = [ "atty", "humantime", @@ -464,9 +458,9 @@ dependencies = [ [[package]] name = "futures" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e7e43a803dae2fa37c1f6a8fe121e1f7bf9548b4dfc0522a42f34145dadfc27" +checksum = "8cd0210d8c325c245ff06fd95a3b13689a1a276ac8cfa8e8720cb840bfb84b9e" dependencies = [ "futures-channel", "futures-core", @@ -479,9 +473,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e682a68b29a882df0545c143dc3646daefe80ba479bcdede94d5a703de2871e2" +checksum = "7fc8cd39e3dbf865f7340dce6a2d401d24fd37c6fe6c4f0ee0de8bfca2252d27" dependencies = [ "futures-core", "futures-sink", @@ -489,15 +483,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" +checksum = "629316e42fe7c2a0b9a65b47d159ceaa5453ab14e8f0a3c5eedbb8cd55b4a445" [[package]] name = "futures-executor" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "badaa6a909fac9e7236d0620a2f57f7664640c56575b71a7552fbd68deafab79" +checksum = "7b808bf53348a36cab739d7e04755909b9fcaaa69b7d7e588b37b6ec62704c97" dependencies = [ "futures-core", "futures-task", @@ -506,18 +500,16 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acc499defb3b348f8d8f3f66415835a9131856ff7714bf10dadfc4ec4bdb29a1" +checksum = "e481354db6b5c353246ccf6a728b0c5511d752c08da7260546fc0933869daa11" [[package]] name = "futures-macro" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" +checksum = "a89f17b21645bc4ed773c69af9c9a0effd4a3f1a3876eadd453469f8854e7fdd" dependencies = [ - "autocfg", - "proc-macro-hack", "proc-macro2", "quote", "syn", @@ -525,23 +517,22 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" +checksum = "996c6442437b62d21a32cd9906f9c41e7dc1e19a9579843fad948696769305af" [[package]] name = "futures-task" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" +checksum = "dabf1872aaab32c886832f2276d2f5399887e2bd613698a02359e4ea83f8de12" [[package]] name = "futures-util" -version = "0.3.15" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" +checksum = "41d22213122356472061ac0f1ab2cee28d2bac8491410fd68c2af53d1cedb83e" dependencies = [ - "autocfg", "futures-channel", "futures-core", "futures-io", @@ -551,8 +542,6 @@ dependencies = [ "memchr", "pin-project-lite", "pin-utils", - "proc-macro-hack", - "proc-macro-nested", "slab", ] @@ -614,7 +603,7 @@ dependencies = [ "anyhow", "heck", "itertools", - "proc-macro-crate", + "proc-macro-crate 0.1.5", "proc-macro-error", "proc-macro2", "quote", @@ -767,24 +756,24 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.9.1" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" +checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" [[package]] name = "headers" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0b7591fb62902706ae8e7aaff416b1b0fa2c0fd0878b46dc13baa3712d8a855" +checksum = "a4c4eb0471fcb85846d8b0690695ef354f9afb11cb03cac2e1d7c9253351afb0" dependencies = [ "base64", "bitflags", "bytes", "headers-core", "http", + "httpdate", "mime", "sha-1", - "time", ] [[package]] @@ -798,18 +787,18 @@ dependencies = [ [[package]] name = "heck" -version = "0.3.2" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" dependencies = [ "unicode-segmentation", ] [[package]] name = "hermit-abi" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" dependencies = [ "libc", ] @@ -854,9 +843,9 @@ dependencies = [ [[package]] name = "http-body" -version = "0.4.2" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60daa14be0e0786db0f03a9e57cb404c9d756eed2b6c62b9ea98ec5743ec75a9" +checksum = "1ff4f84919677303da5f147645dbea6b1881f368d03ac84e1dc09031ebd7b2c6" dependencies = [ "bytes", "http", @@ -871,9 +860,9 @@ checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" [[package]] name = "httpdate" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440" +checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" [[package]] name = "humantime" @@ -960,9 +949,9 @@ dependencies = [ [[package]] name = "if-addrs" -version = "0.6.5" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28538916eb3f3976311f5dfbe67b5362d0add1293d0a9cad17debf86f8e3aa48" +checksum = "2273e421f7c4f0fc99e1934fe4776f59d8df2972f4199d703fc0da9f2a9f73de" dependencies = [ "if-addrs-sys", "libc", @@ -981,9 +970,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.6.2" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" +checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" dependencies = [ "autocfg", "hashbrown", @@ -1000,9 +989,9 @@ dependencies = [ [[package]] name = "instant" -version = "0.1.9" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ "cfg-if 1.0.0", ] @@ -1018,32 +1007,21 @@ dependencies = [ [[package]] name = "itoa" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "jack" -version = "0.6.6" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2deb4974bd7e6b2fb7784f27fa13d819d11292b3b004dce0185ec08163cf686a" -dependencies = [ - "bitflags", - "jack-sys", - "lazy_static", - "libc", -] - -[[package]] -name = "jack" -version = "0.7.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49e720259b4a3e1f33cba335ca524a99a5f2411d405b05f6405fadd69269e2db" +checksum = "d79b205ea723e478eb31a91dcdda100912c69cc32992eb7ba26ec0bbae7bebe4" dependencies = [ "bitflags", "jack-sys", "lazy_static", "libc", + "log", ] [[package]] @@ -1059,9 +1037,9 @@ dependencies = [ [[package]] name = "jni" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24967112a1e4301ca5342ea339763613a37592b8a6ce6cf2e4494537c7a42faf" +checksum = "c6df18c2e3db7e453d3c6ac5b3e9d5182664d28788126d39b91f2d1e22b017ec" dependencies = [ "cesu8", "combine", @@ -1079,18 +1057,18 @@ checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" [[package]] name = "jobserver" -version = "0.1.22" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "972f5ae5d1cb9c6ae417789196c803205313edde988685da5e3aae0827b9e7fd" +checksum = "af25a77299a7f711a01975c35a6a424eb6862092cc2d6c72c4ed6cbc56dfc1fa" dependencies = [ "libc", ] [[package]] name = "js-sys" -version = "0.3.51" +version = "0.3.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" +checksum = "7cc9ffccd38c451a86bf13657df244e9c3f37493cce8e5e21e940963777acc84" dependencies = [ "wasm-bindgen", ] @@ -1120,9 +1098,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.108" +version = "0.2.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119" +checksum = "f98a04dce437184842841303488f70d0188c5f51437d2a834dc097eafa909a01" [[package]] name = "libloading" @@ -1136,9 +1114,9 @@ dependencies = [ [[package]] name = "libloading" -version = "0.7.0" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f84d96438c15fcd6c3f244c8fce01d1e2b9c6b5623e9c711dc9286d8fc92d6a" +checksum = "afe203d669ec979b7128619bae5a63b7b42e9203c1b29146079ee05e2f604b52" dependencies = [ "cfg-if 1.0.0", "winapi", @@ -1152,9 +1130,9 @@ checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" [[package]] name = "libmdns" -version = "0.6.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98477a6781ae1d6a1c2aeabfd2e23353a75fe8eb7c2545f6ed282ac8f3e2fc53" +checksum = "fac185a4d02e873c6d1ead59d674651f8ae5ec23ffe1637bee8de80665562a6a" dependencies = [ "byteorder", "futures-util", @@ -1170,9 +1148,9 @@ dependencies = [ [[package]] name = "libpulse-binding" -version = "2.23.1" +version = "2.25.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db951f37898e19a6785208e3a290261e0f1a8e086716be596aaad684882ca8e3" +checksum = "86835d7763ded6bc16b6c0061ec60214da7550dfcd4ef93745f6f0096129676a" dependencies = [ "bitflags", "libc", @@ -1184,9 +1162,9 @@ dependencies = [ [[package]] name = "libpulse-simple-binding" -version = "2.23.0" +version = "2.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a574975292db859087c3957b9182f7d53278553f06bddaa2099c90e4ac3a0ee0" +checksum = "d6a22538257c4d522bea6089d6478507f5d2589ea32150e20740aaaaaba44590" dependencies = [ "libpulse-binding", "libpulse-simple-sys", @@ -1195,9 +1173,9 @@ dependencies = [ [[package]] name = "libpulse-simple-sys" -version = "1.16.1" +version = "1.19.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "468cf582b7b022c0d1b266fefc7fc8fa7b1ddcb61214224f2f105c95a9c2d5c1" +checksum = "7c73f96f9ca34809692c4760cfe421225860aa000de50edab68a16221fd27cc1" dependencies = [ "libpulse-sys", "pkg-config", @@ -1205,9 +1183,9 @@ dependencies = [ [[package]] name = "libpulse-sys" -version = "1.18.0" +version = "1.19.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf17e9832643c4f320c42b7d78b2c0510f45aa5e823af094413b94e45076ba82" +checksum = "991e6bd0efe2a36e6534e136e7996925e4c1a8e35b7807fe533f2beffff27c30" dependencies = [ "libc", "num-derive", @@ -1218,7 +1196,7 @@ dependencies = [ [[package]] name = "librespot" -version = "0.2.0" +version = "0.3.1" dependencies = [ "base64", "env_logger", @@ -1243,7 +1221,7 @@ dependencies = [ [[package]] name = "librespot-audio" -version = "0.2.0" +version = "0.3.1" dependencies = [ "aes-ctr", "byteorder", @@ -1257,7 +1235,7 @@ dependencies = [ [[package]] name = "librespot-connect" -version = "0.2.0" +version = "0.3.1" dependencies = [ "form_urlencoded", "futures-util", @@ -1276,7 +1254,7 @@ dependencies = [ [[package]] name = "librespot-core" -version = "0.2.0" +version = "0.3.1" dependencies = [ "aes", "base64", @@ -1320,7 +1298,7 @@ dependencies = [ [[package]] name = "librespot-discovery" -version = "0.2.0" +version = "0.3.1" dependencies = [ "aes-ctr", "base64", @@ -1345,7 +1323,7 @@ dependencies = [ [[package]] name = "librespot-metadata" -version = "0.2.0" +version = "0.3.1" dependencies = [ "async-trait", "byteorder", @@ -1361,7 +1339,7 @@ dependencies = [ [[package]] name = "librespot-playback" -version = "0.2.0" +version = "0.3.1" dependencies = [ "alsa", "byteorder", @@ -1371,7 +1349,7 @@ dependencies = [ "glib", "gstreamer", "gstreamer-app", - "jack 0.7.1", + "jack", "lewton", "libpulse-binding", "libpulse-simple-binding", @@ -1393,7 +1371,7 @@ dependencies = [ [[package]] name = "librespot-protocol" -version = "0.2.0" +version = "0.3.1" dependencies = [ "glob", "protobuf", @@ -1402,9 +1380,9 @@ dependencies = [ [[package]] name = "lock_api" -version = "0.4.4" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" +checksum = "712a4d093c9976e24e7dbca41db895dabcbac38eb5f4045393d17a95bdfb1109" dependencies = [ "scopeguard", ] @@ -1435,15 +1413,15 @@ checksum = "ffbee8634e0d45d258acb448e7eaab3fce7a0a467395d4d9f228e3c1f01fb2e4" [[package]] name = "matches" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" +checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] name = "memchr" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "mime" @@ -1453,9 +1431,9 @@ checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" [[package]] name = "mio" -version = "0.7.11" +version = "0.7.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956" +checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" dependencies = [ "libc", "log", @@ -1500,6 +1478,19 @@ dependencies = [ "thiserror", ] +[[package]] +name = "ndk" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d64d6af06fde0e527b1ba5c7b79a6cc89cfc46325b0b2887dffe8f70197e0c3c" +dependencies = [ + "bitflags", + "jni-sys", + "ndk-sys", + "num_enum", + "thiserror", +] + [[package]] name = "ndk-glue" version = "0.3.0" @@ -1509,7 +1500,21 @@ dependencies = [ "lazy_static", "libc", "log", - "ndk", + "ndk 0.3.0", + "ndk-macro", + "ndk-sys", +] + +[[package]] +name = "ndk-glue" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3e9e94628f24e7a3cb5b96a2dc5683acd9230bf11991c2a1677b87695138420" +dependencies = [ + "lazy_static", + "libc", + "log", + "ndk 0.4.0", "ndk-macro", "ndk-sys", ] @@ -1521,7 +1526,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05d1c6307dc424d0f65b9b06e94f88248e6305726b14729fd67a5e47b2dc481d" dependencies = [ "darling", - "proc-macro-crate", + "proc-macro-crate 0.1.5", "proc-macro2", "quote", "syn", @@ -1529,9 +1534,9 @@ dependencies = [ [[package]] name = "ndk-sys" -version = "0.2.1" +version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c44922cb3dbb1c70b5e5f443d63b64363a898564d739ba5198e3a9138442868d" +checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121" [[package]] name = "nix" @@ -1580,9 +1585,9 @@ dependencies = [ [[package]] name = "num-bigint" -version = "0.4.0" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e0d047c1062aa51e256408c560894e5251f08925980e53cf1aa5bd00eec6512" +checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f" dependencies = [ "autocfg", "num-integer", @@ -1676,9 +1681,9 @@ dependencies = [ [[package]] name = "num_enum" -version = "0.5.1" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "226b45a5c2ac4dd696ed30fa6b94b057ad909c7b7fc2e0d0808192bced894066" +checksum = "3f9bd055fb730c4f8f4f57d45d35cd6b3f0980535b056dc7ff119cee6a66ed6f" dependencies = [ "derivative", "num_enum_derive", @@ -1686,11 +1691,11 @@ dependencies = [ [[package]] name = "num_enum_derive" -version = "0.5.1" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c0fd9eba1d5db0994a239e09c1be402d35622277e35468ba891aa5e3188ce7e" +checksum = "486ea01961c4a818096de679a8b740b26d9033146ac5291b1c98557658f8cdd9" dependencies = [ - "proc-macro-crate", + "proc-macro-crate 1.1.0", "proc-macro2", "quote", "syn", @@ -1698,13 +1703,13 @@ dependencies = [ [[package]] name = "oboe" -version = "0.4.2" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfa187b38ae20374617b7ad418034ed3dc90ac980181d211518bd03537ae8f8d" +checksum = "e15e22bc67e047fe342a32ecba55f555e3be6166b04dd157cd0f803dfa9f48e1" dependencies = [ "jni", - "ndk", - "ndk-glue", + "ndk 0.4.0", + "ndk-glue 0.4.0", "num-derive", "num-traits", "oboe-sys", @@ -1712,9 +1717,9 @@ dependencies = [ [[package]] name = "oboe-sys" -version = "0.4.2" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b88e64835aa3f579c08d182526dc34e3907343d5b97e87b71a40ba5bca7aca9e" +checksum = "338142ae5ab0aaedc8275aa8f67f460e43ae0fca76a695a742d56da0a269eadc" dependencies = [ "cc", ] @@ -1730,9 +1735,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.7.2" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" +checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56" [[package]] name = "opaque-debug" @@ -1748,9 +1753,9 @@ checksum = "28988d872ab76095a6e6ac88d99b54fd267702734fd7ffe610ca27f533ddb95a" [[package]] name = "parking_lot" -version = "0.11.1" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" dependencies = [ "instant", "lock_api", @@ -1759,9 +1764,9 @@ dependencies = [ [[package]] name = "parking_lot_core" -version = "0.8.3" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" dependencies = [ "cfg-if 1.0.0", "instant", @@ -1773,9 +1778,9 @@ dependencies = [ [[package]] name = "paste" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "acbf547ad0c65e31259204bd90935776d1c693cec2f4ff7abb7a1bbbd40dfe58" +checksum = "0744126afe1a6dd7f394cb50a716dbe086cb06e255e53d8d0185d82828358fb5" [[package]] name = "pbkdf2" @@ -1799,15 +1804,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" -[[package]] -name = "pest" -version = "2.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53" -dependencies = [ - "ucd-trie", -] - [[package]] name = "pin-project" version = "1.0.8" @@ -1830,9 +1826,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" +checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443" [[package]] name = "pin-utils" @@ -1842,9 +1838,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.19" +version = "0.3.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" +checksum = "d1a3ea4f0dd7f1f3e512cf97bf100819aa547f36a6eccac8dbaae839eb92363e" [[package]] name = "portaudio-rs" @@ -1869,9 +1865,9 @@ dependencies = [ [[package]] name = "ppv-lite86" -version = "0.2.10" +version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba" [[package]] name = "pretty-hex" @@ -1881,9 +1877,9 @@ checksum = "bc5c99d529f0d30937f6f4b8a86d988047327bb88d04d2c4afc356de74722131" [[package]] name = "priority-queue" -version = "1.1.1" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e1340009a04e81f656a4e45e295f0b1191c81de424bf940c865e33577a8e223" +checksum = "00ba480ac08d3cfc40dea10fd466fd2c14dee3ea6fc7873bc4079eda2727caf0" dependencies = [ "autocfg", "indexmap", @@ -1898,6 +1894,16 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-crate" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebace6889caf889b4d3f76becee12e90353f2b8c7d875534a71e5742f8f6f83" +dependencies = [ + "thiserror", + "toml", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1922,23 +1928,11 @@ dependencies = [ "version_check", ] -[[package]] -name = "proc-macro-hack" -version = "0.5.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" - -[[package]] -name = "proc-macro-nested" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" - [[package]] name = "proc-macro2" -version = "1.0.27" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +checksum = "fb37d2df5df740e582f28f8560cf425f52bb267d872fe58358eadb554909f07a" dependencies = [ "unicode-xid", ] @@ -1970,18 +1964,18 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.9" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" dependencies = [ "proc-macro2", ] [[package]] name = "rand" -version = "0.8.3" +version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" dependencies = [ "libc", "rand_chacha", @@ -1991,9 +1985,9 @@ dependencies = [ [[package]] name = "rand_chacha" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", "rand_core", @@ -2001,18 +1995,18 @@ dependencies = [ [[package]] name = "rand_core" -version = "0.6.2" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" dependencies = [ "getrandom", ] [[package]] name = "rand_distr" -version = "0.4.0" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9e8f32ad24fb80d07d2323a9a2ce8b30d68a62b8cb4df88119ff49a698f038" +checksum = "964d548f8e7d12e102ef183a0de7e98180c9f8729f555897a857b96e48122d2f" dependencies = [ "num-traits", "rand", @@ -2020,18 +2014,18 @@ dependencies = [ [[package]] name = "rand_hc" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" dependencies = [ "rand_core", ] [[package]] name = "redox_syscall" -version = "0.2.8" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" +checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff" dependencies = [ "bitflags", ] @@ -2104,9 +2098,9 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" [[package]] name = "rustc_version" -version = "0.3.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0dfe2087c51c460008730de8b57e6a320782fbfb312e1f4d520e6c6fae155ee" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ "semver", ] @@ -2138,9 +2132,9 @@ dependencies = [ [[package]] name = "ryu" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +checksum = "3c9613b5a66ab9ba26415184cfc41156594925a9cf3a2057e57f31ff145f6568" [[package]] name = "same-file" @@ -2202,13 +2196,13 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.3.1" +version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23a2ac85147a3a11d77ecf1bc7166ec0b92febfa4461c37944e180f319ece467" +checksum = "525bc1abfda2e1998d152c45cf13e696f76d0a4972310b22fac1658b05df7c87" dependencies = [ "bitflags", "core-foundation", - "core-foundation-sys 0.8.3", + "core-foundation-sys", "libc", "security-framework-sys", ] @@ -2219,42 +2213,30 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9dd14d83160b528b7bfd66439110573efcfbe281b17fc2ca9f39f550d619c7e" dependencies = [ - "core-foundation-sys 0.8.3", + "core-foundation-sys", "libc", ] [[package]] name = "semver" -version = "0.11.0" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" -dependencies = [ - "semver-parser", -] - -[[package]] -name = "semver-parser" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" -dependencies = [ - "pest", -] +checksum = "568a8e6258aa33c13358f81fd834adb854c6f7c9468520910a9b1e8fac068012" [[package]] name = "serde" -version = "1.0.126" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.126" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" +checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" dependencies = [ "proc-macro2", "quote", @@ -2263,9 +2245,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.64" +version = "1.0.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" +checksum = "d0ffa0837f2dfa6fb90868c2b5468cad482e175f7dad97e7421951e663f2b527" dependencies = [ "itoa", "ryu", @@ -2274,9 +2256,9 @@ dependencies = [ [[package]] name = "sha-1" -version = "0.9.6" +version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c4cfa741c5832d0ef7fab46cabed29c2aae926db0b11bb2069edd8db5e64e16" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ "block-buffer", "cfg-if 1.0.0", @@ -2308,43 +2290,43 @@ checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2" [[package]] name = "signal-hook-registry" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" dependencies = [ "libc", ] [[package]] name = "simple_logger" -version = "1.11.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd57f17c093ead1d4a1499dc9acaafdd71240908d64775465543b8d9a9f1d198" +checksum = "205596cf77a15774e5601c5ef759f4211ac381c0855a1f1d5e24a46f60f93e9a" dependencies = [ "atty", - "chrono", "colored", "log", + "time 0.3.5", "winapi", ] [[package]] name = "slab" -version = "0.4.3" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527" +checksum = "9def91fd1e018fe007022791f865d0ccc9b3a0d5001e01aabb8b40e46000afb5" [[package]] name = "smallvec" -version = "1.6.1" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" [[package]] name = "socket2" -version = "0.4.0" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2" +checksum = "5dc90fe6c7be1a323296982db1836d1ea9e47b6839496dde9a541bc496df3516" dependencies = [ "libc", "winapi", @@ -2388,15 +2370,15 @@ dependencies = [ [[package]] name = "subtle" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "syn" -version = "1.0.72" +version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" +checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" dependencies = [ "proc-macro2", "quote", @@ -2405,9 +2387,9 @@ dependencies = [ [[package]] name = "synstructure" -version = "0.12.4" +version = "0.12.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" dependencies = [ "proc-macro2", "quote", @@ -2455,18 +2437,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.25" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa6f76457f59514c7eeb4e59d891395fab0b2fd1d40723ae737d64153392e9c6" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.25" +version = "1.0.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a36768c0fbf1bb15eca10defa29526bda730a2376c2ab4393ccfa16fb1a318d" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" dependencies = [ "proc-macro2", "quote", @@ -2484,10 +2466,27 @@ dependencies = [ ] [[package]] -name = "tinyvec" -version = "1.2.0" +name = "time" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b5220f05bb7de7f3f53c7c065e1199b3172696fe2db9f9c4d8ad9b4ee74c342" +checksum = "41effe7cfa8af36f439fac33861b66b049edc6f9a32331e2312660529c1c24ad" +dependencies = [ + "itoa", + "libc", + "time-macros", +] + +[[package]] +name = "time-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25eb0ca3468fc0acc11828786797f6ef9aa1555e4a211a60d64cc8e4d1be47d6" + +[[package]] +name = "tinyvec" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c1c1d5a42b6245520c249549ec267180beaffcc0615401ac8e31853d4b6d8d2" dependencies = [ "tinyvec_macros", ] @@ -2598,9 +2597,9 @@ checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" [[package]] name = "tracing" -version = "0.1.26" +version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09adeb8c97449311ccd28a427f96fb563e7fd31aabf994189879d9da2394b89d" +checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" dependencies = [ "cfg-if 1.0.0", "pin-project-lite", @@ -2609,9 +2608,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.18" +version = "0.1.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052" +checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" dependencies = [ "lazy_static", ] @@ -2647,45 +2646,36 @@ dependencies = [ [[package]] name = "typenum" -version = "1.13.0" +version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" - -[[package]] -name = "ucd-trie" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" +checksum = "b63708a265f51345575b27fe43f9500ad611579e764c79edbc2037b1121959ec" [[package]] name = "unicode-bidi" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eeb8be209bb1c96b7c177c7420d26e04eccacb0eeae6b980e35fcb74678107e0" -dependencies = [ - "matches", -] +checksum = "1a01404663e3db436ed2746d9fefef640d868edae3cceb81c3b8d5732fda678f" [[package]] name = "unicode-normalization" -version = "0.1.17" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" +checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9" dependencies = [ "tinyvec", ] [[package]] name = "unicode-segmentation" -version = "1.7.1" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" [[package]] name = "unicode-width" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973" [[package]] name = "unicode-xid" @@ -2778,9 +2768,9 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" [[package]] name = "wasm-bindgen" -version = "0.2.74" +version = "0.2.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" +checksum = "632f73e236b219150ea279196e54e610f5dbafa5d61786303d4da54f84e47fce" dependencies = [ "cfg-if 1.0.0", "wasm-bindgen-macro", @@ -2788,9 +2778,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.74" +version = "0.2.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" +checksum = "a317bf8f9fba2476b4b2c85ef4c4af8ff39c3c7f0cdfeed4f82c34a880aa837b" dependencies = [ "bumpalo", "lazy_static", @@ -2803,9 +2793,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.74" +version = "0.2.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" +checksum = "d56146e7c495528bf6587663bea13a8eb588d39b36b679d83972e1a2dbbdacf9" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2813,9 +2803,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.74" +version = "0.2.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" +checksum = "7803e0eea25835f8abdc585cd3021b3deb11543c6fe226dcd30b228857c5c5ab" dependencies = [ "proc-macro2", "quote", @@ -2826,15 +2816,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.74" +version = "0.2.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" +checksum = "0237232789cf037d5480773fe568aac745bfe2afbc11a863e97901780a6b47cc" [[package]] name = "web-sys" -version = "0.3.51" +version = "0.3.55" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582" +checksum = "38eb105f1c59d9eaa6b5cdc92b859d85b926e82cb2e0945cd0c9259faa6fe9fb" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index ced7d0f9..8429ba2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot" -version = "0.2.0" +version = "0.3.1" authors = ["Librespot Org"] license = "MIT" description = "An open source client library for Spotify, with support for Spotify Connect" @@ -22,31 +22,31 @@ doc = false [dependencies.librespot-audio] path = "audio" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-connect] path = "connect" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-core] path = "core" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-discovery] path = "discovery" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-metadata] path = "metadata" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-playback] path = "playback" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-protocol] path = "protocol" -version = "0.2.0" +version = "0.3.1" [dependencies] base64 = "0.13" diff --git a/README.md b/README.md index bcf73cac..5dbb5487 100644 --- a/README.md +++ b/README.md @@ -2,17 +2,17 @@ [![Gitter chat](https://badges.gitter.im/librespot-org/librespot.png)](https://gitter.im/librespot-org/spotify-connect-resources) [![Crates.io](https://img.shields.io/crates/v/librespot.svg)](https://crates.io/crates/librespot) -Current maintainer is [@awiouy](https://github.com/awiouy) folks. +Current maintainers are [listed on GitHub](https://github.com/orgs/librespot-org/people). # librespot *librespot* is an open source client library for Spotify. It enables applications to use Spotify's service to control and play music via various backends, and to act as a Spotify Connect receiver. It is an alternative to the official and [now deprecated](https://pyspotify.mopidy.com/en/latest/#libspotify-s-deprecation) closed-source `libspotify`. Additionally, it will provide extra features which are not available in the official library. -_Note: librespot only works with Spotify Premium. This will remain the case for the foreseeable future, as we are unlikely to work on implementing the features such as limited skips and adverts that would be required to make librespot compliant with free accounts._ +_Note: librespot only works with Spotify Premium. This will remain the case. We will not support any features to make librespot compatible with free accounts, such as limited skips and adverts._ ## Quick start We're available on [crates.io](https://crates.io/crates/librespot) as the _librespot_ package. Simply run `cargo install librespot` to install librespot on your system. Check the wiki for more info and possible [usage options](https://github.com/librespot-org/librespot/wiki/Options). -After installation, you can run librespot from the CLI using a command such as `librespot -n "Librespot Speaker" -b 160` to create a speaker called _Librespot Speaker_ serving 160kbps audio. +After installation, you can run librespot from the CLI using a command such as `librespot -n "Librespot Speaker" -b 160` to create a speaker called _Librespot Speaker_ serving 160 kbps audio. ## This fork As the origin by [plietar](https://github.com/plietar/) is no longer actively maintained, this organisation and repository have been set up so that the project may be maintained and upgraded in the future. @@ -20,7 +20,7 @@ As the origin by [plietar](https://github.com/plietar/) is no longer actively ma # Documentation Documentation is currently a work in progress, contributions are welcome! -There is some brief documentation on how the protocol works in the [docs](https://github.com/librespot-org/librespot/tree/master/docs) folder, +There is some brief documentation on how the protocol works in the [docs](https://github.com/librespot-org/librespot/tree/master/docs) folder. [COMPILING.md](https://github.com/librespot-org/librespot/blob/master/COMPILING.md) contains detailed instructions on setting up a development environment, and compiling librespot. More general usage and compilation information is available on the [wiki](https://github.com/librespot-org/librespot/wiki). [CONTRIBUTING.md](https://github.com/librespot-org/librespot/blob/master/CONTRIBUTING.md) also contains our contributing guidelines. @@ -30,37 +30,39 @@ If you wish to learn more about how librespot works overall, the best way is to # Issues & Discussions **We have recently started using Github discussions for general questions and feature requests, as they are a more natural medium for such cases, and allow for upvoting to prioritize feature development. Check them out [here](https://github.com/librespot-org/librespot/discussions). Bugs and issues with the underlying library should still be reported as issues.** -If you run into a bug when using librespot, please search the existing issues before opening a new one. Chances are, we've encountered it before, and have provided a resolution. If not, please open a new one, and where possible, include the backtrace librespot generates on crashing, along with anything we can use to reproduce the issue, eg. the Spotify URI of the song that caused the crash. +If you run into a bug when using librespot, please search the existing issues before opening a new one. Chances are, we've encountered it before, and have provided a resolution. If not, please open a new one, and where possible, include the backtrace librespot generates on crashing, along with anything we can use to reproduce the issue, e.g. the Spotify URI of the song that caused the crash. # Building -A quick walk through of the build process is outlined here, while a detailed compilation guide can be found [here](https://github.com/librespot-org/librespot/blob/master/COMPILING.md). +A quick walkthrough of the build process is outlined below, while a detailed compilation guide can be found [here](https://github.com/librespot-org/librespot/blob/master/COMPILING.md). ## Additional Dependencies We recently switched to using [Rodio](https://github.com/tomaka/rodio) for audio playback by default, hence for macOS and Windows, you should just be able to clone and build librespot (with the command below). For Linux, you will need to run the additional commands below, depending on your distro. -On Debian/Ubuntu, the following command will install these dependencies : +On Debian/Ubuntu, the following command will install these dependencies: ```shell sudo apt-get install build-essential libasound2-dev ``` -On Fedora systems, the following command will install these dependencies : +On Fedora systems, the following command will install these dependencies: ```shell sudo dnf install alsa-lib-devel make gcc ``` -librespot currently offers the following selection of [audio backends](https://github.com/librespot-org/librespot/wiki/Audio-Backends). +librespot currently offers the following selection of [audio backends](https://github.com/librespot-org/librespot/wiki/Audio-Backends): ``` Rodio (default) ALSA +GStreamer PortAudio PulseAudio JACK JACK over Rodio SDL Pipe +Subprocess ``` -Please check the corresponding [compiling entry](https://github.com/librespot-org/librespot/wiki/Compiling#general-dependencies) for backend specific dependencies. +Please check the corresponding [Compiling](https://github.com/librespot-org/librespot/wiki/Compiling#general-dependencies) entry on the wiki for backend specific dependencies. Once you've installed the dependencies and cloned this repository you can build *librespot* with the default backend using Cargo. ```shell @@ -84,14 +86,14 @@ The above is a minimal example. Here is a more fully fledged one: ```shell target/release/librespot -n "Librespot" -b 320 -c ./cache --enable-volume-normalisation --initial-volume 75 --device-type avr ``` -The above command will create a receiver named ```Librespot```, with bitrate set to 320kbps, initial volume at 75%, with volume normalisation enabled, and the device displayed in the app as an Audio/Video Receiver. A folder named ```cache``` will be created/used in the current directory, and be used to cache audio data and credentials. +The above command will create a receiver named ```Librespot```, with bitrate set to 320 kbps, initial volume at 75%, with volume normalisation enabled, and the device displayed in the app as an Audio/Video Receiver. A folder named ```cache``` will be created/used in the current directory, and be used to cache audio data and credentials. -A full list of runtime options are available [here](https://github.com/librespot-org/librespot/wiki/Options) +A full list of runtime options is available [here](https://github.com/librespot-org/librespot/wiki/Options). _Please Note: When using the cache feature, an authentication blob is stored for your account in the cache directory. For security purposes, we recommend that you set directory permissions on the cache directory to `700`._ ## Contact -Come and hang out on gitter if you need help or want to offer some. +Come and hang out on gitter if you need help or want to offer some: https://gitter.im/librespot-org/spotify-connect-resources ## Disclaimer @@ -114,3 +116,4 @@ functionality. - [librespot-java](https://github.com/devgianlu/librespot-java) - A Java port of librespot. - [ncspot](https://github.com/hrkfdn/ncspot) - Cross-platform ncurses Spotify client. - [ansible-role-librespot](https://github.com/xMordax/ansible-role-librespot/tree/master) - Ansible role that will build, install and configure Librespot. +- [Spot](https://github.com/xou816/spot) - Gtk/Rust native Spotify client for the GNOME desktop. diff --git a/audio/Cargo.toml b/audio/Cargo.toml index f4440592..77855e62 100644 --- a/audio/Cargo.toml +++ b/audio/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot-audio" -version = "0.2.0" +version = "0.3.1" authors = ["Paul Lietar "] description="The audio fetching and processing logic for librespot" license="MIT" @@ -8,7 +8,7 @@ edition = "2018" [dependencies.librespot-core] path = "../core" -version = "0.2.0" +version = "0.3.1" [dependencies] aes-ctr = "0.6" diff --git a/audio/src/decrypt.rs b/audio/src/decrypt.rs index 616ef4f6..17f4edba 100644 --- a/audio/src/decrypt.rs +++ b/audio/src/decrypt.rs @@ -18,8 +18,8 @@ pub struct AudioDecrypt { impl AudioDecrypt { pub fn new(key: AudioKey, reader: T) -> AudioDecrypt { let cipher = Aes128Ctr::new( - &GenericArray::from_slice(&key.0), - &GenericArray::from_slice(&AUDIO_AESIV), + GenericArray::from_slice(&key.0), + GenericArray::from_slice(&AUDIO_AESIV), ); AudioDecrypt { cipher, reader } } diff --git a/audio/src/fetch/receive.rs b/audio/src/fetch/receive.rs index 5de90b79..d57e6cc4 100644 --- a/audio/src/fetch/receive.rs +++ b/audio/src/fetch/receive.rs @@ -267,7 +267,8 @@ impl AudioFileFetch { fn handle_file_data(&mut self, data: ReceivedData) -> ControlFlow { match data { ReceivedData::ResponseTime(response_time) => { - trace!("Ping time estimated as: {}ms", response_time.as_millis()); + // chatty + // trace!("Ping time estimated as: {}ms", response_time.as_millis()); // prune old response times. Keep at most two so we can push a third. while self.network_response_times.len() >= 3 { diff --git a/connect/Cargo.toml b/connect/Cargo.toml index 89d185ab..4daf89f4 100644 --- a/connect/Cargo.toml +++ b/connect/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot-connect" -version = "0.2.0" +version = "0.3.1" authors = ["Paul Lietar "] description = "The discovery and Spotify Connect logic for librespot" license = "MIT" @@ -20,19 +20,19 @@ tokio-stream = "0.1.1" [dependencies.librespot-core] path = "../core" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-playback] path = "../playback" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-protocol] path = "../protocol" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-discovery] path = "../discovery" -version = "0.2.0" +version = "0.3.1" [features] with-dns-sd = ["librespot-discovery/with-dns-sd"] diff --git a/connect/src/context.rs b/connect/src/context.rs index 63a2aebb..154d9507 100644 --- a/connect/src/context.rs +++ b/connect/src/context.rs @@ -46,6 +46,7 @@ pub struct TrackContext { // pub metadata: MetadataContext, } +#[allow(dead_code)] #[derive(Deserialize, Debug)] #[serde(rename_all = "camelCase")] pub struct ArtistContext { @@ -54,6 +55,7 @@ pub struct ArtistContext { image_uri: String, } +#[allow(dead_code)] #[derive(Deserialize, Debug)] pub struct MetadataContext { album_title: String, diff --git a/connect/src/spirc.rs b/connect/src/spirc.rs index e033b91d..758025a1 100644 --- a/connect/src/spirc.rs +++ b/connect/src/spirc.rs @@ -888,8 +888,8 @@ impl SpircTask { let tracks_len = self.state.get_track().len() as u32; debug!( "At track {:?} of {:?} <{:?}> update [{}]", - new_index, - self.state.get_track().len(), + new_index + 1, + tracks_len, self.state.get_context_uri(), tracks_len - new_index < CONTEXT_FETCH_THRESHOLD ); @@ -903,16 +903,20 @@ impl SpircTask { self.context_fut = self.resolve_station(&context_uri); self.update_tracks_from_context(); } - if self.config.autoplay && new_index == tracks_len - 1 { - // Extend the playlist - // Note: This doesn't seem to reflect in the UI - // the additional tracks in the frame don't show up as with station view - debug!("Extending playlist <{}>", context_uri); - self.update_tracks_from_context(); - } if new_index >= tracks_len { - new_index = 0; // Loop around back to start - continue_playing = self.state.get_repeat(); + if self.config.autoplay { + // Extend the playlist + debug!("Extending playlist <{}>", context_uri); + self.update_tracks_from_context(); + self.player.set_auto_normalise_as_album(false); + } else { + new_index = 0; + continue_playing = self.state.get_repeat(); + debug!( + "Looping around back to start, repeat is {}", + continue_playing + ); + } } if tracks_len > 0 { @@ -1034,7 +1038,7 @@ impl SpircTask { .payload .first() .expect("Empty payload on context uri"); - let response: serde_json::Value = serde_json::from_slice(&data).unwrap(); + let response: serde_json::Value = serde_json::from_slice(data).unwrap(); Ok(response) } @@ -1052,7 +1056,7 @@ impl SpircTask { if let Some(head) = track_vec.len().checked_sub(CONTEXT_TRACKS_HISTORY) { track_vec.drain(0..head); } - track_vec.extend_from_slice(&new_tracks); + track_vec.extend_from_slice(new_tracks); self.state .set_track(protobuf::RepeatedField::from_vec(track_vec)); @@ -1085,6 +1089,9 @@ impl SpircTask { self.autoplay_fut = self.resolve_autoplay_uri(&context_uri); } + self.player + .set_auto_normalise_as_album(context_uri.starts_with("spotify:album:")); + self.state.set_playing_track_index(index); self.state.set_track(tracks.iter().cloned().collect()); self.state.set_context_uri(context_uri); @@ -1125,6 +1132,14 @@ impl SpircTask { fn get_track_id_to_play_from_playlist(&self, index: u32) -> Option<(SpotifyId, u32)> { let tracks_len = self.state.get_track().len(); + // Guard against tracks_len being zero to prevent + // 'index out of bounds: the len is 0 but the index is 0' + // https://github.com/librespot-org/librespot/issues/226#issuecomment-971642037 + if tracks_len == 0 { + warn!("No playable track found in state: {:?}", self.state); + return None; + } + let mut new_playlist_index = index as usize; if new_playlist_index >= tracks_len { @@ -1207,7 +1222,7 @@ impl SpircTask { trace!("Sending status to server: [{}]", status_string); let mut cs = CommandSender::new(self, MessageType::kMessageTypeNotify); if let Some(s) = recipient { - cs = cs.recipient(&s); + cs = cs.recipient(s); } cs.send(); } diff --git a/contrib/librespot.service b/contrib/librespot.service index 76037c8c..2c92a149 100644 --- a/contrib/librespot.service +++ b/contrib/librespot.service @@ -2,12 +2,12 @@ Description=Librespot (an open source Spotify client) Documentation=https://github.com/librespot-org/librespot Documentation=https://github.com/librespot-org/librespot/wiki/Options -Requires=network-online.target -After=network-online.target +Wants=network.target sound.target +After=network.target sound.target [Service] -User=nobody -Group=audio +DynamicUser=yes +SupplementaryGroups=audio Restart=always RestartSec=10 ExecStart=/usr/bin/librespot --name "%p@%H" diff --git a/contrib/librespot.user.service b/contrib/librespot.user.service index a676dde0..36f7f8c9 100644 --- a/contrib/librespot.user.service +++ b/contrib/librespot.user.service @@ -2,6 +2,8 @@ Description=Librespot (an open source Spotify client) Documentation=https://github.com/librespot-org/librespot Documentation=https://github.com/librespot-org/librespot/wiki/Options +Wants=network.target sound.target +After=network.target sound.target [Service] Restart=always diff --git a/core/Cargo.toml b/core/Cargo.toml index 64467366..4321c638 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot-core" -version = "0.2.0" +version = "0.3.1" authors = ["Paul Lietar "] build = "build.rs" description = "The core functionality provided by librespot" @@ -10,7 +10,7 @@ edition = "2018" [dependencies.librespot-protocol] path = "../protocol" -version = "0.2.0" +version = "0.3.1" [dependencies] aes = "0.6" diff --git a/core/src/authentication.rs b/core/src/authentication.rs index db787bbe..3c188ecf 100644 --- a/core/src/authentication.rs +++ b/core/src/authentication.rs @@ -42,7 +42,11 @@ impl Credentials { } } - pub fn with_blob(username: String, encrypted_blob: &str, device_id: &str) -> Credentials { + pub fn with_blob( + username: impl Into, + encrypted_blob: impl AsRef<[u8]>, + device_id: impl AsRef<[u8]>, + ) -> Credentials { fn read_u8(stream: &mut R) -> io::Result { let mut data = [0u8]; stream.read_exact(&mut data)?; @@ -67,7 +71,9 @@ impl Credentials { Ok(data) } - let secret = Sha1::digest(device_id.as_bytes()); + let username = username.into(); + + let secret = Sha1::digest(device_id.as_ref()); let key = { let mut key = [0u8; 24]; @@ -88,9 +94,9 @@ impl Credentials { let mut data = base64::decode(encrypted_blob).unwrap(); let cipher = Aes192::new(GenericArray::from_slice(&key)); let block_size = ::BlockSize::to_usize(); + assert_eq!(data.len() % block_size, 0); - // replace to chunks_exact_mut with MSRV bump to 1.31 - for chunk in data.chunks_mut(block_size) { + for chunk in data.chunks_exact_mut(block_size) { cipher.decrypt_block(GenericArray::from_mut_slice(chunk)); } @@ -102,7 +108,7 @@ impl Credentials { data }; - let mut cursor = io::Cursor::new(&blob); + let mut cursor = io::Cursor::new(blob.as_slice()); read_u8(&mut cursor).unwrap(); read_bytes(&mut cursor).unwrap(); read_u8(&mut cursor).unwrap(); diff --git a/core/src/cache.rs b/core/src/cache.rs index 612b7c39..da2ad022 100644 --- a/core/src/cache.rs +++ b/core/src/cache.rs @@ -238,29 +238,38 @@ pub struct RemoveFileError(()); impl Cache { pub fn new>( - system_location: Option

, - audio_location: Option

, + credentials_path: Option

, + volume_path: Option

, + audio_path: Option

, size_limit: Option, ) -> io::Result { - if let Some(location) = &system_location { + let mut size_limiter = None; + + if let Some(location) = &credentials_path { fs::create_dir_all(location)?; } - let mut size_limiter = None; + let credentials_location = credentials_path + .as_ref() + .map(|p| p.as_ref().join("credentials.json")); - if let Some(location) = &audio_location { + if let Some(location) = &volume_path { fs::create_dir_all(location)?; + } + + let volume_location = volume_path.as_ref().map(|p| p.as_ref().join("volume")); + + if let Some(location) = &audio_path { + fs::create_dir_all(location)?; + if let Some(limit) = size_limit { let limiter = FsSizeLimiter::new(location.as_ref(), limit); + size_limiter = Some(Arc::new(limiter)); } } - let audio_location = audio_location.map(|p| p.as_ref().to_owned()); - let volume_location = system_location.as_ref().map(|p| p.as_ref().join("volume")); - let credentials_location = system_location - .as_ref() - .map(|p| p.as_ref().join("credentials.json")); + let audio_location = audio_path.map(|p| p.as_ref().to_owned()); let cache = Cache { credentials_location, diff --git a/core/src/config.rs b/core/src/config.rs index 0e3eaf4a..b8c448c2 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -125,3 +125,15 @@ pub struct ConnectConfig { pub has_volume_ctrl: bool, pub autoplay: bool, } + +impl Default for ConnectConfig { + fn default() -> ConnectConfig { + ConnectConfig { + name: "Librespot".to_string(), + device_type: DeviceType::default(), + initial_volume: Some(50), + has_volume_ctrl: true, + autoplay: false, + } + } +} diff --git a/core/src/connection/codec.rs b/core/src/connection/codec.rs index 299220f6..86533aaf 100644 --- a/core/src/connection/codec.rs +++ b/core/src/connection/codec.rs @@ -87,8 +87,7 @@ impl Decoder for ApCodec { let mut payload = buf.split_to(size + MAC_SIZE); - self.decode_cipher - .decrypt(&mut payload.get_mut(..size).unwrap()); + self.decode_cipher.decrypt(payload.get_mut(..size).unwrap()); let mac = payload.split_off(size); self.decode_cipher.check_mac(mac.as_ref())?; diff --git a/core/src/connection/handshake.rs b/core/src/connection/handshake.rs index 6b144ca0..7194f0f4 100644 --- a/core/src/connection/handshake.rs +++ b/core/src/connection/handshake.rs @@ -124,7 +124,7 @@ fn compute_keys(shared_secret: &[u8], packets: &[u8]) -> (Vec, Vec, Vec< let mut data = Vec::with_capacity(0x64); for i in 1..6 { let mut mac = - HmacSha1::new_from_slice(&shared_secret).expect("HMAC can take key of any size"); + HmacSha1::new_from_slice(shared_secret).expect("HMAC can take key of any size"); mac.update(packets); mac.update(&[i]); data.extend_from_slice(&mac.finalize().into_bytes()); diff --git a/discovery/Cargo.toml b/discovery/Cargo.toml index 9ea9df48..9b4d415e 100644 --- a/discovery/Cargo.toml +++ b/discovery/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot-discovery" -version = "0.2.0" +version = "0.3.1" authors = ["Paul Lietar "] description = "The discovery logic for librespot" license = "MIT" @@ -28,7 +28,7 @@ dns-sd = { version = "0.1.3", optional = true } [dependencies.librespot-core] path = "../core" default_features = false -version = "0.2.0" +version = "0.3.1" [dev-dependencies] futures = "0.3" diff --git a/discovery/src/server.rs b/discovery/src/server.rs index 53b849f7..57f5bf46 100644 --- a/discovery/src/server.rs +++ b/discovery/src/server.rs @@ -129,11 +129,10 @@ impl RequestHandler { GenericArray::from_slice(iv), ); cipher.apply_keystream(&mut data); - String::from_utf8(data).unwrap() + data }; - let credentials = - Credentials::with_blob(username.to_string(), &decrypted, &self.config.device_id); + let credentials = Credentials::with_blob(username, &decrypted, &self.config.device_id); self.tx.send(credentials).unwrap(); diff --git a/docs/authentication.md b/docs/authentication.md index 86470161..2eeb5645 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -57,20 +57,4 @@ login_data = AES192-DECRYPT(key, data) ``` ## Facebook based Authentication -The client starts an HTTPS server, and makes the user visit -`https://login.spotify.com/login-facebook-sso/?csrf=CSRF&port=PORT` -in their browser, where CSRF is a random token, and PORT is the HTTPS server's port. - -This will redirect to Facebook, where the user must login and authorize Spotify, and -finally make a GET request to -`https://login.spotilocal.com:PORT/login/facebook_login_sso.json?csrf=CSRF&access_token=TOKEN`, -where PORT and CSRF are the same as sent earlier, and TOKEN is the facebook authentication token. - -Since `login.spotilocal.com` resolves the 127.0.0.1, the request is received by the client. - -The client must then contact Facebook's API at -`https://graph.facebook.com/me?fields=id&access_token=TOKEN` -in order to retrieve the user's Facebook ID. - -The Facebook ID is the `username`, the TOKEN the `auth_data`, and `auth_type` is set to `AUTHENTICATION_FACEBOOK_TOKEN`. - +Facebook authentication is currently broken due to Spotify changing the authentication flow. The details of how the new flow works are detailed in https://github.com/librespot-org/librespot/issues/244 and will be implemented at some point in the future. diff --git a/metadata/Cargo.toml b/metadata/Cargo.toml index a12e12f8..c9f108d6 100644 --- a/metadata/Cargo.toml +++ b/metadata/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot-metadata" -version = "0.2.0" +version = "0.3.1" authors = ["Paul Lietar "] description = "The metadata logic for librespot" license = "MIT" @@ -19,8 +19,8 @@ uuid = { version = "0.8", default-features = false } [dependencies.librespot-core] path = "../core" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-protocol] path = "../protocol" -version = "0.2.0" +version = "0.3.1" diff --git a/playback/Cargo.toml b/playback/Cargo.toml index 96b3649a..8946912b 100644 --- a/playback/Cargo.toml +++ b/playback/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot-playback" -version = "0.2.0" +version = "0.3.1" authors = ["Sasha Hilton "] description = "The audio playback logic for librespot" license = "MIT" @@ -9,13 +9,13 @@ edition = "2018" [dependencies.librespot-audio] path = "../audio" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-core] path = "../core" -version = "0.2.0" +version = "0.3.1" [dependencies.librespot-metadata] path = "../metadata" -version = "0.2.0" +version = "0.3.1" [dependencies] futures-executor = "0.3" @@ -25,6 +25,7 @@ byteorder = "1.4" shell-words = "1.0.0" tokio = { version = "1", features = ["rt", "rt-multi-thread", "sync"] } zerocopy = { version = "0.3" } +thiserror = { version = "1" } # Backends alsa = { version = "0.5", optional = true } @@ -40,22 +41,21 @@ glib = { version = "0.10", optional = true } # Rodio dependencies rodio = { version = "0.14", optional = true, default-features = false } cpal = { version = "0.13", optional = true } -thiserror = { version = "1", optional = true } # Decoder lewton = "0.10" ogg = "0.8" # Dithering -rand = "0.8" +rand = { version = "0.8", features = ["small_rng"] } rand_distr = "0.4" [features] -alsa-backend = ["alsa", "thiserror"] +alsa-backend = ["alsa"] portaudio-backend = ["portaudio-rs"] pulseaudio-backend = ["libpulse-binding", "libpulse-simple-binding"] jackaudio-backend = ["jack"] -rodio-backend = ["rodio", "cpal", "thiserror"] -rodiojack-backend = ["rodio", "cpal/jack", "thiserror"] +rodio-backend = ["rodio", "cpal"] +rodiojack-backend = ["rodio", "cpal/jack"] sdl-backend = ["sdl2"] gstreamer-backend = ["gstreamer", "gstreamer-app", "glib"] diff --git a/playback/src/audio_backend/alsa.rs b/playback/src/audio_backend/alsa.rs index 7101f96d..e572f953 100644 --- a/playback/src/audio_backend/alsa.rs +++ b/playback/src/audio_backend/alsa.rs @@ -1,4 +1,4 @@ -use super::{Open, Sink, SinkAsBytes}; +use super::{Open, Sink, SinkAsBytes, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; @@ -7,45 +7,94 @@ use alsa::device_name::HintIter; use alsa::pcm::{Access, Format, HwParams, PCM}; use alsa::{Direction, ValueOr}; use std::cmp::min; -use std::io; use std::process::exit; use std::time::Duration; use thiserror::Error; -// 125 ms Period time * 4 periods = 0.5 sec buffer. -const PERIOD_TIME: Duration = Duration::from_millis(125); -const NUM_PERIODS: u32 = 4; +// 0.5 sec buffer. +const PERIOD_TIME: Duration = Duration::from_millis(100); +const BUFFER_TIME: Duration = Duration::from_millis(500); #[derive(Debug, Error)] enum AlsaError { - #[error("AlsaSink, device {device} may be invalid or busy, {err}")] - PcmSetUp { device: String, err: alsa::Error }, - #[error("AlsaSink, device {device} unsupported access type RWInterleaved, {err}")] - UnsupportedAccessType { device: String, err: alsa::Error }, - #[error("AlsaSink, device {device} unsupported format {format:?}, {err}")] + #[error(" Device {device} Unsupported Format {alsa_format:?} ({format:?}), {e}")] UnsupportedFormat { device: String, + alsa_format: Format, format: AudioFormat, - err: alsa::Error, + e: alsa::Error, }, - #[error("AlsaSink, device {device} unsupported sample rate {samplerate}, {err}")] - UnsupportedSampleRate { - device: String, - samplerate: u32, - err: alsa::Error, - }, - #[error("AlsaSink, device {device} unsupported channel count {channel_count}, {err}")] + + #[error(" Device {device} Unsupported Channel Count {channel_count}, {e}")] UnsupportedChannelCount { device: String, channel_count: u8, - err: alsa::Error, + e: alsa::Error, }, - #[error("AlsaSink Hardware Parameters Error, {0}")] + + #[error(" Device {device} Unsupported Sample Rate {samplerate}, {e}")] + UnsupportedSampleRate { + device: String, + samplerate: u32, + e: alsa::Error, + }, + + #[error(" Device {device} Unsupported Access Type RWInterleaved, {e}")] + UnsupportedAccessType { device: String, e: alsa::Error }, + + #[error(" Device {device} May be Invalid, Busy, or Already in Use, {e}")] + PcmSetUp { device: String, e: alsa::Error }, + + #[error(" Failed to Drain PCM Buffer, {0}")] + DrainFailure(alsa::Error), + + #[error(" {0}")] + OnWrite(alsa::Error), + + #[error(" Hardware, {0}")] HwParams(alsa::Error), - #[error("AlsaSink Software Parameters Error, {0}")] + + #[error(" Software, {0}")] SwParams(alsa::Error), - #[error("AlsaSink PCM Error, {0}")] + + #[error(" PCM, {0}")] Pcm(alsa::Error), + + #[error(" Could Not Parse Ouput Name(s) and/or Description(s)")] + Parsing, + + #[error("")] + NotConnected, +} + +impl From for SinkError { + fn from(e: AlsaError) -> SinkError { + use AlsaError::*; + let es = e.to_string(); + match e { + DrainFailure(_) | OnWrite(_) => SinkError::OnWrite(es), + PcmSetUp { .. } => SinkError::ConnectionRefused(es), + NotConnected => SinkError::NotConnected(es), + _ => SinkError::InvalidParams(es), + } + } +} + +impl From for Format { + fn from(f: AudioFormat) -> Format { + use AudioFormat::*; + match f { + F64 => Format::float64(), + F32 => Format::float(), + S32 => Format::s32(), + S24 => Format::s24(), + S16 => Format::s16(), + #[cfg(target_endian = "little")] + S24_3 => Format::S243LE, + #[cfg(target_endian = "big")] + S24_3 => Format::S243BE, + } + } } pub struct AlsaSink { @@ -55,26 +104,50 @@ pub struct AlsaSink { period_buffer: Vec, } -fn list_outputs() -> io::Result<()> { - println!("Listing available Alsa outputs:"); - for t in &["pcm", "ctl", "hwdep"] { - println!("{} devices:", t); - let i = match HintIter::new_str(None, &t) { - Ok(i) => i, - Err(e) => { - return Err(io::Error::new(io::ErrorKind::Other, e)); - } - }; - for a in i { - if let Some(Direction::Playback) = a.direction { - // mimic aplay -L - let name = a - .name - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not parse name"))?; - let desc = a - .desc - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Could not parse desc"))?; - println!("{}\n\t{}\n", name, desc.replace("\n", "\n\t")); +fn list_compatible_devices() -> SinkResult<()> { + println!("\n\n\tCompatible alsa device(s):\n"); + println!("\t------------------------------------------------------\n"); + + let i = HintIter::new_str(None, "pcm").map_err(|_| AlsaError::Parsing)?; + + for a in i { + if let Some(Direction::Playback) = a.direction { + let name = a.name.ok_or(AlsaError::Parsing)?; + let desc = a.desc.ok_or(AlsaError::Parsing)?; + + if let Ok(pcm) = PCM::new(&name, Direction::Playback, false) { + if let Ok(hwp) = HwParams::any(&pcm) { + // Only show devices that support + // 2 ch 44.1 Interleaved. + if hwp.set_access(Access::RWInterleaved).is_ok() + && hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).is_ok() + && hwp.set_channels(NUM_CHANNELS as u32).is_ok() + { + println!("\tDevice:\n\n\t\t{}\n", name); + println!("\tDescription:\n\n\t\t{}\n", desc.replace("\n", "\n\t\t")); + + let mut supported_formats = vec![]; + + for f in &[ + AudioFormat::S16, + AudioFormat::S24, + AudioFormat::S24_3, + AudioFormat::S32, + AudioFormat::F32, + AudioFormat::F64, + ] { + if hwp.test_format(Format::from(*f)).is_ok() { + supported_formats.push(format!("{:?}", f)); + } + } + + println!( + "\tSupported Format(s):\n\n\t\t{}\n", + supported_formats.join(" ") + ); + println!("\t------------------------------------------------------\n"); + } + }; } } } @@ -82,45 +155,36 @@ fn list_outputs() -> io::Result<()> { Ok(()) } -fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), AlsaError> { +fn open_device(dev_name: &str, format: AudioFormat) -> SinkResult<(PCM, usize)> { let pcm = PCM::new(dev_name, Direction::Playback, false).map_err(|e| AlsaError::PcmSetUp { device: dev_name.to_string(), - err: e, + e, })?; - let alsa_format = match format { - AudioFormat::F64 => Format::float64(), - AudioFormat::F32 => Format::float(), - AudioFormat::S32 => Format::s32(), - AudioFormat::S24 => Format::s24(), - AudioFormat::S16 => Format::s16(), - - #[cfg(target_endian = "little")] - AudioFormat::S24_3 => Format::S243LE, - #[cfg(target_endian = "big")] - AudioFormat::S24_3 => Format::S243BE, - }; - let bytes_per_period = { let hwp = HwParams::any(&pcm).map_err(AlsaError::HwParams)?; + hwp.set_access(Access::RWInterleaved) .map_err(|e| AlsaError::UnsupportedAccessType { device: dev_name.to_string(), - err: e, + e, })?; + let alsa_format = Format::from(format); + hwp.set_format(alsa_format) .map_err(|e| AlsaError::UnsupportedFormat { device: dev_name.to_string(), + alsa_format, format, - err: e, + e, })?; hwp.set_rate(SAMPLE_RATE, ValueOr::Nearest).map_err(|e| { AlsaError::UnsupportedSampleRate { device: dev_name.to_string(), samplerate: SAMPLE_RATE, - err: e, + e, } })?; @@ -128,11 +192,10 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), Alsa .map_err(|e| AlsaError::UnsupportedChannelCount { device: dev_name.to_string(), channel_count: NUM_CHANNELS, - err: e, + e, })?; - // Deal strictly in time and periods. - hwp.set_periods(NUM_PERIODS, ValueOr::Nearest) + hwp.set_buffer_time_near(BUFFER_TIME.as_micros() as u32, ValueOr::Nearest) .map_err(AlsaError::HwParams)?; hwp.set_period_time_near(PERIOD_TIME.as_micros() as u32, ValueOr::Nearest) @@ -142,8 +205,7 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), Alsa let swp = pcm.sw_params_current().map_err(AlsaError::Pcm)?; - // Don't assume we got what we wanted. - // Ask to make sure. + // Don't assume we got what we wanted. Ask to make sure. let frames_per_period = hwp.get_period_size().map_err(AlsaError::HwParams)?; let frames_per_buffer = hwp.get_buffer_size().map_err(AlsaError::HwParams)?; @@ -153,22 +215,27 @@ fn open_device(dev_name: &str, format: AudioFormat) -> Result<(PCM, usize), Alsa pcm.sw_params(&swp).map_err(AlsaError::Pcm)?; + trace!("Frames per Buffer: {:?}", frames_per_buffer); + trace!("Frames per Period: {:?}", frames_per_period); + // Let ALSA do the math for us. pcm.frames_to_bytes(frames_per_period) as usize }; + trace!("Period Buffer size in bytes: {:?}", bytes_per_period); + Ok((pcm, bytes_per_period)) } impl Open for AlsaSink { fn open(device: Option, format: AudioFormat) -> Self { let name = match device.as_deref() { - Some("?") => match list_outputs() { + Some("?") => match list_compatible_devices() { Ok(_) => { exit(0); } - Err(err) => { - error!("Error listing Alsa outputs, {}", err); + Err(e) => { + error!("{}", e); exit(1); } }, @@ -189,38 +256,35 @@ impl Open for AlsaSink { } impl Sink for AlsaSink { - fn start(&mut self) -> io::Result<()> { + fn start(&mut self) -> SinkResult<()> { if self.pcm.is_none() { - match open_device(&self.device, self.format) { - Ok((pcm, bytes_per_period)) => { - self.pcm = Some(pcm); - self.period_buffer = Vec::with_capacity(bytes_per_period); - } - Err(e) => { - return Err(io::Error::new(io::ErrorKind::Other, e)); - } + let (pcm, bytes_per_period) = open_device(&self.device, self.format)?; + self.pcm = Some(pcm); + + if self.period_buffer.capacity() != bytes_per_period { + self.period_buffer = Vec::with_capacity(bytes_per_period); } + + // Should always match the "Period Buffer size in bytes: " trace! message. + trace!( + "Period Buffer capacity: {:?}", + self.period_buffer.capacity() + ); } Ok(()) } - fn stop(&mut self) -> io::Result<()> { - { - // Write any leftover data in the period buffer - // before draining the actual buffer - self.write_bytes(&[])?; - let pcm = self.pcm.as_mut().ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "Error stopping AlsaSink, PCM is None") - })?; - pcm.drain().map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!("Error stopping AlsaSink {}", e), - ) - })? - } - self.pcm = None; + fn stop(&mut self) -> SinkResult<()> { + // Zero fill the remainder of the period buffer and + // write any leftover data before draining the actual PCM buffer. + self.period_buffer.resize(self.period_buffer.capacity(), 0); + self.write_buf()?; + + let pcm = self.pcm.take().ok_or(AlsaError::NotConnected)?; + + pcm.drain().map_err(AlsaError::DrainFailure)?; + Ok(()) } @@ -228,55 +292,51 @@ impl Sink for AlsaSink { } impl SinkAsBytes for AlsaSink { - fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> { - let mut processed_data = 0; - while processed_data < data.len() { - let data_to_buffer = min( - self.period_buffer.capacity() - self.period_buffer.len(), - data.len() - processed_data, - ); - self.period_buffer - .extend_from_slice(&data[processed_data..processed_data + data_to_buffer]); - processed_data += data_to_buffer; - if self.period_buffer.len() == self.period_buffer.capacity() { - self.write_buf()?; - self.period_buffer.clear(); - } - } + fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> { + let mut start_index = 0; + let data_len = data.len(); + let capacity = self.period_buffer.capacity(); - Ok(()) + loop { + let data_left = data_len - start_index; + let space_left = capacity - self.period_buffer.len(); + let data_to_buffer = min(data_left, space_left); + let end_index = start_index + data_to_buffer; + + self.period_buffer + .extend_from_slice(&data[start_index..end_index]); + + if self.period_buffer.len() == capacity { + self.write_buf()?; + } + + if end_index == data_len { + break Ok(()); + } + + start_index = end_index; + } } } impl AlsaSink { pub const NAME: &'static str = "alsa"; - fn write_buf(&mut self) -> io::Result<()> { - let pcm = self.pcm.as_mut().ok_or_else(|| { - io::Error::new( - io::ErrorKind::Other, - "Error writing from AlsaSink buffer to PCM, PCM is None", - ) - })?; - let io = pcm.io_bytes(); - if let Err(err) = io.writei(&self.period_buffer) { + fn write_buf(&mut self) -> SinkResult<()> { + let pcm = self.pcm.as_mut().ok_or(AlsaError::NotConnected)?; + + if let Err(e) = pcm.io_bytes().writei(&self.period_buffer) { // Capture and log the original error as a warning, and then try to recover. // If recovery fails then forward that error back to player. warn!( - "Error writing from AlsaSink buffer to PCM, trying to recover {}", - err + "Error writing from AlsaSink buffer to PCM, trying to recover, {}", + e ); - pcm.try_recover(err, false).map_err(|e| { - io::Error::new( - io::ErrorKind::Other, - format!( - "Error writing from AlsaSink buffer to PCM, recovery failed {}", - e - ), - ) - })? + + pcm.try_recover(e, false).map_err(AlsaError::OnWrite)? } + self.period_buffer.clear(); Ok(()) } } diff --git a/playback/src/audio_backend/gstreamer.rs b/playback/src/audio_backend/gstreamer.rs index 58f6cbc9..8b957577 100644 --- a/playback/src/audio_backend/gstreamer.rs +++ b/playback/src/audio_backend/gstreamer.rs @@ -1,4 +1,4 @@ -use super::{Open, Sink, SinkAsBytes}; +use super::{Open, Sink, SinkAsBytes, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; @@ -11,7 +11,7 @@ use gst::prelude::*; use zerocopy::AsBytes; use std::sync::mpsc::{sync_channel, SyncSender}; -use std::{io, thread}; +use std::thread; #[allow(dead_code)] pub struct GstreamerSink { @@ -131,7 +131,7 @@ impl Sink for GstreamerSink { } impl SinkAsBytes for GstreamerSink { - fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> { + fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> { // Copy expensively (in to_vec()) to avoid thread synchronization self.tx .send(data.to_vec()) diff --git a/playback/src/audio_backend/jackaudio.rs b/playback/src/audio_backend/jackaudio.rs index f55f20a8..15acf99d 100644 --- a/playback/src/audio_backend/jackaudio.rs +++ b/playback/src/audio_backend/jackaudio.rs @@ -1,4 +1,4 @@ -use super::{Open, Sink}; +use super::{Open, Sink, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; @@ -6,7 +6,6 @@ use crate::NUM_CHANNELS; use jack::{ AsyncClient, AudioOut, Client, ClientOptions, Control, Port, ProcessHandler, ProcessScope, }; -use std::io; use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; pub struct JackSink { @@ -25,15 +24,12 @@ pub struct JackData { impl ProcessHandler for JackData { fn process(&mut self, _: &Client, ps: &ProcessScope) -> Control { // get output port buffers - let mut out_r = self.port_r.as_mut_slice(ps); - let mut out_l = self.port_l.as_mut_slice(ps); - let buf_r: &mut [f32] = &mut out_r; - let buf_l: &mut [f32] = &mut out_l; + let buf_r: &mut [f32] = self.port_r.as_mut_slice(ps); + let buf_l: &mut [f32] = self.port_l.as_mut_slice(ps); // get queue iterator let mut queue_iter = self.rec.try_iter(); - let buf_size = buf_r.len(); - for i in 0..buf_size { + for i in 0..buf_r.len() { buf_r[i] = queue_iter.next().unwrap_or(0.0); buf_l[i] = queue_iter.next().unwrap_or(0.0); } @@ -70,8 +66,12 @@ impl Open for JackSink { } impl Sink for JackSink { - fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> { - let samples_f32: &[f32] = &converter.f64_to_f32(packet.samples()); + fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> SinkResult<()> { + let samples = packet + .samples() + .map_err(|e| SinkError::OnWrite(e.to_string()))?; + + let samples_f32: &[f32] = &converter.f64_to_f32(samples); for sample in samples_f32.iter() { let res = self.send.send(*sample); if res.is_err() { diff --git a/playback/src/audio_backend/mod.rs b/playback/src/audio_backend/mod.rs index 31fb847c..dc21fb3d 100644 --- a/playback/src/audio_backend/mod.rs +++ b/playback/src/audio_backend/mod.rs @@ -1,26 +1,40 @@ use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; -use std::io; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum SinkError { + #[error("Audio Sink Error Not Connected: {0}")] + NotConnected(String), + #[error("Audio Sink Error Connection Refused: {0}")] + ConnectionRefused(String), + #[error("Audio Sink Error On Write: {0}")] + OnWrite(String), + #[error("Audio Sink Error Invalid Parameters: {0}")] + InvalidParams(String), +} + +pub type SinkResult = Result; pub trait Open { fn open(_: Option, format: AudioFormat) -> Self; } pub trait Sink { - fn start(&mut self) -> io::Result<()> { + fn start(&mut self) -> SinkResult<()> { Ok(()) } - fn stop(&mut self) -> io::Result<()> { + fn stop(&mut self) -> SinkResult<()> { Ok(()) } - fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()>; + fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> SinkResult<()>; } pub type SinkBuilder = fn(Option, AudioFormat) -> Box; pub trait SinkAsBytes { - fn write_bytes(&mut self, data: &[u8]) -> io::Result<()>; + fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()>; } fn mk_sink(device: Option, format: AudioFormat) -> Box { @@ -30,7 +44,7 @@ fn mk_sink(device: Option, format: AudioFormat // reuse code for various backends macro_rules! sink_as_bytes { () => { - fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> { + fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> SinkResult<()> { use crate::convert::i24; use zerocopy::AsBytes; match packet { @@ -90,7 +104,7 @@ use self::gstreamer::GstreamerSink; #[cfg(any(feature = "rodio-backend", feature = "rodiojack-backend"))] mod rodio; -#[cfg(any(feature = "rodio-backend", feature = "rodiojack-backend"))] +#[cfg(feature = "rodio-backend")] use self::rodio::RodioSink; #[cfg(feature = "sdl-backend")] @@ -132,11 +146,6 @@ pub fn find(name: Option) -> Option { .find(|backend| name == backend.0) .map(|backend| backend.1) } else { - Some( - BACKENDS - .first() - .expect("No backends were enabled at build time") - .1, - ) + BACKENDS.first().map(|backend| backend.1) } } diff --git a/playback/src/audio_backend/pipe.rs b/playback/src/audio_backend/pipe.rs index 56040384..fd804a0e 100644 --- a/playback/src/audio_backend/pipe.rs +++ b/playback/src/audio_backend/pipe.rs @@ -1,4 +1,4 @@ -use super::{Open, Sink, SinkAsBytes}; +use super::{Open, Sink, SinkAsBytes, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; @@ -23,14 +23,14 @@ impl Open for StdoutSink { } impl Sink for StdoutSink { - fn start(&mut self) -> io::Result<()> { + fn start(&mut self) -> SinkResult<()> { if self.output.is_none() { let output: Box = match self.path.as_deref() { Some(path) => { let open_op = OpenOptions::new() .write(true) .open(path) - .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; + .map_err(|e| SinkError::ConnectionRefused(e.to_string()))?; Box::new(open_op) } None => Box::new(io::stdout()), @@ -46,14 +46,18 @@ impl Sink for StdoutSink { } impl SinkAsBytes for StdoutSink { - fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> { + fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> { match self.output.as_deref_mut() { Some(output) => { - output.write_all(data)?; - output.flush()?; + output + .write_all(data) + .map_err(|e| SinkError::OnWrite(e.to_string()))?; + output + .flush() + .map_err(|e| SinkError::OnWrite(e.to_string()))?; } None => { - return Err(io::Error::new(io::ErrorKind::Other, "Output is None")); + return Err(SinkError::NotConnected("Output is None".to_string())); } } diff --git a/playback/src/audio_backend/portaudio.rs b/playback/src/audio_backend/portaudio.rs index 378deb48..7a0b179f 100644 --- a/playback/src/audio_backend/portaudio.rs +++ b/playback/src/audio_backend/portaudio.rs @@ -1,11 +1,10 @@ -use super::{Open, Sink}; +use super::{Open, Sink, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; use crate::{NUM_CHANNELS, SAMPLE_RATE}; use portaudio_rs::device::{get_default_output_index, DeviceIndex, DeviceInfo}; use portaudio_rs::stream::*; -use std::io; use std::process::exit; use std::time::Duration; @@ -96,7 +95,7 @@ impl<'a> Open for PortAudioSink<'a> { } impl<'a> Sink for PortAudioSink<'a> { - fn start(&mut self) -> io::Result<()> { + fn start(&mut self) -> SinkResult<()> { macro_rules! start_sink { (ref mut $stream: ident, ref $parameters: ident) => {{ if $stream.is_none() { @@ -125,7 +124,7 @@ impl<'a> Sink for PortAudioSink<'a> { Ok(()) } - fn stop(&mut self) -> io::Result<()> { + fn stop(&mut self) -> SinkResult<()> { macro_rules! stop_sink { (ref mut $stream: ident) => {{ $stream.as_mut().unwrap().stop().unwrap(); @@ -141,14 +140,17 @@ impl<'a> Sink for PortAudioSink<'a> { Ok(()) } - fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> { + fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> SinkResult<()> { macro_rules! write_sink { (ref mut $stream: expr, $samples: expr) => { $stream.as_mut().unwrap().write($samples) }; } - let samples = packet.samples(); + let samples = packet + .samples() + .map_err(|e| SinkError::OnWrite(e.to_string()))?; + let result = match self { Self::F32(stream, _parameters) => { let samples_f32: &[f32] = &converter.f64_to_f32(samples); diff --git a/playback/src/audio_backend/pulseaudio.rs b/playback/src/audio_backend/pulseaudio.rs index e36941ea..7487517f 100644 --- a/playback/src/audio_backend/pulseaudio.rs +++ b/playback/src/audio_backend/pulseaudio.rs @@ -1,84 +1,129 @@ -use super::{Open, Sink, SinkAsBytes}; +use super::{Open, Sink, SinkAsBytes, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; use crate::{NUM_CHANNELS, SAMPLE_RATE}; -use libpulse_binding::{self as pulse, stream::Direction}; +use libpulse_binding::{self as pulse, error::PAErr, stream::Direction}; use libpulse_simple_binding::Simple; -use std::io; +use thiserror::Error; const APP_NAME: &str = "librespot"; const STREAM_NAME: &str = "Spotify endpoint"; +#[derive(Debug, Error)] +enum PulseError { + #[error(" Unsupported Pulseaudio Sample Spec, Format {pulse_format:?} ({format:?}), Channels {channels}, Rate {rate}")] + InvalidSampleSpec { + pulse_format: pulse::sample::Format, + format: AudioFormat, + channels: u8, + rate: u32, + }, + + #[error(" {0}")] + ConnectionRefused(PAErr), + + #[error(" Failed to Drain Pulseaudio Buffer, {0}")] + DrainFailure(PAErr), + + #[error("")] + NotConnected, + + #[error(" {0}")] + OnWrite(PAErr), +} + +impl From for SinkError { + fn from(e: PulseError) -> SinkError { + use PulseError::*; + let es = e.to_string(); + match e { + DrainFailure(_) | OnWrite(_) => SinkError::OnWrite(es), + ConnectionRefused(_) => SinkError::ConnectionRefused(es), + NotConnected => SinkError::NotConnected(es), + InvalidSampleSpec { .. } => SinkError::InvalidParams(es), + } + } +} + pub struct PulseAudioSink { s: Option, - ss: pulse::sample::Spec, device: Option, format: AudioFormat, } impl Open for PulseAudioSink { fn open(device: Option, format: AudioFormat) -> Self { - info!("Using PulseAudio sink with format: {:?}", format); + let mut actual_format = format; - // PulseAudio calls S24 and S24_3 different from the rest of the world - let pulse_format = match format { - AudioFormat::F32 => pulse::sample::Format::FLOAT32NE, - AudioFormat::S32 => pulse::sample::Format::S32NE, - AudioFormat::S24 => pulse::sample::Format::S24_32NE, - AudioFormat::S24_3 => pulse::sample::Format::S24NE, - AudioFormat::S16 => pulse::sample::Format::S16NE, - _ => { - unimplemented!("PulseAudio currently does not support {:?} output", format) - } - }; + if actual_format == AudioFormat::F64 { + warn!("PulseAudio currently does not support F64 output"); + actual_format = AudioFormat::F32; + } - let ss = pulse::sample::Spec { - format: pulse_format, - channels: NUM_CHANNELS, - rate: SAMPLE_RATE, - }; - debug_assert!(ss.is_valid()); + info!("Using PulseAudioSink with format: {:?}", actual_format); Self { s: None, - ss, device, - format, + format: actual_format, } } } impl Sink for PulseAudioSink { - fn start(&mut self) -> io::Result<()> { - if self.s.is_some() { - return Ok(()); + fn start(&mut self) -> SinkResult<()> { + if self.s.is_none() { + // PulseAudio calls S24 and S24_3 different from the rest of the world + let pulse_format = match self.format { + AudioFormat::F32 => pulse::sample::Format::FLOAT32NE, + AudioFormat::S32 => pulse::sample::Format::S32NE, + AudioFormat::S24 => pulse::sample::Format::S24_32NE, + AudioFormat::S24_3 => pulse::sample::Format::S24NE, + AudioFormat::S16 => pulse::sample::Format::S16NE, + _ => unreachable!(), + }; + + let ss = pulse::sample::Spec { + format: pulse_format, + channels: NUM_CHANNELS, + rate: SAMPLE_RATE, + }; + + if !ss.is_valid() { + let pulse_error = PulseError::InvalidSampleSpec { + pulse_format, + format: self.format, + channels: NUM_CHANNELS, + rate: SAMPLE_RATE, + }; + + return Err(SinkError::from(pulse_error)); + } + + let s = Simple::new( + None, // Use the default server. + APP_NAME, // Our application's name. + Direction::Playback, // Direction. + self.device.as_deref(), // Our device (sink) name. + STREAM_NAME, // Description of our stream. + &ss, // Our sample format. + None, // Use default channel map. + None, // Use default buffering attributes. + ) + .map_err(PulseError::ConnectionRefused)?; + + self.s = Some(s); } - let device = self.device.as_deref(); - let result = Simple::new( - None, // Use the default server. - APP_NAME, // Our application's name. - Direction::Playback, // Direction. - device, // Our device (sink) name. - STREAM_NAME, // Description of our stream. - &self.ss, // Our sample format. - None, // Use default channel map. - None, // Use default buffering attributes. - ); - match result { - Ok(s) => { - self.s = Some(s); - Ok(()) - } - Err(e) => Err(io::Error::new( - io::ErrorKind::ConnectionRefused, - e.to_string().unwrap(), - )), - } + Ok(()) } - fn stop(&mut self) -> io::Result<()> { + fn stop(&mut self) -> SinkResult<()> { + let s = self.s.as_mut().ok_or(PulseError::NotConnected)?; + + s.drain().map_err(PulseError::DrainFailure)?; + self.s = None; Ok(()) } @@ -87,21 +132,12 @@ impl Sink for PulseAudioSink { } impl SinkAsBytes for PulseAudioSink { - fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> { - if let Some(s) = &self.s { - match s.write(data) { - Ok(_) => Ok(()), - Err(e) => Err(io::Error::new( - io::ErrorKind::BrokenPipe, - e.to_string().unwrap(), - )), - } - } else { - Err(io::Error::new( - io::ErrorKind::NotConnected, - "Not connected to PulseAudio", - )) - } + fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> { + let s = self.s.as_mut().ok_or(PulseError::NotConnected)?; + + s.write(data).map_err(PulseError::OnWrite)?; + + Ok(()) } } diff --git a/playback/src/audio_backend/rodio.rs b/playback/src/audio_backend/rodio.rs index 1e999938..ab356d67 100644 --- a/playback/src/audio_backend/rodio.rs +++ b/playback/src/audio_backend/rodio.rs @@ -1,11 +1,11 @@ use std::process::exit; +use std::thread; use std::time::Duration; -use std::{io, thread}; use cpal::traits::{DeviceTrait, HostTrait}; use thiserror::Error; -use super::Sink; +use super::{Sink, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; @@ -33,16 +33,30 @@ pub fn mk_rodiojack(device: Option, format: AudioFormat) -> Box No Device Available")] NoDeviceAvailable, - #[error("Rodio: device \"{0}\" is not available")] + #[error(" device \"{0}\" is Not Available")] DeviceNotAvailable(String), - #[error("Rodio play error: {0}")] + #[error(" Play Error: {0}")] PlayError(#[from] rodio::PlayError), - #[error("Rodio stream error: {0}")] + #[error(" Stream Error: {0}")] StreamError(#[from] rodio::StreamError), - #[error("Cannot get audio devices: {0}")] + #[error(" Cannot Get Audio Devices: {0}")] DevicesError(#[from] cpal::DevicesError), + #[error(" {0}")] + Samples(String), +} + +impl From for SinkError { + fn from(e: RodioError) -> SinkError { + use RodioError::*; + let es = e.to_string(); + match e { + StreamError(_) | PlayError(_) | Samples(_) => SinkError::OnWrite(es), + NoDeviceAvailable | DeviceNotAvailable(_) => SinkError::ConnectionRefused(es), + DevicesError(_) => SinkError::InvalidParams(es), + } + } } pub struct RodioSink { @@ -175,8 +189,10 @@ pub fn open(host: cpal::Host, device: Option, format: AudioFormat) -> Ro } impl Sink for RodioSink { - fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> { - let samples = packet.samples(); + fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> SinkResult<()> { + let samples = packet + .samples() + .map_err(|e| RodioError::Samples(e.to_string()))?; match self.format { AudioFormat::F32 => { let samples_f32: &[f32] = &converter.f64_to_f32(samples); @@ -211,5 +227,6 @@ impl Sink for RodioSink { } impl RodioSink { + #[allow(dead_code)] pub const NAME: &'static str = "rodio"; } diff --git a/playback/src/audio_backend/sdl.rs b/playback/src/audio_backend/sdl.rs index 28d140e8..6272fa32 100644 --- a/playback/src/audio_backend/sdl.rs +++ b/playback/src/audio_backend/sdl.rs @@ -1,11 +1,11 @@ -use super::{Open, Sink}; +use super::{Open, Sink, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; use crate::{NUM_CHANNELS, SAMPLE_RATE}; use sdl2::audio::{AudioQueue, AudioSpecDesired}; +use std::thread; use std::time::Duration; -use std::{io, thread}; pub enum SdlSink { F32(AudioQueue), @@ -52,7 +52,7 @@ impl Open for SdlSink { } impl Sink for SdlSink { - fn start(&mut self) -> io::Result<()> { + fn start(&mut self) -> SinkResult<()> { macro_rules! start_sink { ($queue: expr) => {{ $queue.clear(); @@ -67,7 +67,7 @@ impl Sink for SdlSink { Ok(()) } - fn stop(&mut self) -> io::Result<()> { + fn stop(&mut self) -> SinkResult<()> { macro_rules! stop_sink { ($queue: expr) => {{ $queue.pause(); @@ -82,7 +82,7 @@ impl Sink for SdlSink { Ok(()) } - fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> io::Result<()> { + fn write(&mut self, packet: &AudioPacket, converter: &mut Converter) -> SinkResult<()> { macro_rules! drain_sink { ($queue: expr, $size: expr) => {{ // sleep and wait for sdl thread to drain the queue a bit @@ -92,7 +92,9 @@ impl Sink for SdlSink { }}; } - let samples = packet.samples(); + let samples = packet + .samples() + .map_err(|e| SinkError::OnWrite(e.to_string()))?; match self { Self::F32(queue) => { let samples_f32: &[f32] = &converter.f64_to_f32(samples); diff --git a/playback/src/audio_backend/subprocess.rs b/playback/src/audio_backend/subprocess.rs index 64f04c88..c501cf83 100644 --- a/playback/src/audio_backend/subprocess.rs +++ b/playback/src/audio_backend/subprocess.rs @@ -1,10 +1,10 @@ -use super::{Open, Sink, SinkAsBytes}; +use super::{Open, Sink, SinkAsBytes, SinkError, SinkResult}; use crate::config::AudioFormat; use crate::convert::Converter; use crate::decoder::AudioPacket; use shell_words::split; -use std::io::{self, Write}; +use std::io::Write; use std::process::{Child, Command, Stdio}; pub struct SubprocessSink { @@ -30,21 +30,25 @@ impl Open for SubprocessSink { } impl Sink for SubprocessSink { - fn start(&mut self) -> io::Result<()> { + fn start(&mut self) -> SinkResult<()> { let args = split(&self.shell_command).unwrap(); - self.child = Some( - Command::new(&args[0]) - .args(&args[1..]) - .stdin(Stdio::piped()) - .spawn()?, - ); + let child = Command::new(&args[0]) + .args(&args[1..]) + .stdin(Stdio::piped()) + .spawn() + .map_err(|e| SinkError::ConnectionRefused(e.to_string()))?; + self.child = Some(child); Ok(()) } - fn stop(&mut self) -> io::Result<()> { + fn stop(&mut self) -> SinkResult<()> { if let Some(child) = &mut self.child.take() { - child.kill()?; - child.wait()?; + child + .kill() + .map_err(|e| SinkError::OnWrite(e.to_string()))?; + child + .wait() + .map_err(|e| SinkError::OnWrite(e.to_string()))?; } Ok(()) } @@ -53,11 +57,18 @@ impl Sink for SubprocessSink { } impl SinkAsBytes for SubprocessSink { - fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> { + fn write_bytes(&mut self, data: &[u8]) -> SinkResult<()> { if let Some(child) = &mut self.child { - let child_stdin = child.stdin.as_mut().unwrap(); - child_stdin.write_all(data)?; - child_stdin.flush()?; + let child_stdin = child + .stdin + .as_mut() + .ok_or_else(|| SinkError::NotConnected("Child is None".to_string()))?; + child_stdin + .write_all(data) + .map_err(|e| SinkError::OnWrite(e.to_string()))?; + child_stdin + .flush() + .map_err(|e| SinkError::OnWrite(e.to_string()))?; } Ok(()) } diff --git a/playback/src/config.rs b/playback/src/config.rs index 7604f59f..b8313bf4 100644 --- a/playback/src/config.rs +++ b/playback/src/config.rs @@ -76,10 +76,11 @@ impl AudioFormat { } } -#[derive(Clone, Debug)] +#[derive(Clone, Copy, Debug, PartialEq)] pub enum NormalisationType { Album, Track, + Auto, } impl FromStr for NormalisationType { @@ -88,6 +89,7 @@ impl FromStr for NormalisationType { match s.to_lowercase().as_ref() { "album" => Ok(Self::Album), "track" => Ok(Self::Track), + "auto" => Ok(Self::Auto), _ => Err(()), } } @@ -95,11 +97,11 @@ impl FromStr for NormalisationType { impl Default for NormalisationType { fn default() -> Self { - Self::Album + Self::Auto } } -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq)] pub enum NormalisationMethod { Basic, Dynamic, @@ -151,7 +153,7 @@ impl Default for PlayerConfig { normalisation_type: NormalisationType::default(), normalisation_method: NormalisationMethod::default(), normalisation_pregain: 0.0, - normalisation_threshold: db_to_ratio(-1.0), + normalisation_threshold: db_to_ratio(-2.0), normalisation_attack: Duration::from_millis(5), normalisation_release: Duration::from_millis(100), normalisation_knee: 1.0, diff --git a/playback/src/decoder/lewton_decoder.rs b/playback/src/decoder/lewton_decoder.rs index adf63e2a..bc90b992 100644 --- a/playback/src/decoder/lewton_decoder.rs +++ b/playback/src/decoder/lewton_decoder.rs @@ -1,22 +1,23 @@ -use super::{AudioDecoder, AudioError, AudioPacket}; +use super::{AudioDecoder, AudioPacket, DecoderError, DecoderResult}; +use lewton::audio::AudioReadError::AudioIsHeader; use lewton::inside_ogg::OggStreamReader; use lewton::samples::InterleavedSamples; +use lewton::OggReadError::NoCapturePatternFound; +use lewton::VorbisError::{BadAudio, OggError}; -use std::error; -use std::fmt; use std::io::{Read, Seek}; -use std::time::Duration; pub struct VorbisDecoder(OggStreamReader); -pub struct VorbisError(lewton::VorbisError); impl VorbisDecoder where R: Read + Seek, { - pub fn new(input: R) -> Result, VorbisError> { - Ok(VorbisDecoder(OggStreamReader::new(input)?)) + pub fn new(input: R) -> DecoderResult> { + let reader = + OggStreamReader::new(input).map_err(|e| DecoderError::LewtonDecoder(e.to_string()))?; + Ok(VorbisDecoder(reader)) } } @@ -24,51 +25,22 @@ impl AudioDecoder for VorbisDecoder where R: Read + Seek, { - fn seek(&mut self, ms: i64) -> Result<(), AudioError> { - let absgp = Duration::from_millis(ms as u64 * crate::SAMPLE_RATE as u64).as_secs(); - match self.0.seek_absgp_pg(absgp as u64) { - Ok(_) => Ok(()), - Err(err) => Err(AudioError::VorbisError(err.into())), - } + fn seek(&mut self, absgp: u64) -> DecoderResult<()> { + self.0 + .seek_absgp_pg(absgp) + .map_err(|e| DecoderError::LewtonDecoder(e.to_string()))?; + Ok(()) } - fn next_packet(&mut self) -> Result, AudioError> { - use lewton::audio::AudioReadError::AudioIsHeader; - use lewton::OggReadError::NoCapturePatternFound; - use lewton::VorbisError::{BadAudio, OggError}; + fn next_packet(&mut self) -> DecoderResult> { loop { match self.0.read_dec_packet_generic::>() { Ok(Some(packet)) => return Ok(Some(AudioPacket::samples_from_f32(packet.samples))), Ok(None) => return Ok(None), - Err(BadAudio(AudioIsHeader)) => (), Err(OggError(NoCapturePatternFound)) => (), - Err(err) => return Err(AudioError::VorbisError(err.into())), + Err(e) => return Err(DecoderError::LewtonDecoder(e.to_string())), } } } } - -impl From for VorbisError { - fn from(err: lewton::VorbisError) -> VorbisError { - VorbisError(err) - } -} - -impl fmt::Debug for VorbisError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } -} - -impl fmt::Display for VorbisError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(&self.0, f) - } -} - -impl error::Error for VorbisError { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - error::Error::source(&self.0) - } -} diff --git a/playback/src/decoder/mod.rs b/playback/src/decoder/mod.rs index 9641e8b3..087bba4c 100644 --- a/playback/src/decoder/mod.rs +++ b/playback/src/decoder/mod.rs @@ -1,10 +1,30 @@ -use std::fmt; +use thiserror::Error; mod lewton_decoder; -pub use lewton_decoder::{VorbisDecoder, VorbisError}; +pub use lewton_decoder::VorbisDecoder; mod passthrough_decoder; -pub use passthrough_decoder::{PassthroughDecoder, PassthroughError}; +pub use passthrough_decoder::PassthroughDecoder; + +#[derive(Error, Debug)] +pub enum DecoderError { + #[error("Lewton Decoder Error: {0}")] + LewtonDecoder(String), + #[error("Passthrough Decoder Error: {0}")] + PassthroughDecoder(String), +} + +pub type DecoderResult = Result; + +#[derive(Error, Debug)] +pub enum AudioPacketError { + #[error("Decoder OggData Error: Can't return OggData on Samples")] + OggData, + #[error("Decoder Samples Error: Can't return Samples on OggData")] + Samples, +} + +pub type AudioPacketResult = Result; pub enum AudioPacket { Samples(Vec), @@ -17,17 +37,17 @@ impl AudioPacket { AudioPacket::Samples(f64_samples) } - pub fn samples(&self) -> &[f64] { + pub fn samples(&self) -> AudioPacketResult<&[f64]> { match self { - AudioPacket::Samples(s) => s, - AudioPacket::OggData(_) => panic!("can't return OggData on samples"), + AudioPacket::Samples(s) => Ok(s), + AudioPacket::OggData(_) => Err(AudioPacketError::OggData), } } - pub fn oggdata(&self) -> &[u8] { + pub fn oggdata(&self) -> AudioPacketResult<&[u8]> { match self { - AudioPacket::Samples(_) => panic!("can't return samples on OggData"), - AudioPacket::OggData(d) => d, + AudioPacket::OggData(d) => Ok(d), + AudioPacket::Samples(_) => Err(AudioPacketError::Samples), } } @@ -39,34 +59,7 @@ impl AudioPacket { } } -#[derive(Debug)] -pub enum AudioError { - PassthroughError(PassthroughError), - VorbisError(VorbisError), -} - -impl fmt::Display for AudioError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - AudioError::PassthroughError(err) => write!(f, "PassthroughError({})", err), - AudioError::VorbisError(err) => write!(f, "VorbisError({})", err), - } - } -} - -impl From for AudioError { - fn from(err: VorbisError) -> AudioError { - AudioError::VorbisError(err) - } -} - -impl From for AudioError { - fn from(err: PassthroughError) -> AudioError { - AudioError::PassthroughError(err) - } -} - pub trait AudioDecoder { - fn seek(&mut self, ms: i64) -> Result<(), AudioError>; - fn next_packet(&mut self) -> Result, AudioError>; + fn seek(&mut self, absgp: u64) -> DecoderResult<()>; + fn next_packet(&mut self) -> DecoderResult>; } diff --git a/playback/src/decoder/passthrough_decoder.rs b/playback/src/decoder/passthrough_decoder.rs index 7c1ad532..dd8e3b32 100644 --- a/playback/src/decoder/passthrough_decoder.rs +++ b/playback/src/decoder/passthrough_decoder.rs @@ -1,23 +1,22 @@ // Passthrough decoder for librespot -use super::{AudioDecoder, AudioError, AudioPacket}; -use crate::SAMPLE_RATE; +use super::{AudioDecoder, AudioPacket, DecoderError, DecoderResult}; use ogg::{OggReadError, Packet, PacketReader, PacketWriteEndInfo, PacketWriter}; -use std::fmt; use std::io::{Read, Seek}; -use std::time::Duration; use std::time::{SystemTime, UNIX_EPOCH}; -fn get_header(code: u8, rdr: &mut PacketReader) -> Result, PassthroughError> +fn get_header(code: u8, rdr: &mut PacketReader) -> DecoderResult> where T: Read + Seek, { - let pck: Packet = rdr.read_packet_expected()?; + let pck: Packet = rdr + .read_packet_expected() + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; let pkt_type = pck.data[0]; debug!("Vorbis header type {}", &pkt_type); if pkt_type != code { - return Err(PassthroughError(OggReadError::InvalidData)); + return Err(DecoderError::PassthroughDecoder("Invalid Data".to_string())); } Ok(pck.data.into_boxed_slice()) @@ -35,16 +34,14 @@ pub struct PassthroughDecoder { setup: Box<[u8]>, } -pub struct PassthroughError(ogg::OggReadError); - impl PassthroughDecoder { /// Constructs a new Decoder from a given implementation of `Read + Seek`. - pub fn new(rdr: R) -> Result { + pub fn new(rdr: R) -> DecoderResult { let mut rdr = PacketReader::new(rdr); - let stream_serial = SystemTime::now() + let since_epoch = SystemTime::now() .duration_since(UNIX_EPOCH) - .unwrap() - .as_millis() as u32; + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; + let stream_serial = since_epoch.as_millis() as u32; info!("Starting passthrough track with serial {}", stream_serial); @@ -71,9 +68,7 @@ impl PassthroughDecoder { } impl AudioDecoder for PassthroughDecoder { - fn seek(&mut self, ms: i64) -> Result<(), AudioError> { - info!("Seeking to {}", ms); - + fn seek(&mut self, absgp: u64) -> DecoderResult<()> { // add an eos to previous stream if missing if self.bos && !self.eos { match self.rdr.read_packet() { @@ -86,7 +81,7 @@ impl AudioDecoder for PassthroughDecoder { PacketWriteEndInfo::EndStream, absgp_page, ) - .unwrap(); + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; } _ => warn! {"Cannot write EoS after seeking"}, }; @@ -97,23 +92,29 @@ impl AudioDecoder for PassthroughDecoder { self.ofsgp_page = 0; self.stream_serial += 1; - // hard-coded to 44.1 kHz - match self.rdr.seek_absgp( - None, - Duration::from_millis(ms as u64 * SAMPLE_RATE as u64).as_secs(), - ) { + match self.rdr.seek_absgp(None, absgp) { Ok(_) => { // need to set some offset for next_page() - let pck = self.rdr.read_packet().unwrap().unwrap(); - self.ofsgp_page = pck.absgp_page(); - debug!("Seek to offset page {}", self.ofsgp_page); - Ok(()) + let pck = self + .rdr + .read_packet() + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; + match pck { + Some(pck) => { + self.ofsgp_page = pck.absgp_page(); + debug!("Seek to offset page {}", self.ofsgp_page); + Ok(()) + } + None => Err(DecoderError::PassthroughDecoder( + "Packet is None".to_string(), + )), + } } - Err(err) => Err(AudioError::PassthroughError(err.into())), + Err(e) => Err(DecoderError::PassthroughDecoder(e.to_string())), } } - fn next_packet(&mut self) -> Result, AudioError> { + fn next_packet(&mut self) -> DecoderResult> { // write headers if we are (re)starting if !self.bos { self.wtr @@ -123,7 +124,7 @@ impl AudioDecoder for PassthroughDecoder { PacketWriteEndInfo::EndPage, 0, ) - .unwrap(); + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; self.wtr .write_packet( self.comment.clone(), @@ -131,7 +132,7 @@ impl AudioDecoder for PassthroughDecoder { PacketWriteEndInfo::NormalPacket, 0, ) - .unwrap(); + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; self.wtr .write_packet( self.setup.clone(), @@ -139,7 +140,7 @@ impl AudioDecoder for PassthroughDecoder { PacketWriteEndInfo::EndPage, 0, ) - .unwrap(); + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; self.bos = true; debug!("Wrote Ogg headers"); } @@ -151,7 +152,7 @@ impl AudioDecoder for PassthroughDecoder { info!("end of streaming"); return Ok(None); } - Err(err) => return Err(AudioError::PassthroughError(err.into())), + Err(e) => return Err(DecoderError::PassthroughDecoder(e.to_string())), }; let pckgp_page = pck.absgp_page(); @@ -178,32 +179,14 @@ impl AudioDecoder for PassthroughDecoder { inf, pckgp_page - self.ofsgp_page, ) - .unwrap(); + .map_err(|e| DecoderError::PassthroughDecoder(e.to_string()))?; let data = self.wtr.inner_mut(); if !data.is_empty() { - let result = AudioPacket::OggData(std::mem::take(data)); - return Ok(Some(result)); + let ogg_data = AudioPacket::OggData(std::mem::take(data)); + return Ok(Some(ogg_data)); } } } } - -impl fmt::Debug for PassthroughError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Debug::fmt(&self.0, f) - } -} - -impl From for PassthroughError { - fn from(err: OggReadError) -> PassthroughError { - PassthroughError(err) - } -} - -impl fmt::Display for PassthroughError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(&self.0, f) - } -} diff --git a/playback/src/dither.rs b/playback/src/dither.rs index 2510b886..0f667917 100644 --- a/playback/src/dither.rs +++ b/playback/src/dither.rs @@ -1,4 +1,5 @@ -use rand::rngs::ThreadRng; +use rand::rngs::SmallRng; +use rand::SeedableRng; use rand_distr::{Distribution, Normal, Triangular, Uniform}; use std::fmt; @@ -41,20 +42,19 @@ impl fmt::Display for dyn Ditherer { } } -// Implementation note: we save the handle to ThreadRng so it doesn't require -// a lookup on each call (which is on each sample!). This is ~2.5x as fast. -// Downside is that it is not Send so we cannot move it around player threads. -// +fn create_rng() -> SmallRng { + SmallRng::from_entropy() +} pub struct TriangularDitherer { - cached_rng: ThreadRng, + cached_rng: SmallRng, distribution: Triangular, } impl Ditherer for TriangularDitherer { fn new() -> Self { Self { - cached_rng: rand::thread_rng(), + cached_rng: create_rng(), // 2 LSB peak-to-peak needed to linearize the response: distribution: Triangular::new(-1.0, 1.0, 0.0).unwrap(), } @@ -74,14 +74,14 @@ impl TriangularDitherer { } pub struct GaussianDitherer { - cached_rng: ThreadRng, + cached_rng: SmallRng, distribution: Normal, } impl Ditherer for GaussianDitherer { fn new() -> Self { Self { - cached_rng: rand::thread_rng(), + cached_rng: create_rng(), // 1/2 LSB RMS needed to linearize the response: distribution: Normal::new(0.0, 0.5).unwrap(), } @@ -103,7 +103,7 @@ impl GaussianDitherer { pub struct HighPassDitherer { active_channel: usize, previous_noises: [f64; NUM_CHANNELS], - cached_rng: ThreadRng, + cached_rng: SmallRng, distribution: Uniform, } @@ -112,7 +112,7 @@ impl Ditherer for HighPassDitherer { Self { active_channel: 0, previous_noises: [0.0; NUM_CHANNELS], - cached_rng: rand::thread_rng(), + cached_rng: create_rng(), distribution: Uniform::new_inclusive(-0.5, 0.5), // 1 LSB +/- 1 LSB (previous) = 2 LSB } } diff --git a/playback/src/lib.rs b/playback/src/lib.rs index 689b8470..a52ca2fa 100644 --- a/playback/src/lib.rs +++ b/playback/src/lib.rs @@ -7,8 +7,8 @@ use librespot_metadata as metadata; pub mod audio_backend; pub mod config; -mod convert; -mod decoder; +pub mod convert; +pub mod decoder; pub mod dither; pub mod mixer; pub mod player; @@ -16,3 +16,5 @@ pub mod player; pub const SAMPLE_RATE: u32 = 44100; pub const NUM_CHANNELS: u8 = 2; pub const SAMPLES_PER_SECOND: u32 = SAMPLE_RATE as u32 * NUM_CHANNELS as u32; +pub const PAGES_PER_MS: f64 = SAMPLE_RATE as f64 / 1000.0; +pub const MS_PER_PAGE: f64 = 1000.0 / SAMPLE_RATE as f64; diff --git a/playback/src/mixer/alsamixer.rs b/playback/src/mixer/alsamixer.rs index 8bee9e0d..55398cb7 100644 --- a/playback/src/mixer/alsamixer.rs +++ b/playback/src/mixer/alsamixer.rs @@ -10,6 +10,7 @@ use alsa::{Ctl, Round}; use std::ffi::CString; #[derive(Clone)] +#[allow(dead_code)] pub struct AlsaMixer { config: MixerConfig, min: i64, @@ -31,14 +32,14 @@ const ZERO_DB: MilliBel = MilliBel(0); impl Mixer for AlsaMixer { fn open(config: MixerConfig) -> Self { info!( - "Mixing with alsa and volume control: {:?} for card: {} with mixer control: {},{}", - config.volume_ctrl, config.card, config.control, config.index, + "Mixing with Alsa and volume control: {:?} for device: {} with mixer control: {},{}", + config.volume_ctrl, config.device, config.control, config.index, ); let mut config = config; // clone let mixer = - alsa::mixer::Mixer::new(&config.card, false).expect("Could not open Alsa mixer"); + alsa::mixer::Mixer::new(&config.device, false).expect("Could not open Alsa mixer"); let simple_element = mixer .find_selem(&SelemId::new(&config.control, config.index)) .expect("Could not find Alsa mixer control"); @@ -56,8 +57,8 @@ impl Mixer for AlsaMixer { // Query dB volume range -- note that Alsa exposes a different // API for hardware and software mixers let (min_millibel, max_millibel) = if is_softvol { - let control = - Ctl::new(&config.card, false).expect("Could not open Alsa softvol with that card"); + let control = Ctl::new(&config.device, false) + .expect("Could not open Alsa softvol with that device"); let mut element_id = ElemId::new(ElemIface::Mixer); element_id.set_name( &CString::new(config.control.as_str()) @@ -144,7 +145,7 @@ impl Mixer for AlsaMixer { fn volume(&self) -> u16 { let mixer = - alsa::mixer::Mixer::new(&self.config.card, false).expect("Could not open Alsa mixer"); + alsa::mixer::Mixer::new(&self.config.device, false).expect("Could not open Alsa mixer"); let simple_element = mixer .find_selem(&SelemId::new(&self.config.control, self.config.index)) .expect("Could not find Alsa mixer control"); @@ -184,7 +185,7 @@ impl Mixer for AlsaMixer { fn set_volume(&self, volume: u16) { let mixer = - alsa::mixer::Mixer::new(&self.config.card, false).expect("Could not open Alsa mixer"); + alsa::mixer::Mixer::new(&self.config.device, false).expect("Could not open Alsa mixer"); let simple_element = mixer .find_selem(&SelemId::new(&self.config.control, self.config.index)) .expect("Could not find Alsa mixer control"); @@ -249,7 +250,7 @@ impl AlsaMixer { } let mixer = - alsa::mixer::Mixer::new(&self.config.card, false).expect("Could not open Alsa mixer"); + alsa::mixer::Mixer::new(&self.config.device, false).expect("Could not open Alsa mixer"); let simple_element = mixer .find_selem(&SelemId::new(&self.config.control, self.config.index)) .expect("Could not find Alsa mixer control"); diff --git a/playback/src/mixer/mod.rs b/playback/src/mixer/mod.rs index ed39582e..a3c7a5a1 100644 --- a/playback/src/mixer/mod.rs +++ b/playback/src/mixer/mod.rs @@ -30,7 +30,7 @@ use self::alsamixer::AlsaMixer; #[derive(Debug, Clone)] pub struct MixerConfig { - pub card: String, + pub device: String, pub control: String, pub index: u32, pub volume_ctrl: VolumeCtrl, @@ -39,7 +39,7 @@ pub struct MixerConfig { impl Default for MixerConfig { fn default() -> MixerConfig { MixerConfig { - card: String::from("default"), + device: String::from("default"), control: String::from("PCM"), index: 0, volume_ctrl: VolumeCtrl::default(), @@ -53,11 +53,19 @@ fn mk_sink(config: MixerConfig) -> Box { Box::new(M::open(config)) } +pub const MIXERS: &[(&str, MixerFn)] = &[ + (SoftMixer::NAME, mk_sink::), // default goes first + #[cfg(feature = "alsa-backend")] + (AlsaMixer::NAME, mk_sink::), +]; + pub fn find(name: Option<&str>) -> Option { - match name { - None | Some(SoftMixer::NAME) => Some(mk_sink::), - #[cfg(feature = "alsa-backend")] - Some(AlsaMixer::NAME) => Some(mk_sink::), - _ => None, + if let Some(name) = name { + MIXERS + .iter() + .find(|mixer| name == mixer.0) + .map(|mixer| mixer.1) + } else { + MIXERS.first().map(|mixer| mixer.1) } } diff --git a/playback/src/mixer/softmixer.rs b/playback/src/mixer/softmixer.rs index 27448237..cefc2de5 100644 --- a/playback/src/mixer/softmixer.rs +++ b/playback/src/mixer/softmixer.rs @@ -43,7 +43,7 @@ impl Mixer for SoftMixer { } impl SoftMixer { - pub const NAME: &'static str = "softmixer"; + pub const NAME: &'static str = "softvol"; } struct SoftVolumeApplier { diff --git a/playback/src/player.rs b/playback/src/player.rs index 61c7105a..6dec6a56 100644 --- a/playback/src/player.rs +++ b/playback/src/player.rs @@ -23,11 +23,11 @@ use crate::convert::Converter; use crate::core::session::Session; use crate::core::spotify_id::SpotifyId; use crate::core::util::SeqGenerator; -use crate::decoder::{AudioDecoder, AudioError, AudioPacket, PassthroughDecoder, VorbisDecoder}; +use crate::decoder::{AudioDecoder, AudioPacket, DecoderError, PassthroughDecoder, VorbisDecoder}; use crate::metadata::audio::{AudioFileFormat, AudioItem}; use crate::mixer::AudioFilter; -use crate::{NUM_CHANNELS, SAMPLES_PER_SECOND}; +use crate::{MS_PER_PAGE, NUM_CHANNELS, PAGES_PER_MS, SAMPLES_PER_SECOND}; const PRELOAD_NEXT_TRACK_BEFORE_END_DURATION_MS: u32 = 30000; pub const DB_VOLTAGE_RATIO: f64 = 20.0; @@ -67,6 +67,8 @@ struct PlayerInternal { limiter_peak_sample: f64, limiter_factor: f64, limiter_strength: f64, + + auto_normalise_as_album: bool, } enum PlayerCommand { @@ -86,6 +88,7 @@ enum PlayerCommand { AddEventSender(mpsc::UnboundedSender), SetSinkEventCallback(Option), EmitVolumeSetEvent(u16), + SetAutoNormaliseAsAlbum(bool), } #[derive(Debug, Clone)] @@ -238,9 +241,10 @@ impl NormalisationData { return 1.0; } - let [gain_db, gain_peak] = match config.normalisation_type { - NormalisationType::Album => [data.album_gain_db, data.album_peak], - NormalisationType::Track => [data.track_gain_db, data.track_peak], + let [gain_db, gain_peak] = if config.normalisation_type == NormalisationType::Album { + [data.album_gain_db, data.album_peak] + } else { + [data.track_gain_db, data.track_peak] }; let normalisation_power = gain_db as f64 + config.normalisation_pregain; @@ -264,7 +268,11 @@ impl NormalisationData { } debug!("Normalisation Data: {:?}", data); - debug!("Normalisation Factor: {:.2}%", normalisation_factor * 100.0); + debug!( + "Calculated Normalisation Factor for {:?}: {:.2}%", + config.normalisation_type, + normalisation_factor * 100.0 + ); normalisation_factor as f64 } @@ -327,6 +335,8 @@ impl Player { limiter_peak_sample: 0.0, limiter_factor: 1.0, limiter_strength: 0.0, + + auto_normalise_as_album: false, }; // While PlayerInternal is written as a future, it still contains blocking code. @@ -350,7 +360,11 @@ impl Player { } fn command(&self, cmd: PlayerCommand) { - self.commands.as_ref().unwrap().send(cmd).unwrap(); + if let Some(commands) = self.commands.as_ref() { + if let Err(e) = commands.send(cmd) { + error!("Player Commands Error: {}", e); + } + } } pub fn load(&mut self, track_id: SpotifyId, start_playing: bool, position_ms: u32) -> u64 { @@ -410,6 +424,10 @@ impl Player { pub fn emit_volume_set_event(&self, volume: u16) { self.command(PlayerCommand::EmitVolumeSetEvent(volume)); } + + pub fn set_auto_normalise_as_album(&self, setting: bool) { + self.command(PlayerCommand::SetAutoNormaliseAsAlbum(setting)); + } } impl Drop for Player { @@ -419,7 +437,7 @@ impl Drop for Player { if let Some(handle) = self.thread_handle.take() { match handle.join() { Ok(_) => (), - Err(_) => error!("Player thread panicked!"), + Err(e) => error!("Player thread Error: {:?}", e), } } } @@ -427,7 +445,7 @@ impl Drop for Player { struct PlayerLoadedTrackData { decoder: Decoder, - normalisation_factor: f64, + normalisation_data: NormalisationData, stream_loader_controller: StreamLoaderController, bytes_per_second: usize, duration_ms: u32, @@ -460,6 +478,7 @@ enum PlayerState { track_id: SpotifyId, play_request_id: u64, decoder: Decoder, + normalisation_data: NormalisationData, normalisation_factor: f64, stream_loader_controller: StreamLoaderController, bytes_per_second: usize, @@ -471,6 +490,7 @@ enum PlayerState { track_id: SpotifyId, play_request_id: u64, decoder: Decoder, + normalisation_data: NormalisationData, normalisation_factor: f64, stream_loader_controller: StreamLoaderController, bytes_per_second: usize, @@ -493,7 +513,10 @@ impl PlayerState { match *self { Stopped | EndOfTrack { .. } | Paused { .. } | Loading { .. } => false, Playing { .. } => true, - Invalid => panic!("invalid state"), + Invalid => { + error!("PlayerState is_playing: invalid state"); + exit(1); + } } } @@ -518,7 +541,10 @@ impl PlayerState { | Playing { ref mut decoder, .. } => Some(decoder), - Invalid => panic!("invalid state"), + Invalid => { + error!("PlayerState decoder: invalid state"); + exit(1); + } } } @@ -534,7 +560,10 @@ impl PlayerState { ref mut stream_loader_controller, .. } => Some(stream_loader_controller), - Invalid => panic!("invalid state"), + Invalid => { + error!("PlayerState stream_loader_controller: invalid state"); + exit(1); + } } } @@ -547,7 +576,7 @@ impl PlayerState { decoder, duration_ms, bytes_per_second, - normalisation_factor, + normalisation_data, stream_loader_controller, stream_position_pcm, .. @@ -557,7 +586,7 @@ impl PlayerState { play_request_id, loaded_track: PlayerLoadedTrackData { decoder, - normalisation_factor, + normalisation_data, stream_loader_controller, bytes_per_second, duration_ms, @@ -565,7 +594,10 @@ impl PlayerState { }, }; } - _ => panic!("Called playing_to_end_of_track in non-playing state."), + _ => { + error!("Called playing_to_end_of_track in non-playing state."); + exit(1); + } } } @@ -576,6 +608,7 @@ impl PlayerState { track_id, play_request_id, decoder, + normalisation_data, normalisation_factor, stream_loader_controller, duration_ms, @@ -587,6 +620,7 @@ impl PlayerState { track_id, play_request_id, decoder, + normalisation_data, normalisation_factor, stream_loader_controller, duration_ms, @@ -596,7 +630,10 @@ impl PlayerState { suggested_to_preload_next_track, }; } - _ => panic!("invalid state"), + _ => { + error!("PlayerState paused_to_playing: invalid state"); + exit(1); + } } } @@ -607,6 +644,7 @@ impl PlayerState { track_id, play_request_id, decoder, + normalisation_data, normalisation_factor, stream_loader_controller, duration_ms, @@ -619,6 +657,7 @@ impl PlayerState { track_id, play_request_id, decoder, + normalisation_data, normalisation_factor, stream_loader_controller, duration_ms, @@ -627,7 +666,10 @@ impl PlayerState { suggested_to_preload_next_track, }; } - _ => panic!("invalid state"), + _ => { + error!("PlayerState playing_to_paused: invalid state"); + exit(1); + } } } } @@ -680,8 +722,8 @@ impl PlayerTrackLoader { ) -> Option { let audio = match AudioItem::get_file(&self.session, spotify_id).await { Ok(audio) => audio, - Err(_) => { - error!("Unable to load audio item."); + Err(e) => { + error!("Unable to load audio item: {:?}", e); return None; } }; @@ -753,8 +795,8 @@ impl PlayerTrackLoader { let encrypted_file = match encrypted_file.await { Ok(encrypted_file) => encrypted_file, - Err(_) => { - error!("Unable to load encrypted file."); + Err(e) => { + error!("Unable to load encrypted file: {:?}", e); return None; } }; @@ -772,22 +814,24 @@ impl PlayerTrackLoader { let key = match self.session.audio_key().request(spotify_id, file_id).await { Ok(key) => key, - Err(_) => { - error!("Unable to load decryption key"); + Err(e) => { + error!("Unable to load decryption key: {:?}", e); return None; } }; let mut decrypted_file = AudioDecrypt::new(key, encrypted_file); - let normalisation_factor = match NormalisationData::parse_from_file(&mut decrypted_file) - { - Ok(normalisation_data) => { - NormalisationData::get_factor(&self.config, normalisation_data) - } + let normalisation_data = match NormalisationData::parse_from_file(&mut decrypted_file) { + Ok(data) => data, Err(_) => { warn!("Unable to extract normalisation data, using default value."); - 1.0 + NormalisationData { + track_gain_db: 0.0, + track_peak: 1.0, + album_gain_db: 0.0, + album_peak: 1.0, + } } }; @@ -796,12 +840,12 @@ impl PlayerTrackLoader { let result = if self.config.passthrough { match PassthroughDecoder::new(audio_file) { Ok(result) => Ok(Box::new(result) as Decoder), - Err(e) => Err(AudioError::PassthroughError(e)), + Err(e) => Err(DecoderError::PassthroughDecoder(e.to_string())), } } else { match VorbisDecoder::new(audio_file) { Ok(result) => Ok(Box::new(result) as Decoder), - Err(e) => Err(AudioError::VorbisError(e)), + Err(e) => Err(DecoderError::LewtonDecoder(e.to_string())), } }; @@ -813,14 +857,17 @@ impl PlayerTrackLoader { e ); - if self - .session - .cache() - .expect("If the audio file is cached, a cache should exist") - .remove_file(file_id) - .is_err() - { - return None; + match self.session.cache() { + Some(cache) => { + if cache.remove_file(file_id).is_err() { + error!("Error removing file from cache"); + return None; + } + } + None => { + error!("If the audio file is cached, a cache should exist"); + return None; + } } // Just try it again @@ -832,18 +879,20 @@ impl PlayerTrackLoader { } }; - if position_ms != 0 { - if let Err(err) = decoder.seek(position_ms as i64) { - error!("Vorbis error: {}", err); + let position_pcm = PlayerInternal::position_ms_to_pcm(position_ms); + + if position_pcm != 0 { + if let Err(e) = decoder.seek(position_pcm) { + error!("PlayerTrackLoader load_track: {}", e); } stream_loader_controller.set_stream_mode(); } - let stream_position_pcm = PlayerInternal::position_ms_to_pcm(position_ms); + let stream_position_pcm = position_pcm; info!("<{}> ({} ms) loaded", audio.name, audio.duration); return Some(PlayerLoadedTrackData { decoder, - normalisation_factor, + normalisation_data, stream_loader_controller, bytes_per_second, duration_ms, @@ -895,7 +944,8 @@ impl Future for PlayerInternal { start_playback, ); if let PlayerState::Loading { .. } = self.state { - panic!("The state wasn't changed by start_playback()"); + error!("The state wasn't changed by start_playback()"); + exit(1); } } Poll::Ready(Err(_)) => { @@ -959,47 +1009,67 @@ impl Future for PlayerInternal { .. } = self.state { - let packet = decoder.next_packet().expect("Vorbis error"); + match decoder.next_packet() { + Ok(packet) => { + if !passthrough { + if let Some(ref packet) = packet { + match packet.samples() { + Ok(samples) => { + *stream_position_pcm += + (samples.len() / NUM_CHANNELS as usize) as u64; + let stream_position_millis = + Self::position_pcm_to_ms(*stream_position_pcm); - if !passthrough { - if let Some(ref packet) = packet { - *stream_position_pcm += - (packet.samples().len() / NUM_CHANNELS as usize) as u64; - let stream_position_millis = - Self::position_pcm_to_ms(*stream_position_pcm); - - let notify_about_position = match *reported_nominal_start_time { - None => true, - Some(reported_nominal_start_time) => { - // only notify if we're behind. If we're ahead it's probably due to a buffer of the backend and we're actually in time. - let lag = (Instant::now() - reported_nominal_start_time) - .as_millis() - as i64 - - stream_position_millis as i64; - lag > Duration::from_secs(1).as_millis() as i64 + let notify_about_position = + match *reported_nominal_start_time { + None => true, + Some(reported_nominal_start_time) => { + // only notify if we're behind. If we're ahead it's probably due to a buffer of the backend and we're actually in time. + let lag = (Instant::now() + - reported_nominal_start_time) + .as_millis() + as i64 + - stream_position_millis as i64; + lag > Duration::from_secs(1).as_millis() + as i64 + } + }; + if notify_about_position { + *reported_nominal_start_time = Some( + Instant::now() + - Duration::from_millis( + stream_position_millis as u64, + ), + ); + self.send_event(PlayerEvent::Playing { + track_id, + play_request_id, + position_ms: stream_position_millis as u32, + duration_ms, + }); + } + } + Err(e) => { + error!("PlayerInternal poll: {}", e); + exit(1); + } + } } - }; - if notify_about_position { - *reported_nominal_start_time = Some( - Instant::now() - - Duration::from_millis(stream_position_millis as u64), - ); - self.send_event(PlayerEvent::Playing { - track_id, - play_request_id, - position_ms: stream_position_millis as u32, - duration_ms, - }); + } else { + // position, even if irrelevant, must be set so that seek() is called + *stream_position_pcm = duration_ms.into(); } - } - } else { - // position, even if irrelevant, must be set so that seek() is called - *stream_position_pcm = duration_ms.into(); - } - self.handle_packet(packet, normalisation_factor); + self.handle_packet(packet, normalisation_factor); + } + Err(e) => { + error!("PlayerInternal poll: {}", e); + exit(1); + } + } } else { - unreachable!(); + error!("PlayerInternal poll: Invalid PlayerState"); + exit(1); }; } @@ -1048,11 +1118,11 @@ impl Future for PlayerInternal { impl PlayerInternal { fn position_pcm_to_ms(position_pcm: u64) -> u32 { - (position_pcm * 10 / 441) as u32 + (position_pcm as f64 * MS_PER_PAGE) as u32 } fn position_ms_to_pcm(position_ms: u32) -> u64 { - position_ms as u64 * 441 / 10 + (position_ms as f64 * PAGES_PER_MS) as u64 } fn ensure_sink_running(&mut self) { @@ -1063,8 +1133,8 @@ impl PlayerInternal { } match self.sink.start() { Ok(()) => self.sink_status = SinkStatus::Running, - Err(err) => { - error!("Fatal error, could not start audio sink: {}", err); + Err(e) => { + error!("{}", e); exit(1); } } @@ -1086,8 +1156,8 @@ impl PlayerInternal { callback(self.sink_status); } } - Err(err) => { - error!("Fatal error, could not stop audio sink: {}", err); + Err(e) => { + error!("{}", e); exit(1); } } @@ -1134,7 +1204,10 @@ impl PlayerInternal { self.state = PlayerState::Stopped; } PlayerState::Stopped => (), - PlayerState::Invalid => panic!("invalid state"), + PlayerState::Invalid => { + error!("PlayerInternal handle_player_stop: invalid state"); + exit(1); + } } } @@ -1191,10 +1264,6 @@ impl PlayerInternal { Some(mut packet) => { if !packet.is_empty() { if let AudioPacket::Samples(ref mut data) = packet { - if let Some(ref editor) = self.audio_filter { - editor.modify_stream(data) - } - if self.config.normalisation && !(f64::abs(normalisation_factor - 1.0) <= f64::EPSILON && self.config.normalisation_method == NormalisationMethod::Basic) @@ -1295,22 +1364,17 @@ impl PlayerInternal { } } } - *sample *= actual_normalisation_factor; - - // Extremely sharp attacks, however unlikely, *may* still clip and provide - // undefined results, so strictly enforce output within [-1.0, 1.0]. - if *sample < -1.0 { - *sample = -1.0; - } else if *sample > 1.0 { - *sample = 1.0; - } } } + + if let Some(ref editor) = self.audio_filter { + editor.modify_stream(data) + } } - if let Err(err) = self.sink.write(&packet, &mut self.converter) { - error!("Fatal error, could not write audio to audio sink: {}", err); + if let Err(e) = self.sink.write(&packet, &mut self.converter) { + error!("{}", e); exit(1); } } @@ -1329,7 +1393,8 @@ impl PlayerInternal { play_request_id, }) } else { - unreachable!(); + error!("PlayerInternal handle_packet: Invalid PlayerState"); + exit(1); } } } @@ -1353,6 +1418,17 @@ impl PlayerInternal { ) { let position_ms = Self::position_pcm_to_ms(loaded_track.stream_position_pcm); + let mut config = self.config.clone(); + if config.normalisation_type == NormalisationType::Auto { + if self.auto_normalise_as_album { + config.normalisation_type = NormalisationType::Album; + } else { + config.normalisation_type = NormalisationType::Track; + } + }; + let normalisation_factor = + NormalisationData::get_factor(&config, loaded_track.normalisation_data); + if start_playback { self.ensure_sink_running(); @@ -1367,7 +1443,8 @@ impl PlayerInternal { track_id, play_request_id, decoder: loaded_track.decoder, - normalisation_factor: loaded_track.normalisation_factor, + normalisation_data: loaded_track.normalisation_data, + normalisation_factor, stream_loader_controller: loaded_track.stream_loader_controller, duration_ms: loaded_track.duration_ms, bytes_per_second: loaded_track.bytes_per_second, @@ -1384,7 +1461,8 @@ impl PlayerInternal { track_id, play_request_id, decoder: loaded_track.decoder, - normalisation_factor: loaded_track.normalisation_factor, + normalisation_data: loaded_track.normalisation_data, + normalisation_factor, stream_loader_controller: loaded_track.stream_loader_controller, duration_ms: loaded_track.duration_ms, bytes_per_second: loaded_track.bytes_per_second, @@ -1437,7 +1515,10 @@ impl PlayerInternal { play_request_id, position_ms, }), - PlayerState::Invalid { .. } => panic!("Player is in an invalid state."), + PlayerState::Invalid { .. } => { + error!("PlayerInternal handle_command_load: invalid state"); + exit(1); + } } // Now we check at different positions whether we already have a pre-loaded version @@ -1453,24 +1534,30 @@ impl PlayerInternal { if previous_track_id == track_id { let mut loaded_track = match mem::replace(&mut self.state, PlayerState::Invalid) { PlayerState::EndOfTrack { loaded_track, .. } => loaded_track, - _ => unreachable!(), + _ => { + error!("PlayerInternal handle_command_load: Invalid PlayerState"); + exit(1); + } }; - if Self::position_ms_to_pcm(position_ms) != loaded_track.stream_position_pcm { + let position_pcm = Self::position_ms_to_pcm(position_ms); + + if position_pcm != loaded_track.stream_position_pcm { loaded_track .stream_loader_controller .set_random_access_mode(); - let _ = loaded_track.decoder.seek(position_ms as i64); // This may be blocking. - // But most likely the track is fully - // loaded already because we played - // to the end of it. + if let Err(e) = loaded_track.decoder.seek(position_pcm) { + // This may be blocking. + error!("PlayerInternal handle_command_load: {}", e); + } loaded_track.stream_loader_controller.set_stream_mode(); - loaded_track.stream_position_pcm = Self::position_ms_to_pcm(position_ms); + loaded_track.stream_position_pcm = position_pcm; } self.preload = PlayerPreload::None; self.start_playback(track_id, play_request_id, loaded_track, play); if let PlayerState::Invalid = self.state { - panic!("start_playback() hasn't set a valid player state."); + error!("start_playback() hasn't set a valid player state."); + exit(1); } return; } @@ -1494,11 +1581,16 @@ impl PlayerInternal { { if current_track_id == track_id { // we can use the current decoder. Ensure it's at the correct position. - if Self::position_ms_to_pcm(position_ms) != *stream_position_pcm { + let position_pcm = Self::position_ms_to_pcm(position_ms); + + if position_pcm != *stream_position_pcm { stream_loader_controller.set_random_access_mode(); - let _ = decoder.seek(position_ms as i64); // This may be blocking. + if let Err(e) = decoder.seek(position_pcm) { + // This may be blocking. + error!("PlayerInternal handle_command_load: {}", e); + } stream_loader_controller.set_stream_mode(); - *stream_position_pcm = Self::position_ms_to_pcm(position_ms); + *stream_position_pcm = position_pcm; } // Move the info from the current state into a PlayerLoadedTrackData so we can use @@ -1511,7 +1603,7 @@ impl PlayerInternal { stream_loader_controller, bytes_per_second, duration_ms, - normalisation_factor, + normalisation_data, .. } | PlayerState::Paused { @@ -1520,13 +1612,13 @@ impl PlayerInternal { stream_loader_controller, bytes_per_second, duration_ms, - normalisation_factor, + normalisation_data, .. } = old_state { let loaded_track = PlayerLoadedTrackData { decoder, - normalisation_factor, + normalisation_data, stream_loader_controller, bytes_per_second, duration_ms, @@ -1537,12 +1629,14 @@ impl PlayerInternal { self.start_playback(track_id, play_request_id, loaded_track, play); if let PlayerState::Invalid = self.state { - panic!("start_playback() hasn't set a valid player state."); + error!("start_playback() hasn't set a valid player state."); + exit(1); } return; } else { - unreachable!(); + error!("PlayerInternal handle_command_load: Invalid PlayerState"); + exit(1); } } } @@ -1560,17 +1654,23 @@ impl PlayerInternal { mut loaded_track, } = preload { - if Self::position_ms_to_pcm(position_ms) != loaded_track.stream_position_pcm { + let position_pcm = Self::position_ms_to_pcm(position_ms); + + if position_pcm != loaded_track.stream_position_pcm { loaded_track .stream_loader_controller .set_random_access_mode(); - let _ = loaded_track.decoder.seek(position_ms as i64); // This may be blocking + if let Err(e) = loaded_track.decoder.seek(position_pcm) { + // This may be blocking + error!("PlayerInternal handle_command_load: {}", e); + } loaded_track.stream_loader_controller.set_stream_mode(); } self.start_playback(track_id, play_request_id, *loaded_track, play); return; } else { - unreachable!(); + error!("PlayerInternal handle_command_load: Invalid PlayerState"); + exit(1); } } } @@ -1676,7 +1776,9 @@ impl PlayerInternal { stream_loader_controller.set_random_access_mode(); } if let Some(decoder) = self.state.decoder() { - match decoder.seek(position_ms as i64) { + let position_pcm = Self::position_ms_to_pcm(position_ms); + + match decoder.seek(position_pcm) { Ok(_) => { if let PlayerState::Playing { ref mut stream_position_pcm, @@ -1687,10 +1789,10 @@ impl PlayerInternal { .. } = self.state { - *stream_position_pcm = Self::position_ms_to_pcm(position_ms); + *stream_position_pcm = position_pcm; } } - Err(err) => error!("Vorbis error: {:?}", err), + Err(e) => error!("PlayerInternal handle_command_seek: {}", e), } } else { warn!("Player::seek called from invalid state"); @@ -1764,6 +1866,10 @@ impl PlayerInternal { PlayerCommand::EmitVolumeSetEvent(volume) => { self.send_event(PlayerEvent::VolumeSet { volume }) } + + PlayerCommand::SetAutoNormaliseAsAlbum(setting) => { + self.auto_normalise_as_album = setting + } } } @@ -1870,6 +1976,10 @@ impl ::std::fmt::Debug for PlayerCommand { PlayerCommand::EmitVolumeSetEvent(volume) => { f.debug_tuple("VolumeSet").field(&volume).finish() } + PlayerCommand::SetAutoNormaliseAsAlbum(setting) => f + .debug_tuple("SetAutoNormaliseAsAlbum") + .field(&setting) + .finish(), } } } @@ -1926,7 +2036,9 @@ struct Subfile { impl Subfile { pub fn new(mut stream: T, offset: u64) -> Subfile { - stream.seek(SeekFrom::Start(offset)).unwrap(); + if let Err(e) = stream.seek(SeekFrom::Start(offset)) { + error!("Subfile new Error: {}", e); + } Subfile { stream, offset } } } diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index 2628ecd1..5e24f288 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "librespot-protocol" -version = "0.2.0" +version = "0.3.1" authors = ["Paul Liétar "] build = "build.rs" description = "The protobuf logic for communicating with Spotify servers" diff --git a/publish.sh b/publish.sh index 478741a5..fb4a475a 100755 --- a/publish.sh +++ b/publish.sh @@ -6,7 +6,7 @@ DRY_RUN='false' WORKINGDIR="$( cd "$(dirname "$0")" ; pwd -P )" cd $WORKINGDIR -crates=( "protocol" "core" "audio" "metadata" "playback" "connect" "librespot" ) +crates=( "protocol" "core" "discovery" "audio" "metadata" "playback" "connect" "librespot" ) function switchBranch { if [ "$SKIP_MERGE" = 'false' ] ; then diff --git a/src/main.rs b/src/main.rs index 185a9bf2..660744f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use futures_util::{future, FutureExt, StreamExt}; use librespot_playback::player::PlayerEvent; -use log::{error, info, warn}; +use log::{error, info, trace, warn}; use sha1::{Digest, Sha1}; use thiserror::Error; use tokio::sync::mpsc::UnboundedReceiver; @@ -19,15 +19,15 @@ use librespot::playback::config::{ use librespot::playback::dither; #[cfg(feature = "alsa-backend")] use librespot::playback::mixer::alsamixer::AlsaMixer; -use librespot::playback::mixer::mappings::MappedCtrl; use librespot::playback::mixer::{self, MixerConfig, MixerFn}; -use librespot::playback::player::{db_to_ratio, Player}; +use librespot::playback::player::{db_to_ratio, ratio_to_db, Player}; mod player_event_handler; use player_event_handler::{emit_sink_event, run_program_on_events}; use std::env; use std::io::{stderr, Write}; +use std::ops::RangeInclusive; use std::path::Path; use std::pin::Pin; use std::process::exit; @@ -44,7 +44,24 @@ fn usage(program: &str, opts: &getopts::Options) -> String { opts.usage(&brief) } -fn setup_logging(verbose: bool) { +fn arg_to_var(arg: &str) -> String { + // To avoid name collisions environment variables must be prepended + // with `LIBRESPOT_` so option/flag `foo-bar` becomes `LIBRESPOT_FOO_BAR`. + format!("LIBRESPOT_{}", arg.to_uppercase().replace("-", "_")) +} + +fn env_var_present(arg: &str) -> bool { + env::var(arg_to_var(arg)).is_ok() +} + +fn env_var_opt_str(option: &str) -> Option { + match env::var(arg_to_var(option)) { + Ok(value) => Some(value), + Err(_) => None, + } +} + +fn setup_logging(quiet: bool, verbose: bool) { let mut builder = env_logger::Builder::new(); match env::var("RUST_LOG") { Ok(config) => { @@ -53,21 +70,29 @@ fn setup_logging(verbose: bool) { if verbose { warn!("`--verbose` flag overidden by `RUST_LOG` environment variable"); + } else if quiet { + warn!("`--quiet` flag overidden by `RUST_LOG` environment variable"); } } Err(_) => { if verbose { builder.parse_filters("libmdns=info,librespot=trace"); + } else if quiet { + builder.parse_filters("libmdns=warn,librespot=warn"); } else { builder.parse_filters("libmdns=info,librespot=info"); } builder.init(); + + if verbose && quiet { + warn!("`--verbose` and `--quiet` are mutually exclusive. Logging can not be both verbose and quiet. Using verbose mode."); + } } } } fn list_backends() { - println!("Available backends : "); + println!("Available backends: "); for (&(name, _), idx) in BACKENDS.iter().zip(0..) { if idx == 0 { println!("- {} (default)", name); @@ -160,14 +185,20 @@ pub fn parse_file_size(input: &str) -> Result { Ok((num * base.pow(exponent) as f64) as u64) } -fn print_version() { - println!( - "librespot {semver} {sha} (Built on {build_date}, Build ID: {build_id})", +fn get_version_string() -> String { + #[cfg(debug_assertions)] + const BUILD_PROFILE: &str = "debug"; + #[cfg(not(debug_assertions))] + const BUILD_PROFILE: &str = "release"; + + format!( + "librespot {semver} {sha} (Built on {build_date}, Build ID: {build_id}, Profile: {build_profile})", semver = version::SEMVER, sha = version::SHA_SHORT, build_date = version::BUILD_DATE, - build_id = version::BUILD_ID - ); + build_id = version::BUILD_ID, + build_profile = BUILD_PROFILE + ) } struct Setup { @@ -188,26 +219,35 @@ struct Setup { } fn get_setup(args: &[String]) -> Setup { + const VALID_NORMALISATION_KNEE_RANGE: RangeInclusive = 0.0..=2.0; + const VALID_VOLUME_RANGE: RangeInclusive = 0.0..=100.0; + const VALID_NORMALISATION_PREGAIN_RANGE: RangeInclusive = -10.0..=10.0; + const VALID_NORMALISATION_THRESHOLD_RANGE: RangeInclusive = -10.0..=0.0; + const VALID_NORMALISATION_ATTACK_RANGE: RangeInclusive = 1..=500; + const VALID_NORMALISATION_RELEASE_RANGE: RangeInclusive = 1..=1000; + const AP_PORT: &str = "ap-port"; const AUTOPLAY: &str = "autoplay"; const BACKEND: &str = "backend"; - const BITRATE: &str = "b"; - const CACHE: &str = "c"; + const BITRATE: &str = "bitrate"; + const CACHE: &str = "cache"; const CACHE_SIZE_LIMIT: &str = "cache-size-limit"; const DEVICE: &str = "device"; const DEVICE_TYPE: &str = "device-type"; const DISABLE_AUDIO_CACHE: &str = "disable-audio-cache"; + const DISABLE_CREDENTIAL_CACHE: &str = "disable-credential-cache"; const DISABLE_DISCOVERY: &str = "disable-discovery"; const DISABLE_GAPLESS: &str = "disable-gapless"; const DITHER: &str = "dither"; const EMIT_SINK_EVENTS: &str = "emit-sink-events"; const ENABLE_VOLUME_NORMALISATION: &str = "enable-volume-normalisation"; const FORMAT: &str = "format"; - const HELP: &str = "h"; + const HELP: &str = "help"; const INITIAL_VOLUME: &str = "initial-volume"; - const MIXER_CARD: &str = "mixer-card"; - const MIXER_INDEX: &str = "mixer-index"; - const MIXER_NAME: &str = "mixer-name"; + const MIXER_TYPE: &str = "mixer"; + const ALSA_MIXER_DEVICE: &str = "alsa-mixer-device"; + const ALSA_MIXER_INDEX: &str = "alsa-mixer-index"; + const ALSA_MIXER_CONTROL: &str = "alsa-mixer-control"; const NAME: &str = "name"; const NORMALISATION_ATTACK: &str = "normalisation-attack"; const NORMALISATION_GAIN_TYPE: &str = "normalisation-gain-type"; @@ -220,6 +260,7 @@ fn get_setup(args: &[String]) -> Setup { const PASSTHROUGH: &str = "passthrough"; const PASSWORD: &str = "password"; const PROXY: &str = "proxy"; + const QUIET: &str = "quiet"; const SYSTEM_CACHE: &str = "system-cache"; const USERNAME: &str = "username"; const VERBOSE: &str = "verbose"; @@ -228,177 +269,331 @@ fn get_setup(args: &[String]) -> Setup { const VOLUME_RANGE: &str = "volume-range"; const ZEROCONF_PORT: &str = "zeroconf-port"; + // Mostly arbitrary. + const AUTOPLAY_SHORT: &str = "A"; + const AP_PORT_SHORT: &str = "a"; + const BACKEND_SHORT: &str = "B"; + const BITRATE_SHORT: &str = "b"; + const SYSTEM_CACHE_SHORT: &str = "C"; + const CACHE_SHORT: &str = "c"; + const DITHER_SHORT: &str = "D"; + const DEVICE_SHORT: &str = "d"; + const VOLUME_CTRL_SHORT: &str = "E"; + const VOLUME_RANGE_SHORT: &str = "e"; + const DEVICE_TYPE_SHORT: &str = "F"; + const FORMAT_SHORT: &str = "f"; + const DISABLE_AUDIO_CACHE_SHORT: &str = "G"; + const DISABLE_GAPLESS_SHORT: &str = "g"; + const DISABLE_CREDENTIAL_CACHE_SHORT: &str = "H"; + const HELP_SHORT: &str = "h"; + const CACHE_SIZE_LIMIT_SHORT: &str = "M"; + const MIXER_TYPE_SHORT: &str = "m"; + const ENABLE_VOLUME_NORMALISATION_SHORT: &str = "N"; + const NAME_SHORT: &str = "n"; + const DISABLE_DISCOVERY_SHORT: &str = "O"; + const ONEVENT_SHORT: &str = "o"; + const PASSTHROUGH_SHORT: &str = "P"; + const PASSWORD_SHORT: &str = "p"; + const EMIT_SINK_EVENTS_SHORT: &str = "Q"; + const QUIET_SHORT: &str = "q"; + const INITIAL_VOLUME_SHORT: &str = "R"; + const ALSA_MIXER_DEVICE_SHORT: &str = "S"; + const ALSA_MIXER_INDEX_SHORT: &str = "s"; + const ALSA_MIXER_CONTROL_SHORT: &str = "T"; + const NORMALISATION_ATTACK_SHORT: &str = "U"; + const USERNAME_SHORT: &str = "u"; + const VERSION_SHORT: &str = "V"; + const VERBOSE_SHORT: &str = "v"; + const NORMALISATION_GAIN_TYPE_SHORT: &str = "W"; + const NORMALISATION_KNEE_SHORT: &str = "w"; + const NORMALISATION_METHOD_SHORT: &str = "X"; + const PROXY_SHORT: &str = "x"; + const NORMALISATION_PREGAIN_SHORT: &str = "Y"; + const NORMALISATION_RELEASE_SHORT: &str = "y"; + const NORMALISATION_THRESHOLD_SHORT: &str = "Z"; + const ZEROCONF_PORT_SHORT: &str = "z"; + + // Options that have different desc's + // depending on what backends were enabled at build time. + #[cfg(feature = "alsa-backend")] + const MIXER_TYPE_DESC: &str = "Mixer to use {alsa|softvol}. Defaults to softvol."; + #[cfg(not(feature = "alsa-backend"))] + const MIXER_TYPE_DESC: &str = "Not supported by the included audio backend(s)."; + #[cfg(any( + feature = "alsa-backend", + feature = "rodio-backend", + feature = "portaudio-backend" + ))] + const DEVICE_DESC: &str = "Audio device to use. Use ? to list options if using alsa, portaudio or rodio. Defaults to the backend's default."; + #[cfg(not(any( + feature = "alsa-backend", + feature = "rodio-backend", + feature = "portaudio-backend" + )))] + const DEVICE_DESC: &str = "Not supported by the included audio backend(s)."; + #[cfg(feature = "alsa-backend")] + const ALSA_MIXER_CONTROL_DESC: &str = + "Alsa mixer control, e.g. PCM, Master or similar. Defaults to PCM."; + #[cfg(not(feature = "alsa-backend"))] + const ALSA_MIXER_CONTROL_DESC: &str = "Not supported by the included audio backend(s)."; + #[cfg(feature = "alsa-backend")] + const ALSA_MIXER_DEVICE_DESC: &str = "Alsa mixer device, e.g hw:0 or similar from `aplay -l`. Defaults to `--device` if specified, default otherwise."; + #[cfg(not(feature = "alsa-backend"))] + const ALSA_MIXER_DEVICE_DESC: &str = "Not supported by the included audio backend(s)."; + #[cfg(feature = "alsa-backend")] + const ALSA_MIXER_INDEX_DESC: &str = "Alsa index of the cards mixer. Defaults to 0."; + #[cfg(not(feature = "alsa-backend"))] + const ALSA_MIXER_INDEX_DESC: &str = "Not supported by the included audio backend(s)."; + #[cfg(feature = "alsa-backend")] + const INITIAL_VOLUME_DESC: &str = "Initial volume in % from 0 - 100. Default for softvol: 50. For the alsa mixer: the current volume."; + #[cfg(not(feature = "alsa-backend"))] + const INITIAL_VOLUME_DESC: &str = "Initial volume in % from 0 - 100. Defaults to 50."; + #[cfg(feature = "alsa-backend")] + const VOLUME_RANGE_DESC: &str = "Range of the volume control (dB) from 0.0 to 100.0. Default for softvol: 60.0. For the alsa mixer: what the control supports."; + #[cfg(not(feature = "alsa-backend"))] + const VOLUME_RANGE_DESC: &str = + "Range of the volume control (dB) from 0.0 to 100.0. Defaults to 60.0."; + let mut opts = getopts::Options::new(); opts.optflag( + HELP_SHORT, HELP, - "help", "Print this help menu.", - ).optopt( - CACHE, - "cache", - "Path to a directory where files will be cached.", - "PATH", - ).optopt( - "", - SYSTEM_CACHE, - "Path to a directory where system files (credentials, volume) will be cached. Can be different from cache option value.", - "PATH", - ).optopt( - "", - CACHE_SIZE_LIMIT, - "Limits the size of the cache for audio files.", - "SIZE" - ).optflag("", DISABLE_AUDIO_CACHE, "Disable caching of the audio data.") - .optopt("n", NAME, "Device name.", "NAME") - .optopt("", DEVICE_TYPE, "Displayed device type.", "TYPE") + ) + .optflag( + VERSION_SHORT, + VERSION, + "Display librespot version string.", + ) + .optflag( + VERBOSE_SHORT, + VERBOSE, + "Enable verbose log output.", + ) + .optflag( + QUIET_SHORT, + QUIET, + "Only log warning and error messages.", + ) + .optflag( + DISABLE_AUDIO_CACHE_SHORT, + DISABLE_AUDIO_CACHE, + "Disable caching of the audio data.", + ) + .optflag( + DISABLE_CREDENTIAL_CACHE_SHORT, + DISABLE_CREDENTIAL_CACHE, + "Disable caching of credentials.", + ) + .optflag( + DISABLE_DISCOVERY_SHORT, + DISABLE_DISCOVERY, + "Disable zeroconf discovery mode.", + ) + .optflag( + DISABLE_GAPLESS_SHORT, + DISABLE_GAPLESS, + "Disable gapless playback.", + ) + .optflag( + EMIT_SINK_EVENTS_SHORT, + EMIT_SINK_EVENTS, + "Run PROGRAM set by `--onevent` before the sink is opened and after it is closed.", + ) + .optflag( + AUTOPLAY_SHORT, + AUTOPLAY, + "Automatically play similar songs when your music ends.", + ) + .optflag( + PASSTHROUGH_SHORT, + PASSTHROUGH, + "Pass a raw stream to the output. Only works with the pipe and subprocess backends.", + ) + .optflag( + ENABLE_VOLUME_NORMALISATION_SHORT, + ENABLE_VOLUME_NORMALISATION, + "Play all tracks at approximately the same apparent volume.", + ) .optopt( + NAME_SHORT, + NAME, + "Device name. Defaults to Librespot.", + "NAME", + ) + .optopt( + BITRATE_SHORT, BITRATE, - "bitrate", "Bitrate (kbps) {96|160|320}. Defaults to 160.", "BITRATE", ) .optopt( - "", - ONEVENT, - "Run PROGRAM when a playback event occurs.", - "PROGRAM", - ) - .optflag("", EMIT_SINK_EVENTS, "Run program set by --onevent before sink is opened and after it is closed.") - .optflag("v", VERBOSE, "Enable verbose output.") - .optflag("V", VERSION, "Display librespot version string.") - .optopt("u", USERNAME, "Username to sign in with.", "USERNAME") - .optopt("p", PASSWORD, "Password", "PASSWORD") - .optopt("", PROXY, "HTTP proxy to use when connecting.", "URL") - .optopt("", AP_PORT, "Connect to AP with specified port. If no AP with that port are present fallback AP will be used. Available ports are usually 80, 443 and 4070.", "PORT") - .optflag("", DISABLE_DISCOVERY, "Disable discovery mode.") - .optopt( - "", - BACKEND, - "Audio backend to use. Use '?' to list options.", - "NAME", - ) - .optopt( - "", - DEVICE, - "Audio device to use. Use '?' to list options if using alsa, portaudio or rodio.", - "NAME", - ) - .optopt( - "", + FORMAT_SHORT, FORMAT, "Output format {F64|F32|S32|S24|S24_3|S16}. Defaults to S16.", "FORMAT", ) .optopt( - "", + DITHER_SHORT, DITHER, - "Specify the dither algorithm to use - [none, gpdf, tpdf, tpdf_hp]. Defaults to 'tpdf' for formats S16, S24, S24_3 and 'none' for other formats.", + "Specify the dither algorithm to use {none|gpdf|tpdf|tpdf_hp}. Defaults to tpdf for formats S16, S24, S24_3 and none for other formats.", "DITHER", ) - .optopt("", "mixer", "Mixer to use {alsa|softvol}.", "MIXER") .optopt( - "m", - MIXER_NAME, - "Alsa mixer control, e.g. 'PCM' or 'Master'. Defaults to 'PCM'.", + DEVICE_TYPE_SHORT, + DEVICE_TYPE, + "Displayed device type. Defaults to speaker.", + "TYPE", + ) + .optopt( + CACHE_SHORT, + CACHE, + "Path to a directory where files will be cached.", + "PATH", + ) + .optopt( + SYSTEM_CACHE_SHORT, + SYSTEM_CACHE, + "Path to a directory where system files (credentials, volume) will be cached. May be different from the `--cache` option value.", + "PATH", + ) + .optopt( + CACHE_SIZE_LIMIT_SHORT, + CACHE_SIZE_LIMIT, + "Limits the size of the cache for audio files. It's possible to use suffixes like K, M or G, e.g. 16G for example.", + "SIZE" + ) + .optopt( + BACKEND_SHORT, + BACKEND, + "Audio backend to use. Use ? to list options.", "NAME", ) .optopt( - "", - MIXER_CARD, - "Alsa mixer card, e.g 'hw:0' or similar from `aplay -l`. Defaults to DEVICE if specified, 'default' otherwise.", - "MIXER_CARD", + USERNAME_SHORT, + USERNAME, + "Username used to sign in with.", + "USERNAME", ) .optopt( - "", - MIXER_INDEX, - "Alsa index of the cards mixer. Defaults to 0.", - "INDEX", + PASSWORD_SHORT, + PASSWORD, + "Password used to sign in with.", + "PASSWORD", ) .optopt( - "", + ONEVENT_SHORT, + ONEVENT, + "Run PROGRAM when a playback event occurs.", + "PROGRAM", + ) + .optopt( + ALSA_MIXER_CONTROL_SHORT, + ALSA_MIXER_CONTROL, + ALSA_MIXER_CONTROL_DESC, + "NAME", + ) + .optopt( + ALSA_MIXER_DEVICE_SHORT, + ALSA_MIXER_DEVICE, + ALSA_MIXER_DEVICE_DESC, + "DEVICE", + ) + .optopt( + ALSA_MIXER_INDEX_SHORT, + ALSA_MIXER_INDEX, + ALSA_MIXER_INDEX_DESC, + "NUMBER", + ) + .optopt( + MIXER_TYPE_SHORT, + MIXER_TYPE, + MIXER_TYPE_DESC, + "MIXER", + ) + .optopt( + DEVICE_SHORT, + DEVICE, + DEVICE_DESC, + "NAME", + ) + .optopt( + INITIAL_VOLUME_SHORT, INITIAL_VOLUME, - "Initial volume in % from 0-100. Default for softvol: '50'. For the Alsa mixer: the current volume.", + INITIAL_VOLUME_DESC, "VOLUME", ) .optopt( - "", - ZEROCONF_PORT, - "The port the internal server advertised over zeroconf uses.", - "PORT", - ) - .optflag( - "", - ENABLE_VOLUME_NORMALISATION, - "Play all tracks at the same volume.", + VOLUME_CTRL_SHORT, + VOLUME_CTRL, + "Volume control scale type {cubic|fixed|linear|log}. Defaults to log.", + "VOLUME_CTRL" ) .optopt( - "", + VOLUME_RANGE_SHORT, + VOLUME_RANGE, + VOLUME_RANGE_DESC, + "RANGE", + ) + .optopt( + NORMALISATION_METHOD_SHORT, NORMALISATION_METHOD, "Specify the normalisation method to use {basic|dynamic}. Defaults to dynamic.", "METHOD", ) .optopt( - "", + NORMALISATION_GAIN_TYPE_SHORT, NORMALISATION_GAIN_TYPE, - "Specify the normalisation gain type to use {track|album}. Defaults to album.", + "Specify the normalisation gain type to use {track|album|auto}. Defaults to auto.", "TYPE", ) .optopt( - "", + NORMALISATION_PREGAIN_SHORT, NORMALISATION_PREGAIN, - "Pregain (dB) applied by volume normalisation. Defaults to 0.", + "Pregain (dB) applied by volume normalisation from -10.0 to 10.0. Defaults to 0.0.", "PREGAIN", ) .optopt( - "", + NORMALISATION_THRESHOLD_SHORT, NORMALISATION_THRESHOLD, - "Threshold (dBFS) to prevent clipping. Defaults to -1.0.", + "Threshold (dBFS) at which point the dynamic limiter engages to prevent clipping from 0.0 to -10.0. Defaults to -2.0.", "THRESHOLD", ) .optopt( - "", + NORMALISATION_ATTACK_SHORT, NORMALISATION_ATTACK, - "Attack time (ms) in which the dynamic limiter is reducing gain. Defaults to 5.", + "Attack time (ms) in which the dynamic limiter reduces gain from 1 to 500. Defaults to 5.", "TIME", ) .optopt( - "", + NORMALISATION_RELEASE_SHORT, NORMALISATION_RELEASE, - "Release or decay time (ms) in which the dynamic limiter is restoring gain. Defaults to 100.", + "Release or decay time (ms) in which the dynamic limiter restores gain from 1 to 1000. Defaults to 100.", "TIME", ) .optopt( - "", + NORMALISATION_KNEE_SHORT, NORMALISATION_KNEE, - "Knee steepness of the dynamic limiter. Defaults to 1.0.", + "Knee steepness of the dynamic limiter from 0.0 to 2.0. Defaults to 1.0.", "KNEE", ) .optopt( - "", - VOLUME_CTRL, - "Volume control type {cubic|fixed|linear|log}. Defaults to log.", - "VOLUME_CTRL" + ZEROCONF_PORT_SHORT, + ZEROCONF_PORT, + "The port the internal server advertises over zeroconf 1 - 65535. Ports <= 1024 may require root privileges.", + "PORT", ) .optopt( - "", - VOLUME_RANGE, - "Range of the volume control (dB). Default for softvol: 60. For the Alsa mixer: what the control supports.", - "RANGE", + PROXY_SHORT, + PROXY, + "HTTP proxy to use when connecting.", + "URL", ) - .optflag( - "", - AUTOPLAY, - "Automatically play similar songs when your music ends.", - ) - .optflag( - "", - DISABLE_GAPLESS, - "Disable gapless playback.", - ) - .optflag( - "", - PASSTHROUGH, - "Pass raw stream to output, only works for pipe and subprocess.", + .optopt( + AP_PORT_SHORT, + AP_PORT, + "Connect to an AP with a specified port 1 - 65535. If no AP with that port is present a fallback AP will be used. Available ports are usually 80, 443 and 4070.", + "PORT", ); let matches = match opts.parse(&args[1..]) { @@ -413,96 +608,284 @@ fn get_setup(args: &[String]) -> Setup { } }; - if matches.opt_present(HELP) { + let opt_present = |opt| matches.opt_present(opt) || env_var_present(opt); + + let opt_str = |opt| { + if matches.opt_present(opt) { + matches.opt_str(opt) + } else { + env_var_opt_str(opt) + } + }; + + if opt_present(HELP) { println!("{}", usage(&args[0], &opts)); exit(0); } - if matches.opt_present(VERSION) { - print_version(); + if opt_present(VERSION) { + println!("{}", get_version_string()); exit(0); } - let verbose = matches.opt_present(VERBOSE); - setup_logging(verbose); + setup_logging(opt_present(QUIET), opt_present(VERBOSE)); - info!( - "librespot {semver} {sha} (Built on {build_date}, Build ID: {build_id})", - semver = version::SEMVER, - sha = version::SHA_SHORT, - build_date = version::BUILD_DATE, - build_id = version::BUILD_ID - ); + info!("{}", get_version_string()); - let backend_name = matches.opt_str(BACKEND); + let librespot_env_vars: Vec = env::vars_os() + .filter_map(|(k, v)| { + let mut env_var = None; + if let Some(key) = k.to_str() { + if key.starts_with("LIBRESPOT_") { + if matches!(key, "LIBRESPOT_PASSWORD" | "LIBRESPOT_USERNAME") { + // Don't log creds. + env_var = Some(format!("\t\t{}=XXXXXXXX", key)); + } else if let Some(value) = v.to_str() { + env_var = Some(format!("\t\t{}={}", key, value)); + } + } + } + + env_var + }) + .collect(); + + if !librespot_env_vars.is_empty() { + trace!("Environment variable(s):"); + + for kv in librespot_env_vars { + trace!("{}", kv); + } + } + + let cmd_args = &args[1..]; + + let cmd_args_len = cmd_args.len(); + + if cmd_args_len > 0 { + trace!("Command line argument(s):"); + + for (index, key) in cmd_args.iter().enumerate() { + if key.starts_with('-') || key.starts_with("--") { + if matches!(key.as_str(), "--password" | "-p" | "--username" | "-u") { + // Don't log creds. + trace!("\t\t{} XXXXXXXX", key); + } else { + let mut value = "".to_string(); + let next = index + 1; + if next < cmd_args_len { + let next_key = cmd_args[next].clone(); + if !next_key.starts_with('-') && !next_key.starts_with("--") { + value = next_key; + } + } + + trace!("\t\t{} {}", key, value); + } + } + } + } + + #[cfg(not(feature = "alsa-backend"))] + for a in &[ + MIXER_TYPE, + ALSA_MIXER_DEVICE, + ALSA_MIXER_INDEX, + ALSA_MIXER_CONTROL, + ] { + if opt_present(a) { + warn!("Alsa specific options have no effect if the alsa backend is not enabled at build time."); + break; + } + } + + let backend_name = opt_str(BACKEND); if backend_name == Some("?".into()) { list_backends(); exit(0); } - let backend = audio_backend::find(backend_name).expect("Invalid backend"); + let backend = audio_backend::find(backend_name).unwrap_or_else(|| { + error!( + "Invalid `--{}` / `-{}`: {}", + BACKEND, + BACKEND_SHORT, + opt_str(BACKEND).unwrap_or_default() + ); + list_backends(); + exit(1); + }); - let format = matches - .opt_str(FORMAT) + let format = opt_str(FORMAT) .as_deref() - .map(|format| AudioFormat::from_str(format).expect("Invalid output format")) + .map(|format| { + AudioFormat::from_str(format).unwrap_or_else(|_| { + error!("Invalid `--{}` / `-{}`: {}", FORMAT, FORMAT_SHORT, format); + println!( + "Valid `--{}` / `-{}` values: F64, F32, S32, S24, S24_3, S16", + FORMAT, FORMAT_SHORT + ); + println!("Default: {:?}", AudioFormat::default()); + exit(1); + }) + }) .unwrap_or_default(); - let device = matches.opt_str(DEVICE); + #[cfg(any( + feature = "alsa-backend", + feature = "rodio-backend", + feature = "portaudio-backend" + ))] + let device = opt_str(DEVICE); + + #[cfg(any( + feature = "alsa-backend", + feature = "rodio-backend", + feature = "portaudio-backend" + ))] if device == Some("?".into()) { backend(device, format); exit(0); } - let mixer_name = matches.opt_str(MIXER_NAME); - let mixer = mixer::find(mixer_name.as_deref()).expect("Invalid mixer"); + #[cfg(not(any( + feature = "alsa-backend", + feature = "rodio-backend", + feature = "portaudio-backend" + )))] + let device: Option = None; + + #[cfg(not(any( + feature = "alsa-backend", + feature = "rodio-backend", + feature = "portaudio-backend" + )))] + if opt_present(DEVICE) { + warn!( + "The `--{}` / `-{}` option is not supported by the included audio backend(s), and has no effect.", + DEVICE, DEVICE_SHORT, + ); + } + + #[cfg(feature = "alsa-backend")] + let mixer_type = opt_str(MIXER_TYPE); + #[cfg(not(feature = "alsa-backend"))] + let mixer_type: Option = None; + + let mixer = mixer::find(mixer_type.as_deref()).unwrap_or_else(|| { + error!( + "Invalid `--{}` / `-{}`: {}", + MIXER_TYPE, + MIXER_TYPE_SHORT, + opt_str(MIXER_TYPE).unwrap_or_default() + ); + println!( + "Valid `--{}` / `-{}` values: alsa, softvol", + MIXER_TYPE, MIXER_TYPE_SHORT + ); + println!("Default: softvol"); + exit(1); + }); let mixer_config = { - let card = matches.opt_str(MIXER_CARD).unwrap_or_else(|| { + let mixer_default_config = MixerConfig::default(); + + #[cfg(feature = "alsa-backend")] + let device = opt_str(ALSA_MIXER_DEVICE).unwrap_or_else(|| { if let Some(ref device_name) = device { device_name.to_string() } else { - MixerConfig::default().card + mixer_default_config.device.clone() } }); - let index = matches - .opt_str(MIXER_INDEX) - .map(|index| index.parse::().unwrap()) - .unwrap_or(0); - let control = matches - .opt_str(MIXER_NAME) - .unwrap_or_else(|| MixerConfig::default().control); - let mut volume_range = matches - .opt_str(VOLUME_RANGE) - .map(|range| range.parse::().unwrap()) - .unwrap_or_else(|| match mixer_name.as_deref() { + + #[cfg(not(feature = "alsa-backend"))] + let device = mixer_default_config.device; + + #[cfg(feature = "alsa-backend")] + let index = opt_str(ALSA_MIXER_INDEX) + .map(|index| { + index.parse::().unwrap_or_else(|_| { + error!( + "Invalid `--{}` / `-{}`: {}", + ALSA_MIXER_INDEX, ALSA_MIXER_INDEX_SHORT, index + ); + println!("Default: {}", mixer_default_config.index); + exit(1); + }) + }) + .unwrap_or_else(|| mixer_default_config.index); + + #[cfg(not(feature = "alsa-backend"))] + let index = mixer_default_config.index; + + #[cfg(feature = "alsa-backend")] + let control = opt_str(ALSA_MIXER_CONTROL).unwrap_or(mixer_default_config.control); + + #[cfg(not(feature = "alsa-backend"))] + let control = mixer_default_config.control; + + let volume_range = opt_str(VOLUME_RANGE) + .map(|range| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + VOLUME_RANGE, VOLUME_RANGE_SHORT, range + ); + println!( + "Valid `--{}` / `-{}` values: {} - {}", + VOLUME_RANGE, + VOLUME_RANGE_SHORT, + VALID_VOLUME_RANGE.start(), + VALID_VOLUME_RANGE.end() + ); + #[cfg(feature = "alsa-backend")] + println!( + "Default: softvol - {}, alsa - what the control supports", + VolumeCtrl::DEFAULT_DB_RANGE + ); + #[cfg(not(feature = "alsa-backend"))] + println!("Default: {}", VolumeCtrl::DEFAULT_DB_RANGE); + }; + + let range = range.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if !(VALID_VOLUME_RANGE).contains(&range) { + on_error(); + exit(1); + } + + range + }) + .unwrap_or_else(|| match mixer_type.as_deref() { #[cfg(feature = "alsa-backend")] - Some(AlsaMixer::NAME) => 0.0, // let Alsa query the control + Some(AlsaMixer::NAME) => 0.0, // let alsa query the control _ => VolumeCtrl::DEFAULT_DB_RANGE, }); - if volume_range < 0.0 { - // User might have specified range as minimum dB volume. - volume_range = -volume_range; - warn!( - "Please enter positive volume ranges only, assuming {:.2} dB", - volume_range - ); - } - let volume_ctrl = matches - .opt_str(VOLUME_CTRL) + + let volume_ctrl = opt_str(VOLUME_CTRL) .as_deref() .map(|volume_ctrl| { - VolumeCtrl::from_str_with_range(volume_ctrl, volume_range) - .expect("Invalid volume control type") + VolumeCtrl::from_str_with_range(volume_ctrl, volume_range).unwrap_or_else(|_| { + error!( + "Invalid `--{}` / `-{}`: {}", + VOLUME_CTRL, VOLUME_CTRL_SHORT, volume_ctrl + ); + println!( + "Valid `--{}` / `-{}` values: cubic, fixed, linear, log", + VOLUME_CTRL, VOLUME_CTRL + ); + println!("Default: log"); + exit(1); + }) }) - .unwrap_or_else(|| { - let mut volume_ctrl = VolumeCtrl::default(); - volume_ctrl.set_db_range(volume_range); - volume_ctrl - }); + .unwrap_or_else(|| VolumeCtrl::Log(volume_range)); MixerConfig { - card, + device, control, index, volume_ctrl, @@ -510,33 +893,34 @@ fn get_setup(args: &[String]) -> Setup { }; let cache = { - let audio_dir; - let system_dir; - if matches.opt_present(DISABLE_AUDIO_CACHE) { - audio_dir = None; - system_dir = matches - .opt_str(SYSTEM_CACHE) - .or_else(|| matches.opt_str(CACHE)) - .map(|p| p.into()); + let volume_dir = opt_str(SYSTEM_CACHE) + .or_else(|| opt_str(CACHE)) + .map(|p| p.into()); + + let cred_dir = if opt_present(DISABLE_CREDENTIAL_CACHE) { + None } else { - let cache_dir = matches.opt_str(CACHE); - audio_dir = cache_dir + volume_dir.clone() + }; + + let audio_dir = if opt_present(DISABLE_AUDIO_CACHE) { + None + } else { + opt_str(CACHE) .as_ref() - .map(|p| AsRef::::as_ref(p).join("files")); - system_dir = matches - .opt_str(SYSTEM_CACHE) - .or(cache_dir) - .map(|p| p.into()); - } + .map(|p| AsRef::::as_ref(p).join("files")) + }; let limit = if audio_dir.is_some() { - matches - .opt_str(CACHE_SIZE_LIMIT) + opt_str(CACHE_SIZE_LIMIT) .as_deref() .map(parse_file_size) .map(|e| { e.unwrap_or_else(|e| { - eprintln!("Invalid argument passed as cache size limit: {}", e); + error!( + "Invalid `--{}` / `-{}`: {}", + CACHE_SIZE_LIMIT, CACHE_SIZE_LIMIT_SHORT, e + ); exit(1); }) }) @@ -544,7 +928,14 @@ fn get_setup(args: &[String]) -> Setup { None }; - match Cache::new(system_dir, audio_dir, limit) { + if audio_dir.is_none() && opt_present(CACHE_SIZE_LIMIT) { + warn!( + "Without a `--{}` / `-{}` path, and/or if the `--{}` / `-{}` flag is set, `--{}` / `-{}` has no effect.", + CACHE, CACHE_SHORT, DISABLE_AUDIO_CACHE, DISABLE_AUDIO_CACHE_SHORT, CACHE_SIZE_LIMIT, CACHE_SIZE_LIMIT_SHORT + ); + } + + match Cache::new(cred_dir, volume_dir, audio_dir, limit) { Ok(cache) => Some(cache), Err(e) => { warn!("Cannot create cache: {}", e); @@ -553,31 +944,6 @@ fn get_setup(args: &[String]) -> Setup { } }; - let initial_volume = matches - .opt_str(INITIAL_VOLUME) - .map(|initial_volume| { - let volume = initial_volume.parse::().unwrap(); - if volume > 100 { - error!("Initial volume must be in the range 0-100."); - // the cast will saturate, not necessary to take further action - } - (volume as f32 / 100.0 * VolumeCtrl::MAX_VOLUME as f32) as u16 - }) - .or_else(|| match mixer_name.as_deref() { - #[cfg(feature = "alsa-backend")] - Some(AlsaMixer::NAME) => None, - _ => cache.as_ref().and_then(Cache::volume), - }); - - let zeroconf_port = matches - .opt_str(ZEROCONF_PORT) - .map(|port| port.parse::().unwrap()) - .unwrap_or(0); - - let name = matches - .opt_str(NAME) - .unwrap_or_else(|| "Librespot".to_string()); - let credentials = { let cached_credentials = cache.as_ref().and_then(Cache::credentials); @@ -588,114 +954,490 @@ fn get_setup(args: &[String]) -> Setup { }; get_credentials( - matches.opt_str(USERNAME), - matches.opt_str(PASSWORD), + opt_str(USERNAME), + opt_str(PASSWORD), cached_credentials, password, ) }; + let enable_discovery = !opt_present(DISABLE_DISCOVERY); + + if credentials.is_none() && !enable_discovery { + error!("Credentials are required if discovery is disabled."); + exit(1); + } + + if !enable_discovery && opt_present(ZEROCONF_PORT) { + warn!( + "With the `--{}` / `-{}` flag set `--{}` / `-{}` has no effect.", + DISABLE_DISCOVERY, DISABLE_DISCOVERY_SHORT, ZEROCONF_PORT, ZEROCONF_PORT_SHORT + ); + } + + let zeroconf_port = if enable_discovery { + opt_str(ZEROCONF_PORT) + .map(|port| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + ZEROCONF_PORT, ZEROCONF_PORT_SHORT, port + ); + println!( + "Valid `--{}` / `-{}` values: 1 - 65535", + ZEROCONF_PORT, ZEROCONF_PORT_SHORT + ); + }; + + let port = port.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if port == 0 { + on_error(); + exit(1); + } + + port + }) + .unwrap_or(0) + } else { + 0 + }; + + let connect_config = { + let connect_default_config = ConnectConfig::default(); + + let name = opt_str(NAME).unwrap_or_else(|| connect_default_config.name.clone()); + + let initial_volume = opt_str(INITIAL_VOLUME) + .map(|initial_volume| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + INITIAL_VOLUME, INITIAL_VOLUME_SHORT, initial_volume + ); + println!( + "Valid `--{}` / `-{}` values: 0 - 100", + INITIAL_VOLUME, INITIAL_VOLUME_SHORT + ); + #[cfg(feature = "alsa-backend")] + println!( + "Default: {}, or the current value when the alsa mixer is used.", + connect_default_config.initial_volume.unwrap_or_default() + ); + #[cfg(not(feature = "alsa-backend"))] + println!( + "Default: {}", + connect_default_config.initial_volume.unwrap_or_default() + ); + }; + + let volume = initial_volume.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if volume > 100 { + on_error(); + exit(1); + } + + (volume as f32 / 100.0 * VolumeCtrl::MAX_VOLUME as f32) as u16 + }) + .or_else(|| match mixer_type.as_deref() { + #[cfg(feature = "alsa-backend")] + Some(AlsaMixer::NAME) => None, + _ => cache.as_ref().and_then(Cache::volume), + }); + + let device_type = opt_str(DEVICE_TYPE) + .as_deref() + .map(|device_type| { + DeviceType::from_str(device_type).unwrap_or_else(|_| { + error!("Invalid `--{}` / `-{}`: {}", DEVICE_TYPE, DEVICE_TYPE_SHORT, device_type); + println!("Valid `--{}` / `-{}` values: computer, tablet, smartphone, speaker, tv, avr, stb, audiodongle, \ + gameconsole, castaudio, castvideo, automobile, smartwatch, chromebook, carthing, homething", + DEVICE_TYPE, DEVICE_TYPE_SHORT + ); + println!("Default: speaker"); + exit(1); + }) + }) + .unwrap_or_default(); + + let has_volume_ctrl = !matches!(mixer_config.volume_ctrl, VolumeCtrl::Fixed); + let autoplay = opt_present(AUTOPLAY); + + ConnectConfig { + name, + device_type, + initial_volume, + has_volume_ctrl, + autoplay, + } + }; + let session_config = { - let device_id = device_id(&name); + let device_id = device_id(&connect_config.name); SessionConfig { user_agent: version::VERSION_STRING.to_string(), device_id, - proxy: matches.opt_str(PROXY).or_else(|| std::env::var("http_proxy").ok()).map( + proxy: opt_str(PROXY).or_else(|| std::env::var("http_proxy").ok()).map( |s| { match Url::parse(&s) { Ok(url) => { if url.host().is_none() || url.port_or_known_default().is_none() { - panic!("Invalid proxy url, only URLs on the format \"http(s)://host:port\" are allowed"); + error!("Invalid proxy URL, only URLs in the format \"http(s)://host:port\" are allowed"); + exit(1); } + url }, - Err(err) => panic!("Invalid proxy URL: {}, only URLs in the format \"http(s)://host:port\" are allowed", err) + Err(e) => { + error!("Invalid proxy URL: {}, only URLs in the format \"http(s)://host:port\" are allowed", e); + exit(1); + } } }, ), - ap_port: matches - .opt_str(AP_PORT) - .map(|port| port.parse::().expect("Invalid port")), + ap_port: opt_str(AP_PORT) + .map(|port| { + let on_error = || { + error!("Invalid `--{}` / `-{}`: {}", AP_PORT, AP_PORT_SHORT, port); + println!("Valid `--{}` / `-{}` values: 1 - 65535", AP_PORT, AP_PORT_SHORT); + }; + + let port = port.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if port == 0 { + on_error(); + exit(1); + } + + port + }), } }; let player_config = { - let bitrate = matches - .opt_str(BITRATE) - .as_deref() - .map(|bitrate| Bitrate::from_str(bitrate).expect("Invalid bitrate")) - .unwrap_or_default(); + let player_default_config = PlayerConfig::default(); - let gapless = !matches.opt_present(DISABLE_GAPLESS); - - let normalisation = matches.opt_present(ENABLE_VOLUME_NORMALISATION); - let normalisation_method = matches - .opt_str(NORMALISATION_METHOD) + let bitrate = opt_str(BITRATE) .as_deref() - .map(|method| { - NormalisationMethod::from_str(method).expect("Invalid normalisation method") + .map(|bitrate| { + Bitrate::from_str(bitrate).unwrap_or_else(|_| { + error!( + "Invalid `--{}` / `-{}`: {}", + BITRATE, BITRATE_SHORT, bitrate + ); + println!( + "Valid `--{}` / `-{}` values: 96, 160, 320", + BITRATE, BITRATE_SHORT + ); + println!("Default: 160"); + exit(1); + }) }) - .unwrap_or_default(); - let normalisation_type = matches - .opt_str(NORMALISATION_GAIN_TYPE) - .as_deref() - .map(|gain_type| { - NormalisationType::from_str(gain_type).expect("Invalid normalisation type") - }) - .unwrap_or_default(); - let normalisation_pregain = matches - .opt_str(NORMALISATION_PREGAIN) - .map(|pregain| pregain.parse::().expect("Invalid pregain float value")) - .unwrap_or(PlayerConfig::default().normalisation_pregain); - let normalisation_threshold = matches - .opt_str(NORMALISATION_THRESHOLD) - .map(|threshold| { - db_to_ratio( - threshold - .parse::() - .expect("Invalid threshold float value"), - ) - }) - .unwrap_or(PlayerConfig::default().normalisation_threshold); - let normalisation_attack = matches - .opt_str(NORMALISATION_ATTACK) - .map(|attack| { - Duration::from_millis(attack.parse::().expect("Invalid attack value")) - }) - .unwrap_or(PlayerConfig::default().normalisation_attack); - let normalisation_release = matches - .opt_str(NORMALISATION_RELEASE) - .map(|release| { - Duration::from_millis(release.parse::().expect("Invalid release value")) - }) - .unwrap_or(PlayerConfig::default().normalisation_release); - let normalisation_knee = matches - .opt_str(NORMALISATION_KNEE) - .map(|knee| knee.parse::().expect("Invalid knee float value")) - .unwrap_or(PlayerConfig::default().normalisation_knee); + .unwrap_or(player_default_config.bitrate); - let ditherer_name = matches.opt_str(DITHER); + let gapless = !opt_present(DISABLE_GAPLESS); + + let normalisation = opt_present(ENABLE_VOLUME_NORMALISATION); + + let normalisation_method; + let normalisation_type; + let normalisation_pregain; + let normalisation_threshold; + let normalisation_attack; + let normalisation_release; + let normalisation_knee; + + if !normalisation { + for a in &[ + NORMALISATION_METHOD, + NORMALISATION_GAIN_TYPE, + NORMALISATION_PREGAIN, + NORMALISATION_THRESHOLD, + NORMALISATION_ATTACK, + NORMALISATION_RELEASE, + NORMALISATION_KNEE, + ] { + if opt_present(a) { + warn!( + "Without the `--{}` / `-{}` flag normalisation options have no effect.", + ENABLE_VOLUME_NORMALISATION, ENABLE_VOLUME_NORMALISATION_SHORT, + ); + break; + } + } + + normalisation_method = player_default_config.normalisation_method; + normalisation_type = player_default_config.normalisation_type; + normalisation_pregain = player_default_config.normalisation_pregain; + normalisation_threshold = player_default_config.normalisation_threshold; + normalisation_attack = player_default_config.normalisation_attack; + normalisation_release = player_default_config.normalisation_release; + normalisation_knee = player_default_config.normalisation_knee; + } else { + normalisation_method = opt_str(NORMALISATION_METHOD) + .as_deref() + .map(|method| { + warn!( + "`--{}` / `-{}` will be deprecated in a future release.", + NORMALISATION_METHOD, NORMALISATION_METHOD_SHORT + ); + + let method = NormalisationMethod::from_str(method).unwrap_or_else(|_| { + error!( + "Invalid `--{}` / `-{}`: {}", + NORMALISATION_METHOD, NORMALISATION_METHOD_SHORT, method + ); + println!( + "Valid `--{}` / `-{}` values: basic, dynamic", + NORMALISATION_METHOD, NORMALISATION_METHOD_SHORT + ); + println!("Default: {:?}", player_default_config.normalisation_method); + exit(1); + }); + + if matches!(method, NormalisationMethod::Basic) { + warn!( + "`--{}` / `-{}` {:?} will be deprecated in a future release.", + NORMALISATION_METHOD, NORMALISATION_METHOD_SHORT, method + ); + } + + method + }) + .unwrap_or(player_default_config.normalisation_method); + + normalisation_type = opt_str(NORMALISATION_GAIN_TYPE) + .as_deref() + .map(|gain_type| { + NormalisationType::from_str(gain_type).unwrap_or_else(|_| { + error!( + "Invalid `--{}` / `-{}`: {}", + NORMALISATION_GAIN_TYPE, NORMALISATION_GAIN_TYPE_SHORT, gain_type + ); + println!( + "Valid `--{}` / `-{}` values: track, album, auto", + NORMALISATION_GAIN_TYPE, NORMALISATION_GAIN_TYPE_SHORT, + ); + println!("Default: {:?}", player_default_config.normalisation_type); + exit(1); + }) + }) + .unwrap_or(player_default_config.normalisation_type); + + normalisation_pregain = opt_str(NORMALISATION_PREGAIN) + .map(|pregain| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + NORMALISATION_PREGAIN, NORMALISATION_PREGAIN_SHORT, pregain + ); + println!( + "Valid `--{}` / `-{}` values: {} - {}", + NORMALISATION_PREGAIN, + NORMALISATION_PREGAIN_SHORT, + VALID_NORMALISATION_PREGAIN_RANGE.start(), + VALID_NORMALISATION_PREGAIN_RANGE.end() + ); + println!("Default: {}", player_default_config.normalisation_pregain); + }; + + let pregain = pregain.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if !(VALID_NORMALISATION_PREGAIN_RANGE).contains(&pregain) { + on_error(); + exit(1); + } + + pregain + }) + .unwrap_or(player_default_config.normalisation_pregain); + + normalisation_threshold = opt_str(NORMALISATION_THRESHOLD) + .map(|threshold| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + NORMALISATION_THRESHOLD, NORMALISATION_THRESHOLD_SHORT, threshold + ); + println!( + "Valid `--{}` / `-{}` values: {} - {}", + NORMALISATION_THRESHOLD, + NORMALISATION_THRESHOLD_SHORT, + VALID_NORMALISATION_THRESHOLD_RANGE.start(), + VALID_NORMALISATION_THRESHOLD_RANGE.end() + ); + println!( + "Default: {}", + ratio_to_db(player_default_config.normalisation_threshold) + ); + }; + + let threshold = threshold.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if !(VALID_NORMALISATION_THRESHOLD_RANGE).contains(&threshold) { + on_error(); + exit(1); + } + + db_to_ratio(threshold) + }) + .unwrap_or(player_default_config.normalisation_threshold); + + normalisation_attack = opt_str(NORMALISATION_ATTACK) + .map(|attack| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + NORMALISATION_ATTACK, NORMALISATION_ATTACK_SHORT, attack + ); + println!( + "Valid `--{}` / `-{}` values: {} - {}", + NORMALISATION_ATTACK, + NORMALISATION_ATTACK_SHORT, + VALID_NORMALISATION_ATTACK_RANGE.start(), + VALID_NORMALISATION_ATTACK_RANGE.end() + ); + println!( + "Default: {}", + player_default_config.normalisation_attack.as_millis() + ); + }; + + let attack = attack.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if !(VALID_NORMALISATION_ATTACK_RANGE).contains(&attack) { + on_error(); + exit(1); + } + + Duration::from_millis(attack) + }) + .unwrap_or(player_default_config.normalisation_attack); + + normalisation_release = opt_str(NORMALISATION_RELEASE) + .map(|release| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + NORMALISATION_RELEASE, NORMALISATION_RELEASE_SHORT, release + ); + println!( + "Valid `--{}` / `-{}` values: {} - {}", + NORMALISATION_RELEASE, + NORMALISATION_RELEASE_SHORT, + VALID_NORMALISATION_RELEASE_RANGE.start(), + VALID_NORMALISATION_RELEASE_RANGE.end() + ); + println!( + "Default: {}", + player_default_config.normalisation_release.as_millis() + ); + }; + + let release = release.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if !(VALID_NORMALISATION_RELEASE_RANGE).contains(&release) { + on_error(); + exit(1); + } + + Duration::from_millis(release) + }) + .unwrap_or(player_default_config.normalisation_release); + + normalisation_knee = opt_str(NORMALISATION_KNEE) + .map(|knee| { + let on_error = || { + error!( + "Invalid `--{}` / `-{}`: {}", + NORMALISATION_KNEE, NORMALISATION_KNEE_SHORT, knee + ); + println!( + "Valid `--{}` / `-{}` values: {} - {}", + NORMALISATION_KNEE, + NORMALISATION_KNEE_SHORT, + VALID_NORMALISATION_KNEE_RANGE.start(), + VALID_NORMALISATION_KNEE_RANGE.end() + ); + println!("Default: {}", player_default_config.normalisation_knee); + }; + + let knee = knee.parse::().unwrap_or_else(|_| { + on_error(); + exit(1); + }); + + if !(VALID_NORMALISATION_KNEE_RANGE).contains(&knee) { + on_error(); + exit(1); + } + + knee + }) + .unwrap_or(player_default_config.normalisation_knee); + } + + let ditherer_name = opt_str(DITHER); let ditherer = match ditherer_name.as_deref() { // explicitly disabled on command line Some("none") => None, // explicitly set on command line Some(_) => { - if format == AudioFormat::F64 || format == AudioFormat::F32 { - unimplemented!("Dithering is not available on format {:?}", format); + if matches!(format, AudioFormat::F64 | AudioFormat::F32) { + error!("Dithering is not available with format: {:?}.", format); + exit(1); } - Some(dither::find_ditherer(ditherer_name).expect("Invalid ditherer")) + + Some(dither::find_ditherer(ditherer_name).unwrap_or_else(|| { + error!( + "Invalid `--{}` / `-{}`: {}", + DITHER, + DITHER_SHORT, + opt_str(DITHER).unwrap_or_default() + ); + println!( + "Valid `--{}` / `-{}` values: none, gpdf, tpdf, tpdf_hp", + DITHER, DITHER_SHORT + ); + println!( + "Default: tpdf for formats S16, S24, S24_3 and none for other formats" + ); + exit(1); + })) } // nothing set on command line => use default None => match format { AudioFormat::S16 | AudioFormat::S24 | AudioFormat::S24_3 => { - PlayerConfig::default().ditherer + player_default_config.ditherer } _ => None, }, }; - let passthrough = matches.opt_present(PASSTHROUGH); + let passthrough = opt_present(PASSTHROUGH); PlayerConfig { bitrate, @@ -713,27 +1455,8 @@ fn get_setup(args: &[String]) -> Setup { } }; - let connect_config = { - let device_type = matches - .opt_str(DEVICE_TYPE) - .as_deref() - .map(|device_type| DeviceType::from_str(device_type).expect("Invalid device type")) - .unwrap_or_default(); - let has_volume_ctrl = !matches!(mixer_config.volume_ctrl, VolumeCtrl::Fixed); - let autoplay = matches.opt_present(AUTOPLAY); - - ConnectConfig { - name, - device_type, - initial_volume, - has_volume_ctrl, - autoplay, - } - }; - - let enable_discovery = !matches.opt_present(DISABLE_DISCOVERY); - let player_event_program = matches.opt_str(ONEVENT); - let emit_sink_events = matches.opt_present(EMIT_SINK_EVENTS); + let player_event_program = opt_str(ONEVENT); + let emit_sink_events = opt_present(EMIT_SINK_EVENTS); Setup { format, @@ -867,7 +1590,8 @@ async fn main() { player_event_channel = Some(event_channel); }, Err(e) => { - warn!("Connection failed: {}", e); + error!("Connection failed: {}", e); + exit(1); } }, _ = async { spirc_task.as_mut().unwrap().await }, if spirc_task.is_some() => {