main.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. use futures::sync::mpsc::UnboundedReceiver;
  2. use futures::{Async, Future, Poll, Stream};
  3. use sha1::{Digest, Sha1};
  4. use log::{error, info, trace, warn};
  5. use std::env;
  6. use std::io::{self, stderr, Write};
  7. use std::mem;
  8. use std::path::PathBuf;
  9. use std::process::exit;
  10. use std::str::FromStr;
  11. use tokio_core::reactor::{Core, Handle};
  12. use tokio_io::IoStream;
  13. use url::Url;
  14. use librespot::core::authentication::{get_credentials, Credentials};
  15. use librespot::core::cache::Cache;
  16. use librespot::core::config::{ConnectConfig, DeviceType, SessionConfig};
  17. use librespot::core::session::Session;
  18. use librespot::core::version;
  19. use librespot::connect::discovery::{discovery, DiscoveryStream};
  20. use librespot::connect::spirc::{Spirc, SpircTask};
  21. use librespot::playback::audio_backend::{self, Sink, BACKENDS};
  22. use librespot::playback::config::{Bitrate, PlayerConfig};
  23. use librespot::playback::mixer::{self, Mixer, MixerConfig};
  24. use librespot::playback::player::{Player, PlayerEvent};
  25. mod player_event_handler;
  26. use crate::player_event_handler::run_program_on_events;
  27. fn device_id(name: &str) -> String {
  28. hex::encode(Sha1::digest(name.as_bytes()))
  29. }
  30. fn usage(program: &str, opts: &getopts::Options) -> String {
  31. let brief = format!("Usage: {} [options]", program);
  32. opts.usage(&brief)
  33. }
  34. fn setup_logging(verbose: bool) {
  35. let mut builder = env_logger::Builder::new();
  36. match env::var("RUST_LOG") {
  37. Ok(config) => {
  38. builder.parse_filters(&config);
  39. builder.init();
  40. if verbose {
  41. warn!("`--verbose` flag overidden by `RUST_LOG` environment variable");
  42. }
  43. }
  44. Err(_) => {
  45. if verbose {
  46. builder.parse_filters("mdns=info,librespot=trace");
  47. } else {
  48. builder.parse_filters("mdns=info,librespot=info");
  49. }
  50. builder.init();
  51. }
  52. }
  53. }
  54. fn list_backends() {
  55. println!("Available Backends : ");
  56. for (&(name, _), idx) in BACKENDS.iter().zip(0..) {
  57. if idx == 0 {
  58. println!("- {} (default)", name);
  59. } else {
  60. println!("- {}", name);
  61. }
  62. }
  63. }
  64. #[derive(Clone)]
  65. struct Setup {
  66. backend: fn(Option<String>) -> Box<dyn Sink>,
  67. device: Option<String>,
  68. mixer: fn(Option<MixerConfig>) -> Box<dyn Mixer>,
  69. cache: Option<Cache>,
  70. player_config: PlayerConfig,
  71. session_config: SessionConfig,
  72. connect_config: ConnectConfig,
  73. mixer_config: MixerConfig,
  74. credentials: Option<Credentials>,
  75. enable_discovery: bool,
  76. zeroconf_port: u16,
  77. player_event_program: Option<String>,
  78. }
  79. fn setup(args: &[String]) -> Setup {
  80. let mut opts = getopts::Options::new();
  81. opts.optopt(
  82. "c",
  83. "cache",
  84. "Path to a directory where files will be cached.",
  85. "CACHE",
  86. ).optflag("", "disable-audio-cache", "Disable caching of the audio data.")
  87. .reqopt("n", "name", "Device name", "NAME")
  88. .optopt("", "device-type", "Displayed device type", "DEVICE_TYPE")
  89. .optopt(
  90. "b",
  91. "bitrate",
  92. "Bitrate (96, 160 or 320). Defaults to 160",
  93. "BITRATE",
  94. )
  95. .optopt(
  96. "",
  97. "onevent",
  98. "Run PROGRAM when playback is about to begin.",
  99. "PROGRAM",
  100. )
  101. .optflag("v", "verbose", "Enable verbose output")
  102. .optopt("u", "username", "Username to sign in with", "USERNAME")
  103. .optopt("p", "password", "Password", "PASSWORD")
  104. .optopt("", "proxy", "HTTP proxy to use when connecting", "PROXY")
  105. .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", "AP_PORT")
  106. .optflag("", "disable-discovery", "Disable discovery mode")
  107. .optopt(
  108. "",
  109. "backend",
  110. "Audio backend to use. Use '?' to list options",
  111. "BACKEND",
  112. )
  113. .optopt(
  114. "",
  115. "device",
  116. "Audio device to use. Use '?' to list options if using portaudio or alsa",
  117. "DEVICE",
  118. )
  119. .optopt("", "mixer", "Mixer to use (alsa or softmixer)", "MIXER")
  120. .optopt(
  121. "m",
  122. "mixer-name",
  123. "Alsa mixer name, e.g \"PCM\" or \"Master\". Defaults to 'PCM'",
  124. "MIXER_NAME",
  125. )
  126. .optopt(
  127. "",
  128. "mixer-card",
  129. "Alsa mixer card, e.g \"hw:0\" or similar from `aplay -l`. Defaults to 'default' ",
  130. "MIXER_CARD",
  131. )
  132. .optopt(
  133. "",
  134. "mixer-index",
  135. "Alsa mixer index, Index of the cards mixer. Defaults to 0",
  136. "MIXER_INDEX",
  137. )
  138. .optopt(
  139. "",
  140. "initial-volume",
  141. "Initial volume in %, once connected (must be from 0 to 100)",
  142. "VOLUME",
  143. )
  144. .optopt(
  145. "",
  146. "zeroconf-port",
  147. "The port the internal server advertised over zeroconf uses.",
  148. "ZEROCONF_PORT",
  149. )
  150. .optflag(
  151. "",
  152. "enable-volume-normalisation",
  153. "Play all tracks at the same volume",
  154. )
  155. .optopt(
  156. "",
  157. "normalisation-pregain",
  158. "Pregain (dB) applied by volume normalisation",
  159. "PREGAIN",
  160. )
  161. .optflag(
  162. "",
  163. "linear-volume",
  164. "increase volume linear instead of logarithmic.",
  165. )
  166. .optflag(
  167. "",
  168. "autoplay",
  169. "autoplay similar songs when your music ends.",
  170. );
  171. let matches = match opts.parse(&args[1..]) {
  172. Ok(m) => m,
  173. Err(f) => {
  174. writeln!(
  175. stderr(),
  176. "error: {}\n{}",
  177. f.to_string(),
  178. usage(&args[0], &opts)
  179. )
  180. .unwrap();
  181. exit(1);
  182. }
  183. };
  184. let verbose = matches.opt_present("verbose");
  185. setup_logging(verbose);
  186. info!(
  187. "librespot {} ({}). Built on {}. Build ID: {}",
  188. version::short_sha(),
  189. version::commit_date(),
  190. version::short_now(),
  191. version::build_id()
  192. );
  193. let backend_name = matches.opt_str("backend");
  194. if backend_name == Some("?".into()) {
  195. list_backends();
  196. exit(0);
  197. }
  198. let backend = audio_backend::find(backend_name).expect("Invalid backend");
  199. let device = matches.opt_str("device");
  200. if device == Some("?".into()) {
  201. backend(device);
  202. exit(0);
  203. }
  204. let mixer_name = matches.opt_str("mixer");
  205. let mixer = mixer::find(mixer_name.as_ref()).expect("Invalid mixer");
  206. let mixer_config = MixerConfig {
  207. card: matches
  208. .opt_str("mixer-card")
  209. .unwrap_or(String::from("default")),
  210. mixer: matches.opt_str("mixer-name").unwrap_or(String::from("PCM")),
  211. index: matches
  212. .opt_str("mixer-index")
  213. .map(|index| index.parse::<u32>().unwrap())
  214. .unwrap_or(0),
  215. };
  216. let use_audio_cache = !matches.opt_present("disable-audio-cache");
  217. let cache = matches
  218. .opt_str("c")
  219. .map(|cache_location| Cache::new(PathBuf::from(cache_location), use_audio_cache));
  220. let initial_volume = matches
  221. .opt_str("initial-volume")
  222. .map(|volume| {
  223. let volume = volume.parse::<u16>().unwrap();
  224. if volume > 100 {
  225. panic!("Initial volume must be in the range 0-100");
  226. }
  227. (volume as i32 * 0xFFFF / 100) as u16
  228. })
  229. .or_else(|| cache.as_ref().and_then(Cache::volume))
  230. .unwrap_or(0x8000);
  231. let zeroconf_port = matches
  232. .opt_str("zeroconf-port")
  233. .map(|port| port.parse::<u16>().unwrap())
  234. .unwrap_or(0);
  235. let name = matches.opt_str("name").unwrap();
  236. let credentials = {
  237. let cached_credentials = cache.as_ref().and_then(Cache::credentials);
  238. let password = |username: &String| -> String {
  239. write!(stderr(), "Password for {}: ", username).unwrap();
  240. stderr().flush().unwrap();
  241. rpassword::read_password().unwrap()
  242. };
  243. get_credentials(
  244. matches.opt_str("username"),
  245. matches.opt_str("password"),
  246. cached_credentials,
  247. password,
  248. )
  249. };
  250. let session_config = {
  251. let device_id = device_id(&name);
  252. SessionConfig {
  253. user_agent: version::version_string(),
  254. device_id: device_id,
  255. proxy: matches.opt_str("proxy").or(std::env::var("http_proxy").ok()).map(
  256. |s| {
  257. match Url::parse(&s) {
  258. Ok(url) => {
  259. if url.host().is_none() || url.port().is_none() {
  260. panic!("Invalid proxy url, only urls on the format \"http://host:port\" are allowed");
  261. }
  262. if url.scheme() != "http" {
  263. panic!("Only unsecure http:// proxies are supported");
  264. }
  265. url
  266. },
  267. Err(err) => panic!("Invalid proxy url: {}, only urls on the format \"http://host:port\" are allowed", err)
  268. }
  269. },
  270. ),
  271. ap_port: matches
  272. .opt_str("ap-port")
  273. .map(|port| port.parse::<u16>().expect("Invalid port")),
  274. }
  275. };
  276. let player_config = {
  277. let bitrate = matches
  278. .opt_str("b")
  279. .as_ref()
  280. .map(|bitrate| Bitrate::from_str(bitrate).expect("Invalid bitrate"))
  281. .unwrap_or(Bitrate::default());
  282. PlayerConfig {
  283. bitrate: bitrate,
  284. normalisation: matches.opt_present("enable-volume-normalisation"),
  285. normalisation_pregain: matches
  286. .opt_str("normalisation-pregain")
  287. .map(|pregain| pregain.parse::<f32>().expect("Invalid pregain float value"))
  288. .unwrap_or(PlayerConfig::default().normalisation_pregain),
  289. }
  290. };
  291. let connect_config = {
  292. let device_type = matches
  293. .opt_str("device-type")
  294. .as_ref()
  295. .map(|device_type| DeviceType::from_str(device_type).expect("Invalid device type"))
  296. .unwrap_or(DeviceType::default());
  297. ConnectConfig {
  298. name: name,
  299. device_type: device_type,
  300. volume: initial_volume,
  301. linear_volume: matches.opt_present("linear-volume"),
  302. autoplay: matches.opt_present("autoplay"),
  303. }
  304. };
  305. let enable_discovery = !matches.opt_present("disable-discovery");
  306. Setup {
  307. backend: backend,
  308. cache: cache,
  309. session_config: session_config,
  310. player_config: player_config,
  311. connect_config: connect_config,
  312. credentials: credentials,
  313. device: device,
  314. enable_discovery: enable_discovery,
  315. zeroconf_port: zeroconf_port,
  316. mixer: mixer,
  317. mixer_config: mixer_config,
  318. player_event_program: matches.opt_str("onevent"),
  319. }
  320. }
  321. struct Main {
  322. cache: Option<Cache>,
  323. player_config: PlayerConfig,
  324. session_config: SessionConfig,
  325. connect_config: ConnectConfig,
  326. backend: fn(Option<String>) -> Box<dyn Sink>,
  327. device: Option<String>,
  328. mixer: fn(Option<MixerConfig>) -> Box<dyn Mixer>,
  329. mixer_config: MixerConfig,
  330. handle: Handle,
  331. discovery: Option<DiscoveryStream>,
  332. signal: IoStream<()>,
  333. spirc: Option<Spirc>,
  334. spirc_task: Option<SpircTask>,
  335. connect: Box<dyn Future<Item = Session, Error = io::Error>>,
  336. shutdown: bool,
  337. player_event_channel: Option<UnboundedReceiver<PlayerEvent>>,
  338. player_event_program: Option<String>,
  339. }
  340. impl Main {
  341. fn new(handle: Handle, setup: Setup) -> Main {
  342. let mut task = Main {
  343. handle: handle.clone(),
  344. cache: setup.cache,
  345. session_config: setup.session_config,
  346. player_config: setup.player_config,
  347. connect_config: setup.connect_config,
  348. backend: setup.backend,
  349. device: setup.device,
  350. mixer: setup.mixer,
  351. mixer_config: setup.mixer_config,
  352. connect: Box::new(futures::future::empty()),
  353. discovery: None,
  354. spirc: None,
  355. spirc_task: None,
  356. shutdown: false,
  357. signal: Box::new(tokio_signal::ctrl_c().flatten_stream()),
  358. player_event_channel: None,
  359. player_event_program: setup.player_event_program,
  360. };
  361. if setup.enable_discovery {
  362. let config = task.connect_config.clone();
  363. let device_id = task.session_config.device_id.clone();
  364. task.discovery =
  365. Some(discovery(&handle, config, device_id, setup.zeroconf_port).unwrap());
  366. }
  367. if let Some(credentials) = setup.credentials {
  368. task.credentials(credentials);
  369. }
  370. task
  371. }
  372. fn credentials(&mut self, credentials: Credentials) {
  373. let config = self.session_config.clone();
  374. let handle = self.handle.clone();
  375. let connection = Session::connect(config, credentials, self.cache.clone(), handle);
  376. self.connect = connection;
  377. self.spirc = None;
  378. let task = mem::replace(&mut self.spirc_task, None);
  379. if let Some(task) = task {
  380. self.handle.spawn(task);
  381. }
  382. }
  383. }
  384. impl Future for Main {
  385. type Item = ();
  386. type Error = ();
  387. fn poll(&mut self) -> Poll<(), ()> {
  388. loop {
  389. let mut progress = false;
  390. if let Some(Async::Ready(Some(creds))) =
  391. self.discovery.as_mut().map(|d| d.poll().unwrap())
  392. {
  393. if let Some(ref spirc) = self.spirc {
  394. spirc.shutdown();
  395. }
  396. self.credentials(creds);
  397. progress = true;
  398. }
  399. if let Async::Ready(session) = self.connect.poll().unwrap() {
  400. self.connect = Box::new(futures::future::empty());
  401. let mixer_config = self.mixer_config.clone();
  402. let mixer = (self.mixer)(Some(mixer_config));
  403. let player_config = self.player_config.clone();
  404. let connect_config = self.connect_config.clone();
  405. let audio_filter = mixer.get_audio_filter();
  406. let backend = self.backend;
  407. let device = self.device.clone();
  408. let (player, event_channel) =
  409. Player::new(player_config, session.clone(), audio_filter, move || {
  410. (backend)(device)
  411. });
  412. let (spirc, spirc_task) = Spirc::new(connect_config, session, player, mixer);
  413. self.spirc = Some(spirc);
  414. self.spirc_task = Some(spirc_task);
  415. self.player_event_channel = Some(event_channel);
  416. progress = true;
  417. }
  418. if let Async::Ready(Some(())) = self.signal.poll().unwrap() {
  419. trace!("Ctrl-C received");
  420. if !self.shutdown {
  421. if let Some(ref spirc) = self.spirc {
  422. spirc.shutdown();
  423. } else {
  424. return Ok(Async::Ready(()));
  425. }
  426. self.shutdown = true;
  427. } else {
  428. return Ok(Async::Ready(()));
  429. }
  430. progress = true;
  431. }
  432. if let Some(ref mut spirc_task) = self.spirc_task {
  433. if let Async::Ready(()) = spirc_task.poll().unwrap() {
  434. if self.shutdown {
  435. return Ok(Async::Ready(()));
  436. } else {
  437. panic!("Spirc shut down unexpectedly");
  438. }
  439. }
  440. }
  441. if let Some(ref mut player_event_channel) = self.player_event_channel {
  442. if let Async::Ready(Some(event)) = player_event_channel.poll().unwrap() {
  443. if let Some(ref program) = self.player_event_program {
  444. let child = run_program_on_events(event, program)
  445. .expect("program failed to start")
  446. .map(|status| {
  447. if !status.success() {
  448. error!("child exited with status {:?}", status.code());
  449. }
  450. })
  451. .map_err(|e| error!("failed to wait on child process: {}", e));
  452. self.handle.spawn(child);
  453. }
  454. }
  455. }
  456. if !progress {
  457. return Ok(Async::NotReady);
  458. }
  459. }
  460. }
  461. }
  462. fn main() {
  463. if env::var("RUST_BACKTRACE").is_err() {
  464. env::set_var("RUST_BACKTRACE", "full")
  465. }
  466. let mut core = Core::new().unwrap();
  467. let handle = core.handle();
  468. let args: Vec<String> = std::env::args().collect();
  469. core.run(Main::new(handle, setup(&args))).unwrap()
  470. }