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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
|
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
* Copyright (C) 2013-2023 Apple Inc. All rights reserved.
* Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "MockRealtimeMediaSourceCenter.h"
#if ENABLE(MEDIA_STREAM)
#include "CaptureDevice.h"
#include "Logging.h"
#include "MediaConstraints.h"
#include "MockRealtimeAudioSource.h"
#include "MockRealtimeVideoSource.h"
#include "NotImplemented.h"
#include "RealtimeMediaSourceSettings.h"
#include <math.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/text/StringView.h>
#if PLATFORM(COCOA)
#include "CoreAudioCaptureSource.h"
#include "DisplayCaptureSourceCocoa.h"
#include "MockAudioSharedUnit.h"
#include "MockRealtimeVideoSourceMac.h"
#endif
#if PLATFORM(IOS_FAMILY)
#include "CoreAudioCaptureSourceIOS.h"
#endif
#if USE(GSTREAMER)
#include "MockDisplayCaptureSourceGStreamer.h"
#include "MockRealtimeVideoSourceGStreamer.h"
#endif
namespace WebCore {
static inline Vector<MockMediaDevice> defaultDevices()
{
return Vector<MockMediaDevice> {
MockMediaDevice { "239c24b0-2b15-11e3-8224-0800200c9a66"_s, "Mock audio device 1"_s, { }, MockMicrophoneProperties { 44100 , { } } },
MockMediaDevice { "239c24b1-2b15-11e3-8224-0800200c9a66"_s, "Mock audio device 2"_s, { }, MockMicrophoneProperties { 48000, { false } } },
MockMediaDevice { "239c24b1-3b15-11e3-8224-0800200c9a66"_s, "Mock audio device 3"_s, { }, MockMicrophoneProperties { 96000, { true } } },
MockMediaDevice { "239c24b0-2b15-11e3-8224-0800200c9a67"_s, "Mock speaker device 1"_s, { }, MockSpeakerProperties { "239c24b0-2b15-11e3-8224-0800200c9a66"_s, 44100 } },
MockMediaDevice { "239c24b1-2b15-11e3-8224-0800200c9a67"_s, "Mock speaker device 2"_s, { }, MockSpeakerProperties { "239c24b1-2b15-11e3-8224-0800200c9a66"_s, 48000 } },
MockMediaDevice { "239c24b2-2b15-11e3-8224-0800200c9a67"_s, "Mock speaker device 3"_s, { }, MockSpeakerProperties { String { }, 48000 } },
MockMediaDevice { "239c24b2-2b15-11e3-8224-0800200c9a66"_s, "Mock video device 1"_s, { },
MockCameraProperties {
30,
VideoFacingMode::User, {
{ { 2560, 1440 }, { { 10, 10 }, { 7.5, 7.5 }, { 5, 5 } }, 1, 1, true },
{ { 1280, 720 }, { { 30, 30 }, { 27.5, 27.5 }, { 25, 25 }, { 22.5, 22.5 }, { 20, 20 }, { 17.5, 17.5 }, { 15, 15 }, { 12.5, 12.5 }, { 10, 10 }, { 7.5, 7.5 }, { 5, 5 } }, 1, 1, true },
{ { 640, 480 }, { { 30, 30 }, { 27.5, 27.5 }, { 25, 25 }, { 22.5, 22.5 }, { 20, 20 }, { 17.5, 17.5 }, { 15, 15 }, { 12.5, 12.5 }, { 10, 10 }, { 7.5, 7.5 }, { 5, 5 } }, 1, 1, true },
{ { 112, 112 }, { { 30, 30 }, { 27.5, 27.5 }, { 25, 25 }, { 22.5, 22.5 }, { 20, 20 }, { 17.5, 17.5 }, { 15, 15 }, { 12.5, 12.5 }, { 10, 10 }, { 7.5, 7.5 }, { 5, 5 } }, 1, 1, true },
},
Color::black,
{ }, // whiteBalanceModes
false, // supportsTorch
false, // background blur enabled
} },
MockMediaDevice { "239c24b3-2b15-11e3-8224-0800200c9a66"_s, "Mock video device 2"_s, { },
MockCameraProperties {
15,
VideoFacingMode::Environment, {
{ { 3840, 2160 }, { { 2, 30 } }, 1, 4, false },
{ { 1920, 1080 }, { { 2, 30 } }, 1, 4, true },
{ { 1280, 720 }, { { 3, 120 } }, 1, 4, false },
{ { 960, 540 }, { { 3, 60 } }, 1, 4, false },
{ { 640, 480 }, { { 2, 30 } }, 1, 4, false },
{ { 352, 288 }, { { 2, 30 } }, 1, 4, false },
{ { 320, 240 }, { { 2, 30 } }, 1, 4, false },
{ { 160, 120 }, { { 2, 30 } }, 1, 4, false },
},
Color::darkGray,
{ MeteringMode::Manual, MeteringMode::SingleShot, MeteringMode::Continuous },
true,
true, // background blur enabled
} },
MockMediaDevice { "SCREEN-1"_s, "Mock screen device 1"_s, { }, MockDisplayProperties { CaptureDevice::DeviceType::Screen, Color::lightGray, { 1920, 1080 } } },
MockMediaDevice { "SCREEN-2"_s, "Mock screen device 2"_s, { }, MockDisplayProperties { CaptureDevice::DeviceType::Screen, Color::yellow, { 3840, 2160 } } },
MockMediaDevice { "WINDOW-1"_s, "Mock window device 1"_s, { }, MockDisplayProperties { CaptureDevice::DeviceType::Window, SRGBA<uint8_t> { 255, 241, 181 }, { 640, 480 } } },
MockMediaDevice { "WINDOW-2"_s, "Mock window device 2"_s, { }, MockDisplayProperties { CaptureDevice::DeviceType::Window, SRGBA<uint8_t> { 255, 208, 181 }, { 1280, 600 } } },
};
}
class MockRealtimeVideoSourceFactory : public VideoCaptureFactory {
public:
CaptureSourceOrError createVideoCaptureSource(const CaptureDevice& device, MediaDeviceHashSalts&& hashSalts, const MediaConstraints* constraints, std::optional<PageIdentifier> pageIdentifier) final
{
ASSERT(device.type() == CaptureDevice::DeviceType::Camera);
if (!MockRealtimeMediaSourceCenter::captureDeviceWithPersistentID(CaptureDevice::DeviceType::Camera, device.persistentId()))
return CaptureSourceOrError({ "Unable to find mock camera device with given persistentID"_s, MediaAccessDenialReason::PermissionDenied });
auto mock = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(device.persistentId());
ASSERT(mock);
if (mock->flags.contains(MockMediaDevice::Flag::Invalid))
return CaptureSourceOrError({ "Invalid mock camera device"_s, MediaAccessDenialReason::PermissionDenied });
return MockRealtimeVideoSource::create(String { device.persistentId() }, AtomString { device.label() }, WTFMove(hashSalts), constraints, pageIdentifier);
}
private:
CaptureDeviceManager& videoCaptureDeviceManager() final { return MockRealtimeMediaSourceCenter::singleton().videoCaptureDeviceManager(); }
};
#if PLATFORM(COCOA)
class MockDisplayCapturer final
: public DisplayCaptureSourceCocoa::Capturer
, public CanMakeWeakPtr<MockDisplayCapturer> {
public:
MockDisplayCapturer(CapturerObserver&, const CaptureDevice&, std::optional<PageIdentifier>);
void triggerMockCaptureConfigurationChange();
private:
bool start() final;
void stop() final;
DisplayCaptureSourceCocoa::DisplayFrameType generateFrame() final;
DisplaySurfaceType surfaceType() const final { return DisplaySurfaceType::Monitor; }
void commitConfiguration(const RealtimeMediaSourceSettings&) final;
CaptureDevice::DeviceType deviceType() const final { return CaptureDevice::DeviceType::Screen; }
IntSize intrinsicSize() const final;
#if !RELEASE_LOG_DISABLED
ASCIILiteral logClassName() const final { return "MockDisplayCapturer"_s; }
#endif
void whenReady(CompletionHandler<void(CaptureSourceError&&)>&&) final;
void readyTimerFired();
Ref<MockRealtimeVideoSource> m_source;
RealtimeMediaSourceSettings m_settings;
Timer m_readyTimer;
bool m_isRunning { false };
bool m_didReceiveVideoFrame { false };
CompletionHandler<void(CaptureSourceError&&)> m_whenReadyCallback;
};
MockDisplayCapturer::MockDisplayCapturer(CapturerObserver& observer, const CaptureDevice& device, std::optional<PageIdentifier> pageIdentifier)
: DisplayCaptureSourceCocoa::Capturer(observer)
, m_source(MockRealtimeVideoSourceMac::createForMockDisplayCapturer(String { device.persistentId() }, AtomString { device.label() }, MediaDeviceHashSalts { "persistent"_s, "ephemeral"_s }, pageIdentifier))
, m_readyTimer(*this, &MockDisplayCapturer::readyTimerFired)
{
}
bool MockDisplayCapturer::start()
{
ASSERT(m_settings.frameRate());
ASSERT(!m_whenReadyCallback);
ASSERT(!m_isRunning);
m_isRunning = true;
m_source->start();
return true;
}
void MockDisplayCapturer::stop()
{
ASSERT(!m_whenReadyCallback);
m_isRunning = false;
m_source->stop();
}
void MockDisplayCapturer::whenReady(CompletionHandler<void(CaptureSourceError&&)>&& callback)
{
ASSERT(!m_isRunning);
m_whenReadyCallback = WTFMove(callback);
m_readyTimer.startOneShot(50_ms);
}
void MockDisplayCapturer::readyTimerFired()
{
ASSERT(!m_isRunning);
configurationChanged();
m_whenReadyCallback({ });
}
void MockDisplayCapturer::commitConfiguration(const RealtimeMediaSourceSettings& settings)
{
// FIXME: Update m_source width, height and frameRate according settings
m_settings = settings;
}
DisplayCaptureSourceCocoa::DisplayFrameType MockDisplayCapturer::generateFrame()
{
if (auto* imageBuffer = m_source->imageBuffer())
return imageBuffer->copyNativeImage();
return { };
}
IntSize MockDisplayCapturer::intrinsicSize() const
{
auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(m_source->persistentID());
ASSERT(device);
if (!device)
return { };
ASSERT(device->isDisplay());
if (!device->isDisplay())
return { };
auto& properties = std::get<MockDisplayProperties>(device->properties);
return properties.defaultSize;
}
void MockDisplayCapturer::triggerMockCaptureConfigurationChange()
{
auto deviceId = m_source->persistentID();
auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(deviceId.startsWith("WINDOW"_s) ? "WINDOW-2"_s : "SCREEN-2"_s);
ASSERT(device);
if (!device)
return;
bool isStarted = m_source->isProducingData();
auto pageIdentifier = m_source->pageIdentifier();
m_source = MockRealtimeVideoSourceMac::createForMockDisplayCapturer(String { device->persistentId }, AtomString { device->label }, MediaDeviceHashSalts { "persistent"_s, "ephemeral"_s }, pageIdentifier);
if (isStarted)
m_source->start();
configurationChanged();
}
#endif // PLATFORM(COCOA)
class MockRealtimeDisplaySourceFactory : public DisplayCaptureFactory {
public:
static MockRealtimeDisplaySourceFactory& singleton();
CaptureSourceOrError createDisplayCaptureSource(const CaptureDevice& device, MediaDeviceHashSalts&& hashSalts, const MediaConstraints* constraints, std::optional<PageIdentifier> pageIdentifier) final
{
if (!MockRealtimeMediaSourceCenter::captureDeviceWithPersistentID(device.type(), device.persistentId()))
return CaptureSourceOrError({ "Unable to find mock display device with given persistentID"_s, MediaAccessDenialReason::PermissionDenied });
switch (device.type()) {
case CaptureDevice::DeviceType::Screen:
case CaptureDevice::DeviceType::Window: {
#if PLATFORM(COCOA)
return DisplayCaptureSourceCocoa::create([this, &device, pageIdentifier] (auto& observer) {
auto capturer = makeUniqueRefWithoutRefCountedCheck<MockDisplayCapturer>(observer, device, pageIdentifier);
m_capturer = capturer.get();
return capturer;
}, device, WTFMove(hashSalts), constraints, pageIdentifier);
#elif USE(GSTREAMER)
return MockDisplayCaptureSourceGStreamer::create(device, WTFMove(hashSalts), constraints, pageIdentifier);
#else
return MockRealtimeVideoSource::create(String { device.persistentId() }, AtomString { device.label() }, WTFMove(hashSalts), constraints, pageIdentifier);
#endif
break;
}
case CaptureDevice::DeviceType::Microphone:
case CaptureDevice::DeviceType::Speaker:
case CaptureDevice::DeviceType::Camera:
case CaptureDevice::DeviceType::SystemAudio:
case CaptureDevice::DeviceType::Unknown:
ASSERT_NOT_REACHED();
break;
}
return { };
}
#if PLATFORM(COCOA)
WeakPtr<MockDisplayCapturer> latestCapturer() { return m_capturer; }
#endif
private:
DisplayCaptureManager& displayCaptureDeviceManager() final { return MockRealtimeMediaSourceCenter::singleton().displayCaptureDeviceManager(); }
#if PLATFORM(COCOA)
WeakPtr<MockDisplayCapturer> m_capturer;
#endif
};
class MockRealtimeAudioSourceFactory final
#if PLATFORM(COCOA)
: public CoreAudioCaptureSourceFactory
#else
: public AudioCaptureFactory
#endif
{
public:
CaptureSourceOrError createAudioCaptureSource(const CaptureDevice& device, MediaDeviceHashSalts&& hashSalts, const MediaConstraints* constraints, std::optional<PageIdentifier> pageIdentifier) final
{
ASSERT(device.type() == CaptureDevice::DeviceType::Microphone || device.type() == CaptureDevice::DeviceType::Speaker);
if (!MockRealtimeMediaSourceCenter::captureDeviceWithPersistentID(device.type(), device.persistentId()))
return CaptureSourceOrError({ "Unable to find mock microphone or speaker device with given persistentID"_s, MediaAccessDenialReason::PermissionDenied });
auto mock = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(device.persistentId());
ASSERT(mock);
if (mock->flags.contains(MockMediaDevice::Flag::Invalid))
return CaptureSourceOrError({ "Invalid mock microphone or speaker device"_s, MediaAccessDenialReason::PermissionDenied });
return MockRealtimeAudioSource::create(String { device.persistentId() }, AtomString { device.label() }, WTFMove(hashSalts), constraints, pageIdentifier);
}
private:
CaptureDeviceManager& audioCaptureDeviceManager() final { return MockRealtimeMediaSourceCenter::singleton().audioCaptureDeviceManager(); }
const Vector<CaptureDevice>& speakerDevices() const final { return MockRealtimeMediaSourceCenter::speakerDevices(); }
};
static Vector<MockMediaDevice>& devices()
{
static NeverDestroyed devices = defaultDevices();
return devices;
}
static HashMap<String, MockMediaDevice>& deviceMap()
{
static NeverDestroyed map = [] {
HashMap<String, MockMediaDevice> map;
for (auto& device : devices())
map.add(device.persistentId, device);
return map;
}();
return map;
}
static inline Vector<CaptureDevice>& deviceListForDevice(const MockMediaDevice& device)
{
if (device.isMicrophone())
return MockRealtimeMediaSourceCenter::microphoneDevices();
if (device.isSpeaker())
return MockRealtimeMediaSourceCenter::speakerDevices();
if (device.isCamera())
return MockRealtimeMediaSourceCenter::videoDevices();
ASSERT(device.isDisplay());
return MockRealtimeMediaSourceCenter::displayDevices();
}
MockRealtimeMediaSourceCenter& MockRealtimeMediaSourceCenter::singleton()
{
static NeverDestroyed<MockRealtimeMediaSourceCenter> center;
return center;
}
void MockRealtimeMediaSourceCenter::setMockRealtimeMediaSourceCenterEnabled(bool enabled)
{
MockRealtimeMediaSourceCenter& mock = singleton();
if (mock.m_isEnabled == enabled)
return;
mock.m_isEnabled = enabled;
RealtimeMediaSourceCenter& center = RealtimeMediaSourceCenter::singleton();
if (mock.m_isEnabled) {
if (mock.m_isMockAudioCaptureEnabled) {
#if PLATFORM(COCOA)
MockAudioSharedUnit::enable();
#endif
center.setAudioCaptureFactory(mock.audioCaptureFactory());
}
if (mock.m_isMockVideoCaptureEnabled)
center.setVideoCaptureFactory(mock.videoCaptureFactory());
if (mock.m_isMockDisplayCaptureEnabled)
center.setDisplayCaptureFactory(mock.displayCaptureFactory());
return;
}
if (mock.m_isMockAudioCaptureEnabled) {
#if PLATFORM(COCOA)
MockAudioSharedUnit::disable();
#endif
center.unsetAudioCaptureFactory(mock.audioCaptureFactory());
}
if (mock.m_isMockVideoCaptureEnabled)
center.unsetVideoCaptureFactory(mock.videoCaptureFactory());
if (mock.m_isMockDisplayCaptureEnabled)
center.unsetDisplayCaptureFactory(mock.displayCaptureFactory());
}
bool MockRealtimeMediaSourceCenter::mockRealtimeMediaSourceCenterEnabled()
{
return singleton().m_isEnabled;
}
static CaptureDevice toCaptureDevice(const MockMediaDevice& device)
{
auto captureDevice = device.captureDevice();
captureDevice.setEnabled(true);
captureDevice.setIsMockDevice(true);
return captureDevice;
}
static void createMockDevice(const MockMediaDevice& device, bool isDefault)
{
if (isDefault)
deviceListForDevice(device).insert(0, toCaptureDevice(device));
else
deviceListForDevice(device).append(toCaptureDevice(device));
}
void MockRealtimeMediaSourceCenter::resetDevices()
{
setDevices(defaultDevices());
RealtimeMediaSourceCenter::singleton().captureDevicesChanged();
}
void MockRealtimeMediaSourceCenter::setMockCaptureDevicesInterrupted(bool isCameraInterrupted, bool isMicrophoneInterrupted)
{
MockRealtimeVideoSource::setIsInterrupted(isCameraInterrupted);
MockRealtimeAudioSource::setIsInterrupted(isMicrophoneInterrupted);
}
void MockRealtimeMediaSourceCenter::triggerMockCaptureConfigurationChange(bool forCamera, bool forMicrophone, bool forDisplay)
{
if (forCamera)
MockRealtimeVideoSource::triggerCameraConfigurationChange();
#if PLATFORM(COCOA)
if (forMicrophone) {
auto devices = audioCaptureDeviceManager().captureDevices();
if (devices.size() > 1) {
MockAudioSharedUnit::increaseBufferSize();
CoreAudioSharedUnit::singleton().handleNewCurrentMicrophoneDevice(WTFMove(devices[1]));
}
}
if (forDisplay) {
if (auto capturer = MockRealtimeDisplaySourceFactory::singleton().latestCapturer())
capturer->triggerMockCaptureConfigurationChange();
}
#else
UNUSED_PARAM(forMicrophone);
UNUSED_PARAM(forDisplay);
#endif
}
void MockRealtimeMediaSourceCenter::setDevices(Vector<MockMediaDevice>&& newMockDevices)
{
microphoneDevices().clear();
speakerDevices().clear();
videoDevices().clear();
displayDevices().clear();
auto& mockDevices = devices();
for (auto& device : mockDevices) {
auto& persistentId = device.persistentId;
if (!newMockDevices.containsIf([&persistentId](auto& newDevice) -> bool {
return newDevice.persistentId == persistentId;
}))
RealtimeMediaSourceCenter::singleton().captureDeviceWillBeRemoved(persistentId);
}
mockDevices = WTFMove(newMockDevices);
auto& map = deviceMap();
map.clear();
for (const auto& device : mockDevices) {
map.add(device.persistentId, device);
createMockDevice(device, false);
}
RealtimeMediaSourceCenter::singleton().captureDevicesChanged();
}
static bool shouldBeDefaultDevice(const MockMediaDevice& device)
{
auto* cameraProperties = device.cameraProperties();
return cameraProperties && cameraProperties->facingMode == VideoFacingMode::Unknown;
}
void MockRealtimeMediaSourceCenter::addDevice(const MockMediaDevice& device)
{
bool isDefault = shouldBeDefaultDevice(device);
if (isDefault)
devices().insert(0, device);
else
devices().append(device);
deviceMap().set(device.persistentId, device);
createMockDevice(device, isDefault);
RealtimeMediaSourceCenter::singleton().captureDevicesChanged();
}
void MockRealtimeMediaSourceCenter::removeDevice(const String& persistentId)
{
auto& map = deviceMap();
auto iterator = map.find(persistentId);
if (iterator == map.end())
return;
RealtimeMediaSourceCenter::singleton().captureDeviceWillBeRemoved(persistentId);
devices().removeFirstMatching([&persistentId](const auto& device) {
return device.persistentId == persistentId;
});
deviceListForDevice(iterator->value).removeFirstMatching([&persistentId](const auto& device) {
return device.persistentId() == persistentId;
});
map.remove(iterator);
RealtimeMediaSourceCenter::singleton().captureDevicesChanged();
}
void MockRealtimeMediaSourceCenter::setDeviceIsEphemeral(const String& persistentId, bool isEphemeral)
{
ASSERT(!persistentId.isEmpty());
auto& map = deviceMap();
auto iterator = map.find(persistentId);
if (iterator == map.end())
return;
MockMediaDevice device = iterator->value;
if (isEphemeral)
device.flags.add(MockMediaDevice::Flag::Ephemeral);
else
device.flags.remove(MockMediaDevice::Flag::Ephemeral);
removeDevice(persistentId);
addDevice(device);
}
std::optional<MockMediaDevice> MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(const String& id)
{
ASSERT(!id.isEmpty());
auto& map = deviceMap();
auto iterator = map.find(id);
if (iterator == map.end())
return std::nullopt;
return iterator->value;
}
std::optional<CaptureDevice> MockRealtimeMediaSourceCenter::captureDeviceWithPersistentID(CaptureDevice::DeviceType type, const String& id)
{
ASSERT(!id.isEmpty());
auto& map = deviceMap();
auto iterator = map.find(id);
if (iterator == map.end() || iterator->value.type() != type)
return std::nullopt;
return toCaptureDevice(iterator->value);
}
Vector<CaptureDevice>& MockRealtimeMediaSourceCenter::microphoneDevices()
{
static NeverDestroyed microphoneDevices = [] {
Vector<CaptureDevice> microphoneDevices;
for (const auto& device : devices()) {
if (device.isMicrophone())
microphoneDevices.append(toCaptureDevice(device));
}
return microphoneDevices;
}();
return microphoneDevices;
}
Vector<CaptureDevice>& MockRealtimeMediaSourceCenter::speakerDevices()
{
static NeverDestroyed speakerDevices = [] {
Vector<CaptureDevice> speakerDevices;
for (const auto& device : devices()) {
if (device.isSpeaker())
speakerDevices.append(toCaptureDevice(device));
}
return speakerDevices;
}();
return speakerDevices;
}
Vector<CaptureDevice>& MockRealtimeMediaSourceCenter::videoDevices()
{
static NeverDestroyed videoDevices = [] {
Vector<CaptureDevice> videoDevices;
for (const auto& device : devices()) {
if (device.isCamera())
videoDevices.append(toCaptureDevice(device));
}
return videoDevices;
}();
return videoDevices;
}
Vector<CaptureDevice>& MockRealtimeMediaSourceCenter::displayDevices()
{
static NeverDestroyed displayDevices = [] {
Vector<CaptureDevice> displayDevices;
for (const auto& device : devices()) {
if (device.isDisplay())
displayDevices.append(toCaptureDevice(device));
}
return displayDevices;
}();
return displayDevices;
}
AudioCaptureFactory& MockRealtimeMediaSourceCenter::audioCaptureFactory()
{
static NeverDestroyed<MockRealtimeAudioSourceFactory> factory;
return factory.get();
}
VideoCaptureFactory& MockRealtimeMediaSourceCenter::videoCaptureFactory()
{
static NeverDestroyed<MockRealtimeVideoSourceFactory> factory;
return factory.get();
}
DisplayCaptureFactory& MockRealtimeMediaSourceCenter::displayCaptureFactory()
{
return MockRealtimeDisplaySourceFactory::singleton();
}
MockRealtimeDisplaySourceFactory& MockRealtimeDisplaySourceFactory::singleton()
{
static NeverDestroyed<MockRealtimeDisplaySourceFactory> factory;
return factory.get();
}
} // namespace WebCore
#endif // ENABLE(MEDIA_STREAM)
|