Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion getstream/video/rtc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Optional

from getstream.video.call import Call
from getstream.video.rtc.location_discovery import (
Expand Down Expand Up @@ -44,7 +45,7 @@ async def discover_location():


async def join(
call: Call, user_id: str = None, create=True, **kwargs
call: Call, user_id: Optional[str] = None, create=True, **kwargs
) -> ConnectionManager:
"""
Join a call. This method will:
Expand Down
9 changes: 1 addition & 8 deletions getstream/video/rtc/connection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ async def leave(self):
await self._network_monitor.stop_monitoring()
await self._peer_manager.close()
if self._ws_client:
await self._ws_client.close()
self._ws_client.close()
self._ws_client = None
if self._coordinator_ws_client:
await self._coordinator_ws_client.disconnect()
Expand Down Expand Up @@ -385,13 +385,6 @@ async def add_tracks(self, audio=None, video=None):
"""Add multiple audio and video tracks in a single negotiation."""
await self._peer_manager.add_tracks(audio, video)

async def addTrack(self, track, track_info=None):
"""Add a single track (backward compatibility)."""
if track.kind == "video":
await self.add_tracks(video=track)
else:
await self.add_tracks(audio=track)

async def start_recording(
self, recording_types, user_ids=None, output_dir="recordings"
):
Expand Down
37 changes: 14 additions & 23 deletions getstream/video/rtc/location_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import http.client
import functools
from typing import Optional, Protocol
from typing import Optional, Protocol, ContextManager
from contextlib import contextmanager

# Constants matching the Go implementation
Expand All @@ -23,12 +23,11 @@
class HTTPClient(Protocol):
"""Protocol defining the HTTP client interface."""

def request(self, method: str, url: str, body=None, headers=None, **kwargs):
def request(self, method: str, url: str, body=None, headers=None, **kwargs) -> None:
"""Make an HTTP request."""
...

@contextmanager
def response(self):
def response(self) -> ContextManager[http.client.HTTPResponse]:
"""Get the HTTP response."""
...

Expand Down Expand Up @@ -69,33 +68,25 @@ def discover(self, context=None) -> str:
Returns:
The 3-character location code (e.g. "IAD", "FRA")
"""
# Basic validation to match previous behavior and provide fast-fail
parsed_url = self.url.split("://", 1)
if len(parsed_url) != 2:
self.logger.warning("Invalid URL format: %s", self.url)
return FALLBACK_LOCATION_NAME

protocol, host_path = parsed_url
host = host_path.split("/", 1)[0]
path = "/" + host_path.split("/", 1)[1] if "/" in host_path else "/"

for i in range(self.max_retries):
self.logger.info("Discovering location, attempt %d", i + 1)
try:
if protocol.lower() == "https":
conn = http.client.HTTPSConnection(host, timeout=1)
else:
conn = http.client.HTTPConnection(host, timeout=1)

conn.request("HEAD", path)
response = conn.getresponse()

if response.status != 200:
self.logger.warning("Unexpected status code: %d", response.status)
continue

pop_name = response.getheader(HEADER_CLOUDFRONT_POP, "")
response.read() # Read and discard the response body
conn.close()
# Use injected HTTP client (or default) for requests
self.client.request("HEAD", self.url)
with self.client.response() as response:
if response.status != 200:
self.logger.warning(
"Unexpected status code: %d", response.status
)
continue

pop_name = response.getheader(HEADER_CLOUDFRONT_POP, "")

if len(pop_name) < 3:
self.logger.warning("Invalid pop name: %s", pop_name)
Expand Down
2 changes: 1 addition & 1 deletion getstream/video/rtc/pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async def on_track(track: aiortc.mediastreams.MediaStreamTrack):
handler = AudioTrackHandler(
relay.subscribe(track), lambda pcm: self.emit("audio", pcm, user)
)
asyncio.ensure_future(handler.start())
asyncio.create_task(handler.start())

self.emit("track_added", relay.subscribe(track), user)

Expand Down
3 changes: 1 addition & 2 deletions tests/rtc/test_location_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def setUp(self):
self.discovery = HTTPHintLocationDiscovery(
url=STREAM_PROD_URL,
max_retries=3,
client=self.client_mock,
logger=self.logger_mock,
)

Expand Down Expand Up @@ -67,7 +66,7 @@ def test_discover_success(self, mock_https_connection):
self.assertEqual(location, "IAD")

# Verify that the correct HTTP request was made
mock_conn.request.assert_called_once_with("HEAD", "/")
mock_conn.request.assert_called_once_with("HEAD", "/", None, {})
mock_response.getheader.assert_called_once_with(HEADER_CLOUDFRONT_POP, "")
mock_response.read.assert_called_once()
mock_conn.close.assert_called_once()
Expand Down