[MediaStream] RealtimeMediaSource should be able to vend hashed IDs
[WebKit-https.git] / Source / WebCore / platform / mock / MockRealtimeVideoSource.cpp
1 /*
2  * Copyright (C) 2015-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  * 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 "MockRealtimeVideoSource.h"
33
34 #if ENABLE(MEDIA_STREAM)
35 #include "CaptureDevice.h"
36 #include "GraphicsContext.h"
37 #include "ImageBuffer.h"
38 #include "IntRect.h"
39 #include "Logging.h"
40 #include "MediaConstraints.h"
41 #include "MockRealtimeMediaSourceCenter.h"
42 #include "NotImplemented.h"
43 #include "PlatformLayer.h"
44 #include "RealtimeMediaSourceSettings.h"
45 #include <math.h>
46 #include <wtf/UUID.h>
47 #include <wtf/text/StringView.h>
48
49 namespace WebCore {
50
51 #if !PLATFORM(MAC) && !PLATFORM(IOS) && !(USE(GSTREAMER) && USE(LIBWEBRTC))
52 CaptureSourceOrError MockRealtimeVideoSource::create(String&& deviceID, String&& name, String&& hashSalt, const MediaConstraints* constraints)
53 {
54 #ifndef NDEBUG
55     auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(deviceID);
56     ASSERT(device);
57     if (!device)
58         return { };
59 #endif
60
61     auto source = adoptRef(*new MockRealtimeVideoSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)));
62     if (constraints && source->applyConstraints(*constraints))
63         return { };
64
65     return CaptureSourceOrError(WTFMove(source));
66 }
67 #endif
68
69 MockRealtimeVideoSource::MockRealtimeVideoSource(String&& deviceID, String&& name, String&& hashSalt)
70     : RealtimeVideoSource(WTFMove(name), WTFMove(deviceID), WTFMove(hashSalt))
71     , m_emitFrameTimer(RunLoop::current(), this, &MockRealtimeVideoSource::generateFrame)
72 {
73     auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(persistentID());
74     ASSERT(device);
75     m_device = *device;
76
77     m_dashWidths.reserveInitialCapacity(2);
78     m_dashWidths.uncheckedAppend(6);
79     m_dashWidths.uncheckedAppend(6);
80
81     if (mockDisplay()) {
82         auto& properties = WTF::get<MockDisplayProperties>(m_device.properties);
83         setSize(properties.defaultSize);
84         m_fillColor = properties.fillColor;
85         return;
86     }
87
88     auto& properties = WTF::get<MockCameraProperties>(m_device.properties);
89     setFrameRate(properties.defaultFrameRate);
90     setFacingMode(properties.facingMode);
91     m_fillColor = properties.fillColor;
92 }
93
94 bool MockRealtimeVideoSource::supportsSizeAndFrameRate(std::optional<int> width, std::optional<int> height, std::optional<double> rate)
95 {
96     // FIXME: consider splitting mock display into another class so we don't don't have to do this silly dance
97     // because of the RealtimeVideoSource inheritance.
98     if (mockCamera())
99         return RealtimeVideoSource::supportsSizeAndFrameRate(width, height, rate);
100
101     return RealtimeMediaSource::supportsSizeAndFrameRate(width, height, rate);
102 }
103
104 void MockRealtimeVideoSource::setSizeAndFrameRate(std::optional<int> width, std::optional<int> height, std::optional<double> rate)
105 {
106     // FIXME: consider splitting mock display into another class so we don't don't have to do this silly dance
107     // because of the RealtimeVideoSource inheritance.
108     if (mockCamera())
109         return RealtimeVideoSource::setSizeAndFrameRate(width, height, rate);
110
111     return RealtimeMediaSource::setSizeAndFrameRate(width, height, rate);
112 }
113
114 void MockRealtimeVideoSource::generatePresets()
115 {
116     ASSERT(mockCamera());
117     setSupportedPresets(WTFMove(WTF::get<MockCameraProperties>(m_device.properties).presets));
118 }
119
120 const RealtimeMediaSourceCapabilities& MockRealtimeVideoSource::capabilities()
121 {
122     if (!m_capabilities) {
123         RealtimeMediaSourceCapabilities capabilities(settings().supportedConstraints());
124
125         capabilities.setDeviceId(hashedId());
126         if (mockCamera()) {
127             capabilities.addFacingMode(WTF::get<MockCameraProperties>(m_device.properties).facingMode);
128             updateCapabilities(capabilities);
129         } else {
130             capabilities.setWidth(CapabilityValueOrRange(72, 2880));
131             capabilities.setHeight(CapabilityValueOrRange(45, 1800));
132             capabilities.setFrameRate(CapabilityValueOrRange(.01, 60.0));
133         }
134
135         m_capabilities = WTFMove(capabilities);
136     }
137
138     return m_capabilities.value();
139 }
140
141 const RealtimeMediaSourceSettings& MockRealtimeVideoSource::settings()
142 {
143     if (m_currentSettings)
144         return m_currentSettings.value();
145
146     RealtimeMediaSourceSettings settings;
147     if (mockCamera())
148         settings.setFacingMode(facingMode());
149     else {
150         settings.setDisplaySurface(mockScreen() ? RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor : RealtimeMediaSourceSettings::DisplaySurfaceType::Window);
151         settings.setLogicalSurface(false);
152     }
153     settings.setFrameRate(frameRate());
154     auto& size = this->size();
155     settings.setWidth(size.width());
156     settings.setHeight(size.height());
157     if (aspectRatio())
158         settings.setAspectRatio(aspectRatio());
159     settings.setDeviceId(hashedId());
160
161     RealtimeMediaSourceSupportedConstraints supportedConstraints;
162     supportedConstraints.setSupportsDeviceId(true);
163     supportedConstraints.setSupportsFrameRate(true);
164     supportedConstraints.setSupportsWidth(true);
165     supportedConstraints.setSupportsHeight(true);
166     supportedConstraints.setSupportsAspectRatio(true);
167     if (mockCamera())
168         supportedConstraints.setSupportsFacingMode(true);
169     else {
170         supportedConstraints.setSupportsDisplaySurface(true);
171         supportedConstraints.setSupportsLogicalSurface(true);
172     }
173     settings.setSupportedConstraints(supportedConstraints);
174
175     m_currentSettings = WTFMove(settings);
176
177     return m_currentSettings.value();
178 }
179
180 void MockRealtimeVideoSource::settingsDidChange(OptionSet<RealtimeMediaSourceSettings::Flag> settings)
181 {
182     m_currentSettings = std::nullopt;
183     if (settings.containsAny({ RealtimeMediaSourceSettings::Flag::Width, RealtimeMediaSourceSettings::Flag::Height })) {
184         m_baseFontSize = size().height() * .08;
185         m_bipBopFontSize = m_baseFontSize * 2.5;
186         m_statsFontSize = m_baseFontSize * .5;
187         m_imageBuffer = nullptr;
188     }
189 }
190
191 void MockRealtimeVideoSource::startCaptureTimer()
192 {
193     m_emitFrameTimer.startRepeating(1_s / frameRate());
194 }
195
196 void MockRealtimeVideoSource::startProducingData()
197 {
198     prepareToProduceData();
199     startCaptureTimer();
200     m_startTime = MonotonicTime::now();
201 }
202
203 void MockRealtimeVideoSource::stopProducingData()
204 {
205     m_emitFrameTimer.stop();
206     m_elapsedTime += MonotonicTime::now() - m_startTime;
207     m_startTime = MonotonicTime::nan();
208 }
209
210 Seconds MockRealtimeVideoSource::elapsedTime()
211 {
212     if (std::isnan(m_startTime))
213         return m_elapsedTime;
214
215     return m_elapsedTime + (MonotonicTime::now() - m_startTime);
216 }
217
218 void MockRealtimeVideoSource::drawAnimation(GraphicsContext& context)
219 {
220     float radius = size().width() * .09;
221     FloatPoint location(size().width() * .8, size().height() * .3);
222
223     m_path.clear();
224     m_path.moveTo(location);
225     m_path.addArc(location, radius, 0, 2 * piFloat, false);
226     m_path.closeSubpath();
227     context.setFillColor(Color::white);
228     context.setFillRule(WindRule::NonZero);
229     context.fillPath(m_path);
230
231     float endAngle = piFloat * (((fmod(m_frameNumber, frameRate()) + 0.5) * (2.0 / frameRate())) + 1);
232     m_path.clear();
233     m_path.moveTo(location);
234     m_path.addArc(location, radius, 1.5 * piFloat, endAngle, false);
235     m_path.closeSubpath();
236     context.setFillColor(Color::gray);
237     context.setFillRule(WindRule::NonZero);
238     context.fillPath(m_path);
239 }
240
241 void MockRealtimeVideoSource::drawBoxes(GraphicsContext& context)
242 {
243     static const RGBA32 magenta = 0xffff00ff;
244     static const RGBA32 yellow = 0xffffff00;
245     static const RGBA32 blue = 0xff0000ff;
246     static const RGBA32 red = 0xffff0000;
247     static const RGBA32 green = 0xff008000;
248     static const RGBA32 cyan = 0xFF00FFFF;
249
250     IntSize size = this->size();
251     float boxSize = size.width() * .035;
252     float boxTop = size.height() * .6;
253
254     m_path.clear();
255     FloatRect frameRect(2, 2, size.width() - 3, size.height() - 3);
256     context.setStrokeColor(Color::white);
257     context.setStrokeThickness(3);
258     context.setLineDash(m_dashWidths, 0);
259     m_path.addRect(frameRect);
260     m_path.closeSubpath();
261     context.strokePath(m_path);
262
263     context.setLineDash(DashArray(), 0);
264     m_path.clear();
265     m_path.moveTo(FloatPoint(0, boxTop + boxSize));
266     m_path.addLineTo(FloatPoint(size.width(), boxTop + boxSize));
267     m_path.closeSubpath();
268     context.setStrokeColor(Color::white);
269     context.setStrokeThickness(2);
270     context.strokePath(m_path);
271
272     context.setStrokeThickness(1);
273     float boxLeft = boxSize;
274     m_path.clear();
275     for (unsigned i = 0; i < boxSize / 4; i++) {
276         m_path.moveTo(FloatPoint(boxLeft + 4 * i, boxTop));
277         m_path.addLineTo(FloatPoint(boxLeft + 4 * i, boxTop + boxSize));
278     }
279     boxLeft += boxSize + 2;
280     for (unsigned i = 0; i < boxSize / 4; i++) {
281         m_path.moveTo(FloatPoint(boxLeft, boxTop + 4 * i));
282         m_path.addLineTo(FloatPoint(boxLeft + boxSize - 1, boxTop + 4 * i));
283     }
284     context.setStrokeThickness(3);
285     boxLeft += boxSize + 2;
286     for (unsigned i = 0; i < boxSize / 8; i++) {
287         m_path.moveTo(FloatPoint(boxLeft + 8 * i, boxTop));
288         m_path.addLineTo(FloatPoint(boxLeft + 8 * i, boxTop + boxSize - 1));
289     }
290     boxLeft += boxSize + 2;
291     for (unsigned i = 0; i < boxSize / 8; i++) {
292         m_path.moveTo(FloatPoint(boxLeft, boxTop + 8 * i));
293         m_path.addLineTo(FloatPoint(boxLeft + boxSize - 1, boxTop + 8 * i));
294     }
295
296     boxTop += boxSize + 2;
297     boxLeft = boxSize;
298     Color boxColors[] = { Color::white, yellow, cyan, green, magenta, red, blue };
299     for (unsigned i = 0; i < sizeof(boxColors) / sizeof(boxColors[0]); i++) {
300         context.fillRect(FloatRect(boxLeft, boxTop, boxSize + 1, boxSize + 1), boxColors[i]);
301         boxLeft += boxSize + 1;
302     }
303     context.strokePath(m_path);
304 }
305
306 void MockRealtimeVideoSource::drawText(GraphicsContext& context)
307 {
308     unsigned milliseconds = lround(elapsedTime().milliseconds());
309     unsigned seconds = milliseconds / 1000 % 60;
310     unsigned minutes = seconds / 60 % 60;
311     unsigned hours = minutes / 60 % 60;
312
313     FontCascadeDescription fontDescription;
314     fontDescription.setOneFamily("Courier");
315     fontDescription.setWeight(FontSelectionValue(500));
316
317     fontDescription.setSpecifiedSize(m_baseFontSize);
318     fontDescription.setComputedSize(m_baseFontSize);
319     FontCascade timeFont { FontCascadeDescription { fontDescription }, 0, 0 };
320     timeFont.update(nullptr);
321
322     fontDescription.setSpecifiedSize(m_bipBopFontSize);
323     fontDescription.setComputedSize(m_bipBopFontSize);
324     FontCascade bipBopFont { FontCascadeDescription { fontDescription }, 0, 0 };
325     bipBopFont.update(nullptr);
326
327     fontDescription.setSpecifiedSize(m_statsFontSize);
328     fontDescription.setComputedSize(m_statsFontSize);
329     FontCascade statsFont { WTFMove(fontDescription), 0, 0 };
330     statsFont.update(nullptr);
331
332     IntSize size = this->size();
333     FloatPoint timeLocation(size.width() * .05, size.height() * .15);
334     context.setFillColor(Color::white);
335     context.setTextDrawingMode(TextModeFill);
336     String string = String::format("%02u:%02u:%02u.%03u", hours, minutes, seconds, milliseconds % 1000);
337     context.drawText(timeFont, TextRun((StringView(string))), timeLocation);
338
339     string = String::format("%06u", m_frameNumber++);
340     timeLocation.move(0, m_baseFontSize);
341     context.drawText(timeFont, TextRun((StringView(string))), timeLocation);
342
343     FloatPoint statsLocation(size.width() * .45, size.height() * .75);
344     string = String::format("Requested frame rate: %.1f fps", frameRate());
345     context.drawText(statsFont, TextRun((StringView(string))), statsLocation);
346
347     statsLocation.move(0, m_statsFontSize);
348     string = String::format("Observed frame rate: %.1f fps", observedFrameRate());
349     context.drawText(statsFont, TextRun((StringView(string))), statsLocation);
350
351     statsLocation.move(0, m_statsFontSize);
352     string = String::format("Size: %u x %u", size.width(), size.height());
353     context.drawText(statsFont, TextRun((StringView(string))), statsLocation);
354
355     if (mockCamera()) {
356         const char* camera;
357         switch (facingMode()) {
358         case RealtimeMediaSourceSettings::User:
359             camera = "User facing";
360             break;
361         case RealtimeMediaSourceSettings::Environment:
362             camera = "Environment facing";
363             break;
364         case RealtimeMediaSourceSettings::Left:
365             camera = "Left facing";
366             break;
367         case RealtimeMediaSourceSettings::Right:
368             camera = "Right facing";
369             break;
370         case RealtimeMediaSourceSettings::Unknown:
371             camera = "Unknown";
372             break;
373         }
374         string = String::format("Camera: %s", camera);
375         statsLocation.move(0, m_statsFontSize);
376         context.drawText(statsFont, TextRun((StringView(string))), statsLocation);
377     } else {
378         statsLocation.move(0, m_statsFontSize);
379         context.drawText(statsFont, TextRun { name() }, statsLocation);
380     }
381
382     FloatPoint bipBopLocation(size.width() * .6, size.height() * .6);
383     unsigned frameMod = m_frameNumber % 60;
384     if (frameMod <= 15) {
385         context.setFillColor(Color::cyan);
386         String bip("Bip"_s);
387         context.drawText(bipBopFont, TextRun(StringView(bip)), bipBopLocation);
388     } else if (frameMod > 30 && frameMod <= 45) {
389         context.setFillColor(Color::yellow);
390         String bop("Bop"_s);
391         context.drawText(bipBopFont, TextRun(StringView(bop)), bipBopLocation);
392     }
393 }
394
395 void MockRealtimeVideoSource::delaySamples(Seconds delta)
396 {
397     m_delayUntil = MonotonicTime::now() + delta;
398 }
399
400 void MockRealtimeVideoSource::generateFrame()
401 {
402     if (m_delayUntil) {
403         if (m_delayUntil < MonotonicTime::now())
404             return;
405         m_delayUntil = MonotonicTime();
406     }
407
408     ImageBuffer* buffer = imageBuffer();
409     if (!buffer)
410         return;
411
412     GraphicsContext& context = buffer->context();
413     GraphicsContextStateSaver stateSaver(context);
414
415     auto& size = this->size();
416     FloatRect frameRect(FloatPoint(), size);
417
418     context.fillRect(FloatRect(FloatPoint(), size), m_fillColor);
419
420     if (!muted()) {
421         drawText(context);
422         drawAnimation(context);
423         drawBoxes(context);
424     }
425
426     updateSampleBuffer();
427 }
428
429 ImageBuffer* MockRealtimeVideoSource::imageBuffer() const
430 {
431     if (m_imageBuffer)
432         return m_imageBuffer.get();
433
434     m_imageBuffer = ImageBuffer::create(size(), Unaccelerated);
435     if (!m_imageBuffer)
436         return nullptr;
437
438     m_imageBuffer->context().setImageInterpolationQuality(InterpolationDefault);
439     m_imageBuffer->context().setStrokeThickness(1);
440
441     return m_imageBuffer.get();
442 }
443
444 bool MockRealtimeVideoSource::mockDisplayType(CaptureDevice::DeviceType type) const
445 {
446     if (!WTF::holds_alternative<MockDisplayProperties>(m_device.properties))
447         return false;
448
449     return WTF::get<MockDisplayProperties>(m_device.properties).type == type;
450 }
451
452 } // namespace WebCore
453
454 #endif // ENABLE(MEDIA_STREAM)