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