2015-10-02 09:50:10 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_desktop_capturer.h"
|
2015-10-02 09:50:10 +00:00
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2018-04-09 05:43:35 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "base/strings/string_number_conversions.h"
|
2015-10-02 09:50:10 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2018-10-03 12:26:42 +00:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
|
|
|
#include "chrome/browser/media/webrtc/desktop_media_list.h"
|
2018-12-04 06:42:49 +00:00
|
|
|
#include "chrome/browser/media/webrtc/window_icon_util.h"
|
2018-01-18 01:04:57 +00:00
|
|
|
#include "content/public/browser/desktop_capture.h"
|
2020-05-26 20:34:24 +00:00
|
|
|
#include "content/public/browser/desktop_streams_registry.h"
|
|
|
|
#include "content/public/browser/render_frame_host.h"
|
|
|
|
#include "content/public/browser/render_process_host.h"
|
2020-03-19 18:35:11 +00:00
|
|
|
#include "gin/object_template_builder.h"
|
2020-05-26 20:34:24 +00:00
|
|
|
#include "shell/browser/api/electron_api_web_contents.h"
|
2020-06-22 16:35:24 +00:00
|
|
|
#include "shell/browser/javascript_environment.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/common/api/electron_api_native_image.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "shell/common/gin_converters/gfx_converter.h"
|
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2020-03-19 18:35:11 +00:00
|
|
|
#include "shell/common/gin_helper/event_emitter_caller.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.h"
|
2015-10-02 09:50:10 +00:00
|
|
|
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h"
|
2017-01-23 09:47:30 +00:00
|
|
|
#include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
|
2020-05-26 20:34:24 +00:00
|
|
|
#include "url/origin.h"
|
2018-10-03 12:26:42 +00:00
|
|
|
|
2018-04-09 05:43:35 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include "third_party/webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h"
|
2018-07-03 08:03:05 +00:00
|
|
|
#include "third_party/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h"
|
2018-04-09 05:43:35 +00:00
|
|
|
#include "ui/display/win/display_info.h"
|
|
|
|
#endif // defined(OS_WIN)
|
2015-10-02 09:50:10 +00:00
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
namespace gin {
|
2015-10-06 06:34:54 +00:00
|
|
|
|
2018-04-09 05:43:35 +00:00
|
|
|
template <>
|
2019-06-19 21:23:04 +00:00
|
|
|
struct Converter<electron::api::DesktopCapturer::Source> {
|
2018-04-09 05:43:35 +00:00
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
2019-06-19 21:23:04 +00:00
|
|
|
const electron::api::DesktopCapturer::Source& source) {
|
2019-10-21 07:05:40 +00:00
|
|
|
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate);
|
2018-04-09 05:43:35 +00:00
|
|
|
content::DesktopMediaID id = source.media_list_source.id;
|
|
|
|
dict.Set("name", base::UTF16ToUTF8(source.media_list_source.name));
|
2015-10-06 06:34:54 +00:00
|
|
|
dict.Set("id", id.ToString());
|
2018-04-09 05:43:35 +00:00
|
|
|
dict.Set("thumbnail",
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::api::NativeImage::Create(
|
2018-04-09 05:43:35 +00:00
|
|
|
isolate, gfx::Image(source.media_list_source.thumbnail)));
|
|
|
|
dict.Set("display_id", source.display_id);
|
2018-12-04 06:42:49 +00:00
|
|
|
if (source.fetch_icon) {
|
|
|
|
dict.Set(
|
|
|
|
"appIcon",
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::api::NativeImage::Create(
|
2018-12-04 06:42:49 +00:00
|
|
|
isolate, gfx::Image(GetWindowIcon(source.media_list_source.id))));
|
2020-05-13 17:05:53 +00:00
|
|
|
} else {
|
|
|
|
dict.Set("appIcon", nullptr);
|
2018-12-04 06:42:49 +00:00
|
|
|
}
|
2015-10-06 06:34:54 +00:00
|
|
|
return ConvertToV8(isolate, dict);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
} // namespace gin
|
2015-10-06 06:34:54 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2015-10-02 09:50:10 +00:00
|
|
|
|
2018-10-03 12:26:42 +00:00
|
|
|
namespace api {
|
|
|
|
|
2020-03-19 18:35:11 +00:00
|
|
|
gin::WrapperInfo DesktopCapturer::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
|
|
|
DesktopCapturer::DesktopCapturer(v8::Isolate* isolate) {}
|
2015-10-04 01:35:00 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
DesktopCapturer::~DesktopCapturer() = default;
|
2018-10-03 12:26:42 +00:00
|
|
|
|
|
|
|
void DesktopCapturer::StartHandling(bool capture_window,
|
|
|
|
bool capture_screen,
|
2018-12-04 06:42:49 +00:00
|
|
|
const gfx::Size& thumbnail_size,
|
|
|
|
bool fetch_window_icons) {
|
|
|
|
fetch_window_icons_ = fetch_window_icons;
|
2018-04-09 05:43:35 +00:00
|
|
|
#if defined(OS_WIN)
|
2018-07-03 04:37:10 +00:00
|
|
|
if (content::desktop_capture::CreateDesktopCaptureOptions()
|
2018-09-19 11:10:26 +00:00
|
|
|
.allow_directx_capturer()) {
|
2018-07-03 04:37:10 +00:00
|
|
|
// DxgiDuplicatorController should be alive in this scope according to
|
|
|
|
// screen_capturer_win.cc.
|
2018-07-03 08:03:05 +00:00
|
|
|
auto duplicator = webrtc::DxgiDuplicatorController::Instance();
|
2018-10-03 12:26:42 +00:00
|
|
|
using_directx_capturer_ = webrtc::ScreenCapturerWinDirectx::IsSupported();
|
2018-07-03 04:37:10 +00:00
|
|
|
}
|
2018-04-09 05:43:35 +00:00
|
|
|
#endif // defined(OS_WIN)
|
2018-10-03 12:26:42 +00:00
|
|
|
|
|
|
|
// clear any existing captured sources.
|
|
|
|
captured_sources_.clear();
|
|
|
|
|
|
|
|
// Start listening for captured sources.
|
|
|
|
capture_window_ = capture_window;
|
|
|
|
capture_screen_ = capture_screen;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Initialize the source list.
|
|
|
|
// Apply the new thumbnail size and restart capture.
|
|
|
|
if (capture_window) {
|
2019-09-16 22:12:00 +00:00
|
|
|
window_capturer_ = std::make_unique<NativeDesktopMediaList>(
|
2018-10-03 12:26:42 +00:00
|
|
|
content::DesktopMediaID::TYPE_WINDOW,
|
2019-09-16 22:12:00 +00:00
|
|
|
content::desktop_capture::CreateWindowCapturer());
|
2018-10-03 12:26:42 +00:00
|
|
|
window_capturer_->SetThumbnailSize(thumbnail_size);
|
2019-10-28 22:12:35 +00:00
|
|
|
window_capturer_->AddObserver(this);
|
2019-10-18 19:57:34 +00:00
|
|
|
window_capturer_->Update(base::BindOnce(
|
|
|
|
&DesktopCapturer::UpdateSourcesList, weak_ptr_factory_.GetWeakPtr(),
|
|
|
|
window_capturer_.get()));
|
2018-10-03 12:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (capture_screen) {
|
2019-09-16 22:12:00 +00:00
|
|
|
screen_capturer_ = std::make_unique<NativeDesktopMediaList>(
|
2018-10-03 12:26:42 +00:00
|
|
|
content::DesktopMediaID::TYPE_SCREEN,
|
2019-09-16 22:12:00 +00:00
|
|
|
content::desktop_capture::CreateScreenCapturer());
|
2018-10-03 12:26:42 +00:00
|
|
|
screen_capturer_->SetThumbnailSize(thumbnail_size);
|
2019-10-28 22:12:35 +00:00
|
|
|
screen_capturer_->AddObserver(this);
|
2019-10-18 19:57:34 +00:00
|
|
|
screen_capturer_->Update(base::BindOnce(
|
|
|
|
&DesktopCapturer::UpdateSourcesList, weak_ptr_factory_.GetWeakPtr(),
|
|
|
|
screen_capturer_.get()));
|
2018-10-03 12:26:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
void DesktopCapturer::OnSourceUnchanged(DesktopMediaList* list) {
|
|
|
|
UpdateSourcesList(list);
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:26:42 +00:00
|
|
|
void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) {
|
|
|
|
if (capture_window_ &&
|
|
|
|
list->GetMediaListType() == content::DesktopMediaID::TYPE_WINDOW) {
|
|
|
|
capture_window_ = false;
|
|
|
|
const auto& media_list_sources = list->GetSources();
|
2019-01-25 14:39:32 +00:00
|
|
|
std::vector<DesktopCapturer::Source> window_sources;
|
|
|
|
window_sources.reserve(media_list_sources.size());
|
2018-10-03 12:26:42 +00:00
|
|
|
for (const auto& media_list_source : media_list_sources) {
|
2018-12-04 06:42:49 +00:00
|
|
|
window_sources.emplace_back(DesktopCapturer::Source{
|
|
|
|
media_list_source, std::string(), fetch_window_icons_});
|
2018-10-03 12:26:42 +00:00
|
|
|
}
|
|
|
|
std::move(window_sources.begin(), window_sources.end(),
|
|
|
|
std::back_inserter(captured_sources_));
|
2018-04-09 05:43:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 12:26:42 +00:00
|
|
|
if (capture_screen_ &&
|
|
|
|
list->GetMediaListType() == content::DesktopMediaID::TYPE_SCREEN) {
|
|
|
|
capture_screen_ = false;
|
|
|
|
const auto& media_list_sources = list->GetSources();
|
2019-01-25 14:39:32 +00:00
|
|
|
std::vector<DesktopCapturer::Source> screen_sources;
|
|
|
|
screen_sources.reserve(media_list_sources.size());
|
2018-10-03 12:26:42 +00:00
|
|
|
for (const auto& media_list_source : media_list_sources) {
|
|
|
|
screen_sources.emplace_back(
|
|
|
|
DesktopCapturer::Source{media_list_source, std::string()});
|
|
|
|
}
|
2018-04-09 05:43:35 +00:00
|
|
|
#if defined(OS_WIN)
|
2018-10-03 12:26:42 +00:00
|
|
|
// 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|.
|
2019-04-25 22:12:38 +00:00
|
|
|
if (!webrtc::DxgiDuplicatorController::Instance()->GetDeviceNames(
|
|
|
|
&device_names)) {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::HandleScope scope(isolate);
|
2020-03-19 18:35:11 +00:00
|
|
|
gin_helper::CallMethod(this, "_onerror", "Failed to get sources.");
|
2019-04-25 22:12:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:26:42 +00:00
|
|
|
int device_name_index = 0;
|
|
|
|
for (auto& source : screen_sources) {
|
2018-04-09 05:43:35 +00:00
|
|
|
const auto& device_name = device_names[device_name_index++];
|
|
|
|
std::wstring wide_device_name;
|
|
|
|
base::UTF8ToWide(device_name.c_str(), device_name.size(),
|
|
|
|
&wide_device_name);
|
|
|
|
const int64_t device_id =
|
|
|
|
display::win::DisplayInfo::DeviceIdFromDeviceName(
|
|
|
|
wide_device_name.c_str());
|
2019-04-20 17:20:37 +00:00
|
|
|
source.display_id = base::NumberToString(device_id);
|
2018-04-09 05:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#elif defined(OS_MACOSX)
|
2018-10-03 12:26:42 +00:00
|
|
|
// On Mac, the IDs across the APIs match.
|
|
|
|
for (auto& source : screen_sources) {
|
2019-04-20 17:20:37 +00:00
|
|
|
source.display_id = base::NumberToString(source.media_list_source.id.id);
|
2018-04-09 05:43:35 +00:00
|
|
|
}
|
|
|
|
#endif // defined(OS_WIN)
|
2018-10-03 12:26:42 +00:00
|
|
|
// 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_));
|
|
|
|
}
|
2018-06-19 01:37:53 +00:00
|
|
|
|
2020-04-02 23:07:56 +00:00
|
|
|
if (!capture_window_ && !capture_screen_) {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
|
2020-04-02 23:07:56 +00:00
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::HandleScope scope(isolate);
|
2020-05-13 17:05:53 +00:00
|
|
|
gin_helper::CallMethod(this, "_onfinished", captured_sources_);
|
2020-04-02 23:07:56 +00:00
|
|
|
}
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
// static
|
2019-10-21 07:05:40 +00:00
|
|
|
gin::Handle<DesktopCapturer> DesktopCapturer::Create(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, new DesktopCapturer(isolate));
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 20:34:24 +00:00
|
|
|
// static
|
|
|
|
std::string DesktopCapturer::GetMediaSourceIdForWebContents(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
gin_helper::ErrorThrower thrower,
|
|
|
|
int32_t request_web_contents_id,
|
|
|
|
int32_t web_contents_id) {
|
|
|
|
std::string id;
|
|
|
|
auto* web_contents = gin_helper::TrackableObject<WebContents>::FromWeakMapID(
|
|
|
|
isolate, web_contents_id);
|
|
|
|
|
|
|
|
if (!web_contents) {
|
|
|
|
thrower.ThrowError("Failed to find WebContents with id " +
|
|
|
|
std::to_string(web_contents_id));
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* main_frame = web_contents->web_contents()->GetMainFrame();
|
|
|
|
DCHECK(main_frame);
|
|
|
|
content::DesktopMediaID media_id(
|
|
|
|
content::DesktopMediaID::TYPE_WEB_CONTENTS,
|
|
|
|
content::DesktopMediaID::kNullId,
|
|
|
|
content::WebContentsMediaCaptureId(main_frame->GetProcess()->GetID(),
|
|
|
|
main_frame->GetRoutingID()));
|
|
|
|
|
|
|
|
auto* request_web_contents =
|
|
|
|
gin_helper::TrackableObject<WebContents>::FromWeakMapID(
|
|
|
|
isolate, request_web_contents_id);
|
|
|
|
if (request_web_contents) {
|
|
|
|
// comment copied from
|
|
|
|
// chrome/browser/extensions/api/desktop_capture/desktop_capture_base.cc
|
|
|
|
// TODO(miu): Once render_frame_host() is being set, we should register the
|
|
|
|
// exact RenderFrame requesting the stream, not the main RenderFrame. With
|
|
|
|
// that change, also update
|
|
|
|
// MediaCaptureDevicesDispatcher::ProcessDesktopCaptureAccessRequest().
|
|
|
|
// http://crbug.com/304341
|
|
|
|
auto* const request_main_frame =
|
|
|
|
request_web_contents->web_contents()->GetMainFrame();
|
|
|
|
DCHECK(request_main_frame);
|
|
|
|
id = content::DesktopStreamsRegistry::GetInstance()->RegisterStream(
|
|
|
|
request_main_frame->GetProcess()->GetID(),
|
|
|
|
request_main_frame->GetRoutingID(),
|
|
|
|
url::Origin::Create(
|
|
|
|
request_main_frame->GetLastCommittedURL().GetOrigin()),
|
|
|
|
media_id, "", content::kRegistryStreamTypeTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2020-03-19 18:35:11 +00:00
|
|
|
gin::ObjectTemplateBuilder DesktopCapturer::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin::Wrappable<DesktopCapturer>::GetObjectTemplateBuilder(isolate)
|
2016-04-25 01:17:54 +00:00
|
|
|
.SetMethod("startHandling", &DesktopCapturer::StartHandling);
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 18:35:11 +00:00
|
|
|
const char* DesktopCapturer::GetTypeName() {
|
|
|
|
return "DesktopCapturer";
|
|
|
|
}
|
|
|
|
|
2015-10-02 09:50:10 +00:00
|
|
|
} // namespace api
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|
2015-10-02 09:50:10 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2019-10-21 07:05:40 +00:00
|
|
|
gin_helper::Dictionary dict(context->GetIsolate(), exports);
|
2019-06-19 21:23:04 +00:00
|
|
|
dict.SetMethod("createDesktopCapturer",
|
|
|
|
&electron::api::DesktopCapturer::Create);
|
2020-05-26 20:34:24 +00:00
|
|
|
dict.SetMethod(
|
|
|
|
"getMediaSourceIdForWebContents",
|
|
|
|
&electron::api::DesktopCapturer::GetMediaSourceIdForWebContents);
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-14 11:25:39 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_desktop_capturer, Initialize)
|