refactor: desktop capturer module (#14835)
* Revert "post all desktop capturer apis to worker threads"
This reverts commit 5a28759fea
.
* refactor: desktop capturer module
* Creates the screen and window capturer for the liftime of the app
* Fixes incorrect usage of weak ptr
* build: add //ui/snapshot to chromium_src deps
* fix: handle scenarios when there are no captured sources
This commit is contained in:
parent
e06bc311a9
commit
596ae2c2df
13 changed files with 465 additions and 829 deletions
|
@ -8,20 +8,17 @@
|
|||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using base::PlatformThreadRef;
|
||||
|
||||
#include "atom/common/api/atom_api_native_image.h"
|
||||
#include "atom/common/native_mate_converters/gfx_converter.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "base/task_scheduler/post_task.h"
|
||||
#include "base/threading/thread_task_runner_handle.h"
|
||||
#include "chrome/browser/media/desktop_media_list.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "base/threading/thread_restrictions.h"
|
||||
#include "chrome/browser/media/webrtc/desktop_media_list.h"
|
||||
#include "content/public/browser/desktop_capture.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h"
|
||||
#include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
#include "third_party/webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h"
|
||||
#include "third_party/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h"
|
||||
|
@ -51,63 +48,121 @@ struct Converter<atom::api::DesktopCapturer::Source> {
|
|||
|
||||
} // namespace mate
|
||||
|
||||
namespace {
|
||||
namespace atom {
|
||||
|
||||
void EmitFinished(
|
||||
const std::vector<atom::api::DesktopCapturer::Source>& sources,
|
||||
atom::api::DesktopCapturer* cap) {
|
||||
cap->Emit("finished", sources);
|
||||
namespace api {
|
||||
|
||||
DesktopCapturer::DesktopCapturer(v8::Isolate* isolate) {
|
||||
Init(isolate);
|
||||
}
|
||||
|
||||
void StartHandlingTask(bool capture_window,
|
||||
bool capture_screen,
|
||||
const gfx::Size& thumbnail_size,
|
||||
atom::api::DesktopCapturer* cap) {
|
||||
DesktopCapturer::~DesktopCapturer() {}
|
||||
|
||||
void DesktopCapturer::StartHandling(bool capture_window,
|
||||
bool capture_screen,
|
||||
const gfx::Size& thumbnail_size) {
|
||||
#if defined(OS_WIN)
|
||||
if (content::desktop_capture::CreateDesktopCaptureOptions()
|
||||
.allow_directx_capturer()) {
|
||||
// DxgiDuplicatorController should be alive in this scope according to
|
||||
// screen_capturer_win.cc.
|
||||
auto duplicator = webrtc::DxgiDuplicatorController::Instance();
|
||||
cap->using_directx_capturer_ =
|
||||
webrtc::ScreenCapturerWinDirectx::IsSupported();
|
||||
using_directx_capturer_ = webrtc::ScreenCapturerWinDirectx::IsSupported();
|
||||
}
|
||||
#endif // defined(OS_WIN)
|
||||
std::unique_ptr<webrtc::DesktopCapturer> screen_capturer(
|
||||
capture_screen ? content::desktop_capture::CreateScreenCapturer()
|
||||
: nullptr);
|
||||
std::unique_ptr<webrtc::DesktopCapturer> window_capturer(
|
||||
capture_window ? content::desktop_capture::CreateWindowCapturer()
|
||||
: nullptr);
|
||||
cap->media_list_.reset(new NativeDesktopMediaList(
|
||||
std::move(screen_capturer), std::move(window_capturer)));
|
||||
|
||||
cap->media_list_->SetThumbnailSize(thumbnail_size);
|
||||
cap->media_list_->StartUpdating(cap);
|
||||
// clear any existing captured sources.
|
||||
captured_sources_.clear();
|
||||
|
||||
// Start listening for captured sources.
|
||||
capture_window_ = capture_window;
|
||||
capture_screen_ = capture_screen;
|
||||
|
||||
{
|
||||
// Remove this once
|
||||
// https://bugs.chromium.org/p/chromium/issues/detail?id=795340 is fixed.
|
||||
base::ScopedAllowBaseSyncPrimitivesForTesting
|
||||
scoped_allow_base_sync_primitives;
|
||||
// Initialize the source list.
|
||||
// Apply the new thumbnail size and restart capture.
|
||||
if (capture_window) {
|
||||
window_capturer_.reset(new NativeDesktopMediaList(
|
||||
content::DesktopMediaID::TYPE_WINDOW,
|
||||
content::desktop_capture::CreateWindowCapturer()));
|
||||
window_capturer_->SetThumbnailSize(thumbnail_size);
|
||||
window_capturer_->AddObserver(this);
|
||||
window_capturer_->StartUpdating();
|
||||
}
|
||||
|
||||
if (capture_screen) {
|
||||
screen_capturer_.reset(new NativeDesktopMediaList(
|
||||
content::DesktopMediaID::TYPE_SCREEN,
|
||||
content::desktop_capture::CreateScreenCapturer()));
|
||||
screen_capturer_->SetThumbnailSize(thumbnail_size);
|
||||
screen_capturer_->AddObserver(this);
|
||||
screen_capturer_->StartUpdating();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnRefreshFinishedTask(atom::api::DesktopCapturer* cap) {
|
||||
const auto media_list_sources = cap->media_list_->GetSources();
|
||||
std::vector<atom::api::DesktopCapturer::Source> sources;
|
||||
for (const auto& media_list_source : media_list_sources) {
|
||||
sources.emplace_back(
|
||||
atom::api::DesktopCapturer::Source{media_list_source, std::string()});
|
||||
void DesktopCapturer::OnSourceAdded(DesktopMediaList* list, int index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceRemoved(DesktopMediaList* list, int index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceMoved(DesktopMediaList* list,
|
||||
int old_index,
|
||||
int new_index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceNameChanged(DesktopMediaList* list, int index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceThumbnailChanged(DesktopMediaList* list,
|
||||
int index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceUnchanged(DesktopMediaList* list) {
|
||||
UpdateSourcesList(list);
|
||||
}
|
||||
|
||||
bool DesktopCapturer::ShouldScheduleNextRefresh(DesktopMediaList* list) {
|
||||
UpdateSourcesList(list);
|
||||
return false;
|
||||
}
|
||||
|
||||
void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
|
||||
std::vector<DesktopCapturer::Source> window_sources;
|
||||
if (capture_window_ &&
|
||||
list->GetMediaListType() == content::DesktopMediaID::TYPE_WINDOW) {
|
||||
capture_window_ = false;
|
||||
const auto& media_list_sources = list->GetSources();
|
||||
for (const auto& media_list_source : media_list_sources) {
|
||||
window_sources.emplace_back(
|
||||
DesktopCapturer::Source{media_list_source, std::string()});
|
||||
}
|
||||
std::move(window_sources.begin(), window_sources.end(),
|
||||
std::back_inserter(captured_sources_));
|
||||
}
|
||||
|
||||
std::vector<DesktopCapturer::Source> screen_sources;
|
||||
if (capture_screen_ &&
|
||||
list->GetMediaListType() == content::DesktopMediaID::TYPE_SCREEN) {
|
||||
capture_screen_ = false;
|
||||
const auto& media_list_sources = list->GetSources();
|
||||
for (const auto& media_list_source : media_list_sources) {
|
||||
screen_sources.emplace_back(
|
||||
DesktopCapturer::Source{media_list_source, std::string()});
|
||||
}
|
||||
#if defined(OS_WIN)
|
||||
// Gather the same unique screen IDs used by the electron.screen API in order
|
||||
// to provide an association between it and desktopCapturer/getUserMedia.
|
||||
// This is only required when using the DirectX capturer, otherwise the IDs
|
||||
// across the APIs already match.
|
||||
if (cap->using_directx_capturer_) {
|
||||
std::vector<std::string> device_names;
|
||||
// Crucially, this list of device names will be in the same order as
|
||||
// |media_list_sources|.
|
||||
webrtc::DxgiDuplicatorController::Instance()->GetDeviceNames(&device_names);
|
||||
int device_name_index = 0;
|
||||
for (auto& source : sources) {
|
||||
if (source.media_list_source.id.type ==
|
||||
content::DesktopMediaID::TYPE_SCREEN) {
|
||||
// Gather the same unique screen IDs used by the electron.screen API in
|
||||
// order to provide an association between it and
|
||||
// desktopCapturer/getUserMedia. This is only required when using the
|
||||
// DirectX capturer, otherwise the IDs across the APIs already match.
|
||||
if (using_directx_capturer_) {
|
||||
std::vector<std::string> device_names;
|
||||
// Crucially, this list of device names will be in the same order as
|
||||
// |media_list_sources|.
|
||||
webrtc::DxgiDuplicatorController::Instance()->GetDeviceNames(
|
||||
&device_names);
|
||||
int device_name_index = 0;
|
||||
for (auto& source : screen_sources) {
|
||||
const auto& device_name = device_names[device_name_index++];
|
||||
std::wstring wide_device_name;
|
||||
base::UTF8ToWide(device_name.c_str(), device_name.size(),
|
||||
|
@ -118,61 +173,21 @@ void OnRefreshFinishedTask(atom::api::DesktopCapturer* cap) {
|
|||
source.display_id = base::Int64ToString(device_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
#elif defined(OS_MACOSX)
|
||||
// On Mac, the IDs across the APIs match.
|
||||
for (auto& source : sources) {
|
||||
if (source.media_list_source.id.type ==
|
||||
content::DesktopMediaID::TYPE_SCREEN) {
|
||||
// On Mac, the IDs across the APIs match.
|
||||
for (auto& source : screen_sources) {
|
||||
source.display_id = base::Int64ToString(source.media_list_source.id.id);
|
||||
}
|
||||
}
|
||||
#endif // defined(OS_WIN)
|
||||
// TODO(ajmacd): Add Linux support. The IDs across APIs differ but Chrome only
|
||||
// supports capturing the entire desktop on Linux. Revisit this if individual
|
||||
// screen support is added.
|
||||
// TODO(ajmacd): Add Linux support. The IDs across APIs differ but Chrome
|
||||
// only supports capturing the entire desktop on Linux. Revisit this if
|
||||
// individual screen support is added.
|
||||
std::move(screen_sources.begin(), screen_sources.end(),
|
||||
std::back_inserter(captured_sources_));
|
||||
}
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
|
||||
base::Bind(EmitFinished, sources, cap));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
DesktopCapturer::DesktopCapturer(v8::Isolate* isolate) {
|
||||
Init(isolate);
|
||||
capture_thread_ = base::CreateSequencedTaskRunnerWithTraits(
|
||||
{base::WithBaseSyncPrimitives(), base::MayBlock(),
|
||||
base::TaskPriority::USER_VISIBLE});
|
||||
}
|
||||
|
||||
DesktopCapturer::~DesktopCapturer() {}
|
||||
|
||||
void DesktopCapturer::StartHandling(bool capture_window,
|
||||
bool capture_screen,
|
||||
const gfx::Size& thumbnail_size) {
|
||||
capture_thread_->PostTask(
|
||||
FROM_HERE, base::BindOnce(StartHandlingTask, capture_window,
|
||||
capture_screen, thumbnail_size, this));
|
||||
}
|
||||
|
||||
void DesktopCapturer::OnSourceAdded(int index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceRemoved(int index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceMoved(int old_index, int new_index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceNameChanged(int index) {}
|
||||
|
||||
void DesktopCapturer::OnSourceThumbnailChanged(int index) {}
|
||||
|
||||
bool DesktopCapturer::OnRefreshFinished() {
|
||||
capture_thread_->PostTask(FROM_HERE,
|
||||
base::BindOnce(OnRefreshFinishedTask, this));
|
||||
return false;
|
||||
if (!capture_window_ && !capture_screen_)
|
||||
Emit("finished", captured_sources_);
|
||||
}
|
||||
|
||||
// static
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue