Support arbitrary video resolution in getUserMedia API
[WebKit-https.git] / Source / WebCore / platform / mock / MockRealtimeAudioSource.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 "MockRealtimeAudioSource.h"
33
34 #if ENABLE(MEDIA_STREAM)
35 #include "CaptureDevice.h"
36 #include "Logging.h"
37 #include "MediaConstraints.h"
38 #include "MockRealtimeMediaSourceCenter.h"
39 #include "NotImplemented.h"
40 #include "RealtimeMediaSourceSettings.h"
41 #include <wtf/UUID.h>
42
43 namespace WebCore {
44
45 class MockRealtimeAudioSourceFactory : public RealtimeMediaSource::AudioCaptureFactory
46 {
47 public:
48     CaptureSourceOrError createAudioCaptureSource(const CaptureDevice& device, const MediaConstraints* constraints) final
49     {
50         for (auto& mockDevice : MockRealtimeMediaSourceCenter::audioDevices()) {
51             if (mockDevice.persistentId() == device.persistentId())
52                 return MockRealtimeAudioSource::create(mockDevice.persistentId(), mockDevice.label(), constraints);
53         }
54         return { };
55     }
56 };
57
58 #if !PLATFORM(MAC) && !PLATFORM(IOS) && !(USE(GSTREAMER) && USE(LIBWEBRTC))
59 CaptureSourceOrError MockRealtimeAudioSource::create(const String& deviceID, const String& name, const MediaConstraints* constraints)
60 {
61     auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(deviceID);
62     ASSERT(device);
63     if (!device)
64         return { };
65
66     auto source = adoptRef(*new MockRealtimeAudioSource(WTFMove(device), deviceID, name));
67     if (constraints && source->applyConstraints(*constraints))
68         return { };
69
70     return CaptureSourceOrError(WTFMove(source));
71 }
72 #endif
73
74 static MockRealtimeAudioSourceFactory& mockAudioCaptureSourceFactory()
75 {
76     static NeverDestroyed<MockRealtimeAudioSourceFactory> factory;
77     return factory.get();
78 }
79
80 RealtimeMediaSource::AudioCaptureFactory& MockRealtimeAudioSource::factory()
81 {
82     return mockAudioCaptureSourceFactory();
83 }
84
85 MockRealtimeAudioSource::MockRealtimeAudioSource(const String& deviceID, const String& name)
86     : RealtimeMediaSource(deviceID, RealtimeMediaSource::Type::Audio, name)
87     , m_timer(RunLoop::current(), this, &MockRealtimeAudioSource::tick)
88 {
89     auto device = MockRealtimeMediaSourceCenter::mockDeviceWithPersistentID(deviceID);
90     ASSERT(device);
91     m_device = *device;
92 }
93
94 MockRealtimeAudioSource::~MockRealtimeAudioSource()
95 {
96 #if PLATFORM(IOS)
97     MockRealtimeMediaSourceCenter::audioCaptureSourceFactory().unsetActiveSource(*this);
98 #endif
99 }
100
101 const RealtimeMediaSourceSettings& MockRealtimeAudioSource::settings()
102 {
103     if (!m_currentSettings) {
104         RealtimeMediaSourceSettings settings;
105         settings.setDeviceId(id());
106         settings.setVolume(volume());
107         settings.setEchoCancellation(echoCancellation());
108         settings.setSampleRate(sampleRate());
109
110         RealtimeMediaSourceSupportedConstraints supportedConstraints;
111         supportedConstraints.setSupportsDeviceId(true);
112         supportedConstraints.setSupportsVolume(true);
113         supportedConstraints.setSupportsEchoCancellation(true);
114         supportedConstraints.setSupportsSampleRate(true);
115         settings.setSupportedConstraints(supportedConstraints);
116
117         m_currentSettings = WTFMove(settings);
118     }
119     return m_currentSettings.value();
120 }
121
122 const RealtimeMediaSourceCapabilities& MockRealtimeAudioSource::capabilities()
123 {
124     if (!m_capabilities) {
125         RealtimeMediaSourceCapabilities capabilities(settings().supportedConstraints());
126
127         capabilities.setDeviceId(id());
128         capabilities.setVolume(CapabilityValueOrRange(0.0, 1.0));
129         capabilities.setEchoCancellation(RealtimeMediaSourceCapabilities::EchoCancellation::ReadWrite);
130         capabilities.setSampleRate(CapabilityValueOrRange(44100, 48000));
131
132         m_capabilities = WTFMove(capabilities);
133     }
134     return m_capabilities.value();
135 }
136
137 void MockRealtimeAudioSource::settingsDidChange(OptionSet<RealtimeMediaSourceSettings::Flag> settings)
138 {
139     m_currentSettings = std::nullopt;
140     RealtimeMediaSource::settingsDidChange(settings);
141 }
142
143 void MockRealtimeAudioSource::startProducingData()
144 {
145 #if PLATFORM(IOS)
146     MockRealtimeMediaSourceCenter::audioCaptureSourceFactory().setActiveSource(*this);
147 #endif
148
149     if (!sampleRate())
150         setSampleRate(WTF::get<MockMicrophoneProperties>(m_device.properties).defaultSampleRate);
151
152     m_startTime = MonotonicTime::now();
153     m_timer.startRepeating(renderInterval());
154 }
155
156 void MockRealtimeAudioSource::stopProducingData()
157 {
158     m_timer.stop();
159     m_startTime = MonotonicTime::nan();
160 }
161
162 void MockRealtimeAudioSource::tick()
163 {
164     if (std::isnan(m_lastRenderTime))
165         m_lastRenderTime = MonotonicTime::now();
166
167     MonotonicTime now = MonotonicTime::now();
168
169     if (m_delayUntil) {
170         if (m_delayUntil < now)
171             return;
172         m_delayUntil = MonotonicTime();
173     }
174
175     Seconds delta = now - m_lastRenderTime;
176     m_lastRenderTime = now;
177     render(delta);
178 }
179
180 void MockRealtimeAudioSource::delaySamples(Seconds delta)
181 {
182     m_delayUntil = MonotonicTime::now() + delta;
183 }
184
185 } // namespace WebCore
186
187 #endif // ENABLE(MEDIA_STREAM)