Now it works :-)
[toast/stream2beamer.git] / lagarde.py
index e9fca23f68b9ee02c66211575c3b662f2c31ffa6..c33b912339ac13742de2383f6476b6127969c5c1 100755 (executable)
@@ -177,24 +177,74 @@ class WebRTCClient:
         if not pad.has_current_caps():
             log.info(pad, 'has no caps, ignoring')
             return
-
         caps = pad.get_current_caps()
         padsize = caps.get_size()
+
+        log.info(f'>>>> {padsize} {caps}')
+
         for i in range(padsize):
             s = caps.get_structure(i)  # Gst.Structure
             name = s.get_name()
+            log.info(f'###### {name}')
             if name.startswith('video'):
                 q = Gst.ElementFactory.make('queue')
                 conv = Gst.ElementFactory.make('videoconvert')
-                sink = Gst.ElementFactory.make('intervideosink')
+                enc = Gst.ElementFactory.make('x264enc')
+                enc.set_property('bitrate', 1000)
+                enc.set_property('tune', 'zerolatency')
+                capsfilter = Gst.ElementFactory.make('capsfilter')
+                capsfilter.set_properties(Gst.Caps.from_string('video/x-h264,stream-format=(string)avc'))
+                flmux = Gst.ElementFactory.make('flvmux')
+                sink = Gst.ElementFactory.make('rtmpsink')
+                sink.set_property('location', 'rtmp://192.168.1.46:1935/gregoa')
+                # sink.set_property('location', 'rtmp://bla:1936/gregoa')
+                print(sink.props.location, dir(sink.props))
+                assert q and conv and enc and capsfilter and flmux and sink
+
                 self.pipe.add(q)
                 self.pipe.add(conv)
+                self.pipe.add(enc)
+                self.pipe.add(capsfilter)
+                self.pipe.add(flmux)
+                self.pipe.add(sink)
+
+                q_pad_sink = q.get_static_pad('sink')
+                assert q_pad_sink
+                pad_link_return = pad.link(q_pad_sink)
+                assert pad_link_return == Gst.PadLinkReturn.OK
+
+                # ok = element.link(q)
+                # assert ok
+
+                ok = q.link(conv)
+                assert ok
+                ok = conv.link(enc)
+                assert ok
+                ok = enc.link(capsfilter)
+                assert ok
+                ok = capsfilter.link(flmux)
+                assert ok
+                ok = flmux.link(sink)
+                assert ok
+                self.pipe.set_state(Gst.State.PLAYING)
+                self.pipe.sync_children_states()
+                #print(dir(Gst.DebugGraphDetails))
+                #Gst.debug_bin_to_dot_data(element, Gst.DebugGraphDetails.ALL)
+
+            elif name.startswith('audio'):
+                q = Gst.ElementFactory.make('queue')
+                conv = Gst.ElementFactory.make('audioconvert')
+                resample = Gst.ElementFactory.make('audioresample')
+                sink = Gst.ElementFactory.make('autoaudiosink')
+                self.pipe.add(q)
+                self.pipe.add(conv)
+                self.pipe.add(resample)
                 self.pipe.add(sink)
                 self.pipe.sync_children_states()
                 pad.link(q.get_static_pad('sink'))
                 q.link(conv)
-                conv.link(sink)
-                self.pipe.set_state(Gst.State.PLAYING)
+                conv.link(resample)
+                resample.link(sink)
 
     def set_remote_desciption_done(self, gst_promise):
         gst_promise = Gst.Promise.new_with_change_func(self.create_answer_done)
@@ -258,18 +308,6 @@ class WebRTCClient:
             self.pipe.set_state(Gst.State.NULL)
 
 
-async def gstreamer_main_loop():
-    """Does the equivalent of the following lines in an async friendly way:
-        loop = GLib.MainLoop()
-        loop.run()
-    """
-    gst_loop = GLib.MainLoop()
-    context = gst_loop.get_context()
-    while True:
-        events_dispatched = context.iteration(False)
-        await asyncio.sleep(0. if events_dispatched else 0.01)
-
-
 async def run_repeated(task):
     while True:
         await task()
@@ -279,16 +317,14 @@ async def run_repeated(task):
 async def run(uri):
     try:
         events = Events()
-
-        rtsp = RtspServer()
+        # rtsp = RtspServer()
         webrtc = WebRTCClient(events)
         signaling = SignalingClient(events, uri)
 
-        main_loop_task = asyncio.Task(gstreamer_main_loop())
-        webrtc_task = asyncio.Task(run_repeated(webrtc.run))
-        signaling_task = asyncio.Task(run_repeated(signaling.run))
+        webrtc_task = asyncio.Task(webrtc.run())
+        signaling_task = asyncio.Task(signaling.run())
 
-        done, pending = await asyncio.wait([main_loop_task, webrtc_task, signaling_task],
+        done, pending = await asyncio.wait([webrtc_task, signaling_task],
             return_when=asyncio.FIRST_COMPLETED)
 
         for task in done:
@@ -300,7 +336,7 @@ async def run(uri):
 
 
 def main():
-    logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s')
+    logging.basicConfig(level=logging.DEBUG, format='%(asctime)-15s %(message)s')
     parser = argparse.ArgumentParser()
     parser.add_argument('--uri', default='wss://localhost:1234/ws_connect?id=cug',
                         help='Signalling server URI')