loki-47-6F-64 / sunshine

Host for Moonlight Streaming Client
GNU General Public License v3.0
1.78k stars 152 forks source link

How to run sunshine on a headless server over ssh? #47

Open goddade opened 4 years ago

goddade commented 4 years ago

How to run sunshine on a server without a monitor? The server has a GPU but no monitor.

hans00 commented 4 years ago

If use Linux, you can configure virtual display. Alternatively use dummy plug.

AeliusSaionji commented 3 years ago

I'm trying to use Xvfb like so: xvfb-run -s "-screen 0 1500x1000x24" sunshine .config/sunshine/sunshine.conf

Moonlight connects and looks good, but mouse and keyboard just aren't working for mystery reasons. Any ideas?

loki-47-6F-64 commented 3 years ago

Sunshine uses uinput to emulate mouse and keyboard.

Do you have it enabled?

If you plug in mouse and/or keyboard directly in your pc, do those work?

AeliusSaionji commented 3 years ago

After looking around for some time, it seems that Xvfb just doesn't support... input devices? There are a handful of other people in different contexts bumping into the same issue. You can interact with it via xdotool and similar, and I've seen some dev blogs describing methods to route input devices into the X API xodotool uses.

Seems like it's very much the wrong tool for this job.

Which is a shame because it otherwise "just worked" as a systemd user service, and also "just worked" in allowing me to choose a 3:2 resolution (for my Surface as a moonlight client).

I've moved on to using the X dummy monitor method, but it's much less elegant. Have to set up startx on a user's automatic login, as opposed to a service. Defining modelines for the X dummy display simply does not work for most 3:2 resolutions. xrandr fails to add the resolutions with cryptic errors for which there are no relevant solutions known online.

The wonders of linux...

limlynn commented 3 years ago

Did you find a way to solve this? I am also in the same situation.

BTW, I am also tried to install it with a headless ubuntu machine. However, I don't have GPU. Is there any way to use sunshine without GPU?

AeliusSaionji commented 3 years ago

I have no solutions for Xvfb, but if someone else does I'd love to hear it.

hans00 commented 3 years ago

Maybe you can try this https://github.com/murer/hexblade/tree/master/docker

Instead direct start sunshine

avently commented 3 years ago

I have a xorg-video-dummy installed and running:

[    26.048] (**) |-->Screen "dummy_screen" (0)
[    26.048] (**) |   |-->Monitor "dummy_monitor"
[    26.048] (**) |   |-->Device "dummy_card"
[    26.048] (**) |   |-->GPUDevice "Device0"

However sunshine is exiting after printing Detecting connected monitors. Is something wrong with my setup?

I tried solution mentioned above:

xvfb-run -s "-screen 0 1500x1000x24" sunshine

Sunshine starts and I able to connect to it via Moonlight, however I see a black screen with cross icon X. Same display (:0) is working fine with Anydesk.

Logs:

[2021:10:12:01:50:55]: Info: Detecting connected monitors
[2021:10:12:01:50:55]: Info: Using X11 for screencasting
[2021:10:12:01:50:55]: Info: //////////////////////////////////////////////////////////////////
[2021:10:12:01:50:55]: Info: //                                                              //
[2021:10:12:01:50:55]: Info: //   Testing for available encoders, this may generate errors.  //
[2021:10:12:01:50:55]: Info: //   You can safely ignore those errors.                        //
[2021:10:12:01:50:55]: Info: //                                                              //
[2021:10:12:01:50:55]: Info: //////////////////////////////////////////////////////////////////
[2021:10:12:01:50:55]: Info: Trying encoder [nvenc]
[2021:10:12:01:50:55]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:55]: Info: Color range: [JPEG]
[2021:10:12:01:50:55]: Error: Failed to create a CUDA device: Unknown error occurred
[2021:10:12:01:50:55]: Error: Could not open X11 display
[2021:10:12:01:50:55]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:55]: Info: Color range: [JPEG]
[2021:10:12:01:50:55]: Error: Failed to create a CUDA device: Unknown error occurred
[2021:10:12:01:50:55]: Error: Could not open X11 display
[2021:10:12:01:50:56]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:56]: Info: Color range: [JPEG]
[2021:10:12:01:50:56]: Error: Failed to create a CUDA device: Unknown error occurred
[2021:10:12:01:50:56]: Error: Could not open X11 display
[2021:10:12:01:50:56]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:56]: Info: Color range: [JPEG]
[2021:10:12:01:50:56]: Error: Failed to create a CUDA device: Unknown error occurred
[2021:10:12:01:50:56]: Info: Encoder [nvenc] failed
[2021:10:12:01:50:56]: Info: Trying encoder [vaapi]
[2021:10:12:01:50:56]: Error: Could not open X11 display
[2021:10:12:01:50:56]: Error: Couldn't open /dev/dri/renderD128: No such file or directory
[2021:10:12:01:50:56]: Error: Could not open X11 display
[2021:10:12:01:50:56]: Error: Couldn't open /dev/dri/renderD128: No such file or directory
[2021:10:12:01:50:56]: Error: Could not open X11 display
[2021:10:12:01:50:57]: Error: Couldn't open /dev/dri/renderD128: No such file or directory
[2021:10:12:01:50:57]: Error: Could not open X11 display
[2021:10:12:01:50:57]: Error: Couldn't open /dev/dri/renderD128: No such file or directory
[2021:10:12:01:50:57]: Info: Encoder [vaapi] failed
[2021:10:12:01:50:57]: Info: Trying encoder [software]
[2021:10:12:01:50:57]: Error: Could not open X11 display
[2021:10:12:01:50:57]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:57]: Info: Color range: [JPEG]
[2021:10:12:01:50:57]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:57]: Info: Color range: [JPEG]
[2021:10:12:01:50:57]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:57]: Info: Color range: [JPEG]
x264 [error]: high profile doesn't support a bit depth of 10
[2021:10:12:01:50:58]: Info: Color coding [Rec. 709]
[2021:10:12:01:50:58]: Info: Color range: [JPEG]
[2021:10:12:01:50:58]: Error: Could not open codec [libx264]: Invalid argument
[2021:10:12:01:50:58]: Info: Color coding [Rec. 601]
[2021:10:12:01:50:58]: Info: Color range: [JPEG]
[2021:10:12:01:50:58]: Warning: software: h264: replacing nalu prefix data
[2021:10:12:01:50:58]: Info:
[2021:10:12:01:50:58]: Info: //////////////////////////////////////////////////////////////
[2021:10:12:01:50:58]: Info: //                                                          //
[2021:10:12:01:50:58]: Info: // Ignore any errors mentioned above, they are not relevant //
[2021:10:12:01:50:58]: Info: //                                                          //
[2021:10:12:01:50:58]: Info: //////////////////////////////////////////////////////////////
[2021:10:12:01:50:58]: Info:
[2021:10:12:01:50:58]: Info: Found encoder software: [libx264]
[2021:10:12:01:50:58]: Info: Adding avahi service Sunshine
[2021:10:12:01:50:58]: Info: Configuration UI available at [https://localhost:47990]
[2021:10:12:01:50:59]: Info: Avahi service Sunshine successfully established.
[2021:10:12:01:51:06]: Error: Couldn't discover any UPNP devices
[2021:10:12:01:56:59]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:56:59]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:00]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:00]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:00]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:00]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:00]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:00]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:02]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:05]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:05]: Info: Executing: [xrandr --output HDMI-1 --mode 1920x1080]
[2021:10:12:01:57:05]: Info: /CN=NVIDIA GameStream Client -- verfied
[2021:10:12:01:57:06]: Info: CLIENT CONNECTED
[2021:10:12:01:57:06]: Error: Couldn't connect to pulseaudio: Access denied
[2021:10:12:01:57:06]: Warning: There will be no audio
[2021:10:12:01:57:06]: Info: Detecting connected monitors
[2021:10:12:01:57:06]: Info: Configuring selected monitor (0) to stream
[2021:10:12:01:57:06]: Info: Streaming display: screen with res 1600x900 offset by 0x0
[2021:10:12:01:57:06]: Info: Color coding [Rec. 601]
[2021:10:12:01:57:06]: Info: Color range: [MPEG]
^[^[1112f[2021:10:12:01:57:36]: Info: CLIENT DISCONNECTED

Do you have an idea what I can do and what's wrong with my setup? Also for some reason sunshine can not be started via non-root user even when I added it to group input (via sudo usermod -a -G input $USER).

Error: Could not create Sunshine Mouse: Permission denied

And crash afterwards.

EDIT. figured out that in case of xfvb I need to run something on DISPLAY:99 in order to not see black screen. However I still can't control remote via mouse and keyboard and still see X icon as mentioned above. So xfvb is not an option in my case.

Heath123 commented 2 years ago

I managed to add a dummy display in software to get it working with GPU acceleration on a server (well, someone else did and I copy/pasted the config files), I can tell you more if anyone is interested

AeliusSaionji commented 2 years ago

I also got it working, although I cannot recommend my solution.

After some digging, I learned that intel's linux driver just includes a GPU accelerated virtual display output called VIRTUAL1. Rather than struggling to learn the arcane mysteries of xorg conf, I initialize the display with xrandr via .xprofile

xrandr --newmode "1500x1000_60.00" 102.49 1500 1548 1580 1660 1000 1003 1013 1029 +hsync -vsync
xrandr --addmode VIRTUAL1 "1500x1000_60.00"
xrandr --output VIRTUAL1 --mode "1500x1000_60.00"

Sunshine will see this and it will just work. Unfortunately something is wrong with the driver. It's a known bug. The performance is just really poor. So that's a dead end for now. Luckily, the above xrandr/xprofile approach will "just work" for any real physical display, or a physically connected dummy plug. A dummy plug is my suggested approach, for simplicity.

Of course, @Heath123 should share the solution, why would you dangle that with no details? 😩 https://tenor.com/view/simpsons-steamed-hams-aurora-borealis-skinner-chalmers-gif-5424274

Heath123 commented 2 years ago

Of course, @Heath123 should share the solution, why would you dangle that with no details? 😩

I wasn't sure if anyone was still interested and finding everything I used takes time. Anyway, this might only work on Nvidia, but I'll try to find what I used

Heath123 commented 2 years ago

I used the EDID file from https://superuser.com/a/1278264 and the xorg config is based on https://gist.github.com/ArrEssJay/0e126a56b0ee9f23551a98fb258414c6

Heath123 commented 2 years ago

I haven't tried it but maybe you could use https://github.com/akatrevorjay/edid-generator

geezer500 commented 2 years ago

Is there any working solution for this and an iGPU? (i.e sunshine on a headless server/onboard iGPU) I have a PLEX server with an Intel HD 4000 onboard a XEON CPU (PLEX can use it for hardware transcoding). I only really want to use it to run/play classic arcade and 8/16bit consoles via https://retroarcher.github.io/ via RetroArcher (a plugin for emulation via PLEX).

ReenigneArcher commented 2 years ago

Is there any working solution for this and an iGPU? (i.e sunshine on a headless server/onboard iGPU) I have a PLEX server with an Intel HD 4000 onboard a XEON CPU (PLEX can use it for hardware transcoding). I only really want to use it to run/play classic arcade and 8/16bit consoles via https://retroarcher.github.io/ via RetroArcher (a plugin for emulation via PLEX).

RetroArcher doesn't currently work on linux...

andredyl commented 1 year ago

@Heath123 Would you be so kind to explain a little bit more how you managed to get it working with the resources you shared? I tried using the EDID and adapting the xorg.conf file, but to no avail. I'm running a Ubuntu VM on GCP and struggling to get sunshine to work. I just can't get a virtual monitor setup when connected through SSH / TTY, only if I RDP / VN / Chrome Remote to it and start the service, but then, when launching through Moonlight, I lose all mouse and keyboard inputs (even after manually adding the uinput permissions.

Heath123 commented 1 year ago

@Heath123 Would you be so kind to explain a little bit more how you managed to get it working with the resources you shared? I tried using the EDID and adapting the xorg.conf file, but to no avail. I'm running a Ubuntu VM on GCP and struggling to get sunshine to work. I just can't get a virtual monitor setup when connected through SSH / TTY, only if I RDP / VN / Chrome Remote to it and start the service, but then, when launching through Moonlight, I lose all mouse and keyboard inputs (even after manually adding the uinput permissions.

I can't remember much about it now but I think something about it was specific to Nvidia GPUs, does the VM have one?

andredyl commented 1 year ago

I can't remember much about it now but I think something about it was specific to Nvidia GPUs, does the VM have one?

Yes, it does have a Tesla T4. Drivers are installed. I can't generate an EDID file from Nvidia panel as it won't show any monitors connected, so I used the .txt approach mentioned in that first link of yours https://superuser.com/questions/1278256/x-server-on-nvidia-card-with-no-screen/1278264#1278264

WesleyVanNeck commented 1 year ago

above

If it's slow did you try this

https://stackoverflow.com/questions/74730190/xubuntu-video-rendering-very-slow-when-no-display-connected

WesleyVanNeck commented 1 year ago

@AeliusSaionji

Honigmelone commented 1 year ago

I was able to run sunshine on a headless server with nvidia GPU with the fix presented here: https://github.com/dianariyanto/virtual-display-linux/issues/9#issuecomment-962655947

WesleyVanNeck commented 1 year ago

I was able to run sunshine on a headless server with nvidia GPU with the fix presented here: dianariyanto/virtual-display-linux#9 (comment)

on nvidia am on intel and just buyed a dummy plug

N0m8n commented 1 year ago

So I was finally able to start and run Sunshine 0.20.0 on a Ubuntu 20.04 system via SSH. Posting here in case anyone is still interested. The procedure is pretty long so I attached it as a text file.

UPDATE: Looks like despite triple checking this yesterday I still missed a step :( (setting the DISPLAY variable in your shell before you try to bring up Sunshine). I've updated the steps and added a few more clarification notes; repost is below.

Sunshine_SSH.txt

AlexStapleton commented 1 year ago

So I was finally able to start and run Sunshine 0.20.0 on a Ubuntu 20.04 system via SSH. Posting here in case anyone is still interested. The procedure is pretty long so I attached it as a text file.

I followed your instructions on Ubuntu 22.04 but couldn't get the x server to run. It would just exit out immediately after I press enter. Try running sunshine.appimage but get Connection refused error.

Any ideas on what I'm missing?

N0m8n commented 1 year ago

So I was finally able to start and run Sunshine 0.20.0 on a Ubuntu 20.04 system via SSH. Posting here in case anyone is still interested. The procedure is pretty long so I attached it as a text file.

I followed your instructions on Ubuntu 22.04 but couldn't get the x server to run. It would just exit out immediately after I press enter. Try running sunshine.appimage but get Connection refused error.

Any ideas on what I'm missing?

The Xorg startup should generate 2 log files; the first is whatever path you gave for the -logfile option and the second is the standard stream log created by the redirection (in my example command I had that path represented by [STD_OUT_LOG]). You'll need to look at those to see what error is causing your X-server to fail out.

Yeshey commented 3 months ago

I made it work in a headless VM server without a monitor or mouse attached (without audio yet, havent looked into that). The mouse wouldn't come through, but in wayland KDE setting KWIN_FORCE_SW_CURSOR=1 allows the cursor to render (but it's slow), I suggest making it so the client draws the local cursor by pressing Ctrl+Alt+Shift+M and/or Ctrl+Alt+Shift+C instead (docs)

here is the configuration for nixOS I used:

    hardware.opengl.enable = true;
    hardware.opengl.driSupport = true;

    nixpkgs.config.pulseaudio = true; # (need to fix audio)

    # Use this to force KDE to draw the cursor (slow)
    # you should instead tell moonlight (client) to use 
    # local cursor with Ctrl+Alt+Shift+M and Ctrl+Alt+Shift+C 
    # (https://github.com/moonlight-stream/moonlight-docs/wiki/Setup-Guide#keyboardmousegamepad-input-options)
    environment.sessionVariables = rec {
      #KWIN_FORCE_SW_CURSOR="1";
    };
    services = {
      xserver = {
        displayManager.startx.enable = true;
        enable = true;    # X11 because setting up Wayland is more complicated than it is worth for me.
      };
      desktopManager.plasma6.enable = true;
      displayManager = {
        #autoLogin.enable = true;
        #autoLogin.user = config.mySystem.user;
        sddm.enable = true;
        defaultSession = lib.mkForce "plasma";
      };
    };

    services.xserver.xkb.layout = "br,pt";
    #console.keyMap = lib.mkOverride 1009 "pt";
    services.xserver.xkb.options = "eurosign:e,caps:escape";

    services.avahi.enable = true;
    services.avahi.publish.enable = true;
    services.avahi.publish.userServices = true;
    services.sunshine = {
      enable = true;
      autoStart = true;
      capSysAdmin = true;
      openFirewall = true;
    };
    networking.firewall = {
      enable = true;
      allowedTCPPorts = [ 47984 47989 47990 48010 ];
      allowedUDPPortRanges = [
        { from = 47998; to = 48000; }
        { from = 8000; to = 8010; }
      ];
    };
    environment.systemPackages = with pkgs; [
      sunshine
    ];
    security.wrappers.sunshine = {
            owner = "root";
            group = "root";
            #capabilities = "cap_sys_admin+p";
            #capabilities = "cap_sys_admin+ep";
            source = "${pkgs.sunshine}/bin/sunshine";
    };
    systemd.user.services.sunshine =
      {
        wantedBy = [ "graphical-session.target" ];
        serviceConfig = {
          AmbientCapabilities = "CAP_SYS_ADMIN";
        };
      };
    # Requires to simulate input
    boot.kernelModules = [ "uinput" ];
    services.udev.extraRules = ''
      KERNEL=="uinput", SUBSYSTEM=="misc", OPTIONS+="static_node=uinput", TAG+="uaccess"
    '';

In the case of a VM without a pass-through GPU, using the Red Hat, Inc. Virtio 1.0 GPU virtual GPU, really wasn't very fast. I ended up sticking with xrdp