postlund / pyatv

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

Streaming desktop or video to AppleTV , sending to atvremote #2106

Closed field64 closed 9 months ago

field64 commented 11 months ago

What do you need help with?

The goal is to stream/mirror a linux workstation desktop to an AppleTV 16.5 Network connectivity is good, i can pair and then send commands to the AppleTV perfectly.

Perhaps I have this incorrect, but use this line to send commands successful - stream audio, ,menu, up down, etc... atvremote --id 40:CB:C0:CA:F0:37 --protocol airplay --protocol raop --raop-credentials cat airplay.txt --airplay-credentials cat airplay.txt --companion-credentials cat companion.txt

I am hoping something like this is possible - that is take a shell command and send desktop or webcam device into atvremote: (tested with webcam, ,and numerous video formats)

ffmpeg -device /dev/dri/card0 -f kmsgrab -i - -vf 'hwmap=derive_device=vaapi,scale_vaapi=w=1920:h=1080:format=nv12' -c:v h264_vaapi -b:v 2500k -maxrate 4000k -y - | atvremote --id 40:CB:C0:CA:F0:37 --protocol airplay --protocol raop --raop-credentials cat airplay.txt --airplay-credentials cat airplay.txt --companion-credentials cat companion.txt stream_file=-

Failure is usually the same atvremote 0.13.2 (newest installed minutes ago) Python 3.10.6

Everything runs in the ENV python3 -m venv pyatv_venv

Name: Apple TV Model/SW: Apple TV 4K, tvOS 16.5 Address: x.x.x.x MAC: 40:33:33:CA:33:37 Deep Sleep: False Identifiers:

can stream_file can handle video (RAOP TCP, or similar) , if the video format is correct ?

I strongly suspect my video input to atvremote is incorrect. Or can atvremote only pass an https URL to the appleTV, ,and the appleTV fetches from a reachable server ?

Before I load this forum down with debug files, highly appreciate a push in the right direction from anyone who's experienced with such.

Thank You

postlund commented 11 months ago

You can only stream audio files (local or via HTTP(S)) with stream_file at the moment (this will probably not change for a long time, if ever). To play video, you can use play_url instead. I however just restored that functionality last night and there's no release out with that fix, so you need to run from master or wait until next release for that.

It's a bit unclear what formats the Apple TV supports this way (MP4 is your best bet), so you will have to try. You can either pass a URL or a local file. In the latter case, pyatv will start a web server serving the file and pass a URL to that web server. If you want to stream your desktop or an app of some sort, you'll have to find another application that can record and stream your desktop and serve that via a web server so you can can pass the URL to that stream.

postlund commented 11 months ago

@field64 Any other questions you have related to this?

field64 commented 11 months ago

Thank you for the follow-up.

I need to install the newest with the play_url fixes and test this method, ,which i think is optimal.

Thinking that once the Linux workstation is paired to the AppleTV, i thought perhaps some of the older airplay options using RAOP might work (example https://github.com/zeppelsoftware/airplay) but all of these which used to work - almost all hit something like this:

java.io.IOException: Server returned HTTP response code: 403 for URL: http://1.1.1.1:7000/photo

For the record, did find a method to use VLC - it's a bit ugly as atvremote calls up VLC on the apple TV, menus down to the URL and selects, which works. Not robust at all.

I did not find a method to launch_app VLC and also pass VLC the http url for the video, this is really a question for VLC on the appletv.

I will load the latest and test play_url, thank you !

postlund commented 11 months ago

I think a lot of other options has broken, just like you mention. They kinda rot over time of no one maintains them. I hope pyatv will work good now though.

Since a few versions back, you can launch apps via deep linking. Maybe that works with VLC?

https://pyatv.dev/development/apps/

field64 commented 11 months ago

You motivated me to read through the VLC documentation around "deep linking", which seems to be vlc-x-callback

This line does the job for proof of concept. STill uses the VLC APP.

atvremote --id 40CBC33CA3337 --protocol airplay --protocol raop --raop-credentials --airplay-credentials --companion-credentials launch_app=vlc-x-callback://x-callback-url/stream?url=http://test:test@1.1.1.1:5404&action=play

I was just a bit surprised the AppleTV accepts this, without stating the actual name of the APP.

Thank you for the extra motivational push ! I wished some of the paid SW developers with whom i work daily exhibited the same level of discipline, professionalism and quality that you put into pyatv.

Commendations !

postlund commented 9 months ago

Sorry, lost track of this. Great that you got it working and thanks for the kind words! Feel free to open additional issues if you have any problems!

monomycelium commented 5 months ago

@field64, were you able to mirror your screen to an AirPlay receiver? If so, I would really appreciate some instructions to set it up.

field64 commented 5 months ago

@field64, were you able to mirror your screen to an AirPlay receiver? If so, I would really appreciate some instructions to set it up.

Yes, not sure if this is the correct place to post, but here is the final working script. Note - this is a very raw, inefficient, and very insecure script, which works for my purposes in a private network, and isn't used by others, hence; the lack of polish and documentation. I'm sure it could stand much improvement from someone competent at scripting. Also the VLC video sending is likely not optimal - the resolution, frame rate, and latency could be optimized.

Also note - the sending workstation uses X11, not wayland.

Hope this is helpful and this is the correct way to post such info and that it is helpful.

Attaching text file containing script. [atv-vlc-desktop-rx.txt](https://github.com/postlund/pyatv/files/14002144/atv-vlc-desktop-rx.txt)

monomycelium commented 2 months ago

After setting out to test your method, I realised that it is compatible with only Apple TVs and other devices that support launch_app, which does not include UxPlay or the AirPlay receivers on MacBooks, the only AirPlay receivers I have access to. Does pyatv support the AirPlay 2 Mirroring protocol?