blib/lib/POE/Component/Enc/Flac.pm

filter: Statementstmt 36.2% 29 / 80 Branchbran 8.8% 3 / 34 Conditioncond 63.6% 14 / 22 MC/DCmcdc 58.3% 7 / 12 Subroutinesub 40.0% 6 / 15 Podpod 66.6% 2 / 3 totaltotal 36.7% 61 / 166 CC 29 SCAR 27.5
CC
29
Cov
34%
enc
30.0
__ANON__
30.0
__ANON__
20.0
CRAP 15.6
↑ Summary
1 # FLAC encoding component for POE
2 # Copyright (c) 2004 Steve James. All rights reserved.
3 #
4 # This library is free software; you can redistribute it and/or modify
5 # it under the same terms as Perl itself.
6 #
7  
8 package POE::Component::Enc::Flac;
9  
10 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use POE qw(Wheel::Run Filter::Line Driver::SysRW);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our $VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);
17  
18 # Create a new encoder object
19 5 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod documented
20 5 my $class = shift;
21 5 my $opts = shift;
22  
23 5 my $self = bless({}, $class);
24  
25 5 ▶ my %opts = !defined($opts) ? () : ref($opts) ? %$opts : ($opts, @_);
Branch: 2 branches
BranchTrueFalseTotal
!defined($opts) ? : 1 4 5
ref $opts ? : 0 4 4
26 5 %$self = (%$self, %opts);
27  
28 5 ▶ $self->{compression} ||= 5; # Default compression level
Condition100%2/2
$self->{"compression"} ||= 5
l!l
41
MC/DC100%1/1
$self->{"compression"} ||= 5
$self->{"compression"}
Truth table100%2/2
A
A= $self->{"compression"}
Aresult
11
00
29 5 ▶ $self->{priority} ||= 0; # No priority delta by default
Condition100%2/2
$self->{"priority"} ||= 0
l!l
41
MC/DC100%1/1
$self->{"priority"} ||= 0
$self->{"priority"}
Truth table100%2/2
A
A= $self->{"priority"}
Aresult
11
00
30  
31 5 ▶ $self->{parent} ||= 'main'; # Default parent
Condition100%2/2
$self->{"parent"} ||= "main"
l!l
41
MC/DC100%1/1
$self->{"parent"} ||= "main"
$self->{"parent"}
Truth table100%2/2
A
A= $self->{"parent"}
Aresult
11
00
32 5 ▶ $self->{status} ||= 'status'; # Default events
Condition100%2/2
$self->{"status"} ||= "status"
l!l
41
MC/DC100%1/1
$self->{"status"} ||= "status"
$self->{"status"}
Truth table100%2/2
A
A= $self->{"status"}
Aresult
11
00
33 5 ▶ $self->{error} ||= 'error';
Condition100%2/2
$self->{"error"} ||= "error"
l!l
41
MC/DC100%1/1
$self->{"error"} ||= "error"
$self->{"error"}
Truth table100%2/2
A
A= $self->{"error"}
Aresult
11
00
34 5 ▶ $self->{done} ||= 'done';
Condition100%2/2
$self->{"done"} ||= "done"
l!l
41
MC/DC100%1/1
$self->{"done"} ||= "done"
$self->{"done"}
Truth table100%2/2
A
A= $self->{"done"}
Aresult
11
00
35 5 ▶ $self->{warning} ||= 'warning';
Condition100%2/2
$self->{"warning"} ||= "warning"
l!l
41
MC/DC100%1/1
$self->{"warning"} ||= "warning"
$self->{"warning"}
Truth table100%2/2
A
A= $self->{"warning"}
Aresult
11
00
36  
37 5 return $self;
38 }
39  
40  
41 # Start an encoder.
42 0 ▶sub enc {
Subroutine enc: not called CC=5 SCAR=34.0
Pod documented
43 0 my $self = shift;
44 0 my $opts = shift;
45  
46 0 ▶ my %opts = !defined($opts) ? () : ref($opts) ? %$opts : ($opts, @_);
Branch: 2 branches
BranchTrueFalseTotal
!defined($opts) ? : 0 0 0
ref $opts ? : 0 0 0
47 0 %$self = (%$self, %opts);
48  
49 0 ▶ croak "No input file specified" unless $self->{input};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"input"} 0 0 0
50  
51 # Output filename is derived from input, unless specified
52 0 ▶ unless ($self->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"output"}) 0 0 0
53 0 ($self->{output} = $self->{input}) =~ s/(.*)\.(.*)$/$1.flac/;
54 }
55  
56 # For posting events to the parent session. Always passes $self as
57 # the first event argument.
58 0 ▶ sub post_parent {
Subroutine post_parent: not called CC=2 SCAR=17.9
Pod undocumented
59 0 my $kernel = shift;
60 0 my $self = shift;
61 0 my $event = shift;
62  
63 0 ▶ $kernel->post($self->{parent}, $event, $self, @_)
Branch: 1 branch
BranchTrueFalseTotal
$kernel->post($self->{"parent"}, $event, $self, @_) or carp(qq[Failed to post to '$self->{"parent"}': $!]) 0 0 0
64 or carp "Failed to post to '$self->{parent}': $!";
65 }
66  
67 POE::Session->create(
68 inline_states => {
69 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
70 0 my ($heap, $kernel, $self) = @_[HEAP, KERNEL, ARG0];
71  
72 0 $kernel->sig(CHLD => "child"); # We must handle SIGCHLD
73  
74 0 $heap->{self} = $self;
75  
76 0 my @args; # List of arguments for encoder
77  
78 0 push @args, '--output-name="' . $self->{output} .'"';
79  
80 push @args, '-' . $self->{compression}
81 0 ▶ if $self->{compression};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"compression"} 0 0 0
82  
83 # The comment parameter is a list of tag-value pairs.
84 # Each list element must be passed to the encoder as a
85 # separate --tag argument.
86 0 ▶ if ($self->{comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"comment"}) 0 0 0
87 0 foreach (@{$self->{comment}}) {
88 0 push @args, '--tag="' . $_ .'"'
89 }
90 }
91  
92 # Finally, the input file
93 0 push @args, $self->{input};
94  
95 $heap->{wheel} = POE::Wheel::Run->new(
96 Program => 'flac',
97 ProgramArgs => \@args,
98 Priority => $self->{priority},
99 0 StdioFilter => POE::Filter::Line->new(),
100 Conduit => 'pty',
101 StdoutEvent => 'wheel_stdout',
102 CloseEvent => 'wheel_done',
103 ErrorEvent => 'wheel_error',
104 );
105 },
106  
107 0 ▶ _stop => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
108 },
109  
110 0 ▶ close => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 delete $_[HEAP]->{wheel};
112 },
113  
114 # Handle CHLD signal. Stop the wheel if the exited child is ours.
115 0 ▶ child => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
116 0 my ($kernel, $heap, $signame, $child_pid, $exit_code)
117 = @_[KERNEL, HEAP, ARG0, ARG1, ARG2];
118  
119 0 ▶ if ($heap->{wheel} && $heap->{wheel}->PID() == $child_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($heap->{"wheel"} and $heap->{"wheel"}->PID == $child_pid) 0 0 0
Condition0%0/3
$heap->{"wheel"} and $heap->{"wheel"}->PID == $child_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$heap->{"wheel"} and $heap->{"wheel"}->PID == $child_pid
$heap->{"wheel"} $heap->{"wheel"}->PID == $child_pid
Truth table0%0/3
A and B
A= $heap->{"wheel"}B= $heap->{"wheel"}->PID == $child_pid
ABresult
0X0
100
111
120 0 delete $heap->{wheel};
121  
122 # If we got en exit code, the child died unexpectedly,
123 # so create a wheel-error event. otherwise the child exited
124 # normally, so create a wheel-done event.
125 0 ▶ if ($exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code) { } 0 0 0
126 0 $kernel->yield('wheel_error', $exit_code);
127 } else {
128 0 $kernel->yield('wheel_done');
129 }
130 }
131 },
132  
133 0 ▶ wheel_stdout => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
134 0 my ($kernel, $heap) = @_[KERNEL, HEAP];
135 0 my $self = $heap->{self};
136 0 $_ = $_[ARG0];
137  
138 0 ▶ if (m{^ERROR: (.*)}i) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^ERROR: (.*)/i) { } 0 0 0
elsif (/^WARNING: (.*)/i) { } 0 0 0
elsif (/ \S+:\s+ # input file name (\d+)%\s+complete, # Percentage completion \s+ratio=([0-9.]+) # Current compression ratio /x) { } 0 0 0
elsif (/ \S+:\s+ # input file name wrote\s+(\d+)\s+bytes, # Percentage completion \s+ratio=([0-9.]+) # Compression ratio /x) { } 0 0 0
139 # An error message has been emitted by the encoder.
140 # Remember the message for later
141 0 $self->{message} = $1;
142 } elsif (m{^WARNING: (.*)}i) {
143 # A warning message has been emitted by the encoder.
144 # Post the warning message to the parent
145 post_parent($kernel, $self, $self->{warning},
146 $self->{input},
147 $self->{output},
148 0 $1
149 );
150 0 return;
151 } elsif (m{
152 \S+:\s+ # input file name
153 (\d+)%\s+complete, # Percentage completion
154 \s+ratio=([0-9.]+) # Current compression ratio
155 }x) {
156 # We have a progress message from the compressor
157 # Post the percentage and ratio to the parent.
158 0 my ($percent, $ratio) = ($1, $2);
159  
160 post_parent($kernel, $self, $self->{status},
161 $self->{input},
162 $self->{output},
163 0 $percent, $ratio
164 );
165 } elsif (m{
166 \S+:\s+ # input file name
167 wrote\s+(\d+)\s+bytes, # Percentage completion
168 \s+ratio=([0-9.]+) # Compression ratio
169 }x) {
170 # We have a completion message from the compressor
171 # Post the percentage and ratio to the parent.
172 0 my ($size, $ratio) = ($1, $2);
173  
174 post_parent($kernel, $self, $self->{status},
175 $self->{input},
176 $self->{output},
177 0 100, $ratio
178 );
179 }
180 },
181  
182 0 ▶ wheel_error => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
183 0 my ($kernel, $heap) = @_[KERNEL, HEAP];
184 0 my $self = $heap->{self};
185  
186 post_parent($kernel, $self, $self->{error},
187 $self->{input},
188 $self->{output},
189 $_[ARG0],
190 0 ▶ $self->{message} || ''
Condition0%0/2
$self->{'message'} || ''
l!l
00
MC/DC0%0/1
$self->{'message'} || ''
$self->{'message'}
Truth table0%0/2
A
A= $self->{'message'}
Aresult
11
00
191 );
192  
193 # Remove output file: might be incomplete
194 0 ▶ $_ = $self->{output}; unlink if ($_ && -f);
Branch: 1 branch
BranchTrueFalseTotal
if $_ and -f $_ 0 0 0
Condition0%0/3
$_ and -f $_
!ll&&!rl&&r
000
MC/DC0%0/2
$_ and -f $_
$_ -f $_
Truth table0%0/3
A and B
A= $_B= -f $_
ABresult
0X0
100
111
195 },
196  
197 0 ▶ wheel_done => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
198 0 my ($kernel, $heap) = @_[KERNEL, HEAP];
199 0 my $self = $heap->{self};
200  
201 # Delete the input file if instructed
202 0 ▶ unlink $self->{input} if $self->{delete};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"delete"} 0 0 0
203  
204 post_parent($kernel, $self, $self->{done},
205 $self->{input},
206 $self->{output}
207 0 );
208 },
209 },
210 0 args => [$self]
211 );
212 }
213  
214 1 1;
215 __END__
↑ Summary