<commit_before>#!/usr/bin/python3
import time
from gi.repository import GLib, Gst

class ShmSrc(Gst.Bin):
	last_buffer_arrived = 0
	is_in_failstate = True

	def __init__(self, socket, caps):
		super().__init__()

		# Create elements
		self.shmsrc = Gst.ElementFactory.make('shmsrc', None)
		self.identity1 = Gst.ElementFactory.make('identity', None)
		self.identity2 = Gst.ElementFactory.make('identity', None)
		self.switch = Gst.ElementFactory.make('input-selector', None)
		self.failsrc = Gst.ElementFactory.make('videotestsrc', None)

		# Add elements to Bin
		self.add(self.shmsrc)
		self.add(self.identity1)
		self.add(self.identity2)
		self.add(self.switch)
		self.add(self.failsrc)

		# Get Switcher-Pads
		self.goodpad = self.switch.get_request_pad('sink_%u')
		self.failpad = self.switch.get_request_pad('sink_%u')

		# Set properties
		self.shmsrc.set_property('socket-path', socket)
		self.shmsrc.set_property('is-live', True)
		self.shmsrc.set_property('do-timestamp', True)
		#self.identity1.set_property('sync', True)
		self.identity2.set_property('sync', True)
		self.switch.set_property('active-pad', self.failpad)
		self.failsrc.set_property('pattern', 'snow')

		# Link elements
		self.shmsrc.link_filtered(self.identity1, caps)
		self.identity1.get_static_pad('src').link(self.goodpad)

		self.failsrc.link_filtered(self.identity2, caps)
		self.identity2.get_static_pad('src').link(self.failpad)

		# Install pad probes
		self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.EVENT_DOWNSTREAM, self.event_probe, None)
		self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.BUFFER, self.data_probe, None)

		# Install Watchdog
		GLib.timeout_add(500, self.watchdog)

		# Add Ghost Pads
		self.add_pad(
			Gst.GhostPad.new('sink', self.switch.get_static_pad('src'))
		)

	def event_probe(self, pad, info, ud):
		e = info.get_event()
		if e.type == Gst.EventType.EOS:
			self.switch_to_failstate()
			
			return Gst.PadProbeReturn.DROP
		
		return Gst.PadProbeReturn.PASS


	def data_probe(self, pad, info, ud):
		self.last_buffer_arrived = time.time()
		self.switch_to_goodstate()
		return Gst.PadProbeReturn.PASS

	def watchdog(self):
		if self.last_buffer_arrived + 0.1 < time.time():
			print("watchdog()::timeout")
			self.switch_to_failstate()
		
		if self.last_buffer_arrived + 3 < time.time() and round(time.time() % 3) == 0:
			print("watchdog()::restart")
			self.restart()
		
		return True
	
	def restart(self):
		self.shmsrc.set_state(Gst.State.NULL)
		self.shmsrc.set_state(Gst.State.PLAYING)
	
	def switch_to_goodstate(self):
		if not self.is_in_failstate:
			return
		
		print("switch_to_goodstate()")
		self.is_in_failstate = False
		self.switch.set_property('active-pad', self.goodpad)
	
	def switch_to_failstate(self):
		if self.is_in_failstate:
			return
		
		print("switch_to_failstate()")
		self.is_in_failstate = True
		self.switch.set_property('active-pad', self.failpad)
<commit_msg>drop ERROR message<commit_after>#!/usr/bin/python3
import time
from gi.repository import GLib, Gst

class ShmSrc(Gst.Bin):
	last_buffer_arrived = 0
	is_in_failstate = True

	def __init__(self, socket, caps):
		super().__init__()

		# Create elements
		self.shmsrc = Gst.ElementFactory.make('shmsrc', None)
		self.identity1 = Gst.ElementFactory.make('identity', None)
		self.identity2 = Gst.ElementFactory.make('identity', None)
		self.switch = Gst.ElementFactory.make('input-selector', None)
		self.failsrc = Gst.ElementFactory.make('videotestsrc', None)

		# Add elements to Bin
		self.add(self.shmsrc)
		self.add(self.identity1)
		self.add(self.identity2)
		self.add(self.switch)
		self.add(self.failsrc)

		# Get Switcher-Pads
		self.goodpad = self.switch.get_request_pad('sink_%u')
		self.failpad = self.switch.get_request_pad('sink_%u')

		# Set properties
		self.shmsrc.set_property('socket-path', socket)
		self.shmsrc.set_property('is-live', True)
		self.shmsrc.set_property('do-timestamp', True)
		#self.identity1.set_property('sync', True)
		self.identity2.set_property('sync', True)
		self.switch.set_property('active-pad', self.failpad)
		self.failsrc.set_property('pattern', 'snow')

		# Link elements
		self.shmsrc.link_filtered(self.identity1, caps)
		self.identity1.get_static_pad('src').link(self.goodpad)

		self.failsrc.link_filtered(self.identity2, caps)
		self.identity2.get_static_pad('src').link(self.failpad)

		# Install pad probes
		self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.EVENT_DOWNSTREAM, self.event_probe, None)
		self.shmsrc.get_static_pad('src').add_probe(Gst.PadProbeType.BLOCK | Gst.PadProbeType.BUFFER, self.data_probe, None)

		# Install Watchdog
		GLib.timeout_add(500, self.watchdog)

		# Add Ghost Pads
		self.add_pad(
			Gst.GhostPad.new('sink', self.switch.get_static_pad('src'))
		)

	def do_handle_message(self, msg):
		if msg.type == Gst.MessageType.ERROR:
			print("do_handle_message(): dropping error")
			return
		
		print("do_handle_message()", msg.src, msg.type)
		Gst.Bin.do_handle_message(self, msg)

	def event_probe(self, pad, info, ud):
		e = info.get_event()
		if e.type == Gst.EventType.EOS:
			self.switch_to_failstate()
			return Gst.PadProbeReturn.DROP
		
		return Gst.PadProbeReturn.PASS


	def data_probe(self, pad, info, ud):
		self.last_buffer_arrived = time.time()
		self.switch_to_goodstate()
		return Gst.PadProbeReturn.PASS

	def watchdog(self):
		if self.last_buffer_arrived + 0.1 < time.time():
			print("watchdog()::timeout")
			self.switch_to_failstate()
		
		if self.last_buffer_arrived + 3 < time.time() and round(time.time() % 3) == 0:
			print("watchdog()::restart")
			self.restart()
		
		return True
	
	def restart(self):
		self.shmsrc.set_state(Gst.State.NULL)
		self.shmsrc.set_state(Gst.State.PLAYING)
	
	def switch_to_goodstate(self):
		if not self.is_in_failstate:
			return
		
		print("switch_to_goodstate()")
		self.is_in_failstate = False
		self.switch.set_property('active-pad', self.goodpad)
	
	def switch_to_failstate(self):
		if self.is_in_failstate:
			return
		
		print("switch_to_failstate()")
		self.is_in_failstate = True
		self.switch.set_property('active-pad', self.failpad)

{'fraction_non_alphanumeric': 0.136986301369863, 'fraction_numerical': 0.0064464141821112, 'mean_word_length': 4.730378578024007, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7198248', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2313, 'n_words': 349}