File: fake_video_source_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (136 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download | duplicates (6)
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
// 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 "ash/capture_mode/fake_video_source_provider.h"

#include "base/check.h"
#include "base/system/system_monitor.h"
#include "base/task/single_thread_task_runner.h"
#include "media/base/video_types.h"
#include "media/capture/video/video_capture_device_descriptor.h"
#include "media/capture/video_capture_types.h"

namespace ash {

namespace {

// Triggers a notification that video capture devices have changed.
void NotifyVideoCaptureDevicesChanged() {
  base::SystemMonitor::Get()->ProcessDevicesChanged(
      base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE);
}

media::VideoCaptureFormat CreateCaptureFormat(const gfx::Size& frame_size,
                                              float frame_rate) {
  return media::VideoCaptureFormat(frame_size, frame_rate,
                                   media::PIXEL_FORMAT_I420);
}

media::VideoCaptureFormats GenerateCaptureFormatList() {
  return media::VideoCaptureFormats{
      CreateCaptureFormat(gfx::Size(160, 120), 30.f),
      CreateCaptureFormat(gfx::Size(160, 120), 20.f),
      CreateCaptureFormat(gfx::Size(176, 144), 30.f),
      CreateCaptureFormat(gfx::Size(176, 144), 24.f),
      CreateCaptureFormat(gfx::Size(320, 180), 30.f),
      CreateCaptureFormat(gfx::Size(320, 180), 24.f),
      CreateCaptureFormat(gfx::Size(320, 180), 7.f),
  };
}

media::VideoCaptureDeviceInfo CreateCaptureDeviceInfo(
    const std::string& device_id,
    const std::string& display_name,
    const std::string& model_id,
    media::VideoFacingMode camera_facing_mode) {
  media::VideoCaptureDeviceInfo info{media::VideoCaptureDeviceDescriptor(
      display_name, device_id, model_id, media::VideoCaptureApi::UNKNOWN,
      media::VideoCaptureControlSupport())};
  info.supported_formats = GenerateCaptureFormatList();
  info.descriptor.facing = camera_facing_mode;
  return info;
}

}  // namespace

FakeVideoSourceProvider::FakeVideoSourceProvider() = default;

FakeVideoSourceProvider::~FakeVideoSourceProvider() = default;

void FakeVideoSourceProvider::Bind(
    mojo::PendingReceiver<video_capture::mojom::VideoSourceProvider>
        pending_receiver) {
  receiver_.Bind(std::move(pending_receiver));
}

void FakeVideoSourceProvider::TriggerFatalErrorOnCamera(
    const std::string& device_id) {
  DCHECK(devices_map_.contains(device_id));
  devices_map_.at(device_id)->TriggerFatalError();
}

void FakeVideoSourceProvider::AddFakeCamera(
    const std::string& device_id,
    const std::string& display_name,
    const std::string& model_id,
    media::VideoFacingMode camera_facing_mode) {
  AddFakeCameraWithoutNotifying(device_id, display_name, model_id,
                                camera_facing_mode);
  NotifyVideoCaptureDevicesChanged();
}

void FakeVideoSourceProvider::AddFakeCameraWithoutNotifying(
    const std::string& device_id,
    const std::string& display_name,
    const std::string& model_id,
    media::VideoFacingMode camera_facing_mode) {
  const auto iter = devices_map_.emplace(
      device_id, std::make_unique<FakeCameraDevice>(CreateCaptureDeviceInfo(
                     device_id, display_name, model_id, camera_facing_mode)));
  DCHECK(iter.second);
}

void FakeVideoSourceProvider::RemoveFakeCamera(const std::string& device_id) {
  RemoveFakeCameraWithoutNotifying(device_id);
  NotifyVideoCaptureDevicesChanged();
}

void FakeVideoSourceProvider::RemoveFakeCameraWithoutNotifying(
    const std::string& device_id) {
  DCHECK(devices_map_.contains(device_id));
  devices_map_.erase(device_id);
}

void FakeVideoSourceProvider::GetSourceInfos(GetSourceInfosCallback callback) {
  DCHECK(callback);

  std::vector<media::VideoCaptureDeviceInfo> devices;
  for (const auto& pair : devices_map_)
    devices.emplace_back(pair.second->device_info());

  // Simulate the asynchronously behavior of the actual VideoSourceProvider
  // which does a lot of asynchronous and mojo calls.
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback),
                                video_capture::mojom::VideoSourceProvider::
                                    GetSourceInfosResult::kSuccess,
                                devices));

  if (on_replied_with_source_infos_)
    std::move(on_replied_with_source_infos_).Run();
}

void FakeVideoSourceProvider::GetVideoSource(
    const std::string& source_id,
    mojo::PendingReceiver<video_capture::mojom::VideoSource> stream) {
  // The camera device may get removed after the client has issued an
  // asynchronous mojo call to `GetVideoSource()`.
  auto iter = devices_map_.find(source_id);
  if (iter == devices_map_.end())
    return;

  iter->second->Bind(std::move(stream));
}

}  // namespace ash