sunshine init from arg

This commit is contained in:
pigeatgarlic 2025-08-18 13:50:42 +07:00
parent a456afd36e
commit 1b6316c286
8 changed files with 72 additions and 135 deletions

View File

@ -66,7 +66,6 @@ set(SUNSHINE_TARGET_FILES
"${CMAKE_SOURCE_DIR}/src/logging.cpp"
"${CMAKE_SOURCE_DIR}/src/logging.h"
"${CMAKE_SOURCE_DIR}/src/main.cpp"
"${CMAKE_SOURCE_DIR}/src/main.h"
"${CMAKE_SOURCE_DIR}/src/video.cpp"
"${CMAKE_SOURCE_DIR}/src/video.h"
"${CMAKE_SOURCE_DIR}/src/video_colorspace.cpp"

View File

@ -5,8 +5,9 @@
#define MAX_DISPLAY 3
#define TAG_SIZE 8192
#define IN_PACKET_SIZE 5 * 1024 * 1024
#define OUT_PACKET_SIZE 256
#define MEDIA_PACKET_SIZE 5 * 1024 * 1024
#define DATA_PACKET_SIZE 1024
typedef struct {
int active;
@ -22,33 +23,46 @@ typedef struct {
typedef struct {
int size;
char data[IN_PACKET_SIZE];
} InPacket;
char data[MEDIA_PACKET_SIZE];
} MediaPacket;
typedef struct {
int size;
char data[OUT_PACKET_SIZE];
} OutPacket;
char data[DATA_PACKET_SIZE];
} DataPacket;
typedef struct _Queue{
typedef struct _MediaQueue{
int inindex;
int outindex;
InPacket incoming[IN_QUEUE_SIZE];
OutPacket outgoing[OUT_QUEUE_SIZE];
}Queue;
MediaPacket incoming[IN_QUEUE_SIZE];
DataPacket outgoing[OUT_QUEUE_SIZE];
}MediaQueue;
typedef struct _DataQueue{
int inindex;
int outindex;
DataPacket incoming[IN_QUEUE_SIZE];
DataPacket outgoing[OUT_QUEUE_SIZE];
}DataQueue;
typedef struct _DisplayQueue{
Queue internal;
MediaQueue internal;
QueueMetadata metadata;
}DisplayQueue;
typedef struct _Memory {
typedef struct _HIDQueue{
DataQueue internal;
QueueMetadata metadata[MAX_DISPLAY];
}HIDQueue;
typedef struct _MediaMemory {
DisplayQueue video[MAX_DISPLAY];
Queue audio;
Queue data;
Queue logging;
DataQueue audio;
}MediaMemory;
typedef struct _DataMemory {
DataQueue audio;
HIDQueue data;
char worker_info[TAG_SIZE];
}Memory;
}DataMemory;
#endif

View File

@ -28,13 +28,14 @@ Place, Suite 330, Boston, MA 02111-1307 USA
IVSHMEM * IVSHMEM::m_instance = NULL;
IVSHMEM::IVSHMEM() :
IVSHMEM::IVSHMEM(char* path) :
m_initialized(false),
m_handle(INVALID_HANDLE_VALUE),
m_gotSize(false),
m_gotMemory(false)
{
memset(m_devPath,0,512);
memcpy(m_devPath,path,strlen(path));
}
IVSHMEM::~IVSHMEM()
@ -47,61 +48,14 @@ bool IVSHMEM::Initialize()
if (m_initialized)
DeInitialize();
HDEVINFO deviceInfoSet;
PSP_DEVICE_INTERFACE_DETAIL_DATA infData = NULL;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
deviceInfoSet = SetupDiGetClassDevs(NULL, NULL, NULL, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE);
ZeroMemory(&deviceInterfaceData, sizeof(SP_DEVICE_INTERFACE_DATA));
deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
while (true)
m_handle = CreateFileA(m_devPath, 0, 0, NULL, OPEN_EXISTING, 0, 0);
if (m_handle == INVALID_HANDLE_VALUE)
{
if (SetupDiEnumDeviceInterfaces(deviceInfoSet, NULL, &GUID_DEVINTERFACE_IVSHMEM, 0, &deviceInterfaceData) == FALSE)
{
DWORD err = GetLastError();
if (err == ERROR_NO_MORE_ITEMS)
{
BOOST_LOG(error) << "Unable to enumerate the device, is it attached?";
break;
}
BOOST_LOG(error) << ("SetupDiEnumDeviceInterfaces failed");
break;
}
DWORD reqSize = 0;
SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, NULL, 0, &reqSize, NULL);
if (!reqSize)
{
BOOST_LOG(error) << ("SetupDiGetDeviceInterfaceDetail");
break;
}
infData = static_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(malloc(reqSize));
ZeroMemory(infData, reqSize);
infData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
if (!SetupDiGetDeviceInterfaceDetail(deviceInfoSet, &deviceInterfaceData, infData, reqSize, NULL, NULL))
{
BOOST_LOG(error) << "SetupDiGetDeviceInterfaceDetail";
break;
}
m_handle = CreateFile(infData->DevicePath, 0, 0, NULL, OPEN_EXISTING, 0, 0);
if (m_handle == INVALID_HANDLE_VALUE)
{
BOOST_LOG(error) << "CreateFile returned INVALID_HANDLE_VALUE";
break;
}
m_initialized = true;
break;
BOOST_LOG(error) << "CreateFile returned INVALID_HANDLE_VALUE";
return false;
}
if (infData)
free(infData);
SetupDiDestroyDeviceInfoList(deviceInfoSet);
m_initialized = true;
return m_initialized;
}
@ -126,11 +80,6 @@ void IVSHMEM::DeInitialize()
m_gotMemory = false;
}
bool IVSHMEM::IsInitialized()
{
return m_initialized;
}
UINT64 IVSHMEM::GetSize()
{
if (!m_initialized)
@ -203,7 +152,12 @@ HANDLE IVSHMEM::getHandle()
}
void
copy_to_packet(InPacket* packet,void* data, size_t size) {
copy_to_packet(MediaPacket* packet,void* data, size_t size) {
memcpy(packet->data+packet->size,data,size);
packet->size += size;
}
void
copy_to_dpacket(DataPacket* packet,void* data, size_t size) {
memcpy(packet->data+packet->size,data,size);
packet->size += size;
}

View File

@ -96,20 +96,11 @@ IVSHMEM_EVENT, *PIVSHMEM_EVENT;
class IVSHMEM
{
public:
IVSHMEM();
~IVSHMEM();
static IVSHMEM * Get()
{
if (!m_instance)
m_instance = new IVSHMEM();
return m_instance;
}
IVSHMEM(char* path);
~IVSHMEM();
bool Initialize();
void DeInitialize();
bool IsInitialized();
UINT64 GetSize();
void * GetMemory();
HANDLE getHandle();
@ -119,6 +110,8 @@ protected:
private:
static IVSHMEM * m_instance;
char m_devPath[512];
bool m_initialized;
HANDLE m_handle;
UINT64 m_size ; bool m_gotSize ;
@ -127,4 +120,7 @@ private:
void
copy_to_packet(InPacket* packet,void* data, size_t size);
copy_to_packet(MediaPacket* packet,void* data, size_t size);
void
copy_to_dpacket(DataPacket* packet,void* data, size_t size);

View File

@ -72,7 +72,7 @@ namespace logging {
* ```
*/
[[nodiscard]] std::unique_ptr<deinit_t>
init(int min_log_level, Queue* queue) {
init(int min_log_level) {
if (sink) {
// Deinitialize the logging system before reinitializing it. This can probably only ever be hit in tests.
deinit();
@ -86,7 +86,7 @@ namespace logging {
sink->locked_backend()->add_stream(stream);
sink->set_filter(severity >= min_log_level);
sink->set_formatter([queue](const bl::record_view &view, bl::formatting_ostream &os) {
sink->set_formatter([](const bl::record_view &view, bl::formatting_ostream &os) {
constexpr const char *message = "Message";
constexpr const char *severity = "Severity";
constexpr int DATE_BUFFER_SIZE = 21 + 2 + 1; // Full string plus ": \0"
@ -116,14 +116,6 @@ namespace logging {
};
os << log_type << view.attribute_values()[message].extract<std::string>();
auto updated = queue->inindex + 1;
if (updated >= IN_QUEUE_SIZE)
updated = 0;
copy_to_packet(&queue->incoming[updated],(void*)log_type.cbegin(),log_type.size());
auto msg = view.attribute_values()[message].extract<std::string>();
copy_to_packet(&queue->incoming[updated],(void*)msg->c_str(),msg->size());
queue->inindex = updated;
});
// Flush after each log record to ensure log file contents on disk isn't stale.

View File

@ -9,7 +9,6 @@
// lib includes
#include <boost/log/common.hpp>
#include <boost/log/sinks.hpp>
#include <smemory.h>
using text_sink = boost::log::sinks::asynchronous_sink<boost::log::sinks::text_ostream_backend>;
@ -29,7 +28,7 @@ namespace logging {
void
deinit();
[[nodiscard]] std::unique_ptr<deinit_t>
init(int min_log_level,Queue* queue);
init(int min_log_level);
void
setup_av_logging(int min_log_level);
void

View File

@ -14,7 +14,6 @@
#include "globals.h"
#include "interprocess.h"
#include "logging.h"
#include "main.h"
#include "version.h"
#include "video.h"
#include "audio.h"
@ -81,7 +80,7 @@ split (std::string s, char delim) {
int
main(void) {
main(int argc, char *argv[]) {
#ifdef _WIN32
// Switch default C standard library locale to UTF-8 on Windows 10 1803+
setlocale(LC_ALL, ".UTF-8");
@ -94,24 +93,19 @@ main(void) {
#pragma GCC diagnostic pop
mail::man = std::make_shared<safe::mail_raw_t>();
auto ivshmem = new IVSHMEM();
auto ivshmem = new IVSHMEM(argv[1]);
ivshmem->Initialize();
Memory* memory = NULL;
if (ivshmem->GetSize() < (UINT64)sizeof(Memory)) {
MediaMemory* memory = NULL;
if (ivshmem->GetSize() < (UINT64)sizeof(MediaMemory)) {
BOOST_LOG(error) << "Invalid ivshmem size: "sv << ivshmem->GetSize();
BOOST_LOG(error) << "Expected ivshmem size: "sv << sizeof(Memory);
memory = (Memory*)malloc(sizeof(Memory));
BOOST_LOG(error) << "Expected ivshmem size: "sv << sizeof(MediaMemory);
memory = (MediaMemory*)malloc(sizeof(MediaMemory));
} else {
BOOST_LOG(info) << "Found ivshmem shared memory"sv;
memory = (Memory*)ivshmem->GetMemory();
memory = (MediaMemory*)ivshmem->GetMemory();
}
if (memory == NULL) {
BOOST_LOG(error) << "Failed to allocate shared memory"sv;
return StatusCode::INVALID_IVSHMEM;
}
auto log_deinit_guard = logging::init(config::sunshine.min_log_level,&memory->logging);
auto log_deinit_guard = logging::init(config::sunshine.min_log_level);
if (!log_deinit_guard) {
BOOST_LOG(error) << "Logging failed to initialize"sv;
}
@ -185,7 +179,7 @@ main(void) {
auto mail = std::make_shared<safe::mail_raw_t>();
auto pull = [process_shutdown_event,mail](Queue* queue){
auto pull = [process_shutdown_event,mail](MediaQueue* queue){
auto timer = platf::create_high_precision_timer();
auto local_shutdown= mail->event<bool>(mail::shutdown);
auto bitrate = mail->event<int>(mail::bitrate);
@ -194,7 +188,7 @@ main(void) {
auto expected_index = 0;
auto last_bitrate = 6;
char buffer[OUT_PACKET_SIZE] = {0};
char buffer[DATA_PACKET_SIZE] = {0};
while (!process_shutdown_event->peek() && !local_shutdown->peek()) {
while (expected_index == queue->outindex)
timer->sleep_for(1ms);
@ -248,7 +242,7 @@ main(void) {
};
auto push_video = [process_shutdown_event](safe::mail_t mail, Queue* queue){
auto push_video = [process_shutdown_event](safe::mail_t mail, MediaQueue* queue){
auto video_packets = mail->queue<video::packet_t>(mail::video_packets);
auto audio_packets = mail->queue<audio::packet_t>(mail::audio_packets);
auto local_shutdown= mail->event<bool>(mail::shutdown);
@ -286,7 +280,7 @@ main(void) {
local_shutdown->raise(true);
};
auto push_audio = [process_shutdown_event](safe::mail_t mail, Queue* queue){
auto push_audio = [process_shutdown_event](safe::mail_t mail, DataQueue* queue){
auto video_packets = mail->queue<video::packet_t>(mail::video_packets);
auto audio_packets = mail->queue<audio::packet_t>(mail::audio_packets);
auto local_shutdown= mail->event<bool>(mail::shutdown);
@ -312,10 +306,10 @@ main(void) {
findex++;
uint16_t sum = 0;
queue->incoming[updated].size = 0;
copy_to_packet(&queue->incoming[updated],&findex,sizeof(uint32_t));
copy_to_packet(&queue->incoming[updated],&utimestamp,sizeof(uint64_t));
copy_to_packet(&queue->incoming[updated],&sum,sizeof(uint16_t));
copy_to_packet(&queue->incoming[updated],ptr,size);
copy_to_dpacket(&queue->incoming[updated],&findex,sizeof(uint32_t));
copy_to_dpacket(&queue->incoming[updated],&utimestamp,sizeof(uint64_t));
copy_to_dpacket(&queue->incoming[updated],&sum,sizeof(uint16_t));
copy_to_dpacket(&queue->incoming[updated],ptr,size);
queue->inindex = updated;
} while (audio_packets->peek());
}

View File

@ -1,11 +0,0 @@
/**
* @file src/main.h
* @brief Main header file for the Sunshine application.
*/
// macros
#pragma once
// functions
int
main(void);