I am trying to write a python script that can process images from the camera continuously and show the images over gstreamer rtsp if there is a connection. Although there are similar posts and I can show camera video over rtsp, the video capturing mechanism runs only when GstRtspServer.RTSPMediaFactory.on_need_data() method runs
Ideally I would like to have a SensorFactory class that inherits RTSPMediaFactory class and while loop that captures images, process them and sets SensorFactory.frame. SensorFactory.on_need_data will return latest SensorFactory.frame
class SensorFactory(GstRtspServer.RTSPMediaFactory):
def __init__(self, **properties):
GstRtspServer.RTSPMediaFactory.__init__(self)
self.frame = None
...
def on_need_data(self, src, lenght):
if self.frame is not None:
data = self.frame.tostring()
buf = Gst.Buffer.new_allocate(None, len(data), None)
buf.fill(0, data)
buf.duration = self.duration
timestamp = self.number_frames * self.duration
buf.pts = buf.dts = int(timestamp)
buf.offset = timestamp
self.number_frames += 1
retval = src.emit('push-buffer', buf)
GObject.threads_init()
Gst.init(None)
server = GstServer()
loop = GObject.MainLoop()
loop.run()
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
server.frame = frame
process(frame)
The problem is this code sticks at loop.run(). Although I could read the frame in on_need_data(), this would only process the video when there is an active rtsp connection and I would like to process frames even when there isn't active rtsp connection.
I tried threading.Event and loop.get_context() methods but I was not sure how to integrate them to this scenario.