|
5 | 5 | from collections import deque |
6 | 6 | from fractions import Fraction |
7 | 7 | from http import HTTPStatus |
8 | | -from typing import TYPE_CHECKING |
| 8 | +from typing import TYPE_CHECKING, Union |
9 | 9 |
|
| 10 | +from aiortc import RTCIceGatherer, RTCIceServer |
10 | 11 | from picamera2.outputs import Output |
11 | 12 |
|
12 | 13 | from spyglass import WEBRTC_ENABLED |
@@ -78,9 +79,9 @@ def response_headers() -> None: |
78 | 79 | ): |
79 | 80 | response_headers() |
80 | 81 | handler.send_header("Access-Control-Expose-Headers", "Link") |
81 | | - ice_servers = get_ICE_servers() |
| 82 | + ice_servers = get_ICE_servers_header() |
82 | 83 | if ice_servers is not None: |
83 | | - handler.headers["Link"] = ice_servers |
| 84 | + handler.send_header("Link", ice_servers) |
84 | 85 | handler.end_headers() |
85 | 86 |
|
86 | 87 |
|
@@ -160,9 +161,9 @@ async def on_connectionstatechange() -> None: |
160 | 161 | "Access-Control-Expose-Headers", "ETag, ID, Accept-Patch, Link, Location" |
161 | 162 | ) |
162 | 163 | handler.send_header("Accept-Patch", "application/trickle-ice-sdpfrag") |
163 | | - ice_servers = get_ICE_servers() |
| 164 | + ice_servers = get_ICE_servers_header() |
164 | 165 | if ice_servers is not None: |
165 | | - handler.headers["Link"] = ice_servers |
| 166 | + handler.send_header("Link", ice_servers) |
166 | 167 | handler.send_header("Location", f"/whep/{secret}") |
167 | 168 | handler.send_header("Content-Length", str(len(pc.localDescription.sdp))) |
168 | 169 | handler.end_headers() |
@@ -196,8 +197,17 @@ async def do_PATCH_async(streaming_handler: StreamingHandler) -> None: |
196 | 197 | streaming_handler.end_headers() |
197 | 198 |
|
198 | 199 |
|
199 | | -def get_ICE_servers() -> None: |
200 | | - return None |
| 200 | +def get_ICE_servers_header() -> str | None: |
| 201 | + links = [] |
| 202 | + ice_servers: list[RTCIceServer] = RTCIceGatherer.getDefaultIceServers() |
| 203 | + for ice in ice_servers: |
| 204 | + urls: Union[str, list[str]] = ice.urls |
| 205 | + if isinstance(urls, str): |
| 206 | + urls = [urls] |
| 207 | + |
| 208 | + links.extend([f'<{url}>; rel="ice-server"' for url in urls if url]) |
| 209 | + |
| 210 | + return ",".join(links) if links else None |
201 | 211 |
|
202 | 212 |
|
203 | 213 | def parse_ice_candidates(sdp_message: str) -> list[RTCIceCandidate]: |
|
0 commit comments