Send data from a Python plugin to the GStreamer bus
A plugin can send small control or analytics data to the application by posting
a message on the GStreamer bus. This is useful for detections, counters,
warnings, state changes, and debug data.
#!/usr/bin/env python3importgigi.require_version("Gst","1.0")gi.require_version("GstBase","1.0")fromgi.repositoryimportGst,GstBase,GObjectGst.init(None)classBusMessageFilter(GstBase.BaseTransform):__gstmetadata__=("BusMessageFilter","Filter/Video","Post one bus message for each video buffer","example",)__gsttemplates__=(Gst.PadTemplate.new("sink",Gst.PadDirection.SINK,Gst.PadPresence.ALWAYS,Gst.Caps.from_string("video/x-raw"),),Gst.PadTemplate.new("src",Gst.PadDirection.SRC,Gst.PadPresence.ALWAYS,Gst.Caps.from_string("video/x-raw"),),)def__init__(self):super().__init__()self.frame_count=0defdo_transform_ip(self,buffer):self.frame_count+=1structure=Gst.Structure.new_empty("frame-info")structure.set_value("frame-count",self.frame_count)structure.set_value("pts",int(buffer.pts))structure.set_value("duration",int(buffer.duration))message=Gst.Message.new_element(self,structure)self.post_message(message)returnGst.FlowReturn.OKGObject.type_register(BusMessageFilter)__gstelementfactory__=("busmessagefilter",Gst.Rank.NONE,BusMessageFilter)
Important blocks:
Gst.Structure.new_empty("frame-info") creates the message payload.
structure.set_value(...) adds values to the payload.
Gst.Message.new_element(self, structure) creates an element message.
self.post_message(message) sends it to the pipeline bus.
return Gst.FlowReturn.OK keeps the buffer moving downstream.