Skip to content

Commit

Permalink
Expose Connection environment variables to the app start (#1430)
Browse files Browse the repository at this point in the history
Co-authored-by: ReenigneArcher <[email protected]>
  • Loading branch information
TheElixZammuto and ReenigneArcher authored Jul 29, 2023
1 parent 4b986b2 commit 3b2a098
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 40 deletions.
60 changes: 28 additions & 32 deletions docs/source/about/app_examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,35 @@ Linux
Changing Resolution and Refresh Rate (Linux - X11)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

+----------------------+--------------------------------------------------------------+
| **Field** | **Value** |
+----------------------+--------------------------------------------------------------+
| Command Preparations | Do: ``xrandr --output HDMI-1 --mode 1920x1080 --rate 60`` |
| +--------------------------------------------------------------+
| | Undo: ``xrandr --output HDMI-1 --mode 3840×2160 --rate 120`` |
+----------------------+--------------------------------------------------------------+
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------+
| **Field** | **Value** |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------+
| Command Preparations | Do: ``sh -c "xrandr --output HDMI-1 --mode \"${SUNSHINE_CLIENT_WIDTH}x${SUNSHINE_CLIENT_HEIGHT}\" --range ${SUNSHINE_CLIENT_FPS}"`` |
| +---------------------------------------------------------------------------------------------------------------------------------------+
| | Undo: ``xrandr --output HDMI-1 --mode 3840x2160 --rate 120`` |
+----------------------+---------------------------------------------------------------------------------------------------------------------------------------+

Changing Resolution and Refresh Rate (Linux - Wayland)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

+----------------------+-------------------------------------------------------------+
| **Field** | **Value** |
+----------------------+-------------------------------------------------------------+
| Command Preparations | Do: ``wlr-xrandr --output HDMI-1 --mode 1920x1080@60Hz`` |
| +-------------------------------------------------------------+
| | Undo: ``wlr-xrandr --output HDMI-1 --mode 3840×2160@120Hz`` |
+----------------------+-------------------------------------------------------------+
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------+
| **Field** | **Value** |
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------+
| Command Preparations | Do: ``sh -c "wlr-xrandr --output HDMI-1 --mode \"${SUNSHINE_CLIENT_WIDTH}x${SUNSHINE_CLIENT_HEIGHT}@${SUNSHINE_CLIENT_FPS}Hz\""`` |
| +-------------------------------------------------------------------------------------------------------------------------------------+
| | Undo: ``wlr-xrandr --output HDMI-1 --mode 3840x2160@120Hz`` |
+----------------------+-------------------------------------------------------------------------------------------------------------------------------------+

Changing Resolution and Refresh Rate (Linux - KDE Plasma - Wayland and X11)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

+----------------------+-------------------------------------------------------------+
| **Field** | **Value** |
+----------------------+-------------------------------------------------------------+
| Command Preparations | Do: ``kscreen-doctor output.HDMI-A-1.mode.1920x1080@60`` |
| +-------------------------------------------------------------+
| | Undo: ``kscreen-doctor output.HDMI-A-1.mode.3840×2160@120`` |
+----------------------+-------------------------------------------------------------+
+----------------------+----------------------------------------------------------------------------------------------------------------------------------+
| **Field** | **Value** |
+----------------------+----------------------------------------------------------------------------------------------------------------------------------+
| Command Preparations | Do: ``sh -c "kscreen-doctor output.HDMI-A-1.mode.${SUNSHINE_CLIENT_WIDTH}x${SUNSHINE_CLIENT_HEIGHT}@${SUNSHINE_CLIENT_FPS}"`` |
| +----------------------------------------------------------------------------------------------------------------------------------+
| | Undo: ``kscreen-doctor output.HDMI-A-1.mode.3840x2160@120`` |
+----------------------+----------------------------------------------------------------------------------------------------------------------------------+

Flatpak
^^^^^^^
Expand Down Expand Up @@ -188,17 +188,13 @@ Changing Resolution and Refresh Rate (Windows)
.. Note:: This example uses the `QRes` tool to change the resolution and refresh rate.
This tool can be downloaded from their `SourceForge repository <https://sourceforge.net/projects/qres/>`_.

+----------------------+----------------------------------------------------+
| **Field** | **Value** |
+----------------------+----------------------------------------------------+
| Command Preparations | Do: ``FullPath\qres.exe /x:1920 /y:1080 /r:60`` |
| +----------------------------------------------------+
| | Undo: ``FullPath\qres.exe /x:3840 /y:2160 /r:120`` |
+----------------------+----------------------------------------------------+

.. Tip:: You can change your host resolution to match the client resolution automatically using the
`Nonary/ResolutionAutomation <https://github.com/Nonary/ResolutionAutomation/>`_ project.

+----------------------+------------------------------------------------------------------------------------------------------------------+
| **Field** | **Value** |
+----------------------+------------------------------------------------------------------------------------------------------------------+
| Command Preparations | Do: ``cmd /C FullPath\qres.exe /x:%SUNSHINE_CLIENT_WIDTH% /y:%SUNSHINE_CLIENT_WIDTH% /r:%SUNSHINE_CLIENT_FPS%`` |
| +------------------------------------------------------------------------------------------------------------------+
| | Undo: ``cmd /C FullPath\qres.exe /x:3840 /y:2160 /r:120`` |
+----------------------+------------------------------------------------------------------------------------------------------------------+

Elevating Commands (Windows)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
31 changes: 27 additions & 4 deletions src/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <boost/property_tree/json_parser.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <string>

// local includes
#include "config.h"
Expand Down Expand Up @@ -267,12 +268,32 @@ namespace nvhttp {

launch_session.host_audio = host_audio;
launch_session.gcm_key = util::from_hex<crypto::aes_t>(get_arg(args, "rikey"), true);
std::stringstream mode = std::stringstream(get_arg(args, "mode"));
// Split mode by the char "x", to populate width/height/fps
int x = 0;
std::string segment;
while (std::getline(mode, segment, 'x')) {
if (x == 0) launch_session.width = atoi(segment.c_str());
if (x == 1) launch_session.height = atoi(segment.c_str());
if (x == 2) launch_session.fps = atoi(segment.c_str());
x++;
}
launch_session.unique_id = (get_arg(args, "uniqueid"));
launch_session.uuid = (get_arg(args, "uuid"));
launch_session.appid = util::from_view(get_arg(args, "appid"));
launch_session.enable_sops = util::from_view(get_arg(args, "sops"));
launch_session.surround_info = util::from_view(get_arg(args, "surroundAudioInfo"));
launch_session.gcmap = util::from_view(get_arg(args, "gcmap"));
launch_session.enable_hdr = 0;
if (args.find("enableHdr"s) != std::end(args)) {
launch_session.enable_hdr = util::from_view(get_arg(args, "enableHdr"));
}
uint32_t prepend_iv = util::endian::big<uint32_t>(util::from_view(get_arg(args, "rikeyid")));
auto prepend_iv_p = (uint8_t *) &prepend_iv;

auto next = std::copy(prepend_iv_p, prepend_iv_p + sizeof(prepend_iv), std::begin(launch_session.iv));
std::fill(next, std::end(launch_session.iv), 0);

BOOST_LOG(error) << launch_session.width << " h: " << launch_session.height << " fps: " << launch_session.fps;
return launch_session;
}

Expand Down Expand Up @@ -757,8 +778,11 @@ namespace nvhttp {
}
}

host_audio = util::from_view(get_arg(args, "localAudioPlayMode"));
auto launch_session = make_launch_session(host_audio, args);

if (appid > 0) {
auto err = proc::proc.execute(appid);
auto err = proc::proc.execute(appid, launch_session);
if (err) {
tree.put("root.<xmlattr>.status_code", err);
tree.put("root.<xmlattr>.status_message", "Failed to start the specified application");
Expand All @@ -768,8 +792,7 @@ namespace nvhttp {
}
}

host_audio = util::from_view(get_arg(args, "localAudioPlayMode"));
rtsp_stream::launch_session_raise(make_launch_session(host_audio, args));
rtsp_stream::launch_session_raise(launch_session);

tree.put("root.<xmlattr>.status_code", 200);
tree.put("root.sessionUrl0", "rtsp://"s + request->local_endpoint().address().to_string() + ':' + std::to_string(map_port(rtsp_stream::RTSP_SETUP_PORT)));
Expand Down
27 changes: 25 additions & 2 deletions src/process.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file src/process.cpp
* @brief todo
* @brief Handles the startup and shutdown of the apps started by a streaming Session.
*/
#define BOOST_BIND_GLOBAL_PLACEHOLDERS

Expand Down Expand Up @@ -101,7 +101,7 @@ namespace proc {
}

int
proc_t::execute(int app_id) {
proc_t::execute(int app_id, rtsp_stream::launch_session_t launch_session) {
// Ensure starting from a clean slate
terminate();

Expand All @@ -120,6 +120,29 @@ namespace proc {
_app_prep_begin = std::begin(_app.prep_cmds);
_app_prep_it = _app_prep_begin;

// Add Stream-specific environment variables
_env["SUNSHINE_APP_ID"] = std::to_string(_app_id);
_env["SUNSHINE_APP_NAME"] = _app.name;
_env["SUNSHINE_CLIENT_WIDTH"] = std::to_string(launch_session.width);
_env["SUNSHINE_CLIENT_HEIGHT"] = std::to_string(launch_session.height);
_env["SUNSHINE_CLIENT_FPS"] = std::to_string(launch_session.fps);
_env["SUNSHINE_CLIENT_HDR"] = launch_session.enable_hdr ? "true" : "false";
_env["SUNSHINE_CLIENT_GCMAP"] = std::to_string(launch_session.gcmap);
_env["SUNSHINE_CLIENT_HOST_AUDIO"] = launch_session.host_audio ? "true" : "false";
_env["SUNSHINE_CLIENT_ENABLE_SOPS"] = launch_session.enable_sops ? "true" : "false";
int channelCount = launch_session.surround_info & (65535);
switch (channelCount) {
case 2:
_env["SUNSHINE_CLIENT_AUDIO_CONFIGURATION"] = "2.0";
break;
case 6:
_env["SUNSHINE_CLIENT_AUDIO_CONFIGURATION"] = "5.1";
break;
case 8:
_env["SUNSHINE_CLIENT_AUDIO_CONFIGURATION"] = "7.1";
break;
}

if (!_app.output.empty() && _app.output != "null"sv) {
#ifdef _WIN32
// fopen() interprets the filename as an ANSI string on Windows, so we must convert it
Expand Down
3 changes: 2 additions & 1 deletion src/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "config.h"
#include "platform/common.h"
#include "rtsp.h"
#include "utility.h"

namespace proc {
Expand Down Expand Up @@ -65,7 +66,7 @@ namespace proc {
_apps(std::move(apps)) {}

int
execute(int app_id);
execute(int app_id, rtsp_stream::launch_session_t launch_session);

/**
* @return _app_id if a process is running, otherwise returns 0
Expand Down
10 changes: 10 additions & 0 deletions src/rtsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ namespace rtsp_stream {
crypto::aes_t iv;

bool host_audio;
std::string unique_id;
std::string uuid;
int width;
int height;
int fps;
int gcmap;
int appid;
int surround_info;
bool enable_hdr;
bool enable_sops;
};

void
Expand Down
26 changes: 25 additions & 1 deletion src_assets/common/assets/web/apps.html
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,26 @@ <h4 class="modal-title">Covers Found</h4>
must be a PNG file. If not set, Sunshine will send default box image.
</div>
</div>
<div class="env-hint">
<div class="form-text"><b>About Environment Variables: </b> All commands get these environment variables by default: </div>
<table>
<tr><td><b>Var Name</b></td><td><b></b></td></tr>
<tr><td style="font-family: monospace">SUNSHINE_APP_ID</td><td>App ID</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_APP_NAME</td><td>App Name</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_WIDTH</td><td>The Width requested by the client</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_HEIGHT</td><td>The Height requested by the client</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_FPS</td><td>The FPS requested by the client</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_HDR</td><td>(true/false) if HDR is enabled by the client</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_GCMAP</td><td>(int) the requested gamepad mask, in a bitset/bitfield format</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_HOST_AUDIO</td><td>(true/false) if the client has requested host audio</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_ENABLE_SOPS</td><td>(true/false) if the client has requested the option to optimize the game for optimal streaming</td></tr>
<tr><td style="font-family: monospace">SUNSHINE_CLIENT_AUDIO_CONFIGURATION</td><td>The Audio Configuration requested by the client (2.0/5.1/7.1)</td></tr>
</table>
<div class="form-text" v-if="platform === 'windows'"><b>Example - QRes for Resolution Automation:</b> <pre>cmd /C &lt;qres path&gt;\QRes.exe /X:%SUNSHINE_CLIENT_WIDTH% /Y:%SUNSHINE_CLIENT_HEIGHT%</pre></div>
<div class="form-text" v-else-if="platform === 'linux'"><b>Example - Xrandr for Resolution Automation:</b> <pre>sh -c "xrandr --output HDMI-1 --mode \"${SUNSHINE_CLIENT_WIDTH}x${SUNSHINE_CLIENT_HEIGHT}\" --range 60"</pre></div>
<div class="form-text" v-else-if="platform === 'macos'"><b>Example - displayplacer for Resolution Automation:</b> <pre>sh -c "displayplacer "id:<screenId> res:${SUNSHINE_CLIENT_WIDTH}x${SUNSHINE_CLIENT_HEIGHT} hz:60 scaling:on origin:(0,0) degree:0""</pre></div>
<div class="form-text"><a href="https://docs.lizardbyte.dev/projects/sunshine/en/latest/about/app_examples.html" target="_blank">See More</a></div>
</div>
<!--buttons-->
<div class="d-flex">
<button @click="showEditForm = false" class="btn btn-secondary m-2">
Expand Down Expand Up @@ -550,4 +570,8 @@ <h4 class="modal-title">Covers Found</h4>
border-top: none;
}

</style>
td {
padding: 0 0.5em;
}

</style>

0 comments on commit 3b2a098

Please sign in to comment.