[WTF] Move currentCPUTime and sleep(Seconds) to CPUTime.h and Seconds.h respectively
[WebKit-https.git] / Source / WebCore / platform / mock / MockRealtimeMediaSource.cpp
1 /*
2  * Copyright (C) 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in the documentation and/or other materials provided with the
13  *    distribution.
14  * 3. Neither the name of Google Inc. nor the names of its contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "MockRealtimeMediaSource.h"
33
34 #if ENABLE(MEDIA_STREAM)
35
36 #include "CaptureDevice.h"
37 #include "Logging.h"
38 #include "MediaConstraints.h"
39 #include "NotImplemented.h"
40 #include "RealtimeMediaSourceSettings.h"
41 #include <math.h>
42 #include <wtf/NeverDestroyed.h>
43 #include <wtf/text/StringView.h>
44
45 namespace WebCore {
46
47 struct MockDeviceInfo {
48     const char* id;
49     CaptureDevice::DeviceType type;
50     const char* name;
51     MockRealtimeMediaSource::MockDevice device;
52 };
53
54 static const HashMap<String, MockDeviceInfo>& deviceMap()
55 {
56     static const auto infoMap = makeNeverDestroyed([] {
57         static const MockDeviceInfo devices[] = {
58             { "239c24b0-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Microphone, "Mock audio device 1", MockRealtimeMediaSource::MockDevice::Microphone1 },
59             { "239c24b1-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Microphone, "Mock audio device 2", MockRealtimeMediaSource::MockDevice::Microphone2 },
60
61             { "239c24b2-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Camera, "Mock video device 1", MockRealtimeMediaSource::MockDevice::Camera1 },
62             { "239c24b3-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Camera, "Mock video device 2", MockRealtimeMediaSource::MockDevice::Camera2 },
63
64             { "SCREEN-1", CaptureDevice::DeviceType::Screen, "Mock screen device 1", MockRealtimeMediaSource::MockDevice::Screen1 },
65             { "SCREEN-2", CaptureDevice::DeviceType::Screen, "Mock screen device 2", MockRealtimeMediaSource::MockDevice::Screen2 },
66         };
67
68         HashMap<String, MockDeviceInfo> map;
69         for (auto& info : devices)
70             map.add(ASCIILiteral(info.id), info);
71         return map;
72     }());
73
74     return infoMap;
75 }
76
77 std::optional<CaptureDevice> MockRealtimeMediaSource::captureDeviceWithPersistentID(CaptureDevice::DeviceType type, const String& id)
78 {
79     ASSERT(!id.isEmpty());
80
81     auto map = deviceMap();
82     auto it = map.find(id);
83     if (it != map.end() && it->value.type == type) {
84         auto device = CaptureDevice(it->value.id, it->value.type, it->value.name);
85         device.setEnabled(true);
86         return WTFMove(device);
87     }
88
89     return std::nullopt;
90 }
91
92 Vector<CaptureDevice>& MockRealtimeMediaSource::audioDevices()
93 {
94     static auto info = makeNeverDestroyed([] {
95         Vector<CaptureDevice> vector;
96
97         auto captureDevice = captureDeviceWithPersistentID(CaptureDevice::DeviceType::Microphone, ASCIILiteral("239c24b0-2b15-11e3-8224-0800200c9a66"));
98         ASSERT(captureDevice);
99         vector.append(WTFMove(captureDevice.value()));
100
101         captureDevice = captureDeviceWithPersistentID(CaptureDevice::DeviceType::Microphone, ASCIILiteral("239c24b1-2b15-11e3-8224-0800200c9a66"));
102         ASSERT(captureDevice);
103         vector.append(WTFMove(captureDevice.value()));
104
105         return vector;
106     }());
107     return info;
108 }
109
110 Vector<CaptureDevice>& MockRealtimeMediaSource::videoDevices()
111 {
112     static auto info = makeNeverDestroyed([] {
113         Vector<CaptureDevice> vector;
114
115         auto captureDevice = captureDeviceWithPersistentID(CaptureDevice::DeviceType::Camera, ASCIILiteral("239c24b2-2b15-11e3-8224-0800200c9a66"));
116         ASSERT(captureDevice);
117         vector.append(WTFMove(captureDevice.value()));
118
119         captureDevice = captureDeviceWithPersistentID(CaptureDevice::DeviceType::Camera, ASCIILiteral("239c24b3-2b15-11e3-8224-0800200c9a66"));
120         ASSERT(captureDevice);
121         vector.append(WTFMove(captureDevice.value()));
122
123         return vector;
124     }());
125     return info;
126 }
127
128 Vector<CaptureDevice>& MockRealtimeMediaSource::displayDevices()
129 {
130     static auto devices = makeNeverDestroyed([] {
131         Vector<CaptureDevice> vector;
132
133         auto captureDevice = captureDeviceWithPersistentID(CaptureDevice::DeviceType::Screen, ASCIILiteral("SCREEN-1"));
134         ASSERT(captureDevice);
135         vector.append(WTFMove(captureDevice.value()));
136
137         captureDevice = captureDeviceWithPersistentID(CaptureDevice::DeviceType::Screen, ASCIILiteral("SCREEN-2"));
138         ASSERT(captureDevice);
139         vector.append(WTFMove(captureDevice.value()));
140
141         return vector;
142     }());
143
144     return devices;
145 }
146
147 MockRealtimeMediaSource::MockRealtimeMediaSource(const String& id, RealtimeMediaSource::Type type, const String& name)
148     : RealtimeMediaSource(id, type, name)
149 {
150     ASSERT(type != RealtimeMediaSource::Type::None);
151
152     auto map = deviceMap();
153     auto it = map.find(id);
154     ASSERT(it != map.end());
155
156     m_device = it->value.device;
157     setPersistentID(String(id));
158 }
159
160 void MockRealtimeMediaSource::initializeCapabilities()
161 {
162     m_capabilities = std::make_unique<RealtimeMediaSourceCapabilities>(supportedConstraints());
163     m_capabilities->setDeviceId(id());
164     initializeCapabilities(*m_capabilities.get());
165 }
166
167 const RealtimeMediaSourceCapabilities& MockRealtimeMediaSource::capabilities() const
168 {
169     if (!m_capabilities)
170         const_cast<MockRealtimeMediaSource&>(*this).initializeCapabilities();
171     return *m_capabilities;
172 }
173
174 void MockRealtimeMediaSource::initializeSettings()
175 {
176     if (m_currentSettings.deviceId().isEmpty()) {
177         m_currentSettings.setSupportedConstraints(supportedConstraints());
178         m_currentSettings.setDeviceId(id());
179         m_currentSettings.setLabel(name());
180     }
181
182     updateSettings(m_currentSettings);
183 }
184
185 const RealtimeMediaSourceSettings& MockRealtimeMediaSource::settings() const
186 {
187     const_cast<MockRealtimeMediaSource&>(*this).initializeSettings();
188     return m_currentSettings;
189 }
190
191 RealtimeMediaSourceSupportedConstraints& MockRealtimeMediaSource::supportedConstraints()
192 {
193     if (m_supportedConstraints.supportsDeviceId())
194         return m_supportedConstraints;
195
196     m_supportedConstraints.setSupportsDeviceId(true);
197     initializeSupportedConstraints(m_supportedConstraints);
198
199     return m_supportedConstraints;
200 }
201
202 } // namespace WebCore
203
204 #endif // ENABLE(MEDIA_STREAM)