fake_video_source_provider.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/capture_mode/fake_video_source_provider.h"
  5. #include "base/check.h"
  6. #include "base/system/system_monitor.h"
  7. #include "base/threading/thread_task_runner_handle.h"
  8. #include "media/base/video_types.h"
  9. #include "media/capture/video/video_capture_device_descriptor.h"
  10. #include "media/capture/video_capture_types.h"
  11. namespace ash {
  12. namespace {
  13. // Triggers a notification that video capture devices have changed.
  14. void NotifyVideoCaptureDevicesChanged() {
  15. base::SystemMonitor::Get()->ProcessDevicesChanged(
  16. base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
  17. }
  18. media::VideoCaptureFormat CreateCaptureFormat(const gfx::Size& frame_size,
  19. float frame_rate) {
  20. return media::VideoCaptureFormat(frame_size, frame_rate,
  21. media::PIXEL_FORMAT_I420);
  22. }
  23. media::VideoCaptureFormats GenerateCaptureFormatList() {
  24. return media::VideoCaptureFormats{
  25. CreateCaptureFormat(gfx::Size(160, 120), 30.f),
  26. CreateCaptureFormat(gfx::Size(160, 120), 20.f),
  27. CreateCaptureFormat(gfx::Size(176, 144), 30.f),
  28. CreateCaptureFormat(gfx::Size(176, 144), 24.f),
  29. CreateCaptureFormat(gfx::Size(320, 180), 30.f),
  30. CreateCaptureFormat(gfx::Size(320, 180), 24.f),
  31. CreateCaptureFormat(gfx::Size(320, 180), 7.f),
  32. };
  33. }
  34. media::VideoCaptureDeviceInfo CreateCaptureDeviceInfo(
  35. const std::string& device_id,
  36. const std::string& display_name,
  37. const std::string& model_id,
  38. media::VideoFacingMode camera_facing_mode) {
  39. media::VideoCaptureDeviceInfo info{media::VideoCaptureDeviceDescriptor(
  40. display_name, device_id, model_id, media::VideoCaptureApi::UNKNOWN,
  41. media::VideoCaptureControlSupport())};
  42. info.supported_formats = GenerateCaptureFormatList();
  43. info.descriptor.facing = camera_facing_mode;
  44. return info;
  45. }
  46. } // namespace
  47. FakeVideoSourceProvider::FakeVideoSourceProvider() = default;
  48. FakeVideoSourceProvider::~FakeVideoSourceProvider() = default;
  49. void FakeVideoSourceProvider::Bind(
  50. mojo::PendingReceiver<video_capture::mojom::VideoSourceProvider>
  51. pending_receiver) {
  52. receiver_.Bind(std::move(pending_receiver));
  53. }
  54. void FakeVideoSourceProvider::TriggerFatalErrorOnCamera(
  55. const std::string& device_id) {
  56. DCHECK(devices_map_.contains(device_id));
  57. devices_map_.at(device_id)->TriggerFatalError();
  58. }
  59. void FakeVideoSourceProvider::AddFakeCamera(
  60. const std::string& device_id,
  61. const std::string& display_name,
  62. const std::string& model_id,
  63. media::VideoFacingMode camera_facing_mode) {
  64. AddFakeCameraWithoutNotifying(device_id, display_name, model_id,
  65. camera_facing_mode);
  66. NotifyVideoCaptureDevicesChanged();
  67. }
  68. void FakeVideoSourceProvider::AddFakeCameraWithoutNotifying(
  69. const std::string& device_id,
  70. const std::string& display_name,
  71. const std::string& model_id,
  72. media::VideoFacingMode camera_facing_mode) {
  73. const auto iter = devices_map_.emplace(
  74. device_id, std::make_unique<FakeCameraDevice>(CreateCaptureDeviceInfo(
  75. device_id, display_name, model_id, camera_facing_mode)));
  76. DCHECK(iter.second);
  77. }
  78. void FakeVideoSourceProvider::RemoveFakeCamera(const std::string& device_id) {
  79. RemoveFakeCameraWithoutNotifying(device_id);
  80. NotifyVideoCaptureDevicesChanged();
  81. }
  82. void FakeVideoSourceProvider::RemoveFakeCameraWithoutNotifying(
  83. const std::string& device_id) {
  84. DCHECK(devices_map_.contains(device_id));
  85. devices_map_.erase(device_id);
  86. }
  87. void FakeVideoSourceProvider::GetSourceInfos(GetSourceInfosCallback callback) {
  88. DCHECK(callback);
  89. std::vector<media::VideoCaptureDeviceInfo> devices;
  90. for (const auto& pair : devices_map_)
  91. devices.emplace_back(pair.second->device_info());
  92. // Simulate the asynchronously behavior of the actual VideoSourceProvider
  93. // which does a lot of asynchronous and mojo calls.
  94. base::ThreadTaskRunnerHandle::Get()->PostTask(
  95. FROM_HERE, base::BindOnce(std::move(callback), devices));
  96. if (on_replied_with_source_infos_)
  97. std::move(on_replied_with_source_infos_).Run();
  98. }
  99. void FakeVideoSourceProvider::GetVideoSource(
  100. const std::string& source_id,
  101. mojo::PendingReceiver<video_capture::mojom::VideoSource> stream) {
  102. // The camera device may get removed after the client has issued an
  103. // asynchronous mojo call to `GetVideoSource()`.
  104. auto iter = devices_map_.find(source_id);
  105. if (iter == devices_map_.end())
  106. return;
  107. iter->second->Bind(std::move(stream));
  108. }
  109. } // namespace ash