1 |
=head1 NAME |
2 |
|
3 |
AnyEvent::MPV - remote control mpv (https://mpv.io) |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use AnyEvent::MPV; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
This module is an L<AnyEvent> user, you need to make sure that you use and |
12 |
run a supported event loop. |
13 |
|
14 |
=cut |
15 |
|
16 |
package AnyEvent::MPV; |
17 |
|
18 |
use common::sense; |
19 |
|
20 |
use Fcntl (); |
21 |
use Scalar::Util (); |
22 |
|
23 |
use AnyEvent (); |
24 |
use AnyEvent::Util (); |
25 |
|
26 |
our $JSON = eval { require JSON::XS; JSON::XS:: } |
27 |
|| do { require JSON::PP; JSON::PP:: }; |
28 |
|
29 |
our $VERSION = '0.1'; |
30 |
|
31 |
our $mpv_path; # last mpv path used |
32 |
our $mpv_optionlist; # output of mpv --list-options |
33 |
|
34 |
sub new { |
35 |
my ($class, %kv) = @_; |
36 |
|
37 |
bless { |
38 |
mpv => "mpv", |
39 |
args => [], |
40 |
%kv, |
41 |
}, $class |
42 |
} |
43 |
|
44 |
# can be used to escape filenames |
45 |
sub escape_binary { |
46 |
shift; |
47 |
local $_ = shift; |
48 |
# we escape every "illegal" octet using U+10e5df HEX. this is later undone in cmd |
49 |
s/([\x00-\x1f\x80-\xff])/sprintf "\x{10e5df}%02x", ord $1/ge; |
50 |
$_ |
51 |
} |
52 |
|
53 |
sub start { |
54 |
my ($self, @extra_args) = @_; |
55 |
|
56 |
return if $self->{fh}; |
57 |
|
58 |
# cache optionlist for same "path" |
59 |
($mpv_path, $mpv_optionlist) = ($self->{mpv}, scalar qx{\Q$self->{mpv}\E --list-options}) |
60 |
if $self->{mpv} ne $mpv_path; |
61 |
|
62 |
my $options = $mpv_optionlist; |
63 |
|
64 |
my ($fh, $slave) = AnyEvent::Util::portable_socketpair |
65 |
or die "socketpair: $!\n"; |
66 |
|
67 |
AnyEvent::Util::fh_nonblocking $fh, 1; |
68 |
|
69 |
$self->{pid} = fork; |
70 |
|
71 |
if ($self->{pid} eq 0) { |
72 |
AnyEvent::Util::fh_nonblocking $slave, 0; |
73 |
fcntl $slave, Fcntl::F_SETFD, 0; |
74 |
|
75 |
my $input_file = $options =~ /\s--input-ipc-client\s/ ? "input-ipc-client" : "input-file"; |
76 |
|
77 |
exec $self->{mpv}, |
78 |
qw(--no-input-terminal --idle=yes --pause), |
79 |
($self->{trace} ? "--quiet" : "--really-quiet"), |
80 |
"--$input_file=fd://" . (fileno $slave), |
81 |
@{ $self->{args} }, |
82 |
@extra_args; |
83 |
exit 1; |
84 |
} |
85 |
|
86 |
$self->{fh} = $fh; |
87 |
|
88 |
my $trace = delete $self->{trace} || sub { }; |
89 |
|
90 |
$trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace; |
91 |
|
92 |
my $buf; |
93 |
my $wbuf; |
94 |
|
95 |
Scalar::Util::weaken $self; |
96 |
|
97 |
$self->{rw} = AE::io $fh, 0, sub { |
98 |
if (sysread $fh, $buf, 8192, length $buf) { |
99 |
while ($buf =~ s/^([^\n]+)\n//) { |
100 |
$trace->("mpv>" => "$1"); |
101 |
|
102 |
if ("{" eq substr $1, 0, 1) { |
103 |
eval { |
104 |
my $reply = JSON::XS->new->latin1->decode ($1); |
105 |
|
106 |
if (exists $reply->{event}) { |
107 |
if ( |
108 |
$reply->{event} eq "client-message" |
109 |
and $reply->{args}[0] eq "AnyEvent::MPV" |
110 |
and $reply->{args}[1] eq "keyhack" |
111 |
) { |
112 |
$self->on_key ($reply->{args}[2]); |
113 |
} else { |
114 |
$self->on_event ($reply->{event}); |
115 |
} |
116 |
} elsif (exists $reply->{request_id}) { |
117 |
my $cv = delete $self->{cmd_cv}{$reply->{request_id}}; |
118 |
|
119 |
unless ($cv) { |
120 |
warn "no cv found for request id <$reply->{request_id}>\n"; |
121 |
next; |
122 |
} |
123 |
|
124 |
if (exists $reply->{data}) { |
125 |
$cv->send ($reply->{data}); |
126 |
} elsif ($reply->{error} eq "success") { # success means error... eh.. no... |
127 |
$cv->send; |
128 |
} else { |
129 |
$cv->croak ($reply->{error}); |
130 |
} |
131 |
|
132 |
} else { |
133 |
warn "unexpected reply from mpv, pleasew report: <$1>\n"; |
134 |
} |
135 |
}; |
136 |
warn $@ if $@; |
137 |
} else { |
138 |
$trace->("mpv>" => "$1"); |
139 |
} |
140 |
} |
141 |
} else { |
142 |
$self->stop; |
143 |
$self->on_eof; |
144 |
} |
145 |
}; |
146 |
|
147 |
$self->{_send} = sub { |
148 |
$wbuf .= "$_[0]\n"; |
149 |
|
150 |
$trace->(">mpv" => "$_[0]"); |
151 |
|
152 |
$self->{ww} ||= AE::io $fh, 1, sub { |
153 |
my $len = syswrite $fh, $wbuf; |
154 |
substr $wbuf, 0, $len, ""; |
155 |
undef $self->{ww} unless length $wbuf; |
156 |
}; |
157 |
}; |
158 |
} |
159 |
|
160 |
sub on_eof { |
161 |
my ($self) = @_; |
162 |
|
163 |
$self->{on_eof}($self) if $self->{on_eof}; |
164 |
} |
165 |
|
166 |
sub on_event { |
167 |
my ($self, $key) = @_; |
168 |
|
169 |
$self->{on_event}($self, $key) if $self->{on_event}; |
170 |
} |
171 |
|
172 |
sub on_key { |
173 |
my ($self, $key) = @_; |
174 |
|
175 |
$self->{on_key}($self, $key) if $self->{on_key}; |
176 |
} |
177 |
|
178 |
sub cmd { |
179 |
my ($self, @cmd) = @_; |
180 |
|
181 |
my $cv = AE::cv; |
182 |
|
183 |
my $reqid = ++$self->{reqid}; |
184 |
$self->{cmd_cv}{$reqid} = $cv; |
185 |
|
186 |
my $cmd = JSON::XS::encode_json { command => ref $cmd[0] ? $cmd[0] : \@cmd, request_id => $reqid*1 }; |
187 |
|
188 |
# (un-)apply escape_binary hack |
189 |
$cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8 |
190 |
|
191 |
$self->{_send}($cmd); |
192 |
|
193 |
$cv |
194 |
} |
195 |
|
196 |
sub stop { |
197 |
my ($self) = @_; |
198 |
|
199 |
if ($self->{pid}) { |
200 |
delete $self->{rw}; |
201 |
delete $self->{ww}; |
202 |
|
203 |
close delete $self->{fh}; # current mpv versions should cleanup on their own on close |
204 |
|
205 |
kill TERM => $self->{pid}; |
206 |
|
207 |
delete $self->{pid}; |
208 |
} |
209 |
} |
210 |
|
211 |
=head1 SEE ALSO |
212 |
|
213 |
L<AnyEvent>. |
214 |
|
215 |
=head1 AUTHOR |
216 |
|
217 |
Marc Lehmann <schmorp@schmorp.de> |
218 |
http://home.schmorp.de/ |
219 |
|
220 |
=cut |
221 |
|
222 |
1 |
223 |
|