diff --git a/src/app/client_state.cpp b/src/app/client_state.cpp index 1f0e7db..625174d 100644 --- a/src/app/client_state.cpp +++ b/src/app/client_state.cpp @@ -694,7 +694,7 @@ namespace { /** * @brief Close the active dialog and release staged picker data before streaming can begin. - * @param state Client state containing the dialog and any temporary folder listing. + * @param state Client state containing the dialog and its temporary content. */ void close_modal(app::ClientState &state) { if (state.modal.id == app::ModalId::directory_picker) { @@ -703,6 +703,9 @@ namespace { if (state.modal.id == app::ModalId::screenshot_viewer) { state.screenshotViewer = {}; } + if (state.modal.id == app::ModalId::stream_summary) { + state.streamSummaryLines.clear(); + } state.modal = {}; reset_confirmation(state); } @@ -801,6 +804,7 @@ namespace { case app::ModalId::connection_test: case app::ModalId::screenshot_viewer: case app::ModalId::quit_progress: + case app::ModalId::stream_summary: return 0U; } return 0U; @@ -1600,6 +1604,7 @@ namespace { case app::ModalId::directory_picker: case app::ModalId::screenshot_viewer: case app::ModalId::quit_progress: + case app::ModalId::stream_summary: return true; case app::ModalId::confirmation: return handle_confirmation_modal_activation(state, update); @@ -1798,6 +1803,16 @@ namespace { return false; } + if (state.modal.id == app::ModalId::stream_summary) { + if (const auto confirm = state.shell.inputDevice == input::UiInputDevice::keyboard ? input::UiCommand::confirm : input::UiCommand::activate; command == confirm) { + close_modal_and_mark_closed(state, update); + if (update != nullptr) { + update->navigation.activatedItemId = "confirm-stream-summary"; + } + } + return true; + } + if (state.modal.id == app::ModalId::connection_test && state.connectionTest.inProgress) { return true; } @@ -3023,10 +3038,17 @@ namespace app { state.appQuit = {}; } + void show_stream_summary(ClientState &state, std::vector lines) { + state.streamSummaryLines = std::move(lines); + state.shell.overlayVisible = false; + state.shell.overlayScrollOffset = 0U; + open_modal(state, ModalId::stream_summary); + } + AppUpdate handle_command(ClientState &state, input::UiCommand command) { AppUpdate update {}; - if (handle_overlay_command(state, command, &update)) { + if (state.modal.id != ModalId::stream_summary && handle_overlay_command(state, command, &update)) { return update; } diff --git a/src/app/client_state.h b/src/app/client_state.h index 9083590..f4dadad 100644 --- a/src/app/client_state.h +++ b/src/app/client_state.h @@ -65,6 +65,7 @@ namespace app { directory_picker, ///< Screenshot destination browser and local folder-name keyboard. screenshot_viewer, ///< Local captures associated with the selected server and app. quit_progress, ///< Authenticated host-app quit operation running in the background. + stream_summary, ///< Completed stream details on the app picker. }; /** @@ -462,6 +463,7 @@ namespace app { DirectoryPickerState directoryPicker; ///< Pending screenshot-directory workflow. ScreenshotViewerState screenshotViewer; ///< Local app screenshot library, released when browsing ends. AppQuitState appQuit; ///< Retained target for a confirmed host-app quit operation. + std::vector streamSummaryLines; ///< Session outcome and statistics retained until confirmation. ConfirmationDialogState confirmation; ///< Confirmation dialog content for destructive actions. }; @@ -757,6 +759,13 @@ namespace app { * @param message Completion or actionable failure detail. */ void apply_app_quit_result(ClientState &state, bool succeeded, const std::string &message); + + /** + * @brief Open the completed stream summary over the current app picker. + * @param state Client state receiving the summary. + * @param lines Final session outcome and statistics. + */ + void show_stream_summary(ClientState &state, std::vector lines); /** * @brief Remove a deleted capture from the open viewer and keep its selection in range. * @param state Viewer that requested deletion. diff --git a/src/network/host_pairing.cpp b/src/network/host_pairing.cpp index 68c261a..b3ad09d 100644 --- a/src/network/host_pairing.cpp +++ b/src/network/host_pairing.cpp @@ -2813,7 +2813,8 @@ namespace network { const PairingIdentity *clientIdentity, int appId, std::vector *assetBytes, - std::string *errorMessage + std::string *errorMessage, + const std::atomic *cancelRequested ) { if (address.empty() || httpsPort == 0 || appId <= 0) { return append_error(errorMessage, "The app-asset request requires a valid host address, port, and app ID"); @@ -2822,7 +2823,12 @@ namespace network { std::vector attemptFailures; for (const std::string &path : build_app_asset_paths(resolve_client_unique_id(clientIdentity), appId)) { HttpResponse response {}; - if (std::string attemptError; !http_get(address, httpsPort, path, true, clientIdentity, {}, &response, &attemptError)) { + std::string attemptError; + const bool downloaded = http_get(address, httpsPort, path, true, clientIdentity, {}, &response, &attemptError, cancelRequested); + if (pairing_cancel_requested(cancelRequested)) { + return append_error(errorMessage, "App artwork download cancelled"); + } + if (!downloaded) { std::string failure = path; failure += ": "; failure += attemptError; diff --git a/src/network/host_pairing.h b/src/network/host_pairing.h index fc8c7ac..57f142b 100644 --- a/src/network/host_pairing.h +++ b/src/network/host_pairing.h @@ -221,6 +221,7 @@ namespace network { * @param appId Host application identifier. * @param assetBytes Output vector populated with downloaded asset bytes. * @param errorMessage Optional output for request failures. + * @param cancelRequested Optional flag checked during the transfer and before trying alternate asset paths. * @return true when the asset was downloaded successfully. */ bool query_app_asset( @@ -229,7 +230,8 @@ namespace network { const PairingIdentity *clientIdentity, int appId, std::vector *assetBytes, - std::string *errorMessage = nullptr + std::string *errorMessage = nullptr, + const std::atomic *cancelRequested = nullptr ); /** diff --git a/src/streaming/session.cpp b/src/streaming/session.cpp index b5a2740..fd18f04 100644 --- a/src/streaming/session.cpp +++ b/src/streaming/session.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,9 @@ #include "src/streaming/ffmpeg_stream_backend.h" #include "src/streaming/stats_overlay.h" #include "src/streaming/stream_controls.h" +#include "src/ui/dialog_surface.h" +#include "src/ui/shell_layout.h" +#include "src/ui/shell_view.h" #ifdef NXDK /** @@ -68,12 +72,8 @@ namespace { constexpr Uint8 TEXT_RED = 0xF2; constexpr Uint8 TEXT_GREEN = 0xF5; constexpr Uint8 TEXT_BLUE = 0xF8; - constexpr Uint8 ACCENT_RED = 0x00; - constexpr Uint8 ACCENT_GREEN = 0xF3; - constexpr Uint8 ACCENT_BLUE = 0xD4; constexpr Uint32 STREAM_FRAME_DELAY_MILLISECONDS = 16U; constexpr Uint32 STREAM_PRESENT_POLL_MILLISECONDS = 2U; - constexpr Uint32 STREAM_END_STATS_POLL_MILLISECONDS = 50U; constexpr Uint32 STREAM_VIDEO_IDLE_IDR_MILLISECONDS = 1500U; constexpr Uint32 STREAM_VIDEO_IDLE_IDR_COOLDOWN_MILLISECONDS = 2000U; constexpr int MIN_STREAM_FPS = 15; @@ -83,6 +83,9 @@ namespace { constexpr int DEFAULT_PACKET_SIZE = 1024; constexpr std::size_t MAX_CONNECTION_PROTOCOL_MESSAGES = 24U; + /** + * @brief Holds the state owned by the stream's event and render thread. + */ struct StreamUiResources { SDL_Renderer *renderer = nullptr; TTF_Font *titleFont = nullptr; @@ -92,6 +95,7 @@ namespace { streaming::VideoStatisticsSampler statisticsSampler; ///< One-second telemetry intervals. streaming::StreamControls controller; streaming::FfmpegStreamBackend mediaBackend {}; + std::optional startupTick; ///< Session entry tick, cleared when the first video frame is presented. bool ttfInitialized = false; }; @@ -165,13 +169,16 @@ namespace { std::string rtspSessionUrl; }; + /** + * @brief References needed to run a transport startup attempt and report a failure. + */ struct StreamStartAttemptContext { const app::HostRecord &host; const app::HostAppRecord &app; StreamStartContext &startContext; StreamConnectionState &connectionState; StreamUiResources &resources; - std::string *statusMessage = nullptr; + std::string &statusMessage; ///< Result text to update if setup fails. }; struct ActiveStreamLoopContext { @@ -183,14 +190,6 @@ namespace { StreamUiResources &resources; }; - struct TextLineLayout { - SDL_Color color {}; - int x = 0; - int y = 0; - int maxWidth = 0; - int *drawnHeight = nullptr; - }; - struct ResolvedStreamParameters { VIDEO_MODE videoMode {}; int fps = app::DEFAULT_STREAM_FRAMERATE; @@ -199,12 +198,6 @@ namespace { int streamingRemotely = STREAM_CFG_AUTO; }; - void assign_status_message(std::string *statusMessage, std::string_view message) { - if (statusMessage != nullptr) { - statusMessage->assign(message.data(), message.size()); - } - } - class ScopedThreadPriority { public: explicit ScopedThreadPriority(SDL_ThreadPriority priority) { @@ -935,54 +928,48 @@ namespace { return 0; } - bool render_text_line(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, const TextLineLayout &layout) { - if (renderer == nullptr || font == nullptr || layout.maxWidth <= 0) { - if (layout.drawnHeight != nullptr) { - *layout.drawnHeight = 0; - } - return false; - } - - SDL_Surface *surface = TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), layout.color, static_cast(layout.maxWidth)); - if (surface == nullptr) { - if (layout.drawnHeight != nullptr) { - *layout.drawnHeight = 0; - } - return false; - } - + /** + * @brief Render text into a bounded area without changing SDL's renderer clip. + * @param renderer Active SDL renderer. + * @param surface Rasterized text. + * @param bounds Available space for the text. + * @return True when the text was copied to the renderer. + */ + bool render_text_surface(SDL_Renderer *renderer, SDL_Surface *surface, const SDL_Rect &bounds) { SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); if (texture == nullptr) { - SDL_FreeSurface(surface); - if (layout.drawnHeight != nullptr) { - *layout.drawnHeight = 0; - } return false; } - - const SDL_Rect destination {layout.x, layout.y, surface->w, surface->h}; - const int surfaceHeight = surface->h; - SDL_FreeSurface(surface); - const bool rendered = SDL_RenderCopy(renderer, texture, nullptr, &destination) == 0; + const SDL_Rect source {0, 0, std::min(surface->w, bounds.w), std::min(surface->h, bounds.h)}; + const SDL_Rect destination {bounds.x, bounds.y, source.w, source.h}; + const bool rendered = SDL_RenderCopy(renderer, texture, &source, &destination) == 0; SDL_DestroyTexture(texture); - if (layout.drawnHeight != nullptr) { - *layout.drawnHeight = surfaceHeight; - } return rendered; } - void render_text_lines(SDL_Renderer *renderer, TTF_Font *font, const std::vector &lines, int screenWidth, int *cursorY) { - if (cursorY == nullptr) { - return; + /** + * @brief Rasterize and draw a wrapped run of text at the requested position. + * @param renderer Active SDL renderer. + * @param font Font used for the run. + * @param text Text to draw. + * @param bounds Available space for the text. + * @param color Text color. + * @return True when the text was rendered. + */ + bool render_text_line(SDL_Renderer *renderer, TTF_Font *font, const std::string &text, const SDL_Rect &bounds, SDL_Color color) { + if (renderer == nullptr || font == nullptr || bounds.w <= 0 || bounds.h <= 0) { + return false; } - for (const std::string &line : lines) { - int drawnHeight = 0; - render_text_line(renderer, font, line, {{TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, 28, *cursorY, std::max(1, screenWidth - 56), &drawnHeight}); - *cursorY += drawnHeight + 6; - } + std::unique_ptr surface(TTF_RenderUTF8_Blended_Wrapped(font, text.c_str(), color, static_cast(bounds.w)), &SDL_FreeSurface); + return surface && render_text_surface(renderer, surface.get(), bounds); } + /** + * @brief Describe the current transport stage or an interruption when more detail is available. + * @param connectionState Current transport stage and outcome. + * @return Text overriding the startup step label, or an empty string to use that label. + */ std::string build_stage_status_line(const StreamConnectionState &connectionState) { if (const int failedStage = connectionState.failedStage.load(); failedStage != STAGE_NONE) { return std::string("Connection failed during ") + LiGetStageName(failedStage) + " (error " + std::to_string(connectionState.failedCode.load()) + ")"; @@ -991,32 +978,10 @@ namespace { const int terminationError = connectionState.terminationError.load(); return terminationError == ML_ERROR_GRACEFUL_TERMINATION ? "The host ended the stream gracefully." : "The host ended the stream (error " + std::to_string(terminationError) + ")"; } - if (connectionState.connectionStarted.load()) { - return std::string("Streaming transport is active. ") + input::STREAM_EXIT_HINT; + if (const int stage = connectionState.currentStage.load(); stage != STAGE_NONE && !connectionState.connectionStarted.load()) { + return std::string("Establishing the stream connection (") + LiGetStageName(stage) + ")..."; } - return std::string("Connecting: ") + LiGetStageName(connectionState.currentStage.load()); - } - - void open_controller_if_needed(StreamUiResources *resources, int deviceIndex) { - if (resources == nullptr) { - return; - } - - resources->controller.open_if_needed(deviceIndex); - } - - void close_controller_if_removed(StreamUiResources *resources, int joystickInstanceId) { - if (resources == nullptr) { - return; - } - - resources->controller.close_if_removed(joystickInstanceId); - } - - SDL_ControllerDeviceEvent copy_controller_device_event(const SDL_Event &event) { - SDL_ControllerDeviceEvent controllerEvent {}; - std::memcpy(&controllerEvent, &event, sizeof(controllerEvent)); - return controllerEvent; + return {}; } /** @@ -1190,6 +1155,88 @@ namespace { #endif } + /** + * @brief Paint startup progress with a footer that stays inside television-safe margins. + * @param resources Event-thread renderer and fonts. + * @param dialog Modal content with an optional startup progress step. + * @param screenWidth Output width in pixels. + * @param screenHeight Output height in pixels. + * @param footer Optional stream-menu hint. + * @return True when the dialog was drawn. + */ + bool draw_stream_start_dialog(const StreamUiResources &resources, const ui::ShellModalView &dialog, int screenWidth, int screenHeight, const std::string &footer = {}) { + const int padding = std::max(20, screenHeight / 40); + const int dialogWidth = std::min(760, screenWidth * 3 / 4); + const int innerWidth = dialogWidth - 2 * padding; + const int titleHeight = TTF_FontLineSkip(resources.titleFont); + const int bodyTop = 28 + titleHeight + 12; + constexpr int progressHeight = 8; + int footerTextHeight = 0; + if (!footer.empty()) { + int footerWidth = 0; + const bool wraps = TTF_SizeUTF8(resources.bodyFont, footer.c_str(), &footerWidth, nullptr) == 0 && footerWidth > innerWidth; + footerTextHeight = TTF_FontLineSkip(resources.bodyFont) * (wraps ? 2 : 1); + } + const int footerHeight = progressHeight + 12 + footerTextHeight; + const int maxBodyHeight = screenHeight - screenHeight / 10 - bodyTop - footerHeight - 2 * padding; + if (innerWidth <= 0 || maxBodyHeight <= 0) { + return false; + } + + std::string bodyText; + for (const auto &line : dialog.lines) { + if (!bodyText.empty()) { + bodyText += '\n'; + } + bodyText += line; + } + const auto rasterizeBody = [&](TTF_Font *font) { + return std::unique_ptr( + TTF_RenderUTF8_Blended_Wrapped(font, bodyText.c_str(), {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}, static_cast(innerWidth)), + SDL_FreeSurface + ); + }; + auto body = rasterizeBody(resources.bodyFont); + if (body && body->h > maxBodyHeight && resources.statisticsFont != nullptr) { + if (auto compact = rasterizeBody(resources.statisticsFont); compact && compact->h < body->h) { + body = std::move(compact); + } + } + if (!body) { + return false; + } + + const int bodyHeight = std::min(body->h, maxBodyHeight); + const auto [x, y, w, h] = ui::calculate_shell_dialog(screenWidth, screenHeight, dialogWidth, bodyTop + bodyHeight + footerHeight + 2 * padding); + SDL_SetRenderDrawColor(resources.renderer, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE, 0xFF); + SDL_RenderClear(resources.renderer); + ui::draw_dialog_surface(resources.renderer, {x, y, w, h}); + + const int left = x + padding; + const SDL_Color text {TEXT_RED, TEXT_GREEN, TEXT_BLUE, 0xFF}; + if (!render_text_line(resources.renderer, resources.titleFont, dialog.title, {left, y + 28, innerWidth, titleHeight}, text) || !render_text_surface(resources.renderer, body.get(), {left, y + bodyTop, innerWidth, bodyHeight})) { + return false; + } + if (dialog.streamStartupStep.has_value()) { + ui::draw_dialog_progress(resources.renderer, {left, y + h - padding - footerHeight, innerWidth, progressHeight}, static_cast(*dialog.streamStartupStep), ui::STREAM_STARTUP_STEP_COUNT); + } + if (!footer.empty()) { + const int footerY = y + h - padding - footerTextHeight; + return render_text_line(resources.renderer, resources.bodyFont, footer, {left, footerY, innerWidth, footerTextHeight}, {0x82, 0xB4, 0xFF, 0xFF}); + } + return true; + } + + /** + * @brief Present the latest decoded frame or continue showing startup progress. + * @param host Selected PC. + * @param app Selected app on the PC. + * @param context Stream configuration used for diagnostics. + * @param connectionState Current transport stage and outcome. + * @param mediaBackend Backend supplying video and local controls. + * @param resources Event-thread renderer, fonts and first-frame timing. + * @return True when there are resources to render the frame. + */ bool render_stream_frame( const app::HostRecord &host, const app::HostAppRecord &app, @@ -1209,29 +1256,22 @@ namespace { const bool hasDecodedVideo = mediaBackend != nullptr && mediaBackend->has_decoded_video(); if (hasDecodedVideo && mediaBackend->render_latest_video_frame(resources->renderer, screenWidth, screenHeight)) { + if (resources->startupTick.has_value()) { + logging::info("stream", "Stream startup: first video frame presented after " + std::to_string(SDL_GetTicks() - *resources->startupTick) + " ms in the session"); + resources->startupTick.reset(); + } render_stream_input_diagnostics(resources); return true; } - SDL_SetRenderDrawColor(resources->renderer, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE, 0xFF); - SDL_RenderClear(resources->renderer); - - int cursorY = 28; - int titleHeight = 0; - render_text_line(resources->renderer, resources->titleFont, "Moonlight Streaming", {{ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, 0xFF}, 28, cursorY, std::max(1, screenWidth - 56), &titleHeight}); - cursorY += titleHeight + 8; - - std::vector lines = { - std::string("Host: ") + host.displayName + " (" + context.address + ")", - std::string("App: ") + app.name + (context.rtspSessionUrl.empty() ? std::string {} : std::string(" | Session URL received")), - build_stage_status_line(connectionState), - mediaBackend != nullptr ? mediaBackend->build_overlay_status_line() : std::string("FFmpeg decode backend unavailable"), - input::STREAM_EXIT_HINT, - }; - lines.emplace(lines.begin() + 2, std::string("Mode: ") + std::to_string(context.streamConfiguration.width) + "x" + std::to_string(context.streamConfiguration.height) + " @ " + std::to_string(context.streamConfiguration.fps) + " FPS | " + (mediaBackend != nullptr ? mediaBackend->active_video_decoder_name() : std::string("video")) + " | Stereo"); - lines.emplace(lines.begin() + 4, std::string("Launch mode: ") + (context.serverInformation.rtspSessionUrl != nullptr ? "Session URL supplied by host" : "Default RTSP discovery")); - lines.emplace(lines.begin() + 5, "Waiting for the first decoded video frame and audio output."); - - render_text_lines(resources->renderer, resources->bodyFont, lines, screenWidth, &cursorY); + const auto step = connectionState.connectionStarted.load() ? ui::StreamStartupStep::first_frame : ui::StreamStartupStep::connecting; + auto dialog = ui::build_stream_launch_dialog(host, app, step); + if (auto detail = build_stage_status_line(connectionState); !detail.empty()) { + dialog.lines.front() = std::move(detail); + } + if (resources->statisticsEnabled && mediaBackend != nullptr) { + dialog.lines.push_back(mediaBackend->build_overlay_status_line()); + } + draw_stream_start_dialog(*resources, dialog, screenWidth, screenHeight, input::STREAM_EXIT_HINT); if (resources->controller.has_overlay() && mediaBackend != nullptr) { mediaBackend->render_local_overlay(resources->renderer, screenWidth, screenHeight); @@ -1241,122 +1281,45 @@ namespace { return true; } - bool render_stream_end_statistics_frame( + /** + * @brief Build the summary displayed by the shell after restoring the app picker. + * @param host PC that supplied the stream. + * @param app Selected host app. + * @param context Stream configuration and address. + * @param statisticsSnapshot Final acceleration and performance data. + * @param finalMessage Human-readable session outcome. + * @return Summary rows with optional performance details. + */ + std::vector build_stream_summary_lines( const app::HostRecord &host, const app::HostAppRecord &app, const StreamStartContext &context, const streaming::StreamStatisticsSnapshot &statisticsSnapshot, - const std::string &finalMessage, - StreamUiResources *resources + const std::string &finalMessage ) { - if (resources == nullptr || resources->renderer == nullptr || resources->titleFont == nullptr || resources->bodyFont == nullptr) { - return false; - } - - int screenWidth = 0; - SDL_GetRendererOutputSize(resources->renderer, &screenWidth, nullptr); - - SDL_SetRenderDrawColor(resources->renderer, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE, 0xFF); - SDL_RenderClear(resources->renderer); - - int cursorY = 28; - int titleHeight = 0; - const bool fallbackUsed = !statisticsSnapshot.accelerationSummary.empty() && statisticsSnapshot.accelerationSummary.front() == "SOFTWARE FALLBACK USED"; - const SDL_Color summaryColor = fallbackUsed ? SDL_Color {0xFF, 0xB4, 0x46, 0xFF} : SDL_Color {ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, 0xFF}; - render_text_line(resources->renderer, resources->titleFont, fallbackUsed ? "SOFTWARE FALLBACK USED" : "Stream Summary", {summaryColor, 28, cursorY, std::max(1, screenWidth - 56), &titleHeight}); - cursorY += titleHeight + 8; - - std::vector lines = statisticsSnapshot.accelerationSummary; - lines.push_back(finalMessage); - lines.push_back(std::string("Host: ") + host.displayName + " (" + context.address + ")"); - lines.push_back(std::string("App: ") + app.name); + std::vector lines {finalMessage, "Host: " + host.displayName + " (" + context.address + ")", "App: " + app.name}; + lines.insert(lines.end(), statisticsSnapshot.accelerationSummary.begin(), statisticsSnapshot.accelerationSummary.end()); if (statisticsSnapshot.showPerformanceMetrics) { - for (const std::string &line : streaming::build_stats_overlay_lines(statisticsSnapshot)) { - lines.push_back(line); - } - } - lines.emplace_back("Press any button to return."); - - render_text_lines(resources->renderer, resources->bodyFont, lines, screenWidth, &cursorY); - - SDL_RenderPresent(resources->renderer); - return true; - } - - bool should_close_stream_end_statistics(const SDL_Event &event, StreamUiResources *resources) { - if (event.type == SDL_CONTROLLERDEVICEADDED) { - const SDL_ControllerDeviceEvent controllerEvent = copy_controller_device_event(event); - open_controller_if_needed(resources, controllerEvent.which); - return false; - } - if (event.type == SDL_CONTROLLERDEVICEREMOVED) { - const SDL_ControllerDeviceEvent controllerEvent = copy_controller_device_event(event); - close_controller_if_removed(resources, controllerEvent.which); - return false; - } - return event.type == SDL_QUIT || event.type == SDL_KEYDOWN || event.type == SDL_MOUSEBUTTONDOWN; - } - - void show_stream_end_statistics( - const app::HostRecord &host, - const app::HostAppRecord &app, - const StreamStartContext &context, - const streaming::StreamStatisticsSnapshot &statisticsSnapshot, - const std::string &finalMessage, - StreamUiResources *resources - ) { - if (!render_stream_end_statistics_frame(host, app, context, statisticsSnapshot, finalMessage, resources)) { - return; - } - - resources->controller.poll(); - bool controllerReadyForPress = !resources->controller.any_button_pressed(); - - for (;;) { - SDL_Event event {}; - while (SDL_PollEvent(&event) != 0) { - if (should_close_stream_end_statistics(event, resources)) { - return; - } - } - resources->controller.poll(); - if (!resources->controller.any_button_pressed()) { - controllerReadyForPress = true; - } else if (controllerReadyForPress) { - return; - } - SDL_Delay(STREAM_END_STATS_POLL_MILLISECONDS); + const auto performance = streaming::build_stats_overlay_lines(statisticsSnapshot); + lines.insert(lines.end(), performance.begin(), performance.end()); } + return lines; } /** - * @brief Acknowledge a local quit before waiting for the input sender and transport teardown. - * - * @param resources Stream renderer and font resources owned by the event thread. + * @brief Run transport startup while the event thread handles input and presents progress. + * @param attempt Resources and status for one transport startup attempt. + * @return True when the worker ran to completion or cancellation. */ - void render_stream_stopping_frame(StreamUiResources *resources) { - if (resources == nullptr || resources->renderer == nullptr || resources->titleFont == nullptr) { - return; - } - resources->mediaBackend.stop_video_presentation(); - int screenWidth = 0; - SDL_GetRendererOutputSize(resources->renderer, &screenWidth, nullptr); - SDL_SetRenderDrawColor(resources->renderer, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE, 0xFF); - SDL_RenderClear(resources->renderer); - render_text_line(resources->renderer, resources->titleFont, "Stopping stream...", {{ACCENT_RED, ACCENT_GREEN, ACCENT_BLUE, 0xFF}, 28, 28, std::max(1, screenWidth - 56), nullptr}); - SDL_RenderPresent(resources->renderer); - } - bool run_stream_start_attempt(const StreamStartAttemptContext &attempt) { if (SDL_Thread *startThread = SDL_CreateThread(run_stream_start_thread, "start-stream", &attempt.startContext); startThread != nullptr) { while (!attempt.connectionState.startCompleted.load() && !attempt.connectionState.stopRequested.load()) { pump_stream_events(&attempt.resources, &attempt.connectionState); if (attempt.connectionState.stopRequested.load()) { - render_stream_stopping_frame(&attempt.resources); LiInterruptConnection(); - } else { - render_stream_frame(attempt.host, attempt.app, attempt.startContext, attempt.connectionState, &attempt.resources.mediaBackend, &attempt.resources); + break; } + render_stream_frame(attempt.host, attempt.app, attempt.startContext, attempt.connectionState, &attempt.resources.mediaBackend, &attempt.resources); SDL_Delay(STREAM_FRAME_DELAY_MILLISECONDS); } @@ -1368,7 +1331,7 @@ namespace { const std::string createThreadError = std::string("Failed to start the streaming transport thread: ") + SDL_GetError(); close_stream_ui_resources(&attempt.resources); - assign_status_message(attempt.statusMessage, createThreadError); + attempt.statusMessage = createThreadError; logging::error("stream", createThreadError); return false; } @@ -1425,7 +1388,6 @@ namespace { while (!loopContext.connectionState.connectionTerminated.load() && !loopContext.connectionState.stopRequested.load()) { pump_stream_events(&loopContext.resources, &loopContext.connectionState); if (loopContext.connectionState.stopRequested.load()) { - render_stream_stopping_frame(&loopContext.resources); break; } render_active_stream_frame_if_needed(loopContext, &lastIdleVideoIdrRequestTick); @@ -1478,21 +1440,20 @@ namespace streaming { const app::HostAppRecord &app, const network::PairingIdentity &clientIdentity, SDL_GameController *preferredController, - SDL_GameController **returnController, - std::string *statusMessage + StreamSessionReport &report ) { - if (returnController != nullptr) { - *returnController = nullptr; - } + const Uint32 sessionStarted = SDL_GetTicks(); + report = {}; const ResolvedStreamParameters streamParameters = resolve_stream_parameters(videoMode, settings); const VIDEO_MODE streamPresentationVideoMode = select_stream_presentation_video_mode(videoMode, streamParameters.videoMode); ScopedStreamVideoMode scopedStreamVideoMode(window, videoMode, streamPresentationVideoMode); const VIDEO_MODE &activeStreamVideoMode = scopedStreamVideoMode.active_video_mode(); StreamUiResources resources {}; + resources.startupTick = sessionStarted; resources.statisticsEnabled = settings.presentation.showPerformanceStats; if (std::string initializationError; !initialize_stream_ui_resources(window, activeStreamVideoMode, preferredController, &resources, &initializationError)) { - assign_status_message(statusMessage, initializationError); + report.message = initializationError; logging::error("stream", initializationError); return false; } @@ -1505,7 +1466,7 @@ namespace streaming { std::array remoteInputIv {}; if (std::string randomError; !fill_random_bytes(remoteInputKey.data(), remoteInputKey.size(), &randomError) || !fill_random_bytes(remoteInputIv.data(), remoteInputIv.size(), &randomError)) { close_stream_ui_resources(&resources); - assign_status_message(statusMessage, randomError); + report.message = randomError; logging::error("stream", randomError); return false; } @@ -1525,9 +1486,19 @@ namespace streaming { launchConfiguration.remoteInputAesKeyHex = hex_encode(remoteInputKey.data(), remoteInputKey.size()); launchConfiguration.remoteInputAesIvHex = hex_encode(remoteInputIv.data(), remoteInputIv.size()); - if (std::string launchError; !network::launch_or_resume_stream(hostAddress, httpPort, clientIdentity, launchConfiguration, &launchResult, &launchError)) { + int screenWidth = 0; + int screenHeight = 0; + SDL_GetRendererOutputSize(resources.renderer, &screenWidth, &screenHeight); + draw_stream_start_dialog(resources, ui::build_stream_launch_dialog(host, app, ui::StreamStartupStep::starting_on_host), screenWidth, screenHeight); + SDL_RenderPresent(resources.renderer); + + std::string launchError; + const Uint32 hostRequestStarted = SDL_GetTicks(); + const bool accepted = network::launch_or_resume_stream(hostAddress, httpPort, clientIdentity, launchConfiguration, &launchResult, &launchError); + logging::info("stream", "Stream startup: host request took " + std::to_string(SDL_GetTicks() - hostRequestStarted) + " ms"); + if (!accepted) { close_stream_ui_resources(&resources); - assign_status_message(statusMessage, launchError); + report.message = launchError; logging::error("stream", launchError); return false; } @@ -1572,7 +1543,7 @@ namespace streaming { startContext.connectionCallbacks.logMessage = on_log_message; bool rtspFallbackAttempted = false; - if (const StreamStartAttemptContext startAttempt {host, app, startContext, connectionState, resources, statusMessage}; !run_stream_start_with_rtsp_fallback(startAttempt, &rtspFallbackAttempted)) { + if (const StreamStartAttemptContext startAttempt {host, app, startContext, connectionState, resources, report.message}; !run_stream_start_with_rtsp_fallback(startAttempt, &rtspFallbackAttempted)) { return false; } @@ -1583,7 +1554,7 @@ namespace streaming { } active_connection_state() = nullptr; close_stream_ui_resources(&resources); - assign_status_message(statusMessage, failureMessage); + report.message = failureMessage; logging::warn("stream", failureMessage); return false; } @@ -1628,12 +1599,10 @@ namespace streaming { settings.controls.keyboardDockTop = resources.controller.keyboard_docked_top(); settings.dirty = true; } - show_stream_end_statistics(host, app, startContext, finalStatistics, finalMessage, &resources); - if (returnController != nullptr) { - *returnController = resources.controller.take_controller_for_shell(); - } + report.summaryLines = build_stream_summary_lines(host, app, startContext, finalStatistics, finalMessage); + report.controller = resources.controller.take_controller_for_shell(); close_stream_ui_resources(&resources); - assign_status_message(statusMessage, finalMessage); + report.message = std::move(finalMessage); return true; } diff --git a/src/streaming/session.h b/src/streaming/session.h index ea56600..0e407ef 100644 --- a/src/streaming/session.h +++ b/src/streaming/session.h @@ -6,6 +6,7 @@ // standard includes #include +#include // local includes #include "src/app/client_state.h" @@ -18,10 +19,20 @@ typedef struct _SDL_GameController SDL_GameController; ///< SDL controller hand namespace streaming { + /** + * @brief Result returned to the shell after the stream resources are released. + */ + struct StreamSessionReport { + SDL_GameController *controller = nullptr; ///< Owned controller reference to keep or close on return, or nullptr. + std::string message; ///< Final session status or reason startup failed. + std::vector summaryLines; ///< Outcome and statistics for the app picker's summary dialog. + }; + /** * @brief Run one Xbox streaming session for the selected host app. * - * The session launches or resumes the selected app on the host, starts the + * The session displays a progress dialog while launching or resuming the selected app, + * establishing the connection and waiting for video. It starts the * Moonlight transport runtime, decodes negotiated video and Opus audio with * FFmpeg, forwards controller input, renders the latest decoded frame with a * lightweight overlay, and returns once the user stops streaming or the host @@ -37,8 +48,7 @@ namespace streaming { * @param app Selected host app. * @param clientIdentity Paired client identity used for authenticated launch requests. * @param preferredController Controller retained by the shell throughout the session, or nullptr to use the first available device. - * @param returnController Optional output receiving an owned reference to the controller used to dismiss the summary, or nullptr on failure. The shell must preserve or close this reference. - * @param statusMessage Output message describing the final session result. + * @param report Output receiving the final status, summary and an owned controller reference when available. * @return True when the stream session started successfully. */ bool run_stream_session( @@ -49,8 +59,7 @@ namespace streaming { const app::HostAppRecord &app, const network::PairingIdentity &clientIdentity, SDL_GameController *preferredController, - SDL_GameController **returnController, - std::string *statusMessage + StreamSessionReport &report ); } // namespace streaming diff --git a/src/streaming/stream_controls.cpp b/src/streaming/stream_controls.cpp index 48840d6..164f6cd 100644 --- a/src/streaming/stream_controls.cpp +++ b/src/streaming/stream_controls.cpp @@ -397,12 +397,6 @@ namespace streaming { return chord; } - bool StreamControls::any_button_pressed() const { - return std::any_of(devices_.begin(), devices_.end(), [](const Device &d) { - return d.sample.buttonFlags != 0 || d.sample.leftTrigger != 0 || d.sample.rightTrigger != 0; - }); - } - SDL_GameController *StreamControls::take_controller_for_shell() { poll(); for (auto &device : devices_) { diff --git a/src/streaming/stream_controls.h b/src/streaming/stream_controls.h index ea44ab3..c42874e 100644 --- a/src/streaming/stream_controls.h +++ b/src/streaming/stream_controls.h @@ -92,15 +92,10 @@ namespace streaming { */ void close_if_removed(int instance); /** - * @brief Sample devices for summary dismissal. + * @brief Sample the state of all connected controllers. * @return True when any controller has the menu chord pressed. */ bool poll(); - /** - * @brief Check summary dismissal buttons. - * @return True if any connected pad has a pressed button or trigger. - */ - bool any_button_pressed() const; /** * @brief Transfer a connected controller to the shell. * @return Owned reference, or nullptr. diff --git a/src/ui/dialog_surface.h b/src/ui/dialog_surface.h index 727ead6..e3c3197 100644 --- a/src/ui/dialog_surface.h +++ b/src/ui/dialog_surface.h @@ -48,4 +48,26 @@ namespace ui { draw_dialog_round_rect(renderer, {rect.x + 20, rect.y + 16, 36, 3}, {0x82, 0xB4, 0xFF, 255}, 1); SDL_SetRenderDrawBlendMode(renderer, previousBlend); } + + /** + * @brief Paint a progress bar with separate completed, active and pending segments. + * @param renderer Active SDL renderer. + * @param rect Full progress bar bounds. + * @param step Zero-based index of the active segment. + * @param count Total number of segments. + */ + inline void draw_dialog_progress(SDL_Renderer *renderer, SDL_Rect rect, int step, int count) { + constexpr int gap = 4; + const int trackWidth = rect.w - gap * (count - 1); + if (count <= 0 || trackWidth < count || rect.h <= 0) { + return; + } + for (int segment = 0; segment < count; ++segment) { + const int left = segment * trackWidth / count; + const int right = (segment + 1) * trackWidth / count; + const SDL_Color activeOrPending = segment == step ? SDL_Color {0x4D, 0x77, 0xB3, 255} : SDL_Color {0x30, 0x3D, 0x52, 255}; + const SDL_Color color = segment < step ? SDL_Color {0x82, 0xB4, 0xFF, 255} : activeOrPending; + draw_dialog_round_rect(renderer, {rect.x + left + gap * segment, rect.y, right - left, rect.h}, color, 2); + } + } } // namespace ui diff --git a/src/ui/shell_screen.cpp b/src/ui/shell_screen.cpp index 1768553..7bb6b06 100644 --- a/src/ui/shell_screen.cpp +++ b/src/ui/shell_screen.cpp @@ -2892,14 +2892,18 @@ namespace { std::vector apps; }; + /** + * @brief Owns an artwork download batch that can yield to stream startup. + */ struct AppArtTaskState { - SDL_Thread *thread = nullptr; - std::atomic completed = false; - std::string address; - uint16_t port = 0; - std::vector apps; - std::vector cachedAppIds; - std::size_t failureCount = 0; + SDL_Thread *thread = nullptr; ///< Worker retained until its results have been joined. + std::atomic completed = false; ///< Published once the worker has finished writing results. + std::atomic cancelRequested = false; ///< Set by the shell before handing off to a stream. + std::string address; ///< Host endpoint for the downloads. + uint16_t port = 0; ///< Host HTTPS port. + std::vector apps; ///< Snapshot of the app library. + std::vector cachedAppIds; ///< IDs whose artwork was stored successfully. + std::size_t failureCount = 0; ///< Downloads that failed before cancellation. }; struct HostDiscoveryTaskState { @@ -3060,6 +3064,10 @@ namespace { return task.thread != nullptr && !task.completed.load(); } + /** + * @brief Clear a joined artwork worker and its cancellation request. + * @param task Artwork batch to reset. + */ void reset_app_art_task(AppArtTaskState *task) { if (task == nullptr) { return; @@ -3067,6 +3075,7 @@ namespace { task->thread = nullptr; task->completed.store(false); + task->cancelRequested.store(false); task->address.clear(); task->port = 0; task->apps.clear(); @@ -3690,6 +3699,11 @@ namespace { } } + /** + * @brief Download and store missing cover art until the batch completes or the shell requests a stop. + * @param context Artwork task retained by the shell until the worker has joined. + * @return Zero after publishing completion, or a negative value for an invalid task. + */ int run_app_art_task(void *context) { // NOSONAR(cpp:S5008) SDL_CreateThread requires void* callback signature auto *task = static_cast(context); if (task == nullptr) { @@ -3704,13 +3718,18 @@ namespace { } for (const app::HostAppRecord &appRecord : task->apps) { + if (task->cancelRequested.load()) { + break; + } if (appRecord.boxArtCached || appRecord.boxArtCacheKey.empty()) { continue; } std::vector assetBytes; - if (std::string errorMessage; !network::query_app_asset(task->address, task->port, &clientIdentity, appRecord.id, &assetBytes, &errorMessage)) { - ++task->failureCount; + if (!network::query_app_asset(task->address, task->port, &clientIdentity, appRecord.id, &assetBytes, nullptr, &task->cancelRequested)) { + if (!task->cancelRequested.load()) { + ++task->failureCount; + } continue; } @@ -3726,8 +3745,14 @@ namespace { return 0; } - void finish_app_art_task_if_ready(app::ClientState &state, AppArtTaskState *task, CoverArtTextureCache *textureCache) { - if (task == nullptr || task->thread == nullptr || !task->completed.load()) { + /** + * @brief Collect cached artwork from a finished batch or join a batch asked to stop for streaming. + * @param state Host library receiving the successfully cached app IDs. + * @param task Artwork batch owned by the shell. + * @param textureCache Cover-art textures owned by the active renderer. + */ + void collect_app_art_task(app::ClientState &state, AppArtTaskState *task, CoverArtTextureCache *textureCache) { + if (task == nullptr || task->thread == nullptr || (!task->completed.load() && !task->cancelRequested.load())) { return; } @@ -4010,9 +4035,10 @@ namespace { const int titleHeight = std::max(TTF_FontLineSkip(titleFont), statusIconSize) + 18; const int footerHeight = std::max(40, TTF_FontLineSkip(smallFont) + 18); const int rowHeight = std::max(36, TTF_FontLineSkip(bodyFont) + 16); - const int textHeight = measure_body_lines_height(smallFont, modal.lines, innerWidth, 8); + const int lineGap = modal.compactBody ? 3 : 8; + const int textHeight = measure_body_lines_height(smallFont, modal.lines, innerWidth, lineGap); const int actionsHeight = static_cast(modal.actions.size()) * (rowHeight + 6); - const SDL_Rect bounds = sdl_rect(ui::calculate_shell_dialog(width, height, dialogWidth, padding * 2 + titleHeight + textHeight + actionsHeight + footerHeight + 20)); + const SDL_Rect bounds = sdl_rect(ui::calculate_shell_dialog(width, height, dialogWidth, padding * 2 + titleHeight + textHeight + actionsHeight + footerHeight + (modal.compactBody ? 22 : 20))); fill_rect(renderer, {0, 0, width, height}, 0, 0, 0, 0xA0); render_dialog_surface(renderer, bounds); const int titleInset = statusIconSize == 0 ? 0 : statusIconSize + 12; @@ -4034,7 +4060,7 @@ namespace { if (!render_bounded_text(renderer, smallFont, line, {MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0xFF}, {bounds.x + padding, cursorY, innerWidth, textBottom - cursorY}, &drawnHeight)) { return false; } - cursorY += drawnHeight + 8; + cursorY += drawnHeight + lineGap; } const SDL_Rect rows {bounds.x + padding, cursorY + 4, innerWidth, footerY - cursorY - 12}; if (folder != nullptr) { @@ -4044,6 +4070,11 @@ namespace { if (!modal.actions.empty() && !render_action_rows(renderer, bodyFont, modal.actions, rows, rowHeight, {assetCache, true, folder})) { return false; } + if (modal.streamStartupStep.has_value()) { + constexpr int progressHeight = 8; + ui::draw_dialog_progress(renderer, {bounds.x + padding, footerY + (footerHeight - progressHeight) / 2, innerWidth, progressHeight}, static_cast(*modal.streamStartupStep), ui::STREAM_STARTUP_STEP_COUNT); + return true; + } draw_line(renderer, bounds.x + padding, footerY - 6, bounds.x + bounds.w - padding, footerY - 6, MUTED_RED, MUTED_GREEN, MUTED_BLUE, 0x25); const std::vector defaultActions = modal.actions.empty() ? std::vector {{"close", "Close", "icons\\button-b.svg", {}, false}} : @@ -5058,7 +5089,8 @@ namespace { (void) threadResult; } - void finalize_shell_tasks(ShellRuntimeState *runtime); + void join_shell_session_tasks(ShellRuntimeState &runtime); + void finalize_shell_tasks(ShellRuntimeState &runtime); /** * @brief Open the first detected SDL game controller for shell navigation. @@ -5178,7 +5210,7 @@ namespace { } /** - * @brief Initialize shell tasks and the notification lifetime clock for a new session. + * @brief Reset shell input, app tasks and the notification clock without resetting retained host workers. * * @param state Client state whose logging configuration should be applied. * @param runtime Runtime state to prepare. @@ -5192,8 +5224,6 @@ namespace { reset_pairing_task(&runtime->pairingTask); reset_app_list_task(&runtime->appListTask); reset_app_art_task(&runtime->appArtTask); - reset_host_discovery_task(&runtime->hostDiscoveryTask); - reset_host_probe_task(&runtime->hostProbeTask); runtime->notificationTick = SDL_GetTicks(); logging::set_minimum_level(logging::LogLevel::trace); logging::set_file_minimum_level(state.settings.loggingLevel); @@ -5208,15 +5238,21 @@ namespace { * @param state Client state to visualize. * @param resources Shell resources supplying the renderer and caches. * @param runtime Runtime state receiving redraw and shutdown updates. + * @param launchDialog Optional busy dialog for the handoff to a streaming session. * @return True when the frame rendered successfully. */ - bool draw_current_shell_frame(const VIDEO_MODE &videoMode, app::ClientState &state, ShellResources *resources, ShellRuntimeState *runtime) { + bool draw_current_shell_frame(const VIDEO_MODE &videoMode, app::ClientState &state, ShellResources *resources, ShellRuntimeState *runtime, const ui::ShellModalView *launchDialog = nullptr) { if (resources == nullptr || runtime == nullptr) { return false; } const std::vector retainedEntries = logging::snapshot(logging::LogLevel::info); - if (const auto viewModel = ui::build_shell_view_model(state, retainedEntries); draw_shell(resources->renderer, videoMode, resources->encoderSettings, resources->titleFont, resources->bodyFont, resources->smallFont, viewModel, &resources->coverArtTextureCache, &resources->assetTextureCache, &resources->keypadModalLayoutCache)) { + auto viewModel = ui::build_shell_view_model(state, retainedEntries); + if (launchDialog != nullptr) { + viewModel.modal = *launchDialog; + viewModel.notification.visible = false; + } + if (draw_shell(resources->renderer, videoMode, resources->encoderSettings, resources->titleFont, resources->bodyFont, resources->smallFont, viewModel, &resources->coverArtTextureCache, &resources->assetTextureCache, &resources->keypadModalLayoutCache)) { runtime->keypadRedrawRequested = false; return true; } @@ -5255,7 +5291,7 @@ namespace { finish_connection_test_if_ready(state, &runtime->connectionTestTask); finish_app_list_task_if_ready(state, &runtime->appListTask); finish_app_quit_task(state, runtime->appQuitTask); - finish_app_art_task_if_ready(state, &runtime->appArtTask, &resources->coverArtTextureCache); + collect_app_art_task(state, &runtime->appArtTask, &resources->coverArtTextureCache); finish_host_discovery_task_if_ready(state, &runtime->hostDiscoveryTask, &runtime->nextHostProbeTick); finish_host_probe_task_if_ready(state, &runtime->hostProbeTask); } @@ -5279,7 +5315,7 @@ namespace { } /** - * @brief Launch the selected stream and report startup failures not covered by its end summary. + * @brief Launch the selected stream and show its report after restoring the app picker. * * @param window Shared SDL window reused when entering a streaming session. * @param activeVideoMode Active output mode used by the shell renderer. @@ -5310,6 +5346,19 @@ namespace { return true; } + if (window == nullptr) { + state.shell.statusMessage = "Streaming requires a valid SDL window."; + app::show_notification(state, app::NotificationKind::error, "Couldn't start stream", "Restart Moonlight and try again."); + logging::error("stream", state.shell.statusMessage); + return true; + } + + const Uint32 preparationStarted = SDL_GetTicks(); + if (const auto launchDialog = ui::build_stream_launch_dialog(*streamHost, *streamApp); !draw_current_shell_frame(activeVideoMode, state, resources, runtime, &launchDialog)) { + return false; + } + runtime->keypadRedrawRequested = true; + network::PairingIdentity clientIdentity {}; if (std::string identityError; !load_saved_pairing_identity_for_streaming(&clientIdentity, &identityError)) { state.shell.statusMessage = identityError; @@ -5318,28 +5367,27 @@ namespace { return true; } - if (window == nullptr) { - state.shell.statusMessage = "Streaming requires a valid SDL window."; - app::show_notification(state, app::NotificationKind::error, "Couldn't start stream", "Restart Moonlight and try again."); - logging::error("stream", state.shell.statusMessage); - return true; - } - - finalize_shell_tasks(runtime); + // A library can have many missing covers, each with several candidate URLs. + // Stop the batch and keep any artwork it already saved before changing renderers. + runtime->appArtTask.cancelRequested.store(true); + collect_app_art_task(state, &runtime->appArtTask, &resources->coverArtTextureCache); + // Host refresh workers only use their own snapshots. Keep their state alive + // across the session and let them finish without holding up stream startup. + join_shell_session_tasks(*runtime); // Keep the menu's working controller open while render resources change. The Xbox // SDL backend owns a pending USB read that must not be torn down at this boundary. std::unique_ptr retainedController(std::exchange(resources->controller, nullptr), SDL_GameControllerClose); close_shell_resources(resources); - std::string sessionMessage; - SDL_GameController *returnController = nullptr; - const bool streamStarted = streaming::run_stream_session(window, activeVideoMode, state.settings, *streamHost, *streamApp, clientIdentity, retainedController.get(), &returnController, &sessionMessage); - state.shell.statusMessage = sessionMessage; + streaming::StreamSessionReport report; + logging::info("stream", "Stream startup: menu preparation took " + std::to_string(SDL_GetTicks() - preparationStarted) + " ms"); + const bool streamStarted = streaming::run_stream_session(window, activeVideoMode, state.settings, *streamHost, *streamApp, clientIdentity, retainedController.get(), report); + state.shell.statusMessage = std::move(report.message); if (!streamStarted) { app::show_notification(state, app::NotificationKind::error, "Streaming failed", "Check that the PC is online and Sunshine is running, then try again."); } - if (returnController != nullptr) { - retainedController.reset(returnController); + if (report.controller != nullptr) { + retainedController.reset(report.controller); } resources->controller = retainedController.release(); @@ -5351,6 +5399,8 @@ namespace { } initialize_shell_runtime(state, runtime); + finish_host_discovery_task_if_ready(state, &runtime->hostDiscoveryTask, &runtime->nextHostProbeTick); + finish_host_probe_task_if_ready(state, &runtime->hostProbeTask); app::AppUpdate settingsUpdate {}; settingsUpdate.persistence.settingsChanged = state.settings.dirty; persist_settings_if_needed(state, settingsUpdate); @@ -5359,6 +5409,14 @@ namespace { state.hosts.active.appListStatusMessage = "Refreshing host apps after the stream session..."; state.hosts.active.lastAppListRefreshTick = 0U; } + if (streamStarted) { + // Require a new press once the summary opens, even if input queued during teardown. + SDL_PumpEvents(); + SDL_FlushEvent(SDL_KEYDOWN); + SDL_FlushEvent(SDL_CONTROLLERBUTTONDOWN); + app::show_stream_summary(state, std::move(report.summaryLines)); + return draw_current_shell_frame(activeVideoMode, state, resources, runtime); + } return true; } @@ -5511,36 +5569,37 @@ namespace { } /** - * @brief Join any background shell tasks before renderer resources are released. - * + * @brief Join interactive shell requests before starting a stream or exiting the shell. * @param runtime Runtime state that owns the background tasks. */ - void finalize_shell_tasks(ShellRuntimeState *runtime) { - if (runtime == nullptr) { - return; - } - - if (std::unique_ptr activeAttempt = std::move(runtime->pairingTask.activeAttempt); activeAttempt != nullptr) { + void join_shell_session_tasks(ShellRuntimeState &runtime) { + if (std::unique_ptr activeAttempt = std::move(runtime.pairingTask.activeAttempt); activeAttempt != nullptr) { activeAttempt->discardResult.store(true); finalize_pairing_attempt(nullptr, std::move(activeAttempt)); } - while (!runtime->pairingTask.retiredAttempts.empty()) { - std::unique_ptr attempt = std::move(runtime->pairingTask.retiredAttempts.back()); - runtime->pairingTask.retiredAttempts.pop_back(); + while (!runtime.pairingTask.retiredAttempts.empty()) { + std::unique_ptr attempt = std::move(runtime.pairingTask.retiredAttempts.back()); + runtime.pairingTask.retiredAttempts.pop_back(); if (attempt != nullptr) { attempt->discardResult.store(true); } finalize_pairing_attempt(nullptr, std::move(attempt)); } - wait_for_thread(runtime->appListTask.thread); - wait_for_thread(runtime->appQuitTask.thread); - runtime->appQuitTask.thread = nullptr; - wait_for_thread(runtime->connectionTestTask.thread); - runtime->connectionTestTask.thread = nullptr; - wait_for_thread(runtime->appArtTask.thread); - wait_for_thread(runtime->hostDiscoveryTask.thread); - for (const std::unique_ptr &worker : runtime->hostProbeTask.workers) { + wait_for_thread(std::exchange(runtime.appListTask.thread, nullptr)); + wait_for_thread(std::exchange(runtime.appQuitTask.thread, nullptr)); + wait_for_thread(std::exchange(runtime.connectionTestTask.thread, nullptr)); + wait_for_thread(std::exchange(runtime.appArtTask.thread, nullptr)); + } + + /** + * @brief Join remaining background workers before their shared shell state is destroyed. + * @param runtime Runtime state that owns the background tasks and result queues. + */ + void finalize_shell_tasks(ShellRuntimeState &runtime) { + join_shell_session_tasks(runtime); + wait_for_thread(runtime.hostDiscoveryTask.thread); + for (const std::unique_ptr &worker : runtime.hostProbeTask.workers) { if (worker == nullptr) { continue; } @@ -5578,7 +5637,7 @@ namespace ui { } } - finalize_shell_tasks(&runtime); + finalize_shell_tasks(runtime); close_shell_resources(&resources); return 0; } diff --git a/src/ui/shell_view.cpp b/src/ui/shell_view.cpp index 8b60ee0..58dba84 100644 --- a/src/ui/shell_view.cpp +++ b/src/ui/shell_view.cpp @@ -715,6 +715,12 @@ namespace { viewModel->modal.busy = true; viewModel->modal.iconAssetPath = "icons\\loader-circle.svg"; return; + case app::ModalId::stream_summary: + viewModel->modal.title = "Stream Summary"; + viewModel->modal.lines = state.streamSummaryLines; + viewModel->modal.compactBody = true; + viewModel->modal.footerActions = {{"confirm-stream-summary", "Confirm", "icons\\button-a.svg", {}, true}}; + return; case app::ModalId::log_viewer: fill_log_viewer_modal_view(state, viewModel); return; @@ -926,7 +932,7 @@ namespace ui { } bool same_action_dialog_content(const ShellModalView &previous, const ShellModalView ¤t) { - if (previous.folderBrowser != current.folderBrowser || !previous.visible || !current.visible || previous.busy || current.busy || previous.title != current.title || previous.iconAssetPath != current.iconAssetPath || previous.lines != current.lines || previous.actions.size() != current.actions.size() || previous.footerActions.size() != current.footerActions.size()) { + if (previous.folderBrowser != current.folderBrowser || previous.compactBody != current.compactBody || !previous.visible || !current.visible || previous.busy || current.busy || previous.title != current.title || previous.iconAssetPath != current.iconAssetPath || previous.lines != current.lines || previous.actions.size() != current.actions.size() || previous.footerActions.size() != current.footerActions.size()) { return false; } for (std::size_t i = 0; i < current.actions.size(); ++i) { @@ -1028,6 +1034,29 @@ namespace ui { } } + ShellModalView build_stream_launch_dialog(const app::HostRecord &host, const app::HostAppRecord &app, StreamStartupStep step) { + ShellModalView dialog {}; + dialog.visible = true; + dialog.busy = true; + dialog.title = "Starting " + app.name; + dialog.streamStartupStep = step; + switch (step) { + case StreamStartupStep::preparing: + dialog.lines = {"Preparing to connect to " + host.displayName + "..."}; + break; + case StreamStartupStep::starting_on_host: + dialog.lines = {"Waiting for " + host.displayName + " to start the stream..."}; + break; + case StreamStartupStep::connecting: + dialog.lines = {"Connecting to " + host.displayName + "..."}; + break; + case StreamStartupStep::first_frame: + dialog.lines = {"Waiting for the first video frame..."}; + break; + } + return dialog; + } + ShellViewModel build_shell_view_model( const app::ClientState &state, const std::vector &logEntries, diff --git a/src/ui/shell_view.h b/src/ui/shell_view.h index 6340815..758b1e6 100644 --- a/src/ui/shell_view.h +++ b/src/ui/shell_view.h @@ -6,6 +6,7 @@ // standard includes #include +#include #include #include @@ -151,6 +152,18 @@ namespace ui { std::vector lines; ///< Diagnostics overlay body lines. }; + /** + * @brief Startup milestones in the order shown by the progress bar. + */ + enum class StreamStartupStep { + preparing, ///< Preparing the client and handing off from the app picker. + starting_on_host, ///< Waiting for the host to launch or resume the app. + connecting, ///< Establishing the stream transport. + first_frame, ///< Waiting for video to be presented. + }; + + constexpr int STREAM_STARTUP_STEP_COUNT = static_cast(StreamStartupStep::first_frame) + 1; ///< Number of segments in the startup bar. + /** * @brief Modal dialog view rendered on top of the current shell page. */ @@ -158,11 +171,13 @@ namespace ui { bool visible = false; ///< True when a modal dialog should be rendered. bool busy = false; ///< Show progress instead of dismissal controls while work is pending. bool folderBrowser = false; ///< Directory-dialog style; all static action dialogs share retained focus repainting. + bool compactBody = false; ///< Use closer line spacing for dense details such as stream statistics. std::string title; ///< Modal dialog title. std::string iconAssetPath; ///< Optional status icon beside the dialog title. std::vector lines; ///< Modal dialog body lines. std::vector actions; ///< Modal action rows. std::vector footerActions; ///< Footer actions displayed while a modal is open. + std::optional streamStartupStep; ///< Replaces the busy footer with a stepped progress bar. }; /** @@ -172,6 +187,15 @@ namespace ui { * @return True when every visible property except the focused action is unchanged. */ bool same_action_dialog_content(const ShellModalView &previous, const ShellModalView ¤t); + + /** + * @brief Build the busy dialog shown immediately after selecting a stream. + * @param host Selected PC. + * @param app Selected app on that PC. + * @param step Current startup milestone. + * @return Dialog identifying the stream that is being prepared. + */ + ShellModalView build_stream_launch_dialog(const app::HostRecord &host, const app::HostAppRecord &app, StreamStartupStep step = StreamStartupStep::preparing); /** * @brief Identify views whose visible contents animate without input or a completed task. * @param state Current shell and workflow state. diff --git a/tests/unit/app/client_state_test.cpp b/tests/unit/app/client_state_test.cpp index ca61c6d..d70c385 100644 --- a/tests/unit/app/client_state_test.cpp +++ b/tests/unit/app/client_state_test.cpp @@ -19,6 +19,38 @@ namespace { + /** + * @brief The summary accepts only the displayed confirmation and consumes it before it reaches the app grid. + */ + TEST(ClientStateTest, StreamSummaryRequiresExplicitConfirmation) { + auto state = app::create_initial_state(); + state.shell.activeScreen = app::ScreenId::apps; + state.apps.selectedAppIndex = 1U; + for (const auto device : {input::UiInputDevice::gamepad, input::UiInputDevice::keyboard}) { + state.shell.inputDevice = device; + state.shell.overlayVisible = true; + app::show_stream_summary(state, {"Stopped streaming Desktop"}); + EXPECT_FALSE(state.shell.overlayVisible); + const bool keyboard = device == input::UiInputDevice::keyboard; + const auto confirm = keyboard ? input::UiCommand::confirm : input::UiCommand::activate; + const auto other = keyboard ? input::UiCommand::activate : input::UiCommand::confirm; + for (const auto command : {input::UiCommand::back, input::UiCommand::move_left, input::UiCommand::toggle_overlay, other}) { + const auto update = app::handle_command(state, command); + EXPECT_EQ(state.modal.id, app::ModalId::stream_summary); + EXPECT_FALSE(state.shell.overlayVisible); + EXPECT_FALSE(update.requests.streamLaunchRequested); + EXPECT_FALSE(update.navigation.modalClosed); + } + const auto update = app::handle_command(state, confirm); + EXPECT_EQ(state.modal.id, app::ModalId::none); + EXPECT_TRUE(state.streamSummaryLines.empty()); + EXPECT_EQ(state.shell.activeScreen, app::ScreenId::apps); + EXPECT_EQ(state.apps.selectedAppIndex, 1U); + EXPECT_TRUE(update.navigation.modalClosed); + EXPECT_FALSE(update.requests.streamLaunchRequested); + } + } + /** * @brief Queue distinct events together without duplicating an outstanding notification. */ diff --git a/tests/unit/network/host_pairing_test.cpp b/tests/unit/network/host_pairing_test.cpp index bbd08a2..531a0e7 100644 --- a/tests/unit/network/host_pairing_test.cpp +++ b/tests/unit/network/host_pairing_test.cpp @@ -1245,6 +1245,35 @@ namespace { EXPECT_NE(errorMessage.find("network down"), std::string::npos); } + /** + * @brief Artwork yields to stream startup without starting or retrying a request after cancellation. + */ + TEST(HostPairingTest, QueryAppAssetStopsTryingPathsWhenCancelled) { + std::atomic cancelled {false}; + std::size_t callCount = 0U; + ScopedHostPairingHttpTestHandler guard([&](const HostPairingHttpTestRequest &, HostPairingHttpTestResponse *response, std::string *, const std::atomic *cancelRequested) { + EXPECT_EQ(cancelRequested, &cancelled); + ++callCount; + cancelled.store(true); + response->statusCode = 200; + response->body = std::string("\x89PNG", 4); + return true; + }); + + std::vector assetBytes; + std::string errorMessage; + const auto &address = test_support::kTestIpv4Addresses[test_support::kIpLivingRoom]; + EXPECT_FALSE(network::query_app_asset(address, 47990U, nullptr, 77, &assetBytes, &errorMessage, &cancelled)); + EXPECT_EQ(callCount, 1U); + EXPECT_TRUE(assetBytes.empty()); + EXPECT_EQ(errorMessage, "App artwork download cancelled"); + + EXPECT_FALSE(network::query_app_asset(address, 47990U, nullptr, 77, &assetBytes, &errorMessage, &cancelled)); + EXPECT_EQ(callCount, 1U); + EXPECT_TRUE(assetBytes.empty()); + EXPECT_EQ(errorMessage, "App artwork download cancelled"); + } + TEST(HostPairingTest, PairHostRejectsInvalidRequestsBeforeStartingTransport) { const network::PairingIdentity identity = network::create_pairing_identity(); ASSERT_TRUE(network::is_valid_pairing_identity(identity)); diff --git a/tests/unit/ui/shell_view_test.cpp b/tests/unit/ui/shell_view_test.cpp index 1302f44..92b54a2 100644 --- a/tests/unit/ui/shell_view_test.cpp +++ b/tests/unit/ui/shell_view_test.cpp @@ -491,6 +491,72 @@ namespace { EXPECT_EQ(view.keypad.lines.front(), state.addHostDraft.validationMessage); } + /** + * @brief Keep the app grid behind the summary and show the correct confirmation hint for each device. + */ + TEST(ShellViewTest, StreamSummaryOverlaysAppPickerWithConfirmAction) { + auto state = app::create_initial_state(); + state.shell.activeScreen = app::ScreenId::apps; + state.shell.inputDevice = input::UiInputDevice::gamepad; + state.hosts.activeLoaded = true; + state.hosts.active.pairingState = app::PairingState::paired; + state.hosts.active.appListState = app::HostAppListState::ready; + state.hosts.active.apps = {{"Desktop", 1}}; + const std::vector lines {"Stopped streaming Desktop", "GPU overlay: 12 frames | Software: 0 frames"}; + app::show_stream_summary(state, lines); + + auto view = ui::build_shell_view_model(state, {}); + EXPECT_EQ(view.frame.screen, app::ScreenId::apps); + ASSERT_EQ(view.content.appTiles.size(), 1U); + EXPECT_EQ(view.content.appTiles.front().name, "Desktop"); + EXPECT_TRUE(view.content.appTiles.front().selected); + EXPECT_TRUE(view.modal.visible); + EXPECT_FALSE(view.modal.busy); + EXPECT_TRUE(view.modal.compactBody); + EXPECT_EQ(view.modal.title, "Stream Summary"); + EXPECT_EQ(view.modal.lines, lines); + ASSERT_EQ(view.modal.footerActions.size(), 1U); + EXPECT_TRUE(view.modal.footerActions.front().emphasized); + EXPECT_EQ(view.modal.footerActions.front().label, "Confirm"); + EXPECT_EQ(view.modal.footerActions.front().iconAssetPath, "icons\\button-a.svg"); + + state.shell.inputDevice = input::UiInputDevice::keyboard; + view = ui::build_shell_view_model(state, {}); + ASSERT_EQ(view.modal.footerActions.size(), 1U); + EXPECT_EQ(view.modal.footerActions.front().keyLabel, "Enter"); + } + + /** + * @brief The startup modal keeps the selected app and advances through real launch milestones. + */ + TEST(ShellViewTest, StreamLaunchDialogShowsTheSelectedAppAndPc) { + app::HostRecord host {}; + host.displayName = "Living Room PC"; + app::HostAppRecord app {}; + app.name = "Desktop"; + + const std::array expected { + "Preparing to connect to Living Room PC...", + "Waiting for Living Room PC to start the stream...", + "Connecting to Living Room PC...", + "Waiting for the first video frame...", + }; + ASSERT_EQ(expected.size(), ui::STREAM_STARTUP_STEP_COUNT); + EXPECT_EQ(ui::build_stream_launch_dialog(host, app).streamStartupStep, ui::StreamStartupStep::preparing); + for (std::size_t index = 0; index < expected.size(); ++index) { + const auto step = static_cast(index); + const auto dialog = ui::build_stream_launch_dialog(host, app, step); + EXPECT_TRUE(dialog.visible); + EXPECT_TRUE(dialog.busy); + EXPECT_EQ(dialog.streamStartupStep, step); + EXPECT_EQ(dialog.title, "Starting Desktop"); + ASSERT_EQ(dialog.lines.size(), 1U); + EXPECT_EQ(dialog.lines.front(), expected[index]); + EXPECT_TRUE(dialog.actions.empty()); + EXPECT_TRUE(dialog.footerActions.empty()); + } + } + /** * @brief Show connection progress and result diagnostics with distinct status icons. */