Allow list of rooms.
[toast/stream2beamer.git] / lagarde.py
1 #!/usr/bin/python3
2 import argparse
3 import asyncio
4 import json
5 import logging
6 import ssl
7 import queue
8 from typing import List
9
10 import gi
11 import websockets
12
13 gi.require_version('Gst', '1.0')
14 from gi.repository import Gst
15
16 gi.require_version('GstWebRTC', '1.0')
17 from gi.repository import GstWebRTC
18
19 gi.require_version('GstSdp', '1.0')
20 from gi.repository import GstSdp
21
22 log = logging.getLogger(__name__)
23
24
25 class Events:
26     def __init__(self):
27         self.sdp_offer = queue.Queue()
28         self.sdp_answer = queue.Queue()
29         self.generated_ice_candidates = queue.Queue()
30         self.received_ice_candidates = queue.Queue()
31         self.sdp_info = queue.Queue()  # (sdp_mids, user_fragments)
32         self.room_left = queue.Queue()
33
34
35 class SignalingClient:
36     def __init__(self, events: Events, uri):
37         self.events = events
38         self.uri = uri
39         self.ssl_context = ssl.SSLContext()
40         self.ssl_context.check_hostname = False
41         self.ssl_context.verify_mode = ssl.CERT_NONE
42         self.session_id = None
43
44     async def receive(self, uri):
45         async for msg in self.websocket:
46             msg_json = json.loads(msg)
47             msg_type = msg_json['Type']
48             msg_value = msg_json['Value']
49             assert self.session_id is None or self.session_id == msg_json['SessionID']
50             if msg_type == 'newSession':
51                 self.session_id = msg_json['SessionID']
52                 log.info(f"New session {self.session_id}")
53             elif msg_type == 'gotOffer':
54                 value_json = json.loads(msg_value)
55                 sdp = value_json['sdp']
56                 log.info(f'Got SDP offer')
57                 log.debug(f'SDP offer:\n{sdp}')
58                 self.events.sdp_offer.put_nowait(sdp)
59             elif msg_type == 'addCallerIceCandidate':
60                 value_json = json.loads(msg_value)
61                 log.info(f'Got ICE candidate')
62                 log.debug(f'ICE candidate: {value_json}')
63                 self.events.received_ice_candidates.put_nowait(value_json)
64             elif msg_type == 'roomNotFound':
65                 log.error(f'The room was not found: {uri}')
66                 return
67             elif msg_type == 'roomClosed':
68                 log.info(f'Oh noes, the room went away (session {self.session_id})!')
69                 self.events.room_left.put_nowait(True)
70                 return
71             else:
72                 log.error(f'Unknown message type {msg_type}')
73
74     async def send(self):
75         sdp_mids = None
76         user_fragments = None
77         while True:
78             if self.events.sdp_answer.qsize() > 0:
79                 sdp_answer = self.events.sdp_answer.get_nowait()
80                 sdp_answer_msg = json.dumps({
81                     'SessionID': self.session_id,
82                     'Type': "gotAnswer",
83                     'Value': json.dumps({
84                         'type': 'answer',
85                         'sdp': sdp_answer
86                     })
87                 })
88                 await self.websocket.send(sdp_answer_msg)
89
90             elif self.events.sdp_info.qsize() > 0:
91                 sdp_mids, user_fragments = self.events.sdp_info.get_nowait()
92
93             elif self.events.generated_ice_candidates.qsize() > 0 \
94                     and sdp_mids is not None and user_fragments is not None:
95                 mlineindex, candidate = self.events.generated_ice_candidates.get_nowait()
96                 sdp_mid = sdp_mids[mlineindex]
97                 user_fragment = user_fragments[mlineindex]
98                 icemsg_value = json.dumps({
99                     "candidate": candidate,
100                     "sdpMid": sdp_mid,
101                     "sdpMLineIndex": mlineindex,
102                     "usernameFragment": user_fragment,
103                 })
104                 icemsg = json.dumps({
105                     'SessionID': self.session_id,
106                     'Type': 'addCalleeIceCandidate',
107                     'Value': icemsg_value,
108                 })
109                 log.info(f'Send ICE candidate')
110                 log.debug(f'ICE candidate: {icemsg_value}')
111                 await self.websocket.send(icemsg)
112
113             else:
114                 await asyncio.sleep(0.2)
115
116     async def run(self):
117         self.session_id = None
118         async with websockets.connect(self.uri, ssl=self.ssl_context, close_timeout=0.5) as self.websocket:
119             receive_task = asyncio.Task(self.receive(self.uri))
120             send_task = asyncio.Task(self.send())
121             done, pending = await asyncio.wait([receive_task, send_task], return_when=asyncio.FIRST_COMPLETED)
122             for task in pending:
123                 task.cancel()
124
125
126 class WebRTCClient:
127     def __init__(self, events: Events, rtmp_uri: str):
128         self.events = events
129         self.rtmp_uri = rtmp_uri
130         self.webrtcbin = Gst.ElementFactory.make('webrtcbin', 'laplace')
131         self.pipe = Gst.Pipeline.new("pipeline")
132         Gst.Bin.do_add_element(self.pipe, self.webrtcbin)
133         self.webrtcbin.connect('on-negotiation-needed', self.on_negotiation_needed)
134         self.webrtcbin.connect('on-ice-candidate', self.on_ice_candidate)
135         self.webrtcbin.connect('pad-added', self.webrtcbin_pad_added)
136
137     def on_negotiation_needed(self, element):
138         log.info('on_negotiation_needed')
139
140     def on_ice_candidate(self, element, mlineindex, candidate):
141         log.info('on_ice_candidate')
142         self.events.generated_ice_candidates.put_nowait((mlineindex, candidate))
143
144     def webrtcbin_pad_added(self, element, pad):
145         log.info('webrtcbin_pad_added')
146         if pad.direction != Gst.PadDirection.SRC:
147             return
148         decodebin = Gst.ElementFactory.make('decodebin')
149         decodebin.connect('pad-added', self.decodebin_pad_added)
150         self.pipe.add(decodebin)
151         decodebin.sync_state_with_parent()
152         self.webrtcbin.link(decodebin)
153
154     def decodebin_pad_added(self, element, pad):
155         log.info('decodebin_pad_added')
156         if not pad.has_current_caps():
157             log.info(pad, 'has no caps, ignoring')
158             return
159         caps = pad.get_current_caps()
160         padsize = caps.get_size()
161
162         for i in range(padsize):
163             s = caps.get_structure(i)  # Gst.Structure
164             name = s.get_name()
165             if name.startswith('video'):
166                 q = Gst.ElementFactory.make('queue')
167                 conv = Gst.ElementFactory.make('videoconvert')
168                 enc = Gst.ElementFactory.make('x264enc')
169                 enc.set_property('bitrate', 1000)
170                 enc.set_property('tune', 'zerolatency')
171                 capsfilter = Gst.ElementFactory.make('capsfilter')
172                 capsfilter.set_properties(Gst.Caps.from_string('video/x-h264,stream-format=(string)avc'))
173                 flmux = Gst.ElementFactory.make('flvmux')
174                 sink = Gst.ElementFactory.make('rtmpsink')
175                 sink.set_property('location', self.rtmp_uri)
176                 assert q and conv and enc and capsfilter and flmux and sink
177
178                 self.pipe.add(q)
179                 self.pipe.add(conv)
180                 self.pipe.add(enc)
181                 self.pipe.add(capsfilter)
182                 self.pipe.add(flmux)
183                 self.pipe.add(sink)
184
185                 q_pad_sink = q.get_static_pad('sink')
186                 assert q_pad_sink
187                 pad_link_return = pad.link(q_pad_sink)
188                 assert pad_link_return == Gst.PadLinkReturn.OK
189
190                 ok = q.link(conv)
191                 assert ok
192                 ok = conv.link(enc)
193                 assert ok
194                 ok = enc.link(capsfilter)
195                 assert ok
196                 ok = capsfilter.link(flmux)
197                 assert ok
198                 ok = flmux.link(sink)
199                 assert ok
200                 self.pipe.set_state(Gst.State.PLAYING)
201                 self.pipe.sync_children_states()
202
203             elif name.startswith('audio'):
204                 q = Gst.ElementFactory.make('queue')
205                 conv = Gst.ElementFactory.make('audioconvert')
206                 resample = Gst.ElementFactory.make('audioresample')
207                 sink = Gst.ElementFactory.make('autoaudiosink')
208                 self.pipe.add(q)
209                 self.pipe.add(conv)
210                 self.pipe.add(resample)
211                 self.pipe.add(sink)
212                 self.pipe.sync_children_states()
213                 pad.link(q.get_static_pad('sink'))
214                 q.link(conv)
215                 conv.link(resample)
216                 resample.link(sink)
217
218     def set_remote_desciption_done(self, gst_promise):
219         gst_promise = Gst.Promise.new_with_change_func(self.create_answer_done)
220         self.webrtcbin.emit('create-answer', None, gst_promise)
221
222     def create_answer_done(self, gst_promise):
223         reply = gst_promise.get_reply()
224         answer = reply.get_value('answer')
225         sdp_message = answer.sdp
226         mids = [sdp_message.get_media(i).get_attribute_val('mid')
227                 for i in range(sdp_message.medias_len())]
228         user_fragments = [sdp_message.get_media(i).get_attribute_val('ice-ufrag')
229                           for i in range(sdp_message.medias_len())]
230         self.events.sdp_info.put_nowait((mids, user_fragments))
231         sdp_answer = sdp_message.as_text()
232         log.info(f'Send SDP answer')
233         log.debug(f'SDP answer:\n{sdp_answer}')
234         self.events.sdp_answer.put_nowait(sdp_answer)
235         gst_promise = Gst.Promise.new_with_change_func(self.set_local_description_done)
236         self.webrtcbin.emit('set-local-description', answer, gst_promise)
237
238     def set_local_description_done(self, gst_promise):
239         gst_promise.get_reply()
240
241     async def run(self):
242         bus = Gst.Pipeline.get_bus(self.pipe)
243         self.pipe.set_state(Gst.State.PLAYING)
244         try:
245             while True:
246                 if bus.have_pending():
247                     msg = bus.pop()
248                     if msg.type == Gst.MessageType.ERROR:
249                         log.error(f'Error from gstreamer message bus: {msg.get_structure()}')
250                         return
251                     elif msg.type == Gst.MessageType.EOS:  # end of stream
252                         log.info(f'Gstreamer message bus reports end of stream')
253                         return
254                 elif self.events.sdp_offer.qsize() > 0:
255                     sdp_offer = self.events.sdp_offer.get_nowait()
256                     res, sm = GstSdp.SDPMessage.new()
257                     assert res == GstSdp.SDPResult.OK
258                     GstSdp.sdp_message_parse_buffer(bytes(sdp_offer.encode()), sm)
259                     # the three lines above can also be done this way in new versions of GStreamer:
260                     # sm = GstSdp.SDPMessage.new_from_text(sdp_offer)
261                     rd = GstWebRTC.WebRTCSessionDescription.new(GstWebRTC.WebRTCSDPType.OFFER, sm)
262                     gst_promise = Gst.Promise.new_with_change_func(self.set_remote_desciption_done)
263                     self.webrtcbin.emit('set-remote-description', rd, gst_promise)
264
265                 elif self.events.received_ice_candidates.qsize() > 0:
266                     ic = self.events.received_ice_candidates.get_nowait()
267                     if ic['candidate'] != '':
268                         self.webrtcbin.emit('add-ice-candidate', ic['sdpMLineIndex'], ic['candidate'])
269
270                 elif self.events.room_left.qsize() > 0:
271                     self.events.room_left.get_nowait()
272                     return
273
274                 else:
275                     await asyncio.sleep(0.1)
276         finally:
277             self.pipe.set_state(Gst.State.NULL)
278
279
280 async def run_room(laplace_uri: str, rtmp_uri: str):
281     try:
282         events = Events()
283         webrtc = WebRTCClient(events, rtmp_uri)
284         signaling = SignalingClient(events, laplace_uri)
285
286         webrtc_task = asyncio.Task(webrtc.run())
287         signaling_task = asyncio.Task(signaling.run())
288
289         done, pending = await asyncio.wait([webrtc_task, signaling_task], return_when=asyncio.FIRST_COMPLETED)
290
291         for task in done:
292             task.result()
293         for task in pending:
294             task.cancel()
295     except OSError as e:
296         print(e)
297
298
299 async def run_room_repeated(laplace_uri: str, rtmp_uri: str, sleep_time: float):
300     while True:
301         await run_room(laplace_uri, rtmp_uri)
302         await asyncio.sleep(sleep_time)
303
304
305 async def run_rooms(laplace_base_uri: str, rtmp_base_uri: str, rooms: List[str], retry: bool):
306     tasks = []
307     for room in rooms:
308         laplace_uri = laplace_base_uri + room  # TODO: encode
309         rtmp_uri = rtmp_base_uri + room  # TODO: encode
310         if retry:
311             tasks.append(run_room_repeated(laplace_uri, rtmp_uri, 2.))
312         else:
313             tasks.append(run_room(laplace_uri, rtmp_uri))
314     await asyncio.gather(*tasks)
315
316
317 def main():
318     logging.basicConfig(level=logging.DEBUG, format='%(asctime)-15s %(message)s')
319     default_source = 'wss://localhost:1234/ws_connect?id='
320     default_dest = 'rtmp://localhost:1935/'
321     default_room = 'cug'
322     parser = argparse.ArgumentParser()
323     parser.add_argument('-s', '--source', default=default_source,
324                         help=f'Laplace signalling websocket base URI, default: {default_source}')
325     parser.add_argument('-d', '--destination', default=default_dest,
326                         help=f'RTMP server base URI, default: {default_dest}')
327     parser.add_argument('-r', '--retry', action='store_true', help=f'Retry forever if room not found or closed')
328     parser.add_argument('room', nargs='*', help=f'Room names to be used, "{default_room}" if omitted')
329     args = parser.parse_args()
330
331     Gst.init(None)
332     rooms = args.room
333     if len(rooms) == 0:
334         rooms = [default_room]
335     asyncio.run(run_rooms(args.source, args.destination, rooms, args.retry))
336
337
338 if __name__ == '__main__':
339     main()