|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "dxgi_duplication_session.h" |
| 4 | +#include "wgc_session.h" |
| 5 | + |
| 6 | +#include <cctype> |
| 7 | +#include <cstdlib> |
| 8 | +#include <cstring> |
| 9 | +#include <iostream> |
| 10 | +#include <string> |
| 11 | +#include <utility> |
| 12 | + |
| 13 | +// One frame source, two backends, chosen at initialize() time. |
| 14 | +// |
| 15 | +// main.cpp used to talk to WgcSession directly. It now talks to this instead, |
| 16 | +// so the choice between WGC and DXGI Desktop Duplication lives in one place and |
| 17 | +// every downstream stage -- the encoder, the MP4 muxer, audio, webcam, the |
| 18 | +// cursor sampler, the JSON event protocol -- is unchanged and unaware. |
| 19 | +// |
| 20 | +// The fallback exists because CreateForMonitor can be refused with |
| 21 | +// E_ACCESSDENIED on a machine that captures perfectly well otherwise; see |
| 22 | +// dxgi_duplication_session.h for the measurements. WGC is still tried first |
| 23 | +// everywhere, so a machine where it works keeps the backend it has always had. |
| 24 | +class CaptureSession { |
| 25 | +public: |
| 26 | + using FrameCallback = WgcSession::FrameCallback; |
| 27 | + |
| 28 | + // OPENSCREEN_CAPTURE_BACKEND=dxgi force Desktop Duplication (diagnostics) |
| 29 | + // =wgc force WGC, no fallback (diagnostics) |
| 30 | + // unset try WGC, fall back to DXGI |
| 31 | + static std::string requestedBackend() { |
| 32 | + const char* raw = std::getenv("OPENSCREEN_CAPTURE_BACKEND"); |
| 33 | + if (!raw) { |
| 34 | + return {}; |
| 35 | + } |
| 36 | + std::string value(raw); |
| 37 | + for (char& c : value) { |
| 38 | + c = static_cast<char>(std::tolower(static_cast<unsigned char>(c))); |
| 39 | + } |
| 40 | + return value; |
| 41 | + } |
| 42 | + |
| 43 | + bool initialize(HMONITOR monitor, int fps, bool captureCursor) { |
| 44 | + const std::string requested = requestedBackend(); |
| 45 | + if (requested == "dxgi") { |
| 46 | + return startDxgi(monitor, fps, captureCursor, "requested"); |
| 47 | + } |
| 48 | + if (wgc_.initialize(monitor, fps, captureCursor)) { |
| 49 | + return adoptWgc(); |
| 50 | + } |
| 51 | + wgcUnavailable_ = true; |
| 52 | + if (requested == "wgc") { |
| 53 | + return false; |
| 54 | + } |
| 55 | + wgc_.stop(); |
| 56 | + return startDxgi(monitor, fps, captureCursor, "wgc-unavailable"); |
| 57 | + } |
| 58 | + |
| 59 | + bool initialize(HWND window, int fps, bool captureCursor, bool includeSecondaryWindows = false) { |
| 60 | + // Desktop Duplication duplicates an output, never a single window, so a |
| 61 | + // window take has no fallback: it is WGC or it is a failure. |
| 62 | + if (wgc_.initialize(window, fps, captureCursor, includeSecondaryWindows)) { |
| 63 | + return adoptWgc(); |
| 64 | + } |
| 65 | + wgcUnavailable_ = true; |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + bool start() { |
| 70 | + switch (backend_) { |
| 71 | + case Backend::Wgc: |
| 72 | + return wgc_.start(); |
| 73 | + case Backend::Dxgi: |
| 74 | + return dxgi_.start(); |
| 75 | + default: |
| 76 | + return false; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + bool tryGetNextFrame(ID3D11Texture2D** outTexture, int64_t* outTimestampHns) { |
| 81 | + switch (backend_) { |
| 82 | + case Backend::Wgc: |
| 83 | + return wgc_.tryGetNextFrame(outTexture, outTimestampHns); |
| 84 | + case Backend::Dxgi: |
| 85 | + return dxgi_.tryGetNextFrame(outTexture, outTimestampHns); |
| 86 | + default: |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + void setFrameCallback(FrameCallback callback) { |
| 92 | + if (backend_ == Backend::Wgc) { |
| 93 | + wgc_.setFrameCallback(std::move(callback)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + bool quiesceLegacyCallback(int drainTimeoutMs = 5000) { |
| 98 | + if (backend_ == Backend::Wgc) { |
| 99 | + return wgc_.quiesceLegacyCallback(drainTimeoutMs); |
| 100 | + } |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + void stop() { |
| 105 | + wgc_.stop(); |
| 106 | + dxgi_.stop(); |
| 107 | + backend_ = Backend::None; |
| 108 | + } |
| 109 | + |
| 110 | + int captureWidth() const { |
| 111 | + return backend_ == Backend::Dxgi ? dxgi_.captureWidth() : wgc_.captureWidth(); |
| 112 | + } |
| 113 | + |
| 114 | + int captureHeight() const { |
| 115 | + return backend_ == Backend::Dxgi ? dxgi_.captureHeight() : wgc_.captureHeight(); |
| 116 | + } |
| 117 | + |
| 118 | + ID3D11Device* device() const { |
| 119 | + return backend_ == Backend::Dxgi ? dxgi_.device() : wgc_.device(); |
| 120 | + } |
| 121 | + |
| 122 | + ID3D11DeviceContext* context() const { |
| 123 | + return backend_ == Backend::Dxgi ? dxgi_.context() : wgc_.context(); |
| 124 | + } |
| 125 | + |
| 126 | + bool usingDxgi() const { return backend_ == Backend::Dxgi; } |
| 127 | + bool wgcUnavailable() const { return wgcUnavailable_; } |
| 128 | + |
| 129 | + const char* backendName() const { |
| 130 | + switch (backend_) { |
| 131 | + case Backend::Wgc: |
| 132 | + return "wgc"; |
| 133 | + case Backend::Dxgi: |
| 134 | + return "dxgi-duplication"; |
| 135 | + default: |
| 136 | + return "none"; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +private: |
| 141 | + enum class Backend { None, Wgc, Dxgi }; |
| 142 | + |
| 143 | + bool adoptWgc() { |
| 144 | + backend_ = Backend::Wgc; |
| 145 | + std::cout << "{\"event\":\"capture-backend\",\"backend\":\"wgc\"}" << std::endl; |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + bool startDxgi(HMONITOR monitor, int fps, bool captureCursor, const char* reason) { |
| 150 | + if (!dxgi_.initialize(monitor, fps, captureCursor)) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + backend_ = Backend::Dxgi; |
| 154 | + // Printed rather than only logged: which backend ran is a per-machine |
| 155 | + // outcome the app reports next to encoder-selection, so a future report |
| 156 | + // says which one produced the file without anyone having to guess. |
| 157 | + std::cout << "{\"event\":\"capture-backend\",\"backend\":\"dxgi-duplication\",\"reason\":\"" |
| 158 | + << reason << "\"}" << std::endl; |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + Backend backend_ = Backend::None; |
| 163 | + WgcSession wgc_; |
| 164 | + DxgiDuplicationSession dxgi_; |
| 165 | + bool wgcUnavailable_ = false; |
| 166 | +}; |
0 commit comments