feat: add desktopCapturer.getMediaSourceIdForWebContents() to get stream source id from web contents (#22701)

* feat: add desktopCapturer.getMediaSourceIdForWebContents() to get stream source id from web contents

* Cleanup from #22701 PR comments
This commit is contained in:
Jeremy Judeaux 2020-05-27 04:34:24 +08:00 committed by GitHub
parent dc72f74020
commit 204f001c5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 178 additions and 2 deletions

View file

@ -14,7 +14,11 @@
#include "chrome/browser/media/webrtc/desktop_media_list.h"
#include "chrome/browser/media/webrtc/window_icon_util.h"
#include "content/public/browser/desktop_capture.h"
#include "content/public/browser/desktop_streams_registry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "gin/object_template_builder.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/common/api/electron_api_native_image.h"
#include "shell/common/gin_converters/gfx_converter.h"
#include "shell/common/gin_helper/dictionary.h"
@ -22,6 +26,7 @@
#include "shell/common/node_includes.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h"
#include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
#include "url/origin.h"
#if defined(OS_WIN)
#include "third_party/webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h"
@ -202,6 +207,55 @@ gin::Handle<DesktopCapturer> DesktopCapturer::Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new DesktopCapturer(isolate));
}
// 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;
}
gin::ObjectTemplateBuilder DesktopCapturer::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin::Wrappable<DesktopCapturer>::GetObjectTemplateBuilder(isolate)
@ -225,6 +279,9 @@ void Initialize(v8::Local<v8::Object> exports,
gin_helper::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("createDesktopCapturer",
&electron::api::DesktopCapturer::Create);
dict.SetMethod(
"getMediaSourceIdForWebContents",
&electron::api::DesktopCapturer::GetMediaSourceIdForWebContents);
}
} // namespace