Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/streaming/ffmpeg_video_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include <utility>

// lib includes
#ifdef NXDK
extern "C" {
#include <libswscale/swscale.h>
#include <libswscale/swscale.h>
}

#ifdef NXDK
#include <hal/video.h>
#endif

Expand Down Expand Up @@ -127,7 +127,9 @@ namespace streaming {
}

void FfmpegVideoPresenter::reset() {
#ifdef NXDK
stop_video_presentation();
#endif
if (statisticsTexture_ != nullptr) {
SDL_DestroyTexture(statisticsTexture_);
statisticsTexture_ = nullptr;
Expand All @@ -142,22 +144,30 @@ namespace streaming {
}
textureWidth_ = 0;
textureHeight_ = 0;
#ifdef NXDK
sws_freeContext(presentScaleContext_);
presentScaleContext_ = nullptr;
renderFrame_.clear();
overlayFailed_ = false;
softwareScanoutFailed_ = false;
#endif
renderFrame_.clear();
}

void FfmpegVideoPresenter::configure_gpu_presentation(bool enabled, GpuBufferCount bufferCount) {
gpuPresentationEnabled_ = enabled;
#ifdef NXDK
presenter_.set_buffer_count(bufferCount);
#else
(void) bufferCount;
#endif
}

std::string FfmpegVideoPresenter::presentation_path() const {
#ifdef NXDK
if (presenter_.active()) {
return "GPU";
}
#endif
return acceleration_.softwareFrames > 0 ? "Software" : "Waiting";
}

Expand Down Expand Up @@ -232,6 +242,7 @@ namespace streaming {
}
}

#ifdef NXDK
void FfmpegVideoPresenter::stop_video_presentation() {
if (!presenter_.reset()) {
acceleration_.record_fallback("GPU overlay teardown timed out; acceleration disabled");
Expand All @@ -241,6 +252,7 @@ namespace streaming {
logging::warn("stream", "Software scanout retirement timed out; extra framebuffer retained");
}
}
#endif

bool FfmpegVideoPresenter::render_video_frame(SDL_Renderer *renderer, int screenWidth, int screenHeight, bool newFrame) {
const auto &frame = renderFrame_.view();
Expand Down Expand Up @@ -403,8 +415,8 @@ namespace streaming {
}
}

bool FfmpegVideoPresenter::render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight) {
#ifdef NXDK
bool FfmpegVideoPresenter::render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight) {
const VIDEO_MODE videoMode = XVideoGetMode();
const int bytesPerPixel = xbox_framebuffer_bytes_per_pixel(videoMode.bpp);
const AVPixelFormat destinationFormat = xbox_framebuffer_pixel_format(videoMode.bpp);
Expand Down Expand Up @@ -479,11 +491,7 @@ namespace streaming {
statisticsOverlay_.draw_rgb(framebuffer, framebufferSize, framebufferPitch, videoMode.width, videoMode.height, videoMode.bpp, videoArea);
saveIndicatorOverlay_.draw_rgb(framebuffer, framebufferSize, framebufferPitch, videoMode.width, videoMode.height, videoMode.bpp, videoArea);
return softwareScanout_.present();
#else
(void) screenWidth;
(void) screenHeight;
return false;
#endif
}
#endif

} // namespace streaming
54 changes: 31 additions & 23 deletions src/streaming/ffmpeg_video_presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@

// local includes
#include "src/streaming/ffmpeg_video_frame.h"
#include "src/streaming/pvideo_presenter.h"
#include "src/streaming/software_scanout.h"
#include "src/streaming/video_overlay.h"
#include "src/ui/save_indicator.h"

#ifdef NXDK
#include "src/streaming/pvideo_presenter.h"
#include "src/streaming/software_scanout.h"

struct SwsContext;
#endif

namespace streaming {
/**
Expand All @@ -43,7 +47,7 @@ namespace streaming {
FfmpegVideoPresenter &operator=(const FfmpegVideoPresenter &) = delete;

/**
* @brief Select the preferred presentation path and GPU buffer count for the new session.
* @brief Record the session's GPU preference and configure the NV2A buffer count on Xbox.
* @param enabled True to try NV2A with automatic software fallback.
* @param bufferCount Requested number of YUY2 buffers if NV2A is used.
*/
Expand Down Expand Up @@ -145,15 +149,10 @@ namespace streaming {
*/
bool present(SDL_Renderer *renderer, int screenWidth, int screenHeight, bool newFrame);

/** @brief Restore CPU framebuffer output and clear session-owned presentation resources. */
/** @brief Retire Xbox scanout when present and clear session-owned presentation resources. */
void reset();

private:
/**
* @brief Restore CPU framebuffer output before drawing shutdown or summary UI on the render thread.
*/
void stop_video_presentation();

/**
* @brief Present a frame on the first available output path.
* @param renderer SDL video renderer.
Expand All @@ -164,6 +163,12 @@ namespace streaming {
*/
bool render_video_frame(SDL_Renderer *renderer, int screenWidth, int screenHeight, bool newFrame);

#ifdef NXDK
/**
* @brief Restore Xbox CPU framebuffer output before drawing shutdown or summary UI on the render thread.
*/
void stop_video_presentation();

/**
* @brief Present the current video and UI through the GPU overlay when available.
*
Expand All @@ -175,6 +180,16 @@ namespace streaming {
*/
bool render_gpu_video_frame(const SDL_Rect &destination, bool newFrame);

/**
* @brief Present the latest decoded frame through Xbox direct framebuffer scanout.
*
* @param screenWidth Current framebuffer width.
* @param screenHeight Current framebuffer height.
* @return True when the frame was presented without SDL renderer involvement.
*/
bool render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight);
#endif

/**
* @brief Upload and present the current video and UI through SDL.
* @param renderer SDL video renderer.
Expand All @@ -196,23 +211,18 @@ namespace streaming {
*/
void invalidate_overlay_texture();

/**
* @brief Present the latest decoded frame through a platform-specific direct framebuffer path.
*
* @param screenWidth Current framebuffer width.
* @param screenHeight Current framebuffer height.
* @return True when the frame was presented without SDL renderer involvement.
*/
bool render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight);

VideoAccelerationReport &acceleration_; ///< Session diagnostics, owned by the backend.
FfmpegVideoFrame renderFrame_; ///< Frame retained for redraws and captures.
SwsContext *presentScaleContext_ = nullptr; ///< Software framebuffer conversion context.
SDL_Texture *texture_ = nullptr; ///< SDL fallback video texture.
int textureWidth_ = 0; ///< Width of the uploaded SDL video texture.
int textureHeight_ = 0; ///< Height of the uploaded SDL video texture.
PvideoPresenter presenter_; ///< Native GPU scanout.
SoftwareScanout softwareScanout_; ///< Native direct framebuffer scanout.
#ifdef NXDK
SwsContext *presentScaleContext_ = nullptr; ///< Xbox software framebuffer conversion context.
PvideoPresenter presenter_; ///< Xbox GPU scanout.
SoftwareScanout softwareScanout_; ///< Xbox direct framebuffer scanout.
bool overlayFailed_ = false; ///< Avoid retrying a failed GPU path in the same session.
bool softwareScanoutFailed_ = false; ///< Retain the SDL fallback after native software scanout fails.
#endif
VideoOverlay statisticsOverlay_; ///< Shared cached panel for all presentation paths.
VideoOverlay saveIndicatorOverlay_; ///< Independent corner overlay drawn above stream controls.
ui::SaveIndicator saveIndicator_; ///< Cached logo artwork for the save overlay.
Expand All @@ -222,7 +232,5 @@ namespace streaming {
bool statisticsTextureChanged_ = false; ///< SDL texture requires the current bitmap.
VideoRectangle statisticsTextureDamage_ {}; ///< Pending SDL texture upload.
bool gpuPresentationEnabled_ = false; ///< GPU preference for the session.
bool overlayFailed_ = false; ///< Avoid retrying a failed GPU path in the same session.
bool softwareScanoutFailed_ = false; ///< Retain the SDL fallback after native software scanout fails.
};
} // namespace streaming
19 changes: 1 addition & 18 deletions src/streaming/pvideo_presenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ namespace {
return static_cast<std::uint32_t>((static_cast<std::uint64_t>(source - 1) << 20U) / static_cast<unsigned int>(destination - 1));
}
} // namespace
#endif

namespace streaming {
PvideoPresenter::~PvideoPresenter() {
Expand All @@ -154,7 +153,6 @@ namespace streaming {
}

bool PvideoPresenter::initialize_scanout_surface(std::string *error) {
#ifdef NXDK
const VIDEO_MODE mode = XVideoGetMode();
const auto *framebuffer = XVideoGetFB();
const int pitch = mode.width * (mode.bpp / 8);
Expand Down Expand Up @@ -191,15 +189,10 @@ namespace streaming {
return false;
}
return true;
#else
*error = "GPU scanout is unavailable on this platform";
return false;
#endif
}

bool PvideoPresenter::reset() {
bool synchronized = true;
#ifdef NXDK
if (active_) {
VIDEOREG(VIDEO_STOP) = 1;
VIDEOREG(VIDEO_BUFFER) = 0;
Expand Down Expand Up @@ -242,15 +235,13 @@ namespace streaming {
XVideoSetVideoEnable(true);
scanoutStateSaved_ = false;
}
#endif
active_ = false;
bufferSize_ = 0;
bufferReuseVblank_ = 0;
nextBuffer_ = 0;
return synchronized;
}

#ifdef NXDK
void PvideoPresenter::configure_overlay_buffers(std::size_t required, int pitch, std::string &failure) {
if (!reset()) {
failure = "GPU overlay teardown timed out";
Expand Down Expand Up @@ -366,7 +357,6 @@ namespace streaming {
}
return true;
}
#endif

bool PvideoPresenter::present(const Yuv420FrameView &frame, const VideoRectangle &destination, std::string *error, const OverlaySamplingRegion &sampling, VideoOverlay *overlay, VideoOverlay *saveIndicator) {
std::string failure = overlay_frame_error(frame);
Expand All @@ -379,7 +369,6 @@ namespace streaming {
if (failure.empty() && (source.width < 2 || source.height < 2 || sampling.xHalfPel < 0 || sampling.yHalfPel < 0 || static_cast<std::int64_t>(sampling.xHalfPel) + 2LL * (source.width - 1) + (sampling.xHalfPel & 1) > 2LL * (frame.width - 1) || static_cast<std::int64_t>(sampling.yHalfPel) + 2LL * (source.height - 1) + (sampling.yHalfPel & 1) > 2LL * (frame.height - 1))) {
failure = "Source sampling region exceeds the GPU input buffer";
}
#ifdef NXDK
if (failure.empty() && overlay_quarantine().disabled) {
failure = "GPU overlay disabled after a scanout timeout";
}
Expand All @@ -401,17 +390,11 @@ namespace streaming {
}
failure = "Timed out waiting for the first GPU frame; using software presentation";
}
#else
(void) overlay;
(void) saveIndicator;
if (failure.empty()) {
failure = "GPU overlay is unavailable on this platform";
}
#endif
reset();
if (error != nullptr) {
*error = failure;
}
return false;
}
} // namespace streaming
#endif
16 changes: 8 additions & 8 deletions src/streaming/pvideo_presenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
*/
#pragma once

// local includes
#include "src/streaming/video_acceleration.h"
#include "src/streaming/video_overlay.h"
#ifdef NXDK
// local includes
#include "src/streaming/video_acceleration.h"
#include "src/streaming/video_overlay.h"

namespace streaming {
/**
Expand All @@ -20,7 +21,7 @@ namespace streaming {
};

/**
* @brief Main-thread-only NV2A YUV overlay presenter with a configurable spare buffer.
* @brief Main-thread-only Xbox NV2A YUV overlay presenter with a configurable spare buffer.
*
* Supports progressive limited-range BT.601/BT.709 YUV420 with a compatible
* 16-bit or 32-bit output mode. Callers select software presentation when present()
Expand Down Expand Up @@ -52,8 +53,8 @@ namespace streaming {
* @param destination Letterboxed output rectangle.
* @param error Receives the reason acceleration could not be used.
* @param sampling Optional crop used by the standalone prediction experiment.
* @param overlay Optional cached UI or statistics, composited using the frame's color matrix before GPU scanout.
* @param saveIndicator Optional save icon composited on top of the cached UI.
* @param overlay Optional UI or statistics whose YUY2 cache is refreshed as needed before compositing.
* @param saveIndicator Optional save icon whose YUY2 cache is refreshed as needed before compositing over the UI.
*
* @return True after the first overlay frame has latched or a subsequent frame has been submitted.
*/
Expand Down Expand Up @@ -93,7 +94,6 @@ namespace streaming {
*/
bool initialize_scanout_surface(std::string *error);

#ifdef NXDK
/**
* @brief Replace the scanout surface and allocate the required buffers plus a spare when requested.
*
Expand Down Expand Up @@ -129,7 +129,6 @@ namespace streaming {
* @return True if the submission succeeded or false if the first latch timed out.
*/
bool submit_overlay();
#endif

std::array<std::uint8_t *, 3> buffers_ {}; ///< Two contiguous write-combined YUY2 buffers and an optional spare.
GpuBufferCount requestedBufferCount_ = GpuBufferCount::automatic; ///< Preference used when configuring the DMA buffers.
Expand All @@ -145,3 +144,4 @@ namespace streaming {
bool surfaceActive_ = false; ///< pbkit owns the GPU context and vblank counter for the scanout surface.
};
} // namespace streaming
#endif
Loading
Loading