… | |
… | |
3 | AnyEvent::MPV - remote control mpv (https://mpv.io) |
3 | AnyEvent::MPV - remote control mpv (https://mpv.io) |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent::MPV; |
7 | use AnyEvent::MPV; |
|
|
8 | |
|
|
9 | my $videofile = "path/to/file.mkv"; |
|
|
10 | use AnyEvent; |
|
|
11 | my $mpv = AnyEvent::MPV->new (trace => 1); |
|
|
12 | $mpv->start; |
|
|
13 | $mpv->cmd (loadfile => $mpv->escape_binary ($videofile)); |
|
|
14 | my $quit = AE::cv; |
|
|
15 | $mpv->register_event (end_file => $cv); |
|
|
16 | $quit->recv; |
8 | |
17 | |
9 | =head1 DESCRIPTION |
18 | =head1 DESCRIPTION |
10 | |
19 | |
11 | This module allows you to remote control F<mpv> (a video player). It also |
20 | This module allows you to remote control F<mpv> (a video player). It also |
12 | is an L<AnyEvent> user, you need to make sure that you use and run a |
21 | is an L<AnyEvent> user, you need to make sure that you use and run a |
… | |
… | |
50 | Here is a very simple client: |
59 | Here is a very simple client: |
51 | |
60 | |
52 | use AnyEvent; |
61 | use AnyEvent; |
53 | use AnyEvent::MPV; |
62 | use AnyEvent::MPV; |
54 | |
63 | |
55 | my $videofile = "./xyzzy.mp4"; |
64 | my $videofile = "./xyzzy.mkv"; |
56 | |
65 | |
57 | my $mpv = AnyEvent::MPV->new (trace => 1); |
66 | my $mpv = AnyEvent::MPV->new (trace => 1); |
58 | |
67 | |
59 | $mpv->start ("--", $videofile); |
68 | $mpv->start ("--", $videofile); |
60 | |
69 | |
… | |
… | |
62 | $quit->recv; |
71 | $quit->recv; |
63 | |
72 | |
64 | This starts F<mpv> with the two arguments C<--> and C<$videofile>, which |
73 | This starts F<mpv> with the two arguments C<--> and C<$videofile>, which |
65 | it should load and play. It then waits two seconds by starting a timer and |
74 | it should load and play. It then waits two seconds by starting a timer and |
66 | quits. The C<trace> argument to the constructor makes F<mpv> more verbose |
75 | quits. The C<trace> argument to the constructor makes F<mpv> more verbose |
67 | and also prints the commands and responses, so you cna have an idea what |
76 | and also prints the commands and responses, so you can have an idea what |
68 | is going on. |
77 | is going on. |
|
|
78 | |
|
|
79 | In my case, the above example would output something like this: |
|
|
80 | |
|
|
81 | [uosc] Disabled because original osc is enabled! |
|
|
82 | mpv> {"event":"start-file","playlist_entry_id":1} |
|
|
83 | mpv> {"event":"tracks-changed"} |
|
|
84 | (+) Video --vid=1 (*) (h264 480x480 30.000fps) |
|
|
85 | mpv> {"event":"metadata-update"} |
|
|
86 | mpv> {"event":"file-loaded"} |
|
|
87 | Using hardware decoding (nvdec). |
|
|
88 | mpv> {"event":"video-reconfig"} |
|
|
89 | VO: [gpu] 480x480 cuda[nv12] |
|
|
90 | mpv> {"event":"video-reconfig"} |
|
|
91 | mpv> {"event":"playback-restart"} |
69 | |
92 | |
70 | This is not usually very useful (you could just run F<mpv> as a simple |
93 | This is not usually very useful (you could just run F<mpv> as a simple |
71 | shell command), so let us load the file at runtime: |
94 | shell command), so let us load the file at runtime: |
72 | |
95 | |
73 | use AnyEvent; |
96 | use AnyEvent; |
74 | use AnyEvent::MPV; |
97 | use AnyEvent::MPV; |
75 | |
98 | |
76 | my $videofile = "./xyzzy.mp4"; |
99 | my $videofile = "./xyzzy.mkv"; |
77 | |
100 | |
78 | my $mpv = AnyEvent::MPV->new ( |
101 | my $mpv = AnyEvent::MPV->new ( |
79 | trace => 1, |
102 | trace => 1, |
80 | args => ["--pause", "--idle=yes"], |
103 | args => ["--pause", "--idle=yes"], |
81 | ); |
104 | ); |
… | |
… | |
126 | receiving events (using a somewhat embellished example): |
149 | receiving events (using a somewhat embellished example): |
127 | |
150 | |
128 | use AnyEvent; |
151 | use AnyEvent; |
129 | use AnyEvent::MPV; |
152 | use AnyEvent::MPV; |
130 | |
153 | |
131 | my $videofile = "xyzzy.mp4"; |
154 | my $videofile = "xyzzy.mkv"; |
132 | |
155 | |
133 | my $quit = AE::cv; |
156 | my $quit = AE::cv; |
134 | |
157 | |
135 | my $mpv = AnyEvent::MPV->new ( |
158 | my $mpv = AnyEvent::MPV->new ( |
136 | trace => 1, |
159 | trace => 1, |
137 | args => ["--pause", "--idle=yes"], |
160 | args => ["--pause", "--idle=yes"], |
138 | on_event => sub { |
|
|
139 | my ($mpv, $event, $data) = @_; |
|
|
140 | |
|
|
141 | if ($event eq "start-file") { |
|
|
142 | $mpv->cmd ("set", "pause", "no"); |
|
|
143 | } elsif ($event eq "end-file") { |
|
|
144 | print "end-file<$data->{reason}>\n"; |
|
|
145 | $quit->send; |
|
|
146 | } |
|
|
147 | }, |
|
|
148 | ); |
161 | ); |
149 | |
162 | |
150 | $mpv->start; |
163 | $mpv->start; |
|
|
164 | |
|
|
165 | $mpv->register_event (start_file => sub { |
|
|
166 | $mpv->cmd ("set", "pause", "no"); |
|
|
167 | }); |
|
|
168 | |
|
|
169 | $mpv->register_event (end_file => sub { |
|
|
170 | my ($mpv, $event, $data) = @_; |
|
|
171 | |
|
|
172 | print "end-file<$data->{reason}>\n"; |
|
|
173 | $quit->send; |
|
|
174 | }); |
|
|
175 | |
151 | $mpv->cmd (loadfile => $mpv->escape_binary ($videofile)); |
176 | $mpv->cmd (loadfile => $mpv->escape_binary ($videofile)); |
152 | |
177 | |
153 | $quit->recv; |
178 | $quit->recv; |
154 | |
179 | |
155 | This example uses a global condvar C<$quit> to wait for the file to finish |
180 | This example uses a global condvar C<$quit> to wait for the file to finish |
156 | playing. Also, most of the logic is now in an C<on_event> callback, which |
181 | playing. Also, most of the logic is now implement in event handlers. |
157 | receives an event name and the actual event object. |
|
|
158 | |
182 | |
159 | The two events we handle are C<start-file>, which is emitted by F<mpv> |
183 | The two events handlers we register are C<start-file>, which is emitted by |
160 | once it has loaded a new file, and C<end-file>, which signals the end |
184 | F<mpv> once it has loaded a new file, and C<end-file>, which signals the |
161 | of a file. |
185 | end of a file (underscores are internally replaced by minus signs, so you |
|
|
186 | cna speicfy event names with either). |
162 | |
187 | |
163 | In the former event, we again set the C<pause> property to C<no> so the |
188 | In the C<start-file> event, we again set the C<pause> property to C<no> |
164 | movie starts playing. For the latter event, we tell the main program to |
189 | so the movie starts playing. For the C<end-file> event, we tell the main |
165 | quit by invoking C<$quit>. |
190 | program to quit by invoking C<$quit>. |
166 | |
191 | |
167 | This should conclude the basics of operation. There are a few more |
192 | This should conclude the basics of operation. There are a few more |
168 | examples later in the documentation. |
193 | examples later in the documentation. |
169 | |
194 | |
170 | =head2 ENCODING CONVENTIONS |
195 | =head2 ENCODING CONVENTIONS |
171 | |
196 | |
172 | As a rule of thumb, all data you pass to this module to be sent to F<mpv> |
197 | As a rule of thumb, all data you pass to this module to be sent to F<mpv> |
173 | is expected to be in unicode. To pass something that isn't, you need to |
198 | is expected to be in unicode. To pass something that isn't, you need to |
174 | escape it using C<escape_binary>. |
199 | escape it using C<escape_binary>. |
175 | |
200 | |
176 | Data received from C<$mpv>, however, is I<not> decoded to unicode, as data |
201 | Data received from F<mpv>, however, is I<not> decoded to unicode, as data |
177 | returned by F<mpv> is not generally encoded in unicode, and the encoding |
202 | returned by F<mpv> is not generally encoded in unicode, and the encoding |
178 | is usually unspecified. So if you receive data and expect it to be in |
203 | is usually unspecified. So if you receive data and expect it to be in |
179 | unicode, you need to first decode it from UTF-8, but note that this might |
204 | unicode, you need to first decode it from UTF-8, but note that this might |
180 | fail. This is not a limitation of this module - F<mpv> simply does not |
205 | fail. This is not a limitation of this module - F<mpv> simply does not |
181 | specify nor guarantee a specific encoding, or any encoding at all, in its |
206 | specify nor guarantee a specific encoding, or any encoding at all, in its |
… | |
… | |
195 | use Scalar::Util (); |
220 | use Scalar::Util (); |
196 | |
221 | |
197 | use AnyEvent (); |
222 | use AnyEvent (); |
198 | use AnyEvent::Util (); |
223 | use AnyEvent::Util (); |
199 | |
224 | |
|
|
225 | our $VERSION = '0.2'; |
|
|
226 | |
|
|
227 | sub OBSID() { 0x10000000000000 } # 2**52 |
|
|
228 | |
200 | our $JSON = eval { require JSON::XS; JSON::XS:: } |
229 | our $JSON = eval { require JSON::XS; JSON::XS:: } |
201 | || do { require JSON::PP; JSON::PP:: }; |
230 | || do { require JSON::PP; JSON::PP:: }; |
202 | |
231 | |
203 | our $JSON_CODER = |
232 | our $JSON_ENCODER = $JSON->new->utf8; |
204 | |
233 | our $JSON_DECODER = $JSON->new->latin1; |
205 | our $VERSION = '0.1'; |
|
|
206 | |
234 | |
207 | our $mpv_path; # last mpv path used |
235 | our $mpv_path; # last mpv path used |
208 | our $mpv_optionlist; # output of mpv --list-options |
236 | our $mpv_optionlist; # output of mpv --list-options |
209 | |
237 | |
210 | =item $mpv = AnyEvent::MPV->new (key => value...) |
238 | =item $mpv = AnyEvent::MPV->new (key => value...) |
… | |
… | |
372 | my $trace = delete $self->{trace} || sub { }; |
400 | my $trace = delete $self->{trace} || sub { }; |
373 | |
401 | |
374 | $trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace; |
402 | $trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace; |
375 | |
403 | |
376 | my $buf; |
404 | my $buf; |
377 | my $wbuf; |
|
|
378 | |
405 | |
379 | Scalar::Util::weaken $self; |
406 | Scalar::Util::weaken $self; |
380 | |
407 | |
381 | $self->{rw} = AE::io $fh, 0, sub { |
408 | $self->{rw} = AE::io $fh, 0, sub { |
382 | if (sysread $fh, $buf, 8192, length $buf) { |
409 | if (sysread $fh, $buf, 8192, length $buf) { |
383 | while ($buf =~ s/^([^\n]+)\n//) { |
410 | while ($buf =~ s/^([^\n]+)\n//) { |
384 | $trace->("mpv>" => "$1"); |
411 | $trace->("mpv>" => "$1"); |
385 | |
412 | |
386 | if ("{" eq substr $1, 0, 1) { |
413 | if ("{" eq substr $1, 0, 1) { |
387 | eval { |
414 | eval { |
388 | my $reply = $JSON->new->latin1->decode ($1); |
415 | my $reply = $JSON_DECODER->decode ($1); |
389 | |
416 | |
390 | if (exists $reply->{event}) { |
417 | if (defined (my $event = delete $reply->{event})) { |
391 | if ( |
418 | if ( |
392 | $reply->{event} eq "client-message" |
419 | $event eq "client-message" |
393 | and $reply->{args}[0] eq "AnyEvent::MPV" |
420 | and $reply->{args}[0] eq "AnyEvent::MPV" |
394 | ) { |
421 | ) { |
395 | if ($reply->{args}[1] eq "key") { |
422 | if ($reply->{args}[1] eq "key") { |
396 | (my $key = $reply->{args}[2]) =~ s/\\x(..)/chr hex $1/ge; |
423 | (my $key = $reply->{args}[2]) =~ s/\\x(..)/chr hex $1/ge; |
397 | $self->on_key ($key); |
424 | $self->on_key ($key); |
398 | } |
425 | } |
|
|
426 | } elsif ( |
|
|
427 | $event eq "property-change" |
|
|
428 | and OBSID <= $reply->{id} |
|
|
429 | ) { |
|
|
430 | if (my $cb = $self->{obscb}{$reply->{id}}) { |
|
|
431 | $cb->($self, $event, $reply->{data}); |
|
|
432 | } |
399 | } else { |
433 | } else { |
|
|
434 | if (my $cbs = $self->{evtcb}{$event}) { |
|
|
435 | for my $evtid (keys %$cbs) { |
|
|
436 | my $cb = $cbs->{$evtid} |
|
|
437 | or next; |
|
|
438 | $cb->($self, $event, $reply); |
|
|
439 | } |
|
|
440 | } |
|
|
441 | |
400 | $self->on_event ($reply->{event}, $reply); |
442 | $self->on_event ($event, $reply); |
401 | } |
443 | } |
402 | } elsif (exists $reply->{request_id}) { |
444 | } elsif (exists $reply->{request_id}) { |
403 | my $cv = delete $self->{cmd_cv}{$reply->{request_id}}; |
445 | my $cv = delete $self->{cmdcv}{$reply->{request_id}}; |
404 | |
446 | |
405 | unless ($cv) { |
447 | unless ($cv) { |
406 | warn "no cv found for request id <$reply->{request_id}>\n"; |
448 | warn "no cv found for request id <$reply->{request_id}>\n"; |
407 | next; |
449 | next; |
408 | } |
450 | } |
… | |
… | |
428 | $self->stop; |
470 | $self->stop; |
429 | $self->on_eof; |
471 | $self->on_eof; |
430 | } |
472 | } |
431 | }; |
473 | }; |
432 | |
474 | |
|
|
475 | my $wbuf; |
|
|
476 | my $reqid; |
|
|
477 | |
433 | $self->{_send} = sub { |
478 | $self->{_cmd} = sub { |
434 | $wbuf .= "$_[0]\n"; |
479 | my $cv = AE::cv; |
435 | |
480 | |
|
|
481 | $self->{cmdcv}{++$reqid} = $cv; |
|
|
482 | |
|
|
483 | my $cmd = $JSON_ENCODER->encode ({ command => ref $_[0] ? $_[0] : \@_, request_id => $reqid*1 }); |
|
|
484 | |
|
|
485 | # (un-)apply escape_binary hack |
|
|
486 | $cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8 |
|
|
487 | |
436 | $trace->(">mpv" => "$_[0]"); |
488 | $trace->(">mpv" => $cmd); |
|
|
489 | |
|
|
490 | $wbuf .= "$cmd\n"; |
437 | |
491 | |
438 | $self->{ww} ||= AE::io $fh, 1, sub { |
492 | $self->{ww} ||= AE::io $fh, 1, sub { |
439 | my $len = syswrite $fh, $wbuf; |
493 | my $len = syswrite $fh, $wbuf; |
440 | substr $wbuf, 0, $len, ""; |
494 | substr $wbuf, 0, $len, ""; |
441 | undef $self->{ww} unless length $wbuf; |
495 | undef $self->{ww} unless length $wbuf; |
442 | }; |
496 | }; |
|
|
497 | |
|
|
498 | $cv |
443 | }; |
499 | }; |
444 | |
500 | |
445 | 1 |
501 | 1 |
|
|
502 | } |
|
|
503 | |
|
|
504 | sub DESTROY { |
|
|
505 | $_[0]->stop; |
446 | } |
506 | } |
447 | |
507 | |
448 | =item $mpv->stop |
508 | =item $mpv->stop |
449 | |
509 | |
450 | Ensures that F<mpv> is being stopped, by killing F<mpv> with a C<TERM> |
510 | Ensures that F<mpv> is being stopped, by killing F<mpv> with a C<TERM> |
… | |
… | |
465 | kill TERM => $self->{pid}; |
525 | kill TERM => $self->{pid}; |
466 | |
526 | |
467 | } |
527 | } |
468 | |
528 | |
469 | delete $self->{pid}; |
529 | delete $self->{pid}; |
470 | delete $self->{cmd_cv}; |
530 | delete $self->{cmdcv}; |
|
|
531 | delete $self->{evtid}; |
|
|
532 | delete $self->{evtcb}; |
|
|
533 | delete $self->{obsid}; |
|
|
534 | delete $self->{obscb}; |
|
|
535 | delete $self->{wbuf}; |
471 | } |
536 | } |
472 | |
537 | |
473 | =item $mpv->on_eof |
538 | =item $mpv->on_eof |
474 | |
539 | |
475 | This method is called when F<mpv> quits - usually unexpectedly. The |
540 | This method is called when F<mpv> quits - usually unexpectedly. The |
… | |
… | |
490 | |
555 | |
491 | This method is called when F<mpv> sends an asynchronous event. The default |
556 | This method is called when F<mpv> sends an asynchronous event. The default |
492 | implementation will call the C<on_event> code reference specified in the |
557 | implementation will call the C<on_event> code reference specified in the |
493 | constructor, or do nothing if none was given. |
558 | constructor, or do nothing if none was given. |
494 | |
559 | |
495 | The first/implicit argument is the C<$mpv> object, the second is the event |
560 | The first/implicit argument is the C<$mpv> object, the second is the |
496 | name (same as C<< $data->{event} >>, purely for convenience), and the |
561 | event name (same as C<< $data->{event} >>, purely for convenience), and |
497 | third argument is the full event object as sent by F<mpv>. See L<List of |
562 | the third argument is the event object as sent by F<mpv> (sans C<event> |
498 | events|https://mpv.io/manual/stable/#list-of-events> in its documentation. |
563 | key). See L<List of events|https://mpv.io/manual/stable/#list-of-events> |
|
|
564 | in its documentation. |
499 | |
565 | |
500 | For subclassing, see I<SUBCLASSING>, below. |
566 | For subclassing, see I<SUBCLASSING>, below. |
501 | |
567 | |
502 | =cut |
568 | =cut |
503 | |
569 | |
… | |
… | |
562 | On error, the condvar will croak when C<recv> is called. |
628 | On error, the condvar will croak when C<recv> is called. |
563 | |
629 | |
564 | =cut |
630 | =cut |
565 | |
631 | |
566 | sub cmd { |
632 | sub cmd { |
567 | my ($self, @cmd) = @_; |
633 | my $self = shift; |
568 | |
634 | |
569 | my $cv = AE::cv; |
635 | $self->{_cmd}->(@_) |
570 | |
|
|
571 | my $reqid = ++$self->{reqid}; |
|
|
572 | $self->{cmd_cv}{$reqid} = $cv; |
|
|
573 | |
|
|
574 | my $cmd = $JSON->new->utf8->encode ({ command => ref $cmd[0] ? $cmd[0] : \@cmd, request_id => $reqid*1 }); |
|
|
575 | |
|
|
576 | # (un-)apply escape_binary hack |
|
|
577 | $cmd =~ s/\xf4\x8e\x97\x9f(..)/sprintf sprintf "\\x%02x", hex $1/ges; # f48e979f == 10e5df in utf-8 |
|
|
578 | |
|
|
579 | $self->{_send}($cmd); |
|
|
580 | |
|
|
581 | $cv |
|
|
582 | } |
636 | } |
583 | |
637 | |
584 | =item $result = $mpv->cmd_recv ($command => $arg, $arg...) |
638 | =item $result = $mpv->cmd_recv ($command => $arg, $arg...) |
585 | |
639 | |
586 | The same as calling C<cmd> and immediately C<recv> on its return |
640 | The same as calling C<cmd> and immediately C<recv> on its return |
… | |
… | |
596 | &cmd->recv |
650 | &cmd->recv |
597 | } |
651 | } |
598 | |
652 | |
599 | =item $mpv->bind_key ($INPUT => $string) |
653 | =item $mpv->bind_key ($INPUT => $string) |
600 | |
654 | |
601 | This is an extension implement by this module to make it easy to get key events. The way this is implemented |
655 | This is an extension implement by this module to make it easy to get key |
602 | is to bind a C<client-message> witha first argument of C<AnyEvent::MPV> and the C<$string> you passed. This C<$string> is then |
656 | events. The way this is implemented is to bind a C<client-message> witha |
603 | passed ot the C<on_key> handle when the key is proessed, e.g.: |
657 | first argument of C<AnyEvent::MPV> and the C<$string> you passed. This |
|
|
658 | C<$string> is then passed to the C<on_key> handle when the key is |
|
|
659 | proessed, e.g.: |
604 | |
660 | |
605 | my $mpv = AnyEvent::MPV->new ( |
661 | my $mpv = AnyEvent::MPV->new ( |
606 | on_key => sub { |
662 | on_key => sub { |
607 | my ($mpv, $key) = @_; |
663 | my ($mpv, $key) = @_; |
608 | |
664 | |
… | |
… | |
612 | }, |
668 | }, |
613 | ); |
669 | ); |
614 | |
670 | |
615 | $mpv_>bind_key (ESC => "letmeout"); |
671 | $mpv_>bind_key (ESC => "letmeout"); |
616 | |
672 | |
|
|
673 | You cna find a list of key names L<in the mpv |
|
|
674 | documentation|https://mpv.io/manual/stable/#key-names>. |
|
|
675 | |
617 | The key configuration is lost when F<mpv> is stopped and must be (re-)done |
676 | The key configuration is lost when F<mpv> is stopped and must be (re-)done |
618 | after every C<start>. |
677 | after every C<start>. |
619 | |
678 | |
620 | =cut |
679 | =cut |
621 | |
680 | |
622 | sub bind_key { |
681 | sub bind_key { |
623 | my ($self, $key, $event) = @_; |
682 | my ($self, $key, $event) = @_; |
624 | |
683 | |
625 | $event =~ s/([^A-Za-z0-9\-_])/sprintf "\\x%02x", ord $1/ge; |
684 | $event =~ s/([^A-Za-z0-9\-_])/sprintf "\\x%02x", ord $1/ge; |
626 | $self->cmd (keybind => $key => "no-osd script-message AnyEvent::MPV key $event"); |
685 | $self->cmd (keybind => $key => "no-osd script-message AnyEvent::MPV key $event"); |
|
|
686 | } |
|
|
687 | |
|
|
688 | =item [$guard] = $mpv->register_event ($event => $coderef->($mpv, $event, $data)) |
|
|
689 | |
|
|
690 | This method registers a callback to be invoked for a specific |
|
|
691 | event. Whenever the event occurs, it calls the coderef with the C<$mpv> |
|
|
692 | object, the C<$event> name and the event object, just like the C<on_event> |
|
|
693 | method. |
|
|
694 | |
|
|
695 | For a lst of events, see L<the mpv |
|
|
696 | documentation|https://mpv.io/manual/stable/#list-of-events>. Any |
|
|
697 | underscore in the event name is replaced by a minus sign, so you can |
|
|
698 | specify event names using underscores for easier quoting in Perl. |
|
|
699 | |
|
|
700 | In void context, the handler stays registered until C<stop> is called. In |
|
|
701 | any other context, it returns a guard object that, when destroyed, will |
|
|
702 | unregister the handler. |
|
|
703 | |
|
|
704 | You can register multiple handlers for the same event, and this method |
|
|
705 | does not interfere with the C<on_event> mechanism. That is, you can |
|
|
706 | completely ignore this method and handle events in a C<on_event> handler, |
|
|
707 | or mix both approaches as you see fit. |
|
|
708 | |
|
|
709 | Note that unlike commands, event handlers are registered immediately, that |
|
|
710 | is, you can issue a command, then register an event handler and then get |
|
|
711 | an event for this handler I<before> the command is even sent to F<mpv>. If |
|
|
712 | this kind of race is an issue, you can issue a dummy command such as |
|
|
713 | C<get_version> and register the handler when the reply is received. |
|
|
714 | |
|
|
715 | =cut |
|
|
716 | |
|
|
717 | sub AnyEvent::MPV::Unevent::DESTROY { |
|
|
718 | my ($evtcb, $event, $evtid) = @{$_[0]}; |
|
|
719 | delete $evtcb->{$event}{$evtid}; |
|
|
720 | } |
|
|
721 | |
|
|
722 | sub register_event { |
|
|
723 | my ($self, $event, $cb) = @_; |
|
|
724 | |
|
|
725 | $event =~ y/_/-/; |
|
|
726 | |
|
|
727 | my $evtid = ++$self->{evtid}; |
|
|
728 | $self->{evtcb}{$event}{$evtid} = $cb; |
|
|
729 | |
|
|
730 | defined wantarray |
|
|
731 | and bless [$self->{evtcb}, $event, $evtid], AnyEvent::MPV::Unevent:: |
|
|
732 | } |
|
|
733 | |
|
|
734 | =item [$guard] = $mpv->observe_property ($name => $coderef->($mpv, $name, $value)) |
|
|
735 | |
|
|
736 | =item [$guard] = $mpv->observe_property_string ($name => $coderef->($mpv, $name, $value)) |
|
|
737 | |
|
|
738 | These methods wrap a registry system around F<mpv>'s C<observe_property> |
|
|
739 | and C<observe_property_string> commands - every time the named property |
|
|
740 | changes, the coderef is invoked with the C<$mpv> object, the name of the |
|
|
741 | property and the new value. |
|
|
742 | |
|
|
743 | For a list of properties that you can observe, see L<the mpv |
|
|
744 | documentation|https://mpv.io/manual/stable/#property-list>. |
|
|
745 | |
|
|
746 | Due to the (sane :) way F<mpv> handles these requests, you will always |
|
|
747 | get a property cxhange event right after registering an observer (meaning |
|
|
748 | you don't have to query the current value), and it is also possible to |
|
|
749 | register multiple observers for the same property - they will all be |
|
|
750 | handled properly. |
|
|
751 | |
|
|
752 | When called in void context, the observer stays in place until F<mpv> |
|
|
753 | is stopped. In any otrher context, these methods return a guard |
|
|
754 | object that, when it goes out of scope, unregisters the observe using |
|
|
755 | C<unobserve_property>. |
|
|
756 | |
|
|
757 | Internally, this method uses observer ids of 2**52 (0x10000000000000) or |
|
|
758 | higher - it will not interfere with lower ovserver ids, so it is possible |
|
|
759 | to completely ignore this system and execute C<observe_property> commands |
|
|
760 | yourself, whilst listening to C<property-change> events - as long as your |
|
|
761 | ids stay below 2**52. |
|
|
762 | |
|
|
763 | Example: register observers for changtes in C<aid> and C<sid>. Note that |
|
|
764 | a dummy statement is added to make sure the method is called in void |
|
|
765 | context. |
|
|
766 | |
|
|
767 | sub register_observers { |
|
|
768 | my ($mpv) = @_; |
|
|
769 | |
|
|
770 | $mpv->observe_property (aid => sub { |
|
|
771 | my ($mpv, $name, $value) = @_; |
|
|
772 | print "property aid (=$name) has changed to $value\n"; |
|
|
773 | }); |
|
|
774 | |
|
|
775 | $mpv->observe_property (sid => sub { |
|
|
776 | my ($mpv, $name, $value) = @_; |
|
|
777 | print "property sid (=$name) has changed to $value\n"; |
|
|
778 | }); |
|
|
779 | |
|
|
780 | () # ensure the above method is called in void context |
|
|
781 | } |
|
|
782 | |
|
|
783 | =cut |
|
|
784 | |
|
|
785 | sub AnyEvent::MPV::Unobserve::DESTROY { |
|
|
786 | my ($mpv, $obscb, $obsid) = @{$_[0]}; |
|
|
787 | |
|
|
788 | delete $obscb->{$obsid}; |
|
|
789 | |
|
|
790 | if ($obscb == $mpv->{obscb}) { |
|
|
791 | $mpv->cmd (unobserve_property => $obsid+0); |
|
|
792 | } |
|
|
793 | } |
|
|
794 | |
|
|
795 | sub _observe_property { |
|
|
796 | my ($self, $type, $property, $cb) = @_; |
|
|
797 | |
|
|
798 | my $obsid = OBSID + ++$self->{obsid}; |
|
|
799 | $self->cmd ($type => $obsid+0, $property); |
|
|
800 | $self->{obscb}{$obsid} = $cb; |
|
|
801 | |
|
|
802 | defined wantarray and do { |
|
|
803 | my $unobserve = bless [$self, $self->{obscb}, $obsid], AnyEvent::MPV::Unobserve::; |
|
|
804 | Scalar::Util::weaken $unobserve->[0]; |
|
|
805 | $unobserve |
|
|
806 | } |
|
|
807 | } |
|
|
808 | |
|
|
809 | sub observe_property { |
|
|
810 | my ($self, $property, $cb) = @_; |
|
|
811 | |
|
|
812 | $self->_observe_property (observe_property => $property, $cb) |
|
|
813 | } |
|
|
814 | |
|
|
815 | sub observe_property_string { |
|
|
816 | my ($self, $property, $cb) = @_; |
|
|
817 | |
|
|
818 | $self->_observe_property (observe_property_string => $property, $cb) |
627 | } |
819 | } |
628 | |
820 | |
629 | =back |
821 | =back |
630 | |
822 | |
631 | =head2 SUBCLASSING |
823 | =head2 SUBCLASSING |
… | |
… | |
639 | care and deal with the breakage. |
831 | care and deal with the breakage. |
640 | |
832 | |
641 | If you don't want to go to the effort of subclassing this module, you can |
833 | If you don't want to go to the effort of subclassing this module, you can |
642 | also specify all event handlers as constructor keys. |
834 | also specify all event handlers as constructor keys. |
643 | |
835 | |
|
|
836 | =head1 EXAMPLES |
|
|
837 | |
|
|
838 | Here are some real-world code snippets, thrown in here mainly to give you |
|
|
839 | some example code to copy. |
|
|
840 | |
|
|
841 | =head2 doomfrontend |
|
|
842 | |
|
|
843 | At one point I replaced mythtv-frontend by my own terminal-based video |
|
|
844 | player (based on rxvt-unicode). I toyed with the diea of using F<mpv>'s |
|
|
845 | subtitle engine to create the user interface, but that is hard to use |
|
|
846 | since you don't know how big your letters are. It is also where most of |
|
|
847 | this modules code has originally been developed in. |
|
|
848 | |
|
|
849 | It uses a unified input queue to handle various remote controls, so its |
|
|
850 | event handling needs are very simple - it simply feeds all events into the |
|
|
851 | input queue: |
|
|
852 | |
|
|
853 | my $mpv = AnyEvent::MPV->new ( |
|
|
854 | mpv => $MPV, |
|
|
855 | args => \@MPV_ARGS, |
|
|
856 | on_event => sub { |
|
|
857 | input_feed "mpv/$_[1]", $_[2]; |
|
|
858 | }, |
|
|
859 | on_key => sub { |
|
|
860 | input_feed $_[1]; |
|
|
861 | }, |
|
|
862 | on_eof => sub { |
|
|
863 | input_feed "mpv/quit"; |
|
|
864 | }, |
|
|
865 | ); |
|
|
866 | |
|
|
867 | ... |
|
|
868 | |
|
|
869 | $mpv->start ("--idle=yes", "--pause", "--force-window=no"); |
|
|
870 | |
|
|
871 | It also doesn't use complicated command line arguments - the file search |
|
|
872 | options have the most impact, as they prevent F<mpv> from scanning |
|
|
873 | directories with tens of thousands of files for subtitles and more: |
|
|
874 | |
|
|
875 | --audio-client-name=doomfrontend |
|
|
876 | --osd-on-seek=msg-bar --osd-bar-align-y=-0.85 --osd-bar-w=95 |
|
|
877 | --sub-auto=exact --audio-file-auto=exact |
|
|
878 | |
|
|
879 | Since it runs on a TV without a desktop environemnt, it tries to keep complications such as dbus |
|
|
880 | away and the screensaver happy: |
|
|
881 | |
|
|
882 | # prevent xscreensaver from doing something stupid, such as starting dbus |
|
|
883 | $ENV{DBUS_SESSION_BUS_ADDRESS} = "/"; # prevent dbus autostart for sure |
|
|
884 | $ENV{XDG_CURRENT_DESKTOP} = "generic"; |
|
|
885 | |
|
|
886 | It does bind a number of keys to internal (to doomfrontend) commands: |
|
|
887 | |
|
|
888 | for ( |
|
|
889 | List::Util::pairs qw( |
|
|
890 | ESC return |
|
|
891 | q return |
|
|
892 | ENTER enter |
|
|
893 | SPACE pause |
|
|
894 | [ steprev |
|
|
895 | ] stepfwd |
|
|
896 | j subtitle |
|
|
897 | BS red |
|
|
898 | i green |
|
|
899 | o yellow |
|
|
900 | b blue |
|
|
901 | D triangle |
|
|
902 | UP up |
|
|
903 | DOWN down |
|
|
904 | RIGHT right |
|
|
905 | LEFT left |
|
|
906 | ), |
|
|
907 | (map { ("KP$_" => "num$_") } 0..9), |
|
|
908 | KP_INS => 0, # KP0, but different |
|
|
909 | ) { |
|
|
910 | $mpv->bind_key ($_->[0] => $_->[1]); |
|
|
911 | } |
|
|
912 | |
|
|
913 | It also reacts to sponsorblock chapters, so it needs to know when vidoe |
|
|
914 | chapters change. Preadting C<AnyEvent::MPV>, it handles observers |
|
|
915 | manually: |
|
|
916 | |
|
|
917 | $mpv->cmd (observe_property => 1, "chapter-metadata"); |
|
|
918 | |
|
|
919 | It also tries to apply an F<mpv> profile, if it exists: |
|
|
920 | |
|
|
921 | eval { |
|
|
922 | # the profile is optional |
|
|
923 | $mpv->cmd ("apply-profile" => "doomfrontend"); |
|
|
924 | }; |
|
|
925 | |
|
|
926 | Most of the complicated parts deal with saving and restoring per-video |
|
|
927 | data, such as bookmarks, playing position, selected audio and subtitle |
|
|
928 | tracks and so on. However, since it uses L<Coro>, it can conveniently |
|
|
929 | block and wait for replies, which is n ot possible in purely event based |
|
|
930 | programs, as you are not allowed to block inside event callbacks in most |
|
|
931 | event loops. This simplifies the code quite a bit. |
|
|
932 | |
|
|
933 | When the file to be played is a Tv recording done by mythtv, it uses the |
|
|
934 | C<appending> protocol and deinterlacing: |
|
|
935 | |
|
|
936 | if (is_myth $mpv_path) { |
|
|
937 | $mpv_path = "appending://$mpv_path"; |
|
|
938 | $initial_deinterlace = 1; |
|
|
939 | } |
|
|
940 | |
|
|
941 | Otherwise, it sets some defaults and loads the file (I forgot what the |
|
|
942 | C<dummy> argument is for, but I am sure it is needed by some F<mpv> |
|
|
943 | version): |
|
|
944 | |
|
|
945 | $mpv->cmd ("script-message", "osc-visibility", "never", "dummy"); |
|
|
946 | $mpv->cmd ("set", "vid", "auto"); |
|
|
947 | $mpv->cmd ("set", "aid", "auto"); |
|
|
948 | $mpv->cmd ("set", "sid", "no"); |
|
|
949 | $mpv->cmd ("set", "file-local-options/chapters-file", $mpv->escape_binary ("$mpv_path.chapters")); |
|
|
950 | $mpv->cmd ("loadfile", $mpv->escape_binary ($mpv_path)); |
|
|
951 | $mpv->cmd ("script-message", "osc-visibility", "auto", "dummy"); |
|
|
952 | |
|
|
953 | Handling events makes the main bulk of video playback code. For example, |
|
|
954 | various ways of ending playback: |
|
|
955 | |
|
|
956 | if ($INPUT eq "mpv/quit") { # should not happen, but allows user to kill etc. without consequence |
|
|
957 | $status = 1; |
|
|
958 | mpv_init; # try reinit |
|
|
959 | last; |
|
|
960 | |
|
|
961 | } elsif ($INPUT eq "mpv/idle") { # normal end-of-file |
|
|
962 | last; |
|
|
963 | |
|
|
964 | } elsif ($INPUT eq "return") { |
|
|
965 | $status = 1; |
|
|
966 | last; |
|
|
967 | |
|
|
968 | Or the code that actually starts playback, once the file is loaded: |
|
|
969 | |
|
|
970 | our %SAVE_PROPERTY = (aid => 1, sid => 1, "audio-delay" => 1); |
|
|
971 | |
|
|
972 | ... |
|
|
973 | |
|
|
974 | my $oid = 100; |
|
|
975 | |
|
|
976 | } elsif ($INPUT eq "mpv/file-loaded") { # start playing, configure video |
|
|
977 | $mpv->cmd ("seek", $playback_start, "absolute+exact") if $playback_start > 0; |
|
|
978 | |
|
|
979 | my $target_fps = eval { $mpv->cmd_recv ("get_property", "container-fps") } || 60; |
|
|
980 | $target_fps *= play_video_speed_mult; |
|
|
981 | set_fps $target_fps; |
|
|
982 | |
|
|
983 | unless (eval { $mpv->cmd_recv ("get_property", "video-format") }) { |
|
|
984 | $mpv->cmd ("set", "file-local-options/lavfi-complex", "[aid1] asplit [ao], showcqt=..., format=yuv420p [vo]"); |
|
|
985 | }; |
|
|
986 | |
|
|
987 | for my $prop (keys %SAVE_PROPERTY) { |
|
|
988 | if (exists $PLAYING_STATE->{"mpv_$prop"}) { |
|
|
989 | $mpv->cmd ("set", "$prop", $PLAYING_STATE->{"mpv_$prop"} . ""); |
|
|
990 | } |
|
|
991 | |
|
|
992 | $mpv->cmd ("observe_property", ++$oid, $prop); |
|
|
993 | } |
|
|
994 | |
|
|
995 | play_video_set_speed; |
|
|
996 | $mpv->cmd ("set", "osd-level", "$OSD_LEVEL"); |
|
|
997 | $mpv->cmd ("observe_property", ++$oid, "osd-level"); |
|
|
998 | $mpv->cmd ("set", "pause", "no"); |
|
|
999 | |
|
|
1000 | $mpv->cmd ("set_property", "deinterlace", "yes") |
|
|
1001 | if $initial_deinterlace; |
|
|
1002 | |
|
|
1003 | There is a lot going on here. First it seeks to the actual playback |
|
|
1004 | position, if it is not at the start of the file (it would probaby be more |
|
|
1005 | efficient to set the starting position before loading the file, though, |
|
|
1006 | but this is good enough). |
|
|
1007 | |
|
|
1008 | Then it plays with the display fps, to set it to something harmonious |
|
|
1009 | w.r.t. the video framerate. |
|
|
1010 | |
|
|
1011 | If the file does not have a video part, it assumes it is an audio file and |
|
|
1012 | sets a visualizer. |
|
|
1013 | |
|
|
1014 | Also, a number of properties are not global, but per-file. At the moment, |
|
|
1015 | this is C<audio-delay>, and the current audio/subtitle track, which it |
|
|
1016 | sets, and also creates an observer. Again, this doesn'T use the observe |
|
|
1017 | functionality of this module, but handles it itself, assigning obsevrer |
|
|
1018 | ids 100+ to temporary/per-file observers. |
|
|
1019 | |
|
|
1020 | Lastly, it sets some global (or per-youtube-uploader) parameters, such as |
|
|
1021 | speed, and unpauses. Property changes are handled like other input events: |
|
|
1022 | |
|
|
1023 | } elsif ($INPUT eq "mpv/property-change") { |
|
|
1024 | my $prop = $INPUT_DATA->{name}; |
|
|
1025 | |
|
|
1026 | if ($prop eq "chapter-metadata") { |
|
|
1027 | if ($INPUT_DATA->{data}{TITLE} =~ /^\[SponsorBlock\]: (.*)/) { |
|
|
1028 | my $section = $1; |
|
|
1029 | my $skip; |
|
|
1030 | |
|
|
1031 | $skip ||= $SPONSOR_SKIP{$_} |
|
|
1032 | for split /\s*,\s*/, $section; |
|
|
1033 | |
|
|
1034 | if (defined $skip) { |
|
|
1035 | if ($skip) { |
|
|
1036 | # delay a bit, in case we get two metadata changes in quick succession, e.g. |
|
|
1037 | # because we have a skip at file load time. |
|
|
1038 | $skip_delay = AE::timer 2/50, 0, sub { |
|
|
1039 | $mpv->cmd ("no-osd", "add", "chapter", 1); |
|
|
1040 | $mpv->cmd ("show-text", "skipped sponsorblock section \"$section\"", 3000); |
|
|
1041 | }; |
|
|
1042 | } else { |
|
|
1043 | undef $skip_delay; |
|
|
1044 | $mpv->cmd ("show-text", "NOT skipping sponsorblock section \"$section\"", 3000); |
|
|
1045 | } |
|
|
1046 | } else { |
|
|
1047 | $mpv->cmd ("show-text", "UNRECOGNIZED sponsorblock section \"$section\"", 60000); |
|
|
1048 | } |
|
|
1049 | } else { |
|
|
1050 | # cancel a queued skip |
|
|
1051 | undef $skip_delay; |
|
|
1052 | } |
|
|
1053 | |
|
|
1054 | } elsif (exists $SAVE_PROPERTY{$prop}) { |
|
|
1055 | $PLAYING_STATE->{"mpv_$prop"} = $INPUT_DATA->{data}; |
|
|
1056 | ::state_save; |
|
|
1057 | } |
|
|
1058 | |
|
|
1059 | This saves back the per-file properties, and also handles chapter changes |
|
|
1060 | in a hacky way. |
|
|
1061 | |
|
|
1062 | Most of the handlers are very simple, though. For example: |
|
|
1063 | |
|
|
1064 | } elsif ($INPUT eq "pause") { |
|
|
1065 | $mpv->cmd ("cycle", "pause"); |
|
|
1066 | $PLAYING_STATE->{curpos} = $mpv->cmd_recv ("get_property", "playback-time"); |
|
|
1067 | } elsif ($INPUT eq "right") { |
|
|
1068 | $mpv->cmd ("osd-msg-bar", "seek", 30, "relative+exact"); |
|
|
1069 | } elsif ($INPUT eq "left") { |
|
|
1070 | $mpv->cmd ("osd-msg-bar", "seek", -5, "relative+exact"); |
|
|
1071 | } elsif ($INPUT eq "up") { |
|
|
1072 | $mpv->cmd ("osd-msg-bar", "seek", +600, "relative+exact"); |
|
|
1073 | } elsif ($INPUT eq "down") { |
|
|
1074 | $mpv->cmd ("osd-msg-bar", "seek", -600, "relative+exact"); |
|
|
1075 | } elsif ($INPUT eq "select") { |
|
|
1076 | $mpv->cmd ("osd-msg-bar", "add", "audio-delay", "-0.100"); |
|
|
1077 | } elsif ($INPUT eq "start") { |
|
|
1078 | $mpv->cmd ("osd-msg-bar", "add", "audio-delay", "0.100"); |
|
|
1079 | } elsif ($INPUT eq "intfwd") { |
|
|
1080 | $mpv->cmd ("no-osd", "frame-step"); |
|
|
1081 | } elsif ($INPUT eq "audio") { |
|
|
1082 | $mpv->cmd ("osd-auto", "cycle", "audio"); |
|
|
1083 | } elsif ($INPUT eq "subtitle") { |
|
|
1084 | $mpv->cmd ("osd-auto", "cycle", "sub"); |
|
|
1085 | } elsif ($INPUT eq "triangle") { |
|
|
1086 | $mpv->cmd ("osd-auto", "cycle", "deinterlace"); |
|
|
1087 | |
|
|
1088 | Once a file has finished playing (or the user strops playback), it pauses, |
|
|
1089 | unobserves the per-file observers, and saves the current position for to |
|
|
1090 | be able to resume: |
|
|
1091 | |
|
|
1092 | $mpv->cmd ("set", "pause", "yes"); |
|
|
1093 | |
|
|
1094 | while ($oid > 100) { |
|
|
1095 | $mpv->cmd ("unobserve_property", $oid--); |
|
|
1096 | } |
|
|
1097 | |
|
|
1098 | $PLAYING_STATE->{curpos} = $mpv->cmd_recv ("get_property", "playback-time"); |
|
|
1099 | |
|
|
1100 | And thats most of the F<mpv>-related code. |
|
|
1101 | |
|
|
1102 | =head2 F<Gtk2::CV> |
|
|
1103 | |
|
|
1104 | F<Gtk2::CV> is low-feature image viewer that I use many times daily |
|
|
1105 | because it can handle directories with millions of files without falling |
|
|
1106 | over. It also had the ability to play videos for ages, but it used an |
|
|
1107 | older, crappier protocol to talk to F<mpv> and used F<ffprobe> before |
|
|
1108 | playing each file instead of letting F<mpv> handle format/size detection. |
|
|
1109 | |
|
|
1110 | After writing this module, I decided to upgprade Gtk2::CV by making use |
|
|
1111 | of it, with the goal of getting rid of F<ffprobe> and being ablew to |
|
|
1112 | reuse F<mpv> processes, which would have a multitude of speed benefits |
|
|
1113 | (for example, fork+exec of F<mpv> caused the kernel to close all file |
|
|
1114 | descriptors, which could take minutes if a large file was being copied via |
|
|
1115 | NFS, as the kernel waited for thr buffers to be flushed on close - not |
|
|
1116 | having to start F<mpv> gets rid of this issue). |
|
|
1117 | |
|
|
1118 | Setting up is only complicated by the fact that F<mpv> needs to be |
|
|
1119 | embedded into an existing window. To keep control of all inputs, |
|
|
1120 | F<Gtk2::CV> puts an eventbox in front of F<mpv>, so F<mpv> receives no |
|
|
1121 | input events: |
|
|
1122 | |
|
|
1123 | $self->{mpv} = AnyEvent::MPV->new ( |
|
|
1124 | trace => $ENV{CV_MPV_TRACE}, |
|
|
1125 | ); |
|
|
1126 | |
|
|
1127 | # create an eventbox, so we receive all input events |
|
|
1128 | my $box = $self->{mpv_eventbox} = new Gtk2::EventBox; |
|
|
1129 | $box->set_above_child (1); |
|
|
1130 | $box->set_visible_window (0); |
|
|
1131 | $box->set_events ([]); |
|
|
1132 | $box->can_focus (0); |
|
|
1133 | |
|
|
1134 | # create a drawingarea that mpv can display into |
|
|
1135 | my $window = $self->{mpv_window} = new Gtk2::DrawingArea; |
|
|
1136 | $box->add ($window); |
|
|
1137 | |
|
|
1138 | # put the drawingarea intot he eventbox, and the eventbox into our display window |
|
|
1139 | $self->add ($box); |
|
|
1140 | |
|
|
1141 | # we need to pass the window id to F<mpv>, which means we need to realise |
|
|
1142 | # the drawingarea, so an X window is allocated for it. |
|
|
1143 | $self->show_all; |
|
|
1144 | $window->realize; |
|
|
1145 | my $xid = $window->window->get_xid; |
|
|
1146 | |
|
|
1147 | Then it starts F<mpv> using this setup: |
|
|
1148 | |
|
|
1149 | local $ENV{LC_ALL} = "POSIX"; |
|
|
1150 | $self->{mpv}->start ( |
|
|
1151 | "--no-terminal", |
|
|
1152 | "--no-input-terminal", |
|
|
1153 | "--no-input-default-bindings", |
|
|
1154 | "--no-input-cursor", |
|
|
1155 | "--input-conf=/dev/null", |
|
|
1156 | "--input-vo-keyboard=no", |
|
|
1157 | |
|
|
1158 | "--loop-file=inf", |
|
|
1159 | "--force-window=yes", |
|
|
1160 | "--idle=yes", |
|
|
1161 | |
|
|
1162 | "--audio-client-name=CV", |
|
|
1163 | |
|
|
1164 | "--osc=yes", # --osc=no displays fading play/pause buttons instead |
|
|
1165 | |
|
|
1166 | "--wid=$xid", |
|
|
1167 | ); |
|
|
1168 | |
|
|
1169 | $self->{mpv}->cmd ("script-message" => "osc-visibility" => "never", "dummy"); |
|
|
1170 | $self->{mpv}->cmd ("osc-idlescreen" => "no"); |
|
|
1171 | |
|
|
1172 | It also prepares a hack to force a ConfigureNotify event on every vidoe |
|
|
1173 | reconfig: |
|
|
1174 | |
|
|
1175 | # force a configurenotify on every video-reconfig |
|
|
1176 | $self->{mpv_reconfig} = $self->{mpv}->register_event (video_reconfig => sub { |
|
|
1177 | my ($mpv, $event, $data) = @_; |
|
|
1178 | |
|
|
1179 | $self->mpv_window_update; |
|
|
1180 | }); |
|
|
1181 | |
|
|
1182 | The way this is done is by doing a "dummy" resize to 1x1 and back: |
|
|
1183 | |
|
|
1184 | $self->{mpv_window}->window->resize (1, 1), |
|
|
1185 | $self->{mpv_window}->window->resize ($self->{w}, $self->{h}); |
|
|
1186 | |
|
|
1187 | Without this, F<mpv> often doesn't "get" the correct window size. Doing |
|
|
1188 | it this way is not nice, but I didn't fine a nicer way to do it. |
|
|
1189 | |
|
|
1190 | When no file is being played, F<mpv> is hidden and prepared: |
|
|
1191 | |
|
|
1192 | $self->{mpv_eventbox}->hide; |
|
|
1193 | |
|
|
1194 | $self->{mpv}->cmd (set_property => "pause" => "yes"); |
|
|
1195 | $self->{mpv}->cmd ("playlist_remove", "current"); |
|
|
1196 | $self->{mpv}->cmd (set_property => "video-rotate" => 0); |
|
|
1197 | $self->{mpv}->cmd (set_property => "lavfi-complex" => ""); |
|
|
1198 | |
|
|
1199 | Loading a file is a bit more complicated, as bluray and DVD rips are |
|
|
1200 | supported: |
|
|
1201 | |
|
|
1202 | if ($moviedir) { |
|
|
1203 | if ($moviedir eq "br") { |
|
|
1204 | $mpv->cmd (set => "bluray-device" => $path); |
|
|
1205 | $mpv->cmd (loadfile => "bd://"); |
|
|
1206 | } elsif ($moviedir eq "dvd") { |
|
|
1207 | $mpv->cmd (set => "dvd-device" => $path); |
|
|
1208 | $mpv->cmd (loadfile => "dvd://"); |
|
|
1209 | } |
|
|
1210 | } elsif ($type eq "video/iso-bluray") { |
|
|
1211 | $mpv->cmd (set => "bluray-device" => $path); |
|
|
1212 | $mpv->cmd (loadfile => "bd://"); |
|
|
1213 | } else { |
|
|
1214 | $mpv->cmd (loadfile => $mpv->escape_binary ($path)); |
|
|
1215 | } |
|
|
1216 | |
|
|
1217 | After this, C<Gtk2::CV> waits for the file to be loaded, video to be |
|
|
1218 | configured, and then queries the video size (to resize its own window) |
|
|
1219 | and video format (to decide whether an audio visualizer is needed for |
|
|
1220 | audio playback). The problematic word here is "wait", as this needs to be |
|
|
1221 | imploemented using callbacks. |
|
|
1222 | |
|
|
1223 | This made the code much harder to write, as the whole setup is very |
|
|
1224 | asynchronous (C<Gtk2::CV> talks to the command interface in F<mpv>, which |
|
|
1225 | talks to the decode and playback parts, all of which run asynchronously |
|
|
1226 | w.r.t. each other. In practise, this can mean that C<Gtk2::CV> waits for |
|
|
1227 | a file to be loaded by F<mpv> while the command interface of F<mpv> still |
|
|
1228 | deals with the previous file and the decoder still handles an even older |
|
|
1229 | file). Adding to this fact is that Gtk2::CV is bound by the glib event |
|
|
1230 | loop, which means we cannot wait for replies form F<mpv> anywhere, so |
|
|
1231 | everything has to be chained callbacks. |
|
|
1232 | |
|
|
1233 | The way this is handled is by creating a new empty hash ref that is unique |
|
|
1234 | for each loaded file, and use it to detect whether the event is old or |
|
|
1235 | not, and also store C<AnyEvent::MPV> guard objects in it: |
|
|
1236 | |
|
|
1237 | # every time we loaded a file, we create a new hash |
|
|
1238 | my $guards = $self->{mpv_guards} = { }; |
|
|
1239 | |
|
|
1240 | Then, when we wait for an event to occur, delete the handler, and, if the |
|
|
1241 | C<mpv_guards> object has changed, we ignore it. Something like this: |
|
|
1242 | |
|
|
1243 | $guards->{file_loaded} = $mpv->register_event (file_loaded => sub { |
|
|
1244 | delete $guards->{file_loaded}; |
|
|
1245 | return if $guards != $self->{mpv_guards}; |
|
|
1246 | |
|
|
1247 | Commands do not have guards since they cnanot be cancelled, so we don't |
|
|
1248 | have to do this for commands. But what prevents us form misinterpreting |
|
|
1249 | an old event? Since F<mpv> (by default) handles commands synchronously, |
|
|
1250 | we can queue a dummy command, whose only purpose is to tell us when all |
|
|
1251 | previous commands are done. We use C<get_version> for this. |
|
|
1252 | |
|
|
1253 | The simplified code looks like this: |
|
|
1254 | |
|
|
1255 | Scalar::Util::weaken $self; |
|
|
1256 | |
|
|
1257 | $mpv->cmd ("get_version")->cb (sub { |
|
|
1258 | |
|
|
1259 | $guards->{file_loaded} = $mpv->register_event (file_loaded => sub { |
|
|
1260 | delete $guards->{file_loaded}; |
|
|
1261 | return if $guards != $self->{mpv_guards}; |
|
|
1262 | |
|
|
1263 | $mpv->cmd (get_property => "video-format")->cb (sub { |
|
|
1264 | return if $guards != $self->{mpv_guards}; |
|
|
1265 | |
|
|
1266 | # video-format handling |
|
|
1267 | return if eval { $_[0]->recv; 1 }; |
|
|
1268 | |
|
|
1269 | # no video? assume audio and visualize, cpu usage be damned |
|
|
1270 | $mpv->cmd (set => "lavfi-complex" => ..."); |
|
|
1271 | }); |
|
|
1272 | |
|
|
1273 | $guards->{show} = $mpv->register_event (video_reconfig => sub { |
|
|
1274 | delete $guards->{show}; |
|
|
1275 | return if $guards != $self->{mpv_guards}; |
|
|
1276 | |
|
|
1277 | $self->{mpv_eventbox}->show_all; |
|
|
1278 | |
|
|
1279 | $w = $mpv->cmd (get_property => "dwidth"); |
|
|
1280 | $h = $mpv->cmd (get_property => "dheight"); |
|
|
1281 | |
|
|
1282 | $h->cb (sub { |
|
|
1283 | $w = eval { $w->recv }; |
|
|
1284 | $h = eval { $h->recv }; |
|
|
1285 | |
|
|
1286 | $mpv->cmd (set_property => "pause" => "no"); |
|
|
1287 | |
|
|
1288 | if ($w && $h) { |
|
|
1289 | # resize our window |
|
|
1290 | } |
|
|
1291 | |
|
|
1292 | }); |
|
|
1293 | }); |
|
|
1294 | |
|
|
1295 | }); |
|
|
1296 | |
|
|
1297 | }); |
|
|
1298 | |
|
|
1299 | Most of the rest of the code is much simpler and just deals with forwarding user commands: |
|
|
1300 | |
|
|
1301 | } elsif ($key == $Gtk2::Gdk::Keysyms{Right}) { $mpv->cmd ("osd-msg-bar" => seek => "+10"); |
|
|
1302 | } elsif ($key == $Gtk2::Gdk::Keysyms{Left} ) { $mpv->cmd ("osd-msg-bar" => seek => "-10"); |
|
|
1303 | } elsif ($key == $Gtk2::Gdk::Keysyms{Up} ) { $mpv->cmd ("osd-msg-bar" => seek => "+60"); |
|
|
1304 | } elsif ($key == $Gtk2::Gdk::Keysyms{Down} ) { $mpv->cmd ("osd-msg-bar" => seek => "-60"); |
|
|
1305 | } elsif ($key == $Gtk2::Gdk::Keysyms{a}) ) { $mpv->cmd ("osd-msg-msg" => cycle => "audio"); |
|
|
1306 | } elsif ($key == $Gtk2::Gdk::Keysyms{j} ) { $mpv->cmd ("osd-msg-msg" => cycle => "sub"); |
|
|
1307 | } elsif ($key == $Gtk2::Gdk::Keysyms{o} ) { $mpv->cmd ("no-osd" => "cycle-values", "osd-level", "2", "3", "0", "2"); |
|
|
1308 | } elsif ($key == $Gtk2::Gdk::Keysyms{p} ) { $mpv->cmd ("no-osd" => cycle => "pause"); |
|
|
1309 | } elsif ($key == $Gtk2::Gdk::Keysyms{9} ) { $mpv->cmd ("osd-msg-bar" => add => "ao-volume", "-2"); |
|
|
1310 | } elsif ($key == $Gtk2::Gdk::Keysyms{0} ) { $mpv->cmd ("osd-msg-bar" => add => "ao-volume", "+2"); |
|
|
1311 | |
644 | =head1 SEE ALSO |
1312 | =head1 SEE ALSO |
645 | |
1313 | |
646 | L<AnyEvent>, L<the mpv command documentation|https://mpv.io/manual/stable/#command-interface>. |
1314 | L<AnyEvent>, L<the mpv command documentation|https://mpv.io/manual/stable/#command-interface>. |
647 | |
1315 | |
648 | =head1 AUTHOR |
1316 | =head1 AUTHOR |