AlexxIT / WebRTC

Home Assistant custom component for real-time viewing of almost any camera stream using WebRTC and other technologies.
https://github.com/AlexxIT/Blog
MIT License
1.54k stars 181 forks source link

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

Open borgqueenx opened 9 months ago

borgqueenx commented 9 months ago

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.

AlexxIT commented 9 months ago

Show how you call service

borgqueenx commented 9 months ago

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
AlexxIT commented 7 months ago

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

borgqueenx commented 7 months ago

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://Tomaat66-@192.168.1.182
  • 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
AlexxIT commented 7 months ago

Tapo cameras uses audio: pcma

borgqueenx commented 5 months ago

This issue still remains. The following works:

service: media_player.play_media
metadata:
  title: carlock2.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/carlock2.mp3
  media_content_type: audio/mpeg
target:
  entity_id: media_player.garage_door

And the following does not work. only one camera plays a sound and then i get "unknown error" reported by home assistant.

service: media_player.play_media
metadata:
  title: carlock2.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/carlock2.mp3
  media_content_type: audio/mpeg
target:
  entity_id:
    - media_player.backyard_tree
    - media_player.garage_door
    - media_player.kitchen_cam
    - media_player.house_corner
    - media_player.living_room
    - media_player.outdoor_arch
    - media_player.poolpole_cam

Note the only difference is multiple targets/media players.

If i make it a single media player, everything plays on every camera, but with delays so that is not optimal..

AlexxIT commented 2 months ago

Well. I don't know what is happening in your case. I have tested similar situation without any problem:

action: media_player.play_media
target:
  entity_id:
    - media_player.dahua_camera
    - media_player.tapo_camera
data:
  media_content_id: media-source://media_source/local/pooh.mp3
  media_content_type: audio/mpeg