postlund / pyatv

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

play_url for HomePods and other devices #2364

Open Docjones opened 8 months ago

Docjones commented 8 months ago

What feature would you like?

I would like to be able to play from a internet radio stream url (eg. http://streams.80s80s.de/darkwave/mp3-192)

Describe the solution you'd like

Basically, make play_url available for Homepods (at least) - currently it is listed "Unavailable". I have a shairplay-server as well, might be great, if that will work, too.

atvremote --id XXXXXXXXX play_url=http://streams.80s80s.de/darkwave/mp3-192

postlund commented 8 months ago

It does not support that part of the AirPlay protocol (only Apple TV does). Use stream_file instead (call it in the same way).

Docjones commented 8 months ago

okay -that seems to work - however, this is a blocking call? And the Homepod vanishes from the list of airplay devices in iOS?

Docjones commented 8 months ago

And shairplay refuses streaming:

Traceback (most recent call last):
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/scripts/atvremote.py", line 978, in _run_application
    return await cli_handler(loop)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/scripts/atvremote.py", line 723, in cli_handler
    return await _handle_commands(args, config, storage, loop)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/scripts/atvremote.py", line 862, in _handle_commands
    ret = await _handle_device_command(args, cmd, atv, storage, loop)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/scripts/atvremote.py", line 916, in _handle_device_command
    return await _exec_command(atv.stream, cmd, True, *cmd_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/scripts/atvremote.py", line 945, in _exec_command
    value = await tmp(*args)
            ^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/core/facade.py", line 371, in stream_file
    await self.relay("stream_file")(
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/protocols/raop/__init__.py", line 356, in stream_file
    await client.initialize(self.core.service.properties)
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/protocols/raop/stream_client.py", line 337, in initialize
    await self._protocol.setup(self.timing_server.port, self.control_client.port)
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/protocols/raop/protocols/airplayv2.py", line 108, in setup
    await self._setup_base(timing_server_port)
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/protocols/raop/protocols/airplayv2.py", line 51, in _setup_base
    self._verifier = await verify_connection(
                     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/protocols/airplay/auth/__init__.py", line 104, in verify_connection
    has_encryption_keys = await verifier.verify_credentials()
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/protocols/airplay/auth/hap_transient.py", line 48, in verify_credentials
    await self.http.post("/pair-pin-start", headers=_AIRPLAY_HEADERS)
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/support/http.py", line 432, in post
    return await self.send_and_receive(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mrink/pyatv/lib/python3.11/site-packages/pyatv/support/http.py", line 495, in send_and_receive
    raise exceptions.HttpError(
pyatv.exceptions.HttpError: HTTP/1.1 method POST failed with code 400: Bad Request
postlund commented 8 months ago

Yes, it uses RTP streaming to stream audio to the device. Basically is streams audio from the remote source (internet radio in your case), converts it into raw PCM frames and forwards to the device. I.e. this is normal AirPlay audio streaming. I have not managed to figure out how to use built in apps (like Music), so no support for that. The API used on Apple TVs to just stream directly from a URL does not exist on HomePod.

Only one can stream via AirPlay so I assume that is expected.