Improve logging.
[toast/stream2beamer.git] / laplace_client.py
1 #!/usr/bin/python3
2
3 import argparse
4 import asyncio
5 import json
6 import logging
7 import pathlib
8 import ssl
9 import sys
10
11 import websockets
12
13 import gi
14 gi.require_version('Gst', '1.0')
15 from gi.repository import Gst
16 gi.require_version('GstWebRTC', '1.0')
17 from gi.repository import GstWebRTC
18 gi.require_version('GstSdp', '1.0')
19 from gi.repository import GstSdp
20
21
22 log = logging.getLogger(__name__)
23
24
25 class WebRTCClient:
26
27     def __init__(self, uri: str):
28         self.uri = uri
29         self.ssl_context = ssl.SSLContext()
30         self.ssl_context.check_hostname = False
31         self.ssl_context.verify_mode = ssl.CERT_NONE
32         self.websocket = None
33         self.session_id = None
34         self.userfragments = []
35
36     def send_sdp_offer(self, offer):
37         text = offer.sdp.as_text()
38         log.info(f'send_sdp_offer with {text}')
39         msg = json.dumps({
40             'SessionID': self.session_id,
41             'Type': "gotAnswer",
42             'Value': json.dumps({
43                 'type': 'answer',
44                 'sdp': text
45             })
46         })
47         loop = asyncio.new_event_loop()
48         loop.run_until_complete(self.websocket.send(msg))
49         loop.close()
50
51     def on_offer_created(self, promise, _, __):
52         log.info('on_offer_created')
53         promise.wait()
54         reply = promise.get_reply()
55         offer = reply.get_value('offer')
56         promise = Gst.Promise.new()
57         self.webrtc.emit('set-local-description', offer, promise)
58         promise.interrupt()
59         self.send_sdp_offer(offer)
60
61         sdp = offer.sdp
62         self.userfragments = [sdp.get_media(i).get_attribute_val('ice-ufrag') for i in range(sdp.medias_len())]
63
64     def on_negotiation_needed(self, element):
65         log.info('on_negotiation_needed')
66         promise = Gst.Promise.new_with_change_func(self.on_offer_created, element, None)
67         element.emit('create-offer', None, promise)
68
69     def send_ice_candidate_message(self, _, mlineindex, candidate):
70         icemsg = json.dumps({
71             'SessionID': self.session_id,
72             'Type': 'addCalleeIceCandidate',
73             'Value': json.dumps({
74                 "candidate": candidate,
75                 "sdpMid": f"{mlineindex}",
76                 "sdpMLineIndex": mlineindex,
77                 "usernameFragment": self.userfragments[mlineindex],
78                 })
79             })
80         log.info(f'send_ice_candidate_message with {icemsg}')
81         loop = asyncio.new_event_loop()
82         loop.run_until_complete(self.websocket.send(icemsg))
83         loop.close()
84
85     def on_incoming_decodebin_stream(self, _, pad):
86         log.info('on_incoming_decodebin_stream')
87         if not pad.has_current_caps():
88             log.info(pad, 'has no caps, ignoring')
89             return
90
91         caps = pad.get_current_caps()
92         assert (len(caps))
93         s = caps[0]
94         name = s.get_name()
95         if name.startswith('video'):
96             q = Gst.ElementFactory.make('queue')
97             conv = Gst.ElementFactory.make('videoconvert')
98             sink = Gst.ElementFactory.make('autovideosink')
99             self.pipe.add(q, conv, sink)
100             self.pipe.sync_children_states()
101             pad.link(q.get_static_pad('sink'))
102             q.link(conv)
103             conv.link(sink)
104         elif name.startswith('audio'):
105             q = Gst.ElementFactory.make('queue')
106             conv = Gst.ElementFactory.make('audioconvert')
107             resample = Gst.ElementFactory.make('audioresample')
108             sink = Gst.ElementFactory.make('autoaudiosink')
109             self.pipe.add(q, conv, resample, sink)
110             self.pipe.sync_children_states()
111             pad.link(q.get_static_pad('sink'))
112             q.link(conv)
113             conv.link(resample)
114             resample.link(sink)
115
116     def on_incoming_stream(self, _, pad):
117         log.info('on_incoming_stream')
118         if pad.direction != Gst.PadDirection.SRC:
119             return
120         decodebin = Gst.ElementFactory.make('decodebin')
121         decodebin.connect('pad-added', self.on_incoming_decodebin_stream)
122         self.pipe.add(decodebin)
123         decodebin.sync_state_with_parent()
124         self.webrtc.link(decodebin)
125
126     def start_pipeline(self):
127         self.webrtc = Gst.ElementFactory.make('webrtcbin', 'laplace')
128         # self.webrtc.set_property("bundle-policy", 3)
129         direction = GstWebRTC.WebRTCRTPTransceiverDirection.RECVONLY
130         video_caps = Gst.caps_from_string("application/x-rtp,media=video,encoding-name=VP8/9000,payload=96")
131         audio_caps = Gst.caps_from_string("application/x-rtp,media=audio,encoding-name=OPUS,clock-rate=48000,payload=111")
132         self.webrtc.emit('add-transceiver', direction, video_caps)
133         self.webrtc.emit('add-transceiver', direction, audio_caps)
134         self.pipe = Gst.Pipeline.new("pipeline")
135         Gst.Bin.do_add_element(self.pipe, self.webrtc)
136         self.webrtc.connect('on-negotiation-needed', self.on_negotiation_needed)
137         self.webrtc.connect('on-ice-candidate', self.send_ice_candidate_message)
138         self.webrtc.connect('pad-added', self.on_incoming_stream)
139         self.pipe.set_state(Gst.State.PLAYING)
140         self.webrtc.emit('create-data-channel', 'laplace', None)
141     
142     def close_pipeline(self):
143         self.pipe.set_state(Gst.State.NULL)
144         self.pipe = None
145         self.webrtc = None
146     
147     def handle_sdp(self, sdp):
148         log.info(f'handle_sdp: {sdp}')
149         res, sdpmsg = GstSdp.SDPMessage.new()
150         GstSdp.sdp_message_parse_buffer(bytes(sdp.encode()), sdpmsg)
151         answer = GstWebRTC.WebRTCSessionDescription.new(GstWebRTC.WebRTCSDPType.ANSWER, sdpmsg)
152         promise = Gst.Promise.new()
153         self.webrtc.emit('set-remote-description', answer, promise)
154         promise.interrupt()
155
156     def handle_ice(self, ice):
157         log.info(f'handle_ice: {ice}')
158         candidate = ice['candidate']
159         sdpmlineindex = ice['sdpMLineIndex']
160         self.webrtc.emit('add-ice-candidate', sdpmlineindex, candidate)
161
162     async def run(self):
163         try:
164             async with websockets.connect(self.uri, ssl=self.ssl_context) as websocket:
165                 self.websocket = websocket
166                 self.start_pipeline()
167                 async for msg in websocket:
168                     msg_json = json.loads(msg)
169                     msg_type = msg_json['Type']
170                     msg_value = msg_json['Value']
171                     session_id = msg_json['SessionID']
172                     log.info(f"receive for session {session_id} type {msg_type}")
173                     if msg_type == 'newSession':
174                         self.session_id = session_id
175                     elif msg_type == 'gotOffer':
176                         value_json = json.loads(msg_value)
177                         sdp = value_json['sdp']
178                         self.handle_sdp(sdp)
179                     elif msg_type == 'addCallerIceCandidate':
180                         value_json = json.loads(msg_value)
181                         self.handle_ice(value_json)
182             self.close_pipeline()
183             self.websocket = None
184             self.session_id = None
185         except:
186             log.error(f'Connection to "{self.uri}" failed')
187             sys.exit(1)
188
189
190 def check_plugins():
191     for plugin in ["opus", "vpx", "nice", "webrtc", "dtls", "srtp", "rtp",
192                    "rtpmanager", "videotestsrc", "audiotestsrc"]:
193         if Gst.Registry.get().find_plugin(plugin) is None:
194             print('Missing gstreamer plugin:', plugin)
195             return False
196     return True
197
198
199 def main():
200     logging.basicConfig(level=logging.INFO, format='%(asctime)-15s %(message)s')
201     Gst.init(None)
202     if not check_plugins():
203         sys.exit(1)
204     parser = argparse.ArgumentParser()
205     parser.add_argument('--uri', default='wss://localhost:1234/ws_connect?id=cug',
206         help='Signalling server URI')
207     args = parser.parse_args()
208     c = WebRTCClient(args.uri)
209     loop = asyncio.get_event_loop()
210     loop.run_until_complete(c.run())
211
212
213 if __name__=='__main__':
214     main()