WumpusRover/Video.pm

filter: Statementstmt 77.1% 115 / 149 Branchbran 50.0% 11 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 85.7% 24 / 28 Podpod 50.0% 1 / 2 totaltotal 73.3% 151 / 206 CC 15 SCAR 14.9
CC
15
Cov
72%
_read_to_next_wump_header
20.0
_read_wump_header
7.1
_build_io
6.0
CRAP 4.4
↑ Summary WumpusRover/Packet.pm »
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 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use UAV::Pilot::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use UAV::Pilot::Video::H264Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 ▶use constant BUF_READ_SIZE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use constant WUMP_VIDEO_MAGIC_NUMBER => 0xFB42;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use constant WUMP_VIDEO_MAGIC_NUMBER_ARRAY => [ 0xFB, 0x42 ];
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use constant WUMP_HEADER_SIZE => 32;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use constant WUMP_VERSION => 0x0000;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 use constant {
39 2 CODEC_TYPE_NULL => 0,
40 CODEC_TYPE_H264 => 1,
41 CODEC_TYPE_MJPEG => 2,
42 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 use constant {
45 2 _MODE_WUMP_HEADER => 0,
46 _MODE_FRAME => 1,
47 _MODE_NEXT_WUMP => 2,
48 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 use constant {
51 2 FLAG_HEARTBEAT => 0,
52 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54  
55 2 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 2 handles => {
63 'add_handler' => 'push',
64 },
65 );
66 2 has 'condvar' => (
67 is => 'ro',
68 isa => 'AnyEvent::CondVar',
69 );
70 2 has 'driver' => (
71 is => 'ro',
72 isa => 'UAV::Pilot::WumpusRover::Driver',
73 );
74 2 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 2 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 1 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
93 2 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 1 ▶ my ($class) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
104 1 return $class->_MODE_WUMP_HEADER;
105 },
106 2 );
107 has '_last_wump_header' => (
108 is => 'rw',
109 isa => 'HashRef[Item]',
110 1 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
111 2 );
112  
113  
114 sub BUILDARGS
115 {
116 1 ▶ my ($class, $args) = @_;
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
117 1 my $io = $class->_build_io( $args );
118  
119 1 $$args{'_io'} = $io;
120 1 delete $$args{'host'};
121 1 delete $$args{'port'};
122 1 return $args;
123 }
124  
125 sub init_event_loop
126 {
127 1 ▶ my ($self) = @_;
Subroutine init_event_loop: called CC=1 SCAR=0.0
Pod undocumented
128  
129 1 my $io_event; $io_event = AnyEvent->io(
130 fh => $self->_io,
131 poll => 'r',
132 cb => sub {
133 4289 ▶ $self->_process_io;
Subroutine __ANON__: called CC=1 SCAR=0.0
134 4289 $io_event;
135 },
136 1 );
137 1 return 1;
138 }
139  
140  
141 sub _read_wump_header
142 {
143 4289 ▶ my ($self) = @_;
Subroutine _read_wump_header: called CC=6 SCAR=19.7
144 4289 ▶ return 1 if $self->_byte_buffer_size < $self->WUMP_HEADER_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_byte_buffer_size < $self->WUMP_HEADER_SIZE 4265 24 4289
145  
146 24 my @bytes = $self->_byte_buffer_splice( 0, $self->WUMP_HEADER_SIZE );
147  
148 24 my %packet;
149 24 $packet{magic_number} = UAV::Pilot->convert_16bit_BE( @bytes[0,1] );
150 24 $packet{version} = UAV::Pilot->convert_16bit_BE( @bytes[2,3] );
151 24 $packet{codec_id} = UAV::Pilot->convert_16bit_BE( @bytes[4,5] );
152 24 $packet{flags} = UAV::Pilot->convert_32bit_BE( @bytes[6..9] );
153 24 $packet{length} = UAV::Pilot->convert_32bit_BE( @bytes[10..13] );
154 24 $packet{width} = UAV::Pilot->convert_16bit_BE( @bytes[14,15] );
155 24 $packet{height} = UAV::Pilot->convert_16bit_BE( @bytes[16,17] );
156 24 $packet{checksum} = UAV::Pilot->convert_32bit_BE( @bytes[18..21] );
157 # 10 bytes reserved
158  
159 24 ▶ if( $packet{magic_number} != $self->WUMP_VIDEO_MAGIC_NUMBER ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"magic_number"} != $self->WUMP_VIDEO_MAGIC_NUMBER) 0 24 24
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 24 ▶ if( $packet{version} > $self->WUMP_VERSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"version"} > $self->WUMP_VERSION) 0 24 24
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 24 ▶ if( $packet{codec_id} != $self->CODEC_TYPE_H264 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"codec_id"} != $self->CODEC_TYPE_H264) 0 24 24
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 24 ▶ if( $packet{flags} & (1 << $self->FLAG_HEARTBEAT) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"flags"} & 1 << $self->FLAG_HEARTBEAT) 24 0 24
178 24 $self->_send_heartbeat( $packet{checksum} );
179 }
180  
181 $self->_logger->info( "Received frame " . $self->frames_processed
182 . ", size $packet{length}, checksum "
183 24 . sprintf( '%x', $packet{checksum} ) );
184  
185 24 $self->_add_frames_processed( 1 );
186 24 $self->_last_wump_header( \%packet );
187 24 $self->_mode( $self->_MODE_FRAME );
188 24 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 48 ▶ my ($self) = @_;
Subroutine _read_frame: called CC=3 SCAR=11.0
214 48 my %header = %{ $self->_last_wump_header };
215 48 my $frame_size = $header{length};
216 48 ▶ if( $self->_byte_buffer_size < $frame_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_byte_buffer_size < $frame_size) 24 24 48
217 24 $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 24 return 1;
221 }
222  
223 # TODO verify checksum (Adler32)
224  
225 24 my @frame = $self->_byte_buffer_splice( 0, $frame_size );
226 24 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 24 @header{qw{
232 width
233 height
234 width
235 height
236 }}
237 );
238 }
239  
240 24 $self->_mode( $self->_MODE_WUMP_HEADER );
241 24 return $self->_read_wump_header;
242 }
243  
244  
245 sub _process_io
246 {
247 4289 ▶ my ($self) = @_;
Subroutine _process_io: called CC=4 SCAR=14.5
248  
249 4289 my $buf;
250 4289 my $read_count = $self->_io->read( $buf, $self->BUF_READ_SIZE );
251 4289 my @bytes = unpack 'C*', $buf;
252 4289 $self->_byte_buffer_push( @bytes );
253  
254 4289 ▶ if( $self->_mode == $self->_MODE_WUMP_HEADER ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_mode == $self->_MODE_WUMP_HEADER) { } 4265 24 4289
elsif ($self->_mode == $self->_MODE_FRAME) { } 24 0 24
elsif ($self->_mode == $self->_MODE_NEXT_WUMP) { } 0 0 0
255 4265 $self->_read_wump_header;
256 }
257 elsif( $self->_mode == $self->_MODE_FRAME ) {
258 24 $self->_read_frame;
259 }
260 elsif( $self->_mode == $self->_MODE_NEXT_WUMP ) {
261 0 $self->_read_to_next_wump_header;
262 }
263  
264 4289 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 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
298 2 __PACKAGE__->meta->make_immutable;
299 2 1;
300 __END__
301  
↑ Summary WumpusRover/Packet.pm »