ARDrone/Video.pm

filter: Statementstmt 96.0% 170 / 177 Branchbran 72.7% 16 / 22 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 96.5% 28 / 29 Podpod 100% 2 / 2 totaltotal 92.5% 225 / 243 CC 17 SCAR 9.9
CC
17
Cov
93%
_read_to_next_pave_header
6.1
_process_io
5.2
_read_remaining_pave_header
4.0
CRAP 2.7
« ARDrone/SDLNavOutput.pm ↑ Summary ARDrone/Video/BuildIO.pm »
1 # Copyright (c) 2015 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::ARDrone::Video;
25 7 $UAV::Pilot::ARDrone::Video::VERSION = '1.0';
26 7 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 ▶use UAV::Pilot::Video::H264Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 ▶use Data::Dumper 'Dumper';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 7 $Data::Dumper::Sortkeys = 1;
35  
36  
37 7 ▶use constant READ_INTERVAL => 1 / 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 7 ▶use constant BUF_READ_SIZE => 4096;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶use constant BUF_READ_SIZE_HEADER => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 7 ▶use constant PAVE_HEADER_PARTIAL_PROCESS_SIZE => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use constant PAVE_SIGNATURE => 'PaVE';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 7 ▶use constant PAVE_SIGNATURE_LE => 0x45566150;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 7 ▶use constant PAVE_SIGNATURE_BE => 0x50615645;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 7 ▶use constant PAVE_SIGNATURE_BE_ARRAY => [ 0x50, 0x61, 0x56, 0x45 ];
Subroutine BEGIN: called CC=1 SCAR=0.0
45 use constant {
46 7 CODEC_TYPES => {
47 UNKNOWN => 0,
48 VLIB => 1,
49 P264 => 2,
50 MPEG4_VISUAL => 3,
51 MPEG4_AVC => 4,
52 0 => 'UNKOWN',
53 1 => 'VLIB',
54 2 => 'P264',
55 3 => 'MPEG4_VISUAL',
56 4 => 'MPEG4_AVC',
57 },
58 FRAME_TYPES => {
59 UNKNOWN => 0,
60 IDR_FRAME => 1,
61 I_FRAME => 2,
62 P_FRAME => 3,
63 HEADERS => 4,
64 0 => 'UNKNOWN',
65 1 => 'IDR_FRAME',
66 2 => 'I_FRAME',
67 3 => 'P_FRAME',
68 4 => 'HEADERS',
69 },
70 PAVE_CTRL => {
71 # This one should be interpreted as a bitfield
72 FRAME_DATA => 0,
73 FRAME_ADVERTISEMENT => (1<<0),
74 LAST_FRAME_IN_STREAM => (1<<1),
75 },
76 STREAM_ID_SUFFIX => {
77 MP4_360P => 0,
78 H264_360P => 1,
79 H264_720P => 2,
80 0 => 'MP4_360P',
81 1 => 'H264_360P',
82 2 => 'H264_720P',
83 },
84 7 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 use constant {
87 7 _MODE_PARTIAL_PAVE_HEADER => 0,
88 _MODE_REMAINING_PAVE_HEADER => 1,
89 _MODE_FRAME => 2,
90 _MODE_NEXT_PAVE => 3,
91 7 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93  
94 7 with 'UAV::Pilot::Logger';
95 7 with 'UAV::Pilot::ARDrone::Video::BuildIO';
96  
97  
98 7 has '_io' => (
99 is => 'ro',
100 isa => 'Item',
101 writer => '_set_io',
102 );
103 has 'handlers' => (
104 traits => ['Array'],
105 is => 'rw',
106 isa => 'ArrayRef[UAV::Pilot::Video::H264Handler]',
107 3 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
108 7 handles => {
109 'add_handler' => 'push',
110 },
111 );
112 7 has 'condvar' => (
113 is => 'ro',
114 isa => 'AnyEvent::CondVar',
115 );
116 7 has 'driver' => (
117 is => 'ro',
118 isa => 'UAV::Pilot::ARDrone::Driver',
119 );
120 7 has 'frames_processed' => (
121 traits => ['Number'],
122 is => 'ro',
123 isa => 'Int',
124 default => 0,
125 handles => {
126 '_add_frames_processed' => 'add',
127 },
128 );
129 has '_byte_buffer' => (
130 traits => ['Array'],
131 is => 'rw',
132 isa => 'ArrayRef[Int]',
133 5 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
134 7 handles => {
135 '_byte_buffer_splice' => 'splice',
136 '_byte_buffer_size' => 'count',
137 '_byte_buffer_push' => 'push',
138 },
139 );
140 has '_mode' => (
141 is => 'rw',
142 isa => 'Int',
143 default => sub {
144 5 ▶ my ($class) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
145 5 return $class->_MODE_PARTIAL_PAVE_HEADER;
146 },
147 7 );
148 has '_last_pave_header' => (
149 is => 'rw',
150 isa => 'HashRef[Item]',
151 5 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
152 7 );
153  
154  
155 sub BUILDARGS
156 {
157 3 ▶ my ($class, $args) = @_;
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod documented
158 3 my $io = $class->_build_io( $args );
159  
160 3 $$args{'_io'} = $io;
161 3 delete $$args{'host'};
162 3 delete $$args{'port'};
163 3 return $args;
164 }
165  
166 sub emergency_restart
167 {
168 0 ▶ my ($self) = @_;
Subroutine emergency_restart: not called CC=1 SCAR=6.9
Pod documented
169 # TODO clear buffer of available packets
170 0 $self->_io->close;
171 0 my $io = $self->_build_io({
172 driver => $self->driver,
173 });
174 0 $self->_set_io( $io );
175 0 return 1;
176 }
177  
178  
179 # We split reading the PaVE header into two parts. The first part needs just enough bytes
180 # to get us to the packet size. From there, we know how big the header will actually be,
181 # so we will know if we have enough bytes yet to build the full thing.
182 sub _read_partial_pave_header
183 {
184 75 ▶ my ($self) = @_;
Subroutine _read_partial_pave_header: called CC=3 SCAR=11.0
185 75 ▶ return 1 if $self->_byte_buffer_size < $self->PAVE_HEADER_PARTIAL_PROCESS_SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_byte_buffer_size < $self->PAVE_HEADER_PARTIAL_PROCESS_SIZE 0 75 75
186  
187 75 my @bytes = $self->_byte_buffer_splice( 0, $self->PAVE_HEADER_PARTIAL_PROCESS_SIZE );
188  
189 75 my %packet;
190 75 $packet{'_initial_read_time'} = [Time::HiRes::gettimeofday];
191 75 $packet{signature} = pack 'c4', @bytes[0..3];
192 75 $packet{signature_int} = UAV::Pilot->convert_32bit_LE( @bytes[0..3] );
193 75 $packet{version} = $bytes[4];
194 75 $packet{video_codec} = $bytes[5];
195 75 $packet{packet_size} = UAV::Pilot->convert_16bit_LE( @bytes[6,7]);
196  
197 #warn "Bad PaVE header. Got [$packet{signature}], expected " . $self->PAVE_SIGNATURE
198 75 ▶ if( $packet{signature} ne $self->PAVE_SIGNATURE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet{"signature"} ne $self->PAVE_SIGNATURE) 1 74 75
199 1 $self->_logger->error( "Bad PaVE header. Got [$packet{signature}],"
200 . " expected " . $self->PAVE_SIGNATURE );
201 1 $self->_mode( $self->_MODE_NEXT_PAVE );
202 1 return $self->_read_to_next_pave_header;
203 }
204  
205 74 $self->_last_pave_header( \%packet );
206 74 $self->_mode( $self->_MODE_REMAINING_PAVE_HEADER );
207 74 return $self->_read_remaining_pave_header;
208 }
209  
210 sub _read_remaining_pave_header
211 {
212 77 ▶ my ($self) = @_;
Subroutine _read_remaining_pave_header: called CC=4 SCAR=13.9
213 77 my %packet = %{ $self->_last_pave_header };
214 77 my $remaining_size = $packet{packet_size} - $self->PAVE_HEADER_PARTIAL_PROCESS_SIZE;
215 77 ▶ return 1 if $self->_byte_buffer_size < $remaining_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_byte_buffer_size < $remaining_size 3 74 77
216  
217 74 my @bytes = $self->_byte_buffer_splice( 0, $remaining_size );
218  
219 74 $packet{payload_size} = UAV::Pilot->convert_32bit_LE( @bytes[0..3] );
220 74 $packet{encoded_stream_width} = UAV::Pilot->convert_16bit_LE( @bytes[4,5] );
221 74 $packet{encoded_stream_height} = UAV::Pilot->convert_16bit_LE( @bytes[6,7] );
222 74 $packet{display_width} = UAV::Pilot->convert_16bit_LE( @bytes[8,9] );
223 74 $packet{display_height} = UAV::Pilot->convert_16bit_LE( @bytes[10,11] );
224 74 $packet{frame_number} = UAV::Pilot->convert_32bit_LE( @bytes[12..15] );
225 74 $packet{timestamp} = UAV::Pilot->convert_32bit_LE( @bytes[16..19] );
226 74 $packet{total_chunks} = $bytes[20];
227 74 $packet{chunk_index} = $bytes[21];
228 74 $packet{frame_type} = pack 'C', $bytes[22];
229 74 $packet{control} = $bytes[23];
230 74 $packet{stream_byte_position_lw} = UAV::Pilot->convert_32bit_LE( @bytes[24..27] );
231 74 $packet{stream_byte_position_uw} = UAV::Pilot->convert_32bit_LE( @bytes[28..31] );
232 74 $packet{stream_id_suffix} = $bytes[32];
233 74 $packet{stream_id} = UAV::Pilot->convert_16bit_LE( @bytes[33,34] );
234 74 $packet{total_slices} = $bytes[35];
235 74 $packet{slice_index} = $bytes[36];
236 74 $packet{header1_size} = $bytes[37];
237 74 $packet{header2_size} = $bytes[38];
238 74 $packet{reserved2} = pack 'C2', @bytes[39,40];
239 74 $packet{advertised_size} = UAV::Pilot->convert_32bit_LE( @bytes[41..44] );
240 74 $packet{reserved3} = pack 'C12', @bytes[45..56];
241  
242 74 ▶ $packet{dummy_data} = pack 'C*', @bytes[57..($remaining_size - 1)]
Branch: 1 branch
BranchTrueFalseTotal
if $remaining_size > 57 74 0 74
243 if $remaining_size > 57;
244  
245 74 $packet{video_codec_name} = $self->CODEC_TYPES->{$packet{video_codec}};
246 74 $packet{frame_type_name} = $self->FRAME_TYPES->{$packet{frame_type}};
247 $packet{control_names} = [ map {
248 222 ▶ ($packet{control} & $self->PAVE_CTRL->{$_}) ? $_ : ()
Branch: 1 branch
BranchTrueFalseTotal
$packet{'control'} & $self->PAVE_CTRL->{$_} ? : 0 222 222
249 74 } keys %{ $self->PAVE_CTRL } ];
250 74 $packet{stream_id_suffix_name} = $self->STREAM_ID_SUFFIX->{$packet{stream_id_suffix}};
251 74 $packet{signature_int_hex} = sprintf '0x%x', $packet{signature_int};
252 #warn "Frame " . $self->frames_processed . " dump: " . Dumper( \%packet );
253 74 $self->_logger->info( "Frame num: " . $self->frames_processed
254 . ", size: $packet{payload_size}" );
255  
256  
257 $self->_logger->info( 'VIDEO_FRAME_TIMER,START,' . $self->frames_processed
258 . ','
259 74 . join( ',', @{ +$packet{'_initial_read_time'} } ) );
260  
261 74 $self->_add_frames_processed( 1 );
262 74 $self->_last_pave_header( \%packet );
263 74 $self->_mode( $self->_MODE_FRAME );
264 74 return $self->_read_frame;
265 }
266  
267 sub _read_to_next_pave_header
268 {
269 1 ▶ my ($self) = @_;
Subroutine _read_to_next_pave_header: called CC=6 SCAR=18.0
270 1 my @byte_buf = @{ $self->_byte_buffer };
271 1 my @expect_signature = @{ $self->PAVE_SIGNATURE_BE_ARRAY };
272  
273 1 foreach my $i (0 .. $#byte_buf) {
274 2629 ▶ 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] and $expect_signature[2] == $byte_buf[$i + 2] and $expect_signature[3] == $byte_buf[$i + 3]) 1 2628 2629
Condition77%7/9
$expect_signature[0] == $byte_buf[$i] and $expect_signature[1] == $byte_buf[$i + 1] and $expect_signature[2] == $byte_buf[$i + 2] and $expect_signature[3] == $byte_buf[$i + 3]
!ll&&!rl&&r
262801
$expect_signature[0] == $byte_buf[$i] and $expect_signature[1] == $byte_buf[$i + 1] and $expect_signature[2] == $byte_buf[$i + 2]
!ll&&!rl&&r
262801
$expect_signature[0] == $byte_buf[$i] and $expect_signature[1] == $byte_buf[$i + 1]
!ll&&!rl&&r
2615131
MC/DC50%2/4
$expect_signature[0] == $byte_buf[$i] and $expect_signature[1] == $byte_buf[$i + 1] and $expect_signature[2] == $byte_buf[$i + 2] and $expect_signature[3] == $byte_buf[$i + 3]
$expect_signature[0] == $byte_buf[$i] $expect_signature[1] == $byte_buf[$i + 1] $expect_signature[2] == $byte_buf[$i + 2] $expect_signature[3] == $byte_buf[$i + 3]
Truth table60%3/5
A and B and C and D
A= $expect_signature[0] == $byte_buf[$i]B= $expect_signature[1] == $byte_buf[$i + 1]C= $expect_signature[2] == $byte_buf[$i + 2]D= $expect_signature[3] == $byte_buf[$i + 3]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
275 && ($expect_signature[1] == $byte_buf[$i + 1])
276 && ($expect_signature[2] == $byte_buf[$i + 2])
277 && ($expect_signature[3] == $byte_buf[$i + 3])
278 ) {
279 1 my @new_byte_buffer = @byte_buf[$i..$#byte_buf];
280 1 $self->_byte_buffer( \@new_byte_buffer );
281 1 $self->_mode( $self->_MODE_PARTIAL_PAVE_HEADER );
282 1 return $self->_read_partial_pave_header;
283 }
284 }
285  
286 0 return 1;
287 }
288  
289 sub _read_frame
290 {
291 1079 ▶ my ($self) = @_;
Subroutine _read_frame: called CC=3 SCAR=11.0
292 1079 my %header = %{ $self->_last_pave_header };
293 1079 my $frame_size = $header{payload_size};
294 1079 ▶ return 1 if $self->_byte_buffer_size < $frame_size;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_byte_buffer_size < $frame_size 1008 71 1079
295  
296 71 my @frame = $self->_byte_buffer_splice( 0, $frame_size );
297 71 foreach my $handler (@{ $self->handlers }) {
298 $handler->process_h264_frame(
299 \@frame,
300 48 @header{qw{
301 display_width
302 display_height
303 encoded_stream_width
304 encoded_stream_height
305 }}
306 );
307 }
308  
309 71 $self->_mode( $self->_MODE_PARTIAL_PAVE_HEADER );
310 71 return $self->_read_partial_pave_header;
311 }
312  
313 sub _process_io
314 {
315 1011 ▶ my ($self) = @_;
Subroutine _process_io: called CC=5 SCAR=16.6
316  
317 1011 my $buf;
318 1011 my $read_count = $self->_io->read( $buf, $self->BUF_READ_SIZE );
319 1011 my @bytes = unpack 'C*', $buf;
320 1011 $self->_byte_buffer_push( @bytes );
321  
322 1011 ▶ if( $self->_mode == $self->_MODE_PARTIAL_PAVE_HEADER ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->_mode == $self->_MODE_PARTIAL_PAVE_HEADER) { } 3 1008 1011
elsif ($self->_mode == $self->_MODE_REMAINING_PAVE_HEADER) { } 3 1005 1008
elsif ($self->_mode == $self->_MODE_FRAME) { } 1005 0 1005
elsif ($self->_mode == $self->_MODE_NEXT_PAVE) { } 0 0 0
323 3 $self->_read_partial_pave_header;
324 }
325 elsif( $self->_mode == $self->_MODE_REMAINING_PAVE_HEADER ) {
326 3 $self->_read_remaining_pave_header;
327 }
328 elsif( $self->_mode == $self->_MODE_FRAME ) {
329 1005 $self->_read_frame;
330 }
331 elsif( $self->_mode == $self->_MODE_NEXT_PAVE ) {
332 0 $self->_read_to_next_pave_header;
333 }
334  
335 1011 return 1;
336 }
337  
338  
339 7 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
340 7 __PACKAGE__->meta->make_immutable;
341 7 1;
342 __END__
343  
344  
345 =head1 NAME
346  
347 UAV::Pilot::ARDrone::Video
348  
349 =head1 SYNOPSIS
350  
351 my $cv = AnyEvent->condvar;
352 my $handler = ...; # An object that does UAV::Pilot::Video::H264Handler
353 my $ardrone = ...; # An instance of UAV::Pilot::ARDrone::Driver
354 my $video = UAV::Pilot::ARDrone::Video->new({
355 handlers => [ $handler ],
356 condvar => $cv,
357 driver => $ardrone,
358 });
359
360 $video->init_event_loop;
361 $cv->recv;
362  
363 =head1 DESCRIPTION
364  
365 Processes the Parrot AR.Drone v2 video stream, which is an h264 stream with some
366 additional header data.
367  
368 Note that this I<will not> work with the AR.Drone v1.
369  
370 =head1 METHODS
371  
372 =head2 new
373  
374 new({
375 handlers => [ $handler ],
376 condvar => $cv,
377 driver => $ardrone,
378 })
379  
380 Constructor. The C<handlers> param is an array of objects that do the role
381 C<UAV::Pilot::Video::H264Handler>. Param C<condvar> is an AnyEvent::CondVar.
382 Param C<driver> is an instance of C<UAV::Pilot::Driver::ARDrone>.
383  
384 =head2 init_event_loop
385  
386 Starts the AnyEvent loop for processing video packets.
387  
388 =head2 emergency_restart
389  
390 The AR.Drone will close the connection when emergency mode is toggled. Calling this will
391 close the stream on our end and reintitlize.
392  
393 You shouldn't have to call this directly. Pass this object to your
394 C<UAV::Pilot::Control::ARDrone> instance and it will do it for you.
395  
396 =head2 add_handler
397  
398 add_handler( $handler );
399  
400 Adds a C<UAV::Pilot::Video::H264Handler> object to the list of handlers.
401  
402 =cut
« ARDrone/SDLNavOutput.pm ↑ Summary ARDrone/Video/BuildIO.pm »