blib/lib/UAV/Pilot/WumpusRover/Video.pm

filter: Statementstmt 36.9% 55 / 149 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.5% 15 / 28 Podpod 50.0% 1 / 2 totaltotal 34.4% 71 / 206 CC 15 SCAR 24.6
CC
15
Cov
32%
_read_wump_header
42.0
_read_to_next_wump_header
20.0
_process_io
20.0
CRAP 11.7
↑ Summary Build »
1 # Copyright (c) 2014 Timm Murray
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 #
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 #
13 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 # POSSIBILITY OF SUCH DAMAGE.
24 package UAV::Pilot::WumpusRover::Video;
25 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use UAV::Pilot::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use UAV::Pilot::Video::H264Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 ▶use constant BUF_READ_SIZE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 1 ▶use constant WUMP_VIDEO_MAGIC_NUMBER => 0xFB42;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use constant WUMP_VIDEO_MAGIC_NUMBER_ARRAY => [ 0xFB, 0x42 ];
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use constant WUMP_HEADER_SIZE => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use constant WUMP_VERSION => 0x0000;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 use constant {
39 1 CODEC_TYPE_NULL => 0,
40 CODEC_TYPE_H264 => 1,
41 CODEC_TYPE_MJPEG => 2,
42 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 use constant {
45 1 _MODE_WUMP_HEADER => 0,
46 _MODE_FRAME => 1,
47 _MODE_NEXT_WUMP => 2,
48 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 use constant {
51 1 FLAG_HEARTBEAT => 0,
52 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54  
55 1 with 'UAV::Pilot::Logger';
56  
57 has 'handlers' => (
58 traits => ['Array'],
59 is => 'rw',
60 isa => 'ArrayRef[UAV::Pilot::Video::H264Handler]',
61 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 1 handles => {
63 'add_handler' => 'push',
64 },
65 );
66 1 has 'condvar' => (
67 is => 'ro',
68 isa => 'AnyEvent::CondVar',
69 );
70 1 has 'driver' => (
71 is => 'ro',
72 isa => 'UAV::Pilot::WumpusRover::Driver',
73 );
74 1 has 'frames_processed' => (
75 traits => ['Number'],
76 is => 'ro',
77 isa => 'Int',
78 default => 0,
79 handles => {
80 '_add_frames_processed' => 'add',
81 },
82 );
83 1 has '_io' => (
84 is => 'ro',
85 isa => 'Item',
86 writer => '_set_io',
87 );
88 has '_byte_buffer' => (
89 traits => ['Array'],
90 is => 'rw',
91 isa => 'ArrayRef[Int]',
92 0 ▶ default => sub {[]},
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 1 handles => {
94 '_byte_buffer_splice' => 'splice',
95 '_byte_buffer_size' => 'count',
96 '_byte_buffer_push' => 'push',
97 },
98 );
99 has '_mode' => (
100 is => 'rw',
101 isa => 'Int',
102 default => sub {
103 0 ▶ my ($class) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 return $class->_MODE_WUMP_HEADER;
105 },
106 1 );
107 has '_last_wump_header' => (
108 is => 'rw',
109 isa => 'HashRef[Item]',
110 0 ▶ default => sub {{}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 1 );
112  
113  
114 sub BUILDARGS
115 {
116 0 ▶ my ($class, $args) = @_;
Subroutine BUILDARGS: not called CC=1 SCAR=6.9
Pod documented
117 0 my $io = $class->_build_io( $args );
118  
119 0 $$args{'_io'} = $io;
120 0 delete $$args{'host'};
121 0 delete $$args{'port'};
122 0 return $args;
123 }
124  
125 sub init_event_loop
126 {
127 0 ▶ my ($self) = @_;
Subroutine init_event_loop: not called CC=1 SCAR=6.9
Pod undocumented
128  
129 0 my $io_event; $io_event = AnyEvent->io(
130 fh => $self->_io,
131 poll => 'r',
132 cb => sub {
133 0 ▶ $self->_process_io;
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 $io_event;
135 },
136 0 );
137 0 return 1;
138 }
139  
140  
141 sub _read_wump_header
142 {
143 0 ▶ my ($self) = @_;
Subroutine _read_wump_header: not called CC=6 SCAR=37.4
144 0 ▶ return 1 if $self->_byte_buffer_size < $self->WUMP_HEADER_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_byte_buffer_size < $self->WUMP_HEADER_SIZE 0 0 0
145  
146 0 my @bytes = $self->_byte_buffer_splice( 0, $self->WUMP_HEADER_SIZE );
147  
148 0 my %packet;
149 0 $packet{magic_number} = UAV::Pilot->convert_16bit_BE( @bytes[0,1] );
150 0 $packet{version} = UAV::Pilot->convert_16bit_BE( @bytes[2,3] );
151 0 $packet{codec_id} = UAV::Pilot->convert_16bit_BE( @bytes[4,5] );
152 0 $packet{flags} = UAV::Pilot->convert_32bit_BE( @bytes[6..9] );
153 0 $packet{length} = UAV::Pilot->convert_32bit_BE( @bytes[10..13] );
154 0 $packet{width} = UAV::Pilot->convert_16bit_BE( @bytes[14,15] );
155 0 $packet{height} = UAV::Pilot->convert_16bit_BE( @bytes[16,17] );
156 0 $packet{checksum} = UAV::Pilot->convert_32bit_BE( @bytes[18..21] );
157 # 10 bytes reserved
158  
159 0 ▶ if( $packet{magic_number} != $self->WUMP_VIDEO_MAGIC_NUMBER ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"magic_number"} != $self->WUMP_VIDEO_MAGIC_NUMBER) 0 0 0
160 0 $self->_logger->error( "Bad Wump header. Got [$packet{magic_number}],"
161 . " expected " . $self->WUMP_VIDEO_MAGIC_NUMBER );
162 0 $self->_mode( $self->_MODE_NEXT_WUMP );
163 0 return $self->_read_to_next_wump_header;
164 }
165 0 ▶ if( $packet{version} > $self->WUMP_VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"version"} > $self->WUMP_VERSION) 0 0 0
166 0 $self->_logger->error( "Got Wumpus Video version [$packet{version}]"
167 . ", but only support up to version [" . $self->WUMP_VERSION . "]"
168 );
169 0 $self->_mode( $self->_MODE_NEXT_WUMP );
170 0 return $self->_read_to_next_wump_header;
171 }
172 0 ▶ if( $packet{codec_id} != $self->CODEC_TYPE_H264 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"codec_id"} != $self->CODEC_TYPE_H264) 0 0 0
173 0 $self->_logger->error( "Can only handle encoding h264 packets" );
174 0 $self->_mode( $self->_MODE_NEXT_WUMP );
175 0 return $self->_read_to_next_wump_header;
176 }
177 0 ▶ if( $packet{flags} & (1 << $self->FLAG_HEARTBEAT) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"flags"} & 1 << $self->FLAG_HEARTBEAT) 0 0 0
178 0 $self->_send_heartbeat( $packet{checksum} );
179 }
180  
181 $self->_logger->info( "Received frame " . $self->frames_processed
182 . ", size $packet{length}, checksum "
183 0 . sprintf( '%x', $packet{checksum} ) );
184  
185 0 $self->_add_frames_processed( 1 );
186 0 $self->_last_wump_header( \%packet );
187 0 $self->_mode( $self->_MODE_FRAME );
188 0 return $self->_read_frame;
189 }
190  
191 sub _read_to_next_wump_header
192 {
193 0 ▶ my ($self) = @_;
Subroutine _read_to_next_wump_header: not called CC=4 SCAR=30.0
194 0 my @byte_buf = @{ $self->_byte_buffer };
195 0 my @expect_signature = @{ $self->WUMP_VIDEO_MAGIC_NUMBER_ARRAY };
196  
197 0 foreach my $i (0 .. $#byte_buf) {
198 0 ▶ if( ($expect_signature[0] == $byte_buf[$i])
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_signature[0] == $byte_buf[$i] and $expect_signature[1] == $byte_buf[$i + 1]) 0 0 0
Condition0%0/3
$expect_signature[0] == $byte_buf[$i] and $expect_signature[1] == $byte_buf[$i + 1]
!ll&&!rl&&r
000
MC/DC0%0/2
$expect_signature[0] == $byte_buf[$i] and $expect_signature[1] == $byte_buf[$i + 1]
$expect_signature[0] == $byte_buf[$i] $expect_signature[1] == $byte_buf[$i + 1]
Truth table0%0/3
A and B
A= $expect_signature[0] == $byte_buf[$i]B= $expect_signature[1] == $byte_buf[$i + 1]
ABresult
0X0
100
111
199 && ($expect_signature[1] == $byte_buf[$i + 1])
200 ) {
201 0 my @new_byte_buffer = @byte_buf[$i..$#byte_buf];
202 0 $self->_byte_buffer( \@new_byte_buffer );
203 0 $self->_mode( $self->_MODE_WUMP_HEADER );
204 0 return $self->_read_wump_header;
205 }
206 }
207  
208 0 return 1;
209 }
210  
211 sub _read_frame
212 {
213 0 ▶ my ($self) = @_;
Subroutine _read_frame: not called CC=3 SCAR=24.8
214 0 my %header = %{ $self->_last_wump_header };
215 0 my $frame_size = $header{length};
216 0 ▶ if( $self->_byte_buffer_size < $frame_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_byte_buffer_size < $frame_size) 0 0 0
217 0 $self->_logger->info( "Need $frame_size bytes to read next frame"
218 . ", but only " . $self->_byte_buffer_size . " available"
219 . ", waiting for next read" );
220 0 return 1;
221 }
222  
223 # TODO verify checksum (Adler32)
224  
225 0 my @frame = $self->_byte_buffer_splice( 0, $frame_size );
226 0 foreach my $handler (@{ $self->handlers }) {
227 $handler->process_h264_frame(
228 \@frame,
229 # Redundant width/height in order to fill both width/height
230 # and encoded width/height params
231 0 @header{qw{
232 width
233 height
234 width
235 height
236 }}
237 );
238 }
239  
240 0 $self->_mode( $self->_MODE_WUMP_HEADER );
241 0 return $self->_read_wump_header;
242 }
243  
244  
245 sub _process_io
246 {
247 0 ▶ my ($self) = @_;
Subroutine _process_io: not called CC=4 SCAR=30.0
248  
249 0 my $buf;
250 0 my $read_count = $self->_io->read( $buf, $self->BUF_READ_SIZE );
251 0 my @bytes = unpack 'C*', $buf;
252 0 $self->_byte_buffer_push( @bytes );
253  
254 0 ▶ if( $self->_mode == $self->_MODE_WUMP_HEADER ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_mode == $self->_MODE_WUMP_HEADER) { } 0 0 0
elsif ($self->_mode == $self->_MODE_FRAME) { } 0 0 0
elsif ($self->_mode == $self->_MODE_NEXT_WUMP) { } 0 0 0
255 0 $self->_read_wump_header;
256 }
257 elsif( $self->_mode == $self->_MODE_FRAME ) {
258 0 $self->_read_frame;
259 }
260 elsif( $self->_mode == $self->_MODE_NEXT_WUMP ) {
261 0 $self->_read_to_next_wump_header;
262 }
263  
264 0 return 1;
265 }
266  
267 sub _build_io
268 {
269 0 ▶ my ($class, $args) = @_;
Subroutine _build_io: not called CC=2 SCAR=17.9
270 0 my $driver = $$args{driver};
271 0 my $host = $driver->host;
272 0 my $port = UAV::Pilot::WumpusRover->DEFAULT_VIDEO_PORT;
273  
274 0 ▶ my $io = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $io = "IO::Socket::INET"->new("PeerAddr", $host, "PeerPort", $port, "ReuseAddr", 1, "Blocking", 0) or "UAV::Pilot::IOException"->throw("error", "Could not connect to ${host}:$port for video: $@") 0 0 0
275 PeerAddr => $host,
276 PeerPort => $port,
277 ReuseAddr => 1,
278 Blocking => 0,
279 ) or UAV::Pilot::IOException->throw(
280 error => "Could not connect to $host:$port for video: $@",
281 );
282 0 return $io;
283 }
284  
285 sub _send_heartbeat
286 {
287 0 ▶ my ($self, $checksum) = @_;
Subroutine _send_heartbeat: not called CC=1 SCAR=6.9
288 0 $self->_logger->info( "Sending heartbeat packet for checksum [$checksum]" );
289 0 my $output = pack 'nN'
290 ,$self->WUMP_VIDEO_MAGIC_NUMBER
291 ,$checksum;
292 0 $self->_io->write( $output );
293 0 return 1;
294 }
295  
296  
297 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
298 1 __PACKAGE__->meta->make_immutable;
299 1 1;
300 __END__
301  
↑ Summary Build »