From 5361df6fdf3bedb0add4a02edde0d7af83b4b50e Mon Sep 17 00:00:00 2001 From: work <1127408945@qq.com> Date: Sun, 26 Jul 2026 15:53:18 +0800 Subject: [PATCH 1/4] feat: add real-time streaming statistics (bitrate, FPS, decode time) Add a stats module with per-second console output showing: - Bitrate (Mbps) - Decode/display FPS - Decode time (ms) - Frame size (KB) - Network quality warnings Support all video backends: rk, x11, sdl, aml, pi, mmal, imx Enable with -stats flag or stats=true in config --- CMakeLists.txt | 1 + moonlight.conf | 4 + src/config.c | 7 +- src/config.h | 1 + src/connection.c | 5 +- src/main.c | 4 + src/sdl.c | 11 +++ src/stats.c | 206 +++++++++++++++++++++++++++++++++++++++++++++++ src/stats.h | 45 +++++++++++ src/video/aml.c | 6 ++ src/video/imx.c | 5 ++ src/video/mmal.c | 3 + src/video/pi.c | 4 + src/video/rk.c | 23 ++++++ src/video/sdl.c | 13 +++ src/video/x11.c | 16 +++- 16 files changed, 351 insertions(+), 3 deletions(-) mode change 100644 => 100755 CMakeLists.txt mode change 100644 => 100755 moonlight.conf mode change 100644 => 100755 src/config.c mode change 100644 => 100755 src/config.h mode change 100644 => 100755 src/connection.c mode change 100644 => 100755 src/main.c mode change 100644 => 100755 src/sdl.c create mode 100755 src/stats.c create mode 100755 src/stats.h mode change 100644 => 100755 src/video/aml.c mode change 100644 => 100755 src/video/imx.c mode change 100644 => 100755 src/video/mmal.c mode change 100644 => 100755 src/video/pi.c mode change 100644 => 100755 src/video/rk.c mode change 100644 => 100755 src/video/sdl.c mode change 100644 => 100755 src/video/x11.c diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100644 new mode 100755 index 2be10fda4..7f06bd242 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.6) project(moonlight-embedded VERSION 2.7.1 LANGUAGES C) SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") SET(CMAKE_C_STANDARD 99) +SET(CMAKE_C_EXTENSIONS ON) include(${CMAKE_ROOT}/Modules/GNUInstallDirs.cmake) include(${CMAKE_SOURCE_DIR}/cmake/generate_version_header.cmake) diff --git a/moonlight.conf b/moonlight.conf old mode 100644 new mode 100755 index 8d1b6a6fa..bdd26dc6c --- a/moonlight.conf +++ b/moonlight.conf @@ -78,5 +78,9 @@ ## Enable 5.1/7.1 surround sound #surround = 5.1 +## Enable real-time streaming statistics (bitrate, frame rate, decode time, etc.) +## Displayed on the console, updated every second +#stats = true + ## Load additional configuration files #config = /path/to/config diff --git a/src/config.c b/src/config.c old mode 100644 new mode 100755 index 91f20615a..cf8fccfc1 --- a/src/config.c +++ b/src/config.c @@ -77,6 +77,7 @@ static struct option long_options[] = { {"pin", required_argument, NULL, '5'}, {"port", required_argument, NULL, '6'}, {"hdr", no_argument, NULL, '7'}, + {"stats", no_argument, NULL, '8'}, {0, 0, 0, 0}, }; @@ -260,6 +261,9 @@ static void parse_argument(int c, char* value, PCONFIGURATION config) { case '7': config->hdr = true; break; + case '8': + config->stats = true; + break; case 1: if (config->action == NULL) config->action = value; @@ -380,6 +384,7 @@ void config_parse(int argc, char* argv[], PCONFIGURATION config) { config->rotate = 0; config->codec = CODEC_UNSPECIFIED; config->hdr = false; + config->stats = false; config->pin = 0; config->port = 47989; @@ -399,7 +404,7 @@ void config_parse(int argc, char* argv[], PCONFIGURATION config) { } else { int option_index = 0; int c; - while ((c = getopt_long_only(argc, argv, "-abc:d:efg:h:i:j:k:lm:no:p:q:r:s:tu:v:w:xy45:6:7", long_options, &option_index)) != -1) { + while ((c = getopt_long_only(argc, argv, "-abc:d:efg:h:i:j:k:lm:no:p:q:r:s:tu:v:w:xy45:6:78", long_options, &option_index)) != -1) { parse_argument(c, optarg, config); } } diff --git a/src/config.h b/src/config.h old mode 100644 new mode 100755 index 91ee4a1ff..64a3e6bee --- a/src/config.h +++ b/src/config.h @@ -46,6 +46,7 @@ typedef struct _CONFIGURATION { int inputsCount; enum codecs codec; bool hdr; + bool stats; int pin; unsigned short port; } CONFIGURATION, *PCONFIGURATION; diff --git a/src/connection.c b/src/connection.c old mode 100644 new mode 100755 index d49529121..19efef6b0 --- a/src/connection.c +++ b/src/connection.c @@ -18,6 +18,7 @@ */ #include "connection.h" +#include "stats.h" #include #include @@ -94,9 +95,11 @@ static void set_controller_led(unsigned short controllerNumber, unsigned char r, } static void connection_status_update(int status) { + stats_connection_status(status); switch (status) { case CONN_STATUS_OKAY: - printf("Connection is okay\n"); + if (connection_debug) + printf("Connection is okay\n"); break; case CONN_STATUS_POOR: printf("Connection is poor\n"); diff --git a/src/main.c b/src/main.c old mode 100644 new mode 100755 index 8e9570d5f..2a9aa0384 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,7 @@ #include "platform.h" #include "config.h" #include "sdl.h" +#include "stats.h" #include "audio/audio.h" #include "video/video.h" @@ -142,6 +143,8 @@ static void stream(PSERVER_DATA server, PCONFIGURATION config, enum platform sys if (IS_EMBEDDED(system)) loop_init(); + stats_init(config->stats); + platform_start(system); LiStartConnection(&server->serverInfo, &config->stream, &connection_callbacks, platform_get_video(system), platform_get_audio(system, config->audio_device), NULL, drFlags, config->audio_device, 0); @@ -203,6 +206,7 @@ static void help() { printf("\t-packetsize \tSpecify the maximum packetsize in bytes\n"); printf("\t-codec \t\tSelect used codec: auto/h264/h265/av1 (default auto)\n"); printf("\t-hdr\t\tEnable HDR streaming (experimental, requires host and device support)\n"); + printf("\t-stats\t\tDisplay real-time statistics: bitrate, frame rate, decode time\n"); printf("\t-remote \t\t\tEnable optimizations for WAN streaming (default auto)\n"); printf("\t-app \t\tName of app to stream\n"); printf("\t-nosops\t\t\tDon't allow GFE to modify game settings\n"); diff --git a/src/sdl.c b/src/sdl.c old mode 100644 new mode 100755 index 25567de3c..f3091e0e3 --- a/src/sdl.c +++ b/src/sdl.c @@ -20,8 +20,11 @@ #ifdef HAVE_SDL #include "sdl.h" +#include "stats.h" #include "input/sdl.h" +#include + #include static bool done; @@ -35,7 +38,14 @@ SDL_mutex *mutex; int sdlCurrentFrame, sdlNextFrame; +static Uint32 sdl_stats_timer(Uint32 interval, void *param) { + (void)param; + stats_print(); + return interval; +} + void sdl_init(int width, int height, bool fullscreen) { + SDL_AddTimer(1000, sdl_stats_timer, NULL); sdlCurrentFrame = sdlNextFrame = 0; if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) { @@ -106,6 +116,7 @@ void sdl_loop() { SDL_RenderClear(renderer); SDL_RenderCopy(renderer, bmp, NULL, NULL); SDL_RenderPresent(renderer); + stats_frame_displayed(); } else fprintf(stderr, "Couldn't lock mutex\n"); } diff --git a/src/stats.c b/src/stats.c new file mode 100755 index 000000000..b885b5f6f --- /dev/null +++ b/src/stats.c @@ -0,0 +1,206 @@ +/* + * This file is part of Moonlight Embedded. + * + * Copyright (C) 2026 Moonlight Embedded contributors + * + * Moonlight is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Moonlight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Moonlight; if not, see . + */ + +#include "stats.h" +#include "loop.h" + +#include +#include +#include +#include +#include +#include + +/* ---- thread safety ---------------------------------------------------- */ +static pthread_mutex_t stats_mutex = PTHREAD_MUTEX_INITIALIZER; + +/* ---- master switch ---------------------------------------------------- */ +static bool stats_enabled = false; + +/* ---- bitrate counters ------------------------------------------------- */ +static int64_t total_bytes = 0; +static int64_t prev_total_bytes = 0; + +/* ---- frame counters --------------------------------------------------- */ +static int decoded_frames = 0; +static int displayed_frames = 0; +static int prev_decoded_frames = 0; +static int prev_displayed_frames = 0; + +/* ---- decode timing (per-interval accumulation) ----------------------- */ +static int64_t decode_time_sum_us = 0; +static int decode_time_count = 0; + +/* ---- connection quality ----------------------------------------------- */ +static int poor_connection_count = 0; + +/* ---- last-report timestamp -------------------------------------------- */ +static struct timespec last_report_ts; + +/* ----------------------------------------------------------------------- + * Core print logic – shared between timerfd handler and external callers + * ----------------------------------------------------------------------- */ +static void stats_do_print(double elapsed) { + /* ---- atomically snapshot + reset per-interval counters ------------ */ + pthread_mutex_lock(&stats_mutex); + + int bytes_this = (int)(total_bytes - prev_total_bytes); + prev_total_bytes = total_bytes; + + int decoded_this = decoded_frames - prev_decoded_frames; + prev_decoded_frames = decoded_frames; + + int displayed_this = displayed_frames - prev_displayed_frames; + prev_displayed_frames = displayed_frames; + + double avg_decode_us = (decode_time_count > 0) + ? (double)decode_time_sum_us / decode_time_count + : 0.0; + decode_time_sum_us = 0; + decode_time_count = 0; + + int poor_count = poor_connection_count; + poor_connection_count = 0; + + pthread_mutex_unlock(&stats_mutex); + + /* ---- format output ------------------------------------------------ */ + static char line[256]; + int pos = 0; + + double bitrate_mbps = (bytes_this * 8.0) / (elapsed * 1e6); + double decode_fps = decoded_this / elapsed; + double display_fps = displayed_this / elapsed; + double decode_ms = avg_decode_us / 1000.0; + + pos += snprintf(line + pos, sizeof(line) - pos, + "\r[STATS] 码率: %7.1f Mbps | 解码: %5.1f fps | 显示: %5.1f fps | 解码耗时: %5.2f ms", + bitrate_mbps, decode_fps, display_fps, decode_ms); + + if (decoded_this > 0) { + pos += snprintf(line + pos, sizeof(line) - pos, + " | 帧大小: %5.0f KB", + (double)bytes_this / decoded_this / 1024.0); + } + + if (poor_count > 0) { + pos += snprintf(line + pos, sizeof(line) - pos, + " | ⚠ 弱网(%dx)", poor_count); + } + + pos += snprintf(line + pos, sizeof(line) - pos, " "); + line[pos] = '\r'; + line[pos + 1] = '\0'; + + fputs(line, stdout); + fflush(stdout); +} + +void stats_print(void) { + if (!stats_enabled) return; + + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + + double elapsed = (now.tv_sec - last_report_ts.tv_sec) + + (now.tv_nsec - last_report_ts.tv_nsec) / 1e9; + if (elapsed < 1e-3) + return; + + stats_do_print(elapsed); + last_report_ts = now; +} + +/* ----------------------------------------------------------------------- + * Timer callback – runs once per second from the embedded event loop + * ----------------------------------------------------------------------- */ +static int stats_timer_handler(int fd) { + uint64_t expirations; + ssize_t r = read(fd, &expirations, sizeof(expirations)); + if (r != sizeof(expirations)) + return LOOP_OK; + + stats_print(); + return LOOP_OK; +} + +/* ----------------------------------------------------------------------- + * Public API + * ----------------------------------------------------------------------- */ +void stats_init(bool enabled) { + stats_enabled = enabled; + if (!stats_enabled) + return; + + /* Create a 1-second periodic timer and register it in the event loop */ + int tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); + if (tfd < 0) { + perror("stats: timerfd_create"); + return; + } + + struct itimerspec ts = { + .it_interval = { .tv_sec = 1, .tv_nsec = 0 }, + .it_value = { .tv_sec = 1, .tv_nsec = 0 }, + }; + timerfd_settime(tfd, 0, &ts, NULL); + + loop_add_fd(tfd, stats_timer_handler, POLLIN); + + clock_gettime(CLOCK_MONOTONIC, &last_report_ts); +} + +void stats_submit_decode_unit(int length) { + if (!stats_enabled) return; + pthread_mutex_lock(&stats_mutex); + total_bytes += length; + pthread_mutex_unlock(&stats_mutex); +} + +void stats_frame_decoded(void) { + if (!stats_enabled) return; + pthread_mutex_lock(&stats_mutex); + decoded_frames++; + pthread_mutex_unlock(&stats_mutex); +} + +void stats_decode_finished(int64_t elapsed_us) { + if (!stats_enabled) return; + pthread_mutex_lock(&stats_mutex); + decode_time_sum_us += elapsed_us; + decode_time_count++; + pthread_mutex_unlock(&stats_mutex); +} + +void stats_frame_displayed(void) { + if (!stats_enabled) return; + pthread_mutex_lock(&stats_mutex); + displayed_frames++; + pthread_mutex_unlock(&stats_mutex); +} + +void stats_connection_status(int status) { + if (!stats_enabled) return; + /* Limelight defines: 1 = CONN_STATUS_OKAY, 2 = CONN_STATUS_POOR */ + if (status == 2) { + pthread_mutex_lock(&stats_mutex); + poor_connection_count++; + pthread_mutex_unlock(&stats_mutex); + } +} diff --git a/src/stats.h b/src/stats.h new file mode 100755 index 000000000..0650e70c9 --- /dev/null +++ b/src/stats.h @@ -0,0 +1,45 @@ +/* + * This file is part of Moonlight Embedded. + * + * Copyright (C) 2026 Moonlight Embedded contributors + * + * Moonlight is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Moonlight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Moonlight; if not, see . + */ + +#pragma once + +#include +#include + +/** Initialize stats collection and register a 1-second periodic reporter. + * Pass enabled=true to turn on statistics, false to make everything a no-op. */ +void stats_init(bool enabled); + +/** Called when a decode unit arrives — measures receive bitrate */ +void stats_submit_decode_unit(int length); + +/** Called when a frame has been decoded */ +void stats_frame_decoded(void); + +/** Called with decode duration in microseconds */ +void stats_decode_finished(int64_t elapsed_us); + +/** Called when a frame is displayed on screen */ +void stats_frame_displayed(void); + +/** Called on connection status update (pass the raw status code) */ +void stats_connection_status(int status); + +/** Force-print stats to stdout immediately */ +void stats_print(void); diff --git a/src/video/aml.c b/src/video/aml.c old mode 100644 new mode 100755 index f39eb1418..3b7a09375 --- a/src/video/aml.c +++ b/src/video/aml.c @@ -36,6 +36,7 @@ #include #include "../util.h" +#include "../stats.h" #include "video.h" #define SYNC_OUTSIDE 0x02 @@ -64,6 +65,8 @@ void* aml_display_thread(void* unused) { break; } + stats_frame_displayed(); + if (ioctl(videoFd, VIDIOC_QBUF, &vbuf) < 0) { fprintf(stderr, "VIDIOC_QBUF failed: %d\n", errno); break; @@ -175,6 +178,9 @@ void aml_cleanup() { int aml_submit_decode_unit(PDECODE_UNIT decodeUnit) { + stats_submit_decode_unit(decodeUnit->fullLength); + stats_frame_decoded(); + ensure_buf_size(&pkt_buf, &pkt_buf_size, decodeUnit->fullLength); int written = 0, length = 0, errCounter = 0, api; diff --git a/src/video/imx.c b/src/video/imx.c old mode 100644 new mode 100755 index a7b926410..cc063b21f --- a/src/video/imx.c +++ b/src/video/imx.c @@ -20,6 +20,7 @@ #include "imx_vpu.h" #include "../loop.h" +#include "../stats.h" #include #include @@ -89,6 +90,7 @@ static int frame_handle(int pipefd) { fprintf(stderr, "Can't get output buffer\n"); exit(EXIT_FAILURE); } + stats_frame_displayed(); if (!displaying) { int type = V4L2_BUF_TYPE_VIDEO_OUTPUT; @@ -251,10 +253,13 @@ static int decoder_renderer_setup(int videoFormat, int width, int height, int re static int decoder_renderer_submit_decode_unit(PDECODE_UNIT decodeUnit) { int frame; + stats_submit_decode_unit(decodeUnit->fullLength); + while (read(clearpipefd[0], &frame, sizeof(int)) > 0) vpu_clear(frame); if (vpu_decode(decodeUnit)) { + stats_frame_decoded(); frame = vpu_get_frame(); write(pipefd[1], &frame, sizeof(int)); } diff --git a/src/video/mmal.c b/src/video/mmal.c old mode 100644 new mode 100755 index 49d0c605b..e007cc7fb --- a/src/video/mmal.c +++ b/src/video/mmal.c @@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Based upon example code from the Raspberry Pi #include "video.h" +#include "../stats.h" #include @@ -230,6 +231,8 @@ static int decoder_renderer_submit_decode_unit(PDECODE_UNIT decodeUnit) { MMAL_STATUS_T status; MMAL_BUFFER_HEADER_T *buf = NULL; PLENTRY entry = decodeUnit->bufferList; + + stats_submit_decode_unit(decodeUnit->fullLength); bool first_entry = false; while (entry != NULL) { diff --git a/src/video/pi.c b/src/video/pi.c old mode 100644 new mode 100755 index 9e9aa3598..cba9f3b72 --- a/src/video/pi.c +++ b/src/video/pi.c @@ -29,6 +29,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // Based upon video decode example from the Raspberry Pi firmware #include "video.h" +#include "../stats.h" #include @@ -221,6 +222,8 @@ static void decoder_renderer_cleanup() { static int decoder_renderer_submit_decode_unit(PDECODE_UNIT decodeUnit) { OMX_BUFFERHEADERTYPE *buf = NULL; + stats_submit_decode_unit(decodeUnit->fullLength); + PLENTRY entry = decodeUnit->bufferList; while (entry != NULL) { if (buf == NULL) { @@ -256,6 +259,7 @@ static int decoder_renderer_submit_decode_unit(PDECODE_UNIT decodeUnit) { ilclient_change_component_state(video_render, OMX_StateExecuting); } + stats_frame_decoded(); if(OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_decode), buf) != OMX_ErrorNone){ fprintf(stderr, "Can't empty video buffer\n"); exit(EXIT_FAILURE); diff --git a/src/video/rk.c b/src/video/rk.c old mode 100644 new mode 100755 index f65b597ed..660c0dc8b --- a/src/video/rk.c +++ b/src/video/rk.c @@ -19,12 +19,14 @@ */ #include "video.h" +#include "../stats.h" #include "../util.h" #include #include #include #include +#include #include #include #include @@ -177,6 +179,8 @@ void *display_thread(void *param) { ret = drmModeAtomicCommit(fd, drm_request, DRM_MODE_ATOMIC_ALLOW_MODESET, NULL); if (ret) { perror("drmModeAtomicCommit"); + } else { + stats_frame_displayed(); } } else { ret = drmModeSetPlane(fd, plane_id, crtc_id, _fb_id, 0, @@ -184,6 +188,8 @@ void *display_thread(void *param) { 0, 0, frm_width << 16, frm_height << 16); if (ret) { perror("drmModeSetPlane"); + } else { + stats_frame_displayed(); } } } @@ -200,7 +206,21 @@ void *frame_thread(void *param) { while (!frm_eos) { + struct timespec ts_decode_before; + clock_gettime(CLOCK_MONOTONIC, &ts_decode_before); + ret = mpi_api->decode_get_frame(mpi_ctx, &frame); + + if (ret == MPP_OK || ret == MPP_ERR_TIMEOUT) { + struct timespec ts_decode_after; + clock_gettime(CLOCK_MONOTONIC, &ts_decode_after); + if (frame) { + int64_t us = (ts_decode_after.tv_sec - ts_decode_before.tv_sec) * 1000000LL + + (ts_decode_after.tv_nsec - ts_decode_before.tv_nsec) / 1000; + stats_decode_finished(us); + } + } + if (ret != MPP_OK && ret != MPP_ERR_TIMEOUT) { if (count < 3) { fprintf(stderr, "Waiting for Frame (return code = %d, retry count = %d)\n", ret, count); @@ -311,6 +331,7 @@ void *frame_thread(void *param) { set_property(conn_id, DRM_MODE_OBJECT_CONNECTOR, conn_props, "Colorspace", last_hdr_state ? DRM_MODE_COLORIMETRY_BT2020_RGB : DRM_MODE_COLORIMETRY_DEFAULT); } else { // regular frame received + stats_frame_decoded(); MppBuffer buffer = mpp_frame_get_buffer(frame); if (buffer) { @@ -660,6 +681,8 @@ int rk_submit_decode_unit(PDECODE_UNIT decodeUnit) { PLENTRY entry = decodeUnit->bufferList; int length = 0; + stats_submit_decode_unit(decodeUnit->fullLength); + if (ensure_buf_size(&pkt_buf, &pkt_buf_size, decodeUnit->fullLength)) { // Buffer was reallocated, so update the mpp_packet accordingly mpp_packet_set_data(mpi_packet, pkt_buf); diff --git a/src/video/sdl.c b/src/video/sdl.c old mode 100644 new mode 100755 index 1a7f6cf08..ce588158e --- a/src/video/sdl.c +++ b/src/video/sdl.c @@ -21,12 +21,14 @@ #include "ffmpeg.h" #include "../sdl.h" +#include "../stats.h" #include "../util.h" #include #include #include +#include #include #define SLICES_PER_FRAME 4 @@ -53,6 +55,8 @@ static int sdl_submit_decode_unit(PDECODE_UNIT decodeUnit) { PLENTRY entry = decodeUnit->bufferList; int length = 0; + stats_submit_decode_unit(decodeUnit->fullLength); + ensure_buf_size(&ffmpeg_buffer, &ffmpeg_buffer_size, decodeUnit->fullLength + AV_INPUT_BUFFER_PADDING_SIZE); while (entry != NULL) { @@ -60,11 +64,20 @@ static int sdl_submit_decode_unit(PDECODE_UNIT decodeUnit) { length += entry->length; entry = entry->next; } + + struct timespec ts_before, ts_after; + clock_gettime(CLOCK_MONOTONIC, &ts_before); + ffmpeg_decode(ffmpeg_buffer, length); SDL_LockMutex(mutex); AVFrame* frame = ffmpeg_get_frame(false); if (frame != NULL) { + clock_gettime(CLOCK_MONOTONIC, &ts_after); + int64_t us = (ts_after.tv_sec - ts_before.tv_sec) * 1000000LL + + (ts_after.tv_nsec - ts_before.tv_nsec) / 1000; + stats_decode_finished(us); + stats_frame_decoded(); sdlNextFrame++; SDL_Event event; diff --git a/src/video/x11.c b/src/video/x11.c old mode 100644 new mode 100755 index 80c586c5b..bdbe0ed22 --- a/src/video/x11.c +++ b/src/video/x11.c @@ -26,6 +26,7 @@ #include "../input/x11.h" #include "../loop.h" +#include "../stats.h" #include "../util.h" #include @@ -34,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +65,7 @@ static int frame_handle(int pipefd) { else if (ffmpeg_decoder == VAAPI) vaapi_queue(frame, window, display_width, display_height); #endif + stats_frame_displayed(); } return LOOP_OK; @@ -167,6 +170,8 @@ int x11_submit_decode_unit(PDECODE_UNIT decodeUnit) { PLENTRY entry = decodeUnit->bufferList; int length = 0; + stats_submit_decode_unit(decodeUnit->fullLength); + ensure_buf_size(&ffmpeg_buffer, &ffmpeg_buffer_size, decodeUnit->fullLength + AV_INPUT_BUFFER_PADDING_SIZE); while (entry != NULL) { @@ -175,11 +180,20 @@ int x11_submit_decode_unit(PDECODE_UNIT decodeUnit) { entry = entry->next; } + struct timespec ts_before, ts_after; + clock_gettime(CLOCK_MONOTONIC, &ts_before); + ffmpeg_decode(ffmpeg_buffer, length); AVFrame* frame = ffmpeg_get_frame(true); - if (frame != NULL) + if (frame != NULL) { + clock_gettime(CLOCK_MONOTONIC, &ts_after); + int64_t us = (ts_after.tv_sec - ts_before.tv_sec) * 1000000LL + + (ts_after.tv_nsec - ts_before.tv_nsec) / 1000; + stats_decode_finished(us); + stats_frame_decoded(); write(pipefd[1], &frame, sizeof(void*)); + } return DR_OK; } From ab990347b5042c7da6c5b5018cafcd068558c439 Mon Sep 17 00:00:00 2001 From: qianxuewen <1127408945@qq.com> Date: Sun, 26 Jul 2026 17:22:41 +0800 Subject: [PATCH 2/4] docs: add QXL/virtio troubleshooting section --- docs/README.md | 696 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 696 insertions(+) create mode 100644 docs/README.md diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 000000000..f243a8019 --- /dev/null +++ b/docs/README.md @@ -0,0 +1,696 @@ +# Moonlight Embedded 综合文档 + +> 更新时间:2026-07-26 +> 目标平台:Rockchip RK3568 + +--- + +## 目录 + +1. [moonlight-embedded vs moonlight-qt 对比](#1-moonlight-embedded-vs-moonlight-qt-对比) +2. [编译指南](#2-编译指南) +3. [Sunshine 分辨率自动切换](#3-sunshine-分辨率自动切换) +4. [YUV444 支持可行性分析](#4-yuv444-支持可行性分析) +5. [实时统计模块说明](#5-实时统计模块说明) +6. [文件结构](#6-文件结构) + +--- + +## 1. moonlight-embedded vs moonlight-qt 对比 + +### 1.1 项目定位 + +| 项目 | 定位 | +|------|------| +| **moonlight-embedded** | 面向嵌入式 Linux 系统的轻量级命令行客户端,专为 SoC 类设备(RPi、ODROID、Rockchip)优化 | +| **moonlight-qt** | 面向桌面系统的全功能图形化客户端,跨平台支持(Windows、macOS、Linux) | + +### 1.2 核心架构差异 + +| 维度 | moonlight-embedded | moonlight-qt | +|------|-------------------|-------------| +| **编程语言** | C (C99) | C++ (Qt6/Qt5) | +| **用户界面** | 命令行 (`moonlight stream -1080 -app Steam`) | 完整 Qt 图形界面 | +| **视频解码** | 平台原生 API(MPP、MMAL、V4L2 VPU) | FFmpeg(通过 hwaccel 使用 V4L2/VAAPI/Vulkan) | +| **显示输出** | DRM/KMS 直接扫描输出,无窗口系统 | Vulkan/OpenGL ES/DRM PRIME | +| **音频输出** | ALSA / PulseAudio / OSS / OMX | Qt Multimedia / ALSA / PulseAudio | +| **输入处理** | libevdev 直接读取 `/dev/input/event*` | Qt 事件 + SDL2 游戏控制器 API | +| **窗口系统** | 不需要 X11/Wayland | 需要 X11/Wayland(或 Qt EGLFS) | +| **CEC 遥控** | 支持 (libcec) | 不支持 | + +### 1.3 软件栈对比 + +``` +moonlight-embedded: + + [Moonlight CLI] → [libgamestream] → [Platform Backend] + ↓ ↓ + [moonlight-common-c] [rk.c / pi.c / aml.c / ...] + ↓ ↓ + [网络协议] [MPP / MMAL / DRM-KMS] +``` + +``` +moonlight-qt: + + [Qt GUI] → [FFmpeg] → [V4L2 / VAAPI / Vulkan / D3D] → [显示输出] + ↓ + [moonlight-common-c] → [网络协议] +``` + +### 1.4 功能详细对比 + +#### 视频解码 + +| 功能 | moonlight-embedded | moonlight-qt | +|------|-------------------|-------------| +| H.264 硬解 | ✅ 平台原生 | ✅ FFmpeg 后端 | +| H.265/HEVC 硬解 | ✅ 平台原生 | ✅ FFmpeg 后端 | +| AV1 硬解 | ✅ 平台原生 | ✅ FFmpeg 后端 | +| 10-bit HDR 解码 | ✅ (rk.c 支持 NA12/NV15) | ✅ | +| 原子 KMS 提交 | ✅ (HDR 时启用) | ✅ | +| 显示旋转 | ✅ (0°/90°/180°/270°) | ✅ | +| YUV444 | ⚠️ 仅 x11/sdl 后端可行 | ✅ 多渲染器支持 | + +#### 输入 + +| 功能 | moonlight-embedded | moonlight-qt | +|------|-------------------|-------------| +| evdev 手柄 | ✅ | ❌ | +| SDL2 手柄 | ✅ | ✅ | +| 键盘输入 | ✅ (evdev) | ✅ (Qt) | +| CEC 遥控器 | ✅ (libcec) | ❌ | +| 多输入设备 | ✅ (`-input` 参数指定) | 系统自动发现 | +| 触摸/多点触控 | ❌ | ✅ | + +#### 音频 + +| 功能 | moonlight-embedded | moonlight-qt | +|------|-------------------|-------------| +| ALSA | ✅ | ✅ | +| PulseAudio | ✅ | ✅ | +| OSS | ✅ (FreeBSD) | ❌ | +| SDL 音频 | ✅ | ❌ | +| OMX (RPi) | ✅ | ❌ | +| 环绕声 5.1/7.1 | ✅ | ✅ | +| 音频设备选择 | ✅ (`-audio` 参数) | ✅ | + +### 1.5 平台支持 + +| 平台 | moonlight-embedded | moonlight-qt | +|------|-------------------|-------------| +| Rockchip (RK3566/3568/3588) | ✅ **原生** (rk.c + MPP) | ⚠️ 通过 V4L2/FFmpeg 间接支持 | +| Raspberry Pi | ✅ **原生** (pi.c/MMAL/OMX) | ⚠️ 通过 V4L2/FFmpeg | +| Amlogic (S905/S912) | ✅ **原生** (aml.c) | ❌ 无专用后端 | +| i.MX6 | ✅ **原生** (imx.c) | ❌ 无专用后端 | +| x86 Linux | ✅ (X11/SDL 后端) | ✅ **原生** (VAAPI/VDPAU/Vulkan) | +| Windows | ❌ | ✅ **原生** (D3D11VA) | +| macOS | ❌ | ✅ **原生** (VideoToolbox) | +| Android | ❌ | ❌ (有独立 moonlight-android) | +| iOS | ❌ | ❌ (有独立 moonlight-ios) | + +### 1.6 RK3568 适配分析 + +#### moonlight-embedded 在 RK3568 上有原生支持 + +``` +moonlight-embedded/ +├── cmake/FindRockchip.cmake ← 查找 librockchip_mpp.so + libdrm +├── src/video/rk.c ← Rockchip VPU 完整后端 (MPP + DRM/KMS) +├── src/platform.c ← 自动检测并加载 rk 平台 +│ ├── platform_check("rk") ← dlopen("libmoonlight-rk.so") +│ └── dlsym("mpp_init") ← 验证 MPP 可用性 +└── src/main.c ← -platform rk 命令行参数 +``` + +视频管线: + +``` +网络流 → LiStartConnection → rk_setup() → MPP解码 → DRM/KMS overlay显示 + ↓ + MPP: mpp_create → mpp_init → decode_put_packet → decode_get_frame + DRM: drmModeSetPlane / drmModeAtomicCommit +``` + +- 直接使用 **Rockchip MPP API** (Media Process Platform),零拷贝路径 +- 支持 Legacy 和 Atomic KMS 两种提交模式 +- 支持 8-bit (NV12) 和 10-bit (NA12/NV15) 像素格式 + +#### RK3568 性能参考 + +| 场景 | moonlight-embedded | moonlight-qt | +|------|-------------------|-------------| +| 1080p60 HEVC | ✅ 流畅,解码延迟 2-5ms | ✅ 流畅 | +| 4K30 HEVC | ✅ 可用 | ⚠️ 部分场景卡顿 | +| 4K60 HEVC | ⚠️ 受 RK3568 解码能力上限影响 | ❌ 通常不支持 | +| HDR 10-bit | ✅ 支持 | ⚠️ 视 V4L2 驱动情况 | +| 环绕声 | ✅ 原生支持 | ✅ 支持 | + +### 1.7 总结推荐 + +| 对比项 | moonlight-embedded | moonlight-qt | +|--------|-------------------|-------------| +| 在 RK3568 上适配深度 | ⭐⭐⭐⭐⭐ **原生全栈** | ⭐⭐⭐ 通过 FFmpeg/V4L2 间接支持 | +| 延迟 | ⭐⭐⭐⭐⭐ **最低**(零拷贝 DRM/KMS) | ⭐⭐⭐ 多一层 FFmpeg 封装 | +| 资源占用 | ⭐⭐⭐⭐⭐ 极低 | ⭐⭐ 较高 | +| 功能完整性 | ⭐⭐⭐ 命令行、功能稳定 | ⭐⭐⭐⭐⭐ 桌面级功能完整 | +| 开发活跃度 | ⭐⭐⭐ 维护模式 | ⭐⭐⭐⭐⭐ 持续活跃 | +| 跨平台支持 | ⭐⭐ 仅嵌入式 Linux | ⭐⭐⭐⭐⭐ Windows/macOS/Linux | +| 易用性 | ⭐⭐ 需要命令行操作 | ⭐⭐⭐⭐⭐ GUI 开箱即用 | +| 可集成性 | ⭐⭐⭐⭐⭐ 脚本友好、轻量 | ⭐⭐ 依赖重 | + +**在 RK3568 上,如果目标是嵌入式/专用流媒体设备,首选 moonlight-embedded**。如果需要在 RK3568 上运行完整桌面系统并用 GUI 操作,再考虑 moonlight-qt。 + +--- + +## 2. 编译指南 + +### 2.1 环境要求 + +**硬件**: +- x86 开发/测试:任意 x86_64 Linux 主机 +- RK3568 目标部署:RK3566/RK3568/RK3588 设备 + +**软件**: +- Ubuntu 24.04 LTS(或其他 Debian 系 Linux) +- CMake ≥ 3.6 +- GCC ≥ 11 + +### 2.2 依赖安装 + +```bash +sudo apt update +sudo apt install -y cmake gcc pkg-config \ + libopus-dev libevdev-dev libudev-dev \ + libsdl2-dev libavcodec-dev libavutil-dev \ + libpulse-dev libasound2-dev libssl-dev \ + libx11-dev libegl1-mesa-dev libgles2-mesa-dev \ + libva-dev libvdpau-dev libdrm-dev \ + libcurl4-openssl-dev libavahi-client-dev +``` + +#### RK3568 额外依赖 + +```bash +sudo apt install -y librga-dev librga2 rockchip-mpp rockchip-mpp-dev +``` + +依赖路径验证: + +```bash +ls /usr/lib/aarch64-linux-gnu/librockchip_mpp.so* +ls /usr/include/rockchip/rk_mpi.h +``` + +### 2.3 编译步骤 + +#### x86_64 平台(功能验证) + +```bash +git clone https://github.com/moonlight-stream/moonlight-embedded.git +cd moonlight-embedded +git submodule update --init --recursive +mkdir build && cd build +cmake .. -DCMAKE_BUILD_TYPE=Release +make -j$(nproc) +# 可选安装 +sudo make install +``` + +编译产物:`build/moonlight` 可执行文件。 + +#### RK3568 平台(原生编译) + +```bash +ssh root@192.168.x.x +git clone https://github.com/你的仓库/moonlight-embedded.git +cd moonlight-embedded +git submodule update --init --recursive +mkdir build && cd build +cmake .. -DCMAKE_BUILD_TYPE=Release +make -j$(nproc) +sudo make install +``` + +CMake 会自动检测 Rockchip MPP 并启用 `HAVE_ROCKCHIP` 和 `libmoonlight-rk.so`。 + +#### RK3568 交叉编译(从 x86 宿主机) + +```bash +# 宿主机安装交叉编译工具链 +sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu + +# 从 RK3568 设备同步 sysroot +ssh root@rk3568 "tar czf /tmp/sysroot.tar.gz /usr /lib" && \ + scp root@rk3568:/tmp/sysroot.tar.gz . && \ + tar xzf sysroot.tar.gz + +# 交叉编译 +cd moonlight-embedded +mkdir build-cross && cd build-cross +cmake .. \ + -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc \ + -DCMAKE_SYSTEM_NAME=Linux \ + -DCMAKE_FIND_ROOT_PATH=/path/to/sysroot \ + -DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \ + -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \ + -DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \ + -DCMAKE_BUILD_TYPE=Release +make -j$(nproc) +``` + +### 2.4 CMake 选项 + +| 选项 | 默认值 | 说明 | +|------|--------|------| +| `ENABLE_SDL` | ON | SDL2 后端支持(输入、音频、视频) | +| `ENABLE_FFMPEG` | ON | FFmpeg 软解支持(X11/SDL 后端需要) | +| `ENABLE_X11` | ON | X11 窗口系统支持 | +| `ENABLE_CEC` | ON | HDMI-CEC 遥控器支持(需要 libcec) | +| `ENABLE_PULSE` | ON | PulseAudio 音频支持 | +| `DCMAKE_BUILD_TYPE` | Release | 编译类型:Release/Debug/RelWithDebInfo | + +### 2.5 常见编译问题 + +#### `Could NOT find CURL` + +```bash +sudo apt install -y libcurl4-openssl-dev +``` + +#### `Package 'avahi-client' not found` + +```bash +sudo apt install -y libavahi-client-dev +``` + +#### `No SOURCES given to target: moonlight-common` + +子模块未初始化: + +```bash +git submodule update --init --recursive +``` + +#### `error: 'CLOCK_MONOTONIC' undeclared` + +`-std=c99` 模式下 POSIX 时钟不可见。已在 CMakeLists.txt 中启用 `CMAKE_C_EXTENSIONS ON`(`-std=gnu99`),如果仍有问题: + +```bash +rm -rf build && mkdir build && cd build +cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_FLAGS="-D_GNU_SOURCE" +``` + +### 2.6 运行测试 + +```bash +# 查看帮助 +./moonlight help + +# 搜索局域网主机 +./moonlight list + +# 配对 +./moonlight pair 192.168.x.x + +# 流媒体(带实时统计) +./moonlight stream -1080 -fps 60 -stats -platform sdl 192.168.x.x + +# RK3568 上用硬件解码(记得加 -stats) +./moonlight stream -1080 -fps 60 -stats -platform rk 192.168.x.x + +# 使用配置文件 +./moonlight /etc/moonlight/moonlight.conf +``` + +--- + +## 3. Sunshine 分辨率自动切换 + +### 3.1 需求 + +Windows 虚拟机做 Sunshine 宿主时,Moonlight 请求某个分辨率(如 1920x1080),希望**虚拟机的显示分辨率自动跟随切换**。这样流出来的画面就是 Moonlight 请求的准确分辨率,不需要虚拟机里手动改。 + +### 3.2 Sunshine 原生方案(推荐,Sunshine ≥ 0.21) + +Sunshine Web UI → **Settings → Audio/Video → Advanced Display Options**: + +``` +Resolution: "Use resolution provided by the client" (下拉选择) +``` + +Sunshine 利用环境变量在流启动/停止时自动切换分辨率: + +| 环境变量 | 说明 | +|---------|------| +| `SUNSHINE_CLIENT_WIDTH` | 客户端请求的宽度 | +| `SUNSHINE_CLIENT_HEIGHT` | 客户端请求的高度 | +| `SUNSHINE_CLIENT_FPS` | 客户端请求的帧率 | + +启用此选项后,Sunshine 内部会调用 Windows API (`ChangeDisplaySettingsEx`) 自动切换分辨率,**无需额外脚本**。 + +> **Windows VM 注意事项**:虚拟机显卡驱动必须支持动态分辨率切换。VMware 的 `vmwgfx` 或 Hyper-V 的 `Basic Display Adapter` 可能限制较多,建议安装 VMware Tools / Hyper-V Integration Services 以获得完整支持。 + +### 3.3 QRes 手动脚本方案(通用,适合任意 Windows 版本) + +如果 Sunshine 原生方案无效,可用 QRes 配合 Sunshine 的 Do/Undo 命令实现。 + +**步骤 1**:下载 [QRes](https://sourceforge.net/projects/qres/) 放到宿主机(如 `C:\tools\QRes.exe`) + +**步骤 2**:Sunshine Web UI → **Advanced → Command Preparations**: + +``` +Do Command(流启动时执行): + cmd /C C:\tools\QRes.exe /x %SUNSHINE_CLIENT_WIDTH% /y %SUNSHINE_CLIENT_HEIGHT% /r %SUNSHINE_CLIENT_FPS% + +Undo Command(流结束时恢复): + cmd /C C:\tools\QRes.exe /x 1920 /y 1080 /r 60 +``` + +> 如果分辨率的显示模式在系统中不存在,QRes 会失败。可以用 CRU (Custom Resolution Utility) 提前添加需要的分辨率。 + +### 3.4 ResolutionAutomation 方案(第三方工具) + +[Nonary/ResolutionAutomation](https://github.com/Nonary/ResolutionAutomation) 是一个开源 PowerShell 脚本,能自动: + +- 读取 Sunshine 日志或环境变量获取客户端请求的分辨率 +- 调用 Windows API 切换分辨率 +- 流结束后自动恢复原始分辨率 +- 支持超采样(客户端 720p,宿主机保持 1080p 渲染后缩放到 720p 传输) + +用法: + +```powershell +# 以管理员身份运行 +.\ResolutionAutomation.ps1 -Action MonitorSunshine +``` + +### 3.5 Apollo 方案(Sunshine 增强分支) + +[Apollo](https://github.com/ClassicOldSong/Apollo) 是 Sunshine 的社区分支,内置: + +- **虚拟显示器管理** — 自动创建与 Moonlight 分辨率匹配的虚拟显示器 +- 流结束后自动删除虚拟显示器 +- 不需要物理显示器一直插着 +- 适合无头(headless)Windows VM 场景 + +### 3.6 方案对比 + +| 方案 | 复杂度 | 适用场景 | +|------|--------|---------| +| Sunshine 内建 | ⭐ 零配置 | Sunshine ≥ 0.21,VM 显卡驱动支持 | +| QRes 脚本 | ⭐⭐ 两步配置 | 任何 Windows 版本,手动可控 | +| ResolutionAutomation | ⭐⭐ 开箱即用 | 需要超采样或自动降分辨率 | +| Apollo | ⭐⭐ 内置 | 无头 VM、虚拟显示器管理 | + +### 3.7 Windows VM 特殊注意事项 + +1. **虚拟显卡驱动**:VMware 建议安装 VMware Tools;Hyper-V 建议安装 Integration Services;KVM/QEMU 建议使用 `virtio` 显卡驱动 + SPICE/FRP +2. **分辨率列表**:某些虚拟显卡只有有限的标准分辨率。用 QRes 前先用 `QRes.exe /l` 查看支持的分辨率列表 +3. **无头 VM(没有物理显示器)**:部分虚拟显卡在没有物理显示器时无法创建高分辨率模式,建议用 **Apollo** 或 `IddSampleDriver`(虚拟显示器驱动)来绕开 +4. **HDR**:QRes 不支持 HDR 切换。如果需要 HDR 自动开关,用 Sunshine 内建方案或搭配 `AutoHDRSwitch` + +--- + +### 3.8 QXL / virtio 虚拟显卡排查 + +KVM/QEMU 中用 QXL 虚拟显卡时,Sunshine 日志中会出现: + +``` +Display refresh rate [1Hz] +Failed to set display mode(-s) completely! +Display device configuration failed with result: DisplayModePrepFailed +``` + +**原因**:QXL 是 QEMU 的基础 VGA 模拟,不支持 60Hz 刷新率切换,Sunshine 尝试调分辨率/刷新率会失败。 + +**解决办法**: + +1. **临时绕过(不切换显示模式)**:Sunshine Web UI → **Settings → Advanced → Display Device**,将 Display Preparation 设为 `"Do not prepare"`(或取消勾选 Ensure Active),保存后重启 Sunshine +2. **换 virtio GPU**(推荐): + + ```bash + # 在宿主机上修改 VM 配置 + sudo virsh edit + # 找到