File Coverage

blib/lib/WWW/SVT/Play/Video/Stream.pm
Criterion Covered Total %
statement 47 62 75.8
branch 6 12 50.0
condition 1 2 50.0
subroutine 14 20 70.0
pod 10 10 100.0
total 78 106 73.5


line stmt bran cond sub pod time code
1             package WWW::SVT::Play::Video::Stream;
2              
3             # Copyright (c) 2012 - Olof Johansson
4             # All rights reserved.
5             #
6             # This program is free software; you can redistribute it and/or
7             # modify it under the same terms as Perl itself.
8              
9             =head1 NAME
10              
11             WWW::SVT::Play::Video::Stream, base class representing a stream
12              
13             =head1 SYNOPSIS
14              
15             use WWW::SVT::Play::Video;
16              
17             my $svtp = WWW::SVT::Play::Video->new($url);
18             my $stream = $svtp->stream(protocol => 'HDS');
19              
20              
21             use WWW::SVT::Play::Video::Stream;
22              
23             # get a flashvar json blob, JSON decode it and feed it to ->from_json:
24             my $svtp_stream = WWW::SVT::Play::Video::Stream->from_json($json);
25              
26             =head1 DESCRIPTION
27              
28             This module is responsible for determining the type of stream
29             object that should be created for each stream.
30              
31             =cut
32              
33 1     1   6 use warnings FATAL => 'all';
  1         2  
  1         36  
34 1     1   5 use strict;
  1         2  
  1         38  
35              
36             our $VERSION = 0.12;
37 1     1   5 use Carp;
  1         1  
  1         59  
38              
39 1     1   1553 use WWW::SVT::Play::Utils qw(playertype_map);
  1         3  
  1         73  
40 1     1   189455 use URI;
  1         5442  
  1         39  
41              
42 1     1   874 use WWW::SVT::Play::Video::Stream::HLS;
  1         3  
  1         36  
43 1     1   717 use WWW::SVT::Play::Video::Stream::HDS;
  1         3  
  1         32  
44 1     1   1027 use WWW::SVT::Play::Video::Stream::RTMP;
  1         3  
  1         35  
45 1     1   796 use WWW::SVT::Play::Video::Stream::HTTP;
  1         2  
  1         31  
46              
47 1     1   6 use Data::Dumper;
  1         1  
  1         802  
48              
49             $Data::Dumper::Indent = 1;
50              
51             =head1 CONSTRUCTOR
52              
53             =head2 new
54              
55             Takes the following named parameters for setting attributes:
56              
57             =over
58              
59             =item * url
60              
61             =item * type
62              
63             =back
64              
65             And in some cases, other protocol specific attributes..
66              
67             =cut
68              
69             sub new {
70 10     10 1 14 my $class = shift;
71 10         71 bless { @_ }, $class;
72             }
73              
74             =head2 from_json
75              
76             Wrapper around the constructor; can be fed a videoReference
77             element of the SVT Play JSON blob and return an object
78             representing that stream.
79              
80             =cut
81              
82             sub from_json {
83 10     10 1 16 my $class = shift;
84 10         18 my $json = shift;
85              
86 10         50 my $uri = URI->new($json->{url});
87              
88 10         593 my $type;
89 10 50       39 if (lc $uri->scheme eq 'rtmp') {
90 0 0       0 $type = 'rtmp' if lc $uri->scheme =~ /^rtmpe?$/;
91             } else {
92 10   50     235 $type = playertype_map($json->{playerType}) // '';
93             }
94              
95 10 50       34 if ($type eq 'rtmp') {
96 0         0 return WWW::SVT::Play::Video::Stream::RTMP->new(
97             type => $type,
98             url => $json->{url},
99             bitrate => $json->{bitrate},
100             );
101             }
102              
103 10 100       25 if ($type eq 'hls') {
104 5         39 return WWW::SVT::Play::Video::Stream::HLS->new(
105             type => $type,
106             url => $json->{url},
107             );
108             }
109              
110 5 50       17 if ($type eq 'http') {
111 5 50       28 if ($json->{url} =~ m#/manifest\.f4m$#) {
112 5         49 return WWW::SVT::Play::Video::Stream::HDS->new(
113             type => 'hds',
114             url => $json->{url},
115             );
116             } else {
117 0         0 return WWW::SVT::Play::Video::Stream::HTTP->new(
118             type => $type,
119             url => $json->{url},
120             );
121             }
122             }
123              
124 0         0 return WWW::SVT::Play::Video::Stream->new(
125             type => $json->{playerType},
126             url => $json->{url},
127             );
128             }
129              
130             =head1 METHODS
131              
132             =head2 url
133              
134             Return the url of the stream.
135              
136             =cut
137              
138             sub url {
139 0     0 1 0 my $self = shift;
140 0         0 return $self->{url};
141             }
142              
143             =head2 type
144              
145             Return the protocol type of the stream (e.g. hds, hls, rtmp).
146              
147             =cut
148              
149             sub type {
150 20     20 1 21 my $self = shift;
151 20         84 return $self->{type};
152             }
153              
154             =head2 is_hls
155              
156             Is stream using HLS protocol? Should be overriden.
157              
158             =cut
159              
160 0     0 1 0 sub is_hls { 0 }
161              
162             =head2 is_hds
163              
164             Is stream using HDS protocol? Should be overriden.
165              
166             =cut
167              
168 0     0 1 0 sub is_hds { 0 }
169              
170             =head2 is_rtmp
171              
172             Is stream using RTMP protocol? Should be overriden.
173              
174             =cut
175              
176 10     10 1 36 sub is_rtmp { 0 }
177              
178             =head2 is_http
179              
180             Is stream using HTTP protocol? Should be overriden.
181              
182             =cut
183              
184 0     0 1   sub is_http { 0 }
185              
186             =head2 stream
187              
188             This is a default noop stream handler. This method is meant to be
189             called when the user wants to stream the stream using a media
190             player or similar. It should be overriden with a protocol capable
191             handler.
192              
193             =cut
194              
195             sub stream {
196 0     0 1   my $self = shift;
197 0           carp "No stream handler defined for the $self->{type} protocol.";
198 0           carp "Can't play stream."
199             }
200              
201             =head2 download
202              
203             This is a default noop download handler. This method is meant to
204             be called when the user wants to download the stream. It should
205             be overriden with a protocol capable handler.
206              
207             =cut
208              
209             sub download {
210 0     0 1   my $self = shift;
211 0           carp "No download handler defined for the $self->{type} protocol.";
212 0           carp "Can't download stream."
213             }
214              
215             =head1 COPYRIGHT
216              
217             Copyright (c) 2012 - Olof Johansson
218             All rights reserved.
219              
220             This program is free software; you can redistribute it and/or
221             modify it under the same terms as Perl itself.
222              
223             =cut
224              
225             1;