| 1 | package Mojolicious::Plugin::PNGCast; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use 5.014; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Filter::signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use feature 'signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use Future::Mojo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.22'; | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | Mojolicious::Plugin::PNGCast - in-process server to display a screencast | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | Use this web application to display the screencast of a (headless) web browser | ||||||||||||||||||||
| 20 | or other arbitrary PNG data sent to it via websocket. | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | The synopsis shows how to use this plugin to display | ||||||||||||||||||||
| 23 | a Chrome screencast using L<WWW::Mechanize::Chrome>. | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | use Mojolicious::Lite; | ||||||||||||||||||||
| 28 | use Mojo::Server::Daemon; | ||||||||||||||||||||
| 29 | use WWW::Mechanize::Chrome; | ||||||||||||||||||||
| 30 | plugin 'PNGCast'; | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | my $daemon_url = 'http://localhost:3000'; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | my $ws_monitor = Mojo::Server::Daemon->new(app => app()); | ||||||||||||||||||||
| 35 | $ws_monitor->listen([$daemon_url]); | ||||||||||||||||||||
| 36 | $ws_monitor->start; | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | my $mech = WWW::Mechanize::Chrome->new( headless => 1 ); | ||||||||||||||||||||
| 39 | $mech->setScreenFrameCallback( sub { | ||||||||||||||||||||
| 40 | app->send_frame( $_[1]->{data} )} | ||||||||||||||||||||
| 41 | ); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | print "Watch progress at $daemon_url\n"; | ||||||||||||||||||||
| 44 | sleep 5; | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | $mech->get('https://example.com'); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 0 | ▶ | has 'clients' => sub { {} }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 51 | 1 | has 'last_frame' => undef; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | =head1 HELPERS | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =head2 C<< app->send_frame >> | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | app->send_frame( $png_data ); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | Sends a frame to all connected clients. If a fresh client connects, it will | ||||||||||||||||||||
| 60 | receive the last frame handed to C<send_frame>. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =head1 INTERNAL METHODS | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | =head2 C<< $plugin->notify_clients >> | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | $plugin->notify_clients( $PNGframe ) | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Notify all connected clients that they should display the new frame. | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | ▶ | sub notify_clients( $self, @frames ) { | ||||||||||||||||||
|
Subroutine
notify_clients: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 73 | 0 | my $clients = $self->clients; | |||||||||||||||||||
| 74 | 0 | for my $client_id (sort keys %$clients ) { | |||||||||||||||||||
| 75 | 0 | my $client = $clients->{ $client_id }; | |||||||||||||||||||
| 76 | 0 | for my $frame (@frames) { | |||||||||||||||||||
| 77 | 0 | eval { | |||||||||||||||||||
| 78 | 0 | $client->send({ binary => $frame }); | |||||||||||||||||||
| 79 | }; | ||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 0 | ▶ | sub register( $self, $app, $config ) { | ||||||||||||||||||
|
Subroutine
register: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | ▶ | $app->routes->get('/' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 87 | 0 | my( $c ) = @_; | |||||||||||||||||||
| 88 | 0 | $c->res->headers->content_type('text/html'); | |||||||||||||||||||
| 89 | 0 | $c->res->headers->connection('close'); | |||||||||||||||||||
| 90 | 0 | $c->render('index') | |||||||||||||||||||
| 91 | 0 | }); | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | $app->routes->websocket( '/ws' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 94 | 0 | my( $c ) = @_; | |||||||||||||||||||
| 95 | 0 | $c->inactivity_timeout(300); | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | my $client_id = join ":", $c->tx->original_remote_address || $c->tx->remote_address, | ||||||||||||||||||
|
Condition0%0/3
$c->tx->original_remote_address || $c->tx->remote_address
MC/DC0%0/2
$c->tx->original_remote_address || $c->tx->remote_address
$c->tx->original_remote_address
$c->tx->remote_address
Truth table0%0/3
A || B
A= $c->tx->original_remote_addressB= $c->tx->remote_address
| |||||||||||||||||||||
| 98 | $c->tx->remote_port(); | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | $self->clients->{ $client_id } = $c; | |||||||||||||||||||
| 101 | 0 | ▶ | $c->tx->on( json => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 102 | 0 | my( $c, $data ) = @_; | |||||||||||||||||||
| 103 | #warn Dumper $data ; | ||||||||||||||||||||
| 104 | 0 | warn "Click received (and ignored)"; | |||||||||||||||||||
| 105 | #$mech->click( { selector => '//body', single => 1 }, $data->{x}, $data->{y} ); | ||||||||||||||||||||
| 106 | #$mech->click( { selector => '//body', single => 1 }, $data->{x}, $data->{y} ); | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | }); | |||||||||||||||||||
| 109 | #warn("Client connected"); | ||||||||||||||||||||
| 110 | 0 | ▶ | if( $self->last_frame ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | # send current frame | ||||||||||||||||||||
| 112 | 0 | $c->send({ binary => $self->last_frame }); | |||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||
| 114 | # send a standby frame ?? | ||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||
| 116 | 0 | ▶ | $c->tx->on( finish => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 117 | 0 | my( $c,$code,$reason ) = @_; | |||||||||||||||||||
| 118 | 0 | warn "Client gone ($code,$reason)" ; | |||||||||||||||||||
| 119 | 0 | delete $self->clients->{ $client_id }; | |||||||||||||||||||
| 120 | 0 | }); | |||||||||||||||||||
| 121 | 0 | }); | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # Stop our program | ||||||||||||||||||||
| 124 | 0 | ▶ | $app->routes->get( '/stop' => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 125 | 0 | my( $c ) = @_; | |||||||||||||||||||
| 126 | 0 | $c->res->headers->content_type('text/html'); | |||||||||||||||||||
| 127 | 0 | $c->res->headers->connection('close'); | |||||||||||||||||||
| 128 | 0 | $c->render('stop'); | |||||||||||||||||||
| 129 | 0 | Mojo::IOLoop->stop; | |||||||||||||||||||
| 130 | 0 | }); | |||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 0 | ▶ | $app->helper( 'send_frame' => sub ( $c, $framePNG ) { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 133 | # send this frame to all connected clients | ||||||||||||||||||||
| 134 | 0 | ▶ | if( scalar keys %{ $self->clients } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | Future::Mojo->new->done_next_tick( 1 ) | ||||||||||||||||||||
| 136 | 0 | ▶ | ->then( sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 137 | 0 | $self->notify_clients( $framePNG ); | |||||||||||||||||||
| 138 | 0 | })->retain; | |||||||||||||||||||
| 139 | }; | ||||||||||||||||||||
| 140 | 0 | $self->last_frame( $framePNG ); | |||||||||||||||||||
| 141 | 0 | }); | |||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | # Install our templates | ||||||||||||||||||||
| 144 | 0 | push @{$app->renderer->classes}, __PACKAGE__; | |||||||||||||||||||
| 145 | 0 | push @{$app->static->classes}, __PACKAGE__; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | =head1 EXPORTED HTTP ENDPOINTS | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | This plugin makes the following endpoints available | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =over 4 | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =item * | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | C</> - the index page | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | This is an HTML page that opens a websocket to the webserver and listens for | ||||||||||||||||||||
| 159 | PNG images coming in over that websocket | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | =item * | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | C</ws> - the websocket | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | This is a websocket | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =item * | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | C</stop> - stop the application | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | This stops the complete Mojolicious application | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | =back | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | =head1 REPOSITORY | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | The public repository of this module is | ||||||||||||||||||||
| 178 | L<https://github.com/Corion/Mojolicious-Plugin-PNGCast>. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =head1 SUPPORT | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | The public support forum of this module is L<https://perlmonks.org/>. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =head1 BUG TRACKER | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | Please report bugs in this module via the RT CPAN bug queue at | ||||||||||||||||||||
| 187 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=Mojolicious-Plugin-PNGCast> | ||||||||||||||||||||
| 188 | or via mail to L<Mojolicious-Plugin-PNGCast-Bugs@rt.cpan.org|mailto:Mojolicious-Plugin-PNGCast-Bugs@rt.cpan.org>. | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head1 AUTHOR | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | Max Maischein C<corion@cpan.org> | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | =head1 COPYRIGHT (c) | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | Copyright 2010-2018 by Max Maischein C<corion@cpan.org>. | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | =head1 LICENSE | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | 1 | 1 | |||||||||||||||||||
| 205 | __DATA__ | ||||||||||||||||||||