Refine: make desktop-capturer as a renderer module.
This commit is contained in:
		
					parent
					
						
							
								48fbd47416
							
						
					
				
			
			
				commit
				
					
						1e69ef79de
					
				
			
		
					 12 changed files with 237 additions and 74 deletions
				
			
		| 
						 | 
				
			
			@ -5,7 +5,6 @@
 | 
			
		|||
#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"
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +24,8 @@ struct Converter<DesktopMediaList::Source> {
 | 
			
		|||
    content::DesktopMediaID id = source.id;
 | 
			
		||||
    dict.Set("name", base::UTF16ToUTF8(source.name));
 | 
			
		||||
    dict.Set("id", id.ToString());
 | 
			
		||||
    dict.Set("thumbnail",
 | 
			
		||||
    dict.Set(
 | 
			
		||||
        "thumbnail",
 | 
			
		||||
        atom::api::NativeImage::Create(isolate, gfx::Image(source.thumbnail)));
 | 
			
		||||
    return ConvertToV8(isolate, dict);
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -38,15 +38,29 @@ 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) {
 | 
			
		||||
DesktopCapturer::DesktopCapturer() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
DesktopCapturer::~DesktopCapturer() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DesktopCapturer::StartUpdating(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;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!show_windows && !show_screens)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  webrtc::DesktopCaptureOptions options =
 | 
			
		||||
      webrtc::DesktopCaptureOptions::CreateDefault();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -70,54 +84,39 @@ DesktopCapturer::DesktopCapturer(bool show_screens, bool show_windows) {
 | 
			
		|||
  media_list_->StartUpdating(this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
DesktopCapturer::~DesktopCapturer() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const DesktopMediaList::Source& DesktopCapturer::GetSource(int index) {
 | 
			
		||||
  return media_list_->GetSource(index);
 | 
			
		||||
void DesktopCapturer::StopUpdating() {
 | 
			
		||||
  media_list_.reset();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DesktopCapturer::OnSourceAdded(int index) {
 | 
			
		||||
  Emit("source-added", index);
 | 
			
		||||
  Emit("source-added", media_list_->GetSource(index));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DesktopCapturer::OnSourceRemoved(int index) {
 | 
			
		||||
  Emit("source-removed", index);
 | 
			
		||||
  Emit("source-removed", media_list_->GetSource(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);
 | 
			
		||||
  Emit("source-name-changed", media_list_->GetSource(index));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void DesktopCapturer::OnSourceThumbnailChanged(int index) {
 | 
			
		||||
  Emit("source-thumbnail-changed", index);
 | 
			
		||||
  Emit("source-thumbnail-changed", media_list_->GetSource(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();
 | 
			
		||||
      .SetMethod("startUpdating", &DesktopCapturer::StartUpdating)
 | 
			
		||||
      .SetMethod("stopUpdating", &DesktopCapturer::StopUpdating);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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;
 | 
			
		||||
mate::Handle<DesktopCapturer> DesktopCapturer::Create(v8::Isolate* isolate) {
 | 
			
		||||
  return mate::CreateHandle(isolate, new DesktopCapturer);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
| 
						 | 
				
			
			@ -130,9 +129,7 @@ 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);
 | 
			
		||||
  dict.Set("desktopCapturer", atom::api::DesktopCapturer::Create(isolate));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,8 +2,11 @@
 | 
			
		|||
// 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_
 | 
			
		||||
#ifndef ATOM_BROWSER_API_ATOM_API_DESKTOP_CAPTURER_H_
 | 
			
		||||
#define ATOM_BROWSER_API_ATOM_API_DESKTOP_CAPTURER_H_
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#include "base/memory/scoped_ptr.h"
 | 
			
		||||
#include "atom/browser/api/event_emitter.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -18,13 +21,13 @@ namespace api {
 | 
			
		|||
class DesktopCapturer: public mate::EventEmitter,
 | 
			
		||||
                       public DesktopMediaListObserver {
 | 
			
		||||
 public:
 | 
			
		||||
  static mate::Handle<DesktopCapturer> Create(v8::Isolate* isolate,
 | 
			
		||||
    bool show_screens, bool show_windows);
 | 
			
		||||
  static mate::Handle<DesktopCapturer> Create(v8::Isolate* isolate);
 | 
			
		||||
 | 
			
		||||
  const DesktopMediaList::Source& GetSource(int index);
 | 
			
		||||
  void StartUpdating(const std::vector<std::string>& sources);
 | 
			
		||||
  void StopUpdating();
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  DesktopCapturer(bool show_screens, bool show_windows);
 | 
			
		||||
  DesktopCapturer();
 | 
			
		||||
  ~DesktopCapturer();
 | 
			
		||||
 | 
			
		||||
  // DesktopMediaListObserver overrides.
 | 
			
		||||
| 
						 | 
				
			
			@ -48,4 +51,4 @@ class DesktopCapturer: public mate::EventEmitter,
 | 
			
		|||
 | 
			
		||||
}  // namespace atom
 | 
			
		||||
 | 
			
		||||
#endif  // ATOM_BROWSER_API_ATOM_API_DESKTOP_CAPTURER_
 | 
			
		||||
#endif  // ATOM_BROWSER_API_ATOM_API_DESKTOP_CAPTURER_H_
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -56,21 +56,6 @@ 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();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -122,8 +107,7 @@ mate::ObjectTemplateBuilder Screen::GetObjectTemplateBuilder(
 | 
			
		|||
      .SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
 | 
			
		||||
      .SetMethod("getAllDisplays", &Screen::GetAllDisplays)
 | 
			
		||||
      .SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
 | 
			
		||||
      .SetMethod("getDisplayMatching", &Screen::GetDisplayMatching)
 | 
			
		||||
      .SetMethod("getDesktopCapturer", &Screen::GetDesktopCapturer);
 | 
			
		||||
      .SetMethod("getDisplayMatching", &Screen::GetDisplayMatching);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,9 +6,7 @@
 | 
			
		|||
#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"
 | 
			
		||||
| 
						 | 
				
			
			@ -38,9 +36,6 @@ 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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,18 +3,17 @@ 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
 | 
			
		||||
 | 
			
		||||
wrapToEventListener = (item) ->
 | 
			
		||||
  # item is an Event Emitter.
 | 
			
		||||
  item.__proto__ = EventEmitter.prototype
 | 
			
		||||
wrapSession = (session) ->
 | 
			
		||||
  # session is an Event Emitter.
 | 
			
		||||
  session.__proto__ = EventEmitter.prototype
 | 
			
		||||
 | 
			
		||||
wrapDownloadItem = (download_item) ->
 | 
			
		||||
  # download_item is an Event Emitter.
 | 
			
		||||
  wrapToEventListener download_item
 | 
			
		||||
  download_item.__proto__ = EventEmitter.prototype
 | 
			
		||||
  # Be compatible with old APIs.
 | 
			
		||||
  download_item.url = download_item.getUrl()
 | 
			
		||||
  download_item.filename = download_item.getFilename()
 | 
			
		||||
| 
						 | 
				
			
			@ -59,14 +58,11 @@ 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 wrapToEventListener
 | 
			
		||||
sessionBindings._setWrapSession wrapSession
 | 
			
		||||
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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue