1 | /* |
2 | * Copyright (C) 2018 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 | * |
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
18 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
19 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
21 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
25 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | |
28 | #pragma once |
29 | |
30 | #if ENABLE(MEDIA_STREAM) |
31 | |
32 | #include "CaptureDevice.h" |
33 | #include "RealtimeMediaSource.h" |
34 | #include "RealtimeVideoSource.h" |
35 | |
36 | namespace WebCore { |
37 | |
38 | struct MockMicrophoneProperties { |
39 | template<class Encoder> |
40 | void encode(Encoder& encoder) const |
41 | { |
42 | encoder << static_cast<int32_t>(defaultSampleRate); |
43 | } |
44 | |
45 | template <class Decoder> |
46 | static Optional<MockMicrophoneProperties> decode(Decoder& decoder) |
47 | { |
48 | Optional<int32_t> defaultSampleRate; |
49 | decoder >> defaultSampleRate; |
50 | if (!defaultSampleRate) |
51 | return WTF::nullopt; |
52 | return MockMicrophoneProperties { *defaultSampleRate }; |
53 | } |
54 | |
55 | int defaultSampleRate { 44100 }; |
56 | }; |
57 | |
58 | // FIXME: Add support for other properties. |
59 | struct MockCameraProperties { |
60 | template<class Encoder> |
61 | void encode(Encoder& encoder) const |
62 | { |
63 | encoder << defaultFrameRate; |
64 | encoder << facingMode; |
65 | encoder << presets; |
66 | encoder << fillColor; |
67 | } |
68 | |
69 | template <class Decoder> |
70 | static Optional<MockCameraProperties> decode(Decoder& decoder) |
71 | { |
72 | Optional<double> defaultFrameRate; |
73 | decoder >> defaultFrameRate; |
74 | if (!defaultFrameRate) |
75 | return WTF::nullopt; |
76 | |
77 | Optional<RealtimeMediaSourceSettings::VideoFacingMode> facingMode; |
78 | decoder >> facingMode; |
79 | if (!facingMode) |
80 | return WTF::nullopt; |
81 | |
82 | Optional<Vector<VideoPresetData>> presets; |
83 | decoder >> presets; |
84 | if (!presets) |
85 | return WTF::nullopt; |
86 | |
87 | Optional<Color> fillColor; |
88 | decoder >> fillColor; |
89 | if (!fillColor) |
90 | return WTF::nullopt; |
91 | |
92 | return MockCameraProperties { *defaultFrameRate, *facingMode, WTFMove(*presets), *fillColor }; |
93 | } |
94 | |
95 | double defaultFrameRate { 30 }; |
96 | RealtimeMediaSourceSettings::VideoFacingMode facingMode { RealtimeMediaSourceSettings::VideoFacingMode::User }; |
97 | Vector<VideoPresetData> presets { { { 640, 480 }, { { 30, 30}, { 15, 15 } } } }; |
98 | Color fillColor { Color::black }; |
99 | }; |
100 | |
101 | struct MockDisplayProperties { |
102 | template<class Encoder> |
103 | void encode(Encoder& encoder) const |
104 | { |
105 | encoder.encodeEnum(type); |
106 | encoder << fillColor; |
107 | encoder << defaultSize; |
108 | } |
109 | |
110 | template <class Decoder> |
111 | static Optional<MockDisplayProperties> decode(Decoder& decoder) |
112 | { |
113 | Optional<CaptureDevice::DeviceType> type; |
114 | decoder >> type; |
115 | return WTF::nullopt; |
116 | |
117 | Optional<Color> fillColor; |
118 | decoder >> fillColor; |
119 | if (!fillColor) |
120 | return WTF::nullopt; |
121 | |
122 | Optional<IntSize> defaultSize; |
123 | decoder >> defaultSize; |
124 | if (!defaultSize) |
125 | return WTF::nullopt; |
126 | |
127 | return MockDisplayProperties { *type, *fillColor, *defaultSize }; |
128 | } |
129 | |
130 | CaptureDevice::DeviceType type; |
131 | Color fillColor { Color::lightGray }; |
132 | IntSize defaultSize; |
133 | }; |
134 | |
135 | struct MockMediaDevice { |
136 | bool isMicrophone() const { return WTF::holds_alternative<MockMicrophoneProperties>(properties); } |
137 | bool isCamera() const { return WTF::holds_alternative<MockCameraProperties>(properties); } |
138 | bool isDisplay() const { return WTF::holds_alternative<MockDisplayProperties>(properties); } |
139 | |
140 | CaptureDevice::DeviceType type() const |
141 | { |
142 | if (isMicrophone()) |
143 | return CaptureDevice::DeviceType::Microphone; |
144 | if (isCamera()) |
145 | return CaptureDevice::DeviceType::Camera; |
146 | |
147 | ASSERT(isDisplay()); |
148 | return WTF::get<MockDisplayProperties>(properties).type; |
149 | } |
150 | |
151 | template<class Encoder> |
152 | void encode(Encoder& encoder) const |
153 | { |
154 | encoder << persistentId; |
155 | encoder << label; |
156 | switchOn(properties, [&](const MockMicrophoneProperties& properties) { |
157 | encoder << (uint8_t)1; |
158 | encoder << properties; |
159 | }, [&](const MockCameraProperties& properties) { |
160 | encoder << (uint8_t)2; |
161 | encoder << properties; |
162 | }, [&](const MockDisplayProperties& properties) { |
163 | encoder << (uint8_t)3; |
164 | encoder << properties; |
165 | }); |
166 | } |
167 | |
168 | template <typename Properties, typename Decoder> |
169 | static Optional<MockMediaDevice> decodeMockMediaDevice(Decoder& decoder, String&& persistentId, String&& label) |
170 | { |
171 | Optional<Properties> properties; |
172 | decoder >> properties; |
173 | if (!properties) |
174 | return WTF::nullopt; |
175 | return MockMediaDevice { WTFMove(persistentId), WTFMove(label), WTFMove(*properties) }; |
176 | } |
177 | |
178 | template <class Decoder> |
179 | static Optional<MockMediaDevice> decode(Decoder& decoder) |
180 | { |
181 | Optional<String> persistentId; |
182 | decoder >> persistentId; |
183 | if (!persistentId) |
184 | return WTF::nullopt; |
185 | |
186 | Optional<String> label; |
187 | decoder >> label; |
188 | if (!label) |
189 | return WTF::nullopt; |
190 | |
191 | Optional<uint8_t> index; |
192 | decoder >> index; |
193 | if (!index) |
194 | return WTF::nullopt; |
195 | |
196 | switch (*index) { |
197 | case 1: |
198 | return decodeMockMediaDevice<MockMicrophoneProperties>(decoder, WTFMove(*persistentId), WTFMove(*label)); |
199 | case 2: |
200 | return decodeMockMediaDevice<MockCameraProperties>(decoder, WTFMove(*persistentId), WTFMove(*label)); |
201 | case 3: |
202 | return decodeMockMediaDevice<MockDisplayProperties>(decoder, WTFMove(*persistentId), WTFMove(*label)); |
203 | } |
204 | return WTF::nullopt; |
205 | } |
206 | |
207 | String persistentId; |
208 | String label; |
209 | Variant<MockMicrophoneProperties, MockCameraProperties, MockDisplayProperties> properties; |
210 | }; |
211 | |
212 | } // namespace WebCore |
213 | |
214 | #endif // ENABLE(MEDIA_STREAM) |
215 | |