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