Implement desktop capture API on OS X.

This commit is contained in:
Haojian Wu 2015-10-02 17:50:10 +08:00
parent a0638fe801
commit c9fbde321c
12 changed files with 768 additions and 6 deletions

View file

@ -0,0 +1,127 @@
// 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"
#include "atom/common/api/atom_api_native_image.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/node_includes.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/media/desktop_media_list.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h"
#include "third_party/webrtc/modules/desktop_capture/screen_capturer.h"
#include "third_party/webrtc/modules/desktop_capture/window_capturer.h"
namespace mate {
template<>
struct Converter<DesktopMediaList::Source> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const DesktopMediaList::Source& source) {
mate::Dictionary dict(isolate, v8::Object::New(isolate));
content::DesktopMediaID id = source.id;
dict.Set("name", base::UTF16ToUTF8(source.name));
dict.Set("id", id.ToString());
dict.Set("thumbnail",
atom::api::NativeImage::Create(isolate, gfx::Image(source.thumbnail)));
return ConvertToV8(isolate, dict);
}
};
} // namespace mate
namespace atom {
namespace api {
namespace {
// The wrapDesktopCapturer funtion which is implemented in JavaScript
using WrapDesktopCapturerCallback = base::Callback<void(v8::Local<v8::Value>)>;
WrapDesktopCapturerCallback g_wrap_desktop_capturer;
const int kThumbnailWidth = 150;
const int kThumbnailHeight = 150;
} // namespace
DesktopCapturer::DesktopCapturer(bool show_screens, bool show_windows) {
scoped_ptr<webrtc::ScreenCapturer> screen_capturer(
show_screens ? webrtc::ScreenCapturer::Create() : nullptr);
scoped_ptr<webrtc::WindowCapturer> window_capturer(
show_windows ? webrtc::WindowCapturer::Create() : nullptr);
media_list_.reset(new NativeDesktopMediaList(screen_capturer.Pass(),
window_capturer.Pass()));
media_list_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight));
media_list_->StartUpdating(this);
}
DesktopCapturer::~DesktopCapturer() {
}
const DesktopMediaList::Source& DesktopCapturer::GetSource(int index) {
return media_list_->GetSource(index);
}
void DesktopCapturer::OnSourceAdded(int index) {
Emit("source-added", index);
}
void DesktopCapturer::OnSourceRemoved(int index) {
Emit("source-removed", index);
}
void DesktopCapturer::OnSourceMoved(int old_index, int new_index) {
Emit("source-moved", old_index, new_index);
}
void DesktopCapturer::OnSourceNameChanged(int index) {
Emit("source-name-changed", index);
}
void DesktopCapturer::OnSourceThumbnailChanged(int index) {
Emit("source-thumbnail-changed", index);
}
mate::ObjectTemplateBuilder DesktopCapturer::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
.SetMethod("getSource", &DesktopCapturer::GetSource);
}
void SetWrapDesktopCapturer(const WrapDesktopCapturerCallback& callback) {
g_wrap_desktop_capturer = callback;
}
void ClearWrapDesktopCapturer() {
g_wrap_desktop_capturer.Reset();
}
// static
mate::Handle<DesktopCapturer> DesktopCapturer::Create(v8::Isolate* isolate,
bool show_screens, bool show_windows) {
auto handle = mate::CreateHandle(isolate,
new DesktopCapturer(show_screens, show_windows));
g_wrap_desktop_capturer.Run(handle.ToV8());
return handle;
}
} // namespace api
} // namespace atom
namespace {
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.SetMethod("_setWrapDesktopCapturer", &atom::api::SetWrapDesktopCapturer);
dict.SetMethod("_clearWrapDesktopCapturer",
&atom::api::ClearWrapDesktopCapturer);
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_desktop_capturer, Initialize);

View file

@ -0,0 +1,51 @@
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_ATOM_API_DESKTOP_CAPTURER_
#define ATOM_BROWSER_API_ATOM_API_DESKTOP_CAPTURER_
#include "base/memory/scoped_ptr.h"
#include "atom/browser/api/event_emitter.h"
#include "chrome/browser/media/desktop_media_list_observer.h"
#include "chrome/browser/media/native_desktop_media_list.h"
#include "native_mate/handle.h"
namespace atom {
namespace api {
class DesktopCapturer: public mate::EventEmitter,
public DesktopMediaListObserver {
public:
static mate::Handle<DesktopCapturer> Create(v8::Isolate* isolate,
bool show_screens, bool show_windows);
const DesktopMediaList::Source& GetSource(int index);
protected:
DesktopCapturer(bool show_screens, bool show_windows);
~DesktopCapturer();
// DesktopMediaListObserver overrides.
void OnSourceAdded(int index) override;
void OnSourceRemoved(int index) override;
void OnSourceMoved(int old_index, int new_index) override;
void OnSourceNameChanged(int index) override;
void OnSourceThumbnailChanged(int index) override;
private:
// mate::Wrappable:
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
scoped_ptr<DesktopMediaList> media_list_;
DISALLOW_COPY_AND_ASSIGN(DesktopCapturer);
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_DESKTOP_CAPTURER_

View file

@ -56,6 +56,21 @@ Screen::~Screen() {
screen_->RemoveObserver(this);
}
mate::Handle<atom::api::DesktopCapturer> Screen::GetDesktopCapturer(
const std::vector<std::string>& sources) {
bool show_screens = false;
bool show_windows = false;
for (const auto& source_type : sources) {
if (source_type == "screen") {
show_screens = true;
} else if (source_type == "window") {
show_windows = true;
}
}
return DesktopCapturer::Create(isolate(), show_screens, show_windows);
}
gfx::Point Screen::GetCursorScreenPoint() {
return screen_->GetCursorScreenPoint();
}
@ -107,7 +122,8 @@ mate::ObjectTemplateBuilder Screen::GetObjectTemplateBuilder(
.SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
.SetMethod("getAllDisplays", &Screen::GetAllDisplays)
.SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
.SetMethod("getDisplayMatching", &Screen::GetDisplayMatching);
.SetMethod("getDisplayMatching", &Screen::GetDisplayMatching)
.SetMethod("getDesktopCapturer", &Screen::GetDesktopCapturer);
}
// static

View file

@ -6,7 +6,9 @@
#define ATOM_BROWSER_API_ATOM_API_SCREEN_H_
#include <vector>
#include <string>
#include "atom/browser/api/atom_api_desktop_capturer.h"
#include "atom/browser/api/event_emitter.h"
#include "native_mate/handle.h"
#include "ui/gfx/display_observer.h"
@ -36,6 +38,9 @@ class Screen : public mate::EventEmitter,
gfx::Display GetDisplayNearestPoint(const gfx::Point& point);
gfx::Display GetDisplayMatching(const gfx::Rect& match_rect);
mate::Handle<DesktopCapturer> GetDesktopCapturer(
const std::vector<std::string>& sources);
// gfx::DisplayObserver:
void OnDisplayAdded(const gfx::Display& new_display) override;
void OnDisplayRemoved(const gfx::Display& old_display) override;

View file

@ -3,17 +3,18 @@ EventEmitter = require('events').EventEmitter
bindings = process.atomBinding 'app'
sessionBindings = process.atomBinding 'session'
downloadItemBindings = process.atomBinding 'download_item'
desktopCapturerBindings = process.atomBinding 'desktop_capturer'
app = bindings.app
app.__proto__ = EventEmitter.prototype
wrapSession = (session) ->
# session is an Event Emitter.
session.__proto__ = EventEmitter.prototype
wrapToEventListener = (item) ->
# item is an Event Emitter.
item.__proto__ = EventEmitter.prototype
wrapDownloadItem = (download_item) ->
# download_item is an Event Emitter.
download_item.__proto__ = EventEmitter.prototype
wrapToEventListener download_item
# Be compatible with old APIs.
download_item.url = download_item.getUrl()
download_item.filename = download_item.getFilename()
@ -58,11 +59,14 @@ app.resolveProxy = -> @defaultSession.resolveProxy.apply @defaultSession, argume
app.on 'activate', (event, hasVisibleWindows) -> @emit 'activate-with-no-open-windows' if not hasVisibleWindows
# Session wrapper.
sessionBindings._setWrapSession wrapSession
sessionBindings._setWrapSession wrapToEventListener
process.once 'exit', sessionBindings._clearWrapSession
downloadItemBindings._setWrapDownloadItem wrapDownloadItem
process.once 'exit', downloadItemBindings._clearWrapDownloadItem
desktopCapturerBindings._setWrapDesktopCapturer wrapToEventListener
process.once 'exit', desktopCapturerBindings._clearWrapDesktopCapturer
# Only one App object pemitted.
module.exports = app