e0148523edbfe471d436647d6792e2a95aa59585
[WebKit-https.git] / Source / WebCore / platform / mock / MockRealtimeAudioSource.cpp
1 /*
2  * Copyright (C) 2015 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 "NotImplemented.h"
39 #include "RealtimeMediaSourceSettings.h"
40 #include <wtf/UUID.h>
41
42 namespace WebCore {
43
44 class MockRealtimeAudioSourceFactory : public RealtimeMediaSource::AudioCaptureFactory {
45 public:
46     CaptureSourceOrError createAudioCaptureSource(const String& deviceID, const MediaConstraints* constraints) final {
47         for (auto& device : MockRealtimeMediaSource::audioDevices()) {
48             if (device.persistentId() == deviceID)
49                 return MockRealtimeAudioSource::create(device.label(), constraints);
50         }
51         return { };
52     }
53 };
54
55 #if !PLATFORM(MAC) && !PLATFORM(IOS)
56 CaptureSourceOrError MockRealtimeAudioSource::create(const String& name, const MediaConstraints* constraints)
57 {
58     auto source = adoptRef(*new MockRealtimeAudioSource(name));
59     if (constraints && source->applyConstraints(*constraints))
60         return { };
61
62     return CaptureSourceOrError(WTFMove(source));
63 }
64
65 RefPtr<MockRealtimeAudioSource> MockRealtimeAudioSource::createMuted(const String& name)
66 {
67     auto source = adoptRef(new MockRealtimeAudioSource(name));
68     source->m_muted = true;
69     return source;
70 }
71 #endif
72
73 RealtimeMediaSource::AudioCaptureFactory& MockRealtimeAudioSource::factory()
74 {
75     static NeverDestroyed<MockRealtimeAudioSourceFactory> factory;
76     return factory.get();
77 }
78
79 MockRealtimeAudioSource::MockRealtimeAudioSource(const String& name)
80     : MockRealtimeMediaSource(createCanonicalUUIDString(), RealtimeMediaSource::Type::Audio, name)
81     , m_timer(RunLoop::current(), this, &MockRealtimeAudioSource::tick)
82 {
83 }
84
85 void MockRealtimeAudioSource::updateSettings(RealtimeMediaSourceSettings& settings)
86 {
87     settings.setVolume(volume());
88     settings.setEchoCancellation(echoCancellation());
89     settings.setSampleRate(sampleRate());
90 }
91
92 void MockRealtimeAudioSource::initializeCapabilities(RealtimeMediaSourceCapabilities& capabilities)
93 {
94     capabilities.setVolume(CapabilityValueOrRange(0.0, 1.0));
95     capabilities.setEchoCancellation(RealtimeMediaSourceCapabilities::EchoCancellation::ReadWrite);
96     capabilities.setSampleRate(CapabilityValueOrRange(44100, 48000));
97 }
98
99 void MockRealtimeAudioSource::initializeSupportedConstraints(RealtimeMediaSourceSupportedConstraints& supportedConstraints)
100 {
101     supportedConstraints.setSupportsVolume(true);
102     supportedConstraints.setSupportsEchoCancellation(true);
103     supportedConstraints.setSupportsSampleRate(true);
104 }
105
106 void MockRealtimeAudioSource::startProducingData()
107 {
108     if (!sampleRate())
109         setSampleRate(!deviceIndex() ? 44100 : 48000);
110
111     MockRealtimeMediaSource::startProducingData();
112
113     m_startTime = monotonicallyIncreasingTime();
114     m_timer.startRepeating(renderInterval());
115 }
116
117 void MockRealtimeAudioSource::stopProducingData()
118 {
119     MockRealtimeMediaSource::stopProducingData();
120     m_timer.stop();
121     m_elapsedTime += monotonicallyIncreasingTime() - m_startTime;
122     m_startTime = NAN;
123 }
124
125 double MockRealtimeAudioSource::elapsedTime()
126 {
127     if (std::isnan(m_startTime))
128         return m_elapsedTime;
129
130     return m_elapsedTime + (monotonicallyIncreasingTime() - m_startTime);
131 }
132
133 void MockRealtimeAudioSource::tick()
134 {
135     if (std::isnan(m_lastRenderTime))
136         m_lastRenderTime = monotonicallyIncreasingTime();
137
138     double now = monotonicallyIncreasingTime();
139     double delta = now - m_lastRenderTime;
140     m_lastRenderTime = now;
141     render(delta);
142 }
143
144 } // namespace WebCore
145
146 #endif // ENABLE(MEDIA_STREAM)