Skip to content
Open
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
19 changes: 9 additions & 10 deletions custom_components/webrtc/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{
vol.Required(CONF_NAME): cv.string,
vol.Required("stream"): cv.string,
vol.Required("audio"): cv.string,
vol.Optional("audio"): cv.string,
},
extra=vol.REMOVE_EXTRA,
)
Expand All @@ -42,15 +42,14 @@ async def async_setup_platform(


class WebRTCPlayer(MediaPlayerEntity):
def __init__(self, name: str, stream: str, audio: str, **kwargs):
def __init__(self, name: str, stream: str, audio: str = None, **kwargs):
self._attr_supported_features = (
MediaPlayerEntityFeature.PLAY_MEDIA
| MediaPlayerEntityFeature.BROWSE_MEDIA
| MediaPlayerEntityFeature.STOP
)
self._attr_name = name
self._attr_unique_id = stream
self.audio = audio

async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> None:
if media_source.is_media_source_id(media_id):
Expand All @@ -60,15 +59,15 @@ async def async_play_media(self, media_type: str, media_id: str, **kwargs) -> No
media_id = sourced_media.url

media_id = async_process_play_media_url(self.hass, media_id)
if not media_type.startswith("#"):
media_type = "#input=file"

# Use go2rtc's /api/ffmpeg helper: it sets audio=auto on the ffmpeg
# producer so go2rtc negotiates the codec the camera's backchannel
# actually supports. The previous /api/streams path hardcoded the
# codec from `audio:` config and surfaced mismatches as a misleading
# "can't find consumer" error.
r = await async_get_clientsession(self.hass).post(
utils.api_streams(self.hass),
params={
"dst": self.unique_id,
"src": f"ffmpeg:{media_id}#audio={self.audio}{media_type}",
},
utils.api_ffmpeg(self.hass),
params={"dst": self.unique_id, "file": media_id},
timeout=9,
)
assert r.ok
Expand Down
6 changes: 6 additions & 0 deletions custom_components/webrtc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ def api_streams(hass: HomeAssistant) -> str:
return urljoin(go_url, "api/streams")


def api_ffmpeg(hass: HomeAssistant) -> str:
entry = hass.data[DOMAIN]
go_url = "http://localhost:1984/" if isinstance(entry, Server) else entry
return urljoin(go_url, "api/ffmpeg")


# copied from homeassistant.components.hassio.ingress import _websocket_forward
async def websocket_forward(ws_from, ws_to) -> None:
try:
Expand Down