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.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_desktop_capturer.h"
|
|
|
|
|
2018-09-13 00:25:56 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2018-04-09 05:43:35 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2017-04-04 04:50:44 +00:00
|
|
|
using base::PlatformThreadRef;
|
|
|
|
|
2015-10-02 09:50:10 +00:00
|
|
|
#include "atom/common/api/atom_api_native_image.h"
|
2015-10-06 06:34:54 +00:00
|
|
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
2018-04-09 05:43:35 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2015-10-02 09:50:10 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2018-06-19 01:37:53 +00:00
|
|
|
#include "base/task_scheduler/post_task.h"
|
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2015-10-02 09:50:10 +00:00
|
|
|
#include "chrome/browser/media/desktop_media_list.h"
|
2018-06-19 01:37:53 +00:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
2018-01-18 01:04:57 +00:00
|
|
|
#include "content/public/browser/desktop_capture.h"
|
2015-10-02 09:50:10 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
#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"
|
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
|
|
|
|
2016-03-08 14:28:53 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
2015-10-06 06:34:54 +00:00
|
|
|
namespace mate {
|
|
|
|
|
2018-04-09 05:43:35 +00:00
|
|
|
template <>
|
|
|
|
struct Converter<atom::api::DesktopCapturer::Source> {
|
|
|
|
static v8::Local<v8::Value> ToV8(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
const atom::api::DesktopCapturer::Source& source) {
|
2015-10-06 06:34:54 +00:00
|
|
|
mate::Dictionary dict(isolate, v8::Object::New(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",
|
|
|
|
atom::api::NativeImage::Create(
|
|
|
|
isolate, gfx::Image(source.media_list_source.thumbnail)));
|
|
|
|
dict.Set("display_id", source.display_id);
|
2015-10-06 06:34:54 +00:00
|
|
|
return ConvertToV8(isolate, dict);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mate
|
|
|
|
|
2018-06-19 01:37:53 +00:00
|
|
|
namespace {
|
2015-10-02 09:50:10 +00:00
|
|
|
|
2018-06-19 01:37:53 +00:00
|
|
|
void EmitFinished(
|
|
|
|
const std::vector<atom::api::DesktopCapturer::Source>& sources,
|
|
|
|
atom::api::DesktopCapturer* cap) {
|
|
|
|
cap->Emit("finished", sources);
|
2015-10-04 01:35:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 01:37:53 +00:00
|
|
|
void StartHandlingTask(bool capture_window,
|
|
|
|
bool capture_screen,
|
|
|
|
const gfx::Size& thumbnail_size,
|
|
|
|
atom::api::DesktopCapturer* cap) {
|
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();
|
|
|
|
cap->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)
|
2017-01-23 09:47:30 +00:00
|
|
|
std::unique_ptr<webrtc::DesktopCapturer> screen_capturer(
|
2018-05-11 18:57:09 +00:00
|
|
|
capture_screen ? content::desktop_capture::CreateScreenCapturer()
|
2017-01-23 09:47:30 +00:00
|
|
|
: nullptr);
|
|
|
|
std::unique_ptr<webrtc::DesktopCapturer> window_capturer(
|
2018-07-17 04:20:50 +00:00
|
|
|
capture_window ? content::desktop_capture::CreateWindowCapturer()
|
2017-01-23 09:47:30 +00:00
|
|
|
: nullptr);
|
2018-06-19 01:37:53 +00:00
|
|
|
cap->media_list_.reset(new NativeDesktopMediaList(
|
|
|
|
std::move(screen_capturer), std::move(window_capturer)));
|
2015-10-05 03:32:12 +00:00
|
|
|
|
2018-06-19 01:37:53 +00:00
|
|
|
cap->media_list_->SetThumbnailSize(thumbnail_size);
|
|
|
|
cap->media_list_->StartUpdating(cap);
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 01:37:53 +00:00
|
|
|
void OnRefreshFinishedTask(atom::api::DesktopCapturer* cap) {
|
|
|
|
const auto media_list_sources = cap->media_list_->GetSources();
|
|
|
|
std::vector<atom::api::DesktopCapturer::Source> sources;
|
2018-04-09 05:43:35 +00:00
|
|
|
for (const auto& media_list_source : media_list_sources) {
|
|
|
|
sources.emplace_back(
|
2018-06-19 01:37:53 +00:00
|
|
|
atom::api::DesktopCapturer::Source{media_list_source, std::string()});
|
2018-04-09 05:43:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#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.
|
2018-06-19 05:28:51 +00:00
|
|
|
if (cap->using_directx_capturer_) {
|
2018-04-09 05:43:35 +00:00
|
|
|
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) {
|
|
|
|
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());
|
|
|
|
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) {
|
2018-04-18 01:55:30 +00:00
|
|
|
source.display_id = base::Int64ToString(source.media_list_source.id.id);
|
2018-04-09 05:43:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // defined(OS_WIN)
|
2018-04-18 01:55:30 +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.
|
2018-04-09 05:43:35 +00:00
|
|
|
|
2018-06-19 01:37:53 +00:00
|
|
|
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));
|
2015-10-06 06:34:54 +00:00
|
|
|
return false;
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
// static
|
|
|
|
mate::Handle<DesktopCapturer> DesktopCapturer::Create(v8::Isolate* isolate) {
|
|
|
|
return mate::CreateHandle(isolate, new DesktopCapturer(isolate));
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-04-25 01:17:54 +00:00
|
|
|
void DesktopCapturer::BuildPrototype(
|
2018-04-18 01:55:30 +00:00
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "DesktopCapturer"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2016-04-25 01:17:54 +00:00
|
|
|
.SetMethod("startHandling", &DesktopCapturer::StartHandling);
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
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) {
|
2015-10-02 09:50:10 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
2015-10-04 01:35:00 +00:00
|
|
|
dict.Set("desktopCapturer", atom::api::DesktopCapturer::Create(isolate));
|
2015-10-02 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_desktop_capturer, Initialize);
|