WebRTC icon indicating copy to clipboard operation
WebRTC copied to clipboard

Can't play to multiple camera media_players at the same time

Open borgqueenx opened this issue 5 months ago • 6 comments

I have around 8 camera's that i use as media_player in configuration.yaml and i can send audio files to all of them. However when i try to send a audio file to 2 media players at the same time, it plays it on just 1 camera and the other is ignored. after waiting a bit i get a "error", without it saying what the error exactly is.

In the home assistant core logs, i find the following that could be related:

Logger: homeassistant.components.websocket_api.http.connection
Source: custom_components/webrtc/media_player.py:74
Integration: Home Assistant WebSocket API ([documentation](https://www.home-assistant.io/integrations/websocket_api), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+websocket_api%22))
First occurred: 17:20:32 (2 occurrences)
Last logged: 17:21:59

[139646349665088] Error handling message: Unknown error (unknown_error) borgqueenx from 100.82.15.21 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36)
[139646411738432] Error handling message: Unknown error (unknown_error) borgqueenx from 100.82.15.21 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36)
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 26, in _handle_async_response
    await func(hass, connection, msg)
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 782, in handle_execute_script
    script_result = await script_obj.async_run(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1587, in async_run
    return await asyncio.shield(run.async_run())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 426, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 479, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 502, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 476, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 713, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 675, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2149, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2186, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 272, in handle_service
    return await service.entity_service_call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 907, in entity_service_call
    raise result from None
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1497, in async_request_call
    return await coro
           ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 952, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/config/custom_components/webrtc/media_player.py", line 74, in async_play_media
    assert r.ok
AssertionError

If i try to make multiple actions, each with the same sound to just 1 mediaplayer entity, it works, but with delays of course, so its not ideal.

borgqueenx avatar Feb 28 '24 16:02 borgqueenx

Show how you call service

AlexxIT avatar Feb 28 '24 17:02 AlexxIT

This is how:

service: media_player.play_media
metadata:
  title: wolf-howl.mp3
  thumbnail: null
  media_class: music
  children_media_class: null
  navigateIds:
    - {}
    - media_content_type: app
      media_content_id: media-source://media_source
    - media_content_type: ""
      media_content_id: media-source://media_source/local/sounds
data:
  media_content_id: media-source://media_source/local/sounds/dog_barking.mp3
  media_content_type: audio/mpeg
target:
  entity_id:
    - media_player.poolpole_cam
    - media_player.bedroom_top
    - media_player.living_room

borgqueenx avatar Feb 28 '24 18:02 borgqueenx

Show how you setup media_player. Are you sure it support two way audio and you choose right audio codec?

AlexxIT avatar Apr 07 '24 08:04 AlexxIT

Show how you setup media_player. Are you sure it support two way audio and you choose right audio codec?

do you have suggestions what codec to use? they are added using frigate, most of them using the tapo protocol. I also notice on all my camera's that the sound crackles/lags the first time playing something after a while. if i then repeat the same command immediately after, it's usually playing fine. perhaps i should first play a empty sound and then play the actual sound, it might improve it. Two-way audio definitely works, i can use home assistant to talk to the tapo cameras.

Here is the YAML to execute playing a sound on a tapo camera:

service: media_player.play_media metadata: title: carlock.mp3 thumbnail: null media_class: music children_media_class: null navigateIds: - {} - media_content_type: app media_content_id: media-source://media_source - media_content_type: "" media_content_id: media-source://media_source/local/sounds data: media_content_id: media-source://media_source/local/sounds/carlock.mp3 media_content_type: audio/mpeg target: entity_id: media_player.house_corner_garden_view

And this is how the cameras are added in frigate:

go2rtc: streams: kitchen-cam: - tapo://[email protected] - ffmpeg:kitchen-cam#audio=aac

(the aac is needed or else recordings will not have sound)

And finally, this is the part from the home assistant configuration.yaml how the media players are added. All tapo camera's are pcma. The camera's all the way below are not tapo cameras but annke cameras.

media_player:
  - platform: webrtc
    name: living-room
    stream: living-room
    audio: pcma
  - platform: webrtc
    name: naelis-place
    stream: naelis-place
    audio: pcma
  - platform: webrtc
    name: bedroom-top
    stream: bedroom-top
    audio: pcma
  - platform: webrtc
    name: bedroom-floor
    stream: bedroom-floor
    audio: pcma
  - platform: webrtc
    name: outdoor-arch
    stream: outdoor-arch
    audio: pcma
  - platform: webrtc
    name: kitchen-cam
    stream: kitchen-cam
    audio: pcma
  - platform: webrtc
    name: hallway-cam
    stream: hallway-cam
    audio: pcma
  - platform: webrtc
    name: billiard-room
    stream: billiard-room
    audio: pcma
  - platform: webrtc
    name: house-corner-garden-view
    stream: house-corner-garden-view
    audio: pcmu/8000
  - platform: webrtc
    name: poolpole-cam
    stream: poolpole-cam
    audio: pcmu/8000
  - platform: webrtc
    name: garage-door
    stream: garage-door
    audio: pcmu/8000

borgqueenx avatar Apr 13 '24 19:04 borgqueenx

Tapo cameras uses audio: pcma

AlexxIT avatar Apr 15 '24 11:04 AlexxIT