Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept all media types #19

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions custom_components/stream_assist/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def async_step_init(self, user_input: dict = None):
vol.Optional("player_entity_id"): cv.multi_select(players),
vol.Optional("stt_start_media"): str,
vol.Optional("pipeline_id"): vol.In(pipelines),
vol.Optional("allow_all_mediatypes"): bool,
},
defaults,
),
Expand Down
2 changes: 2 additions & 0 deletions custom_components/stream_assist/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ async def stream_run(hass: HomeAssistant, data: dict, stt_stream: Stream) -> Non
stream_kwargs["file"] = await get_stream_source(hass, entity)
else:
return
if "allow_all_mediatypes" not in stream_kwargs:
stream_kwargs["allow_all_mediatypes"] = data.get("allow_all_mediatypes", False)

stt_stream.open(**stream_kwargs)

Expand Down
24 changes: 14 additions & 10 deletions custom_components/stream_assist/core/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def __init__(self):
self.enabled: bool = False
self.queue: asyncio.Queue[bytes] = asyncio.Queue()

def open(self, file: str, **kwargs):
_LOGGER.debug(f"stream open")

def open(self, file: str, allow_all_mediatypes: bool, **kwargs):
if "options" not in kwargs:
kwargs["options"] = {
"fflags": "nobuffer",
Expand All @@ -27,15 +25,21 @@ def open(self, file: str, **kwargs):

if file.startswith("rtsp"):
kwargs["options"]["rtsp_flags"] = "prefer_tcp"
kwargs["options"]["allowed_media_types"] = "audio"
if not allow_all_mediatypes:
kwargs["options"]["allowed_media_types"] = "audio"

kwargs.setdefault("timeout", 5)
_LOGGER.debug("Starting stream with kwargs: %s", kwargs)

# https://pyav.org/docs/9.0.2/api/_globals.html
self.container = av.open(file, **kwargs)
try:
# https://pyav.org/docs/9.0.2/api/_globals.html
self.container = av.open(file, **kwargs)
_LOGGER.info("Stream started")
except Exception as e:
_LOGGER.error("Can't open stream: %s", e)

def run(self, end=True):
_LOGGER.debug("stream start")
_LOGGER.info("stream start")

resampler = AudioResampler(format="s16", layout="mono", rate=16000)

Expand All @@ -49,18 +53,18 @@ def run(self, end=True):
chunk = frame_raw.to_ndarray().tobytes()
self.queue.put_nowait(chunk)
except Exception as e:
_LOGGER.debug(f"stream exception {type(e)}: {e}")
_LOGGER.error(f"stream exception {type(e)}: {e}")
finally:
self.container.close()
self.container = None

if end and self.enabled:
self.queue.put_nowait(b"")

_LOGGER.debug("stream end")
_LOGGER.info("stream end")

def close(self):
_LOGGER.debug(f"stream close")
_LOGGER.info(f"stream close")
self.closed = True

def start(self):
Expand Down
6 changes: 6 additions & 0 deletions custom_components/stream_assist/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ run:
description: Advanced parameters
selector:
object:

allow_all_mediatypes:
name: Allow all media types
description: Activate if RTSP camera doesn't support filtering media types
selector:
boolean:
3 changes: 2 additions & 1 deletion custom_components/stream_assist/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"camera_entity_id": "Camera Entity",
"player_entity_id": "Player Entity",
"stt_start_media": "STT start media",
"pipeline_id": "Pipeline (Settings > Voice Assistant)"
"pipeline_id": "Pipeline (Settings > Voice Assistant)",
"allow_all_mediatypes": "Allow all Media Types"
}
}
}
Expand Down