/usr/bin/mpstore is in mpdtoys 0.24.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | #!/usr/bin/perl
use strict;
use warnings;
use Audio::MPD q{0.19.0};
use Data::Dumper;
=head1 NAME
mpstore - store and transfer mpd state between daemons
=head1 SYNOPSIS
mpstore [host] > file
mpload [host] < file
mpcp [src] dest
mpmv [src] dest
mpswap [A] B
=head1 DESCRIPTION
These commands allow saving, loading, and transferring state between mpd
daemons running on different hosts.
B<mpstore> dumps a daemon's state to stdout.
B<mpload> loads a state dump from stdin and sends it to a daemon.
B<mpcp> copies the state from the src daemon to the dest daemon, causing it
to begin to play the same song as the src daemon, at the same position.
B<mpmv> moves the state, so the dest daemon is left playing what the src
daemon was playing, and the src daemon is paused.
B<mpswap> exchanges the state of daemons A and B, swapping what they're
playing.
The first hostname passed to each command can be omitted, if it is then
the MPD_HOST environment variable will be used. Like the MPD_HOST variable,
the hostname can be of the form "password@hostname" to specify a password.
If any hostname is "-", the MPD_HOST setting will be used.
The full list of state that is handled is:
=over
=item the contents of the playlist
=item the playback state (playing, paused, stopped)
=item the currently playing song
=item the position within the playing song
=item the volume control
=item the repeat, random, and cross fade settings
=back
=head1 LIMITATIONS
The host that state is transferred to must have the playing song available
in its library, with the same filename. It's ok if some other songs in the
playlist are not available; such songs will be skipped.
B<mpcp> cannot perfectly synchronise playback between the two daemons.
Network latency and timing prevent this. It should manage better than 0.5
second accuracy. If you need better accuracy of synchronised playback,
you should probably use Pulse Audio.
=head1 BUGS
The file format is not the same that mpd uses for saving its own state,
which would be nice.
=head1 AUTHOR
Copyright 2007 Joey Hess <joey@kitenet.net>
Licensed under the GNU GPL version 2 or higher.
http://kitenet.net/~joey/code/mpdtoys
=cut
# Allow "-" to be specified as a host, it will use MPD_HOST then.
my $real_MPD_HOST=$ENV{MPD_HOST};
my $prev_host;
sub sethost {
my $host=shift;
if ($host eq "-") {
$host=$real_MPD_HOST;
if (! defined $host) {
die "error: MPD_HOST is not set, cannot use '-'\n";
}
}
if (defined $prev_host && $host eq $prev_host) {
die "error: src and dest hosts cannot be the same\n";
}
$ENV{MPD_HOST}=$prev_host=$host;
}
if ($0=~/mpswap/) {
if (@ARGV == 2) {
sethost(shift);
}
if (! @ARGV) {
die "error: not enough hosts specified\n";
}
my $a=Audio::MPD->new(conntype => "reuse");
sethost(shift);
my $b=Audio::MPD->new(conntype => "reuse");
my $asnap=snapshot($a, 1);
my $bsnap=snapshot($b, 1);
transfer($asnap, $b);
transfer($bsnap, $a);
}
elsif ($0=~/mpstore/) {
if (@ARGV) {
sethost(shift);
}
my $src=Audio::MPD->new(conntype => "reuse");
my $snap=snapshot($src, 0);
delete $snap->{src}; # don't dump this object
$Data::Dumper::Terse=1;
$Data::Dumper::Indent=1;
print Dumper($snap);
}
elsif ($0=~/mpload/) {
if (@ARGV == 2) {
sethost(shift);
}
my $dest=Audio::MPD->new(conntype => "reuse");
my $code;
{
local $/=undef;
$code=<>;
}
my $snap=eval $code;
if ($@ || ! ref $snap) {
die "error: failed to parse stdin ($@)\n";
}
transfer($snap, $dest);
}
else {
if (@ARGV == 2) {
sethost(shift);
}
if (! @ARGV) {
die "error: not enough hosts specified\n";
}
my $src=Audio::MPD->new(conntype => "reuse");
sethost(shift);
my $dest=Audio::MPD->new(conntype => "reuse");
my $snap=snapshot($src, 1);
transfer($snap, $dest);
}
sub snapshot {
my $mpd=shift;
my $pause=shift;
my $status=$mpd->status;
my $state=$status->state;
my $current=$mpd->current;
if ($pause && $state eq 'play') {
$mpd->pause;
}
my @playlist;
foreach my $song ($mpd->playlist->as_items) {
push @playlist, $song->file;
}
return {
src => $mpd,
state => $state,
repeat => $status->repeat,
volume => $status->volume,
pos => $status->time ? $status->time->seconds_sofar : 0,
xfade => $status->xfade,
current => defined $current ? $mpd->current->file : undef,
playlist => \@playlist,
};
}
sub transfer {
my $snap=shift;
my $dest=shift;
if (ref $snap->{playlist} eq 'ARRAY') {
# Feed playlist to dest.
$dest->playlist->clear;
eval {
$dest->playlist->add(@{$snap->{playlist}});
};
if ($@) {
# Try doing it a song at a time, in case only some
# songs are available.
foreach my $song (@{$snap->{playlist}}) {
eval {
$dest->playlist->add($song);
};
if ($@) {
print STDERR "warning: failed to add song to playlist ($song)\n";
}
}
}
}
# Set misc settings.
$dest->repeat($snap->{repeat}) if exists $snap->{repeat};
$dest->random($snap->{random}) if exists $snap->{random};
$dest->volume($snap->{volume}) if exists $snap->{volume};
$dest->fade($snap->{xfade}) if exists $snap->{xfade};
# Figure out the id of the song to play on dest.
my $id;
if (exists $snap->{current} && defined $snap->{current}) {
foreach my $song ($dest->playlist->as_items) {
if ($song->file eq $snap->{current}) {
$id=$song->id;
}
}
if (! defined $id) {
print STDERR "error: cannot find currently playing song (".
$snap->{current}.") on dest playlist\n";
return;
}
}
# Seek and set play state.
$dest->seekid($snap->{pos}, $id) if exists $snap->{pos} && defined $id;
if (exists $snap->{state}) {
if ($snap->{state} eq 'play') {
if ($0 =~ /mpcp/) {
# mpd only provides second accuracy, so src
# and dest are probably a fraction of a second
# off. For a more accurate copy of the state,
# seek the *src* back to the start of the
# current second too.
# Of course, this isn't perfect, due to
# network latency, etc.
$snap->{src}->seek($snap->{pos});
$snap->{src}->play;
$dest->play;
}
else {
$dest->play;
}
}
elsif ($snap->{state} eq 'pause') {
$dest->pause;
}
elsif ($snap->{state} eq 'stop') {
$dest->stop;
}
}
return 1;
}
|