You are not logged in.

#1 2021-07-28 11:19:31

twoion
一期一会
Registered: 2015-08-10
Posts: 3,318

Access mpd from WSL2 Linux VM on Windows host

mpd is the best way to play back music. If you're using Windows 10 as the host OS and a Linux distribution such as Ubuntu or Debian inside a WSL2 VM, the simplest way to get mpd to work is running mpd.exe on the Windows host in order to bind the audio output and using the "good" Linux power user clients such as ncmpcpp to control the daemon (no Windows-compatible mpd client is quite up to the standards of ncmpcpp, Cantata et al.). So here's how:

On the Windows host, you should use mpd in at least version 0.22.7 in order to take advantage of an important fix in the wasapi output module . Contrary to the linked specific bug ticket, the issue also occurs if you bind mpd as a non-exclusive wsapi client. Just use the latest mpd version available at all times.

On the Windows host, ensure that mpd binds to the correct network adapter, which should be the virtual adapter used to communicate between the Windows host and the WSL2 VM. This is most easily done by using the DNS name of the WSL2 "host" inside mpd.conf, for example:

bind_to_address "cobalt.mshome.net:6600"
music_directory "T:/audio"
log_file "syslog"

database {
  plugin "simple"
  path "C:/Users/me/AppData/Local/mpd/mpd.db"
}

audio_output {
  name "wasapi"
  type "wasapi"
}

The correct DNS name to bind to is "<YOURWINDOWSHOSTNAME>.mshome.net", as demonstrated for my computer "cobalt". Configure the rest of the options as desired.

Inside WSL2, you define the following in ~/.bash_aliases in order to wrap ncmpcpp (or any other MPD client:

ncmpcpp () {
  local mpd_host=${MPD_HOST:-$(ip -j route show default|jq -r '.[] |select(.dst=="default")|.gateway')}
  local mpd_port=${MPD_PORT:-6600}
  /usr/bin/ncmpcpp "--host=$mpd_host" "--port=$mpd_port"
  return $?
}

The default gateway set in the Linux VM equals the bind host name of mpd on the Windows host. We take advantage of iproute2's JSON output option in order to remove the need for parsing text output using cut(1), yielding a very stable solution.

source ~/.bash_aliases

or open a new shell session.

Now, run ncmpcpp to connect to mpd on the Windows host, and use the client to control music playback on the host. An advantage of this particular solution is that mpd on the host does not bind to an externally reachable port, so you don't need to bother with firewall rules.

Online

Board footer

Powered by FluxBB