|
| 1 | +"""Simple gstreamer webrtc consumer example.""" |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | +import gi |
| 6 | +from gst_signalling.utils import find_producer_peer_id_by_name |
| 7 | + |
| 8 | +gi.require_version("Gst", "1.0") |
| 9 | +from gi.repository import GLib, Gst # noqa: E402 |
| 10 | + |
| 11 | + |
| 12 | +class GstConsumer: |
| 13 | + """Gstreamer webrtc consumer class.""" |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + signalling_host: str, |
| 18 | + signalling_port: int, |
| 19 | + peer_name: str, |
| 20 | + ) -> None: |
| 21 | + """Initialize the consumer with signalling server details and peer name.""" |
| 22 | + Gst.init(None) |
| 23 | + |
| 24 | + self.pipeline = Gst.Pipeline.new("webRTC-consumer") |
| 25 | + self.source = Gst.ElementFactory.make("webrtcsrc") |
| 26 | + |
| 27 | + if not self.pipeline: |
| 28 | + print("Pipeline could be created.") |
| 29 | + exit(-1) |
| 30 | + |
| 31 | + if not self.source: |
| 32 | + print( |
| 33 | + "webrtcsrc component could not be created. Please make sure that the plugin is installed \ |
| 34 | + (see https://gitlab.freedesktop.org/gstreamer/gst-plugins-rs/-/tree/main/net/webrtc)" |
| 35 | + ) |
| 36 | + exit(-1) |
| 37 | + |
| 38 | + self.pipeline.add(self.source) |
| 39 | + |
| 40 | + peer_id = find_producer_peer_id_by_name( |
| 41 | + signalling_host, signalling_port, peer_name |
| 42 | + ) |
| 43 | + print(f"found peer id: {peer_id}") |
| 44 | + |
| 45 | + self.source.connect("pad-added", self.webrtcsrc_pad_added_cb) |
| 46 | + signaller = self.source.get_property("signaller") |
| 47 | + signaller.set_property("producer-peer-id", peer_id) |
| 48 | + signaller.set_property("uri", f"ws://{signalling_host}:{signalling_port}") |
| 49 | + |
| 50 | + def dump_latency(self) -> None: |
| 51 | + """Dump the current pipeline latency.""" |
| 52 | + query = Gst.Query.new_latency() |
| 53 | + self.pipeline.query(query) |
| 54 | + print(f"Pipeline latency {query.parse_latency()}") |
| 55 | + |
| 56 | + def _configure_webrtcbin(self, webrtcsrc: Gst.Element) -> None: |
| 57 | + if isinstance(webrtcsrc, Gst.Bin): |
| 58 | + webrtcbin_name = "webrtcbin0" |
| 59 | + webrtcbin = webrtcsrc.get_by_name(webrtcbin_name) |
| 60 | + assert webrtcbin is not None |
| 61 | + # jitterbuffer has a default 200 ms buffer. |
| 62 | + webrtcbin.set_property("latency", 50) |
| 63 | + |
| 64 | + def webrtcsrc_pad_added_cb(self, webrtcsrc: Gst.Element, pad: Gst.Pad) -> None: |
| 65 | + """Add webrtcsrc elements when a new pad is added.""" |
| 66 | + self._configure_webrtcbin(webrtcsrc) |
| 67 | + if pad.get_name().startswith("video"): # type: ignore[union-attr] |
| 68 | + # webrtcsrc automatically decodes and convert the video |
| 69 | + sink = Gst.ElementFactory.make("fpsdisplaysink") |
| 70 | + assert sink is not None |
| 71 | + self.pipeline.add(sink) |
| 72 | + pad.link(sink.get_static_pad("sink")) # type: ignore[arg-type] |
| 73 | + sink.sync_state_with_parent() |
| 74 | + |
| 75 | + elif pad.get_name().startswith("audio"): # type: ignore[union-attr] |
| 76 | + # webrtcsrc automatically decodes and convert the audio |
| 77 | + sink = Gst.ElementFactory.make("autoaudiosink") |
| 78 | + assert sink is not None |
| 79 | + self.pipeline.add(sink) |
| 80 | + pad.link(sink.get_static_pad("sink")) # type: ignore[arg-type] |
| 81 | + sink.sync_state_with_parent() |
| 82 | + |
| 83 | + GLib.timeout_add_seconds(5, self.dump_latency) |
| 84 | + |
| 85 | + def __del__(self) -> None: |
| 86 | + """Destructor to clean up GStreamer resources.""" |
| 87 | + Gst.deinit() |
| 88 | + |
| 89 | + def get_bus(self) -> Gst.Bus: |
| 90 | + """Get the GStreamer bus for the pipeline.""" |
| 91 | + return self.pipeline.get_bus() |
| 92 | + |
| 93 | + def play(self) -> None: |
| 94 | + """Start the GStreamer pipeline.""" |
| 95 | + ret = self.pipeline.set_state(Gst.State.PLAYING) |
| 96 | + if ret == Gst.StateChangeReturn.FAILURE: |
| 97 | + print("Error starting playback.") |
| 98 | + exit(-1) |
| 99 | + print("playing ... (ctrl+c to quit)") |
| 100 | + |
| 101 | + def stop(self) -> None: |
| 102 | + """Stop the GStreamer pipeline.""" |
| 103 | + print("stopping") |
| 104 | + self.pipeline.send_event(Gst.Event.new_eos()) |
| 105 | + self.pipeline.set_state(Gst.State.NULL) |
| 106 | + |
| 107 | + |
| 108 | +def process_msg(bus: Gst.Bus, pipeline: Gst.Pipeline) -> bool: |
| 109 | + """Process messages from the GStreamer bus.""" |
| 110 | + msg = bus.timed_pop_filtered(10 * Gst.MSECOND, Gst.MessageType.ANY) |
| 111 | + if msg: |
| 112 | + if msg.type == Gst.MessageType.ERROR: |
| 113 | + err, debug = msg.parse_error() |
| 114 | + print(f"Error: {err}, {debug}") |
| 115 | + return False |
| 116 | + elif msg.type == Gst.MessageType.EOS: |
| 117 | + print("End-Of-Stream reached.") |
| 118 | + return False |
| 119 | + elif msg.type == Gst.MessageType.LATENCY: |
| 120 | + if pipeline: |
| 121 | + try: |
| 122 | + pipeline.recalculate_latency() |
| 123 | + except Exception as e: |
| 124 | + print("failed to recalculate warning, exception: %s" % str(e)) |
| 125 | + # else: |
| 126 | + # print(f"Message: {msg.type}") |
| 127 | + return True |
| 128 | + |
| 129 | + |
| 130 | +def main() -> None: |
| 131 | + """Run the main function.""" |
| 132 | + parser = argparse.ArgumentParser(description="webrtc gstreamer simple consumer") |
| 133 | + parser.add_argument( |
| 134 | + "--signaling-host", |
| 135 | + default="127.0.0.1", |
| 136 | + help="Gstreamer signaling host - Reachy Mini ip", |
| 137 | + ) |
| 138 | + parser.add_argument( |
| 139 | + "--signaling-port", default=8443, help="Gstreamer signaling port" |
| 140 | + ) |
| 141 | + |
| 142 | + args = parser.parse_args() |
| 143 | + |
| 144 | + consumer = GstConsumer( |
| 145 | + args.signaling_host, |
| 146 | + args.signaling_port, |
| 147 | + "reachymini", |
| 148 | + ) |
| 149 | + consumer.play() |
| 150 | + |
| 151 | + # Wait until error or EOS |
| 152 | + bus = consumer.get_bus() |
| 153 | + try: |
| 154 | + while True: |
| 155 | + if not process_msg(bus, consumer.pipeline): |
| 156 | + break |
| 157 | + |
| 158 | + except KeyboardInterrupt: |
| 159 | + print("User exit") |
| 160 | + finally: |
| 161 | + consumer.stop() |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + main() |
0 commit comments