use IO::Epoll to talk with git and c to invoke repl
[HTML5TV.git] / bin / mplayer.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use IPC::Open3 qw(open3);
7 use IO::Epoll;
8 use Data::Dump qw(dump);
9
10
11 my $movie = shift @ARGV
12         || 'media/lpc-2009-network-namespaces/Pavel Emelyanov.ogg';
13 #       || die "usage: $0 path/to/movie.ogv\n";
14
15 my $edl = "$$.edl";
16
17 our $to_mplayer;
18 our $from_mplayer;
19
20 my $pid = open3( $to_mplayer, $from_mplayer, $from_mplayer,
21          'mplayer',
22                 '-slave', '-idle',
23                 '-edlout', $edl
24 );
25
26 my $epfd = epoll_create(10);
27
28 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno STDIN         , EPOLLIN  ) >= 0 || die $!;
29 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $from_mplayer , EPOLLIN  ) >= 0 || die $!;
30 epoll_ctl($epfd, EPOLL_CTL_ADD, fileno $to_mplayer   , EPOLLOUT ) >= 0 || die $!;
31
32 warn "$movie ", -s $movie, " bytes $edl\n";
33 print $to_mplayer qq|loadfile "$movie"\n|;
34
35 $|=1;
36
37 my $line;
38 my $repl;
39
40 sub repl {
41         print "> ";
42         my $cmd = <STDIN>;
43         warn ">>> $cmd\n";
44         print $to_mplayer $cmd;
45 }
46
47
48 while ( my $events = epoll_wait($epfd, 10, 1000) ) { # Max 10 events returned, 1s timeout
49
50         warn "no events" unless $events;
51
52         foreach my $e ( @$events ) {
53 #               warn "# event: ", dump($e), $/;
54
55                 my $fileno = $e->[0];
56
57                 if ( $fileno == fileno STDIN ) {
58                         my $chr;
59                         read STDIN, $chr, 1;
60                         print "$chr";
61                 } elsif ( $fileno == fileno $from_mplayer ) {
62                         my $chr;
63                         read $from_mplayer, $chr, 1;
64                         print $chr;
65
66                         if ( $chr =~ m{[\n\r]} ) {
67
68                                 exit if $line =~ m{Exiting};
69
70                                 if ( $line =~ m{No bind found for key '(.)'} ) {
71                                         warn "CUSTOM $1\n";
72                                         repl if $1 eq 'c';
73                                 } elsif ( $line =~ m{EDL}i ) {
74                                         print $to_mplayer qq|osd_show_text "$line"\n|;
75                                 }
76
77                                 $line = '';
78                         } else {
79                                 $line .= $chr;
80                         }
81
82
83                 } elsif ( $fileno == fileno $to_mplayer ) {
84 #                       warn "command";
85                 } else {
86                         die "invalid fileno $fileno";
87                 }
88         }
89
90 }
91