postlund / pyatv

A client library for Apple TV and AirPlay devices
https://pyatv.dev
MIT License
866 stars 94 forks source link

RTSP/1.0 method SETUP failed with code 520: Origin Error #1884

Open golles opened 1 year ago

golles commented 1 year ago

Describe the bug

Can't stream anything to Homepod mini, eg tts or radio browser.

Error log

Full log: https://pastebin.com/V4u59p8v

2022-11-25 21:03:23.676 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140291987056976] RTSP/1.0 method SETUP failed with code 520: Origin Error
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 200, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1744, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1781, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 208, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 678, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 943, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 715, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/media_player.py", line 292, in async_play_media
    await self.atv.stream.stream_file(media_id)
  File "/usr/local/lib/python3.10/site-packages/pyatv/core/facade.py", line 392, in stream_file
    await self.relay("stream_file")(file, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/pyatv/protocols/raop/__init__.py", line 352, in stream_file
    await client.initialize(self.core.service.properties)
  File "/usr/local/lib/python3.10/site-packages/pyatv/protocols/raop/raop.py", line 457, in initialize
    await self._setup_session()
  File "/usr/local/lib/python3.10/site-packages/pyatv/protocols/raop/raop.py", line 486, in _setup_session
    resp = await self.rtsp.setup(
  File "/usr/local/lib/python3.10/site-packages/pyatv/support/rtsp.py", line 181, in setup
    return await self.exchange("SETUP", headers=headers, body=body)
  File "/usr/local/lib/python3.10/site-packages/pyatv/support/rtsp.py", line 265, in exchange
    resp = await self.connection.send_and_receive(
  File "/usr/local/lib/python3.10/site-packages/pyatv/support/http.py", line 414, in send_and_receive
    raise exceptions.HttpError(
pyatv.exceptions.HttpError: RTSP/1.0 method SETUP failed with code 520: Origin Error

How to reproduce the bug?

Go the the apple tv integration in Home Assistant Select the media player entity Browse media Select TTS Google/Microsoft (both don't work) Keep the sample text and press play

What is expected behavior?

TTS to work :-)

Operating System

Home Assistant Container (docker on Ubuntu)

Python

3.10

pyatv

0.10.3

Device

Homepod mini, 16.1

Additional context

N/A

strunker commented 1 year ago

I am seeing something similar, although I am not getting a 520 error code. I am seeing the SETUP failed. I am simply trying to send remote control commands to my second gen apple tv 4k and running into this.

Failed to set up remote control channel Traceback (most recent call last): File "C:\Python38\lib\site-packages\pyatv\support\http.py", line 388, in send_and_receive await asyncio.wait_for(event.wait(), timeout=30) File "C:\Python38\lib\asyncio\tasks.py", line 501, in wait_for raise exceptions.TimeoutError() asyncio.exceptions.TimeoutError

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Python38\lib\site-packages\pyatv\protocols\airplay__init__.py", line 258, in _connect_rc await control.start(str(core.config.address), control_port, credentials) File "C:\Python38\lib\site-packages\pyatv\protocols\airplay\remote_control.py", line 60, in start await self._setup_event_channel(self.connection.remote_ip) File "C:\Python38\lib\site-packages\pyatv\protocols\airplay\remote_control.py", line 102, in _setup_event_channel resp = await self._setup( File "C:\Python38\lib\site-packages\pyatv\protocols\airplay\remote_control.py", line 90, in _setup resp = await self.rtsp.setup( File "C:\Python38\lib\site-packages\pyatv\support\rtsp.py", line 175, in setup return await self.exchange("SETUP", headers=headers, body=body) File "C:\Python38\lib\site-packages\pyatv\support\rtsp.py", line 258, in exchange resp = await self.connection.send_and_receive( File "C:\Python38\lib\site-packages\pyatv\support\http.py", line 391, in send_and_receive raise TimeoutError(f"no response to {method} {uri} ({protocol})") from ex TimeoutError: no response to SETUP rtsp://10.1.1.10/3387681805 (RTSP/1.0)

acovaci commented 1 year ago

I am also impacted by this :(. To be clear, I am seeing the same logs as OP; also in HA in docker with net=host. In addition, playing local media or internet radio doesn't work either.

acovaci commented 1 year ago

I've done a bit more digging, using atvremote, and by listing features, I can see that PlayUrl is not supported. On the other hand, I can get it to play using stream_file. Is there a way to change what method the media player uses to cast to homepod?

acovaci commented 1 year ago

Okay, did a bit more digging in the logs, and the play url is not the issue, on the contrary, stream_file is used for music. I don't 100% understand what's happening, but this happens just before the error:

2022-11-29 23:44:18.033 DEBUG (MainThread) [pyatv.auth.hap_channel] Sending data (Encrypted=4f0...57)
2022-11-29 23:44:18.037 DEBUG (MainThread) [pyatv.support.http] Received: b'RTSP/1.0 520 Origin Error\r\nContent-Length: 0\r\nServer: AirTunes/xxx.xx.x\r\nCSeq: 2\r\n\r\n'
2022-11-29 23:44:18.039 DEBUG (MainThread) [pyatv.support.http] Got RTSP response: HttpResponse(protocol='RTSP', version='1.0', code=520, message='Origin Error', headers={'content-length': '0', 'server': 'AirTunes/xxx.xx.x', 'cseq': '2'}, body=''):
2022-11-29 23:44:18.040 DEBUG (MainThread) [pyatv.core.facade] Release (<class 'pyatv.interface.Audio'>, <class 'pyatv.interface.Metadata'>, <class 'pyatv.interface.PushUpdater'>, <class 'pyatv.interface.RemoteControl'>) by Protocol.RAOP
2022-11-29 23:44:18.042 DEBUG (MainThread) [pyatv.protocols.raop.raop] Control connection lost (None)
acovaci commented 1 year ago

Okay so all this thing is a bit over my head, but from what I'm getting, the heartbeat fails.

pyatv/protocols/airplay/remote_control.py:82 - that _failure_func is the only invocation of pyatv.protocols.raop.raop.ControlClient.connection_lost that doesn't check for None

The last heartbeat I'm seeing isn't consistent, but it's in the first 15-ish.

2022-11-29 23:44:18.020 DEBUG (MainThread) [pyatv.core.protocol] Got heartbeat 9 (AirPlay:xxx.xxx.xxx.xxx)

Which empirically makes sense, as I'm hearing a tiny under-half-second-long purr when I hit play. Looking at the logs, I assume the first packet of data had just been sent, then the HomePod sends back the 520. Weird that it works on my Raspberry Pi but not on the Pi inside docker :-?

Anyway, that's all my tired neuron can figure out for now, hopefully @postlund or one of the other contributors can make sense of this.

dkkr23 commented 1 year ago

One thing I noticed - When you call the google_translate service to a homepod mini, it fails. But if you call it again immediately after it fails, it works. Hopefully that helps when trying to find a solution. Watching this one closely for updates!

golles commented 1 year ago

One thing I noticed - When you call the google_translate service to a homepod mini, it fails. But if you call it again immediately after it fails, it works. Hopefully that helps when trying to find a solution. Watching this one closely for updates!

For me, this ain't working either.


I've installed the latest dev build of Home Assistant because I'm really curious if I could use my HomePod mini for TTS, but this problem is still happening.

Verified version:

977ce24d1f12:/config# pip list | grep pyatv
pyatv                            0.11.0

Error:

2023-05-19 17:07:53.100 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140717857421904] RTSP/1.0 method SETUP failed with code 520: Origin Error
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 202, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1892, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1932, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 226, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 811, in entity_service_call
    future.result()  # pop exception if have
    ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1006, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 851, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/media_player.py", line 292, in async_play_media
    await self.atv.stream.stream_file(media_id)
  File "/usr/local/lib/python3.11/site-packages/pyatv/core/facade.py", line 392, in stream_file
    await self.relay("stream_file")(file, **kwargs)
  File "/usr/local/lib/python3.11/site-packages/pyatv/protocols/raop/__init__.py", line 352, in stream_file
    await client.initialize(self.core.service.properties)
  File "/usr/local/lib/python3.11/site-packages/pyatv/protocols/raop/raop.py", line 457, in initialize
    await self._setup_session()
  File "/usr/local/lib/python3.11/site-packages/pyatv/protocols/raop/raop.py", line 486, in _setup_session
    resp = await self.rtsp.setup(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyatv/support/rtsp.py", line 181, in setup
    return await self.exchange("SETUP", headers=headers, body=body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyatv/support/rtsp.py", line 265, in exchange
    resp = await self.connection.send_and_receive(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/pyatv/support/http.py", line 415, in send_and_receive
    raise exceptions.HttpError(
pyatv.exceptions.HttpError: RTSP/1.0 method SETUP failed with code 520: Origin Error
postlund commented 1 year ago

Can someone retest this with the latest release and report back?