fix: `desktopCapturer` and `screen` display ids should match (#42883)

* fix: `desktopCapturer` and `screen` display IDs should match

* simplify wide-to-utf8 conversion

* remove unnecessary include
This commit is contained in:
Calvin 2024-07-14 00:51:57 -07:00 коммит произвёл GitHub
Родитель 3a6a201534
Коммит 493bc3ac9d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 24 добавлений и 16 удалений

Просмотреть файл

@ -399,28 +399,37 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
if (using_directx_capturer_) { if (using_directx_capturer_) {
std::vector<std::string> device_names; std::vector<std::string> device_names;
// Crucially, this list of device names will be in the same order as // Crucially, this list of device names will be in the same order as
// |media_list_sources|. // |screen_sources|.
if (!webrtc::DxgiDuplicatorController::Instance()->GetDeviceNames( if (!webrtc::DxgiDuplicatorController::Instance()->GetDeviceNames(
&device_names)) { &device_names)) {
HandleFailure(); HandleFailure();
return; return;
} }
DCHECK_EQ(device_names.size(), screen_sources.size());
std::vector<HMONITOR> monitors; std::vector<HMONITOR> monitors;
EnumDisplayMonitors(nullptr, nullptr, EnumDisplayMonitorsCallback, EnumDisplayMonitors(nullptr, nullptr, EnumDisplayMonitorsCallback,
reinterpret_cast<LPARAM>(&monitors)); reinterpret_cast<LPARAM>(&monitors));
std::vector<std::pair<std::string, MONITORINFOEX>> pairs; base::flat_map<std::string, int64_t> device_name_to_id;
for (const auto& device_name : device_names) { device_name_to_id.reserve(monitors.size());
std::wstring wide_device_name; for (auto* monitor : monitors) {
base::UTF8ToWide(device_name.c_str(), device_name.size(), MONITORINFOEX monitorInfo{{sizeof(MONITORINFOEX)}};
&wide_device_name); if (!GetMonitorInfo(monitor, &monitorInfo)) {
for (const auto monitor : monitors) { continue;
MONITORINFOEX monitorInfo{{sizeof(MONITORINFOEX)}}; }
if (GetMonitorInfo(monitor, &monitorInfo)) {
if (wide_device_name == monitorInfo.szDevice) device_name_to_id[base::WideToUTF8(monitorInfo.szDevice)] =
pairs.push_back(std::make_pair(device_name, monitorInfo)); display::win::internal::DisplayInfo::DisplayIdFromMonitorInfo(
} monitorInfo);
}
int device_name_index = 0;
for (auto& source : screen_sources) {
const auto& device_name = device_names[device_name_index++];
if (auto id_iter = device_name_to_id.find(device_name);
id_iter != device_name_to_id.end()) {
source.display_id = base::NumberToString(id_iter->second);
} }
} }
} }

Просмотреть файл

@ -1,6 +1,5 @@
import { expect } from 'chai'; import { expect } from 'chai';
import { Display, screen, desktopCapturer } from 'electron/main'; import { Display, screen, desktopCapturer } from 'electron/main';
import { ifit } from './lib/spec-helpers';
describe('screen module', () => { describe('screen module', () => {
describe('methods reassignment', () => { describe('methods reassignment', () => {
@ -24,14 +23,14 @@ describe('screen module', () => {
} }
}); });
// desktopCapturer.getSources does not work as expected in Windows CI. it('returns displays with IDs matching desktopCapturer source display IDs', async () => {
ifit(process.platform !== 'win32')('returns displays with IDs matching desktopCapturer source display IDs', async () => {
const displayIds = screen.getAllDisplays().map(d => `${d.id}`); const displayIds = screen.getAllDisplays().map(d => `${d.id}`);
const sources = await desktopCapturer.getSources({ types: ['screen'] }); const sources = await desktopCapturer.getSources({ types: ['screen'] });
const sourceIds = sources.map(s => s.display_id); const sourceIds = sources.map(s => s.display_id);
expect(displayIds).to.have.members(sourceIds); expect(displayIds).to.have.length(sources.length);
expect(displayIds).to.have.same.members(sourceIds);
}); });
}); });