summaryrefslogtreecommitdiffstats
path: root/dom/media/systemservices/video_engine/desktop_device_info.cc
blob: b3632a509fdff3a05e6a50112ce04cb79137ed71 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "desktop_device_info.h"
#include "modules/desktop_capture/desktop_capture_options.h"
#include "modules/desktop_capture/desktop_capturer.h"
#include "mozilla/Sprintf.h"
#include "mozilla/StaticPrefs_media.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/UniquePtr.h"
#include "nsIBrowserWindowTracker.h"
#include "nsImportModule.h"

#include <cstddef>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <memory>

namespace webrtc {

static inline void SetStringMember(char** aMember, const char* aValue) {
  if (!aValue) {
    return;
  }

  if (*aMember) {
    delete[] * aMember;
    *aMember = nullptr;
  }

  size_t nBufLen = strlen(aValue) + 1;
  char* buffer = new char[nBufLen];
  memcpy(buffer, aValue, nBufLen - 1);
  buffer[nBufLen - 1] = '\0';
  *aMember = buffer;
}

DesktopDisplayDevice::DesktopDisplayDevice() {
  mScreenId = kInvalidScreenId;
  mDeviceUniqueIdUTF8 = nullptr;
  mDeviceNameUTF8 = nullptr;
  mPid = 0;
}

DesktopDisplayDevice::~DesktopDisplayDevice() {
  mScreenId = kInvalidScreenId;

  delete[] mDeviceUniqueIdUTF8;
  delete[] mDeviceNameUTF8;

  mDeviceUniqueIdUTF8 = nullptr;
  mDeviceNameUTF8 = nullptr;
}

void DesktopDisplayDevice::setScreenId(const ScreenId aScreenId) {
  mScreenId = aScreenId;
}

void DesktopDisplayDevice::setDeviceName(const char* aDeviceNameUTF8) {
  SetStringMember(&mDeviceNameUTF8, aDeviceNameUTF8);
}

void DesktopDisplayDevice::setUniqueIdName(const char* aDeviceUniqueIdUTF8) {
  SetStringMember(&mDeviceUniqueIdUTF8, aDeviceUniqueIdUTF8);
}

void DesktopDisplayDevice::setPid(const int aPid) { mPid = aPid; }

ScreenId DesktopDisplayDevice::getScreenId() { return mScreenId; }

const char* DesktopDisplayDevice::getDeviceName() { return mDeviceNameUTF8; }

const char* DesktopDisplayDevice::getUniqueIdName() {
  return mDeviceUniqueIdUTF8;
}

pid_t DesktopDisplayDevice::getPid() { return mPid; }

DesktopDisplayDevice& DesktopDisplayDevice::operator=(
    DesktopDisplayDevice& aOther) {
  if (&aOther == this) {
    return *this;
  }
  mScreenId = aOther.getScreenId();
  setUniqueIdName(aOther.getUniqueIdName());
  setDeviceName(aOther.getDeviceName());
  mPid = aOther.getPid();

  return *this;
}

DesktopTab::DesktopTab() {
  mTabBrowserId = 0;
  mTabNameUTF8 = nullptr;
  mTabUniqueIdUTF8 = nullptr;
  mTabCount = 0;
}

DesktopTab::~DesktopTab() {
  delete[] mTabNameUTF8;
  delete[] mTabUniqueIdUTF8;

  mTabNameUTF8 = nullptr;
  mTabUniqueIdUTF8 = nullptr;
}

void DesktopTab::setTabBrowserId(uint64_t aTabBrowserId) {
  mTabBrowserId = aTabBrowserId;
}

void DesktopTab::setUniqueIdName(const char* aTabUniqueIdUTF8) {
  SetStringMember(&mTabUniqueIdUTF8, aTabUniqueIdUTF8);
}

void DesktopTab::setTabName(const char* aTabNameUTF8) {
  SetStringMember(&mTabNameUTF8, aTabNameUTF8);
}

void DesktopTab::setTabCount(const uint32_t aCount) { mTabCount = aCount; }

uint64_t DesktopTab::getTabBrowserId() { return mTabBrowserId; }

const char* DesktopTab::getUniqueIdName() { return mTabUniqueIdUTF8; }

const char* DesktopTab::getTabName() { return mTabNameUTF8; }

uint32_t DesktopTab::getTabCount() { return mTabCount; }

DesktopTab& DesktopTab::operator=(DesktopTab& aOther) {
  mTabBrowserId = aOther.getTabBrowserId();
  setUniqueIdName(aOther.getUniqueIdName());
  setTabName(aOther.getTabName());

  return *this;
}

class DesktopDeviceInfoImpl : public DesktopDeviceInfo {
 public:
  DesktopDeviceInfoImpl();
  ~DesktopDeviceInfoImpl();

  int32_t Init() override;
  int32_t Refresh() override;
  int32_t getDisplayDeviceCount() override;
  int32_t getDesktopDisplayDeviceInfo(
      uint32_t aIndex, DesktopDisplayDevice& aDesktopDisplayDevice) override;
  int32_t getWindowCount() override;
  int32_t getWindowInfo(uint32_t aIndex,
                        DesktopDisplayDevice& aWindowDevice) override;
  uint32_t getTabCount() override;
  int32_t getTabInfo(uint32_t aIndex, DesktopTab& aDesktopTab) override;

 protected:
  DesktopDisplayDeviceList mDesktopDisplayList;
  DesktopDisplayDeviceList mDesktopWindowList;
  DesktopTabList mDesktopTabList;

  void CleanUp();
  void CleanUpWindowList();
  void CleanUpTabList();
  void CleanUpScreenList();

  void InitializeWindowList();
  virtual void InitializeTabList();
  void InitializeScreenList();

  void RefreshWindowList();
  void RefreshTabList();
  void RefreshScreenList();

  void DummyTabList(DesktopTabList& aList);
};

DesktopDeviceInfoImpl::DesktopDeviceInfoImpl() = default;

DesktopDeviceInfoImpl::~DesktopDeviceInfoImpl() { CleanUp(); }

int32_t DesktopDeviceInfoImpl::getDisplayDeviceCount() {
  return static_cast<int32_t>(mDesktopDisplayList.size());
}

int32_t DesktopDeviceInfoImpl::getDesktopDisplayDeviceInfo(
    uint32_t aIndex, DesktopDisplayDevice& aDesktopDisplayDevice) {
  if (aIndex >= mDesktopDisplayList.size()) {
    return -1;
  }

  std::map<intptr_t, DesktopDisplayDevice*>::iterator iter =
      mDesktopDisplayList.begin();
  std::advance(iter, aIndex);
  DesktopDisplayDevice* desktopDisplayDevice = iter->second;
  if (desktopDisplayDevice) {
    aDesktopDisplayDevice = (*desktopDisplayDevice);
  }

  return 0;
}

int32_t DesktopDeviceInfoImpl::getWindowCount() {
  return static_cast<int32_t>(mDesktopWindowList.size());
}

int32_t DesktopDeviceInfoImpl::getWindowInfo(
    uint32_t aIndex, DesktopDisplayDevice& aWindowDevice) {
  if (aIndex >= mDesktopWindowList.size()) {
    return -1;
  }

  std::map<intptr_t, DesktopDisplayDevice*>::iterator itr =
      mDesktopWindowList.begin();
  std::advance(itr, aIndex);
  DesktopDisplayDevice* window = itr->second;
  if (!window) {
    return -1;
  }

  aWindowDevice = (*window);
  return 0;
}

uint32_t DesktopDeviceInfoImpl::getTabCount() { return mDesktopTabList.size(); }

int32_t DesktopDeviceInfoImpl::getTabInfo(uint32_t aIndex,
                                          DesktopTab& aDesktopTab) {
  if (aIndex >= mDesktopTabList.size()) {
    return -1;
  }

  std::map<intptr_t, DesktopTab*>::iterator iter = mDesktopTabList.begin();
  std::advance(iter, aIndex);
  DesktopTab* desktopTab = iter->second;
  if (desktopTab) {
    aDesktopTab = (*desktopTab);
  }

  return 0;
}

void DesktopDeviceInfoImpl::CleanUp() {
  CleanUpScreenList();
  CleanUpWindowList();
  CleanUpTabList();
}
int32_t DesktopDeviceInfoImpl::Init() {
  InitializeScreenList();
  InitializeWindowList();
  InitializeTabList();

  return 0;
}
int32_t DesktopDeviceInfoImpl::Refresh() {
  RefreshScreenList();
  RefreshWindowList();
  RefreshTabList();

  return 0;
}

void DesktopDeviceInfoImpl::CleanUpWindowList() {
  std::map<intptr_t, DesktopDisplayDevice*>::iterator iterWindow;
  for (iterWindow = mDesktopWindowList.begin();
       iterWindow != mDesktopWindowList.end(); iterWindow++) {
    DesktopDisplayDevice* aWindow = iterWindow->second;
    delete aWindow;
    iterWindow->second = nullptr;
  }
  mDesktopWindowList.clear();
}

void DesktopDeviceInfoImpl::InitializeWindowList() {
  DesktopCaptureOptions options;

// Wayland is special and we will not get any information about windows
// without going through xdg-desktop-portal. We will already have
// a screen placeholder so there is no reason to build windows list.
#if defined(WEBRTC_USE_PIPEWIRE)
  if (mozilla::StaticPrefs::media_webrtc_capture_allow_pipewire() &&
      webrtc::DesktopCapturer::IsRunningUnderWayland()) {
    return;
  }
#endif

// Help avoid an X11 deadlock, see bug 1456101.
#ifdef MOZ_X11
  MOZ_ALWAYS_SUCCEEDS(mozilla::SyncRunnable::DispatchToThread(
      mozilla::GetMainThreadSerialEventTarget(),
      NS_NewRunnableFunction(__func__, [&] {
        options = DesktopCaptureOptions::CreateDefault();
      })));
#else
  options = DesktopCaptureOptions::CreateDefault();
#endif
  std::unique_ptr<DesktopCapturer> winCap =
      DesktopCapturer::CreateWindowCapturer(options);
  DesktopCapturer::SourceList list;
  if (winCap && winCap->GetSourceList(&list)) {
    DesktopCapturer::SourceList::iterator itr;
    for (itr = list.begin(); itr != list.end(); itr++) {
      DesktopDisplayDevice* winDevice = new DesktopDisplayDevice;
      if (!winDevice) {
        continue;
      }

      winDevice->setScreenId(itr->id);
      winDevice->setDeviceName(itr->title.c_str());
      winDevice->setPid(itr->pid);

      char idStr[BUFSIZ];
#if WEBRTC_WIN
      _snprintf_s(idStr, sizeof(idStr), sizeof(idStr) - 1, "%ld",
                  static_cast<long>(winDevice->getScreenId()));
#else
      SprintfLiteral(idStr, "%ld", static_cast<long>(winDevice->getScreenId()));
#endif
      winDevice->setUniqueIdName(idStr);
      mDesktopWindowList[winDevice->getScreenId()] = winDevice;
    }
  }
}

void DesktopDeviceInfoImpl::RefreshWindowList() {
  CleanUpWindowList();
  InitializeWindowList();
}

void DesktopDeviceInfoImpl::CleanUpTabList() {
  for (auto& iterTab : mDesktopTabList) {
    DesktopTab* desktopTab = iterTab.second;
    delete desktopTab;
    iterTab.second = nullptr;
  }
  mDesktopTabList.clear();
}

void webrtc::DesktopDeviceInfoImpl::InitializeTabList() {
  if (!mozilla::StaticPrefs::media_getusermedia_browser_enabled()) {
    return;
  }

  // This is a sync dispatch to main thread, which is unfortunate. To
  // call JavaScript we have to be on main thread, but the remaining
  // DesktopCapturer very much wants to be off main thread. This might
  // be solvable by calling this method earlier on while we're still on
  // main thread and plumbing the information down to here.
  nsCOMPtr<nsIRunnable> runnable = NS_NewRunnableFunction(__func__, [&] {
    nsresult rv;
    nsCOMPtr<nsIBrowserWindowTracker> bwt =
        do_ImportModule("resource:///modules/BrowserWindowTracker.jsm",
                        "BrowserWindowTracker", &rv);
    if (NS_FAILED(rv)) {
      return;
    }

    nsTArray<RefPtr<nsIVisibleTab>> tabArray;
    rv = bwt->GetAllVisibleTabs(tabArray);
    if (NS_FAILED(rv)) {
      return;
    }

    for (const auto& browserTab : tabArray) {
      nsString contentTitle;
      browserTab->GetContentTitle(contentTitle);
      int64_t browserId;
      browserTab->GetBrowserId(&browserId);

      DesktopTab* desktopTab = new DesktopTab;
      if (desktopTab) {
        char* contentTitleUTF8 = ToNewUTF8String(contentTitle);
        desktopTab->setTabBrowserId(browserId);
        desktopTab->setTabName(contentTitleUTF8);
        std::ostringstream uniqueId;
        uniqueId << browserId;
        desktopTab->setUniqueIdName(uniqueId.str().c_str());
        mDesktopTabList[static_cast<intptr_t>(desktopTab->getTabBrowserId())] =
            desktopTab;
        free(contentTitleUTF8);
      }
    }
  });
  mozilla::SyncRunnable::DispatchToThread(
      mozilla::GetMainThreadSerialEventTarget(), runnable);
}

void DesktopDeviceInfoImpl::RefreshTabList() {
  CleanUpTabList();
  InitializeTabList();
}

void DesktopDeviceInfoImpl::CleanUpScreenList() {
  std::map<intptr_t, DesktopDisplayDevice*>::iterator iterDevice;
  for (iterDevice = mDesktopDisplayList.begin();
       iterDevice != mDesktopDisplayList.end(); iterDevice++) {
    DesktopDisplayDevice* desktopDisplayDevice = iterDevice->second;
    delete desktopDisplayDevice;
    iterDevice->second = nullptr;
  }
  mDesktopDisplayList.clear();
}

// With PipeWire we can't select which system resource is shared so
// we don't create a window/screen list. Instead we place these constants
// as window name/id so frontend code can identify PipeWire backend
// and does not try to create screen/window preview.

#define PIPEWIRE_ID 0xaffffff
#define PIPEWIRE_NAME "####_PIPEWIRE_PORTAL_####"

void DesktopDeviceInfoImpl::InitializeScreenList() {
  DesktopCaptureOptions options;

// Wayland is special and we will not get any information about screens
// without going through xdg-desktop-portal so we just need a screen
// placeholder.
#if defined(WEBRTC_USE_PIPEWIRE)
  if (mozilla::StaticPrefs::media_webrtc_capture_allow_pipewire() &&
      webrtc::DesktopCapturer::IsRunningUnderWayland()) {
    DesktopDisplayDevice* screenDevice = new DesktopDisplayDevice;
    if (!screenDevice) {
      return;
    }

    screenDevice->setScreenId(PIPEWIRE_ID);
    screenDevice->setDeviceName(PIPEWIRE_NAME);

    char idStr[BUFSIZ];
    SprintfLiteral(idStr, "%ld",
                   static_cast<long>(screenDevice->getScreenId()));
    screenDevice->setUniqueIdName(idStr);
    mDesktopDisplayList[screenDevice->getScreenId()] = screenDevice;
    return;
  }
#endif

// Help avoid an X11 deadlock, see bug 1456101.
#ifdef MOZ_X11
  MOZ_ALWAYS_SUCCEEDS(mozilla::SyncRunnable::DispatchToThread(
      mozilla::GetMainThreadSerialEventTarget(),
      NS_NewRunnableFunction(__func__, [&] {
        options = DesktopCaptureOptions::CreateDefault();
      })));
#else
  options = DesktopCaptureOptions::CreateDefault();
#endif
  std::unique_ptr<DesktopCapturer> screenCapturer =
      DesktopCapturer::CreateScreenCapturer(options);
  DesktopCapturer::SourceList list;
  if (screenCapturer && screenCapturer->GetSourceList(&list)) {
    DesktopCapturer::SourceList::iterator itr;
    for (itr = list.begin(); itr != list.end(); itr++) {
      DesktopDisplayDevice* screenDevice = new DesktopDisplayDevice;
      screenDevice->setScreenId(itr->id);
      if (list.size() == 1) {
        screenDevice->setDeviceName("Primary Monitor");
      } else {
        screenDevice->setDeviceName(itr->title.c_str());
      }
      screenDevice->setPid(itr->pid);

      char idStr[BUFSIZ];
#if WEBRTC_WIN
      _snprintf_s(idStr, sizeof(idStr), sizeof(idStr) - 1, "%ld",
                  static_cast<long>(screenDevice->getScreenId()));
#else
      SprintfLiteral(idStr, "%ld",
                     static_cast<long>(screenDevice->getScreenId()));
#endif
      screenDevice->setUniqueIdName(idStr);
      mDesktopDisplayList[screenDevice->getScreenId()] = screenDevice;
    }
  }
}

void DesktopDeviceInfoImpl::RefreshScreenList() {
  CleanUpScreenList();
  InitializeScreenList();
}

/* static */
DesktopDeviceInfo* DesktopDeviceInfo::Create() {
  auto info = mozilla::MakeUnique<DesktopDeviceInfoImpl>();
  if (info->Init() != 0) {
    return nullptr;
  }
  return info.release();
}
}  // namespace webrtc