Skip to content

Commit

Permalink
Merge pull request #1302 from JoeyEamigh/add-group-flag
Browse files Browse the repository at this point in the history
add flag to display connect device as group
  • Loading branch information
roderickvd committed Jun 17, 2024
2 parents c10d29b + 180ffac commit cdff6da
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions connect/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::core::config::DeviceType;
pub struct ConnectConfig {
pub name: String,
pub device_type: DeviceType,
pub is_group: bool,
pub initial_volume: Option<u16>,
pub has_volume_ctrl: bool,
}
Expand All @@ -13,6 +14,7 @@ impl Default for ConnectConfig {
ConnectConfig {
name: "Librespot".to_string(),
device_type: DeviceType::default(),
is_group: false,
initial_volume: Some(50),
has_volume_ctrl: true,
}
Expand Down
22 changes: 22 additions & 0 deletions discovery/examples/discovery_group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use futures::StreamExt;
use librespot_core::SessionConfig;
use librespot_discovery::DeviceType;
use sha1::{Digest, Sha1};

#[tokio::main(flavor = "current_thread")]
async fn main() {
let name = "Librespot Group";
let device_id = hex::encode(Sha1::digest(name.as_bytes()));

let mut server =
librespot_discovery::Discovery::builder(device_id, SessionConfig::default().client_id)
.name(name)
.device_type(DeviceType::Speaker)
.is_group(true)
.launch()
.unwrap();

while let Some(x) = server.next().await {
println!("Received {:?}", x);
}
}
7 changes: 7 additions & 0 deletions discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ impl Builder {
server_config: server::Config {
name: "Librespot".into(),
device_type: DeviceType::default(),
is_group: false,
device_id: device_id.into(),
client_id: client_id.into(),
},
Expand All @@ -104,6 +105,12 @@ impl Builder {
self
}

/// Sets whether the device is a group. This affects the icon in Spotify clients. Default is `false`.
pub fn is_group(mut self, is_group: bool) -> Self {
self.server_config.is_group = is_group;
self
}

/// Set the ip addresses on which it should listen to incoming connections. The default is all interfaces.
pub fn zeroconf_ip(mut self, zeroconf_ip: Vec<std::net::IpAddr>) -> Self {
self.zeroconf_ip = zeroconf_ip;
Expand Down
10 changes: 9 additions & 1 deletion discovery/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct Config {
pub name: Cow<'static, str>,
pub device_type: DeviceType,
pub device_id: String,
pub is_group: bool,
pub client_id: String,
}

Expand Down Expand Up @@ -70,6 +71,12 @@ impl RequestHandler {
if let Some(username) = &self.username {
active_user = username.to_string();
}
// options based on zeroconf guide, search for `groupStatus` on page
let group_status = if self.config.is_group {
"GROUP"
} else {
"NONE"
};

// See: https://developer.spotify.com/documentation/commercial-hardware/implementation/guides/zeroconf/
let body = json!({
Expand All @@ -87,7 +94,8 @@ impl RequestHandler {
"modelDisplayName": "librespot",
"libraryVersion": crate::core::version::SEMVER,
"resolverVersion": "1",
"groupStatus": "NONE",
// valid values are "GROUP" and "NONE"
"groupStatus": group_status,
// valid value documented & seen in the wild: "accesstoken"
// Using it will cause clients to fail to connect.
"tokenType": "default",
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ fn get_setup() -> Setup {
const CACHE_SIZE_LIMIT: &str = "cache-size-limit";
const DEVICE: &str = "device";
const DEVICE_TYPE: &str = "device-type";
const DEVICE_IS_GROUP: &str = "group";
const DISABLE_AUDIO_CACHE: &str = "disable-audio-cache";
const DISABLE_CREDENTIAL_CACHE: &str = "disable-credential-cache";
const DISABLE_DISCOVERY: &str = "disable-discovery";
Expand Down Expand Up @@ -409,6 +410,10 @@ fn get_setup() -> Setup {
DEVICE_TYPE,
"Displayed device type. Defaults to speaker.",
"TYPE",
).optflag(
"",
DEVICE_IS_GROUP,
"Whether the device represents a group. Defaults to false.",
)
.optopt(
TEMP_DIR_SHORT,
Expand Down Expand Up @@ -1312,11 +1317,14 @@ fn get_setup() -> Setup {
})
.unwrap_or_default();

let is_group = opt_present(DEVICE_IS_GROUP);

let has_volume_ctrl = !matches!(mixer_config.volume_ctrl, VolumeCtrl::Fixed);

ConnectConfig {
name,
device_type,
is_group,
initial_volume,
has_volume_ctrl,
}
Expand Down Expand Up @@ -1685,6 +1693,7 @@ async fn main() {
match librespot::discovery::Discovery::builder(device_id, client_id)
.name(setup.connect_config.name.clone())
.device_type(setup.connect_config.device_type)
.is_group(setup.connect_config.is_group)
.port(setup.zeroconf_port)
.zeroconf_ip(setup.zeroconf_ip.clone())
.launch()
Expand Down

0 comments on commit cdff6da

Please sign in to comment.