# Sunshine + Moonlight 远程游戏

使用 Sunshine（主机）和 Moonlight（客户端）在 Clore.ai 的 GPU 驱动服务器上以近乎零延迟远程串流并游玩 PC 游戏。

{% hint style="info" %}
**Sunshine** 是一个开源、自托管的游戏串流服务器，拥有 20,000+ GitHub 星标。 **Moonlight** 是用于连接它的免费开源客户端。两者结合可媲美 GeForce NOW——但成本仅为其一小部分。
{% endhint %}

***

## 什么是 Sunshine + Moonlight？

**Sunshine** 是原始 Moonlight/GameStream 堆栈的一个分支，现在由 LizardByte 完全独立并积极维护。它捕获 GPU 输出，使用 H.264/H.265/AV1 实时编码，并以超低延迟通过网络进行串流。

**Moonlight** 是可用于 Windows、macOS、Linux、Android、iOS、Raspberry Pi 甚至一些智能电视的客户端应用。

### 为什么在 Clore.ai 上使用它？

* **按需租用高端 GPU** （RTX 4090、A100 等），以竞价/现货定价
* **将大型游戏串流** 从云端到任何设备——即使是一台廉价笔记本
* **无需 GeForce NOW 订阅** ——你掌控硬件
* **游戏模组、AI 辅助玩法**，或在强大硬件上进行游戏开发测试

***

## 先决条件

| 要求            | 详细信息                         |
| ------------- | ---------------------------- |
| Clore.ai 账户   | 当有足够的 CLORE 余额时              |
| GPU 服务器       | 优先使用 NVIDIA GPU（用于 NVENC 编码） |
| Moonlight 客户端 | 安装在你的本地设备上                   |
| SSH 访问        | 用于初始设置                       |

***

## 步骤 1：在 Clore.ai 上租用 GPU 服务器

1. 前往 [clore.ai](https://clore.ai) → **市场**
2. 筛选条件 **GPU 类型**：建议使用 RTX 3080 或更高以支持 1080p/60fps 串流
3. 查找具有端口 **22**, **47990**，以及 **48010** 可用的服务器
4. 选择服务器并继续下单

{% hint style="warning" %}
**端口要求**：Sunshine 需要端口 47990（HTTPS Web UI）和 48010（串流）。在下单前请确保这些端口列在服务器的可用端口中。
{% endhint %}

### 推荐的串流 GPU

| 分辨率   | 目标帧率    | 最低 GPU          |
| ----- | ------- | --------------- |
| 1080p | 60 fps  | RTX 3060        |
| 1440p | 60 fps  | RTX 3080        |
| 4K    | 60 fps  | RTX 3090 / 4080 |
| 1080p | 120 fps | RTX 4070+       |

***

## 步骤 2：通过 Docker 部署 Sunshine

### Docker Compose（推荐）

创建一个文件 `docker-compose.yml`:

```yaml
version: "3.8"

services:
  sunshine:
    image: lizardbyte/sunshine:latest
    container_name: sunshine
    restart: unless-stopped
    privileged: true
    network_mode: host
    environment:
      - PUID=1000
      - PGID=1000
      - TZ=UTC
      - SUNSHINE_USERNAME=admin
      - SUNSHINE_PASSWORD=your_secure_password
    volumes:
      - ./sunshine-config:/config
      - /tmp/.X11-unix:/tmp/.X11-unix:rw
      - /dev/dri:/dev/dri
    devices:
      - /dev/dri:/dev/dri
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
```

### 启动容器

```bash
# 拉取并启动
docker compose up -d

# 检查日志
docker compose logs -f sunshine

# 验证其是否在运行
docker ps | grep sunshine
```

{% hint style="success" %}
**首次启动**：Sunshine 初始化大约需要 30 秒。请在继续之前观察日志中出现“Sunshine started successfully”的信息。
{% endhint %}

### 单条 Docker 命令（快速开始）

```bash
docker run -d \
  --name sunshine \
  --restart unless-stopped \
  --privileged \
  --network host \
  -e SUNSHINE_USERNAME=admin \
  -e SUNSHINE_PASSWORD=changeme \
  -v $(pwd)/sunshine-config:/config \
  -v /tmp/.X11-unix:/tmp/.X11-unix \
  --device /dev/dri:/dev/dri \
  --gpus all \
  lizardbyte/sunshine:latest
```

***

## 步骤 3：配置 Sunshine Web UI

1. 打开浏览器并访问：

   ```
   https://<your-clore-server-ip>:47990
   ```
2. 接受自签名证书警告
3. 使用你设置的凭证登录（`admin` / `your_secure_password`)

{% hint style="warning" %}
**仅 HTTPS**：Sunshine 的 Web UI 运行在 HTTPS 上。浏览器会显示证书警告——这是预期的。点击“高级”→“继续”以继续。
{% endhint %}

### Web UI 分区

| 分区                     | 用途                |
| ---------------------- | ----------------- |
| **固定**                 | 与 Moonlight 客户端配对 |
| **应用**                 | 定义可串流的应用/游戏       |
| **invokeai.yaml 配置文件** | 编码器、分辨率、码率设置      |
| **日志**                 | 用于调试串流问题          |

***

## 步骤 4：配置编码设置

在你的浏览器中导航到 **invokeai.yaml 配置文件** → **视频** 在 Web UI 中：

### 针对 Clore.ai 的推荐设置

```
# 编码器
encoder: nvenc          # 使用 NVIDIA 硬件编码

# 分辨率
resolution_width: 1920
resolution_height: 1080

# 帧率
fps: 60

# 码率
bitrate: 50000          # 1080p60 使用 50 Mbps —— 极佳画质

# H.265（更好的压缩，如果客户端支持则使用）
codec: hevc
```

{% hint style="info" %}
**NVENC 与 VAAPI**：在 Clore.ai 的 NVIDIA 服务器上，始终使用 `nvenc` 编码器。它将编码卸载到 GPU 硬件，从而释放 CPU 以运行游戏本身。
{% endhint %}

### 码率指南

| 速度      | 分辨率     | 码率        |
| ------- | ------- | --------- |
| 快速      | 1080p60 | 20 Mbps   |
| 很好      | 1080p60 | 35 Mbps   |
| 适合照片级写实 | 1080p60 | 50 Mbps   |
| 很好      | 1440p60 | 50 Mbps   |
| 适合照片级写实 | 4K60    | 100+ Mbps |

***

## 步骤 5：添加要串流的应用

在 Sunshine Web UI → **应用** → **添加新项**:

### 示例：桌面串流

```
名称：Desktop
命令：（留空以串流完整桌面）
工作目录：/home/user
```

### 示例：Steam 游戏

```
名称：Steam Big Picture
命令：steam -bigpicture
工作目录：/home/user
分离命令：（空）
```

### 示例：自定义游戏

```
名称：My Game
命令：/opt/games/mygame/start.sh
工作目录：/opt/games/mygame
图片：/opt/games/mygame/cover.png
```

***

## 步骤 6：为无头服务器设置虚拟显示

Clore.ai 服务器是无头的（无物理显示器）。你需要一个虚拟显示：

```bash
# 安装虚拟显示驱动
apt-get update && apt-get install -y xvfb x11vnc xfce4

# 创建虚拟显示
Xvfb :1 -screen 0 1920x1080x24 &
export DISPLAY=:1

# 启动桌面环境
startxfce4 &
```

### 或者使用虚拟帧缓冲脚本

```bash
#!/bin/bash
# start-display.sh

export DISPLAY=:1

# 启动虚拟显示
Xvfb :1 -screen 0 1920x1080x24 -ac +extension GLX +render -noreset &
sleep 2

# 启动窗口管理器
openbox &
sleep 1

# 启动 Sunshine
docker compose up -d sunshine
```

使其可执行并运行：

```bash
chmod +x start-display.sh
./start-display.sh
```

{% hint style="info" %}
**无显示的 NVIDIA GPU**：将以下内容添加到你的 Sunshine 配置以在没有物理显示器时使用 GPU：

```bash
export __GL_SYNC_TO_VBLANK=0
export __GL_GSYNC_ALLOWED=0
```

{% endhint %}

***

## 步骤 7：使用 Moonlight 客户端连接

### 安装 Moonlight

| 平台           | 下载                                                       |
| ------------ | -------------------------------------------------------- |
| Windows      | [moonlight-stream.org](https://moonlight-stream.org)     |
| macOS        | Mac App Store 或 moonlight-stream.org                     |
| Linux        | `flatpak install flathub com.moonlight_stream.Moonlight` |
| Android      | Google Play 商店                                           |
| iOS          | App Store                                                |
| Raspberry Pi | `apt install moonlight-embedded`                         |

### 将 Moonlight 与 Sunshine 配对

1. 在你的本地设备上打开 Moonlight
2. 点击 **添加 PC** 并输入你的 Clore.ai 服务器 IP
3. Moonlight 将显示一个 **PIN 代码**
4. 转到 Sunshine Web UI → **固定** → 输入 PIN
5. 连接已建立！✅

### Moonlight 的最佳体验设置

```
分辨率：1920x1080
FPS：60
码率：50 Mbps
视频编码：HEVC（H.265）
硬件解码：已启用
音频：立体声
```

***

## 步骤 8：端口转发参考

| 端口          | 协议  | 用途                   |
| ----------- | --- | -------------------- |
| 22          | TCP | SSH 管理               |
| 47984       | TCP | HTTPS（兼容 GameStream） |
| 47989       | TCP | HTTP（兼容 GameStream）  |
| 47990       | TCP | HTTPS Web UI         |
| 48010       | TCP | RTSP（串流控制）           |
| 47998-48000 | UDP | 视频/音频串流              |
| 48010       | UDP | 视频串流                 |

{% hint style="warning" %}
**Clore.ai 端口映射**：下单时，确保端口 47990 和 48010 在你的端口映射中。如果可能，也应提供用于串流的 UDP 端口（47998-48000）。
{% endhint %}

***

## 故障排除

### Sunshine 无法启动

```bash
# 检查容器日志
docker logs sunshine --tail 50

# 常见修复：权限
chmod 777 /tmp/.X11-unix
xhost +local:docker
```

### 串流时黑屏

```bash
# 验证虚拟显示是否在运行
export DISPLAY=:1
xdpyinfo | grep dimensions

# 重启显示
pkill Xvfb
Xvfb :1 -screen 0 1920x1080x24 &
```

### 高延迟

* 将客户端端的 **Wi-Fi 切换到以太网** 在客户端一侧
* 在 Moonlight 设置中降低码率
* 使用 **H.265** 代替 H.264（在相同画质下有更好的压缩）
* 在生成面板中启用 **帧率调节** 在 Sunshine 配置中

### NVENC 错误：“未找到编码器”

```bash
# 验证 NVIDIA GPU 是否可访问
nvidia-smi

# 检查容器是否具有 GPU 访问权限
docker exec sunshine nvidia-smi

# 如果没有，请使用 --gpus all 标志重启
docker stop sunshine && docker rm sunshine
# 使用 --gpus all 重新运行
```

### 认证/配对问题

```bash
# 重置 Sunshine 凭证
docker exec sunshine sunshine --creds admin newpassword

# 清除已配对的客户端
rm -f ./sunshine-config/sunshine_state.json
docker restart sunshine
```

***

## 高级配置

### 多显示器支持

```yaml
# 在 Sunshine 配置（sunshine.conf）中
[video]
adapter_name = /dev/dri/card0
output_name = HDMI-A-1
```

### 通过网络传输音频

安装 PulseAudio 虚拟输出：

```bash
apt-get install -y pulseaudio
pulseaudio --daemon
pactl load-module module-null-sink sink_name=virtual_sink
export PULSE_SERVER=unix:/run/user/1000/pulse/native
```

### 游戏手柄支持

Sunshine 支持虚拟游戏手柄仿真。在配置中启用：

```yaml
[input]
gamepad = enabled
ds4_back_as_touchpad_click = disabled
```

### 唤醒局域网（用于持久化服务器）

```bash
# 即使在不串流时也保持服务器在线
docker update --restart=always sunshine
```

***

## 成本优化

### 计算你的串流成本

```
GPU 租用：~$0.30-0.80/小时（RTX 3080）
存储：~$0.01/小时（50GB SSD）
总计：~$0.31-0.81/小时

对比 GeForce NOW：$9.99-$19.99/月（时长受限）
对比 Xbox Cloud Gaming：$14.99/月（目录受限）
```

### 节省费用的建议

1. **现货实例**：在非高峰时段使用最便宜的可用 GPU
2. **闲置时暂停**：停止容器但保留卷
3. **预先安装游戏**：使用持久卷，这样就无需每次重新下载
4. **自动关机**：添加脚本在无操作时停止服务器

```bash
# 在 30 分钟没有 Moonlight 连接后自动关机
#!/bin/bash
while true; do
  connections=$(ss -tn | grep 47990 | wc -l)
  if [ "$connections" -eq 0 ]; then
    idle_count=$((idle_count + 1))
  else
    idle_count=0
  fi
  if [ "$idle_count" -ge 30 ]; then
    echo "30 分钟未连接，正在关机"
    docker stop sunshine
    break
  fi
  sleep 60
done
```

***

## 安全最佳实践

{% hint style="danger" %}
**切勿在没有认证的情况下暴露 Sunshine！** 始终设置强密码，并考虑使用 VPN 或 SSH 隧道以获得额外的安全性。
{% endhint %}

### SSH 隧道方法（最安全）

```bash
# 在你的本地机器上，创建 SSH 隧道
ssh -L 47990:localhost:47990 \
    -L 48010:localhost:48010 \
    root@<clore-server-ip>

# 然后将 Moonlight 连接到 localhost 而不是服务器 IP
```

### 更改默认凭证

```bash
docker exec sunshine sunshine --creds admin 'Y0urStr0ngP@ssw0rd!'
```

### 按 IP 限制访问

在 Sunshine 配置中：

```
allowed_ips = 192.168.1.0/24,203.0.113.5
```

***

## 性能基准

| GPU      | 分辨率   | FPS | 延迟     | 编码器       |
| -------- | ----- | --- | ------ | --------- |
| RTX 3060 | 1080p | 60  | \~15ms | NVENC     |
| RTX 3080 | 1440p | 60  | \~12ms | NVENC     |
| RTX 4080 | 4K    | 60  | \~10ms | NVENC AV1 |
| RTX 4090 | 4K    | 120 | \~8ms  | NVENC AV1 |

*延迟基于 100 Mbps 本地网络连接测量*

***

## 有用的链接

* [Sunshine GitHub](https://github.com/LizardByte/Sunshine) — 20K+ 星标
* [Moonlight 官方网站](https://moonlight-stream.org)
* [Sunshine 文档](https://docs.lizardbyte.dev/projects/sunshine)
* [LizardByte Discord](https://discord.gg/lizardbyte)
* [Clore.ai 市场](https://clore.ai)

***

## 总结

| 步骤 | 操作                                   |
| -- | ------------------------------------ |
| 1  | 租用带有端口 22、47990、48010 的 GPU 服务器      |
| 2  | 部署 `lizardbyte/sunshine` 通过 Docker   |
| 3  | 在以下地址通过 Web UI 配置 `https://IP:47990` |
| 4  | 设置 NVENC 编码器，码率 50 Mbps              |
| 5  | 创建虚拟显示（Xvfb）                         |
| 6  | 在本地安装 Moonlight 客户端                  |
| 7  | 使用 PIN 代码配对                          |
| 8  | 开始串流并玩游戏！🎮                          |

在 Clore.ai 上使用 Sunshine + Moonlight，可获得媲美任何商业服务的云游戏体验——并对硬件和游戏拥有完全控制。无订阅、无限制、无束缚。

***

## Clore.ai 的 GPU 建议

| 在 Clore.ai 上的预估费用 | 开发/测试 | RTX 3090（24GB） |
| ----------------- | ----- | -------------- |
| 1080p/60fps 串流    | 生产    | RTX 4090（24GB） |
| 4K/高刷新率 串流        | 大规模   | A100 80GB      |
| 大型游戏 + 串流         | 大规模   | A100 80GB      |

> GPU 服务器上。浏览可用 GPU 并按小时租用 — 无需承诺，提供完整的 root 访问权限。 [Clore.ai](https://clore.ai/marketplace) GPU 服务器。浏览可用 GPU 并按小时租用 — 无需承诺，提供完整的 root 访问权限。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/you-xi-yu-liu-mei-ti/sunshine-moonlight.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
