Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebRTC class separation
Browse files Browse the repository at this point in the history
felipecrs committed Nov 27, 2024
1 parent 908603e commit 1e3e713
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions custom_components/frigate/camera.py
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ async def async_setup_entry(

async_add_entities(
[
FrigateCamera(
FrigateCameraWebRTC(
entry,
cam_name,
frigate_client,
@@ -87,7 +87,7 @@ async def async_setup_entry(
for cam_name, camera_config in frigate_config["cameras"].items()
]
+ (
[BirdseyeCamera(entry, frigate_client)]
[BirdseyeCameraWebRTC(entry, frigate_client)]
if frigate_config.get("birdseye", {}).get("restream", False)
else []
)
@@ -275,11 +275,6 @@ def supported_features(self) -> CameraEntityFeature:
return CameraEntityFeature(0)
return CameraEntityFeature.STREAM

@property
def frontend_stream_type(self) -> StreamType | None:
"""Return the type of stream supported by this camera."""
return StreamType.WEB_RTC

async def async_camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
@@ -310,21 +305,6 @@ async def async_enable_motion_detection(self) -> None:
False,
)

async def async_handle_async_webrtc_offer(
self, offer_sdp: str, session_id: str, send_message: WebRTCSendMessage
) -> None:
"""Handle the WebRTC offer and return an answer."""
websession = async_get_clientsession(self.hass)
url = f"{self._url}/api/go2rtc/webrtc?src={self._cam_name}"
payload = {"type": "offer", "sdp": offer_sdp}
async with websession.post(url, json=payload) as resp:
answer = await resp.json()
send_message(WebRTCAnswer(answer["sdp"]))

async def async_on_webrtc_candidate(self, session_id: str, candidate: Any) -> None:
"""Ignore WebRTC candidates for Frigate cameras."""
return

async def async_disable_motion_detection(self) -> None:
"""Disable motion detection for this camera."""
await async_publish(
@@ -362,7 +342,7 @@ async def ptz(self, action: str, argument: str) -> None:


class BirdseyeCamera(FrigateEntity, Camera):
"""Representation of the Frigate birdseye camera."""
"""Frigate birdseye camera."""

# sets the entity name to same as device name ex: camera.front_doorbell
_attr_name = None
@@ -428,11 +408,6 @@ def supported_features(self) -> CameraEntityFeature:
"""Return supported features of this camera."""
return CameraEntityFeature.STREAM

@property
def frontend_stream_type(self) -> StreamType | None:
"""Return the type of stream supported by this camera."""
return StreamType.WEB_RTC

async def async_camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
@@ -453,6 +428,15 @@ async def stream_source(self) -> str | None:
"""Return the source of the stream."""
return self._stream_source


class WebRTCBase:
# TODO: this property can be removed after this fix is released:
# https://github.com/home-assistant/core/pull/130932/files#diff-75655c0eec1c3e736cad1bdb5627100a4595ece9accc391b5c85343bb998594fR598-R603
@property
def frontend_stream_type(self) -> StreamType | None:
"""Return the type of stream supported by this camera."""
return StreamType.WEB_RTC

async def async_handle_async_webrtc_offer(
self, offer_sdp: str, session_id: str, send_message: WebRTCSendMessage
) -> None:
@@ -467,3 +451,9 @@ async def async_handle_async_webrtc_offer(
async def async_on_webrtc_candidate(self, session_id: str, candidate: Any) -> None:
"""Ignore WebRTC candidates for Frigate cameras."""
return

class FrigateCameraWebRTC(FrigateCamera, WebRTCBase):
"""Frigate birdseye camera with WebRTC support."""

class BirdseyeCameraWebRTC(BirdseyeCamera, WebRTCBase):
"""Frigate birdseye camera with WebRTC support."""

0 comments on commit 1e3e713

Please sign in to comment.