def start_test_player(): log_fmt = ( "%(asctime)s %(levelname)s:%(name)s %(filename)s:%(lineno)d " "%(message)s") logging.basicConfig(level=logging.DEBUG, format=log_fmt) service = PlayoutService() schedule = playlist.Schedule() v = schedule.new_program() v.set_program(1758, clock.now() - datetime.timedelta(0, 5), title="First", playback_offset=10, playback_duration=10.0) schedule.add(v) for n in range(1): delta = datetime.timedelta(0, 6+(n)*10.0) v = schedule.new_program() v.set_program(1758, clock.now() + delta, title="No %i" % n, playback_offset=30+60*n, playback_duration=9.0) print(("Added %i @ %s" % (n, v.program_start))) schedule.add(v) player = Playout(service) player.set_schedule(schedule) # import playoutweb # playoutweb.start_web(None, playout_service=service, playout=player, # schedule=schedule, port=8888) reactor.callLater(4.0, player.show_still) reactor.callLater(7.0, player.cancel_still) return player
def testRealtime(self): clock.set(clock.RealTime()) self.assertNotEquals(clock.now(), test_time) a = clock.now() time.sleep(0.1) b = clock.now() d = b-a self.assertTrue(d.microseconds < 200000, "Not equal, delta should be 100000-200000: delta = %f" % (d.microseconds))
def testSchedule(self): t = clock.SimulatedTime(test_time) clock.set(t) s = playlist.Schedule() p1 = s.new_program() p1.set_program(0, program_start=clock.now() + datetime.timedelta(seconds=5)) s.add(p1) p2 = s.new_program() p2.set_program(1, program_start=clock.now() + datetime.timedelta(seconds=10), playback_duration=5) s.add(p2) p3 = s.new_program() p3.set_program(2, program_start=clock.now() + datetime.timedelta(seconds=20), playback_duration=5.5) s.add(p3) # Not started yet self.assertEqual(s.get_current_program(), None) self.assertEqual(s.get_next_program(), p1) self.assertNotEqual(s.get_next_program(), p2) t.advance(datetime.timedelta(seconds=5)) # At beginning of first video self.assertEqual(s.get_current_program(), p1) self.assertEqual(p1.seconds_until_playback(), 0) self.assertEqual(p1.seconds_since_playback(), 0) # At start of second video t.advance(datetime.timedelta(seconds=5)) self.assertEqual(s.get_current_program(), p2) self.assertEqual(s.get_next_program(), p3) # At end of second t.advance(datetime.timedelta(seconds=5)) self.assertEqual(s.get_current_program(), None) self.assertEqual(s.get_next_program(), p3) # At start of tird t.advance(datetime.timedelta(seconds=5.0)) self.assertEqual(s.get_current_program(), p3) self.assertEqual(t.now().second, 20) # Half sec into third t.advance(datetime.timedelta(seconds=0.5)) self.assertEqual(s.get_current_program(), p3) self.assertEqual(s.get_next_program(), None) # At end of third t.advance(datetime.timedelta(seconds=5)) self.assertEqual(s.get_current_program(), None) self.assertEqual(s.get_next_program(), None) # Test remove t = clock.SimulatedTime(test_time) clock.set(t) self.assertEqual(s.get_next_program(), p1) s.remove(p1) self.assertEqual(s.get_next_program(), p2)
def testManualTime(self): source = clock.ManualTime(test_time) clock.set(source) self.assertEquals(clock.now(), test_time) a = clock.now() time.sleep(0.1) b = clock.now() d = b - a self.assertEquals(d.microseconds, 0) source.advance(datetime.timedelta(seconds=5)) self.assertEquals(clock.now().second, 5)
def testManualTime(self): source = clock.ManualTime(test_time) clock.set(source) self.assertEquals(clock.now(), test_time) a = clock.now() time.sleep(0.1) b = clock.now() d = b-a self.assertEquals(d.microseconds, 0) source.advance(datetime.timedelta(seconds=5)) self.assertEquals(clock.now().second, 5)
def testReset(self): source = clock.SimulatedTime(test_time, ratio=0.0) clock.set(source) self.assertEquals(clock.now(), test_time) clock.reset() self.assertNotEquals(clock.now(), test_time) a = clock.now() time.sleep(0.1) b = clock.now() d = b-a self.assertTrue(d.microseconds < 200000, "Not equal, delta should be 100000-200000: delta = %f" % (d.microseconds))
def testSchedule(self): t = clock.SimulatedTime(test_time) clock.set(t) s = playlist.Schedule() p1 = s.new_program() p1.set_program(0, program_start=clock.now()+datetime.timedelta(seconds=5)) s.add(p1) p2 = s.new_program() p2.set_program(1, program_start=clock.now()+datetime.timedelta(seconds=10),playback_duration=5) s.add(p2) p3 = s.new_program() p3.set_program(2, program_start=clock.now()+datetime.timedelta(seconds=20),playback_duration=5.5) s.add(p3) # Not started yet self.assertEqual(s.get_current_program(), None) self.assertEqual(s.get_next_program(), p1) self.assertNotEqual(s.get_next_program(), p2) t.advance(datetime.timedelta(seconds=5)) # At beginning of first video self.assertEqual(s.get_current_program(), p1) self.assertEqual(p1.seconds_until_playback(), 0) self.assertEqual(p1.seconds_since_playback(), 0) # At start of second video t.advance(datetime.timedelta(seconds=5)) self.assertEqual(s.get_current_program(), p2) self.assertEqual(s.get_next_program(), p3) # At end of second t.advance(datetime.timedelta(seconds=5)) self.assertEqual(s.get_current_program(), None) self.assertEqual(s.get_next_program(), p3) # At start of tird t.advance(datetime.timedelta(seconds=5.0)) self.assertEqual(s.get_current_program(), p3) self.assertEqual(t.now().second, 20) # Half sec into third t.advance(datetime.timedelta(seconds=0.5)) self.assertEqual(s.get_current_program(), p3) self.assertEqual(s.get_next_program(), None) # At end of third t.advance(datetime.timedelta(seconds=5)) self.assertEqual(s.get_current_program(), None) self.assertEqual(s.get_next_program(), None) # Test remove t = clock.SimulatedTime(test_time) clock.set(t) self.assertEqual(s.get_next_program(), p1) s.remove(p1) self.assertEqual(s.get_next_program(), p2)
def on_idle(self): time_until_next = float("inf") if self.next_program: time_until_next = self.next_program.seconds_until_playback() # The rules. use_jukebox = configuration.jukebox use_jukebox &= time_until_next > (120+IDENT_LENGTH) use_jukebox &= self.random_provider.enough_room(time_until_next) if use_jukebox: loop_length = 12.0 PAUSE_LENGTH = IDENT_LENGTH+loop_length logging.info("Pause before jukebox: %.1fs" % PAUSE_LENGTH) program = self.schedule.new_program() program.set_program(-1, program_start=clock.now(), playback_duration=loop_length, title="Jukebox pause screen", filename=LOOP_FILENAME, loop=True) self.cue_program(program) self.on_end_call_stack.append(self.play_ident) self.on_end_call_stack.append(self.play_jukebox) elif time_until_next >= 12+IDENT_LENGTH: logging.info("Pause idle: %.1fs" % time_until_next) PAUSE_LENGTH = time_until_next program = self.schedule.new_program() program.set_program(-1, program_start=clock.now(), playback_duration=time_until_next-IDENT_LENGTH, title="Pause screen", filename=LOOP_FILENAME, loop=True) self.cue_program(program) self.on_end_call_stack.append(self.play_ident) else: logging.info("Short idle: %.1fs" % time_until_next) # Show pausescreen program = self.schedule.new_program() t = None if self.next_program: t = self.next_program.seconds_until_playback() program.set_program(-1, program_start=clock.now(), playback_duration=t, title="Pause screen", filename=LOOP_FILENAME, loop=True) #self.cue_program(program) # TODO: Doesn't handle looping self.player.pause_screen() self.playing_program = program self.service.on_playback_started(program)
def play_ident(self): logging.info("Ident playback start") program = self.schedule.new_program() program.set_program( media_id=-1, program_start=clock.now(), playback_duration=IDENT_LENGTH, title="Frikanalen Vignett", filename=IDENT_FILENAME) self.cue_program(program)
def play_jukebox(self): logging.info("Jukebox playback start") program = self.schedule.new_program() limit = 90*60 # 90 minutes long programs max if self.next_program: limit = min(limit, self.next_program.seconds_until_playback()) video = self.random_provider.get_random_video(limit) program.set_program( media_id=video["media_id"], program_start=clock.now(), playback_duration=video["duration"], title=video["name"]) self.cue_program(program)
def testProgram(self): t = clock.SimulatedTime(test_time) clock.set(t) p = playlist.Program() start = clock.now()+datetime.timedelta(seconds=5) p.set_program(100, program_start=start, playback_duration=200) self.assertEqual(p.seconds_until_playback(), 5) self.assertNotEqual(p.seconds_since_playback(), 5) t.advance(datetime.timedelta(seconds=10)) self.assertNotEqual(p.seconds_until_playback(), 5) self.assertEqual(p.seconds_since_playback(), 5) d = p.jsondict() self.assertEqual(d["program_end"], (start+datetime.timedelta(seconds=200)).ctime()) self.assertEqual(d["program_start"], (start).ctime())
def testProgram(self): t = clock.SimulatedTime(test_time) clock.set(t) p = playlist.Program() start = clock.now()+datetime.timedelta(seconds=5) p.set_program(100, program_start=start, playback_duration=200) self.assertEqual(p.seconds_until_playback(), 5) self.assertNotEqual(p.seconds_since_scheduled_start(), 5) t.advance(datetime.timedelta(seconds=10)) self.assertNotEqual(p.seconds_until_playback(), 5) self.assertEqual(p.seconds_since_scheduled_start(), 5) d = p.jsondict() self.assertEqual(d["program_end"], (start+datetime.timedelta(seconds=200)).ctime()) self.assertEqual(d["program_start"], (start).ctime())
def testSimulatedTime(self): source = clock.SimulatedTime(test_time, ratio=0.0) clock.set(source) self.assertEquals(clock.now(), test_time) a = clock.now() time.sleep(0.1) b = clock.now() d = b-a self.assertEquals(d.microseconds, 0) source.advance(datetime.timedelta(seconds=5)) self.assertEquals(clock.now().second, 5) source.set_ratio(100.0) time.sleep(0.125) self.assertAlmostEqual(clock.now().second+clock.now().microsecond/1e6, 17.5, places=0)
def testSimulatedTime(self): source = clock.SimulatedTime(test_time, ratio=0.0) clock.set(source) self.assertEquals(clock.now(), test_time) a = clock.now() time.sleep(0.1) b = clock.now() d = b-a self.assertEquals(d.microseconds, 0) source.advance(datetime.timedelta(seconds=5)) self.assertEquals(clock.now().second, 5) source.set_ratio(100.0) time.sleep(0.125) self.assertAlmostEqual(clock.now().second + clock.now().microsecond / 1e6, 17.5, places=0)