Debian Codegraph @

mpcp

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