Skip to content

Commit 18f07fd

Browse files
fkwpanoadragon453
andauthored
Add MatrixRTC backend/services discovery endpoint (#18967)
Co-authored-by: Andrew Morgan <[email protected]>
1 parent e3344dc commit 18f07fd

File tree

10 files changed

+285
-0
lines changed

10 files changed

+285
-0
lines changed

changelog.d/18967.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add experimental implementation for the latest draft of [MSC4143](https://github.com/matrix-org/matrix-spec-proposals/pull/4143).

docs/usage/configuration/config_documentation.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,6 +2573,28 @@ Example configuration:
25732573
turn_allow_guests: false
25742574
```
25752575
---
2576+
### `matrix_rtc`
2577+
2578+
*(object)* Options related to MatrixRTC. Defaults to `{}`.
2579+
2580+
This setting has the following sub-options:
2581+
2582+
* `transports` (array): A list of transport types and arguments to use for MatrixRTC connections. Defaults to `[]`.
2583+
2584+
Options for each entry include:
2585+
2586+
* `type` (string): The type of transport to use to connect to the selective forwarding unit (SFU).
2587+
2588+
* `livekit_service_url` (string): The base URL of the LiveKit service. Should only be used with LiveKit-based transports.
2589+
2590+
Example configuration:
2591+
```yaml
2592+
matrix_rtc:
2593+
transports:
2594+
- type: livekit
2595+
livekit_service_url: https://matrix-rtc.example.com/livekit/jwt
2596+
```
2597+
---
25762598
## Registration
25772599

25782600
Registration can be rate-limited using the parameters in the [Ratelimiting](#ratelimiting) section of this manual.

schema/synapse-config.schema.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,35 @@ properties:
28842884
default: true
28852885
examples:
28862886
- false
2887+
matrix_rtc:
2888+
type: object
2889+
description: >-
2890+
Options related to MatrixRTC.
2891+
properties:
2892+
transports:
2893+
type: array
2894+
items:
2895+
type: object
2896+
required:
2897+
- type
2898+
properties:
2899+
type:
2900+
type: string
2901+
description: The type of transport to use to connect to the selective forwarding unit (SFU).
2902+
example: livekit
2903+
livekit_service_url:
2904+
type: string
2905+
description: >-
2906+
The base URL of the LiveKit service. Should only be used with LiveKit-based transports.
2907+
example: https://matrix-rtc.example.com/livekit/jwt
2908+
description:
2909+
A list of transport types and arguments to use for MatrixRTC connections.
2910+
default: []
2911+
default: {}
2912+
examples:
2913+
- transports:
2914+
- type: livekit
2915+
livekit_service_url: https://matrix-rtc.example.com/livekit/jwt
28872916
enable_registration:
28882917
type: boolean
28892918
description: >-

synapse/config/_base.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ from synapse.config import ( # noqa: F401
3737
key,
3838
logger,
3939
mas,
40+
matrixrtc,
4041
metrics,
4142
modules,
4243
oembed,
@@ -126,6 +127,7 @@ class RootConfig:
126127
auto_accept_invites: auto_accept_invites.AutoAcceptInvitesConfig
127128
user_types: user_types.UserTypesConfig
128129
mas: mas.MasConfig
130+
matrix_rtc: matrixrtc.MatrixRtcConfig
129131

130132
config_classes: List[Type["Config"]] = ...
131133
config_files: List[str]

synapse/config/experimental.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ def read_config(
556556
# MSC4133: Custom profile fields
557557
self.msc4133_enabled: bool = experimental.get("msc4133_enabled", False)
558558

559+
# MSC4143: Matrix RTC Transport using Livekit Backend
560+
self.msc4143_enabled: bool = experimental.get("msc4143_enabled", False)
561+
559562
# MSC4169: Backwards-compatible redaction sending using `/send`
560563
self.msc4169_enabled: bool = experimental.get("msc4169_enabled", False)
561564

synapse/config/homeserver.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from .key import KeyConfig
3838
from .logger import LoggingConfig
3939
from .mas import MasConfig
40+
from .matrixrtc import MatrixRtcConfig
4041
from .metrics import MetricsConfig
4142
from .modules import ModulesConfig
4243
from .oembed import OembedConfig
@@ -80,6 +81,7 @@ class HomeServerConfig(RootConfig):
8081
OembedConfig,
8182
CaptchaConfig,
8283
VoipConfig,
84+
MatrixRtcConfig,
8385
RegistrationConfig,
8486
AccountValidityConfig,
8587
MetricsConfig,

synapse/config/matrixrtc.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# This file is licensed under the Affero General Public License (AGPL) version 3.
3+
#
4+
# Copyright (C) 2025 New Vector, Ltd
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Affero General Public License as
8+
# published by the Free Software Foundation, either version 3 of the
9+
# License, or (at your option) any later version.
10+
#
11+
# See the GNU Affero General Public License for more details:
12+
# <https://www.gnu.org/licenses/agpl-3.0.html>.
13+
#
14+
# [This file includes modifications made by New Vector Limited]
15+
#
16+
#
17+
18+
from typing import Any, Optional
19+
20+
from pydantic import ValidationError
21+
22+
from synapse._pydantic_compat import Field, StrictStr, validator
23+
from synapse.types import JsonDict
24+
from synapse.util.pydantic_models import ParseModel
25+
26+
from ._base import Config, ConfigError
27+
28+
29+
class TransportConfigModel(ParseModel):
30+
type: StrictStr
31+
32+
livekit_service_url: Optional[StrictStr] = Field(default=None)
33+
"""An optional livekit service URL. Only required if type is "livekit"."""
34+
35+
@validator("livekit_service_url", always=True)
36+
def validate_livekit_service_url(cls, v: Any, values: dict) -> Any:
37+
if values.get("type") == "livekit" and not v:
38+
raise ValueError(
39+
"You must set a `livekit_service_url` when using the 'livekit' transport."
40+
)
41+
42+
return v
43+
44+
45+
class MatrixRtcConfigModel(ParseModel):
46+
transports: list = []
47+
48+
49+
class MatrixRtcConfig(Config):
50+
section = "matrix_rtc"
51+
52+
def read_config(
53+
self, config: JsonDict, allow_secrets_in_config: bool, **kwargs: Any
54+
) -> None:
55+
matrix_rtc = config.get("matrix_rtc", {})
56+
if matrix_rtc is None:
57+
matrix_rtc = {}
58+
59+
try:
60+
parsed = MatrixRtcConfigModel(**matrix_rtc)
61+
except ValidationError as e:
62+
raise ConfigError(
63+
"Could not validate matrix_rtc config",
64+
("matrix_rtc",),
65+
) from e
66+
67+
self.transports = parsed.transports

synapse/rest/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
login,
4343
login_token_request,
4444
logout,
45+
matrixrtc,
4546
mutual_rooms,
4647
notifications,
4748
openid,
@@ -89,6 +90,7 @@
8990
presence.register_servlets,
9091
directory.register_servlets,
9192
voip.register_servlets,
93+
matrixrtc.register_servlets,
9294
pusher.register_servlets,
9395
push_rule.register_servlets,
9496
logout.register_servlets,

synapse/rest/client/matrixrtc.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# This file is licensed under the Affero General Public License (AGPL) version 3.
3+
#
4+
# Copyright (C) 2025 New Vector, Ltd
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Affero General Public License as
8+
# published by the Free Software Foundation, either version 3 of the
9+
# License, or (at your option) any later version.
10+
#
11+
# See the GNU Affero General Public License for more details:
12+
# <https://www.gnu.org/licenses/agpl-3.0.html>.
13+
#
14+
# [This file includes modifications made by New Vector Limited]
15+
#
16+
#
17+
18+
from typing import TYPE_CHECKING, Tuple
19+
20+
from synapse.http.server import HttpServer
21+
from synapse.http.servlet import RestServlet
22+
from synapse.http.site import SynapseRequest
23+
from synapse.rest.client._base import client_patterns
24+
from synapse.types import JsonDict
25+
26+
if TYPE_CHECKING:
27+
from synapse.server import HomeServer
28+
29+
30+
class MatrixRTCRestServlet(RestServlet):
31+
PATTERNS = client_patterns(r"/org\.matrix\.msc4143/rtc/transports$", releases=())
32+
CATEGORY = "Client API requests"
33+
34+
def __init__(self, hs: "HomeServer"):
35+
super().__init__()
36+
self._hs = hs
37+
self._auth = hs.get_auth()
38+
self._transports = hs.config.matrix_rtc.transports
39+
40+
async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
41+
# Require authentication for this endpoint.
42+
await self._auth.get_user_by_req(request)
43+
44+
if self._transports:
45+
return 200, {"rtc_transports": self._transports}
46+
47+
return 200, {}
48+
49+
50+
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
51+
if hs.config.experimental.msc4143_enabled:
52+
MatrixRTCRestServlet(hs).register(http_server)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#
2+
# This file is licensed under the Affero General Public License (AGPL) version 3.
3+
#
4+
# Copyright (C) 2025 New Vector, Ltd
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Affero General Public License as
8+
# published by the Free Software Foundation, either version 3 of the
9+
# License, or (at your option) any later version.
10+
#
11+
# See the GNU Affero General Public License for more details:
12+
# <https://www.gnu.org/licenses/agpl-3.0.html>.
13+
#
14+
# [This file includes modifications made by New Vector Limited]
15+
#
16+
#
17+
18+
"""Tests REST events for /rtc/endpoints path."""
19+
20+
from twisted.internet.testing import MemoryReactor
21+
22+
from synapse.rest import admin
23+
from synapse.rest.client import login, matrixrtc, register, room
24+
from synapse.server import HomeServer
25+
from synapse.util.clock import Clock
26+
27+
from tests.unittest import HomeserverTestCase, override_config
28+
29+
PATH_PREFIX = "/_matrix/client/unstable/org.matrix.msc4143"
30+
RTC_ENDPOINT = {"type": "focusA", "required_field": "theField"}
31+
LIVEKIT_ENDPOINT = {
32+
"type": "livekit",
33+
"livekit_service_url": "https://livekit.example.com",
34+
}
35+
36+
37+
class MatrixRtcTestCase(HomeserverTestCase):
38+
"""Tests /rtc/transports Client-Server REST API."""
39+
40+
servlets = [
41+
admin.register_servlets,
42+
room.register_servlets,
43+
login.register_servlets,
44+
register.register_servlets,
45+
matrixrtc.register_servlets,
46+
]
47+
48+
def prepare(
49+
self, reactor: MemoryReactor, clock: Clock, homeserver: HomeServer
50+
) -> None:
51+
self.register_user("alice", "password")
52+
self._alice_tok = self.login("alice", "password")
53+
54+
def test_matrixrtc_endpoint_not_enabled(self) -> None:
55+
channel = self.make_request(
56+
"GET", f"{PATH_PREFIX}/rtc/transports", access_token=self._alice_tok
57+
)
58+
self.assertEqual(404, channel.code, channel.json_body)
59+
self.assertEqual(
60+
"M_UNRECOGNIZED", channel.json_body["errcode"], channel.json_body
61+
)
62+
63+
@override_config({"experimental_features": {"msc4143_enabled": True}})
64+
def test_matrixrtc_endpoint_requires_authentication(self) -> None:
65+
channel = self.make_request("GET", f"{PATH_PREFIX}/rtc/transports")
66+
self.assertEqual(401, channel.code, channel.json_body)
67+
68+
@override_config(
69+
{
70+
"experimental_features": {"msc4143_enabled": True},
71+
"matrix_rtc": {"transports": [RTC_ENDPOINT]},
72+
}
73+
)
74+
def test_matrixrtc_endpoint_contains_expected_transport(self) -> None:
75+
channel = self.make_request(
76+
"GET", f"{PATH_PREFIX}/rtc/transports", access_token=self._alice_tok
77+
)
78+
self.assertEqual(200, channel.code, channel.json_body)
79+
self.assert_dict({"rtc_transports": [RTC_ENDPOINT]}, channel.json_body)
80+
81+
@override_config(
82+
{
83+
"experimental_features": {"msc4143_enabled": True},
84+
"matrix_rtc": {"transports": []},
85+
}
86+
)
87+
def test_matrixrtc_endpoint_no_transports_configured(self) -> None:
88+
channel = self.make_request(
89+
"GET", f"{PATH_PREFIX}/rtc/transports", access_token=self._alice_tok
90+
)
91+
self.assertEqual(200, channel.code, channel.json_body)
92+
self.assert_dict({}, channel.json_body)
93+
94+
@override_config(
95+
{
96+
"experimental_features": {"msc4143_enabled": True},
97+
"matrix_rtc": {"transports": [LIVEKIT_ENDPOINT]},
98+
}
99+
)
100+
def test_matrixrtc_endpoint_livekit_transport(self) -> None:
101+
channel = self.make_request(
102+
"GET", f"{PATH_PREFIX}/rtc/transports", access_token=self._alice_tok
103+
)
104+
self.assertEqual(200, channel.code, channel.json_body)
105+
self.assert_dict({"rtc_transports": [LIVEKIT_ENDPOINT]}, channel.json_body)

0 commit comments

Comments
 (0)