Mercurial > libervia-backend
comparison libervia/backend/plugins/plugin_xep_0343.py @ 4270:0d7bb4df2343
Reformatted code base using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 19 Jun 2024 18:44:57 +0200 |
parents | 79c8a70e1813 |
children | 39ac821ebbdb |
comparison
equal
deleted
inserted
replaced
4269:64a85ce8be70 | 4270:0d7bb4df2343 |
---|---|
28 from libervia.backend.core import exceptions | 28 from libervia.backend.core import exceptions |
29 from libervia.backend.core.constants import Const as C | 29 from libervia.backend.core.constants import Const as C |
30 from libervia.backend.core.i18n import _ | 30 from libervia.backend.core.i18n import _ |
31 from libervia.backend.core.log import getLogger | 31 from libervia.backend.core.log import getLogger |
32 from libervia.backend.core.xmpp import SatXMPPEntity | 32 from libervia.backend.core.xmpp import SatXMPPEntity |
33 from libervia.backend.plugins.plugin_xep_0166.models import BaseTransportHandler, ContentData | 33 from libervia.backend.plugins.plugin_xep_0166.models import ( |
34 BaseTransportHandler, | |
35 ContentData, | |
36 ) | |
34 from libervia.backend.tools.common import data_format | 37 from libervia.backend.tools.common import data_format |
35 | 38 |
36 from .plugin_xep_0167 import mapping | 39 from .plugin_xep_0167 import mapping |
37 | 40 |
38 log = getLogger(__name__) | 41 log = getLogger(__name__) |
48 C.PI_RECOMMENDATIONS: [], | 51 C.PI_RECOMMENDATIONS: [], |
49 C.PI_MAIN: "XEP_0343", | 52 C.PI_MAIN: "XEP_0343", |
50 C.PI_HANDLER: "yes", | 53 C.PI_HANDLER: "yes", |
51 C.PI_DESCRIPTION: _("""Use WebRTC to create a generic data transport."""), | 54 C.PI_DESCRIPTION: _("""Use WebRTC to create a generic data transport."""), |
52 } | 55 } |
53 NS_JINGLE_WEBRTC_DATACHANNELS: Final[ | 56 NS_JINGLE_WEBRTC_DATACHANNELS: Final[str] = ( |
54 str | 57 "urn:xmpp:jingle:transports:webrtc-datachannel:1" |
55 ] = "urn:xmpp:jingle:transports:webrtc-datachannel:1" | 58 ) |
56 | 59 |
57 | 60 |
58 class XEP_0343(BaseTransportHandler): | 61 class XEP_0343(BaseTransportHandler): |
59 namespace = NS_JINGLE_WEBRTC_DATACHANNELS | 62 namespace = NS_JINGLE_WEBRTC_DATACHANNELS |
60 | 63 |
71 host.trigger.add( | 74 host.trigger.add( |
72 "XEP-0167_generate_sdp_content", self._generate_sdp_content_trigger | 75 "XEP-0167_generate_sdp_content", self._generate_sdp_content_trigger |
73 ) | 76 ) |
74 host.trigger.add("XEP-0176_jingle_handler_send_buffer", self._on_send_ice_buffer) | 77 host.trigger.add("XEP-0176_jingle_handler_send_buffer", self._on_send_ice_buffer) |
75 host.trigger.add("XEP-0176_ice_candidate_send", self._on_ice_candidate_send) | 78 host.trigger.add("XEP-0176_ice_candidate_send", self._on_ice_candidate_send) |
76 host.trigger.add( | 79 host.trigger.add("XEP-0234_file_jingle_send", self._file_jingle_send) |
77 "XEP-0234_file_jingle_send", self._file_jingle_send | |
78 ) | |
79 | 80 |
80 def get_handler(self, client: SatXMPPEntity): | 81 def get_handler(self, client: SatXMPPEntity): |
81 return XEP0343Handler() | 82 return XEP0343Handler() |
82 | 83 |
83 def is_usable(self, client, content_data: ContentData) -> bool: | 84 def is_usable(self, client, content_data: ContentData) -> bool: |
113 content_data: dict, | 114 content_data: dict, |
114 sdp_lines: list[str], | 115 sdp_lines: list[str], |
115 application_data: dict, | 116 application_data: dict, |
116 app_data_key: str, | 117 app_data_key: str, |
117 media_data: dict, | 118 media_data: dict, |
118 media: str | 119 media: str, |
119 ) -> None: | 120 ) -> None: |
120 """Generate "sctp-port" and "max-message-size" attributes""" | 121 """Generate "sctp-port" and "max-message-size" attributes""" |
121 transport_data = content_data["transport_data"] | 122 transport_data = content_data["transport_data"] |
122 sctp_port = transport_data.get("sctp-port") | 123 sctp_port = transport_data.get("sctp-port") |
123 if sctp_port is not None: | 124 if sctp_port is not None: |
125 | 126 |
126 max_message_size = transport_data.get("max-message-size") | 127 max_message_size = transport_data.get("max-message-size") |
127 if max_message_size is not None: | 128 if max_message_size is not None: |
128 sdp_lines.append(f"a=max-message-size:{max_message_size}") | 129 sdp_lines.append(f"a=max-message-size:{max_message_size}") |
129 | 130 |
130 def _wrap_transport_element( | 131 def _wrap_transport_element(self, transport_elt: domish.Element) -> None: |
131 self, | |
132 transport_elt: domish.Element | |
133 ) -> None: | |
134 """Wrap the XEP-0176 transport in a transport with this XEP namespace | 132 """Wrap the XEP-0176 transport in a transport with this XEP namespace |
135 | 133 |
136 @param transport_elt: ICE UDP <transport>. Must be already a child of a <content> | 134 @param transport_elt: ICE UDP <transport>. Must be already a child of a <content> |
137 element. | 135 element. |
138 """ | 136 """ |
150 client: SatXMPPEntity, | 148 client: SatXMPPEntity, |
151 session: dict, | 149 session: dict, |
152 content_name: str, | 150 content_name: str, |
153 content_data: dict, | 151 content_data: dict, |
154 transport_elt: domish.Element, | 152 transport_elt: domish.Element, |
155 iq_elt: domish.Element | 153 iq_elt: domish.Element, |
156 ) -> bool: | 154 ) -> bool: |
157 if content_data["transport"].handler == self: | 155 if content_data["transport"].handler == self: |
158 self._wrap_transport_element(transport_elt) | 156 self._wrap_transport_element(transport_elt) |
159 return True | 157 return True |
160 | 158 |
163 client: SatXMPPEntity, | 161 client: SatXMPPEntity, |
164 session: dict, | 162 session: dict, |
165 media_ice_data: dict[str, dict], | 163 media_ice_data: dict[str, dict], |
166 content_name: str, | 164 content_name: str, |
167 content_data: dict, | 165 content_data: dict, |
168 iq_elt: domish.Element | 166 iq_elt: domish.Element, |
169 ) -> bool: | 167 ) -> bool: |
170 if content_data["transport"].handler == self: | 168 if content_data["transport"].handler == self: |
171 transport_elt = iq_elt.jingle.content.transport | 169 transport_elt = iq_elt.jingle.content.transport |
172 if transport_elt.uri != self._ice_udp.namespace: | 170 if transport_elt.uri != self._ice_udp.namespace: |
173 raise exceptions.InternalError("Was expecting an ICE UDP transport") | 171 raise exceptions.InternalError("Was expecting an ICE UDP transport") |
174 self._wrap_transport_element(transport_elt) | 172 self._wrap_transport_element(transport_elt) |
175 return True | 173 return True |
176 | 174 |
177 async def _file_jingle_send( | 175 async def _file_jingle_send( |
178 self, | 176 self, client: SatXMPPEntity, peer_jid: jid.JID, content: dict |
179 client: SatXMPPEntity, | |
180 peer_jid: jid.JID, | |
181 content: dict | |
182 ) -> None: | 177 ) -> None: |
183 call_data = content["app_kwargs"]["extra"].pop("call_data", None) | 178 call_data = content["app_kwargs"]["extra"].pop("call_data", None) |
184 if call_data: | 179 if call_data: |
185 metadata = self._rtp.parse_call_data(call_data) | 180 metadata = self._rtp.parse_call_data(call_data) |
186 try: | 181 try: |
187 application_data = call_data["application"] | 182 application_data = call_data["application"] |
188 except KeyError: | 183 except KeyError: |
189 raise exceptions.DataError( | 184 raise exceptions.DataError('"call_data" must have an application media.') |
190 '"call_data" must have an application media.' | |
191 ) | |
192 try: | 185 try: |
193 content["transport_data"] = { | 186 content["transport_data"] = { |
194 "sctp-port": metadata["sctp-port"], | 187 "sctp-port": metadata["sctp-port"], |
195 "max-message-size": metadata.get("max-message-size", 65536), | 188 "max-message-size": metadata.get("max-message-size", 65536), |
196 "local_ice_data": { | 189 "local_ice_data": { |
197 "ufrag": metadata["ice-ufrag"], | 190 "ufrag": metadata["ice-ufrag"], |
198 "pwd": metadata["ice-pwd"], | 191 "pwd": metadata["ice-pwd"], |
199 "candidates": application_data.pop("ice-candidates"), | 192 "candidates": application_data.pop("ice-candidates"), |
200 "fingerprint": application_data.pop("fingerprint", {}), | 193 "fingerprint": application_data.pop("fingerprint", {}), |
201 } | 194 }, |
202 } | 195 } |
203 except KeyError as e: | 196 except KeyError as e: |
204 raise exceptions.DataError(f"Mandatory key is missing: {e}") | 197 raise exceptions.DataError(f"Mandatory key is missing: {e}") |
205 | 198 |
206 async def jingle_session_init( | 199 async def jingle_session_init( |
210 content_name: str, | 203 content_name: str, |
211 ) -> domish.Element: | 204 ) -> domish.Element: |
212 content_data = session["contents"][content_name] | 205 content_data = session["contents"][content_name] |
213 transport_data = content_data["transport_data"] | 206 transport_data = content_data["transport_data"] |
214 ice_transport_elt = await self._ice_udp.jingle_session_init( | 207 ice_transport_elt = await self._ice_udp.jingle_session_init( |
215 client, | 208 client, session, content_name |
216 session, | |
217 content_name | |
218 ) | 209 ) |
219 transport_elt = domish.Element( | 210 transport_elt = domish.Element( |
220 (NS_JINGLE_WEBRTC_DATACHANNELS, "transport"), | 211 (NS_JINGLE_WEBRTC_DATACHANNELS, "transport"), |
221 attribs={ | 212 attribs={ |
222 "sctp-port": str(transport_data["sctp-port"]), | 213 "sctp-port": str(transport_data["sctp-port"]), |
223 "max-message-size": str(transport_data["max-message-size"]) | 214 "max-message-size": str(transport_data["max-message-size"]), |
224 } | 215 }, |
225 ) | 216 ) |
226 transport_elt.addChild(ice_transport_elt) | 217 transport_elt.addChild(ice_transport_elt) |
227 return transport_elt | 218 return transport_elt |
228 | 219 |
229 async def _call_ice_udp_handler( | 220 async def _call_ice_udp_handler( |