File Coverage

blib/lib/UAV/Pilot/Video/FileDump.pm
Criterion Covered Total %
statement 25 34 73.5
branch 3 12 25.0
condition 1 2 50.0
subroutine 6 7 85.7
pod 1 3 33.3
total 36 58 62.0


line stmt bran cond sub pod time code
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::Video::FileDump;
25             $UAV::Pilot::Video::FileDump::VERSION = '1.3';
26 1     1   21446 use v5.14;
  1         4  
27 1     1   840 use Moose;
  1         483988  
  1         7  
28 1     1   7811 use namespace::autoclean;
  1         8290  
  1         4  
29              
30             with 'UAV::Pilot::Video::H264Handler';
31              
32             has 'file' => (
33             is => 'ro',
34             isa => 'Str',
35             );
36             has 'single_frame' => (
37             is => 'ro',
38             isa => 'Bool',
39             default => 0,
40             );
41             has '_frame_count' => (
42             is => 'rw',
43             isa => 'Int',
44             default => 0,
45             );
46             has '_fh' => (
47             is => 'ro',
48             isa => 'Item',
49             );
50              
51              
52             sub BUILDARGS
53             {
54 1     1 1 3 my ($class, $args) = @_;
55              
56 1 50       7 if(! $args->{single_frame} ) {
57 1         3 my $fh;
58 1 50       4 if( defined $args->{file}) {
59             open( my $fh, '>', $args->{file} )
60 0 0       0 or die "Can't open $$args{file}: $!\n";
61             }
62             else {
63 1   50     5 $fh = $args->{fh} // \*STDOUT;
64             }
65              
66 1         3 $args->{'_fh'} = $fh;
67             }
68              
69 1         35 return $args;
70             }
71              
72             sub process_h264_frame
73             {
74 1     1 0 4 my ($self, $packet) = @_;
75              
76 1         13 my $data = pack( 'C*', @$packet );
77 1 50       54 if( $self->single_frame) {
78 0         0 my $base_file = $self->file;
79 0         0 my $full_file = sprintf(
80             $base_file . '.%05i',
81             $self->_frame_count
82             );
83              
84 0 0       0 open( my $fh, '>', $full_file )
85             or die "Can't open $full_file: $!\n";
86 0         0 print $fh $data;
87 0         0 close $fh;
88             }
89             else {
90 1         47 my $fh = $self->_fh;
91 1         7 print $fh $data;
92              
93             }
94              
95 1         46 $self->_frame_count( $self->_frame_count + 1 );
96 1         3 return 1;
97             }
98              
99             sub close
100             {
101 0     0 0   my ($self) = @_;
102 0 0         $self->_fh->close if defined $self->_fh;
103 0           return 1;
104             }
105              
106              
107 1     1   438 no Moose;
  1         2  
  1         7  
108             __PACKAGE__->meta->make_immutable;
109             1;
110             __END__
111              
112              
113             =head1 NAME
114              
115             UAV::Pilot::Video::FileDump
116              
117             =head1 SYNOPSIS
118              
119             open( my $vid_out, '>', 'video.h264' ) or die $!;
120             my $file_dump = UAV::Pilot::Video::FileDump->new({
121             fh => $vid_out,
122             });
123             my $video = UAV::Pilot::Driver::ARDrone::Video->new({
124             handler => $file_dump,
125             ...
126             });
127              
128             =head1 DESCRIPTION
129              
130             Writes the h264 video frames to a file. Afterwords, you should be able to play this file
131             with mplayer or other video players that support h264 without being inside a container
132             format.
133              
134             =cut