File Coverage

blib/lib/FLV/AMFWriter.pm
Criterion Covered Total %
statement 36 36 100.0
branch 4 4 100.0
condition 7 9 77.7
subroutine 10 10 100.0
pod 2 2 100.0
total 59 61 96.7


line stmt bran cond sub pod time code
1             package FLV::AMFWriter;
2              
3 6     6   34 use warnings;
  6         13  
  6         174  
4 6     6   31 use strict;
  6         14  
  6         180  
5 6     6   166 use 5.008;
  6         21  
  6         247  
6              
7 6     6   36 use AMF::Perl::Util::Object;
  6         10  
  6         146  
8 6     6   4999 use AMF::Perl::IO::OutputStream;
  6         3531  
  6         173  
9 6     6   36 use base 'AMF::Perl::IO::Serializer';
  6         15  
  6         5739  
10              
11             our $VERSION = '0.24';
12              
13             =for stopwords AMF Remoting
14              
15             =head1 NAME
16              
17             FLV::AMFWriter - Wrapper for the AMF::Perl serializer
18              
19             =head1 LICENSE
20              
21             See L
22              
23             =head1 METHODS
24              
25             This is a subclass of AMF::Perl::IO::Deserializer.
26              
27             That class is optimized for Flash Remoting communications. We are
28             instead just interested in the protocol for the data payload of those
29             messages, since that's all that FLV carries.
30              
31             So, this class is a hack. As of this writing AMF::Perl was at v0.15,
32             which lacked support for hashes. So, we hack that in. Hopefully we
33             did it in a future-friendly way...
34              
35             =over
36              
37             =item $pkg->new($content)
38              
39             Creates a minimal AMF::Perl::IO::Serializer instance.
40              
41             =cut
42              
43             sub new
44             {
45 38     38 1 84 my $pkg = shift;
46              
47 38         381 return $pkg->SUPER::new(AMF::Perl::IO::OutputStream->new());
48             }
49              
50             =item $self->write_flv_meta(@data)
51              
52             Returns a byte string of serialized data
53              
54             =cut
55              
56             sub write_flv_meta
57             {
58 38     38 1 271 my ($self, @data) = @_;
59              
60 38         98 for my $d (@data)
61             {
62 76         234 $self->writeData($d);
63             }
64 38         176 return $self->{out}->flush();
65             }
66              
67             =item $self->writeMixedArray()
68              
69             Serializes a hashref.
70              
71             This is a workaround for versions of AMF::Perl which did not handle
72             hashes (namely v0.15 and earlier). This method is only installed if a
73             method of the same name does not exist in the superclass.
74              
75             This should be removed when a newer release of AMF::Perl is available.
76              
77             =item $self->writeData($datum)
78              
79             This is a minimal override of writeData() in the superclass to add
80             support for mixed arrays (aka hashes).
81              
82             As above, it is only installed if AMF::Perl::IO::Serializer lacks a
83             writeMixedArray() method.
84              
85             =cut
86              
87             if (!__PACKAGE__->can('writeMixedArray'))
88             {
89             *writeMixedArray = sub {
90 64     64   107 my ($self, $d) = @_;
91              
92 64         212 $self->{out}->writeByte(8); # type code
93 64         468 $self->{out}->writeLong(0); # length, bogus value...
94 64         557 $self->writeObject($d);
95 64         920 return;
96             };
97              
98             *writeData = sub {
99 1186     1186   16897 my ($self, $d, $type) = @_;
100              
101 1186 100 66     6326 if (!$type && (ref $d) && (ref $d) =~ m/HASH/xms)
      100        
102             {
103 64         109 $type = 'mixedarray';
104             }
105              
106 1186 100 66     3396 if ($type && 'mixedarray' eq $type)
107             {
108 64         193 $self->writeMixedArray($d);
109             }
110             else
111             {
112 1122         3709 $self->SUPER::writeData($d, $type);
113             }
114 1186         66764 return;
115             };
116             }
117              
118             1;
119              
120             __END__