Closed xXxNIKIxXx closed 1 month ago
I don't think you can run the asyncio event loop from another thread (at least not yet). So I would say that is incorrect usage of asyncio. What are you trying to accomplish?
I want to have an list of all HomePods and the can select one or multiple HomePods, were an live audio stream of my system Audio is played. I have found an workaround, wich is not that nice, but it works. I create like an thread with another process in it. And then it works.
You can do that in asyncio, but you don't use threads. Instead use tasks. Something like this will stream to all HomePods and wait for it to finish:
from pyatv import scan, connect, const
import asyncio
import asyncio.subprocess as asp
TARGET_MODELS = [const.DeviceModel.HomePod, const.DeviceModel.HomePodGen2, const.DeviceModel.HomePodMini]
async def stream_to_device(device):
loop = asyncio.get_event_loop()
atv = await connect(device, loop)
process = await asp.create_subprocess_exec("C:\\Program Files\\ffmpeg\\ffmpeg.exe", "-f", "dshow", "-i", "audio=Home Pod (VB-Audio Virtual Cable)", "-acodec", "libmp3lame", "-f", "mp3", "-", stdout=asp.PIPE, stderr=None)
await atv.stream.stream_file(process.stdout)
async def main():
devices = await scan(asyncio.get_event_loop())
streams = []
for device in devices:
if device.device_info.model in TARGET_MODELS:
streams.append(asyncio.create_task(stream_to_device(device)))
await asyncio.gather(*streams)
if __name__ == "__main__":
asyncio.run(main())
I have not tested this, but should be mostly correct.
Describe the bug
When running a new thread, and then connectiong to an device (HomePod) if you try to use stream_file the connection is apperently broken.
Error log
How to reproduce the bug?
Have an function call the async function were i connect to an device and then strem audio. The start an new thread with the function fich is calling the async funtion.
What is expected behavior?
The stream of ffmpeg should be normaly streamed to the device like it is when i am not in an new thread.
Operating System
Windows
Python
3.9
pyatv
0.14.5
Device
HomePod
Additional context
-