PageRenderTime 43ms CodeModel.GetById 12ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Data/Interleave/IcecastMetadata.pm

http://github.com/motemen/Teto
Perl | 61 lines | 48 code | 13 blank | 0 comment | 3 complexity | 3b65c616108b8e1ea7d3833bea4ee3ec MD5 | raw file
 1package Data::Interleave::IcecastMetadata;
 2use Mouse;
 3use POSIX;
 4
 5has interval => (
 6    is  => 'rw',
 7    isa => 'Int',
 8    default => 16 * 1024,
 9);
10
11has pos => (
12    is  => 'rw',
13    isa => 'Int',
14    lazy => 1,
15    default => sub { $_[0]->interval },
16);
17
18has position => (
19    is  => 'rw',
20    isa => 'Int',
21    default => 0,
22);
23
24has metadata => (
25    is  => 'rw',
26    isa => 'HashRef',
27    default => sub { +{} },
28);
29
30__PACKAGE__->meta->make_immutable;
31
32no Mouse;
33
34sub interleave {
35    my ($self, $data) = @_;
36
37    my $interleaved = '';
38
39    my $title = $self->metadata->{title} || '';
40    utf8::encode $title if utf8::is_utf8 $title;
41    my $meta = qq(StreamTitle='$title';);
42    my $len  = ceil(length($meta) / 16);
43    $meta = chr($len) . $meta . ("\x00" x (16 * $len - length $meta));
44
45    while (length($data) >= $self->pos) {
46        $interleaved .= substr $data, 0, $self->pos, '';
47        $interleaved .= $meta;
48        $self->reset_position;
49    }
50    $interleaved .= $data;
51    $self->{pos} -= length $data;
52
53    return $interleaved;
54}
55
56sub reset_position {
57    my $self = shift;
58    $self->pos($self->interval);
59}
60
611;