Unreviewed, rolling out r234489.
[WebKit-https.git] / Source / WebCore / platform / mock / MockRealtimeVideoSource.cpp
1 /*
2  * Copyright (C) 2015-2017 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 class MockRealtimeVideoSourceFactory : public RealtimeMediaSource::VideoCaptureFactory
52 {
53 public:
54     CaptureSourceOrError createVideoCaptureSource(const CaptureDevice& device, const MediaConstraints* constraints) final
55     {
56         ASSERT(MockRealtimeMediaSource::captureDeviceWithPersistentID(device.type(), device.persistentId()));
57
58         switch (device.type()) {
59         case CaptureDevice::DeviceType::Camera:
60         case CaptureDevice::DeviceType::Screen:
61             return MockRealtimeVideoSource::create(device.persistentId(), device.label(), constraints);
62             break;
63         case CaptureDevice::DeviceType::Application:
64         case CaptureDevice::DeviceType::Window:
65         case CaptureDevice::DeviceType::Browser:
66         case CaptureDevice::DeviceType::Microphone:
67         case CaptureDevice::DeviceType::Unknown:
68             ASSERT_NOT_REACHED();
69             break;
70         }
71
72         return { };
73     }
74
75 #if PLATFORM(IOS)
76 private:
77     void setVideoCapturePageState(bool interrupted, bool pageMuted)
78     {
79         if (activeSource())
80             activeSource()->setInterrupted(interrupted, pageMuted);
81     }
82 #endif
83 };
84
85 #if !PLATFORM(MAC) && !PLATFORM(IOS) && !(USE(GSTREAMER) && USE(LIBWEBRTC))
86 CaptureSourceOrError MockRealtimeVideoSource::create(const String& deviceID, const String& name, const MediaConstraints* constraints)
87 {
88     auto source = adoptRef(*new MockRealtimeVideoSource(deviceID, name));
89     if (constraints && source->applyConstraints(*constraints))
90         return { };
91
92     return CaptureSourceOrError(WTFMove(source));
93 }
94 #endif
95
96 static MockRealtimeVideoSourceFactory& mockVideoCaptureSourceFactory()
97 {
98     static NeverDestroyed<MockRealtimeVideoSourceFactory> factory;
99     return factory.get();
100 }
101
102 RealtimeMediaSource::VideoCaptureFactory& MockRealtimeVideoSource::factory()
103 {
104     return mockVideoCaptureSourceFactory();
105 }
106
107 MockRealtimeVideoSource::MockRealtimeVideoSource(const String& deviceID, const String& name)
108     : MockRealtimeMediaSource(deviceID, RealtimeMediaSource::Type::Video, name)
109     , m_timer(RunLoop::current(), this, &MockRealtimeVideoSource::generateFrame)
110 {
111     m_dashWidths.reserveInitialCapacity(2);
112     m_dashWidths.uncheckedAppend(6);
113     m_dashWidths.uncheckedAppend(6);
114
115     if (mockScreen()) {
116         setFrameRate(WTF::get<MockDisplayProperties>(device().properties).defaultFrameRate);
117         return;
118     }
119
120     auto& properties = WTF::get<MockCameraProperties>(device().properties);
121     setFrameRate(properties.defaultFrameRate);
122     setFacingMode(properties.facingModeCapability);
123 }
124
125 MockRealtimeVideoSource::~MockRealtimeVideoSource()
126 {
127 #if PLATFORM(IOS)
128     MockRealtimeMediaSourceCenter::videoCaptureSourceFactory().unsetActiveSource(*this);
129 #endif
130 }
131
132 void MockRealtimeVideoSource::startProducingData()
133 {
134 #if PLATFORM(IOS)
135     MockRealtimeMediaSourceCenter::videoCaptureSourceFactory().setActiveSource(*this);
136 #endif
137
138     if (size().isEmpty()) {
139         setWidth(640);
140         setHeight(480);
141     }
142
143     m_startTime = MonotonicTime::now();
144     m_timer.startRepeating(1_ms * lround(1000 / frameRate()));
145 }
146
147 void MockRealtimeVideoSource::stopProducingData()
148 {
149     m_timer.stop();
150     m_elapsedTime += MonotonicTime::now() - m_startTime;
151     m_startTime = MonotonicTime::nan();
152 }
153
154 Seconds MockRealtimeVideoSource::elapsedTime()
155 {
156     if (std::isnan(m_startTime))
157         return m_elapsedTime;
158
159     return m_elapsedTime + (MonotonicTime::now() - m_startTime);
160 }
161
162 void MockRealtimeVideoSource::updateSettings(RealtimeMediaSourceSettings& settings)
163 {
164     if (mockCamera())
165         settings.setFacingMode(facingMode());
166     else {
167         settings.setDisplaySurface(RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor);
168         settings.setLogicalSurface(true);
169     }
170     settings.setFrameRate(frameRate());
171     IntSize size = this->size();
172     settings.setWidth(size.width());
173     settings.setHeight(size.height());
174     if (aspectRatio())
175         settings.setAspectRatio(aspectRatio());
176 }
177
178 void MockRealtimeVideoSource::initializeCapabilities(RealtimeMediaSourceCapabilities& capabilities)
179 {
180     if (mockCamera()) {
181         capabilities.addFacingMode(WTF::get<MockCameraProperties>(device().properties).facingModeCapability);
182
183         capabilities.setWidth(CapabilityValueOrRange(320, 1920));
184         capabilities.setHeight(CapabilityValueOrRange(240, 1080));
185         capabilities.setFrameRate(CapabilityValueOrRange(15.0, 60.0));
186         capabilities.setAspectRatio(CapabilityValueOrRange(4 / 3.0, 16 / 9.0));
187     } else {
188         capabilities.setWidth(CapabilityValueOrRange(72, 2880));
189         capabilities.setHeight(CapabilityValueOrRange(45, 1800));
190         capabilities.setFrameRate(CapabilityValueOrRange(.01, 60.0));
191     }
192 }
193
194 void MockRealtimeVideoSource::initializeSupportedConstraints(RealtimeMediaSourceSupportedConstraints& supportedConstraints)
195 {
196     supportedConstraints.setSupportsWidth(true);
197     supportedConstraints.setSupportsHeight(true);
198     supportedConstraints.setSupportsAspectRatio(true);
199     supportedConstraints.setSupportsFrameRate(true);
200     if (mockCamera())
201         supportedConstraints.setSupportsFacingMode(true);
202 }
203
204 bool MockRealtimeVideoSource::applyFrameRate(double rate)
205 {
206     if (m_timer.isActive())
207         m_timer.startRepeating(1_ms * lround(1000 / rate));
208
209     updateSampleBuffer();
210     return true;
211 }
212
213 bool MockRealtimeVideoSource::applySize(const IntSize& size)
214 {
215     m_baseFontSize = size.height() * .08;
216     m_bipBopFontSize = m_baseFontSize * 2.5;
217     m_statsFontSize = m_baseFontSize * .5;
218     m_imageBuffer = nullptr;
219     return true;
220 }
221
222 void MockRealtimeVideoSource::drawAnimation(GraphicsContext& context)
223 {
224     float radius = size().width() * .09;
225     FloatPoint location(size().width() * .8, size().height() * .3);
226
227     m_path.clear();
228     m_path.moveTo(location);
229     m_path.addArc(location, radius, 0, 2 * piFloat, false);
230     m_path.closeSubpath();
231     context.setFillColor(Color::white);
232     context.setFillRule(WindRule::NonZero);
233     context.fillPath(m_path);
234
235     float endAngle = piFloat * (((fmod(m_frameNumber, frameRate()) + 0.5) * (2.0 / frameRate())) + 1);
236     m_path.clear();
237     m_path.moveTo(location);
238     m_path.addArc(location, radius, 1.5 * piFloat, endAngle, false);
239     m_path.closeSubpath();
240     context.setFillColor(Color::gray);
241     context.setFillRule(WindRule::NonZero);
242     context.fillPath(m_path);
243 }
244
245 void MockRealtimeVideoSource::drawBoxes(GraphicsContext& context)
246 {
247     static const RGBA32 magenta = 0xffff00ff;
248     static const RGBA32 yellow = 0xffffff00;
249     static const RGBA32 blue = 0xff0000ff;
250     static const RGBA32 red = 0xffff0000;
251     static const RGBA32 green = 0xff008000;
252     static const RGBA32 cyan = 0xFF00FFFF;
253
254     IntSize size = this->size();
255     float boxSize = size.width() * .035;
256     float boxTop = size.height() * .6;
257
258     m_path.clear();
259     FloatRect frameRect(2, 2, size.width() - 3, size.height() - 3);
260     context.setStrokeColor(Color::white);
261     context.setStrokeThickness(3);
262     context.setLineDash(m_dashWidths, 0);
263     m_path.addRect(frameRect);
264     m_path.closeSubpath();
265     context.strokePath(m_path);
266
267     context.setLineDash(DashArray(), 0);
268     m_path.clear();
269     m_path.moveTo(FloatPoint(0, boxTop + boxSize));
270     m_path.addLineTo(FloatPoint(size.width(), boxTop + boxSize));
271     m_path.closeSubpath();
272     context.setStrokeColor(Color::white);
273     context.setStrokeThickness(2);
274     context.strokePath(m_path);
275
276     context.setStrokeThickness(1);
277     float boxLeft = boxSize;
278     m_path.clear();
279     for (unsigned i = 0; i < boxSize / 4; i++) {
280         m_path.moveTo(FloatPoint(boxLeft + 4 * i, boxTop));
281         m_path.addLineTo(FloatPoint(boxLeft + 4 * i, boxTop + boxSize));
282     }
283     boxLeft += boxSize + 2;
284     for (unsigned i = 0; i < boxSize / 4; i++) {
285         m_path.moveTo(FloatPoint(boxLeft, boxTop + 4 * i));
286         m_path.addLineTo(FloatPoint(boxLeft + boxSize - 1, boxTop + 4 * i));
287     }
288     context.setStrokeThickness(3);
289     boxLeft += boxSize + 2;
290     for (unsigned i = 0; i < boxSize / 8; i++) {
291         m_path.moveTo(FloatPoint(boxLeft + 8 * i, boxTop));
292         m_path.addLineTo(FloatPoint(boxLeft + 8 * i, boxTop + boxSize - 1));
293     }
294     boxLeft += boxSize + 2;
295     for (unsigned i = 0; i < boxSize / 8; i++) {
296         m_path.moveTo(FloatPoint(boxLeft, boxTop + 8 * i));
297         m_path.addLineTo(FloatPoint(boxLeft + boxSize - 1, boxTop + 8 * i));
298     }
299
300     boxTop += boxSize + 2;
301     boxLeft = boxSize;
302     Color boxColors[] = { Color::white, yellow, cyan, green, magenta, red, blue };
303     for (unsigned i = 0; i < sizeof(boxColors) / sizeof(boxColors[0]); i++) {
304         context.fillRect(FloatRect(boxLeft, boxTop, boxSize + 1, boxSize + 1), boxColors[i]);
305         boxLeft += boxSize + 1;
306     }
307     context.strokePath(m_path);
308 }
309
310 void MockRealtimeVideoSource::drawText(GraphicsContext& context)
311 {
312     unsigned milliseconds = lround(elapsedTime().milliseconds());
313     unsigned seconds = milliseconds / 1000 % 60;
314     unsigned minutes = seconds / 60 % 60;
315     unsigned hours = minutes / 60 % 60;
316
317     FontCascadeDescription fontDescription;
318     fontDescription.setOneFamily("Courier");
319     fontDescription.setWeight(FontSelectionValue(500));
320
321     fontDescription.setSpecifiedSize(m_baseFontSize);
322     fontDescription.setComputedSize(m_baseFontSize);
323     FontCascade timeFont { FontCascadeDescription { fontDescription }, 0, 0 };
324     timeFont.update(nullptr);
325
326     fontDescription.setSpecifiedSize(m_bipBopFontSize);
327     fontDescription.setComputedSize(m_bipBopFontSize);
328     FontCascade bipBopFont { FontCascadeDescription { fontDescription }, 0, 0 };
329     bipBopFont.update(nullptr);
330
331     fontDescription.setSpecifiedSize(m_statsFontSize);
332     fontDescription.setComputedSize(m_statsFontSize);
333     FontCascade statsFont { WTFMove(fontDescription), 0, 0 };
334     statsFont.update(nullptr);
335
336     IntSize size = this->size();
337     FloatPoint timeLocation(size.width() * .05, size.height() * .15);
338     context.setFillColor(Color::white);
339     context.setTextDrawingMode(TextModeFill);
340     String string = String::format("%02u:%02u:%02u.%03u", hours, minutes, seconds, milliseconds % 1000);
341     context.drawText(timeFont, TextRun((StringView(string))), timeLocation);
342
343     string = String::format("%06u", m_frameNumber++);
344     timeLocation.move(0, m_baseFontSize);
345     context.drawText(timeFont, TextRun((StringView(string))), timeLocation);
346
347     FloatPoint statsLocation(size.width() * .65, size.height() * .75);
348     string = String::format("Frame rate: %ffps", frameRate());
349     context.drawText(statsFont, TextRun((StringView(string))), statsLocation);
350
351     string = String::format("Size: %u x %u", size.width(), size.height());
352     statsLocation.move(0, m_statsFontSize);
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 { id() }, 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     auto fillColor = mockCamera() ? WTF::get<MockCameraProperties>(device().properties).fillColor : WTF::get<MockDisplayProperties>(device().properties).fillColor;
419     context.fillRect(FloatRect(FloatPoint(), size), fillColor);
420
421     if (!muted()) {
422         drawText(context);
423         drawAnimation(context);
424         drawBoxes(context);
425     }
426
427     updateSampleBuffer();
428 }
429
430 ImageBuffer* MockRealtimeVideoSource::imageBuffer() const
431 {
432     if (m_imageBuffer)
433         return m_imageBuffer.get();
434
435     m_imageBuffer = ImageBuffer::create(size(), Unaccelerated);
436     if (!m_imageBuffer)
437         return nullptr;
438
439     m_imageBuffer->context().setImageInterpolationQuality(InterpolationDefault);
440     m_imageBuffer->context().setStrokeThickness(1);
441
442     return m_imageBuffer.get();
443 }
444
445 } // namespace WebCore
446
447 #endif // ENABLE(MEDIA_STREAM)