lib/Video/PlaybackMachine/Player.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Video/PlaybackMachine/Schema/Result/ScheduleEntry.pm ↑ Summary lib/Video/PlaybackMachine.pm »
1 package Video::PlaybackMachine::Player;
2  
3 our $VERSION = '0.09'; # VERSION
4  
5 ####
6 #### Video::PlaybackMachine::Player
7 ####
8 #### A POE::Session which displays movies and still frames onscreen
9 #### based on events.
10 ####
11  
12 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 our @EXPORT_OK = qw(PLAYER_STATUS_STOP PLAYER_STATUS_PLAY PLAYER_STATUS_STILL
16 PLAYBACK_OK PLAYBACK_ERROR PLAYBACK_STOPPED);
17  
18 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use X11::FullScreen;
Subroutine BEGIN: called CC=1 SCAR=2.6
20 use Video::Xine;
21 use Video::Xine::Stream qw/:status_constants/;
22 use Video::PlaybackMachine::EventWheel::FullScreen;
23 use Video::PlaybackMachine::Player::EventWheel;
24 use Video::PlaybackMachine::Config;
25 use Video::Xine::Util 'make_x11_fs_visual';
26 use Carp;
27  
28 with 'Video::PlaybackMachine::Logger';
29  
30 ############################# Class Constants ################################
31  
32 ## Status codes Xine will report
33 use constant PLAYER_STATUS_STOP => 0;
34 use constant PLAYER_STATUS_PLAY => 1;
35  
36 ## How-the-movie-played status codes
37  
38 # OK == played through and stopped at the end
39 use constant PLAYBACK_OK => 1;
40  
41 # ERROR == problem in trying to play
42 use constant PLAYBACK_ERROR => 2;
43  
44 ## Types of playback
45 use constant PLAYBACK_TYPE_MUSIC => 0;
46 use constant PLAYBACK_TYPE_MOVIE => 1;
47  
48 ############################## Class Methods #################################
49  
50 ##
51 ## new()
52 ##
53 ## Returns a new instance of Player. Note that the session is not created
54 ## until you call spawn().
55 ##
56  
57 has 'xine' => ( is => 'lazy' );
58  
59 has 'fullscreen' => (
60 is => 'lazy',
61 isa => sub { $_[0]->isa('X11::FullScreen') or die; },
62 handles => [ 'window' ]
63 );
64  
65 has 'x_display' => (
66 is => 'ro',
67 default => Video::PlaybackMachine::Config->config()->x_display()
68 );
69  
70 has 'xine_vo' => ( is => 'lazy' );
71  
72 has 'stream' => ( is => 'lazy' );
73  
74 has 'playback_type' => ( is => 'rw' );
75  
76 ############################## Session Methods ###############################
77  
78 sub _build_xine {
79 my $self = shift;
80  
81 return Video::Xine->new();
82 }
83  
84 sub _build_fullscreen {
85 my $self = shift;
86  
87 my $fullscreen = X11::FullScreen->new( $self->x_display() );
88 $fullscreen->show();
89 return $fullscreen;
90  
91 }
92  
93 sub _build_xine_vo {
94 my $self = shift;
95
96 my $x11_visual =
97 Video::Xine::Util::make_x11_fs_visual($self->fullscreen);
98  
99 my $driver =
100 Video::Xine::Driver::Video->new( $self->xine, "auto", 1, $x11_visual,
101 $self->fullscreen() );
102  
103 return $driver;
104 }
105  
106 sub _build_stream {
107 my $self = shift;
108  
109 my $stream = $self->xine()->stream_new( undef, $self->xine_vo() )
110 or croak "Unable to open video stream";
111  
112 return $stream;
113 }
114  
115 ##
116 ## On session start, initializes Xine and prepares it to start playing.
117 ##
118 sub _start {
119 my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
120  
121 $kernel->alias_set('Player');
122  
123 $self->fullscreen()->sync();
124  
125 $heap->{'stream_queue'} = Video::PlaybackMachine::Player::EventWheel->new(
126 {
127 'stream' => $self->stream()
128 }
129 );
130  
131 my $fq = Video::PlaybackMachine::EventWheel::FullScreen->new(
132 'source' => $self->fullscreen()
133 );
134  
135 $fq->set_expose_handler(
136 sub {
137 $self->stream()->get_video_port()
138 ->send_gui_data( XINE_GUI_SEND_EXPOSE_EVENT, $_[1] );
139 }
140 );
141 $fq->spawn();
142  
143 $heap->{'fullscreen_queue'} = $fq;
144  
145 return;
146  
147 }
148  
149 ##
150 ## Responds to a 'play' request by playing a movie on Xine.
151 ##
152 ## Arguments:
153 ##
154 ## ARG0: $postback -- what to call after the play is completed
155 ## ARG1: $offset -- number of seconds after the movie's start to begin
156 ## ARG2: @filenames -- ARG1 onward contains the files to play, in order.
157 ##
158 ## After Xine is started, we'll check on it every $XINE_CHECK_INTERVAL
159 ## seconds to see if it has stopped.
160 ##
161 sub play {
162 my ( $kernel, $self, $heap, $postback, $offset, @files ) =
163 @_[ KERNEL, OBJECT, HEAP, ARG0, ARG1, ARG2 .. $#_ ];
164  
165 defined $offset or $offset = 0;
166  
167 my $log = $self->logger();
168  
169 # TODO Remove fatal
170 @files or die "No files specified! stopped";
171  
172 # Stop if we're playing
173 if ( $self->stream()->get_status() == XINE_STATUS_PLAY ) {
174 $self->stream()->stop();
175 $self->stream()->close();
176 }
177  
178 # Clear out any previous events
179 $heap->{'stream_queue'}->clear_events();
180  
181 # Clear the screen
182 $self->clear_window();
183  
184 $log->info("Playing $files[0]");
185  
186 my $s = $self->stream();
187  
188 $s->open( $files[0] )
189 or do {
190 $log->error( "Unable to open '$files[0]': Error " . $s->get_error() );
191 $postback->(PLAYBACK_ERROR);
192 return;
193 };
194  
195 $s->play( 0, $offset * 1000 )
196 or do {
197 $log->error( "Unable to play '$files[0]': Error " . $s->get_error() );
198 $postback->(PLAYBACK_ERROR);
199 return;
200 };
201  
202 # Tell the system to refresh the window
203 # Drawable changed
204 $s->get_video_port()
205 ->send_gui_data( XINE_GUI_SEND_DRAWABLE_CHANGED, $self->window() );
206 $s->get_video_port()->send_gui_data( XINE_GUI_SEND_VIDEOWIN_VISIBLE, 1 );
207  
208 # Spawn a watcher to call the postback after the fact
209 $heap->{'stream_queue'}->set_stop_handler($postback);
210 $heap->{'stream_queue'}->spawn();
211  
212 $self->playback_type(PLAYBACK_TYPE_MOVIE);
213  
214 }
215  
216 ##
217 ## stop()
218 ##
219 ## Stops the currently-playing movie.
220 ##
221 sub stop {
222 my ($self) = $_[OBJECT];
223  
224 # Stop if we're playing
225 if ( $self->stream()->get_status() == XINE_STATUS_PLAY ) {
226 $self->stream()->stop();
227 }
228  
229 $self->clear_window();
230  
231 }
232  
233 sub clear_window {
234 my $self = shift;
235  
236 $self->fullscreen()->clear();
237  
238 return;
239 }
240  
241 ##
242 ## play_still()
243 ##
244 ## Arguments:
245 ## STILL_FILE: Filename of our stillstore.
246 ##
247 ## Responds to a 'play_still' request by playing a still frame. The
248 ## stillframe will remain there until something replaces it.
249 ##
250 sub play_still {
251 my ( $self, $kernel, $heap, $still, $callback, $time ) =
252 @_[ OBJECT, KERNEL, HEAP, ARG0, ARG1, ARG2 ];
253 my $log = $self->logger();
254 if ( $self->stream()->get_status() == XINE_STATUS_PLAY
255 && $self->playback_type() == PLAYBACK_TYPE_MOVIE )
256 {
257 $log->error("Attempted to show still '$still' while playing a movie");
258 return;
259 }
260 $log->debug("Showing still '$still'");
261 eval {
262 # Clear the screen
263 $self->clear_window();
264  
265 $self->fullscreen()->display_still( $still );
266 };
267 if ($@) {
268 $log->error("Error displaying still '$still': $@");
269 $callback->(PLAYBACK_ERROR) if $callback;
270 return;
271 }
272  
273 if ( defined $time ) {
274 POE::Session->create(
275 inline_states => {
276 _start => sub {
277 $_[KERNEL]->delay( 'end_delay', $time );
278 },
279 end_delay => sub {
280 $log->debug("Still playback finished for '$still'");
281 $callback->( $still, PLAYBACK_OK );
282 }
283 }
284 );
285 }
286  
287 }
288  
289 ##
290 ## play_music()
291 ##
292 ## Arguments:
293 ## ARG0 -- callback. What to call when the music's over.
294 ## ARG1 -- song file. Filename of the song to play.
295 ##
296 ## Responds to a 'play_music' request by playing a particular song.
297 ## Logs a warning and does nothing if we tried to play music during a
298 ## movie. If a song was already playing, lets it play, but substitutes
299 ## the current callback.
300 ##
301 sub play_music {
302 my ( $self, $heap, $kernel, $callback, $song_file ) =
303 @_[ OBJECT, HEAP, KERNEL, ARG0, ARG1 ];
304  
305 defined $callback or die "Must define callback!\n";
306  
307 defined $song_file or die "Must define song file!\n";
308  
309 # If there's a movie running, let it play
310 if ( $self->get_status() == PLAYER_STATUS_PLAY ) {
311 if ( $self->playback_type() == PLAYBACK_TYPE_MOVIE ) {
312 $self->warn(
313 "Attempted to play '$song_file' while a movie is playing");
314 $callback->( $self->stream(), PLAYBACK_ERROR );
315 return;
316 }
317 else {
318 $heap->{'stream_queue'}->set_stop_handler($callback);
319 }
320 }
321 else {
322 $self->debug("Playing music file '$song_file'");
323 $heap->{'stream_queue'}->clear_events();
324 $self->stream()->open($song_file)
325 or do {
326 $self->warn("Unable to play '$song_file'");
327 $callback->( $self->stream(), PLAYBACK_ERROR );
328 return;
329 };
330 $self->stream()->play( 0, 0 );
331 $heap->{'stream_queue'}->set_stop_handler($callback);
332 $heap->{'stream_queue'}->spawn();
333 $self->playback_type(PLAYBACK_TYPE_MUSIC);
334 }
335 }
336  
337 ############################## Object Methods ################################
338  
339 ##
340 ## spawn()
341 ##
342 ## Creates the appropriate Player session.
343 ##
344 sub spawn {
345 my $self = shift;
346  
347 POE::Session->create(
348 object_states => [
349 $self => [
350 qw(_start
351 play
352 play_still
353 play_music
354 stop
355 )
356 ],
357 ],
358 );
359  
360 }
361  
362 ##
363 ## get_status()
364 ##
365 ## Returns one of:
366 ## PLAYER_STATUS_PLAY if a movie (or music) is playing
367 ## PLAYER_STATUS_STOP if nothing is playing.
368 ##
369 sub get_status {
370 my $self = shift;
371  
372 my $session = $poe_kernel->get_active_session();
373 my $heap = $session->get_heap();
374  
375 $self->stream()->get_status() == XINE_STATUS_PLAY
376 and return PLAYER_STATUS_PLAY;
377  
378 return PLAYER_STATUS_STOP;
379 }
380  
381 no Moo;
382  
383 1;
384  
385 __END__
386  
387 =head1 NAME
388  
389 Video::PlaybackMachine::Player - POE component to play movies
390  
391 =head1 SYNOPSIS
392  
393 use Video::PlaybackMachine::Player;
394  
395 my $player = Video::PlaybackMachine::Player->new();
396  
397 # Start the Player session
398 $player->spawn();
399  
400 # Then, in another session...
401 $kernel->post('Player', 'play', sub { "Finished"; }, 0, 'mymovie.mp4');
402  
403 # Is the movie still running?
404 print "Playing\n" if $player->get_status() == PLAYER_STATUS_PLAY;
405  
406  
407 =cut
« lib/Video/PlaybackMachine/Schema/Result/ScheduleEntry.pm ↑ Summary lib/Video/PlaybackMachine.pm »