lib/Video/PlaybackMachine/TimeManager.pm

filter: Statementstmt 97.7% 44 / 45 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 2 totaltotal 91.8% 56 / 61 CC 6 SCAR 9.0
CC
6
Cov
96%
_seconds_remaining
4.0
get_segment
3.0
__ANON__
2.0
CRAP 2.5
« lib/Video/PlaybackMachine/FillProducer/StillFrame.pm ↑ Summary lib/Video/PlaybackMachine/EventWheel/FullScreen.pm »
1 package Video::PlaybackMachine::TimeManager;
2  
3 2 our $VERSION = '0.09'; # VERSION
4  
5 ####
6 #### Video::PlaybackMachine::TimeManager
7 ####
8 #### $Revision$
9 ####
10 #### Tells us what FillSegment to play next in a particular break.
11 #### Keeps track of what has been played.
12 ####
13  
14 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 # TODO: I'm not sure that the implemented algorithm is correct. To get
20 # the effects I'm looking for, basically the TimeManager should go
21 # through in priority order and assign time slots to the Fill
22 # Producers based on minimum and preferred time, stopping when we're
23 # full. Actual playback then follows sequence order. Preassignment
24 # may be against some of the PM philosophy of constantly reacting to
25 # the current situation, but is essential for getting some of the
26 # effects that we want (such as "play fills before and after a short
27 # movie, and ALWAYS do station identification")
28 #
29 # Later update: The algorithm actually works as well, if not better,
30 # than the above. It gets the amount of time remaining, and the amount
31 # of time required, and the amount of time required by items of a
32 # higher priority, and computes the remainder.
33 #
34  
35 ############################# Attributes #############################
36  
37 2 has 'current_seq' => (
38 is => 'rw',
39 default => 0
40 );
41  
42 has 'seq_order' => (
43 is => 'ro',
44 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 2 );
46  
47 2 with 'Video::PlaybackMachine::Logger';
48  
49 ############################## Class Methods ##############################
50  
51 3 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod undocumented
52 3 my $type = shift;
53 3 my (@segments) = @_;
54  
55 return {
56 3 seq_order => [ sort { $a->sequence_order() <=> $b->sequence_order() } @segments ]
57 };
58 }
59  
60 ############################# Object Methods ##############################
61  
62 ##
63 ## get_segment()
64 ##
65 ## Arguments:
66 ## TIME_LEFT: int -- Amount of time before next scheduled content
67 ##
68 ## Returns: (list)
69 ## FillSegment -- Next FillSegment to use
70 ## int -- unreserved time remaining in seconds
71 ##
72 ## Returns the FillSegment to use to fill up time right now. Returns
73 ## undef if no more fill can be played.
74 ##
75  
76 # TODO: Can be simplified to a rotating index. We never play out of
77 # sequence.
78 8 ▶sub get_segment {
Subroutine get_segment: called CC=3 SCAR=11.0
Pod undocumented
79 8 my $self = shift;
80 8 my ($time_left) = @_;
81
82 # For each segment starting from current in display order
83 8 foreach my $segment ( $self->_segments_left() ) {
84  
85 11 $self->debug("Considering segment ", $segment->name(), " with time left $time_left");
86  
87 # Move to next segment if we don't have time to play it
88 11 my $time_remaining = $self->_seconds_remaining($segment, $time_left);
89 11 ▶ $segment->is_available($time_remaining) or do {
Branch: 1 branch
BranchTrueFalseTotal
$segment->is_available($time_remaining) or do { $self->debug("Segment ", $segment->name, " is not available"); next } 6 5 11
90 5 $self->debug("Segment ", $segment->name(), " is not available");
91 5 next;
92 };
93  
94 # Update whatever we should play next
95 $self->current_seq(
96 6 ($self->current_seq + 1) % (scalar @{ $self->seq_order } )
97 );
98  
99 # Return the segment and time remaining
100 6 return ($segment, $time_remaining);
101  
102 } # End for each unfinished segment
103  
104 # All's finished or no time; return undef
105 2 return;
106 }
107  
108 ##
109 ## Returns the segments still to play, in sequence order.
110 ##
111 19 ▶sub _segments_left {
Subroutine _segments_left: called CC=1 SCAR=0.0
112 19 my $self = shift;
113 19 return @{ $self->seq_order }[ $self->{current_seq} ... $#{ $self->{seq_order} } ];
114 }
115  
116 ##
117 ## _seconds_remaining()
118 ##
119 ## Arguments:
120 ## CURRENT_SEGMENT: FillSegment
121 ## TIME_LEFT: int -- seconds
122 ##
123 ## Returns the amount of time in seconds remaining after
124 ## time has been reserved for all of the segments with
125 ## higher priorities than this.
126 ##
127 # TODO Slideshow expects the time remaining to be the expected preferred time
128 11 ▶sub _seconds_remaining {
Subroutine _seconds_remaining: called CC=4 SCAR=13.9
129 11 my $self = shift;
130 11 my ($current_segment, $time_left_in_break) = @_;
131  
132 11 my $time_remaining = $time_left_in_break;
133 11 foreach my $segment (
134 1 sort {$a->priority_order() <=> $b->priority_order()}
135 27 grep { $_->priority_order() < $current_segment->priority_order() }
136 $self->_segments_left()
137 )
138 {
139 6 ▶ $time_remaining > 0 or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$time_remaining > 0 or return 0 6 0 6
140 6 ▶ $segment->is_available($time_remaining) or next;
Branch: 1 branch
BranchTrueFalseTotal
$segment->is_available($time_remaining) or next 3 3 6
141 3 $time_remaining -= $segment->producer()->time_layout()->preferred_time($time_remaining);
142 }
143  
144 11 return $time_remaining;
145  
146 }
147  
148 2 ▶no Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 2 1;
« lib/Video/PlaybackMachine/FillProducer/StillFrame.pm ↑ Summary lib/Video/PlaybackMachine/EventWheel/FullScreen.pm »