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