Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

fix(wgc): address PR review feedback

tokenflow1m004fe4d19738
diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts
index a4bf779f..f859957d 100644
--- a/electron/electron-env.d.ts
+++ b/electron/electron-env.d.ts
@@ -35,6 +35,7 @@ interface Window {
capturesSystemAudio?: boolean
capturesMicrophone?: boolean
microphoneDeviceId?: string
+ microphoneLabel?: string
}
) => Promise<{ success: boolean; path?: string; message?: string; error?: string }>
stopNativeScreenRecording: () => Promise<{ success: boolean; path?: string; message?: string; error?: string }>
@@ -79,7 +80,7 @@ interface Window {
setHasUnsavedChanges: (hasChanges: boolean) => void
onRequestSaveBeforeClose: (callback: () => Promise<void>) => () => void
isWgcAvailable: () => Promise<{ available: boolean }>
- storeWgcAudio: (audioData: ArrayBuffer, type: 'system' | 'mic') => Promise<{ success: boolean; path?: string; error?: string }>
+ muxWgcRecording: () => Promise<{ success: boolean; path?: string; message?: string; error?: string }>
/** Hide the OS cursor before browser capture starts. */
hideOsCursor: () => Promise<{ success: boolean }>
}
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 690a1cf7..20b8119a 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -39,6 +39,7 @@ type NativeMacRecordingOptions = {
capturesSystemAudio?: boolean
capturesMicrophone?: boolean
microphoneDeviceId?: string
+ microphoneLabel?: string
}
type WindowBounds = {
@@ -66,6 +67,7 @@ let wgcScreenRecordingActive = false
let wgcCaptureStopRequested = false
let wgcSystemAudioPath: string | null = null
let wgcMicAudioPath: string | null = null
+let wgcPendingVideoPath: string | null = null
let ffmpegScreenRecordingActive = false
let ffmpegCaptureProcess: ChildProcessWithoutNullStreams | null = null
let ffmpegCaptureOutputBuffer = ''
@@ -92,6 +94,20 @@ export function getSelectedSourceId(): string | null {
return selectedSource?.id as string | null ?? null
}
+export function killWgcCaptureProcess() {
+ if (wgcCaptureProcess) {
+ try { wgcCaptureProcess.kill() } catch { /* ignore */ }
+ wgcCaptureProcess = null
+ wgcCaptureTargetPath = null
+ wgcScreenRecordingActive = false
+ nativeScreenRecordingActive = false
+ wgcCaptureStopRequested = false
+ wgcSystemAudioPath = null
+ wgcMicAudioPath = null
+ wgcPendingVideoPath = null
+ }
+}
+
function normalizePath(filePath: string) {
return path.resolve(filePath)
}
@@ -500,7 +516,7 @@ function getFfmpegBinaryPath() {
}
if (app.isPackaged) {
- return ffmpegStatic.replace('.asar/', '.asar.unpacked/')
+ return ffmpegStatic.replace(/\.asar([\/\\])/, '.asar.unpacked$1')
}
return ffmpegStatic
@@ -836,7 +852,7 @@ async function muxWgcVideoWithAudio(videoPath: string, systemAudioPath: string |
[
'-y',
...inputs,
- '-filter_complex', '[1:a][2:a]amix=inputs=2:duration=longest:normalize=0[aout]',
+ '-filter_complex', '[2:a]atrim=start=0.10,asetpts=PTS-STARTPTS[m];[1:a][m]amix=inputs=2:duration=longest:normalize=0[aout]',
'-map', '0:v:0',
'-map', '[aout]',
'-c:v', 'copy',
@@ -1710,23 +1726,47 @@ export function registerIpcHandlers(
try {
const exePath = getWgcCaptureExePath()
- const outputPath = path.join(RECORDINGS_DIR, `recording-${Date.now()}.mp4`)
- const screenId = Number(source?.display_id)
- const displayId = Number.isFinite(screenId) && screenId > 0
- ? screenId
- : Number(getScreen().getPrimaryDisplay().id)
+ const recordingsDir = await getRecordingsDir()
+ const timestamp = Date.now()
+ const outputPath = path.join(recordingsDir, `recording-${timestamp}.mp4`)
const config: Record<string, unknown> = {
- displayId,
outputPath,
fps: 60,
}
+ if (options?.capturesSystemAudio) {
+ const audioPath = path.join(recordingsDir, `recording-${timestamp}.system.wav`)
+ config.captureSystemAudio = true
+ config.audioOutputPath = audioPath
+ wgcSystemAudioPath = audioPath
+ }
+
+ if (options?.capturesMicrophone) {
+ const micPath = path.join(recordingsDir, `recording-${timestamp}.mic.wav`)
+ config.captureMic = true
+ config.micOutputPath = micPath
+ if (options.microphoneLabel) {
+ config.micDeviceName = options.microphoneLabel
+ }
+ wgcMicAudioPath = micPath
+ }
+
+ const windowId = parseWindowId(source?.id)
+ if (windowId && source?.id?.startsWith('window:')) {
+ config.windowHandle = windowId
+ } else {
+ const screenId = Number(source?.display_id)
+ config.displayId = Number.isFinite(screenId) && screenId > 0
+ ? screenId
+ : Number(getScreen().getPrimaryDisplay().id)
+ }
+
wgcCaptureOutputBuffer = ''
wgcCaptureTargetPath = outputPath
wgcCaptureStopRequested = false
wgcCaptureProcess = spawn(exePath, [JSON.stringify(config)], {
- cwd: RECORDINGS_DIR,
+ cwd: recordingsDir,
stdio: ['pipe', 'pipe', 'pipe'],
})
attachWgcCaptureLifecycle(wgcCaptureProcess)
@@ -1888,17 +1928,8 @@ export function registerIpcHandlers(
await moveFileWithOverwrite(tempVideoPath, finalVideoPath)
}
- if (wgcSystemAudioPath || wgcMicAudioPath) {
- try {
- await muxWgcVideoWithAudio(finalVideoPath, wgcSystemAudioPath, wgcMicAudioPath)
- } catch (muxError) {
- console.warn('Failed to mux WGC audio:', muxError)
- }
- wgcSystemAudioPath = null
- wgcMicAudioPath = null
- }
-
- return await finalizeStoredVideo(finalVideoPath)
+ wgcPendingVideoPath = finalVideoPath
+ return { success: true, path: finalVideoPath }
} catch (error) {
console.error('Failed to stop WGC capture:', error)
const fallbackPath = wgcCaptureTargetPath
@@ -1909,11 +1940,13 @@ export function registerIpcHandlers(
wgcCaptureStopRequested = false
wgcSystemAudioPath = null
wgcMicAudioPath = null
+ wgcPendingVideoPath = null
if (fallbackPath) {
try {
await fs.access(fallbackPath)
- return await finalizeStoredVideo(fallbackPath)
+ wgcPendingVideoPath = fallbackPath
+ return { success: true, path: fallbackPath }
} catch {
// File doesn't exist
}
@@ -2008,18 +2041,31 @@ export function registerIpcHandlers(
return { available: await isWgcCaptureAvailable() }
})
- ipcMain.handle('store-wgc-audio', async (_, audioData: ArrayBuffer, type: 'system' | 'mic') => {
+ ipcMain.handle('mux-wgc-recording', async () => {
+ const videoPath = wgcPendingVideoPath
+ wgcPendingVideoPath = null
+
+ if (!videoPath) {
+ return { success: false, message: 'No WGC video pending for mux' }
+ }
+
try {
- const audioPath = path.join(RECORDINGS_DIR, `recording-${Date.now()}.${type}.webm`)
- await fs.writeFile(audioPath, Buffer.from(audioData))
- if (type === 'system') {
- wgcSystemAudioPath = audioPath
- } else {
- wgcMicAudioPath = audioPath
+ if (wgcSystemAudioPath || wgcMicAudioPath) {
+ await muxWgcVideoWithAudio(videoPath, wgcSystemAudioPath, wgcMicAudioPath)
+ wgcSystemAudioPath = null
+ wgcMicAudioPath = null
}
- return { success: true, path: audioPath }
+
+ return await finalizeStoredVideo(videoPath)
} catch (error) {
- return { success: false, error: String(error) }
+ console.error('Failed to mux WGC recording:', error)
+ wgcSystemAudioPath = null
+ wgcMicAudioPath = null
+ try {
+ return await finalizeStoredVideo(videoPath)
+ } catch {
+ return { success: false, message: 'Failed to mux WGC recording', error: String(error) }
+ }
}
})
diff --git a/electron/main.ts b/electron/main.ts
index c7278878..d3ef0ed9 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -3,7 +3,7 @@ import { fileURLToPath } from 'node:url'
import path from 'node:path'
import fs from 'node:fs/promises'
import { createHudOverlayWindow, createEditorWindow, createSourceSelectorWindow } from './windows'
-import { registerIpcHandlers, getSelectedSourceId } from './ipc/handlers'
+import { registerIpcHandlers, getSelectedSourceId, killWgcCaptureProcess } from './ipc/handlers'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
@@ -310,6 +310,10 @@ function createSourceSelectorWindowWrapper() {
// On macOS, applications and their menu bar stay active until the user quits
// explicitly with Cmd + Q.
+app.on('before-quit', () => {
+ killWgcCaptureProcess()
+})
+
app.on('window-all-closed', () => {
// Keep app running (macOS behavior)
})
diff --git a/electron/native/wgc-capture/CMakeLists.txt b/electron/native/wgc-capture/CMakeLists.txt
index 418fbb8f..6b6c07ae 100644
--- a/electron/native/wgc-capture/CMakeLists.txt
+++ b/electron/native/wgc-capture/CMakeLists.txt
@@ -9,6 +9,7 @@ add_executable(wgc-capture
src/wgc_session.cpp
src/mf_encoder.cpp
src/monitor_utils.cpp
+ src/wasapi_loopback.cpp
)
target_compile_options(wgc-capture PRIVATE /EHsc /W3 /utf-8)
diff --git a/electron/native/wgc-capture/src/main.cpp b/electron/native/wgc-capture/src/main.cpp
index ac2063e4..3f5da69b 100644
--- a/electron/native/wgc-capture/src/main.cpp
+++ b/electron/native/wgc-capture/src/main.cpp
@@ -1,6 +1,7 @@
#include "wgc_session.h"
#include "mf_encoder.h"
#include "monitor_utils.h"
+#include "wasapi_loopback.h"
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.System.h>
@@ -18,10 +19,16 @@ static std::condition_variable g_stopCv;
struct CaptureConfig {
int displayId = 0;
+ int64_t windowHandle = 0;
std::string outputPath;
+ std::string audioOutputPath;
+ std::string micOutputPath;
+ std::string micDeviceName;
int fps = 60;
int width = 0;
int height = 0;
+ bool captureSystemAudio = false;
+ bool captureMic = false;
};
static bool parseSimpleJson(const std::string& json, CaptureConfig& config) {
@@ -39,6 +46,20 @@ static bool parseSimpleJson(const std::string& json, CaptureConfig& config) {
}
};
+ auto findInt64 = [&](const std::string& key) -> int64_t {
+ auto pos = json.find("\"" + key + "\"");
+ if (pos == std::string::npos) return -1;
+ pos = json.find(':', pos);
+ if (pos == std::string::npos) return -1;
+ pos++;
+ while (pos < json.size() && (json[pos] == ' ' || json[pos] == '\t')) pos++;
+ try {
+ return std::stoll(json.substr(pos));
+ } catch (...) {
+ return -1;
+ }
+ };
+
auto findString = [&](const std::string& key) -> std::string {
auto pos = json.find("\"" + key + "\"");
if (pos == std::string::npos) return "";
@@ -72,6 +93,9 @@ static bool parseSimpleJson(const std::string& json, CaptureConfig& config) {
int displayId = findInt("displayId");
if (displayId >= 0) config.displayId = displayId;
+ int64_t windowHandle = findInt64("windowHandle");
+ if (windowHandle > 0) config.windowHandle = windowHandle;
+
int fps = findInt("fps");
if (fps > 0) config.fps = fps;
@@ -81,6 +105,22 @@ static bool parseSimpleJson(const std::string& json, CaptureConfig& config) {
int height = findInt("height");
if (height > 0) config.height = height;
+ config.audioOutputPath = findString("audioOutputPath");
+ config.micOutputPath = findString("micOutputPath");
+ config.micDeviceName = findString("micDeviceName");
+
+ auto findBool = [&](const std::string& key) -> bool {
+ auto pos = json.find("\"" + key + "\"");
+ if (pos == std::string::npos) return false;
+ auto colonPos = json.find(':', pos);
+ if (colonPos == std::string::npos) return false;
+ auto valStart = json.find_first_not_of(" \t", colonPos + 1);
+ return valStart != std::string::npos && json.substr(valStart, 4) == "true";
+ };
+
+ config.captureSystemAudio = findBool("captureSystemAudio");
+ config.captureMic = findBool("captureMic");
+
return true;
}
@@ -126,18 +166,28 @@ int main(int argc, char* argv[]) {
return 1;
}
- // Resolve monitor
- HMONITOR monitor = findMonitorByDisplayId(config.displayId);
- if (!monitor) {
- std::cerr << "ERROR: Could not find monitor for displayId " << config.displayId << std::endl;
- return 1;
- }
-
- // Initialize WGC session
WgcSession session;
- if (!session.initialize(monitor, config.fps)) {
- std::cerr << "ERROR: Failed to initialize WGC capture session" << std::endl;
- return 1;
+
+ if (config.windowHandle > 0) {
+ HWND hwnd = reinterpret_cast<HWND>(static_cast<intptr_t>(config.windowHandle));
+ if (!IsWindow(hwnd)) {
+ std::cerr << "ERROR: Invalid window handle " << config.windowHandle << std::endl;
+ return 1;
+ }
+ if (!session.initialize(hwnd, config.fps)) {
+ std::cerr << "ERROR: Failed to initialize WGC window capture session" << std::endl;
+ return 1;
+ }
+ } else {
+ HMONITOR monitor = findMonitorByDisplayId(config.displayId);
+ if (!monitor) {
+ std::cerr << "ERROR: Could not find monitor for displayId " << config.displayId << std::endl;
+ return 1;
+ }
+ if (!session.initialize(monitor, config.fps)) {
+ std::cerr << "ERROR: Failed to initialize WGC capture session" << std::endl;
+ return 1;
+ }
}
int captureWidth = config.width > 0 ? config.width : session.captureWidth();
@@ -169,12 +219,41 @@ int main(int argc, char* argv[]) {
std::thread stdinThread(stdinListenerThread);
stdinThread.detach();
- // Start capture
+ // Initialize WASAPI captures (but don't start yet)
+ WasapiCapture loopback;
+ WasapiCapture micCapture;
+ bool audioActive = false;
+ bool audioInitialized = false;
+ bool micActive = false;
+ bool micInitialized = false;
+
+ if (config.captureSystemAudio && !config.audioOutputPath.empty()) {
+ audioInitialized = loopback.initializeLoopback(config.audioOutputPath);
+ if (!audioInitialized) {
+ std::cerr << "WARNING: Failed to initialize WASAPI loopback" << std::endl;
+ }
+ }
+
+ if (config.captureMic && !config.micOutputPath.empty()) {
+ micInitialized = micCapture.initializeMic(config.micOutputPath, config.micDeviceName);
+ if (!micInitialized) {
+ std::cerr << "WARNING: Failed to initialize WASAPI mic capture" << std::endl;
+ }
+ }
+
+ // Start video capture, then audio immediately after for sync
if (!session.startCapture()) {
std::cerr << "ERROR: Failed to start WGC capture" << std::endl;
return 1;
}
+ if (audioInitialized) {
+ audioActive = loopback.start();
+ }
+ if (micInitialized) {
+ micActive = micCapture.start();
+ }
+
std::cout << "Recording started" << std::endl;
std::cout.flush();
@@ -186,11 +265,22 @@ int main(int argc, char* argv[]) {
// Stop capture and finalize
session.stopCapture();
+ if (audioActive) loopback.stop();
+ if (micActive) micCapture.stop();
encoder.finalize();
std::cout << "Recording stopped. Output path: " << config.outputPath << std::endl;
+ if (audioActive) {
+ std::cout << "Audio path: " << config.audioOutputPath << std::endl;
+ }
+ if (micActive) {
+ std::cout << "Mic path: " << config.micOutputPath << std::endl;
+ }
std::cout.flush();
+ // Allow pipe buffers to drain before forceful exit
+ Sleep(100);
+
// Fast exit to avoid WinRT/COM teardown crashes during apartment cleanup
ExitProcess(0);
}
diff --git a/electron/native/wgc-capture/src/mf_encoder.cpp b/electron/native/wgc-capture/src/mf_encoder.cpp
index e0b74707..a1474c20 100644
--- a/electron/native/wgc-capture/src/mf_encoder.cpp
+++ b/electron/native/wgc-capture/src/mf_encoder.cpp
@@ -24,6 +24,11 @@ bool MFEncoder::initialize(const std::wstring& outputPath, int width, int height
ID3D11Device* device, ID3D11DeviceContext* context) {
if (initialized_) return false;
+ if (width % 2 != 0 || height % 2 != 0) {
+ std::cerr << "ERROR: Encoder dimensions must be even, got " << width << "x" << height << std::endl;
+ return false;
+ }
+
width_ = width;
height_ = height;
fps_ = fps;
diff --git a/electron/native/wgc-capture/src/wasapi_loopback.cpp b/electron/native/wgc-capture/src/wasapi_loopback.cpp
new file mode 100644
index 00000000..af9b5bc3
--- /dev/null
+++ b/electron/native/wgc-capture/src/wasapi_loopback.cpp
@@ -0,0 +1,242 @@
+#include "wasapi_loopback.h"
+#include <functiondiscoverykeys_devpkey.h>
+#include <iostream>
+#include <cstring>
+
+#pragma comment(lib, "ole32.lib")
+
+static const CLSID CLSID_MMDeviceEnumerator_ = __uuidof(MMDeviceEnumerator);
+static const IID IID_IMMDeviceEnumerator_ = __uuidof(IMMDeviceEnumerator);
+static const IID IID_IAudioClient_ = __uuidof(IAudioClient);
+static const IID IID_IAudioCaptureClient_ = __uuidof(IAudioCaptureClient);
+
+WasapiCapture::WasapiCapture() {}
+
+WasapiCapture::~WasapiCapture() {
+ stop();
+ if (mixFormat_) CoTaskMemFree(mixFormat_);
+ if (captureClient_) captureClient_->Release();
+ if (audioClient_) audioClient_->Release();
+ if (device_) device_->Release();
+ if (enumerator_) enumerator_->Release();
+}
+
+static std::wstring utf8ToWide(const std::string& str) {
+ if (str.empty()) return L"";
+ int len = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.size()), nullptr, 0);
+ std::wstring wstr(len, L'\0');
+ MultiByteToWideChar(CP_UTF8, 0, str.c_str(), static_cast<int>(str.size()), &wstr[0], len);
+ return wstr;
+}
+
+IMMDevice* WasapiCapture::findCaptureDeviceByName(const std::wstring& targetName) {
+ IMMDeviceCollection* collection = nullptr;
+ HRESULT hr = enumerator_->EnumAudioEndpoints(eCapture, DEVICE_STATE_ACTIVE, &collection);
+ if (FAILED(hr)) return nullptr;
+
+ UINT count = 0;
+ collection->GetCount(&count);
+
+ for (UINT i = 0; i < count; i++) {
+ IMMDevice* dev = nullptr;
+ collection->Item(i, &dev);
+
+ IPropertyStore* store = nullptr;
+ dev->OpenPropertyStore(STGM_READ, &store);
+ PROPVARIANT pv;
+ PropVariantInit(&pv);
+ store->GetValue(PKEY_Device_FriendlyName, &pv);
+ std::wstring name = pv.pwszVal ? pv.pwszVal : L"";
+ PropVariantClear(&pv);
+ store->Release();
+
+ if (name.find(targetName) != std::wstring::npos || targetName.find(name) != std::wstring::npos) {
+ collection->Release();
+ return dev;
+ }
+ dev->Release();
+ }
+
+ collection->Release();
+ return nullptr;
+}
+
+bool WasapiCapture::initializeLoopback(const std::string& outputPath) {
+ outputPath_ = outputPath;
+ streamFlags_ = AUDCLNT_STREAMFLAGS_LOOPBACK;
+
+ HRESULT hr = CoCreateInstance(
+ CLSID_MMDeviceEnumerator_, nullptr, CLSCTX_ALL,
+ IID_IMMDeviceEnumerator_, reinterpret_cast<void**>(&enumerator_));
+ if (FAILED(hr)) return false;
+
+ hr = enumerator_->GetDefaultAudioEndpoint(eRender, eConsole, &device_);
+ if (FAILED(hr)) return false;
+
+ return initializeCommon();
+}
+
+bool WasapiCapture::initializeMic(const std::string& outputPath, const std::string& deviceName) {
+ outputPath_ = outputPath;
+ streamFlags_ = 0;
+
+ HRESULT hr = CoCreateInstance(
+ CLSID_MMDeviceEnumerator_, nullptr, CLSCTX_ALL,
+ IID_IMMDeviceEnumerator_, reinterpret_cast<void**>(&enumerator_));
+ if (FAILED(hr)) return false;
+
+ if (!deviceName.empty()) {
+ device_ = findCaptureDeviceByName(utf8ToWide(deviceName));
+ }
+ if (!device_) {
+ hr = enumerator_->GetDefaultAudioEndpoint(eCapture, eCommunications, &device_);
+ if (FAILED(hr)) {
+ hr = enumerator_->GetDefaultAudioEndpoint(eCapture, eConsole, &device_);
+ if (FAILED(hr)) return false;
+ }
+ }
+
+ return initializeCommon();
+}
+
+bool WasapiCapture::initializeCommon() {
+ HRESULT hr = device_->Activate(IID_IAudioClient_, CLSCTX_ALL, nullptr,
+ reinterpret_cast<void**>(&audioClient_));
+ if (FAILED(hr)) return false;
+
+ hr = audioClient_->GetMixFormat(&mixFormat_);
+ if (FAILED(hr)) return false;
+
+ REFERENCE_TIME bufferDuration = 200000; // 20ms
+ hr = audioClient_->Initialize(
+ AUDCLNT_SHAREMODE_SHARED,
+ streamFlags_,
+ bufferDuration, 0, mixFormat_, nullptr);
+ if (FAILED(hr)) return false;
+
+ hr = audioClient_->GetBufferSize(&bufferFrameCount_);
+ if (FAILED(hr)) return false;
+
+ hr = audioClient_->GetService(IID_IAudioCaptureClient_,
+ reinterpret_cast<void**>(&captureClient_));
+ if (FAILED(hr)) return false;
+
+ return true;
+}
+
+bool WasapiCapture::start() {
+ HRESULT hr = audioClient_->Start();
+ if (FAILED(hr)) return false;
+
+ capturing_ = true;
+ thread_ = std::thread(&WasapiCapture::captureThread, this);
+ return true;
+}
+
+void WasapiCapture::stop() {
+ if (!capturing_) return;
+ capturing_ = false;
+ if (thread_.joinable()) thread_.join();
+ if (audioClient_) audioClient_->Stop();
+}
+
+static int16_t floatToInt16(float v) {
+ v = v < -1.0f ? -1.0f : (v > 1.0f ? 1.0f : v);
+ return static_cast<int16_t>(v * 32767.0f);
+}
+
+bool WasapiCapture::writeWavHeader(HANDLE file, DWORD dataSize) {
+ WORD channels = static_cast<WORD>(mixFormat_->nChannels);
+ DWORD sampleRate = mixFormat_->nSamplesPerSec;
+ WORD bitsPerSample = 16;
+ WORD blockAlign = channels * (bitsPerSample / 8);
+ DWORD byteRate = sampleRate * blockAlign;
+
+ DWORD written;
+ WriteFile(file, "RIFF", 4, &written, nullptr);
+ DWORD chunkSize = 36 + dataSize;
+ WriteFile(file, &chunkSize, 4, &written, nullptr);
+ WriteFile(file, "WAVE", 4, &written, nullptr);
+ WriteFile(file, "fmt ", 4, &written, nullptr);
+ DWORD fmtSize = 16;
+ WriteFile(file, &fmtSize, 4, &written, nullptr);
+ WORD audioFormat = 1;
+ WriteFile(file, &audioFormat, 2, &written, nullptr);
+ WriteFile(file, &channels, 2, &written, nullptr);
+ WriteFile(file, &sampleRate, 4, &written, nullptr);
+ WriteFile(file, &byteRate, 4, &written, nullptr);
+ WriteFile(file, &blockAlign, 2, &written, nullptr);
+ WriteFile(file, &bitsPerSample, 2, &written, nullptr);
+ WriteFile(file, "data", 4, &written, nullptr);
+ WriteFile(file, &dataSize, 4, &written, nullptr);
+ return true;
+}
+
+void WasapiCapture::captureThread() {
+ WORD channels = static_cast<WORD>(mixFormat_->nChannels);
+ bool isFloat = (mixFormat_->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) ||
+ (mixFormat_->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
+ reinterpret_cast<WAVEFORMATEXTENSIBLE*>(mixFormat_)->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT);
+
+ std::vector<int16_t> pcmBuffer;
+ HANDLE tmpFile = CreateFileA(
+ outputPath_.c_str(), GENERIC_WRITE, 0, nullptr,
+ CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
+
+ if (tmpFile == INVALID_HANDLE_VALUE) {
+ std::cerr << "ERROR: Cannot create audio output file" << std::endl;
+ return;
+ }
+
+ writeWavHeader(tmpFile, 0);
+ DWORD totalDataBytes = 0;
+
+ DWORD sleepMs = static_cast<DWORD>((static_cast<double>(bufferFrameCount_) / mixFormat_->nSamplesPerSec) * 500.0);
+ if (sleepMs < 5) sleepMs = 5;
+
+ while (capturing_) {
+ Sleep(sleepMs);
+
+ UINT32 packetLength = 0;
+ HRESULT hr = captureClient_->GetNextPacketSize(&packetLength);
+ if (FAILED(hr)) break;
+
+ while (packetLength > 0) {
+ BYTE* data = nullptr;
+ UINT32 numFrames = 0;
+ DWORD flags = 0;
+
+ hr = captureClient_->GetBuffer(&data, &numFrames, &flags, nullptr, nullptr);
+ if (FAILED(hr)) break;
+
+ UINT32 totalSamples = numFrames * channels;
+
+ if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
+ pcmBuffer.assign(totalSamples, 0);
+ } else if (isFloat) {
+ pcmBuffer.resize(totalSamples);
+ const float* src = reinterpret_cast<const float*>(data);
+ for (UINT32 i = 0; i < totalSamples; i++) {
+ pcmBuffer[i] = floatToInt16(src[i]);
+ }
+ } else {
+ pcmBuffer.resize(totalSamples);
+ std::memcpy(pcmBuffer.data(), data, totalSamples * sizeof(int16_t));
+ }
+
+ captureClient_->ReleaseBuffer(numFrames);
+
+ DWORD bytesToWrite = totalSamples * sizeof(int16_t);
+ DWORD written;
+ WriteFile(tmpFile, pcmBuffer.data(), bytesToWrite, &written, nullptr);
+ totalDataBytes += written;
+
+ hr = captureClient_->GetNextPacketSize(&packetLength);
+ if (FAILED(hr)) break;
+ }
+ }
+
+ SetFilePointer(tmpFile, 0, nullptr, FILE_BEGIN);
+ writeWavHeader(tmpFile, totalDataBytes);
+ CloseHandle(tmpFile);
+}
diff --git a/electron/native/wgc-capture/src/wasapi_loopback.h b/electron/native/wgc-capture/src/wasapi_loopback.h
new file mode 100644
index 00000000..3b2b76df
--- /dev/null
+++ b/electron/native/wgc-capture/src/wasapi_loopback.h
@@ -0,0 +1,39 @@
+#pragma once
+
+#include <windows.h>
+#include <mmdeviceapi.h>
+#include <audioclient.h>
+#include <string>
+#include <thread>
+#include <atomic>
+#include <vector>
+
+class WasapiCapture {
+public:
+ WasapiCapture();
+ ~WasapiCapture();
+
+ bool initializeLoopback(const std::string& outputPath);
+ bool initializeMic(const std::string& outputPath, const std::string& deviceName = "");
+ bool start();
+ void stop();
+
+private:
+ bool initializeCommon();
+ void captureThread();
+ bool writeWavHeader(HANDLE file, DWORD dataSize);
+ IMMDevice* findCaptureDeviceByName(const std::wstring& name);
+
+ std::string outputPath_;
+ std::thread thread_;
+ std::atomic<bool> capturing_{false};
+
+ IMMDeviceEnumerator* enumerator_ = nullptr;
+ IMMDevice* device_ = nullptr;
+ IAudioClient* audioClient_ = nullptr;
+ IAudioCaptureClient* captureClient_ = nullptr;
+ WAVEFORMATEX* mixFormat_ = nullptr;
+ DWORD streamFlags_ = 0;
+
+ UINT32 bufferFrameCount_ = 0;
+};
diff --git a/electron/native/wgc-capture/src/wgc_session.cpp b/electron/native/wgc-capture/src/wgc_session.cpp
index b00ece5f..7e59d140 100644
--- a/electron/native/wgc-capture/src/wgc_session.cpp
+++ b/electron/native/wgc-capture/src/wgc_session.cpp
@@ -88,19 +88,27 @@ winrt::Windows::Graphics::Capture::GraphicsCaptureItem WgcSession::createCapture
return item;
}
-bool WgcSession::initialize(HMONITOR monitor, int fps) {
- fps_ = fps;
- frameIntervalHns_ = 10000000LL / fps_;
+winrt::Windows::Graphics::Capture::GraphicsCaptureItem WgcSession::createCaptureItemForWindow(HWND hwnd) {
+ auto factory = winrt::get_activation_factory<
+ winrt::Windows::Graphics::Capture::GraphicsCaptureItem>();
- if (!createD3DDevice()) return false;
+ auto interop = factory.as<IGraphicsCaptureItemInterop>();
- winrtDevice_ = createWinRTDevice();
- if (!winrtDevice_) {
- std::cerr << "ERROR: Failed to create WinRT D3D device" << std::endl;
- return false;
+ winrt::Windows::Graphics::Capture::GraphicsCaptureItem item{nullptr};
+ HRESULT hr = interop->CreateForWindow(
+ hwnd,
+ winrt::guid_of<ABI::Windows::Graphics::Capture::IGraphicsCaptureItem>(),
+ winrt::put_abi(item));
+
+ if (FAILED(hr)) {
+ std::cerr << "ERROR: CreateForWindow failed: 0x" << std::hex << hr << std::endl;
+ return nullptr;
}
- captureItem_ = createCaptureItemForMonitor(monitor);
+ return item;
+}
+
+bool WgcSession::initializeWithItem(int fps) {
if (!captureItem_) return false;
auto size = captureItem_.Size();
@@ -121,6 +129,38 @@ bool WgcSession::initialize(HMONITOR monitor, int fps) {
return true;
}
+bool WgcSession::initialize(HMONITOR monitor, int fps) {
+ fps_ = fps;
+ frameIntervalHns_ = 10000000LL / fps_;
+
+ if (!createD3DDevice()) return false;
+
+ winrtDevice_ = createWinRTDevice();
+ if (!winrtDevice_) {
+ std::cerr << "ERROR: Failed to create WinRT D3D device" << std::endl;
+ return false;
+ }
+
+ captureItem_ = createCaptureItemForMonitor(monitor);
+ return initializeWithItem(fps);
+}
+
+bool WgcSession::initialize(HWND hwnd, int fps) {
+ fps_ = fps;
+ frameIntervalHns_ = 10000000LL / fps_;
+
+ if (!createD3DDevice()) return false;
+
+ winrtDevice_ = createWinRTDevice();
+ if (!winrtDevice_) {
+ std::cerr << "ERROR: Failed to create WinRT D3D device" << std::endl;
+ return false;
+ }
+
+ captureItem_ = createCaptureItemForWindow(hwnd);
+ return initializeWithItem(fps);
+}
+
void WgcSession::setFrameCallback(FrameCallback callback) {
frameCallback_ = std::move(callback);
}
diff --git a/electron/native/wgc-capture/src/wgc_session.h b/electron/native/wgc-capture/src/wgc_session.h
index 094d95d3..5ca6be34 100644
--- a/electron/native/wgc-capture/src/wgc_session.h
+++ b/electron/native/wgc-capture/src/wgc_session.h
@@ -24,6 +24,7 @@ public:
~WgcSession();
bool initialize(HMONITOR monitor, int fps);
+ bool initialize(HWND hwnd, int fps);
void setFrameCallback(FrameCallback callback);
bool startCapture();
void stopCapture();
@@ -53,6 +54,8 @@ private:
bool createD3DDevice();
winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice createWinRTDevice();
winrt::Windows::Graphics::Capture::GraphicsCaptureItem createCaptureItemForMonitor(HMONITOR monitor);
+ winrt::Windows::Graphics::Capture::GraphicsCaptureItem createCaptureItemForWindow(HWND hwnd);
+ bool initializeWithItem(int fps);
void onFrameArrived(
winrt::Windows::Graphics::Capture::Direct3D11CaptureFramePool const& sender,
winrt::Windows::Foundation::IInspectable const& args);
diff --git a/electron/preload.ts b/electron/preload.ts
index e8729f3f..ebc4fed1 100644
--- a/electron/preload.ts
+++ b/electron/preload.ts
@@ -35,6 +35,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
capturesSystemAudio?: boolean
capturesMicrophone?: boolean
microphoneDeviceId?: string
+ microphoneLabel?: string
},
) => {
return ipcRenderer.invoke('start-native-screen-recording', source, options)
@@ -178,7 +179,7 @@ contextBridge.exposeInMainWorld('electronAPI', {
return () => ipcRenderer.removeListener('request-save-before-close', listener)
},
isWgcAvailable: () => ipcRenderer.invoke('is-wgc-available'),
- storeWgcAudio: (audioData: ArrayBuffer, type: 'system' | 'mic') => ipcRenderer.invoke('store-wgc-audio', audioData, type),
+ muxWgcRecording: () => ipcRenderer.invoke('mux-wgc-recording'),
// Cursor visibility control for cursor-free browser capture fallback
hideOsCursor: () => ipcRenderer.invoke('hide-cursor'),
})
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index 7abc9561..53ae6720 100644
--- a/src/hooks/useScreenRecorder.ts
+++ b/src/hooks/useScreenRecorder.ts
@@ -54,8 +54,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
const chunks = useRef<Blob[]>([]);
const startTime = useRef<number>(0);
const nativeScreenRecording = useRef(false);
- const wgcAudioRecorders = useRef<MediaRecorder[]>([]);
- const wgcAudioChunks = useRef<Map<string, Blob[]>>(new Map());
+ const wgcRecording = useRef(false);
const startInFlight = useRef(false);
const hasPromptedForReselect = useRef(false);
@@ -128,95 +127,6 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
return Math.round(BITRATE_BASE * highFrameRateBoost);
};
- const stopWgcAudioCapture = useCallback(async () => {
- for (const recorder of wgcAudioRecorders.current) {
- if (recorder.state === "recording") {
- recorder.stop();
- }
- }
-
- // Wait briefly for onstop callbacks to fire
- await new Promise((resolve) => setTimeout(resolve, 200));
-
- for (const [type, chunks] of wgcAudioChunks.current.entries()) {
- if (chunks.length > 0) {
- const blob = new Blob(chunks, { type: "audio/webm" });
- const buffer = await blob.arrayBuffer();
- try {
- await window.electronAPI.storeWgcAudio(buffer, type as "system" | "mic");
- } catch (error) {
- console.warn(`Failed to store WGC ${type} audio:`, error);
- }
- }
- }
-
- wgcAudioRecorders.current = [];
- wgcAudioChunks.current = new Map();
- }, []);
-
- const startWgcAudioCapture = async (
- source: { id?: string; display_id?: string },
- captureSystemAudio: boolean,
- captureMicrophone: boolean,
- micDeviceId?: string,
- ) => {
- wgcAudioRecorders.current = [];
- wgcAudioChunks.current = new Map();
-
- if (captureSystemAudio) {
- try {
- const systemAudioStream = await (navigator.mediaDevices as any).getUserMedia({
- audio: {
- mandatory: {
- chromeMediaSource: CHROME_MEDIA_SOURCE,
- chromeMediaSourceId: source.id,
- },
- },
- video: false,
- });
-
- const systemChunks: Blob[] = [];
- wgcAudioChunks.current.set("system", systemChunks);
- const recorder = new MediaRecorder(systemAudioStream, {
- mimeType: "audio/webm",
- audioBitsPerSecond: AUDIO_BITRATE_SYSTEM,
- });
- recorder.ondataavailable = (e) => {
- if (e.data.size > 0) systemChunks.push(e.data);
- };
- recorder.start(RECORDER_TIMESLICE_MS);
- wgcAudioRecorders.current.push(recorder);
- } catch (error) {
- console.warn("WGC: System audio capture failed:", error);
- }
- }
-
- if (captureMicrophone) {
- try {
- const micStream = await navigator.mediaDevices.getUserMedia({
- audio: micDeviceId
- ? { deviceId: { exact: micDeviceId }, echoCancellation: true, noiseSuppression: true }
- : { echoCancellation: true, noiseSuppression: true },
- video: false,
- });
-
- const micChunks: Blob[] = [];
- wgcAudioChunks.current.set("mic", micChunks);
- const recorder = new MediaRecorder(micStream, {
- mimeType: "audio/webm",
- audioBitsPerSecond: AUDIO_BITRATE_VOICE,
- });
- recorder.ondataavailable = (e) => {
- if (e.data.size > 0) micChunks.push(e.data);
- };
- recorder.start(RECORDER_TIMESLICE_MS);
- wgcAudioRecorders.current.push(recorder);
- } catch (error) {
- console.warn("WGC: Microphone capture failed:", error);
- }
- }
- };
-
const cleanupCapturedMedia = useCallback(() => {
if (stream.current) {
stream.current.getTracks().forEach((track) => track.stop());
@@ -245,10 +155,8 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
setRecording(false);
void (async () => {
- // Stop WGC audio recorders first so audio data is stored before stop
- if (wgcAudioRecorders.current.length > 0) {
- await stopWgcAudioCapture();
- }
+ const isWgc = wgcRecording.current;
+ wgcRecording.current = false;
const result = await window.electronAPI.stopNativeScreenRecording();
window.electronAPI?.setRecordingState(false);
@@ -258,7 +166,14 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
return;
}
- await window.electronAPI.setCurrentVideoPath(result.path);
+ let finalPath = result.path;
+
+ if (isWgc) {
+ const muxResult = await window.electronAPI.muxWgcRecording();
+ finalPath = muxResult?.path ?? result.path;
+ }
+
+ await window.electronAPI.setCurrentVideoPath(finalPath);
await window.electronAPI.switchToEditor();
})();
return;
@@ -355,7 +270,7 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
let useWgcCapture = false;
if (
platform === "win32" &&
- selectedSource.id?.startsWith("screen:") &&
+ (selectedSource.id?.startsWith("screen:") || selectedSource.id?.startsWith("window:")) &&
typeof window.electronAPI.isWgcAvailable === "function"
) {
try {
@@ -367,10 +282,25 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
}
if (useNativeMacScreenCapture || useWgcCapture) {
+ // WGC: resolve mic device label for native WASAPI capture
+ let micLabel: string | undefined;
+ if (useWgcCapture && microphoneEnabled) {
+ try {
+ const devices = await navigator.mediaDevices.enumerateDevices();
+ const mic = devices.find(
+ (d) => d.deviceId === microphoneDeviceId && d.kind === "audioinput",
+ );
+ micLabel = mic?.label || undefined;
+ } catch {
+ // Fall through — native process will use default mic
+ }
+ }
+
const nativeResult = await window.electronAPI.startNativeScreenRecording(selectedSource, {
capturesSystemAudio: systemAudioEnabled,
capturesMicrophone: microphoneEnabled,
microphoneDeviceId,
+ microphoneLabel: micLabel,
});
if (!nativeResult.success) {
if (useWgcCapture) {
@@ -384,15 +314,11 @@ export function useScreenRecorder(): UseScreenRecorderReturn {
if (nativeResult.success) {
nativeScreenRecording.current = true;
+ wgcRecording.current = useWgcCapture;
startTime.current = Date.now();
setRecording(true);
window.electronAPI?.setRecordingState(true);
- // WGC: start audio-only MediaRecorders in parallel
- if (useWgcCapture && (systemAudioEnabled || microphoneEnabled)) {
- void startWgcAudioCapture(selectedSource, systemAudioEnabled, microphoneEnabled, microphoneDeviceId);
- }
-
return;
}
}
diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts
index f70eecbf..845f5960 100644
--- a/src/vite-env.d.ts
+++ b/src/vite-env.d.ts
@@ -40,6 +40,7 @@ interface Window {
capturesSystemAudio?: boolean
capturesMicrophone?: boolean
microphoneDeviceId?: string
+ microphoneLabel?: string
}) => Promise<{
success: boolean
path?: string