-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathdata-structures.hpp
263 lines (218 loc) · 6.44 KB
/
data-structures.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#pragma once
#include <boost/asio.hpp>
#include <chrono>
#include <core/audio.hpp>
#include <core/input.hpp>
#include <core/virtual-display.hpp>
#include <deque>
#include <eventbus/event_bus.hpp>
#include <helpers/tsqueue.hpp>
#include <immer/array.hpp>
#include <immer/atom.hpp>
#include <immer/box.hpp>
#include <immer/map.hpp>
#include <immer/vector.hpp>
#include <moonlight/control.hpp>
#include <moonlight/data-structures.hpp>
#include <openssl/x509.h>
#include <optional>
#include <toml.hpp>
#include <utility>
namespace state {
using namespace std::chrono_literals;
using namespace wolf::core;
namespace ba = boost::asio;
struct PlugDeviceEvent {
std::size_t session_id;
std::vector<std::map<std::string, std::string>> udev_events;
std::vector<std::pair<std::string, std::vector<std::string>>> udev_hw_db_entries;
};
struct UnplugDeviceEvent {
std::size_t session_id;
std::vector<std::map<std::string, std::string>> udev_events;
std::vector<std::pair<std::string, std::vector<std::string>>> udev_hw_db_entries;
};
using devices_atom_queue = TSQueue<immer::box<PlugDeviceEvent>>;
struct Runner {
virtual void run(std::size_t session_id,
std::string_view app_state_folder,
std::shared_ptr<devices_atom_queue> plugged_devices_queue,
const immer::array<std::string> &virtual_inputs,
const immer::array<std::pair<std::string, std::string>> &paths,
const immer::map<std::string, std::string> &env_variables) = 0;
virtual toml::value serialise() = 0;
};
/**
* All ports are derived from a base port, default: 47989
*/
enum STANDARD_PORTS_MAPPING {
HTTPS_PORT = 47984,
HTTP_PORT = 47989,
CONTROL_PORT = 47999,
VIDEO_PING_PORT = 48100,
AUDIO_PING_PORT = 48200,
RTSP_SETUP_PORT = 48010
};
struct PairedClient {
std::string client_cert;
std::string app_state_folder;
uint run_uid = 1000;
uint run_gid = 1000;
};
struct PairSignal {
std::string client_ip;
std::string host_ip;
std::shared_ptr<boost::promise<std::string>> user_pin;
};
struct RTPVideoPingEvent {
std::string client_ip;
unsigned short client_port;
};
struct RTPAudioPingEvent {
std::string client_ip;
unsigned short client_port;
};
using PairedClientList = immer::vector<immer::box<PairedClient>>;
enum Encoder {
NVIDIA,
VAAPI,
QUICKSYNC,
SOFTWARE,
APPLE,
UNKNOWN
};
struct App {
moonlight::App base;
std::string h264_gst_pipeline;
Encoder h264_encoder;
std::string hevc_gst_pipeline;
Encoder hevc_encoder;
std::string av1_gst_pipeline;
Encoder av1_encoder;
std::string render_node;
std::string opus_gst_pipeline;
bool start_virtual_compositor;
std::shared_ptr<Runner> runner;
moonlight::control::pkts::CONTROLLER_TYPE joypad_type;
};
/**
* The stored (and user modifiable) configuration
*/
struct Config {
std::string uuid;
std::string hostname;
std::string config_source;
bool support_hevc;
bool support_av1;
/**
* Mutable, paired_clients will be loaded up on startup
* but can be added at runtime
*/
immer::atom<PairedClientList> &paired_clients;
/**
* List of available Apps
*/
immer::vector<App> apps;
};
struct AudioMode {
enum Speakers {
FRONT_LEFT,
FRONT_RIGHT,
FRONT_CENTER,
LOW_FREQUENCY,
BACK_LEFT,
BACK_RIGHT,
SIDE_LEFT,
SIDE_RIGHT,
MAX_SPEAKERS,
};
int channels{};
int streams{};
int coupled_streams{};
immer::array<Speakers> speakers;
};
/**
* Host information like network, certificates and displays
*/
struct Host {
immer::array<moonlight::DisplayMode> display_modes;
immer::array<AudioMode> audio_modes;
const X509 *server_cert;
const EVP_PKEY *server_pkey;
// Network information can be manually set by users, if not, we'll automatically gather them
std::optional<std::string> internal_ip;
std::optional<std::string> mac_address;
};
/**
* Holds temporary results in order to achieve the multistep pairing process
*/
struct PairCache {
std::string client_cert;
std::string aes_key;
// Followings will be filled later on during the pair process
std::optional<std::string> server_secret;
std::optional<std::string> server_challenge;
std::optional<std::string> client_hash;
};
using JoypadTypes = std::variant<input::XboxOneJoypad, input::SwitchJoypad, input::PS5Joypad>;
using JoypadList = immer::map<int /* controller number */, std::shared_ptr<JoypadTypes>>;
/**
* A StreamSession is created when a Moonlight user call `launch`
*
* This will then be fired up in the event_bus so that the rtsp, command, audio and video threads
* can start working their magic.
*/
struct StreamSession {
moonlight::DisplayMode display_mode;
AudioMode audio_mode;
std::shared_ptr<dp::event_bus> event_bus;
std::shared_ptr<App> app;
std::string app_state_folder;
// gcm encryption keys
std::string aes_key;
std::string aes_iv;
// client info
std::size_t session_id;
std::string ip;
/**
* Optional: the wayland display for the current session.
* Will be only set during an active streaming and destroyed on stream end.
*/
std::shared_ptr<immer::atom<virtual_display::wl_state_ptr>> wayland_display =
std::make_shared<immer::atom<virtual_display::wl_state_ptr>>();
// virtual devices
std::shared_ptr<input::Mouse> mouse;
std::shared_ptr<input::Keyboard> keyboard;
std::shared_ptr<immer::atom<JoypadList>> joypads;
std::shared_ptr<input::PenTablet> pen_tablet = nullptr; /* Optional, will be set on first use*/
std::shared_ptr<input::TouchScreen> touch_screen = nullptr; /* Optional, will be set on first use*/
};
// TODO: unplug device event? Or should this be tied to the session?
using SessionsAtoms = std::shared_ptr<immer::atom<immer::vector<StreamSession>>>;
/**
* The whole application state as a composition of immutable datastructures
*/
struct AppState {
/**
* The stored (and user modifiable) configuration
*/
immer::box<Config> config;
/**
* Host information like network, certificates and displays
*/
immer::box<Host> host;
/**
* Mutable temporary results in order to achieve the multistep pairing process
* It's shared between the two HTTP/HTTPS threads
*/
std::shared_ptr<immer::atom<immer::map<std::string, PairCache>>> pairing_cache;
/**
* A shared bus of events so that we can decouple modules
*/
std::shared_ptr<dp::event_bus> event_bus;
/**
* A list of all currently running (and paused) streaming sessions
*/
SessionsAtoms running_sessions;
};
} // namespace state