WebRadio/Station/Shoutcast.pm

filter: Statementstmt 21.8% 38 / 174 Branchbran 1.3% 1 / 76 Conditioncond 5.5% 1 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 57.1% 12 / 21 Podpod 54.5% 6 / 11 totaltotal 18.7% 58 / 310 CC 48 SCAR 66.5
CC
48
Cov
15%
receive
1406.0
connect
56.0
icySysread
12.0
CRAP 771.9
↑ Summary WebRadio.pm »
1  
2 package Netx::WebRadio::Station::Shoutcast;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #use Exporter ();
9 1 ▶ use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 $VERSION = 0.03;
11 #@ISA = qw (Exporter);
12  
13 #Give a hoot don't pollute, do not export more than needed by default
14 #@EXPORT = qw ();
15 #@EXPORT_OK = qw ();
16 #%EXPORT_TAGS = ();
17  
18 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use IO::Poll 0.04 qw(POLLIN POLLOUT POLLERR POLLHUP);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use Errno qw(EAGAIN EINPROGRESS);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 use Class::MethodMaker
24 1 ▶ new_with_init => 'new',
Subroutine BEGIN: called CC=1 SCAR=0.0
25 get_set =>
26 1 [qw /pollmode host port socket blocking path useragent stationname/];
27  
28 1 %Netx::WebRadio::Station::Shoutcast::pollmodes = (
29 START => undef,
30 CONNECT => POLLOUT,
31 SENDHEADER => POLLOUT,
32 READHEADER => POLLIN,
33 MDSYNC => POLLIN,
34 MDNOSYNC => POLLIN,
35 READMD => POLLIN
36 );
37 }
38  
39 =head1 NAME
40  
41 Netx::WebRadio::Station::Shoutcast - receive one shoutcast-stream
42  
43 =head1 SYNOPSIS
44  
45 see Netx::WebRadio
46
47  
48 =head1 DESCRIPTION
49  
50 Netx::WebRadio::Station::Shoutcast-objects can be used with Netx::WebRadio to receive a Shoutcast-stream.
51  
52 =head1 USAGE
53  
54 You can overload some methods to change the behaviour of the module.
55  
56 The default implementation does not process the received mp3-data in any way.
57 Overload some of the methods to process the sound-data.
58  
59 =head1 METHODS
60  
61 =head2 host
62  
63 Usage : $station->host( $host )
64 Purpose : set the hostname of the server
65 Returns : the actual hostname if called without arguments
66 Argument : hostname
67 Throws : nothing
68 See Also :
69  
70 =head2 port
71  
72 Usage : $station->port( $port )
73 Purpose : set the port of the server
74 Returns : the actual port if called without arguments
75 Argument : portnumber
76 Throws : nothing
77 See Also :
78  
79 =head2 path
80  
81 Usage : $station->path( $path )
82 Purpose : set the path of the stream on the server
83 Returns : the actual path if called without arguments
84 Argument : path
85 Throws : nothing
86 See Also :
87  
88 =head2 useragent
89  
90 Usage : $station->useragent( 'Winamp...' )
91 Purpose : set the useragent. the value is sent to the server on connect
92 Returns : the actual useragent if called without arguments
93 Argument : useragent-string
94 Throws : nothing
95 See Also :
96  
97  
98 =head2 stationname
99  
100 Usage : $stationname = $station->stationname()
101 Purpose : get the stationname
102 Returns : the stationname
103 Argument : nothing
104 Throws : nothing
105 See Also :
106  
107 =head2 receive
108  
109 Usage : $station->receive( )
110 Purpose :
111 Receives next chunk from the station.
112 You have to call it everytime the socket is ready for the next operation.
113 This is done from Netx::WebRadio in most cases.
114 Returns : 1 for 'ok', other values can be specified in the overloadable method 'disconnected'
115 Argument : nothing
116 Throws : nothing
117 See Also :
118  
119 =cut
120  
121 0 ▶sub receive {
Subroutine receive: not called CC=37 SCAR=72.5
Pod documented
122 0 my $self = shift;
123 0 my $socket = $self->socket;
124  
125 0 ▶ if ( $self->get_state eq 'START' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_state eq "START") 0 0 0
126 0 croak "call connect first\n";
127 }
128  
129 0 ▶ if ( $self->get_state eq 'CONNECT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_state eq "CONNECT") 0 0 0
130 0 ▶ if ( $socket->connected ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->connected) { } 0 0 0
131 0 $self->set_state('SENDHEADER');
132 }
133 else {
134 0 return $self->disconnected();
135 }
136 0 return 1;
137 }
138  
139 0 ▶ if ( $self->get_state eq 'SENDHEADER' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_state eq "SENDHEADER") 0 0 0
140 0 ▶ my $path = $self->path || '/';
Condition0%0/2
$self->path || '/'
l!l
00
MC/DC0%0/1
$self->path || '/'
$self->path
Truth table0%0/2
A
A= $self->path
Aresult
11
00
141 my $header = $self->{_header}
142 0 ▶ || "GET $path HTTP/1.0\r\n" . "Host:"
Condition0%0/2
$self->{'_header'} || "GET $path HTTP/1.0\r\n" . 'Host:' . $self->host . "\r\n" . "Accept:*/*\r\n" . 'User-Agent:' . $self->useragent . "\r\n" . "Icy-Metadata:1\r\n\r\n"
l!l
00
MC/DC0%0/1
$self->{'_header'} || "GET $path HTTP/1.0\r\n" . 'Host:' . $self->host . "\r\n" . "Accept:*/*\r\n" . 'User-Agent:' . $self->useragent . "\r\n" . "Icy-Metadata:1\r\n\r\n"
$self->{'_header'}
Truth table0%0/2
A
A= $self->{'_header'}
Aresult
11
00
143 . $self->host() . "\r\n"
144 . "Accept:*/*\r\n"
145 . "User-Agent:"
146 . $self->useragent() . "\r\n"
147 . "Icy-Metadata:1\r\n\r\n";
148 0 my $bytes = $self->icySyswrite( $socket, $header );
149 0 ▶ unless ($bytes) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bytes) 0 0 0
150 0 ▶ return 1 if $! == EAGAIN;
Branch: 1 branch
BranchTrueFalseTotal
if $! == 11 0 0 0
151 0 return $self->disconnected();
152 }
153 0 substr( $header, 0, $bytes ) = '';
154 0 ▶ unless ($header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($header) 0 0 0
155 0 $self->set_state('READHEADER');
156 }
157 0 $self->{_header} = $header;
158 0 ▶ $self->{_header} || delete $self->{_header};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_header"} or delete $self->{"_header"} 0 0 0
159 0 return 1;
160 }
161  
162 0 ▶ if ( $self->get_state eq 'READHEADER' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_state eq "READHEADER") 0 0 0
163 0 my $in;
164 0 $self->{_metaLength} = 0;
165 0 my $tLength = $self->icySysread( $socket, $in, 1024 );
166 0 ▶ unless ( defined $tLength ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tLength) 0 0 0
167 0 return $self->disconnected();
168 }
169 0 ▶ my $tempHeader = $self->{_tempHeader} || '';
Condition0%0/2
$self->{'_tempHeader'} || ''
l!l
00
MC/DC0%0/1
$self->{'_tempHeader'} || ''
$self->{'_tempHeader'}
Truth table0%0/2
A
A= $self->{'_tempHeader'}
Aresult
11
00
170 0 $in = $tempHeader . $in;
171  
172 0 ▶ if ( $in =~ /\r\n\r\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /\r\n\r\n/) { } 0 0 0
173  
174 # header complete
175 0 $self->{_audio} .= $'; # post-match
176 0 my $header = $`;
177 0 ▶ if ( $header =~ /icy-metaint:\s*(\d*)\r\n/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /icy-metaint:\s*(\d*)\r\n/i) { } 0 0 0
178 0 $self->{_metaLength} = $1;
179 }
180 else {
181 0 croak "no length-information in MetaData\n";
182 }
183 0 for my $line ( split /\r\n/, $header ) {
184 0 ▶ if ( $line =~ /^icy/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^icy/) 0 0 0
185 0 my ( $name, $value ) = $line =~ /(icy.*?):(.*)/i;
186 0 $self->{ '_' . $name } = $value;
187 0 ▶ $self->stationname($value) if $name eq 'icy-name';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "icy-name" 0 0 0
188 }
189 }
190 0 $self->set_state('MDNOSYNC');
191  
192 # header complete
193 # header ist in $header
194 }
195 else {
196 0 $self->{_tempHeader} = $in;
197 }
198 0 return 1;
199 }
200 0 ▶ if ( $self->get_state eq 'MDNOSYNC' ) { # metaData out of sync
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_state eq "MDNOSYNC") 0 0 0
201 #print "metadaten aus dem takt: " . $self->stationname() . "\n";
202 0 my $in;
203 0 my $restLength = 0;
204 0 my $tLength = $self->icySysread( $socket, $in, $self->{_metaLength} );
205 0 ▶ unless ( defined $tLength ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tLength) 0 0 0
206 0 return $self->disconnected();
207 }
208  
209 0 $self->{_audio} .= $in;
210 0 ▶ if ( $self->{_audio} =~ /Stream(.|\n)*\0/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_audio'} =~ /Stream(.|\n)*\0/i) { } 0 0 0
211 0 $self->{_audio} =~ /Stream/i;
212 0 my $lastMatch = $-[0];
213 0 my $stringPre = $`;
214 0 my $stringPost = $';
215  
216 0 my $lM = ord( chop($stringPre) ) * 16;
217 0 my $metaData = "Stream" . substr( $stringPost, 0, $lM - 6 );
218 0 my $mreturn = $self->processMetaData($metaData);
219 0 ▶ if ($mreturn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mreturn) 0 0 0
220 0 $self->set_state('MDSYNC');
221 }
222  
223 0 my $rest = substr( $stringPost, $lM - 6 );
224 0 $self->{_audio} = $stringPre;
225 0 $self->{_audio} .= $rest;
226 0 $restLength = $self->{_metaLength} - length($rest) + 1;
227 0 $self->process_chunk( $self->{_audio} );
228 }
229 else {
230 0 $self->{_audio} .= $in;
231 }
232 0 $self->{_restLength} = $restLength;
233 0 return 1;
234 }
235  
236 0 ▶ if ( $self->get_state eq 'MDSYNC' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_state eq "MDSYNC") 0 0 0
237 0 my $in;
238 0 ▶ $self->{_restLength} ||= ( $self->{_metaLength} + 1 );
Condition0%0/3
$self->{"_restLength"} ||= $self->{"_metaLength"} + 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_restLength"} ||= $self->{"_metaLength"} + 1
$self->{"_restLength"} $self->{"_metaLength"} + 1
Truth table0%0/3
A ||= B
A= $self->{"_restLength"}B= $self->{"_metaLength"} + 1
ABresult
1X1
011
000
239 my $realLength =
240 0 $self->icySysread( $socket, $in, $self->{_restLength} );
241 0 ▶ unless ( defined $realLength ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $realLength) 0 0 0
242 0 return $self->disconnected();
243 }
244  
245 0 $self->{_restLength} -= $realLength;
246  
247 0 ▶ if ( $self->{_restLength} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_restLength'} == 0) { } 0 0 0
248 0 my $l = chop($in);
249 0 ▶ $self->process_chunk($in) if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 0 0 0
250 0 ▶ $self->set_state('READMD') if ord($l);
Branch: 1 branch
BranchTrueFalseTotal
if ord $l 0 0 0
251 0 $self->{_newMetaDataLength} = $l;
252 }
253 else {
254 0 ▶ $self->process_chunk($in) if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 0 0 0
255 }
256 0 return 1;
257 }
258  
259 0 ▶ if ( $self->get_state eq 'READMD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_state eq "READMD") 0 0 0
260 0 my $in;
261 0 ▶ $self->{_newMetaDataLength} ||= 0;
Condition0%0/2
$self->{"_newMetaDataLength"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"_newMetaDataLength"} ||= 0
$self->{"_newMetaDataLength"}
Truth table0%0/2
A
A= $self->{"_newMetaDataLength"}
Aresult
11
00
262 0 ▶ $self->{_newMetaData} ||= '';
Condition0%0/2
$self->{"_newMetaData"} ||= ""
l!l
00
MC/DC0%0/1
$self->{"_newMetaData"} ||= ""
$self->{"_newMetaData"}
Truth table0%0/2
A
A= $self->{"_newMetaData"}
Aresult
11
00
263 my $lengthInBytes =
264 ( ord( $self->{_newMetaDataLength} ) * 16 ) -
265 0 length( $self->{_newMetaData} );
266 0 my $realLength = $self->icySysread( $socket, $in, $lengthInBytes );
267 0 ▶ unless ( defined $realLength ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $realLength) 0 0 0
268 0 return $self->disconnected();
269 }
270  
271 0 $self->{_newMetaData} .= $in;
272  
273 0 ▶ if ( $realLength < $lengthInBytes ) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($realLength < $lengthInBytes) 0 0 0
274  
275 0 my $metaData = 1;
276 $metaData = $self->processMetaData( $self->{_newMetaData} )
277 0 ▶ if $self->{_newMetaData};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_newMetaData"} 0 0 0
278 0 $self->{_newMetaData} = '';
279  
280 0 ▶ if ($metaData) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metaData) { } 0 0 0
281 0 $self->set_state('MDSYNC');
282 }
283 else {
284 0 $self->set_state('MDNOSYNC');
285  
286 }
287 0 return 1;
288 }
289 }
290  
291 =head2 connect
292  
293 Usage : $station->connect( $host, $port );
294 Purpose :
295 connects the station-object with the radio-station
296 Returns : 1 for 'ok',other values can be specified in the overloadable method 'disconnected'
297 Argument : host, port
298 Throws : nothing
299 See Also :
300  
301 =cut
302  
303 0 ▶sub connect {
Subroutine connect: not called CC=7 SCAR=40.3
Pod documented
304 0 my ( $self, $host, $port ) = @_;
305  
306 0 ▶ $self->host($host) if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 0 0 0
307 0 ▶ $self->port($port) if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 0 0 0
308  
309 0 ▶ croak "need more information to connect" unless $self->port && $self->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->port and $self->host 0 0 0
Condition0%0/3
$self->port and $self->host
!ll&&!rl&&r
000
MC/DC0%0/2
$self->port and $self->host
$self->port $self->host
Truth table0%0/3
A and B
A= $self->portB= $self->host
ABresult
0X0
100
111
310  
311 0 my $addr = sockaddr_in( $port, inet_aton($host) );
312 0 ▶ if ( $self->socket->connect($addr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->socket->connect($addr)) { } 0 0 0
313 0 $self->set_state('SENDHEADER');
314 0 return 1;
315 }
316 else {
317 0 ▶ if ( $! == EINPROGRESS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 115) { } 0 0 0
318 0 $self->set_state('CONNECT');
319 }
320 else {
321 0 $self->disconnected();
322 0 return 0;
323 }
324 }
325 0 return 1;
326 }
327 =pod
328 The following functions are overloadable:
329  
330 =head2 init
331  
332 Usage : init is called from new
333 Purpose :
334 Initializes some values, create socket
335 Always call SUPER::init if you overload this method.
336 Returns : nothing
337 Argument : nothing
338 Throws : nothing
339 See Also :
340  
341 =cut
342  
343 1 ▶sub init {
Subroutine init: called CC=3 SCAR=11.3
Pod documented
344 1 my $self = shift;
345  
346 1 $self->set_state('START');
347  
348 1 ▶ my $socket = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::INET"->new("Proto", "tcp", "Type", 1) or die $@ 1 0 1
349 Proto => 'tcp',
350 Type => SOCK_STREAM
351 )
352 or die $@;
353  
354 1 ▶ $socket->blocking( $self->blocking || 0 );
Condition50%1/2
$self->blocking || 0
l!l
01
MC/DC0%0/1
$self->blocking || 0
$self->blocking
Truth table50%1/2
A
A= $self->blocking
Aresult
11
00
355  
356 1 $self->socket($socket);
357 }
358  
359 =head2 process_chunk
360  
361 Usage : process_chunk is called from receive() for processing audio-data-chunks
362 Purpose :
363 overload it
364 Returns : nothing
365 Argument : audio-data
366 Throws : nothing
367 See Also :
368  
369 =cut
370  
371 0 ▶sub process_chunk {
Subroutine process_chunk: not called CC=1 SCAR=6.9
Pod documented
372 0 my ( $self, $chunk ) = @_;
373 }
374  
375 =head2 process_new_title
376  
377 Usage : process_new_title is called everytime the station sends a new song-title
378 Purpose :
379 overload it
380 Returns : nothing
381 Argument : new song title
382 Throws : nothing
383 See Also :
384  
385 =cut
386  
387 0 ▶sub process_new_title {
Subroutine process_new_title: not called CC=1 SCAR=6.9
Pod documented
388 0 my ( $self, $title ) = @_;
389 0 print $title, "\n";
390 }
391  
392 =head2 disconnected
393  
394 Usage : is called when there is a write error on a socket
395 Purpose :
396 overload it.
397 The return value of this method is the value the failed method will return.
398 If you can 'fix' the error in this method you normaly return 1, otherwise 0.
399 You can also change the behaviour of Netx::WebRadio for a '0' return value.
400 Returns : what you want
401 Argument : nothing
402 Throws : nothing
403 See Also :
404  
405 =cut
406  
407 0 ▶sub disconnected {
Subroutine disconnected: not called CC=1 SCAR=6.9
Pod documented
408 0 my $self = shift;
409 0 warn "disconnected " . (caller)[0] . " " . (caller)[2] . "\n";
410 0 return 0;
411 }
412  
413 =head1 BUGS
414  
415 =over 2
416  
417 =item 1
418 doesn't work under Win32
419  
420 =item 2
421 only works with stations that transmit metdata
422  
423 =back
424  
425 =head1 SUPPORT
426  
427  
428  
429 =head1 AUTHOR
430  
431 Nathanael Obermayer
432 CPAN ID: nathanael
433 natom-pause@smi2le.net
434  
435 =head1 COPYRIGHT
436  
437 This program is free software; you can redistribute
438 it and/or modify it under the same terms as Perl itself.
439  
440 The full text of the license can be found in the
441 LICENSE file included with this module.
442  
443  
444 =head1 SEE ALSO
445  
446 perl(1).
447 Netx::WebRadio
448  
449 =cut
450  
451 1 ▶sub set_state {
Subroutine set_state: called CC=1 SCAR=0.0
Pod undocumented
452 1 my ( $self, $state ) = @_;
453 1 $self->{_state} = $state;
454 1 $self->pollmode( $Netx::WebRadio::Station::Shoutcast::pollmodes{$state} );
455 }
456  
457 0 ▶sub get_state {
Subroutine get_state: not called CC=1 SCAR=6.9
Pod undocumented
458 0 my $self = shift;
459 0 return $self->{_state};
460 }
461  
462 0 ▶sub icySysread {
Subroutine icySysread: not called CC=3 SCAR=24.8
Pod undocumented
463 0 my ( $self, $socket, $in, $length ) = @_;
464 0 my $ret = sysread( $socket, $_[2], $length );
465 0 ▶ unless ( defined $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ret) 0 0 0
466 0 ▶ if ( $! == EAGAIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 11) 0 0 0
467 0 return 0;
468 }
469 0 return undef;
470 }
471 0 return $ret;
472 }
473  
474 0 ▶sub processMetaData {
Subroutine processMetaData: not called CC=2 SCAR=17.9
Pod undocumented
475 0 my ( $self, $text ) = @_;
476 0 my ($title) = $text =~ /StreamTitle='(.*?)'/i;
477 0 ▶ return 0 unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
478 0 $self->process_new_title($title);
479 0 return 1;
480 }
481  
482 0 ▶sub icySyswrite {
Subroutine icySyswrite: not called CC=1 SCAR=6.9
Pod undocumented
483 0 my ($self, $socket, $string) = @_;
484 0 return syswrite($socket, $string);
485 }
486  
487 1 1; #this line is important and will help the module return a true value
488 __END__
↑ Summary WebRadio.pm »