File: chrome_screen_enumerator.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (165 lines) | stat: -rw-r--r-- 6,024 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/media/webrtc/chrome_screen_enumerator.h"

#include <tuple>

#include "base/feature_list.h"
#include "base/lazy_instance.h"
#include "base/task/bind_post_task.h"
#include "build/build_config.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/desktop_media_id.h"
#include "content/public/common/content_features.h"
#include "third_party/blink/public/common/features_generated.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#include "ui/display/screen.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/shell.h"
#include "ui/aura/window.h"
#elif BUILDFLAG(IS_LINUX)
#include "base/functional/callback.h"
#include "content/public/browser/desktop_capture.h"
#endif

#if BUILDFLAG(IS_CHROMEOS)
namespace {
base::LazyInstance<std::vector<raw_ptr<aura::Window, VectorExperimental>>>::
    DestructorAtExit root_windows_for_testing_ = LAZY_INSTANCE_INITIALIZER;
}  // namespace

#elif BUILDFLAG(IS_LINUX)
namespace {
base::LazyInstance<std::unique_ptr<webrtc::DesktopCapturer>>::DestructorAtExit
    g_desktop_capturer_for_testing = LAZY_INSTANCE_INITIALIZER;
}  // namespace

#endif

namespace {
#if BUILDFLAG(IS_CHROMEOS)
blink::mojom::StreamDevicesSetPtr EnumerateScreens(
    blink::mojom::MediaStreamType stream_type) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  aura::Window::Windows root_windows =
      (root_windows_for_testing_.IsCreated())
          ? std::move(root_windows_for_testing_.Get())
          : ash::Shell::GetAllRootWindows();

  display::Screen* screen = display::Screen::GetScreen();
  blink::mojom::StreamDevicesSetPtr stream_devices_set =
      blink::mojom::StreamDevicesSet::New();
  for (aura::Window* window : root_windows) {
    content::DesktopMediaID media_id =
        content::DesktopMediaID::RegisterNativeWindow(
            content::DesktopMediaID::TYPE_SCREEN, window);
    DCHECK_EQ(content::DesktopMediaID::Type::TYPE_SCREEN, media_id.type);

    // Add selected desktop source to the list.
    blink::MediaStreamDevice device(
        stream_type, /*id=*/media_id.ToString(),
        /*name=*/"Screen",
        /*display_id=*/
        screen->GetDisplayNearestWindow(window).id());
    device.display_media_info = media::mojom::DisplayMediaInformation::New(
        /*display_surface=*/media::mojom::DisplayCaptureSurfaceType::MONITOR,
        /*logical_surface=*/true,
        /*cursor=*/media::mojom::CursorCaptureType::NEVER,
        /*capture_handle=*/nullptr,
        /*initial_zoom_level=*/100);
    stream_devices_set->stream_devices.push_back(
        blink::mojom::StreamDevices::New(/*audio_device=*/std::nullopt,
                                         /*video_device=*/device));
  }
  return stream_devices_set;
}

#elif BUILDFLAG(IS_LINUX)
blink::mojom::StreamDevicesSetPtr EnumerateScreens(
    blink::mojom::MediaStreamType stream_type) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  blink::mojom::StreamDevicesSetPtr stream_devices_set =
      blink::mojom::StreamDevicesSet::New();

  std::unique_ptr<webrtc::DesktopCapturer> capturer =
      (g_desktop_capturer_for_testing.IsCreated())
          ? std::move(g_desktop_capturer_for_testing.Get())
          : content::desktop_capture::CreateScreenCapturer(
                content::desktop_capture::CreateDesktopCaptureOptions(),
                /*for_snapshot=*/true);
  if (!capturer) {
    return stream_devices_set;
  }

  capturer->Start(/*callback=*/nullptr);
  webrtc::DesktopCapturer::SourceList source_list;
  if (!capturer->GetSourceList(&source_list)) {
    return stream_devices_set;
  }

  for (const auto& source : source_list) {
    const std::string media_id =
        content::DesktopMediaID(content::DesktopMediaID::Type::TYPE_SCREEN,
                                source.id)
            .ToString();
    blink::MediaStreamDevice device(stream_type, media_id,
                                    /*name=*/"Screen",
                                    /*display_id=*/source.display_id);
    stream_devices_set->stream_devices.push_back(
        blink::mojom::StreamDevices::New(/*audio_device=*/std::nullopt,
                                         /*video_device=*/device));
  }

  return stream_devices_set;
}

#endif
}  // namespace

ChromeScreenEnumerator::ChromeScreenEnumerator() = default;

ChromeScreenEnumerator::~ChromeScreenEnumerator() = default;

#if BUILDFLAG(IS_CHROMEOS)
void ChromeScreenEnumerator::SetRootWindowsForTesting(
    std::vector<raw_ptr<aura::Window, VectorExperimental>> root_windows) {
  root_windows_for_testing_.Get() = std::move(root_windows);
}

#elif BUILDFLAG(IS_LINUX)
void ChromeScreenEnumerator::SetDesktopCapturerForTesting(
    std::unique_ptr<webrtc::DesktopCapturer> capturer) {
  g_desktop_capturer_for_testing.Get() = std::move(capturer);
}

#endif  // BUILDFLAG(IS_LINUX)

void ChromeScreenEnumerator::EnumerateScreens(
    blink::mojom::MediaStreamType stream_type,
    ScreensCallback screens_callback) const {
  DCHECK_CURRENTLY_ON(content::BrowserThread::IO);

#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  content::GetUIThreadTaskRunner({})->PostTaskAndReplyWithResult(
      FROM_HERE, base::BindOnce(::EnumerateScreens, stream_type),
      base::BindOnce(
          [](ScreensCallback screens_callback,
             blink::mojom::StreamDevicesSetPtr stream_devices_set) {
            std::move(screens_callback)
                .Run(*stream_devices_set,
                     stream_devices_set->stream_devices.size() > 0
                         ? blink::mojom::MediaStreamRequestResult::OK
                         : blink::mojom::MediaStreamRequestResult::NO_HARDWARE);
          },
          std::move(screens_callback)));
#else
  // TODO(crbug.com/40216442): Implement for other platforms than Chrome OS.
  NOTREACHED();
#endif
}