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

filter: Statementstmt 31.1% 29 / 93 Branchbran 6.0% 3 / 50 Conditioncond 56.0% 14 / 25 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 40.0% 6 / 15 Podpod 66.6% 2 / 3 totaltotal 30.5% 61 / 200 CC 38 SCAR 38.3
CC
38
Cov
27%
__ANON__
132.0
enc
72.0
__ANON__
20.0
CRAP 46.2
↑ Summary
1 # Ogg Vorbis 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::Ogg;
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.5 $ =~ /(\d+)\.(\d+)/);
17  
18 # Create a new encoder object
19 6 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod documented
20 6 my $class = shift;
21 6 my $opts = shift;
22  
23 6 my $self = bless({}, $class);
24  
25 6 ▶ my %opts = !defined($opts) ? () : ref($opts) ? %$opts : ($opts, @_);
Branch: 2 branches
BranchTrueFalseTotal
!defined($opts) ? : 1 5 6
ref $opts ? : 0 5 5
26 6 %$self = (%$self, %opts);
27  
28 6 ▶ $self->{quality} ||= 3; # Default quality level of 3
Condition100%2/2
$self->{"quality"} ||= 3
l!l
51
MC/DC100%1/1
$self->{"quality"} ||= 3
$self->{"quality"}
Truth table100%2/2
A
A= $self->{"quality"}
Aresult
11
00
29 6 ▶ $self->{priority} ||= 0; # No priority delta by default
Condition100%2/2
$self->{"priority"} ||= 0
l!l
51
MC/DC100%1/1
$self->{"priority"} ||= 0
$self->{"priority"}
Truth table100%2/2
A
A= $self->{"priority"}
Aresult
11
00
30  
31 6 ▶ $self->{parent} ||= 'main'; # Default parent
Condition100%2/2
$self->{"parent"} ||= "main"
l!l
51
MC/DC100%1/1
$self->{"parent"} ||= "main"
$self->{"parent"}
Truth table100%2/2
A
A= $self->{"parent"}
Aresult
11
00
32 6 ▶ $self->{status} ||= 'status'; # Default events
Condition100%2/2
$self->{"status"} ||= "status"
l!l
51
MC/DC100%1/1
$self->{"status"} ||= "status"
$self->{"status"}
Truth table100%2/2
A
A= $self->{"status"}
Aresult
11
00
33 6 ▶ $self->{error} ||= 'error';
Condition100%2/2
$self->{"error"} ||= "error"
l!l
51
MC/DC100%1/1
$self->{"error"} ||= "error"
$self->{"error"}
Truth table100%2/2
A
A= $self->{"error"}
Aresult
11
00
34 6 ▶ $self->{done} ||= 'done';
Condition100%2/2
$self->{"done"} ||= "done"
l!l
51
MC/DC100%1/1
$self->{"done"} ||= "done"
$self->{"done"}
Truth table100%2/2
A
A= $self->{"done"}
Aresult
11
00
35 6 ▶ $self->{warning} ||= 'warning';
Condition100%2/2
$self->{"warning"} ||= "warning"
l!l
51
MC/DC100%1/1
$self->{"warning"} ||= "warning"
$self->{"warning"}
Truth table100%2/2
A
A= $self->{"warning"}
Aresult
11
00
36  
37 6 return $self;
38 }
39  
40  
41 # Start an encoder.
42 0 ▶sub enc {
Subroutine enc: not called CC=8 SCAR=42.8
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 croak "Input file does not exist: '$self->{input}'"
52 0 ▶ unless (-f $self->{input});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->{"input"} 0 0 0
53  
54 0 $self->{input} =~ /(.*)\.(.*)$/;
55 0 my ($path, $ext) = ($1, $2);
56  
57 0 ▶ croak "Input file extension must be 'wav' or 'flac': I have '$ext'"
Branch: 1 branch
BranchTrueFalseTotal
unless $ext eq "wav" or $ext eq "flac" 0 0 0
Condition0%0/3
$ext eq "wav" or $ext eq "flac"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ext eq "wav" or $ext eq "flac"
$ext eq "wav" $ext eq "flac"
Truth table0%0/3
A or B
A= $ext eq "wav"B= $ext eq "flac"
ABresult
1X1
011
000
58 unless ($ext eq 'wav' || $ext eq 'flac');
59  
60 # Output filename is derived from input, unless specified
61 0 ▶ unless ($self->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"output"}) 0 0 0
62 0 $self->{output} = "$path.ogg";
63 }
64  
65 # For posting events to the parent session. Always passes $self as
66 # the first event argument.
67 0 ▶ sub post_parent {
Subroutine post_parent: not called CC=2 SCAR=17.9
Pod undocumented
68 0 my $kernel = shift;
69 0 my $self = shift;
70 0 my $event = shift;
71  
72 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
73 or carp "Failed to post to '$self->{parent}': $!";
74 }
75  
76 POE::Session->create(
77 inline_states => {
78 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
79 0 my ($heap, $kernel, $self) = @_[HEAP, KERNEL, ARG0];
80  
81 0 $kernel->sig(CHLD => "child"); # We must handle SIGCHLD
82  
83 0 $heap->{self} = $self;
84  
85 0 my @args; # List of arguments for encoder
86  
87 push @args, '--album="' . $self->{album} .'"'
88 0 ▶ if $self->{album};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"album"} 0 0 0
89  
90 push @args, '--genre="' . $self->{genre} .'"'
91 0 ▶ if $self->{genre};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"genre"} 0 0 0
92  
93 push @args, '--title="' . $self->{title} .'"'
94 0 ▶ if $self->{title};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"title"} 0 0 0
95  
96 push @args, '--date="' . $self->{date} .'"'
97 0 ▶ if $self->{date};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"date"} 0 0 0
98  
99 push @args, '--artist="' . $self->{artist} .'"'
100 0 ▶ if $self->{artist};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"artist"} 0 0 0
101  
102 0 push @args, '--output="' . $self->{output} .'"';
103  
104 push @args, '--quality="' . $self->{quality} .'"'
105 0 ▶ if $self->{quality};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"quality"} 0 0 0
106  
107 push @args, '--tracknum="'. $self->{tracknumber}.'"'
108 0 ▶ if $self->{tracknumber};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"tracknumber"} 0 0 0
109  
110 # The comment parameter is a list of tag-value pairs.
111 # Each list element must be passed to the encoder as a
112 # separate --comment argument.
113 0 ▶ if ($self->{comment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"comment"}) 0 0 0
114 0 foreach (@{$self->{comment}}) {
115 0 push @args, '--comment="' . $_ .'"'
116 }
117 }
118  
119 # Name of the encoder program we will use
120 0 my $encoder = 'oggenc';
121  
122 # We might need to use a decoder front-end pipe
123 0 my $decoder = '';
124  
125 # If the input is in flac format, use a flac decoder
126 # front-end, and pipe it to the encoder.
127 # Otherwise pass the input file name direct to the encoder
128 0 ▶ if ($ext eq 'flac') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq 'flac') { } 0 0 0
129 0 $decoder = "flac --decode --silent --stdout $self->{input} |";
130 0 push @args, '-';
131 } else {
132 0 push @args, $self->{input};
133 }
134  
135 $heap->{wheel} = POE::Wheel::Run->new(
136 Program => $decoder.$encoder,
137 ProgramArgs => \@args,
138 Priority => $self->{priority},
139 0 StdioFilter => POE::Filter::Line->new(),
140 Conduit => 'pty',
141 StdoutEvent => 'wheel_stdout',
142 CloseEvent => 'wheel_done',
143 ErrorEvent => 'wheel_error',
144 );
145 },
146  
147 0 ▶ _stop => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
148 },
149  
150 0 ▶ close => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 delete $_[HEAP]->{wheel};
152 },
153  
154 # Handle CHLD signal. Stop the wheel if the exited child is ours.
155 0 ▶ child => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
156 0 my ($kernel, $heap, $signame, $child_pid, $exit_code)
157 = @_[KERNEL, HEAP, ARG0, ARG1, ARG2];
158  
159 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
160 0 delete $heap->{wheel};
161  
162 # If we got en exit code, the child died unexpectedly,
163 # so create a wheel-error event. otherwise the child exited
164 # normally, so create a wheel-done event.
165 0 ▶ if ($exit_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code) { } 0 0 0
166 0 $kernel->yield('wheel_error', $exit_code);
167 } else {
168 0 $kernel->yield('wheel_done');
169 }
170 }
171 },
172  
173 0 ▶ wheel_stdout => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
174 0 my ($kernel, $heap) = @_[KERNEL, HEAP];
175 0 my $self = $heap->{self};
176 0 $_ = $_[ARG0];
177  
178 0 ▶ if (m{^ERROR: (.*)}i) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^ERROR: (.*)/i) { } 0 0 0
elsif (/^WARNING: (.*)/i) { } 0 0 0
elsif (/^ \s+ \[ \s+ ([0-9.]+) % \s* \] \s+ \[ \s+ (\d+) m (\d+) s \s+ remaining \s* \] /x) { } 0 0 0
179 # An error message has been emitted by the encoder.
180 # Remember the message for later
181 0 $self->{message} = $1;
182 } elsif (m{^WARNING: (.*)}i) {
183 # A warning message has been emitted by the encoder.
184 # Post the warning message to the parent
185 post_parent($kernel, $self, $self->{warning},
186 $self->{input},
187 $self->{output},
188 0 $1
189 );
190 0 return;
191 } elsif (m{^
192 \s+ \[ \s+ ([0-9.]+) % \s* \]
193 \s+ \[ \s+ (\d+) m (\d+) s \s+ remaining \s* \]
194 }x) {
195 # We have a progress message from the encoder
196 # Post the percentage and number of remaining seconds
197 # to the parent.
198 0 my ($percent, $seconds) = ($1, $2 * 60 + $3);
199  
200 post_parent($kernel, $self, $self->{status},
201 $self->{input},
202 $self->{output},
203 0 $percent, $seconds
204 );
205 }
206 },
207  
208 0 ▶ wheel_error => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
209 0 my ($kernel, $heap) = @_[KERNEL, HEAP];
210 0 my $self = $heap->{self};
211  
212 post_parent($kernel, $self, $self->{error},
213 $self->{input},
214 $self->{output},
215 $_[ARG0],
216 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
217 );
218  
219 # Remove output file: might be incomplete
220 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
221 },
222  
223 0 ▶ wheel_done => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
224 0 my ($kernel, $heap) = @_[KERNEL, HEAP];
225 0 my $self = $heap->{self};
226  
227 # Delete the input file if instructed
228 0 ▶ unlink $self->{input} if $self->{delete};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"delete"} 0 0 0
229  
230 post_parent($kernel, $self, $self->{done},
231 $self->{input},
232 $self->{output}
233 0 );
234 },
235 },
236 0 args => [$self]
237 );
238 }
239  
240 1 1;
241 __END__
↑ Summary