-
Notifications
You must be signed in to change notification settings - Fork 43
/
gmusicbrowser_mpv.pm
364 lines (331 loc) · 10.9 KB
/
gmusicbrowser_mpv.pm
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# Copyright (C) 2005-2015 Quentin Sculo <squentin@free.fr>
#
# This file is part of Gmusicbrowser.
# Gmusicbrowser is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation
package Play_mpv;
use strict;
use warnings;
use IO::Socket::UNIX;
use JSON::PP;
use Time::HiRes 'sleep';
use POSIX ':sys_wait_h'; #for WNOHANG in waitpid
my (@cmd_and_args,$ChildPID,$WatchTag,$WatchTag2,@pidToKill,$Kill9);
my $sockfh;
my (%supported,$mpv);
my $preparednext;
my ($Called_from_eof,$gmb_file,$mpv_file,$Last_messages);
my $initseek;
my $watcher;
my $version;
my @cmd_queue;
my $SOCK = $::HomeDir."gmb_mpv_sock";
$::PlayPacks{Play_mpv}=1; #register the package
sub init
{ undef %supported;
$mpv= $::Options{mpv_cmd};
if ($mpv && !-x $mpv && !(::first { -x $_ } map $_.::SLASH.$mpv, split /:/, $ENV{PATH}))
{ $mpv=undef;
}
$mpv ||= ::first { -x $_ } map $_.::SLASH.'mpv', split /:/, $ENV{PATH};
warn "mpv: found mpv version $version\n" if $version && $::debug;
if (!check_version(0, 28))
{ $mpv=undef;
warn "mpv version earlier than 0.28 are not supported -> mpv backend disabled\n"
}
return unless $mpv;
return bless {RG=>1,EQ=>1},__PACKAGE__;
}
sub get_version
{ return $version if $version;
return unless $mpv;
my $output= qx/$mpv -V/;
my ($v) = ($output =~ /mpv\s*(\S+)\s.*/);
return $v;
}
sub check_version
{ my ($major,$minor)=@_;
my $version=get_version();
my $ok;
return unless $version;
if ($version=~m/^v?(\d+)\.(\d+)(\S+)?$/i) { $ok= $1>$major || $2>=$minor; }
elsif ($version=~m/^git-[[:xdigit:]]+$/i) { $ok=1; } #assume git version is ok
else { warn "mpv: error looking up mpv version\n"; }
return $ok;
}
sub supported_formats
{ return () unless $mpv;
unless (keys %supported)
{for (qx($mpv --ad=help))
{ if (m/\Wmp3\W/i) {$supported{mp3}=undef}
elsif (m/\Wvorbis\W/i){$supported{oga}=undef}
elsif (m/\Wopus\W/i) {$supported{opus}=undef}
elsif (m/mpc\d/) {$supported{mpc}=undef}
elsif (m/flac\s/) {$supported{flac}=undef}
elsif (m/wavpack\s/) {$supported{wv}=undef}
elsif (m/ape\s/) {$supported{ape}=undef}
elsif (m/\Waac\W/i) {$supported{m4a}=undef}
}
}
return keys %supported;
}
sub cmd_push
{ return unless $sockfh;
my @args=@_;
my $callback;
# If the first argument is a subroutine, use it as callback
if (ref($args[0]) eq 'CODE') { $callback = shift @args; }
push @cmd_queue, $callback;
my $cmd = JSON::PP->new->encode({command => \@args});
print $sockfh "$cmd\n";
warn "MPVCMD: $cmd\n" if $::debug;
}
sub cmd_shift
{ my $data=shift;
my $callback = shift @cmd_queue;
if ($callback) { $callback->($data); }
}
sub launch_mpv
{ $preparednext=undef;
@cmd_and_args=($mpv, '--input-ipc-server='.$SOCK, qw/--idle --no-video --no-input-terminal --really-quiet --gapless-audio=weak --volume-max=100 --mute=no --no-sub-auto/);
push @cmd_and_args,"--volume=".convertvolume($::Volume);
if ($::Options{use_replaygain})
{ push @cmd_and_args,"--replaygain=".get_RG_mode();
push @cmd_and_args,"--replaygain-preamp=".get_RG_preamp();
push @cmd_and_args,"--replaygain-clip=".($::Options{rg_limiter} ? 'yes' : 'no');
push @cmd_and_args,"--replaygain-fallback=".($::Options{rg_fallback} || 0);
}
push @cmd_and_args,"--af-add=".get_EQ_string($::Options{equalizer}) if $::Options{use_equalizer};
push @cmd_and_args,split / /,$::Options{mpvoptions} if $::Options{mpvoptions};
warn "@cmd_and_args\n" if $::debug;
$ChildPID=fork;
if (!defined $ChildPID) { warn "gmusicbrowser_mpv : fork failed : $!\n"; ::ErrorPlay("Fork failed : $!"); return }
elsif ($ChildPID==0) #child
{ exec @cmd_and_args or print STDERR "launch failed (@cmd_and_args) : $!\n";
POSIX::_exit(1);
}
#wait for mpv to establish socket as server
for (0 .. 200)
{ $sockfh = IO::Socket::UNIX->new(Peer => $SOCK, Type => SOCK_STREAM);
last if $sockfh || (waitpid($ChildPID, WNOHANG) != 0);
warn "gmusicbrowser_mpv: could not connect to socket; retrying\n" if $::debug;
sleep 0.01;
}
unless ($sockfh)
{ handle_error("failed to connect to socket (probably failed to launch mpv): $!");
return;
}
$sockfh->autoflush(1);
$sockfh->blocking(0);
$WatchTag = Glib::IO->add_watch(fileno($sockfh),'hup',\&_eos_cb);
$WatchTag2= Glib::IO->add_watch(fileno($sockfh),'in',\&_remotemsg);
$watcher = {};
::Watch($watcher,'NextSongs', \&append_next);
cmd_push('observe_property', 1, 'playback-time');
cmd_push('request_log_messages', 'error');
return 1;
}
sub Play
{ my (undef,$file,$sec)=@_;
launch_mpv() unless $ChildPID && $sockfh;
return unless $ChildPID;
$Last_messages="";
$gmb_file=$file;
warn "playing $file (pid=$ChildPID)\n" if $::Verbose;
# gapless - check for non-user-initiated EOF
return if ($Called_from_eof && $preparednext && $preparednext eq $gmb_file);
$mpv_file = "";
$initseek = $sec;
cmd_push('loadfile', $file);
cmd_push('playlist_clear');
}
sub append_next
{ $preparednext=undef;
cmd_push('playlist_clear');
if ($::NextFileToPlay && $::NextFileToPlay ne $gmb_file)
{ cmd_push('loadfile', $::NextFileToPlay, 'append');
$preparednext= $::NextFileToPlay;
}
}
sub _remotemsg
{ my $eof;
while (my $line=<$sockfh>)
{ my $msg= JSON::PP->new->decode($line); # use JSON::PP->new->decode instead of decode_json (equivalent to JSON::PP->new->utf8->decode) because decode_json converts to utf8, which gives error with invalid utf8 filenames (only happens for mpv >0.9.0)
warn "mpv raw-output: $line" if $::debug;
if (my $error=$msg->{error})
{ warn "mpv error: $error" unless $error eq 'success';
cmd_shift($msg->{data});
}
elsif (my $event=$msg->{event})
{ if ($event eq 'property-change' && $msg->{name} eq 'path') { $mpv_file= $msg->{data}||""; } # doesn't happen when previous file is same as new file
elsif ($event eq 'file-loaded')
{ SkipTo(undef,$initseek) if $initseek;
$initseek=undef;
cmd_push(sub { $mpv_file=shift; }, ('get_property', 'path'));
last if $eof; #only do eof now to catch log-message that are only sent after end-file and start-file
}
elsif ($mpv_file ne $gmb_file) {} #ignore all other events unless file is current
elsif ($event eq 'property-change' && $msg->{name} eq 'playback-time' && defined $msg->{data})
{ ::UpdateTime($msg->{data}); }
elsif ($event eq 'end-file') { $eof=1; } # ignore EOF signal on user-initiated track change as $mpv_file and $gmb_file aren't equal in those cases
elsif ($event eq 'log-message')
{ my $error= $msg->{text};
chomp $error;
my $time= $::PlayTime||0;
$Last_messages.= sprintf " %02d:%02d [%s] %s\n",
int($time/60), $time%60, $msg->{prefix}, $error;
}
}
}
handle_eof() if $eof;
return 1;
}
sub handle_eof
{ if ($::PlayTime < Songs::Get($::SongID,'length')-5) { handle_error(_"Playback ended unexpectedly.") }
else { $Called_from_eof=1; ::end_of_file(); $Called_from_eof=0; }
}
sub handle_error
{ my $error=shift;
Stop();
my $details=_("File").":\n$gmb_file\n\n";
$details.= _("Last messages:")."\n$Last_messages\n" if $Last_messages;
$details.= _("Command used:")."\n@cmd_and_args";
::ErrorPlay($error,$details);
}
sub _eos_cb
{ my $error;
if ($ChildPID && $ChildPID==waitpid($ChildPID, WNOHANG))
{ $error=_"Check your audio settings" if $?;
}
handle_error ($error or "mpv process closed unexpectedly.");
return 1;
}
sub Pause
{ cmd_push('set', 'pause', 'yes');
}
sub Resume
{ cmd_push('set', 'pause', 'no');
}
sub SkipTo
{ ::setlocale(::LC_NUMERIC, 'C');
my $sec="$_[1]";
::setlocale(::LC_NUMERIC, '');
cmd_push('seek', $sec, 'absolute');
}
sub Stop
{ if ($WatchTag)
{ Glib::Source->remove($WatchTag);
Glib::Source->remove($WatchTag2);
$WatchTag=$WatchTag2=undef;
}
if ($ChildPID)
{ cmd_push('quit');
Glib::Timeout->add( 100,\&_Kill_timeout ) unless @pidToKill;
$Kill9=0; #_Kill_timeout will first try INT, then KILL
push @pidToKill,$ChildPID;
undef $ChildPID;
}
if ($sockfh)
{ shutdown($sockfh,2);
close($sockfh);
unlink $SOCK;
undef $sockfh;
}
if ($watcher)
{ ::UnWatch($watcher,'NextSongs');
undef $watcher;
}
if (@cmd_queue)
{
undef @cmd_queue
}
}
sub _Kill_timeout #make sure old children are dead
{ @pidToKill=grep kill(0,$_), @pidToKill; #checks to see which ones are still there
if (@pidToKill)
{ warn "Sending ".($Kill9 ? 'KILL' : 'INT')." signal to @pidToKill\n" if $::debug;
if ($Kill9) {kill KILL=>@pidToKill;}
else {kill INT=>@pidToKill;}
$Kill9=1; #use KILL if they are still there next time
}
return @pidToKill; #removes the timeout if no more @pidToKill
}
sub AdvancedOptions
{ my $vbox=Gtk3::VBox->new(::FALSE, 2);
my $sg1=Gtk3::SizeGroup->new('horizontal');
my $opt=::NewPrefEntry('mpvoptions',_"mpv options :", sizeg1=>$sg1);
$vbox->pack_start($_,::FALSE,::FALSE,2), for $opt;
return $vbox;
}
# Volume functions
sub GetVolume {$::Volume}
sub GetMute {$::Mute}
sub SetVolume
{ shift;
my $set=shift;
if ($set eq 'mute') { $::Mute=$::Volume; $::Volume=0; }
elsif ($set eq 'unmute') { $::Volume=$::Mute; $::Mute=0; }
elsif ($set=~m/^\+(\d+)$/) { $::Volume+=$1; }
elsif ($set=~m/^-(\d+)$/) { $::Volume-=$1; }
elsif ($set=~m/(\d+)/) { $::Volume =$1; }
$::Volume=0 if $::Volume<0;
$::Volume=100 if $::Volume>100;
my $vol= convertvolume($::Volume);
cmd_push('set', 'volume', $vol);
::HasChanged('Vol');
$::Options{Volume}=$::Volume;
$::Options{Volume_mute}=$::Mute;
}
sub convertvolume
{ my $vol=$_[0];
#$vol= 100*($vol/100)**3; #convert a linear volume to cubic volume scale #doesn't seem to be needed in mpv
# will be sent to mpv as string, make sure it use a dot as decimal separator
::setlocale(::LC_NUMERIC, 'C');
$vol="$vol";
::setlocale(::LC_NUMERIC, '');
return $vol;
}
sub get_EQ_string
{ my $val=shift;
my @freqs = (29, 59, 119, 237, 474, 947, 1900, 3800, 7500, 15000);
my @gains = split /:/, $val;
my $fireq_string = "gain_entry=";
my @entries;
for my $i (0 .. $#freqs)
{ push @entries, "entry(".$freqs[$i].",".$gains[$i].")";
}
return "\@EQ:lavfi=[firequalizer=gain_entry='".(join ';', @entries)."']";
}
sub set_equalizer
{ my (undef,$val)=@_;
cmd_push('af', 'add', get_EQ_string($val));
}
sub EQ_Get_Range
{ return (-12,12,'dB');
}
sub EQ_Get_Hz
{ my $i=$_[1];
# mplayer and GST equalizers use the same bands, but they are indicated differently
# mplayer docs list band center frequences, GST reports band start freqs. Using GST values here for consistency
my @bands=(qw/29Hz 59Hz 119Hz 237Hz 474Hz 947Hz 1.9kHz 3.8kHz 7.5kHz 15.0kHz/);
return $bands[$i];
}
sub get_RG_preamp
{ my $preamp = $::Options{rg_preamp};
#FIXME: enforce limits in interface
$preamp = -15 if $::Options{rg_preamp}<-15;
$preamp = 15 if $::Options{rg_preamp}>15;
return $preamp;
}
sub get_RG_mode
{ return $::Options{rg_albummode} ? 'album' : 'track';
}
sub RG_set_options
{ if (!$::Options{use_replaygain}) { cmd_push('set', 'replaygain', 'no'); return; }
cmd_push('set', 'replaygain', get_RG_mode());
cmd_push('set', 'replaygain-preamp', get_RG_preamp());
cmd_push('set', 'replaygain-clip', $::Options{rg_limiter} ? 'yes' : 'no');
cmd_push('set', 'replaygain-fallback', $::Options{rg_fallback} || 0);
}
1;