lib/Video/PlaybackMachine/FillProducer/SlideShow.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 109 CC 3 SCAR 10.4
CC
3
Cov
0%
show_slide
6.0
song_done
6.0
_build_time_layout
2.0
CRAP 2.8
1 package Video::PlaybackMachine::FillProducer::SlideShow;
2  
3 our $VERSION = '0.09'; # VERSION
4  
5 ####
6 #### Video::PlaybackMachine::FillProducer::SlideShow
7 ####
8 #### $Revision$
9 ####
10 #### Plays a bunch of random photos. Since we need to do things
11 #### at particular delay times, launches its own POE session.
12 ####
13  
14 use Moo;
15  
16 use Carp;
17 use POE;
18 use Log::Log4perl;
19  
20 use Video::PlaybackMachine::TimeLayout::GranularTimeLayout;
21 use Video::PlaybackMachine::Player qw(PLAYBACK_OK PLAYBACK_STOPPED);
22 use Video::PlaybackMachine::FillProducer::Chooser;
23  
24 ############################# Parameters #############################
25  
26 has 'max_slides' => (
27 'is' => 'ro',
28 'default' => 5
29 );
30  
31 has 'frame_chooser' => (
32 is => 'lazy'
33 );
34  
35 has 'time' => (
36 is => 'ro',
37 required => 1
38 );
39  
40 has 'time_layout' => (
41 is => 'lazy',
42 );
43  
44 has 'directory' => (
45 is => 'ro',
46 required => 1
47 );
48  
49 has 'music_directory' => (
50 is => 'ro',
51 required => 1
52 );
53  
54 has 'music_chooser' => (
55 is => 'lazy'
56 );
57  
58 with 'Video::PlaybackMachine::FillProducer', 'Video::PlaybackMachine::Logger';
59  
60  
61 ############################## Class Methods ##############################
62  
63  
64 ############################# Object Methods ##############################
65  
66 sub _build_time_layout {
67 my $self = shift;
68  
69 return Video::PlaybackMachine::TimeLayout::GranularTimeLayout->new(
70 $self->time(), $self->max_slides()
71 );
72 }
73  
74 sub _build_frame_chooser {
75 my $self = shift;
76
77 return Video::PlaybackMachine::FillProducer::Chooser->new(
78 DIRECTORY => $self->directory(),
79 );
80  
81 }
82  
83 sub _build_music_chooser {
84 my $self = shift;
85
86 return Video::PlaybackMachine::FillProducer::Chooser->new(
87 DIRECTORY => $self->music_directory(),
88 FILTER => qr/\.(mp3|wav|ogg)$/
89 ),
90  
91 }
92  
93 ##
94 ## has_audio()
95 ##
96 ## The slide show provides an audio track.
97 ##
98 sub has_audio { return 1; }
99  
100 ##
101 ## Slideshow is available if the directory exists
102 ## and there are images in it.
103 ##
104 sub is_available
105 {
106 my $self = shift;
107  
108 return $self->frame_chooser()->is_available();
109  
110 }
111  
112 ##
113 ## show_slide()
114 ##
115 ## Displays a set of random still frames.
116 ##
117 sub show_slide
118 {
119 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
120  
121 # If we have enough time to play another slide, call the player
122 # to play it.
123 my $time_played = ( time() - $heap->{'slide_start_time'} );
124 if ( $heap->{planned_time} > $time_played )
125 {
126 my $frame = $self->{'frame_chooser'}->choose();
127 $kernel->post( 'Player', 'play_still', $frame );
128 $kernel->delay( 'show_slide', $self->{'time'} );
129 }
130  
131 # Otherwise, cancel all slides and shut things down.
132 # (The alarm cancel should be redundant.)
133 else
134 {
135 $self->debug(
136 "Shutting down slideshow (time left=$time_played, $heap->{planned_time})"
137 );
138 $kernel->alarm_remove('show_slide');
139 $kernel->state('show_slide');
140 $kernel->state('next_song');
141 $kernel->alarm_remove('next_song');
142 $kernel->state('song_done');
143 $kernel->alarm_remove('song_done');
144 delete $heap->{'slide_start_time'};
145 delete $heap->{'planned_time'};
146  
147 $kernel->yield('next_fill');
148 }
149  
150 }
151  
152 ##
153 ## start()
154 ##
155 ## Starts the display of random still frames. Adds event handlers to
156 ## the current session to show slides. Assumes that we're being called
157 ## in a POE Filler session.
158 ##
159 sub start
160 {
161 my $self = shift;
162 my ($planned_time) = @_;
163  
164 $self->debug("Starting slideshow");
165 my $heap = $poe_kernel->get_active_session->get_heap();
166 $heap->{'slide_start_time'} = time();
167 $heap->{'planned_time'} = $planned_time;
168 $poe_kernel->state( 'show_slide', $self );
169 $poe_kernel->state( 'next_song', $self );
170 $poe_kernel->state( 'song_done', $self );
171 $poe_kernel->yield('show_slide');
172 $poe_kernel->yield('next_song');
173 }
174  
175 sub next_song
176 {
177 $_[OBJECT]->debug("Running next song");
178 $_[KERNEL]->post(
179 'Player', 'play_music',
180 $_[SESSION]->postback('song_done'),
181 $_[OBJECT]->music_chooser()->choose()
182 );
183 }
184  
185 sub song_done
186 {
187 $_[OBJECT]->debug("Song done");
188 my ( $stream, $status ) = @{ $_[ARG1] };
189 if ( $status == PLAYBACK_OK() )
190 {
191 $_[OBJECT]->debug("Returned OK, playing next song");
192 $_[KERNEL]->yield('next_song');
193 }
194 else
195 {
196 $_[OBJECT]->debug("'$status' Not OK, stopping");
197 $_[KERNEL]->alarm('next_song');
198 }
199 }
200  
201 no Moo;
202  
203 1;
204