… | |
… | |
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 ("--idle=yes"); |
|
|
13 | $mpv->cmd (loadfile => $mpv->escape_binary ($videofile)); |
|
|
14 | my $quit = AE::cv; |
|
|
15 | $mpv->register_event (end_file => $quit); |
|
|
16 | $quit->recv; |
|
|
17 | |
8 | |
18 | |
9 | =head1 DESCRIPTION |
19 | =head1 DESCRIPTION |
10 | |
20 | |
11 | This module allows you to remote control F<mpv> (a video player). It also |
21 | 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 |
22 | is an L<AnyEvent> user, you need to make sure that you use and run a |
… | |
… | |
50 | Here is a very simple client: |
60 | Here is a very simple client: |
51 | |
61 | |
52 | use AnyEvent; |
62 | use AnyEvent; |
53 | use AnyEvent::MPV; |
63 | use AnyEvent::MPV; |
54 | |
64 | |
55 | my $videofile = "./xyzzy.mp4"; |
65 | my $videofile = "./xyzzy.mkv"; |
56 | |
66 | |
57 | my $mpv = AnyEvent::MPV->new (trace => 1); |
67 | my $mpv = AnyEvent::MPV->new (trace => 1); |
58 | |
68 | |
59 | $mpv->start ("--", $videofile); |
69 | $mpv->start ("--", $videofile); |
60 | |
70 | |
… | |
… | |
85 | shell command), so let us load the file at runtime: |
95 | shell command), so let us load the file at runtime: |
86 | |
96 | |
87 | use AnyEvent; |
97 | use AnyEvent; |
88 | use AnyEvent::MPV; |
98 | use AnyEvent::MPV; |
89 | |
99 | |
90 | my $videofile = "./xyzzy.mp4"; |
100 | my $videofile = "./xyzzy.mkv"; |
91 | |
101 | |
92 | my $mpv = AnyEvent::MPV->new ( |
102 | my $mpv = AnyEvent::MPV->new ( |
93 | trace => 1, |
103 | trace => 1, |
94 | args => ["--pause", "--idle=yes"], |
104 | args => ["--pause", "--idle=yes"], |
95 | ); |
105 | ); |
… | |
… | |
140 | receiving events (using a somewhat embellished example): |
150 | receiving events (using a somewhat embellished example): |
141 | |
151 | |
142 | use AnyEvent; |
152 | use AnyEvent; |
143 | use AnyEvent::MPV; |
153 | use AnyEvent::MPV; |
144 | |
154 | |
145 | my $videofile = "xyzzy.mp4"; |
155 | my $videofile = "xyzzy.mkv"; |
146 | |
156 | |
147 | my $quit = AE::cv; |
157 | my $quit = AE::cv; |
148 | |
158 | |
149 | my $mpv = AnyEvent::MPV->new ( |
159 | my $mpv = AnyEvent::MPV->new ( |
150 | trace => 1, |
160 | trace => 1, |
… | |
… | |
187 | |
197 | |
188 | As a rule of thumb, all data you pass to this module to be sent to F<mpv> |
198 | As a rule of thumb, all data you pass to this module to be sent to F<mpv> |
189 | is expected to be in unicode. To pass something that isn't, you need to |
199 | is expected to be in unicode. To pass something that isn't, you need to |
190 | escape it using C<escape_binary>. |
200 | escape it using C<escape_binary>. |
191 | |
201 | |
192 | Data received from C<$mpv>, however, is I<not> decoded to unicode, as data |
202 | Data received from F<mpv>, however, is I<not> decoded to unicode, as data |
193 | returned by F<mpv> is not generally encoded in unicode, and the encoding |
203 | returned by F<mpv> is not generally encoded in unicode, and the encoding |
194 | is usually unspecified. So if you receive data and expect it to be in |
204 | is usually unspecified. So if you receive data and expect it to be in |
195 | unicode, you need to first decode it from UTF-8, but note that this might |
205 | unicode, you need to first decode it from UTF-8, but note that this might |
196 | fail. This is not a limitation of this module - F<mpv> simply does not |
206 | fail. This is not a limitation of this module - F<mpv> simply does not |
197 | specify nor guarantee a specific encoding, or any encoding at all, in its |
207 | specify nor guarantee a specific encoding, or any encoding at all, in its |
… | |
… | |
211 | use Scalar::Util (); |
221 | use Scalar::Util (); |
212 | |
222 | |
213 | use AnyEvent (); |
223 | use AnyEvent (); |
214 | use AnyEvent::Util (); |
224 | use AnyEvent::Util (); |
215 | |
225 | |
216 | our $VERSION = '0.2'; |
226 | our $VERSION = '1.01'; |
217 | |
227 | |
218 | sub OBSID() { 0x10000000000000 } # 2**52 |
228 | sub OBSID() { 0x10000000000000 } # 2**52 |
219 | |
229 | |
220 | our $JSON = eval { require JSON::XS; JSON::XS:: } |
230 | our $JSON = eval { require JSON::XS; JSON::XS:: } |
221 | || do { require JSON::PP; JSON::PP:: }; |
231 | || do { require JSON::PP; JSON::PP:: }; |
… | |
… | |
386 | exit 1; |
396 | exit 1; |
387 | } |
397 | } |
388 | |
398 | |
389 | $self->{fh} = $fh; |
399 | $self->{fh} = $fh; |
390 | |
400 | |
391 | my $trace = delete $self->{trace} || sub { }; |
401 | my $trace = $self->{trace} || sub { }; |
392 | |
402 | |
393 | $trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace; |
403 | $trace = sub { warn "$_[0] $_[1]\n" } if $trace && !ref $trace; |
394 | |
404 | |
395 | my $buf; |
405 | my $buf; |
396 | |
406 | |
… | |
… | |
557 | For subclassing, see I<SUBCLASSING>, below. |
567 | For subclassing, see I<SUBCLASSING>, below. |
558 | |
568 | |
559 | =cut |
569 | =cut |
560 | |
570 | |
561 | sub on_event { |
571 | sub on_event { |
562 | my ($self, $key) = @_; |
572 | my ($self, $event, $data) = @_; |
563 | |
573 | |
564 | $self->{on_event}($self, $key) if $self->{on_event}; |
574 | $self->{on_event}($self, $event, $data) if $self->{on_event}; |
565 | } |
575 | } |
566 | |
576 | |
567 | =item $mpv->on_key ($string) |
577 | =item $mpv->on_key ($string) |
568 | |
578 | |
569 | Invoked when a key declared by C<< ->bind_key >> is pressed. The default |
579 | Invoked when a key declared by C<< ->bind_key >> is pressed. The default |
… | |
… | |
822 | care and deal with the breakage. |
832 | care and deal with the breakage. |
823 | |
833 | |
824 | If you don't want to go to the effort of subclassing this module, you can |
834 | If you don't want to go to the effort of subclassing this module, you can |
825 | also specify all event handlers as constructor keys. |
835 | also specify all event handlers as constructor keys. |
826 | |
836 | |
|
|
837 | =head1 EXAMPLES |
|
|
838 | |
|
|
839 | Here are some real-world code snippets, thrown in here mainly to give you |
|
|
840 | some example code to copy. |
|
|
841 | |
|
|
842 | =head2 doomfrontend |
|
|
843 | |
|
|
844 | At one point I replaced mythtv-frontend by my own terminal-based video |
|
|
845 | player (based on rxvt-unicode). I toyed with the diea of using F<mpv>'s |
|
|
846 | subtitle engine to create the user interface, but that is hard to use |
|
|
847 | since you don't know how big your letters are. It is also where most of |
|
|
848 | this modules code has originally been developed in. |
|
|
849 | |
|
|
850 | It uses a unified input queue to handle various remote controls, so its |
|
|
851 | event handling needs are very simple - it simply feeds all events into the |
|
|
852 | input queue: |
|
|
853 | |
|
|
854 | my $mpv = AnyEvent::MPV->new ( |
|
|
855 | mpv => $MPV, |
|
|
856 | args => \@MPV_ARGS, |
|
|
857 | on_event => sub { |
|
|
858 | input_feed "mpv/$_[1]", $_[2]; |
|
|
859 | }, |
|
|
860 | on_key => sub { |
|
|
861 | input_feed $_[1]; |
|
|
862 | }, |
|
|
863 | on_eof => sub { |
|
|
864 | input_feed "mpv/quit"; |
|
|
865 | }, |
|
|
866 | ); |
|
|
867 | |
|
|
868 | ... |
|
|
869 | |
|
|
870 | $mpv->start ("--idle=yes", "--pause", "--force-window=no"); |
|
|
871 | |
|
|
872 | It also doesn't use complicated command line arguments - the file search |
|
|
873 | options have the most impact, as they prevent F<mpv> from scanning |
|
|
874 | directories with tens of thousands of files for subtitles and more: |
|
|
875 | |
|
|
876 | --audio-client-name=doomfrontend |
|
|
877 | --osd-on-seek=msg-bar --osd-bar-align-y=-0.85 --osd-bar-w=95 |
|
|
878 | --sub-auto=exact --audio-file-auto=exact |
|
|
879 | |
|
|
880 | Since it runs on a TV without a desktop environemnt, it tries to keep complications such as dbus |
|
|
881 | away and the screensaver happy: |
|
|
882 | |
|
|
883 | # prevent xscreensaver from doing something stupid, such as starting dbus |
|
|
884 | $ENV{DBUS_SESSION_BUS_ADDRESS} = "/"; # prevent dbus autostart for sure |
|
|
885 | $ENV{XDG_CURRENT_DESKTOP} = "generic"; |
|
|
886 | |
|
|
887 | It does bind a number of keys to internal (to doomfrontend) commands: |
|
|
888 | |
|
|
889 | for ( |
|
|
890 | List::Util::pairs qw( |
|
|
891 | ESC return |
|
|
892 | q return |
|
|
893 | ENTER enter |
|
|
894 | SPACE pause |
|
|
895 | [ steprev |
|
|
896 | ] stepfwd |
|
|
897 | j subtitle |
|
|
898 | BS red |
|
|
899 | i green |
|
|
900 | o yellow |
|
|
901 | b blue |
|
|
902 | D triangle |
|
|
903 | UP up |
|
|
904 | DOWN down |
|
|
905 | RIGHT right |
|
|
906 | LEFT left |
|
|
907 | ), |
|
|
908 | (map { ("KP$_" => "num$_") } 0..9), |
|
|
909 | KP_INS => 0, # KP0, but different |
|
|
910 | ) { |
|
|
911 | $mpv->bind_key ($_->[0] => $_->[1]); |
|
|
912 | } |
|
|
913 | |
|
|
914 | It also reacts to sponsorblock chapters, so it needs to know when vidoe |
|
|
915 | chapters change. Preadting C<AnyEvent::MPV>, it handles observers |
|
|
916 | manually: |
|
|
917 | |
|
|
918 | $mpv->cmd (observe_property => 1, "chapter-metadata"); |
|
|
919 | |
|
|
920 | It also tries to apply an F<mpv> profile, if it exists: |
|
|
921 | |
|
|
922 | eval { |
|
|
923 | # the profile is optional |
|
|
924 | $mpv->cmd ("apply-profile" => "doomfrontend"); |
|
|
925 | }; |
|
|
926 | |
|
|
927 | Most of the complicated parts deal with saving and restoring per-video |
|
|
928 | data, such as bookmarks, playing position, selected audio and subtitle |
|
|
929 | tracks and so on. However, since it uses L<Coro>, it can conveniently |
|
|
930 | block and wait for replies, which is n ot possible in purely event based |
|
|
931 | programs, as you are not allowed to block inside event callbacks in most |
|
|
932 | event loops. This simplifies the code quite a bit. |
|
|
933 | |
|
|
934 | When the file to be played is a Tv recording done by mythtv, it uses the |
|
|
935 | C<appending> protocol and deinterlacing: |
|
|
936 | |
|
|
937 | if (is_myth $mpv_path) { |
|
|
938 | $mpv_path = "appending://$mpv_path"; |
|
|
939 | $initial_deinterlace = 1; |
|
|
940 | } |
|
|
941 | |
|
|
942 | Otherwise, it sets some defaults and loads the file (I forgot what the |
|
|
943 | C<dummy> argument is for, but I am sure it is needed by some F<mpv> |
|
|
944 | version): |
|
|
945 | |
|
|
946 | $mpv->cmd ("script-message", "osc-visibility", "never", "dummy"); |
|
|
947 | $mpv->cmd ("set", "vid", "auto"); |
|
|
948 | $mpv->cmd ("set", "aid", "auto"); |
|
|
949 | $mpv->cmd ("set", "sid", "no"); |
|
|
950 | $mpv->cmd ("set", "file-local-options/chapters-file", $mpv->escape_binary ("$mpv_path.chapters")); |
|
|
951 | $mpv->cmd ("loadfile", $mpv->escape_binary ($mpv_path)); |
|
|
952 | $mpv->cmd ("script-message", "osc-visibility", "auto", "dummy"); |
|
|
953 | |
|
|
954 | Handling events makes the main bulk of video playback code. For example, |
|
|
955 | various ways of ending playback: |
|
|
956 | |
|
|
957 | if ($INPUT eq "mpv/quit") { # should not happen, but allows user to kill etc. without consequence |
|
|
958 | $status = 1; |
|
|
959 | mpv_init; # try reinit |
|
|
960 | last; |
|
|
961 | |
|
|
962 | } elsif ($INPUT eq "mpv/idle") { # normal end-of-file |
|
|
963 | last; |
|
|
964 | |
|
|
965 | } elsif ($INPUT eq "return") { |
|
|
966 | $status = 1; |
|
|
967 | last; |
|
|
968 | |
|
|
969 | Or the code that actually starts playback, once the file is loaded: |
|
|
970 | |
|
|
971 | our %SAVE_PROPERTY = (aid => 1, sid => 1, "audio-delay" => 1); |
|
|
972 | |
|
|
973 | ... |
|
|
974 | |
|
|
975 | my $oid = 100; |
|
|
976 | |
|
|
977 | } elsif ($INPUT eq "mpv/file-loaded") { # start playing, configure video |
|
|
978 | $mpv->cmd ("seek", $playback_start, "absolute+exact") if $playback_start > 0; |
|
|
979 | |
|
|
980 | my $target_fps = eval { $mpv->cmd_recv ("get_property", "container-fps") } || 60; |
|
|
981 | $target_fps *= play_video_speed_mult; |
|
|
982 | set_fps $target_fps; |
|
|
983 | |
|
|
984 | unless (eval { $mpv->cmd_recv ("get_property", "video-format") }) { |
|
|
985 | $mpv->cmd ("set", "file-local-options/lavfi-complex", "[aid1] asplit [ao], showcqt=..., format=yuv420p [vo]"); |
|
|
986 | }; |
|
|
987 | |
|
|
988 | for my $prop (keys %SAVE_PROPERTY) { |
|
|
989 | if (exists $PLAYING_STATE->{"mpv_$prop"}) { |
|
|
990 | $mpv->cmd ("set", "$prop", $PLAYING_STATE->{"mpv_$prop"} . ""); |
|
|
991 | } |
|
|
992 | |
|
|
993 | $mpv->cmd ("observe_property", ++$oid, $prop); |
|
|
994 | } |
|
|
995 | |
|
|
996 | play_video_set_speed; |
|
|
997 | $mpv->cmd ("set", "osd-level", "$OSD_LEVEL"); |
|
|
998 | $mpv->cmd ("observe_property", ++$oid, "osd-level"); |
|
|
999 | $mpv->cmd ("set", "pause", "no"); |
|
|
1000 | |
|
|
1001 | $mpv->cmd ("set_property", "deinterlace", "yes") |
|
|
1002 | if $initial_deinterlace; |
|
|
1003 | |
|
|
1004 | There is a lot going on here. First it seeks to the actual playback |
|
|
1005 | position, if it is not at the start of the file (it would probaby be more |
|
|
1006 | efficient to set the starting position before loading the file, though, |
|
|
1007 | but this is good enough). |
|
|
1008 | |
|
|
1009 | Then it plays with the display fps, to set it to something harmonious |
|
|
1010 | w.r.t. the video framerate. |
|
|
1011 | |
|
|
1012 | If the file does not have a video part, it assumes it is an audio file and |
|
|
1013 | sets a visualizer. |
|
|
1014 | |
|
|
1015 | Also, a number of properties are not global, but per-file. At the moment, |
|
|
1016 | this is C<audio-delay>, and the current audio/subtitle track, which it |
|
|
1017 | sets, and also creates an observer. Again, this doesn'T use the observe |
|
|
1018 | functionality of this module, but handles it itself, assigning obsevrer |
|
|
1019 | ids 100+ to temporary/per-file observers. |
|
|
1020 | |
|
|
1021 | Lastly, it sets some global (or per-youtube-uploader) parameters, such as |
|
|
1022 | speed, and unpauses. Property changes are handled like other input events: |
|
|
1023 | |
|
|
1024 | } elsif ($INPUT eq "mpv/property-change") { |
|
|
1025 | my $prop = $INPUT_DATA->{name}; |
|
|
1026 | |
|
|
1027 | if ($prop eq "chapter-metadata") { |
|
|
1028 | if ($INPUT_DATA->{data}{TITLE} =~ /^\[SponsorBlock\]: (.*)/) { |
|
|
1029 | my $section = $1; |
|
|
1030 | my $skip; |
|
|
1031 | |
|
|
1032 | $skip ||= $SPONSOR_SKIP{$_} |
|
|
1033 | for split /\s*,\s*/, $section; |
|
|
1034 | |
|
|
1035 | if (defined $skip) { |
|
|
1036 | if ($skip) { |
|
|
1037 | # delay a bit, in case we get two metadata changes in quick succession, e.g. |
|
|
1038 | # because we have a skip at file load time. |
|
|
1039 | $skip_delay = AE::timer 2/50, 0, sub { |
|
|
1040 | $mpv->cmd ("no-osd", "add", "chapter", 1); |
|
|
1041 | $mpv->cmd ("show-text", "skipped sponsorblock section \"$section\"", 3000); |
|
|
1042 | }; |
|
|
1043 | } else { |
|
|
1044 | undef $skip_delay; |
|
|
1045 | $mpv->cmd ("show-text", "NOT skipping sponsorblock section \"$section\"", 3000); |
|
|
1046 | } |
|
|
1047 | } else { |
|
|
1048 | $mpv->cmd ("show-text", "UNRECOGNIZED sponsorblock section \"$section\"", 60000); |
|
|
1049 | } |
|
|
1050 | } else { |
|
|
1051 | # cancel a queued skip |
|
|
1052 | undef $skip_delay; |
|
|
1053 | } |
|
|
1054 | |
|
|
1055 | } elsif (exists $SAVE_PROPERTY{$prop}) { |
|
|
1056 | $PLAYING_STATE->{"mpv_$prop"} = $INPUT_DATA->{data}; |
|
|
1057 | ::state_save; |
|
|
1058 | } |
|
|
1059 | |
|
|
1060 | This saves back the per-file properties, and also handles chapter changes |
|
|
1061 | in a hacky way. |
|
|
1062 | |
|
|
1063 | Most of the handlers are very simple, though. For example: |
|
|
1064 | |
|
|
1065 | } elsif ($INPUT eq "pause") { |
|
|
1066 | $mpv->cmd ("cycle", "pause"); |
|
|
1067 | $PLAYING_STATE->{curpos} = $mpv->cmd_recv ("get_property", "playback-time"); |
|
|
1068 | } elsif ($INPUT eq "right") { |
|
|
1069 | $mpv->cmd ("osd-msg-bar", "seek", 30, "relative+exact"); |
|
|
1070 | } elsif ($INPUT eq "left") { |
|
|
1071 | $mpv->cmd ("osd-msg-bar", "seek", -5, "relative+exact"); |
|
|
1072 | } elsif ($INPUT eq "up") { |
|
|
1073 | $mpv->cmd ("osd-msg-bar", "seek", +600, "relative+exact"); |
|
|
1074 | } elsif ($INPUT eq "down") { |
|
|
1075 | $mpv->cmd ("osd-msg-bar", "seek", -600, "relative+exact"); |
|
|
1076 | } elsif ($INPUT eq "select") { |
|
|
1077 | $mpv->cmd ("osd-msg-bar", "add", "audio-delay", "-0.100"); |
|
|
1078 | } elsif ($INPUT eq "start") { |
|
|
1079 | $mpv->cmd ("osd-msg-bar", "add", "audio-delay", "0.100"); |
|
|
1080 | } elsif ($INPUT eq "intfwd") { |
|
|
1081 | $mpv->cmd ("no-osd", "frame-step"); |
|
|
1082 | } elsif ($INPUT eq "audio") { |
|
|
1083 | $mpv->cmd ("osd-auto", "cycle", "audio"); |
|
|
1084 | } elsif ($INPUT eq "subtitle") { |
|
|
1085 | $mpv->cmd ("osd-auto", "cycle", "sub"); |
|
|
1086 | } elsif ($INPUT eq "triangle") { |
|
|
1087 | $mpv->cmd ("osd-auto", "cycle", "deinterlace"); |
|
|
1088 | |
|
|
1089 | Once a file has finished playing (or the user strops playback), it pauses, |
|
|
1090 | unobserves the per-file observers, and saves the current position for to |
|
|
1091 | be able to resume: |
|
|
1092 | |
|
|
1093 | $mpv->cmd ("set", "pause", "yes"); |
|
|
1094 | |
|
|
1095 | while ($oid > 100) { |
|
|
1096 | $mpv->cmd ("unobserve_property", $oid--); |
|
|
1097 | } |
|
|
1098 | |
|
|
1099 | $PLAYING_STATE->{curpos} = $mpv->cmd_recv ("get_property", "playback-time"); |
|
|
1100 | |
|
|
1101 | And thats most of the F<mpv>-related code. |
|
|
1102 | |
|
|
1103 | =head2 F<Gtk2::CV> |
|
|
1104 | |
|
|
1105 | F<Gtk2::CV> is low-feature image viewer that I use many times daily |
|
|
1106 | because it can handle directories with millions of files without falling |
|
|
1107 | over. It also had the ability to play videos for ages, but it used an |
|
|
1108 | older, crappier protocol to talk to F<mpv> and used F<ffprobe> before |
|
|
1109 | playing each file instead of letting F<mpv> handle format/size detection. |
|
|
1110 | |
|
|
1111 | After writing this module, I decided to upgprade Gtk2::CV by making use |
|
|
1112 | of it, with the goal of getting rid of F<ffprobe> and being ablew to |
|
|
1113 | reuse F<mpv> processes, which would have a multitude of speed benefits |
|
|
1114 | (for example, fork+exec of F<mpv> caused the kernel to close all file |
|
|
1115 | descriptors, which could take minutes if a large file was being copied via |
|
|
1116 | NFS, as the kernel waited for thr buffers to be flushed on close - not |
|
|
1117 | having to start F<mpv> gets rid of this issue). |
|
|
1118 | |
|
|
1119 | Setting up is only complicated by the fact that F<mpv> needs to be |
|
|
1120 | embedded into an existing window. To keep control of all inputs, |
|
|
1121 | F<Gtk2::CV> puts an eventbox in front of F<mpv>, so F<mpv> receives no |
|
|
1122 | input events: |
|
|
1123 | |
|
|
1124 | $self->{mpv} = AnyEvent::MPV->new ( |
|
|
1125 | trace => $ENV{CV_MPV_TRACE}, |
|
|
1126 | ); |
|
|
1127 | |
|
|
1128 | # create an eventbox, so we receive all input events |
|
|
1129 | my $box = $self->{mpv_eventbox} = new Gtk2::EventBox; |
|
|
1130 | $box->set_above_child (1); |
|
|
1131 | $box->set_visible_window (0); |
|
|
1132 | $box->set_events ([]); |
|
|
1133 | $box->can_focus (0); |
|
|
1134 | |
|
|
1135 | # create a drawingarea that mpv can display into |
|
|
1136 | my $window = $self->{mpv_window} = new Gtk2::DrawingArea; |
|
|
1137 | $box->add ($window); |
|
|
1138 | |
|
|
1139 | # put the drawingarea intot he eventbox, and the eventbox into our display window |
|
|
1140 | $self->add ($box); |
|
|
1141 | |
|
|
1142 | # we need to pass the window id to F<mpv>, which means we need to realise |
|
|
1143 | # the drawingarea, so an X window is allocated for it. |
|
|
1144 | $self->show_all; |
|
|
1145 | $window->realize; |
|
|
1146 | my $xid = $window->window->get_xid; |
|
|
1147 | |
|
|
1148 | Then it starts F<mpv> using this setup: |
|
|
1149 | |
|
|
1150 | local $ENV{LC_ALL} = "POSIX"; |
|
|
1151 | $self->{mpv}->start ( |
|
|
1152 | "--no-terminal", |
|
|
1153 | "--no-input-terminal", |
|
|
1154 | "--no-input-default-bindings", |
|
|
1155 | "--no-input-cursor", |
|
|
1156 | "--input-conf=/dev/null", |
|
|
1157 | "--input-vo-keyboard=no", |
|
|
1158 | |
|
|
1159 | "--loop-file=inf", |
|
|
1160 | "--force-window=yes", |
|
|
1161 | "--idle=yes", |
|
|
1162 | |
|
|
1163 | "--audio-client-name=CV", |
|
|
1164 | |
|
|
1165 | "--osc=yes", # --osc=no displays fading play/pause buttons instead |
|
|
1166 | |
|
|
1167 | "--wid=$xid", |
|
|
1168 | ); |
|
|
1169 | |
|
|
1170 | $self->{mpv}->cmd ("script-message" => "osc-visibility" => "never", "dummy"); |
|
|
1171 | $self->{mpv}->cmd ("osc-idlescreen" => "no"); |
|
|
1172 | |
|
|
1173 | It also prepares a hack to force a ConfigureNotify event on every vidoe |
|
|
1174 | reconfig: |
|
|
1175 | |
|
|
1176 | # force a configurenotify on every video-reconfig |
|
|
1177 | $self->{mpv_reconfig} = $self->{mpv}->register_event (video_reconfig => sub { |
|
|
1178 | my ($mpv, $event, $data) = @_; |
|
|
1179 | |
|
|
1180 | $self->mpv_window_update; |
|
|
1181 | }); |
|
|
1182 | |
|
|
1183 | The way this is done is by doing a "dummy" resize to 1x1 and back: |
|
|
1184 | |
|
|
1185 | $self->{mpv_window}->window->resize (1, 1), |
|
|
1186 | $self->{mpv_window}->window->resize ($self->{w}, $self->{h}); |
|
|
1187 | |
|
|
1188 | Without this, F<mpv> often doesn't "get" the correct window size. Doing |
|
|
1189 | it this way is not nice, but I didn't fine a nicer way to do it. |
|
|
1190 | |
|
|
1191 | When no file is being played, F<mpv> is hidden and prepared: |
|
|
1192 | |
|
|
1193 | $self->{mpv_eventbox}->hide; |
|
|
1194 | |
|
|
1195 | $self->{mpv}->cmd (set_property => "pause" => "yes"); |
|
|
1196 | $self->{mpv}->cmd ("playlist_remove", "current"); |
|
|
1197 | $self->{mpv}->cmd (set_property => "video-rotate" => 0); |
|
|
1198 | $self->{mpv}->cmd (set_property => "lavfi-complex" => ""); |
|
|
1199 | |
|
|
1200 | Loading a file is a bit more complicated, as bluray and DVD rips are |
|
|
1201 | supported: |
|
|
1202 | |
|
|
1203 | if ($moviedir) { |
|
|
1204 | if ($moviedir eq "br") { |
|
|
1205 | $mpv->cmd (set => "bluray-device" => $path); |
|
|
1206 | $mpv->cmd (loadfile => "bd://"); |
|
|
1207 | } elsif ($moviedir eq "dvd") { |
|
|
1208 | $mpv->cmd (set => "dvd-device" => $path); |
|
|
1209 | $mpv->cmd (loadfile => "dvd://"); |
|
|
1210 | } |
|
|
1211 | } elsif ($type eq "video/iso-bluray") { |
|
|
1212 | $mpv->cmd (set => "bluray-device" => $path); |
|
|
1213 | $mpv->cmd (loadfile => "bd://"); |
|
|
1214 | } else { |
|
|
1215 | $mpv->cmd (loadfile => $mpv->escape_binary ($path)); |
|
|
1216 | } |
|
|
1217 | |
|
|
1218 | After this, C<Gtk2::CV> waits for the file to be loaded, video to be |
|
|
1219 | configured, and then queries the video size (to resize its own window) |
|
|
1220 | and video format (to decide whether an audio visualizer is needed for |
|
|
1221 | audio playback). The problematic word here is "wait", as this needs to be |
|
|
1222 | imploemented using callbacks. |
|
|
1223 | |
|
|
1224 | This made the code much harder to write, as the whole setup is very |
|
|
1225 | asynchronous (C<Gtk2::CV> talks to the command interface in F<mpv>, which |
|
|
1226 | talks to the decode and playback parts, all of which run asynchronously |
|
|
1227 | w.r.t. each other. In practise, this can mean that C<Gtk2::CV> waits for |
|
|
1228 | a file to be loaded by F<mpv> while the command interface of F<mpv> still |
|
|
1229 | deals with the previous file and the decoder still handles an even older |
|
|
1230 | file). Adding to this fact is that Gtk2::CV is bound by the glib event |
|
|
1231 | loop, which means we cannot wait for replies form F<mpv> anywhere, so |
|
|
1232 | everything has to be chained callbacks. |
|
|
1233 | |
|
|
1234 | The way this is handled is by creating a new empty hash ref that is unique |
|
|
1235 | for each loaded file, and use it to detect whether the event is old or |
|
|
1236 | not, and also store C<AnyEvent::MPV> guard objects in it: |
|
|
1237 | |
|
|
1238 | # every time we loaded a file, we create a new hash |
|
|
1239 | my $guards = $self->{mpv_guards} = { }; |
|
|
1240 | |
|
|
1241 | Then, when we wait for an event to occur, delete the handler, and, if the |
|
|
1242 | C<mpv_guards> object has changed, we ignore it. Something like this: |
|
|
1243 | |
|
|
1244 | $guards->{file_loaded} = $mpv->register_event (file_loaded => sub { |
|
|
1245 | delete $guards->{file_loaded}; |
|
|
1246 | return if $guards != $self->{mpv_guards}; |
|
|
1247 | |
|
|
1248 | Commands do not have guards since they cnanot be cancelled, so we don't |
|
|
1249 | have to do this for commands. But what prevents us form misinterpreting |
|
|
1250 | an old event? Since F<mpv> (by default) handles commands synchronously, |
|
|
1251 | we can queue a dummy command, whose only purpose is to tell us when all |
|
|
1252 | previous commands are done. We use C<get_version> for this. |
|
|
1253 | |
|
|
1254 | The simplified code looks like this: |
|
|
1255 | |
|
|
1256 | Scalar::Util::weaken $self; |
|
|
1257 | |
|
|
1258 | $mpv->cmd ("get_version")->cb (sub { |
|
|
1259 | |
|
|
1260 | $guards->{file_loaded} = $mpv->register_event (file_loaded => sub { |
|
|
1261 | delete $guards->{file_loaded}; |
|
|
1262 | return if $guards != $self->{mpv_guards}; |
|
|
1263 | |
|
|
1264 | $mpv->cmd (get_property => "video-format")->cb (sub { |
|
|
1265 | return if $guards != $self->{mpv_guards}; |
|
|
1266 | |
|
|
1267 | # video-format handling |
|
|
1268 | return if eval { $_[0]->recv; 1 }; |
|
|
1269 | |
|
|
1270 | # no video? assume audio and visualize, cpu usage be damned |
|
|
1271 | $mpv->cmd (set => "lavfi-complex" => ..."); |
|
|
1272 | }); |
|
|
1273 | |
|
|
1274 | $guards->{show} = $mpv->register_event (video_reconfig => sub { |
|
|
1275 | delete $guards->{show}; |
|
|
1276 | return if $guards != $self->{mpv_guards}; |
|
|
1277 | |
|
|
1278 | $self->{mpv_eventbox}->show_all; |
|
|
1279 | |
|
|
1280 | $w = $mpv->cmd (get_property => "dwidth"); |
|
|
1281 | $h = $mpv->cmd (get_property => "dheight"); |
|
|
1282 | |
|
|
1283 | $h->cb (sub { |
|
|
1284 | $w = eval { $w->recv }; |
|
|
1285 | $h = eval { $h->recv }; |
|
|
1286 | |
|
|
1287 | $mpv->cmd (set_property => "pause" => "no"); |
|
|
1288 | |
|
|
1289 | if ($w && $h) { |
|
|
1290 | # resize our window |
|
|
1291 | } |
|
|
1292 | |
|
|
1293 | }); |
|
|
1294 | }); |
|
|
1295 | |
|
|
1296 | }); |
|
|
1297 | |
|
|
1298 | }); |
|
|
1299 | |
|
|
1300 | Most of the rest of the code is much simpler and just deals with forwarding user commands: |
|
|
1301 | |
|
|
1302 | } elsif ($key == $Gtk2::Gdk::Keysyms{Right}) { $mpv->cmd ("osd-msg-bar" => seek => "+10"); |
|
|
1303 | } elsif ($key == $Gtk2::Gdk::Keysyms{Left} ) { $mpv->cmd ("osd-msg-bar" => seek => "-10"); |
|
|
1304 | } elsif ($key == $Gtk2::Gdk::Keysyms{Up} ) { $mpv->cmd ("osd-msg-bar" => seek => "+60"); |
|
|
1305 | } elsif ($key == $Gtk2::Gdk::Keysyms{Down} ) { $mpv->cmd ("osd-msg-bar" => seek => "-60"); |
|
|
1306 | } elsif ($key == $Gtk2::Gdk::Keysyms{a}) ) { $mpv->cmd ("osd-msg-msg" => cycle => "audio"); |
|
|
1307 | } elsif ($key == $Gtk2::Gdk::Keysyms{j} ) { $mpv->cmd ("osd-msg-msg" => cycle => "sub"); |
|
|
1308 | } elsif ($key == $Gtk2::Gdk::Keysyms{o} ) { $mpv->cmd ("no-osd" => "cycle-values", "osd-level", "2", "3", "0", "2"); |
|
|
1309 | } elsif ($key == $Gtk2::Gdk::Keysyms{p} ) { $mpv->cmd ("no-osd" => cycle => "pause"); |
|
|
1310 | } elsif ($key == $Gtk2::Gdk::Keysyms{9} ) { $mpv->cmd ("osd-msg-bar" => add => "ao-volume", "-2"); |
|
|
1311 | } elsif ($key == $Gtk2::Gdk::Keysyms{0} ) { $mpv->cmd ("osd-msg-bar" => add => "ao-volume", "+2"); |
|
|
1312 | |
827 | =head1 SEE ALSO |
1313 | =head1 SEE ALSO |
828 | |
1314 | |
829 | L<AnyEvent>, L<the mpv command documentation|https://mpv.io/manual/stable/#command-interface>. |
1315 | L<AnyEvent>, L<the mpv command documentation|https://mpv.io/manual/stable/#command-interface>. |
830 | |
1316 | |
831 | =head1 AUTHOR |
1317 | =head1 AUTHOR |