Sunshine/src/stream.cpp

1704 lines
56 KiB
C++
Raw Normal View History

/**
* @file src/stream.cpp
* @brief todo
*/
2020-01-01 18:47:34 +01:00
#include "process.h"
#include <future>
2021-05-17 21:21:57 +02:00
#include <queue>
2020-02-08 16:26:38 +01:00
#include <fstream>
#include <openssl/err.h>
#include <boost/endian/arithmetic.hpp>
extern "C" {
2021-07-03 18:32:33 -05:00
#include <moonlight-common-c/src/RtpAudioQueue.h>
2021-07-04 17:12:41 +02:00
#include <moonlight-common-c/src/Video.h>
#include <rs.h>
}
#include "config.h"
#include "input.h"
#include "main.h"
2021-05-17 21:21:57 +02:00
#include "network.h"
#include "stat_trackers.h"
2021-05-17 21:21:57 +02:00
#include "stream.h"
#include "sync.h"
#include "thread_safe.h"
#include "utility.h"
#define IDX_START_A 0
#define IDX_START_B 1
#define IDX_INVALIDATE_REF_FRAMES 2
#define IDX_LOSS_STATS 3
#define IDX_INPUT_DATA 5
#define IDX_RUMBLE_DATA 6
#define IDX_TERMINATION 7
2021-07-04 17:12:41 +02:00
#define IDX_PERIODIC_PING 8
#define IDX_REQUEST_IDR_FRAME 9
#define IDX_ENCRYPTED 10
2023-01-23 20:54:08 -06:00
#define IDX_HDR_MODE 11
static const short packetTypes[] = {
2023-03-27 21:45:29 -04:00
0x0305, // Start A
0x0307, // Start B
0x0301, // Invalidate reference frames
0x0201, // Loss Stats
0x0204, // Frame Stats (unused)
0x0206, // Input data
0x010b, // Rumble data
0x0109, // Termination
2023-03-27 21:45:29 -04:00
0x0200, // Periodic Ping
0x0302, // IDR frame
0x0001, // fully encrypted
0x010e, // HDR mode
};
2020-02-08 23:41:27 +01:00
namespace asio = boost::asio;
2023-03-27 21:45:29 -04:00
namespace sys = boost::system;
2020-02-08 23:41:27 +01:00
using asio::ip::tcp;
using asio::ip::udp;
using namespace std::literals;
namespace stream {
2023-03-27 21:45:29 -04:00
enum class socket_e : int {
video,
audio
};
2020-02-08 23:41:27 +01:00
#pragma pack(push, 1)
2023-03-27 21:45:29 -04:00
struct video_short_frame_header_t {
uint8_t *
payload() {
return (uint8_t *) (this + 1);
}
2023-03-27 21:45:29 -04:00
std::uint8_t headerType; // Always 0x01 for short headers
// Sunshine extension
// Frame processing latency, in 1/10 ms units
// zero when the frame is repeated or there is no backend implementation
boost::endian::little_uint16_at frame_processing_latency;
2023-03-27 21:45:29 -04:00
// Currently known values:
// 1 = Normal P-frame
// 2 = IDR-frame
// 4 = P-frame with intra-refresh blocks
// 5 = P-frame after reference frame invalidation
std::uint8_t frameType;
2023-03-27 21:45:29 -04:00
std::uint8_t unknown2[4];
};
2023-03-27 21:45:29 -04:00
static_assert(
sizeof(video_short_frame_header_t) == 8,
"Short frame header must be 8 bytes");
2023-03-27 21:45:29 -04:00
struct video_packet_raw_t {
uint8_t *
payload() {
return (uint8_t *) (this + 1);
}
2023-03-27 21:45:29 -04:00
RTP_PACKET rtp;
char reserved[4];
2023-03-27 21:45:29 -04:00
NV_VIDEO_PACKET packet;
};
2023-03-27 21:45:29 -04:00
struct audio_packet_raw_t {
uint8_t *
payload() {
return (uint8_t *) (this + 1);
}
2023-03-27 21:45:29 -04:00
RTP_PACKET rtp;
};
2023-03-27 21:45:29 -04:00
struct control_header_v2 {
std::uint16_t type;
std::uint16_t payloadLength;
2021-07-09 13:26:39 +02:00
2023-03-27 21:45:29 -04:00
uint8_t *
payload() {
return (uint8_t *) (this + 1);
}
};
2021-07-09 13:26:39 +02:00
2023-03-27 21:45:29 -04:00
struct control_terminate_t {
control_header_v2 header;
2021-07-09 13:26:39 +02:00
2023-03-27 21:45:29 -04:00
std::uint32_t ec;
};
2021-07-09 13:26:39 +02:00
2023-03-27 21:45:29 -04:00
struct control_rumble_t {
control_header_v2 header;
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
std::uint32_t useless;
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
std::uint16_t id;
std::uint16_t lowfreq;
std::uint16_t highfreq;
};
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
struct control_hdr_mode_t {
control_header_v2 header;
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
std::uint8_t enabled;
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
// Sunshine protocol extension
SS_HDR_METADATA metadata;
};
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
typedef struct control_encrypted_t {
std::uint16_t encryptedHeaderType; // Always LE 0x0001
std::uint16_t length; // sizeof(seq) + 16 byte tag + secondary header and data
2021-07-09 13:26:39 +02:00
2023-03-27 21:45:29 -04:00
// seq is accepted as an arbitrary value in Moonlight
std::uint32_t seq; // Monotonically increasing sequence number (used as IV for AES-GCM)
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
uint8_t *
payload() {
return (uint8_t *) (this + 1);
}
// encrypted control_header_v2 and payload data follow
} *control_encrypted_p;
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
struct audio_fec_packet_raw_t {
uint8_t *
payload() {
return (uint8_t *) (this + 1);
}
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
RTP_PACKET rtp;
AUDIO_FEC_HEADER fecHeader;
};
2021-07-03 18:32:33 -05:00
#pragma pack(pop)
2023-03-27 21:45:29 -04:00
constexpr std::size_t
round_to_pkcs7_padded(std::size_t size) {
return ((size + 15) / 16) * 16;
}
2023-03-27 21:45:29 -04:00
constexpr std::size_t MAX_AUDIO_PACKET_SIZE = 1400;
using rh_t = util::safe_ptr<reed_solomon, reed_solomon_release>;
using video_packet_t = util::c_ptr<video_packet_raw_t>;
using audio_packet_t = util::c_ptr<audio_packet_raw_t>;
using audio_fec_packet_t = util::c_ptr<audio_fec_packet_raw_t>;
using audio_aes_t = std::array<char, round_to_pkcs7_padded(MAX_AUDIO_PACKET_SIZE)>;
using message_queue_t = std::shared_ptr<safe::queue_t<std::pair<std::uint16_t, std::string>>>;
using message_queue_queue_t = std::shared_ptr<safe::queue_t<std::tuple<socket_e, asio::ip::address, message_queue_t>>>;
// return bytes written on success
// return -1 on error
static inline int
encode_audio(int featureSet, const audio::buffer_t &plaintext, audio_packet_t &destination, std::uint32_t avRiKeyIv, crypto::cipher::cbc_t &cbc) {
// If encryption isn't enabled
if (!(featureSet & 0x20)) {
std::copy(std::begin(plaintext), std::end(plaintext), destination->payload());
return plaintext.size();
}
2023-03-27 21:45:29 -04:00
crypto::aes_t iv {};
*(std::uint32_t *) iv.data() = util::endian::big<std::uint32_t>(avRiKeyIv + destination->rtp.sequenceNumber);
2023-03-27 21:45:29 -04:00
return cbc.encrypt(std::string_view { (char *) std::begin(plaintext), plaintext.size() }, destination->payload(), &iv);
}
2023-03-27 21:45:29 -04:00
static inline void
while_starting_do_nothing(std::atomic<session::state_e> &state) {
while (state.load(std::memory_order_acquire) == session::state_e::STARTING) {
std::this_thread::sleep_for(1ms);
}
}
2023-03-27 21:45:29 -04:00
class control_server_t {
public:
int
bind(std::uint16_t port) {
_host = net::host_create(_addr, config::stream.channels, port);
2023-03-27 21:45:29 -04:00
return !(bool) _host;
}
2023-03-27 21:45:29 -04:00
void
emplace_addr_to_session(const std::string &addr, session_t &session) {
auto lg = _map_addr_session.lock();
2023-03-27 21:45:29 -04:00
_map_addr_session->emplace(addr, std::make_pair(0u, &session));
}
2023-03-27 21:45:29 -04:00
// Get session associated with address.
// If none are found, try to find a session not yet claimed. (It will be marked by a port of value 0
// If none of those are found, return nullptr
session_t *
get_session(const net::peer_t peer);
// Circular dependency:
// iterate refers to session
// session refers to broadcast_ctx_t
// broadcast_ctx_t refers to control_server_t
// Therefore, iterate is implemented further down the source file
void
iterate(std::chrono::milliseconds timeout);
void
call(std::uint16_t type, session_t *session, const std::string_view &payload);
void
map(uint16_t type, std::function<void(session_t *, const std::string_view &)> cb) {
_map_type_cb.emplace(type, std::move(cb));
}
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
int
send(const std::string_view &payload, net::peer_t peer) {
auto packet = enet_packet_create(payload.data(), payload.size(), ENET_PACKET_FLAG_RELIABLE);
if (enet_peer_send(peer, 0, packet)) {
enet_packet_destroy(packet);
2023-03-27 21:45:29 -04:00
return -1;
}
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
return 0;
2021-07-05 20:58:53 +02:00
}
2023-03-27 21:45:29 -04:00
void
flush() {
enet_host_flush(_host.get());
}
2023-03-27 21:45:29 -04:00
// Callbacks
std::unordered_map<std::uint16_t, std::function<void(session_t *, const std::string_view &)>> _map_type_cb;
2023-03-27 21:45:29 -04:00
// Mapping ip:port to session
sync_util::sync_t<std::unordered_multimap<std::string, std::pair<std::uint16_t, session_t *>>> _map_addr_session;
2023-03-27 21:45:29 -04:00
ENetAddress _addr;
net::host_t _host;
};
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
struct broadcast_ctx_t {
message_queue_queue_t message_queue_queue;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
std::thread recv_thread;
std::thread video_thread;
std::thread audio_thread;
std::thread control_thread;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
asio::io_service io;
2020-02-09 16:12:36 +01:00
2023-03-27 21:45:29 -04:00
udp::socket video_sock { io };
udp::socket audio_sock { io };
2023-03-27 21:45:29 -04:00
// This is purely for administrative purposes.
// It's possible two instances of Moonlight are behind a NAT.
// From Sunshine's point of view, the ip addresses are identical
// We need some way to know what ports are already used for different streams
sync_util::sync_t<std::vector<std::pair<std::string, std::uint16_t>>> audio_video_connections;
2023-03-27 21:45:29 -04:00
control_server_t control_server;
};
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
struct session_t {
config_t config;
2023-03-27 21:45:29 -04:00
safe::mail_t mail;
2023-03-27 21:45:29 -04:00
std::shared_ptr<input::input_t> input;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
std::thread audioThread;
std::thread videoThread;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
std::chrono::steady_clock::time_point pingTimeout;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
safe::shared_t<broadcast_ctx_t>::ptr_t broadcast_ref;
2023-03-27 21:45:29 -04:00
struct {
int lowseq;
udp::endpoint peer;
safe::mail_raw_t::event_t<bool> idr_events;
std::unique_ptr<platf::deinit_t> qos;
} video;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
struct {
crypto::cipher::cbc_t cipher;
2023-03-27 21:45:29 -04:00
std::uint16_t sequenceNumber;
// avRiKeyId == util::endian::big(First (sizeof(avRiKeyId)) bytes of launch_session->iv)
std::uint32_t avRiKeyId;
std::uint32_t timestamp;
udp::endpoint peer;
2023-03-27 21:45:29 -04:00
util::buffer_t<char> shards;
util::buffer_t<uint8_t *> shards_p;
2023-03-27 21:45:29 -04:00
audio_fec_packet_t fec_packet;
std::unique_ptr<platf::deinit_t> qos;
} audio;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
struct {
crypto::cipher::gcm_t cipher;
crypto::aes_t iv;
2023-03-27 21:45:29 -04:00
net::peer_t peer;
std::uint8_t seq;
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
platf::rumble_queue_t rumble_queue;
safe::mail_raw_t::event_t<video::hdr_info_t> hdr_queue;
} control;
2023-03-27 21:45:29 -04:00
safe::mail_raw_t::event_t<bool> shutdown_event;
safe::signal_t controlEnd;
2023-03-27 21:45:29 -04:00
std::atomic<session::state_e> state;
};
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
/**
* First part of cipher must be struct of type control_encrypted_t
*
* returns empty string_view on failure
* returns string_view pointing to payload data
*/
2023-03-27 21:45:29 -04:00
template <std::size_t max_payload_size>
static inline std::string_view
encode_control(session_t *session, const std::string_view &plaintext, std::array<std::uint8_t, max_payload_size> &tagged_cipher) {
static_assert(
max_payload_size >= sizeof(control_encrypted_t) + sizeof(crypto::cipher::tag_size),
"max_payload_size >= sizeof(control_encrypted_t) + sizeof(crypto::cipher::tag_size)");
if (session->config.controlProtocolType != 13) {
return plaintext;
}
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
crypto::aes_t iv {};
auto seq = session->control.seq++;
iv[0] = seq;
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
auto packet = (control_encrypted_p) tagged_cipher.data();
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
auto bytes = session->control.cipher.encrypt(plaintext, packet->payload(), &iv);
if (bytes <= 0) {
BOOST_LOG(error) << "Couldn't encrypt control data"sv;
return {};
}
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
std::uint16_t packet_length = bytes + crypto::cipher::tag_size + sizeof(control_encrypted_t::seq);
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
packet->encryptedHeaderType = util::endian::little(0x0001);
packet->length = util::endian::little(packet_length);
packet->seq = util::endian::little(seq);
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
return std::string_view { (char *) tagged_cipher.data(), packet_length + sizeof(control_encrypted_t) - sizeof(control_encrypted_t::seq) };
}
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
int
start_broadcast(broadcast_ctx_t &ctx);
void
end_broadcast(broadcast_ctx_t &ctx);
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
static auto broadcast = safe::make_shared<broadcast_ctx_t>(start_broadcast, end_broadcast);
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
session_t *
control_server_t::get_session(const net::peer_t peer) {
TUPLE_2D(port, addr_string, platf::from_sockaddr_ex((sockaddr *) &peer->address.address));
2023-03-27 21:45:29 -04:00
auto lg = _map_addr_session.lock();
TUPLE_2D(begin, end, _map_addr_session->equal_range(addr_string));
2020-02-10 00:33:12 +01:00
2023-03-27 21:45:29 -04:00
auto it = std::end(_map_addr_session.raw);
for (auto pos = begin; pos != end; ++pos) {
TUPLE_2D_REF(session_port, session_p, pos->second);
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
if (port == session_port) {
return session_p;
}
else if (session_port == 0) {
it = pos;
}
}
2023-03-27 21:45:29 -04:00
if (it != std::end(_map_addr_session.raw)) {
TUPLE_2D_REF(session_port, session_p, it->second);
2023-03-27 21:45:29 -04:00
session_p->control.peer = peer;
session_port = port;
return session_p;
}
2023-03-27 21:45:29 -04:00
return nullptr;
}
2023-03-27 21:45:29 -04:00
void
control_server_t::call(std::uint16_t type, session_t *session, const std::string_view &payload) {
auto cb = _map_type_cb.find(type);
if (cb == std::end(_map_type_cb)) {
BOOST_LOG(debug)
<< "type [Unknown] { "sv << util::hex(type).to_string_view() << " }"sv << std::endl
<< "---data---"sv << std::endl
<< util::hex_vec(payload) << std::endl
<< "---end data---"sv;
}
else {
cb->second(session, payload);
}
2021-07-05 20:58:53 +02:00
}
2023-03-27 21:45:29 -04:00
void
control_server_t::iterate(std::chrono::milliseconds timeout) {
ENetEvent event;
auto res = enet_host_service(_host.get(), &event, timeout.count());
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
if (res > 0) {
auto session = get_session(event.peer);
if (!session) {
BOOST_LOG(warning) << "Rejected connection from ["sv << platf::from_sockaddr((sockaddr *) &event.peer->address.address) << "]: it's not properly set up"sv;
enet_peer_disconnect_now(event.peer, 0);
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
return;
}
session->pingTimeout = std::chrono::steady_clock::now() + config::stream.ping_timeout;
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE: {
net::packet_t packet { event.packet };
auto type = *(std::uint16_t *) packet->data;
std::string_view payload { (char *) packet->data + sizeof(type), packet->dataLength - sizeof(type) };
call(type, session, payload);
} break;
case ENET_EVENT_TYPE_CONNECT:
BOOST_LOG(info) << "CLIENT CONNECTED"sv;
break;
case ENET_EVENT_TYPE_DISCONNECT:
BOOST_LOG(info) << "CLIENT DISCONNECTED"sv;
// No more clients to send video data to ^_^
if (session->state == session::state_e::RUNNING) {
session::stop(*session);
}
break;
case ENET_EVENT_TYPE_NONE:
break;
}
2020-02-10 00:33:12 +01:00
}
2023-03-27 21:45:29 -04:00
}
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
namespace fec {
using rs_t = util::safe_ptr<reed_solomon, reed_solomon_release>;
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
struct fec_t {
size_t data_shards;
size_t nr_shards;
size_t percentage;
2021-05-17 21:21:57 +02:00
2023-03-27 21:45:29 -04:00
size_t blocksize;
util::buffer_t<char> shards;
2023-03-27 21:45:29 -04:00
char *
data(size_t el) {
return &shards[el * blocksize];
2021-05-17 21:21:57 +02:00
}
2023-03-27 21:45:29 -04:00
std::string_view
operator[](size_t el) const {
return { &shards[el * blocksize], blocksize };
}
2023-03-27 21:45:29 -04:00
size_t
size() const {
return nr_shards;
}
};
2023-03-27 21:45:29 -04:00
static fec_t
encode(const std::string_view &payload, size_t blocksize, size_t fecpercentage, size_t minparityshards) {
auto payload_size = payload.size();
2023-03-27 21:45:29 -04:00
auto pad = payload_size % blocksize != 0;
2023-03-27 21:45:29 -04:00
auto data_shards = payload_size / blocksize + (pad ? 1 : 0);
auto parity_shards = (data_shards * fecpercentage + 99) / 100;
2023-03-27 21:45:29 -04:00
// increase the FEC percentage for this frame if the parity shard minimum is not met
if (parity_shards < minparityshards) {
parity_shards = minparityshards;
fecpercentage = (100 * parity_shards) / data_shards;
2023-03-27 21:45:29 -04:00
BOOST_LOG(verbose) << "Increasing FEC percentage to "sv << fecpercentage << " to meet parity shard minimum"sv << std::endl;
}
2023-03-27 21:45:29 -04:00
auto nr_shards = data_shards + parity_shards;
if (nr_shards > DATA_SHARDS_MAX) {
BOOST_LOG(warning)
<< "Number of fragments for reed solomon exceeds DATA_SHARDS_MAX"sv << std::endl
<< nr_shards << " > "sv << DATA_SHARDS_MAX
<< ", skipping error correction"sv;
2023-03-27 21:45:29 -04:00
nr_shards = data_shards;
fecpercentage = 0;
}
2023-03-27 21:45:29 -04:00
util::buffer_t<char> shards { nr_shards * blocksize };
util::buffer_t<uint8_t *> shards_p { nr_shards };
2023-03-27 21:45:29 -04:00
// copy payload + padding
auto next = std::copy(std::begin(payload), std::end(payload), std::begin(shards));
std::fill(next, std::end(shards), 0); // padding with zero
2023-03-27 21:45:29 -04:00
for (auto x = 0; x < nr_shards; ++x) {
shards_p[x] = (uint8_t *) &shards[x * blocksize];
}
2023-03-27 21:45:29 -04:00
if (data_shards + parity_shards <= DATA_SHARDS_MAX) {
// packets = parity_shards + data_shards
rs_t rs { reed_solomon_new(data_shards, parity_shards) };
2023-03-27 21:45:29 -04:00
reed_solomon_encode(rs.get(), shards_p.begin(), nr_shards, blocksize);
}
2023-03-27 21:45:29 -04:00
return {
data_shards,
nr_shards,
fecpercentage,
blocksize,
std::move(shards)
};
}
} // namespace fec
2023-03-27 21:45:29 -04:00
template <class F>
std::vector<uint8_t>
insert(uint64_t insert_size, uint64_t slice_size, const std::string_view &data, F &&f) {
auto pad = data.size() % slice_size != 0;
auto elements = data.size() / slice_size + (pad ? 1 : 0);
2023-03-27 21:45:29 -04:00
std::vector<uint8_t> result;
result.resize(elements * insert_size + data.size());
2023-03-27 21:45:29 -04:00
auto next = std::begin(data);
for (auto x = 0; x < elements - 1; ++x) {
void *p = &result[x * (insert_size + slice_size)];
2023-03-27 21:45:29 -04:00
f(p, x, elements);
2023-03-27 21:45:29 -04:00
std::copy(next, next + slice_size, (char *) p + insert_size);
next += slice_size;
}
2023-03-27 21:45:29 -04:00
auto x = elements - 1;
2021-05-17 21:21:57 +02:00
void *p = &result[x * (insert_size + slice_size)];
f(p, x, elements);
2023-03-27 21:45:29 -04:00
std::copy(next, std::end(data), (char *) p + insert_size);
2023-03-27 21:45:29 -04:00
return result;
}
2023-03-27 21:45:29 -04:00
std::vector<uint8_t>
replace(const std::string_view &original, const std::string_view &old, const std::string_view &_new) {
std::vector<uint8_t> replaced;
2023-03-27 21:45:29 -04:00
auto begin = std::begin(original);
auto end = std::end(original);
auto next = std::search(begin, end, std::begin(old), std::end(old));
2023-03-27 21:45:29 -04:00
std::copy(begin, next, std::back_inserter(replaced));
if (next != end) {
std::copy(std::begin(_new), std::end(_new), std::back_inserter(replaced));
std::copy(next + old.size(), end, std::back_inserter(replaced));
}
2019-12-08 02:44:25 +01:00
2023-03-27 21:45:29 -04:00
return replaced;
}
2019-12-08 02:44:25 +01:00
2023-03-27 21:45:29 -04:00
int
send_rumble(session_t *session, std::uint16_t id, std::uint16_t lowfreq, std::uint16_t highfreq) {
if (!session->control.peer) {
BOOST_LOG(warning) << "Couldn't send rumble data, still waiting for PING from Moonlight"sv;
// Still waiting for PING from Moonlight
return -1;
}
2023-03-27 21:45:29 -04:00
control_rumble_t plaintext;
plaintext.header.type = packetTypes[IDX_RUMBLE_DATA];
plaintext.header.payloadLength = sizeof(control_rumble_t) - sizeof(control_header_v2);
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
plaintext.useless = 0xC0FFEE;
plaintext.id = util::endian::little(id);
plaintext.lowfreq = util::endian::little(lowfreq);
plaintext.highfreq = util::endian::little(highfreq);
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(verbose) << id << " :: "sv << util::hex(lowfreq).to_string_view() << " :: "sv << util::hex(highfreq).to_string_view();
std::array<std::uint8_t,
sizeof(control_encrypted_t) + crypto::cipher::round_to_pkcs7_padded(sizeof(plaintext)) + crypto::cipher::tag_size>
encrypted_payload;
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
auto payload = encode_control(session, util::view(plaintext), encrypted_payload);
if (session->broadcast_ref->control_server.send(payload, session->control.peer)) {
TUPLE_2D(port, addr, platf::from_sockaddr_ex((sockaddr *) &session->control.peer->address.address));
BOOST_LOG(warning) << "Couldn't send termination code to ["sv << addr << ':' << port << ']';
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
return -1;
}
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Send gamepadnr ["sv << id << "] with lowfreq ["sv << lowfreq << "] and highfreq ["sv << highfreq << ']';
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
return 0;
}
2021-07-22 18:19:25 +02:00
2023-03-27 21:45:29 -04:00
int
send_hdr_mode(session_t *session, video::hdr_info_t hdr_info) {
if (!session->control.peer) {
BOOST_LOG(warning) << "Couldn't send HDR mode, still waiting for PING from Moonlight"sv;
// Still waiting for PING from Moonlight
return -1;
}
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
control_hdr_mode_t plaintext {};
plaintext.header.type = packetTypes[IDX_HDR_MODE];
plaintext.header.payloadLength = sizeof(control_hdr_mode_t) - sizeof(control_header_v2);
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
plaintext.enabled = hdr_info->enabled;
plaintext.metadata = hdr_info->metadata;
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
std::array<std::uint8_t,
sizeof(control_encrypted_t) + crypto::cipher::round_to_pkcs7_padded(sizeof(plaintext)) + crypto::cipher::tag_size>
encrypted_payload;
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
auto payload = encode_control(session, util::view(plaintext), encrypted_payload);
if (session->broadcast_ref->control_server.send(payload, session->control.peer)) {
TUPLE_2D(port, addr, platf::from_sockaddr_ex((sockaddr *) &session->control.peer->address.address));
BOOST_LOG(warning) << "Couldn't send HDR mode to ["sv << addr << ':' << port << ']';
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
return -1;
}
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Sent HDR mode: " << hdr_info->enabled;
return 0;
2023-01-23 20:54:08 -06:00
}
2023-03-27 21:45:29 -04:00
void
controlBroadcastThread(control_server_t *server) {
server->map(packetTypes[IDX_PERIODIC_PING], [](session_t *session, const std::string_view &payload) {
BOOST_LOG(verbose) << "type [IDX_START_A]"sv;
});
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
server->map(packetTypes[IDX_START_A], [&](session_t *session, const std::string_view &payload) {
BOOST_LOG(debug) << "type [IDX_START_A]"sv;
});
2021-07-04 17:12:41 +02:00
2023-03-27 21:45:29 -04:00
server->map(packetTypes[IDX_START_B], [&](session_t *session, const std::string_view &payload) {
BOOST_LOG(debug) << "type [IDX_START_B]"sv;
});
2023-03-27 21:45:29 -04:00
server->map(packetTypes[IDX_LOSS_STATS], [&](session_t *session, const std::string_view &payload) {
int32_t *stats = (int32_t *) payload.data();
auto count = stats[0];
std::chrono::milliseconds t { stats[1] };
2023-03-27 21:45:29 -04:00
auto lastGoodFrame = stats[3];
2023-03-27 21:45:29 -04:00
BOOST_LOG(verbose)
<< "type [IDX_LOSS_STATS]"sv << std::endl
<< "---begin stats---" << std::endl
<< "loss count since last report [" << count << ']' << std::endl
<< "time in milli since last report [" << t.count() << ']' << std::endl
<< "last good frame [" << lastGoodFrame << ']' << std::endl
<< "---end stats---";
});
2023-03-27 21:45:29 -04:00
server->map(packetTypes[IDX_REQUEST_IDR_FRAME], [&](session_t *session, const std::string_view &payload) {
BOOST_LOG(debug) << "type [IDX_REQUEST_IDR_FRAME]"sv;
2023-03-27 21:45:29 -04:00
session->video.idr_events->raise(true);
});
2023-03-27 21:45:29 -04:00
server->map(packetTypes[IDX_INVALIDATE_REF_FRAMES], [&](session_t *session, const std::string_view &payload) {
auto frames = (std::int64_t *) payload.data();
auto firstFrame = frames[0];
auto lastFrame = frames[1];
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug)
<< "type [IDX_INVALIDATE_REF_FRAMES]"sv << std::endl
<< "firstFrame [" << firstFrame << ']' << std::endl
<< "lastFrame [" << lastFrame << ']';
2023-03-27 21:45:29 -04:00
session->video.idr_events->raise(true);
});
2023-03-27 21:45:29 -04:00
server->map(packetTypes[IDX_INPUT_DATA], [&](session_t *session, const std::string_view &payload) {
BOOST_LOG(debug) << "type [IDX_INPUT_DATA]"sv;
2023-03-27 21:45:29 -04:00
auto tagged_cipher_length = util::endian::big(*(int32_t *) payload.data());
std::string_view tagged_cipher { payload.data() + sizeof(tagged_cipher_length), (size_t) tagged_cipher_length };
2023-03-27 21:45:29 -04:00
std::vector<uint8_t> plaintext;
2023-03-27 21:45:29 -04:00
auto &cipher = session->control.cipher;
auto &iv = session->control.iv;
if (cipher.decrypt(tagged_cipher, plaintext, &iv)) {
// something went wrong :(
2023-03-27 21:45:29 -04:00
BOOST_LOG(error) << "Failed to verify tag"sv;
2023-03-27 21:45:29 -04:00
session::stop(*session);
return;
}
2019-12-17 23:16:28 +01:00
2023-03-27 21:45:29 -04:00
if (tagged_cipher_length >= 16 + sizeof(crypto::aes_t)) {
std::copy(payload.end() - 16, payload.end(), std::begin(iv));
}
2023-03-27 21:45:29 -04:00
input::print(plaintext.data());
input::passthrough(session->input, std::move(plaintext));
});
2023-03-27 21:45:29 -04:00
server->map(packetTypes[IDX_ENCRYPTED], [server](session_t *session, const std::string_view &payload) {
BOOST_LOG(verbose) << "type [IDX_ENCRYPTED]"sv;
2023-03-27 21:45:29 -04:00
auto header = (control_encrypted_p) (payload.data() - 2);
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
auto length = util::endian::little(header->length);
auto seq = util::endian::little(header->seq);
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
if (length < (16 + 4 + 4)) {
BOOST_LOG(warning) << "Control: Runt packet"sv;
return;
}
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
auto tagged_cipher_length = length - 4;
std::string_view tagged_cipher { (char *) header->payload(), (size_t) tagged_cipher_length };
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
auto &cipher = session->control.cipher;
crypto::aes_t iv {};
iv[0] = (std::uint8_t) seq;
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
// update control sequence
++session->control.seq;
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
std::vector<uint8_t> plaintext;
if (cipher.decrypt(tagged_cipher, plaintext, &iv)) {
// something went wrong :(
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(error) << "Failed to verify tag"sv;
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
session::stop(*session);
return;
}
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
// Ensure compatibility with old packet type
std::string_view next_payload { (char *) plaintext.data(), plaintext.size() };
auto type = *(std::uint16_t *) next_payload.data();
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
if (type == packetTypes[IDX_ENCRYPTED]) {
BOOST_LOG(error) << "Bad packet type [IDX_ENCRYPTED] found"sv;
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
session::stop(*session);
return;
}
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
// IDX_INPUT_DATA will attempt to decrypt unencrypted data, therefore we need to skip it.
if (type != packetTypes[IDX_INPUT_DATA]) {
server->call(type, session, next_payload);
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
return;
}
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
// Ensure compatibility with IDX_INPUT_DATA
constexpr auto skip = sizeof(std::uint16_t) * 2;
plaintext.erase(std::begin(plaintext), std::begin(plaintext) + skip);
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
input::print(plaintext.data());
input::passthrough(session->input, std::move(plaintext));
});
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
// This thread handles latency-sensitive control messages
platf::adjust_thread_priority(platf::thread_priority_e::critical);
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
auto shutdown_event = mail::man->event<bool>(mail::broadcast_shutdown);
while (!shutdown_event->peek()) {
bool has_session_awaiting_peer = false;
2023-03-27 21:45:29 -04:00
{
auto lg = server->_map_addr_session.lock();
2021-07-05 20:58:53 +02:00
2023-03-27 21:45:29 -04:00
auto now = std::chrono::steady_clock::now();
2023-03-27 21:45:29 -04:00
KITTY_WHILE_LOOP(auto pos = std::begin(*server->_map_addr_session), pos != std::end(*server->_map_addr_session), {
TUPLE_2D_REF(addr, port_session, *pos);
auto session = port_session.second;
2023-03-27 21:45:29 -04:00
if (now > session->pingTimeout) {
BOOST_LOG(info) << addr << ": Ping Timeout"sv;
session::stop(*session);
}
2023-03-27 21:45:29 -04:00
if (session->state.load(std::memory_order_acquire) == session::state_e::STOPPING) {
pos = server->_map_addr_session->erase(pos);
2023-03-27 21:45:29 -04:00
if (session->control.peer) {
enet_peer_disconnect_now(session->control.peer, 0);
}
2023-03-27 21:45:29 -04:00
session->controlEnd.raise(true);
continue;
}
// Remember if we have a session that's waiting for a peer to connect to the
// control stream. This ensures the clients are properly notified even when
// the app terminates before they finish connecting.
if (!session->control.peer) {
has_session_awaiting_peer = true;
}
2023-03-27 21:45:29 -04:00
auto &rumble_queue = session->control.rumble_queue;
while (rumble_queue->peek()) {
auto rumble = rumble_queue->pop();
2023-03-27 21:45:29 -04:00
send_rumble(session, rumble->id, rumble->lowfreq, rumble->highfreq);
}
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
// Unlike rumble which we send as best-effort, HDR state messages are critical
// for proper functioning of some clients. We must wait to pop entries from
// the queue until we're sure we have a peer to send them to.
auto &hdr_queue = session->control.hdr_queue;
while (session->control.peer && hdr_queue->peek()) {
auto hdr_info = hdr_queue->pop();
2021-07-18 11:05:34 +02:00
2023-03-27 21:45:29 -04:00
send_hdr_mode(session, std::move(hdr_info));
}
2023-01-23 20:54:08 -06:00
2023-03-27 21:45:29 -04:00
++pos;
})
}
2023-01-23 20:54:08 -06:00
// Don't break until any pending sessions either expire or connect
if (proc::proc.running() == 0 && !has_session_awaiting_peer) {
BOOST_LOG(info) << "Process terminated"sv;
2023-03-27 21:45:29 -04:00
break;
}
2019-12-15 19:36:22 +01:00
2023-03-27 21:45:29 -04:00
server->iterate(150ms);
}
2019-12-15 19:36:22 +01:00
2023-03-27 21:45:29 -04:00
// Let all remaining connections know the server is shutting down
// reason: graceful termination
std::uint32_t reason = 0x80030023;
2019-12-15 19:36:22 +01:00
2023-03-27 21:45:29 -04:00
control_terminate_t plaintext;
plaintext.header.type = packetTypes[IDX_TERMINATION];
plaintext.header.payloadLength = sizeof(plaintext.ec);
plaintext.ec = util::endian::big<uint32_t>(reason);
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
std::array<std::uint8_t,
sizeof(control_encrypted_t) + crypto::cipher::round_to_pkcs7_padded(sizeof(plaintext)) + crypto::cipher::tag_size>
encrypted_payload;
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
auto lg = server->_map_addr_session.lock();
for (auto pos = std::begin(*server->_map_addr_session); pos != std::end(*server->_map_addr_session); ++pos) {
auto session = pos->second.second;
2019-12-15 19:36:22 +01:00
// We may not have gotten far enough to have an ENet connection yet
if (session->control.peer) {
auto payload = encode_control(session, util::view(plaintext), encrypted_payload);
if (server->send(payload, session->control.peer)) {
TUPLE_2D(port, addr, platf::from_sockaddr_ex((sockaddr *) &session->control.peer->address.address));
BOOST_LOG(warning) << "Couldn't send termination code to ["sv << addr << ':' << port << ']';
}
2023-03-27 21:45:29 -04:00
}
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
session->shutdown_event->raise(true);
session->controlEnd.raise(true);
2021-07-09 12:27:38 +02:00
}
2023-03-27 21:45:29 -04:00
server->flush();
}
2021-07-09 12:27:38 +02:00
2023-03-27 21:45:29 -04:00
void
recvThread(broadcast_ctx_t &ctx) {
std::map<asio::ip::address, message_queue_t> peer_to_video_session;
std::map<asio::ip::address, message_queue_t> peer_to_audio_session;
2023-03-27 21:45:29 -04:00
auto &video_sock = ctx.video_sock;
auto &audio_sock = ctx.audio_sock;
2023-03-27 21:45:29 -04:00
auto &message_queue_queue = ctx.message_queue_queue;
auto broadcast_shutdown_event = mail::man->event<bool>(mail::broadcast_shutdown);
2023-03-27 21:45:29 -04:00
auto &io = ctx.io;
2023-03-27 21:45:29 -04:00
udp::endpoint peer;
2023-03-27 21:45:29 -04:00
std::array<char, 2048> buf[2];
std::function<void(const boost::system::error_code, size_t)> recv_func[2];
auto populate_peer_to_session = [&]() {
while (message_queue_queue->peek()) {
auto message_queue_opt = message_queue_queue->pop();
TUPLE_3D_REF(socket_type, addr, message_queue, *message_queue_opt);
switch (socket_type) {
case socket_e::video:
if (message_queue) {
peer_to_video_session.emplace(addr, message_queue);
}
else {
peer_to_video_session.erase(addr);
}
break;
case socket_e::audio:
if (message_queue) {
peer_to_audio_session.emplace(addr, message_queue);
}
else {
peer_to_audio_session.erase(addr);
}
break;
2021-05-17 21:21:57 +02:00
}
2020-02-08 16:26:38 +01:00
}
2023-03-27 21:45:29 -04:00
};
2023-03-27 21:45:29 -04:00
auto recv_func_init = [&](udp::socket &sock, int buf_elem, std::map<asio::ip::address, message_queue_t> &peer_to_session) {
recv_func[buf_elem] = [&, buf_elem](const boost::system::error_code &ec, size_t bytes) {
auto fg = util::fail_guard([&]() {
sock.async_receive_from(asio::buffer(buf[buf_elem]), peer, 0, recv_func[buf_elem]);
});
2023-03-27 21:45:29 -04:00
auto type_str = buf_elem ? "AUDIO"sv : "VIDEO"sv;
BOOST_LOG(verbose) << "Recv: "sv << peer.address().to_string() << ':' << peer.port() << " :: " << type_str;
2020-02-09 16:12:36 +01:00
2023-03-27 21:45:29 -04:00
populate_peer_to_session();
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
// No data, yet no error
if (ec == boost::system::errc::connection_refused || ec == boost::system::errc::connection_reset) {
return;
}
2023-03-27 21:45:29 -04:00
if (ec || !bytes) {
BOOST_LOG(error) << "Couldn't receive data from udp socket: "sv << ec.message();
return;
}
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
auto it = peer_to_session.find(peer.address());
if (it != std::end(peer_to_session)) {
BOOST_LOG(debug) << "RAISE: "sv << peer.address().to_string() << ':' << peer.port() << " :: " << type_str;
it->second->raise(peer.port(), std::string { buf[buf_elem].data(), bytes });
}
};
2020-02-08 23:41:27 +01:00
};
2023-03-27 21:45:29 -04:00
recv_func_init(video_sock, 0, peer_to_video_session);
recv_func_init(audio_sock, 1, peer_to_audio_session);
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
video_sock.async_receive_from(asio::buffer(buf[0]), peer, 0, recv_func[0]);
audio_sock.async_receive_from(asio::buffer(buf[1]), peer, 0, recv_func[1]);
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
while (!broadcast_shutdown_event->peek()) {
io.run();
}
}
2023-03-27 21:45:29 -04:00
void
videoBroadcastThread(udp::socket &sock) {
auto shutdown_event = mail::man->event<bool>(mail::broadcast_shutdown);
auto packets = mail::man->queue<video::packet_t>(mail::video_packets);
auto timebase = boost::posix_time::microsec_clock::universal_time();
2023-03-27 21:45:29 -04:00
// Video traffic is sent on this thread
platf::adjust_thread_priority(platf::thread_priority_e::high);
stat_trackers::min_max_avg_tracker<uint16_t> frame_processing_latency_tracker;
2023-03-27 21:45:29 -04:00
while (auto packet = packets->pop()) {
if (shutdown_event->peek()) {
break;
}
2023-03-27 21:45:29 -04:00
auto session = (session_t *) packet->channel_data;
auto lowseq = session->video.lowseq;
2023-03-27 21:45:29 -04:00
auto av_packet = packet->av_packet;
std::string_view payload { (char *) av_packet->data, (size_t) av_packet->size };
std::vector<uint8_t> payload_new;
2023-03-27 21:45:29 -04:00
video_short_frame_header_t frame_header = {};
frame_header.headerType = 0x01; // Short header type
frame_header.frameType = (av_packet->flags & AV_PKT_FLAG_KEY) ? 2 : 1;
if (packet->frame_timestamp) {
auto duration_to_latency = [](const std::chrono::steady_clock::duration &duration) {
const auto duration_us = std::chrono::duration_cast<std::chrono::microseconds>(duration).count();
return (uint16_t) std::clamp<decltype(duration_us)>((duration_us + 50) / 100, 0, std::numeric_limits<uint16_t>::max());
};
uint16_t latency = duration_to_latency(std::chrono::steady_clock::now() - *packet->frame_timestamp);
if (config::sunshine.min_log_level <= 1) {
// Print frame processing latency stats to debug log every 20 seconds
auto print_info = [&](uint16_t min_latency, uint16_t max_latency, double avg_latency) {
auto f = stat_trackers::one_digit_after_decimal();
BOOST_LOG(debug) << "Frame processing latency (min/max/avg): " << f % (min_latency / 10.) << "ms/" << f % (max_latency / 10.) << "ms/" << f % (avg_latency / 10.) << "ms";
};
frame_processing_latency_tracker.collect_and_callback_on_interval(latency, print_info, 20s);
}
frame_header.frame_processing_latency = latency;
}
else {
frame_header.frame_processing_latency = 0;
}
2023-03-27 21:45:29 -04:00
std::copy_n((uint8_t *) &frame_header, sizeof(frame_header), std::back_inserter(payload_new));
std::copy(std::begin(payload), std::end(payload), std::back_inserter(payload_new));
2023-03-27 21:45:29 -04:00
payload = { (char *) payload_new.data(), payload_new.size() };
2023-03-27 21:45:29 -04:00
if (av_packet->flags & AV_PKT_FLAG_KEY) {
for (auto &replacement : *packet->replacements) {
auto frame_old = replacement.old;
auto frame_new = replacement._new;
2023-03-27 21:45:29 -04:00
payload_new = replace(payload, frame_old, frame_new);
payload = { (char *) payload_new.data(), payload_new.size() };
}
}
2023-03-27 21:45:29 -04:00
// insert packet headers
auto blocksize = session->config.packetsize + MAX_RTP_HEADER_SIZE;
auto payload_blocksize = blocksize - sizeof(video_packet_raw_t);
2023-03-27 21:45:29 -04:00
auto fecPercentage = config::stream.fec_percentage;
2023-03-27 21:45:29 -04:00
payload_new = insert(sizeof(video_packet_raw_t), payload_blocksize,
payload, [&](void *p, int fecIndex, int end) {
video_packet_raw_t *video_packet = (video_packet_raw_t *) p;
2023-03-27 21:45:29 -04:00
video_packet->packet.flags = FLAG_CONTAINS_PIC_DATA;
});
2023-03-27 21:45:29 -04:00
payload = std::string_view { (char *) payload_new.data(), payload_new.size() };
2023-03-27 21:45:29 -04:00
// With a fecpercentage of 255, if payload_new is broken up into more than a 100 data_shards
// it will generate greater than DATA_SHARDS_MAX shards.
// Therefore, we start breaking the data up into three separate fec blocks.
auto multi_fec_threshold = 90 * blocksize;
2023-03-27 21:45:29 -04:00
// We can go up to 4 fec blocks, but 3 is plenty
constexpr auto MAX_FEC_BLOCKS = 3;
2023-03-27 21:45:29 -04:00
std::array<std::string_view, MAX_FEC_BLOCKS> fec_blocks;
decltype(fec_blocks)::iterator
fec_blocks_begin = std::begin(fec_blocks),
fec_blocks_end = std::begin(fec_blocks) + 1;
2023-03-27 21:45:29 -04:00
auto lastBlockIndex = 0;
if (payload.size() > multi_fec_threshold) {
BOOST_LOG(verbose) << "Generating multiple FEC blocks"sv;
2023-03-27 21:45:29 -04:00
// Align individual fec blocks to blocksize
auto unaligned_size = payload.size() / MAX_FEC_BLOCKS;
auto aligned_size = ((unaligned_size + (blocksize - 1)) / blocksize) * blocksize;
2023-03-27 21:45:29 -04:00
// Break the data up into 3 blocks, each containing multiple complete video packets.
fec_blocks[0] = payload.substr(0, aligned_size);
fec_blocks[1] = payload.substr(aligned_size, aligned_size);
fec_blocks[2] = payload.substr(aligned_size * 2);
2023-03-27 21:45:29 -04:00
lastBlockIndex = 2 << 6;
fec_blocks_end = std::end(fec_blocks);
}
else {
BOOST_LOG(verbose) << "Generating single FEC block"sv;
fec_blocks[0] = payload;
}
2023-03-27 21:45:29 -04:00
try {
auto blockIndex = 0;
std::for_each(fec_blocks_begin, fec_blocks_end, [&](std::string_view &current_payload) {
auto packets = (current_payload.size() + (blocksize - 1)) / blocksize;
2023-03-27 21:45:29 -04:00
for (int x = 0; x < packets; ++x) {
auto *inspect = (video_packet_raw_t *) &current_payload[x * blocksize];
auto av_packet = packet->av_packet;
2023-03-27 21:45:29 -04:00
inspect->packet.frameIndex = av_packet->pts;
inspect->packet.streamPacketIndex = ((uint32_t) lowseq + x) << 8;
2023-03-27 21:45:29 -04:00
// Match multiFecFlags with Moonlight
inspect->packet.multiFecFlags = 0x10;
inspect->packet.multiFecBlocks = (blockIndex << 4) | lastBlockIndex;
2023-03-27 21:45:29 -04:00
if (x == 0) {
inspect->packet.flags |= FLAG_SOF;
}
2023-03-27 21:45:29 -04:00
if (x == packets - 1) {
inspect->packet.flags |= FLAG_EOF;
}
}
2023-03-27 21:45:29 -04:00
auto shards = fec::encode(current_payload, blocksize, fecPercentage, session->config.minRequiredFecPackets);
2023-03-27 21:45:29 -04:00
// set FEC info now that we know for sure what our percentage will be for this frame
for (auto x = 0; x < shards.size(); ++x) {
auto *inspect = (video_packet_raw_t *) shards.data(x);
2023-03-27 21:45:29 -04:00
// RTP video timestamps use a 90 KHz clock
auto now = boost::posix_time::microsec_clock::universal_time();
auto timestamp = (now - timebase).total_microseconds() / (1000 / 90);
2023-03-27 21:45:29 -04:00
inspect->packet.fecInfo =
(x << 12 |
shards.data_shards << 22 |
shards.percentage << 4);
2023-03-27 21:45:29 -04:00
inspect->rtp.header = 0x80 | FLAG_EXTENSION;
inspect->rtp.sequenceNumber = util::endian::big<uint16_t>(lowseq + x);
inspect->rtp.timestamp = util::endian::big<uint32_t>(timestamp);
2023-03-27 21:45:29 -04:00
inspect->packet.multiFecBlocks = (blockIndex << 4) | lastBlockIndex;
inspect->packet.frameIndex = av_packet->pts;
}
2023-03-27 21:45:29 -04:00
auto peer_address = session->video.peer.address();
auto batch_info = platf::batched_send_info_t {
shards.shards.begin(),
shards.blocksize,
shards.nr_shards,
(uintptr_t) sock.native_handle(),
peer_address,
session->video.peer.port(),
};
// Use a batched send if it's supported on this platform
if (!platf::send_batch(batch_info)) {
// Batched send is not available, so send each packet individually
BOOST_LOG(verbose) << "Falling back to unbatched send"sv;
for (auto x = 0; x < shards.size(); ++x) {
sock.send_to(asio::buffer(shards[x]), session->video.peer);
}
}
2023-03-27 21:45:29 -04:00
if (av_packet->flags & AV_PKT_FLAG_KEY) {
BOOST_LOG(verbose) << "Key Frame ["sv << av_packet->pts << "] :: send ["sv << shards.size() << "] shards..."sv;
}
else {
BOOST_LOG(verbose) << "Frame ["sv << av_packet->pts << "] :: send ["sv << shards.size() << "] shards..."sv << std::endl;
}
2023-03-27 21:45:29 -04:00
++blockIndex;
lowseq += shards.size();
});
2023-03-27 21:45:29 -04:00
session->video.lowseq = lowseq;
}
catch (const std::exception &e) {
BOOST_LOG(error) << "Broadcast video failed "sv << e.what();
std::this_thread::sleep_for(100ms);
}
}
2023-03-27 21:45:29 -04:00
shutdown_event->raise(true);
}
2023-03-27 21:45:29 -04:00
void
audioBroadcastThread(udp::socket &sock) {
auto shutdown_event = mail::man->event<bool>(mail::broadcast_shutdown);
auto packets = mail::man->queue<audio::packet_t>(mail::audio_packets);
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
constexpr auto max_block_size = crypto::cipher::round_to_pkcs7_padded(2048);
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
audio_packet_t audio_packet { (audio_packet_raw_t *) malloc(sizeof(audio_packet_raw_t) + max_block_size) };
fec::rs_t rs { reed_solomon_new(RTPA_DATA_SHARDS, RTPA_FEC_SHARDS) };
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
// For unknown reasons, the RS parity matrix computed by our RS implementation
// doesn't match the one Nvidia uses for audio data. I'm not exactly sure why,
// but we can simply replace it with the matrix generated by OpenFEC which
// works correctly. This is possible because the data and FEC shard count is
// constant and known in advance.
const unsigned char parity[] = { 0x77, 0x40, 0x38, 0x0e, 0xc7, 0xa7, 0x0d, 0x6c };
memcpy(rs.get()->p, parity, sizeof(parity));
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
audio_packet->rtp.header = 0x80;
audio_packet->rtp.packetType = 97;
audio_packet->rtp.ssrc = 0;
2023-03-27 21:45:29 -04:00
// Audio traffic is sent on this thread
platf::adjust_thread_priority(platf::thread_priority_e::high);
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
while (auto packet = packets->pop()) {
if (shutdown_event->peek()) {
break;
}
2023-03-27 21:45:29 -04:00
TUPLE_2D_REF(channel_data, packet_data, *packet);
auto session = (session_t *) channel_data;
2023-03-27 21:45:29 -04:00
auto sequenceNumber = session->audio.sequenceNumber;
auto timestamp = session->audio.timestamp;
2023-03-27 21:45:29 -04:00
// This will be mapped to big-endianness later
// For now, encode_audio needs it to be the proper sequenceNumber
audio_packet->rtp.sequenceNumber = sequenceNumber;
2023-03-27 21:45:29 -04:00
auto bytes = encode_audio(session->config.featureFlags, packet_data, audio_packet, session->audio.avRiKeyId, session->audio.cipher);
if (bytes < 0) {
BOOST_LOG(error) << "Couldn't encode audio packet"sv;
break;
}
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
audio_packet->rtp.sequenceNumber = util::endian::big(sequenceNumber);
audio_packet->rtp.timestamp = util::endian::big(timestamp);
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
session->audio.sequenceNumber++;
session->audio.timestamp += session->config.audio.packetDuration;
2023-03-27 21:45:29 -04:00
auto &shards_p = session->audio.shards_p;
2023-03-27 21:45:29 -04:00
std::copy_n(audio_packet->payload(), bytes, shards_p[sequenceNumber % RTPA_DATA_SHARDS]);
try {
sock.send_to(asio::buffer((char *) audio_packet.get(), sizeof(audio_packet_raw_t) + bytes), session->audio.peer);
2019-12-08 14:43:48 +01:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(verbose) << "Audio ["sv << sequenceNumber << "] :: send..."sv;
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
auto &fec_packet = session->audio.fec_packet;
// initialize the FEC header at the beginning of the FEC block
if (sequenceNumber % RTPA_DATA_SHARDS == 0) {
fec_packet->fecHeader.baseSequenceNumber = util::endian::big(sequenceNumber);
fec_packet->fecHeader.baseTimestamp = util::endian::big(timestamp);
}
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
// generate parity shards at the end of the FEC block
if ((sequenceNumber + 1) % RTPA_DATA_SHARDS == 0) {
reed_solomon_encode(rs.get(), shards_p.begin(), RTPA_TOTAL_SHARDS, bytes);
2021-07-03 18:32:33 -05:00
2023-03-27 21:45:29 -04:00
for (auto x = 0; x < RTPA_FEC_SHARDS; ++x) {
fec_packet->rtp.sequenceNumber = util::endian::big<std::uint16_t>(sequenceNumber + x + 1);
fec_packet->fecHeader.fecShardIndex = x;
memcpy(fec_packet->payload(), shards_p[RTPA_DATA_SHARDS + x], bytes);
sock.send_to(asio::buffer((char *) fec_packet.get(), sizeof(audio_fec_packet_raw_t) + bytes), session->audio.peer);
BOOST_LOG(verbose) << "Audio FEC ["sv << (sequenceNumber & ~(RTPA_DATA_SHARDS - 1)) << ' ' << x << "] :: send..."sv;
}
}
2021-07-03 18:32:33 -05:00
}
2023-03-27 21:45:29 -04:00
catch (const std::exception &e) {
BOOST_LOG(error) << "Broadcast audio failed "sv << e.what();
std::this_thread::sleep_for(100ms);
}
2021-07-03 18:32:33 -05:00
}
2023-03-27 21:45:29 -04:00
shutdown_event->raise(true);
}
2023-03-27 21:45:29 -04:00
int
start_broadcast(broadcast_ctx_t &ctx) {
auto control_port = map_port(CONTROL_PORT);
auto video_port = map_port(VIDEO_STREAM_PORT);
auto audio_port = map_port(AUDIO_STREAM_PORT);
2023-03-27 21:45:29 -04:00
if (ctx.control_server.bind(control_port)) {
BOOST_LOG(error) << "Couldn't bind Control server to port ["sv << control_port << "], likely another process already bound to the port"sv;
2023-03-27 21:45:29 -04:00
return -1;
}
2023-03-27 21:45:29 -04:00
boost::system::error_code ec;
ctx.video_sock.open(udp::v4(), ec);
if (ec) {
BOOST_LOG(fatal) << "Couldn't open socket for Video server: "sv << ec.message();
2023-03-27 21:45:29 -04:00
return -1;
}
2023-03-27 21:45:29 -04:00
ctx.video_sock.bind(udp::endpoint(udp::v4(), video_port), ec);
if (ec) {
BOOST_LOG(fatal) << "Couldn't bind Video server to port ["sv << video_port << "]: "sv << ec.message();
2023-03-27 21:45:29 -04:00
return -1;
}
2023-03-27 21:45:29 -04:00
ctx.audio_sock.open(udp::v4(), ec);
if (ec) {
BOOST_LOG(fatal) << "Couldn't open socket for Audio server: "sv << ec.message();
2023-03-27 21:45:29 -04:00
return -1;
}
2023-03-27 21:45:29 -04:00
ctx.audio_sock.bind(udp::endpoint(udp::v4(), audio_port), ec);
if (ec) {
BOOST_LOG(fatal) << "Couldn't bind Audio server to port ["sv << audio_port << "]: "sv << ec.message();
2023-03-27 21:45:29 -04:00
return -1;
}
2023-03-27 21:45:29 -04:00
ctx.message_queue_queue = std::make_shared<message_queue_queue_t::element_type>(30);
2023-03-27 21:45:29 -04:00
ctx.video_thread = std::thread { videoBroadcastThread, std::ref(ctx.video_sock) };
ctx.audio_thread = std::thread { audioBroadcastThread, std::ref(ctx.audio_sock) };
ctx.control_thread = std::thread { controlBroadcastThread, &ctx.control_server };
2023-03-27 21:45:29 -04:00
ctx.recv_thread = std::thread { recvThread, std::ref(ctx) };
2023-03-27 21:45:29 -04:00
return 0;
}
2023-03-27 21:45:29 -04:00
void
end_broadcast(broadcast_ctx_t &ctx) {
auto broadcast_shutdown_event = mail::man->event<bool>(mail::broadcast_shutdown);
2023-03-27 21:45:29 -04:00
broadcast_shutdown_event->raise(true);
2023-03-27 21:45:29 -04:00
auto video_packets = mail::man->queue<video::packet_t>(mail::video_packets);
auto audio_packets = mail::man->queue<audio::packet_t>(mail::audio_packets);
2020-02-10 00:33:12 +01:00
2023-03-27 21:45:29 -04:00
// Minimize delay stopping video/audio threads
video_packets->stop();
audio_packets->stop();
2020-02-10 00:33:12 +01:00
2023-03-27 21:45:29 -04:00
ctx.message_queue_queue->stop();
ctx.io.stop();
2023-03-27 21:45:29 -04:00
ctx.video_sock.close();
ctx.audio_sock.close();
2023-03-27 21:45:29 -04:00
video_packets.reset();
audio_packets.reset();
2020-02-10 00:33:12 +01:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Waiting for main listening thread to end..."sv;
ctx.recv_thread.join();
BOOST_LOG(debug) << "Waiting for main video thread to end..."sv;
ctx.video_thread.join();
BOOST_LOG(debug) << "Waiting for main audio thread to end..."sv;
ctx.audio_thread.join();
BOOST_LOG(debug) << "Waiting for main control thread to end..."sv;
ctx.control_thread.join();
BOOST_LOG(debug) << "All broadcasting threads ended"sv;
2023-03-27 21:45:29 -04:00
broadcast_shutdown_event->reset();
}
2023-03-27 21:45:29 -04:00
int
recv_ping(decltype(broadcast)::ptr_t ref, socket_e type, udp::endpoint &peer, std::chrono::milliseconds timeout) {
auto constexpr ping = "PING"sv;
2023-03-27 21:45:29 -04:00
auto messages = std::make_shared<message_queue_t::element_type>(30);
ref->message_queue_queue->raise(type, peer.address(), messages);
2023-03-27 21:45:29 -04:00
auto fg = util::fail_guard([&]() {
messages->stop();
2023-03-27 21:45:29 -04:00
// remove message queue from session
ref->message_queue_queue->raise(type, peer.address(), nullptr);
});
2023-03-27 21:45:29 -04:00
auto start_time = std::chrono::steady_clock::now();
auto current_time = start_time;
2023-03-27 21:45:29 -04:00
while (current_time - start_time < config::stream.ping_timeout) {
auto delta_time = current_time - start_time;
2023-03-27 21:45:29 -04:00
auto msg_opt = messages->pop(config::stream.ping_timeout - delta_time);
if (!msg_opt) {
break;
}
2023-03-27 21:45:29 -04:00
TUPLE_2D_REF(port, msg, *msg_opt);
if (msg == ping) {
BOOST_LOG(debug) << "Received ping from "sv << peer.address() << ':' << port << " ["sv << util::hex_vec(msg) << ']';
2023-03-27 21:45:29 -04:00
// Update connection details.
{
auto addr_str = peer.address().to_string();
2023-03-27 21:45:29 -04:00
auto &connections = ref->audio_video_connections;
2023-03-27 21:45:29 -04:00
auto lg = connections.lock();
2023-03-27 21:45:29 -04:00
std::remove_reference_t<decltype(*connections)>::iterator pos = std::end(*connections);
2023-03-27 21:45:29 -04:00
for (auto it = std::begin(*connections); it != std::end(*connections); ++it) {
TUPLE_2D_REF(addr, port_ref, *it);
2023-03-27 21:45:29 -04:00
if (!port_ref && addr_str == addr) {
pos = it;
}
else if (port_ref == port) {
break;
}
}
2023-03-27 21:45:29 -04:00
if (pos == std::end(*connections)) {
continue;
}
2023-03-27 21:45:29 -04:00
pos->second = port;
peer.port(port);
}
2023-03-27 21:45:29 -04:00
return port;
}
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Received non-ping from "sv << peer.address() << ':' << port << " ["sv << util::hex_vec(msg) << ']';
2023-03-27 21:45:29 -04:00
current_time = std::chrono::steady_clock::now();
}
2023-03-27 21:45:29 -04:00
BOOST_LOG(error) << "Initial Ping Timeout"sv;
return -1;
}
2023-03-27 21:45:29 -04:00
void
videoThread(session_t *session) {
auto fg = util::fail_guard([&]() {
session::stop(*session);
});
2023-03-27 21:45:29 -04:00
while_starting_do_nothing(session->state);
2019-12-08 23:31:37 +01:00
2023-03-27 21:45:29 -04:00
auto ref = broadcast.ref();
auto port = recv_ping(ref, socket_e::video, session->video.peer, config::stream.ping_timeout);
if (port < 0) {
return;
}
2023-03-27 21:45:29 -04:00
// Enable QoS tagging on video traffic if requested by the client
if (session->config.videoQosType) {
auto address = session->video.peer.address();
session->video.qos = std::move(platf::enable_socket_qos(ref->video_sock.native_handle(), address,
session->video.peer.port(), platf::qos_data_type_e::video));
}
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Start capturing Video"sv;
video::capture(session->mail, session->config.monitor, session);
}
2023-03-27 21:45:29 -04:00
void
audioThread(session_t *session) {
auto fg = util::fail_guard([&]() {
session::stop(*session);
});
2023-03-27 21:45:29 -04:00
while_starting_do_nothing(session->state);
2020-01-20 00:22:13 +01:00
2023-03-27 21:45:29 -04:00
auto ref = broadcast.ref();
auto port = recv_ping(ref, socket_e::audio, session->audio.peer, config::stream.ping_timeout);
if (port < 0) {
return;
}
2020-01-20 00:22:13 +01:00
2023-03-27 21:45:29 -04:00
// Enable QoS tagging on audio traffic if requested by the client
if (session->config.audioQosType) {
auto address = session->audio.peer.address();
session->audio.qos = std::move(platf::enable_socket_qos(ref->audio_sock.native_handle(), address,
session->audio.peer.port(), platf::qos_data_type_e::audio));
}
2020-01-20 00:22:13 +01:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Start capturing Audio"sv;
audio::capture(session->mail, session->config.audio, session);
}
2023-03-27 21:45:29 -04:00
namespace session {
std::atomic_uint running_sessions;
2020-02-10 00:33:12 +01:00
2023-03-27 21:45:29 -04:00
state_e
state(session_t &session) {
return session.state.load(std::memory_order_relaxed);
}
2023-03-27 21:45:29 -04:00
void
stop(session_t &session) {
while_starting_do_nothing(session.state);
auto expected = state_e::RUNNING;
auto already_stopping = !session.state.compare_exchange_strong(expected, state_e::STOPPING);
if (already_stopping) {
return;
}
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
session.shutdown_event->raise(true);
}
2023-03-27 21:45:29 -04:00
void
join(session_t &session) {
// Current Nvidia drivers have a bug where NVENC can deadlock the encoder thread with hardware-accelerated
// GPU scheduling enabled. If this happens, we will terminate ourselves and the service can restart.
// The alternative is that Sunshine can never start another session until it's manually restarted.
auto task = []() {
BOOST_LOG(fatal) << "Hang detected! Session failed to terminate in 10 seconds."sv;
log_flush();
std::abort();
};
auto force_kill = task_pool.pushDelayed(task, 10s).task_id;
auto fg = util::fail_guard([&force_kill]() {
// Cancel the kill task if we manage to return from this function
task_pool.cancel(force_kill);
});
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Waiting for video to end..."sv;
session.videoThread.join();
BOOST_LOG(debug) << "Waiting for audio to end..."sv;
session.audioThread.join();
BOOST_LOG(debug) << "Waiting for control to end..."sv;
session.controlEnd.view();
// Reset input on session stop to avoid stuck repeated keys
BOOST_LOG(debug) << "Resetting Input..."sv;
input::reset(session.input);
BOOST_LOG(debug) << "Removing references to any connections..."sv;
{
auto video_addr = session.video.peer.address().to_string();
auto audio_addr = session.audio.peer.address().to_string();
2023-03-27 21:45:29 -04:00
auto video_port = session.video.peer.port();
auto audio_port = session.audio.peer.port();
2023-03-27 21:45:29 -04:00
auto &connections = session.broadcast_ref->audio_video_connections;
2023-03-27 21:45:29 -04:00
auto lg = connections.lock();
2023-03-27 21:45:29 -04:00
auto validate_size = connections->size();
for (auto it = std::begin(*connections); it != std::end(*connections);) {
TUPLE_2D_REF(addr, port, *it);
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
if ((video_port == port && video_addr == addr) ||
(audio_port == port && audio_addr == addr)) {
it = connections->erase(it);
}
else {
++it;
}
}
2020-03-15 21:22:42 +01:00
2023-03-27 21:45:29 -04:00
auto new_size = connections->size();
if (validate_size != new_size + 2) {
BOOST_LOG(warning) << "Couldn't remove reference to session connections: ending all broadcasts"sv;
// A reference to the event object is still stored somewhere else. So no need to keep
// a reference to it.
mail::man->event<bool>(mail::broadcast_shutdown)->raise(true);
}
}
2023-03-27 21:45:29 -04:00
// If this is the last session, invoke the platform callbacks
if (--running_sessions == 0) {
platf::streaming_will_stop();
}
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
BOOST_LOG(debug) << "Session ended"sv;
}
2023-03-27 21:45:29 -04:00
int
start(session_t &session, const std::string &addr_string) {
session.input = input::alloc(session.mail);
2023-03-27 21:45:29 -04:00
session.broadcast_ref = broadcast.ref();
if (!session.broadcast_ref) {
return -1;
}
2023-03-27 21:45:29 -04:00
session.broadcast_ref->control_server.emplace_addr_to_session(addr_string, session);
2023-03-27 21:45:29 -04:00
auto addr = boost::asio::ip::make_address(addr_string);
session.video.peer.address(addr);
session.video.peer.port(0);
2023-03-27 21:45:29 -04:00
session.audio.peer.address(addr);
session.audio.peer.port(0);
2023-03-27 21:45:29 -04:00
{
auto &connections = session.broadcast_ref->audio_video_connections;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
auto lg = connections.lock();
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
// allocate a location for connections
connections->emplace_back(addr_string, 0);
connections->emplace_back(addr_string, 0);
}
2023-03-27 21:45:29 -04:00
session.pingTimeout = std::chrono::steady_clock::now() + config::stream.ping_timeout;
2023-03-27 21:45:29 -04:00
session.audioThread = std::thread { audioThread, &session };
session.videoThread = std::thread { videoThread, &session };
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
session.state.store(state_e::RUNNING, std::memory_order_relaxed);
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
// If this is the first session, invoke the platform callbacks
if (++running_sessions == 1) {
platf::streaming_will_start();
}
2023-03-27 21:45:29 -04:00
return 0;
}
2023-03-27 21:45:29 -04:00
std::shared_ptr<session_t>
alloc(config_t &config, crypto::aes_t &gcm_key, crypto::aes_t &iv) {
auto session = std::make_shared<session_t>();
2023-03-27 21:45:29 -04:00
auto mail = std::make_shared<safe::mail_raw_t>();
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
session->shutdown_event = mail->event<bool>(mail::shutdown);
2023-03-27 21:45:29 -04:00
session->config = config;
2023-03-27 21:45:29 -04:00
session->control.rumble_queue = mail->queue<platf::rumble_t>(mail::rumble);
session->control.hdr_queue = mail->event<video::hdr_info_t>(mail::hdr);
session->control.iv = iv;
session->control.cipher = crypto::cipher::gcm_t {
gcm_key, false
};
2023-03-27 21:45:29 -04:00
session->video.idr_events = mail->event<bool>(mail::idr);
session->video.lowseq = 0;
2023-03-27 21:45:29 -04:00
constexpr auto max_block_size = crypto::cipher::round_to_pkcs7_padded(2048);
2023-03-27 21:45:29 -04:00
util::buffer_t<char> shards { RTPA_TOTAL_SHARDS * max_block_size };
util::buffer_t<uint8_t *> shards_p { RTPA_TOTAL_SHARDS };
2023-03-27 21:45:29 -04:00
for (auto x = 0; x < RTPA_TOTAL_SHARDS; ++x) {
shards_p[x] = (uint8_t *) &shards[x * max_block_size];
}
2023-03-27 21:45:29 -04:00
// Audio FEC spans multiple audio packets,
// therefore its session specific
session->audio.shards = std::move(shards);
session->audio.shards_p = std::move(shards_p);
2023-03-27 21:45:29 -04:00
session->audio.fec_packet.reset((audio_fec_packet_raw_t *) malloc(sizeof(audio_fec_packet_raw_t) + max_block_size));
2023-03-27 21:45:29 -04:00
session->audio.fec_packet->rtp.header = 0x80;
session->audio.fec_packet->rtp.packetType = 127;
session->audio.fec_packet->rtp.timestamp = 0;
session->audio.fec_packet->rtp.ssrc = 0;
2023-03-27 21:45:29 -04:00
session->audio.fec_packet->fecHeader.payloadType = 97;
session->audio.fec_packet->fecHeader.ssrc = 0;
2020-02-08 23:41:27 +01:00
2023-03-27 21:45:29 -04:00
session->audio.cipher = crypto::cipher::cbc_t {
gcm_key, true
};
2023-03-27 21:45:29 -04:00
session->audio.avRiKeyId = util::endian::big(*(std::uint32_t *) iv.data());
session->audio.sequenceNumber = 0;
session->audio.timestamp = 0;
session->control.peer = nullptr;
session->state.store(state_e::STOPPED, std::memory_order_relaxed);
session->mail = std::move(mail);
return session;
}
} // namespace session
} // namespace stream