« lib/Video/PlaybackMachine/FillProducer/TextFrame/Row.pm
↑ Summary
lib/Video/PlaybackMachine/FillProducer/StillFrame.pm »
| 1 | package Video::PlaybackMachine::ScheduleView; | ||
| 2 | |||
| 3 | our $VERSION = '0.09'; # VERSION | ||
| 4 | |||
| 5 | #### | ||
| 6 | #### Video::PlaybackMachine::ScheduleView | ||
| 7 | #### | ||
| 8 | #### $Revision$ | ||
| 9 | #### | ||
| 10 | #### Translates between the times listed in a schedule | ||
| 11 | #### and the current time. | ||
| 12 | #### | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | use Carp; | ||
| 17 | use Log::Log4perl; | ||
| 18 | |||
| 19 | ############################# Class Constants ############################# | ||
| 20 | |||
| 21 | ############################## Class Methods ############################## | ||
| 22 | |||
| 23 | ## | ||
| 24 | ## new() | ||
| 25 | ## | ||
| 26 | ## Arguments: | ||
| 27 | ## | ||
| 28 | ## SCHEDULE_TABLE: Video::PlaybackMachine::ScheduleTable | ||
| 29 | ## OFFSET: int -- Difference between schedule time and real ( s - r ) | ||
| 30 | ## | ||
| 31 | sub new { | ||
| 32 | my $type = shift; | ||
| 33 | |||
| 34 | @_ == 2 or croak "${type}::new(): arguments are SCHEDULE_TABLE and OFFSET; stopped"; | ||
| 35 | |||
| 36 | my ($schedule_table, $offset) = @_; | ||
| 37 | |||
| 38 | defined $offset or $offset = 0; | ||
| 39 | |||
| 40 | my $self = { | ||
| 41 | schedule_table => $schedule_table, | ||
| 42 | offset => $offset, | ||
| 43 | logger => Log::Log4perl->get_logger('Video.PlaybackMachine.ScheduleView') | ||
| 44 | }; | ||
| 45 | |||
| 46 | bless $self, $type; | ||
| 47 | } | ||
| 48 | |||
| 49 | ############################# Object Methods ############################## | ||
| 50 | |||
| 51 | ## | ||
| 52 | ## Returns the given time corrected with the schedule | ||
| 53 | ## offset. If no arguments, returns the current time | ||
| 54 | ## corrected for schedule offset. | ||
| 55 | ## | ||
| 56 | # Note: Currently the offset is positive for the past, negative for | ||
| 57 | # the future. | ||
| 58 | sub real_to_schedule { | ||
| 59 | my $self = shift; | ||
| 60 | my ($real_time) = @_; | ||
| 61 | |||
| 62 | defined $real_time or $real_time = CORE::time(); | ||
| 63 | return $real_time - $self->{offset}; | ||
| 64 | |||
| 65 | } | ||
| 66 | |||
| 67 | sub schedule_to_real { | ||
| 68 | my $self = shift; | ||
| 69 | my ($schedule_time) = @_; | ||
| 70 | |||
| 71 | defined $schedule_time or return CORE::time(); | ||
| 72 | return $schedule_time + $self->{'offset'}; | ||
| 73 | } | ||
| 74 | |||
| 75 | ## | ||
| 76 | ## Returns the offset value. | ||
| 77 | ## | ||
| 78 | sub get_offset { | ||
| 79 | return $_[0]->{offset}; | ||
| 80 | } | ||
| 81 | |||
| 82 | ## | ||
| 83 | ## Returns the schedule table. | ||
| 84 | ## | ||
| 85 | sub get_schedule_table { | ||
| 86 | return $_[0]->{schedule_table}; | ||
| 87 | } | ||
| 88 | |||
| 89 | ## | ||
| 90 | ## get_next_entry() | ||
| 91 | ## | ||
| 92 | ## Returns the next entry appearing on our Schedule Table. | ||
| 93 | ## | ||
| 94 | sub get_next_entry { | ||
| 95 | my $self = shift; | ||
| 96 | my ($real_time, $num_entries) = @_; | ||
| 97 | defined $real_time or $real_time = time(); | ||
| 98 | |||
| 99 | return $self->_do_get_next_entry($real_time, $num_entries); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _do_get_next_entry { | ||
| 103 | my $self = shift; | ||
| 104 | my ($real_time, $num_entries) = @_; | ||
| 105 | |||
| 106 | return scalar($self->{schedule_table} | ||
| 107 | ->get_entries_after( | ||
| 108 | $self->real_to_schedule($real_time + 1), | ||
| 109 | $num_entries | ||
| 110 | ) | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | ## | ||
| 115 | ## Returns the amount of time until the next scheduled entry. | ||
| 116 | ## Returns empty if no scheduled entry remains. | ||
| 117 | ## | ||
| 118 | sub get_time_to_next { | ||
| 119 | my $self = shift; | ||
| 120 | my $real_time = time(); | ||
| 121 | |||
| 122 | my $next_entry = $self->_do_get_next_entry($real_time) | ||
| 123 | or return; | ||
| 124 | |||
| 125 | return $next_entry->get_start_time() - $self->real_to_schedule($real_time); | ||
| 126 | |||
| 127 | } | ||
| 128 | |||
| 129 | ## | ||
| 130 | ## Returns the seek time for a given schedule entry. | ||
| 131 | ## | ||
| 132 | sub get_seek { | ||
| 133 | my $self = shift; | ||
| 134 | my $entry = shift; | ||
| 135 | |||
| 136 | my $seek = time() - $self->schedule_to_real($entry->get_start_time()); | ||
| 137 | return ($seek > 0) ? $seek : 0; | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | 1; |