2 * Copyright (C) 2015-2018 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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.
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.
32 #include "MockRealtimeVideoSource.h"
34 #if ENABLE(MEDIA_STREAM)
35 #include "CaptureDevice.h"
36 #include "GraphicsContext.h"
37 #include "ImageBuffer.h"
40 #include "MediaConstraints.h"
41 #include "MockRealtimeMediaSourceCenter.h"
42 #include "NotImplemented.h"
43 #include "PlatformLayer.h"
44 #include "RealtimeMediaSourceSettings.h"
47 #include <wtf/text/StringConcatenateNumbers.h>
51 #if !PLATFORM(MAC) && !PLATFORM(IOS_FAMILY) && !(USE(GSTREAMER) && USE(LIBWEBRTC))
52 CaptureSourceOrError MockRealtimeVideoSource::create(String&& deviceID, String&& name, String&& hashSalt, const MediaConstraints* constraints)
55 auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(deviceID);
61 auto source = adoptRef(*new MockRealtimeVideoSource(WTFMove(deviceID), WTFMove(name), WTFMove(hashSalt)));
62 if (constraints && source->applyConstraints(*constraints))
65 return CaptureSourceOrError(WTFMove(source));
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)
73 auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(persistentID());
77 m_dashWidths.reserveInitialCapacity(2);
78 m_dashWidths.uncheckedAppend(6);
79 m_dashWidths.uncheckedAppend(6);
82 auto& properties = WTF::get<MockDisplayProperties>(m_device.properties);
83 setIntrinsicSize(properties.defaultSize);
84 setSize(properties.defaultSize);
85 m_fillColor = properties.fillColor;
89 auto& properties = WTF::get<MockCameraProperties>(m_device.properties);
90 setFrameRate(properties.defaultFrameRate);
91 setFacingMode(properties.facingMode);
92 m_fillColor = properties.fillColor;
95 bool MockRealtimeVideoSource::supportsSizeAndFrameRate(Optional<int> width, Optional<int> height, Optional<double> rate)
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.
100 return RealtimeVideoSource::supportsSizeAndFrameRate(width, height, rate);
102 return RealtimeMediaSource::supportsSizeAndFrameRate(width, height, rate);
105 void MockRealtimeVideoSource::setSizeAndFrameRate(Optional<int> width, Optional<int> height, Optional<double> rate)
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.
110 RealtimeVideoSource::setSizeAndFrameRate(width, height, rate);
114 RealtimeMediaSource::setSizeAndFrameRate(width, height, rate);
117 void MockRealtimeVideoSource::generatePresets()
119 ASSERT(mockCamera());
120 setSupportedPresets(WTFMove(WTF::get<MockCameraProperties>(m_device.properties).presets));
123 const RealtimeMediaSourceCapabilities& MockRealtimeVideoSource::capabilities()
125 if (!m_capabilities) {
126 RealtimeMediaSourceCapabilities capabilities(settings().supportedConstraints());
129 capabilities.addFacingMode(WTF::get<MockCameraProperties>(m_device.properties).facingMode);
130 capabilities.setDeviceId(hashedId());
131 updateCapabilities(capabilities);
132 capabilities.setDeviceId(hashedId());
134 capabilities.setWidth(CapabilityValueOrRange(72, 2880));
135 capabilities.setHeight(CapabilityValueOrRange(45, 1800));
136 capabilities.setFrameRate(CapabilityValueOrRange(.01, 60.0));
139 m_capabilities = WTFMove(capabilities);
142 return m_capabilities.value();
145 const RealtimeMediaSourceSettings& MockRealtimeVideoSource::settings()
147 if (m_currentSettings)
148 return m_currentSettings.value();
150 RealtimeMediaSourceSettings settings;
152 settings.setFacingMode(facingMode());
153 settings.setDeviceId(hashedId());
155 settings.setDisplaySurface(mockScreen() ? RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor : RealtimeMediaSourceSettings::DisplaySurfaceType::Window);
156 settings.setLogicalSurface(false);
158 settings.setFrameRate(frameRate());
159 auto& size = this->size();
160 settings.setWidth(size.width());
161 settings.setHeight(size.height());
163 settings.setAspectRatio(aspectRatio());
165 RealtimeMediaSourceSupportedConstraints supportedConstraints;
166 supportedConstraints.setSupportsFrameRate(true);
167 supportedConstraints.setSupportsWidth(true);
168 supportedConstraints.setSupportsHeight(true);
169 supportedConstraints.setSupportsAspectRatio(true);
171 supportedConstraints.setSupportsDeviceId(true);
172 supportedConstraints.setSupportsFacingMode(true);
174 supportedConstraints.setSupportsDisplaySurface(true);
175 supportedConstraints.setSupportsLogicalSurface(true);
177 settings.setSupportedConstraints(supportedConstraints);
179 m_currentSettings = WTFMove(settings);
181 return m_currentSettings.value();
184 void MockRealtimeVideoSource::setFrameRateWithPreset(double, RefPtr<VideoPreset> preset)
186 m_preset = WTFMove(preset);
188 setIntrinsicSize(preset->size);
191 IntSize MockRealtimeVideoSource::captureSize() const
193 return m_preset ? m_preset->size : this->size();
196 void MockRealtimeVideoSource::settingsDidChange(OptionSet<RealtimeMediaSourceSettings::Flag> settings)
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;
207 void MockRealtimeVideoSource::startCaptureTimer()
209 m_emitFrameTimer.startRepeating(1_s / frameRate());
212 void MockRealtimeVideoSource::startProducingData()
214 prepareToProduceData();
216 m_startTime = MonotonicTime::now();
219 void MockRealtimeVideoSource::stopProducingData()
221 m_emitFrameTimer.stop();
222 m_elapsedTime += MonotonicTime::now() - m_startTime;
223 m_startTime = MonotonicTime::nan();
226 Seconds MockRealtimeVideoSource::elapsedTime()
228 if (std::isnan(m_startTime))
229 return m_elapsedTime;
231 return m_elapsedTime + (MonotonicTime::now() - m_startTime);
234 void MockRealtimeVideoSource::drawAnimation(GraphicsContext& context)
236 auto size = captureSize();
237 float radius = size.width() * .09;
238 FloatPoint location(size.width() * .8, size.height() * .3);
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);
248 float endAngle = piFloat * (((fmod(m_frameNumber, frameRate()) + 0.5) * (2.0 / frameRate())) + 1);
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);
258 void MockRealtimeVideoSource::drawBoxes(GraphicsContext& context)
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;
267 IntSize size = captureSize();
268 float boxSize = size.width() * .035;
269 float boxTop = size.height() * .6;
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);
280 context.setLineDash(DashArray(), 0);
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);
289 context.setStrokeThickness(1);
290 float boxLeft = boxSize;
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));
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));
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));
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));
313 boxTop += boxSize + 2;
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;
320 context.strokePath(m_path);
323 void MockRealtimeVideoSource::drawText(GraphicsContext& context)
325 unsigned milliseconds = lround(elapsedTime().milliseconds());
326 unsigned seconds = milliseconds / 1000 % 60;
327 unsigned minutes = seconds / 60 % 60;
328 unsigned hours = minutes / 60 % 60;
330 FontCascadeDescription fontDescription;
331 fontDescription.setOneFamily("Courier");
332 fontDescription.setWeight(FontSelectionValue(500));
334 fontDescription.setSpecifiedSize(m_baseFontSize);
335 fontDescription.setComputedSize(m_baseFontSize);
336 FontCascade timeFont { FontCascadeDescription { fontDescription }, 0, 0 };
337 timeFont.update(nullptr);
339 fontDescription.setSpecifiedSize(m_bipBopFontSize);
340 fontDescription.setComputedSize(m_bipBopFontSize);
341 FontCascade bipBopFont { FontCascadeDescription { fontDescription }, 0, 0 };
342 bipBopFont.update(nullptr);
344 fontDescription.setSpecifiedSize(m_statsFontSize);
345 fontDescription.setComputedSize(m_statsFontSize);
346 FontCascade statsFont { WTFMove(fontDescription), 0, 0 };
347 statsFont.update(nullptr);
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);
356 string = makeString(pad('0', 6, m_frameNumber++));
357 timeLocation.move(0, m_baseFontSize);
358 context.drawText(timeFont, TextRun((StringView(string))), timeLocation);
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);
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);
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);
374 statsLocation.move(0, m_statsFontSize);
375 string = makeString("Preset size: ", captureSize.width(), " x ", captureSize.height());
376 context.drawText(statsFont, TextRun((StringView(string))), statsLocation);
379 switch (facingMode()) {
380 case RealtimeMediaSourceSettings::User:
381 camera = "User facing";
383 case RealtimeMediaSourceSettings::Environment:
384 camera = "Environment facing";
386 case RealtimeMediaSourceSettings::Left:
387 camera = "Left facing";
389 case RealtimeMediaSourceSettings::Right:
390 camera = "Right facing";
392 case RealtimeMediaSourceSettings::Unknown:
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);
404 FloatPoint bipBopLocation(captureSize.width() * .6, captureSize.height() * .6);
405 unsigned frameMod = m_frameNumber % 60;
406 if (frameMod <= 15) {
407 context.setFillColor(Color::cyan);
409 context.drawText(bipBopFont, TextRun(StringView(bip)), bipBopLocation);
410 } else if (frameMod > 30 && frameMod <= 45) {
411 context.setFillColor(Color::yellow);
413 context.drawText(bipBopFont, TextRun(StringView(bop)), bipBopLocation);
417 void MockRealtimeVideoSource::delaySamples(Seconds delta)
419 m_delayUntil = MonotonicTime::now() + delta;
422 void MockRealtimeVideoSource::generateFrame()
425 if (m_delayUntil < MonotonicTime::now())
427 m_delayUntil = MonotonicTime();
430 ImageBuffer* buffer = imageBuffer();
434 GraphicsContext& context = buffer->context();
435 GraphicsContextStateSaver stateSaver(context);
437 auto size = captureSize();
438 FloatRect frameRect(FloatPoint(), size);
440 context.fillRect(FloatRect(FloatPoint(), size), m_fillColor);
444 drawAnimation(context);
448 updateSampleBuffer();
451 ImageBuffer* MockRealtimeVideoSource::imageBuffer() const
454 return m_imageBuffer.get();
456 m_imageBuffer = ImageBuffer::create(captureSize(), Unaccelerated);
460 m_imageBuffer->context().setImageInterpolationQuality(InterpolationDefault);
461 m_imageBuffer->context().setStrokeThickness(1);
463 return m_imageBuffer.get();
466 bool MockRealtimeVideoSource::mockDisplayType(CaptureDevice::DeviceType type) const
468 if (!WTF::holds_alternative<MockDisplayProperties>(m_device.properties))
471 return WTF::get<MockDisplayProperties>(m_device.properties).type == type;
474 } // namespace WebCore
476 #endif // ENABLE(MEDIA_STREAM)